xref: /illumos-gate/usr/src/common/smbsrv/smb_inet.c (revision 2b1ff28b)
17f667e74Sjose borrego /*
27f667e74Sjose borrego  * CDDL HEADER START
37f667e74Sjose borrego  *
47f667e74Sjose borrego  * The contents of this file are subject to the terms of the
57f667e74Sjose borrego  * Common Development and Distribution License (the "License").
67f667e74Sjose borrego  * You may not use this file except in compliance with the License.
77f667e74Sjose borrego  *
87f667e74Sjose borrego  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97f667e74Sjose borrego  * or http://www.opensolaris.org/os/licensing.
107f667e74Sjose borrego  * See the License for the specific language governing permissions
117f667e74Sjose borrego  * and limitations under the License.
127f667e74Sjose borrego  *
137f667e74Sjose borrego  * When distributing Covered Code, include this CDDL HEADER in each
147f667e74Sjose borrego  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157f667e74Sjose borrego  * If applicable, add the following below this CDDL HEADER, with the
167f667e74Sjose borrego  * fields enclosed by brackets "[]" replaced with your own identifying
177f667e74Sjose borrego  * information: Portions Copyright [yyyy] [name of copyright owner]
187f667e74Sjose borrego  *
197f667e74Sjose borrego  * CDDL HEADER END
207f667e74Sjose borrego  */
210b905b49SYuri Pankov 
227f667e74Sjose borrego /*
237f667e74Sjose borrego  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247f667e74Sjose borrego  * Use is subject to license terms.
25*2b1ff28bSGordon Ross  *
26*2b1ff28bSGordon Ross  * Copyright 2019 Nexenta by DDN, Inc. All rights reserved.
277f667e74Sjose borrego  */
287f667e74Sjose borrego 
297f667e74Sjose borrego /*
30*2b1ff28bSGordon Ross  * Wrappers for inet address manipulation to do what SMB needs.
317f667e74Sjose borrego  */
327f667e74Sjose borrego 
337f667e74Sjose borrego #include <sys/types.h>
347f667e74Sjose borrego #include <sys/socket.h>
357f667e74Sjose borrego #include <netinet/in.h>
36b819cea2SGordon Ross 
37b819cea2SGordon Ross #if !defined(_KERNEL)
38b819cea2SGordon Ross #include <errno.h>
39b819cea2SGordon Ross #include <string.h>
40b819cea2SGordon Ross #include <strings.h>
41b819cea2SGordon Ross #include <arpa/inet.h>
42b819cea2SGordon Ross #else	/* !_KERNEL */
43b819cea2SGordon Ross #include <sys/errno.h>
44b819cea2SGordon Ross #include <sys/sunddi.h>
45b819cea2SGordon Ross /* Don't want the rest of what's in inet/ip.h */
46*2b1ff28bSGordon Ross #define	inet_ntop _inet_ntop	// illumos.org/issues/5980
47b819cea2SGordon Ross extern char	*inet_ntop(int, const void *, char *, int);
48b819cea2SGordon Ross extern int	inet_pton(int, char *, void *);
49b819cea2SGordon Ross #endif	/* !_KERNEL */
50b819cea2SGordon Ross 
517f667e74Sjose borrego #include <smbsrv/smb_inet.h>
527f667e74Sjose borrego 
537f667e74Sjose borrego const struct in6_addr ipv6addr_any = IN6ADDR_ANY_INIT;
547f667e74Sjose borrego 
557f667e74Sjose borrego boolean_t
smb_inet_equal(smb_inaddr_t * ip1,smb_inaddr_t * ip2)56fc724630SAlan Wright smb_inet_equal(smb_inaddr_t *ip1, smb_inaddr_t *ip2)
57fc724630SAlan Wright {
58fc724630SAlan Wright 	if ((ip1->a_family == AF_INET) &&
59fc724630SAlan Wright 	    (ip2->a_family == AF_INET) &&
60fc724630SAlan Wright 	    (ip1->a_ipv4 == ip2->a_ipv4))
61fc724630SAlan Wright 		return (B_TRUE);
62fc724630SAlan Wright 
63fc724630SAlan Wright 	if ((ip1->a_family == AF_INET6) &&
64fc724630SAlan Wright 	    (ip2->a_family == AF_INET6) &&
65b819cea2SGordon Ross 	    (!memcmp(&ip1->a_ipv6, &ip2->a_ipv6, sizeof (in6_addr_t))))
66fc724630SAlan Wright 		return (B_TRUE);
67fc724630SAlan Wright 	else
68fc724630SAlan Wright 		return (B_FALSE);
69fc724630SAlan Wright }
70fc724630SAlan Wright 
71fc724630SAlan Wright boolean_t
smb_inet_same_subnet(smb_inaddr_t * ip1,smb_inaddr_t * ip2,uint32_t v4mask)72fc724630SAlan Wright smb_inet_same_subnet(smb_inaddr_t *ip1, smb_inaddr_t *ip2, uint32_t v4mask)
737f667e74Sjose borrego {
747f667e74Sjose borrego 	if ((ip1->a_family == AF_INET) &&
757f667e74Sjose borrego 	    (ip2->a_family == AF_INET) &&
767f667e74Sjose borrego 	    ((ip1->a_ipv4 & v4mask) == (ip2->a_ipv4 & v4mask)))
777f667e74Sjose borrego 		return (B_TRUE);
787f667e74Sjose borrego 
797f667e74Sjose borrego 	if ((ip1->a_family == AF_INET6) &&
807f667e74Sjose borrego 	    (ip2->a_family == AF_INET6) &&
81b819cea2SGordon Ross 	    (!memcmp(&ip1->a_ipv6, &ip2->a_ipv6, sizeof (in6_addr_t))))
827f667e74Sjose borrego 		return (B_TRUE);
837f667e74Sjose borrego 	else
847f667e74Sjose borrego 		return (B_FALSE);
857f667e74Sjose borrego }
867f667e74Sjose borrego 
877f667e74Sjose borrego boolean_t
smb_inet_iszero(smb_inaddr_t * ipaddr)887f667e74Sjose borrego smb_inet_iszero(smb_inaddr_t *ipaddr)
897f667e74Sjose borrego {
907f667e74Sjose borrego 	const void *ipsz = (const void *)&ipv6addr_any;
917f667e74Sjose borrego 
927f667e74Sjose borrego 	if ((ipaddr->a_family == AF_INET) &&
937f667e74Sjose borrego 	    (ipaddr->a_ipv4 == 0))
947f667e74Sjose borrego 		return (B_TRUE);
957f667e74Sjose borrego 
967f667e74Sjose borrego 	if ((ipaddr->a_family == AF_INET6) &&
97b819cea2SGordon Ross 	    !memcmp(&ipaddr->a_ipv6, ipsz, sizeof (in6_addr_t)))
987f667e74Sjose borrego 		return (B_TRUE);
997f667e74Sjose borrego 	else
1007f667e74Sjose borrego 		return (B_FALSE);
1017f667e74Sjose borrego }
1027f667e74Sjose borrego 
1037f667e74Sjose borrego const char *
smb_inet_ntop(smb_inaddr_t * addr,char * buf,int size)1047f667e74Sjose borrego smb_inet_ntop(smb_inaddr_t *addr, char *buf, int size)
1057f667e74Sjose borrego {
1060b905b49SYuri Pankov 	/* Lint avoidance */
1070b905b49SYuri Pankov #ifdef	_KERNEL
108b819cea2SGordon Ross 	int sz = size;
1090b905b49SYuri Pankov #else
1100b905b49SYuri Pankov 	size_t sz = (size_t)size;
1110b905b49SYuri Pankov #endif
11249b5df1eSGordon Ross 
113b819cea2SGordon Ross 	return ((char *)inet_ntop(addr->a_family, addr, buf, sz));
1147f667e74Sjose borrego }