nd6.c revision 151468
150276Speter/*	$FreeBSD: head/sys/netinet6/nd6.c 151468 2005-10-19 15:05:42Z suz $	*/
2166124Srafan/*	$KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $	*/
350276Speter
450276Speter/*-
550276Speter * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
650276Speter * All rights reserved.
750276Speter *
850276Speter * Redistribution and use in source and binary forms, with or without
950276Speter * modification, are permitted provided that the following conditions
1050276Speter * are met:
1150276Speter * 1. Redistributions of source code must retain the above copyright
1250276Speter *    notice, this list of conditions and the following disclaimer.
1350276Speter * 2. Redistributions in binary form must reproduce the above copyright
1450276Speter *    notice, this list of conditions and the following disclaimer in the
1550276Speter *    documentation and/or other materials provided with the distribution.
1650276Speter * 3. Neither the name of the project nor the names of its contributors
1750276Speter *    may be used to endorse or promote products derived from this software
1850276Speter *    without specific prior written permission.
1950276Speter *
2050276Speter * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
2150276Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2250276Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2350276Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
2450276Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2550276Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2650276Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2750276Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2850276Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2950276Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3050276Speter * SUCH DAMAGE.
3150276Speter */
32166124Srafan
3350276Speter#include "opt_inet.h"
3450276Speter#include "opt_inet6.h"
3550276Speter#include "opt_mac.h"
3650276Speter
3750276Speter#include <sys/param.h>
3850276Speter#include <sys/systm.h>
3950276Speter#include <sys/callout.h>
4050276Speter#include <sys/mac.h>
4150276Speter#include <sys/malloc.h>
4250276Speter#include <sys/mbuf.h>
4350276Speter#include <sys/socket.h>
4450276Speter#include <sys/sockio.h>
4550276Speter#include <sys/time.h>
4650276Speter#include <sys/kernel.h>
4750276Speter#include <sys/protosw.h>
4850276Speter#include <sys/errno.h>
4950276Speter#include <sys/syslog.h>
5050276Speter#include <sys/queue.h>
5150276Speter#include <sys/sysctl.h>
5250276Speter
5350276Speter#include <net/if.h>
5450276Speter#include <net/if_arc.h>
5550276Speter#include <net/if_dl.h>
5697049Speter#include <net/if_types.h>
5797049Speter#include <net/iso88025.h>
5850276Speter#include <net/fddi.h>
5950276Speter#include <net/route.h>
6050276Speter
61166124Srafan#include <netinet/in.h>
6250276Speter#include <netinet/if_ether.h>
6366963Speter#include <netinet6/in6_var.h>
6450276Speter#include <netinet/ip6.h>
6550276Speter#include <netinet6/ip6_var.h>
66166124Srafan#include <netinet6/scope6_var.h>
67166124Srafan#include <netinet6/nd6.h>
68166124Srafan#include <netinet/icmp6.h>
69166124Srafan
7050276Speter#include <net/net_osdep.h>
71166124Srafan
7297049Speter#define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
7397049Speter#define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
74166124Srafan
7597049Speter#define SIN6(s) ((struct sockaddr_in6 *)s)
7697049Speter#define SDL(s) ((struct sockaddr_dl *)s)
7797049Speter
7897049Speter/* timer values */
7997049Speterint	nd6_prune	= 1;	/* walk list every 1 seconds */
8097049Speterint	nd6_delay	= 5;	/* delay first probe time 5 second */
8197049Speterint	nd6_umaxtries	= 3;	/* maximum unicast query */
8250276Speterint	nd6_mmaxtries	= 3;	/* maximum multicast query */
8350276Speterint	nd6_useloopback = 1;	/* use loopback interface for local traffic */
8450276Speterint	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
8550276Speter
8650276Speter/* preventing too many loops in ND option parsing */
8750276Speterint nd6_maxndopt = 10;	/* max # of ND options allowed */
8850276Speter
8950276Speterint nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
9050276Speter
9166963Speter#ifdef ND6_DEBUG
9266963Speterint nd6_debug = 1;
9350276Speter#else
9450276Speterint nd6_debug = 0;
9550276Speter#endif
9650276Speter
9750276Speter/* for debugging? */
9850276Speterstatic int nd6_inuse, nd6_allocated;
9950276Speter
10050276Speterstruct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
10150276Speterstruct nd_drhead nd_defrouter;
10250276Speterstruct nd_prhead nd_prefix = { 0 };
10350276Speter
10450276Speterint nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
10550276Speterstatic struct sockaddr_in6 all1_sa;
10650276Speter
10750276Speterstatic int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
10850276Speter	struct ifnet *));
10950276Speterstatic void nd6_setmtu0 __P((struct ifnet *, struct nd_ifinfo *));
11050276Speterstatic void nd6_slowtimo __P((void *));
11150276Speterstatic int regen_tmpaddr __P((struct in6_ifaddr *));
11250276Speter
11350276Speterstruct callout nd6_slowtimo_ch;
11450276Speterstruct callout nd6_timer_ch;
11550276Speterextern struct callout in6_tmpaddrtimer_ch;
11650276Speter
11750276Spetervoid
11850276Speternd6_init()
11950276Speter{
12050276Speter	static int nd6_init_done = 0;
12150276Speter	int i;
12250276Speter
12350276Speter	if (nd6_init_done) {
12450276Speter		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
12550276Speter		return;
12650276Speter	}
12750276Speter
12850276Speter	all1_sa.sin6_family = AF_INET6;
12950276Speter	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
13050276Speter	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
13150276Speter		all1_sa.sin6_addr.s6_addr[i] = 0xff;
13250276Speter
13350276Speter	/* initialization of the default router list */
13450276Speter	TAILQ_INIT(&nd_defrouter);
13562449Speter
13650276Speter	nd6_init_done = 1;
13762449Speter
13862449Speter	/* start timer */
13962449Speter	callout_init(&nd6_slowtimo_ch, 0);
14050276Speter	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
14150276Speter	    nd6_slowtimo, NULL);
14250276Speter}
14350276Speter
14450276Speterstruct nd_ifinfo *
14550276Speternd6_ifattach(ifp)
14650276Speter	struct ifnet *ifp;
14750276Speter{
14850276Speter	struct nd_ifinfo *nd;
14962449Speter
15062449Speter	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
15162449Speter	bzero(nd, sizeof(*nd));
15262449Speter
15350276Speter	nd->initialized = 1;
15462449Speter
15562449Speter	nd->chlim = IPV6_DEFHLIM;
15662449Speter	nd->basereachable = REACHABLE_TIME;
15762449Speter	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
15862449Speter	nd->retrans = RETRANS_TIMER;
15962449Speter	/*
16062449Speter	 * Note that the default value of ip6_accept_rtadv is 0, which means
16162449Speter	 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
16262449Speter	 * here.
16350276Speter	 */
16450276Speter	nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
16550276Speter
16650276Speter	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
16750276Speter	nd6_setmtu0(ifp, nd);
16850276Speter
16950276Speter	return nd;
17050276Speter}
17150276Speter
17250276Spetervoid
17350276Speternd6_ifdetach(nd)
17450276Speter	struct nd_ifinfo *nd;
17550276Speter{
17650276Speter
17750276Speter	free(nd, M_IP6NDP);
17850276Speter}
17950276Speter
18062449Speter/*
18162449Speter * Reset ND level link MTU. This function is called when the physical MTU
18250276Speter * changes, which means we might have to adjust the ND level MTU.
18362449Speter */
18462449Spetervoid
18562449Speternd6_setmtu(ifp)
18662449Speter	struct ifnet *ifp;
18762449Speter{
18862449Speter
18962449Speter	nd6_setmtu0(ifp, ND_IFINFO(ifp));
19050276Speter}
19162449Speter
19262449Speter/* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
19362449Spetervoid
19462449Speternd6_setmtu0(ifp, ndi)
19562449Speter	struct ifnet *ifp;
19650276Speter	struct nd_ifinfo *ndi;
19762449Speter{
19862449Speter	u_int32_t omaxmtu;
19962449Speter
20062449Speter	omaxmtu = ndi->maxmtu;
20162449Speter
20262449Speter	switch (ifp->if_type) {
20362449Speter	case IFT_ARCNET:
20462449Speter		ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
20562449Speter		break;
20662449Speter	case IFT_FDDI:
20762449Speter		ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
20862449Speter		break;
20962449Speter	case IFT_ISO88025:
21050276Speter		 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
21162449Speter		 break;
21262449Speter	default:
21362449Speter		ndi->maxmtu = ifp->if_mtu;
21462449Speter		break;
21562449Speter	}
21662449Speter
21750276Speter	/*
21850276Speter	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
21950276Speter	 * undesirable situation.  We thus notify the operator of the change
22050276Speter	 * explicitly.  The check for omaxmtu is necessary to restrict the
22150276Speter	 * log to the case of changing the MTU, not initializing it.
22250276Speter	 */
22350276Speter	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
22450276Speter		log(LOG_NOTICE, "nd6_setmtu0: "
22550276Speter		    "new link MTU on %s (%lu) is too small for IPv6\n",
22650276Speter		    if_name(ifp), (unsigned long)ndi->maxmtu);
22750276Speter	}
22850276Speter
22950276Speter	if (ndi->maxmtu > in6_maxmtu)
23050276Speter		in6_setmaxmtu(); /* check all interfaces just in case */
231166124Srafan
23250276Speter#undef MIN
23350276Speter}
23450276Speter
23550276Spetervoid
23662449Speternd6_option_init(opt, icmp6len, ndopts)
23750276Speter	void *opt;
23862449Speter	int icmp6len;
23950276Speter	union nd_opts *ndopts;
24050276Speter{
24150276Speter
24250276Speter	bzero(ndopts, sizeof(*ndopts));
24350276Speter	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
24450276Speter	ndopts->nd_opts_last
24550276Speter		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
24650276Speter
24750276Speter	if (icmp6len == 0) {
24850276Speter		ndopts->nd_opts_done = 1;
24950276Speter		ndopts->nd_opts_search = NULL;
25050276Speter	}
25150276Speter}
25250276Speter
25350276Speter/*
25450276Speter * Take one ND option.
25550276Speter */
25650276Speterstruct nd_opt_hdr *
25750276Speternd6_option(ndopts)
25850276Speter	union nd_opts *ndopts;
25950276Speter{
26050276Speter	struct nd_opt_hdr *nd_opt;
26150276Speter	int olen;
26266963Speter
26366963Speter	if (ndopts == NULL)
26466963Speter		panic("ndopts == NULL in nd6_option");
26566963Speter	if (ndopts->nd_opts_last == NULL)
26666963Speter		panic("uninitialized ndopts in nd6_option");
26766963Speter	if (ndopts->nd_opts_search == NULL)
26866963Speter		return NULL;
26966963Speter	if (ndopts->nd_opts_done)
27066963Speter		return NULL;
27150276Speter
27262449Speter	nd_opt = ndopts->nd_opts_search;
27362449Speter
27462449Speter	/* make sure nd_opt_len is inside the buffer */
27562449Speter	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
27662449Speter		bzero(ndopts, sizeof(*ndopts));
27762449Speter		return NULL;
27850276Speter	}
27962449Speter
28062449Speter	olen = nd_opt->nd_opt_len << 3;
28162449Speter	if (olen == 0) {
28262449Speter		/*
28362449Speter		 * Message validation requires that all included
28462449Speter		 * options have a length that is greater than zero.
28562449Speter		 */
28662449Speter		bzero(ndopts, sizeof(*ndopts));
28762449Speter		return NULL;
28862449Speter	}
28962449Speter
29062449Speter	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
29162449Speter	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
29262449Speter		/* option overruns the end of buffer, invalid */
29362449Speter		bzero(ndopts, sizeof(*ndopts));
29462449Speter		return NULL;
29562449Speter	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
29662449Speter		/* reached the end of options chain */
29762449Speter		ndopts->nd_opts_done = 1;
29862449Speter		ndopts->nd_opts_search = NULL;
29962449Speter	}
30062449Speter	return nd_opt;
30150276Speter}
30262449Speter
30350276Speter/*
30462449Speter * Parse multiple ND options.
30562449Speter * This function is much easier to use, for ND routines that do not need
30662449Speter * multiple options of the same type.
30762449Speter */
30862449Speterint
30962449Speternd6_options(ndopts)
31050276Speter	union nd_opts *ndopts;
31150276Speter{
31262449Speter	struct nd_opt_hdr *nd_opt;
31350276Speter	int i = 0;
31462449Speter
31562449Speter	if (ndopts == NULL)
31650276Speter		panic("ndopts == NULL in nd6_options");
31762449Speter	if (ndopts->nd_opts_last == NULL)
31862449Speter		panic("uninitialized ndopts in nd6_options");
31962449Speter	if (ndopts->nd_opts_search == NULL)
32062449Speter		return 0;
32162449Speter
32262449Speter	while (1) {
32366963Speter		nd_opt = nd6_option(ndopts);
32462449Speter		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
32562449Speter			/*
32662449Speter			 * Message validation requires that all included
32762449Speter			 * options have a length that is greater than zero.
32862449Speter			 */
32962449Speter			icmp6stat.icp6s_nd_badopt++;
33062449Speter			bzero(ndopts, sizeof(*ndopts));
33162449Speter			return -1;
33262449Speter		}
33350276Speter
33462449Speter		if (nd_opt == NULL)
33562449Speter			goto skip1;
33662449Speter
33762449Speter		switch (nd_opt->nd_opt_type) {
33862449Speter		case ND_OPT_SOURCE_LINKADDR:
33962449Speter		case ND_OPT_TARGET_LINKADDR:
34062449Speter		case ND_OPT_MTU:
34162449Speter		case ND_OPT_REDIRECTED_HEADER:
34262449Speter			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
34350276Speter				nd6log((LOG_INFO,
34462449Speter				    "duplicated ND6 option found (type=%d)\n",
34562449Speter				    nd_opt->nd_opt_type));
34662449Speter				/* XXX bark? */
34762449Speter			} else {
34862449Speter				ndopts->nd_opt_array[nd_opt->nd_opt_type]
34962449Speter					= nd_opt;
35050276Speter			}
35150276Speter			break;
35262449Speter		case ND_OPT_PREFIX_INFORMATION:
35362449Speter			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
35462449Speter				ndopts->nd_opt_array[nd_opt->nd_opt_type]
35562449Speter					= nd_opt;
35662449Speter			}
35762449Speter			ndopts->nd_opts_pi_end =
35862449Speter				(struct nd_opt_prefix_info *)nd_opt;
35962449Speter			break;
36062449Speter		default:
36162449Speter			/*
36262449Speter			 * Unknown options must be silently ignored,
36362449Speter			 * to accomodate future extension to the protocol.
36462449Speter			 */
36562449Speter			nd6log((LOG_DEBUG,
36662449Speter			    "nd6_options: unsupported option %d - "
36762449Speter			    "option ignored\n", nd_opt->nd_opt_type));
36862449Speter		}
36962449Speter
37062449Speterskip1:
37162449Speter		i++;
37262449Speter		if (i > nd6_maxndopt) {
37362449Speter			icmp6stat.icp6s_nd_toomanyopt++;
37462449Speter			nd6log((LOG_INFO, "too many loop in nd opt\n"));
37562449Speter			break;
37662449Speter		}
37762449Speter
37850276Speter		if (ndopts->nd_opts_done)
37950276Speter			break;
38062449Speter	}
38162449Speter
38262449Speter	return 0;
38362449Speter}
38462449Speter
38562449Speter/*
38662449Speter * ND6 timer routine to expire default route list and prefix list
38762449Speter */
38850276Spetervoid
38962449Speternd6_timer(ignored_arg)
39062449Speter	void	*ignored_arg;
39162449Speter{
39250276Speter	int s;
39362449Speter	struct llinfo_nd6 *ln;
39462449Speter	struct nd_defrouter *dr;
39562449Speter	struct nd_prefix *pr;
39650276Speter	struct ifnet *ifp;
39762449Speter	struct in6_ifaddr *ia6, *nia6;
39862449Speter	struct in6_addrlifetime *lt6;
39962449Speter
40062449Speter	s = splnet();
40162449Speter	callout_reset(&nd6_timer_ch, nd6_prune * hz,
40262449Speter	    nd6_timer, NULL);
40362449Speter
40462449Speter	ln = llinfo_nd6.ln_next;
40562449Speter	while (ln && ln != &llinfo_nd6) {
40662449Speter		struct rtentry *rt;
40762449Speter		struct sockaddr_in6 *dst;
40862449Speter		struct llinfo_nd6 *next = ln->ln_next;
40962449Speter		/* XXX: used for the DELAY case only: */
41062449Speter		struct nd_ifinfo *ndi = NULL;
41162449Speter
41250276Speter		if ((rt = ln->ln_rt) == NULL) {
41362449Speter			ln = next;
41462449Speter			continue;
41562449Speter		}
41662449Speter		if ((ifp = rt->rt_ifp) == NULL) {
41762449Speter			ln = next;
41850276Speter			continue;
41962449Speter		}
42062449Speter		ndi = ND_IFINFO(ifp);
42162449Speter		dst = (struct sockaddr_in6 *)rt_key(rt);
42262449Speter
42362449Speter		if (ln->ln_expire > time_second) {
42450276Speter			ln = next;
42562449Speter			continue;
42662449Speter		}
42762449Speter
42862449Speter		/* sanity check */
42962449Speter		if (!rt)
43062449Speter			panic("rt=0 in nd6_timer(ln=%p)", ln);
43162449Speter		if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
43262449Speter			panic("rt_llinfo(%p) is not equal to ln(%p)",
43362449Speter			      rt->rt_llinfo, ln);
43462449Speter		if (!dst)
43562449Speter			panic("dst=0 in nd6_timer(ln=%p)", ln);
43662449Speter
43762449Speter		switch (ln->ln_state) {
43862449Speter		case ND6_LLINFO_INCOMPLETE:
43962449Speter			if (ln->ln_asked < nd6_mmaxtries) {
44050276Speter				ln->ln_asked++;
44162449Speter				ln->ln_expire = time_second +
44262449Speter					ND_IFINFO(ifp)->retrans / 1000;
44362449Speter				nd6_ns_output(ifp, NULL, &dst->sin6_addr,
44450276Speter					ln, 0);
44562449Speter			} else {
44662449Speter				struct mbuf *m = ln->ln_hold;
44762449Speter				if (m) {
44862449Speter					/*
44962449Speter					 * assuming every packet in ln_hold has
45062449Speter					 * the same IP header
45162449Speter					 */
45262449Speter					ln->ln_hold = NULL;
45362449Speter					icmp6_error2(m, ICMP6_DST_UNREACH,
45462449Speter						     ICMP6_DST_UNREACH_ADDR, 0,
45550276Speter						     rt->rt_ifp);
45650276Speter				}
45762449Speter				next = nd6_free(rt);
45862449Speter			}
45962449Speter			break;
46062449Speter		case ND6_LLINFO_REACHABLE:
46162449Speter			if (ln->ln_expire) {
46262449Speter				ln->ln_state = ND6_LLINFO_STALE;
46350276Speter				ln->ln_expire = time_second + nd6_gctimer;
46462449Speter			}
46562449Speter			break;
46662449Speter
46762449Speter		case ND6_LLINFO_STALE:
46862449Speter			/* Garbage Collection(RFC 2461 5.3) */
46950276Speter			if (ln->ln_expire)
47062449Speter				next = nd6_free(rt);
47162449Speter			break;
47262449Speter
47362449Speter		case ND6_LLINFO_DELAY:
47462449Speter			if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
47562449Speter				/* We need NUD */
47662449Speter				ln->ln_asked = 1;
47762449Speter				ln->ln_state = ND6_LLINFO_PROBE;
47862449Speter				ln->ln_expire = time_second +
47962449Speter					ndi->retrans / 1000;
48062449Speter				nd6_ns_output(ifp, &dst->sin6_addr,
48162449Speter					      &dst->sin6_addr,
48262449Speter					      ln, 0);
48362449Speter			} else {
48450276Speter				ln->ln_state = ND6_LLINFO_STALE; /* XXX */
48562449Speter				ln->ln_expire = time_second + nd6_gctimer;
48666963Speter			}
48762449Speter			break;
48862449Speter		case ND6_LLINFO_PROBE:
48962449Speter			if (ln->ln_asked < nd6_umaxtries) {
49062449Speter				ln->ln_asked++;
49162449Speter				ln->ln_expire = time_second +
49262449Speter					ND_IFINFO(ifp)->retrans / 1000;
49362449Speter				nd6_ns_output(ifp, &dst->sin6_addr,
49462449Speter					       &dst->sin6_addr, ln, 0);
49562449Speter			} else {
49662449Speter				next = nd6_free(rt);
49762449Speter			}
49862449Speter			break;
49962449Speter		}
50062449Speter		ln = next;
50162449Speter	}
50262449Speter
50362449Speter	/* expire default router list */
50462449Speter	dr = TAILQ_FIRST(&nd_defrouter);
50562449Speter	while (dr) {
50662449Speter		if (dr->expire && dr->expire < time_second) {
50750276Speter			struct nd_defrouter *t;
50862449Speter			t = TAILQ_NEXT(dr, dr_entry);
50962449Speter			defrtrlist_del(dr);
51062449Speter			dr = t;
51162449Speter		} else {
51262449Speter			dr = TAILQ_NEXT(dr, dr_entry);
51350276Speter		}
51462449Speter	}
51562449Speter
51662449Speter	/*
51762449Speter	 * expire interface addresses.
51862449Speter	 * in the past the loop was inside prefix expiry processing.
51962449Speter	 * However, from a stricter speci-confrmance standpoint, we should
52050276Speter	 * rather separate address lifetimes and prefix lifetimes.
52162449Speter	 */
52262449Speter  addrloop:
52362449Speter	for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
52462449Speter		nia6 = ia6->ia_next;
52562449Speter		/* check address lifetime */
52662449Speter		lt6 = &ia6->ia6_lifetime;
52762449Speter		if (IFA6_IS_INVALID(ia6)) {
52862449Speter			int regen = 0;
52950276Speter
53062449Speter			/*
53162449Speter			 * If the expiring address is temporary, try
53262449Speter			 * regenerating a new one.  This would be useful when
53362449Speter			 * we suspended a laptop PC, then turned it on after a
53462449Speter			 * period that could invalidate all temporary
53562449Speter			 * addresses.  Although we may have to restart the
53662449Speter			 * loop (see below), it must be after purging the
53762449Speter			 * address.  Otherwise, we'd see an infinite loop of
53862449Speter			 * regeneration.
53962449Speter			 */
54062449Speter			if (ip6_use_tempaddr &&
54162449Speter			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
54262449Speter				if (regen_tmpaddr(ia6) == 0)
54362449Speter					regen = 1;
54462449Speter			}
54562449Speter
54662449Speter			in6_purgeaddr(&ia6->ia_ifa);
54750276Speter
54862449Speter			if (regen)
54962449Speter				goto addrloop; /* XXX: see below */
55062449Speter		}
55162449Speter		if (IFA6_IS_DEPRECATED(ia6)) {
55262449Speter			int oldflags = ia6->ia6_flags;
55362449Speter
55462449Speter			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
55562449Speter
55650276Speter			/*
55762449Speter			 * If a temporary address has just become deprecated,
55862449Speter			 * regenerate a new one if possible.
55962449Speter			 */
56062449Speter			if (ip6_use_tempaddr &&
56162449Speter			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
56250276Speter			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
56362449Speter
56450276Speter				if (regen_tmpaddr(ia6) == 0) {
56562449Speter					/*
56662449Speter					 * A new temporary address is
56762449Speter					 * generated.
56862449Speter					 * XXX: this means the address chain
56962449Speter					 * has changed while we are still in
57062449Speter					 * the loop.  Although the change
57162449Speter					 * would not cause disaster (because
57262449Speter					 * it's not a deletion, but an
57362449Speter					 * addition,) we'd rather restart the
57462449Speter					 * loop just for safety.  Or does this
57562449Speter					 * significantly reduce performance??
57650276Speter					 */
57762449Speter					goto addrloop;
57850276Speter				}
57962449Speter			}
58062449Speter		} else {
58162449Speter			/*
58262449Speter			 * A new RA might have made a deprecated address
58350276Speter			 * preferred.
58450276Speter			 */
58550276Speter			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
58650276Speter		}
58750276Speter	}
58850276Speter
58950276Speter	/* expire prefix list */
59050276Speter	pr = nd_prefix.lh_first;
59150276Speter	while (pr) {
59262449Speter		/*
59362449Speter		 * check prefix lifetime.
59450276Speter		 * since pltime is just for autoconf, pltime processing for
59562449Speter		 * prefix is not necessary.
59662449Speter		 */
59762449Speter		if (pr->ndpr_expire && pr->ndpr_expire < time_second) {
59862449Speter			struct nd_prefix *t;
59962449Speter			t = pr->ndpr_next;
60050276Speter
60162449Speter			/*
60250276Speter			 * address expiration and prefix expiration are
60362449Speter			 * separate.  NEVER perform in6_purgeaddr here.
60450276Speter			 */
60562449Speter
60662449Speter			prelist_remove(pr);
60762449Speter			pr = t;
60862449Speter		} else
60962449Speter			pr = pr->ndpr_next;
61062449Speter	}
61162449Speter	splx(s);
61262449Speter}
61362449Speter
61450276Speterstatic int
61562449Speterregen_tmpaddr(ia6)
61662449Speter	struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */
61762449Speter{
61862449Speter	struct ifaddr *ifa;
61962449Speter	struct ifnet *ifp;
62062449Speter	struct in6_ifaddr *public_ifa6 = NULL;
62162449Speter
62262449Speter	ifp = ia6->ia_ifa.ifa_ifp;
62362449Speter	for (ifa = ifp->if_addrlist.tqh_first; ifa;
62450276Speter	     ifa = ifa->ifa_list.tqe_next) {
62562449Speter		struct in6_ifaddr *it6;
62650276Speter
62750276Speter		if (ifa->ifa_addr->sa_family != AF_INET6)
62850276Speter			continue;
62950276Speter
63050276Speter		it6 = (struct in6_ifaddr *)ifa;
63150276Speter
63250276Speter		/* ignore no autoconf addresses. */
63350276Speter		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
63462449Speter			continue;
63562449Speter
63650276Speter		/* ignore autoconf addresses with different prefixes. */
63762449Speter		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
63850276Speter			continue;
63962449Speter
64062449Speter		/*
64162449Speter		 * Now we are looking at an autoconf address with the same
64262449Speter		 * prefix as ours.  If the address is temporary and is still
64350276Speter		 * preferred, do not create another one.  It would be rare, but
64462449Speter		 * could happen, for example, when we resume a laptop PC after
64550276Speter		 * a long period.
64650276Speter		 */
64750276Speter		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
64850276Speter		    !IFA6_IS_DEPRECATED(it6)) {
64950276Speter			public_ifa6 = NULL;
65050276Speter			break;
65150276Speter		}
65250276Speter
65350276Speter		/*
65450276Speter		 * This is a public autoconf address that has the same prefix
65550276Speter		 * as ours.  If it is preferred, keep it.  We can't break the
65650276Speter		 * loop here, because there may be a still-preferred temporary
65750276Speter		 * address with the prefix.
65850276Speter		 */
65950276Speter		if (!IFA6_IS_DEPRECATED(it6))
66050276Speter		    public_ifa6 = it6;
66150276Speter	}
66250276Speter
66350276Speter	if (public_ifa6 != NULL) {
66450276Speter		int e;
66550276Speter
66650276Speter		if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) {
66750276Speter			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
66850276Speter			    " tmp addr,errno=%d\n", e);
66950276Speter			return (-1);
67050276Speter		}
67150276Speter		return (0);
67250276Speter	}
67350276Speter
67450276Speter	return (-1);
67550276Speter}
67650276Speter
67750276Speter/*
67850276Speter * Nuke neighbor cache/prefix/default router management table, right before
67950276Speter * ifp goes away.
68050276Speter */
68150276Spetervoid
68250276Speternd6_purge(ifp)
68350276Speter	struct ifnet *ifp;
68450276Speter{
68550276Speter	struct llinfo_nd6 *ln, *nln;
68650276Speter	struct nd_defrouter *dr, *ndr, drany;
68750276Speter	struct nd_prefix *pr, *npr;
68850276Speter
68950276Speter	/* Nuke default router list entries toward ifp */
69050276Speter	if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
69150276Speter		/*
69250276Speter		 * The first entry of the list may be stored in
69350276Speter		 * the routing table, so we'll delete it later.
69450276Speter		 */
69550276Speter		for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
69650276Speter			ndr = TAILQ_NEXT(dr, dr_entry);
69750276Speter			if (dr->ifp == ifp)
69850276Speter				defrtrlist_del(dr);
69950276Speter		}
70050276Speter		dr = TAILQ_FIRST(&nd_defrouter);
70150276Speter		if (dr->ifp == ifp)
70250276Speter			defrtrlist_del(dr);
70350276Speter	}
70450276Speter
70562449Speter	/* Nuke prefix list entries toward ifp */
70662449Speter	for (pr = nd_prefix.lh_first; pr; pr = npr) {
70762449Speter		npr = pr->ndpr_next;
70862449Speter		if (pr->ndpr_ifp == ifp) {
70950276Speter			/*
71062449Speter			 * Previously, pr->ndpr_addr is removed as well,
71162449Speter			 * but I strongly believe we don't have to do it.
71262449Speter			 * nd6_purge() is only called from in6_ifdetach(),
71362449Speter			 * which removes all the associated interface addresses
71462449Speter			 * by itself.
71562449Speter			 * (jinmei@kame.net 20010129)
71662449Speter			 */
717166124Srafan			prelist_remove(pr);
71862449Speter		}
71962449Speter	}
72062449Speter
72162449Speter	/* cancel default outgoing interface setting */
72262449Speter	if (nd6_defifindex == ifp->if_index)
72362449Speter		nd6_setdefaultiface(0);
72450276Speter
72562449Speter	if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
72662449Speter		/* refresh default router list */
72762449Speter		bzero(&drany, sizeof(drany));
72862449Speter		defrouter_delreq(&drany, 0);
72962449Speter		defrouter_select();
73062449Speter	}
731166124Srafan
73262449Speter	/*
73350276Speter	 * Nuke neighbor cache entries for the ifp.
73462449Speter	 * Note that rt->rt_ifp may not be the same as ifp,
73550276Speter	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
73662449Speter	 * nd6_rtrequest(), and ip6_input().
73762449Speter	 */
73862449Speter	ln = llinfo_nd6.ln_next;
73962449Speter	while (ln && ln != &llinfo_nd6) {
74062449Speter		struct rtentry *rt;
74162449Speter		struct sockaddr_dl *sdl;
74250276Speter
74350276Speter		nln = ln->ln_next;
74450276Speter		rt = ln->ln_rt;
74550276Speter		if (rt && rt->rt_gateway &&
74650276Speter		    rt->rt_gateway->sa_family == AF_LINK) {
74762449Speter			sdl = (struct sockaddr_dl *)rt->rt_gateway;
74850276Speter			if (sdl->sdl_index == ifp->if_index)
74962449Speter				nln = nd6_free(rt);
75062449Speter		}
751166124Srafan		ln = nln;
75262449Speter	}
75362449Speter}
75450276Speter
75562449Speterstruct rtentry *
75662449Speternd6_lookup(addr6, create, ifp)
75762449Speter	struct in6_addr *addr6;
75862449Speter	int create;
75962449Speter	struct ifnet *ifp;
76062449Speter{
76162449Speter	struct rtentry *rt;
76250276Speter	struct sockaddr_in6 sin6;
76350276Speter
76450276Speter	bzero(&sin6, sizeof(sin6));
76550276Speter	sin6.sin6_len = sizeof(struct sockaddr_in6);
76650276Speter	sin6.sin6_family = AF_INET6;
76750276Speter	sin6.sin6_addr = *addr6;
76850276Speter	rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL);
76950276Speter	if (rt) {
77062449Speter		if ((rt->rt_flags & RTF_LLINFO) == 0 && create) {
77150276Speter			/*
77262449Speter			 * This is the case for the default route.
77362449Speter			 * If we want to create a neighbor cache for the
77476726Speter			 * address, we should free the route for the
77562449Speter			 * destination and allocate an interface route.
77662449Speter			 */
77762449Speter			RTFREE_LOCKED(rt);
77862449Speter			rt = NULL;
77962449Speter		}
78062449Speter	}
781166124Srafan	if (rt == NULL) {
78266963Speter		if (create && ifp) {
78350276Speter			int e;
78462449Speter
78562449Speter			/*
78662449Speter			 * If no route is available and create is set,
78762449Speter			 * we allocate a host route for the destination
78876726Speter			 * and treat it like an interface route.
78950276Speter			 * This hack is necessary for a neighbor which can't
79062449Speter			 * be covered by our own prefix.
79162449Speter			 */
79262449Speter			struct ifaddr *ifa =
79362449Speter			    ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
79462449Speter			if (ifa == NULL)
79562449Speter				return (NULL);
79662449Speter
79762449Speter			/*
79862449Speter			 * Create a new route.  RTF_LLINFO is necessary
79966963Speter			 * to create a Neighbor Cache entry for the
80066963Speter			 * destination in nd6_rtrequest which will be
80197049Speter			 * called in rtrequest via ifa->ifa_rtrequest.
802166124Srafan			 */
80397049Speter			if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
80466963Speter			    ifa->ifa_addr, (struct sockaddr *)&all1_sa,
80562449Speter			    (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
80666963Speter			    ~RTF_CLONING, &rt)) != 0) {
80766963Speter				log(LOG_ERR,
80866963Speter				    "nd6_lookup: failed to add route for a "
80966963Speter				    "neighbor(%s), errno=%d\n",
81066963Speter				    ip6_sprintf(addr6), e);
81166963Speter			}
81266963Speter			if (rt == NULL)
81366963Speter				return (NULL);
81466963Speter			RT_LOCK(rt);
81566963Speter			if (rt->rt_llinfo) {
81697049Speter				struct llinfo_nd6 *ln =
81797049Speter				    (struct llinfo_nd6 *)rt->rt_llinfo;
81850276Speter				ln->ln_state = ND6_LLINFO_NOSTATE;
81966963Speter			}
82066963Speter		} else
82166963Speter			return (NULL);
82250276Speter	}
82362449Speter	RT_LOCK_ASSERT(rt);
82462449Speter	RT_REMREF(rt);
82576726Speter	/*
82662449Speter	 * Validation for the entry.
82762449Speter	 * Note that the check for rt_llinfo is necessary because a cloned
82876726Speter	 * route from a parent route that has the L flag (e.g. the default
82962449Speter	 * route to a p2p interface) may have the flag, too, while the
83062449Speter	 * destination is not actually a neighbor.
83162449Speter	 * XXX: we can't use rt->rt_ifp to check for the interface, since
83262449Speter	 *      it might be the loopback interface if the entry is for our
83362449Speter	 *      own address on a non-loopback interface. Instead, we should
83462449Speter	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL
83562449Speter	 *	interface.
83662449Speter	 */
83750276Speter	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
83862449Speter	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
83962449Speter	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
840166124Srafan		if (create) {
84162449Speter			nd6log((LOG_DEBUG,
84262449Speter			    "nd6_lookup: failed to lookup %s (if = %s)\n",
84350276Speter			    ip6_sprintf(addr6),
84462449Speter			    ifp ? if_name(ifp) : "unspec"));
84550276Speter		}
84662449Speter		RT_UNLOCK(rt);
84750276Speter		return (NULL);
84862449Speter	}
84962449Speter	RT_UNLOCK(rt);		/* XXX not ready to return rt locked */
85062449Speter	return (rt);
85162449Speter}
85262449Speter
85362449Speter/*
85462449Speter * Test whether a given IPv6 address is a neighbor or not, ignoring
85562449Speter * the actual neighbor cache.  The neighbor cache is ignored in order
85662449Speter * to not reenter the routing code from within itself.
85750276Speter */
85862449Speterstatic int
85962449Speternd6_is_new_addr_neighbor(addr, ifp)
86062449Speter	struct sockaddr_in6 *addr;
86162449Speter	struct ifnet *ifp;
86250276Speter{
86362449Speter	struct nd_prefix *pr;
86462449Speter
86562449Speter	/*
86662449Speter	 * A link-local address is always a neighbor.
86762449Speter	 * XXX: a link does not necessarily specify a single interface.
86862449Speter	 */
86950276Speter	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
87062449Speter		struct sockaddr_in6 sin6_copy;
87162449Speter		u_int32_t zone;
87262449Speter
87362449Speter		/*
87462449Speter		 * We need sin6_copy since sa6_recoverscope() may modify the
87562449Speter		 * content (XXX).
87662449Speter		 */
87762449Speter		sin6_copy = *addr;
87862449Speter		if (sa6_recoverscope(&sin6_copy))
87962449Speter			return (0); /* XXX: should be impossible */
88062449Speter		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
88150276Speter			return (0);
88262449Speter		if (sin6_copy.sin6_scope_id == zone)
88350276Speter			return (1);
88462449Speter		else
88562449Speter			return (0);
88662449Speter	}
88750276Speter
88862449Speter	/*
88962449Speter	 * If the address matches one of our addresses,
89062449Speter	 * it should be a neighbor.
89162449Speter	 * If the address matches one of our on-link prefixes, it should be a
89262449Speter	 * neighbor.
893166124Srafan	 */
894166124Srafan	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
895166124Srafan		if (pr->ndpr_ifp != ifp)
896166124Srafan			continue;
897166124Srafan
898166124Srafan		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
899166124Srafan			continue;
900166124Srafan
901166124Srafan		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
902166124Srafan		    &addr->sin6_addr, &pr->ndpr_mask))
903166124Srafan			return (1);
904166124Srafan	}
90562449Speter
90662449Speter	/*
907166124Srafan	 * If the default router list is empty, all addresses are regarded
90862449Speter	 * as on-link, and thus, as a neighbor.
90950276Speter	 * XXX: we restrict the condition to hosts, because routers usually do
91062449Speter	 * not have the "default router list".
91150276Speter	 */
91250276Speter	if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
91350276Speter	    nd6_defifindex == ifp->if_index) {
91450276Speter		return (1);
91550276Speter	}
91650276Speter
91750276Speter	return (0);
91850276Speter}
91950276Speter
92050276Speter
92150276Speter/*
92262449Speter * Detect if a given IPv6 address identifies a neighbor on a given link.
92362449Speter * XXX: should take care of the destination of a p2p link?
92450276Speter */
92597049Speterint
92697049Speternd6_is_addr_neighbor(addr, ifp)
92797049Speter	struct sockaddr_in6 *addr;
92862449Speter	struct ifnet *ifp;
92997049Speter{
93062449Speter
93197049Speter	if (nd6_is_new_addr_neighbor(addr, ifp))
93297049Speter		return (1);
93362449Speter
93497049Speter	/*
93597049Speter	 * Even if the address matches none of our addresses, it might be
93662449Speter	 * in the neighbor cache.
93750276Speter	 */
93850276Speter	if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL)
93950276Speter		return (1);
94050276Speter
94176726Speter	return (0);
942166124Srafan}
94350276Speter
944166124Srafan/*
94562449Speter * Free an nd6 llinfo entry.
94650276Speter */
94762449Speterstruct llinfo_nd6 *
94850276Speternd6_free(rt)
94950276Speter	struct rtentry *rt;
95062449Speter{
951166124Srafan	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
95262449Speter	struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
95362449Speter	struct nd_defrouter *dr;
95450276Speter
95597049Speter	/*
956166124Srafan	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
957166124Srafan	 * even though it is not harmful, it was not really necessary.
958166124Srafan	 */
959166124Srafan
960166124Srafan	if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
961166124Srafan		int s;
962166124Srafan		s = splnet();
963166124Srafan		dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
964166124Srafan		    rt->rt_ifp);
96566963Speter
966166124Srafan		if (ln->ln_router || dr) {
96762449Speter			/*
96862449Speter			 * rt6_flush must be called whether or not the neighbor
96962449Speter			 * is in the Default Router List.
97062449Speter			 * See a corresponding comment in nd6_na_input().
97162449Speter			 */
97250276Speter			rt6_flush(&in6, rt->rt_ifp);
973166124Srafan		}
974166124Srafan
97550276Speter		if (dr) {
97650276Speter			/*
97750276Speter			 * Unreachablity of a router might affect the default
97862449Speter			 * router selection and on-link detection of advertised
97962449Speter			 * prefixes.
98050276Speter			 */
98162449Speter
98262449Speter			/*
98362449Speter			 * Temporarily fake the state to choose a new default
98462449Speter			 * router and to perform on-link determination of
98562449Speter			 * prefixes correctly.
98662449Speter			 * Below the state will be set correctly,
98762449Speter			 * or the entry itself will be deleted.
98862449Speter			 */
98962449Speter			ln->ln_state = ND6_LLINFO_INCOMPLETE;
99062449Speter
99162449Speter			/*
99262449Speter			 * Since defrouter_select() does not affect the
99362449Speter			 * on-link determination and MIP6 needs the check
99462449Speter			 * before the default router selection, we perform
99562449Speter			 * the check now.
99662449Speter			 */
99762449Speter			pfxlist_onlink_check();
99862449Speter
99962449Speter			if (dr == TAILQ_FIRST(&nd_defrouter)) {
100062449Speter				/*
100162449Speter				 * It is used as the current default router,
100262449Speter				 * so we have to move it to the end of the
100362449Speter				 * list and choose a new one.
100462449Speter				 * XXX: it is not very efficient if this is
100562449Speter				 *      the only router.
100662449Speter				 */
100762449Speter				TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
100862449Speter				TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
100962449Speter
101097049Speter				defrouter_select();
101162449Speter			}
101297049Speter		}
101362449Speter		splx(s);
101462449Speter	}
101597049Speter
101697049Speter	/*
101797049Speter	 * Before deleting the entry, remember the next entry as the
101850276Speter	 * return value.  We need this because pfxlist_onlink_check() above
101962449Speter	 * might have freed other entries (particularly the old next entry) as
102066963Speter	 * a side effect (XXX).
1021166124Srafan	 */
102262449Speter	next = ln->ln_next;
102397049Speter
102462449Speter	/*
102562449Speter	 * Detach the route from the routing tree and the list of neighbor
102662449Speter	 * caches, and disable the route entry not to be used in already
102797049Speter	 * cached routes.
102897049Speter	 */
102997049Speter	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
103050276Speter	    rt_mask(rt), 0, (struct rtentry **)0);
103197049Speter
103297049Speter	return (next);
103397049Speter}
103497049Speter
103597049Speter/*
103697049Speter * Upper-layer reachability hint for Neighbor Unreachability Detection.
103797049Speter *
103897049Speter * XXX cost-effective methods?
103997049Speter */
104062449Spetervoid
104150276Speternd6_nud_hint(rt, dst6, force)
104250276Speter	struct rtentry *rt;
104350276Speter	struct in6_addr *dst6;
104450276Speter	int force;
104576726Speter{
104662449Speter	struct llinfo_nd6 *ln;
104762449Speter
104862449Speter	/*
104962449Speter	 * If the caller specified "rt", use that.  Otherwise, resolve the
105062449Speter	 * routing table by supplied "dst6".
105150276Speter	 */
105262449Speter	if (rt == NULL) {
105350276Speter		if (dst6 == NULL)
105497049Speter			return;
105597049Speter		if ((rt = nd6_lookup(dst6, 0, NULL)) == NULL)
105697049Speter			return;
105797049Speter	}
1058166124Srafan
105997049Speter	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
106097049Speter	    (rt->rt_flags & RTF_LLINFO) == 0 ||
106197049Speter	    rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
106297049Speter	    rt->rt_gateway->sa_family != AF_LINK) {
106397049Speter		/* This is not a host route. */
106497049Speter		return;
106597049Speter	}
106697049Speter
106797049Speter	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
106897049Speter	if (ln->ln_state < ND6_LLINFO_REACHABLE)
106997049Speter		return;
107097049Speter
107197049Speter	/*
107297049Speter	 * if we get upper-layer reachability confirmation many times,
107397049Speter	 * it is possible we have false information.
107497049Speter	 */
107597049Speter	if (!force) {
107697049Speter		ln->ln_byhint++;
107797049Speter		if (ln->ln_byhint > nd6_maxnudhint)
107897049Speter			return;
107997049Speter	}
108097049Speter
108197049Speter	ln->ln_state = ND6_LLINFO_REACHABLE;
108297049Speter	if (ln->ln_expire)
108397049Speter		ln->ln_expire = time_second +
108497049Speter			ND_IFINFO(rt->rt_ifp)->reachable;
108562449Speter}
108662449Speter
108762449Spetervoid
108850276Speternd6_rtrequest(req, rt, info)
108962449Speter	int	req;
109062449Speter	struct rtentry *rt;
109162449Speter	struct rt_addrinfo *info; /* xxx unused */
109262449Speter{
109362449Speter	struct sockaddr *gate = rt->rt_gateway;
109462449Speter	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
109562449Speter	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
109650276Speter	struct ifnet *ifp = rt->rt_ifp;
109762449Speter	struct ifaddr *ifa;
109850276Speter
109962449Speter	RT_LOCK_ASSERT(rt);
110097049Speter
110197049Speter	if ((rt->rt_flags & RTF_GATEWAY) != 0)
110262449Speter		return;
110350276Speter
110462449Speter	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
110562449Speter		/*
110662449Speter		 * This is probably an interface direct route for a link
110762449Speter		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
110862449Speter		 * We do not need special treatment below for such a route.
110962449Speter		 * Moreover, the RTF_LLINFO flag which would be set below
111050276Speter		 * would annoy the ndp(8) command.
111162449Speter		 */
111262449Speter		return;
111350276Speter	}
111462449Speter
111597049Speter	if (req == RTM_RESOLVE &&
111697049Speter	    (nd6_need_cache(ifp) == 0 || /* stf case */
111750276Speter	     !nd6_is_new_addr_neighbor((struct sockaddr_in6 *)rt_key(rt),
111850276Speter	     ifp))) {
111962449Speter		/*
1120166124Srafan		 * FreeBSD and BSD/OS often make a cloned host route based
112150276Speter		 * on a less-specific route (e.g. the default route).
112262449Speter		 * If the less specific route does not have a "gateway"
1123166124Srafan		 * (this is the case when the route just goes to a p2p or an
112450276Speter		 * stf interface), we'll mistakenly make a neighbor cache for
112562449Speter		 * the host route, and will see strange neighbor solicitation
112650276Speter		 * for the corresponding destination.  In order to avoid the
112762449Speter		 * confusion, we check if the destination of the route is
112862449Speter		 * a neighbor in terms of neighbor discovery, and stop the
112950276Speter		 * process if not.  Additionally, we remove the LLINFO flag
113062449Speter		 * so that ndp(8) will not try to get the neighbor information
113162449Speter		 * of the destination.
113262449Speter		 */
1133166124Srafan		rt->rt_flags &= ~RTF_LLINFO;
1134166124Srafan		return;
113562449Speter	}
113662449Speter
1137166124Srafan	switch (req) {
1138166124Srafan	case RTM_ADD:
113950276Speter		/*
114062449Speter		 * There is no backward compatibility :)
114162449Speter		 *
114262449Speter		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
114362449Speter		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
114462449Speter		 *	   rt->rt_flags |= RTF_CLONING;
114562449Speter		 */
114662449Speter		if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
114762449Speter			/*
114862449Speter			 * Case 1: This route should come from
114962449Speter			 * a route to interface.  RTF_LLINFO flag is set
115062449Speter			 * for a host route whose destination should be
115162449Speter			 * treated as on-link.
115250276Speter			 */
115362449Speter			rt_setgate(rt, rt_key(rt),
115450276Speter				   (struct sockaddr *)&null_sdl);
1155166124Srafan			gate = rt->rt_gateway;
115662449Speter			SDL(gate)->sdl_type = ifp->if_type;
115762449Speter			SDL(gate)->sdl_index = ifp->if_index;
115862449Speter			if (ln)
115950276Speter				ln->ln_expire = time_second;
116062449Speter			if (ln && ln->ln_expire == 0) {
116162449Speter				/* kludge for desktops */
116250276Speter				ln->ln_expire = 1;
116350276Speter			}
116462449Speter			if ((rt->rt_flags & RTF_CLONING) != 0)
116550276Speter				break;
116650276Speter		}
116776726Speter		/*
116876726Speter		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
116976726Speter		 * We don't do that here since llinfo is not ready yet.
117076726Speter		 *
117162449Speter		 * There are also couple of other things to be discussed:
117262449Speter		 * - unsolicited NA code needs improvement beforehand
117362449Speter		 * - RFC2461 says we MAY send multicast unsolicited NA
117462449Speter		 *   (7.2.6 paragraph 4), however, it also says that we
1175		 *   SHOULD provide a mechanism to prevent multicast NA storm.
1176		 *   we don't have anything like it right now.
1177		 *   note that the mechanism needs a mutual agreement
1178		 *   between proxies, which means that we need to implement
1179		 *   a new protocol, or a new kludge.
1180		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1181		 *   we need to check ip6forwarding before sending it.
1182		 *   (or should we allow proxy ND configuration only for
1183		 *   routers?  there's no mention about proxy ND from hosts)
1184		 */
1185#if 0
1186		/* XXX it does not work */
1187		if (rt->rt_flags & RTF_ANNOUNCE)
1188			nd6_na_output(ifp,
1189			      &SIN6(rt_key(rt))->sin6_addr,
1190			      &SIN6(rt_key(rt))->sin6_addr,
1191			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1192			      1, NULL);
1193#endif
1194		/* FALLTHROUGH */
1195	case RTM_RESOLVE:
1196		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1197			/*
1198			 * Address resolution isn't necessary for a point to
1199			 * point link, so we can skip this test for a p2p link.
1200			 */
1201			if (gate->sa_family != AF_LINK ||
1202			    gate->sa_len < sizeof(null_sdl)) {
1203				log(LOG_DEBUG,
1204				    "nd6_rtrequest: bad gateway value: %s\n",
1205				    if_name(ifp));
1206				break;
1207			}
1208			SDL(gate)->sdl_type = ifp->if_type;
1209			SDL(gate)->sdl_index = ifp->if_index;
1210		}
1211		if (ln != NULL)
1212			break;	/* This happens on a route change */
1213		/*
1214		 * Case 2: This route may come from cloning, or a manual route
1215		 * add with a LL address.
1216		 */
1217		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1218		rt->rt_llinfo = (caddr_t)ln;
1219		if (ln == NULL) {
1220			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1221			break;
1222		}
1223		nd6_inuse++;
1224		nd6_allocated++;
1225		bzero(ln, sizeof(*ln));
1226		ln->ln_rt = rt;
1227		/* this is required for "ndp" command. - shin */
1228		if (req == RTM_ADD) {
1229		        /*
1230			 * gate should have some valid AF_LINK entry,
1231			 * and ln->ln_expire should have some lifetime
1232			 * which is specified by ndp command.
1233			 */
1234			ln->ln_state = ND6_LLINFO_REACHABLE;
1235			ln->ln_byhint = 0;
1236		} else {
1237		        /*
1238			 * When req == RTM_RESOLVE, rt is created and
1239			 * initialized in rtrequest(), so rt_expire is 0.
1240			 */
1241			ln->ln_state = ND6_LLINFO_NOSTATE;
1242			ln->ln_expire = time_second;
1243		}
1244		rt->rt_flags |= RTF_LLINFO;
1245		ln->ln_next = llinfo_nd6.ln_next;
1246		llinfo_nd6.ln_next = ln;
1247		ln->ln_prev = &llinfo_nd6;
1248		ln->ln_next->ln_prev = ln;
1249
1250		/*
1251		 * check if rt_key(rt) is one of my address assigned
1252		 * to the interface.
1253		 */
1254		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1255		    &SIN6(rt_key(rt))->sin6_addr);
1256		if (ifa) {
1257			caddr_t macp = nd6_ifptomac(ifp);
1258			ln->ln_expire = 0;
1259			ln->ln_state = ND6_LLINFO_REACHABLE;
1260			ln->ln_byhint = 0;
1261			if (macp) {
1262				bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1263				SDL(gate)->sdl_alen = ifp->if_addrlen;
1264			}
1265			if (nd6_useloopback) {
1266				rt->rt_ifp = &loif[0];	/* XXX */
1267				/*
1268				 * Make sure rt_ifa be equal to the ifaddr
1269				 * corresponding to the address.
1270				 * We need this because when we refer
1271				 * rt_ifa->ia6_flags in ip6_input, we assume
1272				 * that the rt_ifa points to the address instead
1273				 * of the loopback address.
1274				 */
1275				if (ifa != rt->rt_ifa) {
1276					IFAFREE(rt->rt_ifa);
1277					IFAREF(ifa);
1278					rt->rt_ifa = ifa;
1279				}
1280			}
1281		} else if (rt->rt_flags & RTF_ANNOUNCE) {
1282			ln->ln_expire = 0;
1283			ln->ln_state = ND6_LLINFO_REACHABLE;
1284			ln->ln_byhint = 0;
1285
1286			/* join solicited node multicast for proxy ND */
1287			if (ifp->if_flags & IFF_MULTICAST) {
1288				struct in6_addr llsol;
1289				int error;
1290
1291				llsol = SIN6(rt_key(rt))->sin6_addr;
1292				llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1293				llsol.s6_addr32[1] = 0;
1294				llsol.s6_addr32[2] = htonl(1);
1295				llsol.s6_addr8[12] = 0xff;
1296				if (in6_setscope(&llsol, ifp, NULL))
1297					break;
1298				if (!in6_addmulti(&llsol, ifp, &error)) {
1299					nd6log((LOG_ERR, "%s: failed to join "
1300					    "%s (errno=%d)\n", if_name(ifp),
1301					    ip6_sprintf(&llsol), error));
1302				}
1303			}
1304		}
1305		break;
1306
1307	case RTM_DELETE:
1308		if (ln == NULL)
1309			break;
1310		/* leave from solicited node multicast for proxy ND */
1311		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1312		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1313			struct in6_addr llsol;
1314			struct in6_multi *in6m;
1315
1316			llsol = SIN6(rt_key(rt))->sin6_addr;
1317			llsol.s6_addr32[0] = IPV6_ADDR_INT32_MLL;
1318			llsol.s6_addr32[1] = 0;
1319			llsol.s6_addr32[2] = htonl(1);
1320			llsol.s6_addr8[12] = 0xff;
1321			if (in6_setscope(&llsol, ifp, NULL) == 0) {
1322				IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1323				if (in6m)
1324					in6_delmulti(in6m);
1325			} else
1326				; /* XXX: should not happen. bark here? */
1327		}
1328		nd6_inuse--;
1329		ln->ln_next->ln_prev = ln->ln_prev;
1330		ln->ln_prev->ln_next = ln->ln_next;
1331		ln->ln_prev = NULL;
1332		rt->rt_llinfo = 0;
1333		rt->rt_flags &= ~RTF_LLINFO;
1334		if (ln->ln_hold)
1335			m_freem(ln->ln_hold);
1336		Free((caddr_t)ln);
1337	}
1338}
1339
1340int
1341nd6_ioctl(cmd, data, ifp)
1342	u_long cmd;
1343	caddr_t	data;
1344	struct ifnet *ifp;
1345{
1346	struct in6_drlist *drl = (struct in6_drlist *)data;
1347	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1348	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1349	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1350	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1351	struct nd_defrouter *dr, any;
1352	struct nd_prefix *pr;
1353	struct rtentry *rt;
1354	int i = 0, error = 0;
1355	int s;
1356
1357	switch (cmd) {
1358	case SIOCGDRLST_IN6:
1359		/*
1360		 * obsolete API, use sysctl under net.inet6.icmp6
1361		 */
1362		bzero(drl, sizeof(*drl));
1363		s = splnet();
1364		dr = TAILQ_FIRST(&nd_defrouter);
1365		while (dr && i < DRLSTSIZ) {
1366			drl->defrouter[i].rtaddr = dr->rtaddr;
1367			in6_clearscope(&drl->defrouter[i].rtaddr);
1368
1369			drl->defrouter[i].flags = dr->flags;
1370			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1371			drl->defrouter[i].expire = dr->expire;
1372			drl->defrouter[i].if_index = dr->ifp->if_index;
1373			i++;
1374			dr = TAILQ_NEXT(dr, dr_entry);
1375		}
1376		splx(s);
1377		break;
1378	case SIOCGPRLST_IN6:
1379		/*
1380		 * obsolete API, use sysctl under net.inet6.icmp6
1381		 *
1382		 * XXX the structure in6_prlist was changed in backward-
1383		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1384		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1385		 */
1386		/*
1387		 * XXX meaning of fields, especialy "raflags", is very
1388		 * differnet between RA prefix list and RR/static prefix list.
1389		 * how about separating ioctls into two?
1390		 */
1391		bzero(oprl, sizeof(*oprl));
1392		s = splnet();
1393		pr = nd_prefix.lh_first;
1394		while (pr && i < PRLSTSIZ) {
1395			struct nd_pfxrouter *pfr;
1396			int j;
1397
1398			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1399			oprl->prefix[i].raflags = pr->ndpr_raf;
1400			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1401			oprl->prefix[i].vltime = pr->ndpr_vltime;
1402			oprl->prefix[i].pltime = pr->ndpr_pltime;
1403			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1404			oprl->prefix[i].expire = pr->ndpr_expire;
1405
1406			pfr = pr->ndpr_advrtrs.lh_first;
1407			j = 0;
1408			while (pfr) {
1409				if (j < DRLSTSIZ) {
1410#define RTRADDR oprl->prefix[i].advrtr[j]
1411					RTRADDR = pfr->router->rtaddr;
1412					in6_clearscope(&RTRADDR);
1413#undef RTRADDR
1414				}
1415				j++;
1416				pfr = pfr->pfr_next;
1417			}
1418			oprl->prefix[i].advrtrs = j;
1419			oprl->prefix[i].origin = PR_ORIG_RA;
1420
1421			i++;
1422			pr = pr->ndpr_next;
1423		}
1424		splx(s);
1425
1426		break;
1427	case OSIOCGIFINFO_IN6:
1428#define ND	ndi->ndi
1429		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1430		bzero(&ND, sizeof(ND));
1431		ND.linkmtu = IN6_LINKMTU(ifp);
1432		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1433		ND.basereachable = ND_IFINFO(ifp)->basereachable;
1434		ND.reachable = ND_IFINFO(ifp)->reachable;
1435		ND.retrans = ND_IFINFO(ifp)->retrans;
1436		ND.flags = ND_IFINFO(ifp)->flags;
1437		ND.recalctm = ND_IFINFO(ifp)->recalctm;
1438		ND.chlim = ND_IFINFO(ifp)->chlim;
1439		break;
1440	case SIOCGIFINFO_IN6:
1441		ND = *ND_IFINFO(ifp);
1442		ND.linkmtu = IN6_LINKMTU(ifp);
1443		break;
1444	case SIOCSIFINFO_IN6:
1445		/*
1446		 * used to change host variables from userland.
1447		 * intented for a use on router to reflect RA configurations.
1448		 */
1449		/* 0 means 'unspecified' */
1450		if (ND.linkmtu != 0) {
1451			if (ND.linkmtu < IPV6_MMTU ||
1452			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1453				error = EINVAL;
1454				break;
1455			}
1456			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1457		}
1458
1459		if (ND.basereachable != 0) {
1460			int obasereachable = ND_IFINFO(ifp)->basereachable;
1461
1462			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1463			if (ND.basereachable != obasereachable)
1464				ND_IFINFO(ifp)->reachable =
1465				    ND_COMPUTE_RTIME(ND.basereachable);
1466		}
1467		if (ND.retrans != 0)
1468			ND_IFINFO(ifp)->retrans = ND.retrans;
1469		if (ND.chlim != 0)
1470			ND_IFINFO(ifp)->chlim = ND.chlim;
1471		/* FALLTHROUGH */
1472	case SIOCSIFINFO_FLAGS:
1473		ND_IFINFO(ifp)->flags = ND.flags;
1474		break;
1475#undef ND
1476	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1477		/* flush default router list */
1478		/*
1479		 * xxx sumikawa: should not delete route if default
1480		 * route equals to the top of default router list
1481		 */
1482		bzero(&any, sizeof(any));
1483		defrouter_delreq(&any, 0);
1484		defrouter_select();
1485		/* xxx sumikawa: flush prefix list */
1486		break;
1487	case SIOCSPFXFLUSH_IN6:
1488	{
1489		/* flush all the prefix advertised by routers */
1490		struct nd_prefix *pr, *next;
1491
1492		s = splnet();
1493		for (pr = nd_prefix.lh_first; pr; pr = next) {
1494			struct in6_ifaddr *ia, *ia_next;
1495
1496			next = pr->ndpr_next;
1497
1498			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1499				continue; /* XXX */
1500
1501			/* do we really have to remove addresses as well? */
1502			for (ia = in6_ifaddr; ia; ia = ia_next) {
1503				/* ia might be removed.  keep the next ptr. */
1504				ia_next = ia->ia_next;
1505
1506				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1507					continue;
1508
1509				if (ia->ia6_ndpr == pr)
1510					in6_purgeaddr(&ia->ia_ifa);
1511			}
1512			prelist_remove(pr);
1513		}
1514		splx(s);
1515		break;
1516	}
1517	case SIOCSRTRFLUSH_IN6:
1518	{
1519		/* flush all the default routers */
1520		struct nd_defrouter *dr, *next;
1521
1522		s = splnet();
1523		if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
1524			/*
1525			 * The first entry of the list may be stored in
1526			 * the routing table, so we'll delete it later.
1527			 */
1528			for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
1529				next = TAILQ_NEXT(dr, dr_entry);
1530				defrtrlist_del(dr);
1531			}
1532			defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
1533		}
1534		splx(s);
1535		break;
1536	}
1537	case SIOCGNBRINFO_IN6:
1538	{
1539		struct llinfo_nd6 *ln;
1540		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1541
1542		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1543			return (error);
1544
1545		s = splnet();
1546		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
1547			error = EINVAL;
1548			splx(s);
1549			break;
1550		}
1551		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1552		nbi->state = ln->ln_state;
1553		nbi->asked = ln->ln_asked;
1554		nbi->isrouter = ln->ln_router;
1555		nbi->expire = ln->ln_expire;
1556		splx(s);
1557
1558		break;
1559	}
1560	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1561		ndif->ifindex = nd6_defifindex;
1562		break;
1563	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1564		return (nd6_setdefaultiface(ndif->ifindex));
1565	}
1566	return (error);
1567}
1568
1569/*
1570 * Create neighbor cache entry and cache link-layer address,
1571 * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1572 */
1573struct rtentry *
1574nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
1575	struct ifnet *ifp;
1576	struct in6_addr *from;
1577	char *lladdr;
1578	int lladdrlen;
1579	int type;	/* ICMP6 type */
1580	int code;	/* type dependent information */
1581{
1582	struct rtentry *rt = NULL;
1583	struct llinfo_nd6 *ln = NULL;
1584	int is_newentry;
1585	struct sockaddr_dl *sdl = NULL;
1586	int do_update;
1587	int olladdr;
1588	int llchange;
1589	int newstate = 0;
1590
1591	if (ifp == NULL)
1592		panic("ifp == NULL in nd6_cache_lladdr");
1593	if (from == NULL)
1594		panic("from == NULL in nd6_cache_lladdr");
1595
1596	/* nothing must be updated for unspecified address */
1597	if (IN6_IS_ADDR_UNSPECIFIED(from))
1598		return NULL;
1599
1600	/*
1601	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1602	 * the caller.
1603	 *
1604	 * XXX If the link does not have link-layer adderss, what should
1605	 * we do? (ifp->if_addrlen == 0)
1606	 * Spec says nothing in sections for RA, RS and NA.  There's small
1607	 * description on it in NS section (RFC 2461 7.2.3).
1608	 */
1609
1610	rt = nd6_lookup(from, 0, ifp);
1611	if (rt == NULL) {
1612#if 0
1613		/* nothing must be done if there's no lladdr */
1614		if (!lladdr || !lladdrlen)
1615			return NULL;
1616#endif
1617
1618		rt = nd6_lookup(from, 1, ifp);
1619		is_newentry = 1;
1620	} else {
1621		/* do nothing if static ndp is set */
1622		if (rt->rt_flags & RTF_STATIC)
1623			return NULL;
1624		is_newentry = 0;
1625	}
1626
1627	if (rt == NULL)
1628		return NULL;
1629	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1630fail:
1631		(void)nd6_free(rt);
1632		return NULL;
1633	}
1634	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1635	if (ln == NULL)
1636		goto fail;
1637	if (rt->rt_gateway == NULL)
1638		goto fail;
1639	if (rt->rt_gateway->sa_family != AF_LINK)
1640		goto fail;
1641	sdl = SDL(rt->rt_gateway);
1642
1643	olladdr = (sdl->sdl_alen) ? 1 : 0;
1644	if (olladdr && lladdr) {
1645		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1646			llchange = 1;
1647		else
1648			llchange = 0;
1649	} else
1650		llchange = 0;
1651
1652	/*
1653	 * newentry olladdr  lladdr  llchange	(*=record)
1654	 *	0	n	n	--	(1)
1655	 *	0	y	n	--	(2)
1656	 *	0	n	y	--	(3) * STALE
1657	 *	0	y	y	n	(4) *
1658	 *	0	y	y	y	(5) * STALE
1659	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1660	 *	1	--	y	--	(7) * STALE
1661	 */
1662
1663	if (lladdr) {		/* (3-5) and (7) */
1664		/*
1665		 * Record source link-layer address
1666		 * XXX is it dependent to ifp->if_type?
1667		 */
1668		sdl->sdl_alen = ifp->if_addrlen;
1669		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1670	}
1671
1672	if (!is_newentry) {
1673		if ((!olladdr && lladdr != NULL) ||	/* (3) */
1674		    (olladdr && lladdr != NULL && llchange)) {	/* (5) */
1675			do_update = 1;
1676			newstate = ND6_LLINFO_STALE;
1677		} else					/* (1-2,4) */
1678			do_update = 0;
1679	} else {
1680		do_update = 1;
1681		if (lladdr == NULL)			/* (6) */
1682			newstate = ND6_LLINFO_NOSTATE;
1683		else					/* (7) */
1684			newstate = ND6_LLINFO_STALE;
1685	}
1686
1687	if (do_update) {
1688		/*
1689		 * Update the state of the neighbor cache.
1690		 */
1691		ln->ln_state = newstate;
1692
1693		if (ln->ln_state == ND6_LLINFO_STALE) {
1694			/*
1695			 * XXX: since nd6_output() below will cause
1696			 * state tansition to DELAY and reset the timer,
1697			 * we must set the timer now, although it is actually
1698			 * meaningless.
1699			 */
1700			ln->ln_expire = time_second + nd6_gctimer;
1701
1702			if (ln->ln_hold) {
1703				/*
1704				 * we assume ifp is not a p2p here, so just
1705				 * set the 2nd argument as the 1st one.
1706				 */
1707				nd6_output(ifp, ifp, ln->ln_hold,
1708				    (struct sockaddr_in6 *)rt_key(rt), rt);
1709				ln->ln_hold = NULL;
1710			}
1711		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1712			/* probe right away */
1713			ln->ln_expire = time_second;
1714		}
1715	}
1716
1717	/*
1718	 * ICMP6 type dependent behavior.
1719	 *
1720	 * NS: clear IsRouter if new entry
1721	 * RS: clear IsRouter
1722	 * RA: set IsRouter if there's lladdr
1723	 * redir: clear IsRouter if new entry
1724	 *
1725	 * RA case, (1):
1726	 * The spec says that we must set IsRouter in the following cases:
1727	 * - If lladdr exist, set IsRouter.  This means (1-5).
1728	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1729	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1730	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1731	 * neighbor cache, this is similar to (6).
1732	 * This case is rare but we figured that we MUST NOT set IsRouter.
1733	 *
1734	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1735	 *							D R
1736	 *	0	n	n	--	(1)	c   ?     s
1737	 *	0	y	n	--	(2)	c   s     s
1738	 *	0	n	y	--	(3)	c   s     s
1739	 *	0	y	y	n	(4)	c   s     s
1740	 *	0	y	y	y	(5)	c   s     s
1741	 *	1	--	n	--	(6) c	c 	c s
1742	 *	1	--	y	--	(7) c	c   s	c s
1743	 *
1744	 *					(c=clear s=set)
1745	 */
1746	switch (type & 0xff) {
1747	case ND_NEIGHBOR_SOLICIT:
1748		/*
1749		 * New entry must have is_router flag cleared.
1750		 */
1751		if (is_newentry)	/* (6-7) */
1752			ln->ln_router = 0;
1753		break;
1754	case ND_REDIRECT:
1755		/*
1756		 * If the icmp is a redirect to a better router, always set the
1757		 * is_router flag.  Otherwise, if the entry is newly created,
1758		 * clear the flag.  [RFC 2461, sec 8.3]
1759		 */
1760		if (code == ND_REDIRECT_ROUTER)
1761			ln->ln_router = 1;
1762		else if (is_newentry) /* (6-7) */
1763			ln->ln_router = 0;
1764		break;
1765	case ND_ROUTER_SOLICIT:
1766		/*
1767		 * is_router flag must always be cleared.
1768		 */
1769		ln->ln_router = 0;
1770		break;
1771	case ND_ROUTER_ADVERT:
1772		/*
1773		 * Mark an entry with lladdr as a router.
1774		 */
1775		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1776		    (is_newentry && lladdr)) {			/* (7) */
1777			ln->ln_router = 1;
1778		}
1779		break;
1780	}
1781
1782	/*
1783	 * When the link-layer address of a router changes, select the
1784	 * best router again.  In particular, when the neighbor entry is newly
1785	 * created, it might affect the selection policy.
1786	 * Question: can we restrict the first condition to the "is_newentry"
1787	 * case?
1788	 * XXX: when we hear an RA from a new router with the link-layer
1789	 * address option, defrouter_select() is called twice, since
1790	 * defrtrlist_update called the function as well.  However, I believe
1791	 * we can compromise the overhead, since it only happens the first
1792	 * time.
1793	 * XXX: although defrouter_select() should not have a bad effect
1794	 * for those are not autoconfigured hosts, we explicitly avoid such
1795	 * cases for safety.
1796	 */
1797	if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
1798		defrouter_select();
1799
1800	return rt;
1801}
1802
1803static void
1804nd6_slowtimo(ignored_arg)
1805    void *ignored_arg;
1806{
1807	int s = splnet();
1808	struct nd_ifinfo *nd6if;
1809	struct ifnet *ifp;
1810
1811	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1812	    nd6_slowtimo, NULL);
1813	IFNET_RLOCK();
1814	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1815		nd6if = ND_IFINFO(ifp);
1816		if (nd6if->basereachable && /* already initialized */
1817		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1818			/*
1819			 * Since reachable time rarely changes by router
1820			 * advertisements, we SHOULD insure that a new random
1821			 * value gets recomputed at least once every few hours.
1822			 * (RFC 2461, 6.3.4)
1823			 */
1824			nd6if->recalctm = nd6_recalc_reachtm_interval;
1825			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1826		}
1827	}
1828	IFNET_RUNLOCK();
1829	splx(s);
1830}
1831
1832#define senderr(e) { error = (e); goto bad;}
1833int
1834nd6_output(ifp, origifp, m0, dst, rt0)
1835	struct ifnet *ifp;
1836	struct ifnet *origifp;
1837	struct mbuf *m0;
1838	struct sockaddr_in6 *dst;
1839	struct rtentry *rt0;
1840{
1841	struct mbuf *m = m0;
1842	struct rtentry *rt = rt0;
1843	struct sockaddr_in6 *gw6 = NULL;
1844	struct llinfo_nd6 *ln = NULL;
1845	int error = 0;
1846
1847	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1848		goto sendpkt;
1849
1850	if (nd6_need_cache(ifp) == 0)
1851		goto sendpkt;
1852
1853	/*
1854	 * next hop determination.  This routine is derived from ether_output.
1855	 */
1856again:
1857	if (rt) {
1858		if ((rt->rt_flags & RTF_UP) == 0) {
1859			rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL);
1860			if (rt != NULL) {
1861				RT_REMREF(rt);
1862				RT_UNLOCK(rt);
1863				if (rt->rt_ifp != ifp)
1864					/*
1865					 * XXX maybe we should update ifp too,
1866					 * but the original code didn't and I
1867					 * don't know what is correct here.
1868					 */
1869					goto again;
1870			} else
1871				senderr(EHOSTUNREACH);
1872		}
1873
1874		if (rt->rt_flags & RTF_GATEWAY) {
1875			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1876
1877			/*
1878			 * We skip link-layer address resolution and NUD
1879			 * if the gateway is not a neighbor from ND point
1880			 * of view, regardless of the value of nd_ifinfo.flags.
1881			 * The second condition is a bit tricky; we skip
1882			 * if the gateway is our own address, which is
1883			 * sometimes used to install a route to a p2p link.
1884			 */
1885			if (!nd6_is_addr_neighbor(gw6, ifp) ||
1886			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1887				/*
1888				 * We allow this kind of tricky route only
1889				 * when the outgoing interface is p2p.
1890				 * XXX: we may need a more generic rule here.
1891				 */
1892				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1893					senderr(EHOSTUNREACH);
1894
1895				goto sendpkt;
1896			}
1897
1898			if (rt->rt_gwroute == 0)
1899				goto lookup;
1900			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1901				RT_LOCK(rt);
1902				rtfree(rt); rt = rt0;
1903			lookup:
1904				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
1905				if ((rt = rt->rt_gwroute) == 0)
1906					senderr(EHOSTUNREACH);
1907				RT_UNLOCK(rt);
1908			}
1909		}
1910	}
1911
1912	/*
1913	 * Address resolution or Neighbor Unreachability Detection
1914	 * for the next hop.
1915	 * At this point, the destination of the packet must be a unicast
1916	 * or an anycast address(i.e. not a multicast).
1917	 */
1918
1919	/* Look up the neighbor cache for the nexthop */
1920	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1921		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1922	else {
1923		/*
1924		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1925		 * the condition below is not very efficient.  But we believe
1926		 * it is tolerable, because this should be a rare case.
1927		 */
1928		if (nd6_is_addr_neighbor(dst, ifp) &&
1929		    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1930			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1931	}
1932	if (ln == NULL || rt == NULL) {
1933		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1934		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1935			log(LOG_DEBUG,
1936			    "nd6_output: can't allocate llinfo for %s "
1937			    "(ln=%p, rt=%p)\n",
1938			    ip6_sprintf(&dst->sin6_addr), ln, rt);
1939			senderr(EIO);	/* XXX: good error? */
1940		}
1941
1942		goto sendpkt;	/* send anyway */
1943	}
1944
1945	/* We don't have to do link-layer address resolution on a p2p link. */
1946	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1947	    ln->ln_state < ND6_LLINFO_REACHABLE) {
1948		ln->ln_state = ND6_LLINFO_STALE;
1949		ln->ln_expire = time_second + nd6_gctimer;
1950	}
1951
1952	/*
1953	 * The first time we send a packet to a neighbor whose entry is
1954	 * STALE, we have to change the state to DELAY and a sets a timer to
1955	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1956	 * neighbor unreachability detection on expiration.
1957	 * (RFC 2461 7.3.3)
1958	 */
1959	if (ln->ln_state == ND6_LLINFO_STALE) {
1960		ln->ln_asked = 0;
1961		ln->ln_state = ND6_LLINFO_DELAY;
1962		ln->ln_expire = time_second + nd6_delay;
1963	}
1964
1965	/*
1966	 * If the neighbor cache entry has a state other than INCOMPLETE
1967	 * (i.e. its link-layer address is already resolved), just
1968	 * send the packet.
1969	 */
1970	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1971		goto sendpkt;
1972
1973	/*
1974	 * There is a neighbor cache entry, but no ethernet address
1975	 * response yet.  Replace the held mbuf (if any) with this
1976	 * latest one.
1977	 *
1978	 * This code conforms to the rate-limiting rule described in Section
1979	 * 7.2.2 of RFC 2461, because the timer is set correctly after sending
1980	 * an NS below.
1981	 */
1982	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1983		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1984	if (ln->ln_hold)
1985		m_freem(ln->ln_hold);
1986	ln->ln_hold = m;
1987	if (ln->ln_expire) {
1988		if (ln->ln_asked < nd6_mmaxtries &&
1989		    ln->ln_expire < time_second) {
1990			ln->ln_asked++;
1991			ln->ln_expire = time_second +
1992				ND_IFINFO(ifp)->retrans / 1000;
1993			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1994		}
1995	}
1996	return (0);
1997
1998  sendpkt:
1999#ifdef IPSEC
2000	/* clean ipsec history once it goes out of the node */
2001	ipsec_delaux(m);
2002#endif
2003
2004#ifdef MAC
2005	mac_create_mbuf_linklayer(ifp, m);
2006#endif
2007	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
2008		return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
2009		    rt));
2010	}
2011	return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
2012
2013  bad:
2014	if (m)
2015		m_freem(m);
2016	return (error);
2017}
2018#undef senderr
2019
2020int
2021nd6_need_cache(ifp)
2022	struct ifnet *ifp;
2023{
2024	/*
2025	 * XXX: we currently do not make neighbor cache on any interface
2026	 * other than ARCnet, Ethernet, FDDI and GIF.
2027	 *
2028	 * RFC2893 says:
2029	 * - unidirectional tunnels needs no ND
2030	 */
2031	switch (ifp->if_type) {
2032	case IFT_ARCNET:
2033	case IFT_ETHER:
2034	case IFT_FDDI:
2035	case IFT_IEEE1394:
2036#ifdef IFT_L2VLAN
2037	case IFT_L2VLAN:
2038#endif
2039#ifdef IFT_IEEE80211
2040	case IFT_IEEE80211:
2041#endif
2042#ifdef IFT_CARP
2043	case IFT_CARP:
2044#endif
2045	case IFT_GIF:		/* XXX need more cases? */
2046	case IFT_BRIDGE:
2047		return (1);
2048	default:
2049		return (0);
2050	}
2051}
2052
2053int
2054nd6_storelladdr(ifp, rt0, m, dst, desten)
2055	struct ifnet *ifp;
2056	struct rtentry *rt0;
2057	struct mbuf *m;
2058	struct sockaddr *dst;
2059	u_char *desten;
2060{
2061	struct sockaddr_dl *sdl;
2062	struct rtentry *rt;
2063	int error;
2064
2065	if (m->m_flags & M_MCAST) {
2066		int i;
2067
2068		switch (ifp->if_type) {
2069		case IFT_ETHER:
2070		case IFT_FDDI:
2071#ifdef IFT_L2VLAN
2072		case IFT_L2VLAN:
2073#endif
2074#ifdef IFT_IEEE80211
2075		case IFT_IEEE80211:
2076#endif
2077		case IFT_BRIDGE:
2078		case IFT_ISO88025:
2079			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2080						 desten);
2081			return (0);
2082		case IFT_IEEE1394:
2083			/*
2084			 * netbsd can use if_broadcastaddr, but we don't do so
2085			 * to reduce # of ifdef.
2086			 */
2087			for (i = 0; i < ifp->if_addrlen; i++)
2088				desten[i] = ~0;
2089			return (0);
2090		case IFT_ARCNET:
2091			*desten = 0;
2092			return (0);
2093		default:
2094			m_freem(m);
2095			return (EAFNOSUPPORT);
2096		}
2097	}
2098
2099	if (rt0 == NULL) {
2100		/* this could happen, if we could not allocate memory */
2101		m_freem(m);
2102		return (ENOMEM);
2103	}
2104
2105	error = rt_check(&rt, &rt0, dst);
2106	if (error) {
2107		m_freem(m);
2108		return (error);
2109	}
2110	RT_UNLOCK(rt);
2111
2112	if (rt->rt_gateway->sa_family != AF_LINK) {
2113		printf("nd6_storelladdr: something odd happens\n");
2114		m_freem(m);
2115		return (EINVAL);
2116	}
2117	sdl = SDL(rt->rt_gateway);
2118	if (sdl->sdl_alen == 0) {
2119		/* this should be impossible, but we bark here for debugging */
2120		printf("nd6_storelladdr: sdl_alen == 0\n");
2121		m_freem(m);
2122		return (EINVAL);
2123	}
2124
2125	bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2126	return (0);
2127}
2128
2129static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2130static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2131#ifdef SYSCTL_DECL
2132SYSCTL_DECL(_net_inet6_icmp6);
2133#endif
2134SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2135	CTLFLAG_RD, nd6_sysctl_drlist, "");
2136SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2137	CTLFLAG_RD, nd6_sysctl_prlist, "");
2138
2139static int
2140nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2141{
2142	int error;
2143	char buf[1024];
2144	struct in6_defrouter *d, *de;
2145	struct nd_defrouter *dr;
2146
2147	if (req->newptr)
2148		return EPERM;
2149	error = 0;
2150
2151	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2152	     dr = TAILQ_NEXT(dr, dr_entry)) {
2153		d = (struct in6_defrouter *)buf;
2154		de = (struct in6_defrouter *)(buf + sizeof(buf));
2155
2156		if (d + 1 <= de) {
2157			bzero(d, sizeof(*d));
2158			d->rtaddr.sin6_family = AF_INET6;
2159			d->rtaddr.sin6_len = sizeof(d->rtaddr);
2160			d->rtaddr.sin6_addr = dr->rtaddr;
2161			if (sa6_recoverscope(&d->rtaddr)) {
2162				log(LOG_ERR,
2163				    "scope error in router list (%s)\n",
2164				    ip6_sprintf(&d->rtaddr.sin6_addr));
2165				/* XXX: press on... */
2166			}
2167			d->flags = dr->flags;
2168			d->rtlifetime = dr->rtlifetime;
2169			d->expire = dr->expire;
2170			d->if_index = dr->ifp->if_index;
2171		} else
2172			panic("buffer too short");
2173
2174		error = SYSCTL_OUT(req, buf, sizeof(*d));
2175		if (error)
2176			break;
2177	}
2178
2179	return (error);
2180}
2181
2182static int
2183nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2184{
2185	int error;
2186	char buf[1024];
2187	struct in6_prefix *p, *pe;
2188	struct nd_prefix *pr;
2189
2190	if (req->newptr)
2191		return EPERM;
2192	error = 0;
2193
2194	for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2195		u_short advrtrs;
2196		size_t advance;
2197		struct sockaddr_in6 *sin6, *s6;
2198		struct nd_pfxrouter *pfr;
2199
2200		p = (struct in6_prefix *)buf;
2201		pe = (struct in6_prefix *)(buf + sizeof(buf));
2202
2203		if (p + 1 <= pe) {
2204			bzero(p, sizeof(*p));
2205			sin6 = (struct sockaddr_in6 *)(p + 1);
2206
2207			p->prefix = pr->ndpr_prefix;
2208			if (sa6_recoverscope(&p->prefix)) {
2209				log(LOG_ERR,
2210				    "scope error in prefix list (%s)\n",
2211				    ip6_sprintf(&p->prefix.sin6_addr));
2212				/* XXX: press on... */
2213			}
2214			p->raflags = pr->ndpr_raf;
2215			p->prefixlen = pr->ndpr_plen;
2216			p->vltime = pr->ndpr_vltime;
2217			p->pltime = pr->ndpr_pltime;
2218			p->if_index = pr->ndpr_ifp->if_index;
2219			p->expire = pr->ndpr_expire;
2220			p->refcnt = pr->ndpr_refcnt;
2221			p->flags = pr->ndpr_stateflags;
2222			p->origin = PR_ORIG_RA;
2223			advrtrs = 0;
2224			for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2225			     pfr = pfr->pfr_next) {
2226				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2227					advrtrs++;
2228					continue;
2229				}
2230				s6 = &sin6[advrtrs];
2231				bzero(s6, sizeof(*s6));
2232				s6->sin6_family = AF_INET6;
2233				s6->sin6_len = sizeof(*sin6);
2234				s6->sin6_addr = pfr->router->rtaddr;
2235				if (sa6_recoverscope(s6)) {
2236					log(LOG_ERR,
2237					    "scope error in "
2238					    "prefix list (%s)\n",
2239					    ip6_sprintf(&pfr->router->rtaddr));
2240				}
2241				advrtrs++;
2242			}
2243			p->advrtrs = advrtrs;
2244		} else
2245			panic("buffer too short");
2246
2247		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2248		error = SYSCTL_OUT(req, buf, advance);
2249		if (error)
2250			break;
2251	}
2252
2253	return (error);
2254}
2255