Home | History | Annotate | Line # | Download | only in rtadvd
rtadvd.c revision 1.12
      1  1.12  itojun /*	$NetBSD: rtadvd.c,v 1.12 2001/02/04 06:19:41 itojun Exp $	*/
      2  1.12  itojun /*	$KAME: rtadvd.c,v 1.50 2001/02/04 06:15:15 itojun Exp $	*/
      3   1.2  itojun 
      4   1.1  itojun /*
      5   1.1  itojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6   1.1  itojun  * All rights reserved.
      7   1.1  itojun  *
      8   1.1  itojun  * Redistribution and use in source and binary forms, with or without
      9   1.1  itojun  * modification, are permitted provided that the following conditions
     10   1.1  itojun  * are met:
     11   1.1  itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.1  itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.1  itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  itojun  *    documentation and/or other materials provided with the distribution.
     16   1.1  itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.1  itojun  *    may be used to endorse or promote products derived from this software
     18   1.1  itojun  *    without specific prior written permission.
     19   1.1  itojun  *
     20   1.1  itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.1  itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.1  itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.1  itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.1  itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.1  itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.1  itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.1  itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.1  itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.1  itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.1  itojun  * SUCH DAMAGE.
     31   1.1  itojun  */
     32   1.1  itojun 
     33   1.1  itojun #include <sys/param.h>
     34   1.1  itojun #include <sys/socket.h>
     35   1.1  itojun #include <sys/uio.h>
     36   1.1  itojun #include <sys/time.h>
     37  1.11  itojun #include <sys/queue.h>
     38   1.1  itojun 
     39   1.1  itojun #include <net/if.h>
     40   1.1  itojun #include <net/route.h>
     41   1.1  itojun #include <net/if_dl.h>
     42   1.1  itojun #include <netinet/in.h>
     43   1.1  itojun #include <netinet/ip6.h>
     44   1.1  itojun #include <netinet6/ip6_var.h>
     45   1.1  itojun #include <netinet/icmp6.h>
     46   1.1  itojun 
     47   1.1  itojun #include <arpa/inet.h>
     48   1.1  itojun 
     49   1.1  itojun #include <time.h>
     50   1.1  itojun #include <unistd.h>
     51   1.1  itojun #include <stdio.h>
     52   1.6  itojun #include <stdlib.h>
     53   1.1  itojun #include <err.h>
     54   1.1  itojun #include <errno.h>
     55   1.1  itojun #include <string.h>
     56   1.1  itojun #include <stdlib.h>
     57   1.1  itojun #include <syslog.h>
     58   1.1  itojun #include "rtadvd.h"
     59   1.1  itojun #include "rrenum.h"
     60   1.1  itojun #include "advcap.h"
     61   1.1  itojun #include "timer.h"
     62   1.1  itojun #include "if.h"
     63   1.1  itojun #include "config.h"
     64   1.9  itojun #include "dump.h"
     65   1.1  itojun 
     66   1.1  itojun struct msghdr rcvmhdr;
     67   1.6  itojun static u_char *rcvcmsgbuf;
     68   1.6  itojun static size_t rcvcmsgbuflen;
     69   1.6  itojun static u_char *sndcmsgbuf = NULL;
     70   1.6  itojun static size_t sndcmsgbuflen;
     71   1.9  itojun static int do_dump;
     72  1.11  itojun static int do_die;
     73   1.1  itojun struct msghdr sndmhdr;
     74   1.1  itojun struct iovec rcviov[2];
     75   1.1  itojun struct iovec sndiov[2];
     76   1.1  itojun struct sockaddr_in6 from;
     77   1.1  itojun struct sockaddr_in6 sin6_allnodes = {sizeof(sin6_allnodes), AF_INET6};
     78  1.11  itojun struct in6_addr in6a_site_allrouters;
     79   1.9  itojun static char *dumpfilename = "/var/run/rtadvd.dump"; /* XXX: should be configurable */
     80   1.9  itojun static char *pidfilename = "/var/run/rtadvd.pid"; /* should be configurable */
     81  1.11  itojun static char *mcastif;
     82  1.12  itojun int sock;
     83  1.12  itojun int rtsock = -1;
     84   1.7  itojun #ifdef MIP6
     85   1.7  itojun int mobileip6 = 0;
     86   1.7  itojun #endif
     87   1.5  itojun int accept_rr = 0;
     88   1.1  itojun int dflag = 0, sflag = 0;
     89   1.1  itojun 
     90   1.1  itojun u_char *conffile = NULL;
     91   1.1  itojun 
     92   1.1  itojun struct rainfo *ralist = NULL;
     93   1.1  itojun struct nd_optlist {
     94   1.1  itojun 	struct nd_optlist *next;
     95   1.1  itojun 	struct nd_opt_hdr *opt;
     96   1.1  itojun };
     97   1.1  itojun union nd_opts {
     98   1.1  itojun 	struct nd_opt_hdr *nd_opt_array[7];
     99   1.1  itojun 	struct {
    100   1.1  itojun 		struct nd_opt_hdr *zero;
    101   1.1  itojun 		struct nd_opt_hdr *src_lladdr;
    102   1.1  itojun 		struct nd_opt_hdr *tgt_lladdr;
    103   1.1  itojun 		struct nd_opt_prefix_info *pi;
    104   1.1  itojun 		struct nd_opt_rd_hdr *rh;
    105   1.1  itojun 		struct nd_opt_mtu *mtu;
    106   1.1  itojun 		struct nd_optlist *list;
    107   1.1  itojun 	} nd_opt_each;
    108   1.1  itojun };
    109   1.1  itojun #define nd_opts_src_lladdr	nd_opt_each.src_lladdr
    110   1.1  itojun #define nd_opts_tgt_lladdr	nd_opt_each.tgt_lladdr
    111   1.1  itojun #define nd_opts_pi		nd_opt_each.pi
    112   1.1  itojun #define nd_opts_rh		nd_opt_each.rh
    113   1.1  itojun #define nd_opts_mtu		nd_opt_each.mtu
    114   1.1  itojun #define nd_opts_list		nd_opt_each.list
    115   1.1  itojun 
    116   1.1  itojun #define NDOPT_FLAG_SRCLINKADDR 0x1
    117   1.1  itojun #define NDOPT_FLAG_TGTLINKADDR 0x2
    118   1.1  itojun #define NDOPT_FLAG_PREFIXINFO 0x4
    119   1.1  itojun #define NDOPT_FLAG_RDHDR 0x8
    120   1.1  itojun #define NDOPT_FLAG_MTU 0x10
    121   1.1  itojun 
    122   1.1  itojun u_int32_t ndopt_flags[] = {
    123   1.1  itojun 	0, NDOPT_FLAG_SRCLINKADDR, NDOPT_FLAG_TGTLINKADDR,
    124   1.1  itojun 	NDOPT_FLAG_PREFIXINFO, NDOPT_FLAG_RDHDR, NDOPT_FLAG_MTU
    125   1.1  itojun };
    126   1.1  itojun 
    127   1.1  itojun int main __P((int, char *[]));
    128  1.11  itojun static void set_die __P((int));
    129  1.11  itojun static void die __P((void));
    130   1.1  itojun static void sock_open __P((void));
    131   1.1  itojun static void rtsock_open __P((void));
    132   1.1  itojun static void rtadvd_input __P((void));
    133   1.1  itojun static void rs_input __P((int, struct nd_router_solicit *,
    134   1.1  itojun 			  struct in6_pktinfo *, struct sockaddr_in6 *));
    135   1.1  itojun static void ra_input __P((int, struct nd_router_advert *,
    136   1.1  itojun 			  struct in6_pktinfo *, struct sockaddr_in6 *));
    137   1.9  itojun static int prefix_check __P((struct nd_opt_prefix_info *, struct rainfo *,
    138   1.9  itojun 			     struct sockaddr_in6 *));
    139   1.1  itojun static int nd6_options __P((struct nd_opt_hdr *, int,
    140   1.1  itojun 			    union nd_opts *, u_int32_t));
    141   1.1  itojun static void free_ndopts __P((union nd_opts *));
    142   1.1  itojun static void ra_output __P((struct rainfo *));
    143   1.1  itojun static void rtmsg_input __P((void));
    144   1.9  itojun static void rtadvd_set_dump_file __P((void));
    145   1.9  itojun 
    146   1.1  itojun struct prefix *find_prefix __P((struct rainfo *, struct in6_addr *, int));
    147   1.1  itojun 
    148   1.1  itojun int
    149   1.1  itojun main(argc, argv)
    150   1.1  itojun 	int argc;
    151   1.1  itojun 	char *argv[];
    152   1.1  itojun {
    153   1.1  itojun 	fd_set fdset;
    154   1.1  itojun 	int maxfd = 0;
    155   1.1  itojun 	struct timeval *timeout;
    156   1.1  itojun 	int i, ch;
    157   1.1  itojun 	int fflag = 0;
    158   1.9  itojun 	FILE *pidfp;
    159   1.9  itojun 	pid_t pid;
    160   1.1  itojun 
    161   1.9  itojun 	openlog("rtadvd", LOG_NDELAY|LOG_PID, LOG_DAEMON);
    162   1.1  itojun 
    163   1.1  itojun 	/* get command line options and arguments */
    164   1.7  itojun #ifdef MIP6
    165  1.11  itojun #define OPTIONS "c:dDfM:mRs"
    166   1.7  itojun #else
    167  1.11  itojun #define OPTIONS "c:dDfM:Rs"
    168   1.7  itojun #endif
    169   1.7  itojun 	while ((ch = getopt(argc, argv, OPTIONS)) != -1) {
    170   1.7  itojun #undef OPTIONS
    171   1.1  itojun 		switch(ch) {
    172   1.1  itojun 		 case 'c':
    173   1.1  itojun 			 conffile = optarg;
    174   1.1  itojun 			 break;
    175   1.1  itojun 		 case 'd':
    176   1.1  itojun 			 dflag = 1;
    177   1.1  itojun 			 break;
    178   1.1  itojun 		 case 'D':
    179   1.1  itojun 			 dflag = 2;
    180   1.1  itojun 			 break;
    181   1.1  itojun 		 case 'f':
    182   1.1  itojun 			 fflag = 1;
    183   1.1  itojun 			 break;
    184  1.11  itojun 		case 'M':
    185  1.11  itojun 			mcastif = optarg;
    186  1.11  itojun 			break;
    187   1.7  itojun #ifdef MIP6
    188   1.7  itojun 		 case 'm':
    189   1.7  itojun 			 mobileip6 = 1;
    190   1.7  itojun 			 break;
    191   1.7  itojun #endif
    192   1.5  itojun 		 case 'R':
    193  1.12  itojun 			 fprintf(stderr, "rtadvd: "
    194  1.12  itojun 				 "the -R option is currently ignored.\n");
    195  1.12  itojun 			 /* accept_rr = 1; */
    196  1.12  itojun 			 /* run anyway... */
    197   1.5  itojun 			 break;
    198   1.1  itojun 		 case 's':
    199   1.1  itojun 			 sflag = 1;
    200   1.5  itojun 			 break;
    201   1.1  itojun 		}
    202   1.1  itojun 	}
    203   1.1  itojun 	argc -= optind;
    204   1.1  itojun 	argv += optind;
    205   1.1  itojun 	if (argc == 0) {
    206   1.1  itojun 		fprintf(stderr,
    207   1.7  itojun #ifdef MIP6
    208  1.11  itojun 			"usage: rtadvd [-dDfMmRs] [-c conffile] "
    209   1.7  itojun #else
    210  1.11  itojun 			"usage: rtadvd [-dDfMRs] [-c conffile] "
    211   1.7  itojun #endif
    212   1.1  itojun 			"interfaces...\n");
    213   1.1  itojun 		exit(1);
    214   1.1  itojun 	}
    215   1.1  itojun 
    216   1.1  itojun 	/* set log level */
    217   1.1  itojun 	if (dflag == 0)
    218   1.1  itojun 		(void)setlogmask(LOG_UPTO(LOG_ERR));
    219   1.1  itojun 	if (dflag == 1)
    220   1.1  itojun 		(void)setlogmask(LOG_UPTO(LOG_INFO));
    221   1.1  itojun 
    222   1.1  itojun 	/* timer initialization */
    223   1.1  itojun 	rtadvd_timer_init();
    224   1.1  itojun 
    225   1.1  itojun 	/* random value initialization */
    226   1.1  itojun 	srandom((u_long)time(NULL));
    227   1.1  itojun 
    228   1.1  itojun 	/* get iflist block from kernel */
    229   1.1  itojun 	init_iflist();
    230   1.1  itojun 
    231   1.1  itojun 	while (argc--)
    232   1.1  itojun 		getconfig(*argv++);
    233   1.1  itojun 
    234   1.3  itojun 	if (inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr) != 1) {
    235   1.3  itojun 		fprintf(stderr, "fatal: inet_pton failed\n");
    236   1.3  itojun 		exit(1);
    237   1.3  itojun 	}
    238   1.1  itojun 	sock_open();
    239   1.1  itojun 
    240   1.1  itojun 	if (!fflag)
    241   1.1  itojun 		daemon(1, 0);
    242   1.1  itojun 
    243   1.9  itojun 	/* record the current PID */
    244   1.9  itojun 	pid = getpid();
    245   1.9  itojun 	if ((pidfp = fopen(pidfilename, "w")) == NULL)
    246   1.9  itojun 		syslog(LOG_ERR,
    247   1.9  itojun 		       "<%s> failed to open a log file(%s), run anyway.",
    248   1.9  itojun 		       __FUNCTION__, pidfilename);
    249   1.9  itojun 	else {
    250   1.9  itojun 		fprintf(pidfp, "%d\n", pid);
    251   1.9  itojun 		fclose(pidfp);
    252   1.9  itojun 	}
    253   1.9  itojun 
    254   1.1  itojun 	FD_ZERO(&fdset);
    255   1.1  itojun 	FD_SET(sock, &fdset);
    256   1.1  itojun 	maxfd = sock;
    257  1.12  itojun 	if (sflag == 0) {
    258  1.12  itojun 		rtsock_open();
    259  1.12  itojun 		FD_SET(rtsock, &fdset);
    260  1.12  itojun 		if (rtsock > sock)
    261  1.12  itojun 			maxfd = rtsock;
    262  1.12  itojun 	} else
    263  1.12  itojun 		rtsock = -1;
    264   1.1  itojun 
    265  1.11  itojun 	signal(SIGTERM, (void *)set_die);
    266   1.9  itojun 	signal(SIGUSR1, (void *)rtadvd_set_dump_file);
    267   1.5  itojun 
    268   1.1  itojun 	while (1) {
    269   1.1  itojun 		struct fd_set select_fd = fdset; /* reinitialize */
    270   1.1  itojun 
    271   1.9  itojun 		if (do_dump) {	/* SIGUSR1 */
    272   1.9  itojun 			do_dump = 0;
    273   1.9  itojun 			rtadvd_dump_file(dumpfilename);
    274   1.9  itojun 		}
    275   1.9  itojun 
    276  1.11  itojun 		if (do_die) {
    277  1.11  itojun 			die();
    278  1.11  itojun 			/*NOTREACHED*/
    279  1.11  itojun 		}
    280  1.11  itojun 
    281   1.1  itojun 		/* timer expiration check and reset the timer */
    282   1.1  itojun 		timeout = rtadvd_check_timer();
    283   1.1  itojun 
    284  1.10  itojun 		if (timeout != NULL) {
    285  1.10  itojun 			syslog(LOG_DEBUG,
    286  1.10  itojun 			       "<%s> set timer to %ld:%ld. waiting for "
    287  1.10  itojun 			       "inputs or timeout",
    288  1.10  itojun 			       __FUNCTION__,
    289  1.10  itojun 			       (long int)timeout->tv_sec,
    290  1.10  itojun 			       (long int)timeout->tv_usec);
    291  1.10  itojun 		} else {
    292  1.10  itojun 			syslog(LOG_DEBUG,
    293  1.10  itojun 			       "<%s> there's no timer. waiting for inputs",
    294  1.10  itojun 			       __FUNCTION__);
    295  1.10  itojun 		}
    296   1.1  itojun 
    297   1.1  itojun 		if ((i = select(maxfd + 1, &select_fd,
    298   1.9  itojun 				NULL, NULL, timeout)) < 0) {
    299   1.9  itojun 			/* EINTR would occur upon SIGUSR1 for status dump */
    300   1.9  itojun 			if (errno != EINTR)
    301   1.9  itojun 				syslog(LOG_ERR, "<%s> select: %s",
    302   1.9  itojun 				       __FUNCTION__, strerror(errno));
    303   1.1  itojun 			continue;
    304   1.1  itojun 		}
    305   1.1  itojun 		if (i == 0)	/* timeout */
    306   1.1  itojun 			continue;
    307  1.12  itojun 		if (rtsock != -1 && FD_ISSET(rtsock, &select_fd))
    308   1.1  itojun 			rtmsg_input();
    309   1.1  itojun 		if (FD_ISSET(sock, &select_fd))
    310   1.1  itojun 			rtadvd_input();
    311   1.1  itojun 	}
    312   1.1  itojun 	exit(0);		/* NOTREACHED */
    313   1.1  itojun }
    314   1.1  itojun 
    315   1.1  itojun static void
    316   1.9  itojun rtadvd_set_dump_file()
    317   1.9  itojun {
    318   1.9  itojun 	do_dump = 1;
    319   1.9  itojun }
    320   1.9  itojun 
    321   1.9  itojun static void
    322  1.11  itojun set_die(sig)
    323   1.5  itojun 	int sig;
    324   1.5  itojun {
    325  1.11  itojun 	do_die = 1;
    326  1.11  itojun }
    327  1.11  itojun 
    328  1.11  itojun static void
    329  1.11  itojun die()
    330  1.11  itojun {
    331   1.5  itojun 	struct rainfo *ra;
    332   1.5  itojun 	int i;
    333   1.5  itojun 	const int retrans = MAX_FINAL_RTR_ADVERTISEMENTS;
    334   1.5  itojun 
    335   1.5  itojun 	if (dflag > 1) {
    336   1.5  itojun 		syslog(LOG_DEBUG, "<%s> cease to be an advertising router\n",
    337   1.5  itojun 		    __FUNCTION__);
    338   1.5  itojun 	}
    339   1.5  itojun 
    340   1.5  itojun 	for (ra = ralist; ra; ra = ra->next) {
    341   1.5  itojun 		ra->lifetime = 0;
    342   1.5  itojun 		make_packet(ra);
    343   1.5  itojun 	}
    344   1.5  itojun 	for (i = 0; i < retrans; i++) {
    345   1.5  itojun 		for (ra = ralist; ra; ra = ra->next)
    346   1.5  itojun 			ra_output(ra);
    347   1.5  itojun 		sleep(MIN_DELAY_BETWEEN_RAS);
    348   1.5  itojun 	}
    349   1.5  itojun 	exit(0);
    350   1.5  itojun 	/*NOTREACHED*/
    351   1.5  itojun }
    352   1.5  itojun 
    353   1.5  itojun static void
    354   1.1  itojun rtmsg_input()
    355   1.1  itojun {
    356   1.9  itojun 	int n, type, ifindex = 0, plen;
    357   1.2  itojun 	size_t len;
    358   1.1  itojun 	char msg[2048], *next, *lim;
    359  1.11  itojun 	u_char ifname[IF_NAMESIZE];
    360   1.1  itojun 	struct prefix *prefix;
    361   1.1  itojun 	struct rainfo *rai;
    362   1.1  itojun 	struct in6_addr *addr;
    363   1.1  itojun 	char addrbuf[INET6_ADDRSTRLEN];
    364   1.1  itojun 
    365  1.11  itojun 	n = read(rtsock, msg, sizeof(msg));
    366   1.1  itojun 	if (dflag > 1) {
    367   1.1  itojun 		syslog(LOG_DEBUG,
    368   1.1  itojun 		       "<%s> received a routing message "
    369   1.1  itojun 		       "(type = %d, len = %d)",
    370   1.1  itojun 		       __FUNCTION__,
    371   1.1  itojun 		       rtmsg_type(msg), n);
    372   1.1  itojun 	}
    373   1.1  itojun 	if (n > rtmsg_len(msg)) {
    374   1.1  itojun 		/*
    375   1.1  itojun 		 * This usually won't happen for messages received on
    376  1.10  itojun 		 * a routing socket.
    377   1.1  itojun 		 */
    378   1.1  itojun 		if (dflag > 1)
    379   1.1  itojun 			syslog(LOG_DEBUG,
    380   1.1  itojun 			       "<%s> received data length is larger than"
    381   1.1  itojun 			       "1st routing message len. multiple messages?"
    382   1.1  itojun 			       " read %d bytes, but 1st msg len = %d",
    383   1.1  itojun 			       __FUNCTION__, n, rtmsg_len(msg));
    384   1.1  itojun #if 0
    385   1.1  itojun 		/* adjust length */
    386   1.1  itojun 		n = rtmsg_len(msg);
    387   1.1  itojun #endif
    388   1.1  itojun 	}
    389   1.1  itojun 
    390   1.1  itojun 	lim = msg + n;
    391   1.1  itojun 	for (next = msg; next < lim; next += len) {
    392  1.10  itojun 		int oldifflags;
    393  1.10  itojun 
    394   1.1  itojun 		next = get_next_msg(next, lim, 0, &len,
    395   1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_ADD) |
    396   1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_DELETE) |
    397   1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_NEWADDR) |
    398   1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_DELADDR) |
    399   1.1  itojun 				    RTADV_TYPE2BITMASK(RTM_IFINFO));
    400   1.1  itojun 		if (len == 0)
    401   1.1  itojun 			break;
    402   1.1  itojun 		type = rtmsg_type(next);
    403   1.1  itojun 		switch (type) {
    404   1.1  itojun 		case RTM_ADD:
    405   1.1  itojun 		case RTM_DELETE:
    406   1.1  itojun 			ifindex = get_rtm_ifindex(next);
    407   1.1  itojun 			break;
    408   1.1  itojun 		case RTM_NEWADDR:
    409   1.1  itojun 		case RTM_DELADDR:
    410   1.1  itojun 			ifindex = get_ifam_ifindex(next);
    411   1.1  itojun 			break;
    412   1.1  itojun 		case RTM_IFINFO:
    413   1.1  itojun 			ifindex = get_ifm_ifindex(next);
    414   1.1  itojun 			break;
    415   1.1  itojun 		default:
    416   1.1  itojun 			/* should not reach here */
    417   1.1  itojun 			if (dflag > 1) {
    418   1.1  itojun 				syslog(LOG_DEBUG,
    419   1.1  itojun 				       "<%s:%d> unknown rtmsg %d on %s",
    420   1.1  itojun 				       __FUNCTION__, __LINE__, type,
    421   1.1  itojun 				       if_indextoname(ifindex, ifname));
    422   1.1  itojun 			}
    423  1.10  itojun 			continue;
    424   1.1  itojun 		}
    425   1.1  itojun 
    426   1.1  itojun 		if ((rai = if_indextorainfo(ifindex)) == NULL) {
    427   1.1  itojun 			if (dflag > 1) {
    428   1.1  itojun 				syslog(LOG_DEBUG,
    429   1.1  itojun 				       "<%s> route changed on "
    430   1.1  itojun 				       "non advertising interface(%s)",
    431   1.1  itojun 				       __FUNCTION__,
    432   1.1  itojun 				       if_indextoname(ifindex, ifname));
    433   1.1  itojun 			}
    434  1.10  itojun 			continue;
    435   1.1  itojun 		}
    436  1.10  itojun 		oldifflags = iflist[ifindex]->ifm_flags;
    437   1.1  itojun 
    438   1.1  itojun 		switch(type) {
    439   1.1  itojun 		 case RTM_ADD:
    440  1.10  itojun 			 /* init ifflags because it may have changed */
    441   1.1  itojun 			 iflist[ifindex]->ifm_flags =
    442   1.1  itojun 			 	if_getflags(ifindex,
    443   1.1  itojun 					    iflist[ifindex]->ifm_flags);
    444   1.1  itojun 
    445  1.10  itojun 			 if (sflag)
    446  1.10  itojun 				 break;	/* we aren't interested in prefixes  */
    447  1.10  itojun 
    448   1.1  itojun 			 addr = get_addr(msg);
    449   1.1  itojun 			 plen = get_prefixlen(msg);
    450   1.1  itojun 			 /* sanity check for plen */
    451   1.1  itojun 			 if (plen < 4 /* as RFC2373, prefixlen is at least 4 */
    452   1.1  itojun 			     || plen > 127) {
    453   1.1  itojun 				syslog(LOG_INFO, "<%s> new interface route's"
    454   1.1  itojun 				       "plen %d is invalid for a prefix",
    455   1.1  itojun 				       __FUNCTION__, plen);
    456  1.10  itojun 				break;
    457   1.1  itojun 			 }
    458   1.1  itojun 			 prefix = find_prefix(rai, addr, plen);
    459   1.1  itojun 			 if (prefix) {
    460   1.1  itojun 				 if (dflag > 1) {
    461   1.1  itojun 					 syslog(LOG_DEBUG,
    462   1.1  itojun 						"<%s> new prefix(%s/%d) "
    463   1.1  itojun 						"added on %s, "
    464   1.1  itojun 						"but it was already in list",
    465   1.1  itojun 						__FUNCTION__,
    466   1.1  itojun 						inet_ntop(AF_INET6,
    467   1.1  itojun 							  addr, (char *)addrbuf,
    468   1.1  itojun 							  INET6_ADDRSTRLEN),
    469   1.1  itojun 						plen,
    470   1.1  itojun 						rai->ifname);
    471   1.1  itojun 				 }
    472  1.10  itojun 				 break;
    473   1.1  itojun 			 }
    474   1.1  itojun 			 make_prefix(rai, ifindex, addr, plen);
    475   1.1  itojun 			 break;
    476   1.1  itojun 		 case RTM_DELETE:
    477   1.1  itojun 			 /* init ifflags because it may have changed */
    478   1.1  itojun 			 iflist[ifindex]->ifm_flags =
    479   1.1  itojun 			 	if_getflags(ifindex,
    480   1.1  itojun 					    iflist[ifindex]->ifm_flags);
    481   1.1  itojun 
    482  1.10  itojun 			 if (sflag)
    483  1.10  itojun 				 break;
    484  1.10  itojun 
    485   1.1  itojun 			 addr = get_addr(msg);
    486   1.1  itojun 			 plen = get_prefixlen(msg);
    487   1.1  itojun 			 /* sanity check for plen */
    488   1.1  itojun 			 if (plen < 4 /* as RFC2373, prefixlen is at least 4 */
    489   1.1  itojun 			     || plen > 127) {
    490   1.1  itojun 				syslog(LOG_INFO, "<%s> deleted interface"
    491   1.1  itojun 				       "route's"
    492   1.1  itojun 				       "plen %d is invalid for a prefix",
    493   1.1  itojun 				       __FUNCTION__, plen);
    494  1.10  itojun 				break;
    495   1.1  itojun 			 }
    496   1.1  itojun 			 prefix = find_prefix(rai, addr, plen);
    497   1.1  itojun 			 if (prefix == NULL) {
    498   1.1  itojun 				 if (dflag > 1) {
    499   1.1  itojun 					 syslog(LOG_DEBUG,
    500   1.1  itojun 						"<%s> prefix(%s/%d) was "
    501   1.1  itojun 						"deleted on %s, "
    502   1.1  itojun 						"but it was not in list",
    503   1.1  itojun 						__FUNCTION__,
    504   1.1  itojun 						inet_ntop(AF_INET6,
    505   1.1  itojun 							  addr, (char *)addrbuf,
    506   1.1  itojun 							  INET6_ADDRSTRLEN),
    507   1.1  itojun 						plen,
    508   1.1  itojun 						rai->ifname);
    509   1.1  itojun 				 }
    510  1.10  itojun 				 break;
    511   1.1  itojun 			 }
    512   1.1  itojun 			 delete_prefix(rai, prefix);
    513   1.1  itojun 			 break;
    514   1.1  itojun 		case RTM_NEWADDR:
    515   1.1  itojun 		case RTM_DELADDR:
    516   1.1  itojun 			 /* init ifflags because it may have changed */
    517   1.1  itojun 			 iflist[ifindex]->ifm_flags =
    518   1.1  itojun 			 	if_getflags(ifindex,
    519   1.1  itojun 					    iflist[ifindex]->ifm_flags);
    520   1.1  itojun 			 break;
    521   1.1  itojun 		case RTM_IFINFO:
    522   1.1  itojun 			 iflist[ifindex]->ifm_flags = get_ifm_flags(next);
    523   1.1  itojun 			 break;
    524   1.1  itojun 		default:
    525   1.1  itojun 			/* should not reach here */
    526   1.1  itojun 			if (dflag > 1) {
    527   1.1  itojun 				syslog(LOG_DEBUG,
    528   1.1  itojun 				       "<%s:%d> unknown rtmsg %d on %s",
    529   1.1  itojun 				       __FUNCTION__, __LINE__, type,
    530   1.1  itojun 				       if_indextoname(ifindex, ifname));
    531   1.1  itojun 			}
    532   1.1  itojun 			return;
    533   1.1  itojun 		}
    534  1.10  itojun 
    535  1.10  itojun 		/* check if an interface flag is changed */
    536  1.10  itojun 		if ((oldifflags & IFF_UP) != 0 &&	/* UP to DOWN */
    537  1.10  itojun 		    (iflist[ifindex]->ifm_flags & IFF_UP) == 0) {
    538  1.10  itojun 			syslog(LOG_INFO,
    539  1.10  itojun 			       "<%s> interface %s becomes down. stop timer.",
    540  1.10  itojun 			       __FUNCTION__, rai->ifname);
    541  1.10  itojun 			rtadvd_remove_timer(&rai->timer);
    542  1.10  itojun 		}
    543  1.10  itojun 		else if ((oldifflags & IFF_UP) == 0 &&	/* DOWN to UP */
    544  1.10  itojun 			 (iflist[ifindex]->ifm_flags & IFF_UP) != 0) {
    545  1.10  itojun 			syslog(LOG_INFO,
    546  1.10  itojun 			       "<%s> interface %s becomes up. restart timer.",
    547  1.10  itojun 			       __FUNCTION__, rai->ifname);
    548  1.10  itojun 
    549  1.10  itojun 			rai->initcounter = 0; /* reset the counter */
    550  1.10  itojun 			rai->waiting = 0; /* XXX */
    551  1.10  itojun 			rai->timer = rtadvd_add_timer(ra_timeout,
    552  1.10  itojun 						      ra_timer_update,
    553  1.10  itojun 						      rai, rai);
    554  1.10  itojun 			ra_timer_update((void *)rai, &rai->timer->tm);
    555  1.10  itojun 			rtadvd_set_timer(&rai->timer->tm, rai->timer);
    556  1.10  itojun 		}
    557   1.1  itojun 	}
    558   1.1  itojun 
    559   1.1  itojun 	return;
    560   1.1  itojun }
    561   1.1  itojun 
    562   1.1  itojun void
    563   1.1  itojun rtadvd_input()
    564   1.1  itojun {
    565   1.1  itojun 	int i;
    566   1.1  itojun 	int *hlimp = NULL;
    567   1.1  itojun #ifdef OLDRAWSOCKET
    568   1.1  itojun 	struct ip6_hdr *ip;
    569   1.1  itojun #endif
    570   1.1  itojun 	struct icmp6_hdr *icp;
    571   1.1  itojun 	int ifindex = 0;
    572   1.1  itojun 	struct cmsghdr *cm;
    573   1.1  itojun 	struct in6_pktinfo *pi = NULL;
    574   1.1  itojun 	u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
    575   1.1  itojun 	struct in6_addr dst = in6addr_any;
    576   1.1  itojun 
    577   1.1  itojun 	/*
    578   1.1  itojun 	 * Get message. We reset msg_controllen since the field could
    579   1.1  itojun 	 * be modified if we had received a message before setting
    580   1.1  itojun 	 * receive options.
    581   1.1  itojun 	 */
    582   1.6  itojun 	rcvmhdr.msg_controllen = rcvcmsgbuflen;
    583   1.1  itojun 	if ((i = recvmsg(sock, &rcvmhdr, 0)) < 0)
    584   1.1  itojun 		return;
    585   1.1  itojun 
    586   1.1  itojun 	/* extract optional information via Advanced API */
    587   1.1  itojun 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
    588   1.1  itojun 	     cm;
    589   1.1  itojun 	     cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
    590   1.1  itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
    591   1.1  itojun 		    cm->cmsg_type == IPV6_PKTINFO &&
    592   1.1  itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
    593   1.1  itojun 			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
    594   1.1  itojun 			ifindex = pi->ipi6_ifindex;
    595   1.1  itojun 			dst = pi->ipi6_addr;
    596   1.1  itojun 		}
    597   1.1  itojun 		if (cm->cmsg_level == IPPROTO_IPV6 &&
    598   1.1  itojun 		    cm->cmsg_type == IPV6_HOPLIMIT &&
    599   1.1  itojun 		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
    600   1.1  itojun 			hlimp = (int *)CMSG_DATA(cm);
    601   1.1  itojun 	}
    602   1.1  itojun 	if (ifindex == 0) {
    603   1.1  itojun 		syslog(LOG_ERR,
    604   1.1  itojun 		       "<%s> failed to get receiving interface",
    605   1.1  itojun 		       __FUNCTION__);
    606   1.1  itojun 		return;
    607   1.1  itojun 	}
    608   1.1  itojun 	if (hlimp == NULL) {
    609   1.1  itojun 		syslog(LOG_ERR,
    610   1.1  itojun 		       "<%s> failed to get receiving hop limit",
    611   1.1  itojun 		       __FUNCTION__);
    612   1.1  itojun 		return;
    613   1.1  itojun 	}
    614   1.1  itojun 
    615  1.10  itojun 	/*
    616  1.10  itojun 	 * If we happen to receive data on an interface which is now down,
    617  1.10  itojun 	 * just discard the data.
    618  1.10  itojun 	 */
    619  1.10  itojun 	if ((iflist[pi->ipi6_ifindex]->ifm_flags & IFF_UP) == 0) {
    620  1.10  itojun 		syslog(LOG_INFO,
    621  1.10  itojun 		       "<%s> received data on a disabled interface (%s)",
    622  1.10  itojun 		       __FUNCTION__,
    623  1.10  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    624  1.10  itojun 		return;
    625  1.10  itojun 	}
    626  1.10  itojun 
    627   1.1  itojun #ifdef OLDRAWSOCKET
    628   1.1  itojun 	if (i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
    629   1.1  itojun 		syslog(LOG_ERR,
    630   1.1  itojun 		       "<%s> packet size(%d) is too short",
    631   1.1  itojun 		       __FUNCTION__, i);
    632   1.1  itojun 		return;
    633   1.1  itojun 	}
    634   1.1  itojun 
    635   1.1  itojun 	ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
    636   1.1  itojun 	icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
    637   1.1  itojun #else
    638   1.1  itojun 	if (i < sizeof(struct icmp6_hdr)) {
    639   1.1  itojun 		syslog(LOG_ERR,
    640   1.1  itojun 		       "<%s> packet size(%d) is too short",
    641   1.1  itojun 		       __FUNCTION__, i);
    642   1.1  itojun 		return;
    643   1.1  itojun 	}
    644   1.1  itojun 
    645   1.1  itojun 	icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
    646  1.10  itojun #endif
    647   1.1  itojun 
    648   1.1  itojun 	switch(icp->icmp6_type) {
    649   1.1  itojun 	 case ND_ROUTER_SOLICIT:
    650   1.3  itojun 		 /*
    651   1.3  itojun 		  * Message verification - RFC-2461 6.1.1
    652   1.3  itojun 		  * XXX: these checks must be done in the kernel as well,
    653   1.3  itojun 		  *      but we can't completely rely on them.
    654   1.3  itojun 		  */
    655   1.1  itojun 		 if (*hlimp != 255) {
    656   1.1  itojun 			 syslog(LOG_NOTICE,
    657   1.3  itojun 				"<%s> RS with invalid hop limit(%d) "
    658   1.1  itojun 				"received from %s on %s",
    659   1.1  itojun 				__FUNCTION__, *hlimp,
    660   1.1  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    661   1.1  itojun 					  INET6_ADDRSTRLEN),
    662   1.1  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    663   1.1  itojun 			 return;
    664   1.1  itojun 		 }
    665   1.3  itojun 		 if (icp->icmp6_code) {
    666   1.3  itojun 			 syslog(LOG_NOTICE,
    667   1.3  itojun 				"<%s> RS with invalid ICMP6 code(%d) "
    668   1.3  itojun 				"received from %s on %s",
    669   1.3  itojun 				__FUNCTION__, icp->icmp6_code,
    670   1.3  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    671   1.3  itojun 					  INET6_ADDRSTRLEN),
    672   1.3  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    673   1.3  itojun 			 return;
    674   1.3  itojun 		 }
    675   1.3  itojun 		 if (i < sizeof(struct nd_router_solicit)) {
    676   1.3  itojun 			 syslog(LOG_NOTICE,
    677   1.3  itojun 				"<%s> RS from %s on %s does not have enough "
    678   1.3  itojun 				"length (len = %d)",
    679   1.3  itojun 				__FUNCTION__,
    680   1.3  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    681   1.3  itojun 					  INET6_ADDRSTRLEN),
    682   1.3  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
    683   1.3  itojun 			 return;
    684   1.3  itojun 		 }
    685   1.1  itojun 		 rs_input(i, (struct nd_router_solicit *)icp, pi, &from);
    686   1.1  itojun 		 break;
    687   1.1  itojun 	 case ND_ROUTER_ADVERT:
    688   1.3  itojun 		 /*
    689   1.3  itojun 		  * Message verification - RFC-2461 6.1.2
    690   1.3  itojun 		  * XXX: there's a same dilemma as above...
    691   1.3  itojun 		  */
    692   1.1  itojun 		 if (*hlimp != 255) {
    693   1.1  itojun 			 syslog(LOG_NOTICE,
    694   1.3  itojun 				"<%s> RA with invalid hop limit(%d) "
    695   1.1  itojun 				"received from %s on %s",
    696   1.1  itojun 				__FUNCTION__, *hlimp,
    697   1.1  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    698   1.1  itojun 					  INET6_ADDRSTRLEN),
    699   1.1  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    700   1.3  itojun 			 return;
    701   1.3  itojun 		 }
    702   1.3  itojun 		 if (icp->icmp6_code) {
    703   1.3  itojun 			 syslog(LOG_NOTICE,
    704   1.3  itojun 				"<%s> RA with invalid ICMP6 code(%d) "
    705   1.3  itojun 				"received from %s on %s",
    706   1.3  itojun 				__FUNCTION__, icp->icmp6_code,
    707   1.3  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    708   1.3  itojun 					  INET6_ADDRSTRLEN),
    709   1.3  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    710   1.3  itojun 			 return;
    711   1.3  itojun 		 }
    712   1.3  itojun 		 if (i < sizeof(struct nd_router_advert)) {
    713   1.3  itojun 			 syslog(LOG_NOTICE,
    714   1.3  itojun 				"<%s> RA from %s on %s does not have enough "
    715   1.3  itojun 				"length (len = %d)",
    716   1.3  itojun 				__FUNCTION__,
    717   1.3  itojun 				inet_ntop(AF_INET6, &from.sin6_addr, ntopbuf,
    718   1.3  itojun 					  INET6_ADDRSTRLEN),
    719   1.3  itojun 				if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
    720   1.1  itojun 			 return;
    721   1.1  itojun 		 }
    722   1.1  itojun 		 ra_input(i, (struct nd_router_advert *)icp, pi, &from);
    723   1.1  itojun 		 break;
    724   1.1  itojun 	 case ICMP6_ROUTER_RENUMBERING:
    725   1.1  itojun 		 if (accept_rr == 0) {
    726   1.1  itojun 			 syslog(LOG_ERR,
    727   1.1  itojun 				"<%s> received a router renumbering "
    728   1.1  itojun 				"message, but not allowed to be accepted",
    729   1.1  itojun 				__FUNCTION__);
    730   1.1  itojun 			 break;
    731   1.1  itojun 		 }
    732   1.1  itojun 		 rr_input(i, (struct icmp6_router_renum *)icp, pi, &from,
    733   1.1  itojun 			  &dst);
    734   1.1  itojun 		 break;
    735   1.1  itojun 	 default:
    736   1.1  itojun 		 /*
    737   1.1  itojun 		  * Note that this case is POSSIBLE, especially just
    738   1.1  itojun 		  * after invocation of the daemon. This is because we
    739   1.1  itojun 		  * could receive message after opening the socket and
    740   1.1  itojun 		  * before setting ICMP6 type filter(see sock_open()).
    741   1.1  itojun 		  */
    742   1.1  itojun 		 syslog(LOG_ERR,
    743   1.1  itojun 			"<%s> invalid icmp type(%d)",
    744   1.1  itojun 			__FUNCTION__, icp->icmp6_type);
    745   1.1  itojun 		 return;
    746   1.1  itojun 	}
    747   1.1  itojun 
    748   1.1  itojun 	return;
    749   1.1  itojun }
    750   1.1  itojun 
    751   1.1  itojun static void
    752   1.1  itojun rs_input(int len, struct nd_router_solicit *rs,
    753   1.1  itojun 	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
    754   1.1  itojun {
    755   1.1  itojun 	u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
    756   1.1  itojun 	union nd_opts ndopts;
    757   1.1  itojun 	struct rainfo *ra;
    758   1.1  itojun 
    759   1.1  itojun 	syslog(LOG_DEBUG,
    760   1.1  itojun 	       "<%s> RS received from %s on %s",
    761   1.1  itojun 	       __FUNCTION__,
    762   1.1  itojun 	       inet_ntop(AF_INET6, &from->sin6_addr,
    763   1.1  itojun 			 ntopbuf, INET6_ADDRSTRLEN),
    764   1.1  itojun 	       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    765   1.1  itojun 
    766   1.1  itojun 	/* ND option check */
    767   1.1  itojun 	memset(&ndopts, 0, sizeof(ndopts));
    768   1.1  itojun 	if (nd6_options((struct nd_opt_hdr *)(rs + 1),
    769   1.1  itojun 			len - sizeof(struct nd_router_solicit),
    770   1.1  itojun 			 &ndopts, NDOPT_FLAG_SRCLINKADDR)) {
    771   1.1  itojun 		syslog(LOG_DEBUG,
    772   1.1  itojun 		       "<%s> ND option check failed for an RS from %s on %s",
    773   1.1  itojun 		       __FUNCTION__,
    774   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    775   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    776   1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    777   1.1  itojun 		return;
    778   1.1  itojun 	}
    779   1.1  itojun 
    780   1.1  itojun 	/*
    781   1.1  itojun 	 * If the IP source address is the unspecified address, there
    782   1.1  itojun 	 * must be no source link-layer address option in the message.
    783   1.1  itojun 	 * (RFC-2461 6.1.1)
    784   1.1  itojun 	 */
    785   1.1  itojun 	if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
    786   1.1  itojun 	    ndopts.nd_opts_src_lladdr) {
    787   1.1  itojun 		syslog(LOG_ERR,
    788   1.1  itojun 		       "<%s> RS from unspecified src on %s has a link-layer"
    789   1.1  itojun 		       " address option",
    790   1.1  itojun 		       __FUNCTION__,
    791   1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    792   1.1  itojun 		goto done;
    793   1.1  itojun 	}
    794   1.1  itojun 
    795   1.1  itojun 	ra = ralist;
    796   1.1  itojun 	while (ra != NULL) {
    797   1.1  itojun 		if (pi->ipi6_ifindex == ra->ifindex)
    798   1.1  itojun 			break;
    799   1.1  itojun 		ra = ra->next;
    800   1.1  itojun 	}
    801   1.1  itojun 	if (ra == NULL) {
    802   1.1  itojun 		syslog(LOG_INFO,
    803   1.1  itojun 		       "<%s> RS received on non advertising interface(%s)",
    804   1.1  itojun 		       __FUNCTION__,
    805   1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    806   1.1  itojun 		goto done;
    807   1.1  itojun 	}
    808   1.1  itojun 
    809   1.9  itojun 	ra->rsinput++;		/* increment statistics */
    810   1.9  itojun 
    811   1.1  itojun 	/*
    812   1.1  itojun 	 * Decide whether to send RA according to the rate-limit
    813   1.1  itojun 	 * consideration.
    814   1.1  itojun 	 */
    815   1.1  itojun 	{
    816   1.1  itojun 		long delay;	/* must not be greater than 1000000 */
    817   1.1  itojun 		struct timeval interval, now, min_delay, tm_tmp, *rest;
    818  1.10  itojun 		struct soliciter *sol;
    819  1.10  itojun 
    820  1.10  itojun 		/*
    821  1.10  itojun 		 * record sockaddr waiting for RA, if possible
    822  1.10  itojun 		 */
    823  1.10  itojun 		sol = (struct soliciter *)malloc(sizeof(*sol));
    824  1.10  itojun 		if (sol) {
    825  1.10  itojun 			sol->addr = *from;
    826  1.10  itojun 			/*XXX RFC2553 need clarification on flowinfo */
    827  1.10  itojun 			sol->addr.sin6_flowinfo = 0;
    828  1.10  itojun 			sol->next = ra->soliciter;
    829  1.10  itojun 			ra->soliciter = sol->next;
    830  1.10  itojun 		}
    831   1.1  itojun 
    832   1.1  itojun 		/*
    833   1.1  itojun 		 * If there is already a waiting RS packet, don't
    834   1.1  itojun 		 * update the timer.
    835   1.1  itojun 		 */
    836   1.1  itojun 		if (ra->waiting++)
    837   1.1  itojun 			goto done;
    838   1.1  itojun 
    839   1.1  itojun 		/*
    840   1.1  itojun 		 * Compute a random delay. If the computed value
    841   1.1  itojun 		 * corresponds to a time later than the time the next
    842   1.1  itojun 		 * multicast RA is scheduled to be sent, ignore the random
    843   1.1  itojun 		 * delay and send the advertisement at the
    844   1.1  itojun 		 * already-scheduled time. RFC-2461 6.2.6
    845   1.1  itojun 		 */
    846   1.1  itojun 		delay = random() % MAX_RA_DELAY_TIME;
    847   1.1  itojun 		interval.tv_sec = 0;
    848   1.1  itojun 		interval.tv_usec = delay;
    849   1.1  itojun 		rest = rtadvd_timer_rest(ra->timer);
    850   1.1  itojun 		if (TIMEVAL_LT(*rest, interval)) {
    851   1.1  itojun 			syslog(LOG_DEBUG,
    852   1.1  itojun 			       "<%s> random delay is larger than "
    853   1.1  itojun 			       "the rest of normal timer",
    854   1.1  itojun 			       __FUNCTION__);
    855   1.1  itojun 			interval = *rest;
    856   1.1  itojun 		}
    857   1.1  itojun 
    858   1.1  itojun 		/*
    859   1.1  itojun 		 * If we sent a multicast Router Advertisement within
    860   1.1  itojun 		 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
    861   1.1  itojun 		 * the advertisement to be sent at a time corresponding to
    862   1.1  itojun 		 * MIN_DELAY_BETWEEN_RAS plus the random value after the
    863   1.1  itojun 		 * previous advertisement was sent.
    864   1.1  itojun 		 */
    865   1.1  itojun 		gettimeofday(&now, NULL);
    866   1.1  itojun 		TIMEVAL_SUB(&now, &ra->lastsent, &tm_tmp);
    867   1.1  itojun 		min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
    868   1.1  itojun 		min_delay.tv_usec = 0;
    869   1.1  itojun 		if (TIMEVAL_LT(tm_tmp, min_delay)) {
    870   1.1  itojun 			TIMEVAL_SUB(&min_delay, &tm_tmp, &min_delay);
    871   1.1  itojun 			TIMEVAL_ADD(&min_delay, &interval, &interval);
    872   1.1  itojun 		}
    873   1.1  itojun 		rtadvd_set_timer(&interval, ra->timer);
    874  1.10  itojun 		goto done;
    875   1.1  itojun 	}
    876   1.1  itojun 
    877   1.1  itojun   done:
    878   1.1  itojun 	free_ndopts(&ndopts);
    879   1.1  itojun 	return;
    880   1.1  itojun }
    881   1.1  itojun 
    882   1.1  itojun static void
    883   1.1  itojun ra_input(int len, struct nd_router_advert *ra,
    884   1.1  itojun 	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
    885   1.1  itojun {
    886   1.1  itojun 	struct rainfo *rai;
    887   1.1  itojun 	u_char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
    888   1.1  itojun 	union nd_opts ndopts;
    889   1.1  itojun 	char *on_off[] = {"OFF", "ON"};
    890   1.1  itojun 	u_int32_t reachabletime, retranstimer, mtu;
    891   1.9  itojun 	int inconsistent = 0;
    892   1.1  itojun 
    893   1.1  itojun 	syslog(LOG_DEBUG,
    894   1.1  itojun 	       "<%s> RA received from %s on %s",
    895   1.1  itojun 	       __FUNCTION__,
    896   1.1  itojun 	       inet_ntop(AF_INET6, &from->sin6_addr,
    897   1.1  itojun 			 ntopbuf, INET6_ADDRSTRLEN),
    898   1.1  itojun 	       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    899   1.1  itojun 
    900   1.1  itojun 	/* ND option check */
    901   1.1  itojun 	memset(&ndopts, 0, sizeof(ndopts));
    902   1.1  itojun 	if (nd6_options((struct nd_opt_hdr *)(ra + 1),
    903   1.1  itojun 			len - sizeof(struct nd_router_advert),
    904  1.11  itojun 			&ndopts, NDOPT_FLAG_SRCLINKADDR |
    905  1.11  itojun 			NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU)) {
    906   1.1  itojun 		syslog(LOG_ERR,
    907   1.1  itojun 		       "<%s> ND option check failed for an RA from %s on %s",
    908   1.1  itojun 		       __FUNCTION__,
    909   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    910   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    911   1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    912   1.1  itojun 		return;
    913   1.1  itojun 	}
    914   1.1  itojun 
    915   1.1  itojun 	/*
    916   1.1  itojun 	 * RA consistency check according to RFC-2461 6.2.7
    917   1.1  itojun 	 */
    918   1.1  itojun 	if ((rai = if_indextorainfo(pi->ipi6_ifindex)) == 0) {
    919   1.1  itojun 		syslog(LOG_INFO,
    920   1.1  itojun 		       "<%s> received RA from %s on non-advertising"
    921   1.1  itojun 		       " interface(%s)",
    922   1.1  itojun 		       __FUNCTION__,
    923   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    924   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    925   1.1  itojun 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
    926   1.1  itojun 		goto done;
    927   1.1  itojun 	}
    928   1.9  itojun 	rai->rainput++;		/* increment statistics */
    929   1.9  itojun 
    930   1.1  itojun 	/* Cur Hop Limit value */
    931   1.1  itojun 	if (ra->nd_ra_curhoplimit && rai->hoplimit &&
    932   1.1  itojun 	    ra->nd_ra_curhoplimit != rai->hoplimit) {
    933  1.11  itojun 		syslog(LOG_INFO,
    934   1.1  itojun 		       "<%s> CurHopLimit inconsistent on %s:"
    935   1.1  itojun 		       " %d from %s, %d from us",
    936   1.1  itojun 		       __FUNCTION__,
    937   1.1  itojun 		       rai->ifname,
    938   1.1  itojun 		       ra->nd_ra_curhoplimit,
    939   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    940   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    941   1.1  itojun 		       rai->hoplimit);
    942   1.9  itojun 		inconsistent++;
    943   1.1  itojun 	}
    944   1.1  itojun 	/* M flag */
    945   1.1  itojun 	if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
    946   1.1  itojun 	    rai->managedflg) {
    947  1.11  itojun 		syslog(LOG_INFO,
    948   1.1  itojun 		       "<%s> M flag inconsistent on %s:"
    949   1.1  itojun 		       " %s from %s, %s from us",
    950   1.1  itojun 		       __FUNCTION__,
    951   1.1  itojun 		       rai->ifname,
    952   1.1  itojun 		       on_off[!rai->managedflg],
    953   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    954   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    955   1.1  itojun 		       on_off[rai->managedflg]);
    956   1.9  itojun 		inconsistent++;
    957   1.1  itojun 	}
    958   1.1  itojun 	/* O flag */
    959   1.1  itojun 	if ((ra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
    960   1.1  itojun 	    rai->otherflg) {
    961  1.11  itojun 		syslog(LOG_INFO,
    962   1.1  itojun 		       "<%s> O flag inconsistent on %s:"
    963   1.1  itojun 		       " %s from %s, %s from us",
    964   1.1  itojun 		       __FUNCTION__,
    965   1.1  itojun 		       rai->ifname,
    966   1.1  itojun 		       on_off[!rai->otherflg],
    967   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    968   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    969   1.1  itojun 		       on_off[rai->otherflg]);
    970   1.9  itojun 		inconsistent++;
    971   1.1  itojun 	}
    972   1.1  itojun 	/* Reachable Time */
    973   1.1  itojun 	reachabletime = ntohl(ra->nd_ra_reachable);
    974   1.1  itojun 	if (reachabletime && rai->reachabletime &&
    975   1.1  itojun 	    reachabletime != rai->reachabletime) {
    976  1.11  itojun 		syslog(LOG_INFO,
    977   1.1  itojun 		       "<%s> ReachableTime inconsistent on %s:"
    978   1.1  itojun 		       " %d from %s, %d from us",
    979   1.1  itojun 		       __FUNCTION__,
    980   1.1  itojun 		       rai->ifname,
    981   1.1  itojun 		       reachabletime,
    982   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    983   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    984   1.1  itojun 		       rai->reachabletime);
    985   1.9  itojun 		inconsistent++;
    986   1.1  itojun 	}
    987   1.1  itojun 	/* Retrans Timer */
    988   1.1  itojun 	retranstimer = ntohl(ra->nd_ra_retransmit);
    989   1.1  itojun 	if (retranstimer && rai->retranstimer &&
    990   1.1  itojun 	    retranstimer != rai->retranstimer) {
    991  1.11  itojun 		syslog(LOG_INFO,
    992   1.1  itojun 		       "<%s> RetranceTimer inconsistent on %s:"
    993   1.1  itojun 		       " %d from %s, %d from us",
    994   1.1  itojun 		       __FUNCTION__,
    995   1.1  itojun 		       rai->ifname,
    996   1.1  itojun 		       retranstimer,
    997   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
    998   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
    999   1.1  itojun 		       rai->retranstimer);
   1000   1.9  itojun 		inconsistent++;
   1001   1.1  itojun 	}
   1002   1.1  itojun 	/* Values in the MTU options */
   1003   1.1  itojun 	if (ndopts.nd_opts_mtu) {
   1004   1.1  itojun 		mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu);
   1005   1.1  itojun 		if (mtu && rai->linkmtu && mtu != rai->linkmtu) {
   1006  1.11  itojun 			syslog(LOG_INFO,
   1007   1.1  itojun 			       "<%s> MTU option value inconsistent on %s:"
   1008   1.1  itojun 			       " %d from %s, %d from us",
   1009   1.1  itojun 			       __FUNCTION__,
   1010   1.1  itojun 			       rai->ifname, mtu,
   1011   1.1  itojun 			       inet_ntop(AF_INET6, &from->sin6_addr,
   1012   1.1  itojun 					 ntopbuf, INET6_ADDRSTRLEN),
   1013   1.1  itojun 			       rai->linkmtu);
   1014   1.9  itojun 			inconsistent++;
   1015   1.1  itojun 		}
   1016   1.1  itojun 	}
   1017   1.1  itojun 	/* Preferred and Valid Lifetimes for prefixes */
   1018   1.1  itojun 	{
   1019   1.1  itojun 		struct nd_optlist *optp = ndopts.nd_opts_list;
   1020  1.11  itojun 
   1021   1.9  itojun 		if (ndopts.nd_opts_pi) {
   1022   1.9  itojun 			if (prefix_check(ndopts.nd_opts_pi, rai, from))
   1023   1.9  itojun 				inconsistent++;
   1024   1.9  itojun 		}
   1025   1.1  itojun 		while (optp) {
   1026   1.9  itojun 			if (prefix_check((struct nd_opt_prefix_info *)optp->opt,
   1027   1.9  itojun 					 rai, from))
   1028   1.9  itojun 				inconsistent++;
   1029   1.1  itojun 			optp = optp->next;
   1030   1.1  itojun 		}
   1031   1.1  itojun 	}
   1032   1.9  itojun 
   1033  1.11  itojun 	if (inconsistent)
   1034   1.9  itojun 		rai->rainconsistent++;
   1035   1.1  itojun 
   1036   1.1  itojun   done:
   1037   1.1  itojun 	free_ndopts(&ndopts);
   1038   1.1  itojun 	return;
   1039   1.1  itojun }
   1040   1.1  itojun 
   1041   1.9  itojun /* return a non-zero value if the received prefix is inconsitent with ours */
   1042   1.9  itojun static int
   1043   1.1  itojun prefix_check(struct nd_opt_prefix_info *pinfo,
   1044   1.1  itojun 	     struct rainfo *rai, struct sockaddr_in6 *from)
   1045   1.1  itojun {
   1046   1.1  itojun 	u_int32_t preferred_time, valid_time;
   1047   1.1  itojun 	struct prefix *pp;
   1048   1.9  itojun 	int inconsistent = 0;
   1049   1.1  itojun 	u_char ntopbuf[INET6_ADDRSTRLEN], prefixbuf[INET6_ADDRSTRLEN];
   1050  1.11  itojun 	struct timeval now;
   1051   1.1  itojun 
   1052   1.1  itojun #if 0				/* impossible */
   1053   1.1  itojun 	if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
   1054   1.9  itojun 		return(0);
   1055   1.1  itojun #endif
   1056   1.1  itojun 
   1057   1.1  itojun 	/*
   1058   1.1  itojun 	 * log if the adveritsed prefix has link-local scope(sanity check?)
   1059   1.1  itojun 	 */
   1060   1.1  itojun 	if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix)) {
   1061   1.1  itojun 		syslog(LOG_INFO,
   1062   1.1  itojun 		       "<%s> link-local prefix %s/%d is advertised "
   1063   1.1  itojun 		       "from %s on %s",
   1064   1.1  itojun 		       __FUNCTION__,
   1065   1.1  itojun 		       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
   1066   1.1  itojun 				 prefixbuf, INET6_ADDRSTRLEN),
   1067   1.1  itojun 		       pinfo->nd_opt_pi_prefix_len,
   1068   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
   1069   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
   1070   1.1  itojun 		       rai->ifname);
   1071   1.1  itojun 	}
   1072   1.1  itojun 
   1073   1.1  itojun 	if ((pp = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
   1074   1.1  itojun 			      pinfo->nd_opt_pi_prefix_len)) == NULL) {
   1075   1.1  itojun 		syslog(LOG_INFO,
   1076   1.1  itojun 		       "<%s> prefix %s/%d from %s on %s is not in our list",
   1077   1.1  itojun 		       __FUNCTION__,
   1078   1.1  itojun 		       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
   1079   1.1  itojun 				 prefixbuf, INET6_ADDRSTRLEN),
   1080   1.1  itojun 		       pinfo->nd_opt_pi_prefix_len,
   1081   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
   1082   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
   1083   1.1  itojun 		       rai->ifname);
   1084   1.9  itojun 		return(0);
   1085   1.1  itojun 	}
   1086   1.1  itojun 
   1087   1.1  itojun 	preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
   1088  1.11  itojun 	if (pp->pltimeexpire) {
   1089  1.11  itojun 		/*
   1090  1.11  itojun 		 * The lifetime is decremented in real time, so we should
   1091  1.11  itojun 		 * compare the expiration time.
   1092  1.11  itojun 		 * (RFC 2461 Section 6.2.7.)
   1093  1.11  itojun 		 * XXX: can we really expect that all routers on the link
   1094  1.11  itojun 		 * have synchronized clocks?
   1095  1.11  itojun 		 */
   1096  1.11  itojun 		gettimeofday(&now, NULL);
   1097  1.11  itojun 		preferred_time += now.tv_sec;
   1098  1.11  itojun 
   1099  1.11  itojun 		if (rai->clockskew &&
   1100  1.11  itojun 		    abs(preferred_time - pp->pltimeexpire) > rai->clockskew) {
   1101  1.11  itojun 			syslog(LOG_INFO,
   1102  1.11  itojun 			       "<%s> prefeerred lifetime for %s/%d"
   1103  1.11  itojun 			       " (decr. in real time) inconsistent on %s:"
   1104  1.11  itojun 			       " %d from %s, %ld from us",
   1105  1.11  itojun 			       __FUNCTION__,
   1106  1.11  itojun 			       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
   1107  1.11  itojun 					 prefixbuf, INET6_ADDRSTRLEN),
   1108  1.11  itojun 			       pinfo->nd_opt_pi_prefix_len,
   1109  1.11  itojun 			       rai->ifname, preferred_time,
   1110  1.11  itojun 			       inet_ntop(AF_INET6, &from->sin6_addr,
   1111  1.11  itojun 					 ntopbuf, INET6_ADDRSTRLEN),
   1112  1.11  itojun 			       pp->pltimeexpire);
   1113  1.11  itojun 			inconsistent++;
   1114  1.11  itojun 		}
   1115  1.11  itojun 	}
   1116  1.11  itojun 	else if (preferred_time != pp->preflifetime) {
   1117  1.11  itojun 		syslog(LOG_INFO,
   1118   1.1  itojun 		       "<%s> prefeerred lifetime for %s/%d"
   1119   1.1  itojun 		       " inconsistent on %s:"
   1120   1.1  itojun 		       " %d from %s, %d from us",
   1121   1.1  itojun 		       __FUNCTION__,
   1122   1.1  itojun 		       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
   1123   1.1  itojun 				 prefixbuf, INET6_ADDRSTRLEN),
   1124   1.1  itojun 		       pinfo->nd_opt_pi_prefix_len,
   1125   1.1  itojun 		       rai->ifname, preferred_time,
   1126   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
   1127   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
   1128   1.1  itojun 		       pp->preflifetime);
   1129   1.9  itojun 	}
   1130   1.1  itojun 
   1131   1.1  itojun 	valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
   1132  1.11  itojun 	if (pp->vltimeexpire) {
   1133  1.11  itojun 		gettimeofday(&now, NULL);
   1134  1.11  itojun 		valid_time += now.tv_sec;
   1135  1.11  itojun 
   1136  1.11  itojun 		if (rai->clockskew &&
   1137  1.11  itojun 		    abs(valid_time - pp->vltimeexpire) > rai->clockskew) {
   1138  1.11  itojun 			syslog(LOG_INFO,
   1139  1.11  itojun 			       "<%s> valid lifetime for %s/%d"
   1140  1.11  itojun 			       " (decr. in real time) inconsistent on %s:"
   1141  1.11  itojun 			       " %d from %s, %ld from us",
   1142  1.11  itojun 			       __FUNCTION__,
   1143  1.11  itojun 			       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
   1144  1.11  itojun 					 prefixbuf, INET6_ADDRSTRLEN),
   1145  1.11  itojun 			       pinfo->nd_opt_pi_prefix_len,
   1146  1.11  itojun 			       rai->ifname, preferred_time,
   1147  1.11  itojun 			       inet_ntop(AF_INET6, &from->sin6_addr,
   1148  1.11  itojun 					 ntopbuf, INET6_ADDRSTRLEN),
   1149  1.11  itojun 			       pp->vltimeexpire);
   1150  1.11  itojun 			inconsistent++;
   1151  1.11  itojun 		}
   1152  1.11  itojun 	}
   1153  1.11  itojun 	else if (valid_time != pp->validlifetime) {
   1154  1.11  itojun 		syslog(LOG_INFO,
   1155   1.1  itojun 		       "<%s> valid lifetime for %s/%d"
   1156   1.1  itojun 		       " inconsistent on %s:"
   1157   1.1  itojun 		       " %d from %s, %d from us",
   1158   1.1  itojun 		       __FUNCTION__,
   1159   1.1  itojun 		       inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix,
   1160   1.1  itojun 				 prefixbuf, INET6_ADDRSTRLEN),
   1161   1.1  itojun 		       pinfo->nd_opt_pi_prefix_len,
   1162   1.1  itojun 		       rai->ifname, valid_time,
   1163   1.1  itojun 		       inet_ntop(AF_INET6, &from->sin6_addr,
   1164   1.1  itojun 				 ntopbuf, INET6_ADDRSTRLEN),
   1165   1.1  itojun 		       pp->validlifetime);
   1166   1.9  itojun 		inconsistent++;
   1167   1.9  itojun 	}
   1168   1.9  itojun 
   1169   1.9  itojun 	return(inconsistent);
   1170   1.1  itojun }
   1171   1.1  itojun 
   1172   1.1  itojun struct prefix *
   1173   1.1  itojun find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
   1174   1.1  itojun {
   1175   1.1  itojun 	struct prefix *pp;
   1176   1.1  itojun 	int bytelen, bitlen;
   1177   1.1  itojun 
   1178   1.1  itojun 	for (pp = rai->prefix.next; pp != &rai->prefix; pp = pp->next) {
   1179   1.1  itojun 		if (plen != pp->prefixlen)
   1180   1.1  itojun 			continue;
   1181   1.1  itojun 		bytelen = plen / 8;
   1182   1.1  itojun 		bitlen = plen % 8;
   1183   1.1  itojun 		if (memcmp((void *)prefix, (void *)&pp->prefix, bytelen))
   1184   1.1  itojun 			continue;
   1185   1.1  itojun 		if (prefix->s6_addr[bytelen] >> (8 - bitlen) ==
   1186   1.1  itojun 		    pp->prefix.s6_addr[bytelen] >> (8 - bitlen))
   1187   1.1  itojun 			return(pp);
   1188   1.1  itojun 	}
   1189   1.1  itojun 
   1190   1.1  itojun 	return(NULL);
   1191   1.1  itojun }
   1192   1.1  itojun 
   1193  1.11  itojun /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
   1194  1.11  itojun int
   1195  1.11  itojun prefix_match(struct in6_addr *p0, int plen0,
   1196  1.11  itojun 	     struct in6_addr *p1, int plen1)
   1197  1.11  itojun {
   1198  1.11  itojun 	int bytelen, bitlen;
   1199  1.11  itojun 
   1200  1.11  itojun 	if (plen0 < plen1)
   1201  1.11  itojun 		return(0);
   1202  1.11  itojun 	bytelen = plen1 / 8;
   1203  1.11  itojun 	bitlen = plen1 % 8;
   1204  1.11  itojun 	if (memcmp((void *)p0, (void *)p1, bytelen))
   1205  1.11  itojun 		return(0);
   1206  1.11  itojun 	if (p0->s6_addr[bytelen] >> (8 - bitlen) ==
   1207  1.11  itojun 	    p1->s6_addr[bytelen] >> (8 - bitlen))
   1208  1.11  itojun 		return(1);
   1209  1.11  itojun 
   1210  1.11  itojun 	return(0);
   1211  1.11  itojun }
   1212  1.11  itojun 
   1213   1.1  itojun static int
   1214   1.1  itojun nd6_options(struct nd_opt_hdr *hdr, int limit,
   1215   1.1  itojun 	    union nd_opts *ndopts, u_int32_t optflags)
   1216   1.1  itojun {
   1217   1.1  itojun 	int optlen = 0;
   1218   1.1  itojun 
   1219   1.1  itojun 	for (; limit > 0; limit -= optlen) {
   1220   1.1  itojun 		hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
   1221   1.1  itojun 		optlen = hdr->nd_opt_len << 3;
   1222   1.1  itojun 		if (hdr->nd_opt_len == 0) {
   1223   1.1  itojun 			syslog(LOG_ERR,
   1224   1.1  itojun 			       "<%s> bad ND option length(0) (type = %d)",
   1225   1.1  itojun 			       __FUNCTION__, hdr->nd_opt_type);
   1226   1.1  itojun 			goto bad;
   1227   1.1  itojun 		}
   1228   1.1  itojun 
   1229   1.1  itojun 		if (hdr->nd_opt_type > ND_OPT_MTU) {
   1230   1.1  itojun 			syslog(LOG_INFO,
   1231   1.1  itojun 			       "<%s> unknown ND option(type %d)",
   1232   1.1  itojun 			       __FUNCTION__,
   1233   1.1  itojun 			       hdr->nd_opt_type);
   1234   1.1  itojun 			continue;
   1235   1.1  itojun 		}
   1236   1.1  itojun 
   1237   1.1  itojun 		if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
   1238   1.1  itojun 			syslog(LOG_INFO,
   1239   1.1  itojun 			       "<%s> unexpected ND option(type %d)",
   1240   1.1  itojun 			       __FUNCTION__,
   1241   1.1  itojun 			       hdr->nd_opt_type);
   1242   1.1  itojun 			continue;
   1243   1.1  itojun 		}
   1244   1.1  itojun 
   1245   1.1  itojun 		switch(hdr->nd_opt_type) {
   1246   1.1  itojun 		 case ND_OPT_SOURCE_LINKADDR:
   1247   1.1  itojun 		 case ND_OPT_TARGET_LINKADDR:
   1248   1.1  itojun 		 case ND_OPT_REDIRECTED_HEADER:
   1249   1.1  itojun 		 case ND_OPT_MTU:
   1250   1.1  itojun 			 if (ndopts->nd_opt_array[hdr->nd_opt_type]) {
   1251   1.1  itojun 				 syslog(LOG_INFO,
   1252   1.1  itojun 					"<%s> duplicated ND option"
   1253   1.1  itojun 					" (type = %d)",
   1254   1.1  itojun 					__FUNCTION__,
   1255   1.1  itojun 					hdr->nd_opt_type);
   1256   1.1  itojun 			 }
   1257   1.1  itojun 			 ndopts->nd_opt_array[hdr->nd_opt_type] = hdr;
   1258   1.1  itojun 			 break;
   1259   1.1  itojun 		 case ND_OPT_PREFIX_INFORMATION:
   1260   1.1  itojun 		 {
   1261   1.1  itojun 			 struct nd_optlist *pfxlist;
   1262   1.1  itojun 
   1263   1.1  itojun 			 if (ndopts->nd_opts_pi == 0) {
   1264   1.1  itojun 				 ndopts->nd_opts_pi =
   1265   1.1  itojun 					 (struct nd_opt_prefix_info *)hdr;
   1266   1.1  itojun 				 continue;
   1267   1.1  itojun 			 }
   1268   1.1  itojun 			 if ((pfxlist = malloc(sizeof(*pfxlist))) == NULL) {
   1269   1.1  itojun 				 syslog(LOG_ERR,
   1270   1.1  itojun 					"<%s> can't allocate memory",
   1271   1.1  itojun 					__FUNCTION__);
   1272   1.1  itojun 				 goto bad;
   1273   1.1  itojun 			 }
   1274   1.1  itojun 			 pfxlist->next = ndopts->nd_opts_list;
   1275   1.1  itojun 			 pfxlist->opt = hdr;
   1276   1.1  itojun 			 ndopts->nd_opts_list = pfxlist;
   1277   1.1  itojun 
   1278   1.1  itojun 			 break;
   1279   1.1  itojun 		 }
   1280   1.1  itojun 		 default:	/* impossible */
   1281   1.1  itojun 			 break;
   1282   1.1  itojun 		}
   1283   1.1  itojun 	}
   1284   1.1  itojun 
   1285   1.1  itojun 	return(0);
   1286   1.1  itojun 
   1287   1.1  itojun   bad:
   1288   1.1  itojun 	free_ndopts(ndopts);
   1289   1.1  itojun 
   1290   1.1  itojun 	return(-1);
   1291   1.1  itojun }
   1292   1.1  itojun 
   1293   1.1  itojun static void
   1294   1.1  itojun free_ndopts(union nd_opts *ndopts)
   1295   1.1  itojun {
   1296   1.1  itojun 	struct nd_optlist *opt = ndopts->nd_opts_list, *next;
   1297   1.1  itojun 
   1298   1.1  itojun 	while(opt) {
   1299   1.1  itojun 		next = opt->next;
   1300   1.1  itojun 		free(opt);
   1301   1.1  itojun 		opt = next;
   1302   1.1  itojun 	}
   1303   1.1  itojun }
   1304   1.1  itojun 
   1305   1.1  itojun void
   1306   1.1  itojun sock_open()
   1307   1.1  itojun {
   1308   1.1  itojun 	struct icmp6_filter filt;
   1309   1.1  itojun 	struct ipv6_mreq mreq;
   1310   1.1  itojun 	struct rainfo *ra = ralist;
   1311   1.1  itojun 	int on;
   1312   1.1  itojun 	/* XXX: should be max MTU attached to the node */
   1313   1.1  itojun 	static u_char answer[1500];
   1314   1.6  itojun 
   1315   1.6  itojun 	rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
   1316   1.6  itojun 				CMSG_SPACE(sizeof(int));
   1317   1.6  itojun 	rcvcmsgbuf = (u_char *)malloc(rcvcmsgbuflen);
   1318   1.6  itojun 	if (rcvcmsgbuf == NULL) {
   1319   1.6  itojun 		syslog(LOG_ERR, "<%s> not enough core", __FUNCTION__);
   1320   1.6  itojun 		exit(1);
   1321   1.6  itojun 	}
   1322   1.6  itojun 
   1323   1.6  itojun 	sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
   1324   1.6  itojun 				CMSG_SPACE(sizeof(int));
   1325   1.6  itojun 	sndcmsgbuf = (u_char *)malloc(sndcmsgbuflen);
   1326   1.6  itojun 	if (sndcmsgbuf == NULL) {
   1327   1.6  itojun 		syslog(LOG_ERR, "<%s> not enough core", __FUNCTION__);
   1328   1.6  itojun 		exit(1);
   1329   1.6  itojun 	}
   1330   1.1  itojun 
   1331   1.1  itojun 	if ((sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
   1332   1.1  itojun 		syslog(LOG_ERR, "<%s> socket: %s", __FUNCTION__,
   1333   1.1  itojun 		       strerror(errno));
   1334   1.1  itojun 		exit(1);
   1335   1.1  itojun 	}
   1336   1.1  itojun 
   1337   1.1  itojun 	/* specify to tell receiving interface */
   1338   1.1  itojun 	on = 1;
   1339   1.5  itojun #ifdef IPV6_RECVPKTINFO
   1340   1.5  itojun 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
   1341   1.5  itojun 		       sizeof(on)) < 0) {
   1342   1.5  itojun 		syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s",
   1343   1.5  itojun 		       __FUNCTION__, strerror(errno));
   1344   1.5  itojun 		exit(1);
   1345   1.5  itojun 	}
   1346   1.5  itojun #else  /* old adv. API */
   1347   1.1  itojun 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &on,
   1348   1.1  itojun 		       sizeof(on)) < 0) {
   1349   1.1  itojun 		syslog(LOG_ERR, "<%s> IPV6_PKTINFO: %s",
   1350   1.1  itojun 		       __FUNCTION__, strerror(errno));
   1351   1.1  itojun 		exit(1);
   1352   1.1  itojun 	}
   1353   1.5  itojun #endif
   1354   1.1  itojun 
   1355   1.1  itojun 	on = 1;
   1356   1.1  itojun 	/* specify to tell value of hoplimit field of received IP6 hdr */
   1357   1.5  itojun #ifdef IPV6_RECVHOPLIMIT
   1358   1.5  itojun 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
   1359   1.5  itojun 		       sizeof(on)) < 0) {
   1360   1.5  itojun 		syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s",
   1361   1.5  itojun 		       __FUNCTION__, strerror(errno));
   1362   1.5  itojun 		exit(1);
   1363   1.5  itojun 	}
   1364   1.5  itojun #else  /* old adv. API */
   1365   1.1  itojun 	if (setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &on,
   1366   1.1  itojun 		       sizeof(on)) < 0) {
   1367   1.1  itojun 		syslog(LOG_ERR, "<%s> IPV6_HOPLIMIT: %s",
   1368   1.1  itojun 		       __FUNCTION__, strerror(errno));
   1369   1.1  itojun 		exit(1);
   1370   1.1  itojun 	}
   1371  1.11  itojun #endif
   1372   1.1  itojun 
   1373   1.1  itojun 	ICMP6_FILTER_SETBLOCKALL(&filt);
   1374   1.1  itojun 	ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
   1375   1.1  itojun 	ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
   1376   1.1  itojun 	if (accept_rr)
   1377   1.1  itojun 		ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
   1378   1.1  itojun 	if (setsockopt(sock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
   1379   1.1  itojun 		       sizeof(filt)) < 0) {
   1380   1.1  itojun 		syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
   1381   1.1  itojun 		       __FUNCTION__, strerror(errno));
   1382   1.1  itojun 		exit(1);
   1383   1.1  itojun 	}
   1384   1.1  itojun 
   1385   1.1  itojun 	/*
   1386   1.1  itojun 	 * join all routers multicast address on each advertising interface.
   1387   1.1  itojun 	 */
   1388  1.11  itojun 	if (inet_pton(AF_INET6, ALLROUTERS_LINK,
   1389  1.11  itojun 		      &mreq.ipv6mr_multiaddr.s6_addr)
   1390   1.1  itojun 	    != 1) {
   1391   1.1  itojun 		syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
   1392   1.1  itojun 		       __FUNCTION__);
   1393   1.1  itojun 		exit(1);
   1394   1.1  itojun 	}
   1395   1.1  itojun 	while(ra) {
   1396   1.1  itojun 		mreq.ipv6mr_interface = ra->ifindex;
   1397  1.11  itojun 		if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq,
   1398   1.1  itojun 			       sizeof(mreq)) < 0) {
   1399  1.11  itojun 			syslog(LOG_ERR, "<%s> IPV6_JOIN_GROUP(link) on %s: %s",
   1400   1.1  itojun 			       __FUNCTION__, ra->ifname, strerror(errno));
   1401   1.1  itojun 			exit(1);
   1402   1.1  itojun 		}
   1403   1.1  itojun 		ra = ra->next;
   1404   1.1  itojun 	}
   1405  1.11  itojun 
   1406  1.11  itojun 	/*
   1407  1.11  itojun 	 * When attending router renumbering, join all-routers site-local
   1408  1.11  itojun 	 * multicast group.
   1409  1.11  itojun 	 */
   1410  1.11  itojun 	if (accept_rr) {
   1411  1.11  itojun 		if (inet_pton(AF_INET6, ALLROUTERS_SITE,
   1412  1.11  itojun 			      &in6a_site_allrouters) != 1) {
   1413  1.11  itojun 			syslog(LOG_ERR, "<%s> inet_pton failed(library bug?)",
   1414  1.11  itojun 			       __FUNCTION__);
   1415  1.11  itojun 			exit(1);
   1416  1.11  itojun 		}
   1417  1.11  itojun 		mreq.ipv6mr_multiaddr = in6a_site_allrouters;
   1418  1.11  itojun 		if (mcastif) {
   1419  1.11  itojun 			if ((mreq.ipv6mr_interface = if_nametoindex(mcastif))
   1420  1.11  itojun 			    == 0) {
   1421  1.11  itojun 				syslog(LOG_ERR,
   1422  1.11  itojun 				       "<%s> invalid interface: %s",
   1423  1.11  itojun 				       __FUNCTION__, mcastif);
   1424  1.11  itojun 				exit(1);
   1425  1.11  itojun 			}
   1426  1.11  itojun 		} else
   1427  1.11  itojun 			mreq.ipv6mr_interface = ralist->ifindex;
   1428  1.11  itojun 		if (setsockopt(sock, IPPROTO_IPV6, IPV6_JOIN_GROUP,
   1429  1.11  itojun 			       &mreq, sizeof(mreq)) < 0) {
   1430  1.11  itojun 			syslog(LOG_ERR,
   1431  1.11  itojun 			       "<%s> IPV6_JOIN_GROUP(site) on %s: %s",
   1432  1.11  itojun 			       __FUNCTION__,
   1433  1.11  itojun 			       mcastif ? mcastif : ralist->ifname,
   1434  1.11  itojun 			       strerror(errno));
   1435  1.11  itojun 			exit(1);
   1436  1.11  itojun 		}
   1437  1.11  itojun 	}
   1438   1.1  itojun 
   1439   1.1  itojun 	/* initialize msghdr for receiving packets */
   1440   1.1  itojun 	rcviov[0].iov_base = (caddr_t)answer;
   1441   1.1  itojun 	rcviov[0].iov_len = sizeof(answer);
   1442   1.1  itojun 	rcvmhdr.msg_name = (caddr_t)&from;
   1443   1.1  itojun 	rcvmhdr.msg_namelen = sizeof(from);
   1444   1.1  itojun 	rcvmhdr.msg_iov = rcviov;
   1445   1.1  itojun 	rcvmhdr.msg_iovlen = 1;
   1446   1.1  itojun 	rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
   1447   1.6  itojun 	rcvmhdr.msg_controllen = rcvcmsgbuflen;
   1448   1.1  itojun 
   1449   1.1  itojun 	/* initialize msghdr for sending packets */
   1450   1.1  itojun 	sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
   1451   1.1  itojun 	sndmhdr.msg_iov = sndiov;
   1452   1.1  itojun 	sndmhdr.msg_iovlen = 1;
   1453   1.1  itojun 	sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
   1454   1.6  itojun 	sndmhdr.msg_controllen = sndcmsgbuflen;
   1455   1.1  itojun 
   1456   1.1  itojun 	return;
   1457   1.1  itojun }
   1458   1.1  itojun 
   1459   1.1  itojun /* open a routing socket to watch the routing table */
   1460   1.1  itojun static void
   1461   1.1  itojun rtsock_open()
   1462   1.1  itojun {
   1463   1.1  itojun 	if ((rtsock = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
   1464   1.1  itojun 		syslog(LOG_ERR,
   1465   1.1  itojun 		       "<%s> socket: %s", __FUNCTION__, strerror(errno));
   1466   1.1  itojun 		exit(1);
   1467   1.1  itojun 	}
   1468   1.1  itojun }
   1469   1.1  itojun 
   1470  1.11  itojun struct rainfo *
   1471   1.1  itojun if_indextorainfo(int index)
   1472   1.1  itojun {
   1473   1.1  itojun 	struct rainfo *rai = ralist;
   1474   1.1  itojun 
   1475   1.1  itojun 	for (rai = ralist; rai; rai = rai->next) {
   1476   1.1  itojun 		if (rai->ifindex == index)
   1477   1.1  itojun 			return(rai);
   1478   1.1  itojun 	}
   1479   1.1  itojun 
   1480   1.1  itojun 	return(NULL);		/* search failed */
   1481   1.1  itojun }
   1482   1.1  itojun 
   1483   1.1  itojun static void
   1484   1.1  itojun ra_output(rainfo)
   1485   1.1  itojun struct rainfo *rainfo;
   1486   1.1  itojun {
   1487   1.1  itojun 	int i;
   1488   1.1  itojun 	struct cmsghdr *cm;
   1489   1.1  itojun 	struct in6_pktinfo *pi;
   1490  1.10  itojun 	struct soliciter *sol, *nextsol;
   1491  1.10  itojun 
   1492  1.10  itojun 	if ((iflist[rainfo->ifindex]->ifm_flags & IFF_UP) == 0) {
   1493  1.10  itojun 		syslog(LOG_DEBUG, "<%s> %s is not up, skip sending RA",
   1494  1.10  itojun 		       __FUNCTION__, rainfo->ifname);
   1495  1.10  itojun 		return;
   1496  1.10  itojun 	}
   1497   1.1  itojun 
   1498  1.11  itojun 	make_packet(rainfo);	/* XXX: inefficient */
   1499  1.11  itojun 
   1500   1.1  itojun 	sndmhdr.msg_name = (caddr_t)&sin6_allnodes;
   1501   1.1  itojun 	sndmhdr.msg_iov[0].iov_base = (caddr_t)rainfo->ra_data;
   1502   1.1  itojun 	sndmhdr.msg_iov[0].iov_len = rainfo->ra_datalen;
   1503   1.1  itojun 
   1504   1.1  itojun 	cm = CMSG_FIRSTHDR(&sndmhdr);
   1505   1.1  itojun 	/* specify the outgoing interface */
   1506   1.1  itojun 	cm->cmsg_level = IPPROTO_IPV6;
   1507   1.1  itojun 	cm->cmsg_type = IPV6_PKTINFO;
   1508   1.1  itojun 	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
   1509   1.1  itojun 	pi = (struct in6_pktinfo *)CMSG_DATA(cm);
   1510   1.1  itojun 	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));	/*XXX*/
   1511   1.1  itojun 	pi->ipi6_ifindex = rainfo->ifindex;
   1512   1.1  itojun 
   1513   1.1  itojun 	/* specify the hop limit of the packet */
   1514   1.1  itojun 	{
   1515   1.1  itojun 		int hoplimit = 255;
   1516   1.1  itojun 
   1517   1.1  itojun 		cm = CMSG_NXTHDR(&sndmhdr, cm);
   1518   1.1  itojun 		cm->cmsg_level = IPPROTO_IPV6;
   1519   1.1  itojun 		cm->cmsg_type = IPV6_HOPLIMIT;
   1520   1.1  itojun 		cm->cmsg_len = CMSG_LEN(sizeof(int));
   1521   1.1  itojun 		memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
   1522   1.1  itojun 	}
   1523   1.1  itojun 
   1524   1.1  itojun 	syslog(LOG_DEBUG,
   1525   1.1  itojun 	       "<%s> send RA on %s, # of waitings = %d",
   1526   1.1  itojun 	       __FUNCTION__, rainfo->ifname, rainfo->waiting);
   1527   1.1  itojun 
   1528   1.1  itojun 	i = sendmsg(sock, &sndmhdr, 0);
   1529   1.1  itojun 
   1530   1.1  itojun 	if (i < 0 || i != rainfo->ra_datalen)  {
   1531   1.1  itojun 		if (i < 0) {
   1532   1.4  itojun 			syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
   1533   1.4  itojun 			       __FUNCTION__, rainfo->ifname,
   1534   1.4  itojun 			       strerror(errno));
   1535   1.1  itojun 		}
   1536   1.1  itojun 	}
   1537   1.1  itojun 
   1538  1.10  itojun 	/*
   1539  1.10  itojun 	 * unicast advertisements
   1540  1.10  itojun 	 * XXX commented out.  reason: though spec does not forbit it, unicast
   1541  1.10  itojun 	 * advert does not really help
   1542  1.10  itojun 	 */
   1543  1.10  itojun 	for (sol = rainfo->soliciter; sol; sol = nextsol) {
   1544  1.10  itojun 		nextsol = sol->next;
   1545  1.10  itojun 
   1546  1.10  itojun #if 0
   1547  1.10  itojun 		sndmhdr.msg_name = (caddr_t)&sol->addr;
   1548  1.10  itojun 		i = sendmsg(sock, &sndmhdr, 0);
   1549  1.10  itojun 		if (i < 0 || i != rainfo->ra_datalen)  {
   1550  1.10  itojun 			if (i < 0) {
   1551  1.10  itojun 				syslog(LOG_ERR,
   1552  1.10  itojun 				    "<%s> unicast sendmsg on %s: %s",
   1553  1.10  itojun 				    __FUNCTION__, rainfo->ifname,
   1554  1.10  itojun 				    strerror(errno));
   1555  1.10  itojun 			}
   1556  1.10  itojun 		}
   1557  1.10  itojun #endif
   1558  1.10  itojun 
   1559  1.10  itojun 		sol->next = NULL;
   1560  1.10  itojun 		free(sol);
   1561  1.10  itojun 	}
   1562  1.10  itojun 	rainfo->soliciter = NULL;
   1563  1.10  itojun 
   1564   1.1  itojun 	/* update counter */
   1565   1.1  itojun 	if (rainfo->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS)
   1566   1.1  itojun 		rainfo->initcounter++;
   1567   1.9  itojun 	rainfo->raoutput++;
   1568   1.1  itojun 
   1569   1.1  itojun 	/* update timestamp */
   1570   1.1  itojun 	gettimeofday(&rainfo->lastsent, NULL);
   1571   1.1  itojun 
   1572   1.1  itojun 	/* reset waiting conter */
   1573   1.1  itojun 	rainfo->waiting = 0;
   1574   1.1  itojun }
   1575   1.1  itojun 
   1576   1.1  itojun /* process RA timer */
   1577   1.1  itojun void
   1578   1.1  itojun ra_timeout(void *data)
   1579   1.1  itojun {
   1580   1.1  itojun 	struct rainfo *rai = (struct rainfo *)data;
   1581   1.1  itojun 
   1582   1.1  itojun #ifdef notyet
   1583   1.1  itojun 	/* if necessary, reconstruct the packet. */
   1584   1.1  itojun #endif
   1585   1.1  itojun 
   1586   1.1  itojun 	syslog(LOG_DEBUG,
   1587   1.1  itojun 	       "<%s> RA timer on %s is expired",
   1588   1.1  itojun 	       __FUNCTION__, rai->ifname);
   1589   1.1  itojun 
   1590  1.10  itojun 	ra_output(rai);
   1591   1.1  itojun }
   1592   1.1  itojun 
   1593   1.1  itojun /* update RA timer */
   1594   1.1  itojun void
   1595   1.1  itojun ra_timer_update(void *data, struct timeval *tm)
   1596   1.1  itojun {
   1597   1.1  itojun 	struct rainfo *rai = (struct rainfo *)data;
   1598   1.1  itojun 	long interval;
   1599   1.1  itojun 
   1600   1.1  itojun 	/*
   1601   1.1  itojun 	 * Whenever a multicast advertisement is sent from an interface,
   1602   1.1  itojun 	 * the timer is reset to a uniformly-distributed random value
   1603   1.1  itojun 	 * between the interface's configured MinRtrAdvInterval and
   1604  1.11  itojun 	 * MaxRtrAdvInterval (RFC2461 6.2.4).
   1605   1.1  itojun 	 */
   1606   1.1  itojun 	interval = rai->mininterval;
   1607   1.1  itojun 	interval += random() % (rai->maxinterval - rai->mininterval);
   1608   1.1  itojun 
   1609   1.1  itojun 	/*
   1610   1.1  itojun 	 * For the first few advertisements (up to
   1611   1.1  itojun 	 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen interval
   1612   1.1  itojun 	 * is greater than MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer
   1613   1.1  itojun 	 * SHOULD be set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.
   1614   1.1  itojun 	 * (RFC-2461 6.2.4)
   1615   1.1  itojun 	 */
   1616   1.1  itojun 	if (rai->initcounter < MAX_INITIAL_RTR_ADVERTISEMENTS &&
   1617   1.1  itojun 	    interval > MAX_INITIAL_RTR_ADVERT_INTERVAL)
   1618   1.1  itojun 		interval = MAX_INITIAL_RTR_ADVERT_INTERVAL;
   1619   1.1  itojun 
   1620   1.1  itojun 	tm->tv_sec = interval;
   1621   1.1  itojun 	tm->tv_usec = 0;
   1622   1.1  itojun 
   1623   1.1  itojun 	syslog(LOG_DEBUG,
   1624   1.1  itojun 	       "<%s> RA timer on %s is set to %ld:%ld",
   1625   1.8  kleink 	       __FUNCTION__, rai->ifname,
   1626   1.8  kleink 	       (long int)tm->tv_sec, (long int)tm->tv_usec);
   1627   1.1  itojun 
   1628   1.1  itojun 	return;
   1629   1.1  itojun }
   1630