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