Home | History | Annotate | Line # | Download | only in rtadvd
if.c revision 1.4
      1 /*	$NetBSD: if.c,v 1.4 2000/02/28 09:55:45 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * 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  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/param.h>
     33 #include <sys/socket.h>
     34 #include <sys/sysctl.h>
     35 #include <sys/ioctl.h>
     36 #include <net/if.h>
     37 #include <net/if_types.h>
     38 #ifdef __FreeBSD__
     39 # include <net/ethernet.h>
     40 #endif
     41 #ifdef __bsdi__
     42 # include <ifaddrs.h>
     43 #endif
     44 #ifdef __NetBSD__
     45 #include <net/if_ether.h>
     46 #endif
     47 #include <net/route.h>
     48 #include <net/if_dl.h>
     49 #include <netinet/in.h>
     50 #include <netinet/icmp6.h>
     51 #ifdef __bsdi__
     52 # include <netinet/if_ether.h>
     53 #endif
     54 #ifdef __OpenBSD__
     55 #include <netinet/if_ether.h>
     56 #endif
     57 #include <unistd.h>
     58 #include <errno.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <syslog.h>
     62 #include "rtadvd.h"
     63 #include "if.h"
     64 
     65 #define ROUNDUP(a, size) \
     66 	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
     67 
     68 #define NEXT_SA(ap) (ap) = (struct sockaddr *) \
     69 	((caddr_t)(ap) + ((ap)->sa_len ? ROUNDUP((ap)->sa_len,\
     70 						 sizeof(u_long)) :\
     71 			  			 sizeof(u_long)))
     72 
     73 struct if_msghdr **iflist;
     74 int iflist_init_ok;
     75 size_t ifblock_size;
     76 char *ifblock;
     77 
     78 static void get_iflist __P((char **buf, size_t *size));
     79 static void parse_iflist __P((struct if_msghdr ***ifmlist_p, char *buf,
     80 		       size_t bufsize));
     81 
     82 static void
     83 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
     84 {
     85 	int i;
     86 
     87 	for (i = 0; i < RTAX_MAX; i++) {
     88 		if (addrs & (1 << i)) {
     89 			rti_info[i] = sa;
     90 			NEXT_SA(sa);
     91 		}
     92 		else
     93 			rti_info[i] = NULL;
     94 	}
     95 }
     96 
     97 struct sockaddr_dl *
     98 if_nametosdl(char *name)
     99 {
    100 	int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
    101 	char *buf, *next, *lim;
    102 	size_t len;
    103 	struct if_msghdr *ifm;
    104 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    105 	struct sockaddr_dl *sdl = NULL, *ret_sdl;
    106 
    107 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
    108 		return(NULL);
    109 	if ((buf = malloc(len)) == NULL)
    110 		return(NULL);
    111 	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
    112 		free(buf);
    113 		return(NULL);
    114 	}
    115 
    116 	lim = buf + len;
    117 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
    118 		ifm = (struct if_msghdr *)next;
    119 		if (ifm->ifm_type == RTM_IFINFO) {
    120 			sa = (struct sockaddr *)(ifm + 1);
    121 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
    122 			if ((sa = rti_info[RTAX_IFP]) != NULL) {
    123 				if (sa->sa_family == AF_LINK) {
    124 					sdl = (struct sockaddr_dl *)sa;
    125 					if (strlen(name) != sdl->sdl_nlen)
    126 						continue; /* not same len */
    127 					if (strncmp(&sdl->sdl_data[0],
    128 						    name,
    129 						    sdl->sdl_nlen) == 0) {
    130 						break;
    131 					}
    132 				}
    133 			}
    134 		}
    135 	}
    136 	if (next == lim) {
    137 		/* search failed */
    138 		free(buf);
    139 		return(NULL);
    140 	}
    141 
    142 	if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
    143 		return(NULL);
    144 	memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
    145 	return(ret_sdl);
    146 }
    147 
    148 int
    149 if_getmtu(char *name)
    150 {
    151 #if defined(__FreeBSD__) || defined(__NetBSD__)
    152 	struct ifreq ifr;
    153 	int s;
    154 
    155 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    156 		return(0);
    157 
    158 	ifr.ifr_addr.sa_family = AF_INET6;
    159 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    160 	if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
    161 		close(s);
    162 		return(0);
    163 	}
    164 
    165 	close(s);
    166 
    167 	return(ifr.ifr_mtu);
    168 #endif
    169 #ifdef __bsdi__
    170 	struct ifaddrs *ifap, *ifa;
    171 	struct if_data *ifd;
    172 
    173 	if (getifaddrs(&ifap) < 0)
    174 		return(0);
    175 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
    176 		if (strcmp(ifa->ifa_name, name) == 0) {
    177 			ifd = ifa->ifa_data;
    178 			freeifaddrs(ifap);
    179 			if (ifd)
    180 				return ifd->ifi_mtu;
    181 			else
    182 				return 0;
    183 		}
    184 	}
    185 	freeifaddrs(ifap);
    186 	return 0;
    187 #endif
    188 	/* last resort */
    189 	return 0;
    190 }
    191 
    192 /* give interface index and its old flags, then new flags returned */
    193 int
    194 if_getflags(int ifindex, int oifflags)
    195 {
    196 	struct ifreq ifr;
    197 	int s;
    198 
    199 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    200 		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
    201 		       strerror(errno));
    202 		return (oifflags & ~IFF_UP);
    203 	}
    204 
    205 	if_indextoname(ifindex, ifr.ifr_name);
    206 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
    207 		syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
    208 		       __FUNCTION__, ifr.ifr_name);
    209 		close(s);
    210 		return (oifflags & ~IFF_UP);
    211 	}
    212 	return (ifr.ifr_flags);
    213 }
    214 
    215 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
    216 int
    217 lladdropt_length(struct sockaddr_dl *sdl)
    218 {
    219 	switch(sdl->sdl_type) {
    220 	 case IFT_ETHER:
    221 		 return(ROUNDUP8(ETHER_ADDR_LEN + 2));
    222 	 default:
    223 		 return(0);
    224 	}
    225 }
    226 
    227 void
    228 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
    229 {
    230 	char *addr;
    231 
    232 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
    233 
    234 	switch(sdl->sdl_type) {
    235 	 case IFT_ETHER:
    236 		 ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
    237 		 addr = (char *)(ndopt + 1);
    238 		 memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
    239 		 break;
    240 	 default:
    241 		 syslog(LOG_ERR,
    242 			"<%s> unsupported link type(%d)",
    243 			__FUNCTION__, sdl->sdl_type);
    244 		 exit(1);
    245 	}
    246 
    247 	return;
    248 }
    249 
    250 int
    251 rtbuf_len()
    252 {
    253 	size_t len;
    254 
    255 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
    256 
    257 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
    258 		return(-1);
    259 
    260 	return(len);
    261 }
    262 
    263 int
    264 get_rtinfo(char *buf, size_t *len)
    265 {
    266 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
    267 
    268 	if (sysctl(mib, 6, buf, len, NULL, 0) < 0)
    269 		return(-1);
    270 
    271 	return(0);
    272 }
    273 
    274 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
    275 #define SIN6(s) ((struct sockaddr_in6 *)(s))
    276 #define SDL(s) ((struct sockaddr_dl *)(s))
    277 char *
    278 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
    279 {
    280 	struct rt_msghdr *rtm;
    281 	struct ifa_msghdr *ifam;
    282 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
    283 
    284 	*lenp = 0;
    285 	for (rtm = (struct rt_msghdr *)buf;
    286 	     rtm < (struct rt_msghdr *)lim;
    287 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
    288 		/* just for safety */
    289 		if (!rtm->rtm_msglen) {
    290 			syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
    291 				"(buf=%p lim=%p rtm=%p)", __FUNCTION__,
    292 				buf, lim, rtm);
    293 			break;
    294 		}
    295 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
    296 			continue;
    297 		}
    298 
    299 		switch (rtm->rtm_type) {
    300 		case RTM_GET:
    301 		case RTM_ADD:
    302 		case RTM_DELETE:
    303 			/* address related checks */
    304 			sa = (struct sockaddr *)(rtm + 1);
    305 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    306 			if ((dst = rti_info[RTAX_DST]) == NULL ||
    307 			    dst->sa_family != AF_INET6)
    308 				continue;
    309 
    310 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
    311 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
    312 				continue;
    313 
    314 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
    315 			    gw->sa_family != AF_LINK)
    316 				continue;
    317 			if (ifindex && SDL(gw)->sdl_index != ifindex)
    318 				continue;
    319 
    320 			if (rti_info[RTAX_NETMASK] == NULL)
    321 				continue;
    322 
    323 			/* found */
    324 			*lenp = rtm->rtm_msglen;
    325 			return (char *)rtm;
    326 			/* NOTREACHED */
    327 		case RTM_NEWADDR:
    328 		case RTM_DELADDR:
    329 			ifam = (struct ifa_msghdr *)rtm;
    330 
    331 			/* address related checks */
    332 			sa = (struct sockaddr *)(ifam + 1);
    333 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
    334 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
    335 			    (ifa->sa_family != AF_INET &&
    336 			     ifa->sa_family != AF_INET6))
    337 				continue;
    338 
    339 			if (ifa->sa_family == AF_INET6 &&
    340 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
    341 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
    342 				continue;
    343 
    344 			if (ifindex && ifam->ifam_index != ifindex)
    345 				continue;
    346 
    347 			/* found */
    348 			*lenp = ifam->ifam_msglen;
    349 			return (char *)rtm;
    350 			/* NOTREACHED */
    351 		case RTM_IFINFO:
    352 			/* found */
    353 			*lenp = rtm->rtm_msglen;
    354 			return (char *)rtm;
    355 			/* NOTREACHED */
    356 		}
    357 	}
    358 
    359 	return (char *)rtm;
    360 }
    361 #undef FILTER_MATCH(type, filter)
    362 
    363 struct in6_addr *
    364 get_addr(char *buf)
    365 {
    366 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    367 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    368 
    369 	sa = (struct sockaddr *)(rtm + 1);
    370 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    371 
    372 	return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
    373 }
    374 
    375 int
    376 get_rtm_ifindex(char *buf)
    377 {
    378 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    379 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    380 
    381 	sa = (struct sockaddr *)(rtm + 1);
    382 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    383 
    384 	return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
    385 }
    386 
    387 int
    388 get_ifm_ifindex(char *buf)
    389 {
    390 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
    391 
    392 	return ((int)ifm->ifm_index);
    393 }
    394 
    395 int
    396 get_ifam_ifindex(char *buf)
    397 {
    398 	struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
    399 
    400 	return ((int)ifam->ifam_index);
    401 }
    402 
    403 int
    404 get_ifm_flags(char *buf)
    405 {
    406 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
    407 
    408 	return (ifm->ifm_flags);
    409 }
    410 
    411 int
    412 get_prefixlen(char *buf)
    413 {
    414 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    415 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    416 	int masklen;
    417 	u_char *p, *lim;
    418 
    419 	sa = (struct sockaddr *)(rtm + 1);
    420 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    421 	sa = rti_info[RTAX_NETMASK];
    422 
    423 	p = (u_char *)(&SIN6(sa)->sin6_addr);
    424 	lim = (u_char *)sa + sa->sa_len;
    425 	for (masklen = 0; p < lim; p++) {
    426 		switch (*p) {
    427 		case 0xff:
    428 			masklen += 8;
    429 			break;
    430 		case 0xfe:
    431 			masklen += 7;
    432 			break;
    433 		case 0xfc:
    434 			masklen += 6;
    435 			break;
    436 		case 0xf8:
    437 			masklen += 5;
    438 			break;
    439 		case 0xf0:
    440 			masklen += 4;
    441 			break;
    442 		case 0xe0:
    443 			masklen += 3;
    444 			break;
    445 		case 0xc0:
    446 			masklen += 2;
    447 			break;
    448 		case 0x80:
    449 			masklen += 1;
    450 			break;
    451 		case 0x00:
    452 			break;
    453 		default:
    454 			return(-1);
    455 		}
    456 	}
    457 
    458 	return(masklen);
    459 }
    460 
    461 int
    462 rtmsg_type(char *buf)
    463 {
    464 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    465 
    466 	return(rtm->rtm_type);
    467 }
    468 
    469 int
    470 rtmsg_len(char *buf)
    471 {
    472 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    473 
    474 	return(rtm->rtm_msglen);
    475 }
    476 
    477 int
    478 ifmsg_len(char *buf)
    479 {
    480 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
    481 
    482 	return(ifm->ifm_msglen);
    483 }
    484 
    485 /*
    486  * alloc buffer and get if_msghdrs block from kernel,
    487  * and put them into the buffer
    488  */
    489 static void
    490 get_iflist(char **buf, size_t *size)
    491 {
    492 	int mib[6];
    493 
    494 	mib[0] = CTL_NET;
    495 	mib[1] = PF_ROUTE;
    496 	mib[2] = 0;
    497 	mib[3] = AF_INET6;
    498 	mib[4] = NET_RT_IFLIST;
    499 	mib[5] = 0;
    500 
    501 	if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
    502 		syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
    503 		       __FUNCTION__);
    504 		exit(1);
    505 	}
    506 	if ((*buf = malloc(*size)) == NULL) {
    507 		syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
    508 		exit(1);
    509 	}
    510 	if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
    511 		syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
    512 		       __FUNCTION__);
    513 		exit(1);
    514 	}
    515 	return;
    516 }
    517 
    518 /*
    519  * alloc buffer and parse if_msghdrs block passed as arg,
    520  * and init the buffer as list of pointers ot each of the if_msghdr.
    521  */
    522 static void
    523 parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
    524 {
    525 	int iflentry_size, malloc_size;
    526 	struct if_msghdr *ifm;
    527 	struct ifa_msghdr *ifam;
    528 	char *lim;
    529 
    530 	/*
    531 	 * Estimate least size of an iflist entry, to be obtained from kernel.
    532 	 * Should add sizeof(sockaddr) ??
    533 	 */
    534 	iflentry_size = sizeof(struct if_msghdr);
    535 	/* roughly estimate max list size of pointers to each if_msghdr */
    536 	malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
    537 	if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
    538 		syslog(LOG_ERR, "<%s> malloc failed", __FUNCTION__);
    539 		exit(1);
    540 	}
    541 
    542 	lim = buf + bufsize;
    543 	for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
    544 		if (ifm->ifm_msglen == 0) {
    545 			syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
    546 			       "(buf=%p lim=%p ifm=%p)", __FUNCTION__,
    547 			       buf, lim, ifm);
    548 			return;
    549 		}
    550 
    551 		if (ifm->ifm_type == RTM_IFINFO) {
    552 			(*ifmlist_p)[ifm->ifm_index] = ifm;
    553 		} else {
    554 			syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
    555 			       "expected %d, got %d\n msglen = %d\n"
    556 			       "buf:%p, ifm:%p, lim:%p\n",
    557 			       RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
    558 			       buf, ifm, lim);
    559 			exit (1);
    560 		}
    561 		for (ifam = (struct ifa_msghdr *)
    562 			((char *)ifm + ifm->ifm_msglen);
    563 		     ifam < (struct ifa_msghdr *)lim;
    564 		     ifam = (struct ifa_msghdr *)
    565 		     	((char *)ifam + ifam->ifam_msglen)) {
    566 			/* just for safety */
    567 			if (!ifam->ifam_msglen) {
    568 				syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
    569 				       "(buf=%p lim=%p ifam=%p)", __FUNCTION__,
    570 				       buf, lim, ifam);
    571 				return;
    572 			}
    573 			if (ifam->ifam_type != RTM_NEWADDR)
    574 				break;
    575 		}
    576 		ifm = (struct if_msghdr *)ifam;
    577 	}
    578 }
    579 
    580 void
    581 init_iflist()
    582 {
    583 	if (ifblock) {
    584 		free(ifblock);
    585 		ifblock_size = 0;
    586 	}
    587 	if (iflist)
    588 		free(iflist);
    589 	/* get iflist block from kernel */
    590 	get_iflist(&ifblock, &ifblock_size);
    591 
    592 	/* make list of pointers to each if_msghdr */
    593 	parse_iflist(&iflist, ifblock, ifblock_size);
    594 
    595 }
    596