171333Sitojun/*	$FreeBSD$	*/
2118968Sume/*	$KAME: rtadvd.c,v 1.82 2003/08/05 12:34:23 itojun Exp $	*/
362656Skris
455505Sshin/*
555505Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6224144Shrs * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
755505Sshin * All rights reserved.
8222732Shrs *
955505Sshin * Redistribution and use in source and binary forms, with or without
1055505Sshin * modification, are permitted provided that the following conditions
1155505Sshin * are met:
1255505Sshin * 1. Redistributions of source code must retain the above copyright
1355505Sshin *    notice, this list of conditions and the following disclaimer.
1455505Sshin * 2. Redistributions in binary form must reproduce the above copyright
1555505Sshin *    notice, this list of conditions and the following disclaimer in the
1655505Sshin *    documentation and/or other materials provided with the distribution.
1755505Sshin * 3. Neither the name of the project nor the names of its contributors
1855505Sshin *    may be used to endorse or promote products derived from this software
1955505Sshin *    without specific prior written permission.
20222732Shrs *
2155505Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2255505Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355505Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455505Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2555505Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655505Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755505Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855505Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955505Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055505Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155505Sshin * SUCH DAMAGE.
3255505Sshin */
3355505Sshin
3455505Sshin#include <sys/param.h>
35222732Shrs#include <sys/ioctl.h>
3655505Sshin#include <sys/socket.h>
3755505Sshin#include <sys/uio.h>
3878064Sume#include <sys/queue.h>
39224144Shrs#include <sys/stat.h>
40222732Shrs#include <sys/sysctl.h>
4155505Sshin
4255505Sshin#include <net/if.h>
43224144Shrs#include <net/if_types.h>
44222732Shrs#include <net/if_media.h>
45224144Shrs#include <net/if_dl.h>
4655505Sshin#include <net/route.h>
4755505Sshin#include <netinet/in.h>
4855505Sshin#include <netinet/ip6.h>
4955505Sshin#include <netinet6/ip6_var.h>
5055505Sshin#include <netinet/icmp6.h>
5155505Sshin
5255505Sshin#include <arpa/inet.h>
5355505Sshin
54222732Shrs#include <net/if_var.h>
55222732Shrs#include <netinet/in_var.h>
56222732Shrs#include <netinet6/nd6.h>
57222732Shrs
5855505Sshin#include <time.h>
5955505Sshin#include <unistd.h>
6055505Sshin#include <stdio.h>
6155505Sshin#include <err.h>
6255505Sshin#include <errno.h>
63224144Shrs#include <inttypes.h>
64216675Sdelphij#include <libutil.h>
65222732Shrs#include <netdb.h>
66224144Shrs#include <signal.h>
6755505Sshin#include <string.h>
6855505Sshin#include <stdlib.h>
6955505Sshin#include <syslog.h>
70118916Sume#include <poll.h>
71118916Sume
72224144Shrs#include "pathnames.h"
7355505Sshin#include "rtadvd.h"
74224144Shrs#include "if.h"
7555505Sshin#include "rrenum.h"
7655505Sshin#include "advcap.h"
77224144Shrs#include "timer_subr.h"
7855505Sshin#include "timer.h"
7955505Sshin#include "config.h"
80224144Shrs#include "control.h"
81224144Shrs#include "control_server.h"
8255505Sshin
83224144Shrs#define RTADV_TYPE2BITMASK(type) (0x1 << type)
84224144Shrs
8562656Skrisstruct msghdr rcvmhdr;
86224144Shrsstatic char *rcvcmsgbuf;
8762656Skrisstatic size_t rcvcmsgbuflen;
88224144Shrsstatic char *sndcmsgbuf = NULL;
8962656Skrisstatic size_t sndcmsgbuflen;
9062656Skrisstruct msghdr sndmhdr;
9162656Skrisstruct iovec rcviov[2];
9262656Skrisstruct iovec sndiov[2];
93118913Sumestruct sockaddr_in6 rcvfrom;
94222732Shrsstatic const char *pidfilename = _PATH_RTADVDPID;
95222732Shrsconst char *conffile = _PATH_RTADVDCONF;
96216675Sdelphijstatic struct pidfh *pfh;
97253058Shrsstatic int dflag, sflag;
98224144Shrsstatic int wait_shutdown;
9955505Sshin
100224144Shrs#define	PFD_RAWSOCK	0
101224144Shrs#define	PFD_RTSOCK	1
102224144Shrs#define	PFD_CSOCK	2
103224144Shrs#define	PFD_MAX		3
104224144Shrs
105222732Shrsstruct railist_head_t railist =
106222732Shrs    TAILQ_HEAD_INITIALIZER(railist);
107224144Shrsstruct ifilist_head_t ifilist =
108224144Shrs    TAILQ_HEAD_INITIALIZER(ifilist);
10955505Sshin
11062656Skrisstruct nd_optlist {
111222732Shrs	TAILQ_ENTRY(nd_optlist)	nol_next;
112222732Shrs	struct nd_opt_hdr *nol_opt;
11355505Sshin};
114222732Shrsunion nd_opt {
115222732Shrs	struct nd_opt_hdr *opt_array[9];
11655505Sshin	struct {
11762656Skris		struct nd_opt_hdr *zero;
11862656Skris		struct nd_opt_hdr *src_lladdr;
11962656Skris		struct nd_opt_hdr *tgt_lladdr;
12062656Skris		struct nd_opt_prefix_info *pi;
12162656Skris		struct nd_opt_rd_hdr *rh;
12262656Skris		struct nd_opt_mtu *mtu;
123222732Shrs		TAILQ_HEAD(, nd_optlist) opt_list;
12455505Sshin	} nd_opt_each;
12555505Sshin};
126222732Shrs#define opt_src_lladdr	nd_opt_each.src_lladdr
127222732Shrs#define opt_tgt_lladdr	nd_opt_each.tgt_lladdr
128222732Shrs#define opt_pi		nd_opt_each.pi
129222732Shrs#define opt_rh		nd_opt_each.rh
130222732Shrs#define opt_mtu		nd_opt_each.mtu
131222732Shrs#define opt_list	nd_opt_each.opt_list
13255505Sshin
133222732Shrs#define NDOPT_FLAG_SRCLINKADDR	(1 << 0)
134222732Shrs#define NDOPT_FLAG_TGTLINKADDR	(1 << 1)
135222732Shrs#define NDOPT_FLAG_PREFIXINFO	(1 << 2)
136222732Shrs#define NDOPT_FLAG_RDHDR	(1 << 3)
137222732Shrs#define NDOPT_FLAG_MTU		(1 << 4)
138222732Shrs#define NDOPT_FLAG_RDNSS	(1 << 5)
139222732Shrs#define NDOPT_FLAG_DNSSL	(1 << 6)
14055505Sshin
141253058Shrsstatic uint32_t ndopt_flags[] = {
142222732Shrs	[ND_OPT_SOURCE_LINKADDR]	= NDOPT_FLAG_SRCLINKADDR,
143222732Shrs	[ND_OPT_TARGET_LINKADDR]	= NDOPT_FLAG_TGTLINKADDR,
144222732Shrs	[ND_OPT_PREFIX_INFORMATION]	= NDOPT_FLAG_PREFIXINFO,
145222732Shrs	[ND_OPT_REDIRECTED_HEADER]	= NDOPT_FLAG_RDHDR,
146222732Shrs	[ND_OPT_MTU]			= NDOPT_FLAG_MTU,
147222732Shrs	[ND_OPT_RDNSS]			= NDOPT_FLAG_RDNSS,
148222732Shrs	[ND_OPT_DNSSL]			= NDOPT_FLAG_DNSSL,
14955505Sshin};
15055505Sshin
151224144Shrsstatic void	rtadvd_shutdown(void);
152224144Shrsstatic void	sock_open(struct sockinfo *);
153224144Shrsstatic void	rtsock_open(struct sockinfo *);
154224144Shrsstatic void	rtadvd_input(struct sockinfo *);
155222732Shrsstatic void	rs_input(int, struct nd_router_solicit *,
156222732Shrs		    struct in6_pktinfo *, struct sockaddr_in6 *);
157222732Shrsstatic void	ra_input(int, struct nd_router_advert *,
158222732Shrs		    struct in6_pktinfo *, struct sockaddr_in6 *);
159222732Shrsstatic int	prefix_check(struct nd_opt_prefix_info *, struct rainfo *,
160222732Shrs		    struct sockaddr_in6 *);
161222732Shrsstatic int	nd6_options(struct nd_opt_hdr *, int,
162224144Shrs		    union nd_opt *, uint32_t);
163222732Shrsstatic void	free_ndopts(union nd_opt *);
164224144Shrsstatic void	rtmsg_input(struct sockinfo *);
165224144Shrsstatic void	set_short_delay(struct ifinfo *);
166222732Shrsstatic int	check_accept_rtadv(int);
167222732Shrs
168247270Sdesstatic void
169247270Sdesusage(void)
170247270Sdes{
171247270Sdes
172247270Sdes	fprintf(stderr, "usage: rtadvd [-dDfRs] "
173247270Sdes	    "[-c configfile] [-C ctlsock] [-M ifname] [-p pidfile]\n");
174247270Sdes	exit(1);
175247270Sdes}
176247270Sdes
17755505Sshinint
178222732Shrsmain(int argc, char *argv[])
17955505Sshin{
180224144Shrs	struct pollfd set[PFD_MAX];
181253970Shrs	struct timespec *timeout;
18255505Sshin	int i, ch;
183118959Sume	int fflag = 0, logopt;
184224144Shrs	int error;
185216675Sdelphij	pid_t pid, otherpid;
18655505Sshin
18755505Sshin	/* get command line options and arguments */
188247270Sdes	while ((ch = getopt(argc, argv, "c:C:dDfhM:p:Rs")) != -1) {
18997712Sume		switch (ch) {
19097712Sume		case 'c':
19197712Sume			conffile = optarg;
19297712Sume			break;
193224144Shrs		case 'C':
194224144Shrs			ctrlsock.si_name = optarg;
195224144Shrs			break;
19697712Sume		case 'd':
197222732Shrs			dflag++;
19897712Sume			break;
19997712Sume		case 'D':
200225519Shrs			dflag += 3;
20197712Sume			break;
20297712Sume		case 'f':
20397712Sume			fflag = 1;
20497712Sume			break;
20578064Sume		case 'M':
20678064Sume			mcastif = optarg;
20778064Sume			break;
20897712Sume		case 'R':
20997712Sume			fprintf(stderr, "rtadvd: "
21097712Sume				"the -R option is currently ignored.\n");
21197712Sume			/* accept_rr = 1; */
21297712Sume			/* run anyway... */
21397712Sume			break;
21497712Sume		case 's':
21597712Sume			sflag = 1;
21697712Sume			break;
217216675Sdelphij		case 'p':
218216675Sdelphij			pidfilename = optarg;
219216675Sdelphij			break;
220247270Sdes		default:
221247270Sdes			usage();
22255505Sshin		}
22355505Sshin	}
22455505Sshin	argc -= optind;
22555505Sshin	argv += optind;
22655505Sshin
227118959Sume	logopt = LOG_NDELAY | LOG_PID;
228118959Sume	if (fflag)
229118959Sume		logopt |= LOG_PERROR;
230118959Sume	openlog("rtadvd", logopt, LOG_DAEMON);
231118959Sume
23255505Sshin	/* set log level */
233225519Shrs	if (dflag > 2)
234222732Shrs		(void)setlogmask(LOG_UPTO(LOG_DEBUG));
235225519Shrs	else if (dflag > 1)
236225519Shrs		(void)setlogmask(LOG_UPTO(LOG_INFO));
237222732Shrs	else if (dflag > 0)
238225519Shrs		(void)setlogmask(LOG_UPTO(LOG_NOTICE));
239222732Shrs	else
24055505Sshin		(void)setlogmask(LOG_UPTO(LOG_ERR));
24155505Sshin
24255505Sshin	/* timer initialization */
24355505Sshin	rtadvd_timer_init();
24455505Sshin
245216675Sdelphij	pfh = pidfile_open(pidfilename, 0600, &otherpid);
246216675Sdelphij	if (pfh == NULL) {
247216675Sdelphij		if (errno == EEXIST)
248216675Sdelphij			errx(1, "%s already running, pid: %d",
249216675Sdelphij			    getprogname(), otherpid);
250216675Sdelphij		syslog(LOG_ERR,
251225519Shrs		    "failed to open the pid file %s, run anyway.",
252225519Shrs		    pidfilename);
253216675Sdelphij	}
25455505Sshin	if (!fflag)
25555505Sshin		daemon(1, 0);
25655505Sshin
257224144Shrs	sock_open(&sock);
258118920Sume
259224144Shrs	update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
260224144Shrs	for (i = 0; i < argc; i++)
261224144Shrs		update_persist_ifinfo(&ifilist, argv[i]);
262224144Shrs
263224144Shrs	csock_open(&ctrlsock, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
264224144Shrs	if (ctrlsock.si_fd == -1) {
265225519Shrs		syslog(LOG_ERR, "cannot open control socket: %s",
266225519Shrs		    strerror(errno));
267224144Shrs		exit(1);
268224144Shrs	}
269224144Shrs
27062656Skris	/* record the current PID */
27162656Skris	pid = getpid();
272216675Sdelphij	pidfile_write(pfh);
27362656Skris
274224144Shrs	set[PFD_RAWSOCK].fd = sock.si_fd;
275224144Shrs	set[PFD_RAWSOCK].events = POLLIN;
276118916Sume	if (sflag == 0) {
277224144Shrs		rtsock_open(&rtsock);
278224144Shrs		set[PFD_RTSOCK].fd = rtsock.si_fd;
279224144Shrs		set[PFD_RTSOCK].events = POLLIN;
280118916Sume	} else
281224144Shrs		set[PFD_RTSOCK].fd = -1;
282224144Shrs	set[PFD_CSOCK].fd = ctrlsock.si_fd;
283224144Shrs	set[PFD_CSOCK].events = POLLIN;
284224144Shrs	signal(SIGTERM, set_do_shutdown);
285224144Shrs	signal(SIGINT, set_do_shutdown);
286224144Shrs	signal(SIGHUP, set_do_reload);
28755505Sshin
288224144Shrs	error = csock_listen(&ctrlsock);
289224144Shrs	if (error) {
290225519Shrs		syslog(LOG_ERR, "cannot listen control socket: %s",
291225519Shrs		    strerror(errno));
292224144Shrs		exit(1);
293118911Sume	}
29462656Skris
295224144Shrs	/* load configuration file */
296224144Shrs	set_do_reload(0);
297224144Shrs
29855505Sshin	while (1) {
299224144Shrs		if (is_do_shutdown())
300224144Shrs			rtadvd_shutdown();
30162656Skris
302224144Shrs		if (is_do_reload()) {
303224144Shrs			loadconfig_ifname(reload_ifname());
304224144Shrs			if (reload_ifname() == NULL)
305224144Shrs				syslog(LOG_INFO,
306224144Shrs				    "configuration file reloaded.");
307224144Shrs			else
308224144Shrs				syslog(LOG_INFO,
309224144Shrs				    "configuration file for %s reloaded.",
310224144Shrs				    reload_ifname());
311224144Shrs			reset_do_reload();
31278064Sume		}
31378064Sume
314224144Shrs		/* timeout handler update for active interfaces */
315224144Shrs		rtadvd_update_timeout_handler();
316222972Shrs
31755505Sshin		/* timer expiration check and reset the timer */
31855505Sshin		timeout = rtadvd_check_timer();
31955505Sshin
32062656Skris		if (timeout != NULL) {
32162656Skris			syslog(LOG_DEBUG,
32297712Sume			    "<%s> set timer to %ld:%ld. waiting for "
323118660Sume			    "inputs or timeout", __func__,
32497712Sume			    (long int)timeout->tv_sec,
325253970Shrs			    (long int)timeout->tv_nsec / 1000);
32662656Skris		} else {
32762656Skris			syslog(LOG_DEBUG,
32897712Sume			    "<%s> there's no timer. waiting for inputs",
329118660Sume			    __func__);
33062656Skris		}
331224144Shrs		if ((i = poll(set, sizeof(set)/sizeof(set[0]),
332224144Shrs			    timeout ? (timeout->tv_sec * 1000 +
333253970Shrs				timeout->tv_nsec / 1000 / 1000) : INFTIM)) < 0) {
334225519Shrs
335225519Shrs			/* EINTR would occur if a signal was delivered */
33662656Skris			if (errno != EINTR)
337225519Shrs				syslog(LOG_ERR, "poll() failed: %s",
338225519Shrs				    strerror(errno));
33955505Sshin			continue;
34055505Sshin		}
34155505Sshin		if (i == 0)	/* timeout */
34255505Sshin			continue;
343224144Shrs		if (rtsock.si_fd != -1 && set[PFD_RTSOCK].revents & POLLIN)
344224144Shrs			rtmsg_input(&rtsock);
34555505Sshin
346224144Shrs		if (set[PFD_RAWSOCK].revents & POLLIN)
347224144Shrs			rtadvd_input(&sock);
348222732Shrs
349224144Shrs		if (set[PFD_CSOCK].revents & POLLIN) {
350224144Shrs			int fd;
351222732Shrs
352224144Shrs			fd = csock_accept(&ctrlsock);
353224144Shrs			if (fd == -1)
354225519Shrs				syslog(LOG_ERR,
355225519Shrs				    "cannot accept() control socket: %s",
356225519Shrs				    strerror(errno));
357224144Shrs			else {
358225519Shrs				cm_handler_server(fd);
359224144Shrs				close(fd);
360224144Shrs			}
361224144Shrs		}
362224144Shrs	}
363224144Shrs	exit(0);		/* NOTREACHED */
36462656Skris}
36562656Skris
36662656Skrisstatic void
367224144Shrsrtadvd_shutdown(void)
368222972Shrs{
369224144Shrs	struct ifinfo *ifi;
370224144Shrs	struct rainfo *rai;
371224144Shrs	struct rdnss *rdn;
372224144Shrs	struct dnssl *dns;
373222972Shrs
374224144Shrs	if (wait_shutdown) {
375224144Shrs		syslog(LOG_INFO,
376225519Shrs		    "waiting expiration of the all RA timers.");
377222972Shrs
378224144Shrs		TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
379225683Shrs			/*
380225683Shrs			 * Ignore !IFF_UP interfaces in waiting for shutdown.
381225683Shrs			 */
382225683Shrs			if (!(ifi->ifi_flags & IFF_UP) &&
383225683Shrs			    ifi->ifi_ra_timer != NULL) {
384225683Shrs				ifi->ifi_state = IFI_STATE_UNCONFIGURED;
385225683Shrs				rtadvd_remove_timer(ifi->ifi_ra_timer);
386225683Shrs				ifi->ifi_ra_timer = NULL;
387225683Shrs				syslog(LOG_DEBUG, "<%s> %s(idx=%d) is down. "
388225683Shrs				    "Timer removed and marked as UNCONFIGURED.",
389225683Shrs				     __func__, ifi->ifi_ifname,
390225683Shrs				    ifi->ifi_ifindex);
391225683Shrs			}
392225683Shrs		}
393225683Shrs		TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
394224144Shrs			if (ifi->ifi_ra_timer != NULL)
395224144Shrs				break;
396224144Shrs		}
397224144Shrs		if (ifi == NULL) {
398225519Shrs			syslog(LOG_NOTICE, "gracefully terminated.");
399224144Shrs			exit(0);
400224144Shrs		}
401222732Shrs
402224144Shrs		sleep(1);
403224144Shrs		return;
404224144Shrs	}
40578064Sume
406225519Shrs	syslog(LOG_DEBUG, "<%s> cease to be an advertising router",
407222732Shrs	    __func__);
40862656Skris
409224144Shrs	wait_shutdown = 1;
410224144Shrs
411222732Shrs	TAILQ_FOREACH(rai, &railist, rai_next) {
412222732Shrs		rai->rai_lifetime = 0;
413222732Shrs		TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next)
414222732Shrs			rdn->rd_ltime = 0;
415222732Shrs		TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next)
416222732Shrs			dns->dn_ltime = 0;
41762656Skris	}
418224144Shrs	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
419224144Shrs		if (!ifi->ifi_persist)
420224144Shrs			continue;
421224144Shrs		if (ifi->ifi_state == IFI_STATE_UNCONFIGURED)
422224144Shrs			continue;
423224144Shrs		if (ifi->ifi_ra_timer == NULL)
424224144Shrs			continue;
425225519Shrs		if (ifi->ifi_ra_lastsent.tv_sec == 0 &&
426253970Shrs		    ifi->ifi_ra_lastsent.tv_nsec == 0 &&
427225519Shrs		    ifi->ifi_ra_timer != NULL) {
428225519Shrs			/*
429225519Shrs			 * When RA configured but never sent,
430225519Shrs			 * ignore the IF immediately.
431225519Shrs			 */
432225519Shrs			rtadvd_remove_timer(ifi->ifi_ra_timer);
433225519Shrs			ifi->ifi_ra_timer = NULL;
434225519Shrs			ifi->ifi_state = IFI_STATE_UNCONFIGURED;
435225519Shrs			continue;
436225519Shrs		}
437224144Shrs
438224144Shrs		ifi->ifi_state = IFI_STATE_TRANSITIVE;
439224144Shrs
440224144Shrs		/* Mark as the shut-down state. */
441224144Shrs		ifi->ifi_rainfo_trans = ifi->ifi_rainfo;
442224144Shrs		ifi->ifi_rainfo = NULL;
443224144Shrs
444224144Shrs		ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
445224144Shrs		ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
446224144Shrs
447224144Shrs		ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
448224144Shrs		rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
449224144Shrs		    ifi->ifi_ra_timer);
45062656Skris	}
451225519Shrs	syslog(LOG_NOTICE, "final RA transmission started.");
452224144Shrs
453216675Sdelphij	pidfile_remove(pfh);
454224144Shrs	csock_close(&ctrlsock);
45562656Skris}
45662656Skris
45762656Skrisstatic void
458224144Shrsrtmsg_input(struct sockinfo *s)
45955505Sshin{
46062656Skris	int n, type, ifindex = 0, plen;
46155505Sshin	size_t len;
46255505Sshin	char msg[2048], *next, *lim;
463224144Shrs	char ifname[IFNAMSIZ];
464222732Shrs	struct if_announcemsghdr *ifan;
465224144Shrs	struct rt_msghdr *rtm;
466222732Shrs	struct prefix *pfx;
46755505Sshin	struct rainfo *rai;
46855505Sshin	struct in6_addr *addr;
469224144Shrs	struct ifinfo *ifi;
47055505Sshin	char addrbuf[INET6_ADDRSTRLEN];
471118968Sume	int prefixchange = 0;
47255505Sshin
473224144Shrs	if (s == NULL) {
474224144Shrs		syslog(LOG_ERR, "<%s> internal error", __func__);
475224144Shrs		exit(1);
476224144Shrs	}
477224144Shrs	n = read(s->si_fd, msg, sizeof(msg));
478224144Shrs	rtm = (struct rt_msghdr *)msg;
479222732Shrs	syslog(LOG_DEBUG, "<%s> received a routing message "
480224144Shrs	    "(type = %d, len = %d)", __func__, rtm->rtm_type, n);
481222732Shrs
482224144Shrs	if (n > rtm->rtm_msglen) {
48355505Sshin		/*
484222732Shrs		 * This usually won't happen for messages received on
48562656Skris		 * a routing socket.
48655505Sshin		 */
487222732Shrs		syslog(LOG_DEBUG,
488222732Shrs		    "<%s> received data length is larger than "
489222732Shrs		    "1st routing message len. multiple messages? "
490222732Shrs		    "read %d bytes, but 1st msg len = %d",
491224144Shrs		    __func__, n, rtm->rtm_msglen);
49262656Skris#if 0
49362656Skris		/* adjust length */
494224144Shrs		n = rtm->rtm_msglen;
49562656Skris#endif
49655505Sshin	}
49755505Sshin
49855505Sshin	lim = msg + n;
49955505Sshin	for (next = msg; next < lim; next += len) {
50062656Skris		int oldifflags;
50162656Skris
50255505Sshin		next = get_next_msg(next, lim, 0, &len,
503222732Shrs		    RTADV_TYPE2BITMASK(RTM_ADD) |
504222732Shrs		    RTADV_TYPE2BITMASK(RTM_DELETE) |
505222732Shrs		    RTADV_TYPE2BITMASK(RTM_NEWADDR) |
506222732Shrs		    RTADV_TYPE2BITMASK(RTM_DELADDR) |
507222732Shrs		    RTADV_TYPE2BITMASK(RTM_IFINFO) |
508222732Shrs		    RTADV_TYPE2BITMASK(RTM_IFANNOUNCE));
50955505Sshin		if (len == 0)
51055505Sshin			break;
511224144Shrs		type = ((struct rt_msghdr *)next)->rtm_type;
51255505Sshin		switch (type) {
51355505Sshin		case RTM_ADD:
51455505Sshin		case RTM_DELETE:
51555505Sshin			ifindex = get_rtm_ifindex(next);
51655505Sshin			break;
51755505Sshin		case RTM_NEWADDR:
51855505Sshin		case RTM_DELADDR:
519224144Shrs			ifindex = (int)((struct ifa_msghdr *)next)->ifam_index;
52055505Sshin			break;
52155505Sshin		case RTM_IFINFO:
522224144Shrs			ifindex = (int)((struct if_msghdr *)next)->ifm_index;
52355505Sshin			break;
524222732Shrs		case RTM_IFANNOUNCE:
525222732Shrs			ifan = (struct if_announcemsghdr *)next;
526222732Shrs			switch (ifan->ifan_what) {
527222732Shrs			case IFAN_ARRIVAL:
528222732Shrs			case IFAN_DEPARTURE:
529222732Shrs				break;
530222732Shrs			default:
53155505Sshin				syslog(LOG_DEBUG,
532222732Shrs				    "<%s:%d> unknown ifan msg (ifan_what=%d)",
533222732Shrs				   __func__, __LINE__, ifan->ifan_what);
534222732Shrs				continue;
53555505Sshin			}
536222732Shrs
537225519Shrs			syslog(LOG_DEBUG, "<%s>: if_announcemsg (idx=%d:%d)",
538222732Shrs			       __func__, ifan->ifan_index, ifan->ifan_what);
539222732Shrs			switch (ifan->ifan_what) {
540222732Shrs			case IFAN_ARRIVAL:
541225519Shrs				syslog(LOG_NOTICE,
542225519Shrs				    "interface added (idx=%d)",
543225519Shrs				    ifan->ifan_index);
544224144Shrs				update_ifinfo(&ifilist, ifan->ifan_index);
545224144Shrs				loadconfig_index(ifan->ifan_index);
546222732Shrs				break;
547222732Shrs			case IFAN_DEPARTURE:
548225519Shrs				syslog(LOG_NOTICE,
549225519Shrs				    "interface removed (idx=%d)",
550225519Shrs				    ifan->ifan_index);
551224144Shrs				rm_ifinfo_index(ifan->ifan_index);
552224144Shrs
553224144Shrs				/* Clear ifi_ifindex */
554224144Shrs				TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
555224144Shrs					if (ifi->ifi_ifindex
556224144Shrs					    == ifan->ifan_index) {
557224144Shrs						ifi->ifi_ifindex = 0;
558224144Shrs						break;
559224144Shrs					}
560224144Shrs				}
561224144Shrs				update_ifinfo(&ifilist, ifan->ifan_index);
562222732Shrs				break;
563222732Shrs			}
56462656Skris			continue;
565222732Shrs		default:
566222732Shrs			/* should not reach here */
567222732Shrs			syslog(LOG_DEBUG,
568222732Shrs			       "<%s:%d> unknown rtmsg %d on %s",
569222732Shrs			       __func__, __LINE__, type,
570222732Shrs			       if_indextoname(ifindex, ifname));
571222732Shrs			continue;
57255505Sshin		}
573224144Shrs		ifi = if_indextoifinfo(ifindex);
574224144Shrs		if (ifi == NULL) {
575222732Shrs			syslog(LOG_DEBUG,
576224144Shrs			    "<%s> ifinfo not found for idx=%d.  Why?",
577224144Shrs			    __func__, ifindex);
57862656Skris			continue;
57955505Sshin		}
580224144Shrs		rai = ifi->ifi_rainfo;
581224144Shrs		if (rai == NULL) {
582224144Shrs			syslog(LOG_DEBUG,
583224144Shrs			    "<%s> route changed on "
584224144Shrs			    "non advertising interface(%s)",
585224144Shrs			    __func__, ifi->ifi_ifname);
586224144Shrs			continue;
587224144Shrs		}
58855505Sshin
589224144Shrs		oldifflags = ifi->ifi_flags;
590224144Shrs		/* init ifflags because it may have changed */
591224144Shrs		update_ifinfo(&ifilist, ifindex);
592224144Shrs
59397712Sume		switch (type) {
59497712Sume		case RTM_ADD:
59597712Sume			if (sflag)
59697712Sume				break;	/* we aren't interested in prefixes  */
59762656Skris
59897712Sume			addr = get_addr(msg);
59997712Sume			plen = get_prefixlen(msg);
60097712Sume			/* sanity check for plen */
60197712Sume			/* as RFC2373, prefixlen is at least 4 */
60297712Sume			if (plen < 4 || plen > 127) {
60355505Sshin				syslog(LOG_INFO, "<%s> new interface route's"
60497712Sume				    "plen %d is invalid for a prefix",
605118660Sume				    __func__, plen);
60662656Skris				break;
60797712Sume			}
608222732Shrs			pfx = find_prefix(rai, addr, plen);
609222732Shrs			if (pfx) {
610222732Shrs				if (pfx->pfx_timer) {
61198172Sume					/*
61298172Sume					 * If the prefix has been invalidated,
61398172Sume					 * make it available again.
61498172Sume					 */
615222732Shrs					update_prefix(pfx);
616118968Sume					prefixchange = 1;
617222732Shrs				} else
61897712Sume					syslog(LOG_DEBUG,
61997712Sume					    "<%s> new prefix(%s/%d) "
62097712Sume					    "added on %s, "
62197712Sume					    "but it was already in list",
622118660Sume					    __func__,
62397712Sume					    inet_ntop(AF_INET6, addr,
624222732Shrs						(char *)addrbuf,
625222732Shrs						sizeof(addrbuf)),
626224144Shrs					    plen, ifi->ifi_ifname);
62797712Sume				break;
62897712Sume			}
62997712Sume			make_prefix(rai, ifindex, addr, plen);
630118968Sume			prefixchange = 1;
63197712Sume			break;
63297712Sume		case RTM_DELETE:
63397712Sume			if (sflag)
63497712Sume				break;
63562656Skris
63697712Sume			addr = get_addr(msg);
63797712Sume			plen = get_prefixlen(msg);
63897712Sume			/* sanity check for plen */
63997712Sume			/* as RFC2373, prefixlen is at least 4 */
64097712Sume			if (plen < 4 || plen > 127) {
64197712Sume				syslog(LOG_INFO,
64297712Sume				    "<%s> deleted interface route's "
64397712Sume				    "plen %d is invalid for a prefix",
644118660Sume				    __func__, plen);
64562656Skris				break;
64697712Sume			}
647222732Shrs			pfx = find_prefix(rai, addr, plen);
648222732Shrs			if (pfx == NULL) {
649222732Shrs				syslog(LOG_DEBUG,
650222732Shrs				    "<%s> prefix(%s/%d) was deleted on %s, "
651222732Shrs				    "but it was not in list",
652222732Shrs				    __func__, inet_ntop(AF_INET6, addr,
653222732Shrs					(char *)addrbuf, sizeof(addrbuf)),
654224144Shrs					plen, ifi->ifi_ifname);
65597712Sume				break;
65697712Sume			}
657222732Shrs			invalidate_prefix(pfx);
658118968Sume			prefixchange = 1;
65997712Sume			break;
66055505Sshin		case RTM_NEWADDR:
66155505Sshin		case RTM_DELADDR:
66255505Sshin		case RTM_IFINFO:
66397712Sume			break;
66455505Sshin		default:
66555505Sshin			/* should not reach here */
666222732Shrs			syslog(LOG_DEBUG,
667222732Shrs			    "<%s:%d> unknown rtmsg %d on %s",
668222732Shrs			    __func__, __LINE__, type,
669222732Shrs			    if_indextoname(ifindex, ifname));
67055505Sshin			return;
67155505Sshin		}
67262656Skris
67362656Skris		/* check if an interface flag is changed */
674118664Sume		if ((oldifflags & IFF_UP) && /* UP to DOWN */
675224144Shrs		    !(ifi->ifi_flags & IFF_UP)) {
676225519Shrs			syslog(LOG_NOTICE,
677225519Shrs			    "<interface %s becomes down. stop timer.",
678225519Shrs			    ifi->ifi_ifname);
679224144Shrs			rtadvd_remove_timer(ifi->ifi_ra_timer);
680224144Shrs			ifi->ifi_ra_timer = NULL;
681118664Sume		} else if (!(oldifflags & IFF_UP) && /* DOWN to UP */
682224144Shrs		    (ifi->ifi_flags & IFF_UP)) {
683225519Shrs			syslog(LOG_NOTICE,
684225519Shrs			    "interface %s becomes up. restart timer.",
685225519Shrs			    ifi->ifi_ifname);
68662656Skris
687224144Shrs			ifi->ifi_state = IFI_STATE_TRANSITIVE;
688224144Shrs			ifi->ifi_burstcount =
689224144Shrs			    MAX_INITIAL_RTR_ADVERTISEMENTS;
690224144Shrs			ifi->ifi_burstinterval =
691224144Shrs			    MAX_INITIAL_RTR_ADVERT_INTERVAL;
692224144Shrs
693224144Shrs			ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
694224144Shrs			    ra_timer_update, ifi, ifi);
695224144Shrs			ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
696224144Shrs			rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
697224144Shrs			    ifi->ifi_ra_timer);
698118968Sume		} else if (prefixchange &&
699224144Shrs		    (ifi->ifi_flags & IFF_UP)) {
700118968Sume			/*
701118968Sume			 * An advertised prefix has been added or invalidated.
702118968Sume			 * Will notice the change in a short delay.
703118968Sume			 */
704224144Shrs			set_short_delay(ifi);
70562656Skris		}
70655505Sshin	}
70755505Sshin
70855505Sshin	return;
70955505Sshin}
71055505Sshin
71155505Sshinvoid
712224144Shrsrtadvd_input(struct sockinfo *s)
71355505Sshin{
714222732Shrs	ssize_t i;
71555505Sshin	int *hlimp = NULL;
71662656Skris#ifdef OLDRAWSOCKET
71762656Skris	struct ip6_hdr *ip;
718222732Shrs#endif
71955505Sshin	struct icmp6_hdr *icp;
72055505Sshin	int ifindex = 0;
72155505Sshin	struct cmsghdr *cm;
72255505Sshin	struct in6_pktinfo *pi = NULL;
723224144Shrs	char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
72455505Sshin	struct in6_addr dst = in6addr_any;
725224144Shrs	struct ifinfo *ifi;
72655505Sshin
727224144Shrs	syslog(LOG_DEBUG, "<%s> enter", __func__);
728224144Shrs
729224144Shrs	if (s == NULL) {
730224144Shrs		syslog(LOG_ERR, "<%s> internal error", __func__);
731224144Shrs		exit(1);
732224144Shrs	}
73355505Sshin	/*
73455505Sshin	 * Get message. We reset msg_controllen since the field could
73555505Sshin	 * be modified if we had received a message before setting
73655505Sshin	 * receive options.
73755505Sshin	 */
73862656Skris	rcvmhdr.msg_controllen = rcvcmsgbuflen;
739224144Shrs	if ((i = recvmsg(s->si_fd, &rcvmhdr, 0)) < 0)
74055505Sshin		return;
74155505Sshin
74255505Sshin	/* extract optional information via Advanced API */
74355505Sshin	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
74455505Sshin	     cm;
74555505Sshin	     cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
74655505Sshin		if (cm->cmsg_level == IPPROTO_IPV6 &&
74755505Sshin		    cm->cmsg_type == IPV6_PKTINFO &&
74855505Sshin		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
74955505Sshin			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
75055505Sshin			ifindex = pi->ipi6_ifindex;
75155505Sshin			dst = pi->ipi6_addr;
75255505Sshin		}
75355505Sshin		if (cm->cmsg_level == IPPROTO_IPV6 &&
75455505Sshin		    cm->cmsg_type == IPV6_HOPLIMIT &&
75555505Sshin		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
75655505Sshin			hlimp = (int *)CMSG_DATA(cm);
75755505Sshin	}
75855505Sshin	if (ifindex == 0) {
759225519Shrs		syslog(LOG_ERR, "failed to get receiving interface");
76055505Sshin		return;
76155505Sshin	}
76255505Sshin	if (hlimp == NULL) {
763225519Shrs		syslog(LOG_ERR, "failed to get receiving hop limit");
76455505Sshin		return;
76555505Sshin	}
76655505Sshin
76762656Skris	/*
768219184Sbz	 * If we happen to receive data on an interface which is now gone
769219184Sbz	 * or down, just discard the data.
77062656Skris	 */
771224144Shrs	ifi = if_indextoifinfo(pi->ipi6_ifindex);
772224144Shrs	if (ifi == NULL || !(ifi->ifi_flags & IFF_UP)) {
77362656Skris		syslog(LOG_INFO,
774222732Shrs		    "<%s> received data on a disabled interface (%s)",
775222732Shrs		    __func__,
776224144Shrs		    (ifi == NULL) ? "[gone]" : ifi->ifi_ifname);
77762656Skris		return;
77862656Skris	}
77962656Skris
78062656Skris#ifdef OLDRAWSOCKET
781222732Shrs	if ((size_t)i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
78262656Skris		syslog(LOG_ERR,
783225519Shrs		    "packet size(%d) is too short", i);
78462656Skris		return;
78562656Skris	}
78662656Skris
78762656Skris	ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
78862656Skris	icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
78962656Skris#else
790222732Shrs	if ((size_t)i < sizeof(struct icmp6_hdr)) {
791225519Shrs		syslog(LOG_ERR, "packet size(%zd) is too short", i);
79255505Sshin		return;
79355505Sshin	}
79455505Sshin
79555505Sshin	icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
79662656Skris#endif
79755505Sshin
79897712Sume	switch (icp->icmp6_type) {
79997712Sume	case ND_ROUTER_SOLICIT:
80097712Sume		/*
801222732Shrs		 * Message verification - RFC 4861 6.1.1
80297712Sume		 * XXX: these checks must be done in the kernel as well,
80397712Sume		 *      but we can't completely rely on them.
80497712Sume		 */
80597712Sume		if (*hlimp != 255) {
80697712Sume			syslog(LOG_NOTICE,
807225519Shrs			    "RS with invalid hop limit(%d) "
80897712Sume			    "received from %s on %s",
809225519Shrs			    *hlimp,
810118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
811222732Shrs			    sizeof(ntopbuf)),
81297712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
81397712Sume			return;
81497712Sume		}
81597712Sume		if (icp->icmp6_code) {
81697712Sume			syslog(LOG_NOTICE,
817225519Shrs			    "RS with invalid ICMP6 code(%d) "
81897712Sume			    "received from %s on %s",
819225519Shrs			    icp->icmp6_code,
820118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
821222732Shrs			    sizeof(ntopbuf)),
82297712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
82397712Sume			return;
82497712Sume		}
825222732Shrs		if ((size_t)i < sizeof(struct nd_router_solicit)) {
82697712Sume			syslog(LOG_NOTICE,
827225519Shrs			    "RS from %s on %s does not have enough "
828222743Shrs			    "length (len = %zd)",
829118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
830222732Shrs			    sizeof(ntopbuf)),
83197712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
83297712Sume			return;
83397712Sume		}
834118913Sume		rs_input(i, (struct nd_router_solicit *)icp, pi, &rcvfrom);
83597712Sume		break;
83697712Sume	case ND_ROUTER_ADVERT:
83797712Sume		/*
838222732Shrs		 * Message verification - RFC 4861 6.1.2
839222732Shrs		 * XXX: there's the same dilemma as above...
84097712Sume		 */
841222732Shrs		if (!IN6_IS_ADDR_LINKLOCAL(&rcvfrom.sin6_addr)) {
842222732Shrs			syslog(LOG_NOTICE,
843228990Suqs			    "RA with non-linklocal source address "
844222732Shrs			    "received from %s on %s",
845225519Shrs			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr,
846222732Shrs			    ntopbuf, sizeof(ntopbuf)),
847222732Shrs			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
848222732Shrs			return;
849222732Shrs		}
85097712Sume		if (*hlimp != 255) {
85197712Sume			syslog(LOG_NOTICE,
852225519Shrs			    "RA with invalid hop limit(%d) "
85397712Sume			    "received from %s on %s",
854225519Shrs			    *hlimp,
855118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
856222732Shrs			    sizeof(ntopbuf)),
85797712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
85897712Sume			return;
85997712Sume		}
86097712Sume		if (icp->icmp6_code) {
86197712Sume			syslog(LOG_NOTICE,
862225519Shrs			    "RA with invalid ICMP6 code(%d) "
86397712Sume			    "received from %s on %s",
864225519Shrs			    icp->icmp6_code,
865118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
866222732Shrs			    sizeof(ntopbuf)),
86797712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
86897712Sume			return;
86997712Sume		}
870222732Shrs		if ((size_t)i < sizeof(struct nd_router_advert)) {
87197712Sume			syslog(LOG_NOTICE,
872225519Shrs			    "RA from %s on %s does not have enough "
873222743Shrs			    "length (len = %zd)",
874118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
875222732Shrs			    sizeof(ntopbuf)),
87697712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
87797712Sume			return;
87897712Sume		}
879118913Sume		ra_input(i, (struct nd_router_advert *)icp, pi, &rcvfrom);
88097712Sume		break;
88197712Sume	case ICMP6_ROUTER_RENUMBERING:
882224144Shrs		if (mcastif == NULL) {
883225519Shrs			syslog(LOG_ERR, "received a router renumbering "
884225519Shrs			    "message, but not allowed to be accepted");
88597712Sume			break;
88697712Sume		}
887118913Sume		rr_input(i, (struct icmp6_router_renum *)icp, pi, &rcvfrom,
888222732Shrs		    &dst);
88997712Sume		break;
89097712Sume	default:
89197712Sume		/*
89297712Sume		 * Note that this case is POSSIBLE, especially just
89397712Sume		 * after invocation of the daemon. This is because we
89497712Sume		 * could receive message after opening the socket and
89597712Sume		 * before setting ICMP6 type filter(see sock_open()).
89697712Sume		 */
897225519Shrs		syslog(LOG_ERR, "invalid icmp type(%d)", icp->icmp6_type);
89897712Sume		return;
89955505Sshin	}
90055505Sshin
90155505Sshin	return;
90255505Sshin}
90355505Sshin
90455505Sshinstatic void
90555505Sshinrs_input(int len, struct nd_router_solicit *rs,
90655505Sshin	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
90755505Sshin{
908224144Shrs	char ntopbuf[INET6_ADDRSTRLEN];
909224144Shrs	char ifnamebuf[IFNAMSIZ];
910222732Shrs	union nd_opt ndopts;
911222732Shrs	struct rainfo *rai;
912224144Shrs	struct ifinfo *ifi;
913118968Sume	struct soliciter *sol;
91455505Sshin
91555505Sshin	syslog(LOG_DEBUG,
916222732Shrs	    "<%s> RS received from %s on %s",
917222732Shrs	    __func__,
918222732Shrs	    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
919222732Shrs	    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
92055505Sshin
92155505Sshin	/* ND option check */
92255505Sshin	memset(&ndopts, 0, sizeof(ndopts));
923225519Shrs	TAILQ_INIT(&ndopts.opt_list);
92455505Sshin	if (nd6_options((struct nd_opt_hdr *)(rs + 1),
92555505Sshin			len - sizeof(struct nd_router_solicit),
926118664Sume			&ndopts, NDOPT_FLAG_SRCLINKADDR)) {
927151470Ssuz		syslog(LOG_INFO,
928222732Shrs		    "<%s> ND option check failed for an RS from %s on %s",
929222732Shrs		    __func__,
930222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
931222732Shrs			sizeof(ntopbuf)),
932222732Shrs		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
93355505Sshin		return;
93455505Sshin	}
93555505Sshin
93655505Sshin	/*
93755505Sshin	 * If the IP source address is the unspecified address, there
93855505Sshin	 * must be no source link-layer address option in the message.
939222732Shrs	 * (RFC 4861 6.1.1)
94055505Sshin	 */
94155505Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
942222732Shrs	    ndopts.opt_src_lladdr) {
943151470Ssuz		syslog(LOG_INFO,
944222732Shrs		    "<%s> RS from unspecified src on %s has a link-layer"
945222732Shrs		    " address option",
946222732Shrs		    __func__, if_indextoname(pi->ipi6_ifindex, ifnamebuf));
94755505Sshin		goto done;
94855505Sshin	}
94955505Sshin
950224144Shrs	ifi = if_indextoifinfo(pi->ipi6_ifindex);
951224144Shrs	if (ifi == NULL) {
952224144Shrs		syslog(LOG_INFO,
953224144Shrs		    "<%s> if (idx=%d) not found.  Why?",
954224144Shrs		    __func__, pi->ipi6_ifindex);
955224144Shrs		goto done;
956224144Shrs	}
957224144Shrs	rai = ifi->ifi_rainfo;
958222732Shrs	if (rai == NULL) {
95955505Sshin		syslog(LOG_INFO,
96055505Sshin		       "<%s> RS received on non advertising interface(%s)",
961118660Sume		       __func__,
96255505Sshin		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
96355505Sshin		goto done;
96455505Sshin	}
96555505Sshin
966224144Shrs	rai->rai_ifinfo->ifi_rsinput++;
96762656Skris
96855505Sshin	/*
96955505Sshin	 * Decide whether to send RA according to the rate-limit
97055505Sshin	 * consideration.
97155505Sshin	 */
97255505Sshin
973118968Sume	/* record sockaddr waiting for RA, if possible */
974118968Sume	sol = (struct soliciter *)malloc(sizeof(*sol));
975118968Sume	if (sol) {
976222732Shrs		sol->sol_addr = *from;
977222732Shrs		/* XXX RFC 2553 need clarification on flowinfo */
978222732Shrs		sol->sol_addr.sin6_flowinfo = 0;
979222732Shrs		TAILQ_INSERT_TAIL(&rai->rai_soliciter, sol, sol_next);
980118968Sume	}
98162656Skris
982118968Sume	/*
983118968Sume	 * If there is already a waiting RS packet, don't
984118968Sume	 * update the timer.
985118968Sume	 */
986224144Shrs	if (ifi->ifi_rs_waitcount++)
987118968Sume		goto done;
98855505Sshin
989224144Shrs	set_short_delay(ifi);
99055505Sshin
99155505Sshin  done:
99255505Sshin	free_ndopts(&ndopts);
99355505Sshin	return;
99455505Sshin}
99555505Sshin
99655505Sshinstatic void
997224144Shrsset_short_delay(struct ifinfo *ifi)
998118968Sume{
999118968Sume	long delay;	/* must not be greater than 1000000 */
1000253970Shrs	struct timespec interval, now, min_delay, tm_tmp, *rest;
1001118968Sume
1002247863Shrs	if (ifi->ifi_ra_timer == NULL)
1003247863Shrs		return;
1004118968Sume	/*
1005118968Sume	 * Compute a random delay. If the computed value
1006118968Sume	 * corresponds to a time later than the time the next
1007118968Sume	 * multicast RA is scheduled to be sent, ignore the random
1008118968Sume	 * delay and send the advertisement at the
1009222732Shrs	 * already-scheduled time. RFC 4861 6.2.6
1010118968Sume	 */
1011180823Sache	delay = arc4random_uniform(MAX_RA_DELAY_TIME);
1012118968Sume	interval.tv_sec = 0;
1013253970Shrs	interval.tv_nsec = delay * 1000;
1014224144Shrs	rest = rtadvd_timer_rest(ifi->ifi_ra_timer);
1015253970Shrs	if (TS_CMP(rest, &interval, <)) {
1016118968Sume		syslog(LOG_DEBUG, "<%s> random delay is larger than "
1017118968Sume		    "the rest of the current timer", __func__);
1018118968Sume		interval = *rest;
1019118968Sume	}
1020118968Sume
1021118968Sume	/*
1022118968Sume	 * If we sent a multicast Router Advertisement within
1023118968Sume	 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
1024118968Sume	 * the advertisement to be sent at a time corresponding to
1025118968Sume	 * MIN_DELAY_BETWEEN_RAS plus the random value after the
1026118968Sume	 * previous advertisement was sent.
1027118968Sume	 */
1028253970Shrs	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1029253970Shrs	TS_SUB(&now, &ifi->ifi_ra_lastsent, &tm_tmp);
1030118968Sume	min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
1031253970Shrs	min_delay.tv_nsec = 0;
1032253970Shrs	if (TS_CMP(&tm_tmp, &min_delay, <)) {
1033253970Shrs		TS_SUB(&min_delay, &tm_tmp, &min_delay);
1034253970Shrs		TS_ADD(&min_delay, &interval, &interval);
1035118968Sume	}
1036224144Shrs	rtadvd_set_timer(&interval, ifi->ifi_ra_timer);
1037118968Sume}
1038118968Sume
1039222732Shrsstatic int
1040222732Shrscheck_accept_rtadv(int idx)
1041222732Shrs{
1042224144Shrs	struct ifinfo *ifi;
1043222732Shrs
1044224144Shrs	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1045224144Shrs		if (ifi->ifi_ifindex == idx)
1046224144Shrs			break;
1047224144Shrs	}
1048224144Shrs	if (ifi == NULL) {
1049225519Shrs		syslog(LOG_DEBUG,
1050224144Shrs		    "<%s> if (idx=%d) not found.  Why?",
1051222732Shrs		    __func__, idx);
1052222732Shrs		return (0);
1053222732Shrs	}
1054224144Shrs#if (__FreeBSD_version < 900000)
1055224144Shrs	/*
1056224144Shrs	 * RA_RECV: !ip6.forwarding && ip6.accept_rtadv
1057224144Shrs	 * RA_SEND: ip6.forwarding
1058224144Shrs	 */
1059224144Shrs	return ((getinet6sysctl(IPV6CTL_FORWARDING) == 0) &&
1060224144Shrs	    (getinet6sysctl(IPV6CTL_ACCEPT_RTADV) == 1));
1061224144Shrs#else
1062224144Shrs	/*
1063224144Shrs	 * RA_RECV: ND6_IFF_ACCEPT_RTADV
1064224144Shrs	 * RA_SEND: ip6.forwarding
1065224144Shrs	 */
1066224144Shrs	if (update_ifinfo_nd_flags(ifi) != 0) {
1067225519Shrs		syslog(LOG_ERR, "cannot get nd6 flags (idx=%d)", idx);
1068222732Shrs		return (0);
1069222732Shrs	}
1070222732Shrs
1071224144Shrs	return (ifi->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV);
1072224144Shrs#endif
1073222732Shrs}
1074222732Shrs
1075118968Sumestatic void
1076222732Shrsra_input(int len, struct nd_router_advert *nra,
107755505Sshin	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
107855505Sshin{
107955505Sshin	struct rainfo *rai;
1080224144Shrs	struct ifinfo *ifi;
1081224144Shrs	char ntopbuf[INET6_ADDRSTRLEN];
1082224144Shrs	char ifnamebuf[IFNAMSIZ];
1083222732Shrs	union nd_opt ndopts;
1084222732Shrs	const char *on_off[] = {"OFF", "ON"};
1085224144Shrs	uint32_t reachabletime, retranstimer, mtu;
108662656Skris	int inconsistent = 0;
1087222732Shrs	int error;
108855505Sshin
1089222732Shrs	syslog(LOG_DEBUG, "<%s> RA received from %s on %s", __func__,
1090222732Shrs	    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
1091222732Shrs	    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
1092222732Shrs
109355505Sshin	/* ND option check */
109455505Sshin	memset(&ndopts, 0, sizeof(ndopts));
1095225519Shrs	TAILQ_INIT(&ndopts.opt_list);
1096222732Shrs	error = nd6_options((struct nd_opt_hdr *)(nra + 1),
1097222732Shrs	    len - sizeof(struct nd_router_advert), &ndopts,
1098222732Shrs	    NDOPT_FLAG_SRCLINKADDR | NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU |
1099222732Shrs	    NDOPT_FLAG_RDNSS | NDOPT_FLAG_DNSSL);
1100222732Shrs	if (error) {
1101151470Ssuz		syslog(LOG_INFO,
1102222732Shrs		    "<%s> ND option check failed for an RA from %s on %s",
1103222732Shrs		    __func__,
1104222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1105222732Shrs			sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1106222732Shrs			ifnamebuf));
110755505Sshin		return;
110855505Sshin	}
110955505Sshin
111055505Sshin	/*
1111222732Shrs	 * RA consistency check according to RFC 4861 6.2.7
111255505Sshin	 */
1113224144Shrs	ifi = if_indextoifinfo(pi->ipi6_ifindex);
1114224144Shrs	if (ifi->ifi_rainfo == NULL) {
111555505Sshin		syslog(LOG_INFO,
1116222732Shrs		    "<%s> received RA from %s on non-advertising"
1117222732Shrs		    " interface(%s)",
1118222732Shrs		    __func__,
1119222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1120222732Shrs			sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1121222732Shrs			ifnamebuf));
112255505Sshin		goto done;
112355505Sshin	}
1124224144Shrs	rai = ifi->ifi_rainfo;
1125224144Shrs	ifi->ifi_rainput++;
1126225519Shrs	syslog(LOG_DEBUG, "<%s> ifi->ifi_rainput = %" PRIu64, __func__,
1127224144Shrs	    ifi->ifi_rainput);
1128222732Shrs
112955505Sshin	/* Cur Hop Limit value */
1130222732Shrs	if (nra->nd_ra_curhoplimit && rai->rai_hoplimit &&
1131222732Shrs	    nra->nd_ra_curhoplimit != rai->rai_hoplimit) {
1132225519Shrs		syslog(LOG_NOTICE,
1133225519Shrs		    "CurHopLimit inconsistent on %s:"
1134222732Shrs		    " %d from %s, %d from us",
1135225519Shrs		    ifi->ifi_ifname, nra->nd_ra_curhoplimit,
1136222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1137222732Shrs			sizeof(ntopbuf)), rai->rai_hoplimit);
113862656Skris		inconsistent++;
113955505Sshin	}
114055505Sshin	/* M flag */
1141222732Shrs	if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
1142222732Shrs	    rai->rai_managedflg) {
1143225519Shrs		syslog(LOG_NOTICE,
1144225519Shrs		    "M flag inconsistent on %s:"
1145222732Shrs		    " %s from %s, %s from us",
1146225519Shrs		    ifi->ifi_ifname, on_off[!rai->rai_managedflg],
1147222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1148222732Shrs			sizeof(ntopbuf)), on_off[rai->rai_managedflg]);
114962656Skris		inconsistent++;
115055505Sshin	}
115155505Sshin	/* O flag */
1152222732Shrs	if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
1153222732Shrs	    rai->rai_otherflg) {
1154225519Shrs		syslog(LOG_NOTICE,
1155225519Shrs		    "O flag inconsistent on %s:"
1156222732Shrs		    " %s from %s, %s from us",
1157225519Shrs		    ifi->ifi_ifname, on_off[!rai->rai_otherflg],
1158222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1159222732Shrs			sizeof(ntopbuf)), on_off[rai->rai_otherflg]);
116062656Skris		inconsistent++;
116155505Sshin	}
116255505Sshin	/* Reachable Time */
1163222732Shrs	reachabletime = ntohl(nra->nd_ra_reachable);
1164222732Shrs	if (reachabletime && rai->rai_reachabletime &&
1165222732Shrs	    reachabletime != rai->rai_reachabletime) {
1166225519Shrs		syslog(LOG_NOTICE,
1167225519Shrs		    "ReachableTime inconsistent on %s:"
1168222732Shrs		    " %d from %s, %d from us",
1169225519Shrs		    ifi->ifi_ifname, reachabletime,
1170222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1171222732Shrs			sizeof(ntopbuf)), rai->rai_reachabletime);
117262656Skris		inconsistent++;
117355505Sshin	}
117455505Sshin	/* Retrans Timer */
1175222732Shrs	retranstimer = ntohl(nra->nd_ra_retransmit);
1176222732Shrs	if (retranstimer && rai->rai_retranstimer &&
1177222732Shrs	    retranstimer != rai->rai_retranstimer) {
1178225519Shrs		syslog(LOG_NOTICE,
1179225519Shrs		    "RetranceTimer inconsistent on %s:"
1180222732Shrs		    " %d from %s, %d from us",
1181225519Shrs		    ifi->ifi_ifname, retranstimer,
1182222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1183222732Shrs			sizeof(ntopbuf)), rai->rai_retranstimer);
118462656Skris		inconsistent++;
118555505Sshin	}
118655505Sshin	/* Values in the MTU options */
1187222732Shrs	if (ndopts.opt_mtu) {
1188222732Shrs		mtu = ntohl(ndopts.opt_mtu->nd_opt_mtu_mtu);
1189222732Shrs		if (mtu && rai->rai_linkmtu && mtu != rai->rai_linkmtu) {
1190225519Shrs			syslog(LOG_NOTICE,
1191225519Shrs			    "MTU option value inconsistent on %s:"
1192222732Shrs			    " %d from %s, %d from us",
1193225519Shrs			    ifi->ifi_ifname, mtu,
1194222732Shrs			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1195222732Shrs				sizeof(ntopbuf)), rai->rai_linkmtu);
119662656Skris			inconsistent++;
119755505Sshin		}
119855505Sshin	}
119955505Sshin	/* Preferred and Valid Lifetimes for prefixes */
120055505Sshin	{
1201222732Shrs		struct nd_optlist *nol;
120278064Sume
1203222732Shrs		if (ndopts.opt_pi)
1204222732Shrs			if (prefix_check(ndopts.opt_pi, rai, from))
120562656Skris				inconsistent++;
1206222732Shrs
1207222732Shrs		TAILQ_FOREACH(nol, &ndopts.opt_list, nol_next)
1208222732Shrs			if (prefix_check((struct nd_opt_prefix_info *)nol->nol_opt,
1209222732Shrs				rai, from))
121062656Skris				inconsistent++;
121155505Sshin	}
121255505Sshin
121378064Sume	if (inconsistent)
1214224144Shrs		ifi->ifi_rainconsistent++;
1215222732Shrs
121655505Sshin  done:
121755505Sshin	free_ndopts(&ndopts);
121855505Sshin	return;
121955505Sshin}
122055505Sshin
1221275038Sdimstatic uint32_t
1222275038Sdimudiff(uint32_t u, uint32_t v)
1223275038Sdim{
1224275038Sdim	return (u >= v ? u - v : v - u);
1225275038Sdim}
1226275038Sdim
122762656Skris/* return a non-zero value if the received prefix is inconsitent with ours */
122862656Skrisstatic int
122955505Sshinprefix_check(struct nd_opt_prefix_info *pinfo,
1230222732Shrs	struct rainfo *rai, struct sockaddr_in6 *from)
123155505Sshin{
1232224144Shrs	struct ifinfo *ifi;
1233224144Shrs	uint32_t preferred_time, valid_time;
1234222732Shrs	struct prefix *pfx;
123562656Skris	int inconsistent = 0;
1236224144Shrs	char ntopbuf[INET6_ADDRSTRLEN];
1237224144Shrs	char prefixbuf[INET6_ADDRSTRLEN];
1238253970Shrs	struct timespec now;
123955505Sshin
124062656Skris#if 0				/* impossible */
124162656Skris	if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1242222732Shrs		return (0);
124362656Skris#endif
1244224144Shrs	ifi = rai->rai_ifinfo;
124555505Sshin	/*
124655505Sshin	 * log if the adveritsed prefix has link-local scope(sanity check?)
124755505Sshin	 */
1248222732Shrs	if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix))
124955505Sshin		syslog(LOG_INFO,
1250222732Shrs		    "<%s> link-local prefix %s/%d is advertised "
1251222732Shrs		    "from %s on %s",
1252222732Shrs		    __func__,
1253222732Shrs		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1254222732Shrs			sizeof(prefixbuf)),
1255222732Shrs		    pinfo->nd_opt_pi_prefix_len,
1256222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1257224144Shrs			sizeof(ntopbuf)), ifi->ifi_ifname);
125855505Sshin
1259222732Shrs	if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1260222732Shrs		pinfo->nd_opt_pi_prefix_len)) == NULL) {
126155505Sshin		syslog(LOG_INFO,
1262222732Shrs		    "<%s> prefix %s/%d from %s on %s is not in our list",
1263222732Shrs		    __func__,
1264222732Shrs		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1265222732Shrs			sizeof(prefixbuf)),
1266222732Shrs		    pinfo->nd_opt_pi_prefix_len,
1267222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1268224144Shrs			sizeof(ntopbuf)), ifi->ifi_ifname);
1269222732Shrs		return (0);
127055505Sshin	}
127155505Sshin
127255505Sshin	preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1273222732Shrs	if (pfx->pfx_pltimeexpire) {
127478064Sume		/*
127578064Sume		 * The lifetime is decremented in real time, so we should
127678064Sume		 * compare the expiration time.
127778064Sume		 * (RFC 2461 Section 6.2.7.)
127878064Sume		 * XXX: can we really expect that all routers on the link
127978064Sume		 * have synchronized clocks?
128078064Sume		 */
1281253970Shrs		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
128278064Sume		preferred_time += now.tv_sec;
128378064Sume
1284222732Shrs		if (!pfx->pfx_timer && rai->rai_clockskew &&
1285275038Sdim		    udiff(preferred_time, pfx->pfx_pltimeexpire) > rai->rai_clockskew) {
128678064Sume			syslog(LOG_INFO,
1287222732Shrs			    "<%s> preferred lifetime for %s/%d"
1288222732Shrs			    " (decr. in real time) inconsistent on %s:"
1289224144Shrs			    " %" PRIu32 " from %s, %" PRIu32 " from us",
1290222732Shrs			    __func__,
1291222732Shrs			    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1292222732Shrs				sizeof(prefixbuf)),
1293222732Shrs			    pinfo->nd_opt_pi_prefix_len,
1294224144Shrs			    ifi->ifi_ifname, preferred_time,
1295222732Shrs			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1296222732Shrs				sizeof(ntopbuf)), pfx->pfx_pltimeexpire);
129778064Sume			inconsistent++;
129878064Sume		}
1299222732Shrs	} else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime)
130078064Sume		syslog(LOG_INFO,
1301222732Shrs		    "<%s> preferred lifetime for %s/%d"
1302222732Shrs		    " inconsistent on %s:"
1303222732Shrs		    " %d from %s, %d from us",
1304222732Shrs		    __func__,
1305222732Shrs		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1306222732Shrs			sizeof(prefixbuf)),
1307222732Shrs		    pinfo->nd_opt_pi_prefix_len,
1308224144Shrs		    ifi->ifi_ifname, preferred_time,
1309222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1310222732Shrs			sizeof(ntopbuf)), pfx->pfx_preflifetime);
131155505Sshin
131255505Sshin	valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1313222732Shrs	if (pfx->pfx_vltimeexpire) {
1314253970Shrs		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
131578064Sume		valid_time += now.tv_sec;
131678064Sume
1317222732Shrs		if (!pfx->pfx_timer && rai->rai_clockskew &&
1318275038Sdim		    udiff(valid_time, pfx->pfx_vltimeexpire) > rai->rai_clockskew) {
131978064Sume			syslog(LOG_INFO,
1320222732Shrs			    "<%s> valid lifetime for %s/%d"
1321222732Shrs			    " (decr. in real time) inconsistent on %s:"
1322224144Shrs			    " %d from %s, %" PRIu32 " from us",
1323222732Shrs			    __func__,
1324222732Shrs			    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1325222732Shrs				sizeof(prefixbuf)),
1326222732Shrs			    pinfo->nd_opt_pi_prefix_len,
1327224144Shrs			    ifi->ifi_ifname, preferred_time,
1328222732Shrs			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1329222732Shrs				sizeof(ntopbuf)), pfx->pfx_vltimeexpire);
133078064Sume			inconsistent++;
133178064Sume		}
1332222732Shrs	} else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) {
133378064Sume		syslog(LOG_INFO,
1334222732Shrs		    "<%s> valid lifetime for %s/%d"
1335222732Shrs		    " inconsistent on %s:"
1336222732Shrs		    " %d from %s, %d from us",
1337222732Shrs		    __func__,
1338222732Shrs		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1339222732Shrs			sizeof(prefixbuf)),
1340222732Shrs		    pinfo->nd_opt_pi_prefix_len,
1341224144Shrs		    ifi->ifi_ifname, valid_time,
1342222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1343222732Shrs			sizeof(ntopbuf)), pfx->pfx_validlifetime);
134462656Skris		inconsistent++;
134562656Skris	}
134662656Skris
1347222732Shrs	return (inconsistent);
134855505Sshin}
134955505Sshin
135055505Sshinstruct prefix *
135155505Sshinfind_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
135255505Sshin{
1353222732Shrs	struct prefix *pfx;
135455505Sshin	int bytelen, bitlen;
1355224144Shrs	char bitmask;
135655505Sshin
1357222732Shrs	TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1358222732Shrs		if (plen != pfx->pfx_prefixlen)
135955505Sshin			continue;
1360222732Shrs
136155505Sshin		bytelen = plen / 8;
136255505Sshin		bitlen = plen % 8;
1363118968Sume		bitmask = 0xff << (8 - bitlen);
1364222732Shrs
1365222732Shrs		if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen))
136655505Sshin			continue;
1367222732Shrs
1368118968Sume		if (bitlen == 0 ||
1369222732Shrs		    ((prefix->s6_addr[bytelen] & bitmask) ==
1370222732Shrs		     (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) {
1371222732Shrs			return (pfx);
1372118968Sume		}
137355505Sshin	}
137455505Sshin
1375222732Shrs	return (NULL);
137655505Sshin}
137755505Sshin
137878064Sume/* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
137978064Sumeint
138078064Sumeprefix_match(struct in6_addr *p0, int plen0,
1381222732Shrs	struct in6_addr *p1, int plen1)
138278064Sume{
138378064Sume	int bytelen, bitlen;
1384224144Shrs	char bitmask;
138578064Sume
138678064Sume	if (plen0 < plen1)
1387222732Shrs		return (0);
1388222732Shrs
138978064Sume	bytelen = plen1 / 8;
139078064Sume	bitlen = plen1 % 8;
1391118968Sume	bitmask = 0xff << (8 - bitlen);
1392222732Shrs
139378064Sume	if (memcmp((void *)p0, (void *)p1, bytelen))
1394222732Shrs		return (0);
1395222732Shrs
1396118968Sume	if (bitlen == 0 ||
1397118968Sume	    ((p0->s6_addr[bytelen] & bitmask) ==
1398118968Sume	     (p1->s6_addr[bytelen] & bitmask))) {
1399222732Shrs		return (1);
1400118968Sume	}
140178064Sume
1402222732Shrs	return (0);
140378064Sume}
140478064Sume
140555505Sshinstatic int
140655505Sshinnd6_options(struct nd_opt_hdr *hdr, int limit,
1407224144Shrs	union nd_opt *ndopts, uint32_t optflags)
140855505Sshin{
140955505Sshin	int optlen = 0;
141055505Sshin
141155505Sshin	for (; limit > 0; limit -= optlen) {
1412222732Shrs		if ((size_t)limit < sizeof(struct nd_opt_hdr)) {
1413118660Sume			syslog(LOG_INFO, "<%s> short option header", __func__);
1414112676Sume			goto bad;
1415112676Sume		}
1416112676Sume
141755505Sshin		hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
141855505Sshin		if (hdr->nd_opt_len == 0) {
1419112676Sume			syslog(LOG_INFO,
142097712Sume			    "<%s> bad ND option length(0) (type = %d)",
1421118660Sume			    __func__, hdr->nd_opt_type);
142255505Sshin			goto bad;
142355505Sshin		}
1424112676Sume		optlen = hdr->nd_opt_len << 3;
1425112676Sume		if (optlen > limit) {
1426118660Sume			syslog(LOG_INFO, "<%s> short option", __func__);
1427112676Sume			goto bad;
1428112676Sume		}
142955505Sshin
1430222732Shrs		if (hdr->nd_opt_type > ND_OPT_MTU &&
1431222732Shrs		    hdr->nd_opt_type != ND_OPT_RDNSS &&
1432222732Shrs		    hdr->nd_opt_type != ND_OPT_DNSSL) {
1433118664Sume			syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1434118660Sume			    __func__, hdr->nd_opt_type);
143555505Sshin			continue;
143655505Sshin		}
143755505Sshin
143855505Sshin		if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1439118664Sume			syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1440118664Sume			    __func__, hdr->nd_opt_type);
144155505Sshin			continue;
144255505Sshin		}
144355505Sshin
1444112676Sume		/*
1445112676Sume		 * Option length check.  Do it here for all fixed-length
1446112676Sume		 * options.
1447112676Sume		 */
1448222732Shrs		switch (hdr->nd_opt_type) {
1449222732Shrs		case ND_OPT_MTU:
1450222732Shrs			if (optlen == sizeof(struct nd_opt_mtu))
1451222732Shrs				break;
1452222732Shrs			goto skip;
1453222732Shrs		case ND_OPT_RDNSS:
1454222732Shrs			if (optlen >= 24 &&
1455222732Shrs			    (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0)
1456222732Shrs				break;
1457222732Shrs			goto skip;
1458222732Shrs		case ND_OPT_DNSSL:
1459222732Shrs			if (optlen >= 16 &&
1460222732Shrs			    (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0)
1461222732Shrs				break;
1462222732Shrs			goto skip;
1463222732Shrs		case ND_OPT_PREFIX_INFORMATION:
1464222732Shrs			if (optlen == sizeof(struct nd_opt_prefix_info))
1465222732Shrs				break;
1466222732Shrsskip:
1467112676Sume			syslog(LOG_INFO, "<%s> invalid option length",
1468118660Sume			    __func__);
1469112676Sume			continue;
1470112676Sume		}
1471112676Sume
147297712Sume		switch (hdr->nd_opt_type) {
147397712Sume		case ND_OPT_TARGET_LINKADDR:
147497712Sume		case ND_OPT_REDIRECTED_HEADER:
1475222732Shrs		case ND_OPT_RDNSS:
1476222732Shrs		case ND_OPT_DNSSL:
1477112676Sume			break;	/* we don't care about these options */
1478151469Ssuz		case ND_OPT_SOURCE_LINKADDR:
147997712Sume		case ND_OPT_MTU:
1480222732Shrs			if (ndopts->opt_array[hdr->nd_opt_type]) {
148197712Sume				syslog(LOG_INFO,
148297712Sume				    "<%s> duplicated ND option (type = %d)",
1483118660Sume				    __func__, hdr->nd_opt_type);
148497712Sume			}
1485222732Shrs			ndopts->opt_array[hdr->nd_opt_type] = hdr;
148697712Sume			break;
148797712Sume		case ND_OPT_PREFIX_INFORMATION:
148897712Sume		{
1489222732Shrs			struct nd_optlist *nol;
149055505Sshin
1491222732Shrs			if (ndopts->opt_pi == 0) {
1492222732Shrs				ndopts->opt_pi =
149397712Sume				    (struct nd_opt_prefix_info *)hdr;
149497712Sume				continue;
149597712Sume			}
1496222732Shrs			nol = malloc(sizeof(*nol));
1497222732Shrs			if (nol == NULL) {
149897712Sume				syslog(LOG_ERR, "<%s> can't allocate memory",
1499118660Sume				    __func__);
150097712Sume				goto bad;
150197712Sume			}
1502222732Shrs			nol->nol_opt = hdr;
1503222732Shrs			TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next);
150455505Sshin
150597712Sume			break;
150655505Sshin		}
150797712Sume		default:	/* impossible */
150897712Sume			break;
150997712Sume		}
151055505Sshin	}
151155505Sshin
1512222732Shrs	return (0);
151355505Sshin
151455505Sshin  bad:
151555505Sshin	free_ndopts(ndopts);
151655505Sshin
1517222732Shrs	return (-1);
151855505Sshin}
151955505Sshin
152055505Sshinstatic void
1521222732Shrsfree_ndopts(union nd_opt *ndopts)
152255505Sshin{
1523222732Shrs	struct nd_optlist *nol;
152455505Sshin
1525222732Shrs	while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) {
1526222732Shrs		TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next);
1527222732Shrs		free(nol);
152855505Sshin	}
152955505Sshin}
153055505Sshin
153155505Sshinvoid
1532224144Shrssock_open(struct sockinfo *s)
153355505Sshin{
153455505Sshin	struct icmp6_filter filt;
153555505Sshin	int on;
153655505Sshin	/* XXX: should be max MTU attached to the node */
1537224144Shrs	static char answer[1500];
153855505Sshin
1539224144Shrs	syslog(LOG_DEBUG, "<%s> enter", __func__);
1540224144Shrs
1541224144Shrs	if (s == NULL) {
1542224144Shrs		syslog(LOG_ERR, "<%s> internal error", __func__);
1543224144Shrs		exit(1);
1544224144Shrs	}
154562656Skris	rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1546222732Shrs	    CMSG_SPACE(sizeof(int));
1547224144Shrs	rcvcmsgbuf = (char *)malloc(rcvcmsgbuflen);
154862656Skris	if (rcvcmsgbuf == NULL) {
1549118660Sume		syslog(LOG_ERR, "<%s> not enough core", __func__);
155062656Skris		exit(1);
155162656Skris	}
155262656Skris
1553222732Shrs	sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1554222732Shrs	    CMSG_SPACE(sizeof(int));
1555224144Shrs	sndcmsgbuf = (char *)malloc(sndcmsgbuflen);
155662656Skris	if (sndcmsgbuf == NULL) {
1557118660Sume		syslog(LOG_ERR, "<%s> not enough core", __func__);
155862656Skris		exit(1);
155962656Skris	}
156062656Skris
1561224144Shrs	if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1562222732Shrs		syslog(LOG_ERR, "<%s> socket: %s", __func__, strerror(errno));
156355505Sshin		exit(1);
156455505Sshin	}
156555505Sshin	/* specify to tell receiving interface */
156655505Sshin	on = 1;
1567224144Shrs	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1568222732Shrs	    sizeof(on)) < 0) {
1569222732Shrs		syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s", __func__,
1570222732Shrs		    strerror(errno));
157162656Skris		exit(1);
157262656Skris	}
157355505Sshin	on = 1;
157455505Sshin	/* specify to tell value of hoplimit field of received IP6 hdr */
1575224144Shrs	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1576222732Shrs		sizeof(on)) < 0) {
1577222732Shrs		syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s", __func__,
1578222732Shrs		    strerror(errno));
157962656Skris		exit(1);
158062656Skris	}
158155505Sshin	ICMP6_FILTER_SETBLOCKALL(&filt);
158262656Skris	ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
158362656Skris	ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1584224144Shrs	if (mcastif != NULL)
158555505Sshin		ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1586222732Shrs
1587224144Shrs	if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1588222732Shrs	    sizeof(filt)) < 0) {
158955505Sshin		syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1590222732Shrs		    __func__, strerror(errno));
159155505Sshin		exit(1);
159255505Sshin	}
159355505Sshin
159455505Sshin	/* initialize msghdr for receiving packets */
159555505Sshin	rcviov[0].iov_base = (caddr_t)answer;
159655505Sshin	rcviov[0].iov_len = sizeof(answer);
1597118913Sume	rcvmhdr.msg_name = (caddr_t)&rcvfrom;
1598118913Sume	rcvmhdr.msg_namelen = sizeof(rcvfrom);
159955505Sshin	rcvmhdr.msg_iov = rcviov;
160055505Sshin	rcvmhdr.msg_iovlen = 1;
160155505Sshin	rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
160262656Skris	rcvmhdr.msg_controllen = rcvcmsgbuflen;
160355505Sshin
160455505Sshin	/* initialize msghdr for sending packets */
160555505Sshin	sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
160655505Sshin	sndmhdr.msg_iov = sndiov;
160755505Sshin	sndmhdr.msg_iovlen = 1;
160855505Sshin	sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
160962656Skris	sndmhdr.msg_controllen = sndcmsgbuflen;
1610222732Shrs
161155505Sshin	return;
161255505Sshin}
161355505Sshin
161455505Sshin/* open a routing socket to watch the routing table */
161555505Sshinstatic void
1616224144Shrsrtsock_open(struct sockinfo *s)
161755505Sshin{
1618224144Shrs	if (s == NULL) {
1619224144Shrs		syslog(LOG_ERR, "<%s> internal error", __func__);
1620224144Shrs		exit(1);
1621224144Shrs	}
1622224144Shrs	if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
162355505Sshin		syslog(LOG_ERR,
1624222732Shrs		    "<%s> socket: %s", __func__, strerror(errno));
162555505Sshin		exit(1);
162655505Sshin	}
162755505Sshin}
162855505Sshin
1629224144Shrsstruct ifinfo *
1630224144Shrsif_indextoifinfo(int idx)
163155505Sshin{
1632224144Shrs	struct ifinfo *ifi;
1633253058Shrs	char *name, name0[IFNAMSIZ];
163455505Sshin
1635253058Shrs	/* Check if the interface has a valid name or not. */
1636253058Shrs	if (if_indextoname(idx, name0) == NULL)
1637253058Shrs		return (NULL);
1638253058Shrs
1639224144Shrs	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1640224144Shrs		if (ifi->ifi_ifindex == idx)
1641224144Shrs			return (ifi);
164255505Sshin	}
164355505Sshin
1644224144Shrs	if (ifi != NULL)
1645224144Shrs		syslog(LOG_DEBUG, "<%s> ifi found (idx=%d)",
1646224144Shrs		    __func__, idx);
1647224144Shrs	else
1648224144Shrs		syslog(LOG_DEBUG, "<%s> ifi not found (idx=%d)",
1649224144Shrs		    __func__, idx);
1650224144Shrs
1651222732Shrs	return (NULL);		/* search failed */
165255505Sshin}
165355505Sshin
1654222972Shrsvoid
1655224144Shrsra_output(struct ifinfo *ifi)
165655505Sshin{
165755505Sshin	int i;
165855505Sshin	struct cmsghdr *cm;
165955505Sshin	struct in6_pktinfo *pi;
1660222732Shrs	struct soliciter *sol;
1661224144Shrs	struct rainfo *rai;
166255505Sshin
1663224144Shrs	switch (ifi->ifi_state) {
1664224144Shrs	case IFI_STATE_CONFIGURED:
1665224144Shrs		rai = ifi->ifi_rainfo;
1666224144Shrs		break;
1667224144Shrs	case IFI_STATE_TRANSITIVE:
1668224144Shrs		rai = ifi->ifi_rainfo_trans;
1669224144Shrs		break;
1670224144Shrs	case IFI_STATE_UNCONFIGURED:
1671224144Shrs		syslog(LOG_DEBUG, "<%s> %s is unconfigured.  "
1672224144Shrs		    "Skip sending RAs.",
1673224144Shrs		    __func__, ifi->ifi_ifname);
167462656Skris		return;
1675224144Shrs	default:
1676224144Shrs		rai = NULL;
167762656Skris	}
1678224144Shrs	if (rai == NULL) {
1679224144Shrs		syslog(LOG_DEBUG, "<%s> rainfo is NULL on %s."
1680224144Shrs		    "Skip sending RAs.",
1681224144Shrs		    __func__, ifi->ifi_ifname);
1682224144Shrs		return;
1683224144Shrs	}
1684224144Shrs	if (!(ifi->ifi_flags & IFF_UP)) {
1685224144Shrs		syslog(LOG_DEBUG, "<%s> %s is not up.  "
1686224144Shrs		    "Skip sending RAs.",
1687224144Shrs		    __func__, ifi->ifi_ifname);
1688224144Shrs		return;
1689224144Shrs	}
1690222732Shrs	/*
1691222732Shrs	 * Check lifetime, ACCEPT_RTADV flag, and ip6.forwarding.
1692222732Shrs	 *
1693222732Shrs	 * (lifetime == 0) = output
1694224144Shrs	 * (lifetime != 0 && (check_accept_rtadv()) = no output
1695222732Shrs	 *
1696222732Shrs	 * Basically, hosts MUST NOT send Router Advertisement
1697222732Shrs	 * messages at any time (RFC 4861, Section 6.2.3). However, it
1698222732Shrs	 * would sometimes be useful to allow hosts to advertise some
1699222732Shrs	 * parameters such as prefix information and link MTU. Thus,
1700222732Shrs	 * we allow hosts to invoke rtadvd only when router lifetime
1701222732Shrs	 * (on every advertising interface) is explicitly set
1702222732Shrs	 * zero. (see also the above section)
1703222732Shrs	 */
1704222732Shrs	syslog(LOG_DEBUG,
1705224144Shrs	    "<%s> check lifetime=%d, ACCEPT_RTADV=%d, ip6.forwarding=%d "
1706224144Shrs	    "on %s", __func__,
1707224144Shrs	    rai->rai_lifetime,
1708224144Shrs	    check_accept_rtadv(ifi->ifi_ifindex),
1709224144Shrs	    getinet6sysctl(IPV6CTL_FORWARDING),
1710224144Shrs	    ifi->ifi_ifname);
1711224144Shrs
1712222732Shrs	if (rai->rai_lifetime != 0) {
1713225519Shrs		if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) {
1714225519Shrs			syslog(LOG_ERR,
1715225519Shrs			    "non-zero lifetime RA "
1716225519Shrs			    "but net.inet6.ip6.forwarding=0.  "
1717225519Shrs			    "Ignored.");
1718225519Shrs			return;
1719225519Shrs		}
1720224144Shrs		if (check_accept_rtadv(ifi->ifi_ifindex)) {
1721225519Shrs			syslog(LOG_ERR,
1722225519Shrs			    "non-zero lifetime RA "
1723222732Shrs			    "on RA receiving interface %s."
1724225519Shrs			    "  Ignored.", ifi->ifi_ifname);
1725222732Shrs			return;
1726222732Shrs		}
1727222732Shrs	}
172878064Sume
1729222732Shrs	make_packet(rai);	/* XXX: inefficient */
173055505Sshin
1731222732Shrs	sndmhdr.msg_name = (caddr_t)&sin6_linklocal_allnodes;
1732222732Shrs	sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data;
1733222732Shrs	sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen;
1734222732Shrs
173555505Sshin	cm = CMSG_FIRSTHDR(&sndmhdr);
173655505Sshin	/* specify the outgoing interface */
173755505Sshin	cm->cmsg_level = IPPROTO_IPV6;
173855505Sshin	cm->cmsg_type = IPV6_PKTINFO;
173955505Sshin	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
174055505Sshin	pi = (struct in6_pktinfo *)CMSG_DATA(cm);
174155505Sshin	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));	/*XXX*/
1742224144Shrs	pi->ipi6_ifindex = ifi->ifi_ifindex;
174355505Sshin
174455505Sshin	/* specify the hop limit of the packet */
174555505Sshin	{
174655505Sshin		int hoplimit = 255;
174755505Sshin
174855505Sshin		cm = CMSG_NXTHDR(&sndmhdr, cm);
174955505Sshin		cm->cmsg_level = IPPROTO_IPV6;
175055505Sshin		cm->cmsg_type = IPV6_HOPLIMIT;
175155505Sshin		cm->cmsg_len = CMSG_LEN(sizeof(int));
175255505Sshin		memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
175355505Sshin	}
175455505Sshin
175555505Sshin	syslog(LOG_DEBUG,
1756224144Shrs	    "<%s> send RA on %s, # of RS waitings = %d",
1757224144Shrs	    __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount);
175855505Sshin
1759224144Shrs	i = sendmsg(sock.si_fd, &sndmhdr, 0);
176055505Sshin
1761222732Shrs	if (i < 0 || (size_t)i != rai->rai_ra_datalen)  {
176255505Sshin		if (i < 0) {
176362656Skris			syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1764224144Shrs			    __func__, ifi->ifi_ifname,
1765222732Shrs			    strerror(errno));
176655505Sshin		}
176755505Sshin	}
176855505Sshin
176962656Skris	/*
177062656Skris	 * unicast advertisements
177162656Skris	 * XXX commented out.  reason: though spec does not forbit it, unicast
177262656Skris	 * advert does not really help
177362656Skris	 */
1774222732Shrs	while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
1775222732Shrs		TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
177662656Skris		free(sol);
177762656Skris	}
177862656Skris
177955505Sshin	/* update timestamp */
1780253970Shrs	clock_gettime(CLOCK_MONOTONIC_FAST, &ifi->ifi_ra_lastsent);
178155505Sshin
1782224144Shrs	/* update counter */
1783224144Shrs	ifi->ifi_rs_waitcount = 0;
1784224144Shrs	ifi->ifi_raoutput++;
1785224144Shrs
1786224144Shrs	switch (ifi->ifi_state) {
1787224144Shrs	case IFI_STATE_CONFIGURED:
1788224144Shrs		if (ifi->ifi_burstcount > 0)
1789224144Shrs			ifi->ifi_burstcount--;
1790224144Shrs		break;
1791224144Shrs	case IFI_STATE_TRANSITIVE:
1792224144Shrs		ifi->ifi_burstcount--;
1793224144Shrs		if (ifi->ifi_burstcount == 0) {
1794224144Shrs			if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1795228990Suqs				/* Initial burst finished. */
1796224144Shrs				if (ifi->ifi_rainfo_trans != NULL)
1797224144Shrs					ifi->ifi_rainfo_trans = NULL;
1798224144Shrs			}
1799224144Shrs
1800224144Shrs			/* Remove burst RA information */
1801224144Shrs			if (ifi->ifi_rainfo_trans != NULL) {
1802224144Shrs				rm_rainfo(ifi->ifi_rainfo_trans);
1803224144Shrs				ifi->ifi_rainfo_trans = NULL;
1804224144Shrs			}
1805224144Shrs
1806224144Shrs			if (ifi->ifi_rainfo != NULL) {
1807224144Shrs				/*
1808224144Shrs				 * TRANSITIVE -> CONFIGURED
1809224144Shrs				 *
1810224144Shrs				 * After initial burst or transition from
1811224144Shrs				 * one configuration to another,
1812224144Shrs				 * ifi_rainfo always points to the next RA
1813224144Shrs				 * information.
1814224144Shrs				 */
1815224144Shrs				ifi->ifi_state = IFI_STATE_CONFIGURED;
1816224144Shrs				syslog(LOG_DEBUG,
1817224144Shrs				    "<%s> ifname=%s marked as "
1818224144Shrs				    "CONFIGURED.", __func__,
1819224144Shrs				    ifi->ifi_ifname);
1820224144Shrs			} else {
1821224144Shrs				/*
1822224144Shrs				 * TRANSITIVE -> UNCONFIGURED
1823224144Shrs				 *
1824224144Shrs				 * If ifi_rainfo points to NULL, this
1825224144Shrs				 * interface is shutting down.
1826224144Shrs				 *
1827224144Shrs				 */
1828224144Shrs				int error;
1829224144Shrs
1830224144Shrs				ifi->ifi_state = IFI_STATE_UNCONFIGURED;
1831224144Shrs				syslog(LOG_DEBUG,
1832224144Shrs				    "<%s> ifname=%s marked as "
1833224144Shrs				    "UNCONFIGURED.", __func__,
1834224144Shrs				    ifi->ifi_ifname);
1835224144Shrs				error = sock_mc_leave(&sock,
1836224144Shrs				    ifi->ifi_ifindex);
1837224144Shrs				if (error)
1838224144Shrs					exit(1);
1839224144Shrs			}
1840224144Shrs		}
1841224144Shrs		break;
1842224144Shrs	}
184355505Sshin}
184455505Sshin
184555505Sshin/* process RA timer */
184698172Sumestruct rtadvd_timer *
1847222732Shrsra_timeout(void *arg)
184855505Sshin{
1849224144Shrs	struct ifinfo *ifi;
185055505Sshin
1851224144Shrs	ifi = (struct ifinfo *)arg;
1852222732Shrs	syslog(LOG_DEBUG, "<%s> RA timer on %s is expired",
1853224144Shrs	    __func__, ifi->ifi_ifname);
185455505Sshin
1855224144Shrs	ra_output(ifi);
185698172Sume
1857224144Shrs	return (ifi->ifi_ra_timer);
185855505Sshin}
185955505Sshin
186055505Sshin/* update RA timer */
186155505Sshinvoid
1862253970Shrsra_timer_update(void *arg, struct timespec *tm)
186355505Sshin{
1864224144Shrs	uint16_t interval;
1865222732Shrs	struct rainfo *rai;
1866224144Shrs	struct ifinfo *ifi;
186755505Sshin
1868224144Shrs	ifi = (struct ifinfo *)arg;
1869224144Shrs	rai = ifi->ifi_rainfo;
1870224144Shrs	interval = 0;
1871224144Shrs
1872224144Shrs	switch (ifi->ifi_state) {
1873224144Shrs	case IFI_STATE_UNCONFIGURED:
1874224144Shrs		return;
1875224144Shrs		break;
1876224144Shrs	case IFI_STATE_CONFIGURED:
1877224144Shrs		/*
1878224144Shrs		 * Whenever a multicast advertisement is sent from
1879224144Shrs		 * an interface, the timer is reset to a
1880224144Shrs		 * uniformly-distributed random value between the
1881224144Shrs		 * interface's configured MinRtrAdvInterval and
1882224144Shrs		 * MaxRtrAdvInterval (RFC4861 6.2.4).
1883224144Shrs		 */
1884224144Shrs		interval = rai->rai_mininterval;
1885224144Shrs		interval += arc4random_uniform(rai->rai_maxinterval -
1886224144Shrs		    rai->rai_mininterval);
1887224144Shrs		break;
1888224144Shrs	case IFI_STATE_TRANSITIVE:
1889224144Shrs		/*
1890224144Shrs		 * For the first few advertisements (up to
1891224144Shrs		 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen
1892224144Shrs		 * interval is greater than
1893224144Shrs		 * MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer SHOULD be
1894224144Shrs		 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.  (RFC
1895224144Shrs		 * 4861 6.2.4)
1896224144Shrs		 *
1897224144Shrs		 * In such cases, the router SHOULD transmit one or more
1898224144Shrs		 * (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final
1899224144Shrs		 * multicast Router Advertisements on the interface with a
1900224144Shrs		 * Router Lifetime field of zero.  (RFC 4861 6.2.5)
1901224144Shrs		 */
1902224144Shrs		interval = ifi->ifi_burstinterval;
1903224144Shrs		break;
1904224144Shrs	}
190555505Sshin
190655505Sshin	tm->tv_sec = interval;
1907253970Shrs	tm->tv_nsec = 0;
190855505Sshin
190955505Sshin	syslog(LOG_DEBUG,
1910222732Shrs	    "<%s> RA timer on %s is set to %ld:%ld",
1911224144Shrs	    __func__, ifi->ifi_ifname,
1912253970Shrs	    (long int)tm->tv_sec, (long int)tm->tv_nsec / 1000);
191355505Sshin
191455505Sshin	return;
191555505Sshin}
1916