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