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