1*0a6a1f1dSLionel Sambuc /* $NetBSD: getifaddrs.c,v 1.15 2012/03/13 21:13:40 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc * Copyright (c) 1995, 1999
5*0a6a1f1dSLionel Sambuc * Berkeley Software Design, Inc. All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
8*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
9*0a6a1f1dSLionel Sambuc * are met:
10*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*0a6a1f1dSLionel Sambuc *
13*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND
14*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE
17*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
24*0a6a1f1dSLionel Sambuc *
25*0a6a1f1dSLionel Sambuc * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp
26*0a6a1f1dSLionel Sambuc */
27*0a6a1f1dSLionel Sambuc
28*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
29*0a6a1f1dSLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
30*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: getifaddrs.c,v 1.15 2012/03/13 21:13:40 christos Exp $");
31*0a6a1f1dSLionel Sambuc #endif /* LIBC_SCCS and not lint */
32*0a6a1f1dSLionel Sambuc
33*0a6a1f1dSLionel Sambuc #ifndef RUMP_ACTION
34*0a6a1f1dSLionel Sambuc #include "namespace.h"
35*0a6a1f1dSLionel Sambuc #endif
36*0a6a1f1dSLionel Sambuc #include <sys/types.h>
37*0a6a1f1dSLionel Sambuc #include <sys/ioctl.h>
38*0a6a1f1dSLionel Sambuc #include <sys/socket.h>
39*0a6a1f1dSLionel Sambuc #include <net/if.h>
40*0a6a1f1dSLionel Sambuc #include <sys/param.h>
41*0a6a1f1dSLionel Sambuc #include <net/route.h>
42*0a6a1f1dSLionel Sambuc #include <sys/sysctl.h>
43*0a6a1f1dSLionel Sambuc #include <net/if_dl.h>
44*0a6a1f1dSLionel Sambuc
45*0a6a1f1dSLionel Sambuc #include <assert.h>
46*0a6a1f1dSLionel Sambuc #include <errno.h>
47*0a6a1f1dSLionel Sambuc #include <ifaddrs.h>
48*0a6a1f1dSLionel Sambuc #include <stdlib.h>
49*0a6a1f1dSLionel Sambuc #include <string.h>
50*0a6a1f1dSLionel Sambuc
51*0a6a1f1dSLionel Sambuc #if defined(__weak_alias) && !defined(RUMP_ACTION)
__weak_alias(getifaddrs,_getifaddrs)52*0a6a1f1dSLionel Sambuc __weak_alias(getifaddrs,_getifaddrs)
53*0a6a1f1dSLionel Sambuc __weak_alias(freeifaddrs,_freeifaddrs)
54*0a6a1f1dSLionel Sambuc #endif
55*0a6a1f1dSLionel Sambuc
56*0a6a1f1dSLionel Sambuc #ifdef RUMP_ACTION
57*0a6a1f1dSLionel Sambuc #include <rump/rump_syscalls.h>
58*0a6a1f1dSLionel Sambuc #define sysctl(a,b,c,d,e,f) rump_sys___sysctl(a,b,c,d,e,f)
59*0a6a1f1dSLionel Sambuc #endif
60*0a6a1f1dSLionel Sambuc
61*0a6a1f1dSLionel Sambuc #define SA_RLEN(sa) RT_ROUNDUP((sa)->sa_len)
62*0a6a1f1dSLionel Sambuc
63*0a6a1f1dSLionel Sambuc int
64*0a6a1f1dSLionel Sambuc getifaddrs(struct ifaddrs **pif)
65*0a6a1f1dSLionel Sambuc {
66*0a6a1f1dSLionel Sambuc size_t icnt = 1;
67*0a6a1f1dSLionel Sambuc size_t dcnt = 0;
68*0a6a1f1dSLionel Sambuc size_t ncnt = 0;
69*0a6a1f1dSLionel Sambuc static const int mib[] = {
70*0a6a1f1dSLionel Sambuc CTL_NET,
71*0a6a1f1dSLionel Sambuc PF_ROUTE,
72*0a6a1f1dSLionel Sambuc 0, /* protocol */
73*0a6a1f1dSLionel Sambuc 0, /* wildcard address family */
74*0a6a1f1dSLionel Sambuc NET_RT_IFLIST,
75*0a6a1f1dSLionel Sambuc 0 /* no flags */
76*0a6a1f1dSLionel Sambuc };
77*0a6a1f1dSLionel Sambuc size_t needed;
78*0a6a1f1dSLionel Sambuc char *buf;
79*0a6a1f1dSLionel Sambuc char *next;
80*0a6a1f1dSLionel Sambuc struct ifaddrs cif;
81*0a6a1f1dSLionel Sambuc char *p, *p0;
82*0a6a1f1dSLionel Sambuc struct rt_msghdr *rtm;
83*0a6a1f1dSLionel Sambuc struct if_msghdr *ifm;
84*0a6a1f1dSLionel Sambuc struct ifa_msghdr *ifam;
85*0a6a1f1dSLionel Sambuc struct sockaddr *sa;
86*0a6a1f1dSLionel Sambuc struct ifaddrs *ifa, *ift;
87*0a6a1f1dSLionel Sambuc u_short idx = 0;
88*0a6a1f1dSLionel Sambuc int i;
89*0a6a1f1dSLionel Sambuc size_t len, alen;
90*0a6a1f1dSLionel Sambuc char *data;
91*0a6a1f1dSLionel Sambuc char *names;
92*0a6a1f1dSLionel Sambuc
93*0a6a1f1dSLionel Sambuc _DIAGASSERT(pif != NULL);
94*0a6a1f1dSLionel Sambuc
95*0a6a1f1dSLionel Sambuc if (sysctl(mib, (u_int)__arraycount(mib), NULL, &needed, NULL, 0) < 0)
96*0a6a1f1dSLionel Sambuc return (-1);
97*0a6a1f1dSLionel Sambuc if ((buf = malloc(needed)) == NULL)
98*0a6a1f1dSLionel Sambuc return (-1);
99*0a6a1f1dSLionel Sambuc if (sysctl(mib, (u_int)__arraycount(mib), buf, &needed, NULL, 0) < 0) {
100*0a6a1f1dSLionel Sambuc free(buf);
101*0a6a1f1dSLionel Sambuc return (-1);
102*0a6a1f1dSLionel Sambuc }
103*0a6a1f1dSLionel Sambuc
104*0a6a1f1dSLionel Sambuc for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
105*0a6a1f1dSLionel Sambuc rtm = (struct rt_msghdr *)(void *)next;
106*0a6a1f1dSLionel Sambuc if (rtm->rtm_version != RTM_VERSION)
107*0a6a1f1dSLionel Sambuc continue;
108*0a6a1f1dSLionel Sambuc switch (rtm->rtm_type) {
109*0a6a1f1dSLionel Sambuc case RTM_IFINFO:
110*0a6a1f1dSLionel Sambuc ifm = (struct if_msghdr *)(void *)rtm;
111*0a6a1f1dSLionel Sambuc if (ifm->ifm_addrs & RTA_IFP) {
112*0a6a1f1dSLionel Sambuc const struct sockaddr_dl *dl;
113*0a6a1f1dSLionel Sambuc
114*0a6a1f1dSLionel Sambuc idx = ifm->ifm_index;
115*0a6a1f1dSLionel Sambuc ++icnt;
116*0a6a1f1dSLionel Sambuc dl = (struct sockaddr_dl *)(void *)(ifm + 1);
117*0a6a1f1dSLionel Sambuc dcnt += SA_RLEN((const struct sockaddr *)(const void *)dl) +
118*0a6a1f1dSLionel Sambuc ALIGNBYTES;
119*0a6a1f1dSLionel Sambuc dcnt += sizeof(ifm->ifm_data);
120*0a6a1f1dSLionel Sambuc ncnt += dl->sdl_nlen + 1;
121*0a6a1f1dSLionel Sambuc } else
122*0a6a1f1dSLionel Sambuc idx = 0;
123*0a6a1f1dSLionel Sambuc break;
124*0a6a1f1dSLionel Sambuc
125*0a6a1f1dSLionel Sambuc case RTM_NEWADDR:
126*0a6a1f1dSLionel Sambuc ifam = (struct ifa_msghdr *)(void *)rtm;
127*0a6a1f1dSLionel Sambuc if (idx && ifam->ifam_index != idx)
128*0a6a1f1dSLionel Sambuc abort(); /* this cannot happen */
129*0a6a1f1dSLionel Sambuc
130*0a6a1f1dSLionel Sambuc #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD)
131*0a6a1f1dSLionel Sambuc if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
132*0a6a1f1dSLionel Sambuc break;
133*0a6a1f1dSLionel Sambuc p = (char *)(void *)(ifam + 1);
134*0a6a1f1dSLionel Sambuc ++icnt;
135*0a6a1f1dSLionel Sambuc /* Scan to look for length of address */
136*0a6a1f1dSLionel Sambuc alen = 0;
137*0a6a1f1dSLionel Sambuc for (p0 = p, i = 0; i < RTAX_MAX; i++) {
138*0a6a1f1dSLionel Sambuc if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
139*0a6a1f1dSLionel Sambuc == 0)
140*0a6a1f1dSLionel Sambuc continue;
141*0a6a1f1dSLionel Sambuc sa = (struct sockaddr *)(void *)p;
142*0a6a1f1dSLionel Sambuc len = SA_RLEN(sa);
143*0a6a1f1dSLionel Sambuc if (i == RTAX_IFA) {
144*0a6a1f1dSLionel Sambuc alen = len;
145*0a6a1f1dSLionel Sambuc break;
146*0a6a1f1dSLionel Sambuc }
147*0a6a1f1dSLionel Sambuc p += len;
148*0a6a1f1dSLionel Sambuc }
149*0a6a1f1dSLionel Sambuc for (p = p0, i = 0; i < RTAX_MAX; i++) {
150*0a6a1f1dSLionel Sambuc if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
151*0a6a1f1dSLionel Sambuc == 0)
152*0a6a1f1dSLionel Sambuc continue;
153*0a6a1f1dSLionel Sambuc sa = (struct sockaddr *)(void *)p;
154*0a6a1f1dSLionel Sambuc len = SA_RLEN(sa);
155*0a6a1f1dSLionel Sambuc if (i == RTAX_NETMASK && sa->sa_len == 0)
156*0a6a1f1dSLionel Sambuc dcnt += alen;
157*0a6a1f1dSLionel Sambuc else
158*0a6a1f1dSLionel Sambuc dcnt += len;
159*0a6a1f1dSLionel Sambuc p += len;
160*0a6a1f1dSLionel Sambuc }
161*0a6a1f1dSLionel Sambuc break;
162*0a6a1f1dSLionel Sambuc }
163*0a6a1f1dSLionel Sambuc }
164*0a6a1f1dSLionel Sambuc
165*0a6a1f1dSLionel Sambuc if (icnt + dcnt + ncnt == 1) {
166*0a6a1f1dSLionel Sambuc *pif = NULL;
167*0a6a1f1dSLionel Sambuc free(buf);
168*0a6a1f1dSLionel Sambuc return (0);
169*0a6a1f1dSLionel Sambuc }
170*0a6a1f1dSLionel Sambuc data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt);
171*0a6a1f1dSLionel Sambuc if (data == NULL) {
172*0a6a1f1dSLionel Sambuc free(buf);
173*0a6a1f1dSLionel Sambuc return(-1);
174*0a6a1f1dSLionel Sambuc }
175*0a6a1f1dSLionel Sambuc
176*0a6a1f1dSLionel Sambuc ifa = (struct ifaddrs *)(void *)data;
177*0a6a1f1dSLionel Sambuc data += sizeof(struct ifaddrs) * icnt;
178*0a6a1f1dSLionel Sambuc names = data + dcnt;
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
181*0a6a1f1dSLionel Sambuc ift = ifa;
182*0a6a1f1dSLionel Sambuc
183*0a6a1f1dSLionel Sambuc idx = 0;
184*0a6a1f1dSLionel Sambuc for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
185*0a6a1f1dSLionel Sambuc rtm = (struct rt_msghdr *)(void *)next;
186*0a6a1f1dSLionel Sambuc if (rtm->rtm_version != RTM_VERSION)
187*0a6a1f1dSLionel Sambuc continue;
188*0a6a1f1dSLionel Sambuc switch (rtm->rtm_type) {
189*0a6a1f1dSLionel Sambuc case RTM_IFINFO:
190*0a6a1f1dSLionel Sambuc ifm = (struct if_msghdr *)(void *)rtm;
191*0a6a1f1dSLionel Sambuc if (ifm->ifm_addrs & RTA_IFP) {
192*0a6a1f1dSLionel Sambuc const struct sockaddr_dl *dl;
193*0a6a1f1dSLionel Sambuc
194*0a6a1f1dSLionel Sambuc idx = ifm->ifm_index;
195*0a6a1f1dSLionel Sambuc dl = (struct sockaddr_dl *)(void *)(ifm + 1);
196*0a6a1f1dSLionel Sambuc
197*0a6a1f1dSLionel Sambuc memset(&cif, 0, sizeof(cif));
198*0a6a1f1dSLionel Sambuc
199*0a6a1f1dSLionel Sambuc cif.ifa_name = names;
200*0a6a1f1dSLionel Sambuc cif.ifa_flags = (int)ifm->ifm_flags;
201*0a6a1f1dSLionel Sambuc memcpy(names, dl->sdl_data,
202*0a6a1f1dSLionel Sambuc (size_t)dl->sdl_nlen);
203*0a6a1f1dSLionel Sambuc names[dl->sdl_nlen] = 0;
204*0a6a1f1dSLionel Sambuc names += dl->sdl_nlen + 1;
205*0a6a1f1dSLionel Sambuc
206*0a6a1f1dSLionel Sambuc cif.ifa_addr = (struct sockaddr *)(void *)data;
207*0a6a1f1dSLionel Sambuc memcpy(data, dl, (size_t)dl->sdl_len);
208*0a6a1f1dSLionel Sambuc data += SA_RLEN((const struct sockaddr *)(const void *)dl);
209*0a6a1f1dSLionel Sambuc
210*0a6a1f1dSLionel Sambuc /* ifm_data needs to be aligned */
211*0a6a1f1dSLionel Sambuc cif.ifa_data = data = (void *)ALIGN(data);
212*0a6a1f1dSLionel Sambuc memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
213*0a6a1f1dSLionel Sambuc data += sizeof(ifm->ifm_data);
214*0a6a1f1dSLionel Sambuc } else
215*0a6a1f1dSLionel Sambuc idx = 0;
216*0a6a1f1dSLionel Sambuc break;
217*0a6a1f1dSLionel Sambuc
218*0a6a1f1dSLionel Sambuc case RTM_NEWADDR:
219*0a6a1f1dSLionel Sambuc ifam = (struct ifa_msghdr *)(void *)rtm;
220*0a6a1f1dSLionel Sambuc if (idx && ifam->ifam_index != idx)
221*0a6a1f1dSLionel Sambuc abort(); /* this cannot happen */
222*0a6a1f1dSLionel Sambuc
223*0a6a1f1dSLionel Sambuc if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
224*0a6a1f1dSLionel Sambuc break;
225*0a6a1f1dSLionel Sambuc ift->ifa_name = cif.ifa_name;
226*0a6a1f1dSLionel Sambuc ift->ifa_flags = cif.ifa_flags;
227*0a6a1f1dSLionel Sambuc ift->ifa_data = NULL;
228*0a6a1f1dSLionel Sambuc p = (char *)(void *)(ifam + 1);
229*0a6a1f1dSLionel Sambuc /* Scan to look for length of address */
230*0a6a1f1dSLionel Sambuc alen = 0;
231*0a6a1f1dSLionel Sambuc for (p0 = p, i = 0; i < RTAX_MAX; i++) {
232*0a6a1f1dSLionel Sambuc if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
233*0a6a1f1dSLionel Sambuc == 0)
234*0a6a1f1dSLionel Sambuc continue;
235*0a6a1f1dSLionel Sambuc sa = (struct sockaddr *)(void *)p;
236*0a6a1f1dSLionel Sambuc len = SA_RLEN(sa);
237*0a6a1f1dSLionel Sambuc if (i == RTAX_IFA) {
238*0a6a1f1dSLionel Sambuc alen = len;
239*0a6a1f1dSLionel Sambuc break;
240*0a6a1f1dSLionel Sambuc }
241*0a6a1f1dSLionel Sambuc p += len;
242*0a6a1f1dSLionel Sambuc }
243*0a6a1f1dSLionel Sambuc for (p = p0, i = 0; i < RTAX_MAX; i++) {
244*0a6a1f1dSLionel Sambuc if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
245*0a6a1f1dSLionel Sambuc == 0)
246*0a6a1f1dSLionel Sambuc continue;
247*0a6a1f1dSLionel Sambuc sa = (struct sockaddr *)(void *)p;
248*0a6a1f1dSLionel Sambuc len = SA_RLEN(sa);
249*0a6a1f1dSLionel Sambuc switch (i) {
250*0a6a1f1dSLionel Sambuc case RTAX_IFA:
251*0a6a1f1dSLionel Sambuc ift->ifa_addr =
252*0a6a1f1dSLionel Sambuc (struct sockaddr *)(void *)data;
253*0a6a1f1dSLionel Sambuc memcpy(data, p, len);
254*0a6a1f1dSLionel Sambuc data += len;
255*0a6a1f1dSLionel Sambuc if (ift->ifa_addr->sa_family == AF_LINK)
256*0a6a1f1dSLionel Sambuc ift->ifa_data = cif.ifa_data;
257*0a6a1f1dSLionel Sambuc break;
258*0a6a1f1dSLionel Sambuc
259*0a6a1f1dSLionel Sambuc case RTAX_NETMASK:
260*0a6a1f1dSLionel Sambuc ift->ifa_netmask =
261*0a6a1f1dSLionel Sambuc (struct sockaddr *)(void *)data;
262*0a6a1f1dSLionel Sambuc if (sa->sa_len == 0) {
263*0a6a1f1dSLionel Sambuc memset(data, 0, alen);
264*0a6a1f1dSLionel Sambuc data += alen;
265*0a6a1f1dSLionel Sambuc break;
266*0a6a1f1dSLionel Sambuc }
267*0a6a1f1dSLionel Sambuc memcpy(data, p, len);
268*0a6a1f1dSLionel Sambuc data += len;
269*0a6a1f1dSLionel Sambuc break;
270*0a6a1f1dSLionel Sambuc
271*0a6a1f1dSLionel Sambuc case RTAX_BRD:
272*0a6a1f1dSLionel Sambuc ift->ifa_broadaddr =
273*0a6a1f1dSLionel Sambuc (struct sockaddr *)(void *)data;
274*0a6a1f1dSLionel Sambuc memcpy(data, p, len);
275*0a6a1f1dSLionel Sambuc data += len;
276*0a6a1f1dSLionel Sambuc break;
277*0a6a1f1dSLionel Sambuc }
278*0a6a1f1dSLionel Sambuc p += len;
279*0a6a1f1dSLionel Sambuc }
280*0a6a1f1dSLionel Sambuc
281*0a6a1f1dSLionel Sambuc
282*0a6a1f1dSLionel Sambuc ift = (ift->ifa_next = ift + 1);
283*0a6a1f1dSLionel Sambuc break;
284*0a6a1f1dSLionel Sambuc }
285*0a6a1f1dSLionel Sambuc }
286*0a6a1f1dSLionel Sambuc
287*0a6a1f1dSLionel Sambuc free(buf);
288*0a6a1f1dSLionel Sambuc if (--ift >= ifa) {
289*0a6a1f1dSLionel Sambuc ift->ifa_next = NULL;
290*0a6a1f1dSLionel Sambuc *pif = ifa;
291*0a6a1f1dSLionel Sambuc } else {
292*0a6a1f1dSLionel Sambuc *pif = NULL;
293*0a6a1f1dSLionel Sambuc free(ifa);
294*0a6a1f1dSLionel Sambuc }
295*0a6a1f1dSLionel Sambuc return (0);
296*0a6a1f1dSLionel Sambuc }
297*0a6a1f1dSLionel Sambuc
298*0a6a1f1dSLionel Sambuc void
freeifaddrs(struct ifaddrs * ifp)299*0a6a1f1dSLionel Sambuc freeifaddrs(struct ifaddrs *ifp)
300*0a6a1f1dSLionel Sambuc {
301*0a6a1f1dSLionel Sambuc
302*0a6a1f1dSLionel Sambuc _DIAGASSERT(ifp != NULL);
303*0a6a1f1dSLionel Sambuc
304*0a6a1f1dSLionel Sambuc free(ifp);
305*0a6a1f1dSLionel Sambuc }
306