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 "namespace.h"
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
40 #include <sys/param.h>
41 #include <net/route.h>
42 #include <sys/sysctl.h>
43 #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 #define MAX_SYSCTL_TRY 5
77 getifaddrs(struct ifaddrs **pif)
89 struct rt_msghdr *rtm;
90 struct if_msghdrl *ifm;
91 struct ifa_msghdrl *ifam;
92 struct sockaddr_dl *dl;
94 struct ifaddrs *ifa, *ift;
95 struct if_data *if_data;
104 mib[2] = 0; /* protocol */
105 mib[3] = 0; /* wildcard address family */
106 mib[4] = NET_RT_IFLISTL;/* extra fields for extensible msghdr structs */
107 mib[5] = 0; /* no flags */
110 * We'll try to get addresses several times in case that
111 * the number of addresses is unexpectedly increased during
112 * the two sysctl calls. This should rarely happen, but we'll
113 * try to do our best for applications that assume success of
114 * this library (which should usually be the case).
115 * Portability note: since FreeBSD does not add margin of
116 * memory at the first sysctl, the possibility of failure on
117 * the second sysctl call is a bit higher.
120 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
122 if ((buf = malloc(needed)) == NULL)
124 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
125 if (errno != ENOMEM || ++ntry >= MAX_SYSCTL_TRY) {
132 } while (buf == NULL);
134 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
135 rtm = (struct rt_msghdr *)(void *)next;
136 if (rtm->rtm_version != RTM_VERSION)
138 switch (rtm->rtm_type) {
140 ifm = (struct if_msghdrl *)(void *)rtm;
141 if (ifm->ifm_addrs & RTA_IFP) {
142 idx = ifm->ifm_index;
144 if_data = IF_MSGHDRL_IFM_DATA(ifm);
145 dcnt += if_data->ifi_datalen;
146 dl = (struct sockaddr_dl *)IF_MSGHDRL_RTA(ifm);
147 dcnt += SA_RLEN((struct sockaddr *)(void*)dl) +
149 ncnt += dl->sdl_nlen + 1;
155 ifam = (struct ifa_msghdrl *)(void *)rtm;
156 if (idx && ifam->ifam_index != idx)
157 abort(); /* this cannot happen */
159 #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
160 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
162 p = (char *)IFA_MSGHDRL_RTA(ifam);
164 if_data = IFA_MSGHDRL_IFAM_DATA(ifam);
165 dcnt += if_data->ifi_datalen + ALIGNBYTES;
167 /* Scan to look for length of address */
169 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
170 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
173 sa = (struct sockaddr *)(void *)p;
181 for (p = p0, i = 0; i < RTAX_MAX; i++) {
182 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
185 sa = (struct sockaddr *)(void *)p;
187 if (i == RTAX_NETMASK && SA_LEN(sa) == 0)
197 if (icnt + dcnt + ncnt == 1) {
202 data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
208 ifa = (struct ifaddrs *)(void *)data;
209 data += sizeof(struct ifaddrs) * icnt;
212 memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
217 for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
218 rtm = (struct rt_msghdr *)(void *)next;
219 if (rtm->rtm_version != RTM_VERSION)
221 switch (rtm->rtm_type) {
223 ifm = (struct if_msghdrl *)(void *)rtm;
224 if ((ifm->ifm_addrs & RTA_IFP) == 0) {
229 idx = ifm->ifm_index;
230 dl = (struct sockaddr_dl *)IF_MSGHDRL_RTA(ifm);
233 ift->ifa_name = names;
234 ift->ifa_flags = (int)ifm->ifm_flags;
235 memcpy(names, dl->sdl_data, (size_t)dl->sdl_nlen);
236 names[dl->sdl_nlen] = 0;
237 names += dl->sdl_nlen + 1;
239 ift->ifa_addr = (struct sockaddr *)(void *)data;
240 memcpy(data, dl, (size_t)SA_LEN((struct sockaddr *)
242 data += SA_RLEN((struct sockaddr *)(void *)dl);
244 if_data = IF_MSGHDRL_IFM_DATA(ifm);
245 /* ifm_data needs to be aligned */
246 ift->ifa_data = data = (void *)ALIGN(data);
247 memcpy(data, if_data, if_data->ifi_datalen);
248 data += if_data->ifi_datalen;
250 ift = (ift->ifa_next = ift + 1);
254 ifam = (struct ifa_msghdrl *)(void *)rtm;
255 if (idx && ifam->ifam_index != idx)
256 abort(); /* this cannot happen */
258 if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
260 ift->ifa_name = cif->ifa_name;
261 ift->ifa_flags = cif->ifa_flags;
262 ift->ifa_data = NULL;
264 p = (char *)IFA_MSGHDRL_RTA(ifam);
265 /* Scan to look for length of address */
267 for (p0 = p, i = 0; i < RTAX_MAX; i++) {
268 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
271 sa = (struct sockaddr *)(void *)p;
279 for (p = p0, i = 0; i < RTAX_MAX; i++) {
280 if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
283 sa = (struct sockaddr *)(void *)p;
288 (struct sockaddr *)(void *)data;
289 memcpy(data, p, len);
295 (struct sockaddr *)(void *)data;
296 if (SA_LEN(sa) == 0) {
297 memset(data, 0, alen);
301 memcpy(data, p, len);
307 (struct sockaddr *)(void *)data;
308 memcpy(data, p, len);
315 if_data = IFA_MSGHDRL_IFAM_DATA(ifam);
316 /* ifam_data needs to be aligned */
317 ift->ifa_data = data = (void *)ALIGN(data);
318 memcpy(data, if_data, if_data->ifi_datalen);
319 data += if_data->ifi_datalen;
321 ift = (ift->ifa_next = ift + 1);
329 ift->ifa_next = NULL;
339 freeifaddrs(struct ifaddrs *ifp)