1 /* $KAME: getifaddrs.c,v 1.9 2001/08/20 02:31:20 itojun Exp $ */
4 * SPDX-License-Identifier: BSD-1-Clause
6 * Copyright (c) 1995, 1999
7 * Berkeley Software Design, Inc. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
30 * NOTE: SIOCGIFCONF case is not LP64 friendly. it also does not perform
31 * try-and-error for region size.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include "namespace.h"
38 #include <sys/types.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
43 #include <sys/param.h>
44 #include <net/route.h>
45 #include <sys/sysctl.h>
46 #include <net/if_dl.h>
53 #include "un-namespace.h"
56 #define SA_LEN(sa) sizeof(struct sockaddr)
60 #define SA_LEN(sa) (sa)->sa_len
63 #define SALIGN (sizeof(long) - 1)
64 #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1))
68 * On systems with a routing socket, ALIGNBYTES should match the value
69 * that the kernel uses when building the messages.
71 #define ALIGNBYTES XXX
74 #define ALIGN(p) (((u_long)(p) + ALIGNBYTES) &~ ALIGNBYTES)
77 #define MAX_SYSCTL_TRY 5
80 getifaddrs(struct ifaddrs **pif)
92 struct rt_msghdr *rtm;
93 struct if_msghdrl *ifm;
94 struct ifa_msghdrl *ifam;
95 struct sockaddr_dl *dl;
97 struct ifaddrs *ifa, *ift;
98 struct if_data *if_data;
107 mib[2] = 0; /* protocol */
108 mib[3] = 0; /* wildcard address family */
109 mib[4] = NET_RT_IFLISTL;/* extra fields for extensible msghdr structs */
110 mib[5] = 0; /* no flags */
113 * We'll try to get addresses several times in case that
114 * the number of addresses is unexpectedly increased during
115 * the two sysctl calls. This should rarely happen, but we'll
116 * try to do our best for applications that assume success of
117 * this library (which should usually be the case).
118 * Portability note: since FreeBSD does not add margin of
119 * memory at the first sysctl, the possibility of failure on
120 * the second sysctl call is a bit higher.
123 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
125 if ((buf = malloc(needed)) == NULL)
127 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
128 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
135 } while (buf == NULL);
137 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
138 rtm = (struct rt_msghdr *)(void *)next;
139 if (rtm->rtm_version != RTM_VERSION)
141 switch (rtm->rtm_type) {
143 ifm = (struct if_msghdrl *)(void *)rtm;
144 if (ifm->ifm_addrs & RTA_IFP) {
145 idx = ifm->ifm_index;
147 if_data = IF_MSGHDRL_IFM_DATA(ifm);
148 dcnt += if_data->ifi_datalen;
149 dl = (struct sockaddr_dl *)IF_MSGHDRL_RTA(ifm);
150 dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
152 ncnt += dl->sdl_nlen + 1;
158 ifam = (struct ifa_msghdrl *)(void *)rtm;
159 if (idx && ifam->ifam_index != idx)
160 abort(); /* this cannot happen */
162 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
163 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
165 p = (char *)IFA_MSGHDRL_RTA(ifam);
167 if_data = IFA_MSGHDRL_IFAM_DATA(ifam);
168 dcnt += if_data->ifi_datalen + ALIGNBYTES;
170 /* Scan to look for length of address */
172 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
173 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
176 sa = (struct sockaddr *)(void *)p;
184 for (p = p0, i = 0; i < RTAX_MAX; i++) {
185 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
188 sa = (struct sockaddr *)(void *)p;
190 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
200 if (icnt + dcnt + ncnt == 1) {
205 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
211 ifa = (struct ifaddrs *)(void *)data;
212 data += sizeof(struct ifaddrs) * icnt;
215 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
220 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
221 rtm = (struct rt_msghdr *)(void *)next;
222 if (rtm->rtm_version != RTM_VERSION)
224 switch (rtm->rtm_type) {
226 ifm = (struct if_msghdrl *)(void *)rtm;
227 if ((ifm->ifm_addrs & RTA_IFP) == 0) {
232 idx = ifm->ifm_index;
233 dl = (struct sockaddr_dl *)IF_MSGHDRL_RTA(ifm);
236 ift->ifa_name = names;
237 ift->ifa_flags = (int)ifm->ifm_flags;
238 memcpy(names, dl->sdl_data, (size_t)dl->sdl_nlen);
239 names[dl->sdl_nlen] = 0;
240 names += dl->sdl_nlen + 1;
242 ift->ifa_addr = (struct sockaddr *)(void *)data;
243 memcpy(data, dl, (size_t)SA_LEN((struct sockaddr *)
245 data += SA_RLEN((struct sockaddr *)(void *)dl);
247 if_data = IF_MSGHDRL_IFM_DATA(ifm);
248 /* ifm_data needs to be aligned */
249 ift->ifa_data = data = (void *)ALIGN(data);
250 memcpy(data, if_data, if_data->ifi_datalen);
251 data += if_data->ifi_datalen;
253 ift = (ift->ifa_next = ift + 1);
257 ifam = (struct ifa_msghdrl *)(void *)rtm;
258 if (idx && ifam->ifam_index != idx)
259 abort(); /* this cannot happen */
261 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
263 ift->ifa_name = cif->ifa_name;
264 ift->ifa_flags = cif->ifa_flags;
265 ift->ifa_data = NULL;
267 p = (char *)IFA_MSGHDRL_RTA(ifam);
268 /* Scan to look for length of address */
270 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
271 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
274 sa = (struct sockaddr *)(void *)p;
282 for (p = p0, i = 0; i < RTAX_MAX; i++) {
283 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
286 sa = (struct sockaddr *)(void *)p;
291 (struct sockaddr *)(void *)data;
292 memcpy(data, p, len);
298 (struct sockaddr *)(void *)data;
299 if (SA_LEN(sa) == 0) {
300 memset(data, 0, alen);
304 memcpy(data, p, len);
310 (struct sockaddr *)(void *)data;
311 memcpy(data, p, len);
318 if_data = IFA_MSGHDRL_IFAM_DATA(ifam);
319 /* ifam_data needs to be aligned */
320 ift->ifa_data = data = (void *)ALIGN(data);
321 memcpy(data, if_data, if_data->ifi_datalen);
322 data += if_data->ifi_datalen;
324 ift = (ift->ifa_next = ift + 1);
332 ift->ifa_next = NULL;
342 freeifaddrs(struct ifaddrs *ifp)