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