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