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