xref: /freebsd/usr.sbin/rtadvd/if.c (revision db82af41db538fba5938d8585b2e2e2c206affb6)
1ae326725SJun-ichiro itojun Hagino /*	$FreeBSD$	*/
233841545SHajimu UMEMOTO /*	$KAME: if.c,v 1.17 2001/01/21 15:27:30 itojun Exp $	*/
3b26e03e9SKris Kennaway 
49a4365d0SYoshinobu Inoue /*
59a4365d0SYoshinobu Inoue  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
69a4365d0SYoshinobu Inoue  * All rights reserved.
79a4365d0SYoshinobu Inoue  *
89a4365d0SYoshinobu Inoue  * Redistribution and use in source and binary forms, with or without
99a4365d0SYoshinobu Inoue  * modification, are permitted provided that the following conditions
109a4365d0SYoshinobu Inoue  * are met:
119a4365d0SYoshinobu Inoue  * 1. Redistributions of source code must retain the above copyright
129a4365d0SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer.
139a4365d0SYoshinobu Inoue  * 2. Redistributions in binary form must reproduce the above copyright
149a4365d0SYoshinobu Inoue  *    notice, this list of conditions and the following disclaimer in the
159a4365d0SYoshinobu Inoue  *    documentation and/or other materials provided with the distribution.
169a4365d0SYoshinobu Inoue  * 3. Neither the name of the project nor the names of its contributors
179a4365d0SYoshinobu Inoue  *    may be used to endorse or promote products derived from this software
189a4365d0SYoshinobu Inoue  *    without specific prior written permission.
199a4365d0SYoshinobu Inoue  *
209a4365d0SYoshinobu Inoue  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
219a4365d0SYoshinobu Inoue  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229a4365d0SYoshinobu Inoue  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239a4365d0SYoshinobu Inoue  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
249a4365d0SYoshinobu Inoue  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259a4365d0SYoshinobu Inoue  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269a4365d0SYoshinobu Inoue  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279a4365d0SYoshinobu Inoue  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289a4365d0SYoshinobu Inoue  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299a4365d0SYoshinobu Inoue  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309a4365d0SYoshinobu Inoue  * SUCH DAMAGE.
319a4365d0SYoshinobu Inoue  */
329a4365d0SYoshinobu Inoue 
339a4365d0SYoshinobu Inoue #include <sys/param.h>
349a4365d0SYoshinobu Inoue #include <sys/socket.h>
359a4365d0SYoshinobu Inoue #include <sys/sysctl.h>
369a4365d0SYoshinobu Inoue #include <sys/ioctl.h>
379a4365d0SYoshinobu Inoue #include <net/if.h>
389a4365d0SYoshinobu Inoue #include <net/if_types.h>
399a4365d0SYoshinobu Inoue #include <net/ethernet.h>
40b26e03e9SKris Kennaway #include <ifaddrs.h>
419a4365d0SYoshinobu Inoue #include <net/route.h>
429a4365d0SYoshinobu Inoue #include <net/if_dl.h>
439a4365d0SYoshinobu Inoue #include <netinet/in.h>
449a4365d0SYoshinobu Inoue #include <netinet/icmp6.h>
459a4365d0SYoshinobu Inoue #include <unistd.h>
469a4365d0SYoshinobu Inoue #include <errno.h>
47*db82af41SHiroki Sato #include <netdb.h>
489a4365d0SYoshinobu Inoue #include <stdlib.h>
499a4365d0SYoshinobu Inoue #include <string.h>
509a4365d0SYoshinobu Inoue #include <syslog.h>
519a4365d0SYoshinobu Inoue #include "rtadvd.h"
529a4365d0SYoshinobu Inoue #include "if.h"
539a4365d0SYoshinobu Inoue 
549a4365d0SYoshinobu Inoue #define ROUNDUP(a, size)					\
559a4365d0SYoshinobu Inoue 	(((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))
569a4365d0SYoshinobu Inoue 
57*db82af41SHiroki Sato #define	NEXT_SA(ap)							\
58*db82af41SHiroki Sato 	(ap) = (struct sockaddr *)((caddr_t)(ap) +			\
59*db82af41SHiroki Sato 	    ((ap)->sa_len ? ROUNDUP((ap)->sa_len, sizeof(u_long)) :	\
609a4365d0SYoshinobu Inoue 	    sizeof(u_long)))
619a4365d0SYoshinobu Inoue 
629a4365d0SYoshinobu Inoue struct if_msghdr **iflist;
639a4365d0SYoshinobu Inoue int iflist_init_ok;
649a4365d0SYoshinobu Inoue size_t ifblock_size;
659a4365d0SYoshinobu Inoue char *ifblock;
669a4365d0SYoshinobu Inoue 
67784bddbcSKevin Lo static void	get_iflist(char **buf, size_t *size);
68*db82af41SHiroki Sato static void	parse_iflist(struct if_msghdr ***ifmlist_p,
69*db82af41SHiroki Sato 		    char *buf, size_t bufsize);
709a4365d0SYoshinobu Inoue 
719a4365d0SYoshinobu Inoue static void
729a4365d0SYoshinobu Inoue get_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info)
739a4365d0SYoshinobu Inoue {
749a4365d0SYoshinobu Inoue 	int i;
759a4365d0SYoshinobu Inoue 
769a4365d0SYoshinobu Inoue 	for (i = 0; i < RTAX_MAX; i++) {
779a4365d0SYoshinobu Inoue 		if (addrs & (1 << i)) {
789a4365d0SYoshinobu Inoue 			rti_info[i] = sa;
799a4365d0SYoshinobu Inoue 			NEXT_SA(sa);
809a4365d0SYoshinobu Inoue 		}
819a4365d0SYoshinobu Inoue 		else
829a4365d0SYoshinobu Inoue 			rti_info[i] = NULL;
839a4365d0SYoshinobu Inoue 	}
849a4365d0SYoshinobu Inoue }
859a4365d0SYoshinobu Inoue 
869a4365d0SYoshinobu Inoue struct sockaddr_dl *
879a4365d0SYoshinobu Inoue if_nametosdl(char *name)
889a4365d0SYoshinobu Inoue {
899a4365d0SYoshinobu Inoue 	int mib[6] = {CTL_NET, AF_ROUTE, 0, 0, NET_RT_IFLIST, 0};
909a4365d0SYoshinobu Inoue 	char *buf, *next, *lim;
919a4365d0SYoshinobu Inoue 	size_t len;
929a4365d0SYoshinobu Inoue 	struct if_msghdr *ifm;
939a4365d0SYoshinobu Inoue 	struct sockaddr *sa, *rti_info[RTAX_MAX];
949a4365d0SYoshinobu Inoue 	struct sockaddr_dl *sdl = NULL, *ret_sdl;
959a4365d0SYoshinobu Inoue 
969a4365d0SYoshinobu Inoue 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
979a4365d0SYoshinobu Inoue 		return (NULL);
989a4365d0SYoshinobu Inoue 	if ((buf = malloc(len)) == NULL)
999a4365d0SYoshinobu Inoue 		return (NULL);
1009a4365d0SYoshinobu Inoue 	if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
1019a4365d0SYoshinobu Inoue 		free(buf);
1029a4365d0SYoshinobu Inoue 		return (NULL);
1039a4365d0SYoshinobu Inoue 	}
1049a4365d0SYoshinobu Inoue 
1059a4365d0SYoshinobu Inoue 	lim = buf + len;
1069a4365d0SYoshinobu Inoue 	for (next = buf; next < lim; next += ifm->ifm_msglen) {
1079a4365d0SYoshinobu Inoue 		ifm = (struct if_msghdr *)next;
1089a4365d0SYoshinobu Inoue 		if (ifm->ifm_type == RTM_IFINFO) {
1099a4365d0SYoshinobu Inoue 			sa = (struct sockaddr *)(ifm + 1);
1109a4365d0SYoshinobu Inoue 			get_rtaddrs(ifm->ifm_addrs, sa, rti_info);
1119a4365d0SYoshinobu Inoue 			if ((sa = rti_info[RTAX_IFP]) != NULL) {
1129a4365d0SYoshinobu Inoue 				if (sa->sa_family == AF_LINK) {
1139a4365d0SYoshinobu Inoue 					sdl = (struct sockaddr_dl *)sa;
114b26e03e9SKris Kennaway 					if (strlen(name) != sdl->sdl_nlen)
115b26e03e9SKris Kennaway 						continue; /* not same len */
1169a4365d0SYoshinobu Inoue 					if (strncmp(&sdl->sdl_data[0],
1179a4365d0SYoshinobu Inoue 						    name,
1189a4365d0SYoshinobu Inoue 						    sdl->sdl_nlen) == 0) {
1199a4365d0SYoshinobu Inoue 						break;
1209a4365d0SYoshinobu Inoue 					}
1219a4365d0SYoshinobu Inoue 				}
1229a4365d0SYoshinobu Inoue 			}
1239a4365d0SYoshinobu Inoue 		}
1249a4365d0SYoshinobu Inoue 	}
1259a4365d0SYoshinobu Inoue 	if (next == lim) {
1269a4365d0SYoshinobu Inoue 		/* search failed */
1279a4365d0SYoshinobu Inoue 		free(buf);
1289a4365d0SYoshinobu Inoue 		return (NULL);
1299a4365d0SYoshinobu Inoue 	}
1309a4365d0SYoshinobu Inoue 
1319a4365d0SYoshinobu Inoue 	if ((ret_sdl = malloc(sdl->sdl_len)) == NULL)
132f9547841SSUZUKI Shinsuke 		goto end;
1339a4365d0SYoshinobu Inoue 	memcpy((caddr_t)ret_sdl, (caddr_t)sdl, sdl->sdl_len);
134f9547841SSUZUKI Shinsuke 
135f9547841SSUZUKI Shinsuke end:
136f9547841SSUZUKI Shinsuke 	free(buf);
1379a4365d0SYoshinobu Inoue 	return (ret_sdl);
1389a4365d0SYoshinobu Inoue }
1399a4365d0SYoshinobu Inoue 
1409a4365d0SYoshinobu Inoue int
1419a4365d0SYoshinobu Inoue if_getmtu(char *name)
1429a4365d0SYoshinobu Inoue {
1437c991abcSHajimu UMEMOTO 	struct ifaddrs *ifap, *ifa;
1447c991abcSHajimu UMEMOTO 	struct if_data *ifd;
1457c991abcSHajimu UMEMOTO 	u_long mtu = 0;
1467c991abcSHajimu UMEMOTO 
1477c991abcSHajimu UMEMOTO 	if (getifaddrs(&ifap) < 0)
1487c991abcSHajimu UMEMOTO 		return (0);
1497c991abcSHajimu UMEMOTO 	for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
1507c991abcSHajimu UMEMOTO 		if (strcmp(ifa->ifa_name, name) == 0) {
1517c991abcSHajimu UMEMOTO 			ifd = ifa->ifa_data;
1527c991abcSHajimu UMEMOTO 			if (ifd)
1537c991abcSHajimu UMEMOTO 				mtu = ifd->ifi_mtu;
1547c991abcSHajimu UMEMOTO 			break;
1557c991abcSHajimu UMEMOTO 		}
1567c991abcSHajimu UMEMOTO 	}
1577c991abcSHajimu UMEMOTO 	freeifaddrs(ifap);
1587c991abcSHajimu UMEMOTO 
1597c991abcSHajimu UMEMOTO #ifdef SIOCGIFMTU		/* XXX: this ifdef may not be necessary */
1607c991abcSHajimu UMEMOTO 	if (mtu == 0) {
1619a4365d0SYoshinobu Inoue 		struct ifreq ifr;
1629a4365d0SYoshinobu Inoue 		int s;
1639a4365d0SYoshinobu Inoue 
1649a4365d0SYoshinobu Inoue 		if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
1659a4365d0SYoshinobu Inoue 			return (0);
1669a4365d0SYoshinobu Inoue 
1679a4365d0SYoshinobu Inoue 		ifr.ifr_addr.sa_family = AF_INET6;
1687c991abcSHajimu UMEMOTO 		strncpy(ifr.ifr_name, name,
1697c991abcSHajimu UMEMOTO 			sizeof(ifr.ifr_name));
1709a4365d0SYoshinobu Inoue 		if (ioctl(s, SIOCGIFMTU, (caddr_t)&ifr) < 0) {
1719a4365d0SYoshinobu Inoue 			close(s);
1729a4365d0SYoshinobu Inoue 			return (0);
1739a4365d0SYoshinobu Inoue 		}
1749a4365d0SYoshinobu Inoue 		close(s);
1759a4365d0SYoshinobu Inoue 
1767c991abcSHajimu UMEMOTO 		mtu = ifr.ifr_mtu;
177b26e03e9SKris Kennaway 	}
178b26e03e9SKris Kennaway #endif
1797c991abcSHajimu UMEMOTO 
1807c991abcSHajimu UMEMOTO 	return (mtu);
1819a4365d0SYoshinobu Inoue }
1829a4365d0SYoshinobu Inoue 
1839a4365d0SYoshinobu Inoue /* give interface index and its old flags, then new flags returned */
1849a4365d0SYoshinobu Inoue int
1859a4365d0SYoshinobu Inoue if_getflags(int ifindex, int oifflags)
1869a4365d0SYoshinobu Inoue {
1879a4365d0SYoshinobu Inoue 	struct ifreq ifr;
1889a4365d0SYoshinobu Inoue 	int s;
1899a4365d0SYoshinobu Inoue 
1909a4365d0SYoshinobu Inoue 	if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
1911533bed0SHajimu UMEMOTO 		syslog(LOG_ERR, "<%s> socket: %s", __func__,
1929a4365d0SYoshinobu Inoue 		    strerror(errno));
1939a4365d0SYoshinobu Inoue 		return (oifflags & ~IFF_UP);
1949a4365d0SYoshinobu Inoue 	}
1959a4365d0SYoshinobu Inoue 
1969a4365d0SYoshinobu Inoue 	if_indextoname(ifindex, ifr.ifr_name);
1979a4365d0SYoshinobu Inoue 	if (ioctl(s, SIOCGIFFLAGS, (caddr_t)&ifr) < 0) {
1989a4365d0SYoshinobu Inoue 		syslog(LOG_ERR, "<%s> ioctl:SIOCGIFFLAGS: failed for %s",
1991533bed0SHajimu UMEMOTO 		    __func__, ifr.ifr_name);
2009a4365d0SYoshinobu Inoue 		close(s);
2019a4365d0SYoshinobu Inoue 		return (oifflags & ~IFF_UP);
2029a4365d0SYoshinobu Inoue 	}
2037c991abcSHajimu UMEMOTO 	close(s);
2049a4365d0SYoshinobu Inoue 	return (ifr.ifr_flags);
2059a4365d0SYoshinobu Inoue }
2069a4365d0SYoshinobu Inoue 
2079a4365d0SYoshinobu Inoue #define ROUNDUP8(a) (1 + (((a) - 1) | 7))
2089a4365d0SYoshinobu Inoue int
2099a4365d0SYoshinobu Inoue lladdropt_length(struct sockaddr_dl *sdl)
2109a4365d0SYoshinobu Inoue {
2119a4365d0SYoshinobu Inoue 	switch (sdl->sdl_type) {
2129a4365d0SYoshinobu Inoue 	case IFT_ETHER:
2139a4365d0SYoshinobu Inoue 		return (ROUNDUP8(ETHER_ADDR_LEN + 2));
2149a4365d0SYoshinobu Inoue 	default:
2159a4365d0SYoshinobu Inoue 		return (0);
2169a4365d0SYoshinobu Inoue 	}
2179a4365d0SYoshinobu Inoue }
2189a4365d0SYoshinobu Inoue 
2199a4365d0SYoshinobu Inoue void
2209a4365d0SYoshinobu Inoue lladdropt_fill(struct sockaddr_dl *sdl, struct nd_opt_hdr *ndopt)
2219a4365d0SYoshinobu Inoue {
2229a4365d0SYoshinobu Inoue 	char *addr;
2239a4365d0SYoshinobu Inoue 
2249a4365d0SYoshinobu Inoue 	ndopt->nd_opt_type = ND_OPT_SOURCE_LINKADDR; /* fixed */
2259a4365d0SYoshinobu Inoue 
2269a4365d0SYoshinobu Inoue 	switch (sdl->sdl_type) {
2279a4365d0SYoshinobu Inoue 	case IFT_ETHER:
2289a4365d0SYoshinobu Inoue 		ndopt->nd_opt_len = (ROUNDUP8(ETHER_ADDR_LEN + 2)) >> 3;
2299a4365d0SYoshinobu Inoue 		addr = (char *)(ndopt + 1);
2309a4365d0SYoshinobu Inoue 		memcpy(addr, LLADDR(sdl), ETHER_ADDR_LEN);
2319a4365d0SYoshinobu Inoue 		break;
2329a4365d0SYoshinobu Inoue 	default:
233fa19f9beSHajimu UMEMOTO 		syslog(LOG_ERR, "<%s> unsupported link type(%d)",
2341533bed0SHajimu UMEMOTO 		    __func__, sdl->sdl_type);
2359a4365d0SYoshinobu Inoue 		exit(1);
2369a4365d0SYoshinobu Inoue 	}
2379a4365d0SYoshinobu Inoue 
2389a4365d0SYoshinobu Inoue 	return;
2399a4365d0SYoshinobu Inoue }
2409a4365d0SYoshinobu Inoue 
2419a4365d0SYoshinobu Inoue int
242*db82af41SHiroki Sato rtbuf_len(void)
2439a4365d0SYoshinobu Inoue {
2449a4365d0SYoshinobu Inoue 	size_t len;
2459a4365d0SYoshinobu Inoue 	int mib[6] = {CTL_NET, AF_ROUTE, 0, AF_INET6, NET_RT_DUMP, 0};
2469a4365d0SYoshinobu Inoue 
2479a4365d0SYoshinobu Inoue 	if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0)
2489a4365d0SYoshinobu Inoue 		return (-1);
2499a4365d0SYoshinobu Inoue 
2509a4365d0SYoshinobu Inoue 	return (len);
2519a4365d0SYoshinobu Inoue }
2529a4365d0SYoshinobu Inoue 
2539a4365d0SYoshinobu Inoue #define FILTER_MATCH(type, filter) ((0x1 << type) & filter)
2549a4365d0SYoshinobu Inoue #define SIN6(s) ((struct sockaddr_in6 *)(s))
2559a4365d0SYoshinobu Inoue #define SDL(s) ((struct sockaddr_dl *)(s))
2569a4365d0SYoshinobu Inoue char *
2579a4365d0SYoshinobu Inoue get_next_msg(char *buf, char *lim, int ifindex, size_t *lenp, int filter)
2589a4365d0SYoshinobu Inoue {
2599a4365d0SYoshinobu Inoue 	struct rt_msghdr *rtm;
2609a4365d0SYoshinobu Inoue 	struct ifa_msghdr *ifam;
2619a4365d0SYoshinobu Inoue 	struct sockaddr *sa, *dst, *gw, *ifa, *rti_info[RTAX_MAX];
2629a4365d0SYoshinobu Inoue 
2639a4365d0SYoshinobu Inoue 	*lenp = 0;
2649a4365d0SYoshinobu Inoue 	for (rtm = (struct rt_msghdr *)buf;
2659a4365d0SYoshinobu Inoue 	     rtm < (struct rt_msghdr *)lim;
2669a4365d0SYoshinobu Inoue 	     rtm = (struct rt_msghdr *)(((char *)rtm) + rtm->rtm_msglen)) {
2679a4365d0SYoshinobu Inoue 		/* just for safety */
2689a4365d0SYoshinobu Inoue 		if (!rtm->rtm_msglen) {
2699a4365d0SYoshinobu Inoue 			syslog(LOG_WARNING, "<%s> rtm_msglen is 0 "
2701533bed0SHajimu UMEMOTO 			    "(buf=%p lim=%p rtm=%p)", __func__,
2719a4365d0SYoshinobu Inoue 			    buf, lim, rtm);
2729a4365d0SYoshinobu Inoue 			break;
2739a4365d0SYoshinobu Inoue 		}
274*db82af41SHiroki Sato 		if (((struct rt_msghdr *)buf)->rtm_version != RTM_VERSION) {
275*db82af41SHiroki Sato 			syslog(LOG_WARNING,
276*db82af41SHiroki Sato 			    "<%s> routing message version mismatch "
277*db82af41SHiroki Sato 			    "(buf=%p lim=%p rtm=%p)", __func__,
278*db82af41SHiroki Sato 			    buf, lim, rtm);
2799a4365d0SYoshinobu Inoue 			continue;
2809a4365d0SYoshinobu Inoue 		}
2819a4365d0SYoshinobu Inoue 
282*db82af41SHiroki Sato 		if (FILTER_MATCH(rtm->rtm_type, filter) == 0)
283*db82af41SHiroki Sato 			continue;
284*db82af41SHiroki Sato 
2859a4365d0SYoshinobu Inoue 		switch (rtm->rtm_type) {
2869a4365d0SYoshinobu Inoue 		case RTM_GET:
2879a4365d0SYoshinobu Inoue 		case RTM_ADD:
2889a4365d0SYoshinobu Inoue 		case RTM_DELETE:
2899a4365d0SYoshinobu Inoue 			/* address related checks */
2909a4365d0SYoshinobu Inoue 			sa = (struct sockaddr *)(rtm + 1);
2919a4365d0SYoshinobu Inoue 			get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
2929a4365d0SYoshinobu Inoue 			if ((dst = rti_info[RTAX_DST]) == NULL ||
2939a4365d0SYoshinobu Inoue 			    dst->sa_family != AF_INET6)
2949a4365d0SYoshinobu Inoue 				continue;
2959a4365d0SYoshinobu Inoue 
2969a4365d0SYoshinobu Inoue 			if (IN6_IS_ADDR_LINKLOCAL(&SIN6(dst)->sin6_addr) ||
2979a4365d0SYoshinobu Inoue 			    IN6_IS_ADDR_MULTICAST(&SIN6(dst)->sin6_addr))
2989a4365d0SYoshinobu Inoue 				continue;
2999a4365d0SYoshinobu Inoue 
3009a4365d0SYoshinobu Inoue 			if ((gw = rti_info[RTAX_GATEWAY]) == NULL ||
3019a4365d0SYoshinobu Inoue 			    gw->sa_family != AF_LINK)
3029a4365d0SYoshinobu Inoue 				continue;
3039a4365d0SYoshinobu Inoue 			if (ifindex && SDL(gw)->sdl_index != ifindex)
3049a4365d0SYoshinobu Inoue 				continue;
3059a4365d0SYoshinobu Inoue 
3069a4365d0SYoshinobu Inoue 			if (rti_info[RTAX_NETMASK] == NULL)
3079a4365d0SYoshinobu Inoue 				continue;
3089a4365d0SYoshinobu Inoue 
3099a4365d0SYoshinobu Inoue 			/* found */
3109a4365d0SYoshinobu Inoue 			*lenp = rtm->rtm_msglen;
3119a4365d0SYoshinobu Inoue 			return (char *)rtm;
3129a4365d0SYoshinobu Inoue 			/* NOTREACHED */
3139a4365d0SYoshinobu Inoue 		case RTM_NEWADDR:
3149a4365d0SYoshinobu Inoue 		case RTM_DELADDR:
3159a4365d0SYoshinobu Inoue 			ifam = (struct ifa_msghdr *)rtm;
3169a4365d0SYoshinobu Inoue 
3179a4365d0SYoshinobu Inoue 			/* address related checks */
3189a4365d0SYoshinobu Inoue 			sa = (struct sockaddr *)(ifam + 1);
3199a4365d0SYoshinobu Inoue 			get_rtaddrs(ifam->ifam_addrs, sa, rti_info);
3209a4365d0SYoshinobu Inoue 			if ((ifa = rti_info[RTAX_IFA]) == NULL ||
3219a4365d0SYoshinobu Inoue 			    (ifa->sa_family != AF_INET &&
3229a4365d0SYoshinobu Inoue 			     ifa->sa_family != AF_INET6))
3239a4365d0SYoshinobu Inoue 				continue;
3249a4365d0SYoshinobu Inoue 
3259a4365d0SYoshinobu Inoue 			if (ifa->sa_family == AF_INET6 &&
3269a4365d0SYoshinobu Inoue 			    (IN6_IS_ADDR_LINKLOCAL(&SIN6(ifa)->sin6_addr) ||
3279a4365d0SYoshinobu Inoue 			     IN6_IS_ADDR_MULTICAST(&SIN6(ifa)->sin6_addr)))
3289a4365d0SYoshinobu Inoue 				continue;
3299a4365d0SYoshinobu Inoue 
3309a4365d0SYoshinobu Inoue 			if (ifindex && ifam->ifam_index != ifindex)
3319a4365d0SYoshinobu Inoue 				continue;
3329a4365d0SYoshinobu Inoue 
3339a4365d0SYoshinobu Inoue 			/* found */
3349a4365d0SYoshinobu Inoue 			*lenp = ifam->ifam_msglen;
3359a4365d0SYoshinobu Inoue 			return (char *)rtm;
3369a4365d0SYoshinobu Inoue 			/* NOTREACHED */
3379a4365d0SYoshinobu Inoue 		case RTM_IFINFO:
338*db82af41SHiroki Sato 		case RTM_IFANNOUNCE:
3399a4365d0SYoshinobu Inoue 			/* found */
3409a4365d0SYoshinobu Inoue 			*lenp = rtm->rtm_msglen;
3419a4365d0SYoshinobu Inoue 			return (char *)rtm;
3429a4365d0SYoshinobu Inoue 			/* NOTREACHED */
3439a4365d0SYoshinobu Inoue 		}
3449a4365d0SYoshinobu Inoue 	}
3459a4365d0SYoshinobu Inoue 
346*db82af41SHiroki Sato 	return ((char *)rtm);
3479a4365d0SYoshinobu Inoue }
34833841545SHajimu UMEMOTO #undef FILTER_MATCH
3499a4365d0SYoshinobu Inoue 
3509a4365d0SYoshinobu Inoue struct in6_addr *
3519a4365d0SYoshinobu Inoue get_addr(char *buf)
3529a4365d0SYoshinobu Inoue {
3539a4365d0SYoshinobu Inoue 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
3549a4365d0SYoshinobu Inoue 	struct sockaddr *sa, *rti_info[RTAX_MAX];
3559a4365d0SYoshinobu Inoue 
3569a4365d0SYoshinobu Inoue 	sa = (struct sockaddr *)(rtm + 1);
3579a4365d0SYoshinobu Inoue 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
3589a4365d0SYoshinobu Inoue 
3599a4365d0SYoshinobu Inoue 	return (&SIN6(rti_info[RTAX_DST])->sin6_addr);
3609a4365d0SYoshinobu Inoue }
3619a4365d0SYoshinobu Inoue 
3629a4365d0SYoshinobu Inoue int
3639a4365d0SYoshinobu Inoue get_rtm_ifindex(char *buf)
3649a4365d0SYoshinobu Inoue {
3659a4365d0SYoshinobu Inoue 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
3669a4365d0SYoshinobu Inoue 	struct sockaddr *sa, *rti_info[RTAX_MAX];
3679a4365d0SYoshinobu Inoue 
3689a4365d0SYoshinobu Inoue 	sa = (struct sockaddr *)(rtm + 1);
3699a4365d0SYoshinobu Inoue 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
3709a4365d0SYoshinobu Inoue 
3719a4365d0SYoshinobu Inoue 	return (((struct sockaddr_dl *)rti_info[RTAX_GATEWAY])->sdl_index);
3729a4365d0SYoshinobu Inoue }
3739a4365d0SYoshinobu Inoue 
3749a4365d0SYoshinobu Inoue int
3759a4365d0SYoshinobu Inoue get_ifm_ifindex(char *buf)
3769a4365d0SYoshinobu Inoue {
3779a4365d0SYoshinobu Inoue 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
3789a4365d0SYoshinobu Inoue 
3799a4365d0SYoshinobu Inoue 	return ((int)ifm->ifm_index);
3809a4365d0SYoshinobu Inoue }
3819a4365d0SYoshinobu Inoue 
3829a4365d0SYoshinobu Inoue int
3839a4365d0SYoshinobu Inoue get_ifam_ifindex(char *buf)
3849a4365d0SYoshinobu Inoue {
3859a4365d0SYoshinobu Inoue 	struct ifa_msghdr *ifam = (struct ifa_msghdr *)buf;
3869a4365d0SYoshinobu Inoue 
3879a4365d0SYoshinobu Inoue 	return ((int)ifam->ifam_index);
3889a4365d0SYoshinobu Inoue }
3899a4365d0SYoshinobu Inoue 
3909a4365d0SYoshinobu Inoue int
3919a4365d0SYoshinobu Inoue get_ifm_flags(char *buf)
3929a4365d0SYoshinobu Inoue {
3939a4365d0SYoshinobu Inoue 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
3949a4365d0SYoshinobu Inoue 
3959a4365d0SYoshinobu Inoue 	return (ifm->ifm_flags);
3969a4365d0SYoshinobu Inoue }
3979a4365d0SYoshinobu Inoue 
3989a4365d0SYoshinobu Inoue int
3999a4365d0SYoshinobu Inoue get_prefixlen(char *buf)
4009a4365d0SYoshinobu Inoue {
4019a4365d0SYoshinobu Inoue 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
4029a4365d0SYoshinobu Inoue 	struct sockaddr *sa, *rti_info[RTAX_MAX];
4039a4365d0SYoshinobu Inoue 	u_char *p, *lim;
4049a4365d0SYoshinobu Inoue 
4059a4365d0SYoshinobu Inoue 	sa = (struct sockaddr *)(rtm + 1);
4069a4365d0SYoshinobu Inoue 	get_rtaddrs(rtm->rtm_addrs, sa, rti_info);
4079a4365d0SYoshinobu Inoue 	sa = rti_info[RTAX_NETMASK];
4089a4365d0SYoshinobu Inoue 
4099a4365d0SYoshinobu Inoue 	p = (u_char *)(&SIN6(sa)->sin6_addr);
4109a4365d0SYoshinobu Inoue 	lim = (u_char *)sa + sa->sa_len;
4117c991abcSHajimu UMEMOTO 	return prefixlen(p, lim);
4127c991abcSHajimu UMEMOTO }
4137c991abcSHajimu UMEMOTO 
4147c991abcSHajimu UMEMOTO int
4157c991abcSHajimu UMEMOTO prefixlen(u_char *p, u_char *lim)
4167c991abcSHajimu UMEMOTO {
4177c991abcSHajimu UMEMOTO 	int masklen;
4187c991abcSHajimu UMEMOTO 
4199a4365d0SYoshinobu Inoue 	for (masklen = 0; p < lim; p++) {
4209a4365d0SYoshinobu Inoue 		switch (*p) {
4219a4365d0SYoshinobu Inoue 		case 0xff:
4229a4365d0SYoshinobu Inoue 			masklen += 8;
4239a4365d0SYoshinobu Inoue 			break;
4249a4365d0SYoshinobu Inoue 		case 0xfe:
4259a4365d0SYoshinobu Inoue 			masklen += 7;
4269a4365d0SYoshinobu Inoue 			break;
4279a4365d0SYoshinobu Inoue 		case 0xfc:
4289a4365d0SYoshinobu Inoue 			masklen += 6;
4299a4365d0SYoshinobu Inoue 			break;
4309a4365d0SYoshinobu Inoue 		case 0xf8:
4319a4365d0SYoshinobu Inoue 			masklen += 5;
4329a4365d0SYoshinobu Inoue 			break;
4339a4365d0SYoshinobu Inoue 		case 0xf0:
4349a4365d0SYoshinobu Inoue 			masklen += 4;
4359a4365d0SYoshinobu Inoue 			break;
4369a4365d0SYoshinobu Inoue 		case 0xe0:
4379a4365d0SYoshinobu Inoue 			masklen += 3;
4389a4365d0SYoshinobu Inoue 			break;
4399a4365d0SYoshinobu Inoue 		case 0xc0:
4409a4365d0SYoshinobu Inoue 			masklen += 2;
4419a4365d0SYoshinobu Inoue 			break;
4429a4365d0SYoshinobu Inoue 		case 0x80:
4439a4365d0SYoshinobu Inoue 			masklen += 1;
4449a4365d0SYoshinobu Inoue 			break;
4459a4365d0SYoshinobu Inoue 		case 0x00:
4469a4365d0SYoshinobu Inoue 			break;
4479a4365d0SYoshinobu Inoue 		default:
4489a4365d0SYoshinobu Inoue 			return (-1);
4499a4365d0SYoshinobu Inoue 		}
4509a4365d0SYoshinobu Inoue 	}
4519a4365d0SYoshinobu Inoue 
4529a4365d0SYoshinobu Inoue 	return (masklen);
4539a4365d0SYoshinobu Inoue }
4549a4365d0SYoshinobu Inoue 
4559a4365d0SYoshinobu Inoue int
4569a4365d0SYoshinobu Inoue rtmsg_type(char *buf)
4579a4365d0SYoshinobu Inoue {
4589a4365d0SYoshinobu Inoue 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
4599a4365d0SYoshinobu Inoue 
4609a4365d0SYoshinobu Inoue 	return (rtm->rtm_type);
4619a4365d0SYoshinobu Inoue }
4629a4365d0SYoshinobu Inoue 
4639a4365d0SYoshinobu Inoue int
4649a4365d0SYoshinobu Inoue rtmsg_len(char *buf)
4659a4365d0SYoshinobu Inoue {
4669a4365d0SYoshinobu Inoue 	struct rt_msghdr *rtm = (struct rt_msghdr *)buf;
4679a4365d0SYoshinobu Inoue 
4689a4365d0SYoshinobu Inoue 	return (rtm->rtm_msglen);
4699a4365d0SYoshinobu Inoue }
4709a4365d0SYoshinobu Inoue 
4719a4365d0SYoshinobu Inoue int
4729a4365d0SYoshinobu Inoue ifmsg_len(char *buf)
4739a4365d0SYoshinobu Inoue {
4749a4365d0SYoshinobu Inoue 	struct if_msghdr *ifm = (struct if_msghdr *)buf;
4759a4365d0SYoshinobu Inoue 
4769a4365d0SYoshinobu Inoue 	return (ifm->ifm_msglen);
4779a4365d0SYoshinobu Inoue }
4789a4365d0SYoshinobu Inoue 
4799a4365d0SYoshinobu Inoue /*
4809a4365d0SYoshinobu Inoue  * alloc buffer and get if_msghdrs block from kernel,
4819a4365d0SYoshinobu Inoue  * and put them into the buffer
4829a4365d0SYoshinobu Inoue  */
4839a4365d0SYoshinobu Inoue static void
4849a4365d0SYoshinobu Inoue get_iflist(char **buf, size_t *size)
4859a4365d0SYoshinobu Inoue {
4869a4365d0SYoshinobu Inoue 	int mib[6];
4879a4365d0SYoshinobu Inoue 
4889a4365d0SYoshinobu Inoue 	mib[0] = CTL_NET;
4899a4365d0SYoshinobu Inoue 	mib[1] = PF_ROUTE;
4909a4365d0SYoshinobu Inoue 	mib[2] = 0;
4919a4365d0SYoshinobu Inoue 	mib[3] = AF_INET6;
4929a4365d0SYoshinobu Inoue 	mib[4] = NET_RT_IFLIST;
4939a4365d0SYoshinobu Inoue 	mib[5] = 0;
4949a4365d0SYoshinobu Inoue 
4959a4365d0SYoshinobu Inoue 	if (sysctl(mib, 6, NULL, size, NULL, 0) < 0) {
4969a4365d0SYoshinobu Inoue 		syslog(LOG_ERR, "<%s> sysctl: iflist size get failed",
4971533bed0SHajimu UMEMOTO 		    __func__);
4989a4365d0SYoshinobu Inoue 		exit(1);
4999a4365d0SYoshinobu Inoue 	}
5009a4365d0SYoshinobu Inoue 	if ((*buf = malloc(*size)) == NULL) {
5011533bed0SHajimu UMEMOTO 		syslog(LOG_ERR, "<%s> malloc failed", __func__);
5029a4365d0SYoshinobu Inoue 		exit(1);
5039a4365d0SYoshinobu Inoue 	}
5049a4365d0SYoshinobu Inoue 	if (sysctl(mib, 6, *buf, size, NULL, 0) < 0) {
5059a4365d0SYoshinobu Inoue 		syslog(LOG_ERR, "<%s> sysctl: iflist get failed",
5061533bed0SHajimu UMEMOTO 		     __func__);
5079a4365d0SYoshinobu Inoue 		exit(1);
5089a4365d0SYoshinobu Inoue 	}
5099a4365d0SYoshinobu Inoue 	return;
5109a4365d0SYoshinobu Inoue }
5119a4365d0SYoshinobu Inoue 
5129a4365d0SYoshinobu Inoue /*
5139a4365d0SYoshinobu Inoue  * alloc buffer and parse if_msghdrs block passed as arg,
5149a4365d0SYoshinobu Inoue  * and init the buffer as list of pointers ot each of the if_msghdr.
5159a4365d0SYoshinobu Inoue  */
5169a4365d0SYoshinobu Inoue static void
5179a4365d0SYoshinobu Inoue parse_iflist(struct if_msghdr ***ifmlist_p, char *buf, size_t bufsize)
5189a4365d0SYoshinobu Inoue {
5199a4365d0SYoshinobu Inoue 	int iflentry_size, malloc_size;
5209a4365d0SYoshinobu Inoue 	struct if_msghdr *ifm;
5219a4365d0SYoshinobu Inoue 	struct ifa_msghdr *ifam;
5229a4365d0SYoshinobu Inoue 	char *lim;
5239a4365d0SYoshinobu Inoue 
5249a4365d0SYoshinobu Inoue 	/*
5259a4365d0SYoshinobu Inoue 	 * Estimate least size of an iflist entry, to be obtained from kernel.
5269a4365d0SYoshinobu Inoue 	 * Should add sizeof(sockaddr) ??
5279a4365d0SYoshinobu Inoue 	 */
5289a4365d0SYoshinobu Inoue 	iflentry_size = sizeof(struct if_msghdr);
5299a4365d0SYoshinobu Inoue 	/* roughly estimate max list size of pointers to each if_msghdr */
5309a4365d0SYoshinobu Inoue 	malloc_size = (bufsize/iflentry_size) * sizeof(size_t);
5319a4365d0SYoshinobu Inoue 	if ((*ifmlist_p = (struct if_msghdr **)malloc(malloc_size)) == NULL) {
5321533bed0SHajimu UMEMOTO 		syslog(LOG_ERR, "<%s> malloc failed", __func__);
5339a4365d0SYoshinobu Inoue 		exit(1);
5349a4365d0SYoshinobu Inoue 	}
5359a4365d0SYoshinobu Inoue 
5369a4365d0SYoshinobu Inoue 	lim = buf + bufsize;
5379a4365d0SYoshinobu Inoue 	for (ifm = (struct if_msghdr *)buf; ifm < (struct if_msghdr *)lim;) {
5389a4365d0SYoshinobu Inoue 		if (ifm->ifm_msglen == 0) {
5399a4365d0SYoshinobu Inoue 			syslog(LOG_WARNING, "<%s> ifm_msglen is 0 "
5401533bed0SHajimu UMEMOTO 			    "(buf=%p lim=%p ifm=%p)", __func__,
5419a4365d0SYoshinobu Inoue 			    buf, lim, ifm);
5429a4365d0SYoshinobu Inoue 			return;
5439a4365d0SYoshinobu Inoue 		}
5449a4365d0SYoshinobu Inoue 
5459a4365d0SYoshinobu Inoue 		if (ifm->ifm_type == RTM_IFINFO) {
5469a4365d0SYoshinobu Inoue 			(*ifmlist_p)[ifm->ifm_index] = ifm;
5479a4365d0SYoshinobu Inoue 		} else {
5489a4365d0SYoshinobu Inoue 			syslog(LOG_ERR, "out of sync parsing NET_RT_IFLIST\n"
5499a4365d0SYoshinobu Inoue 			    "expected %d, got %d\n msglen = %d\n"
5509a4365d0SYoshinobu Inoue 			    "buf:%p, ifm:%p, lim:%p\n",
5519a4365d0SYoshinobu Inoue 			    RTM_IFINFO, ifm->ifm_type, ifm->ifm_msglen,
5529a4365d0SYoshinobu Inoue 			    buf, ifm, lim);
5539a4365d0SYoshinobu Inoue 			exit (1);
5549a4365d0SYoshinobu Inoue 		}
5559a4365d0SYoshinobu Inoue 		for (ifam = (struct ifa_msghdr *)
5569a4365d0SYoshinobu Inoue 			((char *)ifm + ifm->ifm_msglen);
5579a4365d0SYoshinobu Inoue 		     ifam < (struct ifa_msghdr *)lim;
5589a4365d0SYoshinobu Inoue 		     ifam = (struct ifa_msghdr *)
5599a4365d0SYoshinobu Inoue 		     	((char *)ifam + ifam->ifam_msglen)) {
5609a4365d0SYoshinobu Inoue 			/* just for safety */
5619a4365d0SYoshinobu Inoue 			if (!ifam->ifam_msglen) {
5629a4365d0SYoshinobu Inoue 				syslog(LOG_WARNING, "<%s> ifa_msglen is 0 "
5631533bed0SHajimu UMEMOTO 				    "(buf=%p lim=%p ifam=%p)", __func__,
5649a4365d0SYoshinobu Inoue 				    buf, lim, ifam);
5659a4365d0SYoshinobu Inoue 				return;
5669a4365d0SYoshinobu Inoue 			}
5679a4365d0SYoshinobu Inoue 			if (ifam->ifam_type != RTM_NEWADDR)
5689a4365d0SYoshinobu Inoue 				break;
5699a4365d0SYoshinobu Inoue 		}
5709a4365d0SYoshinobu Inoue 		ifm = (struct if_msghdr *)ifam;
5719a4365d0SYoshinobu Inoue 	}
5729a4365d0SYoshinobu Inoue }
5739a4365d0SYoshinobu Inoue 
5749a4365d0SYoshinobu Inoue void
575*db82af41SHiroki Sato init_iflist(void)
5769a4365d0SYoshinobu Inoue {
577*db82af41SHiroki Sato 	syslog(LOG_DEBUG,
578*db82af41SHiroki Sato 	    "<%s> generate iflist.", __func__);
579*db82af41SHiroki Sato 
5809a4365d0SYoshinobu Inoue 	if (ifblock) {
5819a4365d0SYoshinobu Inoue 		free(ifblock);
5829a4365d0SYoshinobu Inoue 		ifblock_size = 0;
5839a4365d0SYoshinobu Inoue 	}
5849a4365d0SYoshinobu Inoue 	if (iflist)
5859a4365d0SYoshinobu Inoue 		free(iflist);
5869a4365d0SYoshinobu Inoue 	/* get iflist block from kernel */
5879a4365d0SYoshinobu Inoue 	get_iflist(&ifblock, &ifblock_size);
5889a4365d0SYoshinobu Inoue 
5899a4365d0SYoshinobu Inoue 	/* make list of pointers to each if_msghdr */
5909a4365d0SYoshinobu Inoue 	parse_iflist(&iflist, ifblock, ifblock_size);
5919a4365d0SYoshinobu Inoue }
592