xref: /freebsd-src/contrib/ntp/libntp/lib/isc/unix/interfaceiter.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2005, 2007, 2008  Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert  * Copyright (C) 1999-2003  Internet Software Consortium.
4*a466cc55SCy Schubert  *
5*a466cc55SCy Schubert  * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert  * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert  *
9*a466cc55SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert  * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert  */
17*a466cc55SCy Schubert 
18*a466cc55SCy Schubert /* $Id: interfaceiter.c,v 1.45 2008/12/01 03:51:47 marka Exp $ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /*! \file */
21*a466cc55SCy Schubert 
22*a466cc55SCy Schubert #include <config.h>
23*a466cc55SCy Schubert 
24*a466cc55SCy Schubert #include <sys/types.h>
25*a466cc55SCy Schubert #include <sys/ioctl.h>
26*a466cc55SCy Schubert #ifdef HAVE_SYS_SOCKIO_H
27*a466cc55SCy Schubert #include <sys/sockio.h>		/* Required for ifiter_ioctl.c. */
28*a466cc55SCy Schubert #endif
29*a466cc55SCy Schubert 
30*a466cc55SCy Schubert #include <stdio.h>
31*a466cc55SCy Schubert #include <stdlib.h>
32*a466cc55SCy Schubert #include <unistd.h>
33*a466cc55SCy Schubert #include <errno.h>
34*a466cc55SCy Schubert 
35*a466cc55SCy Schubert #include <isc/interfaceiter.h>
36*a466cc55SCy Schubert #include <isc/log.h>
37*a466cc55SCy Schubert #include <isc/magic.h>
38*a466cc55SCy Schubert #include <isc/mem.h>
39*a466cc55SCy Schubert #include <isc/msgs.h>
40*a466cc55SCy Schubert #include <isc/net.h>
41*a466cc55SCy Schubert #include <isc/print.h>
42*a466cc55SCy Schubert #include <isc/result.h>
43*a466cc55SCy Schubert #include <isc/strerror.h>
44*a466cc55SCy Schubert #include <isc/string.h>
45*a466cc55SCy Schubert #include <isc/types.h>
46*a466cc55SCy Schubert #include <isc/util.h>
47*a466cc55SCy Schubert 
48*a466cc55SCy Schubert /* Must follow <isc/net.h>. */
49*a466cc55SCy Schubert #ifdef HAVE_NET_IF6_H
50*a466cc55SCy Schubert #include <net/if6.h>
51*a466cc55SCy Schubert #endif
52*a466cc55SCy Schubert #include <net/if.h>
53*a466cc55SCy Schubert 
54*a466cc55SCy Schubert #ifdef HAVE_LINUX_IF_ADDR_H
55*a466cc55SCy Schubert # include <linux/if_addr.h>
56*a466cc55SCy Schubert #endif
57*a466cc55SCy Schubert 
58*a466cc55SCy Schubert /* Common utility functions */
59*a466cc55SCy Schubert 
60*a466cc55SCy Schubert /*%
61*a466cc55SCy Schubert  * Extract the network address part from a "struct sockaddr".
62*a466cc55SCy Schubert  * \brief
63*a466cc55SCy Schubert  * The address family is given explicitly
64*a466cc55SCy Schubert  * instead of using src->sa_family, because the latter does not work
65*a466cc55SCy Schubert  * for copying a network mask obtained by SIOCGIFNETMASK (it does
66*a466cc55SCy Schubert  * not have a valid address family).
67*a466cc55SCy Schubert  */
68*a466cc55SCy Schubert 
69*a466cc55SCy Schubert static void
get_addr(unsigned int family,isc_netaddr_t * dst,struct sockaddr * src,char * ifname)70*a466cc55SCy Schubert get_addr(unsigned int family, isc_netaddr_t *dst, struct sockaddr *src,
71*a466cc55SCy Schubert 	 char *ifname)
72*a466cc55SCy Schubert {
73*a466cc55SCy Schubert 	struct sockaddr_in6 *sa6;
74*a466cc55SCy Schubert 
75*a466cc55SCy Schubert #if !defined(ISC_PLATFORM_HAVEIFNAMETOINDEX) || \
76*a466cc55SCy Schubert     !defined(ISC_PLATFORM_HAVESCOPEID)
77*a466cc55SCy Schubert 	UNUSED(ifname);
78*a466cc55SCy Schubert #endif
79*a466cc55SCy Schubert 
80*a466cc55SCy Schubert 	/* clear any remaining value for safety */
81*a466cc55SCy Schubert 	memset(dst, 0, sizeof(*dst));
82*a466cc55SCy Schubert 
83*a466cc55SCy Schubert 	dst->family = family;
84*a466cc55SCy Schubert 	switch (family) {
85*a466cc55SCy Schubert 	case AF_INET:
86*a466cc55SCy Schubert 		memcpy(&dst->type.in,
87*a466cc55SCy Schubert 		       &((struct sockaddr_in *)(void *)src)->sin_addr,
88*a466cc55SCy Schubert 		       sizeof(struct in_addr));
89*a466cc55SCy Schubert 		break;
90*a466cc55SCy Schubert 	case AF_INET6:
91*a466cc55SCy Schubert 		sa6 = (struct sockaddr_in6 *)(void *)src;
92*a466cc55SCy Schubert 		memcpy(&dst->type.in6, &sa6->sin6_addr,
93*a466cc55SCy Schubert 		       sizeof(struct in6_addr));
94*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESCOPEID
95*a466cc55SCy Schubert 		if (sa6->sin6_scope_id != 0)
96*a466cc55SCy Schubert 			isc_netaddr_setzone(dst, sa6->sin6_scope_id);
97*a466cc55SCy Schubert 		else {
98*a466cc55SCy Schubert 			/*
99*a466cc55SCy Schubert 			 * BSD variants embed scope zone IDs in the 128bit
100*a466cc55SCy Schubert 			 * address as a kernel internal form.  Unfortunately,
101*a466cc55SCy Schubert 			 * the embedded IDs are not hidden from applications
102*a466cc55SCy Schubert 			 * when getting access to them by sysctl or ioctl.
103*a466cc55SCy Schubert 			 * We convert the internal format to the pure address
104*a466cc55SCy Schubert 			 * part and the zone ID part.
105*a466cc55SCy Schubert 			 * Since multicast addresses should not appear here
106*a466cc55SCy Schubert 			 * and they cannot be distinguished from netmasks,
107*a466cc55SCy Schubert 			 * we only consider unicast link-local addresses.
108*a466cc55SCy Schubert 			 */
109*a466cc55SCy Schubert 			if (IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr)) {
110*a466cc55SCy Schubert 				isc_uint16_t zone16;
111*a466cc55SCy Schubert 
112*a466cc55SCy Schubert 				memcpy(&zone16, &sa6->sin6_addr.s6_addr[2],
113*a466cc55SCy Schubert 				       sizeof(zone16));
114*a466cc55SCy Schubert 				zone16 = ntohs(zone16);
115*a466cc55SCy Schubert 				if (zone16 != 0) {
116*a466cc55SCy Schubert 					/* the zone ID is embedded */
117*a466cc55SCy Schubert 					isc_netaddr_setzone(dst,
118*a466cc55SCy Schubert 							    (isc_uint32_t)zone16);
119*a466cc55SCy Schubert 					dst->type.in6.s6_addr[2] = 0;
120*a466cc55SCy Schubert 					dst->type.in6.s6_addr[3] = 0;
121*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVEIFNAMETOINDEX
122*a466cc55SCy Schubert 				} else if (ifname != NULL) {
123*a466cc55SCy Schubert 					unsigned int zone;
124*a466cc55SCy Schubert 
125*a466cc55SCy Schubert 					/*
126*a466cc55SCy Schubert 					 * sin6_scope_id is still not provided,
127*a466cc55SCy Schubert 					 * but the corresponding interface name
128*a466cc55SCy Schubert 					 * is know.  Use the interface ID as
129*a466cc55SCy Schubert 					 * the link ID.
130*a466cc55SCy Schubert 					 */
131*a466cc55SCy Schubert 					zone = if_nametoindex(ifname);
132*a466cc55SCy Schubert 					if (zone != 0) {
133*a466cc55SCy Schubert 						isc_netaddr_setzone(dst,
134*a466cc55SCy Schubert 								    (isc_uint32_t)zone);
135*a466cc55SCy Schubert 					}
136*a466cc55SCy Schubert #endif
137*a466cc55SCy Schubert 				}
138*a466cc55SCy Schubert 			}
139*a466cc55SCy Schubert 		}
140*a466cc55SCy Schubert #endif
141*a466cc55SCy Schubert 		break;
142*a466cc55SCy Schubert 	default:
143*a466cc55SCy Schubert 		INSIST(0);
144*a466cc55SCy Schubert 		break;
145*a466cc55SCy Schubert 	}
146*a466cc55SCy Schubert }
147*a466cc55SCy Schubert 
148*a466cc55SCy Schubert /*
149*a466cc55SCy Schubert  * Include system-dependent code.
150*a466cc55SCy Schubert  */
151*a466cc55SCy Schubert 
152*a466cc55SCy Schubert #ifdef __linux
153*a466cc55SCy Schubert #define ISC_IF_INET6_SZ \
154*a466cc55SCy Schubert     sizeof("00000000000000000000000000000001 01 80 10 80 XXXXXXloXXXXXXXX\n")
155*a466cc55SCy Schubert static isc_result_t linux_if_inet6_next(isc_interfaceiter_t *);
156*a466cc55SCy Schubert static isc_result_t linux_if_inet6_current(isc_interfaceiter_t *);
157*a466cc55SCy Schubert static void linux_if_inet6_first(isc_interfaceiter_t *iter);
158*a466cc55SCy Schubert #endif
159*a466cc55SCy Schubert 
160*a466cc55SCy Schubert #if HAVE_GETIFADDRS
161*a466cc55SCy Schubert #include "ifiter_getifaddrs.c"
162*a466cc55SCy Schubert #elif HAVE_IFLIST_SYSCTL
163*a466cc55SCy Schubert #include "ifiter_sysctl.c"
164*a466cc55SCy Schubert #else
165*a466cc55SCy Schubert #include "ifiter_ioctl.c"
166*a466cc55SCy Schubert #endif
167*a466cc55SCy Schubert 
168*a466cc55SCy Schubert #ifdef __linux
169*a466cc55SCy Schubert static void
linux_if_inet6_first(isc_interfaceiter_t * iter)170*a466cc55SCy Schubert linux_if_inet6_first(isc_interfaceiter_t *iter) {
171*a466cc55SCy Schubert 	if (iter->proc != NULL) {
172*a466cc55SCy Schubert 		rewind(iter->proc);
173*a466cc55SCy Schubert 		(void)linux_if_inet6_next(iter);
174*a466cc55SCy Schubert 	} else
175*a466cc55SCy Schubert 		iter->valid = ISC_R_NOMORE;
176*a466cc55SCy Schubert }
177*a466cc55SCy Schubert 
178*a466cc55SCy Schubert static isc_result_t
linux_if_inet6_next(isc_interfaceiter_t * iter)179*a466cc55SCy Schubert linux_if_inet6_next(isc_interfaceiter_t *iter) {
180*a466cc55SCy Schubert 	if (iter->proc != NULL &&
181*a466cc55SCy Schubert 	    fgets(iter->entry, sizeof(iter->entry), iter->proc) != NULL)
182*a466cc55SCy Schubert 		iter->valid = ISC_R_SUCCESS;
183*a466cc55SCy Schubert 	else
184*a466cc55SCy Schubert 		iter->valid = ISC_R_NOMORE;
185*a466cc55SCy Schubert 	return (iter->valid);
186*a466cc55SCy Schubert }
187*a466cc55SCy Schubert 
188*a466cc55SCy Schubert static isc_result_t
linux_if_inet6_current(isc_interfaceiter_t * iter)189*a466cc55SCy Schubert linux_if_inet6_current(isc_interfaceiter_t *iter) {
190*a466cc55SCy Schubert 	char address[33];
191*a466cc55SCy Schubert 	char name[IF_NAMESIZE+1];
192*a466cc55SCy Schubert 	struct in6_addr addr6;
193*a466cc55SCy Schubert 	unsigned int ifindex;
194*a466cc55SCy Schubert 	int prefix, scope, flags;
195*a466cc55SCy Schubert 	int res;
196*a466cc55SCy Schubert 	unsigned int i;
197*a466cc55SCy Schubert 
198*a466cc55SCy Schubert 	if (iter->valid != ISC_R_SUCCESS)
199*a466cc55SCy Schubert 		return (iter->valid);
200*a466cc55SCy Schubert 	if (iter->proc == NULL) {
201*a466cc55SCy Schubert 		isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
202*a466cc55SCy Schubert 			      ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
203*a466cc55SCy Schubert 			      "/proc/net/if_inet6:iter->proc == NULL");
204*a466cc55SCy Schubert 		return (ISC_R_FAILURE);
205*a466cc55SCy Schubert 	}
206*a466cc55SCy Schubert 
207*a466cc55SCy Schubert 	res = sscanf(iter->entry, "%32[a-f0-9] %x %x %x %x %16s\n",
208*a466cc55SCy Schubert 		     address, &ifindex, &prefix, &scope, &flags, name);
209*a466cc55SCy Schubert 	if (res != 6) {
210*a466cc55SCy Schubert 		isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
211*a466cc55SCy Schubert 			      ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
212*a466cc55SCy Schubert 			      "/proc/net/if_inet6:sscanf() -> %d (expected 6)",
213*a466cc55SCy Schubert 			      res);
214*a466cc55SCy Schubert 		return (ISC_R_FAILURE);
215*a466cc55SCy Schubert 	}
216*a466cc55SCy Schubert 	if (strlen(address) != 32) {
217*a466cc55SCy Schubert 		isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
218*a466cc55SCy Schubert 			      ISC_LOGMODULE_INTERFACE, ISC_LOG_ERROR,
219*a466cc55SCy Schubert 			      "/proc/net/if_inet6:strlen(%s) != 32", address);
220*a466cc55SCy Schubert 		return (ISC_R_FAILURE);
221*a466cc55SCy Schubert 	}
222*a466cc55SCy Schubert 	/*
223*a466cc55SCy Schubert 	** Ignore DAD addresses --
224*a466cc55SCy Schubert 	** we can't bind to them until they are resolved
225*a466cc55SCy Schubert 	*/
226*a466cc55SCy Schubert #ifdef IFA_F_TENTATIVE
227*a466cc55SCy Schubert 	if (flags & IFA_F_TENTATIVE)
228*a466cc55SCy Schubert 		return (ISC_R_IGNORE);
229*a466cc55SCy Schubert #endif
230*a466cc55SCy Schubert 
231*a466cc55SCy Schubert 	for (i = 0; i < 16; i++) {
232*a466cc55SCy Schubert 		unsigned char byte;
233*a466cc55SCy Schubert 		static const char hex[] = "0123456789abcdef";
234*a466cc55SCy Schubert 		byte = ((strchr(hex, address[i * 2]) - hex) << 4) |
235*a466cc55SCy Schubert 		       (strchr(hex, address[i * 2 + 1]) - hex);
236*a466cc55SCy Schubert 		addr6.s6_addr[i] = byte;
237*a466cc55SCy Schubert 	}
238*a466cc55SCy Schubert 	iter->current.af = AF_INET6;
239*a466cc55SCy Schubert 	iter->current.flags = INTERFACE_F_UP;
240*a466cc55SCy Schubert 	isc_netaddr_fromin6(&iter->current.address, &addr6);
241*a466cc55SCy Schubert 	iter->current.ifindex = ifindex;
242*a466cc55SCy Schubert 	if (isc_netaddr_islinklocal(&iter->current.address)) {
243*a466cc55SCy Schubert 		isc_netaddr_setzone(&iter->current.address,
244*a466cc55SCy Schubert 				    (isc_uint32_t)ifindex);
245*a466cc55SCy Schubert 	}
246*a466cc55SCy Schubert 	for (i = 0; i < 16; i++) {
247*a466cc55SCy Schubert 		if (prefix > 8) {
248*a466cc55SCy Schubert 			addr6.s6_addr[i] = 0xff;
249*a466cc55SCy Schubert 			prefix -= 8;
250*a466cc55SCy Schubert 		} else {
251*a466cc55SCy Schubert 			addr6.s6_addr[i] = (0xff << (8 - prefix)) & 0xff;
252*a466cc55SCy Schubert 			prefix = 0;
253*a466cc55SCy Schubert 		}
254*a466cc55SCy Schubert 	}
255*a466cc55SCy Schubert 	isc_netaddr_fromin6(&iter->current.netmask, &addr6);
256*a466cc55SCy Schubert 	strncpy(iter->current.name, name, sizeof(iter->current.name));
257*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
258*a466cc55SCy Schubert }
259*a466cc55SCy Schubert #endif
260*a466cc55SCy Schubert 
261*a466cc55SCy Schubert /*
262*a466cc55SCy Schubert  * The remaining code is common to the sysctl and ioctl case.
263*a466cc55SCy Schubert  */
264*a466cc55SCy Schubert 
265*a466cc55SCy Schubert isc_result_t
isc_interfaceiter_current(isc_interfaceiter_t * iter,isc_interface_t * ifdata)266*a466cc55SCy Schubert isc_interfaceiter_current(isc_interfaceiter_t *iter,
267*a466cc55SCy Schubert 			  isc_interface_t *ifdata)
268*a466cc55SCy Schubert {
269*a466cc55SCy Schubert 	REQUIRE(iter->result == ISC_R_SUCCESS);
270*a466cc55SCy Schubert 	memcpy(ifdata, &iter->current, sizeof(*ifdata));
271*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
272*a466cc55SCy Schubert }
273*a466cc55SCy Schubert 
274*a466cc55SCy Schubert isc_result_t
isc_interfaceiter_first(isc_interfaceiter_t * iter)275*a466cc55SCy Schubert isc_interfaceiter_first(isc_interfaceiter_t *iter) {
276*a466cc55SCy Schubert 	isc_result_t result;
277*a466cc55SCy Schubert 
278*a466cc55SCy Schubert 	REQUIRE(VALID_IFITER(iter));
279*a466cc55SCy Schubert 
280*a466cc55SCy Schubert 	internal_first(iter);
281*a466cc55SCy Schubert 	for (;;) {
282*a466cc55SCy Schubert 		result = internal_current(iter);
283*a466cc55SCy Schubert 		if (result != ISC_R_IGNORE)
284*a466cc55SCy Schubert 			break;
285*a466cc55SCy Schubert 		result = internal_next(iter);
286*a466cc55SCy Schubert 		if (result != ISC_R_SUCCESS)
287*a466cc55SCy Schubert 			break;
288*a466cc55SCy Schubert 	}
289*a466cc55SCy Schubert 	iter->result = result;
290*a466cc55SCy Schubert 	return (result);
291*a466cc55SCy Schubert }
292*a466cc55SCy Schubert 
293*a466cc55SCy Schubert isc_result_t
isc_interfaceiter_next(isc_interfaceiter_t * iter)294*a466cc55SCy Schubert isc_interfaceiter_next(isc_interfaceiter_t *iter) {
295*a466cc55SCy Schubert 	isc_result_t result;
296*a466cc55SCy Schubert 
297*a466cc55SCy Schubert 	REQUIRE(VALID_IFITER(iter));
298*a466cc55SCy Schubert 	REQUIRE(iter->result == ISC_R_SUCCESS);
299*a466cc55SCy Schubert 
300*a466cc55SCy Schubert 	for (;;) {
301*a466cc55SCy Schubert 		result = internal_next(iter);
302*a466cc55SCy Schubert 		if (result != ISC_R_SUCCESS)
303*a466cc55SCy Schubert 			break;
304*a466cc55SCy Schubert 		result = internal_current(iter);
305*a466cc55SCy Schubert 		if (result != ISC_R_IGNORE)
306*a466cc55SCy Schubert 			break;
307*a466cc55SCy Schubert 	}
308*a466cc55SCy Schubert 	iter->result = result;
309*a466cc55SCy Schubert 	return (result);
310*a466cc55SCy Schubert }
311*a466cc55SCy Schubert 
312*a466cc55SCy Schubert void
isc_interfaceiter_destroy(isc_interfaceiter_t ** iterp)313*a466cc55SCy Schubert isc_interfaceiter_destroy(isc_interfaceiter_t **iterp)
314*a466cc55SCy Schubert {
315*a466cc55SCy Schubert 	isc_interfaceiter_t *iter;
316*a466cc55SCy Schubert 	REQUIRE(iterp != NULL);
317*a466cc55SCy Schubert 	iter = *iterp;
318*a466cc55SCy Schubert 	REQUIRE(VALID_IFITER(iter));
319*a466cc55SCy Schubert 
320*a466cc55SCy Schubert 	internal_destroy(iter);
321*a466cc55SCy Schubert 	if (iter->buf != NULL)
322*a466cc55SCy Schubert 		isc_mem_put(iter->mctx, iter->buf, iter->bufsize);
323*a466cc55SCy Schubert 
324*a466cc55SCy Schubert 	iter->magic = 0;
325*a466cc55SCy Schubert 	isc_mem_put(iter->mctx, iter, sizeof(*iter));
326*a466cc55SCy Schubert 	*iterp = NULL;
327*a466cc55SCy Schubert }
328