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