Home | History | Annotate | Line # | Download | only in net
getifaddrs.c revision 1.11.20.3
      1  1.11.20.3      matt /*	$NetBSD: getifaddrs.c,v 1.11.20.3 2010/05/13 05:38:16 matt 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.11.20.3      matt __RCSID("$NetBSD: getifaddrs.c,v 1.11.20.3 2010/05/13 05:38:16 matt Exp $");
     31        1.4    itojun #endif /* LIBC_SCCS and not lint */
     32        1.4    itojun 
     33        1.3    itojun #include "namespace.h"
     34        1.1    itojun #include <sys/types.h>
     35        1.1    itojun #include <sys/ioctl.h>
     36        1.1    itojun #include <sys/socket.h>
     37        1.1    itojun #include <net/if.h>
     38        1.1    itojun #include <sys/param.h>
     39        1.1    itojun #include <net/route.h>
     40        1.1    itojun #include <sys/sysctl.h>
     41        1.1    itojun #include <net/if_dl.h>
     42        1.1    itojun 
     43        1.7     lukem #include <assert.h>
     44        1.1    itojun #include <errno.h>
     45        1.1    itojun #include <ifaddrs.h>
     46        1.1    itojun #include <stdlib.h>
     47        1.1    itojun #include <string.h>
     48  1.11.20.3      matt 
     49  1.11.20.3      matt #ifdef RTDEBUG
     50  1.11.20.3      matt #include <stdio.h>
     51  1.11.20.3      matt #endif
     52        1.3    itojun 
     53        1.3    itojun #ifdef __weak_alias
     54        1.3    itojun __weak_alias(getifaddrs,_getifaddrs)
     55        1.3    itojun __weak_alias(freeifaddrs,_freeifaddrs)
     56        1.3    itojun #endif
     57        1.1    itojun 
     58  1.11.20.2      matt #define	SA_RLEN(sa)	RT_ROUNDUP((sa)->sa_len)
     59        1.1    itojun 
     60        1.1    itojun int
     61        1.1    itojun getifaddrs(struct ifaddrs **pif)
     62        1.1    itojun {
     63        1.1    itojun 	int icnt = 1;
     64        1.1    itojun 	int dcnt = 0;
     65        1.1    itojun 	int ncnt = 0;
     66        1.1    itojun 	int mib[6];
     67        1.1    itojun 	size_t needed;
     68        1.1    itojun 	char *buf;
     69        1.1    itojun 	char *next;
     70  1.11.20.1      matt 	struct ifaddrs cif;
     71        1.1    itojun 	char *p, *p0;
     72        1.1    itojun 	struct rt_msghdr *rtm;
     73        1.1    itojun 	struct if_msghdr *ifm;
     74        1.1    itojun 	struct ifa_msghdr *ifam;
     75        1.1    itojun 	struct sockaddr *sa;
     76        1.1    itojun 	struct ifaddrs *ifa, *ift;
     77        1.5  christos 	u_short idx = 0;
     78        1.1    itojun 	int i;
     79        1.1    itojun 	size_t len, alen;
     80        1.1    itojun 	char *data;
     81        1.1    itojun 	char *names;
     82        1.1    itojun 
     83  1.11.20.3      matt #ifdef RTDEBUG
     84  1.11.20.3      matt 	static const char rtax_names[][8] = RTAX_NAMES;
     85  1.11.20.3      matt #endif
     86  1.11.20.3      matt 
     87        1.7     lukem 	_DIAGASSERT(pif != NULL);
     88        1.7     lukem 
     89        1.1    itojun 	mib[0] = CTL_NET;
     90        1.1    itojun 	mib[1] = PF_ROUTE;
     91        1.1    itojun 	mib[2] = 0;             /* protocol */
     92        1.1    itojun 	mib[3] = 0;             /* wildcard address family */
     93        1.1    itojun 	mib[4] = NET_RT_IFLIST;
     94        1.1    itojun 	mib[5] = 0;             /* no flags */
     95       1.11    dyoung 	if (sysctl(mib, __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.11    dyoung 	if (sysctl(mib, __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.11.20.3      matt #ifdef RTDEBUG
    109  1.11.20.3      matt 		printf("%d: %zd rtm len=%d type=%d\n", __LINE__,
    110  1.11.20.3      matt 			(char *)rtm - buf,
    111  1.11.20.3      matt 			rtm->rtm_msglen, rtm->rtm_type);
    112  1.11.20.3      matt #endif
    113        1.1    itojun 		switch (rtm->rtm_type) {
    114        1.1    itojun 		case RTM_IFINFO:
    115        1.5  christos 			ifm = (struct if_msghdr *)(void *)rtm;
    116        1.1    itojun 			if (ifm->ifm_addrs & RTA_IFP) {
    117  1.11.20.1      matt 				const struct sockaddr_dl *dl;
    118  1.11.20.1      matt 
    119        1.5  christos 				idx = ifm->ifm_index;
    120        1.1    itojun 				++icnt;
    121  1.11.20.3      matt 				dl = (struct sockaddr_dl *)
    122  1.11.20.3      matt 				    ((uintptr_t)ifm + RT_ROUNDUP(sizeof(*ifm)));
    123  1.11.20.2      matt 				dcnt += SA_RLEN((const struct sockaddr *)(const void *)dl);
    124  1.11.20.3      matt 				dcnt += RT_ROUNDUP(sizeof(ifm->ifm_data));
    125        1.1    itojun 				ncnt += dl->sdl_nlen + 1;
    126  1.11.20.3      matt #ifdef RTDEBUG
    127  1.11.20.3      matt 				printf("%d: %zd sdl idx=%d dcnt=%#x flags=%#x name=",
    128  1.11.20.3      matt 					__LINE__, (const char *)dl - buf, idx,
    129  1.11.20.3      matt 					dcnt, ifm->ifm_flags);
    130  1.11.20.3      matt 				fwrite(dl->sdl_data, 1, dl->sdl_nlen, stdout);
    131  1.11.20.3      matt 				putchar('\n');
    132  1.11.20.3      matt #endif
    133        1.1    itojun 			} else
    134        1.5  christos 				idx = 0;
    135        1.1    itojun 			break;
    136        1.1    itojun 
    137        1.1    itojun 		case RTM_NEWADDR:
    138        1.5  christos 			ifam = (struct ifa_msghdr *)(void *)rtm;
    139        1.5  christos 			if (idx && ifam->ifam_index != idx)
    140        1.1    itojun 				abort();	/* this cannot happen */
    141        1.1    itojun 
    142        1.1    itojun #define	RTA_MASKS	(RTA_NETMASK | RTA_IFA | RTA_BRD)
    143        1.5  christos 			if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
    144        1.1    itojun 				break;
    145  1.11.20.3      matt 			p = (char *)
    146  1.11.20.3      matt 			    ((uintptr_t)ifam + RT_ROUNDUP(sizeof(*ifam)));
    147        1.1    itojun 			++icnt;
    148  1.11.20.3      matt #ifdef RTDEBUG
    149  1.11.20.3      matt 			printf("%d: %zd sa index=%d addrs=%#x\n",
    150  1.11.20.3      matt 				__LINE__, p - buf,
    151  1.11.20.3      matt 				ifam->ifam_index, ifam->ifam_addrs);
    152  1.11.20.3      matt #endif
    153        1.1    itojun 			/* Scan to look for length of address */
    154        1.1    itojun 			alen = 0;
    155        1.1    itojun 			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
    156        1.1    itojun 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
    157        1.1    itojun 				    == 0)
    158        1.1    itojun 					continue;
    159        1.5  christos 				sa = (struct sockaddr *)(void *)p;
    160        1.1    itojun 				len = SA_RLEN(sa);
    161  1.11.20.3      matt #ifdef RTDEBUG
    162  1.11.20.3      matt 				printf("%d: %zd sa %s len=%d family=%d\n",
    163  1.11.20.3      matt 					__LINE__, p - buf, rtax_names[i],
    164  1.11.20.3      matt 					sa->sa_len, sa->sa_family);
    165  1.11.20.3      matt #endif
    166        1.1    itojun 				if (i == RTAX_IFA) {
    167        1.1    itojun 					alen = len;
    168        1.1    itojun 					break;
    169        1.1    itojun 				}
    170        1.1    itojun 				p += len;
    171        1.1    itojun 			}
    172        1.1    itojun 			for (p = p0, i = 0; i < RTAX_MAX; i++) {
    173        1.1    itojun 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
    174        1.1    itojun 				    == 0)
    175        1.1    itojun 					continue;
    176        1.5  christos 				sa = (struct sockaddr *)(void *)p;
    177        1.1    itojun 				len = SA_RLEN(sa);
    178        1.9    itojun 				if (i == RTAX_NETMASK && sa->sa_len == 0)
    179        1.1    itojun 					dcnt += alen;
    180        1.1    itojun 				else
    181        1.1    itojun 					dcnt += len;
    182  1.11.20.3      matt #ifdef RTDEBUG
    183  1.11.20.3      matt 				printf("%d: %zd sa %s dcnt=%#x, len=%d family=%d\n",
    184  1.11.20.3      matt 					__LINE__, p - buf, rtax_names[i], dcnt,
    185  1.11.20.3      matt 					sa->sa_len, sa->sa_family);
    186  1.11.20.3      matt #endif
    187        1.1    itojun 				p += len;
    188        1.1    itojun 			}
    189        1.1    itojun 			break;
    190        1.1    itojun 		}
    191        1.1    itojun 	}
    192        1.1    itojun 
    193        1.1    itojun 	if (icnt + dcnt + ncnt == 1) {
    194        1.1    itojun 		*pif = NULL;
    195        1.1    itojun 		free(buf);
    196        1.1    itojun 		return (0);
    197        1.1    itojun 	}
    198  1.11.20.3      matt 	data = malloc(RT_ROUNDUP(sizeof(struct ifaddrs) * icnt) + dcnt + ncnt);
    199        1.1    itojun 	if (data == NULL) {
    200        1.1    itojun 		free(buf);
    201        1.1    itojun 		return(-1);
    202        1.1    itojun 	}
    203        1.1    itojun 
    204        1.5  christos 	ifa = (struct ifaddrs *)(void *)data;
    205  1.11.20.3      matt 	data += RT_ROUNDUP(sizeof(struct ifaddrs) * icnt);
    206        1.1    itojun 	names = data + dcnt;
    207        1.1    itojun 
    208        1.1    itojun 	memset(ifa, 0, sizeof(struct ifaddrs) * icnt);
    209        1.1    itojun 	ift = ifa;
    210  1.11.20.3      matt #ifdef RTDEBUG
    211  1.11.20.3      matt 	printf("ifa=%p data=%p names=%p end=%p\n",
    212  1.11.20.3      matt 	    ifa, data, names, names + ncnt);
    213  1.11.20.3      matt #endif
    214        1.1    itojun 
    215        1.5  christos 	idx = 0;
    216        1.1    itojun 	for (next = buf; next < buf + needed; next += rtm->rtm_msglen) {
    217        1.5  christos 		rtm = (struct rt_msghdr *)(void *)next;
    218        1.1    itojun 		if (rtm->rtm_version != RTM_VERSION)
    219        1.1    itojun 			continue;
    220  1.11.20.3      matt #ifdef RTDEBUG
    221  1.11.20.3      matt 		printf("%d: %zd rtm len=%d type=%d\n", __LINE__,
    222  1.11.20.3      matt 			(char *)rtm - buf,
    223  1.11.20.3      matt 			rtm->rtm_msglen, rtm->rtm_type);
    224  1.11.20.3      matt #endif
    225        1.1    itojun 		switch (rtm->rtm_type) {
    226        1.1    itojun 		case RTM_IFINFO:
    227        1.5  christos 			ifm = (struct if_msghdr *)(void *)rtm;
    228        1.1    itojun 			if (ifm->ifm_addrs & RTA_IFP) {
    229  1.11.20.1      matt 				const struct sockaddr_dl *dl;
    230  1.11.20.1      matt 
    231        1.5  christos 				idx = ifm->ifm_index;
    232  1.11.20.3      matt 				dl = (struct sockaddr_dl *)
    233  1.11.20.3      matt 				    ((uintptr_t)ifm + RT_ROUNDUP(sizeof(*ifm)));
    234        1.1    itojun 
    235  1.11.20.1      matt 				memset(&cif, 0, sizeof(cif));
    236  1.11.20.1      matt 
    237  1.11.20.1      matt 				cif.ifa_name = names;
    238  1.11.20.1      matt 				cif.ifa_flags = (int)ifm->ifm_flags;
    239        1.5  christos 				memcpy(names, dl->sdl_data,
    240        1.5  christos 				    (size_t)dl->sdl_nlen);
    241        1.1    itojun 				names[dl->sdl_nlen] = 0;
    242  1.11.20.3      matt 
    243  1.11.20.3      matt #ifdef RTDEBUG
    244  1.11.20.3      matt 				printf("%d: %zd sdl idx=%d flags=%#x name=%s\n",
    245  1.11.20.3      matt 					__LINE__, (const char *)dl - buf, idx,
    246  1.11.20.3      matt 					ifm->ifm_flags, names);
    247  1.11.20.3      matt #endif
    248        1.1    itojun 				names += dl->sdl_nlen + 1;
    249        1.1    itojun 
    250  1.11.20.1      matt 				cif.ifa_addr = (struct sockaddr *)(void *)data;
    251  1.11.20.1      matt 				memcpy(data, dl, (size_t)dl->sdl_len);
    252  1.11.20.1      matt 				data += SA_RLEN((const struct sockaddr *)(const void *)dl);
    253        1.1    itojun 
    254        1.1    itojun 				/* ifm_data needs to be aligned */
    255  1.11.20.1      matt 				cif.ifa_data = data = (void *)ALIGN(data);
    256        1.1    itojun 				memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data));
    257        1.1    itojun  				data += sizeof(ifm->ifm_data);
    258        1.1    itojun 			} else
    259        1.5  christos 				idx = 0;
    260        1.1    itojun 			break;
    261        1.1    itojun 
    262        1.1    itojun 		case RTM_NEWADDR:
    263        1.5  christos 			ifam = (struct ifa_msghdr *)(void *)rtm;
    264        1.5  christos 			if (idx && ifam->ifam_index != idx)
    265        1.1    itojun 				abort();	/* this cannot happen */
    266        1.1    itojun 
    267        1.5  christos 			if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0)
    268        1.1    itojun 				break;
    269  1.11.20.1      matt 			ift->ifa_name = cif.ifa_name;
    270  1.11.20.1      matt 			ift->ifa_flags = cif.ifa_flags;
    271        1.1    itojun 			ift->ifa_data = NULL;
    272  1.11.20.3      matt 			p = (char *)
    273  1.11.20.3      matt 			    ((uintptr_t)ifam + RT_ROUNDUP(sizeof(*ifam)));
    274  1.11.20.3      matt #ifdef RTDEBUG
    275  1.11.20.3      matt 			printf("%d: %zd sa index=%d addrs=%#x name=%s\n",
    276  1.11.20.3      matt 				__LINE__, p - buf,
    277  1.11.20.3      matt 				ifam->ifam_index, ifam->ifam_addrs, ifa->ifa_name);
    278  1.11.20.3      matt #endif
    279        1.1    itojun 			/* Scan to look for length of address */
    280        1.1    itojun 			alen = 0;
    281        1.1    itojun 			for (p0 = p, i = 0; i < RTAX_MAX; i++) {
    282        1.1    itojun 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
    283        1.1    itojun 				    == 0)
    284        1.1    itojun 					continue;
    285        1.5  christos 				sa = (struct sockaddr *)(void *)p;
    286        1.1    itojun 				len = SA_RLEN(sa);
    287  1.11.20.3      matt #ifdef RTDEBUG
    288  1.11.20.3      matt 				printf("%d: %zd sa %s len=%d family=%d\n",
    289  1.11.20.3      matt 					__LINE__, p - buf, rtax_names[i],
    290  1.11.20.3      matt 					sa->sa_len, sa->sa_family);
    291  1.11.20.3      matt #endif
    292        1.1    itojun 				if (i == RTAX_IFA) {
    293        1.1    itojun 					alen = len;
    294        1.1    itojun 					break;
    295        1.1    itojun 				}
    296        1.1    itojun 				p += len;
    297        1.1    itojun 			}
    298        1.1    itojun 			for (p = p0, i = 0; i < RTAX_MAX; i++) {
    299        1.1    itojun 				if ((RTA_MASKS & ifam->ifam_addrs & (1 << i))
    300        1.1    itojun 				    == 0)
    301        1.1    itojun 					continue;
    302        1.5  christos 				sa = (struct sockaddr *)(void *)p;
    303        1.1    itojun 				len = SA_RLEN(sa);
    304  1.11.20.3      matt #ifdef RTDEBUG
    305  1.11.20.3      matt 				printf("%d: %zd sa %s len=%d family=%d\n",
    306  1.11.20.3      matt 					__LINE__, p - buf, rtax_names[i],
    307  1.11.20.3      matt 					sa->sa_len, sa->sa_family);
    308  1.11.20.3      matt #endif
    309        1.1    itojun 				switch (i) {
    310        1.1    itojun 				case RTAX_IFA:
    311        1.5  christos 					ift->ifa_addr =
    312        1.5  christos 					    (struct sockaddr *)(void *)data;
    313        1.1    itojun 					memcpy(data, p, len);
    314        1.1    itojun 					data += len;
    315  1.11.20.1      matt 					if (ift->ifa_addr->sa_family == AF_LINK)
    316  1.11.20.1      matt 						ift->ifa_data = cif.ifa_data;
    317        1.1    itojun 					break;
    318        1.1    itojun 
    319        1.1    itojun 				case RTAX_NETMASK:
    320        1.1    itojun 					ift->ifa_netmask =
    321        1.5  christos 					    (struct sockaddr *)(void *)data;
    322        1.9    itojun 					if (sa->sa_len == 0) {
    323        1.1    itojun 						memset(data, 0, alen);
    324        1.1    itojun 						data += alen;
    325        1.1    itojun 						break;
    326        1.1    itojun 					}
    327        1.1    itojun 					memcpy(data, p, len);
    328        1.1    itojun 					data += len;
    329        1.1    itojun 					break;
    330        1.1    itojun 
    331        1.1    itojun 				case RTAX_BRD:
    332        1.1    itojun 					ift->ifa_broadaddr =
    333        1.5  christos 					    (struct sockaddr *)(void *)data;
    334        1.1    itojun 					memcpy(data, p, len);
    335        1.1    itojun 					data += len;
    336        1.1    itojun 					break;
    337        1.1    itojun 				}
    338        1.1    itojun 				p += len;
    339        1.1    itojun 			}
    340        1.1    itojun 
    341  1.11.20.3      matt #ifdef RTDEBUG
    342  1.11.20.3      matt 			if (ift->ifa_name == NULL || ift->ifa_name[0] == 0)
    343  1.11.20.3      matt 				printf("%p: ifa_name == NULL!\n", ift);
    344  1.11.20.3      matt 			else
    345  1.11.20.3      matt 				printf("%p: ifa_name=%s\n", ift, ift->ifa_name);
    346  1.11.20.3      matt #endif
    347        1.1    itojun 			ift = (ift->ifa_next = ift + 1);
    348        1.1    itojun 			break;
    349        1.1    itojun 		}
    350        1.1    itojun 	}
    351        1.1    itojun 
    352  1.11.20.3      matt #ifdef RTDEBUG
    353  1.11.20.3      matt 	printf("ifa=%p data=%p names=%p\n",
    354  1.11.20.3      matt 	    ifa, data, names);
    355  1.11.20.3      matt #endif
    356  1.11.20.3      matt 
    357        1.1    itojun 	free(buf);
    358        1.1    itojun 	if (--ift >= ifa) {
    359        1.1    itojun 		ift->ifa_next = NULL;
    360        1.1    itojun 		*pif = ifa;
    361        1.1    itojun 	} else {
    362        1.1    itojun 		*pif = NULL;
    363        1.1    itojun 		free(ifa);
    364        1.1    itojun 	}
    365        1.1    itojun 	return (0);
    366        1.2    itojun }
    367        1.2    itojun 
    368        1.2    itojun void
    369        1.2    itojun freeifaddrs(struct ifaddrs *ifp)
    370        1.2    itojun {
    371        1.7     lukem 
    372        1.7     lukem 	_DIAGASSERT(ifp != NULL);
    373        1.7     lukem 
    374        1.2    itojun 	free(ifp);
    375        1.1    itojun }
    376