1 /* $KAME: getifaddrs.c,v 1.9 2001/08/20 02:31:20 itojun Exp $ */
4 * Copyright (c) 1995, 1999
5 * Berkeley Software Design, Inc. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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
25 * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
28 * NOTE: SIOCGIFCONF case is not LP64 friendly. it also does not perform
29 * try-and-error for region size.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "namespace.h"
36 #include <sys/types.h>
37 #include <sys/ioctl.h>
38 #include <sys/socket.h>
41 #include <sys/param.h>
42 #include <net/route.h>
43 #include <sys/sysctl.h>
44 #include <net/if_dl.h>
51 #include "un-namespace.h"
54 #define SA_LEN(sa) sizeof(struct sockaddr)
58 #define SA_LEN(sa) (sa)->sa_len
61 #define SALIGN (sizeof(long) - 1)
62 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
66 * On systems with a routing socket, ALIGNBYTES should match the value
67 * that the kernel uses when building the messages.
69 #define ALIGNBYTES XXX
72 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
75 #if _BSDI_VERSION >= 199701
79 #if _BSDI_VERSION >= 199802
80 /* ifam_data is very specific to recent versions of bsdi */
81 #define HAVE_IFAM_DATA
84 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
88 #define MAX_SYSCTL_TRY 5
91 getifaddrs(struct ifaddrs **pif)
102 struct ifaddrs *cif = 0;
104 struct rt_msghdr *rtm;
105 struct if_msghdr *ifm;
106 struct ifa_msghdr *ifam;
107 struct sockaddr_dl *dl;
109 struct ifaddrs *ifa, *ift;
111 #else /* NET_RT_IFLIST */
117 #endif /* NET_RT_IFLIST */
126 mib[2] = 0; /* protocol */
127 mib[3] = 0; /* wildcard address family */
128 mib[4] = NET_RT_IFLIST;
129 mib[5] = 0; /* no flags */
132 * We'll try to get addresses several times in case that
133 * the number of addresses is unexpectedly increased during
134 * the two sysctl calls. This should rarely happen, but we'll
135 * try to do our best for applications that assume success of
136 * this library (which should usually be the case).
137 * Portability note: since FreeBSD does not add margin of
138 * memory at the first sysctl, the possibility of failure on
139 * the second sysctl call is a bit higher.
142 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
144 if ((buf = malloc(needed)) == NULL)
146 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
147 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
154 } while (buf == NULL);
156 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
157 rtm = (struct rt_msghdr *)(void *)next;
158 if (rtm->rtm_version != RTM_VERSION)
160 switch (rtm->rtm_type) {
162 ifm = (struct if_msghdr *)(void *)rtm;
163 if (ifm->ifm_addrs & RTA_IFP) {
164 idx = ifm->ifm_index;
166 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
167 dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
170 dcnt += sizeof(ifm->ifm_data);
171 #endif /* HAVE_IFM_DATA */
172 ncnt += dl->sdl_nlen + 1;
178 ifam = (struct ifa_msghdr *)(void *)rtm;
179 if (idx && ifam->ifam_index != idx)
180 abort(); /* this cannot happen */
182 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
183 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
185 p = (char *)(void *)(ifam + 1);
187 #ifdef HAVE_IFAM_DATA
188 dcnt += sizeof(ifam->ifam_data) + ALIGNBYTES;
189 #endif /* HAVE_IFAM_DATA */
190 /* Scan to look for length of address */
192 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
193 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
196 sa = (struct sockaddr *)(void *)p;
204 for (p = p0, i = 0; i < RTAX_MAX; i++) {
205 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
208 sa = (struct sockaddr *)(void *)p;
210 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
219 #else /* NET_RT_IFLIST */
221 ifc.ifc_len = sizeof(buf);
223 if ((sock = _socket(AF_INET, SOCK_STREAM, 0)) < 0)
225 i = _ioctl(sock, SIOCGIFCONF, (char *)&ifc);
231 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
239 ncnt += sizeof(ifr->ifr_name) + 1;
241 if (SA_LEN(sa) < sizeof(*sa))
242 ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
244 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
246 #endif /* NET_RT_IFLIST */
248 if (icnt + dcnt + ncnt == 1) {
253 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
259 ifa = (struct ifaddrs *)(void *)data;
260 data += sizeof(struct ifaddrs) * icnt;
263 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
268 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
269 rtm = (struct rt_msghdr *)(void *)next;
270 if (rtm->rtm_version != RTM_VERSION)
272 switch (rtm->rtm_type) {
274 ifm = (struct if_msghdr *)(void *)rtm;
275 if (ifm->ifm_addrs & RTA_IFP) {
276 idx = ifm->ifm_index;
277 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
280 ift->ifa_name = names;
281 ift->ifa_flags = (int)ifm->ifm_flags;
282 memcpy(names, dl->sdl_data,
283 (size_t)dl->sdl_nlen);
284 names[dl->sdl_nlen] = 0;
285 names += dl->sdl_nlen + 1;
287 ift->ifa_addr = (struct sockaddr *)(void *)data;
289 (size_t)SA_LEN((struct sockaddr *)
291 data += SA_RLEN((struct sockaddr *)(void *)dl);
294 /* ifm_data needs to be aligned */
295 ift->ifa_data = data = (void *)ALIGN(data);
296 memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
297 data += sizeof(ifm->ifm_data);
298 #else /* HAVE_IFM_DATA */
299 ift->ifa_data = NULL;
300 #endif /* HAVE_IFM_DATA */
302 ift = (ift->ifa_next = ift + 1);
308 ifam = (struct ifa_msghdr *)(void *)rtm;
309 if (idx && ifam->ifam_index != idx)
310 abort(); /* this cannot happen */
312 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
314 ift->ifa_name = cif->ifa_name;
315 ift->ifa_flags = cif->ifa_flags;
316 ift->ifa_data = NULL;
317 p = (char *)(void *)(ifam + 1);
318 /* Scan to look for length of address */
320 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
321 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
324 sa = (struct sockaddr *)(void *)p;
332 for (p = p0, i = 0; i < RTAX_MAX; i++) {
333 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
336 sa = (struct sockaddr *)(void *)p;
341 (struct sockaddr *)(void *)data;
342 memcpy(data, p, len);
348 (struct sockaddr *)(void *)data;
349 if (SA_LEN(sa) == 0) {
350 memset(data, 0, alen);
354 memcpy(data, p, len);
360 (struct sockaddr *)(void *)data;
361 memcpy(data, p, len);
368 #ifdef HAVE_IFAM_DATA
369 /* ifam_data needs to be aligned */
370 ift->ifa_data = data = (void *)ALIGN(data);
371 memcpy(data, &ifam->ifam_data, sizeof(ifam->ifam_data));
372 data += sizeof(ifam->ifam_data);
373 #endif /* HAVE_IFAM_DATA */
375 ift = (ift->ifa_next = ift + 1);
381 #else /* NET_RT_IFLIST */
383 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
388 ift->ifa_name = names;
389 names[sizeof(ifr->ifr_name)] = 0;
390 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
394 ift->ifa_addr = (struct sockaddr *)data;
396 memcpy(data, sa, SA_LEN(sa));
399 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
400 ift = (ift->ifa_next = ift + 1);
402 #endif /* NET_RT_IFLIST */
404 ift->ifa_next = NULL;
414 freeifaddrs(struct ifaddrs *ifp)