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