Home | History | Annotate | Line # | Download | only in netinet6
nd6.c revision 1.117
      1 /*	$NetBSD: nd6.c,v 1.117 2007/07/19 20:48:57 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.117 2007/07/19 20:48:57 dyoung Exp $");
     35 
     36 #include "opt_ipsec.h"
     37 
     38 #include <sys/param.h>
     39 #include <sys/systm.h>
     40 #include <sys/callout.h>
     41 #include <sys/malloc.h>
     42 #include <sys/mbuf.h>
     43 #include <sys/socket.h>
     44 #include <sys/sockio.h>
     45 #include <sys/time.h>
     46 #include <sys/kernel.h>
     47 #include <sys/protosw.h>
     48 #include <sys/errno.h>
     49 #include <sys/ioctl.h>
     50 #include <sys/syslog.h>
     51 #include <sys/queue.h>
     52 
     53 #include <net/if.h>
     54 #include <net/if_dl.h>
     55 #include <net/if_types.h>
     56 #include <net/route.h>
     57 #include <net/if_ether.h>
     58 #include <net/if_fddi.h>
     59 #include <net/if_arc.h>
     60 
     61 #include <netinet/in.h>
     62 #include <netinet6/in6_var.h>
     63 #include <netinet/ip6.h>
     64 #include <netinet6/ip6_var.h>
     65 #include <netinet6/scope6_var.h>
     66 #include <netinet6/nd6.h>
     67 #include <netinet/icmp6.h>
     68 
     69 #ifdef IPSEC
     70 #include <netinet6/ipsec.h>
     71 #endif
     72 
     73 #include <net/net_osdep.h>
     74 
     75 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
     76 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
     77 
     78 #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 = satocsin6(rt_getkey(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 = satocsin6(rt_getkey(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(&satocsin6(rt_getkey(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_getkey(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 	RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1130 	    __LINE__, (void *)rt->_rt_key);
   1131 
   1132 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
   1133 		return;
   1134 
   1135 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
   1136 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1137 		    __LINE__, (void *)rt->_rt_key);
   1138 		/*
   1139 		 * This is probably an interface direct route for a link
   1140 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
   1141 		 * We do not need special treatment below for such a route.
   1142 		 * Moreover, the RTF_LLINFO flag which would be set below
   1143 		 * would annoy the ndp(8) command.
   1144 		 */
   1145 		return;
   1146 	}
   1147 
   1148 	if (req == RTM_RESOLVE &&
   1149 	    (nd6_need_cache(ifp) == 0 || /* stf case */
   1150 	     !nd6_is_addr_neighbor(satocsin6(rt_getkey(rt)), ifp))) {
   1151 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1152 		    __LINE__, (void *)rt->_rt_key);
   1153 		/*
   1154 		 * FreeBSD and BSD/OS often make a cloned host route based
   1155 		 * on a less-specific route (e.g. the default route).
   1156 		 * If the less specific route does not have a "gateway"
   1157 		 * (this is the case when the route just goes to a p2p or an
   1158 		 * stf interface), we'll mistakenly make a neighbor cache for
   1159 		 * the host route, and will see strange neighbor solicitation
   1160 		 * for the corresponding destination.  In order to avoid the
   1161 		 * confusion, we check if the destination of the route is
   1162 		 * a neighbor in terms of neighbor discovery, and stop the
   1163 		 * process if not.  Additionally, we remove the LLINFO flag
   1164 		 * so that ndp(8) will not try to get the neighbor information
   1165 		 * of the destination.
   1166 		 */
   1167 		rt->rt_flags &= ~RTF_LLINFO;
   1168 		return;
   1169 	}
   1170 
   1171 	switch (req) {
   1172 	case RTM_ADD:
   1173 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1174 		    __LINE__, (void *)rt->_rt_key);
   1175 		/*
   1176 		 * There is no backward compatibility :)
   1177 		 *
   1178 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
   1179 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
   1180 		 *	   rt->rt_flags |= RTF_CLONING;
   1181 		 */
   1182 		if ((rt->rt_flags & RTF_CLONING) ||
   1183 		    ((rt->rt_flags & RTF_LLINFO) && !ln)) {
   1184 			/*
   1185 			 * Case 1: This route should come from a route to
   1186 			 * interface (RTF_CLONING case) or the route should be
   1187 			 * treated as on-link but is currently not
   1188 			 * (RTF_LLINFO && !ln case).
   1189 			 */
   1190 			rt_setgate(rt, (const struct sockaddr *)&null_sdl);
   1191 			RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1192 			    __LINE__, (void *)rt->_rt_key);
   1193 			gate = rt->rt_gateway;
   1194 			SDL(gate)->sdl_type = ifp->if_type;
   1195 			SDL(gate)->sdl_index = ifp->if_index;
   1196 			if (ln)
   1197 				nd6_llinfo_settimer(ln, 0);
   1198 			RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1199 			    __LINE__, (void *)rt->_rt_key);
   1200 			if ((rt->rt_flags & RTF_CLONING) != 0)
   1201 				break;
   1202 		}
   1203 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1204 		    __LINE__, (void *)rt->_rt_key);
   1205 		/*
   1206 		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
   1207 		 * We don't do that here since llinfo is not ready yet.
   1208 		 *
   1209 		 * There are also couple of other things to be discussed:
   1210 		 * - unsolicited NA code needs improvement beforehand
   1211 		 * - RFC2461 says we MAY send multicast unsolicited NA
   1212 		 *   (7.2.6 paragraph 4), however, it also says that we
   1213 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
   1214 		 *   we don't have anything like it right now.
   1215 		 *   note that the mechanism needs a mutual agreement
   1216 		 *   between proxies, which means that we need to implement
   1217 		 *   a new protocol, or a new kludge.
   1218 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
   1219 		 *   we need to check ip6forwarding before sending it.
   1220 		 *   (or should we allow proxy ND configuration only for
   1221 		 *   routers?  there's no mention about proxy ND from hosts)
   1222 		 */
   1223 #if 0
   1224 		/* XXX it does not work */
   1225 		if (rt->rt_flags & RTF_ANNOUNCE)
   1226 			nd6_na_output(ifp,
   1227 			      &SIN6(rt_getkey(rt))->sin6_addr,
   1228 			      &SIN6(rt_getkey(rt))->sin6_addr,
   1229 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
   1230 			      1, NULL);
   1231 #endif
   1232 		/* FALLTHROUGH */
   1233 	case RTM_RESOLVE:
   1234 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
   1235 			RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1236 			    __LINE__, (void *)rt->_rt_key);
   1237 			/*
   1238 			 * Address resolution isn't necessary for a point to
   1239 			 * point link, so we can skip this test for a p2p link.
   1240 			 */
   1241 			if (gate->sa_family != AF_LINK ||
   1242 			    gate->sa_len < sizeof(null_sdl)) {
   1243 				log(LOG_DEBUG,
   1244 				    "nd6_rtrequest: bad gateway value: %s\n",
   1245 				    if_name(ifp));
   1246 				break;
   1247 			}
   1248 			SDL(gate)->sdl_type = ifp->if_type;
   1249 			SDL(gate)->sdl_index = ifp->if_index;
   1250 			RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1251 			    __LINE__, (void *)rt->_rt_key);
   1252 		}
   1253 		if (ln != NULL)
   1254 			break;	/* This happens on a route change */
   1255 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1256 		    __LINE__, (void *)rt->_rt_key);
   1257 		/*
   1258 		 * Case 2: This route may come from cloning, or a manual route
   1259 		 * add with a LL address.
   1260 		 */
   1261 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
   1262 		rt->rt_llinfo = (void *)ln;
   1263 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1264 		    __LINE__, (void *)rt->_rt_key);
   1265 		if (ln == NULL) {
   1266 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
   1267 			break;
   1268 		}
   1269 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1270 		    __LINE__, (void *)rt->_rt_key);
   1271 		nd6_inuse++;
   1272 		nd6_allocated++;
   1273 		bzero(ln, sizeof(*ln));
   1274 		ln->ln_rt = rt;
   1275 		callout_init(&ln->ln_timer_ch, 0);
   1276 		/* this is required for "ndp" command. - shin */
   1277 		if (req == RTM_ADD) {
   1278 		        /*
   1279 			 * gate should have some valid AF_LINK entry,
   1280 			 * and ln->ln_expire should have some lifetime
   1281 			 * which is specified by ndp command.
   1282 			 */
   1283 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1284 			ln->ln_byhint = 0;
   1285 		} else {
   1286 		        /*
   1287 			 * When req == RTM_RESOLVE, rt is created and
   1288 			 * initialized in rtrequest(), so rt_expire is 0.
   1289 			 */
   1290 			ln->ln_state = ND6_LLINFO_NOSTATE;
   1291 			nd6_llinfo_settimer(ln, 0);
   1292 		}
   1293 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1294 		    __LINE__, (void *)rt->_rt_key);
   1295 		rt->rt_flags |= RTF_LLINFO;
   1296 		ln->ln_next = llinfo_nd6.ln_next;
   1297 		llinfo_nd6.ln_next = ln;
   1298 		ln->ln_prev = &llinfo_nd6;
   1299 		ln->ln_next->ln_prev = ln;
   1300 
   1301 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1302 		    __LINE__, (void *)rt->_rt_key);
   1303 		/*
   1304 		 * check if rt_getkey(rt) is an address assigned
   1305 		 * to the interface.
   1306 		 */
   1307 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
   1308 		    &SIN6(rt_getkey(rt))->sin6_addr);
   1309 		RT_DPRINTF("%s l.%d: rt->_rt_key = %p\n", __func__,
   1310 		    __LINE__, (void *)rt->_rt_key);
   1311 		if (ifa) {
   1312 			void *macp = nd6_ifptomac(ifp);
   1313 			nd6_llinfo_settimer(ln, -1);
   1314 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1315 			ln->ln_byhint = 0;
   1316 			if (macp) {
   1317 				bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
   1318 				SDL(gate)->sdl_alen = ifp->if_addrlen;
   1319 			}
   1320 			if (nd6_useloopback) {
   1321 				rt->rt_ifp = lo0ifp;	/* XXX */
   1322 				/*
   1323 				 * Make sure rt_ifa be equal to the ifaddr
   1324 				 * corresponding to the address.
   1325 				 * We need this because when we refer
   1326 				 * rt_ifa->ia6_flags in ip6_input, we assume
   1327 				 * that the rt_ifa points to the address instead
   1328 				 * of the loopback address.
   1329 				 */
   1330 				if (ifa != rt->rt_ifa)
   1331 					rt_replace_ifa(rt, ifa);
   1332 				rt->rt_flags &= ~RTF_CLONED;
   1333 			}
   1334 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
   1335 			nd6_llinfo_settimer(ln, -1);
   1336 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1337 			ln->ln_byhint = 0;
   1338 
   1339 			/* join solicited node multicast for proxy ND */
   1340 			if (ifp->if_flags & IFF_MULTICAST) {
   1341 				struct in6_addr llsol;
   1342 				int error;
   1343 
   1344 				llsol = SIN6(rt_getkey(rt))->sin6_addr;
   1345 				llsol.s6_addr32[0] = htonl(0xff020000);
   1346 				llsol.s6_addr32[1] = 0;
   1347 				llsol.s6_addr32[2] = htonl(1);
   1348 				llsol.s6_addr8[12] = 0xff;
   1349 				if (in6_setscope(&llsol, ifp, NULL))
   1350 					break;
   1351 				if (!in6_addmulti(&llsol, ifp, &error, 0)) {
   1352 					nd6log((LOG_ERR, "%s: failed to join "
   1353 					    "%s (errno=%d)\n", if_name(ifp),
   1354 					    ip6_sprintf(&llsol), error));
   1355 				}
   1356 			}
   1357 		}
   1358 		break;
   1359 
   1360 	case RTM_DELETE:
   1361 		if (ln == NULL)
   1362 			break;
   1363 		/* leave from solicited node multicast for proxy ND */
   1364 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
   1365 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
   1366 			struct in6_addr llsol;
   1367 			struct in6_multi *in6m;
   1368 
   1369 			llsol = SIN6(rt_getkey(rt))->sin6_addr;
   1370 			llsol.s6_addr32[0] = htonl(0xff020000);
   1371 			llsol.s6_addr32[1] = 0;
   1372 			llsol.s6_addr32[2] = htonl(1);
   1373 			llsol.s6_addr8[12] = 0xff;
   1374 			if (in6_setscope(&llsol, ifp, NULL) == 0) {
   1375 				IN6_LOOKUP_MULTI(llsol, ifp, in6m);
   1376 				if (in6m)
   1377 					in6_delmulti(in6m);
   1378 			}
   1379 		}
   1380 		nd6_inuse--;
   1381 		ln->ln_next->ln_prev = ln->ln_prev;
   1382 		ln->ln_prev->ln_next = ln->ln_next;
   1383 		ln->ln_prev = NULL;
   1384 		nd6_llinfo_settimer(ln, -1);
   1385 		rt->rt_llinfo = 0;
   1386 		rt->rt_flags &= ~RTF_LLINFO;
   1387 		clear_llinfo_pqueue(ln);
   1388 		Free((void *)ln);
   1389 	}
   1390 }
   1391 
   1392 int
   1393 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
   1394 {
   1395 	struct in6_drlist *drl = (struct in6_drlist *)data;
   1396 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
   1397 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
   1398 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
   1399 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
   1400 	struct nd_defrouter *dr;
   1401 	struct nd_prefix *pr;
   1402 	struct rtentry *rt;
   1403 	int i = 0, error = 0;
   1404 	int s;
   1405 
   1406 	switch (cmd) {
   1407 	case SIOCGDRLST_IN6:
   1408 		/*
   1409 		 * obsolete API, use sysctl under net.inet6.icmp6
   1410 		 */
   1411 		bzero(drl, sizeof(*drl));
   1412 		s = splsoftnet();
   1413 		TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
   1414 			if (i >= DRLSTSIZ)
   1415 				break;
   1416 			drl->defrouter[i].rtaddr = dr->rtaddr;
   1417 			in6_clearscope(&drl->defrouter[i].rtaddr);
   1418 
   1419 			drl->defrouter[i].flags = dr->flags;
   1420 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
   1421 			drl->defrouter[i].expire = dr->expire;
   1422 			drl->defrouter[i].if_index = dr->ifp->if_index;
   1423 			i++;
   1424 		}
   1425 		splx(s);
   1426 		break;
   1427 	case SIOCGPRLST_IN6:
   1428 		/*
   1429 		 * obsolete API, use sysctl under net.inet6.icmp6
   1430 		 *
   1431 		 * XXX the structure in6_prlist was changed in backward-
   1432 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
   1433 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
   1434 		 */
   1435 		/*
   1436 		 * XXX meaning of fields, especialy "raflags", is very
   1437 		 * differnet between RA prefix list and RR/static prefix list.
   1438 		 * how about separating ioctls into two?
   1439 		 */
   1440 		bzero(oprl, sizeof(*oprl));
   1441 		s = splsoftnet();
   1442 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   1443 			struct nd_pfxrouter *pfr;
   1444 			int j;
   1445 
   1446 			if (i >= PRLSTSIZ)
   1447 				break;
   1448 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
   1449 			oprl->prefix[i].raflags = pr->ndpr_raf;
   1450 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
   1451 			oprl->prefix[i].vltime = pr->ndpr_vltime;
   1452 			oprl->prefix[i].pltime = pr->ndpr_pltime;
   1453 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
   1454 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
   1455 				oprl->prefix[i].expire = 0;
   1456 			else {
   1457 				time_t maxexpire;
   1458 
   1459 				/* XXX: we assume time_t is signed. */
   1460 				maxexpire = (-1) &
   1461 				    ~((time_t)1 <<
   1462 				    ((sizeof(maxexpire) * 8) - 1));
   1463 				if (pr->ndpr_vltime <
   1464 				    maxexpire - pr->ndpr_lastupdate) {
   1465 					oprl->prefix[i].expire =
   1466 						 pr->ndpr_lastupdate +
   1467 						pr->ndpr_vltime;
   1468 				} else
   1469 					oprl->prefix[i].expire = maxexpire;
   1470 			}
   1471 
   1472 			j = 0;
   1473 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
   1474 				if (j < DRLSTSIZ) {
   1475 #define RTRADDR oprl->prefix[i].advrtr[j]
   1476 					RTRADDR = pfr->router->rtaddr;
   1477 					in6_clearscope(&RTRADDR);
   1478 #undef RTRADDR
   1479 				}
   1480 				j++;
   1481 			}
   1482 			oprl->prefix[i].advrtrs = j;
   1483 			oprl->prefix[i].origin = PR_ORIG_RA;
   1484 
   1485 			i++;
   1486 		}
   1487 		splx(s);
   1488 
   1489 		break;
   1490 	case OSIOCGIFINFO_IN6:
   1491 #define ND	ndi->ndi
   1492 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
   1493 		memset(&ND, 0, sizeof(ND));
   1494 		ND.linkmtu = IN6_LINKMTU(ifp);
   1495 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
   1496 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
   1497 		ND.reachable = ND_IFINFO(ifp)->reachable;
   1498 		ND.retrans = ND_IFINFO(ifp)->retrans;
   1499 		ND.flags = ND_IFINFO(ifp)->flags;
   1500 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
   1501 		ND.chlim = ND_IFINFO(ifp)->chlim;
   1502 		break;
   1503 	case SIOCGIFINFO_IN6:
   1504 		ND = *ND_IFINFO(ifp);
   1505 		break;
   1506 	case SIOCSIFINFO_IN6:
   1507 		/*
   1508 		 * used to change host variables from userland.
   1509 		 * intented for a use on router to reflect RA configurations.
   1510 		 */
   1511 		/* 0 means 'unspecified' */
   1512 		if (ND.linkmtu != 0) {
   1513 			if (ND.linkmtu < IPV6_MMTU ||
   1514 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
   1515 				error = EINVAL;
   1516 				break;
   1517 			}
   1518 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
   1519 		}
   1520 
   1521 		if (ND.basereachable != 0) {
   1522 			int obasereachable = ND_IFINFO(ifp)->basereachable;
   1523 
   1524 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
   1525 			if (ND.basereachable != obasereachable)
   1526 				ND_IFINFO(ifp)->reachable =
   1527 				    ND_COMPUTE_RTIME(ND.basereachable);
   1528 		}
   1529 		if (ND.retrans != 0)
   1530 			ND_IFINFO(ifp)->retrans = ND.retrans;
   1531 		if (ND.chlim != 0)
   1532 			ND_IFINFO(ifp)->chlim = ND.chlim;
   1533 		/* FALLTHROUGH */
   1534 	case SIOCSIFINFO_FLAGS:
   1535 		ND_IFINFO(ifp)->flags = ND.flags;
   1536 		break;
   1537 #undef ND
   1538 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
   1539 		/* sync kernel routing table with the default router list */
   1540 		defrouter_reset();
   1541 		defrouter_select();
   1542 		break;
   1543 	case SIOCSPFXFLUSH_IN6:
   1544 	{
   1545 		/* flush all the prefix advertised by routers */
   1546 		struct nd_prefix *pfx, *next;
   1547 
   1548 		s = splsoftnet();
   1549 		for (pfx = LIST_FIRST(&nd_prefix); pfx; pfx = next) {
   1550 			struct in6_ifaddr *ia, *ia_next;
   1551 
   1552 			next = LIST_NEXT(pfx, ndpr_entry);
   1553 
   1554 			if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr))
   1555 				continue; /* XXX */
   1556 
   1557 			/* do we really have to remove addresses as well? */
   1558 			for (ia = in6_ifaddr; ia; ia = ia_next) {
   1559 				/* ia might be removed.  keep the next ptr. */
   1560 				ia_next = ia->ia_next;
   1561 
   1562 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
   1563 					continue;
   1564 
   1565 				if (ia->ia6_ndpr == pfx)
   1566 					in6_purgeaddr(&ia->ia_ifa);
   1567 			}
   1568 			prelist_remove(pfx);
   1569 		}
   1570 		splx(s);
   1571 		break;
   1572 	}
   1573 	case SIOCSRTRFLUSH_IN6:
   1574 	{
   1575 		/* flush all the default routers */
   1576 		struct nd_defrouter *drtr, *next;
   1577 
   1578 		s = splsoftnet();
   1579 		defrouter_reset();
   1580 		for (drtr = TAILQ_FIRST(&nd_defrouter); drtr; drtr = next) {
   1581 			next = TAILQ_NEXT(drtr, dr_entry);
   1582 			defrtrlist_del(drtr);
   1583 		}
   1584 		defrouter_select();
   1585 		splx(s);
   1586 		break;
   1587 	}
   1588 	case SIOCGNBRINFO_IN6:
   1589 	{
   1590 		struct llinfo_nd6 *ln;
   1591 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
   1592 
   1593 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
   1594 			return error;
   1595 
   1596 		s = splsoftnet();
   1597 		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
   1598 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
   1599 			error = EINVAL;
   1600 			splx(s);
   1601 			break;
   1602 		}
   1603 		nbi->state = ln->ln_state;
   1604 		nbi->asked = ln->ln_asked;
   1605 		nbi->isrouter = ln->ln_router;
   1606 		nbi->expire = ln->ln_expire;
   1607 		splx(s);
   1608 
   1609 		break;
   1610 	}
   1611 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
   1612 		ndif->ifindex = nd6_defifindex;
   1613 		break;
   1614 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
   1615 		return nd6_setdefaultiface(ndif->ifindex);
   1616 	}
   1617 	return error;
   1618 }
   1619 
   1620 void
   1621 nd6_llinfo_release_pkts(struct llinfo_nd6 *ln, struct ifnet *ifp,
   1622     struct rtentry *rt)
   1623 {
   1624 	struct mbuf *m_hold, *m_hold_next;
   1625 
   1626 	for (m_hold = ln->ln_hold, ln->ln_hold = NULL;
   1627 	     m_hold != NULL;
   1628 	     m_hold = m_hold_next) {
   1629 		m_hold_next = m_hold->m_nextpkt;
   1630 		m_hold->m_nextpkt = NULL;
   1631 
   1632 		/*
   1633 		 * we assume ifp is not a p2p here, so
   1634 		 * just set the 2nd argument as the
   1635 		 * 1st one.
   1636 		 */
   1637 		nd6_output(ifp, ifp, m_hold, satocsin6(rt_getkey(rt)), rt);
   1638 	}
   1639 }
   1640 
   1641 /*
   1642  * Create neighbor cache entry and cache link-layer address,
   1643  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
   1644  */
   1645 struct rtentry *
   1646 nd6_cache_lladdr(
   1647     struct ifnet *ifp,
   1648     struct in6_addr *from,
   1649     char *lladdr,
   1650     int lladdrlen,
   1651     int type,	/* ICMP6 type */
   1652     int code	/* type dependent information */
   1653 )
   1654 {
   1655 	struct rtentry *rt = NULL;
   1656 	struct llinfo_nd6 *ln = NULL;
   1657 	int is_newentry;
   1658 	struct sockaddr_dl *sdl = NULL;
   1659 	int do_update;
   1660 	int olladdr;
   1661 	int llchange;
   1662 	int newstate = 0;
   1663 
   1664 	if (ifp == NULL)
   1665 		panic("ifp == NULL in nd6_cache_lladdr");
   1666 	if (from == NULL)
   1667 		panic("from == NULL in nd6_cache_lladdr");
   1668 
   1669 	/* nothing must be updated for unspecified address */
   1670 	if (IN6_IS_ADDR_UNSPECIFIED(from))
   1671 		return NULL;
   1672 
   1673 	/*
   1674 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
   1675 	 * the caller.
   1676 	 *
   1677 	 * XXX If the link does not have link-layer adderss, what should
   1678 	 * we do? (ifp->if_addrlen == 0)
   1679 	 * Spec says nothing in sections for RA, RS and NA.  There's small
   1680 	 * description on it in NS section (RFC 2461 7.2.3).
   1681 	 */
   1682 
   1683 	rt = nd6_lookup(from, 0, ifp);
   1684 	if (rt == NULL) {
   1685 #if 0
   1686 		/* nothing must be done if there's no lladdr */
   1687 		if (!lladdr || !lladdrlen)
   1688 			return NULL;
   1689 #endif
   1690 
   1691 		rt = nd6_lookup(from, 1, ifp);
   1692 		is_newentry = 1;
   1693 	} else {
   1694 		/* do nothing if static ndp is set */
   1695 		if (rt->rt_flags & RTF_STATIC)
   1696 			return NULL;
   1697 		is_newentry = 0;
   1698 	}
   1699 
   1700 	if (rt == NULL)
   1701 		return NULL;
   1702 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
   1703 fail:
   1704 		(void)nd6_free(rt, 0);
   1705 		return NULL;
   1706 	}
   1707 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1708 	if (ln == NULL)
   1709 		goto fail;
   1710 	if (rt->rt_gateway == NULL)
   1711 		goto fail;
   1712 	if (rt->rt_gateway->sa_family != AF_LINK)
   1713 		goto fail;
   1714 	sdl = SDL(rt->rt_gateway);
   1715 
   1716 	olladdr = (sdl->sdl_alen) ? 1 : 0;
   1717 	if (olladdr && lladdr) {
   1718 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
   1719 			llchange = 1;
   1720 		else
   1721 			llchange = 0;
   1722 	} else
   1723 		llchange = 0;
   1724 
   1725 	/*
   1726 	 * newentry olladdr  lladdr  llchange	(*=record)
   1727 	 *	0	n	n	--	(1)
   1728 	 *	0	y	n	--	(2)
   1729 	 *	0	n	y	--	(3) * STALE
   1730 	 *	0	y	y	n	(4) *
   1731 	 *	0	y	y	y	(5) * STALE
   1732 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
   1733 	 *	1	--	y	--	(7) * STALE
   1734 	 */
   1735 
   1736 	if (lladdr) {		/* (3-5) and (7) */
   1737 		/*
   1738 		 * Record source link-layer address
   1739 		 * XXX is it dependent to ifp->if_type?
   1740 		 */
   1741 		sdl->sdl_alen = ifp->if_addrlen;
   1742 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
   1743 	}
   1744 
   1745 	if (!is_newentry) {
   1746 		if ((!olladdr && lladdr) ||		/* (3) */
   1747 		    (olladdr && lladdr && llchange)) {	/* (5) */
   1748 			do_update = 1;
   1749 			newstate = ND6_LLINFO_STALE;
   1750 		} else					/* (1-2,4) */
   1751 			do_update = 0;
   1752 	} else {
   1753 		do_update = 1;
   1754 		if (lladdr == NULL)			/* (6) */
   1755 			newstate = ND6_LLINFO_NOSTATE;
   1756 		else					/* (7) */
   1757 			newstate = ND6_LLINFO_STALE;
   1758 	}
   1759 
   1760 	if (do_update) {
   1761 		/*
   1762 		 * Update the state of the neighbor cache.
   1763 		 */
   1764 		ln->ln_state = newstate;
   1765 
   1766 		if (ln->ln_state == ND6_LLINFO_STALE) {
   1767 			/*
   1768 			 * XXX: since nd6_output() below will cause
   1769 			 * state tansition to DELAY and reset the timer,
   1770 			 * we must set the timer now, although it is actually
   1771 			 * meaningless.
   1772 			 */
   1773 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
   1774 
   1775 			nd6_llinfo_release_pkts(ln, ifp, rt);
   1776 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
   1777 			/* probe right away */
   1778 			nd6_llinfo_settimer((void *)ln, 0);
   1779 		}
   1780 	}
   1781 
   1782 	/*
   1783 	 * ICMP6 type dependent behavior.
   1784 	 *
   1785 	 * NS: clear IsRouter if new entry
   1786 	 * RS: clear IsRouter
   1787 	 * RA: set IsRouter if there's lladdr
   1788 	 * redir: clear IsRouter if new entry
   1789 	 *
   1790 	 * RA case, (1):
   1791 	 * The spec says that we must set IsRouter in the following cases:
   1792 	 * - If lladdr exist, set IsRouter.  This means (1-5).
   1793 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
   1794 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
   1795 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
   1796 	 * neighbor cache, this is similar to (6).
   1797 	 * This case is rare but we figured that we MUST NOT set IsRouter.
   1798 	 *
   1799 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
   1800 	 *							D R
   1801 	 *	0	n	n	--	(1)	c   ?     s
   1802 	 *	0	y	n	--	(2)	c   s     s
   1803 	 *	0	n	y	--	(3)	c   s     s
   1804 	 *	0	y	y	n	(4)	c   s     s
   1805 	 *	0	y	y	y	(5)	c   s     s
   1806 	 *	1	--	n	--	(6) c	c 	c s
   1807 	 *	1	--	y	--	(7) c	c   s	c s
   1808 	 *
   1809 	 *					(c=clear s=set)
   1810 	 */
   1811 	switch (type & 0xff) {
   1812 	case ND_NEIGHBOR_SOLICIT:
   1813 		/*
   1814 		 * New entry must have is_router flag cleared.
   1815 		 */
   1816 		if (is_newentry)	/* (6-7) */
   1817 			ln->ln_router = 0;
   1818 		break;
   1819 	case ND_REDIRECT:
   1820 		/*
   1821 		 * If the icmp is a redirect to a better router, always set the
   1822 		 * is_router flag.  Otherwise, if the entry is newly created,
   1823 		 * clear the flag.  [RFC 2461, sec 8.3]
   1824 		 */
   1825 		if (code == ND_REDIRECT_ROUTER)
   1826 			ln->ln_router = 1;
   1827 		else if (is_newentry) /* (6-7) */
   1828 			ln->ln_router = 0;
   1829 		break;
   1830 	case ND_ROUTER_SOLICIT:
   1831 		/*
   1832 		 * is_router flag must always be cleared.
   1833 		 */
   1834 		ln->ln_router = 0;
   1835 		break;
   1836 	case ND_ROUTER_ADVERT:
   1837 		/*
   1838 		 * Mark an entry with lladdr as a router.
   1839 		 */
   1840 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
   1841 		    (is_newentry && lladdr)) {			/* (7) */
   1842 			ln->ln_router = 1;
   1843 		}
   1844 		break;
   1845 	}
   1846 
   1847 	/*
   1848 	 * When the link-layer address of a router changes, select the
   1849 	 * best router again.  In particular, when the neighbor entry is newly
   1850 	 * created, it might affect the selection policy.
   1851 	 * Question: can we restrict the first condition to the "is_newentry"
   1852 	 * case?
   1853 	 * XXX: when we hear an RA from a new router with the link-layer
   1854 	 * address option, defrouter_select() is called twice, since
   1855 	 * defrtrlist_update called the function as well.  However, I believe
   1856 	 * we can compromise the overhead, since it only happens the first
   1857 	 * time.
   1858 	 * XXX: although defrouter_select() should not have a bad effect
   1859 	 * for those are not autoconfigured hosts, we explicitly avoid such
   1860 	 * cases for safety.
   1861 	 */
   1862 	if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
   1863 		defrouter_select();
   1864 
   1865 	return rt;
   1866 }
   1867 
   1868 static void
   1869 nd6_slowtimo(void *ignored_arg)
   1870 {
   1871 	int s = splsoftnet();
   1872 	struct nd_ifinfo *nd6if;
   1873 	struct ifnet *ifp;
   1874 
   1875 	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
   1876 	    nd6_slowtimo, NULL);
   1877 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
   1878 		nd6if = ND_IFINFO(ifp);
   1879 		if (nd6if->basereachable && /* already initialized */
   1880 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
   1881 			/*
   1882 			 * Since reachable time rarely changes by router
   1883 			 * advertisements, we SHOULD insure that a new random
   1884 			 * value gets recomputed at least once every few hours.
   1885 			 * (RFC 2461, 6.3.4)
   1886 			 */
   1887 			nd6if->recalctm = nd6_recalc_reachtm_interval;
   1888 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
   1889 		}
   1890 	}
   1891 	splx(s);
   1892 }
   1893 
   1894 #define senderr(e) { error = (e); goto bad;}
   1895 int
   1896 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
   1897     const struct sockaddr_in6 *dst, struct rtentry *rt0)
   1898 {
   1899 	struct mbuf *m = m0;
   1900 	struct rtentry *rt = rt0;
   1901 	struct sockaddr_in6 *gw6 = NULL;
   1902 	struct llinfo_nd6 *ln = NULL;
   1903 	int error = 0;
   1904 
   1905 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
   1906 		goto sendpkt;
   1907 
   1908 	if (nd6_need_cache(ifp) == 0)
   1909 		goto sendpkt;
   1910 
   1911 	/*
   1912 	 * next hop determination.  This routine is derived from ether_output.
   1913 	 */
   1914 	if (rt) {
   1915 		if ((rt->rt_flags & RTF_UP) == 0) {
   1916 			if ((rt0 = rt = rtalloc1(sin6tocsa(dst), 1)) != NULL) {
   1917 				rt->rt_refcnt--;
   1918 				if (rt->rt_ifp != ifp)
   1919 					senderr(EHOSTUNREACH);
   1920 			} else
   1921 				senderr(EHOSTUNREACH);
   1922 		}
   1923 
   1924 		if (rt->rt_flags & RTF_GATEWAY) {
   1925 			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
   1926 
   1927 			/*
   1928 			 * We skip link-layer address resolution and NUD
   1929 			 * if the gateway is not a neighbor from ND point
   1930 			 * of view, regardless of the value of nd_ifinfo.flags.
   1931 			 * The second condition is a bit tricky; we skip
   1932 			 * if the gateway is our own address, which is
   1933 			 * sometimes used to install a route to a p2p link.
   1934 			 */
   1935 			if (!nd6_is_addr_neighbor(gw6, ifp) ||
   1936 			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
   1937 				/*
   1938 				 * We allow this kind of tricky route only
   1939 				 * when the outgoing interface is p2p.
   1940 				 * XXX: we may need a more generic rule here.
   1941 				 */
   1942 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
   1943 					senderr(EHOSTUNREACH);
   1944 
   1945 				goto sendpkt;
   1946 			}
   1947 
   1948 			if (rt->rt_gwroute == 0)
   1949 				goto lookup;
   1950 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
   1951 				rtfree(rt); rt = rt0;
   1952 			lookup:
   1953 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
   1954 				if ((rt = rt->rt_gwroute) == 0)
   1955 					senderr(EHOSTUNREACH);
   1956 				/* the "G" test below also prevents rt == rt0 */
   1957 				if ((rt->rt_flags & RTF_GATEWAY) ||
   1958 				    (rt->rt_ifp != ifp)) {
   1959 					rt->rt_refcnt--;
   1960 					rt0->rt_gwroute = 0;
   1961 					senderr(EHOSTUNREACH);
   1962 				}
   1963 			}
   1964 		}
   1965 	}
   1966 
   1967 	/*
   1968 	 * Address resolution or Neighbor Unreachability Detection
   1969 	 * for the next hop.
   1970 	 * At this point, the destination of the packet must be a unicast
   1971 	 * or an anycast address(i.e. not a multicast).
   1972 	 */
   1973 
   1974 	/* Look up the neighbor cache for the nexthop */
   1975 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
   1976 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1977 	else {
   1978 		/*
   1979 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
   1980 		 * the condition below is not very efficient.  But we believe
   1981 		 * it is tolerable, because this should be a rare case.
   1982 		 */
   1983 		if (nd6_is_addr_neighbor(dst, ifp) &&
   1984 		    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
   1985 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1986 	}
   1987 	if (ln == NULL || rt == NULL) {
   1988 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
   1989 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
   1990 			log(LOG_DEBUG,
   1991 			    "nd6_output: can't allocate llinfo for %s "
   1992 			    "(ln=%p, rt=%p)\n",
   1993 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
   1994 			senderr(EIO);	/* XXX: good error? */
   1995 		}
   1996 
   1997 		goto sendpkt;	/* send anyway */
   1998 	}
   1999 
   2000 	/* We don't have to do link-layer address resolution on a p2p link. */
   2001 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
   2002 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
   2003 		ln->ln_state = ND6_LLINFO_STALE;
   2004 		nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
   2005 	}
   2006 
   2007 	/*
   2008 	 * The first time we send a packet to a neighbor whose entry is
   2009 	 * STALE, we have to change the state to DELAY and a sets a timer to
   2010 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
   2011 	 * neighbor unreachability detection on expiration.
   2012 	 * (RFC 2461 7.3.3)
   2013 	 */
   2014 	if (ln->ln_state == ND6_LLINFO_STALE) {
   2015 		ln->ln_asked = 0;
   2016 		ln->ln_state = ND6_LLINFO_DELAY;
   2017 		nd6_llinfo_settimer(ln, (long)nd6_delay * hz);
   2018 	}
   2019 
   2020 	/*
   2021 	 * If the neighbor cache entry has a state other than INCOMPLETE
   2022 	 * (i.e. its link-layer address is already resolved), just
   2023 	 * send the packet.
   2024 	 */
   2025 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
   2026 		goto sendpkt;
   2027 
   2028 	/*
   2029 	 * There is a neighbor cache entry, but no ethernet address
   2030 	 * response yet.  Append this latest packet to the end of the
   2031 	 * packet queue in the mbuf, unless the number of the packet
   2032 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
   2033 	 * the oldest packet in the queue will be removed.
   2034 	 */
   2035 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
   2036 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
   2037 	if (ln->ln_hold) {
   2038 		struct mbuf *m_hold;
   2039 		int i;
   2040 
   2041 		i = 0;
   2042 		for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) {
   2043 			i++;
   2044 			if (m_hold->m_nextpkt == NULL) {
   2045 				m_hold->m_nextpkt = m;
   2046 				break;
   2047 			}
   2048 		}
   2049 		while (i >= nd6_maxqueuelen) {
   2050 			m_hold = ln->ln_hold;
   2051 			ln->ln_hold = ln->ln_hold->m_nextpkt;
   2052 			m_freem(m_hold);
   2053 			i--;
   2054 		}
   2055 	} else {
   2056 		ln->ln_hold = m;
   2057 	}
   2058 
   2059 	/*
   2060 	 * If there has been no NS for the neighbor after entering the
   2061 	 * INCOMPLETE state, send the first solicitation.
   2062 	 */
   2063 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
   2064 		ln->ln_asked++;
   2065 		nd6_llinfo_settimer(ln,
   2066 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
   2067 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
   2068 	}
   2069 	return 0;
   2070 
   2071   sendpkt:
   2072 	/* discard the packet if IPv6 operation is disabled on the interface */
   2073 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
   2074 		error = ENETDOWN; /* better error? */
   2075 		goto bad;
   2076 	}
   2077 
   2078 #ifdef IPSEC
   2079 	/* clean ipsec history once it goes out of the node */
   2080 	ipsec_delaux(m);
   2081 #endif
   2082 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
   2083 		return (*ifp->if_output)(origifp, m, sin6tocsa(dst), rt);
   2084 	}
   2085 	return (*ifp->if_output)(ifp, m, sin6tocsa(dst), rt);
   2086 
   2087   bad:
   2088 	if (m != NULL)
   2089 		m_freem(m);
   2090 	return error;
   2091 }
   2092 #undef senderr
   2093 
   2094 int
   2095 nd6_need_cache(struct ifnet *ifp)
   2096 {
   2097 	/*
   2098 	 * XXX: we currently do not make neighbor cache on any interface
   2099 	 * other than ARCnet, Ethernet, FDDI and GIF.
   2100 	 *
   2101 	 * RFC2893 says:
   2102 	 * - unidirectional tunnels needs no ND
   2103 	 */
   2104 	switch (ifp->if_type) {
   2105 	case IFT_ARCNET:
   2106 	case IFT_ETHER:
   2107 	case IFT_FDDI:
   2108 	case IFT_IEEE1394:
   2109 	case IFT_CARP:
   2110 	case IFT_GIF:		/* XXX need more cases? */
   2111 	case IFT_PPP:
   2112 	case IFT_TUNNEL:
   2113 		return 1;
   2114 	default:
   2115 		return 0;
   2116 	}
   2117 }
   2118 
   2119 int
   2120 nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt,
   2121     struct mbuf *m, const struct sockaddr *dst, u_char *lldst,
   2122     size_t dstsize)
   2123 {
   2124 	const struct sockaddr_dl *sdl;
   2125 
   2126 	if (m->m_flags & M_MCAST) {
   2127 		switch (ifp->if_type) {
   2128 		case IFT_ETHER:
   2129 		case IFT_FDDI:
   2130 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, lldst);
   2131 			return 1;
   2132 		case IFT_IEEE1394:
   2133 			bcopy(ifp->if_broadcastaddr, lldst, ifp->if_addrlen);
   2134 			return 1;
   2135 		case IFT_ARCNET:
   2136 			*lldst = 0;
   2137 			return 1;
   2138 		default:
   2139 			m_freem(m);
   2140 			return 0;
   2141 		}
   2142 	}
   2143 
   2144 	if (rt == NULL) {
   2145 		/* this could happen, if we could not allocate memory */
   2146 		m_freem(m);
   2147 		return 0;
   2148 	}
   2149 	if (rt->rt_gateway->sa_family != AF_LINK) {
   2150 		printf("%s: something odd happens\n", __func__);
   2151 		m_freem(m);
   2152 		return 0;
   2153 	}
   2154 	sdl = SDL(rt->rt_gateway);
   2155 	if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) {
   2156 		/* this should be impossible, but we bark here for debugging */
   2157 		printf("%s: sdl_alen == 0, dst=%s, if=%s\n", __func__,
   2158 		    ip6_sprintf(&SIN6(dst)->sin6_addr), if_name(ifp));
   2159 		m_freem(m);
   2160 		return 0;
   2161 	}
   2162 
   2163 	memcpy(lldst, CLLADDR(sdl), MIN(dstsize, sdl->sdl_alen));
   2164 	return 1;
   2165 }
   2166 
   2167 static void
   2168 clear_llinfo_pqueue(struct llinfo_nd6 *ln)
   2169 {
   2170 	struct mbuf *m_hold, *m_hold_next;
   2171 
   2172 	for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) {
   2173 		m_hold_next = m_hold->m_nextpkt;
   2174 		m_hold->m_nextpkt = NULL;
   2175 		m_freem(m_hold);
   2176 	}
   2177 
   2178 	ln->ln_hold = NULL;
   2179 	return;
   2180 }
   2181 
   2182 int
   2183 nd6_sysctl(
   2184     int name,
   2185     void *oldp,	/* syscall arg, need copyout */
   2186     size_t *oldlenp,
   2187     void *newp,	/* syscall arg, need copyin */
   2188     size_t newlen
   2189 )
   2190 {
   2191 	void *p;
   2192 	size_t ol;
   2193 	int error;
   2194 
   2195 	error = 0;
   2196 
   2197 	if (newp)
   2198 		return EPERM;
   2199 	if (oldp && !oldlenp)
   2200 		return EINVAL;
   2201 	ol = oldlenp ? *oldlenp : 0;
   2202 
   2203 	if (oldp) {
   2204 		p = malloc(*oldlenp, M_TEMP, M_WAITOK);
   2205 		if (p == NULL)
   2206 			return ENOMEM;
   2207 	} else
   2208 		p = NULL;
   2209 	switch (name) {
   2210 	case ICMPV6CTL_ND6_DRLIST:
   2211 		error = fill_drlist(p, oldlenp, ol);
   2212 		if (!error && p != NULL && oldp != NULL)
   2213 			error = copyout(p, oldp, *oldlenp);
   2214 		break;
   2215 
   2216 	case ICMPV6CTL_ND6_PRLIST:
   2217 		error = fill_prlist(p, oldlenp, ol);
   2218 		if (!error && p != NULL && oldp != NULL)
   2219 			error = copyout(p, oldp, *oldlenp);
   2220 		break;
   2221 
   2222 	case ICMPV6CTL_ND6_MAXQLEN:
   2223 		break;
   2224 
   2225 	default:
   2226 		error = ENOPROTOOPT;
   2227 		break;
   2228 	}
   2229 	if (p)
   2230 		free(p, M_TEMP);
   2231 
   2232 	return error;
   2233 }
   2234 
   2235 static int
   2236 fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
   2237 {
   2238 	int error = 0, s;
   2239 	struct in6_defrouter *d = NULL, *de = NULL;
   2240 	struct nd_defrouter *dr;
   2241 	size_t l;
   2242 
   2243 	s = splsoftnet();
   2244 
   2245 	if (oldp) {
   2246 		d = (struct in6_defrouter *)oldp;
   2247 		de = (struct in6_defrouter *)((char *)oldp + *oldlenp);
   2248 	}
   2249 	l = 0;
   2250 
   2251 	TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
   2252 
   2253 		if (oldp && d + 1 <= de) {
   2254 			bzero(d, sizeof(*d));
   2255 			d->rtaddr.sin6_family = AF_INET6;
   2256 			d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
   2257 			d->rtaddr.sin6_addr = dr->rtaddr;
   2258 			if (sa6_recoverscope(&d->rtaddr)) {
   2259 				log(LOG_ERR,
   2260 				    "scope error in router list (%s)\n",
   2261 				    ip6_sprintf(&d->rtaddr.sin6_addr));
   2262 				/* XXX: press on... */
   2263 			}
   2264 			d->flags = dr->flags;
   2265 			d->rtlifetime = dr->rtlifetime;
   2266 			d->expire = dr->expire;
   2267 			d->if_index = dr->ifp->if_index;
   2268 		}
   2269 
   2270 		l += sizeof(*d);
   2271 		if (d)
   2272 			d++;
   2273 	}
   2274 
   2275 	if (oldp) {
   2276 		if (l > ol)
   2277 			error = ENOMEM;
   2278 	}
   2279 	if (oldlenp)
   2280 		*oldlenp = l;	/* (void *)d - (void *)oldp */
   2281 
   2282 	splx(s);
   2283 
   2284 	return error;
   2285 }
   2286 
   2287 static int
   2288 fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
   2289 {
   2290 	int error = 0, s;
   2291 	struct nd_prefix *pr;
   2292 	struct in6_prefix *p = NULL;
   2293 	struct in6_prefix *pe = NULL;
   2294 	size_t l;
   2295 
   2296 	s = splsoftnet();
   2297 
   2298 	if (oldp) {
   2299 		p = (struct in6_prefix *)oldp;
   2300 		pe = (struct in6_prefix *)((char *)oldp + *oldlenp);
   2301 	}
   2302 	l = 0;
   2303 
   2304 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
   2305 		u_short advrtrs;
   2306 		size_t advance;
   2307 		struct sockaddr_in6 *sin6;
   2308 		struct sockaddr_in6 *s6;
   2309 		struct nd_pfxrouter *pfr;
   2310 
   2311 		if (oldp && p + 1 <= pe)
   2312 		{
   2313 			bzero(p, sizeof(*p));
   2314 			sin6 = (struct sockaddr_in6 *)(p + 1);
   2315 
   2316 			p->prefix = pr->ndpr_prefix;
   2317 			if (sa6_recoverscope(&p->prefix)) {
   2318 				log(LOG_ERR,
   2319 				    "scope error in prefix list (%s)\n",
   2320 				    ip6_sprintf(&p->prefix.sin6_addr));
   2321 				/* XXX: press on... */
   2322 			}
   2323 			p->raflags = pr->ndpr_raf;
   2324 			p->prefixlen = pr->ndpr_plen;
   2325 			p->vltime = pr->ndpr_vltime;
   2326 			p->pltime = pr->ndpr_pltime;
   2327 			p->if_index = pr->ndpr_ifp->if_index;
   2328 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
   2329 				p->expire = 0;
   2330 			else {
   2331 				time_t maxexpire;
   2332 
   2333 				/* XXX: we assume time_t is signed. */
   2334 				maxexpire = (-1) &
   2335 				    ~((time_t)1 <<
   2336 				    ((sizeof(maxexpire) * 8) - 1));
   2337 				if (pr->ndpr_vltime <
   2338 				    maxexpire - pr->ndpr_lastupdate) {
   2339 					p->expire = pr->ndpr_lastupdate +
   2340 						pr->ndpr_vltime;
   2341 				} else
   2342 					p->expire = maxexpire;
   2343 			}
   2344 			p->refcnt = pr->ndpr_refcnt;
   2345 			p->flags = pr->ndpr_stateflags;
   2346 			p->origin = PR_ORIG_RA;
   2347 			advrtrs = 0;
   2348 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
   2349 				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
   2350 					advrtrs++;
   2351 					continue;
   2352 				}
   2353 				s6 = &sin6[advrtrs];
   2354 				s6->sin6_family = AF_INET6;
   2355 				s6->sin6_len = sizeof(struct sockaddr_in6);
   2356 				s6->sin6_addr = pfr->router->rtaddr;
   2357 				s6->sin6_scope_id = 0;
   2358 				if (sa6_recoverscope(s6)) {
   2359 					log(LOG_ERR,
   2360 					    "scope error in "
   2361 					    "prefix list (%s)\n",
   2362 					    ip6_sprintf(&pfr->router->rtaddr));
   2363 				}
   2364 				advrtrs++;
   2365 			}
   2366 			p->advrtrs = advrtrs;
   2367 		}
   2368 		else {
   2369 			advrtrs = 0;
   2370 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
   2371 				advrtrs++;
   2372 		}
   2373 
   2374 		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
   2375 		l += advance;
   2376 		if (p)
   2377 			p = (struct in6_prefix *)((char *)p + advance);
   2378 	}
   2379 
   2380 	if (oldp) {
   2381 		*oldlenp = l;	/* (void *)d - (void *)oldp */
   2382 		if (l > ol)
   2383 			error = ENOMEM;
   2384 	} else
   2385 		*oldlenp = l;
   2386 
   2387 	splx(s);
   2388 
   2389 	return error;
   2390 }
   2391