Home | History | Annotate | Line # | Download | only in netinet6
nd6.c revision 1.169
      1 /*	$NetBSD: nd6.c,v 1.169 2015/08/24 22:21:27 pooka Exp $	*/
      2 /*	$KAME: nd6.c,v 1.279 2002/06/08 11:16:51 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 __KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.169 2015/08/24 22:21:27 pooka Exp $");
     35 
     36 #ifdef _KERNEL_OPT
     37 #include "opt_net_mpsafe.h"
     38 #endif
     39 
     40 #include "bridge.h"
     41 #include "carp.h"
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/callout.h>
     46 #include <sys/malloc.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/socket.h>
     49 #include <sys/socketvar.h>
     50 #include <sys/sockio.h>
     51 #include <sys/time.h>
     52 #include <sys/kernel.h>
     53 #include <sys/protosw.h>
     54 #include <sys/errno.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/syslog.h>
     57 #include <sys/queue.h>
     58 #include <sys/cprng.h>
     59 
     60 #include <net/if.h>
     61 #include <net/if_dl.h>
     62 #include <net/if_types.h>
     63 #include <net/route.h>
     64 #include <net/if_ether.h>
     65 #include <net/if_fddi.h>
     66 #include <net/if_arc.h>
     67 
     68 #include <netinet/in.h>
     69 #include <netinet6/in6_var.h>
     70 #include <netinet/ip6.h>
     71 #include <netinet6/ip6_var.h>
     72 #include <netinet6/scope6_var.h>
     73 #include <netinet6/nd6.h>
     74 #include <netinet6/in6_ifattach.h>
     75 #include <netinet/icmp6.h>
     76 #include <netinet6/icmp6_private.h>
     77 
     78 #include <net/net_osdep.h>
     79 
     80 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
     81 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
     82 
     83 /* timer values */
     84 int	nd6_prune	= 1;	/* walk list every 1 seconds */
     85 int	nd6_delay	= 5;	/* delay first probe time 5 second */
     86 int	nd6_umaxtries	= 3;	/* maximum unicast query */
     87 int	nd6_mmaxtries	= 3;	/* maximum multicast query */
     88 int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
     89 int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
     90 
     91 /* preventing too many loops in ND option parsing */
     92 int nd6_maxndopt = 10;	/* max # of ND options allowed */
     93 
     94 int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
     95 
     96 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */
     97 
     98 #ifdef ND6_DEBUG
     99 int nd6_debug = 1;
    100 #else
    101 int nd6_debug = 0;
    102 #endif
    103 
    104 /* for debugging? */
    105 static int nd6_inuse, nd6_allocated;
    106 
    107 struct llinfo_nd6 llinfo_nd6 = {
    108 	.ln_prev = &llinfo_nd6,
    109 	.ln_next = &llinfo_nd6,
    110 };
    111 struct nd_drhead nd_defrouter;
    112 struct nd_prhead nd_prefix = { 0 };
    113 
    114 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
    115 static const struct sockaddr_in6 all1_sa = {
    116 	  .sin6_family = AF_INET6
    117 	, .sin6_len = sizeof(struct sockaddr_in6)
    118 	, .sin6_addr = {.s6_addr = {0xff, 0xff, 0xff, 0xff,
    119 				    0xff, 0xff, 0xff, 0xff,
    120 				    0xff, 0xff, 0xff, 0xff,
    121 				    0xff, 0xff, 0xff, 0xff}}
    122 };
    123 
    124 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
    125 static void nd6_slowtimo(void *);
    126 static int regen_tmpaddr(struct in6_ifaddr *);
    127 static struct llinfo_nd6 *nd6_free(struct rtentry *, int);
    128 static void nd6_llinfo_timer(void *);
    129 static void clear_llinfo_pqueue(struct llinfo_nd6 *);
    130 
    131 callout_t nd6_slowtimo_ch;
    132 callout_t nd6_timer_ch;
    133 extern callout_t in6_tmpaddrtimer_ch;
    134 
    135 static int fill_drlist(void *, size_t *, size_t);
    136 static int fill_prlist(void *, size_t *, size_t);
    137 
    138 MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
    139 
    140 #define LN_DEQUEUE(ln) do { \
    141 	(ln)->ln_next->ln_prev = (ln)->ln_prev; \
    142 	(ln)->ln_prev->ln_next = (ln)->ln_next; \
    143 	} while (/*CONSTCOND*/0)
    144 #define LN_INSERTHEAD(ln) do { \
    145 	(ln)->ln_next = llinfo_nd6.ln_next; \
    146 	llinfo_nd6.ln_next = (ln); \
    147 	(ln)->ln_prev = &llinfo_nd6; \
    148 	(ln)->ln_next->ln_prev = (ln); \
    149 	} while (/*CONSTCOND*/0)
    150 void
    151 nd6_init(void)
    152 {
    153 	static int nd6_init_done = 0;
    154 
    155 	if (nd6_init_done) {
    156 		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
    157 		return;
    158 	}
    159 
    160 	/* initialization of the default router list */
    161 	TAILQ_INIT(&nd_defrouter);
    162 
    163 	nd6_init_done = 1;
    164 
    165 	callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE);
    166 	callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
    167 
    168 	/* start timer */
    169 	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
    170 	    nd6_slowtimo, NULL);
    171 }
    172 
    173 struct nd_ifinfo *
    174 nd6_ifattach(struct ifnet *ifp)
    175 {
    176 	struct nd_ifinfo *nd;
    177 
    178 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO);
    179 
    180 	nd->initialized = 1;
    181 
    182 	nd->chlim = IPV6_DEFHLIM;
    183 	nd->basereachable = REACHABLE_TIME;
    184 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
    185 	nd->retrans = RETRANS_TIMER;
    186 
    187 	nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV;
    188 
    189 	/* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
    190 	 * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL
    191 	 * because one of its members should. */
    192 	if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
    193 	    (ifp->if_flags & IFF_LOOPBACK))
    194 		nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
    195 
    196 	/* A loopback interface does not need to accept RTADV.
    197 	 * A bridge interface should not accept RTADV
    198 	 * because one of its members should. */
    199 	if (ip6_accept_rtadv &&
    200 	    !(ifp->if_flags & IFF_LOOPBACK) &&
    201 	    !(ifp->if_type != IFT_BRIDGE))
    202 		nd->flags |= ND6_IFF_ACCEPT_RTADV;
    203 
    204 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
    205 	nd6_setmtu0(ifp, nd);
    206 
    207 	return nd;
    208 }
    209 
    210 void
    211 nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext)
    212 {
    213 
    214 	nd6_purge(ifp, ext);
    215 	free(ext->nd_ifinfo, M_IP6NDP);
    216 }
    217 
    218 void
    219 nd6_setmtu(struct ifnet *ifp)
    220 {
    221 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
    222 }
    223 
    224 void
    225 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
    226 {
    227 	u_int32_t omaxmtu;
    228 
    229 	omaxmtu = ndi->maxmtu;
    230 
    231 	switch (ifp->if_type) {
    232 	case IFT_ARCNET:
    233 		ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
    234 		break;
    235 	case IFT_FDDI:
    236 		ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
    237 		break;
    238 	default:
    239 		ndi->maxmtu = ifp->if_mtu;
    240 		break;
    241 	}
    242 
    243 	/*
    244 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
    245 	 * undesirable situation.  We thus notify the operator of the change
    246 	 * explicitly.  The check for omaxmtu is necessary to restrict the
    247 	 * log to the case of changing the MTU, not initializing it.
    248 	 */
    249 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
    250 		log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too"
    251 		    " small for IPv6 which needs %lu\n",
    252 		    if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long)
    253 		    IPV6_MMTU);
    254 	}
    255 
    256 	if (ndi->maxmtu > in6_maxmtu)
    257 		in6_setmaxmtu(); /* check all interfaces just in case */
    258 }
    259 
    260 void
    261 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
    262 {
    263 
    264 	memset(ndopts, 0, sizeof(*ndopts));
    265 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
    266 	ndopts->nd_opts_last
    267 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
    268 
    269 	if (icmp6len == 0) {
    270 		ndopts->nd_opts_done = 1;
    271 		ndopts->nd_opts_search = NULL;
    272 	}
    273 }
    274 
    275 /*
    276  * Take one ND option.
    277  */
    278 struct nd_opt_hdr *
    279 nd6_option(union nd_opts *ndopts)
    280 {
    281 	struct nd_opt_hdr *nd_opt;
    282 	int olen;
    283 
    284 	KASSERT(ndopts != NULL);
    285 	KASSERT(ndopts->nd_opts_last != NULL);
    286 
    287 	if (ndopts->nd_opts_search == NULL)
    288 		return NULL;
    289 	if (ndopts->nd_opts_done)
    290 		return NULL;
    291 
    292 	nd_opt = ndopts->nd_opts_search;
    293 
    294 	/* make sure nd_opt_len is inside the buffer */
    295 	if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) {
    296 		memset(ndopts, 0, sizeof(*ndopts));
    297 		return NULL;
    298 	}
    299 
    300 	olen = nd_opt->nd_opt_len << 3;
    301 	if (olen == 0) {
    302 		/*
    303 		 * Message validation requires that all included
    304 		 * options have a length that is greater than zero.
    305 		 */
    306 		memset(ndopts, 0, sizeof(*ndopts));
    307 		return NULL;
    308 	}
    309 
    310 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen);
    311 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
    312 		/* option overruns the end of buffer, invalid */
    313 		memset(ndopts, 0, sizeof(*ndopts));
    314 		return NULL;
    315 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
    316 		/* reached the end of options chain */
    317 		ndopts->nd_opts_done = 1;
    318 		ndopts->nd_opts_search = NULL;
    319 	}
    320 	return nd_opt;
    321 }
    322 
    323 /*
    324  * Parse multiple ND options.
    325  * This function is much easier to use, for ND routines that do not need
    326  * multiple options of the same type.
    327  */
    328 int
    329 nd6_options(union nd_opts *ndopts)
    330 {
    331 	struct nd_opt_hdr *nd_opt;
    332 	int i = 0;
    333 
    334 	KASSERT(ndopts != NULL);
    335 	KASSERT(ndopts->nd_opts_last != NULL);
    336 
    337 	if (ndopts->nd_opts_search == NULL)
    338 		return 0;
    339 
    340 	while (1) {
    341 		nd_opt = nd6_option(ndopts);
    342 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
    343 			/*
    344 			 * Message validation requires that all included
    345 			 * options have a length that is greater than zero.
    346 			 */
    347 			ICMP6_STATINC(ICMP6_STAT_ND_BADOPT);
    348 			memset(ndopts, 0, sizeof(*ndopts));
    349 			return -1;
    350 		}
    351 
    352 		if (nd_opt == NULL)
    353 			goto skip1;
    354 
    355 		switch (nd_opt->nd_opt_type) {
    356 		case ND_OPT_SOURCE_LINKADDR:
    357 		case ND_OPT_TARGET_LINKADDR:
    358 		case ND_OPT_MTU:
    359 		case ND_OPT_REDIRECTED_HEADER:
    360 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
    361 				nd6log((LOG_INFO,
    362 				    "duplicated ND6 option found (type=%d)\n",
    363 				    nd_opt->nd_opt_type));
    364 				/* XXX bark? */
    365 			} else {
    366 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
    367 					= nd_opt;
    368 			}
    369 			break;
    370 		case ND_OPT_PREFIX_INFORMATION:
    371 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
    372 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
    373 					= nd_opt;
    374 			}
    375 			ndopts->nd_opts_pi_end =
    376 				(struct nd_opt_prefix_info *)nd_opt;
    377 			break;
    378 		default:
    379 			/*
    380 			 * Unknown options must be silently ignored,
    381 			 * to accommodate future extension to the protocol.
    382 			 */
    383 			nd6log((LOG_DEBUG,
    384 			    "nd6_options: unsupported option %d - "
    385 			    "option ignored\n", nd_opt->nd_opt_type));
    386 		}
    387 
    388 skip1:
    389 		i++;
    390 		if (i > nd6_maxndopt) {
    391 			ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT);
    392 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
    393 			break;
    394 		}
    395 
    396 		if (ndopts->nd_opts_done)
    397 			break;
    398 	}
    399 
    400 	return 0;
    401 }
    402 
    403 /*
    404  * ND6 timer routine to handle ND6 entries
    405  */
    406 void
    407 nd6_llinfo_settimer(struct llinfo_nd6 *ln, long xtick)
    408 {
    409 	int s;
    410 
    411 	s = splsoftnet();
    412 
    413 	if (xtick < 0) {
    414 		ln->ln_expire = 0;
    415 		ln->ln_ntick = 0;
    416 		callout_stop(&ln->ln_timer_ch);
    417 	} else {
    418 		ln->ln_expire = time_uptime + xtick / hz;
    419 		if (xtick > INT_MAX) {
    420 			ln->ln_ntick = xtick - INT_MAX;
    421 			callout_reset(&ln->ln_timer_ch, INT_MAX,
    422 			    nd6_llinfo_timer, ln);
    423 		} else {
    424 			ln->ln_ntick = 0;
    425 			callout_reset(&ln->ln_timer_ch, xtick,
    426 			    nd6_llinfo_timer, ln);
    427 		}
    428 	}
    429 
    430 	splx(s);
    431 }
    432 
    433 static void
    434 nd6_llinfo_timer(void *arg)
    435 {
    436 	struct llinfo_nd6 *ln;
    437 	struct rtentry *rt;
    438 	const struct sockaddr_in6 *dst;
    439 	struct ifnet *ifp;
    440 	struct nd_ifinfo *ndi = NULL;
    441 
    442 	mutex_enter(softnet_lock);
    443 	KERNEL_LOCK(1, NULL);
    444 
    445 	ln = (struct llinfo_nd6 *)arg;
    446 
    447 	if (ln->ln_ntick > 0) {
    448 		nd6_llinfo_settimer(ln, ln->ln_ntick);
    449 		KERNEL_UNLOCK_ONE(NULL);
    450 		mutex_exit(softnet_lock);
    451 		return;
    452 	}
    453 
    454 	if ((rt = ln->ln_rt) == NULL)
    455 		panic("ln->ln_rt == NULL");
    456 	if ((ifp = rt->rt_ifp) == NULL)
    457 		panic("ln->ln_rt->rt_ifp == NULL");
    458 	ndi = ND_IFINFO(ifp);
    459 	dst = satocsin6(rt_getkey(rt));
    460 
    461 	/* sanity check */
    462 	if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
    463 		panic("rt_llinfo(%p) is not equal to ln(%p)",
    464 		      rt->rt_llinfo, ln);
    465 	if (!dst)
    466 		panic("dst=0 in nd6_timer(ln=%p)", ln);
    467 
    468 	switch (ln->ln_state) {
    469 	case ND6_LLINFO_INCOMPLETE:
    470 		if (ln->ln_asked < nd6_mmaxtries) {
    471 			ln->ln_asked++;
    472 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
    473 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
    474 		} else {
    475 			struct mbuf *m = ln->ln_hold;
    476 			if (m) {
    477 				struct mbuf *m0;
    478 
    479 				/*
    480 				 * assuming every packet in ln_hold has
    481 				 * the same IP header
    482 				 */
    483 				m0 = m->m_nextpkt;
    484 				m->m_nextpkt = NULL;
    485 				icmp6_error2(m, ICMP6_DST_UNREACH,
    486 				    ICMP6_DST_UNREACH_ADDR, 0, rt->rt_ifp);
    487 
    488 				ln->ln_hold = m0;
    489 				clear_llinfo_pqueue(ln);
    490  			}
    491 			(void)nd6_free(rt, 0);
    492 			ln = NULL;
    493 		}
    494 		break;
    495 	case ND6_LLINFO_REACHABLE:
    496 		if (!ND6_LLINFO_PERMANENT(ln)) {
    497 			ln->ln_state = ND6_LLINFO_STALE;
    498 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
    499 		}
    500 		break;
    501 
    502 	case ND6_LLINFO_PURGE:
    503 	case ND6_LLINFO_STALE:
    504 		/* Garbage Collection(RFC 2461 5.3) */
    505 		if (!ND6_LLINFO_PERMANENT(ln)) {
    506 			(void)nd6_free(rt, 1);
    507 			ln = NULL;
    508 		}
    509 		break;
    510 
    511 	case ND6_LLINFO_DELAY:
    512 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
    513 			/* We need NUD */
    514 			ln->ln_asked = 1;
    515 			ln->ln_state = ND6_LLINFO_PROBE;
    516 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
    517 			nd6_ns_output(ifp, &dst->sin6_addr,
    518 			    &dst->sin6_addr, ln, 0);
    519 		} else {
    520 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
    521 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
    522 		}
    523 		break;
    524 	case ND6_LLINFO_PROBE:
    525 		if (ln->ln_asked < nd6_umaxtries) {
    526 			ln->ln_asked++;
    527 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
    528 			nd6_ns_output(ifp, &dst->sin6_addr,
    529 			    &dst->sin6_addr, ln, 0);
    530 		} else {
    531 			(void)nd6_free(rt, 0);
    532 			ln = NULL;
    533 		}
    534 		break;
    535 	}
    536 
    537 	KERNEL_UNLOCK_ONE(NULL);
    538 	mutex_exit(softnet_lock);
    539 }
    540 
    541 /*
    542  * ND6 timer routine to expire default route list and prefix list
    543  */
    544 void
    545 nd6_timer(void *ignored_arg)
    546 {
    547 	struct nd_defrouter *next_dr, *dr;
    548 	struct nd_prefix *next_pr, *pr;
    549 	struct in6_ifaddr *ia6, *nia6;
    550 
    551 	callout_reset(&nd6_timer_ch, nd6_prune * hz,
    552 	    nd6_timer, NULL);
    553 
    554 	mutex_enter(softnet_lock);
    555 	KERNEL_LOCK(1, NULL);
    556 
    557 	/* expire default router list */
    558 
    559 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, next_dr) {
    560 		if (dr->expire && dr->expire < time_uptime) {
    561 			defrtrlist_del(dr, NULL);
    562 		}
    563 	}
    564 
    565 	/*
    566 	 * expire interface addresses.
    567 	 * in the past the loop was inside prefix expiry processing.
    568 	 * However, from a stricter speci-confrmance standpoint, we should
    569 	 * rather separate address lifetimes and prefix lifetimes.
    570 	 */
    571   addrloop:
    572 	for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
    573 		nia6 = ia6->ia_next;
    574 		/* check address lifetime */
    575 		if (IFA6_IS_INVALID(ia6)) {
    576 			int regen = 0;
    577 
    578 			/*
    579 			 * If the expiring address is temporary, try
    580 			 * regenerating a new one.  This would be useful when
    581 			 * we suspended a laptop PC, then turned it on after a
    582 			 * period that could invalidate all temporary
    583 			 * addresses.  Although we may have to restart the
    584 			 * loop (see below), it must be after purging the
    585 			 * address.  Otherwise, we'd see an infinite loop of
    586 			 * regeneration.
    587 			 */
    588 			if (ip6_use_tempaddr &&
    589 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
    590 				if (regen_tmpaddr(ia6) == 0)
    591 					regen = 1;
    592 			}
    593 
    594  			in6_purgeaddr(&ia6->ia_ifa);
    595 
    596 			if (regen)
    597 				goto addrloop; /* XXX: see below */
    598 		} else if (IFA6_IS_DEPRECATED(ia6)) {
    599 			int oldflags = ia6->ia6_flags;
    600 
    601 			if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
    602 				ia6->ia6_flags |= IN6_IFF_DEPRECATED;
    603 				rt_newaddrmsg(RTM_NEWADDR,
    604 				    (struct ifaddr *)ia6, 0, NULL);
    605 			}
    606 
    607 			/*
    608 			 * If a temporary address has just become deprecated,
    609 			 * regenerate a new one if possible.
    610 			 */
    611 			if (ip6_use_tempaddr &&
    612 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
    613 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
    614 
    615 				if (regen_tmpaddr(ia6) == 0) {
    616 					/*
    617 					 * A new temporary address is
    618 					 * generated.
    619 					 * XXX: this means the address chain
    620 					 * has changed while we are still in
    621 					 * the loop.  Although the change
    622 					 * would not cause disaster (because
    623 					 * it's not a deletion, but an
    624 					 * addition,) we'd rather restart the
    625 					 * loop just for safety.  Or does this
    626 					 * significantly reduce performance??
    627 					 */
    628 					goto addrloop;
    629 				}
    630 			}
    631 		} else {
    632 			/*
    633 			 * A new RA might have made a deprecated address
    634 			 * preferred.
    635 			 */
    636 			if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
    637 				ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
    638 				rt_newaddrmsg(RTM_NEWADDR,
    639 				    (struct ifaddr *)ia6, 0, NULL);
    640 			}
    641 		}
    642 	}
    643 
    644 	/* expire prefix list */
    645 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, next_pr) {
    646 		/*
    647 		 * check prefix lifetime.
    648 		 * since pltime is just for autoconf, pltime processing for
    649 		 * prefix is not necessary.
    650 		 */
    651 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
    652 		    time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) {
    653 
    654 			/*
    655 			 * address expiration and prefix expiration are
    656 			 * separate.  NEVER perform in6_purgeaddr here.
    657 			 */
    658 
    659 			prelist_remove(pr);
    660 		}
    661 	}
    662 
    663 	KERNEL_UNLOCK_ONE(NULL);
    664 	mutex_exit(softnet_lock);
    665 }
    666 
    667 /* ia6: deprecated/invalidated temporary address */
    668 static int
    669 regen_tmpaddr(struct in6_ifaddr *ia6)
    670 {
    671 	struct ifaddr *ifa;
    672 	struct ifnet *ifp;
    673 	struct in6_ifaddr *public_ifa6 = NULL;
    674 
    675 	ifp = ia6->ia_ifa.ifa_ifp;
    676 	IFADDR_FOREACH(ifa, ifp) {
    677 		struct in6_ifaddr *it6;
    678 
    679 		if (ifa->ifa_addr->sa_family != AF_INET6)
    680 			continue;
    681 
    682 		it6 = (struct in6_ifaddr *)ifa;
    683 
    684 		/* ignore no autoconf addresses. */
    685 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
    686 			continue;
    687 
    688 		/* ignore autoconf addresses with different prefixes. */
    689 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
    690 			continue;
    691 
    692 		/*
    693 		 * Now we are looking at an autoconf address with the same
    694 		 * prefix as ours.  If the address is temporary and is still
    695 		 * preferred, do not create another one.  It would be rare, but
    696 		 * could happen, for example, when we resume a laptop PC after
    697 		 * a long period.
    698 		 */
    699 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
    700 		    !IFA6_IS_DEPRECATED(it6)) {
    701 			public_ifa6 = NULL;
    702 			break;
    703 		}
    704 
    705 		/*
    706 		 * This is a public autoconf address that has the same prefix
    707 		 * as ours.  If it is preferred, keep it.  We can't break the
    708 		 * loop here, because there may be a still-preferred temporary
    709 		 * address with the prefix.
    710 		 */
    711 		if (!IFA6_IS_DEPRECATED(it6))
    712 		    public_ifa6 = it6;
    713 	}
    714 
    715 	if (public_ifa6 != NULL) {
    716 		int e;
    717 
    718 		/*
    719 		 * Random factor is introduced in the preferred lifetime, so
    720 		 * we do not need additional delay (3rd arg to in6_tmpifadd).
    721 		 */
    722 		if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
    723 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
    724 			    " tmp addr, errno=%d\n", e);
    725 			return -1;
    726 		}
    727 		return 0;
    728 	}
    729 
    730 	return -1;
    731 }
    732 
    733 bool
    734 nd6_accepts_rtadv(const struct nd_ifinfo *ndi)
    735 {
    736 	switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) {
    737 	case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV:
    738 		return true;
    739 	case ND6_IFF_ACCEPT_RTADV:
    740 		return ip6_accept_rtadv != 0;
    741 	case ND6_IFF_OVERRIDE_RTADV:
    742 	case 0:
    743 	default:
    744 		return false;
    745 	}
    746 }
    747 
    748 /*
    749  * Nuke neighbor cache/prefix/default router management table, right before
    750  * ifp goes away.
    751  */
    752 void
    753 nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext)
    754 {
    755 	struct llinfo_nd6 *ln, *nln;
    756 	struct nd_defrouter *dr, *ndr;
    757 	struct nd_prefix *pr, *npr;
    758 
    759 	/*
    760 	 * During detach, the ND info might be already removed, but
    761 	 * then is explitly passed as argument.
    762 	 * Otherwise get it from ifp->if_afdata.
    763 	 */
    764 	if (ext == NULL)
    765 		ext = ifp->if_afdata[AF_INET6];
    766 	if (ext == NULL)
    767 		return;
    768 
    769 	/*
    770 	 * Nuke default router list entries toward ifp.
    771 	 * We defer removal of default router list entries that is installed
    772 	 * in the routing table, in order to keep additional side effects as
    773 	 * small as possible.
    774 	 */
    775 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
    776 		if (dr->installed)
    777 			continue;
    778 
    779 		if (dr->ifp == ifp) {
    780 			KASSERT(ext != NULL);
    781 			defrtrlist_del(dr, ext);
    782 		}
    783 	}
    784 
    785 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
    786 		if (!dr->installed)
    787 			continue;
    788 
    789 		if (dr->ifp == ifp) {
    790 			KASSERT(ext != NULL);
    791 			defrtrlist_del(dr, ext);
    792 		}
    793 	}
    794 
    795 	/* Nuke prefix list entries toward ifp */
    796 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
    797 		if (pr->ndpr_ifp == ifp) {
    798 			/*
    799 			 * Because if_detach() does *not* release prefixes
    800 			 * while purging addresses the reference count will
    801 			 * still be above zero. We therefore reset it to
    802 			 * make sure that the prefix really gets purged.
    803 			 */
    804 			pr->ndpr_refcnt = 0;
    805 			/*
    806 			 * Previously, pr->ndpr_addr is removed as well,
    807 			 * but I strongly believe we don't have to do it.
    808 			 * nd6_purge() is only called from in6_ifdetach(),
    809 			 * which removes all the associated interface addresses
    810 			 * by itself.
    811 			 * (jinmei (at) kame.net 20010129)
    812 			 */
    813 			prelist_remove(pr);
    814 		}
    815 	}
    816 
    817 	/* cancel default outgoing interface setting */
    818 	if (nd6_defifindex == ifp->if_index)
    819 		nd6_setdefaultiface(0);
    820 
    821 	/* XXX: too restrictive? */
    822 	if (!ip6_forwarding && ifp->if_afdata[AF_INET6]) {
    823 		struct nd_ifinfo *ndi = ND_IFINFO(ifp);
    824 		if (ndi && nd6_accepts_rtadv(ndi)) {
    825 			/* refresh default router list */
    826 			defrouter_select();
    827 		}
    828 	}
    829 
    830 	/*
    831 	 * Nuke neighbor cache entries for the ifp.
    832 	 * Note that rt->rt_ifp may not be the same as ifp,
    833 	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
    834 	 * nd6_rtrequest(), and ip6_input().
    835 	 */
    836 	ln = llinfo_nd6.ln_next;
    837 	while (ln != NULL && ln != &llinfo_nd6) {
    838 		struct rtentry *rt;
    839 		const struct sockaddr_dl *sdl;
    840 
    841 		nln = ln->ln_next;
    842 		rt = ln->ln_rt;
    843 		if (rt && rt->rt_gateway &&
    844 		    rt->rt_gateway->sa_family == AF_LINK) {
    845 			sdl = satocsdl(rt->rt_gateway);
    846 			if (sdl->sdl_index == ifp->if_index)
    847 				nln = nd6_free(rt, 0);
    848 		}
    849 		ln = nln;
    850 	}
    851 }
    852 
    853 static struct rtentry *
    854 nd6_lookup1(const struct in6_addr *addr6, int create, struct ifnet *ifp,
    855     int cloning)
    856 {
    857 	struct rtentry *rt;
    858 	struct sockaddr_in6 sin6;
    859 
    860 	sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
    861 	rt = rtalloc1((struct sockaddr *)&sin6, create);
    862 	if (rt != NULL && (rt->rt_flags & RTF_LLINFO) == 0) {
    863 		/*
    864 		 * This is the case for the default route.
    865 		 * If we want to create a neighbor cache for the address, we
    866 		 * should free the route for the destination and allocate an
    867 		 * interface route.
    868 		 */
    869 		if (create) {
    870 			rtfree(rt);
    871 			rt = NULL;
    872 		}
    873 	}
    874 	if (rt != NULL)
    875 		;
    876 	else if (create && ifp) {
    877 		int e;
    878 
    879 		/*
    880 		 * If no route is available and create is set,
    881 		 * we allocate a host route for the destination
    882 		 * and treat it like an interface route.
    883 		 * This hack is necessary for a neighbor which can't
    884 		 * be covered by our own prefix.
    885 		 */
    886 		struct ifaddr *ifa =
    887 		    ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
    888 		if (ifa == NULL)
    889 			return NULL;
    890 
    891 		/*
    892 		 * Create a new route.  RTF_LLINFO is necessary
    893 		 * to create a Neighbor Cache entry for the
    894 		 * destination in nd6_rtrequest which will be
    895 		 * called in rtrequest via ifa->ifa_rtrequest.
    896 		 */
    897 		if ((e = rtrequest(RTM_ADD, (const struct sockaddr *)&sin6,
    898 		    ifa->ifa_addr, (const struct sockaddr *)&all1_sa,
    899 		    (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) &
    900 		    ~RTF_CLONING, &rt)) != 0) {
    901 #if 0
    902 			log(LOG_ERR,
    903 			    "nd6_lookup: failed to add route for a "
    904 			    "neighbor(%s), errno=%d\n",
    905 			    ip6_sprintf(addr6), e);
    906 #endif
    907 			return NULL;
    908 		}
    909 		if (rt == NULL)
    910 			return NULL;
    911 		if (rt->rt_llinfo) {
    912 			struct llinfo_nd6 *ln =
    913 			    (struct llinfo_nd6 *)rt->rt_llinfo;
    914 			ln->ln_state = ND6_LLINFO_NOSTATE;
    915 		}
    916 	} else
    917 		return NULL;
    918 
    919 	/*
    920 	 * Check for a cloning route to match the address.
    921 	 * This should only be set from in6_is_addr_neighbor so we avoid
    922 	 * a potentially expensive second call to rtalloc1.
    923 	 */
    924 	if (cloning &&
    925 	    rt->rt_flags & (RTF_CLONING | RTF_CLONED) &&
    926 	    (rt->rt_ifp == ifp
    927 #if NBRIDGE > 0
    928 	    || rt->rt_ifp->if_bridge == ifp->if_bridge
    929 #endif
    930 #if NCARP > 0
    931 	    || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) ||
    932 	    (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)||
    933 	    (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP &&
    934 	    rt->rt_ifp->if_carpdev == ifp->if_carpdev)
    935 #endif
    936 	    ))
    937 		return rt;
    938 
    939 	/*
    940 	 * Validation for the entry.
    941 	 * Note that the check for rt_llinfo is necessary because a cloned
    942 	 * route from a parent route that has the L flag (e.g. the default
    943 	 * route to a p2p interface) may have the flag, too, while the
    944 	 * destination is not actually a neighbor.
    945 	 * XXX: we can't use rt->rt_ifp to check for the interface, since
    946 	 *      it might be the loopback interface if the entry is for our
    947 	 *      own address on a non-loopback interface. Instead, we should
    948 	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL
    949 	 *	interface.
    950 	 * Note also that ifa_ifp and ifp may differ when we connect two
    951 	 * interfaces to a same link, install a link prefix to an interface,
    952 	 * and try to install a neighbor cache on an interface that does not
    953 	 * have a route to the prefix.
    954 	 */
    955 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
    956 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
    957 	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
    958 		if (create) {
    959 			nd6log((LOG_DEBUG,
    960 			    "nd6_lookup: failed to lookup %s (if = %s)\n",
    961 			    ip6_sprintf(addr6),
    962 			    ifp ? if_name(ifp) : "unspec"));
    963 		}
    964 		rtfree(rt);
    965 		return NULL;
    966 	}
    967 	return rt;
    968 }
    969 
    970 struct rtentry *
    971 nd6_lookup(const struct in6_addr *addr6, int create, struct ifnet *ifp)
    972 {
    973 
    974 	return nd6_lookup1(addr6, create, ifp, 0);
    975 }
    976 
    977 /*
    978  * Detect if a given IPv6 address identifies a neighbor on a given link.
    979  * XXX: should take care of the destination of a p2p link?
    980  */
    981 int
    982 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
    983 {
    984 	struct nd_prefix *pr;
    985 	struct rtentry *rt;
    986 
    987 	/*
    988 	 * A link-local address is always a neighbor.
    989 	 * XXX: a link does not necessarily specify a single interface.
    990 	 */
    991 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
    992 		struct sockaddr_in6 sin6_copy;
    993 		u_int32_t zone;
    994 
    995 		/*
    996 		 * We need sin6_copy since sa6_recoverscope() may modify the
    997 		 * content (XXX).
    998 		 */
    999 		sin6_copy = *addr;
   1000 		if (sa6_recoverscope(&sin6_copy))
   1001 			return 0; /* XXX: should be impossible */
   1002 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
   1003 			return 0;
   1004 		if (sin6_copy.sin6_scope_id == zone)
   1005 			return 1;
   1006 		else
   1007 			return 0;
   1008 	}
   1009 
   1010 	/*
   1011 	 * If the address matches one of our on-link prefixes, it should be a
   1012 	 * neighbor.
   1013 	 */
   1014 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1015 		if (pr->ndpr_ifp != ifp)
   1016 			continue;
   1017 
   1018 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
   1019 			continue;
   1020 
   1021 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
   1022 		    &addr->sin6_addr, &pr->ndpr_mask))
   1023 			return 1;
   1024 	}
   1025 
   1026 	/*
   1027 	 * If the default router list is empty, all addresses are regarded
   1028 	 * as on-link, and thus, as a neighbor.
   1029 	 * XXX: we restrict the condition to hosts, because routers usually do
   1030 	 * not have the "default router list".
   1031 	 */
   1032 	if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
   1033 	    nd6_defifindex == ifp->if_index) {
   1034 		return 1;
   1035 	}
   1036 
   1037 	/*
   1038 	 * Even if the address matches none of our addresses, it might match
   1039 	 * a cloning route or be in the neighbor cache.
   1040 	 */
   1041 	rt = nd6_lookup1(&addr->sin6_addr, 0, ifp, 1);
   1042 	if (rt != NULL) {
   1043 		rtfree(rt);
   1044 		return 1;
   1045 	}
   1046 
   1047 	return 0;
   1048 }
   1049 
   1050 /*
   1051  * Free an nd6 llinfo entry.
   1052  * Since the function would cause significant changes in the kernel, DO NOT
   1053  * make it global, unless you have a strong reason for the change, and are sure
   1054  * that the change is safe.
   1055  */
   1056 static struct llinfo_nd6 *
   1057 nd6_free(struct rtentry *rt, int gc)
   1058 {
   1059 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
   1060 	struct in6_addr in6 = satocsin6(rt_getkey(rt))->sin6_addr;
   1061 	struct nd_defrouter *dr;
   1062 	int error;
   1063 
   1064 	/*
   1065 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
   1066 	 * even though it is not harmful, it was not really necessary.
   1067 	 */
   1068 
   1069 	/* cancel timer */
   1070 	nd6_llinfo_settimer(ln, -1);
   1071 
   1072 	if (!ip6_forwarding) {
   1073 		int s;
   1074 		s = splsoftnet();
   1075 		dr = defrouter_lookup(&satocsin6(rt_getkey(rt))->sin6_addr,
   1076 		    rt->rt_ifp);
   1077 
   1078 		if (dr != NULL && dr->expire &&
   1079 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
   1080 			/*
   1081 			 * If the reason for the deletion is just garbage
   1082 			 * collection, and the neighbor is an active default
   1083 			 * router, do not delete it.  Instead, reset the GC
   1084 			 * timer using the router's lifetime.
   1085 			 * Simply deleting the entry would affect default
   1086 			 * router selection, which is not necessarily a good
   1087 			 * thing, especially when we're using router preference
   1088 			 * values.
   1089 			 * XXX: the check for ln_state would be redundant,
   1090 			 *      but we intentionally keep it just in case.
   1091 			 */
   1092 			if (dr->expire > time_uptime)
   1093 				nd6_llinfo_settimer(ln,
   1094 				    (dr->expire - time_uptime) * hz);
   1095 			else
   1096 				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
   1097 			splx(s);
   1098 			return ln->ln_next;
   1099 		}
   1100 
   1101 		if (ln->ln_router || dr) {
   1102 			/*
   1103 			 * rt6_flush must be called whether or not the neighbor
   1104 			 * is in the Default Router List.
   1105 			 * See a corresponding comment in nd6_na_input().
   1106 			 */
   1107 			rt6_flush(&in6, rt->rt_ifp);
   1108 		}
   1109 
   1110 		if (dr) {
   1111 			/*
   1112 			 * Unreachablity of a router might affect the default
   1113 			 * router selection and on-link detection of advertised
   1114 			 * prefixes.
   1115 			 */
   1116 
   1117 			/*
   1118 			 * Temporarily fake the state to choose a new default
   1119 			 * router and to perform on-link determination of
   1120 			 * prefixes correctly.
   1121 			 * Below the state will be set correctly,
   1122 			 * or the entry itself will be deleted.
   1123 			 */
   1124 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
   1125 
   1126 			/*
   1127 			 * Since defrouter_select() does not affect the
   1128 			 * on-link determination and MIP6 needs the check
   1129 			 * before the default router selection, we perform
   1130 			 * the check now.
   1131 			 */
   1132 			pfxlist_onlink_check();
   1133 
   1134 			/*
   1135 			 * refresh default router list
   1136 			 */
   1137 			defrouter_select();
   1138 		}
   1139 		splx(s);
   1140 	}
   1141 
   1142 	/*
   1143 	 * Before deleting the entry, remember the next entry as the
   1144 	 * return value.  We need this because pfxlist_onlink_check() above
   1145 	 * might have freed other entries (particularly the old next entry) as
   1146 	 * a side effect (XXX).
   1147 	 */
   1148 	next = ln->ln_next;
   1149 
   1150 	/*
   1151 	 * Detach the route from the routing tree and the list of neighbor
   1152 	 * caches, and disable the route entry not to be used in already
   1153 	 * cached routes.
   1154 	 */
   1155 	error = rtrequest_newmsg(RTM_DELETE, rt_getkey(rt), NULL,
   1156 	    rt_mask(rt), 0);
   1157 	if (error != 0) {
   1158 		/* XXX need error message? */;
   1159 	}
   1160 
   1161 	return next;
   1162 }
   1163 
   1164 /*
   1165  * Upper-layer reachability hint for Neighbor Unreachability Detection.
   1166  *
   1167  * XXX cost-effective methods?
   1168  */
   1169 void
   1170 nd6_nud_hint(struct rtentry *rt0)
   1171 {
   1172 	struct llinfo_nd6 *ln;
   1173 	struct rtentry *rt = rt0;
   1174 
   1175 	if (rt == NULL)
   1176 		return;
   1177 
   1178 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
   1179 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
   1180 	    !rt->rt_llinfo || !rt->rt_gateway ||
   1181 	    rt->rt_gateway->sa_family != AF_LINK) {
   1182 		/* This is not a host route. */
   1183 		goto exit;
   1184 	}
   1185 
   1186 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1187 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
   1188 		goto exit;
   1189 
   1190 	/*
   1191 	 * if we get upper-layer reachability confirmation many times,
   1192 	 * it is possible we have false information.
   1193 	 */
   1194 	ln->ln_byhint++;
   1195 	if (ln->ln_byhint > nd6_maxnudhint)
   1196 		goto exit;
   1197 
   1198 	ln->ln_state = ND6_LLINFO_REACHABLE;
   1199 	if (!ND6_LLINFO_PERMANENT(ln)) {
   1200 		nd6_llinfo_settimer(ln,
   1201 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
   1202 	}
   1203 exit:
   1204 	if (rt != rt0)
   1205 		rtfree(rt);
   1206 	return;
   1207 }
   1208 
   1209 void
   1210 nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
   1211 {
   1212 	struct sockaddr *gate = rt->rt_gateway;
   1213 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1214 	struct ifnet *ifp = rt->rt_ifp;
   1215 	uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
   1216 	struct ifaddr *ifa;
   1217 
   1218 	RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1219 
   1220 	if (req == RTM_LLINFO_UPD) {
   1221 		int rc;
   1222 		struct in6_addr *in6;
   1223 		struct in6_addr in6_all;
   1224 		int anycast;
   1225 
   1226 		if ((ifa = info->rti_ifa) == NULL)
   1227 			return;
   1228 
   1229 		in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
   1230 		anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
   1231 
   1232 		in6_all = in6addr_linklocal_allnodes;
   1233 		if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
   1234 			log(LOG_ERR, "%s: failed to set scope %s "
   1235 			    "(errno=%d)\n", __func__, if_name(ifp), rc);
   1236 			return;
   1237 		}
   1238 
   1239 		/* XXX don't set Override for proxy addresses */
   1240 		nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
   1241 		    (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
   1242 #if 0
   1243 		    | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
   1244 #endif
   1245 		    , 1, NULL);
   1246 		return;
   1247 	}
   1248 
   1249 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
   1250 		return;
   1251 
   1252 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
   1253 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1254 		/*
   1255 		 * This is probably an interface direct route for a link
   1256 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
   1257 		 * We do not need special treatment below for such a route.
   1258 		 * Moreover, the RTF_LLINFO flag which would be set below
   1259 		 * would annoy the ndp(8) command.
   1260 		 */
   1261 		return;
   1262 	}
   1263 
   1264 	if (req == RTM_RESOLVE &&
   1265 	    (nd6_need_cache(ifp) == 0 || /* stf case */
   1266 	     !nd6_is_addr_neighbor(satocsin6(rt_getkey(rt)), ifp))) {
   1267 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1268 		/*
   1269 		 * FreeBSD and BSD/OS often make a cloned host route based
   1270 		 * on a less-specific route (e.g. the default route).
   1271 		 * If the less specific route does not have a "gateway"
   1272 		 * (this is the case when the route just goes to a p2p or an
   1273 		 * stf interface), we'll mistakenly make a neighbor cache for
   1274 		 * the host route, and will see strange neighbor solicitation
   1275 		 * for the corresponding destination.  In order to avoid the
   1276 		 * confusion, we check if the destination of the route is
   1277 		 * a neighbor in terms of neighbor discovery, and stop the
   1278 		 * process if not.  Additionally, we remove the LLINFO flag
   1279 		 * so that ndp(8) will not try to get the neighbor information
   1280 		 * of the destination.
   1281 		 */
   1282 		rt->rt_flags &= ~RTF_LLINFO;
   1283 		return;
   1284 	}
   1285 
   1286 	switch (req) {
   1287 	case RTM_ADD:
   1288 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1289 		/*
   1290 		 * There is no backward compatibility :)
   1291 		 *
   1292 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
   1293 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
   1294 		 *	   rt->rt_flags |= RTF_CLONING;
   1295 		 */
   1296 		if ((rt->rt_flags & RTF_CLONING) ||
   1297 		    ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) {
   1298 			union {
   1299 				struct sockaddr sa;
   1300 				struct sockaddr_dl sdl;
   1301 				struct sockaddr_storage ss;
   1302 			} u;
   1303 			/*
   1304 			 * Case 1: This route should come from a route to
   1305 			 * interface (RTF_CLONING case) or the route should be
   1306 			 * treated as on-link but is currently not
   1307 			 * (RTF_LLINFO && ln == NULL case).
   1308 			 */
   1309 			if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
   1310 			    ifp->if_index, ifp->if_type,
   1311 			    NULL, namelen, NULL, addrlen) == NULL) {
   1312 				printf("%s.%d: sockaddr_dl_init(, %zu, ) "
   1313 				    "failed on %s\n", __func__, __LINE__,
   1314 				    sizeof(u.ss), if_name(ifp));
   1315 			}
   1316 			rt_setgate(rt, &u.sa);
   1317 			gate = rt->rt_gateway;
   1318 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1319 			if (ln != NULL)
   1320 				nd6_llinfo_settimer(ln, 0);
   1321 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1322 			if ((rt->rt_flags & RTF_CLONING) != 0)
   1323 				break;
   1324 		}
   1325 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1326 		/*
   1327 		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
   1328 		 * We don't do that here since llinfo is not ready yet.
   1329 		 *
   1330 		 * There are also couple of other things to be discussed:
   1331 		 * - unsolicited NA code needs improvement beforehand
   1332 		 * - RFC2461 says we MAY send multicast unsolicited NA
   1333 		 *   (7.2.6 paragraph 4), however, it also says that we
   1334 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
   1335 		 *   we don't have anything like it right now.
   1336 		 *   note that the mechanism needs a mutual agreement
   1337 		 *   between proxies, which means that we need to implement
   1338 		 *   a new protocol, or a new kludge.
   1339 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
   1340 		 *   we need to check ip6forwarding before sending it.
   1341 		 *   (or should we allow proxy ND configuration only for
   1342 		 *   routers?  there's no mention about proxy ND from hosts)
   1343 		 */
   1344 #if 0
   1345 		/* XXX it does not work */
   1346 		if (rt->rt_flags & RTF_ANNOUNCE)
   1347 			nd6_na_output(ifp,
   1348 			      &satocsin6(rt_getkey(rt))->sin6_addr,
   1349 			      &satocsin6(rt_getkey(rt))->sin6_addr,
   1350 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
   1351 			      1, NULL);
   1352 #endif
   1353 		/* FALLTHROUGH */
   1354 	case RTM_RESOLVE:
   1355 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
   1356 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1357 			/*
   1358 			 * Address resolution isn't necessary for a point to
   1359 			 * point link, so we can skip this test for a p2p link.
   1360 			 */
   1361 			if (gate->sa_family != AF_LINK ||
   1362 			    gate->sa_len <
   1363 			    sockaddr_dl_measure(namelen, addrlen)) {
   1364 				log(LOG_DEBUG,
   1365 				    "nd6_rtrequest: bad gateway value: %s\n",
   1366 				    if_name(ifp));
   1367 				break;
   1368 			}
   1369 			satosdl(gate)->sdl_type = ifp->if_type;
   1370 			satosdl(gate)->sdl_index = ifp->if_index;
   1371 			RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1372 		}
   1373 		if (ln != NULL)
   1374 			break;	/* This happens on a route change */
   1375 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1376 		/*
   1377 		 * Case 2: This route may come from cloning, or a manual route
   1378 		 * add with a LL address.
   1379 		 */
   1380 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
   1381 		rt->rt_llinfo = ln;
   1382 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1383 		if (ln == NULL) {
   1384 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
   1385 			break;
   1386 		}
   1387 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1388 		nd6_inuse++;
   1389 		nd6_allocated++;
   1390 		memset(ln, 0, sizeof(*ln));
   1391 		ln->ln_rt = rt;
   1392 		callout_init(&ln->ln_timer_ch, CALLOUT_MPSAFE);
   1393 		/* this is required for "ndp" command. - shin */
   1394 		if (req == RTM_ADD) {
   1395 		        /*
   1396 			 * gate should have some valid AF_LINK entry,
   1397 			 * and ln->ln_expire should have some lifetime
   1398 			 * which is specified by ndp command.
   1399 			 */
   1400 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1401 			ln->ln_byhint = 0;
   1402 		} else {
   1403 		        /*
   1404 			 * When req == RTM_RESOLVE, rt is created and
   1405 			 * initialized in rtrequest(), so rt_expire is 0.
   1406 			 */
   1407 			ln->ln_state = ND6_LLINFO_NOSTATE;
   1408 			nd6_llinfo_settimer(ln, 0);
   1409 		}
   1410 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1411 		rt->rt_flags |= RTF_LLINFO;
   1412 		ln->ln_next = llinfo_nd6.ln_next;
   1413 		llinfo_nd6.ln_next = ln;
   1414 		ln->ln_prev = &llinfo_nd6;
   1415 		ln->ln_next->ln_prev = ln;
   1416 
   1417 		/*
   1418 		 * If we have too many cache entries, initiate immediate
   1419 		 * purging for some "less recently used" entries.  Note that
   1420 		 * we cannot directly call nd6_free() here because it would
   1421 		 * cause re-entering rtable related routines triggering an LOR
   1422 		 * problem for FreeBSD.
   1423 		 */
   1424 		if (ip6_neighborgcthresh >= 0 &&
   1425 		    nd6_inuse >= ip6_neighborgcthresh) {
   1426 			int i;
   1427 
   1428 			for (i = 0; i < 10 && llinfo_nd6.ln_prev != ln; i++) {
   1429 				struct llinfo_nd6 *ln_end = llinfo_nd6.ln_prev;
   1430 
   1431 				/* Move this entry to the head */
   1432 				LN_DEQUEUE(ln_end);
   1433 				LN_INSERTHEAD(ln_end);
   1434 
   1435 				if (ND6_LLINFO_PERMANENT(ln_end))
   1436 					continue;
   1437 
   1438 				if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
   1439 					ln_end->ln_state = ND6_LLINFO_STALE;
   1440 				else
   1441 					ln_end->ln_state = ND6_LLINFO_PURGE;
   1442 				nd6_llinfo_settimer(ln_end, 0);
   1443 			}
   1444 		}
   1445 
   1446 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1447 		/*
   1448 		 * check if rt_getkey(rt) is an address assigned
   1449 		 * to the interface.
   1450 		 */
   1451 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp,
   1452 		    &satocsin6(rt_getkey(rt))->sin6_addr);
   1453 		RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
   1454 		if (ifa != NULL) {
   1455 			const void *mac;
   1456 			nd6_llinfo_settimer(ln, -1);
   1457 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1458 			ln->ln_byhint = 0;
   1459 			if ((mac = nd6_ifptomac(ifp)) != NULL) {
   1460 				/* XXX check for error */
   1461 				if (sockaddr_dl_setaddr(satosdl(gate),
   1462 				    gate->sa_len, mac,
   1463 				    ifp->if_addrlen) == NULL) {
   1464 					printf("%s.%d: "
   1465 					    "sockaddr_dl_setaddr(, %d, ) "
   1466 					    "failed on %s\n", __func__,
   1467 					    __LINE__, gate->sa_len,
   1468 					    if_name(ifp));
   1469 				}
   1470 			}
   1471 			if (nd6_useloopback) {
   1472 				ifp = rt->rt_ifp = lo0ifp;	/* XXX */
   1473 				/*
   1474 				 * Make sure rt_ifa be equal to the ifaddr
   1475 				 * corresponding to the address.
   1476 				 * We need this because when we refer
   1477 				 * rt_ifa->ia6_flags in ip6_input, we assume
   1478 				 * that the rt_ifa points to the address instead
   1479 				 * of the loopback address.
   1480 				 */
   1481 				if (ifa != rt->rt_ifa)
   1482 					rt_replace_ifa(rt, ifa);
   1483 				rt->rt_flags &= ~RTF_CLONED;
   1484 			}
   1485 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
   1486 			nd6_llinfo_settimer(ln, -1);
   1487 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1488 			ln->ln_byhint = 0;
   1489 
   1490 			/* join solicited node multicast for proxy ND */
   1491 			if (ifp->if_flags & IFF_MULTICAST) {
   1492 				struct in6_addr llsol;
   1493 				int error;
   1494 
   1495 				llsol = satocsin6(rt_getkey(rt))->sin6_addr;
   1496 				llsol.s6_addr32[0] = htonl(0xff020000);
   1497 				llsol.s6_addr32[1] = 0;
   1498 				llsol.s6_addr32[2] = htonl(1);
   1499 				llsol.s6_addr8[12] = 0xff;
   1500 				if (in6_setscope(&llsol, ifp, NULL))
   1501 					break;
   1502 				if (!in6_addmulti(&llsol, ifp, &error, 0)) {
   1503 					nd6log((LOG_ERR, "%s: failed to join "
   1504 					    "%s (errno=%d)\n", if_name(ifp),
   1505 					    ip6_sprintf(&llsol), error));
   1506 				}
   1507 			}
   1508 		}
   1509 		break;
   1510 
   1511 	case RTM_DELETE:
   1512 		if (ln == NULL)
   1513 			break;
   1514 		/* leave from solicited node multicast for proxy ND */
   1515 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
   1516 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
   1517 			struct in6_addr llsol;
   1518 			struct in6_multi *in6m;
   1519 
   1520 			llsol = satocsin6(rt_getkey(rt))->sin6_addr;
   1521 			llsol.s6_addr32[0] = htonl(0xff020000);
   1522 			llsol.s6_addr32[1] = 0;
   1523 			llsol.s6_addr32[2] = htonl(1);
   1524 			llsol.s6_addr8[12] = 0xff;
   1525 			if (in6_setscope(&llsol, ifp, NULL) == 0) {
   1526 				IN6_LOOKUP_MULTI(llsol, ifp, in6m);
   1527 				if (in6m)
   1528 					in6_delmulti(in6m);
   1529 			}
   1530 		}
   1531 		nd6_inuse--;
   1532 		ln->ln_next->ln_prev = ln->ln_prev;
   1533 		ln->ln_prev->ln_next = ln->ln_next;
   1534 		ln->ln_prev = NULL;
   1535 		nd6_llinfo_settimer(ln, -1);
   1536 		rt->rt_llinfo = 0;
   1537 		rt->rt_flags &= ~RTF_LLINFO;
   1538 		clear_llinfo_pqueue(ln);
   1539 		Free(ln);
   1540 	}
   1541 }
   1542 
   1543 int
   1544 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
   1545 {
   1546 	struct in6_drlist *drl = (struct in6_drlist *)data;
   1547 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
   1548 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
   1549 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
   1550 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
   1551 	struct nd_defrouter *dr;
   1552 	struct nd_prefix *pr;
   1553 	struct rtentry *rt;
   1554 	int i = 0, error = 0;
   1555 	int s;
   1556 
   1557 	switch (cmd) {
   1558 	case SIOCGDRLST_IN6:
   1559 		/*
   1560 		 * obsolete API, use sysctl under net.inet6.icmp6
   1561 		 */
   1562 		memset(drl, 0, sizeof(*drl));
   1563 		s = splsoftnet();
   1564 		TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
   1565 			if (i >= DRLSTSIZ)
   1566 				break;
   1567 			drl->defrouter[i].rtaddr = dr->rtaddr;
   1568 			in6_clearscope(&drl->defrouter[i].rtaddr);
   1569 
   1570 			drl->defrouter[i].flags = dr->flags;
   1571 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
   1572 			drl->defrouter[i].expire = dr->expire ?
   1573 			    time_mono_to_wall(dr->expire) : 0;
   1574 			drl->defrouter[i].if_index = dr->ifp->if_index;
   1575 			i++;
   1576 		}
   1577 		splx(s);
   1578 		break;
   1579 	case SIOCGPRLST_IN6:
   1580 		/*
   1581 		 * obsolete API, use sysctl under net.inet6.icmp6
   1582 		 *
   1583 		 * XXX the structure in6_prlist was changed in backward-
   1584 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
   1585 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
   1586 		 */
   1587 		/*
   1588 		 * XXX meaning of fields, especialy "raflags", is very
   1589 		 * differnet between RA prefix list and RR/static prefix list.
   1590 		 * how about separating ioctls into two?
   1591 		 */
   1592 		memset(oprl, 0, sizeof(*oprl));
   1593 		s = splsoftnet();
   1594 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1595 			struct nd_pfxrouter *pfr;
   1596 			int j;
   1597 
   1598 			if (i >= PRLSTSIZ)
   1599 				break;
   1600 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
   1601 			oprl->prefix[i].raflags = pr->ndpr_raf;
   1602 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
   1603 			oprl->prefix[i].vltime = pr->ndpr_vltime;
   1604 			oprl->prefix[i].pltime = pr->ndpr_pltime;
   1605 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
   1606 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
   1607 				oprl->prefix[i].expire = 0;
   1608 			else {
   1609 				time_t maxexpire;
   1610 
   1611 				/* XXX: we assume time_t is signed. */
   1612 				maxexpire = (-1) &
   1613 				    ~((time_t)1 <<
   1614 				    ((sizeof(maxexpire) * 8) - 1));
   1615 				if (pr->ndpr_vltime <
   1616 				    maxexpire - pr->ndpr_lastupdate) {
   1617 					time_t expire;
   1618 					expire = pr->ndpr_lastupdate +
   1619 					    pr->ndpr_vltime;
   1620 					oprl->prefix[i].expire = expire ?
   1621 					    time_mono_to_wall(expire) : 0;
   1622 				} else
   1623 					oprl->prefix[i].expire = maxexpire;
   1624 			}
   1625 
   1626 			j = 0;
   1627 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
   1628 				if (j < DRLSTSIZ) {
   1629 #define RTRADDR oprl->prefix[i].advrtr[j]
   1630 					RTRADDR = pfr->router->rtaddr;
   1631 					in6_clearscope(&RTRADDR);
   1632 #undef RTRADDR
   1633 				}
   1634 				j++;
   1635 			}
   1636 			oprl->prefix[i].advrtrs = j;
   1637 			oprl->prefix[i].origin = PR_ORIG_RA;
   1638 
   1639 			i++;
   1640 		}
   1641 		splx(s);
   1642 
   1643 		break;
   1644 	case OSIOCGIFINFO_IN6:
   1645 #define ND	ndi->ndi
   1646 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
   1647 		memset(&ND, 0, sizeof(ND));
   1648 		ND.linkmtu = IN6_LINKMTU(ifp);
   1649 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
   1650 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
   1651 		ND.reachable = ND_IFINFO(ifp)->reachable;
   1652 		ND.retrans = ND_IFINFO(ifp)->retrans;
   1653 		ND.flags = ND_IFINFO(ifp)->flags;
   1654 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
   1655 		ND.chlim = ND_IFINFO(ifp)->chlim;
   1656 		break;
   1657 	case SIOCGIFINFO_IN6:
   1658 		ND = *ND_IFINFO(ifp);
   1659 		break;
   1660 	case SIOCSIFINFO_IN6:
   1661 		/*
   1662 		 * used to change host variables from userland.
   1663 		 * intented for a use on router to reflect RA configurations.
   1664 		 */
   1665 		/* 0 means 'unspecified' */
   1666 		if (ND.linkmtu != 0) {
   1667 			if (ND.linkmtu < IPV6_MMTU ||
   1668 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
   1669 				error = EINVAL;
   1670 				break;
   1671 			}
   1672 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
   1673 		}
   1674 
   1675 		if (ND.basereachable != 0) {
   1676 			int obasereachable = ND_IFINFO(ifp)->basereachable;
   1677 
   1678 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
   1679 			if (ND.basereachable != obasereachable)
   1680 				ND_IFINFO(ifp)->reachable =
   1681 				    ND_COMPUTE_RTIME(ND.basereachable);
   1682 		}
   1683 		if (ND.retrans != 0)
   1684 			ND_IFINFO(ifp)->retrans = ND.retrans;
   1685 		if (ND.chlim != 0)
   1686 			ND_IFINFO(ifp)->chlim = ND.chlim;
   1687 		/* FALLTHROUGH */
   1688 	case SIOCSIFINFO_FLAGS:
   1689 	{
   1690 		struct ifaddr *ifa;
   1691 		struct in6_ifaddr *ia;
   1692 
   1693 		if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
   1694 		    !(ND.flags & ND6_IFF_IFDISABLED))
   1695 		{
   1696 			/*
   1697 			 * If the interface is marked as ND6_IFF_IFDISABLED and
   1698 			 * has a link-local address with IN6_IFF_DUPLICATED,
   1699 			 * do not clear ND6_IFF_IFDISABLED.
   1700 			 * See RFC 4862, section 5.4.5.
   1701 			 */
   1702 			int duplicated_linklocal = 0;
   1703 
   1704 			IFADDR_FOREACH(ifa, ifp) {
   1705 				if (ifa->ifa_addr->sa_family != AF_INET6)
   1706 					continue;
   1707 				ia = (struct in6_ifaddr *)ifa;
   1708 				if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
   1709 				    IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
   1710 				{
   1711 					duplicated_linklocal = 1;
   1712 					break;
   1713 				}
   1714 			}
   1715 
   1716 			if (duplicated_linklocal) {
   1717 				ND.flags |= ND6_IFF_IFDISABLED;
   1718 				log(LOG_ERR, "Cannot enable an interface"
   1719 				    " with a link-local address marked"
   1720 				    " duplicate.\n");
   1721 			} else {
   1722 				ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED;
   1723 				if (ifp->if_flags & IFF_UP)
   1724 					in6_if_up(ifp);
   1725 			}
   1726 		} else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) &&
   1727 		    (ND.flags & ND6_IFF_IFDISABLED))
   1728 		{
   1729 			/* Mark all IPv6 addresses as tentative. */
   1730 
   1731 			ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED;
   1732 			IFADDR_FOREACH(ifa, ifp) {
   1733 				if (ifa->ifa_addr->sa_family != AF_INET6)
   1734 					continue;
   1735 				nd6_dad_stop(ifa);
   1736 				ia = (struct in6_ifaddr *)ifa;
   1737 				ia->ia6_flags |= IN6_IFF_TENTATIVE;
   1738 			}
   1739 		}
   1740 
   1741 		if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
   1742 			if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) {
   1743 				/* auto_linklocal 0->1 transition */
   1744 
   1745 				ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL;
   1746 				in6_ifattach(ifp, NULL);
   1747 			} else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
   1748 			    ifp->if_flags & IFF_UP)
   1749 			{
   1750 				/*
   1751 				 * When the IF already has
   1752 				 * ND6_IFF_AUTO_LINKLOCAL, no link-local
   1753 				 * address is assigned, and IFF_UP, try to
   1754 				 * assign one.
   1755 				 */
   1756 				 int haslinklocal = 0;
   1757 
   1758 				 IFADDR_FOREACH(ifa, ifp) {
   1759 					if (ifa->ifa_addr->sa_family !=AF_INET6)
   1760 						continue;
   1761 					ia = (struct in6_ifaddr *)ifa;
   1762 					if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){
   1763 						haslinklocal = 1;
   1764 						break;
   1765 					}
   1766 				 }
   1767 				 if (!haslinklocal)
   1768 					in6_ifattach(ifp, NULL);
   1769 			}
   1770 		}
   1771 	}
   1772 		ND_IFINFO(ifp)->flags = ND.flags;
   1773 		break;
   1774 #undef ND
   1775 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
   1776 		/* sync kernel routing table with the default router list */
   1777 		defrouter_reset();
   1778 		defrouter_select();
   1779 		break;
   1780 	case SIOCSPFXFLUSH_IN6:
   1781 	{
   1782 		/* flush all the prefix advertised by routers */
   1783 		struct nd_prefix *pfx, *next;
   1784 
   1785 		s = splsoftnet();
   1786 		LIST_FOREACH_SAFE(pfx, &nd_prefix, ndpr_entry, next) {
   1787 			struct in6_ifaddr *ia, *ia_next;
   1788 
   1789 			if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr))
   1790 				continue; /* XXX */
   1791 
   1792 			/* do we really have to remove addresses as well? */
   1793 			for (ia = in6_ifaddr; ia; ia = ia_next) {
   1794 				/* ia might be removed.  keep the next ptr. */
   1795 				ia_next = ia->ia_next;
   1796 
   1797 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
   1798 					continue;
   1799 
   1800 				if (ia->ia6_ndpr == pfx)
   1801 					in6_purgeaddr(&ia->ia_ifa);
   1802 			}
   1803 			prelist_remove(pfx);
   1804 		}
   1805 		splx(s);
   1806 		break;
   1807 	}
   1808 	case SIOCSRTRFLUSH_IN6:
   1809 	{
   1810 		/* flush all the default routers */
   1811 		struct nd_defrouter *drtr, *next;
   1812 
   1813 		s = splsoftnet();
   1814 		defrouter_reset();
   1815 		TAILQ_FOREACH_SAFE(drtr, &nd_defrouter, dr_entry, next) {
   1816 			defrtrlist_del(drtr, NULL);
   1817 		}
   1818 		defrouter_select();
   1819 		splx(s);
   1820 		break;
   1821 	}
   1822 	case SIOCGNBRINFO_IN6:
   1823 	{
   1824 		struct llinfo_nd6 *ln;
   1825 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
   1826 
   1827 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
   1828 			return error;
   1829 
   1830 		s = splsoftnet();
   1831 		rt = nd6_lookup(&nb_addr, 0, ifp);
   1832 		if (rt == NULL) {
   1833 			error = EINVAL;
   1834 			splx(s);
   1835 			break;
   1836 		}
   1837 
   1838 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1839 		rtfree(rt);
   1840 		if (ln == NULL) {
   1841 			error = EINVAL;
   1842 			splx(s);
   1843 			break;
   1844 		}
   1845 		nbi->state = ln->ln_state;
   1846 		nbi->asked = ln->ln_asked;
   1847 		nbi->isrouter = ln->ln_router;
   1848 		nbi->expire = ln->ln_expire ?
   1849 		    time_mono_to_wall(ln->ln_expire) : 0;
   1850 		splx(s);
   1851 
   1852 		break;
   1853 	}
   1854 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
   1855 		ndif->ifindex = nd6_defifindex;
   1856 		break;
   1857 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
   1858 		return nd6_setdefaultiface(ndif->ifindex);
   1859 	}
   1860 	return error;
   1861 }
   1862 
   1863 void
   1864 nd6_llinfo_release_pkts(struct llinfo_nd6 *ln, struct ifnet *ifp,
   1865     struct rtentry *rt)
   1866 {
   1867 	struct mbuf *m_hold, *m_hold_next;
   1868 
   1869 	for (m_hold = ln->ln_hold, ln->ln_hold = NULL;
   1870 	     m_hold != NULL;
   1871 	     m_hold = m_hold_next) {
   1872 		m_hold_next = m_hold->m_nextpkt;
   1873 		m_hold->m_nextpkt = NULL;
   1874 
   1875 		/*
   1876 		 * we assume ifp is not a p2p here, so
   1877 		 * just set the 2nd argument as the
   1878 		 * 1st one.
   1879 		 */
   1880 		nd6_output(ifp, ifp, m_hold, satocsin6(rt_getkey(rt)), rt);
   1881 	}
   1882 }
   1883 
   1884 /*
   1885  * Create neighbor cache entry and cache link-layer address,
   1886  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
   1887  */
   1888 void
   1889 nd6_cache_lladdr(
   1890     struct ifnet *ifp,
   1891     struct in6_addr *from,
   1892     char *lladdr,
   1893     int lladdrlen,
   1894     int type,	/* ICMP6 type */
   1895     int code	/* type dependent information */
   1896 )
   1897 {
   1898 	struct nd_ifinfo *ndi = ND_IFINFO(ifp);
   1899 	struct rtentry *rt = NULL;
   1900 	struct llinfo_nd6 *ln = NULL;
   1901 	int is_newentry;
   1902 	struct sockaddr_dl *sdl = NULL;
   1903 	int do_update;
   1904 	int olladdr;
   1905 	int llchange;
   1906 	int newstate = 0;
   1907 
   1908 	KASSERT(ifp != NULL);
   1909 	KASSERT(from != NULL);
   1910 
   1911 	/* nothing must be updated for unspecified address */
   1912 	if (IN6_IS_ADDR_UNSPECIFIED(from))
   1913 		return;
   1914 
   1915 	/*
   1916 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
   1917 	 * the caller.
   1918 	 *
   1919 	 * XXX If the link does not have link-layer adderss, what should
   1920 	 * we do? (ifp->if_addrlen == 0)
   1921 	 * Spec says nothing in sections for RA, RS and NA.  There's small
   1922 	 * description on it in NS section (RFC 2461 7.2.3).
   1923 	 */
   1924 
   1925 	rt = nd6_lookup(from, 0, ifp);
   1926 	if (rt == NULL) {
   1927 #if 0
   1928 		/* nothing must be done if there's no lladdr */
   1929 		if (!lladdr || !lladdrlen)
   1930 			return NULL;
   1931 #endif
   1932 
   1933 		rt = nd6_lookup(from, 1, ifp);
   1934 		is_newentry = 1;
   1935 	} else {
   1936 		/* do nothing if static ndp is set */
   1937 		if (rt->rt_flags & RTF_STATIC) {
   1938 			rtfree(rt);
   1939 			return;
   1940 		}
   1941 		is_newentry = 0;
   1942 	}
   1943 
   1944 	if (rt == NULL)
   1945 		return;
   1946 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
   1947 fail:
   1948 		(void)nd6_free(rt, 0);
   1949 		rtfree(rt);
   1950 		return;
   1951 	}
   1952 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1953 	if (ln == NULL)
   1954 		goto fail;
   1955 	if (rt->rt_gateway == NULL)
   1956 		goto fail;
   1957 	if (rt->rt_gateway->sa_family != AF_LINK)
   1958 		goto fail;
   1959 	sdl = satosdl(rt->rt_gateway);
   1960 
   1961 	olladdr = (sdl->sdl_alen) ? 1 : 0;
   1962 	if (olladdr && lladdr) {
   1963 		if (memcmp(lladdr, CLLADDR(sdl), ifp->if_addrlen))
   1964 			llchange = 1;
   1965 		else
   1966 			llchange = 0;
   1967 	} else
   1968 		llchange = 0;
   1969 
   1970 	/*
   1971 	 * newentry olladdr  lladdr  llchange	(*=record)
   1972 	 *	0	n	n	--	(1)
   1973 	 *	0	y	n	--	(2)
   1974 	 *	0	n	y	--	(3) * STALE
   1975 	 *	0	y	y	n	(4) *
   1976 	 *	0	y	y	y	(5) * STALE
   1977 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
   1978 	 *	1	--	y	--	(7) * STALE
   1979 	 */
   1980 
   1981 	if (lladdr) {		/* (3-5) and (7) */
   1982 		/*
   1983 		 * Record source link-layer address
   1984 		 * XXX is it dependent to ifp->if_type?
   1985 		 */
   1986 		/* XXX check for error */
   1987 		if (sockaddr_dl_setaddr(sdl, sdl->sdl_len, lladdr,
   1988 		    ifp->if_addrlen) == NULL) {
   1989 			printf("%s.%d: sockaddr_dl_setaddr(, %d, ) "
   1990 			    "failed on %s\n", __func__, __LINE__,
   1991 			    sdl->sdl_len, if_name(ifp));
   1992 		}
   1993 	}
   1994 
   1995 	if (!is_newentry) {
   1996 		if ((!olladdr && lladdr) ||		/* (3) */
   1997 		    (olladdr && lladdr && llchange)) {	/* (5) */
   1998 			do_update = 1;
   1999 			newstate = ND6_LLINFO_STALE;
   2000 		} else					/* (1-2,4) */
   2001 			do_update = 0;
   2002 	} else {
   2003 		do_update = 1;
   2004 		if (lladdr == NULL)			/* (6) */
   2005 			newstate = ND6_LLINFO_NOSTATE;
   2006 		else					/* (7) */
   2007 			newstate = ND6_LLINFO_STALE;
   2008 	}
   2009 
   2010 	if (do_update) {
   2011 		/*
   2012 		 * Update the state of the neighbor cache.
   2013 		 */
   2014 		ln->ln_state = newstate;
   2015 
   2016 		if (ln->ln_state == ND6_LLINFO_STALE) {
   2017 			/*
   2018 			 * XXX: since nd6_output() below will cause
   2019 			 * state tansition to DELAY and reset the timer,
   2020 			 * we must set the timer now, although it is actually
   2021 			 * meaningless.
   2022 			 */
   2023 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
   2024 
   2025 			nd6_llinfo_release_pkts(ln, ifp, rt);
   2026 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
   2027 			/* probe right away */
   2028 			nd6_llinfo_settimer((void *)ln, 0);
   2029 		}
   2030 	}
   2031 
   2032 	/*
   2033 	 * ICMP6 type dependent behavior.
   2034 	 *
   2035 	 * NS: clear IsRouter if new entry
   2036 	 * RS: clear IsRouter
   2037 	 * RA: set IsRouter if there's lladdr
   2038 	 * redir: clear IsRouter if new entry
   2039 	 *
   2040 	 * RA case, (1):
   2041 	 * The spec says that we must set IsRouter in the following cases:
   2042 	 * - If lladdr exist, set IsRouter.  This means (1-5).
   2043 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
   2044 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
   2045 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
   2046 	 * neighbor cache, this is similar to (6).
   2047 	 * This case is rare but we figured that we MUST NOT set IsRouter.
   2048 	 *
   2049 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
   2050 	 *							D R
   2051 	 *	0	n	n	--	(1)	c   ?     s
   2052 	 *	0	y	n	--	(2)	c   s     s
   2053 	 *	0	n	y	--	(3)	c   s     s
   2054 	 *	0	y	y	n	(4)	c   s     s
   2055 	 *	0	y	y	y	(5)	c   s     s
   2056 	 *	1	--	n	--	(6) c	c 	c s
   2057 	 *	1	--	y	--	(7) c	c   s	c s
   2058 	 *
   2059 	 *					(c=clear s=set)
   2060 	 */
   2061 	switch (type & 0xff) {
   2062 	case ND_NEIGHBOR_SOLICIT:
   2063 		/*
   2064 		 * New entry must have is_router flag cleared.
   2065 		 */
   2066 		if (is_newentry)	/* (6-7) */
   2067 			ln->ln_router = 0;
   2068 		break;
   2069 	case ND_REDIRECT:
   2070 		/*
   2071 		 * If the icmp is a redirect to a better router, always set the
   2072 		 * is_router flag.  Otherwise, if the entry is newly created,
   2073 		 * clear the flag.  [RFC 2461, sec 8.3]
   2074 		 */
   2075 		if (code == ND_REDIRECT_ROUTER)
   2076 			ln->ln_router = 1;
   2077 		else if (is_newentry) /* (6-7) */
   2078 			ln->ln_router = 0;
   2079 		break;
   2080 	case ND_ROUTER_SOLICIT:
   2081 		/*
   2082 		 * is_router flag must always be cleared.
   2083 		 */
   2084 		ln->ln_router = 0;
   2085 		break;
   2086 	case ND_ROUTER_ADVERT:
   2087 		/*
   2088 		 * Mark an entry with lladdr as a router.
   2089 		 */
   2090 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
   2091 		    (is_newentry && lladdr)) {			/* (7) */
   2092 			ln->ln_router = 1;
   2093 		}
   2094 		break;
   2095 	}
   2096 
   2097 	if (do_update)
   2098 		rt_newmsg(RTM_CHANGE, rt);  /* tell user process */
   2099 
   2100 	/*
   2101 	 * When the link-layer address of a router changes, select the
   2102 	 * best router again.  In particular, when the neighbor entry is newly
   2103 	 * created, it might affect the selection policy.
   2104 	 * Question: can we restrict the first condition to the "is_newentry"
   2105 	 * case?
   2106 	 * XXX: when we hear an RA from a new router with the link-layer
   2107 	 * address option, defrouter_select() is called twice, since
   2108 	 * defrtrlist_update called the function as well.  However, I believe
   2109 	 * we can compromise the overhead, since it only happens the first
   2110 	 * time.
   2111 	 * XXX: although defrouter_select() should not have a bad effect
   2112 	 * for those are not autoconfigured hosts, we explicitly avoid such
   2113 	 * cases for safety.
   2114 	 */
   2115 	if (do_update && ln->ln_router && !ip6_forwarding &&
   2116 	    nd6_accepts_rtadv(ndi))
   2117 		defrouter_select();
   2118 
   2119 	rtfree(rt);
   2120 }
   2121 
   2122 static void
   2123 nd6_slowtimo(void *ignored_arg)
   2124 {
   2125 	struct nd_ifinfo *nd6if;
   2126 	struct ifnet *ifp;
   2127 
   2128 	mutex_enter(softnet_lock);
   2129 	KERNEL_LOCK(1, NULL);
   2130       	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
   2131 	    nd6_slowtimo, NULL);
   2132 	IFNET_FOREACH(ifp) {
   2133 		nd6if = ND_IFINFO(ifp);
   2134 		if (nd6if->basereachable && /* already initialized */
   2135 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
   2136 			/*
   2137 			 * Since reachable time rarely changes by router
   2138 			 * advertisements, we SHOULD insure that a new random
   2139 			 * value gets recomputed at least once every few hours.
   2140 			 * (RFC 2461, 6.3.4)
   2141 			 */
   2142 			nd6if->recalctm = nd6_recalc_reachtm_interval;
   2143 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
   2144 		}
   2145 	}
   2146 	KERNEL_UNLOCK_ONE(NULL);
   2147 	mutex_exit(softnet_lock);
   2148 }
   2149 
   2150 #define senderr(e) { error = (e); goto bad;}
   2151 int
   2152 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
   2153     const struct sockaddr_in6 *dst, struct rtentry *rt00)
   2154 {
   2155 	struct mbuf *m = m0;
   2156 	struct rtentry *rt, *rt0;
   2157 	struct sockaddr_in6 *gw6 = NULL;
   2158 	struct llinfo_nd6 *ln = NULL;
   2159 	int error = 0;
   2160 
   2161 #define RTFREE_IF_NEEDED(_rt) \
   2162 	if ((_rt) != NULL && (_rt) != rt00) \
   2163 		rtfree((_rt));
   2164 
   2165 	rt = rt0 = rt00;
   2166 
   2167 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
   2168 		goto sendpkt;
   2169 
   2170 	if (nd6_need_cache(ifp) == 0)
   2171 		goto sendpkt;
   2172 
   2173 	/*
   2174 	 * next hop determination.  This routine is derived from ether_output.
   2175 	 */
   2176 	if (rt) {
   2177 		if ((rt->rt_flags & RTF_UP) == 0) {
   2178 			if ((rt0 = rt = rtalloc1(sin6tocsa(dst), 1)) != NULL) {
   2179 				if (rt->rt_ifp != ifp)
   2180 					senderr(EHOSTUNREACH);
   2181 			} else
   2182 				senderr(EHOSTUNREACH);
   2183 		}
   2184 
   2185 		if (rt->rt_flags & RTF_GATEWAY) {
   2186 			struct rtentry *gwrt;
   2187 			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
   2188 
   2189 			/*
   2190 			 * We skip link-layer address resolution and NUD
   2191 			 * if the gateway is not a neighbor from ND point
   2192 			 * of view, regardless of the value of nd_ifinfo.flags.
   2193 			 * The second condition is a bit tricky; we skip
   2194 			 * if the gateway is our own address, which is
   2195 			 * sometimes used to install a route to a p2p link.
   2196 			 */
   2197 			if (!nd6_is_addr_neighbor(gw6, ifp) ||
   2198 			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
   2199 				/*
   2200 				 * We allow this kind of tricky route only
   2201 				 * when the outgoing interface is p2p.
   2202 				 * XXX: we may need a more generic rule here.
   2203 				 */
   2204 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
   2205 					senderr(EHOSTUNREACH);
   2206 
   2207 				goto sendpkt;
   2208 			}
   2209 
   2210 			gwrt = rt_get_gwroute(rt);
   2211 			if (gwrt == NULL)
   2212 				goto lookup;
   2213 
   2214 			RTFREE_IF_NEEDED(rt);
   2215 			rt = gwrt;
   2216 			if ((rt->rt_flags & RTF_UP) == 0) {
   2217 				rtfree(rt);
   2218 				rt = rt0;
   2219 			lookup:
   2220 				gwrt = rt->rt_gwroute =
   2221 				    rtalloc1(rt->rt_gateway, 1);
   2222 				rtfree(rt);
   2223 				rt = gwrt;
   2224 				if (rt == NULL)
   2225 					senderr(EHOSTUNREACH);
   2226 				/* the "G" test below also prevents rt == rt0 */
   2227 				if ((rt->rt_flags & RTF_GATEWAY) ||
   2228 				    (rt->rt_ifp != ifp)) {
   2229 					rt0->rt_gwroute = NULL;
   2230 					senderr(EHOSTUNREACH);
   2231 				}
   2232 			}
   2233 		}
   2234 	}
   2235 
   2236 	/*
   2237 	 * Address resolution or Neighbor Unreachability Detection
   2238 	 * for the next hop.
   2239 	 * At this point, the destination of the packet must be a unicast
   2240 	 * or an anycast address(i.e. not a multicast).
   2241 	 */
   2242 
   2243 	/* Look up the neighbor cache for the nexthop */
   2244 	if (rt != NULL && (rt->rt_flags & RTF_LLINFO) != 0)
   2245 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   2246 	else {
   2247 		/*
   2248 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
   2249 		 * the condition below is not very efficient.  But we believe
   2250 		 * it is tolerable, because this should be a rare case.
   2251 		 */
   2252 		if (nd6_is_addr_neighbor(dst, ifp)) {
   2253 			RTFREE_IF_NEEDED(rt);
   2254 			rt = nd6_lookup(&dst->sin6_addr, 1, ifp);
   2255 			if (rt != NULL)
   2256 				ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   2257 		}
   2258 	}
   2259 	if (ln == NULL || rt == NULL) {
   2260 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
   2261 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
   2262 			log(LOG_DEBUG,
   2263 			    "nd6_output: can't allocate llinfo for %s "
   2264 			    "(ln=%p, rt=%p)\n",
   2265 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
   2266 			senderr(EIO);	/* XXX: good error? */
   2267 		}
   2268 
   2269 		goto sendpkt;	/* send anyway */
   2270 	}
   2271 
   2272 	/*
   2273 	 * Move this entry to the head of the queue so that it is less likely
   2274 	 * for this entry to be a target of forced garbage collection (see
   2275 	 * nd6_rtrequest()).
   2276 	 */
   2277 	LN_DEQUEUE(ln);
   2278 	LN_INSERTHEAD(ln);
   2279 
   2280 	/* We don't have to do link-layer address resolution on a p2p link. */
   2281 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
   2282 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
   2283 		ln->ln_state = ND6_LLINFO_STALE;
   2284 		nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
   2285 	}
   2286 
   2287 	/*
   2288 	 * The first time we send a packet to a neighbor whose entry is
   2289 	 * STALE, we have to change the state to DELAY and a sets a timer to
   2290 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
   2291 	 * neighbor unreachability detection on expiration.
   2292 	 * (RFC 2461 7.3.3)
   2293 	 */
   2294 	if (ln->ln_state == ND6_LLINFO_STALE) {
   2295 		ln->ln_asked = 0;
   2296 		ln->ln_state = ND6_LLINFO_DELAY;
   2297 		nd6_llinfo_settimer(ln, (long)nd6_delay * hz);
   2298 	}
   2299 
   2300 	/*
   2301 	 * If the neighbor cache entry has a state other than INCOMPLETE
   2302 	 * (i.e. its link-layer address is already resolved), just
   2303 	 * send the packet.
   2304 	 */
   2305 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
   2306 		goto sendpkt;
   2307 
   2308 	/*
   2309 	 * There is a neighbor cache entry, but no ethernet address
   2310 	 * response yet.  Append this latest packet to the end of the
   2311 	 * packet queue in the mbuf, unless the number of the packet
   2312 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
   2313 	 * the oldest packet in the queue will be removed.
   2314 	 */
   2315 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
   2316 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
   2317 	if (ln->ln_hold) {
   2318 		struct mbuf *m_hold;
   2319 		int i;
   2320 
   2321 		i = 0;
   2322 		for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
   2323 			i++;
   2324 			if (m_hold->m_nextpkt == NULL) {
   2325 				m_hold->m_nextpkt = m;
   2326 				break;
   2327 			}
   2328 		}
   2329 		while (i >= nd6_maxqueuelen) {
   2330 			m_hold = ln->ln_hold;
   2331 			ln->ln_hold = ln->ln_hold->m_nextpkt;
   2332 			m_freem(m_hold);
   2333 			i--;
   2334 		}
   2335 	} else {
   2336 		ln->ln_hold = m;
   2337 	}
   2338 
   2339 	/*
   2340 	 * If there has been no NS for the neighbor after entering the
   2341 	 * INCOMPLETE state, send the first solicitation.
   2342 	 */
   2343 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
   2344 		ln->ln_asked++;
   2345 		nd6_llinfo_settimer(ln,
   2346 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
   2347 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
   2348 	}
   2349 	error = 0;
   2350 	goto exit;
   2351 
   2352   sendpkt:
   2353 	/* discard the packet if IPv6 operation is disabled on the interface */
   2354 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
   2355 		error = ENETDOWN; /* better error? */
   2356 		goto bad;
   2357 	}
   2358 
   2359 #ifndef NET_MPSAFE
   2360 	KERNEL_LOCK(1, NULL);
   2361 #endif
   2362 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
   2363 		error = (*ifp->if_output)(origifp, m, sin6tocsa(dst), rt);
   2364 	else
   2365 		error = (*ifp->if_output)(ifp, m, sin6tocsa(dst), rt);
   2366 #ifndef NET_MPSAFE
   2367 	KERNEL_UNLOCK_ONE(NULL);
   2368 #endif
   2369 	goto exit;
   2370 
   2371   bad:
   2372 	if (m != NULL)
   2373 		m_freem(m);
   2374   exit:
   2375 	RTFREE_IF_NEEDED(rt);
   2376 
   2377 	return error;
   2378 #undef RTFREE_IF_NEEDED
   2379 }
   2380 #undef senderr
   2381 
   2382 int
   2383 nd6_need_cache(struct ifnet *ifp)
   2384 {
   2385 	/*
   2386 	 * XXX: we currently do not make neighbor cache on any interface
   2387 	 * other than ARCnet, Ethernet, FDDI and GIF.
   2388 	 *
   2389 	 * RFC2893 says:
   2390 	 * - unidirectional tunnels needs no ND
   2391 	 */
   2392 	switch (ifp->if_type) {
   2393 	case IFT_ARCNET:
   2394 	case IFT_ETHER:
   2395 	case IFT_FDDI:
   2396 	case IFT_IEEE1394:
   2397 	case IFT_CARP:
   2398 	case IFT_GIF:		/* XXX need more cases? */
   2399 	case IFT_PPP:
   2400 	case IFT_TUNNEL:
   2401 		return 1;
   2402 	default:
   2403 		return 0;
   2404 	}
   2405 }
   2406 
   2407 int
   2408 nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt,
   2409     struct mbuf *m, const struct sockaddr *dst, uint8_t *lldst,
   2410     size_t dstsize)
   2411 {
   2412 	const struct sockaddr_dl *sdl;
   2413 
   2414 	if (m->m_flags & M_MCAST) {
   2415 		switch (ifp->if_type) {
   2416 		case IFT_ETHER:
   2417 		case IFT_FDDI:
   2418 			ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr,
   2419 			    lldst);
   2420 			return 1;
   2421 		case IFT_IEEE1394:
   2422 			memcpy(lldst, ifp->if_broadcastaddr,
   2423 			    MIN(dstsize, ifp->if_addrlen));
   2424 			return 1;
   2425 		case IFT_ARCNET:
   2426 			*lldst = 0;
   2427 			return 1;
   2428 		default:
   2429 			m_freem(m);
   2430 			return 0;
   2431 		}
   2432 	}
   2433 
   2434 	if (rt == NULL) {
   2435 		/* this could happen, if we could not allocate memory */
   2436 		m_freem(m);
   2437 		return 0;
   2438 	}
   2439 	if (rt->rt_gateway->sa_family != AF_LINK) {
   2440 		char gbuf[256];
   2441 		char dbuf[LINK_ADDRSTRLEN];
   2442 		sockaddr_format(rt->rt_gateway, gbuf, sizeof(gbuf));
   2443 		printf("%s: bad gateway address type %s for dst %s"
   2444 		    " through interface %s\n", __func__, gbuf,
   2445 		    IN6_PRINT(dbuf, &satocsin6(dst)->sin6_addr),
   2446 		    if_name(ifp));
   2447 		m_freem(m);
   2448 		return 0;
   2449 	}
   2450 	sdl = satocsdl(rt->rt_gateway);
   2451 	if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) {
   2452 		char sbuf[INET6_ADDRSTRLEN];
   2453 		char dbuf[LINK_ADDRSTRLEN];
   2454 		/* this should be impossible, but we bark here for debugging */
   2455 		printf("%s: sdl_alen == %" PRIu8 ", if=%s, dst=%s, sdl=%s\n",
   2456 		    __func__, sdl->sdl_alen, if_name(ifp),
   2457 		    IN6_PRINT(sbuf, &satocsin6(dst)->sin6_addr),
   2458 		    DL_PRINT(dbuf, &sdl->sdl_addr));
   2459 		m_freem(m);
   2460 		return 0;
   2461 	}
   2462 
   2463 	memcpy(lldst, CLLADDR(sdl), MIN(dstsize, sdl->sdl_alen));
   2464 	return 1;
   2465 }
   2466 
   2467 static void
   2468 clear_llinfo_pqueue(struct llinfo_nd6 *ln)
   2469 {
   2470 	struct mbuf *m_hold, *m_hold_next;
   2471 
   2472 	for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
   2473 		m_hold_next = m_hold->m_nextpkt;
   2474 		m_hold->m_nextpkt = NULL;
   2475 		m_freem(m_hold);
   2476 	}
   2477 
   2478 	ln->ln_hold = NULL;
   2479 	return;
   2480 }
   2481 
   2482 int
   2483 nd6_sysctl(
   2484     int name,
   2485     void *oldp,	/* syscall arg, need copyout */
   2486     size_t *oldlenp,
   2487     void *newp,	/* syscall arg, need copyin */
   2488     size_t newlen
   2489 )
   2490 {
   2491 	void *p;
   2492 	size_t ol;
   2493 	int error;
   2494 
   2495 	error = 0;
   2496 
   2497 	if (newp)
   2498 		return EPERM;
   2499 	if (oldp && !oldlenp)
   2500 		return EINVAL;
   2501 	ol = oldlenp ? *oldlenp : 0;
   2502 
   2503 	if (oldp) {
   2504 		p = malloc(*oldlenp, M_TEMP, M_WAITOK);
   2505 		if (p == NULL)
   2506 			return ENOMEM;
   2507 	} else
   2508 		p = NULL;
   2509 	switch (name) {
   2510 	case ICMPV6CTL_ND6_DRLIST:
   2511 		error = fill_drlist(p, oldlenp, ol);
   2512 		if (!error && p != NULL && oldp != NULL)
   2513 			error = copyout(p, oldp, *oldlenp);
   2514 		break;
   2515 
   2516 	case ICMPV6CTL_ND6_PRLIST:
   2517 		error = fill_prlist(p, oldlenp, ol);
   2518 		if (!error && p != NULL && oldp != NULL)
   2519 			error = copyout(p, oldp, *oldlenp);
   2520 		break;
   2521 
   2522 	case ICMPV6CTL_ND6_MAXQLEN:
   2523 		break;
   2524 
   2525 	default:
   2526 		error = ENOPROTOOPT;
   2527 		break;
   2528 	}
   2529 	if (p)
   2530 		free(p, M_TEMP);
   2531 
   2532 	return error;
   2533 }
   2534 
   2535 static int
   2536 fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
   2537 {
   2538 	int error = 0, s;
   2539 	struct in6_defrouter *d = NULL, *de = NULL;
   2540 	struct nd_defrouter *dr;
   2541 	size_t l;
   2542 
   2543 	s = splsoftnet();
   2544 
   2545 	if (oldp) {
   2546 		d = (struct in6_defrouter *)oldp;
   2547 		de = (struct in6_defrouter *)((char *)oldp + *oldlenp);
   2548 	}
   2549 	l = 0;
   2550 
   2551 	TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
   2552 
   2553 		if (oldp && d + 1 <= de) {
   2554 			memset(d, 0, sizeof(*d));
   2555 			sockaddr_in6_init(&d->rtaddr, &dr->rtaddr, 0, 0, 0);
   2556 			if (sa6_recoverscope(&d->rtaddr)) {
   2557 				log(LOG_ERR,
   2558 				    "scope error in router list (%s)\n",
   2559 				    ip6_sprintf(&d->rtaddr.sin6_addr));
   2560 				/* XXX: press on... */
   2561 			}
   2562 			d->flags = dr->flags;
   2563 			d->rtlifetime = dr->rtlifetime;
   2564 			d->expire = dr->expire ?
   2565 			    time_mono_to_wall(dr->expire) : 0;
   2566 			d->if_index = dr->ifp->if_index;
   2567 		}
   2568 
   2569 		l += sizeof(*d);
   2570 		if (d)
   2571 			d++;
   2572 	}
   2573 
   2574 	if (oldp) {
   2575 		if (l > ol)
   2576 			error = ENOMEM;
   2577 	}
   2578 	if (oldlenp)
   2579 		*oldlenp = l;	/* (void *)d - (void *)oldp */
   2580 
   2581 	splx(s);
   2582 
   2583 	return error;
   2584 }
   2585 
   2586 static int
   2587 fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
   2588 {
   2589 	int error = 0, s;
   2590 	struct nd_prefix *pr;
   2591 	uint8_t *p = NULL, *ps = NULL;
   2592 	uint8_t *pe = NULL;
   2593 	size_t l;
   2594 
   2595 	s = splsoftnet();
   2596 
   2597 	if (oldp) {
   2598 		ps = p = (uint8_t*)oldp;
   2599 		pe = (uint8_t*)oldp + *oldlenp;
   2600 	}
   2601 	l = 0;
   2602 
   2603 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   2604 		u_short advrtrs;
   2605 		struct sockaddr_in6 sin6;
   2606 		struct nd_pfxrouter *pfr;
   2607 		struct in6_prefix pfx;
   2608 
   2609 		if (oldp && p + sizeof(struct in6_prefix) <= pe)
   2610 		{
   2611 			memset(&pfx, 0, sizeof(pfx));
   2612 			ps = p;
   2613 			pfx.prefix = pr->ndpr_prefix;
   2614 
   2615 			if (sa6_recoverscope(&pfx.prefix)) {
   2616 				log(LOG_ERR,
   2617 				    "scope error in prefix list (%s)\n",
   2618 				    ip6_sprintf(&pfx.prefix.sin6_addr));
   2619 				/* XXX: press on... */
   2620 			}
   2621 			pfx.raflags = pr->ndpr_raf;
   2622 			pfx.prefixlen = pr->ndpr_plen;
   2623 			pfx.vltime = pr->ndpr_vltime;
   2624 			pfx.pltime = pr->ndpr_pltime;
   2625 			pfx.if_index = pr->ndpr_ifp->if_index;
   2626 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
   2627 				pfx.expire = 0;
   2628 			else {
   2629 				time_t maxexpire;
   2630 
   2631 				/* XXX: we assume time_t is signed. */
   2632 				maxexpire = (-1) &
   2633 				    ~((time_t)1 <<
   2634 				    ((sizeof(maxexpire) * 8) - 1));
   2635 				if (pr->ndpr_vltime <
   2636 				    maxexpire - pr->ndpr_lastupdate) {
   2637 					pfx.expire = pr->ndpr_lastupdate +
   2638 						pr->ndpr_vltime;
   2639 				} else
   2640 					pfx.expire = maxexpire;
   2641 			}
   2642 			pfx.refcnt = pr->ndpr_refcnt;
   2643 			pfx.flags = pr->ndpr_stateflags;
   2644 			pfx.origin = PR_ORIG_RA;
   2645 
   2646 			p += sizeof(pfx); l += sizeof(pfx);
   2647 
   2648 			advrtrs = 0;
   2649 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
   2650 				if (p + sizeof(sin6) > pe) {
   2651 					advrtrs++;
   2652 					continue;
   2653 				}
   2654 
   2655 				sockaddr_in6_init(&sin6, &pfr->router->rtaddr,
   2656 				    0, 0, 0);
   2657 				if (sa6_recoverscope(&sin6)) {
   2658 					log(LOG_ERR,
   2659 					    "scope error in "
   2660 					    "prefix list (%s)\n",
   2661 					    ip6_sprintf(&pfr->router->rtaddr));
   2662 				}
   2663 				advrtrs++;
   2664 				memcpy(p, &sin6, sizeof(sin6));
   2665 				p += sizeof(sin6);
   2666 				l += sizeof(sin6);
   2667 			}
   2668 			pfx.advrtrs = advrtrs;
   2669 			memcpy(ps, &pfx, sizeof(pfx));
   2670 		}
   2671 		else {
   2672 			l += sizeof(pfx);
   2673 			advrtrs = 0;
   2674 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
   2675 				advrtrs++;
   2676 				l += sizeof(sin6);
   2677 			}
   2678 		}
   2679 	}
   2680 
   2681 	if (oldp) {
   2682 		*oldlenp = l;	/* (void *)d - (void *)oldp */
   2683 		if (l > ol)
   2684 			error = ENOMEM;
   2685 	} else
   2686 		*oldlenp = l;
   2687 
   2688 	splx(s);
   2689 
   2690 	return error;
   2691 }
   2692