Home | History | Annotate | Line # | Download | only in rtadvd
if.c revision 1.22
      1  1.22       roy /*	$NetBSD: if.c,v 1.22 2012/12/13 15:36:36 roy Exp $	*/
      2  1.18    rpaulo /*	$KAME: if.c,v 1.36 2004/11/30 22:32:01 suz Exp $	*/
      3   1.2    itojun 
      4   1.1    itojun /*
      5   1.1    itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.1    itojun  * All rights reserved.
      7   1.1    itojun  *
      8   1.1    itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1    itojun  * modification, are permitted provided that the following conditions
     10   1.1    itojun  * are met:
     11   1.1    itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1    itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1    itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1    itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1    itojun  *    documentation and/or other materials provided with the distribution.
     16   1.1    itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.1    itojun  *    may be used to endorse or promote products derived from this software
     18   1.1    itojun  *    without specific prior written permission.
     19   1.1    itojun  *
     20   1.1    itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.1    itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1    itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.1    itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1    itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1    itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1    itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1    itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1    itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1    itojun  * SUCH DAMAGE.
     31   1.1    itojun  */
     32   1.1    itojun 
     33   1.1    itojun #include <sys/param.h>
     34  1.22       roy #include <sys/queue.h>
     35   1.1    itojun #include <sys/socket.h>
     36   1.1    itojun #include <sys/sysctl.h>
     37   1.1    itojun #include <sys/ioctl.h>
     38   1.1    itojun #include <net/if.h>
     39   1.1    itojun #include <net/if_types.h>
     40   1.6    itojun #include <ifaddrs.h>
     41  1.22       roy #ifdef __FreeBSD__
     42  1.22       roy #include <net/ethernet.h>
     43  1.22       roy #else
     44   1.1    itojun #include <net/if_ether.h>
     45  1.22       roy #endif
     46   1.1    itojun #include <net/route.h>
     47   1.1    itojun #include <net/if_dl.h>
     48   1.1    itojun #include <netinet/in.h>
     49   1.1    itojun #include <netinet/icmp6.h>
     50   1.1    itojun #include <unistd.h>
     51   1.1    itojun #include <errno.h>
     52   1.1    itojun #include <stdlib.h>
     53   1.1    itojun #include <string.h>
     54   1.1    itojun #include <syslog.h>
     55  1.22       roy 
     56   1.1    itojun #include "rtadvd.h"
     57   1.1    itojun #include "if.h"
     58   1.1    itojun 
     59  1.21       roy #ifndef RT_ROUNDUP
     60  1.21       roy #define RT_ROUNDUP(a)							       \
     61  1.21       roy 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     62  1.21       roy #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len))
     63  1.21       roy #endif
     64   1.1    itojun 
     65   1.1    itojun static void
     66   1.1    itojun get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
     67   1.1    itojun {
     68   1.1    itojun 	int i;
     69   1.1    itojun 
     70   1.1    itojun 	for (i = 0; i < RTAX_MAX; i++) {
     71   1.1    itojun 		if (addrs & (1 << i)) {
     72   1.1    itojun 			rti_info[i] = sa;
     73  1.21       roy 			RT_ADVANCE(sa, sa);
     74   1.1    itojun 		}
     75   1.1    itojun 		else
     76   1.1    itojun 			rti_info[i] = NULL;
     77   1.1    itojun 	}
     78   1.1    itojun }
     79   1.1    itojun 
     80   1.1    itojun struct sockaddr_dl *
     81  1.20  christos if_nametosdl(const char *name)
     82   1.1    itojun {
     83  1.16    itojun 	struct ifaddrs *ifap, *ifa;
     84  1.17    itojun 	struct sockaddr_dl *sdl;
     85   1.1    itojun 
     86  1.16    itojun 	if (getifaddrs(&ifap) != 0)
     87  1.15    itojun 		return (NULL);
     88   1.1    itojun 
     89  1.16    itojun 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
     90  1.16    itojun 		if (strcmp(ifa->ifa_name, name) != 0)
     91  1.16    itojun 			continue;
     92  1.16    itojun 		if (ifa->ifa_addr->sa_family != AF_LINK)
     93  1.16    itojun 			continue;
     94  1.16    itojun 
     95  1.16    itojun 		sdl = malloc(ifa->ifa_addr->sa_len);
     96  1.16    itojun 		if (!sdl)
     97  1.16    itojun 			continue;	/*XXX*/
     98  1.16    itojun 
     99  1.16    itojun 		memcpy(sdl, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    100  1.16    itojun 		freeifaddrs(ifap);
    101  1.16    itojun 		return (sdl);
    102   1.1    itojun 	}
    103   1.1    itojun 
    104  1.16    itojun 	freeifaddrs(ifap);
    105  1.16    itojun 	return (NULL);
    106   1.1    itojun }
    107   1.1    itojun 
    108   1.1    itojun int
    109  1.20  christos if_getmtu(const char *name)
    110   1.1    itojun {
    111  1.22       roy 	struct ifreq ifr;
    112  1.22       roy 	int s, mtu;
    113   1.8    itojun 
    114  1.22       roy 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    115  1.22       roy 		return 0;
    116   1.8    itojun 
    117  1.22       roy 	memset(&ifr, 0, sizeof(ifr));
    118  1.22       roy 	ifr.ifr_addr.sa_family = AF_INET6;
    119  1.22       roy 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    120  1.22       roy 	if (ioctl(s, SIOCGIFMTU, &ifr) != -1)
    121   1.8    itojun 		mtu = ifr.ifr_mtu;
    122  1.22       roy 	else
    123  1.22       roy 		mtu = 0;
    124  1.22       roy 	close(s);
    125   1.8    itojun 
    126  1.22       roy 	return mtu;
    127   1.1    itojun }
    128   1.1    itojun 
    129   1.1    itojun /* give interface index and its old flags, then new flags returned */
    130   1.1    itojun int
    131   1.1    itojun if_getflags(int ifindex, int oifflags)
    132   1.1    itojun {
    133   1.1    itojun 	struct ifreq ifr;
    134   1.1    itojun 	int s;
    135   1.1    itojun 
    136   1.1    itojun 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    137  1.14    itojun 		syslog(LOG_ERR, "<%s> socket: %s", __func__,
    138   1.1    itojun 		       strerror(errno));
    139   1.1    itojun 		return (oifflags & ~IFF_UP);
    140   1.1    itojun 	}
    141   1.1    itojun 
    142  1.22       roy 	memset(&ifr, 0, sizeof(ifr));
    143   1.1    itojun 	if_indextoname(ifindex, ifr.ifr_name);
    144  1.19       roy 	if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
    145   1.1    itojun 		syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
    146  1.14    itojun 		       __func__, ifr.ifr_name);
    147   1.1    itojun 		close(s);
    148   1.1    itojun 		return (oifflags & ~IFF_UP);
    149   1.1    itojun 	}
    150   1.6    itojun 	close(s);
    151   1.1    itojun 	return (ifr.ifr_flags);
    152   1.1    itojun }
    153   1.1    itojun 
    154   1.1    itojun #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
    155   1.1    itojun int
    156   1.1    itojun lladdropt_length(struct sockaddr_dl *sdl)
    157   1.1    itojun {
    158  1.12    itojun 	switch (sdl->sdl_type) {
    159  1.11    itojun 	case IFT_ETHER:
    160  1.13    itojun 	case IFT_FDDI:
    161  1.11    itojun 		return(ROUNDUP8(ETHER_ADDR_LEN + 2));
    162  1.11    itojun 	default:
    163  1.11    itojun 		return(0);
    164   1.1    itojun 	}
    165   1.1    itojun }
    166   1.1    itojun 
    167   1.1    itojun void
    168   1.1    itojun lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
    169   1.1    itojun {
    170   1.1    itojun 	char *addr;
    171   1.1    itojun 
    172   1.1    itojun 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
    173   1.1    itojun 
    174  1.12    itojun 	switch (sdl->sdl_type) {
    175  1.11    itojun 	case IFT_ETHER:
    176  1.13    itojun 	case IFT_FDDI:
    177  1.11    itojun 		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
    178  1.11    itojun 		addr = (char *)(ndopt + 1);
    179  1.11    itojun 		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
    180  1.11    itojun 		break;
    181  1.11    itojun 	default:
    182  1.11    itojun 		syslog(LOG_ERR, "<%s> unsupported link type(%d)",
    183  1.14    itojun 		    __func__, sdl->sdl_type);
    184  1.11    itojun 		exit(1);
    185   1.1    itojun 	}
    186   1.1    itojun 
    187   1.1    itojun 	return;
    188   1.1    itojun }
    189   1.1    itojun 
    190   1.1    itojun #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
    191   1.1    itojun #define SIN6(s) ((struct sockaddr_in6 *)(s))
    192   1.1    itojun #define SDL(s) ((struct sockaddr_dl *)(s))
    193   1.1    itojun char *
    194   1.2    itojun get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
    195   1.1    itojun {
    196   1.1    itojun 	struct rt_msghdr *rtm;
    197   1.1    itojun 	struct ifa_msghdr *ifam;
    198   1.1    itojun 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
    199   1.1    itojun 
    200   1.1    itojun 	*lenp = 0;
    201   1.1    itojun 	for (rtm = (struct rt_msghdr *)buf;
    202   1.1    itojun 	     rtm < (struct rt_msghdr *)lim;
    203   1.1    itojun 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
    204   1.1    itojun 		/* just for safety */
    205   1.1    itojun 		if (!rtm->rtm_msglen) {
    206   1.1    itojun 			syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
    207  1.14    itojun 				"(buf=%p lim=%p rtm=%p)", __func__,
    208   1.1    itojun 				buf, lim, rtm);
    209   1.1    itojun 			break;
    210   1.1    itojun 		}
    211   1.1    itojun 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
    212   1.1    itojun 			continue;
    213   1.1    itojun 		}
    214   1.1    itojun 
    215   1.1    itojun 		switch (rtm->rtm_type) {
    216   1.1    itojun 		case RTM_GET:
    217   1.1    itojun 		case RTM_ADD:
    218   1.1    itojun 		case RTM_DELETE:
    219   1.1    itojun 			/* address related checks */
    220   1.1    itojun 			sa = (struct sockaddr *)(rtm + 1);
    221   1.1    itojun 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    222   1.1    itojun 			if ((dst = rti_info[RTAX_DST]) == NULL ||
    223   1.1    itojun 			    dst->sa_family != AF_INET6)
    224   1.1    itojun 				continue;
    225   1.1    itojun 
    226   1.1    itojun 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
    227   1.1    itojun 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
    228   1.1    itojun 				continue;
    229   1.1    itojun 
    230   1.1    itojun 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
    231   1.1    itojun 			    gw->sa_family != AF_LINK)
    232   1.1    itojun 				continue;
    233   1.1    itojun 			if (ifindex && SDL(gw)->sdl_index != ifindex)
    234   1.1    itojun 				continue;
    235   1.1    itojun 
    236   1.1    itojun 			if (rti_info[RTAX_NETMASK] == NULL)
    237   1.1    itojun 				continue;
    238   1.1    itojun 
    239   1.1    itojun 			/* found */
    240   1.1    itojun 			*lenp = rtm->rtm_msglen;
    241   1.1    itojun 			return (char *)rtm;
    242   1.1    itojun 			/* NOTREACHED */
    243   1.1    itojun 		case RTM_NEWADDR:
    244   1.1    itojun 		case RTM_DELADDR:
    245   1.1    itojun 			ifam = (struct ifa_msghdr *)rtm;
    246   1.1    itojun 
    247   1.1    itojun 			/* address related checks */
    248   1.1    itojun 			sa = (struct sockaddr *)(ifam + 1);
    249   1.1    itojun 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
    250   1.1    itojun 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
    251   1.1    itojun 			    (ifa->sa_family != AF_INET &&
    252   1.1    itojun 			     ifa->sa_family != AF_INET6))
    253   1.1    itojun 				continue;
    254   1.1    itojun 
    255   1.1    itojun 			if (ifa->sa_family == AF_INET6 &&
    256   1.1    itojun 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
    257   1.1    itojun 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
    258   1.1    itojun 				continue;
    259   1.1    itojun 
    260   1.1    itojun 			if (ifindex && ifam->ifam_index != ifindex)
    261   1.1    itojun 				continue;
    262   1.1    itojun 
    263   1.1    itojun 			/* found */
    264   1.1    itojun 			*lenp = ifam->ifam_msglen;
    265   1.1    itojun 			return (char *)rtm;
    266   1.1    itojun 			/* NOTREACHED */
    267  1.22       roy #ifdef RTM_IFANNOUNCE
    268  1.22       roy 		case RTM_IFANNOUNCE:
    269  1.22       roy #endif
    270   1.1    itojun 		case RTM_IFINFO:
    271   1.1    itojun 			/* found */
    272   1.1    itojun 			*lenp = rtm->rtm_msglen;
    273   1.1    itojun 			return (char *)rtm;
    274   1.1    itojun 			/* NOTREACHED */
    275   1.1    itojun 		}
    276   1.1    itojun 	}
    277   1.1    itojun 
    278   1.1    itojun 	return (char *)rtm;
    279   1.1    itojun }
    280   1.9       cgd #undef FILTER_MATCH
    281   1.1    itojun 
    282   1.1    itojun struct in6_addr *
    283   1.1    itojun get_addr(char *buf)
    284   1.1    itojun {
    285   1.1    itojun 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    286   1.1    itojun 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    287   1.1    itojun 
    288   1.1    itojun 	sa = (struct sockaddr *)(rtm + 1);
    289   1.1    itojun 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    290   1.1    itojun 
    291   1.1    itojun 	return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
    292   1.1    itojun }
    293   1.1    itojun 
    294   1.1    itojun int
    295   1.1    itojun get_rtm_ifindex(char *buf)
    296   1.1    itojun {
    297   1.1    itojun 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    298   1.1    itojun 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    299   1.1    itojun 
    300   1.1    itojun 	sa = (struct sockaddr *)(rtm + 1);
    301   1.1    itojun 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    302   1.1    itojun 
    303   1.1    itojun 	return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
    304   1.1    itojun }
    305   1.1    itojun 
    306   1.1    itojun int
    307   1.1    itojun get_ifm_ifindex(char *buf)
    308   1.1    itojun {
    309   1.1    itojun 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
    310   1.1    itojun 
    311   1.1    itojun 	return ((int)ifm->ifm_index);
    312   1.1    itojun }
    313   1.1    itojun 
    314   1.1    itojun int
    315   1.1    itojun get_ifam_ifindex(char *buf)
    316   1.1    itojun {
    317   1.1    itojun 	struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
    318   1.1    itojun 
    319   1.1    itojun 	return ((int)ifam->ifam_index);
    320   1.1    itojun }
    321   1.1    itojun 
    322   1.1    itojun int
    323   1.1    itojun get_ifm_flags(char *buf)
    324   1.1    itojun {
    325   1.1    itojun 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
    326   1.1    itojun 
    327   1.1    itojun 	return (ifm->ifm_flags);
    328   1.1    itojun }
    329   1.1    itojun 
    330  1.22       roy #ifdef RTM_IFANNOUNCE
    331  1.22       roy int
    332  1.22       roy get_ifan_ifindex(char *buf)
    333  1.22       roy {
    334  1.22       roy 	struct if_announcemsghdr *ifan = (struct if_announcemsghdr *)buf;
    335  1.22       roy 
    336  1.22       roy 	return ((int)ifan->ifan_index);
    337  1.22       roy }
    338  1.22       roy 
    339  1.22       roy int
    340  1.22       roy get_ifan_what(char *buf)
    341  1.22       roy {
    342  1.22       roy 	struct if_announcemsghdr *ifan = (struct if_announcemsghdr *)buf;
    343  1.22       roy 
    344  1.22       roy 	return ((int)ifan->ifan_what);
    345  1.22       roy }
    346  1.22       roy #endif
    347  1.22       roy 
    348   1.1    itojun int
    349   1.1    itojun get_prefixlen(char *buf)
    350   1.1    itojun {
    351   1.1    itojun 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    352   1.1    itojun 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    353  1.19       roy 	unsigned char *p, *lim;
    354   1.1    itojun 
    355   1.1    itojun 	sa = (struct sockaddr *)(rtm + 1);
    356   1.1    itojun 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    357   1.1    itojun 	sa = rti_info[RTAX_NETMASK];
    358   1.1    itojun 
    359  1.19       roy 	p = (unsigned char *)(&SIN6(sa)->sin6_addr);
    360  1.19       roy 	lim = (unsigned char *)sa + sa->sa_len;
    361   1.8    itojun 	return prefixlen(p, lim);
    362   1.8    itojun }
    363   1.8    itojun 
    364   1.8    itojun int
    365  1.19       roy prefixlen(const unsigned char *p, const unsigned char *lim)
    366   1.8    itojun {
    367   1.8    itojun 	int masklen;
    368   1.8    itojun 
    369   1.1    itojun 	for (masklen = 0; p < lim; p++) {
    370   1.1    itojun 		switch (*p) {
    371   1.1    itojun 		case 0xff:
    372   1.1    itojun 			masklen += 8;
    373   1.1    itojun 			break;
    374   1.1    itojun 		case 0xfe:
    375   1.1    itojun 			masklen += 7;
    376   1.1    itojun 			break;
    377   1.1    itojun 		case 0xfc:
    378   1.1    itojun 			masklen += 6;
    379   1.1    itojun 			break;
    380   1.1    itojun 		case 0xf8:
    381   1.1    itojun 			masklen += 5;
    382   1.1    itojun 			break;
    383   1.1    itojun 		case 0xf0:
    384   1.1    itojun 			masklen += 4;
    385   1.1    itojun 			break;
    386   1.1    itojun 		case 0xe0:
    387   1.1    itojun 			masklen += 3;
    388   1.1    itojun 			break;
    389   1.1    itojun 		case 0xc0:
    390   1.1    itojun 			masklen += 2;
    391   1.1    itojun 			break;
    392   1.1    itojun 		case 0x80:
    393   1.1    itojun 			masklen += 1;
    394   1.1    itojun 			break;
    395   1.1    itojun 		case 0x00:
    396   1.1    itojun 			break;
    397   1.1    itojun 		default:
    398   1.1    itojun 			return(-1);
    399   1.1    itojun 		}
    400   1.1    itojun 	}
    401   1.1    itojun 
    402   1.1    itojun 	return(masklen);
    403   1.1    itojun }
    404   1.1    itojun 
    405   1.1    itojun int
    406   1.1    itojun rtmsg_type(char *buf)
    407   1.1    itojun {
    408   1.1    itojun 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    409   1.1    itojun 
    410   1.1    itojun 	return(rtm->rtm_type);
    411   1.1    itojun }
    412   1.1    itojun 
    413   1.1    itojun int
    414   1.1    itojun rtmsg_len(char *buf)
    415   1.1    itojun {
    416   1.1    itojun 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    417   1.1    itojun 
    418   1.1    itojun 	return(rtm->rtm_msglen);
    419   1.1    itojun }
    420