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