xref: /freebsd/lib/libc/net/getifaddrs.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
1 /*	$FreeBSD$	*/
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 #include "namespace.h"
32 #include <sys/types.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <net/if.h>
36 #ifdef	NET_RT_IFLIST
37 #include <sys/param.h>
38 #include <net/route.h>
39 #include <sys/sysctl.h>
40 #include <net/if_dl.h>
41 #endif
42 
43 #include <ifaddrs.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include "un-namespace.h"
47 
48 #if !defined(AF_LINK)
49 #define	SA_LEN(sa)	sizeof(struct sockaddr)
50 #endif
51 
52 #if !defined(SA_LEN)
53 #define	SA_LEN(sa)	(sa)->sa_len
54 #endif
55 
56 #define	SALIGN	(sizeof(long) - 1)
57 #define	SA_RLEN(sa)	((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
58 
59 #ifndef	ALIGNBYTES
60 /*
61  * On systems with a routing socket, ALIGNBYTES should match the value
62  * that the kernel uses when building the messages.
63  */
64 #define	ALIGNBYTES	XXX
65 #endif
66 #ifndef	ALIGN
67 #define	ALIGN(p)	(((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
68 #endif
69 
70 #if	_BSDI_VERSION >= 199701
71 #define	HAVE_IFM_DATA
72 #endif
73 
74 #if	_BSDI_VERSION >= 199802
75 /* ifam_data is very specific to recent versions of bsdi */
76 #define	HAVE_IFAM_DATA
77 #endif
78 
79 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
80 #define	HAVE_IFM_DATA
81 #endif
82 
83 int
84 getifaddrs(struct ifaddrs **pif)
85 {
86 	int icnt = 1;
87 	int dcnt = 0;
88 	int ncnt = 0;
89 #ifdef	NET_RT_IFLIST
90 	int mib[6];
91 	size_t needed;
92 	char *buf;
93 	char *next;
94 	struct ifaddrs *cif = 0;
95 	char *p, *p0;
96 	struct rt_msghdr *rtm;
97 	struct if_msghdr *ifm;
98 	struct ifa_msghdr *ifam;
99 	struct sockaddr_dl *dl;
100 	struct sockaddr *sa;
101 	struct ifaddrs *ifa, *ift;
102 	u_short index = 0;
103 #else	/* NET_RT_IFLIST */
104 	char buf[1024];
105 	int m, sock;
106 	struct ifconf ifc;
107 	struct ifreq *ifr;
108 	struct ifreq *lifr;
109 #endif	/* NET_RT_IFLIST */
110 	int i;
111 	size_t len, alen;
112 	char *data;
113 	char *names;
114 
115 #ifdef	NET_RT_IFLIST
116 	mib[0] = CTL_NET;
117 	mib[1] = PF_ROUTE;
118 	mib[2] = 0;             /* protocol */
119 	mib[3] = 0;             /* wildcard address family */
120 	mib[4] = NET_RT_IFLIST;
121 	mib[5] = 0;             /* no flags */
122 	if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
123 		return (-1);
124 	if ((buf = malloc(needed)) == NULL)
125 		return (-1);
126 	if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
127 		free(buf);
128 		return (-1);
129 	}
130 
131 	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
132 		rtm = (struct rt_msghdr *)next;
133 		if (rtm->rtm_version != RTM_VERSION)
134 			continue;
135 		switch (rtm->rtm_type) {
136 		case RTM_IFINFO:
137 			ifm = (struct if_msghdr *)rtm;
138 			if (ifm->ifm_addrs & RTA_IFP) {
139 				index = ifm->ifm_index;
140 				++icnt;
141 				dl = (struct sockaddr_dl *)(ifm + 1);
142 				dcnt += SA_RLEN((struct sockaddr *)dl) +
143 					ALIGNBYTES;
144 #ifdef	HAVE_IFM_DATA
145 				dcnt += sizeof(ifm->ifm_data);
146 #endif	/* HAVE_IFM_DATA */
147 				ncnt += dl->sdl_nlen + 1;
148 			} else
149 				index = 0;
150 			break;
151 
152 		case RTM_NEWADDR:
153 			ifam = (struct ifa_msghdr *)rtm;
154 			if (index && ifam->ifam_index != index)
155 				abort();	/* this cannot happen */
156 
157 #define	RTA_MASKS	(RTA_NETMASK | RTA_IFA | RTA_BRD)
158 			if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
159 				break;
160 			p = (char *)(ifam + 1);
161 			++icnt;
162 #ifdef	HAVE_IFAM_DATA
163 			dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
164 #endif	/* HAVE_IFAM_DATA */
165 			/* Scan to look for length of address */
166 			alen = 0;
167 			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
168 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
169 				    == 0)
170 					continue;
171 				sa = (struct sockaddr *)p;
172 				len = SA_RLEN(sa);
173 				if (i == RTAX_IFA) {
174 					alen = len;
175 					break;
176 				}
177 				p += len;
178 			}
179 			for (p = p0, i = 0; i < RTAX_MAX; i++) {
180 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
181 				    == 0)
182 					continue;
183 				sa = (struct sockaddr *)p;
184 				len = SA_RLEN(sa);
185 				if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
186 					dcnt += alen;
187 				else
188 					dcnt += len;
189 				p += len;
190 			}
191 			break;
192 		}
193 	}
194 #else	/* NET_RT_IFLIST */
195 	ifc.ifc_buf = buf;
196 	ifc.ifc_len = sizeof(buf);
197 
198 	if ((sock = _socket(AF_INET, SOCK_STREAM, 0)) < 0)
199 		return (-1);
200 	i =  _ioctl(sock, SIOCGIFCONF, (char *)&ifc);
201 	_close(sock);
202 	if (i < 0)
203 		return (-1);
204 
205 	ifr = ifc.ifc_req;
206 	lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
207 
208 	while (ifr < lifr) {
209 		struct sockaddr *sa;
210 
211 		sa = &ifr->ifr_addr;
212 		++icnt;
213 		dcnt += SA_RLEN(sa);
214 		ncnt += sizeof(ifr->ifr_name) + 1;
215 
216 		ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
217 	}
218 #endif	/* NET_RT_IFLIST */
219 
220 	if (icnt + dcnt + ncnt == 1) {
221 		*pif = NULL;
222 		free(buf);
223 		return (0);
224 	}
225 	data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
226 	if (data == NULL) {
227 		free(buf);
228 		return(-1);
229 	}
230 
231 	ifa = (struct ifaddrs *)data;
232 	data += sizeof(struct ifaddrs) * icnt;
233 	names = data + dcnt;
234 
235 	memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
236 	ift = ifa;
237 
238 #ifdef	NET_RT_IFLIST
239 	index = 0;
240 	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
241 		rtm = (struct rt_msghdr *)next;
242 		if (rtm->rtm_version != RTM_VERSION)
243 			continue;
244 		switch (rtm->rtm_type) {
245 		case RTM_IFINFO:
246 			ifm = (struct if_msghdr *)rtm;
247 			if (ifm->ifm_addrs & RTA_IFP) {
248 				index = ifm->ifm_index;
249 				dl = (struct sockaddr_dl *)(ifm + 1);
250 
251 				cif = ift;
252 				ift->ifa_name = names;
253 				ift->ifa_flags = (int)ifm->ifm_flags;
254 				memcpy(names, dl->sdl_data, dl->sdl_nlen);
255 				names[dl->sdl_nlen] = 0;
256 				names += dl->sdl_nlen + 1;
257 
258 				ift->ifa_addr = (struct sockaddr *)data;
259 				memcpy(data, dl, SA_LEN((struct sockaddr *)dl));
260 				data += SA_RLEN((struct sockaddr *)dl);
261 
262 #ifdef	HAVE_IFM_DATA
263 				/* ifm_data needs to be aligned */
264 				ift->ifa_data = data = (void *)ALIGN(data);
265 				memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
266  				data += sizeof(ifm->ifm_data);
267 #else	/* HAVE_IFM_DATA */
268 				ift->ifa_data = NULL;
269 #endif	/* HAVE_IFM_DATA */
270 
271 				ift = (ift->ifa_next = ift + 1);
272 			} else
273 				index = 0;
274 			break;
275 
276 		case RTM_NEWADDR:
277 			ifam = (struct ifa_msghdr *)rtm;
278 			if (index && ifam->ifam_index != index)
279 				abort();	/* this cannot happen */
280 
281 			if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
282 				break;
283 			ift->ifa_name = cif->ifa_name;
284 			ift->ifa_flags = cif->ifa_flags;
285 			ift->ifa_data = NULL;
286 			p = (char *)(ifam + 1);
287 			/* Scan to look for length of address */
288 			alen = 0;
289 			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
290 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
291 				    == 0)
292 					continue;
293 				sa = (struct sockaddr *)p;
294 				len = SA_RLEN(sa);
295 				if (i == RTAX_IFA) {
296 					alen = len;
297 					break;
298 				}
299 				p += len;
300 			}
301 			for (p = p0, i = 0; i < RTAX_MAX; i++) {
302 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
303 				    == 0)
304 					continue;
305 				sa = (struct sockaddr *)p;
306 				len = SA_RLEN(sa);
307 				switch (i) {
308 				case RTAX_IFA:
309 					ift->ifa_addr = (struct sockaddr *)data;
310 					memcpy(data, p, len);
311 					data += len;
312 					break;
313 
314 				case RTAX_NETMASK:
315 					ift->ifa_netmask =
316 					    (struct sockaddr *)data;
317 					if (SA_LEN(sa) == 0) {
318 						memset(data, 0, alen);
319 						data += alen;
320 						break;
321 					}
322 					memcpy(data, p, len);
323 					data += len;
324 					break;
325 
326 				case RTAX_BRD:
327 					ift->ifa_broadaddr =
328 					    (struct sockaddr *)data;
329 					memcpy(data, p, len);
330 					data += len;
331 					break;
332 				}
333 				p += len;
334 			}
335 
336 #ifdef	HAVE_IFAM_DATA
337 			/* ifam_data needs to be aligned */
338 			ift->ifa_data = data = (void *)ALIGN(data);
339 			memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
340 			data += sizeof(ifam->ifam_data);
341 #endif	/* HAVE_IFAM_DATA */
342 
343 			ift = (ift->ifa_next = ift + 1);
344 			break;
345 		}
346 	}
347 
348 	free(buf);
349 #else	/* NET_RT_IFLIST */
350 	ifr = ifc.ifc_req;
351 	lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
352 
353 	while (ifr < lifr) {
354 		struct sockaddr *sa;
355 
356 		ift->ifa_name = names;
357 		names[sizeof(ifr->ifr_name)] = 0;
358 		strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
359 		while (*names++)
360 			;
361 
362 		ift->ifa_addr = (struct sockaddr *)data;
363 		sa = &ifr->ifr_addr;
364 		memcpy(data, sa, SA_LEN(sa));
365 		data += SA_RLEN(sa);
366 
367 		ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
368 		ift = (ift->ifa_next = ift + 1);
369 	}
370 #endif	/* NET_RT_IFLIST */
371 	if (--ift >= ifa) {
372 		ift->ifa_next = NULL;
373 		*pif = ifa;
374 	} else {
375 		*pif = NULL;
376 		free(ifa);
377 	}
378 	return (0);
379 }
380 
381 void
382 freeifaddrs(struct ifaddrs *ifp)
383 {
384 	free(ifp);
385 }
386