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