xref: /freebsd/lib/libc/net/getifaddrs.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*	$KAME: getifaddrs.c,v 1.9 2001/08/20 02:31:20 itojun Exp $	*/
2 
3 /*
4  * Copyright (c) 1995, 1999
5  *	Berkeley Software Design, Inc.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  *
25  *	BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
26  */
27 /*
28  * NOTE: SIOCGIFCONF case is not LP64 friendly.  it also does not perform
29  * try-and-error for region size.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "namespace.h"
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
39 #include <net/if.h>
40 #ifdef	NET_RT_IFLIST
41 #include <sys/param.h>
42 #include <net/route.h>
43 #include <sys/sysctl.h>
44 #include <net/if_dl.h>
45 #endif
46 
47 #include <ifaddrs.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include "un-namespace.h"
51 
52 #if !defined(AF_LINK)
53 #define	SA_LEN(sa)	sizeof(struct sockaddr)
54 #endif
55 
56 #if !defined(SA_LEN)
57 #define	SA_LEN(sa)	(sa)->sa_len
58 #endif
59 
60 #define	SALIGN	(sizeof(long) - 1)
61 #define	SA_RLEN(sa)	((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
62 
63 #ifndef	ALIGNBYTES
64 /*
65  * On systems with a routing socket, ALIGNBYTES should match the value
66  * that the kernel uses when building the messages.
67  */
68 #define	ALIGNBYTES	XXX
69 #endif
70 #ifndef	ALIGN
71 #define	ALIGN(p)	(((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
72 #endif
73 
74 #if	_BSDI_VERSION >= 199701
75 #define	HAVE_IFM_DATA
76 #endif
77 
78 #if	_BSDI_VERSION >= 199802
79 /* ifam_data is very specific to recent versions of bsdi */
80 #define	HAVE_IFAM_DATA
81 #endif
82 
83 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
84 #define	HAVE_IFM_DATA
85 #endif
86 
87 int
88 getifaddrs(struct ifaddrs **pif)
89 {
90 	int icnt = 1;
91 	int dcnt = 0;
92 	int ncnt = 0;
93 #ifdef	NET_RT_IFLIST
94 	int mib[6];
95 	size_t needed;
96 	char *buf;
97 	char *next;
98 	struct ifaddrs *cif = 0;
99 	char *p, *p0;
100 	struct rt_msghdr *rtm;
101 	struct if_msghdr *ifm;
102 	struct ifa_msghdr *ifam;
103 	struct sockaddr_dl *dl;
104 	struct sockaddr *sa;
105 	struct ifaddrs *ifa, *ift;
106 	u_short idx = 0;
107 #else	/* NET_RT_IFLIST */
108 	char buf[1024];
109 	int m, sock;
110 	struct ifconf ifc;
111 	struct ifreq *ifr;
112 	struct ifreq *lifr;
113 #endif	/* NET_RT_IFLIST */
114 	int i;
115 	size_t len, alen;
116 	char *data;
117 	char *names;
118 
119 #ifdef	NET_RT_IFLIST
120 	mib[0] = CTL_NET;
121 	mib[1] = PF_ROUTE;
122 	mib[2] = 0;             /* protocol */
123 	mib[3] = 0;             /* wildcard address family */
124 	mib[4] = NET_RT_IFLIST;
125 	mib[5] = 0;             /* no flags */
126 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
127 		return (-1);
128 	if ((buf = malloc(needed)) == NULL)
129 		return (-1);
130 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
131 		free(buf);
132 		return (-1);
133 	}
134 
135 	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
136 		rtm = (struct rt_msghdr *)(void *)next;
137 		if (rtm->rtm_version != RTM_VERSION)
138 			continue;
139 		switch (rtm->rtm_type) {
140 		case RTM_IFINFO:
141 			ifm = (struct if_msghdr *)(void *)rtm;
142 			if (ifm->ifm_addrs & RTA_IFP) {
143 				idx = ifm->ifm_index;
144 				++icnt;
145 				dl = (struct sockaddr_dl *)(void *)(ifm + 1);
146 				dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
147 				    ALIGNBYTES;
148 #ifdef	HAVE_IFM_DATA
149 				dcnt += sizeof(ifm->ifm_data);
150 #endif	/* HAVE_IFM_DATA */
151 				ncnt += dl->sdl_nlen + 1;
152 			} else
153 				idx = 0;
154 			break;
155 
156 		case RTM_NEWADDR:
157 			ifam = (struct ifa_msghdr *)(void *)rtm;
158 			if (idx && ifam->ifam_index != idx)
159 				abort();	/* this cannot happen */
160 
161 #define	RTA_MASKS	(RTA_NETMASK | RTA_IFA | RTA_BRD)
162 			if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
163 				break;
164 			p = (char *)(void *)(ifam + 1);
165 			++icnt;
166 #ifdef	HAVE_IFAM_DATA
167 			dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
168 #endif	/* HAVE_IFAM_DATA */
169 			/* Scan to look for length of address */
170 			alen = 0;
171 			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
172 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
173 				    == 0)
174 					continue;
175 				sa = (struct sockaddr *)(void *)p;
176 				len = SA_RLEN(sa);
177 				if (i == RTAX_IFA) {
178 					alen = len;
179 					break;
180 				}
181 				p += len;
182 			}
183 			for (p = p0, i = 0; i < RTAX_MAX; i++) {
184 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
185 				    == 0)
186 					continue;
187 				sa = (struct sockaddr *)(void *)p;
188 				len = SA_RLEN(sa);
189 				if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
190 					dcnt += alen;
191 				else
192 					dcnt += len;
193 				p += len;
194 			}
195 			break;
196 		}
197 	}
198 #else	/* NET_RT_IFLIST */
199 	ifc.ifc_buf = buf;
200 	ifc.ifc_len = sizeof(buf);
201 
202 	if ((sock = _socket(AF_INET, SOCK_STREAM, 0)) < 0)
203 		return (-1);
204 	i =  _ioctl(sock, SIOCGIFCONF, (char *)&ifc);
205 	_close(sock);
206 	if (i < 0)
207 		return (-1);
208 
209 	ifr = ifc.ifc_req;
210 	lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
211 
212 	while (ifr < lifr) {
213 		struct sockaddr *sa;
214 
215 		sa = &ifr->ifr_addr;
216 		++icnt;
217 		dcnt += SA_RLEN(sa);
218 		ncnt += sizeof(ifr->ifr_name) + 1;
219 
220 		if (SA_LEN(sa) < sizeof(*sa))
221 			ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
222 		else
223 			ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
224 	}
225 #endif	/* NET_RT_IFLIST */
226 
227 	if (icnt + dcnt + ncnt == 1) {
228 		*pif = NULL;
229 		free(buf);
230 		return (0);
231 	}
232 	data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
233 	if (data == NULL) {
234 		free(buf);
235 		return(-1);
236 	}
237 
238 	ifa = (struct ifaddrs *)(void *)data;
239 	data += sizeof(struct ifaddrs) * icnt;
240 	names = data + dcnt;
241 
242 	memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
243 	ift = ifa;
244 
245 #ifdef	NET_RT_IFLIST
246 	idx = 0;
247 	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
248 		rtm = (struct rt_msghdr *)(void *)next;
249 		if (rtm->rtm_version != RTM_VERSION)
250 			continue;
251 		switch (rtm->rtm_type) {
252 		case RTM_IFINFO:
253 			ifm = (struct if_msghdr *)(void *)rtm;
254 			if (ifm->ifm_addrs & RTA_IFP) {
255 				idx = ifm->ifm_index;
256 				dl = (struct sockaddr_dl *)(void *)(ifm + 1);
257 
258 				cif = ift;
259 				ift->ifa_name = names;
260 				ift->ifa_flags = (int)ifm->ifm_flags;
261 				memcpy(names, dl->sdl_data,
262 				    (size_t)dl->sdl_nlen);
263 				names[dl->sdl_nlen] = 0;
264 				names += dl->sdl_nlen + 1;
265 
266 				ift->ifa_addr = (struct sockaddr *)(void *)data;
267 				memcpy(data, dl,
268 				    (size_t)SA_LEN((struct sockaddr *)
269 				    (void *)dl));
270 				data += SA_RLEN((struct sockaddr *)(void *)dl);
271 
272 #ifdef	HAVE_IFM_DATA
273 				/* ifm_data needs to be aligned */
274 				ift->ifa_data = data = (void *)ALIGN(data);
275 				memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
276  				data += sizeof(ifm->ifm_data);
277 #else	/* HAVE_IFM_DATA */
278 				ift->ifa_data = NULL;
279 #endif	/* HAVE_IFM_DATA */
280 
281 				ift = (ift->ifa_next = ift + 1);
282 			} else
283 				idx = 0;
284 			break;
285 
286 		case RTM_NEWADDR:
287 			ifam = (struct ifa_msghdr *)(void *)rtm;
288 			if (idx && ifam->ifam_index != idx)
289 				abort();	/* this cannot happen */
290 
291 			if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
292 				break;
293 			ift->ifa_name = cif->ifa_name;
294 			ift->ifa_flags = cif->ifa_flags;
295 			ift->ifa_data = NULL;
296 			p = (char *)(void *)(ifam + 1);
297 			/* Scan to look for length of address */
298 			alen = 0;
299 			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
300 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
301 				    == 0)
302 					continue;
303 				sa = (struct sockaddr *)(void *)p;
304 				len = SA_RLEN(sa);
305 				if (i == RTAX_IFA) {
306 					alen = len;
307 					break;
308 				}
309 				p += len;
310 			}
311 			for (p = p0, i = 0; i < RTAX_MAX; i++) {
312 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
313 				    == 0)
314 					continue;
315 				sa = (struct sockaddr *)(void *)p;
316 				len = SA_RLEN(sa);
317 				switch (i) {
318 				case RTAX_IFA:
319 					ift->ifa_addr =
320 					    (struct sockaddr *)(void *)data;
321 					memcpy(data, p, len);
322 					data += len;
323 					break;
324 
325 				case RTAX_NETMASK:
326 					ift->ifa_netmask =
327 					    (struct sockaddr *)(void *)data;
328 					if (SA_LEN(sa) == 0) {
329 						memset(data, 0, alen);
330 						data += alen;
331 						break;
332 					}
333 					memcpy(data, p, len);
334 					data += len;
335 					break;
336 
337 				case RTAX_BRD:
338 					ift->ifa_broadaddr =
339 					    (struct sockaddr *)(void *)data;
340 					memcpy(data, p, len);
341 					data += len;
342 					break;
343 				}
344 				p += len;
345 			}
346 
347 #ifdef	HAVE_IFAM_DATA
348 			/* ifam_data needs to be aligned */
349 			ift->ifa_data = data = (void *)ALIGN(data);
350 			memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
351 			data += sizeof(ifam->ifam_data);
352 #endif	/* HAVE_IFAM_DATA */
353 
354 			ift = (ift->ifa_next = ift + 1);
355 			break;
356 		}
357 	}
358 
359 	free(buf);
360 #else	/* NET_RT_IFLIST */
361 	ifr = ifc.ifc_req;
362 	lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
363 
364 	while (ifr < lifr) {
365 		struct sockaddr *sa;
366 
367 		ift->ifa_name = names;
368 		names[sizeof(ifr->ifr_name)] = 0;
369 		strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
370 		while (*names++)
371 			;
372 
373 		ift->ifa_addr = (struct sockaddr *)data;
374 		sa = &ifr->ifr_addr;
375 		memcpy(data, sa, SA_LEN(sa));
376 		data += SA_RLEN(sa);
377 
378 		ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
379 		ift = (ift->ifa_next = ift + 1);
380 	}
381 #endif	/* NET_RT_IFLIST */
382 	if (--ift >= ifa) {
383 		ift->ifa_next = NULL;
384 		*pif = ifa;
385 	} else {
386 		*pif = NULL;
387 		free(ifa);
388 	}
389 	return (0);
390 }
391 
392 void
393 freeifaddrs(struct ifaddrs *ifp)
394 {
395 
396 	free(ifp);
397 }
398