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