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
245118962Sume#ifndef HAVE_ARC4RANDOM
24655505Sshin	/* random value initialization */
247110668Sache#ifdef __FreeBSD__
248110668Sache	srandomdev();
249110668Sache#else
250224144Shrs	srandom((unsigned long)time(NULL));
251110668Sache#endif
252118962Sume#endif
253216675Sdelphij	pfh = pidfile_open(pidfilename, 0600, &otherpid);
254216675Sdelphij	if (pfh == NULL) {
255216675Sdelphij		if (errno == EEXIST)
256216675Sdelphij			errx(1, "%s already running, pid: %d",
257216675Sdelphij			    getprogname(), otherpid);
258216675Sdelphij		syslog(LOG_ERR,
259225519Shrs		    "failed to open the pid file %s, run anyway.",
260225519Shrs		    pidfilename);
261216675Sdelphij	}
26255505Sshin	if (!fflag)
26355505Sshin		daemon(1, 0);
26455505Sshin
265224144Shrs	sock_open(&sock);
266118920Sume
267224144Shrs	update_ifinfo(&ifilist, UPDATE_IFINFO_ALL);
268224144Shrs	for (i = 0; i < argc; i++)
269224144Shrs		update_persist_ifinfo(&ifilist, argv[i]);
270224144Shrs
271224144Shrs	csock_open(&ctrlsock, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
272224144Shrs	if (ctrlsock.si_fd == -1) {
273225519Shrs		syslog(LOG_ERR, "cannot open control socket: %s",
274225519Shrs		    strerror(errno));
275224144Shrs		exit(1);
276224144Shrs	}
277224144Shrs
27862656Skris	/* record the current PID */
27962656Skris	pid = getpid();
280216675Sdelphij	pidfile_write(pfh);
28162656Skris
282224144Shrs	set[PFD_RAWSOCK].fd = sock.si_fd;
283224144Shrs	set[PFD_RAWSOCK].events = POLLIN;
284118916Sume	if (sflag == 0) {
285224144Shrs		rtsock_open(&rtsock);
286224144Shrs		set[PFD_RTSOCK].fd = rtsock.si_fd;
287224144Shrs		set[PFD_RTSOCK].events = POLLIN;
288118916Sume	} else
289224144Shrs		set[PFD_RTSOCK].fd = -1;
290224144Shrs	set[PFD_CSOCK].fd = ctrlsock.si_fd;
291224144Shrs	set[PFD_CSOCK].events = POLLIN;
292224144Shrs	signal(SIGTERM, set_do_shutdown);
293224144Shrs	signal(SIGINT, set_do_shutdown);
294224144Shrs	signal(SIGHUP, set_do_reload);
29555505Sshin
296224144Shrs	error = csock_listen(&ctrlsock);
297224144Shrs	if (error) {
298225519Shrs		syslog(LOG_ERR, "cannot listen control socket: %s",
299225519Shrs		    strerror(errno));
300224144Shrs		exit(1);
301118911Sume	}
30262656Skris
303224144Shrs	/* load configuration file */
304224144Shrs	set_do_reload(0);
305224144Shrs
30655505Sshin	while (1) {
307224144Shrs		if (is_do_shutdown())
308224144Shrs			rtadvd_shutdown();
30962656Skris
310224144Shrs		if (is_do_reload()) {
311224144Shrs			loadconfig_ifname(reload_ifname());
312224144Shrs			if (reload_ifname() == NULL)
313224144Shrs				syslog(LOG_INFO,
314224144Shrs				    "configuration file reloaded.");
315224144Shrs			else
316224144Shrs				syslog(LOG_INFO,
317224144Shrs				    "configuration file for %s reloaded.",
318224144Shrs				    reload_ifname());
319224144Shrs			reset_do_reload();
32078064Sume		}
32178064Sume
322224144Shrs		/* timeout handler update for active interfaces */
323224144Shrs		rtadvd_update_timeout_handler();
324222972Shrs
32555505Sshin		/* timer expiration check and reset the timer */
32655505Sshin		timeout = rtadvd_check_timer();
32755505Sshin
32862656Skris		if (timeout != NULL) {
32962656Skris			syslog(LOG_DEBUG,
33097712Sume			    "<%s> set timer to %ld:%ld. waiting for "
331118660Sume			    "inputs or timeout", __func__,
33297712Sume			    (long int)timeout->tv_sec,
333253970Shrs			    (long int)timeout->tv_nsec / 1000);
33462656Skris		} else {
33562656Skris			syslog(LOG_DEBUG,
33697712Sume			    "<%s> there's no timer. waiting for inputs",
337118660Sume			    __func__);
33862656Skris		}
339224144Shrs		if ((i = poll(set, sizeof(set)/sizeof(set[0]),
340224144Shrs			    timeout ? (timeout->tv_sec * 1000 +
341253970Shrs				timeout->tv_nsec / 1000 / 1000) : INFTIM)) < 0) {
342225519Shrs
343225519Shrs			/* EINTR would occur if a signal was delivered */
34462656Skris			if (errno != EINTR)
345225519Shrs				syslog(LOG_ERR, "poll() failed: %s",
346225519Shrs				    strerror(errno));
34755505Sshin			continue;
34855505Sshin		}
34955505Sshin		if (i == 0)	/* timeout */
35055505Sshin			continue;
351224144Shrs		if (rtsock.si_fd != -1 && set[PFD_RTSOCK].revents & POLLIN)
352224144Shrs			rtmsg_input(&rtsock);
35355505Sshin
354224144Shrs		if (set[PFD_RAWSOCK].revents & POLLIN)
355224144Shrs			rtadvd_input(&sock);
356222732Shrs
357224144Shrs		if (set[PFD_CSOCK].revents & POLLIN) {
358224144Shrs			int fd;
359222732Shrs
360224144Shrs			fd = csock_accept(&ctrlsock);
361224144Shrs			if (fd == -1)
362225519Shrs				syslog(LOG_ERR,
363225519Shrs				    "cannot accept() control socket: %s",
364225519Shrs				    strerror(errno));
365224144Shrs			else {
366225519Shrs				cm_handler_server(fd);
367224144Shrs				close(fd);
368224144Shrs			}
369224144Shrs		}
370224144Shrs	}
371224144Shrs	exit(0);		/* NOTREACHED */
37262656Skris}
37362656Skris
37462656Skrisstatic void
375224144Shrsrtadvd_shutdown(void)
376222972Shrs{
377224144Shrs	struct ifinfo *ifi;
378224144Shrs	struct rainfo *rai;
379224144Shrs	struct rdnss *rdn;
380224144Shrs	struct dnssl *dns;
381222972Shrs
382224144Shrs	if (wait_shutdown) {
383224144Shrs		syslog(LOG_INFO,
384225519Shrs		    "waiting expiration of the all RA timers.");
385222972Shrs
386224144Shrs		TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
387225683Shrs			/*
388225683Shrs			 * Ignore !IFF_UP interfaces in waiting for shutdown.
389225683Shrs			 */
390225683Shrs			if (!(ifi->ifi_flags & IFF_UP) &&
391225683Shrs			    ifi->ifi_ra_timer != NULL) {
392225683Shrs				ifi->ifi_state = IFI_STATE_UNCONFIGURED;
393225683Shrs				rtadvd_remove_timer(ifi->ifi_ra_timer);
394225683Shrs				ifi->ifi_ra_timer = NULL;
395225683Shrs				syslog(LOG_DEBUG, "<%s> %s(idx=%d) is down. "
396225683Shrs				    "Timer removed and marked as UNCONFIGURED.",
397225683Shrs				     __func__, ifi->ifi_ifname,
398225683Shrs				    ifi->ifi_ifindex);
399225683Shrs			}
400225683Shrs		}
401225683Shrs		TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
402224144Shrs			if (ifi->ifi_ra_timer != NULL)
403224144Shrs				break;
404224144Shrs		}
405224144Shrs		if (ifi == NULL) {
406225519Shrs			syslog(LOG_NOTICE, "gracefully terminated.");
407224144Shrs			exit(0);
408224144Shrs		}
409222732Shrs
410224144Shrs		sleep(1);
411224144Shrs		return;
412224144Shrs	}
41378064Sume
414225519Shrs	syslog(LOG_DEBUG, "<%s> cease to be an advertising router",
415222732Shrs	    __func__);
41662656Skris
417224144Shrs	wait_shutdown = 1;
418224144Shrs
419222732Shrs	TAILQ_FOREACH(rai, &railist, rai_next) {
420222732Shrs		rai->rai_lifetime = 0;
421222732Shrs		TAILQ_FOREACH(rdn, &rai->rai_rdnss, rd_next)
422222732Shrs			rdn->rd_ltime = 0;
423222732Shrs		TAILQ_FOREACH(dns, &rai->rai_dnssl, dn_next)
424222732Shrs			dns->dn_ltime = 0;
42562656Skris	}
426224144Shrs	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
427224144Shrs		if (!ifi->ifi_persist)
428224144Shrs			continue;
429224144Shrs		if (ifi->ifi_state == IFI_STATE_UNCONFIGURED)
430224144Shrs			continue;
431224144Shrs		if (ifi->ifi_ra_timer == NULL)
432224144Shrs			continue;
433225519Shrs		if (ifi->ifi_ra_lastsent.tv_sec == 0 &&
434253970Shrs		    ifi->ifi_ra_lastsent.tv_nsec == 0 &&
435225519Shrs		    ifi->ifi_ra_timer != NULL) {
436225519Shrs			/*
437225519Shrs			 * When RA configured but never sent,
438225519Shrs			 * ignore the IF immediately.
439225519Shrs			 */
440225519Shrs			rtadvd_remove_timer(ifi->ifi_ra_timer);
441225519Shrs			ifi->ifi_ra_timer = NULL;
442225519Shrs			ifi->ifi_state = IFI_STATE_UNCONFIGURED;
443225519Shrs			continue;
444225519Shrs		}
445224144Shrs
446224144Shrs		ifi->ifi_state = IFI_STATE_TRANSITIVE;
447224144Shrs
448224144Shrs		/* Mark as the shut-down state. */
449224144Shrs		ifi->ifi_rainfo_trans = ifi->ifi_rainfo;
450224144Shrs		ifi->ifi_rainfo = NULL;
451224144Shrs
452224144Shrs		ifi->ifi_burstcount = MAX_FINAL_RTR_ADVERTISEMENTS;
453224144Shrs		ifi->ifi_burstinterval = MIN_DELAY_BETWEEN_RAS;
454224144Shrs
455224144Shrs		ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
456224144Shrs		rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
457224144Shrs		    ifi->ifi_ra_timer);
45862656Skris	}
459225519Shrs	syslog(LOG_NOTICE, "final RA transmission started.");
460224144Shrs
461216675Sdelphij	pidfile_remove(pfh);
462224144Shrs	csock_close(&ctrlsock);
46362656Skris}
46462656Skris
46562656Skrisstatic void
466224144Shrsrtmsg_input(struct sockinfo *s)
46755505Sshin{
46862656Skris	int n, type, ifindex = 0, plen;
46955505Sshin	size_t len;
47055505Sshin	char msg[2048], *next, *lim;
471224144Shrs	char ifname[IFNAMSIZ];
472222732Shrs	struct if_announcemsghdr *ifan;
473224144Shrs	struct rt_msghdr *rtm;
474222732Shrs	struct prefix *pfx;
47555505Sshin	struct rainfo *rai;
47655505Sshin	struct in6_addr *addr;
477224144Shrs	struct ifinfo *ifi;
47855505Sshin	char addrbuf[INET6_ADDRSTRLEN];
479118968Sume	int prefixchange = 0;
48055505Sshin
481224144Shrs	if (s == NULL) {
482224144Shrs		syslog(LOG_ERR, "<%s> internal error", __func__);
483224144Shrs		exit(1);
484224144Shrs	}
485224144Shrs	n = read(s->si_fd, msg, sizeof(msg));
486224144Shrs	rtm = (struct rt_msghdr *)msg;
487222732Shrs	syslog(LOG_DEBUG, "<%s> received a routing message "
488224144Shrs	    "(type = %d, len = %d)", __func__, rtm->rtm_type, n);
489222732Shrs
490224144Shrs	if (n > rtm->rtm_msglen) {
49155505Sshin		/*
492222732Shrs		 * This usually won't happen for messages received on
49362656Skris		 * a routing socket.
49455505Sshin		 */
495222732Shrs		syslog(LOG_DEBUG,
496222732Shrs		    "<%s> received data length is larger than "
497222732Shrs		    "1st routing message len. multiple messages? "
498222732Shrs		    "read %d bytes, but 1st msg len = %d",
499224144Shrs		    __func__, n, rtm->rtm_msglen);
50062656Skris#if 0
50162656Skris		/* adjust length */
502224144Shrs		n = rtm->rtm_msglen;
50362656Skris#endif
50455505Sshin	}
50555505Sshin
50655505Sshin	lim = msg + n;
50755505Sshin	for (next = msg; next < lim; next += len) {
50862656Skris		int oldifflags;
50962656Skris
51055505Sshin		next = get_next_msg(next, lim, 0, &len,
511222732Shrs		    RTADV_TYPE2BITMASK(RTM_ADD) |
512222732Shrs		    RTADV_TYPE2BITMASK(RTM_DELETE) |
513222732Shrs		    RTADV_TYPE2BITMASK(RTM_NEWADDR) |
514222732Shrs		    RTADV_TYPE2BITMASK(RTM_DELADDR) |
515222732Shrs		    RTADV_TYPE2BITMASK(RTM_IFINFO) |
516222732Shrs		    RTADV_TYPE2BITMASK(RTM_IFANNOUNCE));
51755505Sshin		if (len == 0)
51855505Sshin			break;
519224144Shrs		type = ((struct rt_msghdr *)next)->rtm_type;
52055505Sshin		switch (type) {
52155505Sshin		case RTM_ADD:
52255505Sshin		case RTM_DELETE:
52355505Sshin			ifindex = get_rtm_ifindex(next);
52455505Sshin			break;
52555505Sshin		case RTM_NEWADDR:
52655505Sshin		case RTM_DELADDR:
527224144Shrs			ifindex = (int)((struct ifa_msghdr *)next)->ifam_index;
52855505Sshin			break;
52955505Sshin		case RTM_IFINFO:
530224144Shrs			ifindex = (int)((struct if_msghdr *)next)->ifm_index;
53155505Sshin			break;
532222732Shrs		case RTM_IFANNOUNCE:
533222732Shrs			ifan = (struct if_announcemsghdr *)next;
534222732Shrs			switch (ifan->ifan_what) {
535222732Shrs			case IFAN_ARRIVAL:
536222732Shrs			case IFAN_DEPARTURE:
537222732Shrs				break;
538222732Shrs			default:
53955505Sshin				syslog(LOG_DEBUG,
540222732Shrs				    "<%s:%d> unknown ifan msg (ifan_what=%d)",
541222732Shrs				   __func__, __LINE__, ifan->ifan_what);
542222732Shrs				continue;
54355505Sshin			}
544222732Shrs
545225519Shrs			syslog(LOG_DEBUG, "<%s>: if_announcemsg (idx=%d:%d)",
546222732Shrs			       __func__, ifan->ifan_index, ifan->ifan_what);
547222732Shrs			switch (ifan->ifan_what) {
548222732Shrs			case IFAN_ARRIVAL:
549225519Shrs				syslog(LOG_NOTICE,
550225519Shrs				    "interface added (idx=%d)",
551225519Shrs				    ifan->ifan_index);
552224144Shrs				update_ifinfo(&ifilist, ifan->ifan_index);
553224144Shrs				loadconfig_index(ifan->ifan_index);
554222732Shrs				break;
555222732Shrs			case IFAN_DEPARTURE:
556225519Shrs				syslog(LOG_NOTICE,
557225519Shrs				    "interface removed (idx=%d)",
558225519Shrs				    ifan->ifan_index);
559224144Shrs				rm_ifinfo_index(ifan->ifan_index);
560224144Shrs
561224144Shrs				/* Clear ifi_ifindex */
562224144Shrs				TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
563224144Shrs					if (ifi->ifi_ifindex
564224144Shrs					    == ifan->ifan_index) {
565224144Shrs						ifi->ifi_ifindex = 0;
566224144Shrs						break;
567224144Shrs					}
568224144Shrs				}
569224144Shrs				update_ifinfo(&ifilist, ifan->ifan_index);
570222732Shrs				break;
571222732Shrs			}
57262656Skris			continue;
573222732Shrs		default:
574222732Shrs			/* should not reach here */
575222732Shrs			syslog(LOG_DEBUG,
576222732Shrs			       "<%s:%d> unknown rtmsg %d on %s",
577222732Shrs			       __func__, __LINE__, type,
578222732Shrs			       if_indextoname(ifindex, ifname));
579222732Shrs			continue;
58055505Sshin		}
581224144Shrs		ifi = if_indextoifinfo(ifindex);
582224144Shrs		if (ifi == NULL) {
583222732Shrs			syslog(LOG_DEBUG,
584224144Shrs			    "<%s> ifinfo not found for idx=%d.  Why?",
585224144Shrs			    __func__, ifindex);
58662656Skris			continue;
58755505Sshin		}
588224144Shrs		rai = ifi->ifi_rainfo;
589224144Shrs		if (rai == NULL) {
590224144Shrs			syslog(LOG_DEBUG,
591224144Shrs			    "<%s> route changed on "
592224144Shrs			    "non advertising interface(%s)",
593224144Shrs			    __func__, ifi->ifi_ifname);
594224144Shrs			continue;
595224144Shrs		}
59655505Sshin
597224144Shrs		oldifflags = ifi->ifi_flags;
598224144Shrs		/* init ifflags because it may have changed */
599224144Shrs		update_ifinfo(&ifilist, ifindex);
600224144Shrs
60197712Sume		switch (type) {
60297712Sume		case RTM_ADD:
60397712Sume			if (sflag)
60497712Sume				break;	/* we aren't interested in prefixes  */
60562656Skris
60697712Sume			addr = get_addr(msg);
60797712Sume			plen = get_prefixlen(msg);
60897712Sume			/* sanity check for plen */
60997712Sume			/* as RFC2373, prefixlen is at least 4 */
61097712Sume			if (plen < 4 || plen > 127) {
61155505Sshin				syslog(LOG_INFO, "<%s> new interface route's"
61297712Sume				    "plen %d is invalid for a prefix",
613118660Sume				    __func__, plen);
61462656Skris				break;
61597712Sume			}
616222732Shrs			pfx = find_prefix(rai, addr, plen);
617222732Shrs			if (pfx) {
618222732Shrs				if (pfx->pfx_timer) {
61998172Sume					/*
62098172Sume					 * If the prefix has been invalidated,
62198172Sume					 * make it available again.
62298172Sume					 */
623222732Shrs					update_prefix(pfx);
624118968Sume					prefixchange = 1;
625222732Shrs				} else
62697712Sume					syslog(LOG_DEBUG,
62797712Sume					    "<%s> new prefix(%s/%d) "
62897712Sume					    "added on %s, "
62997712Sume					    "but it was already in list",
630118660Sume					    __func__,
63197712Sume					    inet_ntop(AF_INET6, addr,
632222732Shrs						(char *)addrbuf,
633222732Shrs						sizeof(addrbuf)),
634224144Shrs					    plen, ifi->ifi_ifname);
63597712Sume				break;
63697712Sume			}
63797712Sume			make_prefix(rai, ifindex, addr, plen);
638118968Sume			prefixchange = 1;
63997712Sume			break;
64097712Sume		case RTM_DELETE:
64197712Sume			if (sflag)
64297712Sume				break;
64362656Skris
64497712Sume			addr = get_addr(msg);
64597712Sume			plen = get_prefixlen(msg);
64697712Sume			/* sanity check for plen */
64797712Sume			/* as RFC2373, prefixlen is at least 4 */
64897712Sume			if (plen < 4 || plen > 127) {
64997712Sume				syslog(LOG_INFO,
65097712Sume				    "<%s> deleted interface route's "
65197712Sume				    "plen %d is invalid for a prefix",
652118660Sume				    __func__, plen);
65362656Skris				break;
65497712Sume			}
655222732Shrs			pfx = find_prefix(rai, addr, plen);
656222732Shrs			if (pfx == NULL) {
657222732Shrs				syslog(LOG_DEBUG,
658222732Shrs				    "<%s> prefix(%s/%d) was deleted on %s, "
659222732Shrs				    "but it was not in list",
660222732Shrs				    __func__, inet_ntop(AF_INET6, addr,
661222732Shrs					(char *)addrbuf, sizeof(addrbuf)),
662224144Shrs					plen, ifi->ifi_ifname);
66397712Sume				break;
66497712Sume			}
665222732Shrs			invalidate_prefix(pfx);
666118968Sume			prefixchange = 1;
66797712Sume			break;
66855505Sshin		case RTM_NEWADDR:
66955505Sshin		case RTM_DELADDR:
67055505Sshin		case RTM_IFINFO:
67197712Sume			break;
67255505Sshin		default:
67355505Sshin			/* should not reach here */
674222732Shrs			syslog(LOG_DEBUG,
675222732Shrs			    "<%s:%d> unknown rtmsg %d on %s",
676222732Shrs			    __func__, __LINE__, type,
677222732Shrs			    if_indextoname(ifindex, ifname));
67855505Sshin			return;
67955505Sshin		}
68062656Skris
68162656Skris		/* check if an interface flag is changed */
682118664Sume		if ((oldifflags & IFF_UP) && /* UP to DOWN */
683224144Shrs		    !(ifi->ifi_flags & IFF_UP)) {
684225519Shrs			syslog(LOG_NOTICE,
685225519Shrs			    "<interface %s becomes down. stop timer.",
686225519Shrs			    ifi->ifi_ifname);
687224144Shrs			rtadvd_remove_timer(ifi->ifi_ra_timer);
688224144Shrs			ifi->ifi_ra_timer = NULL;
689118664Sume		} else if (!(oldifflags & IFF_UP) && /* DOWN to UP */
690224144Shrs		    (ifi->ifi_flags & IFF_UP)) {
691225519Shrs			syslog(LOG_NOTICE,
692225519Shrs			    "interface %s becomes up. restart timer.",
693225519Shrs			    ifi->ifi_ifname);
69462656Skris
695224144Shrs			ifi->ifi_state = IFI_STATE_TRANSITIVE;
696224144Shrs			ifi->ifi_burstcount =
697224144Shrs			    MAX_INITIAL_RTR_ADVERTISEMENTS;
698224144Shrs			ifi->ifi_burstinterval =
699224144Shrs			    MAX_INITIAL_RTR_ADVERT_INTERVAL;
700224144Shrs
701224144Shrs			ifi->ifi_ra_timer = rtadvd_add_timer(ra_timeout,
702224144Shrs			    ra_timer_update, ifi, ifi);
703224144Shrs			ra_timer_update(ifi, &ifi->ifi_ra_timer->rat_tm);
704224144Shrs			rtadvd_set_timer(&ifi->ifi_ra_timer->rat_tm,
705224144Shrs			    ifi->ifi_ra_timer);
706118968Sume		} else if (prefixchange &&
707224144Shrs		    (ifi->ifi_flags & IFF_UP)) {
708118968Sume			/*
709118968Sume			 * An advertised prefix has been added or invalidated.
710118968Sume			 * Will notice the change in a short delay.
711118968Sume			 */
712224144Shrs			set_short_delay(ifi);
71362656Skris		}
71455505Sshin	}
71555505Sshin
71655505Sshin	return;
71755505Sshin}
71855505Sshin
71955505Sshinvoid
720224144Shrsrtadvd_input(struct sockinfo *s)
72155505Sshin{
722222732Shrs	ssize_t i;
72355505Sshin	int *hlimp = NULL;
72462656Skris#ifdef OLDRAWSOCKET
72562656Skris	struct ip6_hdr *ip;
726222732Shrs#endif
72755505Sshin	struct icmp6_hdr *icp;
72855505Sshin	int ifindex = 0;
72955505Sshin	struct cmsghdr *cm;
73055505Sshin	struct in6_pktinfo *pi = NULL;
731224144Shrs	char ntopbuf[INET6_ADDRSTRLEN], ifnamebuf[IFNAMSIZ];
73255505Sshin	struct in6_addr dst = in6addr_any;
733224144Shrs	struct ifinfo *ifi;
73455505Sshin
735224144Shrs	syslog(LOG_DEBUG, "<%s> enter", __func__);
736224144Shrs
737224144Shrs	if (s == NULL) {
738224144Shrs		syslog(LOG_ERR, "<%s> internal error", __func__);
739224144Shrs		exit(1);
740224144Shrs	}
74155505Sshin	/*
74255505Sshin	 * Get message. We reset msg_controllen since the field could
74355505Sshin	 * be modified if we had received a message before setting
74455505Sshin	 * receive options.
74555505Sshin	 */
74662656Skris	rcvmhdr.msg_controllen = rcvcmsgbuflen;
747224144Shrs	if ((i = recvmsg(s->si_fd, &rcvmhdr, 0)) < 0)
74855505Sshin		return;
74955505Sshin
75055505Sshin	/* extract optional information via Advanced API */
75155505Sshin	for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(&rcvmhdr);
75255505Sshin	     cm;
75355505Sshin	     cm = (struct cmsghdr *)CMSG_NXTHDR(&rcvmhdr, cm)) {
75455505Sshin		if (cm->cmsg_level == IPPROTO_IPV6 &&
75555505Sshin		    cm->cmsg_type == IPV6_PKTINFO &&
75655505Sshin		    cm->cmsg_len == CMSG_LEN(sizeof(struct in6_pktinfo))) {
75755505Sshin			pi = (struct in6_pktinfo *)(CMSG_DATA(cm));
75855505Sshin			ifindex = pi->ipi6_ifindex;
75955505Sshin			dst = pi->ipi6_addr;
76055505Sshin		}
76155505Sshin		if (cm->cmsg_level == IPPROTO_IPV6 &&
76255505Sshin		    cm->cmsg_type == IPV6_HOPLIMIT &&
76355505Sshin		    cm->cmsg_len == CMSG_LEN(sizeof(int)))
76455505Sshin			hlimp = (int *)CMSG_DATA(cm);
76555505Sshin	}
76655505Sshin	if (ifindex == 0) {
767225519Shrs		syslog(LOG_ERR, "failed to get receiving interface");
76855505Sshin		return;
76955505Sshin	}
77055505Sshin	if (hlimp == NULL) {
771225519Shrs		syslog(LOG_ERR, "failed to get receiving hop limit");
77255505Sshin		return;
77355505Sshin	}
77455505Sshin
77562656Skris	/*
776219184Sbz	 * If we happen to receive data on an interface which is now gone
777219184Sbz	 * or down, just discard the data.
77862656Skris	 */
779224144Shrs	ifi = if_indextoifinfo(pi->ipi6_ifindex);
780224144Shrs	if (ifi == NULL || !(ifi->ifi_flags & IFF_UP)) {
78162656Skris		syslog(LOG_INFO,
782222732Shrs		    "<%s> received data on a disabled interface (%s)",
783222732Shrs		    __func__,
784224144Shrs		    (ifi == NULL) ? "[gone]" : ifi->ifi_ifname);
78562656Skris		return;
78662656Skris	}
78762656Skris
78862656Skris#ifdef OLDRAWSOCKET
789222732Shrs	if ((size_t)i < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) {
79062656Skris		syslog(LOG_ERR,
791225519Shrs		    "packet size(%d) is too short", i);
79262656Skris		return;
79362656Skris	}
79462656Skris
79562656Skris	ip = (struct ip6_hdr *)rcvmhdr.msg_iov[0].iov_base;
79662656Skris	icp = (struct icmp6_hdr *)(ip + 1); /* XXX: ext. hdr? */
79762656Skris#else
798222732Shrs	if ((size_t)i < sizeof(struct icmp6_hdr)) {
799225519Shrs		syslog(LOG_ERR, "packet size(%zd) is too short", i);
80055505Sshin		return;
80155505Sshin	}
80255505Sshin
80355505Sshin	icp = (struct icmp6_hdr *)rcvmhdr.msg_iov[0].iov_base;
80462656Skris#endif
80555505Sshin
80697712Sume	switch (icp->icmp6_type) {
80797712Sume	case ND_ROUTER_SOLICIT:
80897712Sume		/*
809222732Shrs		 * Message verification - RFC 4861 6.1.1
81097712Sume		 * XXX: these checks must be done in the kernel as well,
81197712Sume		 *      but we can't completely rely on them.
81297712Sume		 */
81397712Sume		if (*hlimp != 255) {
81497712Sume			syslog(LOG_NOTICE,
815225519Shrs			    "RS with invalid hop limit(%d) "
81697712Sume			    "received from %s on %s",
817225519Shrs			    *hlimp,
818118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
819222732Shrs			    sizeof(ntopbuf)),
82097712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
82197712Sume			return;
82297712Sume		}
82397712Sume		if (icp->icmp6_code) {
82497712Sume			syslog(LOG_NOTICE,
825225519Shrs			    "RS with invalid ICMP6 code(%d) "
82697712Sume			    "received from %s on %s",
827225519Shrs			    icp->icmp6_code,
828118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
829222732Shrs			    sizeof(ntopbuf)),
83097712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
83197712Sume			return;
83297712Sume		}
833222732Shrs		if ((size_t)i < sizeof(struct nd_router_solicit)) {
83497712Sume			syslog(LOG_NOTICE,
835225519Shrs			    "RS from %s on %s does not have enough "
836222743Shrs			    "length (len = %zd)",
837118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
838222732Shrs			    sizeof(ntopbuf)),
83997712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
84097712Sume			return;
84197712Sume		}
842118913Sume		rs_input(i, (struct nd_router_solicit *)icp, pi, &rcvfrom);
84397712Sume		break;
84497712Sume	case ND_ROUTER_ADVERT:
84597712Sume		/*
846222732Shrs		 * Message verification - RFC 4861 6.1.2
847222732Shrs		 * XXX: there's the same dilemma as above...
84897712Sume		 */
849222732Shrs		if (!IN6_IS_ADDR_LINKLOCAL(&rcvfrom.sin6_addr)) {
850222732Shrs			syslog(LOG_NOTICE,
851228990Suqs			    "RA with non-linklocal source address "
852222732Shrs			    "received from %s on %s",
853225519Shrs			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr,
854222732Shrs			    ntopbuf, sizeof(ntopbuf)),
855222732Shrs			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
856222732Shrs			return;
857222732Shrs		}
85897712Sume		if (*hlimp != 255) {
85997712Sume			syslog(LOG_NOTICE,
860225519Shrs			    "RA with invalid hop limit(%d) "
86197712Sume			    "received from %s on %s",
862225519Shrs			    *hlimp,
863118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
864222732Shrs			    sizeof(ntopbuf)),
86597712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
86697712Sume			return;
86797712Sume		}
86897712Sume		if (icp->icmp6_code) {
86997712Sume			syslog(LOG_NOTICE,
870225519Shrs			    "RA with invalid ICMP6 code(%d) "
87197712Sume			    "received from %s on %s",
872225519Shrs			    icp->icmp6_code,
873118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
874222732Shrs			    sizeof(ntopbuf)),
87597712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
87697712Sume			return;
87797712Sume		}
878222732Shrs		if ((size_t)i < sizeof(struct nd_router_advert)) {
87997712Sume			syslog(LOG_NOTICE,
880225519Shrs			    "RA from %s on %s does not have enough "
881222743Shrs			    "length (len = %zd)",
882118913Sume			    inet_ntop(AF_INET6, &rcvfrom.sin6_addr, ntopbuf,
883222732Shrs			    sizeof(ntopbuf)),
88497712Sume			    if_indextoname(pi->ipi6_ifindex, ifnamebuf), i);
88597712Sume			return;
88697712Sume		}
887118913Sume		ra_input(i, (struct nd_router_advert *)icp, pi, &rcvfrom);
88897712Sume		break;
88997712Sume	case ICMP6_ROUTER_RENUMBERING:
890224144Shrs		if (mcastif == NULL) {
891225519Shrs			syslog(LOG_ERR, "received a router renumbering "
892225519Shrs			    "message, but not allowed to be accepted");
89397712Sume			break;
89497712Sume		}
895118913Sume		rr_input(i, (struct icmp6_router_renum *)icp, pi, &rcvfrom,
896222732Shrs		    &dst);
89797712Sume		break;
89897712Sume	default:
89997712Sume		/*
90097712Sume		 * Note that this case is POSSIBLE, especially just
90197712Sume		 * after invocation of the daemon. This is because we
90297712Sume		 * could receive message after opening the socket and
90397712Sume		 * before setting ICMP6 type filter(see sock_open()).
90497712Sume		 */
905225519Shrs		syslog(LOG_ERR, "invalid icmp type(%d)", icp->icmp6_type);
90697712Sume		return;
90755505Sshin	}
90855505Sshin
90955505Sshin	return;
91055505Sshin}
91155505Sshin
91255505Sshinstatic void
91355505Sshinrs_input(int len, struct nd_router_solicit *rs,
91455505Sshin	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
91555505Sshin{
916224144Shrs	char ntopbuf[INET6_ADDRSTRLEN];
917224144Shrs	char ifnamebuf[IFNAMSIZ];
918222732Shrs	union nd_opt ndopts;
919222732Shrs	struct rainfo *rai;
920224144Shrs	struct ifinfo *ifi;
921118968Sume	struct soliciter *sol;
92255505Sshin
92355505Sshin	syslog(LOG_DEBUG,
924222732Shrs	    "<%s> RS received from %s on %s",
925222732Shrs	    __func__,
926222732Shrs	    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
927222732Shrs	    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
92855505Sshin
92955505Sshin	/* ND option check */
93055505Sshin	memset(&ndopts, 0, sizeof(ndopts));
931225519Shrs	TAILQ_INIT(&ndopts.opt_list);
93255505Sshin	if (nd6_options((struct nd_opt_hdr *)(rs + 1),
93355505Sshin			len - sizeof(struct nd_router_solicit),
934118664Sume			&ndopts, NDOPT_FLAG_SRCLINKADDR)) {
935151470Ssuz		syslog(LOG_INFO,
936222732Shrs		    "<%s> ND option check failed for an RS from %s on %s",
937222732Shrs		    __func__,
938222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
939222732Shrs			sizeof(ntopbuf)),
940222732Shrs		    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
94155505Sshin		return;
94255505Sshin	}
94355505Sshin
94455505Sshin	/*
94555505Sshin	 * If the IP source address is the unspecified address, there
94655505Sshin	 * must be no source link-layer address option in the message.
947222732Shrs	 * (RFC 4861 6.1.1)
94855505Sshin	 */
94955505Sshin	if (IN6_IS_ADDR_UNSPECIFIED(&from->sin6_addr) &&
950222732Shrs	    ndopts.opt_src_lladdr) {
951151470Ssuz		syslog(LOG_INFO,
952222732Shrs		    "<%s> RS from unspecified src on %s has a link-layer"
953222732Shrs		    " address option",
954222732Shrs		    __func__, if_indextoname(pi->ipi6_ifindex, ifnamebuf));
95555505Sshin		goto done;
95655505Sshin	}
95755505Sshin
958224144Shrs	ifi = if_indextoifinfo(pi->ipi6_ifindex);
959224144Shrs	if (ifi == NULL) {
960224144Shrs		syslog(LOG_INFO,
961224144Shrs		    "<%s> if (idx=%d) not found.  Why?",
962224144Shrs		    __func__, pi->ipi6_ifindex);
963224144Shrs		goto done;
964224144Shrs	}
965224144Shrs	rai = ifi->ifi_rainfo;
966222732Shrs	if (rai == NULL) {
96755505Sshin		syslog(LOG_INFO,
96855505Sshin		       "<%s> RS received on non advertising interface(%s)",
969118660Sume		       __func__,
97055505Sshin		       if_indextoname(pi->ipi6_ifindex, ifnamebuf));
97155505Sshin		goto done;
97255505Sshin	}
97355505Sshin
974224144Shrs	rai->rai_ifinfo->ifi_rsinput++;
97562656Skris
97655505Sshin	/*
97755505Sshin	 * Decide whether to send RA according to the rate-limit
97855505Sshin	 * consideration.
97955505Sshin	 */
98055505Sshin
981118968Sume	/* record sockaddr waiting for RA, if possible */
982118968Sume	sol = (struct soliciter *)malloc(sizeof(*sol));
983118968Sume	if (sol) {
984222732Shrs		sol->sol_addr = *from;
985222732Shrs		/* XXX RFC 2553 need clarification on flowinfo */
986222732Shrs		sol->sol_addr.sin6_flowinfo = 0;
987222732Shrs		TAILQ_INSERT_TAIL(&rai->rai_soliciter, sol, sol_next);
988118968Sume	}
98962656Skris
990118968Sume	/*
991118968Sume	 * If there is already a waiting RS packet, don't
992118968Sume	 * update the timer.
993118968Sume	 */
994224144Shrs	if (ifi->ifi_rs_waitcount++)
995118968Sume		goto done;
99655505Sshin
997224144Shrs	set_short_delay(ifi);
99855505Sshin
99955505Sshin  done:
100055505Sshin	free_ndopts(&ndopts);
100155505Sshin	return;
100255505Sshin}
100355505Sshin
100455505Sshinstatic void
1005224144Shrsset_short_delay(struct ifinfo *ifi)
1006118968Sume{
1007118968Sume	long delay;	/* must not be greater than 1000000 */
1008253970Shrs	struct timespec interval, now, min_delay, tm_tmp, *rest;
1009118968Sume
1010247863Shrs	if (ifi->ifi_ra_timer == NULL)
1011247863Shrs		return;
1012118968Sume	/*
1013118968Sume	 * Compute a random delay. If the computed value
1014118968Sume	 * corresponds to a time later than the time the next
1015118968Sume	 * multicast RA is scheduled to be sent, ignore the random
1016118968Sume	 * delay and send the advertisement at the
1017222732Shrs	 * already-scheduled time. RFC 4861 6.2.6
1018118968Sume	 */
1019118968Sume#ifdef HAVE_ARC4RANDOM
1020180823Sache	delay = arc4random_uniform(MAX_RA_DELAY_TIME);
1021118968Sume#else
1022118968Sume	delay = random() % MAX_RA_DELAY_TIME;
1023118968Sume#endif
1024118968Sume	interval.tv_sec = 0;
1025253970Shrs	interval.tv_nsec = delay * 1000;
1026224144Shrs	rest = rtadvd_timer_rest(ifi->ifi_ra_timer);
1027253970Shrs	if (TS_CMP(rest, &interval, <)) {
1028118968Sume		syslog(LOG_DEBUG, "<%s> random delay is larger than "
1029118968Sume		    "the rest of the current timer", __func__);
1030118968Sume		interval = *rest;
1031118968Sume	}
1032118968Sume
1033118968Sume	/*
1034118968Sume	 * If we sent a multicast Router Advertisement within
1035118968Sume	 * the last MIN_DELAY_BETWEEN_RAS seconds, schedule
1036118968Sume	 * the advertisement to be sent at a time corresponding to
1037118968Sume	 * MIN_DELAY_BETWEEN_RAS plus the random value after the
1038118968Sume	 * previous advertisement was sent.
1039118968Sume	 */
1040253970Shrs	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
1041253970Shrs	TS_SUB(&now, &ifi->ifi_ra_lastsent, &tm_tmp);
1042118968Sume	min_delay.tv_sec = MIN_DELAY_BETWEEN_RAS;
1043253970Shrs	min_delay.tv_nsec = 0;
1044253970Shrs	if (TS_CMP(&tm_tmp, &min_delay, <)) {
1045253970Shrs		TS_SUB(&min_delay, &tm_tmp, &min_delay);
1046253970Shrs		TS_ADD(&min_delay, &interval, &interval);
1047118968Sume	}
1048224144Shrs	rtadvd_set_timer(&interval, ifi->ifi_ra_timer);
1049118968Sume}
1050118968Sume
1051222732Shrsstatic int
1052222732Shrscheck_accept_rtadv(int idx)
1053222732Shrs{
1054224144Shrs	struct ifinfo *ifi;
1055222732Shrs
1056224144Shrs	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1057224144Shrs		if (ifi->ifi_ifindex == idx)
1058224144Shrs			break;
1059224144Shrs	}
1060224144Shrs	if (ifi == NULL) {
1061225519Shrs		syslog(LOG_DEBUG,
1062224144Shrs		    "<%s> if (idx=%d) not found.  Why?",
1063222732Shrs		    __func__, idx);
1064222732Shrs		return (0);
1065222732Shrs	}
1066224144Shrs#if (__FreeBSD_version < 900000)
1067224144Shrs	/*
1068224144Shrs	 * RA_RECV: !ip6.forwarding && ip6.accept_rtadv
1069224144Shrs	 * RA_SEND: ip6.forwarding
1070224144Shrs	 */
1071224144Shrs	return ((getinet6sysctl(IPV6CTL_FORWARDING) == 0) &&
1072224144Shrs	    (getinet6sysctl(IPV6CTL_ACCEPT_RTADV) == 1));
1073224144Shrs#else
1074224144Shrs	/*
1075224144Shrs	 * RA_RECV: ND6_IFF_ACCEPT_RTADV
1076224144Shrs	 * RA_SEND: ip6.forwarding
1077224144Shrs	 */
1078224144Shrs	if (update_ifinfo_nd_flags(ifi) != 0) {
1079225519Shrs		syslog(LOG_ERR, "cannot get nd6 flags (idx=%d)", idx);
1080222732Shrs		return (0);
1081222732Shrs	}
1082222732Shrs
1083224144Shrs	return (ifi->ifi_nd_flags & ND6_IFF_ACCEPT_RTADV);
1084224144Shrs#endif
1085222732Shrs}
1086222732Shrs
1087118968Sumestatic void
1088222732Shrsra_input(int len, struct nd_router_advert *nra,
108955505Sshin	 struct in6_pktinfo *pi, struct sockaddr_in6 *from)
109055505Sshin{
109155505Sshin	struct rainfo *rai;
1092224144Shrs	struct ifinfo *ifi;
1093224144Shrs	char ntopbuf[INET6_ADDRSTRLEN];
1094224144Shrs	char ifnamebuf[IFNAMSIZ];
1095222732Shrs	union nd_opt ndopts;
1096222732Shrs	const char *on_off[] = {"OFF", "ON"};
1097224144Shrs	uint32_t reachabletime, retranstimer, mtu;
109862656Skris	int inconsistent = 0;
1099222732Shrs	int error;
110055505Sshin
1101222732Shrs	syslog(LOG_DEBUG, "<%s> RA received from %s on %s", __func__,
1102222732Shrs	    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf, sizeof(ntopbuf)),
1103222732Shrs	    if_indextoname(pi->ipi6_ifindex, ifnamebuf));
1104222732Shrs
110555505Sshin	/* ND option check */
110655505Sshin	memset(&ndopts, 0, sizeof(ndopts));
1107225519Shrs	TAILQ_INIT(&ndopts.opt_list);
1108222732Shrs	error = nd6_options((struct nd_opt_hdr *)(nra + 1),
1109222732Shrs	    len - sizeof(struct nd_router_advert), &ndopts,
1110222732Shrs	    NDOPT_FLAG_SRCLINKADDR | NDOPT_FLAG_PREFIXINFO | NDOPT_FLAG_MTU |
1111222732Shrs	    NDOPT_FLAG_RDNSS | NDOPT_FLAG_DNSSL);
1112222732Shrs	if (error) {
1113151470Ssuz		syslog(LOG_INFO,
1114222732Shrs		    "<%s> ND option check failed for an RA from %s on %s",
1115222732Shrs		    __func__,
1116222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1117222732Shrs			sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1118222732Shrs			ifnamebuf));
111955505Sshin		return;
112055505Sshin	}
112155505Sshin
112255505Sshin	/*
1123222732Shrs	 * RA consistency check according to RFC 4861 6.2.7
112455505Sshin	 */
1125224144Shrs	ifi = if_indextoifinfo(pi->ipi6_ifindex);
1126224144Shrs	if (ifi->ifi_rainfo == NULL) {
112755505Sshin		syslog(LOG_INFO,
1128222732Shrs		    "<%s> received RA from %s on non-advertising"
1129222732Shrs		    " interface(%s)",
1130222732Shrs		    __func__,
1131222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1132222732Shrs			sizeof(ntopbuf)), if_indextoname(pi->ipi6_ifindex,
1133222732Shrs			ifnamebuf));
113455505Sshin		goto done;
113555505Sshin	}
1136224144Shrs	rai = ifi->ifi_rainfo;
1137224144Shrs	ifi->ifi_rainput++;
1138225519Shrs	syslog(LOG_DEBUG, "<%s> ifi->ifi_rainput = %" PRIu64, __func__,
1139224144Shrs	    ifi->ifi_rainput);
1140222732Shrs
114155505Sshin	/* Cur Hop Limit value */
1142222732Shrs	if (nra->nd_ra_curhoplimit && rai->rai_hoplimit &&
1143222732Shrs	    nra->nd_ra_curhoplimit != rai->rai_hoplimit) {
1144225519Shrs		syslog(LOG_NOTICE,
1145225519Shrs		    "CurHopLimit inconsistent on %s:"
1146222732Shrs		    " %d from %s, %d from us",
1147225519Shrs		    ifi->ifi_ifname, nra->nd_ra_curhoplimit,
1148222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1149222732Shrs			sizeof(ntopbuf)), rai->rai_hoplimit);
115062656Skris		inconsistent++;
115155505Sshin	}
115255505Sshin	/* M flag */
1153222732Shrs	if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_MANAGED) !=
1154222732Shrs	    rai->rai_managedflg) {
1155225519Shrs		syslog(LOG_NOTICE,
1156225519Shrs		    "M flag inconsistent on %s:"
1157222732Shrs		    " %s from %s, %s from us",
1158225519Shrs		    ifi->ifi_ifname, on_off[!rai->rai_managedflg],
1159222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1160222732Shrs			sizeof(ntopbuf)), on_off[rai->rai_managedflg]);
116162656Skris		inconsistent++;
116255505Sshin	}
116355505Sshin	/* O flag */
1164222732Shrs	if ((nra->nd_ra_flags_reserved & ND_RA_FLAG_OTHER) !=
1165222732Shrs	    rai->rai_otherflg) {
1166225519Shrs		syslog(LOG_NOTICE,
1167225519Shrs		    "O flag inconsistent on %s:"
1168222732Shrs		    " %s from %s, %s from us",
1169225519Shrs		    ifi->ifi_ifname, on_off[!rai->rai_otherflg],
1170222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1171222732Shrs			sizeof(ntopbuf)), on_off[rai->rai_otherflg]);
117262656Skris		inconsistent++;
117355505Sshin	}
117455505Sshin	/* Reachable Time */
1175222732Shrs	reachabletime = ntohl(nra->nd_ra_reachable);
1176222732Shrs	if (reachabletime && rai->rai_reachabletime &&
1177222732Shrs	    reachabletime != rai->rai_reachabletime) {
1178225519Shrs		syslog(LOG_NOTICE,
1179225519Shrs		    "ReachableTime inconsistent on %s:"
1180222732Shrs		    " %d from %s, %d from us",
1181225519Shrs		    ifi->ifi_ifname, reachabletime,
1182222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1183222732Shrs			sizeof(ntopbuf)), rai->rai_reachabletime);
118462656Skris		inconsistent++;
118555505Sshin	}
118655505Sshin	/* Retrans Timer */
1187222732Shrs	retranstimer = ntohl(nra->nd_ra_retransmit);
1188222732Shrs	if (retranstimer && rai->rai_retranstimer &&
1189222732Shrs	    retranstimer != rai->rai_retranstimer) {
1190225519Shrs		syslog(LOG_NOTICE,
1191225519Shrs		    "RetranceTimer inconsistent on %s:"
1192222732Shrs		    " %d from %s, %d from us",
1193225519Shrs		    ifi->ifi_ifname, retranstimer,
1194222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1195222732Shrs			sizeof(ntopbuf)), rai->rai_retranstimer);
119662656Skris		inconsistent++;
119755505Sshin	}
119855505Sshin	/* Values in the MTU options */
1199222732Shrs	if (ndopts.opt_mtu) {
1200222732Shrs		mtu = ntohl(ndopts.opt_mtu->nd_opt_mtu_mtu);
1201222732Shrs		if (mtu && rai->rai_linkmtu && mtu != rai->rai_linkmtu) {
1202225519Shrs			syslog(LOG_NOTICE,
1203225519Shrs			    "MTU option value inconsistent on %s:"
1204222732Shrs			    " %d from %s, %d from us",
1205225519Shrs			    ifi->ifi_ifname, mtu,
1206222732Shrs			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1207222732Shrs				sizeof(ntopbuf)), rai->rai_linkmtu);
120862656Skris			inconsistent++;
120955505Sshin		}
121055505Sshin	}
121155505Sshin	/* Preferred and Valid Lifetimes for prefixes */
121255505Sshin	{
1213222732Shrs		struct nd_optlist *nol;
121478064Sume
1215222732Shrs		if (ndopts.opt_pi)
1216222732Shrs			if (prefix_check(ndopts.opt_pi, rai, from))
121762656Skris				inconsistent++;
1218222732Shrs
1219222732Shrs		TAILQ_FOREACH(nol, &ndopts.opt_list, nol_next)
1220222732Shrs			if (prefix_check((struct nd_opt_prefix_info *)nol->nol_opt,
1221222732Shrs				rai, from))
122262656Skris				inconsistent++;
122355505Sshin	}
122455505Sshin
122578064Sume	if (inconsistent)
1226224144Shrs		ifi->ifi_rainconsistent++;
1227222732Shrs
122855505Sshin  done:
122955505Sshin	free_ndopts(&ndopts);
123055505Sshin	return;
123155505Sshin}
123255505Sshin
123362656Skris/* return a non-zero value if the received prefix is inconsitent with ours */
123462656Skrisstatic int
123555505Sshinprefix_check(struct nd_opt_prefix_info *pinfo,
1236222732Shrs	struct rainfo *rai, struct sockaddr_in6 *from)
123755505Sshin{
1238224144Shrs	struct ifinfo *ifi;
1239224144Shrs	uint32_t preferred_time, valid_time;
1240222732Shrs	struct prefix *pfx;
124162656Skris	int inconsistent = 0;
1242224144Shrs	char ntopbuf[INET6_ADDRSTRLEN];
1243224144Shrs	char prefixbuf[INET6_ADDRSTRLEN];
1244253970Shrs	struct timespec now;
124555505Sshin
124662656Skris#if 0				/* impossible */
124762656Skris	if (pinfo->nd_opt_pi_type != ND_OPT_PREFIX_INFORMATION)
1248222732Shrs		return (0);
124962656Skris#endif
1250224144Shrs	ifi = rai->rai_ifinfo;
125155505Sshin	/*
125255505Sshin	 * log if the adveritsed prefix has link-local scope(sanity check?)
125355505Sshin	 */
1254222732Shrs	if (IN6_IS_ADDR_LINKLOCAL(&pinfo->nd_opt_pi_prefix))
125555505Sshin		syslog(LOG_INFO,
1256222732Shrs		    "<%s> link-local prefix %s/%d is advertised "
1257222732Shrs		    "from %s on %s",
1258222732Shrs		    __func__,
1259222732Shrs		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1260222732Shrs			sizeof(prefixbuf)),
1261222732Shrs		    pinfo->nd_opt_pi_prefix_len,
1262222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1263224144Shrs			sizeof(ntopbuf)), ifi->ifi_ifname);
126455505Sshin
1265222732Shrs	if ((pfx = find_prefix(rai, &pinfo->nd_opt_pi_prefix,
1266222732Shrs		pinfo->nd_opt_pi_prefix_len)) == NULL) {
126755505Sshin		syslog(LOG_INFO,
1268222732Shrs		    "<%s> prefix %s/%d from %s on %s is not in our list",
1269222732Shrs		    __func__,
1270222732Shrs		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1271222732Shrs			sizeof(prefixbuf)),
1272222732Shrs		    pinfo->nd_opt_pi_prefix_len,
1273222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1274224144Shrs			sizeof(ntopbuf)), ifi->ifi_ifname);
1275222732Shrs		return (0);
127655505Sshin	}
127755505Sshin
127855505Sshin	preferred_time = ntohl(pinfo->nd_opt_pi_preferred_time);
1279222732Shrs	if (pfx->pfx_pltimeexpire) {
128078064Sume		/*
128178064Sume		 * The lifetime is decremented in real time, so we should
128278064Sume		 * compare the expiration time.
128378064Sume		 * (RFC 2461 Section 6.2.7.)
128478064Sume		 * XXX: can we really expect that all routers on the link
128578064Sume		 * have synchronized clocks?
128678064Sume		 */
1287253970Shrs		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
128878064Sume		preferred_time += now.tv_sec;
128978064Sume
1290222732Shrs		if (!pfx->pfx_timer && rai->rai_clockskew &&
1291222732Shrs		    abs(preferred_time - pfx->pfx_pltimeexpire) > rai->rai_clockskew) {
129278064Sume			syslog(LOG_INFO,
1293222732Shrs			    "<%s> preferred lifetime for %s/%d"
1294222732Shrs			    " (decr. in real time) inconsistent on %s:"
1295224144Shrs			    " %" PRIu32 " from %s, %" PRIu32 " from us",
1296222732Shrs			    __func__,
1297222732Shrs			    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1298222732Shrs				sizeof(prefixbuf)),
1299222732Shrs			    pinfo->nd_opt_pi_prefix_len,
1300224144Shrs			    ifi->ifi_ifname, preferred_time,
1301222732Shrs			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1302222732Shrs				sizeof(ntopbuf)), pfx->pfx_pltimeexpire);
130378064Sume			inconsistent++;
130478064Sume		}
1305222732Shrs	} else if (!pfx->pfx_timer && preferred_time != pfx->pfx_preflifetime)
130678064Sume		syslog(LOG_INFO,
1307222732Shrs		    "<%s> preferred lifetime for %s/%d"
1308222732Shrs		    " inconsistent on %s:"
1309222732Shrs		    " %d from %s, %d from us",
1310222732Shrs		    __func__,
1311222732Shrs		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1312222732Shrs			sizeof(prefixbuf)),
1313222732Shrs		    pinfo->nd_opt_pi_prefix_len,
1314224144Shrs		    ifi->ifi_ifname, preferred_time,
1315222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1316222732Shrs			sizeof(ntopbuf)), pfx->pfx_preflifetime);
131755505Sshin
131855505Sshin	valid_time = ntohl(pinfo->nd_opt_pi_valid_time);
1319222732Shrs	if (pfx->pfx_vltimeexpire) {
1320253970Shrs		clock_gettime(CLOCK_MONOTONIC_FAST, &now);
132178064Sume		valid_time += now.tv_sec;
132278064Sume
1323222732Shrs		if (!pfx->pfx_timer && rai->rai_clockskew &&
1324222732Shrs		    abs(valid_time - pfx->pfx_vltimeexpire) > rai->rai_clockskew) {
132578064Sume			syslog(LOG_INFO,
1326222732Shrs			    "<%s> valid lifetime for %s/%d"
1327222732Shrs			    " (decr. in real time) inconsistent on %s:"
1328224144Shrs			    " %d from %s, %" PRIu32 " from us",
1329222732Shrs			    __func__,
1330222732Shrs			    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1331222732Shrs				sizeof(prefixbuf)),
1332222732Shrs			    pinfo->nd_opt_pi_prefix_len,
1333224144Shrs			    ifi->ifi_ifname, preferred_time,
1334222732Shrs			    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1335222732Shrs				sizeof(ntopbuf)), pfx->pfx_vltimeexpire);
133678064Sume			inconsistent++;
133778064Sume		}
1338222732Shrs	} else if (!pfx->pfx_timer && valid_time != pfx->pfx_validlifetime) {
133978064Sume		syslog(LOG_INFO,
1340222732Shrs		    "<%s> valid lifetime for %s/%d"
1341222732Shrs		    " inconsistent on %s:"
1342222732Shrs		    " %d from %s, %d from us",
1343222732Shrs		    __func__,
1344222732Shrs		    inet_ntop(AF_INET6, &pinfo->nd_opt_pi_prefix, prefixbuf,
1345222732Shrs			sizeof(prefixbuf)),
1346222732Shrs		    pinfo->nd_opt_pi_prefix_len,
1347224144Shrs		    ifi->ifi_ifname, valid_time,
1348222732Shrs		    inet_ntop(AF_INET6, &from->sin6_addr, ntopbuf,
1349222732Shrs			sizeof(ntopbuf)), pfx->pfx_validlifetime);
135062656Skris		inconsistent++;
135162656Skris	}
135262656Skris
1353222732Shrs	return (inconsistent);
135455505Sshin}
135555505Sshin
135655505Sshinstruct prefix *
135755505Sshinfind_prefix(struct rainfo *rai, struct in6_addr *prefix, int plen)
135855505Sshin{
1359222732Shrs	struct prefix *pfx;
136055505Sshin	int bytelen, bitlen;
1361224144Shrs	char bitmask;
136255505Sshin
1363222732Shrs	TAILQ_FOREACH(pfx, &rai->rai_prefix, pfx_next) {
1364222732Shrs		if (plen != pfx->pfx_prefixlen)
136555505Sshin			continue;
1366222732Shrs
136755505Sshin		bytelen = plen / 8;
136855505Sshin		bitlen = plen % 8;
1369118968Sume		bitmask = 0xff << (8 - bitlen);
1370222732Shrs
1371222732Shrs		if (memcmp((void *)prefix, (void *)&pfx->pfx_prefix, bytelen))
137255505Sshin			continue;
1373222732Shrs
1374118968Sume		if (bitlen == 0 ||
1375222732Shrs		    ((prefix->s6_addr[bytelen] & bitmask) ==
1376222732Shrs		     (pfx->pfx_prefix.s6_addr[bytelen] & bitmask))) {
1377222732Shrs			return (pfx);
1378118968Sume		}
137955505Sshin	}
138055505Sshin
1381222732Shrs	return (NULL);
138255505Sshin}
138355505Sshin
138478064Sume/* check if p0/plen0 matches p1/plen1; return 1 if matches, otherwise 0. */
138578064Sumeint
138678064Sumeprefix_match(struct in6_addr *p0, int plen0,
1387222732Shrs	struct in6_addr *p1, int plen1)
138878064Sume{
138978064Sume	int bytelen, bitlen;
1390224144Shrs	char bitmask;
139178064Sume
139278064Sume	if (plen0 < plen1)
1393222732Shrs		return (0);
1394222732Shrs
139578064Sume	bytelen = plen1 / 8;
139678064Sume	bitlen = plen1 % 8;
1397118968Sume	bitmask = 0xff << (8 - bitlen);
1398222732Shrs
139978064Sume	if (memcmp((void *)p0, (void *)p1, bytelen))
1400222732Shrs		return (0);
1401222732Shrs
1402118968Sume	if (bitlen == 0 ||
1403118968Sume	    ((p0->s6_addr[bytelen] & bitmask) ==
1404118968Sume	     (p1->s6_addr[bytelen] & bitmask))) {
1405222732Shrs		return (1);
1406118968Sume	}
140778064Sume
1408222732Shrs	return (0);
140978064Sume}
141078064Sume
141155505Sshinstatic int
141255505Sshinnd6_options(struct nd_opt_hdr *hdr, int limit,
1413224144Shrs	union nd_opt *ndopts, uint32_t optflags)
141455505Sshin{
141555505Sshin	int optlen = 0;
141655505Sshin
141755505Sshin	for (; limit > 0; limit -= optlen) {
1418222732Shrs		if ((size_t)limit < sizeof(struct nd_opt_hdr)) {
1419118660Sume			syslog(LOG_INFO, "<%s> short option header", __func__);
1420112676Sume			goto bad;
1421112676Sume		}
1422112676Sume
142355505Sshin		hdr = (struct nd_opt_hdr *)((caddr_t)hdr + optlen);
142455505Sshin		if (hdr->nd_opt_len == 0) {
1425112676Sume			syslog(LOG_INFO,
142697712Sume			    "<%s> bad ND option length(0) (type = %d)",
1427118660Sume			    __func__, hdr->nd_opt_type);
142855505Sshin			goto bad;
142955505Sshin		}
1430112676Sume		optlen = hdr->nd_opt_len << 3;
1431112676Sume		if (optlen > limit) {
1432118660Sume			syslog(LOG_INFO, "<%s> short option", __func__);
1433112676Sume			goto bad;
1434112676Sume		}
143555505Sshin
1436222732Shrs		if (hdr->nd_opt_type > ND_OPT_MTU &&
1437222732Shrs		    hdr->nd_opt_type != ND_OPT_RDNSS &&
1438222732Shrs		    hdr->nd_opt_type != ND_OPT_DNSSL) {
1439118664Sume			syslog(LOG_INFO, "<%s> unknown ND option(type %d)",
1440118660Sume			    __func__, hdr->nd_opt_type);
144155505Sshin			continue;
144255505Sshin		}
144355505Sshin
144455505Sshin		if ((ndopt_flags[hdr->nd_opt_type] & optflags) == 0) {
1445118664Sume			syslog(LOG_INFO, "<%s> unexpected ND option(type %d)",
1446118664Sume			    __func__, hdr->nd_opt_type);
144755505Sshin			continue;
144855505Sshin		}
144955505Sshin
1450112676Sume		/*
1451112676Sume		 * Option length check.  Do it here for all fixed-length
1452112676Sume		 * options.
1453112676Sume		 */
1454222732Shrs		switch (hdr->nd_opt_type) {
1455222732Shrs		case ND_OPT_MTU:
1456222732Shrs			if (optlen == sizeof(struct nd_opt_mtu))
1457222732Shrs				break;
1458222732Shrs			goto skip;
1459222732Shrs		case ND_OPT_RDNSS:
1460222732Shrs			if (optlen >= 24 &&
1461222732Shrs			    (optlen - sizeof(struct nd_opt_rdnss)) % 16 == 0)
1462222732Shrs				break;
1463222732Shrs			goto skip;
1464222732Shrs		case ND_OPT_DNSSL:
1465222732Shrs			if (optlen >= 16 &&
1466222732Shrs			    (optlen - sizeof(struct nd_opt_dnssl)) % 8 == 0)
1467222732Shrs				break;
1468222732Shrs			goto skip;
1469222732Shrs		case ND_OPT_PREFIX_INFORMATION:
1470222732Shrs			if (optlen == sizeof(struct nd_opt_prefix_info))
1471222732Shrs				break;
1472222732Shrsskip:
1473112676Sume			syslog(LOG_INFO, "<%s> invalid option length",
1474118660Sume			    __func__);
1475112676Sume			continue;
1476112676Sume		}
1477112676Sume
147897712Sume		switch (hdr->nd_opt_type) {
147997712Sume		case ND_OPT_TARGET_LINKADDR:
148097712Sume		case ND_OPT_REDIRECTED_HEADER:
1481222732Shrs		case ND_OPT_RDNSS:
1482222732Shrs		case ND_OPT_DNSSL:
1483112676Sume			break;	/* we don't care about these options */
1484151469Ssuz		case ND_OPT_SOURCE_LINKADDR:
148597712Sume		case ND_OPT_MTU:
1486222732Shrs			if (ndopts->opt_array[hdr->nd_opt_type]) {
148797712Sume				syslog(LOG_INFO,
148897712Sume				    "<%s> duplicated ND option (type = %d)",
1489118660Sume				    __func__, hdr->nd_opt_type);
149097712Sume			}
1491222732Shrs			ndopts->opt_array[hdr->nd_opt_type] = hdr;
149297712Sume			break;
149397712Sume		case ND_OPT_PREFIX_INFORMATION:
149497712Sume		{
1495222732Shrs			struct nd_optlist *nol;
149655505Sshin
1497222732Shrs			if (ndopts->opt_pi == 0) {
1498222732Shrs				ndopts->opt_pi =
149997712Sume				    (struct nd_opt_prefix_info *)hdr;
150097712Sume				continue;
150197712Sume			}
1502222732Shrs			nol = malloc(sizeof(*nol));
1503222732Shrs			if (nol == NULL) {
150497712Sume				syslog(LOG_ERR, "<%s> can't allocate memory",
1505118660Sume				    __func__);
150697712Sume				goto bad;
150797712Sume			}
1508222732Shrs			nol->nol_opt = hdr;
1509222732Shrs			TAILQ_INSERT_TAIL(&(ndopts->opt_list), nol, nol_next);
151055505Sshin
151197712Sume			break;
151255505Sshin		}
151397712Sume		default:	/* impossible */
151497712Sume			break;
151597712Sume		}
151655505Sshin	}
151755505Sshin
1518222732Shrs	return (0);
151955505Sshin
152055505Sshin  bad:
152155505Sshin	free_ndopts(ndopts);
152255505Sshin
1523222732Shrs	return (-1);
152455505Sshin}
152555505Sshin
152655505Sshinstatic void
1527222732Shrsfree_ndopts(union nd_opt *ndopts)
152855505Sshin{
1529222732Shrs	struct nd_optlist *nol;
153055505Sshin
1531222732Shrs	while ((nol = TAILQ_FIRST(&ndopts->opt_list)) != NULL) {
1532222732Shrs		TAILQ_REMOVE(&ndopts->opt_list, nol, nol_next);
1533222732Shrs		free(nol);
153455505Sshin	}
153555505Sshin}
153655505Sshin
153755505Sshinvoid
1538224144Shrssock_open(struct sockinfo *s)
153955505Sshin{
154055505Sshin	struct icmp6_filter filt;
154155505Sshin	int on;
154255505Sshin	/* XXX: should be max MTU attached to the node */
1543224144Shrs	static char answer[1500];
154455505Sshin
1545224144Shrs	syslog(LOG_DEBUG, "<%s> enter", __func__);
1546224144Shrs
1547224144Shrs	if (s == NULL) {
1548224144Shrs		syslog(LOG_ERR, "<%s> internal error", __func__);
1549224144Shrs		exit(1);
1550224144Shrs	}
155162656Skris	rcvcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1552222732Shrs	    CMSG_SPACE(sizeof(int));
1553224144Shrs	rcvcmsgbuf = (char *)malloc(rcvcmsgbuflen);
155462656Skris	if (rcvcmsgbuf == NULL) {
1555118660Sume		syslog(LOG_ERR, "<%s> not enough core", __func__);
155662656Skris		exit(1);
155762656Skris	}
155862656Skris
1559222732Shrs	sndcmsgbuflen = CMSG_SPACE(sizeof(struct in6_pktinfo)) +
1560222732Shrs	    CMSG_SPACE(sizeof(int));
1561224144Shrs	sndcmsgbuf = (char *)malloc(sndcmsgbuflen);
156262656Skris	if (sndcmsgbuf == NULL) {
1563118660Sume		syslog(LOG_ERR, "<%s> not enough core", __func__);
156462656Skris		exit(1);
156562656Skris	}
156662656Skris
1567224144Shrs	if ((s->si_fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6)) < 0) {
1568222732Shrs		syslog(LOG_ERR, "<%s> socket: %s", __func__, strerror(errno));
156955505Sshin		exit(1);
157055505Sshin	}
157155505Sshin	/* specify to tell receiving interface */
157255505Sshin	on = 1;
1573224144Shrs	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on,
1574222732Shrs	    sizeof(on)) < 0) {
1575222732Shrs		syslog(LOG_ERR, "<%s> IPV6_RECVPKTINFO: %s", __func__,
1576222732Shrs		    strerror(errno));
157762656Skris		exit(1);
157862656Skris	}
157955505Sshin	on = 1;
158055505Sshin	/* specify to tell value of hoplimit field of received IP6 hdr */
1581224144Shrs	if (setsockopt(s->si_fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on,
1582222732Shrs		sizeof(on)) < 0) {
1583222732Shrs		syslog(LOG_ERR, "<%s> IPV6_RECVHOPLIMIT: %s", __func__,
1584222732Shrs		    strerror(errno));
158562656Skris		exit(1);
158662656Skris	}
158755505Sshin	ICMP6_FILTER_SETBLOCKALL(&filt);
158862656Skris	ICMP6_FILTER_SETPASS(ND_ROUTER_SOLICIT, &filt);
158962656Skris	ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt);
1590224144Shrs	if (mcastif != NULL)
159155505Sshin		ICMP6_FILTER_SETPASS(ICMP6_ROUTER_RENUMBERING, &filt);
1592222732Shrs
1593224144Shrs	if (setsockopt(s->si_fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
1594222732Shrs	    sizeof(filt)) < 0) {
159555505Sshin		syslog(LOG_ERR, "<%s> IICMP6_FILTER: %s",
1596222732Shrs		    __func__, strerror(errno));
159755505Sshin		exit(1);
159855505Sshin	}
159955505Sshin
160055505Sshin	/* initialize msghdr for receiving packets */
160155505Sshin	rcviov[0].iov_base = (caddr_t)answer;
160255505Sshin	rcviov[0].iov_len = sizeof(answer);
1603118913Sume	rcvmhdr.msg_name = (caddr_t)&rcvfrom;
1604118913Sume	rcvmhdr.msg_namelen = sizeof(rcvfrom);
160555505Sshin	rcvmhdr.msg_iov = rcviov;
160655505Sshin	rcvmhdr.msg_iovlen = 1;
160755505Sshin	rcvmhdr.msg_control = (caddr_t) rcvcmsgbuf;
160862656Skris	rcvmhdr.msg_controllen = rcvcmsgbuflen;
160955505Sshin
161055505Sshin	/* initialize msghdr for sending packets */
161155505Sshin	sndmhdr.msg_namelen = sizeof(struct sockaddr_in6);
161255505Sshin	sndmhdr.msg_iov = sndiov;
161355505Sshin	sndmhdr.msg_iovlen = 1;
161455505Sshin	sndmhdr.msg_control = (caddr_t)sndcmsgbuf;
161562656Skris	sndmhdr.msg_controllen = sndcmsgbuflen;
1616222732Shrs
161755505Sshin	return;
161855505Sshin}
161955505Sshin
162055505Sshin/* open a routing socket to watch the routing table */
162155505Sshinstatic void
1622224144Shrsrtsock_open(struct sockinfo *s)
162355505Sshin{
1624224144Shrs	if (s == NULL) {
1625224144Shrs		syslog(LOG_ERR, "<%s> internal error", __func__);
1626224144Shrs		exit(1);
1627224144Shrs	}
1628224144Shrs	if ((s->si_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0) {
162955505Sshin		syslog(LOG_ERR,
1630222732Shrs		    "<%s> socket: %s", __func__, strerror(errno));
163155505Sshin		exit(1);
163255505Sshin	}
163355505Sshin}
163455505Sshin
1635224144Shrsstruct ifinfo *
1636224144Shrsif_indextoifinfo(int idx)
163755505Sshin{
1638224144Shrs	struct ifinfo *ifi;
1639253058Shrs	char *name, name0[IFNAMSIZ];
164055505Sshin
1641253058Shrs	/* Check if the interface has a valid name or not. */
1642253058Shrs	if (if_indextoname(idx, name0) == NULL)
1643253058Shrs		return (NULL);
1644253058Shrs
1645224144Shrs	TAILQ_FOREACH(ifi, &ifilist, ifi_next) {
1646224144Shrs		if (ifi->ifi_ifindex == idx)
1647224144Shrs			return (ifi);
164855505Sshin	}
164955505Sshin
1650224144Shrs	if (ifi != NULL)
1651224144Shrs		syslog(LOG_DEBUG, "<%s> ifi found (idx=%d)",
1652224144Shrs		    __func__, idx);
1653224144Shrs	else
1654224144Shrs		syslog(LOG_DEBUG, "<%s> ifi not found (idx=%d)",
1655224144Shrs		    __func__, idx);
1656224144Shrs
1657222732Shrs	return (NULL);		/* search failed */
165855505Sshin}
165955505Sshin
1660222972Shrsvoid
1661224144Shrsra_output(struct ifinfo *ifi)
166255505Sshin{
166355505Sshin	int i;
166455505Sshin	struct cmsghdr *cm;
166555505Sshin	struct in6_pktinfo *pi;
1666222732Shrs	struct soliciter *sol;
1667224144Shrs	struct rainfo *rai;
166855505Sshin
1669224144Shrs	switch (ifi->ifi_state) {
1670224144Shrs	case IFI_STATE_CONFIGURED:
1671224144Shrs		rai = ifi->ifi_rainfo;
1672224144Shrs		break;
1673224144Shrs	case IFI_STATE_TRANSITIVE:
1674224144Shrs		rai = ifi->ifi_rainfo_trans;
1675224144Shrs		break;
1676224144Shrs	case IFI_STATE_UNCONFIGURED:
1677224144Shrs		syslog(LOG_DEBUG, "<%s> %s is unconfigured.  "
1678224144Shrs		    "Skip sending RAs.",
1679224144Shrs		    __func__, ifi->ifi_ifname);
168062656Skris		return;
1681224144Shrs	default:
1682224144Shrs		rai = NULL;
168362656Skris	}
1684224144Shrs	if (rai == NULL) {
1685224144Shrs		syslog(LOG_DEBUG, "<%s> rainfo is NULL on %s."
1686224144Shrs		    "Skip sending RAs.",
1687224144Shrs		    __func__, ifi->ifi_ifname);
1688224144Shrs		return;
1689224144Shrs	}
1690224144Shrs	if (!(ifi->ifi_flags & IFF_UP)) {
1691224144Shrs		syslog(LOG_DEBUG, "<%s> %s is not up.  "
1692224144Shrs		    "Skip sending RAs.",
1693224144Shrs		    __func__, ifi->ifi_ifname);
1694224144Shrs		return;
1695224144Shrs	}
1696222732Shrs	/*
1697222732Shrs	 * Check lifetime, ACCEPT_RTADV flag, and ip6.forwarding.
1698222732Shrs	 *
1699222732Shrs	 * (lifetime == 0) = output
1700224144Shrs	 * (lifetime != 0 && (check_accept_rtadv()) = no output
1701222732Shrs	 *
1702222732Shrs	 * Basically, hosts MUST NOT send Router Advertisement
1703222732Shrs	 * messages at any time (RFC 4861, Section 6.2.3). However, it
1704222732Shrs	 * would sometimes be useful to allow hosts to advertise some
1705222732Shrs	 * parameters such as prefix information and link MTU. Thus,
1706222732Shrs	 * we allow hosts to invoke rtadvd only when router lifetime
1707222732Shrs	 * (on every advertising interface) is explicitly set
1708222732Shrs	 * zero. (see also the above section)
1709222732Shrs	 */
1710222732Shrs	syslog(LOG_DEBUG,
1711224144Shrs	    "<%s> check lifetime=%d, ACCEPT_RTADV=%d, ip6.forwarding=%d "
1712224144Shrs	    "on %s", __func__,
1713224144Shrs	    rai->rai_lifetime,
1714224144Shrs	    check_accept_rtadv(ifi->ifi_ifindex),
1715224144Shrs	    getinet6sysctl(IPV6CTL_FORWARDING),
1716224144Shrs	    ifi->ifi_ifname);
1717224144Shrs
1718222732Shrs	if (rai->rai_lifetime != 0) {
1719225519Shrs		if (getinet6sysctl(IPV6CTL_FORWARDING) == 0) {
1720225519Shrs			syslog(LOG_ERR,
1721225519Shrs			    "non-zero lifetime RA "
1722225519Shrs			    "but net.inet6.ip6.forwarding=0.  "
1723225519Shrs			    "Ignored.");
1724225519Shrs			return;
1725225519Shrs		}
1726224144Shrs		if (check_accept_rtadv(ifi->ifi_ifindex)) {
1727225519Shrs			syslog(LOG_ERR,
1728225519Shrs			    "non-zero lifetime RA "
1729222732Shrs			    "on RA receiving interface %s."
1730225519Shrs			    "  Ignored.", ifi->ifi_ifname);
1731222732Shrs			return;
1732222732Shrs		}
1733222732Shrs	}
173478064Sume
1735222732Shrs	make_packet(rai);	/* XXX: inefficient */
173655505Sshin
1737222732Shrs	sndmhdr.msg_name = (caddr_t)&sin6_linklocal_allnodes;
1738222732Shrs	sndmhdr.msg_iov[0].iov_base = (caddr_t)rai->rai_ra_data;
1739222732Shrs	sndmhdr.msg_iov[0].iov_len = rai->rai_ra_datalen;
1740222732Shrs
174155505Sshin	cm = CMSG_FIRSTHDR(&sndmhdr);
174255505Sshin	/* specify the outgoing interface */
174355505Sshin	cm->cmsg_level = IPPROTO_IPV6;
174455505Sshin	cm->cmsg_type = IPV6_PKTINFO;
174555505Sshin	cm->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
174655505Sshin	pi = (struct in6_pktinfo *)CMSG_DATA(cm);
174755505Sshin	memset(&pi->ipi6_addr, 0, sizeof(pi->ipi6_addr));	/*XXX*/
1748224144Shrs	pi->ipi6_ifindex = ifi->ifi_ifindex;
174955505Sshin
175055505Sshin	/* specify the hop limit of the packet */
175155505Sshin	{
175255505Sshin		int hoplimit = 255;
175355505Sshin
175455505Sshin		cm = CMSG_NXTHDR(&sndmhdr, cm);
175555505Sshin		cm->cmsg_level = IPPROTO_IPV6;
175655505Sshin		cm->cmsg_type = IPV6_HOPLIMIT;
175755505Sshin		cm->cmsg_len = CMSG_LEN(sizeof(int));
175855505Sshin		memcpy(CMSG_DATA(cm), &hoplimit, sizeof(int));
175955505Sshin	}
176055505Sshin
176155505Sshin	syslog(LOG_DEBUG,
1762224144Shrs	    "<%s> send RA on %s, # of RS waitings = %d",
1763224144Shrs	    __func__, ifi->ifi_ifname, ifi->ifi_rs_waitcount);
176455505Sshin
1765224144Shrs	i = sendmsg(sock.si_fd, &sndmhdr, 0);
176655505Sshin
1767222732Shrs	if (i < 0 || (size_t)i != rai->rai_ra_datalen)  {
176855505Sshin		if (i < 0) {
176962656Skris			syslog(LOG_ERR, "<%s> sendmsg on %s: %s",
1770224144Shrs			    __func__, ifi->ifi_ifname,
1771222732Shrs			    strerror(errno));
177255505Sshin		}
177355505Sshin	}
177455505Sshin
177562656Skris	/*
177662656Skris	 * unicast advertisements
177762656Skris	 * XXX commented out.  reason: though spec does not forbit it, unicast
177862656Skris	 * advert does not really help
177962656Skris	 */
1780222732Shrs	while ((sol = TAILQ_FIRST(&rai->rai_soliciter)) != NULL) {
1781222732Shrs		TAILQ_REMOVE(&rai->rai_soliciter, sol, sol_next);
178262656Skris		free(sol);
178362656Skris	}
178462656Skris
178555505Sshin	/* update timestamp */
1786253970Shrs	clock_gettime(CLOCK_MONOTONIC_FAST, &ifi->ifi_ra_lastsent);
178755505Sshin
1788224144Shrs	/* update counter */
1789224144Shrs	ifi->ifi_rs_waitcount = 0;
1790224144Shrs	ifi->ifi_raoutput++;
1791224144Shrs
1792224144Shrs	switch (ifi->ifi_state) {
1793224144Shrs	case IFI_STATE_CONFIGURED:
1794224144Shrs		if (ifi->ifi_burstcount > 0)
1795224144Shrs			ifi->ifi_burstcount--;
1796224144Shrs		break;
1797224144Shrs	case IFI_STATE_TRANSITIVE:
1798224144Shrs		ifi->ifi_burstcount--;
1799224144Shrs		if (ifi->ifi_burstcount == 0) {
1800224144Shrs			if (ifi->ifi_rainfo == ifi->ifi_rainfo_trans) {
1801228990Suqs				/* Initial burst finished. */
1802224144Shrs				if (ifi->ifi_rainfo_trans != NULL)
1803224144Shrs					ifi->ifi_rainfo_trans = NULL;
1804224144Shrs			}
1805224144Shrs
1806224144Shrs			/* Remove burst RA information */
1807224144Shrs			if (ifi->ifi_rainfo_trans != NULL) {
1808224144Shrs				rm_rainfo(ifi->ifi_rainfo_trans);
1809224144Shrs				ifi->ifi_rainfo_trans = NULL;
1810224144Shrs			}
1811224144Shrs
1812224144Shrs			if (ifi->ifi_rainfo != NULL) {
1813224144Shrs				/*
1814224144Shrs				 * TRANSITIVE -> CONFIGURED
1815224144Shrs				 *
1816224144Shrs				 * After initial burst or transition from
1817224144Shrs				 * one configuration to another,
1818224144Shrs				 * ifi_rainfo always points to the next RA
1819224144Shrs				 * information.
1820224144Shrs				 */
1821224144Shrs				ifi->ifi_state = IFI_STATE_CONFIGURED;
1822224144Shrs				syslog(LOG_DEBUG,
1823224144Shrs				    "<%s> ifname=%s marked as "
1824224144Shrs				    "CONFIGURED.", __func__,
1825224144Shrs				    ifi->ifi_ifname);
1826224144Shrs			} else {
1827224144Shrs				/*
1828224144Shrs				 * TRANSITIVE -> UNCONFIGURED
1829224144Shrs				 *
1830224144Shrs				 * If ifi_rainfo points to NULL, this
1831224144Shrs				 * interface is shutting down.
1832224144Shrs				 *
1833224144Shrs				 */
1834224144Shrs				int error;
1835224144Shrs
1836224144Shrs				ifi->ifi_state = IFI_STATE_UNCONFIGURED;
1837224144Shrs				syslog(LOG_DEBUG,
1838224144Shrs				    "<%s> ifname=%s marked as "
1839224144Shrs				    "UNCONFIGURED.", __func__,
1840224144Shrs				    ifi->ifi_ifname);
1841224144Shrs				error = sock_mc_leave(&sock,
1842224144Shrs				    ifi->ifi_ifindex);
1843224144Shrs				if (error)
1844224144Shrs					exit(1);
1845224144Shrs			}
1846224144Shrs		}
1847224144Shrs		break;
1848224144Shrs	}
184955505Sshin}
185055505Sshin
185155505Sshin/* process RA timer */
185298172Sumestruct rtadvd_timer *
1853222732Shrsra_timeout(void *arg)
185455505Sshin{
1855224144Shrs	struct ifinfo *ifi;
185655505Sshin
1857224144Shrs	ifi = (struct ifinfo *)arg;
1858222732Shrs	syslog(LOG_DEBUG, "<%s> RA timer on %s is expired",
1859224144Shrs	    __func__, ifi->ifi_ifname);
186055505Sshin
1861224144Shrs	ra_output(ifi);
186298172Sume
1863224144Shrs	return (ifi->ifi_ra_timer);
186455505Sshin}
186555505Sshin
186655505Sshin/* update RA timer */
186755505Sshinvoid
1868253970Shrsra_timer_update(void *arg, struct timespec *tm)
186955505Sshin{
1870224144Shrs	uint16_t interval;
1871222732Shrs	struct rainfo *rai;
1872224144Shrs	struct ifinfo *ifi;
187355505Sshin
1874224144Shrs	ifi = (struct ifinfo *)arg;
1875224144Shrs	rai = ifi->ifi_rainfo;
1876224144Shrs	interval = 0;
1877224144Shrs
1878224144Shrs	switch (ifi->ifi_state) {
1879224144Shrs	case IFI_STATE_UNCONFIGURED:
1880224144Shrs		return;
1881224144Shrs		break;
1882224144Shrs	case IFI_STATE_CONFIGURED:
1883224144Shrs		/*
1884224144Shrs		 * Whenever a multicast advertisement is sent from
1885224144Shrs		 * an interface, the timer is reset to a
1886224144Shrs		 * uniformly-distributed random value between the
1887224144Shrs		 * interface's configured MinRtrAdvInterval and
1888224144Shrs		 * MaxRtrAdvInterval (RFC4861 6.2.4).
1889224144Shrs		 */
1890224144Shrs		interval = rai->rai_mininterval;
1891118962Sume#ifdef HAVE_ARC4RANDOM
1892224144Shrs		interval += arc4random_uniform(rai->rai_maxinterval -
1893224144Shrs		    rai->rai_mininterval);
1894118962Sume#else
1895224144Shrs		interval += random() % (rai->rai_maxinterval -
1896224144Shrs		    rai->rai_mininterval);
1897118962Sume#endif
1898224144Shrs		break;
1899224144Shrs	case IFI_STATE_TRANSITIVE:
1900224144Shrs		/*
1901224144Shrs		 * For the first few advertisements (up to
1902224144Shrs		 * MAX_INITIAL_RTR_ADVERTISEMENTS), if the randomly chosen
1903224144Shrs		 * interval is greater than
1904224144Shrs		 * MAX_INITIAL_RTR_ADVERT_INTERVAL, the timer SHOULD be
1905224144Shrs		 * set to MAX_INITIAL_RTR_ADVERT_INTERVAL instead.  (RFC
1906224144Shrs		 * 4861 6.2.4)
1907224144Shrs		 *
1908224144Shrs		 * In such cases, the router SHOULD transmit one or more
1909224144Shrs		 * (but not more than MAX_FINAL_RTR_ADVERTISEMENTS) final
1910224144Shrs		 * multicast Router Advertisements on the interface with a
1911224144Shrs		 * Router Lifetime field of zero.  (RFC 4861 6.2.5)
1912224144Shrs		 */
1913224144Shrs		interval = ifi->ifi_burstinterval;
1914224144Shrs		break;
1915224144Shrs	}
191655505Sshin
191755505Sshin	tm->tv_sec = interval;
1918253970Shrs	tm->tv_nsec = 0;
191955505Sshin
192055505Sshin	syslog(LOG_DEBUG,
1921222732Shrs	    "<%s> RA timer on %s is set to %ld:%ld",
1922224144Shrs	    __func__, ifi->ifi_ifname,
1923253970Shrs	    (long int)tm->tv_sec, (long int)tm->tv_nsec / 1000);
192455505Sshin
192555505Sshin	return;
192655505Sshin}
1927