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