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