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>
50 #include "un-namespace.h"
53 #define SA_LEN(sa) sizeof(struct sockaddr)
57 #define SA_LEN(sa) (sa)->sa_len
60 #define SALIGN (sizeof(long) - 1)
61 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
65 * On systems with a routing socket, ALIGNBYTES should match the value
66 * that the kernel uses when building the messages.
68 #define ALIGNBYTES XXX
71 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
74 #if _BSDI_VERSION >= 199701
78 #if _BSDI_VERSION >= 199802
79 /* ifam_data is very specific to recent versions of bsdi */
80 #define HAVE_IFAM_DATA
83 #if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
88 getifaddrs(struct ifaddrs **pif)
98 struct ifaddrs *cif = 0;
100 struct rt_msghdr *rtm;
101 struct if_msghdr *ifm;
102 struct ifa_msghdr *ifam;
103 struct sockaddr_dl *dl;
105 struct ifaddrs *ifa, *ift;
107 #else /* NET_RT_IFLIST */
113 #endif /* NET_RT_IFLIST */
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)
128 if ((buf = malloc(needed)) == NULL)
130 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
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)
139 switch (rtm->rtm_type) {
141 ifm = (struct if_msghdr *)(void *)rtm;
142 if (ifm->ifm_addrs & RTA_IFP) {
143 idx = ifm->ifm_index;
145 dl = (struct sockaddr_dl *)(void *)(ifm + 1);
146 dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
149 dcnt += sizeof(ifm->ifm_data);
150 #endif /* HAVE_IFM_DATA */
151 ncnt += dl->sdl_nlen + 1;
157 ifam = (struct ifa_msghdr *)(void *)rtm;
158 if (idx && ifam->ifam_index != idx)
159 abort(); /* this cannot happen */
161 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
162 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
164 p = (char *)(void *)(ifam + 1);
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 */
171 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
172 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
175 sa = (struct sockaddr *)(void *)p;
183 for (p = p0, i = 0; i < RTAX_MAX; i++) {
184 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
187 sa = (struct sockaddr *)(void *)p;
189 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
198 #else /* NET_RT_IFLIST */
200 ifc.ifc_len = sizeof(buf);
202 if ((sock = _socket(AF_INET, SOCK_STREAM, 0)) < 0)
204 i = _ioctl(sock, SIOCGIFCONF, (char *)&ifc);
210 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
218 ncnt += sizeof(ifr->ifr_name) + 1;
220 if (SA_LEN(sa) < sizeof(*sa))
221 ifr = (struct ifreq *)(((char *)sa) + sizeof(*sa));
223 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
225 #endif /* NET_RT_IFLIST */
227 if (icnt + dcnt + ncnt == 1) {
232 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
238 ifa = (struct ifaddrs *)(void *)data;
239 data += sizeof(struct ifaddrs) * icnt;
242 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
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)
251 switch (rtm->rtm_type) {
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);
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;
266 ift->ifa_addr = (struct sockaddr *)(void *)data;
268 (size_t)SA_LEN((struct sockaddr *)
270 data += SA_RLEN((struct sockaddr *)(void *)dl);
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 */
281 ift = (ift->ifa_next = ift + 1);
287 ifam = (struct ifa_msghdr *)(void *)rtm;
288 if (idx && ifam->ifam_index != idx)
289 abort(); /* this cannot happen */
291 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
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 */
299 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
300 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
303 sa = (struct sockaddr *)(void *)p;
311 for (p = p0, i = 0; i < RTAX_MAX; i++) {
312 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
315 sa = (struct sockaddr *)(void *)p;
320 (struct sockaddr *)(void *)data;
321 memcpy(data, p, len);
327 (struct sockaddr *)(void *)data;
328 if (SA_LEN(sa) == 0) {
329 memset(data, 0, alen);
333 memcpy(data, p, len);
339 (struct sockaddr *)(void *)data;
340 memcpy(data, p, len);
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 */
354 ift = (ift->ifa_next = ift + 1);
360 #else /* NET_RT_IFLIST */
362 lifr = (struct ifreq *)&ifc.ifc_buf[ifc.ifc_len];
367 ift->ifa_name = names;
368 names[sizeof(ifr->ifr_name)] = 0;
369 strncpy(names, ifr->ifr_name, sizeof(ifr->ifr_name));
373 ift->ifa_addr = (struct sockaddr *)data;
375 memcpy(data, sa, SA_LEN(sa));
378 ifr = (struct ifreq *)(((char *)sa) + SA_LEN(sa));
379 ift = (ift->ifa_next = ift + 1);
381 #endif /* NET_RT_IFLIST */
383 ift->ifa_next = NULL;
393 freeifaddrs(struct ifaddrs *ifp)