xref: /freebsd/usr.sbin/rtadvd/rtadvd.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*	$KAME: rtadvd.c,v 1.82 2003/08/05 12:34:23 itojun Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7  * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the project nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/socket.h>
38 #include <sys/uio.h>
39 #include <sys/queue.h>
40 #include <sys/stat.h>
41 #include <sys/sysctl.h>
42 
43 #include <net/if.h>
44 #include <net/if_types.h>
45 #include <net/if_media.h>
46 #include <net/if_dl.h>
47 #include <net/route.h>
48 #include <netinet/in.h>
49 #include <netinet/ip6.h>
50 #include <netinet6/ip6_var.h>
51 #include <netinet/icmp6.h>
52 
53 #include <arpa/inet.h>
54 
55 #include <netinet/in_var.h>
56 #include <netinet6/nd6.h>
57 
58 #include <time.h>
59 #include <unistd.h>
60 #include <stdio.h>
61 #include <err.h>
62 #include <errno.h>
63 #include <inttypes.h>
64 #include <libutil.h>
65 #include <netdb.h>
66 #include <signal.h>
67 #include <string.h>
68 #include <stdlib.h>
69 #include <syslog.h>
70 #include <poll.h>
71 
72 #include "pathnames.h"
73 #include "rtadvd.h"
74 #include "if.h"
75 #include "rrenum.h"
76 #include "advcap.h"
77 #include "timer_subr.h"
78 #include "timer.h"
79 #include "config.h"
80 #include "control.h"
81 #include "control_server.h"
82 
83 #define RTADV_TYPE2BITMASK(type) (0x1 << type)
84 
85 struct msghdr rcvmhdr;
86 static char *rcvcmsgbuf;
87 static size_t rcvcmsgbuflen;
88 static char *sndcmsgbuf = NULL;
89 static size_t sndcmsgbuflen;
90 struct msghdr sndmhdr;
91 struct iovec rcviov[2];
92 struct iovec sndiov[2];
93 struct sockaddr_in6 rcvfrom;
94 static const char *pidfilename = _PATH_RTADVDPID;
95 const char *conffile = _PATH_RTADVDCONF;
96 static struct pidfh *pfh;
97 static int dflag, sflag;
98 static int wait_shutdown;
99 
100 #define	PFD_RAWSOCK	0
101 #define	PFD_RTSOCK	1
102 #define	PFD_CSOCK	2
103 #define	PFD_MAX		3
104 
105 struct railist_head_t railist =
106     TAILQ_HEAD_INITIALIZER(railist);
107 struct ifilist_head_t ifilist =
108     TAILQ_HEAD_INITIALIZER(ifilist);
109 
110 struct nd_optlist {
111 	TAILQ_ENTRY(nd_optlist)	nol_next;
112 	struct nd_opt_hdr *nol_opt;
113 };
114 union nd_opt {
115 	struct nd_opt_hdr *opt_array[9];
116 	struct {
117 		struct nd_opt_hdr *zero;
118 		struct nd_opt_hdr *src_lladdr;
119 		struct nd_opt_hdr *tgt_lladdr;
120 		struct nd_opt_prefix_info *pi;
121 		struct nd_opt_rd_hdr *rh;
122 		struct nd_opt_mtu *mtu;
123 		TAILQ_HEAD(, nd_optlist) opt_list;
124 	} nd_opt_each;
125 };
126 #define opt_src_lladdr	nd_opt_each.src_lladdr
127 #define opt_tgt_lladdr	nd_opt_each.tgt_lladdr
128 #define opt_pi		nd_opt_each.pi
129 #define opt_rh		nd_opt_each.rh
130 #define opt_mtu		nd_opt_each.mtu
131 #define opt_list	nd_opt_each.opt_list
132 
133 #define NDOPT_FLAG_SRCLINKADDR	(1 << 0)
134 #define NDOPT_FLAG_TGTLINKADDR	(1 << 1)
135 #define NDOPT_FLAG_PREFIXINFO	(1 << 2)
136 #define NDOPT_FLAG_RDHDR	(1 << 3)
137 #define NDOPT_FLAG_MTU		(1 << 4)
138 #define NDOPT_FLAG_RDNSS	(1 << 5)
139 #define NDOPT_FLAG_DNSSL	(1 << 6)
140 
141 static uint32_t ndopt_flags[] = {
142 	[ND_OPT_SOURCE_LINKADDR]	= NDOPT_FLAG_SRCLINKADDR,
143 	[ND_OPT_TARGET_LINKADDR]	= NDOPT_FLAG_TGTLINKADDR,
144 	[ND_OPT_PREFIX_INFORMATION]	= NDOPT_FLAG_PREFIXINFO,
145 	[ND_OPT_REDIRECTED_HEADER]	= NDOPT_FLAG_RDHDR,
146 	[ND_OPT_MTU]			= NDOPT_FLAG_MTU,
147 	[ND_OPT_RDNSS]			= NDOPT_FLAG_RDNSS,
148 	[ND_OPT_DNSSL]			= NDOPT_FLAG_DNSSL,
149 };
150 
151 static void	rtadvd_shutdown(void);
152 static void	sock_open(struct sockinfo *);
153 static void	rtsock_open(struct sockinfo *);
154 static void	rtadvd_input(struct sockinfo *);
155 static void	rs_input(int, struct nd_router_solicit *,
156 		    struct in6_pktinfo *, struct sockaddr_in6 *);
157 static void	ra_input(int, struct nd_router_advert *,
158 		    struct in6_pktinfo *, struct sockaddr_in6 *);
159 static int	prefix_check(struct nd_opt_prefix_info *, struct rainfo *,
160 		    struct sockaddr_in6 *);
161 static int	nd6_options(struct nd_opt_hdr *, int,
162 		    union nd_opt *, uint32_t);
163 static void	free_ndopts(union nd_opt *);
164 static void	rtmsg_input(struct sockinfo *);
165 static void	set_short_delay(struct ifinfo *);
166 static int	check_accept_rtadv(int);
167 
168 static void
169 usage(void)
170 {
171 
172 	fprintf(stderr, "usage: rtadvd [-dDfRs] "
173 	    "[-c configfile] [-C ctlsock] [-M ifname] [-p pidfile]\n");
174 	exit(1);
175 }
176 
177 int
178 main(int argc, char *argv[])
179 {
180 	struct pollfd set[PFD_MAX];
181 	struct timespec *timeout;
182 	int i, ch;
183 	int fflag = 0, logopt;
184 	int error;
185 	pid_t pid, otherpid;
186 
187 	/* get command line options and arguments */
188 	while ((ch = getopt(argc, argv, "c:C:dDfhM:p:Rs")) != -1) {
189 		switch (ch) {
190 		case 'c':
191 			conffile = optarg;
192 			break;
193 		case 'C':
194 			ctrlsock.si_name = optarg;
195 			break;
196 		case 'd':
197 			dflag++;
198 			break;
199 		case 'D':
200 			dflag += 3;
201 			break;
202 		case 'f':
203 			fflag = 1;
204 			break;
205 		case 'M':
206 			mcastif = optarg;
207 			break;
208 		case 'R':
209 			fprintf(stderr, "rtadvd: "
210 				"the -R option is currently ignored.\n");
211 			/* accept_rr = 1; */
212 			/* run anyway... */
213 			break;
214 		case 's':
215 			sflag = 1;
216 			break;
217 		case 'p':
218 			pidfilename = optarg;
219 			break;
220 		default:
221 			usage();
222 		}
223 	}
224 	argc -= optind;
225 	argv += optind;
226 
227 	logopt = LOG_NDELAY | LOG_PID;
228 	if (fflag)
229 		logopt |= LOG_PERROR;
230 	openlog("rtadvd", logopt, LOG_DAEMON);
231 
232 	/* set log level */
233 	if (dflag > 2)
234 		(void)setlogmask(LOG_UPTO(LOG_DEBUG));
235 	else if (dflag > 1)
236 		(void)setlogmask(LOG_UPTO(LOG_INFO));
237 	else if (dflag > 0)
238 		(void)setlogmask(LOG_UPTO(LOG_NOTICE));
239 	else
240 		(void)setlogmask(LOG_UPTO(LOG_ERR));
241 
242 	/* timer initialization */
243 	rtadvd_timer_init();
244 
245 	pfh = pidfile_open(pidfilename, 0600, &otherpid);
246 	if (pfh == NULL) {
247 		if (errno == EEXIST)
248 			errx(1, "%s already running, pid: %d",
249 			    getprogname(), otherpid);
250 		syslog(LOG_ERR,
251 		    "failed to open the pid file %s, run anyway.",
252 		    pidfilename);
253 	}
254 	if (!fflag)
255 		daemon(1, 0);
256 
257 	sock_open(&sock);
258 
259 	update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
260 	for (i = 0; i < argc; i++)
261 		update_persist_ifinfo(&ifilist, argv[i]);
262 
263 	csock_open(&ctrlsock, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
264 	if (ctrlsock.si_fd == -1) {
265 		syslog(LOG_ERR, "cannot open control socket: %s",
266 		    strerror(errno));
267 		exit(1);
268 	}
269 
270 	/* record the current PID */
271 	pid = getpid();
272 	pidfile_write(pfh);
273 
274 	set[PFD_RAWSOCK].fd = sock.si_fd;
275 	set[PFD_RAWSOCK].events = POLLIN;
276 	if (sflag == 0) {
277 		rtsock_open(&rtsock);
278 		set[PFD_RTSOCK].fd = rtsock.si_fd;
279 		set[PFD_RTSOCK].events = POLLIN;
280 	} else
281 		set[PFD_RTSOCK].fd = -1;
282 	set[PFD_CSOCK].fd = ctrlsock.si_fd;
283 	set[PFD_CSOCK].events = POLLIN;
284 	signal(SIGTERM, set_do_shutdown);
285 	signal(SIGINT, set_do_shutdown);
286 	signal(SIGHUP, set_do_reload);
287 
288 	error = csock_listen(&ctrlsock);
289 	if (error) {
290 		syslog(LOG_ERR, "cannot listen control socket: %s",
291 		    strerror(errno));
292 		exit(1);
293 	}
294 
295 	/* load configuration file */
296 	set_do_reload(0);
297 
298 	while (1) {
299 		if (is_do_shutdown())
300 			rtadvd_shutdown();
301 
302 		if (is_do_reload()) {
303 			loadconfig_ifname(reload_ifname());
304 			if (reload_ifname() == NULL)
305 				syslog(LOG_INFO,
306 				    "configuration file reloaded.");
307 			else
308 				syslog(LOG_INFO,
309 				    "configuration file for %s reloaded.",
310 				    reload_ifname());
311 			reset_do_reload();
312 		}
313 
314 		/* timeout handler update for active interfaces */
315 		rtadvd_update_timeout_handler();
316 
317 		/* timer expiration check and reset the timer */
318 		timeout = rtadvd_check_timer();
319 
320 		if (timeout != NULL) {
321 			syslog(LOG_DEBUG,
322 			    "<%s> set timer to %ld:%ld. waiting for "
323 			    "inputs or timeout", __func__,
324 			    (long int)timeout->tv_sec,
325 			    (long int)timeout->tv_nsec / 1000);
326 		} else {
327 			syslog(LOG_DEBUG,
328 			    "<%s> there's no timer. waiting for inputs",
329 			    __func__);
330 		}
331 		if ((i = poll(set, sizeof(set)/sizeof(set[0]),
332 			    timeout ? (timeout->tv_sec * 1000 +
333 				timeout->tv_nsec / 1000 / 1000) : INFTIM)) < 0) {
334 
335 			/* EINTR would occur if a signal was delivered */
336 			if (errno != EINTR)
337 				syslog(LOG_ERR, "poll() failed: %s",
338 				    strerror(errno));
339 			continue;
340 		}
341 		if (i == 0)	/* timeout */
342 			continue;
343 		if (rtsock.si_fd != -1 && set[PFD_RTSOCK].revents & POLLIN)
344 			rtmsg_input(&rtsock);
345 
346 		if (set[PFD_RAWSOCK].revents & POLLIN)
347 			rtadvd_input(&sock);
348 
349 		if (set[PFD_CSOCK].revents & POLLIN) {
350 			int fd;
351 
352 			fd = csock_accept(&ctrlsock);
353 			if (fd == -1)
354 				syslog(LOG_ERR,
355 				    "cannot accept() control socket: %s",
356 				    strerror(errno));
357 			else {
358 				cm_handler_server(fd);
359 				close(fd);
360 			}
361 		}
362 	}
363 	exit(0);		/* NOTREACHED */
364 }
365 
366 static void
367 rtadvd_shutdown(void)
368 {
369 	struct ifinfo *ifi;
370 	struct rainfo *rai;
371 	struct rdnss *rdn;
372 	struct dnssl *dns;
373 
374 	if (wait_shutdown) {
375 		syslog(LOG_INFO,
376 		    "waiting expiration of the all RA timers.");
377 
378 		TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
379 			/*
380 			 * Ignore !IFF_UP interfaces in waiting for shutdown.
381 			 */
382 			if (!(ifi->ifi_flags & IFF_UP) &&
383 			    ifi->ifi_ra_timer != NULL) {
384 				ifi->ifi_state = IFI_STATE_UNCONFIGURED;
385 				rtadvd_remove_timer(ifi->ifi_ra_timer);
386 				ifi->ifi_ra_timer = NULL;
387 				syslog(LOG_DEBUG, "<%s> %s(idx=%d) is down. "
388 				    "Timer removed and marked as UNCONFIGURED.",
389 				     __func__, ifi->ifi_ifname,
390 				    ifi->ifi_ifindex);
391 			}
392 		}
393 		TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
394 			if (ifi->ifi_ra_timer != NULL)
395 				break;
396 		}
397 		if (ifi == NULL) {
398 			syslog(LOG_NOTICE, "gracefully terminated.");
399 			exit(0);
400 		}
401 
402 		sleep(1);
403 		return;
404 	}
405 
406 	syslog(LOG_DEBUG, "<%s> cease to be an advertising router",
407 	    __func__);
408 
409 	wait_shutdown = 1;
410 
411 	TAILQ_FOREACH(rai, &railist, rai_next) {
412 		rai->rai_lifetime = 0;
413 		TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next)
414 			rdn->rd_ltime = 0;
415 		TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next)
416 			dns->dn_ltime = 0;
417 	}
418 	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
419 		if (!ifi->ifi_persist)
420 			continue;
421 		if (ifi->ifi_state == IFI_STATE_UNCONFIGURED)
422 			continue;
423 		if (ifi->ifi_ra_timer == NULL)
424 			continue;
425 		if (ifi->ifi_ra_lastsent.tv_sec == 0 &&
426 		    ifi->ifi_ra_lastsent.tv_nsec == 0 &&
427 		    ifi->ifi_ra_timer != NULL) {
428 			/*
429 			 * When RA configured but never sent,
430 			 * ignore the IF immediately.
431 			 */
432 			rtadvd_remove_timer(ifi->ifi_ra_timer);
433 			ifi->ifi_ra_timer = NULL;
434 			ifi->ifi_state = IFI_STATE_UNCONFIGURED;
435 			continue;
436 		}
437 
438 		ifi->ifi_state = IFI_STATE_TRANSITIVE;
439 
440 		/* Mark as the shut-down state. */
441 		ifi->ifi_rainfo_trans = ifi->ifi_rainfo;
442 		ifi->ifi_rainfo = NULL;
443 
444 		ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
445 		ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
446 
447 		ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
448 		rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
449 		    ifi->ifi_ra_timer);
450 	}
451 	syslog(LOG_NOTICE, "final RA transmission started.");
452 
453 	pidfile_remove(pfh);
454 	csock_close(&ctrlsock);
455 }
456 
457 static void
458 rtmsg_input(struct sockinfo *s)
459 {
460 	int n, type, ifindex = 0, plen;
461 	size_t len;
462 	char msg[2048], *next, *lim;
463 	char ifname[IFNAMSIZ];
464 	struct if_announcemsghdr *ifan;
465 	struct rt_msghdr *rtm;
466 	struct prefix *pfx;
467 	struct rainfo *rai;
468 	struct in6_addr *addr;
469 	struct ifinfo *ifi;
470 	char addrbuf[INET6_ADDRSTRLEN];
471 	int prefixchange = 0;
472 
473 	if (s == NULL) {
474 		syslog(LOG_ERR, "<%s> internal error", __func__);
475 		exit(1);
476 	}
477 	n = read(s->si_fd, msg, sizeof(msg));
478 	rtm = (struct rt_msghdr *)msg;
479 	syslog(LOG_DEBUG, "<%s> received a routing message "
480 	    "(type = %d, len = %d)", __func__, rtm->rtm_type, n);
481 
482 	if (n > rtm->rtm_msglen) {
483 		/*
484 		 * This usually won't happen for messages received on
485 		 * a routing socket.
486 		 */
487 		syslog(LOG_DEBUG,
488 		    "<%s> received data length is larger than "
489 		    "1st routing message len. multiple messages? "
490 		    "read %d bytes, but 1st msg len = %d",
491 		    __func__, n, rtm->rtm_msglen);
492 #if 0
493 		/* adjust length */
494 		n = rtm->rtm_msglen;
495 #endif
496 	}
497 
498 	lim = msg + n;
499 	for (next = msg; next < lim; next += len) {
500 		int oldifflags;
501 
502 		next = get_next_msg(next, lim, 0, &len,
503 		    RTADV_TYPE2BITMASK(RTM_ADD) |
504 		    RTADV_TYPE2BITMASK(RTM_DELETE) |
505 		    RTADV_TYPE2BITMASK(RTM_NEWADDR) |
506 		    RTADV_TYPE2BITMASK(RTM_DELADDR) |
507 		    RTADV_TYPE2BITMASK(RTM_IFINFO) |
508 		    RTADV_TYPE2BITMASK(RTM_IFANNOUNCE));
509 		if (len == 0)
510 			break;
511 		type = ((struct rt_msghdr *)next)->rtm_type;
512 		switch (type) {
513 		case RTM_ADD:
514 		case RTM_DELETE:
515 			ifindex = get_rtm_ifindex(next);
516 			break;
517 		case RTM_NEWADDR:
518 		case RTM_DELADDR:
519 			ifindex = (int)((struct ifa_msghdr *)next)->ifam_index;
520 			break;
521 		case RTM_IFINFO:
522 			ifindex = (int)((struct if_msghdr *)next)->ifm_index;
523 			break;
524 		case RTM_IFANNOUNCE:
525 			ifan = (struct if_announcemsghdr *)next;
526 			switch (ifan->ifan_what) {
527 			case IFAN_ARRIVAL:
528 			case IFAN_DEPARTURE:
529 				break;
530 			default:
531 				syslog(LOG_DEBUG,
532 				    "<%s:%d> unknown ifan msg (ifan_what=%d)",
533 				   __func__, __LINE__, ifan->ifan_what);
534 				continue;
535 			}
536 
537 			syslog(LOG_DEBUG, "<%s>: if_announcemsg (idx=%d:%d)",
538 			       __func__, ifan->ifan_index, ifan->ifan_what);
539 			switch (ifan->ifan_what) {
540 			case IFAN_ARRIVAL:
541 				syslog(LOG_NOTICE,
542 				    "interface added (idx=%d)",
543 				    ifan->ifan_index);
544 				update_ifinfo(&ifilist, ifan->ifan_index);
545 				loadconfig_index(ifan->ifan_index);
546 				break;
547 			case IFAN_DEPARTURE:
548 				syslog(LOG_NOTICE,
549 				    "interface removed (idx=%d)",
550 				    ifan->ifan_index);
551 				rm_ifinfo_index(ifan->ifan_index);
552 
553 				/* Clear ifi_ifindex */
554 				TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
555 					if (ifi->ifi_ifindex
556 					    == ifan->ifan_index) {
557 						ifi->ifi_ifindex = 0;
558 						break;
559 					}
560 				}
561 				update_ifinfo(&ifilist, ifan->ifan_index);
562 				break;
563 			}
564 			continue;
565 		default:
566 			/* should not reach here */
567 			syslog(LOG_DEBUG,
568 			       "<%s:%d> unknown rtmsg %d on %s",
569 			       __func__, __LINE__, type,
570 			       if_indextoname(ifindex, ifname));
571 			continue;
572 		}
573 		ifi = if_indextoifinfo(ifindex);
574 		if (ifi == NULL) {
575 			syslog(LOG_DEBUG,
576 			    "<%s> ifinfo not found for idx=%d.  Why?",
577 			    __func__, ifindex);
578 			continue;
579 		}
580 		rai = ifi->ifi_rainfo;
581 		if (rai == NULL) {
582 			syslog(LOG_DEBUG,
583 			    "<%s> route changed on "
584 			    "non advertising interface(%s)",
585 			    __func__, ifi->ifi_ifname);
586 			continue;
587 		}
588 
589 		oldifflags = ifi->ifi_flags;
590 		/* init ifflags because it may have changed */
591 		update_ifinfo(&ifilist, ifindex);
592 
593 		switch (type) {
594 		case RTM_ADD:
595 			if (sflag)
596 				break;	/* we aren't interested in prefixes  */
597 
598 			addr = get_addr(msg);
599 			plen = get_prefixlen(msg);
600 			/* sanity check for plen */
601 			/* as RFC2373, prefixlen is at least 4 */
602 			if (plen < 4 || plen > 127) {
603 				syslog(LOG_INFO, "<%s> new interface route's"
604 				    "plen %d is invalid for a prefix",
605 				    __func__, plen);
606 				break;
607 			}
608 			pfx = find_prefix(rai, addr, plen);
609 			if (pfx) {
610 				if (pfx->pfx_timer) {
611 					/*
612 					 * If the prefix has been invalidated,
613 					 * make it available again.
614 					 */
615 					update_prefix(pfx);
616 					prefixchange = 1;
617 				} else
618 					syslog(LOG_DEBUG,
619 					    "<%s> new prefix(%s/%d) "
620 					    "added on %s, "
621 					    "but it was already in list",
622 					    __func__,
623 					    inet_ntop(AF_INET6, addr,
624 						(char *)addrbuf,
625 						sizeof(addrbuf)),
626 					    plen, ifi->ifi_ifname);
627 				break;
628 			}
629 			make_prefix(rai, ifindex, addr, plen);
630 			prefixchange = 1;
631 			break;
632 		case RTM_DELETE:
633 			if (sflag)
634 				break;
635 
636 			addr = get_addr(msg);
637 			plen = get_prefixlen(msg);
638 			/* sanity check for plen */
639 			/* as RFC2373, prefixlen is at least 4 */
640 			if (plen < 4 || plen > 127) {
641 				syslog(LOG_INFO,
642 				    "<%s> deleted interface route's "
643 				    "plen %d is invalid for a prefix",
644 				    __func__, plen);
645 				break;
646 			}
647 			pfx = find_prefix(rai, addr, plen);
648 			if (pfx == NULL) {
649 				syslog(LOG_DEBUG,
650 				    "<%s> prefix(%s/%d) was deleted on %s, "
651 				    "but it was not in list",
652 				    __func__, inet_ntop(AF_INET6, addr,
653 					(char *)addrbuf, sizeof(addrbuf)),
654 					plen, ifi->ifi_ifname);
655 				break;
656 			}
657 			invalidate_prefix(pfx);
658 			prefixchange = 1;
659 			break;
660 		case RTM_NEWADDR:
661 		case RTM_DELADDR:
662 		case RTM_IFINFO:
663 			break;
664 		default:
665 			/* should not reach here */
666 			syslog(LOG_DEBUG,
667 			    "<%s:%d> unknown rtmsg %d on %s",
668 			    __func__, __LINE__, type,
669 			    if_indextoname(ifindex, ifname));
670 			return;
671 		}
672 
673 		/* check if an interface flag is changed */
674 		if ((oldifflags & IFF_UP) && /* UP to DOWN */
675 		    !(ifi->ifi_flags & IFF_UP)) {
676 			syslog(LOG_NOTICE,
677 			    "<interface %s becomes down. stop timer.",
678 			    ifi->ifi_ifname);
679 			rtadvd_remove_timer(ifi->ifi_ra_timer);
680 			ifi->ifi_ra_timer = NULL;
681 		} else if (!(oldifflags & IFF_UP) && /* DOWN to UP */
682 		    (ifi->ifi_flags & IFF_UP)) {
683 			syslog(LOG_NOTICE,
684 			    "interface %s becomes up. restart timer.",
685 			    ifi->ifi_ifname);
686 
687 			ifi->ifi_state = IFI_STATE_TRANSITIVE;
688 			ifi->ifi_burstcount =
689 			    MAX_INITIAL_RTR_ADVERTISEMENTS;
690 			ifi->ifi_burstinterval =
691 			    MAX_INITIAL_RTR_ADVERT_INTERVAL;
692 
693 			ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
694 			    ra_timer_update, ifi, ifi);
695 			ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
696 			rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
697 			    ifi->ifi_ra_timer);
698 		} else if (prefixchange &&
699 		    (ifi->ifi_flags & IFF_UP)) {
700 			/*
701 			 * An advertised prefix has been added or invalidated.
702 			 * Will notice the change in a short delay.
703 			 */
704 			set_short_delay(ifi);
705 		}
706 	}
707 
708 	return;
709 }
710 
711 void
712 rtadvd_input(struct sockinfo *s)
713 {
714 	ssize_t i;
715 	int *hlimp = NULL;
716 #ifdef OLDRAWSOCKET
717 	struct ip6_hdr *ip;
718 #endif
719 	struct icmp6_hdr *icp;
720 	int ifindex = 0;
721 	struct cmsghdr *cm;
722 	struct in6_pktinfo *pi = NULL;
723 	char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
724 	struct in6_addr dst = in6addr_any;
725 	struct ifinfo *ifi;
726 
727 	syslog(LOG_DEBUG, "<%s> enter", __func__);
728 
729 	if (s == NULL) {
730 		syslog(LOG_ERR, "<%s> internal error", __func__);
731 		exit(1);
732 	}
733 	/*
734 	 * Get message. We reset msg_controllen since the field could
735 	 * be modified if we had received a message before setting
736 	 * receive options.
737 	 */
738 	rcvmhdr.msg_controllen = rcvcmsgbuflen;
739 	if ((i = recvmsg(s->si_fd, &rcvmhdr, 0)) < 0)
740 		return;
741 
742 	/* extract optional information via Advanced API */
743 	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
744 	     cm;
745 	     cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
746 		if (cm->cmsg_level == IPPROTO_IPV6 &&
747 		    cm->cmsg_type == IPV6_PKTINFO &&
748 		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
749 			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
750 			ifindex = pi->ipi6_ifindex;
751 			dst = pi->ipi6_addr;
752 		}
753 		if (cm->cmsg_level == IPPROTO_IPV6 &&
754 		    cm->cmsg_type == IPV6_HOPLIMIT &&
755 		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
756 			hlimp = (int *)CMSG_DATA(cm);
757 	}
758 	if (ifindex == 0) {
759 		syslog(LOG_ERR, "failed to get receiving interface");
760 		return;
761 	}
762 	if (hlimp == NULL) {
763 		syslog(LOG_ERR, "failed to get receiving hop limit");
764 		return;
765 	}
766 
767 	/*
768 	 * If we happen to receive data on an interface which is now gone
769 	 * or down, just discard the data.
770 	 */
771 	ifi = if_indextoifinfo(pi->ipi6_ifindex);
772 	if (ifi == NULL || !(ifi->ifi_flags & IFF_UP)) {
773 		syslog(LOG_INFO,
774 		    "<%s> received data on a disabled interface (%s)",
775 		    __func__,
776 		    (ifi == NULL) ? "[gone]" : ifi->ifi_ifname);
777 		return;
778 	}
779 
780 #ifdef OLDRAWSOCKET
781 	if ((size_t)i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
782 		syslog(LOG_ERR,
783 		    "packet size(%d) is too short", i);
784 		return;
785 	}
786 
787 	ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
788 	icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
789 #else
790 	if ((size_t)i < sizeof(struct icmp6_hdr)) {
791 		syslog(LOG_ERR, "packet size(%zd) is too short", i);
792 		return;
793 	}
794 
795 	icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
796 #endif
797 
798 	switch (icp->icmp6_type) {
799 	case ND_ROUTER_SOLICIT:
800 		/*
801 		 * Message verification - RFC 4861 6.1.1
802 		 * XXX: these checks must be done in the kernel as well,
803 		 *      but we can't completely rely on them.
804 		 */
805 		if (*hlimp != 255) {
806 			syslog(LOG_NOTICE,
807 			    "RS with invalid hop limit(%d) "
808 			    "received from %s on %s",
809 			    *hlimp,
810 			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
811 			    sizeof(ntopbuf)),
812 			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
813 			return;
814 		}
815 		if (icp->icmp6_code) {
816 			syslog(LOG_NOTICE,
817 			    "RS with invalid ICMP6 code(%d) "
818 			    "received from %s on %s",
819 			    icp->icmp6_code,
820 			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
821 			    sizeof(ntopbuf)),
822 			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
823 			return;
824 		}
825 		if ((size_t)i < sizeof(struct nd_router_solicit)) {
826 			syslog(LOG_NOTICE,
827 			    "RS from %s on %s does not have enough "
828 			    "length (len = %zd)",
829 			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
830 			    sizeof(ntopbuf)),
831 			    if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
832 			return;
833 		}
834 		rs_input(i, (struct nd_router_solicit *)icp, pi, &rcvfrom);
835 		break;
836 	case ND_ROUTER_ADVERT:
837 		/*
838 		 * Message verification - RFC 4861 6.1.2
839 		 * XXX: there's the same dilemma as above...
840 		 */
841 		if (!IN6_IS_ADDR_LINKLOCAL(&rcvfrom.sin6_addr)) {
842 			syslog(LOG_NOTICE,
843 			    "RA with non-linklocal source address "
844 			    "received from %s on %s",
845 			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr,
846 			    ntopbuf, sizeof(ntopbuf)),
847 			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
848 			return;
849 		}
850 		if (*hlimp != 255) {
851 			syslog(LOG_NOTICE,
852 			    "RA with invalid hop limit(%d) "
853 			    "received from %s on %s",
854 			    *hlimp,
855 			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
856 			    sizeof(ntopbuf)),
857 			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
858 			return;
859 		}
860 		if (icp->icmp6_code) {
861 			syslog(LOG_NOTICE,
862 			    "RA with invalid ICMP6 code(%d) "
863 			    "received from %s on %s",
864 			    icp->icmp6_code,
865 			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
866 			    sizeof(ntopbuf)),
867 			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
868 			return;
869 		}
870 		if ((size_t)i < sizeof(struct nd_router_advert)) {
871 			syslog(LOG_NOTICE,
872 			    "RA from %s on %s does not have enough "
873 			    "length (len = %zd)",
874 			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
875 			    sizeof(ntopbuf)),
876 			    if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
877 			return;
878 		}
879 		ra_input(i, (struct nd_router_advert *)icp, pi, &rcvfrom);
880 		break;
881 	case ICMP6_ROUTER_RENUMBERING:
882 		if (mcastif == NULL) {
883 			syslog(LOG_ERR, "received a router renumbering "
884 			    "message, but not allowed to be accepted");
885 			break;
886 		}
887 		rr_input(i, (struct icmp6_router_renum *)icp, pi, &rcvfrom,
888 		    &dst);
889 		break;
890 	default:
891 		/*
892 		 * Note that this case is POSSIBLE, especially just
893 		 * after invocation of the daemon. This is because we
894 		 * could receive message after opening the socket and
895 		 * before setting ICMP6 type filter(see sock_open()).
896 		 */
897 		syslog(LOG_ERR, "invalid icmp type(%d)", icp->icmp6_type);
898 		return;
899 	}
900 
901 	return;
902 }
903 
904 static void
905 rs_input(int len, struct nd_router_solicit *rs,
906 	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
907 {
908 	char ntopbuf[INET6_ADDRSTRLEN];
909 	char ifnamebuf[IFNAMSIZ];
910 	union nd_opt ndopts;
911 	struct rainfo *rai;
912 	struct ifinfo *ifi;
913 	struct soliciter *sol;
914 
915 	syslog(LOG_DEBUG,
916 	    "<%s> RS received from %s on %s",
917 	    __func__,
918 	    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
919 	    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
920 
921 	/* ND option check */
922 	memset(&ndopts, 0, sizeof(ndopts));
923 	TAILQ_INIT(&ndopts.opt_list);
924 	if (nd6_options((struct nd_opt_hdr *)(rs + 1),
925 			len - sizeof(struct nd_router_solicit),
926 			&ndopts, NDOPT_FLAG_SRCLINKADDR)) {
927 		syslog(LOG_INFO,
928 		    "<%s> ND option check failed for an RS from %s on %s",
929 		    __func__,
930 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
931 			sizeof(ntopbuf)),
932 		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
933 		return;
934 	}
935 
936 	/*
937 	 * If the IP source address is the unspecified address, there
938 	 * must be no source link-layer address option in the message.
939 	 * (RFC 4861 6.1.1)
940 	 */
941 	if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
942 	    ndopts.opt_src_lladdr) {
943 		syslog(LOG_INFO,
944 		    "<%s> RS from unspecified src on %s has a link-layer"
945 		    " address option",
946 		    __func__, if_indextoname(pi->ipi6_ifindex, ifnamebuf));
947 		goto done;
948 	}
949 
950 	ifi = if_indextoifinfo(pi->ipi6_ifindex);
951 	if (ifi == NULL) {
952 		syslog(LOG_INFO,
953 		    "<%s> if (idx=%d) not found.  Why?",
954 		    __func__, pi->ipi6_ifindex);
955 		goto done;
956 	}
957 	rai = ifi->ifi_rainfo;
958 	if (rai == NULL) {
959 		syslog(LOG_INFO,
960 		       "<%s> RS received on non advertising interface(%s)",
961 		       __func__,
962 		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
963 		goto done;
964 	}
965 
966 	rai->rai_ifinfo->ifi_rsinput++;
967 
968 	/*
969 	 * Decide whether to send RA according to the rate-limit
970 	 * consideration.
971 	 */
972 
973 	/* record sockaddr waiting for RA, if possible */
974 	sol = (struct soliciter *)malloc(sizeof(*sol));
975 	if (sol) {
976 		sol->sol_addr = *from;
977 		/* XXX RFC 2553 need clarification on flowinfo */
978 		sol->sol_addr.sin6_flowinfo = 0;
979 		TAILQ_INSERT_TAIL(&rai->rai_soliciter, sol, sol_next);
980 	}
981 
982 	/*
983 	 * If there is already a waiting RS packet, don't
984 	 * update the timer.
985 	 */
986 	if (ifi->ifi_rs_waitcount++)
987 		goto done;
988 
989 	set_short_delay(ifi);
990 
991   done:
992 	free_ndopts(&ndopts);
993 	return;
994 }
995 
996 static void
997 set_short_delay(struct ifinfo *ifi)
998 {
999 	long delay;	/* must not be greater than 1000000 */
1000 	struct timespec interval, now, min_delay, tm_tmp, *rest;
1001 
1002 	if (ifi->ifi_ra_timer == NULL)
1003 		return;
1004 	/*
1005 	 * Compute a random delay. If the computed value
1006 	 * corresponds to a time later than the time the next
1007 	 * multicast RA is scheduled to be sent, ignore the random
1008 	 * delay and send the advertisement at the
1009 	 * already-scheduled time. RFC 4861 6.2.6
1010 	 */
1011 	delay = arc4random_uniform(MAX_RA_DELAY_TIME);
1012 	interval.tv_sec = 0;
1013 	interval.tv_nsec = delay * 1000;
1014 	rest = rtadvd_timer_rest(ifi->ifi_ra_timer);
1015 	if (TS_CMP(rest, &interval, <)) {
1016 		syslog(LOG_DEBUG, "<%s> random delay is larger than "
1017 		    "the rest of the current timer", __func__);
1018 		interval = *rest;
1019 	}
1020 
1021 	/*
1022 	 * If we sent a multicast Router Advertisement within
1023 	 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
1024 	 * the advertisement to be sent at a time corresponding to
1025 	 * MIN_DELAY_BETWEEN_RAS plus the random value after the
1026 	 * previous advertisement was sent.
1027 	 */
1028 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1029 	TS_SUB(&now, &ifi->ifi_ra_lastsent, &tm_tmp);
1030 	min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
1031 	min_delay.tv_nsec = 0;
1032 	if (TS_CMP(&tm_tmp, &min_delay, <)) {
1033 		TS_SUB(&min_delay, &tm_tmp, &min_delay);
1034 		TS_ADD(&min_delay, &interval, &interval);
1035 	}
1036 	rtadvd_set_timer(&interval, ifi->ifi_ra_timer);
1037 }
1038 
1039 static int
1040 check_accept_rtadv(int idx)
1041 {
1042 	struct ifinfo *ifi;
1043 
1044 	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1045 		if (ifi->ifi_ifindex == idx)
1046 			break;
1047 	}
1048 	if (ifi == NULL) {
1049 		syslog(LOG_DEBUG,
1050 		    "<%s> if (idx=%d) not found.  Why?",
1051 		    __func__, idx);
1052 		return (0);
1053 	}
1054 
1055 	/*
1056 	 * RA_RECV: ND6_IFF_ACCEPT_RTADV
1057 	 * RA_SEND: ip6.forwarding
1058 	 */
1059 	if (update_ifinfo_nd_flags(ifi) != 0) {
1060 		syslog(LOG_ERR, "cannot get nd6 flags (idx=%d)", idx);
1061 		return (0);
1062 	}
1063 
1064 	return (ifi->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV);
1065 }
1066 
1067 static void
1068 ra_input(int len, struct nd_router_advert *nra,
1069 	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
1070 {
1071 	struct rainfo *rai;
1072 	struct ifinfo *ifi;
1073 	char ntopbuf[INET6_ADDRSTRLEN];
1074 	char ifnamebuf[IFNAMSIZ];
1075 	union nd_opt ndopts;
1076 	const char *on_off[] = {"OFF", "ON"};
1077 	uint32_t reachabletime, retranstimer, mtu;
1078 	int inconsistent = 0;
1079 	int error;
1080 
1081 	syslog(LOG_DEBUG, "<%s> RA received from %s on %s", __func__,
1082 	    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
1083 	    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
1084 
1085 	/* ND option check */
1086 	memset(&ndopts, 0, sizeof(ndopts));
1087 	TAILQ_INIT(&ndopts.opt_list);
1088 	error = nd6_options((struct nd_opt_hdr *)(nra + 1),
1089 	    len - sizeof(struct nd_router_advert), &ndopts,
1090 	    NDOPT_FLAG_SRCLINKADDR | NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU |
1091 	    NDOPT_FLAG_RDNSS | NDOPT_FLAG_DNSSL);
1092 	if (error) {
1093 		syslog(LOG_INFO,
1094 		    "<%s> ND option check failed for an RA from %s on %s",
1095 		    __func__,
1096 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1097 			sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1098 			ifnamebuf));
1099 		return;
1100 	}
1101 
1102 	/*
1103 	 * RA consistency check according to RFC 4861 6.2.7
1104 	 */
1105 	ifi = if_indextoifinfo(pi->ipi6_ifindex);
1106 	if (ifi->ifi_rainfo == NULL) {
1107 		syslog(LOG_INFO,
1108 		    "<%s> received RA from %s on non-advertising"
1109 		    " interface(%s)",
1110 		    __func__,
1111 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1112 			sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1113 			ifnamebuf));
1114 		goto done;
1115 	}
1116 	rai = ifi->ifi_rainfo;
1117 	ifi->ifi_rainput++;
1118 	syslog(LOG_DEBUG, "<%s> ifi->ifi_rainput = %" PRIu64, __func__,
1119 	    ifi->ifi_rainput);
1120 
1121 	/* Cur Hop Limit value */
1122 	if (nra->nd_ra_curhoplimit && rai->rai_hoplimit &&
1123 	    nra->nd_ra_curhoplimit != rai->rai_hoplimit) {
1124 		syslog(LOG_NOTICE,
1125 		    "CurHopLimit inconsistent on %s:"
1126 		    " %d from %s, %d from us",
1127 		    ifi->ifi_ifname, nra->nd_ra_curhoplimit,
1128 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1129 			sizeof(ntopbuf)), rai->rai_hoplimit);
1130 		inconsistent++;
1131 	}
1132 	/* M flag */
1133 	if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
1134 	    rai->rai_managedflg) {
1135 		syslog(LOG_NOTICE,
1136 		    "M flag inconsistent on %s:"
1137 		    " %s from %s, %s from us",
1138 		    ifi->ifi_ifname, on_off[!rai->rai_managedflg],
1139 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1140 			sizeof(ntopbuf)), on_off[rai->rai_managedflg]);
1141 		inconsistent++;
1142 	}
1143 	/* O flag */
1144 	if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
1145 	    rai->rai_otherflg) {
1146 		syslog(LOG_NOTICE,
1147 		    "O flag inconsistent on %s:"
1148 		    " %s from %s, %s from us",
1149 		    ifi->ifi_ifname, on_off[!rai->rai_otherflg],
1150 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1151 			sizeof(ntopbuf)), on_off[rai->rai_otherflg]);
1152 		inconsistent++;
1153 	}
1154 #ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
1155 	/* S "IPv6-Only" (Six, Silence-IPv4) flag */
1156 	if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_IPV6_ONLY) !=
1157 	    rai->rai_ipv6onlyflg) {
1158 		syslog(LOG_NOTICE,
1159 		    "S flag inconsistent on %s:"
1160 		    " %s from %s, %s from us",
1161 		    ifi->ifi_ifname, on_off[!rai->rai_ipv6onlyflg],
1162 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1163 			sizeof(ntopbuf)), on_off[rai->rai_ipv6onlyflg]);
1164 		inconsistent++;
1165 	}
1166 #endif
1167 	/* Reachable Time */
1168 	reachabletime = ntohl(nra->nd_ra_reachable);
1169 	if (reachabletime && rai->rai_reachabletime &&
1170 	    reachabletime != rai->rai_reachabletime) {
1171 		syslog(LOG_NOTICE,
1172 		    "ReachableTime inconsistent on %s:"
1173 		    " %d from %s, %d from us",
1174 		    ifi->ifi_ifname, reachabletime,
1175 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1176 			sizeof(ntopbuf)), rai->rai_reachabletime);
1177 		inconsistent++;
1178 	}
1179 	/* Retrans Timer */
1180 	retranstimer = ntohl(nra->nd_ra_retransmit);
1181 	if (retranstimer && rai->rai_retranstimer &&
1182 	    retranstimer != rai->rai_retranstimer) {
1183 		syslog(LOG_NOTICE,
1184 		    "RetranceTimer inconsistent on %s:"
1185 		    " %d from %s, %d from us",
1186 		    ifi->ifi_ifname, retranstimer,
1187 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1188 			sizeof(ntopbuf)), rai->rai_retranstimer);
1189 		inconsistent++;
1190 	}
1191 	/* Values in the MTU options */
1192 	if (ndopts.opt_mtu) {
1193 		mtu = ntohl(ndopts.opt_mtu->nd_opt_mtu_mtu);
1194 		if (mtu && rai->rai_linkmtu && mtu != rai->rai_linkmtu) {
1195 			syslog(LOG_NOTICE,
1196 			    "MTU option value inconsistent on %s:"
1197 			    " %d from %s, %d from us",
1198 			    ifi->ifi_ifname, mtu,
1199 			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1200 				sizeof(ntopbuf)), rai->rai_linkmtu);
1201 			inconsistent++;
1202 		}
1203 	}
1204 	/* Preferred and Valid Lifetimes for prefixes */
1205 	{
1206 		struct nd_optlist *nol;
1207 
1208 		if (ndopts.opt_pi)
1209 			if (prefix_check(ndopts.opt_pi, rai, from))
1210 				inconsistent++;
1211 
1212 		TAILQ_FOREACH(nol, &ndopts.opt_list, nol_next)
1213 			if (prefix_check((struct nd_opt_prefix_info *)nol->nol_opt,
1214 				rai, from))
1215 				inconsistent++;
1216 	}
1217 
1218 	if (inconsistent)
1219 		ifi->ifi_rainconsistent++;
1220 
1221   done:
1222 	free_ndopts(&ndopts);
1223 	return;
1224 }
1225 
1226 static uint32_t
1227 udiff(uint32_t u, uint32_t v)
1228 {
1229 	return (u >= v ? u - v : v - u);
1230 }
1231 
1232 /* return a non-zero value if the received prefix is inconsistent with ours */
1233 static int
1234 prefix_check(struct nd_opt_prefix_info *pinfo,
1235 	struct rainfo *rai, struct sockaddr_in6 *from)
1236 {
1237 	struct ifinfo *ifi;
1238 	uint32_t preferred_time, valid_time;
1239 	struct prefix *pfx;
1240 	int inconsistent = 0;
1241 	char ntopbuf[INET6_ADDRSTRLEN];
1242 	char prefixbuf[INET6_ADDRSTRLEN];
1243 	struct timespec now;
1244 
1245 #if 0				/* impossible */
1246 	if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1247 		return (0);
1248 #endif
1249 	ifi = rai->rai_ifinfo;
1250 	/*
1251 	 * log if the adveritsed prefix has link-local scope(sanity check?)
1252 	 */
1253 	if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix))
1254 		syslog(LOG_INFO,
1255 		    "<%s> link-local prefix %s/%d is advertised "
1256 		    "from %s on %s",
1257 		    __func__,
1258 		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1259 			sizeof(prefixbuf)),
1260 		    pinfo->nd_opt_pi_prefix_len,
1261 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1262 			sizeof(ntopbuf)), ifi->ifi_ifname);
1263 
1264 	if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1265 		pinfo->nd_opt_pi_prefix_len)) == NULL) {
1266 		syslog(LOG_INFO,
1267 		    "<%s> prefix %s/%d from %s on %s is not in our list",
1268 		    __func__,
1269 		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1270 			sizeof(prefixbuf)),
1271 		    pinfo->nd_opt_pi_prefix_len,
1272 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1273 			sizeof(ntopbuf)), ifi->ifi_ifname);
1274 		return (0);
1275 	}
1276 
1277 	preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1278 	if (pfx->pfx_pltimeexpire) {
1279 		/*
1280 		 * The lifetime is decremented in real time, so we should
1281 		 * compare the expiration time.
1282 		 * (RFC 2461 Section 6.2.7.)
1283 		 * XXX: can we really expect that all routers on the link
1284 		 * have synchronized clocks?
1285 		 */
1286 		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1287 		preferred_time += now.tv_sec;
1288 
1289 		if (!pfx->pfx_timer && rai->rai_clockskew &&
1290 		    udiff(preferred_time, pfx->pfx_pltimeexpire) > rai->rai_clockskew) {
1291 			syslog(LOG_INFO,
1292 			    "<%s> preferred lifetime for %s/%d"
1293 			    " (decr. in real time) inconsistent on %s:"
1294 			    " %" PRIu32 " from %s, %" PRIu32 " from us",
1295 			    __func__,
1296 			    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1297 				sizeof(prefixbuf)),
1298 			    pinfo->nd_opt_pi_prefix_len,
1299 			    ifi->ifi_ifname, preferred_time,
1300 			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1301 				sizeof(ntopbuf)), pfx->pfx_pltimeexpire);
1302 			inconsistent++;
1303 		}
1304 	} else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime)
1305 		syslog(LOG_INFO,
1306 		    "<%s> preferred lifetime for %s/%d"
1307 		    " inconsistent on %s:"
1308 		    " %d from %s, %d from us",
1309 		    __func__,
1310 		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1311 			sizeof(prefixbuf)),
1312 		    pinfo->nd_opt_pi_prefix_len,
1313 		    ifi->ifi_ifname, preferred_time,
1314 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1315 			sizeof(ntopbuf)), pfx->pfx_preflifetime);
1316 
1317 	valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1318 	if (pfx->pfx_vltimeexpire) {
1319 		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1320 		valid_time += now.tv_sec;
1321 
1322 		if (!pfx->pfx_timer && rai->rai_clockskew &&
1323 		    udiff(valid_time, pfx->pfx_vltimeexpire) > rai->rai_clockskew) {
1324 			syslog(LOG_INFO,
1325 			    "<%s> valid lifetime for %s/%d"
1326 			    " (decr. in real time) inconsistent on %s:"
1327 			    " %d from %s, %" PRIu32 " from us",
1328 			    __func__,
1329 			    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1330 				sizeof(prefixbuf)),
1331 			    pinfo->nd_opt_pi_prefix_len,
1332 			    ifi->ifi_ifname, preferred_time,
1333 			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1334 				sizeof(ntopbuf)), pfx->pfx_vltimeexpire);
1335 			inconsistent++;
1336 		}
1337 	} else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) {
1338 		syslog(LOG_INFO,
1339 		    "<%s> valid lifetime for %s/%d"
1340 		    " inconsistent on %s:"
1341 		    " %d from %s, %d from us",
1342 		    __func__,
1343 		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1344 			sizeof(prefixbuf)),
1345 		    pinfo->nd_opt_pi_prefix_len,
1346 		    ifi->ifi_ifname, valid_time,
1347 		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1348 			sizeof(ntopbuf)), pfx->pfx_validlifetime);
1349 		inconsistent++;
1350 	}
1351 
1352 	return (inconsistent);
1353 }
1354 
1355 struct prefix *
1356 find_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
1357 {
1358 	struct prefix *pfx;
1359 	int bytelen, bitlen;
1360 	char bitmask;
1361 
1362 	TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1363 		if (plen != pfx->pfx_prefixlen)
1364 			continue;
1365 
1366 		bytelen = plen / 8;
1367 		bitlen = plen % 8;
1368 		bitmask = 0xff << (8 - bitlen);
1369 
1370 		if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen))
1371 			continue;
1372 
1373 		if (bitlen == 0 ||
1374 		    ((prefix->s6_addr[bytelen] & bitmask) ==
1375 		     (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) {
1376 			return (pfx);
1377 		}
1378 	}
1379 
1380 	return (NULL);
1381 }
1382 
1383 /* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
1384 int
1385 prefix_match(struct in6_addr *p0, int plen0,
1386 	struct in6_addr *p1, int plen1)
1387 {
1388 	int bytelen, bitlen;
1389 	char bitmask;
1390 
1391 	if (plen0 < plen1)
1392 		return (0);
1393 
1394 	bytelen = plen1 / 8;
1395 	bitlen = plen1 % 8;
1396 	bitmask = 0xff << (8 - bitlen);
1397 
1398 	if (memcmp((void *)p0, (void *)p1, bytelen))
1399 		return (0);
1400 
1401 	if (bitlen == 0 ||
1402 	    ((p0->s6_addr[bytelen] & bitmask) ==
1403 	     (p1->s6_addr[bytelen] & bitmask))) {
1404 		return (1);
1405 	}
1406 
1407 	return (0);
1408 }
1409 
1410 static int
1411 nd6_options(struct nd_opt_hdr *hdr, int limit,
1412 	union nd_opt *ndopts, uint32_t optflags)
1413 {
1414 	int optlen = 0;
1415 
1416 	for (; limit > 0; limit -= optlen) {
1417 		if ((size_t)limit < sizeof(struct nd_opt_hdr)) {
1418 			syslog(LOG_INFO, "<%s> short option header", __func__);
1419 			goto bad;
1420 		}
1421 
1422 		hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
1423 		if (hdr->nd_opt_len == 0) {
1424 			syslog(LOG_INFO,
1425 			    "<%s> bad ND option length(0) (type = %d)",
1426 			    __func__, hdr->nd_opt_type);
1427 			goto bad;
1428 		}
1429 		optlen = hdr->nd_opt_len << 3;
1430 		if (optlen > limit) {
1431 			syslog(LOG_INFO, "<%s> short option", __func__);
1432 			goto bad;
1433 		}
1434 
1435 		if (hdr->nd_opt_type > ND_OPT_MTU &&
1436 		    hdr->nd_opt_type != ND_OPT_RDNSS &&
1437 		    hdr->nd_opt_type != ND_OPT_DNSSL) {
1438 			syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1439 			    __func__, hdr->nd_opt_type);
1440 			continue;
1441 		}
1442 
1443 		if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1444 			syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1445 			    __func__, hdr->nd_opt_type);
1446 			continue;
1447 		}
1448 
1449 		/*
1450 		 * Option length check.  Do it here for all fixed-length
1451 		 * options.
1452 		 */
1453 		switch (hdr->nd_opt_type) {
1454 		case ND_OPT_MTU:
1455 			if (optlen == sizeof(struct nd_opt_mtu))
1456 				break;
1457 			goto skip;
1458 		case ND_OPT_RDNSS:
1459 			if (optlen >= 24 &&
1460 			    (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0)
1461 				break;
1462 			goto skip;
1463 		case ND_OPT_DNSSL:
1464 			if (optlen >= 16 &&
1465 			    (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0)
1466 				break;
1467 			goto skip;
1468 		case ND_OPT_PREFIX_INFORMATION:
1469 			if (optlen == sizeof(struct nd_opt_prefix_info))
1470 				break;
1471 skip:
1472 			syslog(LOG_INFO, "<%s> invalid option length",
1473 			    __func__);
1474 			continue;
1475 		}
1476 
1477 		switch (hdr->nd_opt_type) {
1478 		case ND_OPT_TARGET_LINKADDR:
1479 		case ND_OPT_REDIRECTED_HEADER:
1480 		case ND_OPT_RDNSS:
1481 		case ND_OPT_DNSSL:
1482 			break;	/* we don't care about these options */
1483 		case ND_OPT_SOURCE_LINKADDR:
1484 		case ND_OPT_MTU:
1485 			if (ndopts->opt_array[hdr->nd_opt_type]) {
1486 				syslog(LOG_INFO,
1487 				    "<%s> duplicated ND option (type = %d)",
1488 				    __func__, hdr->nd_opt_type);
1489 			}
1490 			ndopts->opt_array[hdr->nd_opt_type] = hdr;
1491 			break;
1492 		case ND_OPT_PREFIX_INFORMATION:
1493 		{
1494 			struct nd_optlist *nol;
1495 
1496 			if (ndopts->opt_pi == 0) {
1497 				ndopts->opt_pi =
1498 				    (struct nd_opt_prefix_info *)hdr;
1499 				continue;
1500 			}
1501 			nol = malloc(sizeof(*nol));
1502 			if (nol == NULL) {
1503 				syslog(LOG_ERR, "<%s> can't allocate memory",
1504 				    __func__);
1505 				goto bad;
1506 			}
1507 			nol->nol_opt = hdr;
1508 			TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next);
1509 
1510 			break;
1511 		}
1512 		default:	/* impossible */
1513 			break;
1514 		}
1515 	}
1516 
1517 	return (0);
1518 
1519   bad:
1520 	free_ndopts(ndopts);
1521 
1522 	return (-1);
1523 }
1524 
1525 static void
1526 free_ndopts(union nd_opt *ndopts)
1527 {
1528 	struct nd_optlist *nol;
1529 
1530 	while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) {
1531 		TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next);
1532 		free(nol);
1533 	}
1534 }
1535 
1536 void
1537 sock_open(struct sockinfo *s)
1538 {
1539 	struct icmp6_filter filt;
1540 	int on;
1541 	/* XXX: should be max MTU attached to the node */
1542 	static char answer[1500];
1543 
1544 	syslog(LOG_DEBUG, "<%s> enter", __func__);
1545 
1546 	if (s == NULL) {
1547 		syslog(LOG_ERR, "<%s> internal error", __func__);
1548 		exit(1);
1549 	}
1550 	rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1551 	    CMSG_SPACE(sizeof(int));
1552 	rcvcmsgbuf = (char *)malloc(rcvcmsgbuflen);
1553 	if (rcvcmsgbuf == NULL) {
1554 		syslog(LOG_ERR, "<%s> not enough core", __func__);
1555 		exit(1);
1556 	}
1557 
1558 	sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1559 	    CMSG_SPACE(sizeof(int));
1560 	sndcmsgbuf = (char *)malloc(sndcmsgbuflen);
1561 	if (sndcmsgbuf == NULL) {
1562 		syslog(LOG_ERR, "<%s> not enough core", __func__);
1563 		exit(1);
1564 	}
1565 
1566 	if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1567 		syslog(LOG_ERR, "<%s> socket: %s", __func__, strerror(errno));
1568 		exit(1);
1569 	}
1570 	/* specify to tell receiving interface */
1571 	on = 1;
1572 	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1573 	    sizeof(on)) < 0) {
1574 		syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s", __func__,
1575 		    strerror(errno));
1576 		exit(1);
1577 	}
1578 	on = 1;
1579 	/* specify to tell value of hoplimit field of received IP6 hdr */
1580 	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1581 		sizeof(on)) < 0) {
1582 		syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s", __func__,
1583 		    strerror(errno));
1584 		exit(1);
1585 	}
1586 	ICMP6_FILTER_SETBLOCKALL(&filt);
1587 	ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
1588 	ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1589 	if (mcastif != NULL)
1590 		ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1591 
1592 	if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1593 	    sizeof(filt)) < 0) {
1594 		syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1595 		    __func__, strerror(errno));
1596 		exit(1);
1597 	}
1598 
1599 	/* initialize msghdr for receiving packets */
1600 	rcviov[0].iov_base = (caddr_t)answer;
1601 	rcviov[0].iov_len = sizeof(answer);
1602 	rcvmhdr.msg_name = (caddr_t)&rcvfrom;
1603 	rcvmhdr.msg_namelen = sizeof(rcvfrom);
1604 	rcvmhdr.msg_iov = rcviov;
1605 	rcvmhdr.msg_iovlen = 1;
1606 	rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
1607 	rcvmhdr.msg_controllen = rcvcmsgbuflen;
1608 
1609 	/* initialize msghdr for sending packets */
1610 	sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
1611 	sndmhdr.msg_iov = sndiov;
1612 	sndmhdr.msg_iovlen = 1;
1613 	sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
1614 	sndmhdr.msg_controllen = sndcmsgbuflen;
1615 
1616 	return;
1617 }
1618 
1619 /* open a routing socket to watch the routing table */
1620 static void
1621 rtsock_open(struct sockinfo *s)
1622 {
1623 	if (s == NULL) {
1624 		syslog(LOG_ERR, "<%s> internal error", __func__);
1625 		exit(1);
1626 	}
1627 	if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
1628 		syslog(LOG_ERR,
1629 		    "<%s> socket: %s", __func__, strerror(errno));
1630 		exit(1);
1631 	}
1632 }
1633 
1634 struct ifinfo *
1635 if_indextoifinfo(int idx)
1636 {
1637 	struct ifinfo *ifi;
1638 	char *name, name0[IFNAMSIZ];
1639 
1640 	/* Check if the interface has a valid name or not. */
1641 	if (if_indextoname(idx, name0) == NULL)
1642 		return (NULL);
1643 
1644 	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1645 		if (ifi->ifi_ifindex == idx)
1646 			return (ifi);
1647 	}
1648 
1649 	if (ifi != NULL)
1650 		syslog(LOG_DEBUG, "<%s> ifi found (idx=%d)",
1651 		    __func__, idx);
1652 	else
1653 		syslog(LOG_DEBUG, "<%s> ifi not found (idx=%d)",
1654 		    __func__, idx);
1655 
1656 	return (NULL);		/* search failed */
1657 }
1658 
1659 void
1660 ra_output(struct ifinfo *ifi)
1661 {
1662 	int i;
1663 	struct cmsghdr *cm;
1664 	struct in6_pktinfo *pi;
1665 	struct soliciter *sol;
1666 	struct rainfo *rai;
1667 
1668 	switch (ifi->ifi_state) {
1669 	case IFI_STATE_CONFIGURED:
1670 		rai = ifi->ifi_rainfo;
1671 		break;
1672 	case IFI_STATE_TRANSITIVE:
1673 		rai = ifi->ifi_rainfo_trans;
1674 		break;
1675 	case IFI_STATE_UNCONFIGURED:
1676 		syslog(LOG_DEBUG, "<%s> %s is unconfigured.  "
1677 		    "Skip sending RAs.",
1678 		    __func__, ifi->ifi_ifname);
1679 		return;
1680 	default:
1681 		rai = NULL;
1682 	}
1683 	if (rai == NULL) {
1684 		syslog(LOG_DEBUG, "<%s> rainfo is NULL on %s."
1685 		    "Skip sending RAs.",
1686 		    __func__, ifi->ifi_ifname);
1687 		return;
1688 	}
1689 	if (!(ifi->ifi_flags & IFF_UP)) {
1690 		syslog(LOG_DEBUG, "<%s> %s is not up.  "
1691 		    "Skip sending RAs.",
1692 		    __func__, ifi->ifi_ifname);
1693 		return;
1694 	}
1695 	/*
1696 	 * Check lifetime, ACCEPT_RTADV flag, and ip6.forwarding.
1697 	 *
1698 	 * (lifetime == 0) = output
1699 	 * (lifetime != 0 && (check_accept_rtadv()) = no output
1700 	 *
1701 	 * Basically, hosts MUST NOT send Router Advertisement
1702 	 * messages at any time (RFC 4861, Section 6.2.3). However, it
1703 	 * would sometimes be useful to allow hosts to advertise some
1704 	 * parameters such as prefix information and link MTU. Thus,
1705 	 * we allow hosts to invoke rtadvd only when router lifetime
1706 	 * (on every advertising interface) is explicitly set
1707 	 * zero. (see also the above section)
1708 	 */
1709 	syslog(LOG_DEBUG,
1710 	    "<%s> check lifetime=%d, ACCEPT_RTADV=%d, ip6.forwarding=%d "
1711 	    "on %s", __func__,
1712 	    rai->rai_lifetime,
1713 	    check_accept_rtadv(ifi->ifi_ifindex),
1714 	    getinet6sysctl(IPV6CTL_FORWARDING),
1715 	    ifi->ifi_ifname);
1716 
1717 	if (rai->rai_lifetime != 0) {
1718 		if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) {
1719 			syslog(LOG_ERR,
1720 			    "non-zero lifetime RA "
1721 			    "but net.inet6.ip6.forwarding=0.  "
1722 			    "Ignored.");
1723 			return;
1724 		}
1725 		if (check_accept_rtadv(ifi->ifi_ifindex)) {
1726 			syslog(LOG_ERR,
1727 			    "non-zero lifetime RA "
1728 			    "on RA receiving interface %s."
1729 			    "  Ignored.", ifi->ifi_ifname);
1730 			return;
1731 		}
1732 	}
1733 
1734 	make_packet(rai);	/* XXX: inefficient */
1735 
1736 	sndmhdr.msg_name = (caddr_t)&sin6_linklocal_allnodes;
1737 	sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data;
1738 	sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen;
1739 
1740 	cm = CMSG_FIRSTHDR(&sndmhdr);
1741 	/* specify the outgoing interface */
1742 	cm->cmsg_level = IPPROTO_IPV6;
1743 	cm->cmsg_type = IPV6_PKTINFO;
1744 	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
1745 	pi = (struct in6_pktinfo *)CMSG_DATA(cm);
1746 	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));	/*XXX*/
1747 	pi->ipi6_ifindex = ifi->ifi_ifindex;
1748 
1749 	/* specify the hop limit of the packet */
1750 	{
1751 		int hoplimit = 255;
1752 
1753 		cm = CMSG_NXTHDR(&sndmhdr, cm);
1754 		cm->cmsg_level = IPPROTO_IPV6;
1755 		cm->cmsg_type = IPV6_HOPLIMIT;
1756 		cm->cmsg_len = CMSG_LEN(sizeof(int));
1757 		memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
1758 	}
1759 
1760 	syslog(LOG_DEBUG,
1761 	    "<%s> send RA on %s, # of RS waitings = %d",
1762 	    __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount);
1763 
1764 	i = sendmsg(sock.si_fd, &sndmhdr, 0);
1765 
1766 	if (i < 0 || (size_t)i != rai->rai_ra_datalen)  {
1767 		if (i < 0) {
1768 			syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1769 			    __func__, ifi->ifi_ifname,
1770 			    strerror(errno));
1771 		}
1772 	}
1773 
1774 	/*
1775 	 * unicast advertisements
1776 	 * XXX commented out.  reason: though spec does not forbit it, unicast
1777 	 * advert does not really help
1778 	 */
1779 	while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
1780 		TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
1781 		free(sol);
1782 	}
1783 
1784 	/* update timestamp */
1785 	clock_gettime(CLOCK_MONOTONIC_FAST, &ifi->ifi_ra_lastsent);
1786 
1787 	/* update counter */
1788 	ifi->ifi_rs_waitcount = 0;
1789 	ifi->ifi_raoutput++;
1790 
1791 	switch (ifi->ifi_state) {
1792 	case IFI_STATE_CONFIGURED:
1793 		if (ifi->ifi_burstcount > 0)
1794 			ifi->ifi_burstcount--;
1795 		break;
1796 	case IFI_STATE_TRANSITIVE:
1797 		ifi->ifi_burstcount--;
1798 		if (ifi->ifi_burstcount == 0) {
1799 			if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1800 				/* Initial burst finished. */
1801 				if (ifi->ifi_rainfo_trans != NULL)
1802 					ifi->ifi_rainfo_trans = NULL;
1803 			}
1804 
1805 			/* Remove burst RA information */
1806 			if (ifi->ifi_rainfo_trans != NULL) {
1807 				rm_rainfo(ifi->ifi_rainfo_trans);
1808 				ifi->ifi_rainfo_trans = NULL;
1809 			}
1810 
1811 			if (ifi->ifi_rainfo != NULL) {
1812 				/*
1813 				 * TRANSITIVE -> CONFIGURED
1814 				 *
1815 				 * After initial burst or transition from
1816 				 * one configuration to another,
1817 				 * ifi_rainfo always points to the next RA
1818 				 * information.
1819 				 */
1820 				ifi->ifi_state = IFI_STATE_CONFIGURED;
1821 				syslog(LOG_DEBUG,
1822 				    "<%s> ifname=%s marked as "
1823 				    "CONFIGURED.", __func__,
1824 				    ifi->ifi_ifname);
1825 			} else {
1826 				/*
1827 				 * TRANSITIVE -> UNCONFIGURED
1828 				 *
1829 				 * If ifi_rainfo points to NULL, this
1830 				 * interface is shutting down.
1831 				 *
1832 				 */
1833 				int error;
1834 
1835 				ifi->ifi_state = IFI_STATE_UNCONFIGURED;
1836 				syslog(LOG_DEBUG,
1837 				    "<%s> ifname=%s marked as "
1838 				    "UNCONFIGURED.", __func__,
1839 				    ifi->ifi_ifname);
1840 				error = sock_mc_leave(&sock,
1841 				    ifi->ifi_ifindex);
1842 				if (error)
1843 					exit(1);
1844 			}
1845 		}
1846 		break;
1847 	}
1848 }
1849 
1850 /* process RA timer */
1851 struct rtadvd_timer *
1852 ra_timeout(void *arg)
1853 {
1854 	struct ifinfo *ifi;
1855 
1856 	ifi = (struct ifinfo *)arg;
1857 	syslog(LOG_DEBUG, "<%s> RA timer on %s is expired",
1858 	    __func__, ifi->ifi_ifname);
1859 
1860 	ra_output(ifi);
1861 
1862 	return (ifi->ifi_ra_timer);
1863 }
1864 
1865 /* update RA timer */
1866 void
1867 ra_timer_update(void *arg, struct timespec *tm)
1868 {
1869 	uint16_t interval;
1870 	struct rainfo *rai;
1871 	struct ifinfo *ifi;
1872 
1873 	ifi = (struct ifinfo *)arg;
1874 	rai = ifi->ifi_rainfo;
1875 	interval = 0;
1876 
1877 	switch (ifi->ifi_state) {
1878 	case IFI_STATE_UNCONFIGURED:
1879 		return;
1880 		break;
1881 	case IFI_STATE_CONFIGURED:
1882 		/*
1883 		 * Whenever a multicast advertisement is sent from
1884 		 * an interface, the timer is reset to a
1885 		 * uniformly-distributed random value between the
1886 		 * interface's configured MinRtrAdvInterval and
1887 		 * MaxRtrAdvInterval (RFC4861 6.2.4).
1888 		 */
1889 		interval = rai->rai_mininterval;
1890 		interval += arc4random_uniform(rai->rai_maxinterval -
1891 		    rai->rai_mininterval);
1892 		break;
1893 	case IFI_STATE_TRANSITIVE:
1894 		/*
1895 		 * For the first few advertisements (up to
1896 		 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen
1897 		 * interval is greater than
1898 		 * MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer SHOULD be
1899 		 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.  (RFC
1900 		 * 4861 6.2.4)
1901 		 *
1902 		 * In such cases, the router SHOULD transmit one or more
1903 		 * (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final
1904 		 * multicast Router Advertisements on the interface with a
1905 		 * Router Lifetime field of zero.  (RFC 4861 6.2.5)
1906 		 */
1907 		interval = ifi->ifi_burstinterval;
1908 		break;
1909 	}
1910 
1911 	tm->tv_sec = interval;
1912 	tm->tv_nsec = 0;
1913 
1914 	syslog(LOG_DEBUG,
1915 	    "<%s> RA timer on %s is set to %ld:%ld",
1916 	    __func__, ifi->ifi_ifname,
1917 	    (long int)tm->tv_sec, (long int)tm->tv_nsec / 1000);
1918 
1919 	return;
1920 }
1921