xref: /onnv-gate/usr/src/lib/libinetutil/common/inetutil.c (revision 12805:288ae711509e)
18485SPeter.Memishian@Sun.COM /*
28485SPeter.Memishian@Sun.COM  * CDDL HEADER START
38485SPeter.Memishian@Sun.COM  *
48485SPeter.Memishian@Sun.COM  * The contents of this file are subject to the terms of the
58485SPeter.Memishian@Sun.COM  * Common Development and Distribution License (the "License").
68485SPeter.Memishian@Sun.COM  * You may not use this file except in compliance with the License.
78485SPeter.Memishian@Sun.COM  *
88485SPeter.Memishian@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
98485SPeter.Memishian@Sun.COM  * or http://www.opensolaris.org/os/licensing.
108485SPeter.Memishian@Sun.COM  * See the License for the specific language governing permissions
118485SPeter.Memishian@Sun.COM  * and limitations under the License.
128485SPeter.Memishian@Sun.COM  *
138485SPeter.Memishian@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
148485SPeter.Memishian@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
158485SPeter.Memishian@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
168485SPeter.Memishian@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
178485SPeter.Memishian@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
188485SPeter.Memishian@Sun.COM  *
198485SPeter.Memishian@Sun.COM  * CDDL HEADER END
208485SPeter.Memishian@Sun.COM  */
218485SPeter.Memishian@Sun.COM 
228485SPeter.Memishian@Sun.COM /*
23*12805SDarren.Reed@Oracle.COM  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
248485SPeter.Memishian@Sun.COM  */
258485SPeter.Memishian@Sun.COM 
268485SPeter.Memishian@Sun.COM #include <unistd.h>
278485SPeter.Memishian@Sun.COM #include <netinet/in.h>
288485SPeter.Memishian@Sun.COM #include <libinetutil.h>
2912016SGirish.Moodalbail@Sun.COM #include <inet/ip.h>
3012016SGirish.Moodalbail@Sun.COM #include <strings.h>
31*12805SDarren.Reed@Oracle.COM #include <stddef.h>
3212016SGirish.Moodalbail@Sun.COM #include <errno.h>
3312016SGirish.Moodalbail@Sun.COM #include <libsocket_priv.h>
348485SPeter.Memishian@Sun.COM 
358485SPeter.Memishian@Sun.COM /*
368485SPeter.Memishian@Sun.COM  * Internet utility functions.
378485SPeter.Memishian@Sun.COM  */
388485SPeter.Memishian@Sun.COM 
398485SPeter.Memishian@Sun.COM /*
408485SPeter.Memishian@Sun.COM  * Given a host-order address, calculate client's default net mask.
418485SPeter.Memishian@Sun.COM  * Consult netmasks database to see if net is further subnetted.
428485SPeter.Memishian@Sun.COM  * We'll only snag the first netmask that matches our criteria.
438485SPeter.Memishian@Sun.COM  * We return the resultant netmask in host order.
448485SPeter.Memishian@Sun.COM  */
458485SPeter.Memishian@Sun.COM void
get_netmask4(const struct in_addr * n_addrp,struct in_addr * s_addrp)468485SPeter.Memishian@Sun.COM get_netmask4(const struct in_addr *n_addrp, struct in_addr *s_addrp)
478485SPeter.Memishian@Sun.COM {
488485SPeter.Memishian@Sun.COM 	struct in_addr	hp, tp;
498485SPeter.Memishian@Sun.COM 
508485SPeter.Memishian@Sun.COM 	/*
518485SPeter.Memishian@Sun.COM 	 * First check if VLSM is in use.
528485SPeter.Memishian@Sun.COM 	 */
538485SPeter.Memishian@Sun.COM 	hp.s_addr = htonl(n_addrp->s_addr);
548485SPeter.Memishian@Sun.COM 	if (getnetmaskbyaddr(hp, &tp) == 0) {
558485SPeter.Memishian@Sun.COM 		s_addrp->s_addr = ntohl(tp.s_addr);
568485SPeter.Memishian@Sun.COM 		return;
578485SPeter.Memishian@Sun.COM 	}
588485SPeter.Memishian@Sun.COM 
598485SPeter.Memishian@Sun.COM 	/*
608485SPeter.Memishian@Sun.COM 	 * Fall back on standard classed networks.
618485SPeter.Memishian@Sun.COM 	 */
628485SPeter.Memishian@Sun.COM 	if (IN_CLASSA(n_addrp->s_addr))
638485SPeter.Memishian@Sun.COM 		s_addrp->s_addr = IN_CLASSA_NET;
648485SPeter.Memishian@Sun.COM 	else if (IN_CLASSB(n_addrp->s_addr))
658485SPeter.Memishian@Sun.COM 		s_addrp->s_addr = IN_CLASSB_NET;
668485SPeter.Memishian@Sun.COM 	else if (IN_CLASSC(n_addrp->s_addr))
678485SPeter.Memishian@Sun.COM 		s_addrp->s_addr = IN_CLASSC_NET;
688485SPeter.Memishian@Sun.COM 	else
698485SPeter.Memishian@Sun.COM 		s_addrp->s_addr = IN_CLASSE_NET;
708485SPeter.Memishian@Sun.COM }
718485SPeter.Memishian@Sun.COM 
728485SPeter.Memishian@Sun.COM /*
738485SPeter.Memishian@Sun.COM  * Checks if the IP addresses `ssp1' and `ssp2' are equal.
748485SPeter.Memishian@Sun.COM  */
758485SPeter.Memishian@Sun.COM boolean_t
sockaddrcmp(const struct sockaddr_storage * ssp1,const struct sockaddr_storage * ssp2)768485SPeter.Memishian@Sun.COM sockaddrcmp(const struct sockaddr_storage *ssp1,
778485SPeter.Memishian@Sun.COM     const struct sockaddr_storage *ssp2)
788485SPeter.Memishian@Sun.COM {
798485SPeter.Memishian@Sun.COM 	struct in_addr addr1, addr2;
808485SPeter.Memishian@Sun.COM 	const struct in6_addr *addr6p1, *addr6p2;
818485SPeter.Memishian@Sun.COM 
828485SPeter.Memishian@Sun.COM 	if (ssp1->ss_family != ssp2->ss_family)
838485SPeter.Memishian@Sun.COM 		return (B_FALSE);
848485SPeter.Memishian@Sun.COM 
858485SPeter.Memishian@Sun.COM 	if (ssp1 == ssp2)
868485SPeter.Memishian@Sun.COM 		return (B_TRUE);
878485SPeter.Memishian@Sun.COM 
888485SPeter.Memishian@Sun.COM 	switch (ssp1->ss_family) {
898485SPeter.Memishian@Sun.COM 	case AF_INET:
908485SPeter.Memishian@Sun.COM 		addr1 = ((const struct sockaddr_in *)ssp1)->sin_addr;
918485SPeter.Memishian@Sun.COM 		addr2 = ((const struct sockaddr_in *)ssp2)->sin_addr;
928485SPeter.Memishian@Sun.COM 		return (addr1.s_addr == addr2.s_addr);
938485SPeter.Memishian@Sun.COM 	case AF_INET6:
948485SPeter.Memishian@Sun.COM 		addr6p1 = &((const struct sockaddr_in6 *)ssp1)->sin6_addr;
958485SPeter.Memishian@Sun.COM 		addr6p2 = &((const struct sockaddr_in6 *)ssp2)->sin6_addr;
968485SPeter.Memishian@Sun.COM 		return (IN6_ARE_ADDR_EQUAL(addr6p1, addr6p2));
978485SPeter.Memishian@Sun.COM 	}
988485SPeter.Memishian@Sun.COM 	return (B_FALSE);
998485SPeter.Memishian@Sun.COM }
10012016SGirish.Moodalbail@Sun.COM 
10112016SGirish.Moodalbail@Sun.COM /*
10212016SGirish.Moodalbail@Sun.COM  * Stores the netmask in `mask' for the given prefixlen `plen' and also sets
103*12805SDarren.Reed@Oracle.COM  * `sa_family' in `mask'. Because this function does not require aligned
104*12805SDarren.Reed@Oracle.COM  * access to the data inside of the sockaddr_in/6 structures, the code can
105*12805SDarren.Reed@Oracle.COM  * use offsetof() to find the right place in the incoming structure. Why is
106*12805SDarren.Reed@Oracle.COM  * using that beneficial? Less issues with lint. When using a direct cast
107*12805SDarren.Reed@Oracle.COM  * of the struct sockaddr_storage structure to sockaddr_in6, a lint warning
108*12805SDarren.Reed@Oracle.COM  * is generated because the former is composed of 16bit & 8bit elements whilst
109*12805SDarren.Reed@Oracle.COM  * sockaddr_in6 has a 32bit alignment requirement.
11012016SGirish.Moodalbail@Sun.COM  */
11112016SGirish.Moodalbail@Sun.COM int
plen2mask(uint_t prefixlen,sa_family_t af,struct sockaddr * mask)112*12805SDarren.Reed@Oracle.COM plen2mask(uint_t prefixlen, sa_family_t af, struct sockaddr *mask)
11312016SGirish.Moodalbail@Sun.COM {
11412016SGirish.Moodalbail@Sun.COM 	uint8_t	*addr;
11512016SGirish.Moodalbail@Sun.COM 
11612016SGirish.Moodalbail@Sun.COM 	if (af == AF_INET) {
11712016SGirish.Moodalbail@Sun.COM 		if (prefixlen > IP_ABITS)
11812016SGirish.Moodalbail@Sun.COM 			return (EINVAL);
119*12805SDarren.Reed@Oracle.COM 		bzero(mask, sizeof (struct sockaddr_in));
120*12805SDarren.Reed@Oracle.COM 		addr = (uint8_t *)mask;
121*12805SDarren.Reed@Oracle.COM 		addr += offsetof(struct sockaddr_in, sin_addr);
12212016SGirish.Moodalbail@Sun.COM 	} else {
12312016SGirish.Moodalbail@Sun.COM 		if (prefixlen > IPV6_ABITS)
12412016SGirish.Moodalbail@Sun.COM 			return (EINVAL);
125*12805SDarren.Reed@Oracle.COM 		bzero(mask, sizeof (struct sockaddr_in6));
126*12805SDarren.Reed@Oracle.COM 		addr = (uint8_t *)mask;
127*12805SDarren.Reed@Oracle.COM 		addr += offsetof(struct sockaddr_in6, sin6_addr);
12812016SGirish.Moodalbail@Sun.COM 	}
129*12805SDarren.Reed@Oracle.COM 	mask->sa_family = af;
13012016SGirish.Moodalbail@Sun.COM 
13112016SGirish.Moodalbail@Sun.COM 	while (prefixlen > 0) {
13212016SGirish.Moodalbail@Sun.COM 		if (prefixlen >= 8) {
13312016SGirish.Moodalbail@Sun.COM 			*addr++ = 0xFF;
13412016SGirish.Moodalbail@Sun.COM 			prefixlen -= 8;
13512016SGirish.Moodalbail@Sun.COM 			continue;
13612016SGirish.Moodalbail@Sun.COM 		}
13712016SGirish.Moodalbail@Sun.COM 		*addr |= 1 << (8 - prefixlen);
13812016SGirish.Moodalbail@Sun.COM 		prefixlen--;
13912016SGirish.Moodalbail@Sun.COM 	}
14012016SGirish.Moodalbail@Sun.COM 	return (0);
14112016SGirish.Moodalbail@Sun.COM }
14212016SGirish.Moodalbail@Sun.COM 
14312016SGirish.Moodalbail@Sun.COM /*
14412016SGirish.Moodalbail@Sun.COM  * Convert a mask to a prefix length.
14512016SGirish.Moodalbail@Sun.COM  * Returns prefix length on success, -1 otherwise.
146*12805SDarren.Reed@Oracle.COM  * The comments (above) for plen2mask about the use of `mask' also apply
147*12805SDarren.Reed@Oracle.COM  * to this function and the choice to use offsetof here too.
14812016SGirish.Moodalbail@Sun.COM  */
14912016SGirish.Moodalbail@Sun.COM int
mask2plen(const struct sockaddr * mask)150*12805SDarren.Reed@Oracle.COM mask2plen(const struct sockaddr *mask)
15112016SGirish.Moodalbail@Sun.COM {
15212016SGirish.Moodalbail@Sun.COM 	int rc = 0;
15312016SGirish.Moodalbail@Sun.COM 	uint8_t last;
15412016SGirish.Moodalbail@Sun.COM 	uint8_t *addr;
15512016SGirish.Moodalbail@Sun.COM 	int limit;
15612016SGirish.Moodalbail@Sun.COM 
157*12805SDarren.Reed@Oracle.COM 	if (mask->sa_family == AF_INET) {
15812016SGirish.Moodalbail@Sun.COM 		limit = IP_ABITS;
159*12805SDarren.Reed@Oracle.COM 		addr = (uint8_t *)mask;
160*12805SDarren.Reed@Oracle.COM 		addr += offsetof(struct sockaddr_in, sin_addr);
16112016SGirish.Moodalbail@Sun.COM 	} else {
16212016SGirish.Moodalbail@Sun.COM 		limit = IPV6_ABITS;
163*12805SDarren.Reed@Oracle.COM 		addr = (uint8_t *)mask;
164*12805SDarren.Reed@Oracle.COM 		addr += offsetof(struct sockaddr_in6, sin6_addr);
16512016SGirish.Moodalbail@Sun.COM 	}
16612016SGirish.Moodalbail@Sun.COM 
16712016SGirish.Moodalbail@Sun.COM 	while (*addr == 0xff) {
16812016SGirish.Moodalbail@Sun.COM 		rc += 8;
16912016SGirish.Moodalbail@Sun.COM 		if (rc == limit)
17012016SGirish.Moodalbail@Sun.COM 			return (limit);
17112016SGirish.Moodalbail@Sun.COM 		addr++;
17212016SGirish.Moodalbail@Sun.COM 	}
17312016SGirish.Moodalbail@Sun.COM 
17412016SGirish.Moodalbail@Sun.COM 	last = *addr;
17512016SGirish.Moodalbail@Sun.COM 	while (last != 0) {
17612016SGirish.Moodalbail@Sun.COM 		rc++;
17712016SGirish.Moodalbail@Sun.COM 		last = (last << 1) & 0xff;
17812016SGirish.Moodalbail@Sun.COM 	}
17912016SGirish.Moodalbail@Sun.COM 
18012016SGirish.Moodalbail@Sun.COM 	return (rc);
18112016SGirish.Moodalbail@Sun.COM }
18212016SGirish.Moodalbail@Sun.COM 
18312016SGirish.Moodalbail@Sun.COM /*
18412016SGirish.Moodalbail@Sun.COM  * Returns B_TRUE if the address in `ss' is INADDR_ANY for IPv4 or
18512016SGirish.Moodalbail@Sun.COM  * :: for IPv6. Otherwise, returns B_FALSE.
18612016SGirish.Moodalbail@Sun.COM  */
18712016SGirish.Moodalbail@Sun.COM boolean_t
sockaddrunspec(const struct sockaddr * ss)188*12805SDarren.Reed@Oracle.COM sockaddrunspec(const struct sockaddr *ss)
18912016SGirish.Moodalbail@Sun.COM {
190*12805SDarren.Reed@Oracle.COM 	struct sockaddr_storage data;
19112016SGirish.Moodalbail@Sun.COM 
192*12805SDarren.Reed@Oracle.COM 	switch (ss->sa_family) {
19312016SGirish.Moodalbail@Sun.COM 	case AF_INET:
194*12805SDarren.Reed@Oracle.COM 		(void) memcpy(&data, ss, sizeof (struct sockaddr_in));
195*12805SDarren.Reed@Oracle.COM 		return (((struct sockaddr_in *)&data)->sin_addr.s_addr ==
19612016SGirish.Moodalbail@Sun.COM 		    INADDR_ANY);
19712016SGirish.Moodalbail@Sun.COM 	case AF_INET6:
198*12805SDarren.Reed@Oracle.COM 		(void) memcpy(&data, ss, sizeof (struct sockaddr_in6));
19912016SGirish.Moodalbail@Sun.COM 		return (IN6_IS_ADDR_UNSPECIFIED(
200*12805SDarren.Reed@Oracle.COM 		    &((struct sockaddr_in6 *)&data)->sin6_addr));
20112016SGirish.Moodalbail@Sun.COM 	}
20212016SGirish.Moodalbail@Sun.COM 
203*12805SDarren.Reed@Oracle.COM 	return (B_FALSE);
20412016SGirish.Moodalbail@Sun.COM }
205