xref: /freebsd-src/contrib/ntp/libntp/lib/isc/unix/ifiter_sysctl.c (revision a466cc55373fc3cf86837f09da729535b57e69a1)
1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * Copyright (C) 2004, 2005, 2007  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: ifiter_sysctl.c,v 1.25 2007/06/19 23:47:18 tbox Exp $ */
19*a466cc55SCy Schubert 
20*a466cc55SCy Schubert /*! \file
21*a466cc55SCy Schubert  * \brief
22*a466cc55SCy Schubert  * Obtain the list of network interfaces using sysctl.
23*a466cc55SCy Schubert  * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
24*a466cc55SCy Schubert  * and 19.16.
25*a466cc55SCy Schubert  */
26*a466cc55SCy Schubert 
27*a466cc55SCy Schubert #include <sys/param.h>
28*a466cc55SCy Schubert #include <sys/sysctl.h>
29*a466cc55SCy Schubert 
30*a466cc55SCy Schubert #include <net/route.h>
31*a466cc55SCy Schubert #include <net/if_dl.h>
32*a466cc55SCy Schubert 
33*a466cc55SCy Schubert /* XXX what about Alpha? */
34*a466cc55SCy Schubert #ifdef sgi
35*a466cc55SCy Schubert #define ROUNDUP(a) ((a) > 0 ? \
36*a466cc55SCy Schubert 		(1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
37*a466cc55SCy Schubert 		sizeof(__uint64_t))
38*a466cc55SCy Schubert #else
39*a466cc55SCy Schubert #define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
40*a466cc55SCy Schubert                     : sizeof(long))
41*a466cc55SCy Schubert #endif
42*a466cc55SCy Schubert 
43*a466cc55SCy Schubert #define IFITER_MAGIC		ISC_MAGIC('I', 'F', 'I', 'S')
44*a466cc55SCy Schubert #define VALID_IFITER(t)		ISC_MAGIC_VALID(t, IFITER_MAGIC)
45*a466cc55SCy Schubert 
46*a466cc55SCy Schubert struct isc_interfaceiter {
47*a466cc55SCy Schubert 	unsigned int		magic;		/* Magic number. */
48*a466cc55SCy Schubert 	isc_mem_t		*mctx;
49*a466cc55SCy Schubert 	void			*buf;		/* Buffer for sysctl data. */
50*a466cc55SCy Schubert 	unsigned int		bufsize;	/* Bytes allocated. */
51*a466cc55SCy Schubert 	unsigned int		bufused;	/* Bytes used. */
52*a466cc55SCy Schubert 	unsigned int		pos;		/* Current offset in
53*a466cc55SCy Schubert 						   sysctl data. */
54*a466cc55SCy Schubert 	isc_interface_t		current;	/* Current interface data. */
55*a466cc55SCy Schubert 	isc_result_t		result;		/* Last result code. */
56*a466cc55SCy Schubert };
57*a466cc55SCy Schubert 
58*a466cc55SCy Schubert static int mib[6] = {
59*a466cc55SCy Schubert 	CTL_NET,
60*a466cc55SCy Schubert 	PF_ROUTE,
61*a466cc55SCy Schubert         0,
62*a466cc55SCy Schubert 	0, 			/* Any address family. */
63*a466cc55SCy Schubert         NET_RT_IFLIST,
64*a466cc55SCy Schubert 	0 			/* Flags. */
65*a466cc55SCy Schubert };
66*a466cc55SCy Schubert 
67*a466cc55SCy Schubert isc_result_t
isc_interfaceiter_create(isc_mem_t * mctx,isc_interfaceiter_t ** iterp)68*a466cc55SCy Schubert isc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
69*a466cc55SCy Schubert 	isc_interfaceiter_t *iter;
70*a466cc55SCy Schubert 	isc_result_t result;
71*a466cc55SCy Schubert 	size_t bufsize;
72*a466cc55SCy Schubert 	size_t bufused;
73*a466cc55SCy Schubert 	char strbuf[ISC_STRERRORSIZE];
74*a466cc55SCy Schubert 
75*a466cc55SCy Schubert 	REQUIRE(mctx != NULL);
76*a466cc55SCy Schubert 	REQUIRE(iterp != NULL);
77*a466cc55SCy Schubert 	REQUIRE(*iterp == NULL);
78*a466cc55SCy Schubert 
79*a466cc55SCy Schubert 	iter = isc_mem_get(mctx, sizeof(*iter));
80*a466cc55SCy Schubert 	if (iter == NULL)
81*a466cc55SCy Schubert 		return (ISC_R_NOMEMORY);
82*a466cc55SCy Schubert 
83*a466cc55SCy Schubert 	iter->mctx = mctx;
84*a466cc55SCy Schubert 	iter->buf = 0;
85*a466cc55SCy Schubert 
86*a466cc55SCy Schubert 	/*
87*a466cc55SCy Schubert 	 * Determine the amount of memory needed.
88*a466cc55SCy Schubert 	 */
89*a466cc55SCy Schubert 	bufsize = 0;
90*a466cc55SCy Schubert 	if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
91*a466cc55SCy Schubert 		isc__strerror(errno, strbuf, sizeof(strbuf));
92*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__,
93*a466cc55SCy Schubert 				 isc_msgcat_get(isc_msgcat,
94*a466cc55SCy Schubert 						ISC_MSGSET_IFITERSYSCTL,
95*a466cc55SCy Schubert 						ISC_MSG_GETIFLISTSIZE,
96*a466cc55SCy Schubert 						"getting interface "
97*a466cc55SCy Schubert 						"list size: sysctl: %s"),
98*a466cc55SCy Schubert 				 strbuf);
99*a466cc55SCy Schubert 		result = ISC_R_UNEXPECTED;
100*a466cc55SCy Schubert 		goto failure;
101*a466cc55SCy Schubert 	}
102*a466cc55SCy Schubert 	iter->bufsize = bufsize;
103*a466cc55SCy Schubert 
104*a466cc55SCy Schubert 	iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
105*a466cc55SCy Schubert 	if (iter->buf == NULL) {
106*a466cc55SCy Schubert 		result = ISC_R_NOMEMORY;
107*a466cc55SCy Schubert 		goto failure;
108*a466cc55SCy Schubert 	}
109*a466cc55SCy Schubert 
110*a466cc55SCy Schubert 	bufused = bufsize;
111*a466cc55SCy Schubert 	if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
112*a466cc55SCy Schubert 		isc__strerror(errno, strbuf, sizeof(strbuf));
113*a466cc55SCy Schubert 		UNEXPECTED_ERROR(__FILE__, __LINE__,
114*a466cc55SCy Schubert 				 isc_msgcat_get(isc_msgcat,
115*a466cc55SCy Schubert 						ISC_MSGSET_IFITERSYSCTL,
116*a466cc55SCy Schubert 						ISC_MSG_GETIFLIST,
117*a466cc55SCy Schubert 						"getting interface list: "
118*a466cc55SCy Schubert 						"sysctl: %s"),
119*a466cc55SCy Schubert 				 strbuf);
120*a466cc55SCy Schubert 		result = ISC_R_UNEXPECTED;
121*a466cc55SCy Schubert 		goto failure;
122*a466cc55SCy Schubert 	}
123*a466cc55SCy Schubert 	iter->bufused = bufused;
124*a466cc55SCy Schubert 	INSIST(iter->bufused <= iter->bufsize);
125*a466cc55SCy Schubert 
126*a466cc55SCy Schubert 	/*
127*a466cc55SCy Schubert 	 * A newly created iterator has an undefined position
128*a466cc55SCy Schubert 	 * until isc_interfaceiter_first() is called.
129*a466cc55SCy Schubert 	 */
130*a466cc55SCy Schubert 	iter->pos = (unsigned int) -1;
131*a466cc55SCy Schubert 	iter->result = ISC_R_FAILURE;
132*a466cc55SCy Schubert 
133*a466cc55SCy Schubert 	iter->magic = IFITER_MAGIC;
134*a466cc55SCy Schubert 	*iterp = iter;
135*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
136*a466cc55SCy Schubert 
137*a466cc55SCy Schubert  failure:
138*a466cc55SCy Schubert 	if (iter->buf != NULL)
139*a466cc55SCy Schubert 		isc_mem_put(mctx, iter->buf, iter->bufsize);
140*a466cc55SCy Schubert 	isc_mem_put(mctx, iter, sizeof(*iter));
141*a466cc55SCy Schubert 	return (result);
142*a466cc55SCy Schubert }
143*a466cc55SCy Schubert 
144*a466cc55SCy Schubert /*
145*a466cc55SCy Schubert  * Get information about the current interface to iter->current.
146*a466cc55SCy Schubert  * If successful, return ISC_R_SUCCESS.
147*a466cc55SCy Schubert  * If the interface has an unsupported address family,
148*a466cc55SCy Schubert  * return ISC_R_IGNORE.  In case of other failure,
149*a466cc55SCy Schubert  * return ISC_R_UNEXPECTED.
150*a466cc55SCy Schubert  */
151*a466cc55SCy Schubert 
152*a466cc55SCy Schubert static isc_result_t
internal_current(isc_interfaceiter_t * iter)153*a466cc55SCy Schubert internal_current(isc_interfaceiter_t *iter) {
154*a466cc55SCy Schubert 	struct ifa_msghdr *ifam, *ifam_end;
155*a466cc55SCy Schubert 
156*a466cc55SCy Schubert 	REQUIRE(VALID_IFITER(iter));
157*a466cc55SCy Schubert 	REQUIRE (iter->pos < (unsigned int) iter->bufused);
158*a466cc55SCy Schubert 
159*a466cc55SCy Schubert 	ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
160*a466cc55SCy Schubert 	ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
161*a466cc55SCy Schubert 
162*a466cc55SCy Schubert 	// Skip wrong RTM version headers
163*a466cc55SCy Schubert 	if (ifam->ifam_version != RTM_VERSION)
164*a466cc55SCy Schubert 		return (ISC_R_IGNORE);
165*a466cc55SCy Schubert 
166*a466cc55SCy Schubert 	if (ifam->ifam_type == RTM_IFINFO) {
167*a466cc55SCy Schubert 		struct if_msghdr *ifm = (struct if_msghdr *) ifam;
168*a466cc55SCy Schubert 		struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
169*a466cc55SCy Schubert 		unsigned int namelen;
170*a466cc55SCy Schubert 
171*a466cc55SCy Schubert 		memset(&iter->current, 0, sizeof(iter->current));
172*a466cc55SCy Schubert 
173*a466cc55SCy Schubert 		iter->current.ifindex = sdl->sdl_index;
174*a466cc55SCy Schubert 		namelen = sdl->sdl_nlen;
175*a466cc55SCy Schubert 		if (namelen > sizeof(iter->current.name) - 1)
176*a466cc55SCy Schubert 			namelen = sizeof(iter->current.name) - 1;
177*a466cc55SCy Schubert 
178*a466cc55SCy Schubert 		memset(iter->current.name, 0, sizeof(iter->current.name));
179*a466cc55SCy Schubert 		memcpy(iter->current.name, sdl->sdl_data, namelen);
180*a466cc55SCy Schubert 
181*a466cc55SCy Schubert 		iter->current.flags = 0;
182*a466cc55SCy Schubert 
183*a466cc55SCy Schubert 		if ((ifam->ifam_flags & IFF_UP) != 0)
184*a466cc55SCy Schubert 			iter->current.flags |= INTERFACE_F_UP;
185*a466cc55SCy Schubert 
186*a466cc55SCy Schubert 		if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0)
187*a466cc55SCy Schubert 			iter->current.flags |= INTERFACE_F_POINTTOPOINT;
188*a466cc55SCy Schubert 
189*a466cc55SCy Schubert 		if ((ifam->ifam_flags & IFF_LOOPBACK) != 0)
190*a466cc55SCy Schubert 			iter->current.flags |= INTERFACE_F_LOOPBACK;
191*a466cc55SCy Schubert 
192*a466cc55SCy Schubert 		if ((ifam->ifam_flags & IFF_BROADCAST) != 0)
193*a466cc55SCy Schubert 			iter->current.flags |= INTERFACE_F_BROADCAST;
194*a466cc55SCy Schubert 
195*a466cc55SCy Schubert #ifdef IFF_MULTICAST
196*a466cc55SCy Schubert 		if ((ifam->ifam_flags & IFF_MULTICAST) != 0)
197*a466cc55SCy Schubert 			iter->current.flags |= INTERFACE_F_MULTICAST;
198*a466cc55SCy Schubert #endif
199*a466cc55SCy Schubert 
200*a466cc55SCy Schubert 		/*
201*a466cc55SCy Schubert 		 * This is not an interface address.
202*a466cc55SCy Schubert 		 * Force another iteration.
203*a466cc55SCy Schubert 		 */
204*a466cc55SCy Schubert 		return (ISC_R_IGNORE);
205*a466cc55SCy Schubert 	} else if (ifam->ifam_type == RTM_NEWADDR) {
206*a466cc55SCy Schubert 		int i;
207*a466cc55SCy Schubert 		int family;
208*a466cc55SCy Schubert 		struct sockaddr *mask_sa = NULL;
209*a466cc55SCy Schubert 		struct sockaddr *addr_sa = NULL;
210*a466cc55SCy Schubert 		struct sockaddr *dst_sa = NULL;
211*a466cc55SCy Schubert 
212*a466cc55SCy Schubert 		struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
213*a466cc55SCy Schubert 		family = sa->sa_family;
214*a466cc55SCy Schubert 
215*a466cc55SCy Schubert 		for (i = 0; i < RTAX_MAX; i++)
216*a466cc55SCy Schubert 		{
217*a466cc55SCy Schubert 			if ((ifam->ifam_addrs & (1 << i)) == 0)
218*a466cc55SCy Schubert 				continue;
219*a466cc55SCy Schubert 
220*a466cc55SCy Schubert 			INSIST(sa < (struct sockaddr *) ifam_end);
221*a466cc55SCy Schubert 
222*a466cc55SCy Schubert 			switch (i) {
223*a466cc55SCy Schubert 			case RTAX_NETMASK: /* Netmask */
224*a466cc55SCy Schubert 				mask_sa = sa;
225*a466cc55SCy Schubert 				break;
226*a466cc55SCy Schubert 			case RTAX_IFA: /* Interface address */
227*a466cc55SCy Schubert 				addr_sa = sa;
228*a466cc55SCy Schubert 				break;
229*a466cc55SCy Schubert 			case RTAX_BRD: /* Broadcast or destination address */
230*a466cc55SCy Schubert 				dst_sa = sa;
231*a466cc55SCy Schubert 				break;
232*a466cc55SCy Schubert 			}
233*a466cc55SCy Schubert #ifdef ISC_PLATFORM_HAVESALEN
234*a466cc55SCy Schubert 			sa = (struct sockaddr *)((char*)(sa)
235*a466cc55SCy Schubert 					 + ROUNDUP(sa->sa_len));
236*a466cc55SCy Schubert #else
237*a466cc55SCy Schubert #ifdef sgi
238*a466cc55SCy Schubert 			/*
239*a466cc55SCy Schubert 			 * Do as the contributed SGI code does.
240*a466cc55SCy Schubert 			 */
241*a466cc55SCy Schubert 			sa = (struct sockaddr *)((char*)(sa)
242*a466cc55SCy Schubert 					 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
243*a466cc55SCy Schubert #else
244*a466cc55SCy Schubert 			/* XXX untested. */
245*a466cc55SCy Schubert 			sa = (struct sockaddr *)((char*)(sa)
246*a466cc55SCy Schubert 					 + ROUNDUP(sizeof(struct sockaddr)));
247*a466cc55SCy Schubert #endif
248*a466cc55SCy Schubert #endif
249*a466cc55SCy Schubert 		}
250*a466cc55SCy Schubert 
251*a466cc55SCy Schubert 		if (addr_sa == NULL)
252*a466cc55SCy Schubert 			return (ISC_R_IGNORE);
253*a466cc55SCy Schubert 
254*a466cc55SCy Schubert 		family = addr_sa->sa_family;
255*a466cc55SCy Schubert 		if (family != AF_INET && family != AF_INET6)
256*a466cc55SCy Schubert 			return (ISC_R_IGNORE);
257*a466cc55SCy Schubert 
258*a466cc55SCy Schubert 		iter->current.af = family;
259*a466cc55SCy Schubert 
260*a466cc55SCy Schubert 		get_addr(family, &iter->current.address, addr_sa,
261*a466cc55SCy Schubert 			 iter->current.name);
262*a466cc55SCy Schubert 
263*a466cc55SCy Schubert 		if (mask_sa != NULL)
264*a466cc55SCy Schubert 			get_addr(family, &iter->current.netmask, mask_sa,
265*a466cc55SCy Schubert 				 iter->current.name);
266*a466cc55SCy Schubert 
267*a466cc55SCy Schubert 		if (dst_sa != NULL &&
268*a466cc55SCy Schubert 		    (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
269*a466cc55SCy Schubert 			get_addr(family, &iter->current.dstaddress, dst_sa,
270*a466cc55SCy Schubert 				 iter->current.name);
271*a466cc55SCy Schubert 
272*a466cc55SCy Schubert 		if (dst_sa != NULL &&
273*a466cc55SCy Schubert 		    (iter->current.flags & INTERFACE_F_BROADCAST) != 0)
274*a466cc55SCy Schubert 			get_addr(family, &iter->current.broadcast, dst_sa,
275*a466cc55SCy Schubert 				 iter->current.name);
276*a466cc55SCy Schubert 
277*a466cc55SCy Schubert 		return (ISC_R_SUCCESS);
278*a466cc55SCy Schubert 	} else {
279*a466cc55SCy Schubert 		printf("%s", isc_msgcat_get(isc_msgcat,
280*a466cc55SCy Schubert 					    ISC_MSGSET_IFITERSYSCTL,
281*a466cc55SCy Schubert 					    ISC_MSG_UNEXPECTEDTYPE,
282*a466cc55SCy Schubert 					    "warning: unexpected interface "
283*a466cc55SCy Schubert 					    "list message type\n"));
284*a466cc55SCy Schubert 		return (ISC_R_IGNORE);
285*a466cc55SCy Schubert 	}
286*a466cc55SCy Schubert }
287*a466cc55SCy Schubert 
288*a466cc55SCy Schubert /*
289*a466cc55SCy Schubert  * Step the iterator to the next interface.  Unlike
290*a466cc55SCy Schubert  * isc_interfaceiter_next(), this may leave the iterator
291*a466cc55SCy Schubert  * positioned on an interface that will ultimately
292*a466cc55SCy Schubert  * be ignored.  Return ISC_R_NOMORE if there are no more
293*a466cc55SCy Schubert  * interfaces, otherwise ISC_R_SUCCESS.
294*a466cc55SCy Schubert  */
295*a466cc55SCy Schubert static isc_result_t
internal_next(isc_interfaceiter_t * iter)296*a466cc55SCy Schubert internal_next(isc_interfaceiter_t *iter) {
297*a466cc55SCy Schubert 	struct ifa_msghdr *ifam;
298*a466cc55SCy Schubert 	REQUIRE (iter->pos < (unsigned int) iter->bufused);
299*a466cc55SCy Schubert 
300*a466cc55SCy Schubert 	ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
301*a466cc55SCy Schubert 
302*a466cc55SCy Schubert 	iter->pos += ifam->ifam_msglen;
303*a466cc55SCy Schubert 
304*a466cc55SCy Schubert 	if (iter->pos >= iter->bufused)
305*a466cc55SCy Schubert 		return (ISC_R_NOMORE);
306*a466cc55SCy Schubert 
307*a466cc55SCy Schubert 	return (ISC_R_SUCCESS);
308*a466cc55SCy Schubert }
309*a466cc55SCy Schubert 
310*a466cc55SCy Schubert static void
internal_destroy(isc_interfaceiter_t * iter)311*a466cc55SCy Schubert internal_destroy(isc_interfaceiter_t *iter) {
312*a466cc55SCy Schubert 	UNUSED(iter); /* Unused. */
313*a466cc55SCy Schubert 	/*
314*a466cc55SCy Schubert 	 * Do nothing.
315*a466cc55SCy Schubert 	 */
316*a466cc55SCy Schubert }
317*a466cc55SCy Schubert 
318*a466cc55SCy Schubert static
internal_first(isc_interfaceiter_t * iter)319*a466cc55SCy Schubert void internal_first(isc_interfaceiter_t *iter) {
320*a466cc55SCy Schubert 	iter->pos = 0;
321*a466cc55SCy Schubert }
322