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