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