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