Home | History | Annotate | Line # | Download | only in rtadvd
rtadvd.c revision 1.5
      1  1.5  itojun /*	$NetBSD: rtadvd.c,v 1.5 2000/02/02 04:07:51 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/uio.h>
     35  1.1  itojun #include <sys/time.h>
     36  1.1  itojun 
     37  1.1  itojun #include <net/if.h>
     38  1.1  itojun #include <net/route.h>
     39  1.1  itojun #include <net/if_dl.h>
     40  1.1  itojun #include <netinet/in.h>
     41  1.1  itojun #include <netinet/ip6.h>
     42  1.1  itojun #include <netinet6/ip6_var.h>
     43  1.1  itojun #include <netinet/icmp6.h>
     44  1.1  itojun 
     45  1.1  itojun #include <arpa/inet.h>
     46  1.1  itojun 
     47  1.1  itojun #include <time.h>
     48  1.1  itojun #include <unistd.h>
     49  1.1  itojun #include <stdio.h>
     50  1.1  itojun #include <err.h>
     51  1.1  itojun #include <errno.h>
     52  1.1  itojun #include <string.h>
     53  1.1  itojun #include <stdlib.h>
     54  1.1  itojun #include <syslog.h>
     55  1.1  itojun #include "rtadvd.h"
     56  1.1  itojun #include "rrenum.h"
     57  1.1  itojun #include "advcap.h"
     58  1.1  itojun #include "timer.h"
     59  1.1  itojun #include "if.h"
     60  1.1  itojun #include "config.h"
     61  1.1  itojun 
     62  1.1  itojun struct msghdr rcvmhdr;
     63  1.1  itojun static u_char rcvcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
     64  1.1  itojun 			CMSG_SPACE(sizeof(int))];
     65  1.1  itojun struct msghdr sndmhdr;
     66  1.1  itojun struct iovec rcviov[2];
     67  1.1  itojun struct iovec sndiov[2];
     68  1.1  itojun struct sockaddr_in6 from;
     69  1.1  itojun struct sockaddr_in6 sin6_allnodes = {sizeof(sin6_allnodes), AF_INET6};
     70  1.1  itojun int sock, rtsock;
     71  1.5  itojun int accept_rr = 0;
     72  1.1  itojun int dflag = 0, sflag = 0;
     73  1.1  itojun 
     74  1.1  itojun u_char *conffile = NULL;
     75  1.1  itojun 
     76  1.1  itojun struct rainfo *ralist = NULL;
     77  1.1  itojun struct nd_optlist {
     78  1.1  itojun 	struct nd_optlist *next;
     79  1.1  itojun 	struct nd_opt_hdr *opt;
     80  1.1  itojun };
     81  1.1  itojun union nd_opts {
     82  1.1  itojun 	struct nd_opt_hdr *nd_opt_array[7];
     83  1.1  itojun 	struct {
     84  1.1  itojun 		struct nd_opt_hdr *zero;
     85  1.1  itojun 		struct nd_opt_hdr *src_lladdr;
     86  1.1  itojun 		struct nd_opt_hdr *tgt_lladdr;
     87  1.1  itojun 		struct nd_opt_prefix_info *pi;
     88  1.1  itojun 		struct nd_opt_rd_hdr *rh;
     89  1.1  itojun 		struct nd_opt_mtu *mtu;
     90  1.1  itojun 		struct nd_optlist *list;
     91  1.1  itojun 	} nd_opt_each;
     92  1.1  itojun };
     93  1.1  itojun #define nd_opts_src_lladdr	nd_opt_each.src_lladdr
     94  1.1  itojun #define nd_opts_tgt_lladdr	nd_opt_each.tgt_lladdr
     95  1.1  itojun #define nd_opts_pi		nd_opt_each.pi
     96  1.1  itojun #define nd_opts_rh		nd_opt_each.rh
     97  1.1  itojun #define nd_opts_mtu		nd_opt_each.mtu
     98  1.1  itojun #define nd_opts_list		nd_opt_each.list
     99  1.1  itojun 
    100  1.1  itojun #define NDOPT_FLAG_SRCLINKADDR 0x1
    101  1.1  itojun #define NDOPT_FLAG_TGTLINKADDR 0x2
    102  1.1  itojun #define NDOPT_FLAG_PREFIXINFO 0x4
    103  1.1  itojun #define NDOPT_FLAG_RDHDR 0x8
    104  1.1  itojun #define NDOPT_FLAG_MTU 0x10
    105  1.1  itojun 
    106  1.1  itojun u_int32_t ndopt_flags[] = {
    107  1.1  itojun 	0, NDOPT_FLAG_SRCLINKADDR, NDOPT_FLAG_TGTLINKADDR,
    108  1.1  itojun 	NDOPT_FLAG_PREFIXINFO, NDOPT_FLAG_RDHDR, NDOPT_FLAG_MTU
    109  1.1  itojun };
    110  1.1  itojun 
    111  1.1  itojun int main __P((int, char *[]));
    112  1.5  itojun static void die __P((int));
    113  1.1  itojun static void sock_open __P((void));
    114  1.1  itojun static void rtsock_open __P((void));
    115  1.1  itojun static void rtadvd_input __P((void));
    116  1.1  itojun static void rs_input __P((int, struct nd_router_solicit *,
    117  1.1  itojun 			  struct in6_pktinfo *, struct sockaddr_in6 *));
    118  1.1  itojun static void ra_input __P((int, struct nd_router_advert *,
    119  1.1  itojun 			  struct in6_pktinfo *, struct sockaddr_in6 *));
    120  1.1  itojun static void prefix_check __P((struct nd_opt_prefix_info *, struct rainfo *,
    121  1.1  itojun 			      struct sockaddr_in6 *));
    122  1.1  itojun static int nd6_options __P((struct nd_opt_hdr *, int,
    123  1.1  itojun 			    union nd_opts *, u_int32_t));
    124  1.1  itojun static void free_ndopts __P((union nd_opts *));
    125  1.1  itojun static struct rainfo *if_indextorainfo __P((int));
    126  1.1  itojun static void ra_output __P((struct rainfo *));
    127  1.1  itojun static void rtmsg_input __P((void));
    128  1.1  itojun struct prefix *find_prefix __P((struct rainfo *, struct in6_addr *, int));
    129  1.1  itojun 
    130  1.1  itojun 
    131  1.1  itojun int
    132  1.1  itojun main(argc, argv)
    133  1.1  itojun 	int argc;
    134  1.1  itojun 	char *argv[];
    135  1.1  itojun {
    136  1.1  itojun 	fd_set fdset;
    137  1.1  itojun 	int maxfd = 0;
    138  1.1  itojun 	struct timeval *timeout;
    139  1.1  itojun 	int i, ch;
    140  1.1  itojun 	int fflag = 0;
    141  1.1  itojun 
    142  1.1  itojun 	openlog(*argv, LOG_NDELAY|LOG_PID, LOG_DAEMON);
    143  1.1  itojun 
    144  1.1  itojun 	/* get command line options and arguments */
    145  1.5  itojun 	while ((ch = getopt(argc, argv, "c:dDfRs")) != -1) {
    146  1.1  itojun 		switch(ch) {
    147  1.1  itojun 		 case 'c':
    148  1.1  itojun 			 conffile = optarg;
    149  1.1  itojun 			 break;
    150  1.1  itojun 		 case 'd':
    151  1.1  itojun 			 dflag = 1;
    152  1.1  itojun 			 break;
    153  1.1  itojun 		 case 'D':
    154  1.1  itojun 			 dflag = 2;
    155  1.1  itojun 			 break;
    156  1.1  itojun 		 case 'f':
    157  1.1  itojun 			 fflag = 1;
    158  1.1  itojun 			 break;
    159  1.5  itojun 		 case 'R':
    160  1.5  itojun 			 accept_rr = 1;
    161  1.5  itojun 			 break;
    162  1.1  itojun 		 case 's':
    163  1.1  itojun 			 sflag = 1;
    164  1.5  itojun 			 break;
    165  1.1  itojun 		}
    166  1.1  itojun 	}
    167  1.1  itojun 	argc -= optind;
    168  1.1  itojun 	argv += optind;
    169  1.1  itojun 	if (argc == 0) {
    170  1.1  itojun 		fprintf(stderr,
    171  1.5  itojun 			"usage: rtadvd [-dDfsR] [-c conffile] "
    172  1.1  itojun 			"interfaces...\n");
    173  1.1  itojun 		exit(1);
    174  1.1  itojun 	}
    175  1.1  itojun 
    176  1.1  itojun 	/* set log level */
    177  1.1  itojun 	if (dflag == 0)
    178  1.1  itojun 		(void)setlogmask(LOG_UPTO(LOG_ERR));
    179  1.1  itojun 	if (dflag == 1)
    180  1.1  itojun 		(void)setlogmask(LOG_UPTO(LOG_INFO));
    181  1.1  itojun 
    182  1.1  itojun 	/* timer initialization */
    183  1.1  itojun 	rtadvd_timer_init();
    184  1.1  itojun 
    185  1.1  itojun 	/* random value initialization */
    186  1.1  itojun 	srandom((u_long)time(NULL));
    187  1.1  itojun 
    188  1.1  itojun 	/* get iflist block from kernel */
    189  1.1  itojun 	init_iflist();
    190  1.1  itojun 
    191  1.1  itojun 	while (argc--)
    192  1.1  itojun 		getconfig(*argv++);
    193  1.1  itojun 
    194  1.3  itojun 	if (inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr) != 1) {
    195  1.3  itojun 		fprintf(stderr, "fatal: inet_pton failed\n");
    196  1.3  itojun 		exit(1);
    197  1.3  itojun 	}
    198  1.1  itojun 	sock_open();
    199  1.1  itojun 
    200  1.1  itojun 	if (!fflag)
    201  1.1  itojun 		daemon(1, 0);
    202  1.1  itojun 
    203  1.1  itojun 	FD_ZERO(&fdset);
    204  1.1  itojun 	FD_SET(sock, &fdset);
    205  1.1  itojun 	maxfd = sock;
    206  1.1  itojun 	if (sflag == 0) {
    207  1.1  itojun 		rtsock_open();
    208  1.1  itojun 		FD_SET(rtsock, &fdset);
    209  1.1  itojun 		if (rtsock > sock)
    210  1.1  itojun 			maxfd = rtsock;
    211  1.1  itojun 	}
    212  1.1  itojun 
    213  1.5  itojun 	signal(SIGTERM, die);
    214  1.5  itojun 
    215  1.1  itojun 	while (1) {
    216  1.1  itojun 		struct fd_set select_fd = fdset; /* reinitialize */
    217  1.1  itojun 
    218  1.1  itojun 		/* timer expiration check and reset the timer */
    219  1.1  itojun 		timeout = rtadvd_check_timer();
    220  1.1  itojun 
    221  1.1  itojun 		syslog(LOG_DEBUG,
    222  1.1  itojun 		       "<%s> set timer to %ld:%ld. waiting for inputs "
    223  1.1  itojun 		       "or timeout",
    224  1.1  itojun 		       __FUNCTION__,
    225  1.1  itojun 		       timeout->tv_sec, timeout->tv_usec);
    226  1.1  itojun 
    227  1.1  itojun 		if ((i = select(maxfd + 1, &select_fd,
    228  1.1  itojun 				NULL, NULL, timeout)) < 0){
    229  1.1  itojun 			syslog(LOG_ERR, "<%s> select: %s",
    230  1.1  itojun 			       __FUNCTION__, strerror(errno));
    231  1.1  itojun 			continue;
    232  1.1  itojun 		}
    233  1.1  itojun 		if (i == 0)	/* timeout */
    234  1.1  itojun 			continue;
    235  1.1  itojun 		if (sflag == 0 && FD_ISSET(rtsock, &select_fd))
    236  1.1  itojun 			rtmsg_input();
    237  1.1  itojun 		if (FD_ISSET(sock, &select_fd))
    238  1.1  itojun 			rtadvd_input();
    239  1.1  itojun 	}
    240  1.1  itojun 	exit(0);		/* NOTREACHED */
    241  1.1  itojun }
    242  1.1  itojun 
    243  1.1  itojun static void
    244  1.5  itojun die(sig)
    245  1.5  itojun 	int sig;
    246  1.5  itojun {
    247  1.5  itojun 	struct rainfo *ra;
    248  1.5  itojun 	int i;
    249  1.5  itojun 	const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
    250  1.5  itojun 
    251  1.5  itojun 	if (dflag > 1) {
    252  1.5  itojun 		syslog(LOG_DEBUG, "<%s> cease to be an advertising router\n",
    253  1.5  itojun 		    __FUNCTION__);
    254  1.5  itojun 	}
    255  1.5  itojun 
    256  1.5  itojun 	for (ra = ralist; ra; ra = ra->next) {
    257  1.5  itojun 		ra->lifetime = 0;
    258  1.5  itojun 		make_packet(ra);
    259  1.5  itojun 	}
    260  1.5  itojun 	for (i = 0; i < retrans; i++) {
    261  1.5  itojun 		for (ra = ralist; ra; ra = ra->next)
    262  1.5  itojun 			ra_output(ra);
    263  1.5  itojun 		sleep(MIN_DELAY_BETWEEN_RAS);
    264  1.5  itojun 	}
    265  1.5  itojun 	exit(0);
    266  1.5  itojun 	/*NOTREACHED*/
    267  1.5  itojun }
    268  1.5  itojun 
    269  1.5  itojun static void
    270  1.1  itojun rtmsg_input()
    271  1.1  itojun {
    272  1.2  itojun 	int n, type, ifindex, plen;
    273  1.2  itojun 	size_t len;
    274  1.1  itojun 	char msg[2048], *next, *lim;
    275  1.1  itojun 	u_char ifname[16];
    276  1.1  itojun 	struct prefix *prefix;
    277  1.1  itojun 	struct rainfo *rai;
    278  1.1  itojun 	struct in6_addr *addr;
    279  1.1  itojun 	char addrbuf[INET6_ADDRSTRLEN];
    280  1.1  itojun 
    281  1.1  itojun 	n = read(rtsock, msg, 2048);
    282  1.1  itojun 	if (dflag > 1) {
    283  1.1  itojun 		syslog(LOG_DEBUG,
    284  1.1  itojun 		       "<%s> received a routing message "
    285  1.1  itojun 		       "(type = %d, len = %d)",
    286  1.1  itojun 		       __FUNCTION__,
    287  1.1  itojun 		       rtmsg_type(msg), n);
    288  1.1  itojun 	}
    289  1.1  itojun 	if (n > rtmsg_len(msg)) {
    290  1.1  itojun 		/*
    291  1.1  itojun 		 * This usually won't happen for messages received on
    292  1.1  itojun 		 * an routing socket.
    293  1.1  itojun 		 */
    294  1.1  itojun 		if (dflag > 1)
    295  1.1  itojun 			syslog(LOG_DEBUG,
    296  1.1  itojun 			       "<%s> received data length is larger than"
    297  1.1  itojun 			       "1st routing message len. multiple messages?"
    298  1.1  itojun 			       " read %d bytes, but 1st msg len = %d",
    299  1.1  itojun 			       __FUNCTION__, n, rtmsg_len(msg));
    300  1.1  itojun #if 0
    301  1.1  itojun 		/* adjust length */
    302  1.1  itojun 		n = rtmsg_len(msg);
    303  1.1  itojun #endif
    304  1.1  itojun 	}
    305  1.1  itojun 
    306  1.1  itojun 	lim = msg + n;
    307  1.1  itojun 	for (next = msg; next < lim; next += len) {
    308  1.1  itojun 		next = get_next_msg(next, lim, 0, &len,
    309  1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_ADD) |
    310  1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_DELETE) |
    311  1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_NEWADDR) |
    312  1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_DELADDR) |
    313  1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_IFINFO));
    314  1.1  itojun 		if (len == 0)
    315  1.1  itojun 			break;
    316  1.1  itojun 		type = rtmsg_type(next);
    317  1.1  itojun 		switch (type) {
    318  1.1  itojun 		case RTM_ADD:
    319  1.1  itojun 		case RTM_DELETE:
    320  1.1  itojun 			ifindex = get_rtm_ifindex(next);
    321  1.1  itojun 			break;
    322  1.1  itojun 		case RTM_NEWADDR:
    323  1.1  itojun 		case RTM_DELADDR:
    324  1.1  itojun 			ifindex = get_ifam_ifindex(next);
    325  1.1  itojun 			break;
    326  1.1  itojun 		case RTM_IFINFO:
    327  1.1  itojun 			ifindex = get_ifm_ifindex(next);
    328  1.1  itojun 			break;
    329  1.1  itojun 		default:
    330  1.1  itojun 			/* should not reach here */
    331  1.1  itojun 			if (dflag > 1) {
    332  1.1  itojun 				syslog(LOG_DEBUG,
    333  1.1  itojun 				       "<%s:%d> unknown rtmsg %d on %s",
    334  1.1  itojun 				       __FUNCTION__, __LINE__, type,
    335  1.1  itojun 				       if_indextoname(ifindex, ifname));
    336  1.1  itojun 			}
    337  1.1  itojun 			return;
    338  1.1  itojun 		}
    339  1.1  itojun 
    340  1.1  itojun 		if ((rai = if_indextorainfo(ifindex)) == NULL) {
    341  1.1  itojun 			if (dflag > 1) {
    342  1.1  itojun 				syslog(LOG_DEBUG,
    343  1.1  itojun 				       "<%s> route changed on "
    344  1.1  itojun 				       "non advertising interface(%s)",
    345  1.1  itojun 				       __FUNCTION__,
    346  1.1  itojun 				       if_indextoname(ifindex, ifname));
    347  1.1  itojun 			}
    348  1.1  itojun 			return;
    349  1.1  itojun 		}
    350  1.1  itojun 
    351  1.1  itojun 		switch(type) {
    352  1.1  itojun 		 case RTM_ADD:
    353  1.1  itojun 			 /* init iffalgs because it may have changed */
    354  1.1  itojun 			 iflist[ifindex]->ifm_flags =
    355  1.1  itojun 			 	if_getflags(ifindex,
    356  1.1  itojun 					    iflist[ifindex]->ifm_flags);
    357  1.1  itojun 
    358  1.1  itojun 			 addr = get_addr(msg);
    359  1.1  itojun 			 plen = get_prefixlen(msg);
    360  1.1  itojun 			 /* sanity check for plen */
    361  1.1  itojun 			 if (plen < 4 /* as RFC2373, prefixlen is at least 4 */
    362  1.1  itojun 			     || plen > 127) {
    363  1.1  itojun 				syslog(LOG_INFO, "<%s> new interface route's"
    364  1.1  itojun 				       "plen %d is invalid for a prefix",
    365  1.1  itojun 				       __FUNCTION__, plen);
    366  1.1  itojun 				return;
    367  1.1  itojun 			 }
    368  1.1  itojun 			 prefix = find_prefix(rai, addr, plen);
    369  1.1  itojun 			 if (prefix) {
    370  1.1  itojun 				 if (dflag > 1) {
    371  1.1  itojun 					 syslog(LOG_DEBUG,
    372  1.1  itojun 						"<%s> new prefix(%s/%d) "
    373  1.1  itojun 						"added on %s, "
    374  1.1  itojun 						"but it was already in list",
    375  1.1  itojun 						__FUNCTION__,
    376  1.1  itojun 						inet_ntop(AF_INET6,
    377  1.1  itojun 							  addr, (char *)addrbuf,
    378  1.1  itojun 							  INET6_ADDRSTRLEN),
    379  1.1  itojun 						plen,
    380  1.1  itojun 						rai->ifname);
    381  1.1  itojun 				 }
    382  1.1  itojun 				 return;
    383  1.1  itojun 			 }
    384  1.1  itojun 			 make_prefix(rai, ifindex, addr, plen);
    385  1.1  itojun 			 break;
    386  1.1  itojun 		 case RTM_DELETE:
    387  1.1  itojun 			 /* init ifflags because it may have changed */
    388  1.1  itojun 			 iflist[ifindex]->ifm_flags =
    389  1.1  itojun 			 	if_getflags(ifindex,
    390  1.1  itojun 					    iflist[ifindex]->ifm_flags);
    391  1.1  itojun 
    392  1.1  itojun 			 addr = get_addr(msg);
    393  1.1  itojun 			 plen = get_prefixlen(msg);
    394  1.1  itojun 			 /* sanity check for plen */
    395  1.1  itojun 			 if (plen < 4 /* as RFC2373, prefixlen is at least 4 */
    396  1.1  itojun 			     || plen > 127) {
    397  1.1  itojun 				syslog(LOG_INFO, "<%s> deleted interface"
    398  1.1  itojun 				       "route's"
    399  1.1  itojun 				       "plen %d is invalid for a prefix",
    400  1.1  itojun 				       __FUNCTION__, plen);
    401  1.1  itojun 				return;
    402  1.1  itojun 			 }
    403  1.1  itojun 			 prefix = find_prefix(rai, addr, plen);
    404  1.1  itojun 			 if (prefix == NULL) {
    405  1.1  itojun 				 if (dflag > 1) {
    406  1.1  itojun 					 syslog(LOG_DEBUG,
    407  1.1  itojun 						"<%s> prefix(%s/%d) was "
    408  1.1  itojun 						"deleted on %s, "
    409  1.1  itojun 						"but it was not in list",
    410  1.1  itojun 						__FUNCTION__,
    411  1.1  itojun 						inet_ntop(AF_INET6,
    412  1.1  itojun 							  addr, (char *)addrbuf,
    413  1.1  itojun 							  INET6_ADDRSTRLEN),
    414  1.1  itojun 						plen,
    415  1.1  itojun 						rai->ifname);
    416  1.1  itojun 				 }
    417  1.1  itojun 				 return;
    418  1.1  itojun 			 }
    419  1.1  itojun 			 delete_prefix(rai, prefix);
    420  1.1  itojun 			 break;
    421  1.1  itojun 		case RTM_NEWADDR:
    422  1.1  itojun 		case RTM_DELADDR:
    423  1.1  itojun 			 /* init ifflags because it may have changed */
    424  1.1  itojun 			 iflist[ifindex]->ifm_flags =
    425  1.1  itojun 			 	if_getflags(ifindex,
    426  1.1  itojun 					    iflist[ifindex]->ifm_flags);
    427  1.1  itojun 			 break;
    428  1.1  itojun 		case RTM_IFINFO:
    429  1.1  itojun 			 iflist[ifindex]->ifm_flags = get_ifm_flags(next);
    430  1.1  itojun 			 break;
    431  1.1  itojun 		default:
    432  1.1  itojun 			/* should not reach here */
    433  1.1  itojun 			if (dflag > 1) {
    434  1.1  itojun 				syslog(LOG_DEBUG,
    435  1.1  itojun 				       "<%s:%d> unknown rtmsg %d on %s",
    436  1.1  itojun 				       __FUNCTION__, __LINE__, type,
    437  1.1  itojun 				       if_indextoname(ifindex, ifname));
    438  1.1  itojun 			}
    439  1.1  itojun 			return;
    440  1.1  itojun 		}
    441  1.1  itojun 	}
    442  1.1  itojun 
    443  1.1  itojun 	return;
    444  1.1  itojun }
    445  1.1  itojun 
    446  1.1  itojun void
    447  1.1  itojun rtadvd_input()
    448  1.1  itojun {
    449  1.1  itojun 	int i;
    450  1.1  itojun 	int *hlimp = NULL;
    451  1.1  itojun #ifdef OLDRAWSOCKET
    452  1.1  itojun 	struct ip6_hdr *ip;
    453  1.1  itojun #endif
    454  1.1  itojun 	struct icmp6_hdr *icp;
    455  1.1  itojun 	int ifindex = 0;
    456  1.1  itojun 	struct cmsghdr *cm;
    457  1.1  itojun 	struct in6_pktinfo *pi = NULL;
    458  1.1  itojun 	u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
    459  1.1  itojun 	struct in6_addr dst = in6addr_any;
    460  1.1  itojun 
    461  1.1  itojun 	/*
    462  1.1  itojun 	 * Get message. We reset msg_controllen since the field could
    463  1.1  itojun 	 * be modified if we had received a message before setting
    464  1.1  itojun 	 * receive options.
    465  1.1  itojun 	 */
    466  1.1  itojun 	rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf);
    467  1.1  itojun 	if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
    468  1.1  itojun 		return;
    469  1.1  itojun 
    470  1.1  itojun 	/* extract optional information via Advanced API */
    471  1.1  itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
    472  1.1  itojun 	     cm;
    473  1.1  itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
    474  1.1  itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
    475  1.1  itojun 		    cm->cmsg_type == IPV6_PKTINFO &&
    476  1.1  itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
    477  1.1  itojun 			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
    478  1.1  itojun 			ifindex = pi->ipi6_ifindex;
    479  1.1  itojun 			dst = pi->ipi6_addr;
    480  1.1  itojun 		}
    481  1.1  itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
    482  1.1  itojun 		    cm->cmsg_type == IPV6_HOPLIMIT &&
    483  1.1  itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
    484  1.1  itojun 			hlimp = (int *)CMSG_DATA(cm);
    485  1.1  itojun 	}
    486  1.1  itojun 	if (ifindex == 0) {
    487  1.1  itojun 		syslog(LOG_ERR,
    488  1.1  itojun 		       "<%s> failed to get receiving interface",
    489  1.1  itojun 		       __FUNCTION__);
    490  1.1  itojun 		return;
    491  1.1  itojun 	}
    492  1.1  itojun 	if (hlimp == NULL) {
    493  1.1  itojun 		syslog(LOG_ERR,
    494  1.1  itojun 		       "<%s> failed to get receiving hop limit",
    495  1.1  itojun 		       __FUNCTION__);
    496  1.1  itojun 		return;
    497  1.1  itojun 	}
    498  1.1  itojun 
    499  1.1  itojun #ifdef OLDRAWSOCKET
    500  1.1  itojun 	if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
    501  1.1  itojun 		syslog(LOG_ERR,
    502  1.1  itojun 		       "<%s> packet size(%d) is too short",
    503  1.1  itojun 		       __FUNCTION__, i);
    504  1.1  itojun 		return;
    505  1.1  itojun 	}
    506  1.1  itojun 
    507  1.1  itojun 	ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
    508  1.1  itojun 	icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
    509  1.1  itojun #else
    510  1.1  itojun 	if (i < sizeof(struct icmp6_hdr)) {
    511  1.1  itojun 		syslog(LOG_ERR,
    512  1.1  itojun 		       "<%s> packet size(%d) is too short",
    513  1.1  itojun 		       __FUNCTION__, i);
    514  1.1  itojun 		return;
    515  1.1  itojun 	}
    516  1.1  itojun 
    517  1.1  itojun 	icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
    518  1.1  itojun #endif
    519  1.1  itojun 
    520  1.1  itojun 	switch(icp->icmp6_type) {
    521  1.1  itojun 	 case ND_ROUTER_SOLICIT:
    522  1.3  itojun 		 /*
    523  1.3  itojun 		  * Message verification - RFC-2461 6.1.1
    524  1.3  itojun 		  * XXX: these checks must be done in the kernel as well,
    525  1.3  itojun 		  *      but we can't completely rely on them.
    526  1.3  itojun 		  */
    527  1.1  itojun 		 if (*hlimp != 255) {
    528  1.1  itojun 			 syslog(LOG_NOTICE,
    529  1.3  itojun 				"<%s> RS with invalid hop limit(%d) "
    530  1.1  itojun 				"received from %s on %s",
    531  1.1  itojun 				__FUNCTION__, *hlimp,
    532  1.1  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    533  1.1  itojun 					  INET6_ADDRSTRLEN),
    534  1.1  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    535  1.1  itojun 			 return;
    536  1.1  itojun 		 }
    537  1.3  itojun 		 if (icp->icmp6_code) {
    538  1.3  itojun 			 syslog(LOG_NOTICE,
    539  1.3  itojun 				"<%s> RS with invalid ICMP6 code(%d) "
    540  1.3  itojun 				"received from %s on %s",
    541  1.3  itojun 				__FUNCTION__, icp->icmp6_code,
    542  1.3  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    543  1.3  itojun 					  INET6_ADDRSTRLEN),
    544  1.3  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    545  1.3  itojun 			 return;
    546  1.3  itojun 		 }
    547  1.3  itojun 		 if (i < sizeof(struct nd_router_solicit)) {
    548  1.3  itojun 			 syslog(LOG_NOTICE,
    549  1.3  itojun 				"<%s> RS from %s on %s does not have enough "
    550  1.3  itojun 				"length (len = %d)",
    551  1.3  itojun 				__FUNCTION__,
    552  1.3  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    553  1.3  itojun 					  INET6_ADDRSTRLEN),
    554  1.3  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
    555  1.3  itojun 			 return;
    556  1.3  itojun 		 }
    557  1.1  itojun 		 rs_input(i, (struct nd_router_solicit *)icp, pi, &from);
    558  1.1  itojun 		 break;
    559  1.1  itojun 	 case ND_ROUTER_ADVERT:
    560  1.3  itojun 		 /*
    561  1.3  itojun 		  * Message verification - RFC-2461 6.1.2
    562  1.3  itojun 		  * XXX: there's a same dilemma as above...
    563  1.3  itojun 		  */
    564  1.1  itojun 		 if (*hlimp != 255) {
    565  1.1  itojun 			 syslog(LOG_NOTICE,
    566  1.3  itojun 				"<%s> RA with invalid hop limit(%d) "
    567  1.1  itojun 				"received from %s on %s",
    568  1.1  itojun 				__FUNCTION__, *hlimp,
    569  1.1  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    570  1.1  itojun 					  INET6_ADDRSTRLEN),
    571  1.1  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    572  1.3  itojun 			 return;
    573  1.3  itojun 		 }
    574  1.3  itojun 		 if (icp->icmp6_code) {
    575  1.3  itojun 			 syslog(LOG_NOTICE,
    576  1.3  itojun 				"<%s> RA with invalid ICMP6 code(%d) "
    577  1.3  itojun 				"received from %s on %s",
    578  1.3  itojun 				__FUNCTION__, icp->icmp6_code,
    579  1.3  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    580  1.3  itojun 					  INET6_ADDRSTRLEN),
    581  1.3  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    582  1.3  itojun 			 return;
    583  1.3  itojun 		 }
    584  1.3  itojun 		 if (i < sizeof(struct nd_router_advert)) {
    585  1.3  itojun 			 syslog(LOG_NOTICE,
    586  1.3  itojun 				"<%s> RA from %s on %s does not have enough "
    587  1.3  itojun 				"length (len = %d)",
    588  1.3  itojun 				__FUNCTION__,
    589  1.3  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    590  1.3  itojun 					  INET6_ADDRSTRLEN),
    591  1.3  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
    592  1.1  itojun 			 return;
    593  1.1  itojun 		 }
    594  1.1  itojun 		 ra_input(i, (struct nd_router_advert *)icp, pi, &from);
    595  1.1  itojun 		 break;
    596  1.1  itojun 	 case ICMP6_ROUTER_RENUMBERING:
    597  1.1  itojun 		 if (accept_rr == 0) {
    598  1.1  itojun 			 syslog(LOG_ERR,
    599  1.1  itojun 				"<%s> received a router renumbering "
    600  1.1  itojun 				"message, but not allowed to be accepted",
    601  1.1  itojun 				__FUNCTION__);
    602  1.1  itojun 			 break;
    603  1.1  itojun 		 }
    604  1.1  itojun 		 rr_input(i, (struct icmp6_router_renum *)icp, pi, &from,
    605  1.1  itojun 			  &dst);
    606  1.1  itojun 		 break;
    607  1.1  itojun 	 default:
    608  1.1  itojun 		 /*
    609  1.1  itojun 		  * Note that this case is POSSIBLE, especially just
    610  1.1  itojun 		  * after invocation of the daemon. This is because we
    611  1.1  itojun 		  * could receive message after opening the socket and
    612  1.1  itojun 		  * before setting ICMP6 type filter(see sock_open()).
    613  1.1  itojun 		  */
    614  1.1  itojun 		 syslog(LOG_ERR,
    615  1.1  itojun 			"<%s> invalid icmp type(%d)",
    616  1.1  itojun 			__FUNCTION__, icp->icmp6_type);
    617  1.1  itojun 		 return;
    618  1.1  itojun 	}
    619  1.1  itojun 
    620  1.1  itojun 	return;
    621  1.1  itojun }
    622  1.1  itojun 
    623  1.1  itojun static void
    624  1.1  itojun rs_input(int len, struct nd_router_solicit *rs,
    625  1.1  itojun 	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
    626  1.1  itojun {
    627  1.1  itojun 	u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
    628  1.1  itojun 	union nd_opts ndopts;
    629  1.1  itojun 	struct rainfo *ra;
    630  1.1  itojun 
    631  1.1  itojun 	syslog(LOG_DEBUG,
    632  1.1  itojun 	       "<%s> RS received from %s on %s",
    633  1.1  itojun 	       __FUNCTION__,
    634  1.1  itojun 	       inet_ntop(AF_INET6, &from->sin6_addr,
    635  1.1  itojun 			 ntopbuf, INET6_ADDRSTRLEN),
    636  1.1  itojun 	       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    637  1.1  itojun 
    638  1.1  itojun 	/* ND option check */
    639  1.1  itojun 	memset(&ndopts, 0, sizeof(ndopts));
    640  1.1  itojun 	if (nd6_options((struct nd_opt_hdr *)(rs + 1),
    641  1.1  itojun 			len - sizeof(struct nd_router_solicit),
    642  1.1  itojun 			 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
    643  1.1  itojun 		syslog(LOG_DEBUG,
    644  1.1  itojun 		       "<%s> ND option check failed for an RS from %s on %s",
    645  1.1  itojun 		       __FUNCTION__,
    646  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    647  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    648  1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    649  1.1  itojun 		return;
    650  1.1  itojun 	}
    651  1.1  itojun 
    652  1.1  itojun 	/*
    653  1.1  itojun 	 * If the IP source address is the unspecified address, there
    654  1.1  itojun 	 * must be no source link-layer address option in the message.
    655  1.1  itojun 	 * (RFC-2461 6.1.1)
    656  1.1  itojun 	 */
    657  1.1  itojun 	if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
    658  1.1  itojun 	    ndopts.nd_opts_src_lladdr) {
    659  1.1  itojun 		syslog(LOG_ERR,
    660  1.1  itojun 		       "<%s> RS from unspecified src on %s has a link-layer"
    661  1.1  itojun 		       " address option",
    662  1.1  itojun 		       __FUNCTION__,
    663  1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    664  1.1  itojun 		goto done;
    665  1.1  itojun 	}
    666  1.1  itojun 
    667  1.1  itojun 	ra = ralist;
    668  1.1  itojun 	while (ra != NULL) {
    669  1.1  itojun 		if (pi->ipi6_ifindex == ra->ifindex)
    670  1.1  itojun 			break;
    671  1.1  itojun 		ra = ra->next;
    672  1.1  itojun 	}
    673  1.1  itojun 	if (ra == NULL) {
    674  1.1  itojun 		syslog(LOG_INFO,
    675  1.1  itojun 		       "<%s> RS received on non advertising interface(%s)",
    676  1.1  itojun 		       __FUNCTION__,
    677  1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    678  1.1  itojun 		goto done;
    679  1.1  itojun 	}
    680  1.1  itojun 
    681  1.1  itojun 	/*
    682  1.1  itojun 	 * Decide whether to send RA according to the rate-limit
    683  1.1  itojun 	 * consideration.
    684  1.1  itojun 	 */
    685  1.1  itojun 	{
    686  1.1  itojun 		long delay;	/* must not be greater than 1000000 */
    687  1.1  itojun 		struct timeval interval, now, min_delay, tm_tmp, *rest;
    688  1.1  itojun 
    689  1.1  itojun 		/*
    690  1.1  itojun 		 * If there is already a waiting RS packet, don't
    691  1.1  itojun 		 * update the timer.
    692  1.1  itojun 		 */
    693  1.1  itojun 		if (ra->waiting++)
    694  1.1  itojun 			goto done;
    695  1.1  itojun 
    696  1.1  itojun 		/*
    697  1.1  itojun 		 * Compute a random delay. If the computed value
    698  1.1  itojun 		 * corresponds to a time later than the time the next
    699  1.1  itojun 		 * multicast RA is scheduled to be sent, ignore the random
    700  1.1  itojun 		 * delay and send the advertisement at the
    701  1.1  itojun 		 * already-scheduled time. RFC-2461 6.2.6
    702  1.1  itojun 		 */
    703  1.1  itojun 		delay = random() % MAX_RA_DELAY_TIME;
    704  1.1  itojun 		interval.tv_sec = 0;
    705  1.1  itojun 		interval.tv_usec = delay;
    706  1.1  itojun 		rest = rtadvd_timer_rest(ra->timer);
    707  1.1  itojun 		if (TIMEVAL_LT(*rest, interval)) {
    708  1.1  itojun 			syslog(LOG_DEBUG,
    709  1.1  itojun 			       "<%s> random delay is larger than "
    710  1.1  itojun 			       "the rest of normal timer",
    711  1.1  itojun 			       __FUNCTION__);
    712  1.1  itojun 			interval = *rest;
    713  1.1  itojun 		}
    714  1.1  itojun 
    715  1.1  itojun 		/*
    716  1.1  itojun 		 * If we sent a multicast Router Advertisement within
    717  1.1  itojun 		 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
    718  1.1  itojun 		 * the advertisement to be sent at a time corresponding to
    719  1.1  itojun 		 * MIN_DELAY_BETWEEN_RAS plus the random value after the
    720  1.1  itojun 		 * previous advertisement was sent.
    721  1.1  itojun 		 */
    722  1.1  itojun 		gettimeofday(&now, NULL);
    723  1.1  itojun 		TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp);
    724  1.1  itojun 		min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
    725  1.1  itojun 		min_delay.tv_usec = 0;
    726  1.1  itojun 		if (TIMEVAL_LT(tm_tmp, min_delay)) {
    727  1.1  itojun 			TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
    728  1.1  itojun 			TIMEVAL_ADD(&min_delay, &interval, &interval);
    729  1.1  itojun 		}
    730  1.1  itojun 		rtadvd_set_timer(&interval, ra->timer);
    731  1.1  itojun 			goto done;
    732  1.1  itojun 	}
    733  1.1  itojun 
    734  1.1  itojun   done:
    735  1.1  itojun 	free_ndopts(&ndopts);
    736  1.1  itojun 	return;
    737  1.1  itojun }
    738  1.1  itojun 
    739  1.1  itojun static void
    740  1.1  itojun ra_input(int len, struct nd_router_advert *ra,
    741  1.1  itojun 	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
    742  1.1  itojun {
    743  1.1  itojun 	struct rainfo *rai;
    744  1.1  itojun 	u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
    745  1.1  itojun 	union nd_opts ndopts;
    746  1.1  itojun 	char *on_off[] = {"OFF", "ON"};
    747  1.1  itojun 	u_int32_t reachabletime, retranstimer, mtu;
    748  1.1  itojun 
    749  1.1  itojun 	syslog(LOG_DEBUG,
    750  1.1  itojun 	       "<%s> RA received from %s on %s",
    751  1.1  itojun 	       __FUNCTION__,
    752  1.1  itojun 	       inet_ntop(AF_INET6, &from->sin6_addr,
    753  1.1  itojun 			 ntopbuf, INET6_ADDRSTRLEN),
    754  1.1  itojun 	       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    755  1.1  itojun 
    756  1.1  itojun 	/* ND option check */
    757  1.1  itojun 	memset(&ndopts, 0, sizeof(ndopts));
    758  1.1  itojun 	if (nd6_options((struct nd_opt_hdr *)(ra + 1),
    759  1.1  itojun 			len - sizeof(struct nd_router_advert),
    760  1.1  itojun 			 &ndopts,
    761  1.1  itojun 			NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
    762  1.1  itojun 		syslog(LOG_ERR,
    763  1.1  itojun 		       "<%s> ND option check failed for an RA from %s on %s",
    764  1.1  itojun 		       __FUNCTION__,
    765  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    766  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    767  1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    768  1.1  itojun 		return;
    769  1.1  itojun 	}
    770  1.1  itojun 
    771  1.1  itojun 	/*
    772  1.1  itojun 	 * RA consistency check according to RFC-2461 6.2.7
    773  1.1  itojun 	 */
    774  1.1  itojun 	if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == 0) {
    775  1.1  itojun 		syslog(LOG_INFO,
    776  1.1  itojun 		       "<%s> received RA from %s on non-advertising"
    777  1.1  itojun 		       " interface(%s)",
    778  1.1  itojun 		       __FUNCTION__,
    779  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    780  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    781  1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    782  1.1  itojun 		goto done;
    783  1.1  itojun 	}
    784  1.1  itojun 	/* Cur Hop Limit value */
    785  1.1  itojun 	if (ra->nd_ra_curhoplimit && rai->hoplimit &&
    786  1.1  itojun 	    ra->nd_ra_curhoplimit != rai->hoplimit) {
    787  1.1  itojun 		syslog(LOG_WARNING,
    788  1.1  itojun 		       "<%s> CurHopLimit inconsistent on %s:"
    789  1.1  itojun 		       " %d from %s, %d from us",
    790  1.1  itojun 		       __FUNCTION__,
    791  1.1  itojun 		       rai->ifname,
    792  1.1  itojun 		       ra->nd_ra_curhoplimit,
    793  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    794  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    795  1.1  itojun 		       rai->hoplimit);
    796  1.1  itojun 	}
    797  1.1  itojun 	/* M flag */
    798  1.1  itojun 	if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
    799  1.1  itojun 	    rai->managedflg) {
    800  1.1  itojun 		syslog(LOG_WARNING,
    801  1.1  itojun 		       "<%s> M flag inconsistent on %s:"
    802  1.1  itojun 		       " %s from %s, %s from us",
    803  1.1  itojun 		       __FUNCTION__,
    804  1.1  itojun 		       rai->ifname,
    805  1.1  itojun 		       on_off[!rai->managedflg],
    806  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    807  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    808  1.1  itojun 		       on_off[rai->managedflg]);
    809  1.1  itojun 	}
    810  1.1  itojun 	/* O flag */
    811  1.1  itojun 	if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
    812  1.1  itojun 	    rai->otherflg) {
    813  1.1  itojun 		syslog(LOG_WARNING,
    814  1.1  itojun 		       "<%s> O flag inconsistent on %s:"
    815  1.1  itojun 		       " %s from %s, %s from us",
    816  1.1  itojun 		       __FUNCTION__,
    817  1.1  itojun 		       rai->ifname,
    818  1.1  itojun 		       on_off[!rai->otherflg],
    819  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    820  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    821  1.1  itojun 		       on_off[rai->otherflg]);
    822  1.1  itojun 	}
    823  1.1  itojun 	/* Reachable Time */
    824  1.1  itojun 	reachabletime = ntohl(ra->nd_ra_reachable);
    825  1.1  itojun 	if (reachabletime && rai->reachabletime &&
    826  1.1  itojun 	    reachabletime != rai->reachabletime) {
    827  1.1  itojun 		syslog(LOG_WARNING,
    828  1.1  itojun 		       "<%s> ReachableTime inconsistent on %s:"
    829  1.1  itojun 		       " %d from %s, %d from us",
    830  1.1  itojun 		       __FUNCTION__,
    831  1.1  itojun 		       rai->ifname,
    832  1.1  itojun 		       reachabletime,
    833  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    834  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    835  1.1  itojun 		       rai->reachabletime);
    836  1.1  itojun 	}
    837  1.1  itojun 	/* Retrans Timer */
    838  1.1  itojun 	retranstimer = ntohl(ra->nd_ra_retransmit);
    839  1.1  itojun 	if (retranstimer && rai->retranstimer &&
    840  1.1  itojun 	    retranstimer != rai->retranstimer) {
    841  1.1  itojun 		syslog(LOG_WARNING,
    842  1.1  itojun 		       "<%s> RetranceTimer inconsistent on %s:"
    843  1.1  itojun 		       " %d from %s, %d from us",
    844  1.1  itojun 		       __FUNCTION__,
    845  1.1  itojun 		       rai->ifname,
    846  1.1  itojun 		       retranstimer,
    847  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    848  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    849  1.1  itojun 		       rai->retranstimer);
    850  1.1  itojun 	}
    851  1.1  itojun 	/* Values in the MTU options */
    852  1.1  itojun 	if (ndopts.nd_opts_mtu) {
    853  1.1  itojun 		mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
    854  1.1  itojun 		if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
    855  1.1  itojun 			syslog(LOG_WARNING,
    856  1.1  itojun 			       "<%s> MTU option value inconsistent on %s:"
    857  1.1  itojun 			       " %d from %s, %d from us",
    858  1.1  itojun 			       __FUNCTION__,
    859  1.1  itojun 			       rai->ifname, mtu,
    860  1.1  itojun 			       inet_ntop(AF_INET6, &from->sin6_addr,
    861  1.1  itojun 					 ntopbuf, INET6_ADDRSTRLEN),
    862  1.1  itojun 			       rai->linkmtu);
    863  1.1  itojun 		}
    864  1.1  itojun 	}
    865  1.1  itojun 	/* Preferred and Valid Lifetimes for prefixes */
    866  1.1  itojun 	{
    867  1.1  itojun 		struct nd_optlist *optp = ndopts.nd_opts_list;
    868  1.1  itojun 
    869  1.1  itojun 		if (ndopts.nd_opts_pi)
    870  1.1  itojun 			prefix_check(ndopts.nd_opts_pi, rai, from);
    871  1.1  itojun 		while (optp) {
    872  1.1  itojun 			prefix_check((struct nd_opt_prefix_info *)optp->opt,
    873  1.1  itojun 				     rai, from);
    874  1.1  itojun 			optp = optp->next;
    875  1.1  itojun 		}
    876  1.1  itojun 	}
    877  1.1  itojun 
    878  1.1  itojun   done:
    879  1.1  itojun 	free_ndopts(&ndopts);
    880  1.1  itojun 	return;
    881  1.1  itojun }
    882  1.1  itojun 
    883  1.1  itojun static void
    884  1.1  itojun prefix_check(struct nd_opt_prefix_info *pinfo,
    885  1.1  itojun 	     struct rainfo *rai, struct sockaddr_in6 *from)
    886  1.1  itojun {
    887  1.1  itojun 	u_int32_t preferred_time, valid_time;
    888  1.1  itojun 	struct prefix *pp;
    889  1.1  itojun 	u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
    890  1.1  itojun 
    891  1.1  itojun #if 0				/* impossible */
    892  1.1  itojun 	if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
    893  1.1  itojun 		return;
    894  1.1  itojun #endif
    895  1.1  itojun 
    896  1.1  itojun 	/*
    897  1.1  itojun 	 * log if the adveritsed prefix has link-local scope(sanity check?)
    898  1.1  itojun 	 */
    899  1.1  itojun 	if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
    900  1.1  itojun 		syslog(LOG_INFO,
    901  1.1  itojun 		       "<%s> link-local prefix %s/%d is advertised "
    902  1.1  itojun 		       "from %s on %s",
    903  1.1  itojun 		       __FUNCTION__,
    904  1.1  itojun 		       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
    905  1.1  itojun 				 prefixbuf, INET6_ADDRSTRLEN),
    906  1.1  itojun 		       pinfo->nd_opt_pi_prefix_len,
    907  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    908  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    909  1.1  itojun 		       rai->ifname);
    910  1.1  itojun 	}
    911  1.1  itojun 
    912  1.1  itojun 	if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
    913  1.1  itojun 			      pinfo->nd_opt_pi_prefix_len)) == NULL) {
    914  1.1  itojun 		syslog(LOG_INFO,
    915  1.1  itojun 		       "<%s> prefix %s/%d from %s on %s is not in our list",
    916  1.1  itojun 		       __FUNCTION__,
    917  1.1  itojun 		       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
    918  1.1  itojun 				 prefixbuf, INET6_ADDRSTRLEN),
    919  1.1  itojun 		       pinfo->nd_opt_pi_prefix_len,
    920  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    921  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    922  1.1  itojun 		       rai->ifname);
    923  1.1  itojun 		return;
    924  1.1  itojun 	}
    925  1.1  itojun 
    926  1.1  itojun 	preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
    927  1.1  itojun 	if (preferred_time != pp->preflifetime)
    928  1.1  itojun 		syslog(LOG_WARNING,
    929  1.1  itojun 		       "<%s> prefeerred lifetime for %s/%d"
    930  1.1  itojun 		       " inconsistent on %s:"
    931  1.1  itojun 		       " %d from %s, %d from us",
    932  1.1  itojun 		       __FUNCTION__,
    933  1.1  itojun 		       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
    934  1.1  itojun 				 prefixbuf, INET6_ADDRSTRLEN),
    935  1.1  itojun 		       pinfo->nd_opt_pi_prefix_len,
    936  1.1  itojun 		       rai->ifname, preferred_time,
    937  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    938  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    939  1.1  itojun 		       pp->preflifetime);
    940  1.1  itojun 
    941  1.1  itojun 	valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
    942  1.1  itojun 	if (valid_time != pp->validlifetime)
    943  1.1  itojun 		syslog(LOG_WARNING,
    944  1.1  itojun 		       "<%s> valid lifetime for %s/%d"
    945  1.1  itojun 		       " inconsistent on %s:"
    946  1.1  itojun 		       " %d from %s, %d from us",
    947  1.1  itojun 		       __FUNCTION__,
    948  1.1  itojun 		       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
    949  1.1  itojun 				 prefixbuf, INET6_ADDRSTRLEN),
    950  1.1  itojun 		       pinfo->nd_opt_pi_prefix_len,
    951  1.1  itojun 		       rai->ifname, valid_time,
    952  1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    953  1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    954  1.1  itojun 		       pp->validlifetime);
    955  1.1  itojun }
    956  1.1  itojun 
    957  1.1  itojun struct prefix *
    958  1.1  itojun find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
    959  1.1  itojun {
    960  1.1  itojun 	struct prefix *pp;
    961  1.1  itojun 	int bytelen, bitlen;
    962  1.1  itojun 
    963  1.1  itojun 	for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
    964  1.1  itojun 		if (plen != pp->prefixlen)
    965  1.1  itojun 			continue;
    966  1.1  itojun 		bytelen = plen / 8;
    967  1.1  itojun 		bitlen = plen % 8;
    968  1.1  itojun 		if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
    969  1.1  itojun 			continue;
    970  1.1  itojun 		if (prefix->s6_addr[bytelen] >> (8 - bitlen) ==
    971  1.1  itojun 		    pp->prefix.s6_addr[bytelen] >> (8 - bitlen))
    972  1.1  itojun 			return(pp);
    973  1.1  itojun 	}
    974  1.1  itojun 
    975  1.1  itojun 	return(NULL);
    976  1.1  itojun }
    977  1.1  itojun 
    978  1.1  itojun static int
    979  1.1  itojun nd6_options(struct nd_opt_hdr *hdr, int limit,
    980  1.1  itojun 	    union nd_opts *ndopts, u_int32_t optflags)
    981  1.1  itojun {
    982  1.1  itojun 	int optlen = 0;
    983  1.1  itojun 
    984  1.1  itojun 	for (; limit > 0; limit -= optlen) {
    985  1.1  itojun 		hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
    986  1.1  itojun 		optlen = hdr->nd_opt_len << 3;
    987  1.1  itojun 		if (hdr->nd_opt_len == 0) {
    988  1.1  itojun 			syslog(LOG_ERR,
    989  1.1  itojun 			       "<%s> bad ND option length(0) (type = %d)",
    990  1.1  itojun 			       __FUNCTION__, hdr->nd_opt_type);
    991  1.1  itojun 			goto bad;
    992  1.1  itojun 		}
    993  1.1  itojun 
    994  1.1  itojun 		if (hdr->nd_opt_type > ND_OPT_MTU) {
    995  1.1  itojun 			syslog(LOG_INFO,
    996  1.1  itojun 			       "<%s> unknown ND option(type %d)",
    997  1.1  itojun 			       __FUNCTION__,
    998  1.1  itojun 			       hdr->nd_opt_type);
    999  1.1  itojun 			continue;
   1000  1.1  itojun 		}
   1001  1.1  itojun 
   1002  1.1  itojun 		if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
   1003  1.1  itojun 			syslog(LOG_INFO,
   1004  1.1  itojun 			       "<%s> unexpected ND option(type %d)",
   1005  1.1  itojun 			       __FUNCTION__,
   1006  1.1  itojun 			       hdr->nd_opt_type);
   1007  1.1  itojun 			continue;
   1008  1.1  itojun 		}
   1009  1.1  itojun 
   1010  1.1  itojun 		switch(hdr->nd_opt_type) {
   1011  1.1  itojun 		 case ND_OPT_SOURCE_LINKADDR:
   1012  1.1  itojun 		 case ND_OPT_TARGET_LINKADDR:
   1013  1.1  itojun 		 case ND_OPT_REDIRECTED_HEADER:
   1014  1.1  itojun 		 case ND_OPT_MTU:
   1015  1.1  itojun 			 if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
   1016  1.1  itojun 				 syslog(LOG_INFO,
   1017  1.1  itojun 					"<%s> duplicated ND option"
   1018  1.1  itojun 					" (type = %d)",
   1019  1.1  itojun 					__FUNCTION__,
   1020  1.1  itojun 					hdr->nd_opt_type);
   1021  1.1  itojun 			 }
   1022  1.1  itojun 			 ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
   1023  1.1  itojun 			 break;
   1024  1.1  itojun 		 case ND_OPT_PREFIX_INFORMATION:
   1025  1.1  itojun 		 {
   1026  1.1  itojun 			 struct nd_optlist *pfxlist;
   1027  1.1  itojun 
   1028  1.1  itojun 			 if (ndopts->nd_opts_pi == 0) {
   1029  1.1  itojun 				 ndopts->nd_opts_pi =
   1030  1.1  itojun 					 (struct nd_opt_prefix_info *)hdr;
   1031  1.1  itojun 				 continue;
   1032  1.1  itojun 			 }
   1033  1.1  itojun 			 if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
   1034  1.1  itojun 				 syslog(LOG_ERR,
   1035  1.1  itojun 					"<%s> can't allocate memory",
   1036  1.1  itojun 					__FUNCTION__);
   1037  1.1  itojun 				 goto bad;
   1038  1.1  itojun 			 }
   1039  1.1  itojun 			 pfxlist->next = ndopts->nd_opts_list;
   1040  1.1  itojun 			 pfxlist->opt = hdr;
   1041  1.1  itojun 			 ndopts->nd_opts_list = pfxlist;
   1042  1.1  itojun 
   1043  1.1  itojun 			 break;
   1044  1.1  itojun 		 }
   1045  1.1  itojun 		 default:	/* impossible */
   1046  1.1  itojun 			 break;
   1047  1.1  itojun 		}
   1048  1.1  itojun 	}
   1049  1.1  itojun 
   1050  1.1  itojun 	return(0);
   1051  1.1  itojun 
   1052  1.1  itojun   bad:
   1053  1.1  itojun 	free_ndopts(ndopts);
   1054  1.1  itojun 
   1055  1.1  itojun 	return(-1);
   1056  1.1  itojun }
   1057  1.1  itojun 
   1058  1.1  itojun static void
   1059  1.1  itojun free_ndopts(union nd_opts *ndopts)
   1060  1.1  itojun {
   1061  1.1  itojun 	struct nd_optlist *opt = ndopts->nd_opts_list, *next;
   1062  1.1  itojun 
   1063  1.1  itojun 	while(opt) {
   1064  1.1  itojun 		next = opt->next;
   1065  1.1  itojun 		free(opt);
   1066  1.1  itojun 		opt = next;
   1067  1.1  itojun 	}
   1068  1.1  itojun }
   1069  1.1  itojun 
   1070  1.1  itojun void
   1071  1.1  itojun sock_open()
   1072  1.1  itojun {
   1073  1.1  itojun 	struct icmp6_filter filt;
   1074  1.1  itojun 	struct ipv6_mreq mreq;
   1075  1.1  itojun 	struct rainfo *ra = ralist;
   1076  1.1  itojun 	int on;
   1077  1.1  itojun 	/* XXX: should be max MTU attached to the node */
   1078  1.1  itojun 	static u_char answer[1500];
   1079  1.1  itojun 	static u_char sndcmsgbuf[CMSG_SPACE(sizeof(struct in6_pktinfo)) +
   1080  1.1  itojun 				CMSG_SPACE(sizeof(int))];
   1081  1.1  itojun 
   1082  1.1  itojun 	if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
   1083  1.1  itojun 		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
   1084  1.1  itojun 		       strerror(errno));
   1085  1.1  itojun 		exit(1);
   1086  1.1  itojun 	}
   1087  1.1  itojun 
   1088  1.1  itojun 	/* specify to tell receiving interface */
   1089  1.1  itojun 	on = 1;
   1090  1.5  itojun #ifdef IPV6_RECVPKTINFO
   1091  1.5  itojun 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
   1092  1.5  itojun 		       sizeof(on)) < 0) {
   1093  1.5  itojun 		syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s",
   1094  1.5  itojun 		       __FUNCTION__, strerror(errno));
   1095  1.5  itojun 		exit(1);
   1096  1.5  itojun 	}
   1097  1.5  itojun #else  /* old adv. API */
   1098  1.1  itojun 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
   1099  1.1  itojun 		       sizeof(on)) < 0) {
   1100  1.1  itojun 		syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
   1101  1.1  itojun 		       __FUNCTION__, strerror(errno));
   1102  1.1  itojun 		exit(1);
   1103  1.1  itojun 	}
   1104  1.5  itojun #endif
   1105  1.1  itojun 
   1106  1.1  itojun 	on = 1;
   1107  1.1  itojun 	/* specify to tell value of hoplimit field of received IP6 hdr */
   1108  1.5  itojun #ifdef IPV6_RECVHOPLIMIT
   1109  1.5  itojun 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
   1110  1.5  itojun 		       sizeof(on)) < 0) {
   1111  1.5  itojun 		syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s",
   1112  1.5  itojun 		       __FUNCTION__, strerror(errno));
   1113  1.5  itojun 		exit(1);
   1114  1.5  itojun 	}
   1115  1.5  itojun #else  /* old adv. API */
   1116  1.1  itojun 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
   1117  1.1  itojun 		       sizeof(on)) < 0) {
   1118  1.1  itojun 		syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
   1119  1.1  itojun 		       __FUNCTION__, strerror(errno));
   1120  1.1  itojun 		exit(1);
   1121  1.1  itojun 	}
   1122  1.5  itojun #endif
   1123  1.1  itojun 
   1124  1.1  itojun 	ICMP6_FILTER_SETBLOCKALL(&filt);
   1125  1.1  itojun 	ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
   1126  1.1  itojun 	ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
   1127  1.1  itojun 	if (accept_rr)
   1128  1.1  itojun 		ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
   1129  1.1  itojun 	if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
   1130  1.1  itojun 		       sizeof(filt)) < 0) {
   1131  1.1  itojun 		syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
   1132  1.1  itojun 		       __FUNCTION__, strerror(errno));
   1133  1.1  itojun 		exit(1);
   1134  1.1  itojun 	}
   1135  1.1  itojun 
   1136  1.1  itojun 	/*
   1137  1.1  itojun 	 * join all routers multicast address on each advertising interface.
   1138  1.1  itojun 	 */
   1139  1.1  itojun 	if (inet_pton(AF_INET6, ALLROUTERS, &mreq.ipv6mr_multiaddr.s6_addr)
   1140  1.1  itojun 	    != 1) {
   1141  1.1  itojun 		syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
   1142  1.1  itojun 		       __FUNCTION__);
   1143  1.1  itojun 		exit(1);
   1144  1.1  itojun 	}
   1145  1.1  itojun 	while(ra) {
   1146  1.1  itojun 		mreq.ipv6mr_interface = ra->ifindex;
   1147  1.1  itojun 		if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
   1148  1.1  itojun 			       &mreq,
   1149  1.1  itojun 			       sizeof(mreq)) < 0) {
   1150  1.1  itojun 			syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP on %s: %s",
   1151  1.1  itojun 			       __FUNCTION__, ra->ifname, strerror(errno));
   1152  1.1  itojun 			exit(1);
   1153  1.1  itojun 		}
   1154  1.1  itojun 		ra = ra->next;
   1155  1.1  itojun 	}
   1156  1.1  itojun 
   1157  1.1  itojun 	/* initialize msghdr for receiving packets */
   1158  1.1  itojun 	rcviov[0].iov_base = (caddr_t)answer;
   1159  1.1  itojun 	rcviov[0].iov_len = sizeof(answer);
   1160  1.1  itojun 	rcvmhdr.msg_name = (caddr_t)&from;
   1161  1.1  itojun 	rcvmhdr.msg_namelen = sizeof(from);
   1162  1.1  itojun 	rcvmhdr.msg_iov = rcviov;
   1163  1.1  itojun 	rcvmhdr.msg_iovlen = 1;
   1164  1.1  itojun 	rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
   1165  1.1  itojun 	rcvmhdr.msg_controllen = sizeof(rcvcmsgbuf);
   1166  1.1  itojun 
   1167  1.1  itojun 	/* initialize msghdr for sending packets */
   1168  1.1  itojun 	sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
   1169  1.1  itojun 	sndmhdr.msg_iov = sndiov;
   1170  1.1  itojun 	sndmhdr.msg_iovlen = 1;
   1171  1.1  itojun 	sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
   1172  1.1  itojun 	sndmhdr.msg_controllen = sizeof(sndcmsgbuf);
   1173  1.1  itojun 
   1174  1.1  itojun 	return;
   1175  1.1  itojun }
   1176  1.1  itojun 
   1177  1.1  itojun /* open a routing socket to watch the routing table */
   1178  1.1  itojun static void
   1179  1.1  itojun rtsock_open()
   1180  1.1  itojun {
   1181  1.1  itojun 	if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
   1182  1.1  itojun 		syslog(LOG_ERR,
   1183  1.1  itojun 		       "<%s> socket: %s", __FUNCTION__, strerror(errno));
   1184  1.1  itojun 		exit(1);
   1185  1.1  itojun 	}
   1186  1.1  itojun }
   1187  1.1  itojun 
   1188  1.1  itojun static struct rainfo *
   1189  1.1  itojun if_indextorainfo(int index)
   1190  1.1  itojun {
   1191  1.1  itojun 	struct rainfo *rai = ralist;
   1192  1.1  itojun 
   1193  1.1  itojun 	for (rai = ralist; rai; rai = rai->next) {
   1194  1.1  itojun 		if (rai->ifindex == index)
   1195  1.1  itojun 			return(rai);
   1196  1.1  itojun 	}
   1197  1.1  itojun 
   1198  1.1  itojun 	return(NULL);		/* search failed */
   1199  1.1  itojun }
   1200  1.1  itojun 
   1201  1.1  itojun static void
   1202  1.1  itojun ra_output(rainfo)
   1203  1.1  itojun struct rainfo *rainfo;
   1204  1.1  itojun {
   1205  1.1  itojun 	int i;
   1206  1.1  itojun 
   1207  1.1  itojun 	struct cmsghdr *cm;
   1208  1.1  itojun 	struct in6_pktinfo *pi;
   1209  1.1  itojun 
   1210  1.1  itojun 	sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
   1211  1.1  itojun 	sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
   1212  1.1  itojun 	sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
   1213  1.1  itojun 
   1214  1.1  itojun 	cm = CMSG_FIRSTHDR(&sndmhdr);
   1215  1.1  itojun 	/* specify the outgoing interface */
   1216  1.1  itojun 	cm->cmsg_level = IPPROTO_IPV6;
   1217  1.1  itojun 	cm->cmsg_type = IPV6_PKTINFO;
   1218  1.1  itojun 	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
   1219  1.1  itojun 	pi = (struct in6_pktinfo *)CMSG_DATA(cm);
   1220  1.1  itojun 	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));	/*XXX*/
   1221  1.1  itojun 	pi->ipi6_ifindex = rainfo->ifindex;
   1222  1.1  itojun 
   1223  1.1  itojun 	/* specify the hop limit of the packet */
   1224  1.1  itojun 	{
   1225  1.1  itojun 		int hoplimit = 255;
   1226  1.1  itojun 
   1227  1.1  itojun 		cm = CMSG_NXTHDR(&sndmhdr, cm);
   1228  1.1  itojun 		cm->cmsg_level = IPPROTO_IPV6;
   1229  1.1  itojun 		cm->cmsg_type = IPV6_HOPLIMIT;
   1230  1.1  itojun 		cm->cmsg_len = CMSG_LEN(sizeof(int));
   1231  1.1  itojun 		memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
   1232  1.1  itojun 	}
   1233  1.1  itojun 
   1234  1.1  itojun 	syslog(LOG_DEBUG,
   1235  1.1  itojun 	       "<%s> send RA on %s, # of waitings = %d",
   1236  1.1  itojun 	       __FUNCTION__, rainfo->ifname, rainfo->waiting);
   1237  1.1  itojun 
   1238  1.1  itojun 	i = sendmsg(sock, &sndmhdr, 0);
   1239  1.1  itojun 
   1240  1.1  itojun 	if (i < 0 || i != rainfo->ra_datalen)  {
   1241  1.1  itojun 		if (i < 0) {
   1242  1.4  itojun 			syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
   1243  1.4  itojun 			       __FUNCTION__, rainfo->ifname,
   1244  1.4  itojun 			       strerror(errno));
   1245  1.1  itojun 		}
   1246  1.1  itojun 	}
   1247  1.1  itojun 
   1248  1.1  itojun 	/* update counter */
   1249  1.1  itojun 	if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
   1250  1.1  itojun 		rainfo->initcounter++;
   1251  1.1  itojun 
   1252  1.1  itojun 	/* update timestamp */
   1253  1.1  itojun 	gettimeofday(&rainfo->lastsent, NULL);
   1254  1.1  itojun 
   1255  1.1  itojun 	/* reset waiting conter */
   1256  1.1  itojun 	rainfo->waiting = 0;
   1257  1.1  itojun }
   1258  1.1  itojun 
   1259  1.1  itojun /* process RA timer */
   1260  1.1  itojun void
   1261  1.1  itojun ra_timeout(void *data)
   1262  1.1  itojun {
   1263  1.1  itojun 	struct rainfo *rai = (struct rainfo *)data;
   1264  1.1  itojun 
   1265  1.1  itojun #ifdef notyet
   1266  1.1  itojun 	/* if necessary, reconstruct the packet. */
   1267  1.1  itojun #endif
   1268  1.1  itojun 
   1269  1.1  itojun 	syslog(LOG_DEBUG,
   1270  1.1  itojun 	       "<%s> RA timer on %s is expired",
   1271  1.1  itojun 	       __FUNCTION__, rai->ifname);
   1272  1.1  itojun 
   1273  1.1  itojun 	if (iflist[rai->ifindex]->ifm_flags & IFF_UP)
   1274  1.1  itojun 		ra_output(rai);
   1275  1.1  itojun 	else
   1276  1.1  itojun 		syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
   1277  1.1  itojun 		       __FUNCTION__, rai->ifname);
   1278  1.1  itojun }
   1279  1.1  itojun 
   1280  1.1  itojun /* update RA timer */
   1281  1.1  itojun void
   1282  1.1  itojun ra_timer_update(void *data, struct timeval *tm)
   1283  1.1  itojun {
   1284  1.1  itojun 	struct rainfo *rai = (struct rainfo *)data;
   1285  1.1  itojun 	long interval;
   1286  1.1  itojun 
   1287  1.1  itojun 	/*
   1288  1.1  itojun 	 * Whenever a multicast advertisement is sent from an interface,
   1289  1.1  itojun 	 * the timer is reset to a uniformly-distributed random value
   1290  1.1  itojun 	 * between the interface's configured MinRtrAdvInterval and
   1291  1.1  itojun 	 * MaxRtrAdvInterval(discovery-v2-02 6.2.4).
   1292  1.1  itojun 	 */
   1293  1.1  itojun 	interval = rai->mininterval;
   1294  1.1  itojun 	interval += random() % (rai->maxinterval - rai->mininterval);
   1295  1.1  itojun 
   1296  1.1  itojun 	/*
   1297  1.1  itojun 	 * For the first few advertisements (up to
   1298  1.1  itojun 	 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
   1299  1.1  itojun 	 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
   1300  1.1  itojun 	 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
   1301  1.1  itojun 	 * (RFC-2461 6.2.4)
   1302  1.1  itojun 	 */
   1303  1.1  itojun 	if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
   1304  1.1  itojun 	    interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
   1305  1.1  itojun 		interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
   1306  1.1  itojun 
   1307  1.1  itojun 	tm->tv_sec = interval;
   1308  1.1  itojun 	tm->tv_usec = 0;
   1309  1.1  itojun 
   1310  1.1  itojun 	syslog(LOG_DEBUG,
   1311  1.1  itojun 	       "<%s> RA timer on %s is set to %ld:%ld",
   1312  1.1  itojun 	       __FUNCTION__, rai->ifname, tm->tv_sec, tm->tv_usec);
   1313  1.1  itojun 
   1314  1.1  itojun 	return;
   1315  1.1  itojun }
   1316