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