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