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