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