Home | History | Annotate | Line # | Download | only in rtadvd
if.c revision 1.26
      1 /*	$NetBSD: if.c,v 1.26 2018/04/20 10:39:37 roy 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/queue.h>
     35 #include <sys/socket.h>
     36 #include <sys/sysctl.h>
     37 #include <sys/ioctl.h>
     38 #include <net/if.h>
     39 #include <net/if_types.h>
     40 #include <ifaddrs.h>
     41 #ifdef __FreeBSD__
     42 #include <net/ethernet.h>
     43 #else
     44 #include <net/if_ether.h>
     45 #endif
     46 #include <net/route.h>
     47 #include <net/if_dl.h>
     48 #include <netinet/in.h>
     49 #include <netinet/icmp6.h>
     50 #include <unistd.h>
     51 #include <errno.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <syslog.h>
     55 
     56 #include "rtadvd.h"
     57 #include "if.h"
     58 #include "logit.h"
     59 #include "prog_ops.h"
     60 
     61 #ifndef RT_ROUNDUP
     62 #define RT_ROUNDUP(a)							       \
     63 	((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
     64 #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len))
     65 #endif
     66 
     67 static void
     68 get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
     69 {
     70 	int i;
     71 
     72 	for (i = 0; i < RTAX_MAX; i++) {
     73 		if (addrs & (1 << i)) {
     74 			rti_info[i] = sa;
     75 			RT_ADVANCE(sa, sa);
     76 		}
     77 		else
     78 			rti_info[i] = NULL;
     79 	}
     80 }
     81 
     82 struct sockaddr_dl *
     83 if_nametosdl(const char *name)
     84 {
     85 	struct ifaddrs *ifap, *ifa;
     86 	struct sockaddr_dl *sdl;
     87 
     88 	if (getifaddrs(&ifap) != 0)
     89 		return (NULL);
     90 
     91 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
     92 		if (strcmp(ifa->ifa_name, name) != 0)
     93 			continue;
     94 		if (ifa->ifa_addr->sa_family != AF_LINK)
     95 			continue;
     96 
     97 		sdl = malloc(ifa->ifa_addr->sa_len);
     98 		if (!sdl)
     99 			continue;	/*XXX*/
    100 
    101 		memcpy(sdl, ifa->ifa_addr, ifa->ifa_addr->sa_len);
    102 		freeifaddrs(ifap);
    103 		return (sdl);
    104 	}
    105 
    106 	freeifaddrs(ifap);
    107 	return (NULL);
    108 }
    109 
    110 int
    111 if_getmtu(const char *name)
    112 {
    113 	struct ifreq ifr;
    114 	int s, mtu;
    115 
    116 	if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
    117 		return 0;
    118 
    119 	memset(&ifr, 0, sizeof(ifr));
    120 	ifr.ifr_addr.sa_family = AF_INET6;
    121 	strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
    122 	if (prog_ioctl(s, SIOCGIFMTU, &ifr) != -1)
    123 		mtu = ifr.ifr_mtu;
    124 	else
    125 		mtu = 0;
    126 	prog_close(s);
    127 
    128 	return mtu;
    129 }
    130 
    131 /* give interface index and its old flags, then new flags returned */
    132 int
    133 if_getflags(int ifindex, int oifflags)
    134 {
    135 	struct ifreq ifr;
    136 	int s;
    137 
    138 	if ((s = prog_socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
    139 		logit(LOG_ERR, "<%s> socket: %m", __func__);
    140 		return (oifflags & ~IFF_UP);
    141 	}
    142 
    143 	memset(&ifr, 0, sizeof(ifr));
    144 	if_indextoname(ifindex, ifr.ifr_name);
    145 	if (prog_ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
    146 		logit(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
    147 		       __func__, ifr.ifr_name);
    148 		prog_close(s);
    149 		return (oifflags & ~IFF_UP);
    150 	}
    151 	prog_close(s);
    152 	return (ifr.ifr_flags);
    153 }
    154 
    155 #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
    156 int
    157 lladdropt_length(struct sockaddr_dl *sdl)
    158 {
    159 	switch (sdl->sdl_type) {
    160 	case IFT_ETHER:
    161 	case IFT_FDDI:
    162 		return(ROUNDUP8(ETHER_ADDR_LEN + 2));
    163 	default:
    164 		return(0);
    165 	}
    166 }
    167 
    168 void
    169 lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
    170 {
    171 	char *addr;
    172 
    173 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
    174 
    175 	switch (sdl->sdl_type) {
    176 	case IFT_ETHER:
    177 	case IFT_FDDI:
    178 		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
    179 		addr = (char *)(ndopt + 1);
    180 		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
    181 		break;
    182 	default:
    183 		logit(LOG_ERR, "<%s> unsupported link type(%d)",
    184 		    __func__, sdl->sdl_type);
    185 		exit(1);
    186 	}
    187 
    188 	return;
    189 }
    190 
    191 #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
    192 #define SIN6(s) ((struct sockaddr_in6 *)(s))
    193 #define SDL(s) ((struct sockaddr_dl *)(s))
    194 char *
    195 get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
    196 {
    197 	struct rt_msghdr *rtm;
    198 	struct ifa_msghdr *ifam;
    199 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
    200 
    201 	*lenp = 0;
    202 	for (rtm = (struct rt_msghdr *)buf;
    203 	     rtm < (struct rt_msghdr *)lim;
    204 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
    205 		/* just for safety */
    206 		if (!rtm->rtm_msglen) {
    207 			logit(LOG_WARNING, "<%s> rtm_msglen is 0 "
    208 				"(buf=%p lim=%p rtm=%p)", __func__,
    209 				buf, lim, rtm);
    210 			break;
    211 		}
    212 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0) {
    213 			continue;
    214 		}
    215 
    216 		switch (rtm->rtm_type) {
    217 		case RTM_GET:
    218 		case RTM_ADD:
    219 		case RTM_DELETE:
    220 			/* address related checks */
    221 			sa = (struct sockaddr *)(rtm + 1);
    222 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    223 			if ((dst = rti_info[RTAX_DST]) == NULL ||
    224 			    dst->sa_family != AF_INET6)
    225 				continue;
    226 
    227 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
    228 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
    229 				continue;
    230 
    231 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
    232 			    gw->sa_family != AF_LINK)
    233 				continue;
    234 			if (ifindex && SDL(gw)->sdl_index != ifindex)
    235 				continue;
    236 
    237 			if (rti_info[RTAX_NETMASK] == NULL)
    238 				continue;
    239 
    240 			/* found */
    241 			*lenp = rtm->rtm_msglen;
    242 			return (char *)rtm;
    243 			/* NOTREACHED */
    244 		case RTM_NEWADDR:
    245 		case RTM_DELADDR:
    246 			ifam = (struct ifa_msghdr *)rtm;
    247 
    248 			/* address related checks */
    249 			sa = (struct sockaddr *)(ifam + 1);
    250 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
    251 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
    252 			    (ifa->sa_family != AF_INET &&
    253 			     ifa->sa_family != AF_INET6))
    254 				continue;
    255 
    256 			if (ifa->sa_family == AF_INET6 &&
    257 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
    258 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
    259 				continue;
    260 
    261 			if (ifindex && ifam->ifam_index != ifindex)
    262 				continue;
    263 
    264 			/* found */
    265 			*lenp = ifam->ifam_msglen;
    266 			return (char *)rtm;
    267 			/* NOTREACHED */
    268 #ifdef RTM_IFANNOUNCE
    269 		case RTM_IFANNOUNCE:
    270 #endif
    271 		case RTM_IFINFO:
    272 			/* found */
    273 			*lenp = rtm->rtm_msglen;
    274 			return (char *)rtm;
    275 			/* NOTREACHED */
    276 		}
    277 	}
    278 
    279 	return (char *)rtm;
    280 }
    281 #undef FILTER_MATCH
    282 
    283 struct in6_addr *
    284 get_addr(char *buf)
    285 {
    286 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    287 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    288 
    289 	sa = (struct sockaddr *)(rtm + 1);
    290 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    291 
    292 	return(&SIN6(rti_info[RTAX_DST])->sin6_addr);
    293 }
    294 
    295 int
    296 get_rtm_ifindex(char *buf)
    297 {
    298 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    299 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    300 
    301 	sa = (struct sockaddr *)(rtm + 1);
    302 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    303 
    304 	return(((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
    305 }
    306 
    307 int
    308 get_ifm_ifindex(char *buf)
    309 {
    310 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
    311 
    312 	return ((int)ifm->ifm_index);
    313 }
    314 
    315 int
    316 get_ifam_ifindex(char *buf)
    317 {
    318 	struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
    319 
    320 	return ((int)ifam->ifam_index);
    321 }
    322 
    323 int
    324 get_ifm_flags(char *buf)
    325 {
    326 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
    327 
    328 	return (ifm->ifm_flags);
    329 }
    330 
    331 #ifdef RTM_IFANNOUNCE
    332 int
    333 get_ifan_ifindex(char *buf)
    334 {
    335 	struct if_announcemsghdr *ifan = (struct if_announcemsghdr *)buf;
    336 
    337 	return ((int)ifan->ifan_index);
    338 }
    339 
    340 int
    341 get_ifan_what(char *buf)
    342 {
    343 	struct if_announcemsghdr *ifan = (struct if_announcemsghdr *)buf;
    344 
    345 	return ((int)ifan->ifan_what);
    346 }
    347 #endif
    348 
    349 int
    350 get_prefixlen(char *buf)
    351 {
    352 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    353 	struct sockaddr *sa, *rti_info[RTAX_MAX];
    354 	unsigned char *p, *lim;
    355 
    356 	sa = (struct sockaddr *)(rtm + 1);
    357 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
    358 	sa = rti_info[RTAX_NETMASK];
    359 
    360 	p = (unsigned char *)(&SIN6(sa)->sin6_addr);
    361 	lim = (unsigned char *)sa + sa->sa_len;
    362 	return prefixlen(p, lim);
    363 }
    364 
    365 int
    366 prefixlen(const unsigned char *p, const unsigned char *lim)
    367 {
    368 	int masklen;
    369 
    370 	for (masklen = 0; p < lim; p++) {
    371 		switch (*p) {
    372 		case 0xff:
    373 			masklen += 8;
    374 			break;
    375 		case 0xfe:
    376 			masklen += 7;
    377 			break;
    378 		case 0xfc:
    379 			masklen += 6;
    380 			break;
    381 		case 0xf8:
    382 			masklen += 5;
    383 			break;
    384 		case 0xf0:
    385 			masklen += 4;
    386 			break;
    387 		case 0xe0:
    388 			masklen += 3;
    389 			break;
    390 		case 0xc0:
    391 			masklen += 2;
    392 			break;
    393 		case 0x80:
    394 			masklen += 1;
    395 			break;
    396 		case 0x00:
    397 			break;
    398 		default:
    399 			return(-1);
    400 		}
    401 	}
    402 
    403 	return(masklen);
    404 }
    405 
    406 int
    407 rtmsg_type(char *buf)
    408 {
    409 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    410 
    411 	return(rtm->rtm_type);
    412 }
    413 
    414 int
    415 rtmsg_len(char *buf)
    416 {
    417 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
    418 
    419 	return(rtm->rtm_msglen);
    420 }
    421