ifiter_sysctl.c revision 258945
1258945Sroberto/*
2258945Sroberto * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto * Copyright (C) 1999-2003  Internet Software Consortium.
4258945Sroberto *
5258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
6258945Sroberto * purpose with or without fee is hereby granted, provided that the above
7258945Sroberto * copyright notice and this permission notice appear in all copies.
8258945Sroberto *
9258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
16258945Sroberto */
17258945Sroberto
18258945Sroberto/* $Id: ifiter_sysctl.c,v 1.25 2007/06/19 23:47:18 tbox Exp $ */
19258945Sroberto
20258945Sroberto/*! \file
21258945Sroberto * \brief
22258945Sroberto * Obtain the list of network interfaces using sysctl.
23258945Sroberto * See TCP/IP Illustrated Volume 2, sections 19.8, 19.14,
24258945Sroberto * and 19.16.
25258945Sroberto */
26258945Sroberto
27258945Sroberto#include <sys/param.h>
28258945Sroberto#include <sys/sysctl.h>
29258945Sroberto
30258945Sroberto#include <net/route.h>
31258945Sroberto#include <net/if_dl.h>
32258945Sroberto
33258945Sroberto/* XXX what about Alpha? */
34258945Sroberto#ifdef sgi
35258945Sroberto#define ROUNDUP(a) ((a) > 0 ? \
36258945Sroberto		(1 + (((a) - 1) | (sizeof(__uint64_t) - 1))) : \
37258945Sroberto		sizeof(__uint64_t))
38258945Sroberto#else
39258945Sroberto#define ROUNDUP(a) ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) \
40258945Sroberto                    : sizeof(long))
41258945Sroberto#endif
42258945Sroberto
43258945Sroberto#define IFITER_MAGIC		ISC_MAGIC('I', 'F', 'I', 'S')
44258945Sroberto#define VALID_IFITER(t)		ISC_MAGIC_VALID(t, IFITER_MAGIC)
45258945Sroberto
46258945Srobertostruct isc_interfaceiter {
47258945Sroberto	unsigned int		magic;		/* Magic number. */
48258945Sroberto	isc_mem_t		*mctx;
49258945Sroberto	void			*buf;		/* Buffer for sysctl data. */
50258945Sroberto	unsigned int		bufsize;	/* Bytes allocated. */
51258945Sroberto	unsigned int		bufused;	/* Bytes used. */
52258945Sroberto	unsigned int		pos;		/* Current offset in
53258945Sroberto						   sysctl data. */
54258945Sroberto	isc_interface_t		current;	/* Current interface data. */
55258945Sroberto	isc_result_t		result;		/* Last result code. */
56258945Sroberto};
57258945Sroberto
58258945Srobertostatic int mib[6] = {
59258945Sroberto	CTL_NET,
60258945Sroberto	PF_ROUTE,
61258945Sroberto        0,
62258945Sroberto	0, 			/* Any address family. */
63258945Sroberto        NET_RT_IFLIST,
64258945Sroberto	0 			/* Flags. */
65258945Sroberto};
66258945Sroberto
67258945Srobertoisc_result_t
68258945Srobertoisc_interfaceiter_create(isc_mem_t *mctx, isc_interfaceiter_t **iterp) {
69258945Sroberto	isc_interfaceiter_t *iter;
70258945Sroberto	isc_result_t result;
71258945Sroberto	size_t bufsize;
72258945Sroberto	size_t bufused;
73258945Sroberto	char strbuf[ISC_STRERRORSIZE];
74258945Sroberto
75258945Sroberto	REQUIRE(mctx != NULL);
76258945Sroberto	REQUIRE(iterp != NULL);
77258945Sroberto	REQUIRE(*iterp == NULL);
78258945Sroberto
79258945Sroberto	iter = isc_mem_get(mctx, sizeof(*iter));
80258945Sroberto	if (iter == NULL)
81258945Sroberto		return (ISC_R_NOMEMORY);
82258945Sroberto
83258945Sroberto	iter->mctx = mctx;
84258945Sroberto	iter->buf = 0;
85258945Sroberto
86258945Sroberto	/*
87258945Sroberto	 * Determine the amount of memory needed.
88258945Sroberto	 */
89258945Sroberto	bufsize = 0;
90258945Sroberto	if (sysctl(mib, 6, NULL, &bufsize, NULL, (size_t) 0) < 0) {
91258945Sroberto		isc__strerror(errno, strbuf, sizeof(strbuf));
92258945Sroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
93258945Sroberto				 isc_msgcat_get(isc_msgcat,
94258945Sroberto						ISC_MSGSET_IFITERSYSCTL,
95258945Sroberto						ISC_MSG_GETIFLISTSIZE,
96258945Sroberto						"getting interface "
97258945Sroberto						"list size: sysctl: %s"),
98258945Sroberto				 strbuf);
99258945Sroberto		result = ISC_R_UNEXPECTED;
100258945Sroberto		goto failure;
101258945Sroberto	}
102258945Sroberto	iter->bufsize = bufsize;
103258945Sroberto
104258945Sroberto	iter->buf = isc_mem_get(iter->mctx, iter->bufsize);
105258945Sroberto	if (iter->buf == NULL) {
106258945Sroberto		result = ISC_R_NOMEMORY;
107258945Sroberto		goto failure;
108258945Sroberto	}
109258945Sroberto
110258945Sroberto	bufused = bufsize;
111258945Sroberto	if (sysctl(mib, 6, iter->buf, &bufused, NULL, (size_t) 0) < 0) {
112258945Sroberto		isc__strerror(errno, strbuf, sizeof(strbuf));
113258945Sroberto		UNEXPECTED_ERROR(__FILE__, __LINE__,
114258945Sroberto				 isc_msgcat_get(isc_msgcat,
115258945Sroberto						ISC_MSGSET_IFITERSYSCTL,
116258945Sroberto						ISC_MSG_GETIFLIST,
117258945Sroberto						"getting interface list: "
118258945Sroberto						"sysctl: %s"),
119258945Sroberto				 strbuf);
120258945Sroberto		result = ISC_R_UNEXPECTED;
121258945Sroberto		goto failure;
122258945Sroberto	}
123258945Sroberto	iter->bufused = bufused;
124258945Sroberto	INSIST(iter->bufused <= iter->bufsize);
125258945Sroberto
126258945Sroberto	/*
127258945Sroberto	 * A newly created iterator has an undefined position
128258945Sroberto	 * until isc_interfaceiter_first() is called.
129258945Sroberto	 */
130258945Sroberto	iter->pos = (unsigned int) -1;
131258945Sroberto	iter->result = ISC_R_FAILURE;
132258945Sroberto
133258945Sroberto	iter->magic = IFITER_MAGIC;
134258945Sroberto	*iterp = iter;
135258945Sroberto	return (ISC_R_SUCCESS);
136258945Sroberto
137258945Sroberto failure:
138258945Sroberto	if (iter->buf != NULL)
139258945Sroberto		isc_mem_put(mctx, iter->buf, iter->bufsize);
140258945Sroberto	isc_mem_put(mctx, iter, sizeof(*iter));
141258945Sroberto	return (result);
142258945Sroberto}
143258945Sroberto
144258945Sroberto/*
145258945Sroberto * Get information about the current interface to iter->current.
146258945Sroberto * If successful, return ISC_R_SUCCESS.
147258945Sroberto * If the interface has an unsupported address family,
148258945Sroberto * return ISC_R_IGNORE.  In case of other failure,
149258945Sroberto * return ISC_R_UNEXPECTED.
150258945Sroberto */
151258945Sroberto
152258945Srobertostatic isc_result_t
153258945Srobertointernal_current(isc_interfaceiter_t *iter) {
154258945Sroberto	struct ifa_msghdr *ifam, *ifam_end;
155258945Sroberto
156258945Sroberto	REQUIRE(VALID_IFITER(iter));
157258945Sroberto	REQUIRE (iter->pos < (unsigned int) iter->bufused);
158258945Sroberto
159258945Sroberto	ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
160258945Sroberto	ifam_end = (struct ifa_msghdr *) ((char *) iter->buf + iter->bufused);
161258945Sroberto
162258945Sroberto	if (ifam->ifam_type == RTM_IFINFO) {
163258945Sroberto		struct if_msghdr *ifm = (struct if_msghdr *) ifam;
164258945Sroberto		struct sockaddr_dl *sdl = (struct sockaddr_dl *) (ifm + 1);
165258945Sroberto		unsigned int namelen;
166258945Sroberto
167258945Sroberto		memset(&iter->current, 0, sizeof(iter->current));
168258945Sroberto
169258945Sroberto		iter->current.ifindex = sdl->sdl_index;
170258945Sroberto		namelen = sdl->sdl_nlen;
171258945Sroberto		if (namelen > sizeof(iter->current.name) - 1)
172258945Sroberto			namelen = sizeof(iter->current.name) - 1;
173258945Sroberto
174258945Sroberto		memset(iter->current.name, 0, sizeof(iter->current.name));
175258945Sroberto		memcpy(iter->current.name, sdl->sdl_data, namelen);
176258945Sroberto
177258945Sroberto		iter->current.flags = 0;
178258945Sroberto
179258945Sroberto		if ((ifam->ifam_flags & IFF_UP) != 0)
180258945Sroberto			iter->current.flags |= INTERFACE_F_UP;
181258945Sroberto
182258945Sroberto		if ((ifam->ifam_flags & IFF_POINTOPOINT) != 0)
183258945Sroberto			iter->current.flags |= INTERFACE_F_POINTTOPOINT;
184258945Sroberto
185258945Sroberto		if ((ifam->ifam_flags & IFF_LOOPBACK) != 0)
186258945Sroberto			iter->current.flags |= INTERFACE_F_LOOPBACK;
187258945Sroberto
188258945Sroberto		if ((ifam->ifam_flags & IFF_BROADCAST) != 0)
189258945Sroberto			iter->current.flags |= INTERFACE_F_BROADCAST;
190258945Sroberto
191258945Sroberto#ifdef IFF_MULTICAST
192258945Sroberto		if ((ifam->ifam_flags & IFF_MULTICAST) != 0)
193258945Sroberto			iter->current.flags |= INTERFACE_F_MULTICAST;
194258945Sroberto#endif
195258945Sroberto
196258945Sroberto		/*
197258945Sroberto		 * This is not an interface address.
198258945Sroberto		 * Force another iteration.
199258945Sroberto		 */
200258945Sroberto		return (ISC_R_IGNORE);
201258945Sroberto	} else if (ifam->ifam_type == RTM_NEWADDR) {
202258945Sroberto		int i;
203258945Sroberto		int family;
204258945Sroberto		struct sockaddr *mask_sa = NULL;
205258945Sroberto		struct sockaddr *addr_sa = NULL;
206258945Sroberto		struct sockaddr *dst_sa = NULL;
207258945Sroberto
208258945Sroberto		struct sockaddr *sa = (struct sockaddr *)(ifam + 1);
209258945Sroberto		family = sa->sa_family;
210258945Sroberto
211258945Sroberto		for (i = 0; i < RTAX_MAX; i++)
212258945Sroberto		{
213258945Sroberto			if ((ifam->ifam_addrs & (1 << i)) == 0)
214258945Sroberto				continue;
215258945Sroberto
216258945Sroberto			INSIST(sa < (struct sockaddr *) ifam_end);
217258945Sroberto
218258945Sroberto			switch (i) {
219258945Sroberto			case RTAX_NETMASK: /* Netmask */
220258945Sroberto				mask_sa = sa;
221258945Sroberto				break;
222258945Sroberto			case RTAX_IFA: /* Interface address */
223258945Sroberto				addr_sa = sa;
224258945Sroberto				break;
225258945Sroberto			case RTAX_BRD: /* Broadcast or destination address */
226258945Sroberto				dst_sa = sa;
227258945Sroberto				break;
228258945Sroberto			}
229258945Sroberto#ifdef ISC_PLATFORM_HAVESALEN
230258945Sroberto			sa = (struct sockaddr *)((char*)(sa)
231258945Sroberto					 + ROUNDUP(sa->sa_len));
232258945Sroberto#else
233258945Sroberto#ifdef sgi
234258945Sroberto			/*
235258945Sroberto			 * Do as the contributed SGI code does.
236258945Sroberto			 */
237258945Sroberto			sa = (struct sockaddr *)((char*)(sa)
238258945Sroberto					 + ROUNDUP(_FAKE_SA_LEN_DST(sa)));
239258945Sroberto#else
240258945Sroberto			/* XXX untested. */
241258945Sroberto			sa = (struct sockaddr *)((char*)(sa)
242258945Sroberto					 + ROUNDUP(sizeof(struct sockaddr)));
243258945Sroberto#endif
244258945Sroberto#endif
245258945Sroberto		}
246258945Sroberto
247258945Sroberto		if (addr_sa == NULL)
248258945Sroberto			return (ISC_R_IGNORE);
249258945Sroberto
250258945Sroberto		family = addr_sa->sa_family;
251258945Sroberto		if (family != AF_INET && family != AF_INET6)
252258945Sroberto			return (ISC_R_IGNORE);
253258945Sroberto
254258945Sroberto		iter->current.af = family;
255258945Sroberto
256258945Sroberto		get_addr(family, &iter->current.address, addr_sa,
257258945Sroberto			 iter->current.name);
258258945Sroberto
259258945Sroberto		if (mask_sa != NULL)
260258945Sroberto			get_addr(family, &iter->current.netmask, mask_sa,
261258945Sroberto				 iter->current.name);
262258945Sroberto
263258945Sroberto		if (dst_sa != NULL &&
264258945Sroberto		    (iter->current.flags & INTERFACE_F_POINTTOPOINT) != 0)
265258945Sroberto			get_addr(family, &iter->current.dstaddress, dst_sa,
266258945Sroberto				 iter->current.name);
267258945Sroberto
268258945Sroberto		if (dst_sa != NULL &&
269258945Sroberto		    (iter->current.flags & INTERFACE_F_BROADCAST) != 0)
270258945Sroberto			get_addr(family, &iter->current.broadcast, dst_sa,
271258945Sroberto				 iter->current.name);
272258945Sroberto
273258945Sroberto		return (ISC_R_SUCCESS);
274258945Sroberto	} else {
275258945Sroberto		printf(isc_msgcat_get(isc_msgcat, ISC_MSGSET_IFITERSYSCTL,
276258945Sroberto				      ISC_MSG_UNEXPECTEDTYPE,
277258945Sroberto				      "warning: unexpected interface list "
278258945Sroberto				      "message type\n"));
279258945Sroberto		return (ISC_R_IGNORE);
280258945Sroberto	}
281258945Sroberto}
282258945Sroberto
283258945Sroberto/*
284258945Sroberto * Step the iterator to the next interface.  Unlike
285258945Sroberto * isc_interfaceiter_next(), this may leave the iterator
286258945Sroberto * positioned on an interface that will ultimately
287258945Sroberto * be ignored.  Return ISC_R_NOMORE if there are no more
288258945Sroberto * interfaces, otherwise ISC_R_SUCCESS.
289258945Sroberto */
290258945Srobertostatic isc_result_t
291258945Srobertointernal_next(isc_interfaceiter_t *iter) {
292258945Sroberto	struct ifa_msghdr *ifam;
293258945Sroberto	REQUIRE (iter->pos < (unsigned int) iter->bufused);
294258945Sroberto
295258945Sroberto	ifam = (struct ifa_msghdr *) ((char *) iter->buf + iter->pos);
296258945Sroberto
297258945Sroberto	iter->pos += ifam->ifam_msglen;
298258945Sroberto
299258945Sroberto	if (iter->pos >= iter->bufused)
300258945Sroberto		return (ISC_R_NOMORE);
301258945Sroberto
302258945Sroberto	return (ISC_R_SUCCESS);
303258945Sroberto}
304258945Sroberto
305258945Srobertostatic void
306258945Srobertointernal_destroy(isc_interfaceiter_t *iter) {
307258945Sroberto	UNUSED(iter); /* Unused. */
308258945Sroberto	/*
309258945Sroberto	 * Do nothing.
310258945Sroberto	 */
311258945Sroberto}
312258945Sroberto
313258945Srobertostatic
314258945Srobertovoid internal_first(isc_interfaceiter_t *iter) {
315258945Sroberto	iter->pos = 0;
316258945Sroberto}
317