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