Home | History | Annotate | Line # | Download | only in netinet6
nd6.c revision 1.39
      1 /*	$NetBSD: nd6.c,v 1.39 2001/02/21 16:28:43 itojun Exp $	*/
      2 /*	$KAME: nd6.c,v 1.118 2001/02/08 12:14:33 itojun Exp $	*/
      3 
      4 /*
      5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. Neither the name of the project nor the names of its contributors
     17  *    may be used to endorse or promote products derived from this software
     18  *    without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  * SUCH DAMAGE.
     31  */
     32 
     33 /*
     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 	struct llinfo_nd6 *ln;
    393 	struct nd_defrouter *dr;
    394 	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 				next = 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 				next = nd6_free(rt);
    494 			break;
    495 		case ND6_LLINFO_WAITDELETE:
    496 			next = nd6_free(rt);
    497 			break;
    498 		}
    499 		ln = next;
    500 	}
    501 
    502 	/* expire */
    503 	dr = TAILQ_FIRST(&nd_defrouter);
    504 	while (dr) {
    505 		if (dr->expire && dr->expire < time_second) {
    506 			struct nd_defrouter *t;
    507 			t = TAILQ_NEXT(dr, dr_entry);
    508 			defrtrlist_del(dr);
    509 			dr = t;
    510 		} else {
    511 			dr = TAILQ_NEXT(dr, dr_entry);
    512 		}
    513 	}
    514 	pr = nd_prefix.lh_first;
    515 	while (pr) {
    516 		struct in6_ifaddr *ia6;
    517 		struct in6_addrlifetime *lt6;
    518 
    519 		if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
    520 			ia6 = NULL;
    521 		else
    522 			ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
    523 
    524 		if (ia6) {
    525 			/* check address lifetime */
    526 			lt6 = &ia6->ia6_lifetime;
    527 			if (lt6->ia6t_preferred && lt6->ia6t_preferred < time_second)
    528 				ia6->ia6_flags |= IN6_IFF_DEPRECATED;
    529 			if (lt6->ia6t_expire && lt6->ia6t_expire < time_second) {
    530 				if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
    531 					in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
    532 				/* xxx ND_OPT_PI_FLAG_ONLINK processing */
    533 			}
    534 		}
    535 
    536 		/*
    537 		 * check prefix lifetime.
    538 		 * since pltime is just for autoconf, pltime processing for
    539 		 * prefix is not necessary.
    540 		 *
    541 		 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
    542 		 * can use the old prefix information to validate the
    543 		 * next prefix information to come.  See prelist_update()
    544 		 * for actual validation.
    545 		 */
    546 		if (pr->ndpr_expire
    547 		 && pr->ndpr_expire + NDPR_KEEP_EXPIRED < time_second) {
    548 			struct nd_prefix *t;
    549 			t = pr->ndpr_next;
    550 
    551 			/*
    552 			 * address expiration and prefix expiration are
    553 			 * separate.  NEVER perform in6_ifdel here.
    554 			 */
    555 
    556 			prelist_remove(pr);
    557 			pr = t;
    558 		} else
    559 			pr = pr->ndpr_next;
    560 	}
    561 	splx(s);
    562 }
    563 
    564 /*
    565  * Nuke neighbor cache/prefix/default router management table, right before
    566  * ifp goes away.
    567  */
    568 void
    569 nd6_purge(ifp)
    570 	struct ifnet *ifp;
    571 {
    572 	struct llinfo_nd6 *ln, *nln;
    573 	struct nd_defrouter *dr, *ndr, drany;
    574 	struct nd_prefix *pr, *npr;
    575 
    576 	/* Nuke default router list entries toward ifp */
    577 	if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
    578 		/*
    579 		 * The first entry of the list may be stored in
    580 		 * the routing table, so we'll delete it later.
    581 		 */
    582 		for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) {
    583 			ndr = TAILQ_NEXT(dr, dr_entry);
    584 			if (dr->ifp == ifp)
    585 				defrtrlist_del(dr);
    586 		}
    587 		dr = TAILQ_FIRST(&nd_defrouter);
    588 		if (dr->ifp == ifp)
    589 			defrtrlist_del(dr);
    590 	}
    591 
    592 	/* Nuke prefix list entries toward ifp */
    593 	for (pr = nd_prefix.lh_first; pr; pr = npr) {
    594 		npr = pr->ndpr_next;
    595 		if (pr->ndpr_ifp == ifp) {
    596 			if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
    597 				in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
    598 			prelist_remove(pr);
    599 		}
    600 	}
    601 
    602 	/* cancel default outgoing interface setting */
    603 	if (nd6_defifindex == ifp->if_index)
    604 		nd6_setdefaultiface(0);
    605 
    606 	/* refresh default router list */
    607 	bzero(&drany, sizeof(drany));
    608 	defrouter_delreq(&drany, 0);
    609 	defrouter_select();
    610 
    611 	/*
    612 	 * Nuke neighbor cache entries for the ifp.
    613 	 * Note that rt->rt_ifp may not be the same as ifp,
    614 	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
    615 	 * nd6_rtrequest(), and ip6_input().
    616 	 */
    617 	ln = llinfo_nd6.ln_next;
    618 	while (ln && ln != &llinfo_nd6) {
    619 		struct rtentry *rt;
    620 		struct sockaddr_dl *sdl;
    621 
    622 		nln = ln->ln_next;
    623 		rt = ln->ln_rt;
    624 		if (rt && rt->rt_gateway &&
    625 		    rt->rt_gateway->sa_family == AF_LINK) {
    626 			sdl = (struct sockaddr_dl *)rt->rt_gateway;
    627 			if (sdl->sdl_index == ifp->if_index)
    628 				nln = nd6_free(rt);
    629 		}
    630 		ln = nln;
    631 	}
    632 
    633 	/*
    634 	 * Neighbor cache entry for interface route will be retained
    635 	 * with ND6_LLINFO_WAITDELETE state, by nd6_free().  Nuke it.
    636 	 */
    637 	ln = llinfo_nd6.ln_next;
    638 	while (ln && ln != &llinfo_nd6) {
    639 		struct rtentry *rt;
    640 		struct sockaddr_dl *sdl;
    641 
    642 		nln = ln->ln_next;
    643 		rt = ln->ln_rt;
    644 		if (rt && rt->rt_gateway &&
    645 		    rt->rt_gateway->sa_family == AF_LINK) {
    646 			sdl = (struct sockaddr_dl *)rt->rt_gateway;
    647 			if (sdl->sdl_index == ifp->if_index) {
    648 				rtrequest(RTM_DELETE, rt_key(rt),
    649 				    (struct sockaddr *)0, rt_mask(rt), 0,
    650 				    (struct rtentry **)0);
    651 			}
    652 		}
    653 		ln = nln;
    654 	}
    655 }
    656 
    657 struct rtentry *
    658 nd6_lookup(addr6, create, ifp)
    659 	struct in6_addr *addr6;
    660 	int create;
    661 	struct ifnet *ifp;
    662 {
    663 	struct rtentry *rt;
    664 	struct sockaddr_in6 sin6;
    665 
    666 	bzero(&sin6, sizeof(sin6));
    667 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    668 	sin6.sin6_family = AF_INET6;
    669 	sin6.sin6_addr = *addr6;
    670 	rt = rtalloc1((struct sockaddr *)&sin6, create);
    671 	if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
    672 		/*
    673 		 * This is the case for the default route.
    674 		 * If we want to create a neighbor cache for the address, we
    675 		 * should free the route for the destination and allocate an
    676 		 * interface route.
    677 		 */
    678 		if (create) {
    679 			RTFREE(rt);
    680 			rt = 0;
    681 		}
    682 	}
    683 	if (!rt) {
    684 		if (create && ifp) {
    685 			int e;
    686 
    687 			/*
    688 			 * If no route is available and create is set,
    689 			 * we allocate a host route for the destination
    690 			 * and treat it like an interface route.
    691 			 * This hack is necessary for a neighbor which can't
    692 			 * be covered by our own prefix.
    693 			 */
    694 			struct ifaddr *ifa =
    695 				ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
    696 			if (ifa == NULL)
    697 				return(NULL);
    698 
    699 			/*
    700 			 * Create a new route. RTF_LLINFO is necessary
    701 			 * to create a Neighbor Cache entry for the
    702 			 * destination in nd6_rtrequest which will be
    703 			 * called in rtequest via ifa->ifa_rtrequest.
    704 			 */
    705 			if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
    706 					   ifa->ifa_addr,
    707 					   (struct sockaddr *)&all1_sa,
    708 					   (ifa->ifa_flags |
    709 					    RTF_HOST | RTF_LLINFO) &
    710 					   ~RTF_CLONING,
    711 					   &rt)) != 0)
    712 				log(LOG_ERR,
    713 				    "nd6_lookup: failed to add route for a "
    714 				    "neighbor(%s), errno=%d\n",
    715 				    ip6_sprintf(addr6), e);
    716 			if (rt == NULL)
    717 				return(NULL);
    718 			if (rt->rt_llinfo) {
    719 				struct llinfo_nd6 *ln =
    720 					(struct llinfo_nd6 *)rt->rt_llinfo;
    721 				ln->ln_state = ND6_LLINFO_NOSTATE;
    722 			}
    723 		} else
    724 			return(NULL);
    725 	}
    726 	rt->rt_refcnt--;
    727 	/*
    728 	 * Validation for the entry.
    729 	 * XXX: we can't use rt->rt_ifp to check for the interface, since
    730 	 *      it might be the loopback interface if the entry is for our
    731 	 *      own address on a non-loopback interface. Instead, we should
    732 	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL interface.
    733 	 */
    734 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
    735 	    rt->rt_gateway->sa_family != AF_LINK ||
    736 	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
    737 		if (create) {
    738 			log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n",
    739 			    ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec");
    740 			/* xxx more logs... kazu */
    741 		}
    742 		return(0);
    743 	}
    744 	return(rt);
    745 }
    746 
    747 /*
    748  * Detect if a given IPv6 address identifies a neighbor on a given link.
    749  * XXX: should take care of the destination of a p2p link?
    750  */
    751 int
    752 nd6_is_addr_neighbor(addr, ifp)
    753 	struct sockaddr_in6 *addr;
    754 	struct ifnet *ifp;
    755 {
    756 	struct ifaddr *ifa;
    757 	int i;
    758 
    759 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
    760 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
    761 
    762 	/*
    763 	 * A link-local address is always a neighbor.
    764 	 * XXX: we should use the sin6_scope_id field rather than the embedded
    765 	 * interface index.
    766 	 */
    767 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
    768 	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
    769 		return(1);
    770 
    771 	/*
    772 	 * If the address matches one of our addresses,
    773 	 * it should be a neighbor.
    774 	 */
    775 	for (ifa = ifp->if_addrlist.tqh_first;
    776 	     ifa;
    777 	     ifa = ifa->ifa_list.tqe_next)
    778 	{
    779 		if (ifa->ifa_addr->sa_family != AF_INET6)
    780 			next: continue;
    781 
    782 		for (i = 0; i < 4; i++) {
    783 			if ((IFADDR6(ifa).s6_addr32[i] ^
    784 			     addr->sin6_addr.s6_addr32[i]) &
    785 			    IFMASK6(ifa).s6_addr32[i])
    786 				goto next;
    787 		}
    788 		return(1);
    789 	}
    790 
    791 	/*
    792 	 * Even if the address matches none of our addresses, it might be
    793 	 * in the neighbor cache.
    794 	 */
    795 	if (nd6_lookup(&addr->sin6_addr, 0, ifp))
    796 		return(1);
    797 
    798 	return(0);
    799 #undef IFADDR6
    800 #undef IFMASK6
    801 }
    802 
    803 /*
    804  * Free an nd6 llinfo entry.
    805  */
    806 struct llinfo_nd6 *
    807 nd6_free(rt)
    808 	struct rtentry *rt;
    809 {
    810 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
    811 	struct sockaddr_dl *sdl;
    812 	struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
    813 	struct nd_defrouter *dr;
    814 
    815 	/*
    816 	 * Clear all destination cache entries for the neighbor.
    817 	 * XXX: is it better to restrict this to hosts?
    818 	 */
    819 	pfctlinput(PRC_HOSTDEAD, rt_key(rt));
    820 
    821 	if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
    822 		int s;
    823 		s = splsoftnet();
    824 		dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
    825 				      rt->rt_ifp);
    826 		if (ln->ln_router || dr) {
    827 			/*
    828 			 * rt6_flush must be called whether or not the neighbor
    829 			 * is in the Default Router List.
    830 			 * See a corresponding comment in nd6_na_input().
    831 			 */
    832 			rt6_flush(&in6, rt->rt_ifp);
    833 		}
    834 
    835 		if (dr) {
    836 			/*
    837 			 * Unreachablity of a router might affect the default
    838 			 * router selection and on-link detection of advertised
    839 			 * prefixes.
    840 			 */
    841 
    842 			/*
    843 			 * Temporarily fake the state to choose a new default
    844 			 * router and to perform on-link determination of
    845 			 * prefixes coreectly.
    846 			 * Below the state will be set correctly,
    847 			 * or the entry itself will be deleted.
    848 			 */
    849 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
    850 
    851 			if (dr == TAILQ_FIRST(&nd_defrouter)) {
    852 				/*
    853 				 * It is used as the current default router,
    854 				 * so we have to move it to the end of the
    855 				 * list and choose a new one.
    856 				 * XXX: it is not very efficient if this is
    857 				 *      the only router.
    858 				 */
    859 				TAILQ_REMOVE(&nd_defrouter, dr, dr_entry);
    860 				TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry);
    861 
    862 				defrouter_select();
    863 			}
    864 			pfxlist_onlink_check();
    865 		}
    866 		splx(s);
    867 	}
    868 
    869 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
    870 	    sdl->sdl_family == AF_LINK) {
    871 		sdl->sdl_alen = 0;
    872 		ln->ln_state = ND6_LLINFO_WAITDELETE;
    873 		ln->ln_asked = 0;
    874 		rt->rt_flags &= ~RTF_REJECT;
    875 		return ln->ln_next;
    876 	}
    877 
    878 	/*
    879 	 * Before deleting the entry, remember the next entry as the
    880 	 * return value.  We need this because pfxlist_onlink_check() above
    881 	 * might have freed other entries (particularly the old next entry) as
    882 	 * a side effect (XXX).
    883 	 */
    884 	next = ln->ln_next;
    885 
    886 	/*
    887 	 * Detach the route from the routing tree and the list of neighbor
    888 	 * caches, and disable the route entry not to be used in already
    889 	 * cached routes.
    890 	 */
    891 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0,
    892 		  rt_mask(rt), 0, (struct rtentry **)0);
    893 
    894 	return next;
    895 }
    896 
    897 /*
    898  * Upper-layer reachability hint for Neighbor Unreachability Detection.
    899  *
    900  * XXX cost-effective metods?
    901  */
    902 void
    903 nd6_nud_hint(rt, dst6, force)
    904 	struct rtentry *rt;
    905 	struct in6_addr *dst6;
    906 	int force;
    907 {
    908 	struct llinfo_nd6 *ln;
    909 	long time_second = time.tv_sec;
    910 
    911 	/*
    912 	 * If the caller specified "rt", use that.  Otherwise, resolve the
    913 	 * routing table by supplied "dst6".
    914 	 */
    915 	if (!rt) {
    916 		if (!dst6)
    917 			return;
    918 		if (!(rt = nd6_lookup(dst6, 0, NULL)))
    919 			return;
    920 	}
    921 
    922 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
    923 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
    924 	    !rt->rt_llinfo || !rt->rt_gateway ||
    925 	    rt->rt_gateway->sa_family != AF_LINK) {
    926 		/* This is not a host route. */
    927 		return;
    928 	}
    929 
    930 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
    931 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
    932 		return;
    933 
    934 	/*
    935 	 * if we get upper-layer reachability confirmation many times,
    936 	 * it is possible we have false information.
    937 	 */
    938 	if (!force) {
    939 		ln->ln_byhint++;
    940 		if (ln->ln_byhint > nd6_maxnudhint)
    941 			return;
    942 	}
    943 
    944 	ln->ln_state = ND6_LLINFO_REACHABLE;
    945 	if (ln->ln_expire)
    946 		ln->ln_expire = time_second +
    947 			nd_ifinfo[rt->rt_ifp->if_index].reachable;
    948 }
    949 
    950 #ifdef OLDIP6OUTPUT
    951 /*
    952  * Resolve an IP6 address into an ethernet address. If success,
    953  * desten is filled in. If there is no entry in ndptab,
    954  * set one up and multicast a solicitation for the IP6 address.
    955  * Hold onto this mbuf and resend it once the address
    956  * is finally resolved. A return value of 1 indicates
    957  * that desten has been filled in and the packet should be sent
    958  * normally; a 0 return indicates that the packet has been
    959  * taken over here, either now or for later transmission.
    960  */
    961 int
    962 nd6_resolve(ifp, rt, m, dst, desten)
    963 	struct ifnet *ifp;
    964 	struct rtentry *rt;
    965 	struct mbuf *m;
    966 	struct sockaddr *dst;
    967 	u_char *desten;
    968 {
    969 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
    970 	struct sockaddr_dl *sdl;
    971 	long time_second = time.tv_sec;
    972 
    973 	if (m->m_flags & M_MCAST) {
    974 		switch (ifp->if_type) {
    975 		case IFT_ETHER:
    976 		case IFT_FDDI:
    977 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
    978 						 desten);
    979 			return(1);
    980 		case IFT_IEEE1394:
    981 			bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
    982 			return(1);
    983 		case IFT_ARCNET:
    984 			*desten = 0;
    985 			return(1);
    986 		default:
    987 			return(0);
    988 		}
    989 	}
    990 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
    991 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
    992 	else {
    993 		if ((rt = nd6_lookup(&(SIN6(dst)->sin6_addr), 1, ifp)) != NULL)
    994 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
    995 	}
    996 	if (!ln || !rt) {
    997 		log(LOG_DEBUG, "nd6_resolve: can't allocate llinfo for %s\n",
    998 			ip6_sprintf(&(SIN6(dst)->sin6_addr)));
    999 		m_freem(m);
   1000 		return(0);
   1001 	}
   1002 	sdl = SDL(rt->rt_gateway);
   1003 	/*
   1004 	 * Ckeck the address family and length is valid, the address
   1005 	 * is resolved; otherwise, try to resolve.
   1006 	 */
   1007 	if (ln->ln_state >= ND6_LLINFO_REACHABLE
   1008 	   && sdl->sdl_family == AF_LINK
   1009 	   && sdl->sdl_alen != 0) {
   1010 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
   1011 		if (ln->ln_state == ND6_LLINFO_STALE) {
   1012 			ln->ln_asked = 0;
   1013 			ln->ln_state = ND6_LLINFO_DELAY;
   1014 			ln->ln_expire = time_second + nd6_delay;
   1015 		}
   1016 		return(1);
   1017 	}
   1018 	/*
   1019 	 * There is an ndp entry, but no ethernet address
   1020 	 * response yet. Replace the held mbuf with this
   1021 	 * latest one.
   1022 	 *
   1023 	 * XXX Does the code conform to rate-limiting rule?
   1024 	 * (RFC 2461 7.2.2)
   1025 	 */
   1026 	if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
   1027 	    ln->ln_state == ND6_LLINFO_NOSTATE)
   1028 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
   1029 	if (ln->ln_hold)
   1030 		m_freem(ln->ln_hold);
   1031 	ln->ln_hold = m;
   1032 	if (ln->ln_expire) {
   1033 		rt->rt_flags &= ~RTF_REJECT;
   1034 		if (ln->ln_asked < nd6_mmaxtries &&
   1035 		    ln->ln_expire < time_second) {
   1036 			ln->ln_asked++;
   1037 			ln->ln_expire = time_second +
   1038 				nd_ifinfo[ifp->if_index].retrans / 1000;
   1039 			nd6_ns_output(ifp, NULL, &(SIN6(dst)->sin6_addr),
   1040 				ln, 0);
   1041 		}
   1042 	}
   1043 	return(0);
   1044 }
   1045 #endif /* OLDIP6OUTPUT */
   1046 
   1047 void
   1048 nd6_rtrequest(req, rt, info)
   1049 	int	req;
   1050 	struct rtentry *rt;
   1051 	struct rt_addrinfo *info; /* xxx unused */
   1052 {
   1053 	struct sockaddr *gate = rt->rt_gateway;
   1054 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1055 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
   1056 	struct ifnet *ifp = rt->rt_ifp;
   1057 	struct ifaddr *ifa;
   1058 	long time_second = time.tv_sec;
   1059 
   1060 	if (rt->rt_flags & RTF_GATEWAY)
   1061 		return;
   1062 
   1063 	switch (req) {
   1064 	case RTM_ADD:
   1065 		/*
   1066 		 * There is no backward compatibility :)
   1067 		 *
   1068 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
   1069 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
   1070 		 *	   rt->rt_flags |= RTF_CLONING;
   1071 		 */
   1072 		if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) {
   1073 			/*
   1074 			 * Case 1: This route should come from
   1075 			 * a route to interface. RTF_LLINFO flag is set
   1076 			 * for a host route whose destination should be
   1077 			 * treated as on-link.
   1078 			 */
   1079 			rt_setgate(rt, rt_key(rt),
   1080 				   (struct sockaddr *)&null_sdl);
   1081 			gate = rt->rt_gateway;
   1082 			SDL(gate)->sdl_type = ifp->if_type;
   1083 			SDL(gate)->sdl_index = ifp->if_index;
   1084 			if (ln)
   1085 				ln->ln_expire = time_second;
   1086 #if 1
   1087 			if (ln && ln->ln_expire == 0) {
   1088 				/* cludge for desktops */
   1089 #if 0
   1090 				printf("nd6_request: time.tv_sec is zero; "
   1091 				       "treat it as 1\n");
   1092 #endif
   1093 				ln->ln_expire = 1;
   1094 			}
   1095 #endif
   1096 			if (rt->rt_flags & RTF_CLONING)
   1097 				break;
   1098 		}
   1099 		/*
   1100 		 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
   1101 		 * We don't do that here since llinfo is not ready yet.
   1102 		 *
   1103 		 * There are also couple of other things to be discussed:
   1104 		 * - unsolicited NA code needs improvement beforehand
   1105 		 * - RFC2461 says we MAY send multicast unsolicited NA
   1106 		 *   (7.2.6 paragraph 4), however, it also says that we
   1107 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
   1108 		 *   we don't have anything like it right now.
   1109 		 *   note that the mechanism needs a mutual agreement
   1110 		 *   between proxies, which means that we need to implement
   1111 		 *   a new protocol, or a new kludge.
   1112 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
   1113 		 *   we need to check ip6forwarding before sending it.
   1114 		 *   (or should we allow proxy ND configuration only for
   1115 		 *   routers?  there's no mention about proxy ND from hosts)
   1116 		 */
   1117 #if 0
   1118 		/* XXX it does not work */
   1119 		if (rt->rt_flags & RTF_ANNOUNCE)
   1120 			nd6_na_output(ifp,
   1121 			      &SIN6(rt_key(rt))->sin6_addr,
   1122 			      &SIN6(rt_key(rt))->sin6_addr,
   1123 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
   1124 			      1, NULL);
   1125 #endif
   1126 		/* FALLTHROUGH */
   1127 	case RTM_RESOLVE:
   1128 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
   1129 			/*
   1130 			 * Address resolution isn't necessary for a point to
   1131 			 * point link, so we can skip this test for a p2p link.
   1132 			 */
   1133 			if (gate->sa_family != AF_LINK ||
   1134 			    gate->sa_len < sizeof(null_sdl)) {
   1135 				log(LOG_DEBUG,
   1136 				    "nd6_rtrequest: bad gateway value: %s\n",
   1137 				    if_name(ifp));
   1138 				break;
   1139 			}
   1140 			SDL(gate)->sdl_type = ifp->if_type;
   1141 			SDL(gate)->sdl_index = ifp->if_index;
   1142 		}
   1143 		if (ln != NULL)
   1144 			break;	/* This happens on a route change */
   1145 		/*
   1146 		 * Case 2: This route may come from cloning, or a manual route
   1147 		 * add with a LL address.
   1148 		 */
   1149 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
   1150 		rt->rt_llinfo = (caddr_t)ln;
   1151 		if (!ln) {
   1152 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
   1153 			break;
   1154 		}
   1155 		nd6_inuse++;
   1156 		nd6_allocated++;
   1157 		Bzero(ln, sizeof(*ln));
   1158 		ln->ln_rt = rt;
   1159 		/* this is required for "ndp" command. - shin */
   1160 		if (req == RTM_ADD) {
   1161 		        /*
   1162 			 * gate should have some valid AF_LINK entry,
   1163 			 * and ln->ln_expire should have some lifetime
   1164 			 * which is specified by ndp command.
   1165 			 */
   1166 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1167 			ln->ln_byhint = 0;
   1168 		} else {
   1169 		        /*
   1170 			 * When req == RTM_RESOLVE, rt is created and
   1171 			 * initialized in rtrequest(), so rt_expire is 0.
   1172 			 */
   1173 			ln->ln_state = ND6_LLINFO_NOSTATE;
   1174 			ln->ln_expire = time_second;
   1175 		}
   1176 		rt->rt_flags |= RTF_LLINFO;
   1177 		ln->ln_next = llinfo_nd6.ln_next;
   1178 		llinfo_nd6.ln_next = ln;
   1179 		ln->ln_prev = &llinfo_nd6;
   1180 		ln->ln_next->ln_prev = ln;
   1181 
   1182 		/*
   1183 		 * check if rt_key(rt) is one of my address assigned
   1184 		 * to the interface.
   1185 		 */
   1186 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
   1187 					  &SIN6(rt_key(rt))->sin6_addr);
   1188 		if (ifa) {
   1189 			caddr_t macp = nd6_ifptomac(ifp);
   1190 			ln->ln_expire = 0;
   1191 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1192 			ln->ln_byhint = 0;
   1193 			if (macp) {
   1194 				Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
   1195 				SDL(gate)->sdl_alen = ifp->if_addrlen;
   1196 			}
   1197 			if (nd6_useloopback) {
   1198 				rt->rt_ifp = &loif[0];	/*XXX*/
   1199 				/*
   1200 				 * Make sure rt_ifa be equal to the ifaddr
   1201 				 * corresponding to the address.
   1202 				 * We need this because when we refer
   1203 				 * rt_ifa->ia6_flags in ip6_input, we assume
   1204 				 * that the rt_ifa points to the address instead
   1205 				 * of the loopback address.
   1206 				 */
   1207 				if (ifa != rt->rt_ifa) {
   1208 					IFAFREE(rt->rt_ifa);
   1209 					IFAREF(ifa);
   1210 					rt->rt_ifa = ifa;
   1211 				}
   1212 			}
   1213 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
   1214 			ln->ln_expire = 0;
   1215 			ln->ln_state = ND6_LLINFO_REACHABLE;
   1216 			ln->ln_byhint = 0;
   1217 
   1218 			/* join solicited node multicast for proxy ND */
   1219 			if (ifp->if_flags & IFF_MULTICAST) {
   1220 				struct in6_addr llsol;
   1221 				int error;
   1222 
   1223 				llsol = SIN6(rt_key(rt))->sin6_addr;
   1224 				llsol.s6_addr16[0] = htons(0xff02);
   1225 				llsol.s6_addr16[1] = htons(ifp->if_index);
   1226 				llsol.s6_addr32[1] = 0;
   1227 				llsol.s6_addr32[2] = htonl(1);
   1228 				llsol.s6_addr8[12] = 0xff;
   1229 
   1230 				(void)in6_addmulti(&llsol, ifp, &error);
   1231 				if (error)
   1232 					printf(
   1233 "nd6_rtrequest: could not join solicited node multicast (errno=%d)\n", error);
   1234 			}
   1235 		}
   1236 		break;
   1237 
   1238 	case RTM_DELETE:
   1239 		if (!ln)
   1240 			break;
   1241 		/* leave from solicited node multicast for proxy ND */
   1242 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
   1243 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
   1244 			struct in6_addr llsol;
   1245 			struct in6_multi *in6m;
   1246 
   1247 			llsol = SIN6(rt_key(rt))->sin6_addr;
   1248 			llsol.s6_addr16[0] = htons(0xff02);
   1249 			llsol.s6_addr16[1] = htons(ifp->if_index);
   1250 			llsol.s6_addr32[1] = 0;
   1251 			llsol.s6_addr32[2] = htonl(1);
   1252 			llsol.s6_addr8[12] = 0xff;
   1253 
   1254 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
   1255 			if (in6m)
   1256 				in6_delmulti(in6m);
   1257 		}
   1258 		nd6_inuse--;
   1259 		ln->ln_next->ln_prev = ln->ln_prev;
   1260 		ln->ln_prev->ln_next = ln->ln_next;
   1261 		ln->ln_prev = NULL;
   1262 		rt->rt_llinfo = 0;
   1263 		rt->rt_flags &= ~RTF_LLINFO;
   1264 		if (ln->ln_hold)
   1265 			m_freem(ln->ln_hold);
   1266 		Free((caddr_t)ln);
   1267 	}
   1268 }
   1269 
   1270 void
   1271 nd6_p2p_rtrequest(req, rt, info)
   1272 	int	req;
   1273 	struct rtentry *rt;
   1274 	struct rt_addrinfo *info; /* xxx unused */
   1275 {
   1276 	struct sockaddr *gate = rt->rt_gateway;
   1277 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
   1278 	struct ifnet *ifp = rt->rt_ifp;
   1279 	struct ifaddr *ifa;
   1280 
   1281 	if (rt->rt_flags & RTF_GATEWAY)
   1282 		return;
   1283 
   1284 	switch (req) {
   1285 	case RTM_ADD:
   1286 		/*
   1287 		 * There is no backward compatibility :)
   1288 		 *
   1289 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
   1290 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
   1291 		 *	   rt->rt_flags |= RTF_CLONING;
   1292 		 */
   1293 		if (rt->rt_flags & RTF_CLONING) {
   1294 			/*
   1295 			 * Case 1: This route should come from
   1296 			 * a route to interface.
   1297 			 */
   1298 			rt_setgate(rt, rt_key(rt),
   1299 				   (struct sockaddr *)&null_sdl);
   1300 			gate = rt->rt_gateway;
   1301 			SDL(gate)->sdl_type = ifp->if_type;
   1302 			SDL(gate)->sdl_index = ifp->if_index;
   1303 			break;
   1304 		}
   1305 		/* Announce a new entry if requested. */
   1306 		if (rt->rt_flags & RTF_ANNOUNCE)
   1307 			nd6_na_output(ifp,
   1308 				      &SIN6(rt_key(rt))->sin6_addr,
   1309 				      &SIN6(rt_key(rt))->sin6_addr,
   1310 				      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
   1311 				      1, NULL);
   1312 		/* FALLTHROUGH */
   1313 	case RTM_RESOLVE:
   1314 		/*
   1315 		 * check if rt_key(rt) is one of my address assigned
   1316 		 * to the interface.
   1317 		 */
   1318  		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
   1319 					  &SIN6(rt_key(rt))->sin6_addr);
   1320 		if (ifa) {
   1321 			if (nd6_useloopback) {
   1322 				rt->rt_ifp = &loif[0];	/*XXX*/
   1323 			}
   1324 		}
   1325 		break;
   1326 	}
   1327 }
   1328 
   1329 int
   1330 nd6_ioctl(cmd, data, ifp)
   1331 	u_long cmd;
   1332 	caddr_t	data;
   1333 	struct ifnet *ifp;
   1334 {
   1335 	struct in6_drlist *drl = (struct in6_drlist *)data;
   1336 	struct in6_prlist *prl = (struct in6_prlist *)data;
   1337 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
   1338 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
   1339 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
   1340 	struct nd_defrouter *dr, any;
   1341 	struct nd_prefix *pr;
   1342 	struct rtentry *rt;
   1343 	int i = 0, error = 0;
   1344 	int s;
   1345 
   1346 	switch (cmd) {
   1347 	case SIOCGDRLST_IN6:
   1348 		bzero(drl, sizeof(*drl));
   1349 		s = splsoftnet();
   1350 		dr = TAILQ_FIRST(&nd_defrouter);
   1351 		while (dr && i < DRLSTSIZ) {
   1352 			drl->defrouter[i].rtaddr = dr->rtaddr;
   1353 			if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
   1354 				/* XXX: need to this hack for KAME stack */
   1355 				drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
   1356 			} else
   1357 				log(LOG_ERR,
   1358 				    "default router list contains a "
   1359 				    "non-linklocal address(%s)\n",
   1360 				    ip6_sprintf(&drl->defrouter[i].rtaddr));
   1361 
   1362 			drl->defrouter[i].flags = dr->flags;
   1363 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
   1364 			drl->defrouter[i].expire = dr->expire;
   1365 			drl->defrouter[i].if_index = dr->ifp->if_index;
   1366 			i++;
   1367 			dr = TAILQ_NEXT(dr, dr_entry);
   1368 		}
   1369 		splx(s);
   1370 		break;
   1371 	case SIOCGPRLST_IN6:
   1372 		/*
   1373 		 * XXX meaning of fields, especialy "raflags", is very
   1374 		 * differnet between RA prefix list and RR/static prefix list.
   1375 		 * how about separating ioctls into two?
   1376 		 */
   1377 		bzero(prl, sizeof(*prl));
   1378 		s = splsoftnet();
   1379 		pr = nd_prefix.lh_first;
   1380 		while (pr && i < PRLSTSIZ) {
   1381 			struct nd_pfxrouter *pfr;
   1382 			int j;
   1383 
   1384 			prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
   1385 			prl->prefix[i].raflags = pr->ndpr_raf;
   1386 			prl->prefix[i].prefixlen = pr->ndpr_plen;
   1387 			prl->prefix[i].vltime = pr->ndpr_vltime;
   1388 			prl->prefix[i].pltime = pr->ndpr_pltime;
   1389 			prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
   1390 			prl->prefix[i].expire = pr->ndpr_expire;
   1391 
   1392 			pfr = pr->ndpr_advrtrs.lh_first;
   1393 			j = 0;
   1394 			while(pfr) {
   1395 				if (j < DRLSTSIZ) {
   1396 #define RTRADDR prl->prefix[i].advrtr[j]
   1397 					RTRADDR = pfr->router->rtaddr;
   1398 					if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
   1399 						/* XXX: hack for KAME */
   1400 						RTRADDR.s6_addr16[1] = 0;
   1401 					} else
   1402 						log(LOG_ERR,
   1403 						    "a router(%s) advertises "
   1404 						    "a prefix with "
   1405 						    "non-link local address\n",
   1406 						    ip6_sprintf(&RTRADDR));
   1407 #undef RTRADDR
   1408 				}
   1409 				j++;
   1410 				pfr = pfr->pfr_next;
   1411 			}
   1412 			prl->prefix[i].advrtrs = j;
   1413 			prl->prefix[i].origin = PR_ORIG_RA;
   1414 
   1415 			i++;
   1416 			pr = pr->ndpr_next;
   1417 		}
   1418 	      {
   1419 		struct rr_prefix *rpp;
   1420 
   1421 		for (rpp = LIST_FIRST(&rr_prefix); rpp;
   1422 		     rpp = LIST_NEXT(rpp, rp_entry)) {
   1423 			if (i >= PRLSTSIZ)
   1424 				break;
   1425 			prl->prefix[i].prefix = rpp->rp_prefix.sin6_addr;
   1426 			prl->prefix[i].raflags = rpp->rp_raf;
   1427 			prl->prefix[i].prefixlen = rpp->rp_plen;
   1428 			prl->prefix[i].vltime = rpp->rp_vltime;
   1429 			prl->prefix[i].pltime = rpp->rp_pltime;
   1430 			prl->prefix[i].if_index = rpp->rp_ifp->if_index;
   1431 			prl->prefix[i].expire = rpp->rp_expire;
   1432 			prl->prefix[i].advrtrs = 0;
   1433 			prl->prefix[i].origin = rpp->rp_origin;
   1434 			i++;
   1435 		}
   1436 	      }
   1437 		splx(s);
   1438 
   1439 		break;
   1440 	case SIOCGIFINFO_IN6:
   1441 		if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
   1442 			error = EINVAL;
   1443 			break;
   1444 		}
   1445 		ndi->ndi = nd_ifinfo[ifp->if_index];
   1446 		break;
   1447 	case SIOCSIFINFO_FLAGS:
   1448 		/* XXX: almost all other fields of ndi->ndi is unused */
   1449 		if (!nd_ifinfo || i >= nd_ifinfo_indexlim) {
   1450 			error = EINVAL;
   1451 			break;
   1452 		}
   1453 		nd_ifinfo[ifp->if_index].flags = ndi->ndi.flags;
   1454 		break;
   1455 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
   1456 		/* flush default router list */
   1457 		/*
   1458 		 * xxx sumikawa: should not delete route if default
   1459 		 * route equals to the top of default router list
   1460 		 */
   1461 		bzero(&any, sizeof(any));
   1462 		defrouter_delreq(&any, 0);
   1463 		defrouter_select();
   1464 		/* xxx sumikawa: flush prefix list */
   1465 		break;
   1466 	case SIOCSPFXFLUSH_IN6:
   1467 	    {
   1468 		/* flush all the prefix advertised by routers */
   1469 		struct nd_prefix *pr, *next;
   1470 
   1471 		s = splsoftnet();
   1472 		for (pr = nd_prefix.lh_first; pr; pr = next) {
   1473 			next = pr->ndpr_next;
   1474 			if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
   1475 				in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
   1476 			prelist_remove(pr);
   1477 		}
   1478 		splx(s);
   1479 		break;
   1480 	    }
   1481 	case SIOCSRTRFLUSH_IN6:
   1482 	    {
   1483 		/* flush all the default routers */
   1484 		struct nd_defrouter *dr, *next;
   1485 
   1486 		s = splsoftnet();
   1487 		if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) {
   1488 			/*
   1489 			 * The first entry of the list may be stored in
   1490 			 * the routing table, so we'll delete it later.
   1491 			 */
   1492 			for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) {
   1493 				next = TAILQ_NEXT(dr, dr_entry);
   1494 				defrtrlist_del(dr);
   1495 			}
   1496 			defrtrlist_del(TAILQ_FIRST(&nd_defrouter));
   1497 		}
   1498 		splx(s);
   1499 		break;
   1500 	    }
   1501 	case SIOCGNBRINFO_IN6:
   1502 	    {
   1503 		struct llinfo_nd6 *ln;
   1504 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
   1505 
   1506 		/*
   1507 		 * XXX: KAME specific hack for scoped addresses
   1508 		 *      XXXX: for other scopes than link-local?
   1509 		 */
   1510 		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
   1511 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
   1512 			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
   1513 
   1514 			if (*idp == 0)
   1515 				*idp = htons(ifp->if_index);
   1516 		}
   1517 
   1518 		s = splsoftnet();
   1519 		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) {
   1520 			error = EINVAL;
   1521 			splx(s);
   1522 			break;
   1523 		}
   1524 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1525 		nbi->state = ln->ln_state;
   1526 		nbi->asked = ln->ln_asked;
   1527 		nbi->isrouter = ln->ln_router;
   1528 		nbi->expire = ln->ln_expire;
   1529 		splx(s);
   1530 
   1531 		break;
   1532 	    }
   1533 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
   1534 		ndif->ifindex = nd6_defifindex;
   1535 		break;
   1536 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
   1537 		return(nd6_setdefaultiface(ndif->ifindex));
   1538 		break;
   1539 	}
   1540 	return(error);
   1541 }
   1542 
   1543 /*
   1544  * Create neighbor cache entry and cache link-layer address,
   1545  * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
   1546  */
   1547 struct rtentry *
   1548 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
   1549 	struct ifnet *ifp;
   1550 	struct in6_addr *from;
   1551 	char *lladdr;
   1552 	int lladdrlen;
   1553 	int type;	/* ICMP6 type */
   1554 	int code;	/* type dependent information */
   1555 {
   1556 	struct rtentry *rt = NULL;
   1557 	struct llinfo_nd6 *ln = NULL;
   1558 	int is_newentry;
   1559 	struct sockaddr_dl *sdl = NULL;
   1560 	int do_update;
   1561 	int olladdr;
   1562 	int llchange;
   1563 	int newstate = 0;
   1564 	long time_second = time.tv_sec;
   1565 
   1566 	if (!ifp)
   1567 		panic("ifp == NULL in nd6_cache_lladdr");
   1568 	if (!from)
   1569 		panic("from == NULL in nd6_cache_lladdr");
   1570 
   1571 	/* nothing must be updated for unspecified address */
   1572 	if (IN6_IS_ADDR_UNSPECIFIED(from))
   1573 		return NULL;
   1574 
   1575 	/*
   1576 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
   1577 	 * the caller.
   1578 	 *
   1579 	 * XXX If the link does not have link-layer adderss, what should
   1580 	 * we do? (ifp->if_addrlen == 0)
   1581 	 * Spec says nothing in sections for RA, RS and NA.  There's small
   1582 	 * description on it in NS section (RFC 2461 7.2.3).
   1583 	 */
   1584 
   1585 	rt = nd6_lookup(from, 0, ifp);
   1586 	if (!rt) {
   1587 #if 0
   1588 		/* nothing must be done if there's no lladdr */
   1589 		if (!lladdr || !lladdrlen)
   1590 			return NULL;
   1591 #endif
   1592 
   1593 		rt = nd6_lookup(from, 1, ifp);
   1594 		is_newentry = 1;
   1595 	} else
   1596 		is_newentry = 0;
   1597 
   1598 	if (!rt)
   1599 		return NULL;
   1600 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
   1601 fail:
   1602 		(void)nd6_free(rt);
   1603 		return NULL;
   1604 	}
   1605 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1606 	if (!ln)
   1607 		goto fail;
   1608 	if (!rt->rt_gateway)
   1609 		goto fail;
   1610 	if (rt->rt_gateway->sa_family != AF_LINK)
   1611 		goto fail;
   1612 	sdl = SDL(rt->rt_gateway);
   1613 
   1614 	olladdr = (sdl->sdl_alen) ? 1 : 0;
   1615 	if (olladdr && lladdr) {
   1616 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
   1617 			llchange = 1;
   1618 		else
   1619 			llchange = 0;
   1620 	} else
   1621 		llchange = 0;
   1622 
   1623 	/*
   1624 	 * newentry olladdr  lladdr  llchange	(*=record)
   1625 	 *	0	n	n	--	(1)
   1626 	 *	0	y	n	--	(2)
   1627 	 *	0	n	y	--	(3) * STALE
   1628 	 *	0	y	y	n	(4) *
   1629 	 *	0	y	y	y	(5) * STALE
   1630 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
   1631 	 *	1	--	y	--	(7) * STALE
   1632 	 */
   1633 
   1634 	if (lladdr) {		/*(3-5) and (7)*/
   1635 		/*
   1636 		 * Record source link-layer address
   1637 		 * XXX is it dependent to ifp->if_type?
   1638 		 */
   1639 		sdl->sdl_alen = ifp->if_addrlen;
   1640 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
   1641 	}
   1642 
   1643 	if (!is_newentry) {
   1644 		if ((!olladdr && lladdr)		/*(3)*/
   1645 		 || (olladdr && lladdr && llchange)) {	/*(5)*/
   1646 			do_update = 1;
   1647 			newstate = ND6_LLINFO_STALE;
   1648 		} else					/*(1-2,4)*/
   1649 			do_update = 0;
   1650 	} else {
   1651 		do_update = 1;
   1652 		if (!lladdr)				/*(6)*/
   1653 			newstate = ND6_LLINFO_NOSTATE;
   1654 		else					/*(7)*/
   1655 			newstate = ND6_LLINFO_STALE;
   1656 	}
   1657 
   1658 	if (do_update) {
   1659 		/*
   1660 		 * Update the state of the neighbor cache.
   1661 		 */
   1662 		ln->ln_state = newstate;
   1663 
   1664 		if (ln->ln_state == ND6_LLINFO_STALE) {
   1665 			rt->rt_flags &= ~RTF_REJECT;
   1666 			if (ln->ln_hold) {
   1667 #ifdef OLDIP6OUTPUT
   1668 				(*ifp->if_output)(ifp, ln->ln_hold,
   1669 						  rt_key(rt), rt);
   1670 #else
   1671 				/*
   1672 				 * we assume ifp is not a p2p here, so just
   1673 				 * set the 2nd argument as the 1st one.
   1674 				 */
   1675 				nd6_output(ifp, ifp, ln->ln_hold,
   1676 					   (struct sockaddr_in6 *)rt_key(rt),
   1677 					   rt);
   1678 #endif
   1679 				ln->ln_hold = 0;
   1680 			}
   1681 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
   1682 			/* probe right away */
   1683 			ln->ln_expire = time_second;
   1684 		}
   1685 	}
   1686 
   1687 	/*
   1688 	 * ICMP6 type dependent behavior.
   1689 	 *
   1690 	 * NS: clear IsRouter if new entry
   1691 	 * RS: clear IsRouter
   1692 	 * RA: set IsRouter if there's lladdr
   1693 	 * redir: clear IsRouter if new entry
   1694 	 *
   1695 	 * RA case, (1):
   1696 	 * The spec says that we must set IsRouter in the following cases:
   1697 	 * - If lladdr exist, set IsRouter.  This means (1-5).
   1698 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
   1699 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
   1700 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
   1701 	 * neighbor cache, this is similar to (6).
   1702 	 * This case is rare but we figured that we MUST NOT set IsRouter.
   1703 	 *
   1704 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
   1705 	 *							D R
   1706 	 *	0	n	n	--	(1)	c   ?     s
   1707 	 *	0	y	n	--	(2)	c   s     s
   1708 	 *	0	n	y	--	(3)	c   s     s
   1709 	 *	0	y	y	n	(4)	c   s     s
   1710 	 *	0	y	y	y	(5)	c   s     s
   1711 	 *	1	--	n	--	(6) c	c 	c s
   1712 	 *	1	--	y	--	(7) c	c   s	c s
   1713 	 *
   1714 	 *					(c=clear s=set)
   1715 	 */
   1716 	switch (type & 0xff) {
   1717 	case ND_NEIGHBOR_SOLICIT:
   1718 		/*
   1719 		 * New entry must have is_router flag cleared.
   1720 		 */
   1721 		if (is_newentry)	/*(6-7)*/
   1722 			ln->ln_router = 0;
   1723 		break;
   1724 	case ND_REDIRECT:
   1725 		/*
   1726 		 * If the icmp is a redirect to a better router, always set the
   1727 		 * is_router flag. Otherwise, if the entry is newly created,
   1728 		 * clear the flag. [RFC 2461, sec 8.3]
   1729 		 */
   1730 		if (code == ND_REDIRECT_ROUTER)
   1731 			ln->ln_router = 1;
   1732 		else if (is_newentry) /*(6-7)*/
   1733 			ln->ln_router = 0;
   1734 		break;
   1735 	case ND_ROUTER_SOLICIT:
   1736 		/*
   1737 		 * is_router flag must always be cleared.
   1738 		 */
   1739 		ln->ln_router = 0;
   1740 		break;
   1741 	case ND_ROUTER_ADVERT:
   1742 		/*
   1743 		 * Mark an entry with lladdr as a router.
   1744 		 */
   1745 		if ((!is_newentry && (olladdr || lladdr))	/*(2-5)*/
   1746 		 || (is_newentry && lladdr)) {			/*(7)*/
   1747 			ln->ln_router = 1;
   1748 		}
   1749 		break;
   1750 	}
   1751 
   1752 	return rt;
   1753 }
   1754 
   1755 static void
   1756 nd6_slowtimo(ignored_arg)
   1757     void *ignored_arg;
   1758 {
   1759 	int s = splsoftnet();
   1760 	int i;
   1761 	struct nd_ifinfo *nd6if;
   1762 
   1763 	callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
   1764 	    nd6_slowtimo, NULL);
   1765 	for (i = 1; i < if_index + 1; i++) {
   1766 		if (!nd_ifinfo || i >= nd_ifinfo_indexlim)
   1767 			continue;
   1768 		nd6if = &nd_ifinfo[i];
   1769 		if (nd6if->basereachable && /* already initialized */
   1770 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
   1771 			/*
   1772 			 * Since reachable time rarely changes by router
   1773 			 * advertisements, we SHOULD insure that a new random
   1774 			 * value gets recomputed at least once every few hours.
   1775 			 * (RFC 2461, 6.3.4)
   1776 			 */
   1777 			nd6if->recalctm = nd6_recalc_reachtm_interval;
   1778 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
   1779 		}
   1780 	}
   1781 	splx(s);
   1782 }
   1783 
   1784 #define senderr(e) { error = (e); goto bad;}
   1785 int
   1786 nd6_output(ifp, origifp, m0, dst, rt0)
   1787 	struct ifnet *ifp;
   1788 	struct ifnet *origifp;
   1789 	struct mbuf *m0;
   1790 	struct sockaddr_in6 *dst;
   1791 	struct rtentry *rt0;
   1792 {
   1793 	struct mbuf *m = m0;
   1794 	struct rtentry *rt = rt0;
   1795 	struct sockaddr_in6 *gw6 = NULL;
   1796 	struct llinfo_nd6 *ln = NULL;
   1797 	int error = 0;
   1798 	long time_second = time.tv_sec;
   1799 
   1800 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
   1801 		goto sendpkt;
   1802 
   1803 	/*
   1804 	 * XXX: we currently do not make neighbor cache on any interface
   1805 	 * other than ARCnet, Ethernet, FDDI and GIF.
   1806 	 *
   1807 	 * RFC2893 says:
   1808 	 * - unidirectional tunnels needs no ND
   1809 	 */
   1810 	switch (ifp->if_type) {
   1811 	case IFT_ARCNET:
   1812 	case IFT_ETHER:
   1813 	case IFT_FDDI:
   1814 	case IFT_IEEE1394:
   1815 	case IFT_GIF:		/* XXX need more cases? */
   1816 		break;
   1817 	default:
   1818 		goto sendpkt;
   1819 	}
   1820 
   1821 	/*
   1822 	 * next hop determination. This routine is derived from ether_outpout.
   1823 	 */
   1824 	if (rt) {
   1825 		if ((rt->rt_flags & RTF_UP) == 0) {
   1826 			if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
   1827 				NULL)
   1828 			{
   1829 				rt->rt_refcnt--;
   1830 				if (rt->rt_ifp != ifp) {
   1831 					/* XXX: loop care? */
   1832 					return nd6_output(ifp, origifp, m0,
   1833 							  dst, rt);
   1834 				}
   1835 			} else
   1836 				senderr(EHOSTUNREACH);
   1837 		}
   1838 
   1839 		if (rt->rt_flags & RTF_GATEWAY) {
   1840 			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
   1841 
   1842 			/*
   1843 			 * We skip link-layer address resolution and NUD
   1844 			 * if the gateway is not a neighbor from ND point
   1845 			 * of view, regardless the value of the value of
   1846 			 * nd_ifinfo.flags.
   1847 			 * The second condition is a bit tricky: we skip
   1848 			 * if the gateway is our own address, which is
   1849 			 * sometimes used to install a route to a p2p link.
   1850 			 */
   1851 			if (!nd6_is_addr_neighbor(gw6, ifp) ||
   1852 			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
   1853 				if (rt->rt_flags & RTF_REJECT)
   1854 					senderr(EHOSTDOWN);
   1855 
   1856 				/*
   1857 				 * We allow this kind of tricky route only
   1858 				 * when the outgoing interface is p2p.
   1859 				 * XXX: we may need a more generic rule here.
   1860 				 */
   1861 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
   1862 					senderr(EHOSTUNREACH);
   1863 
   1864 				goto sendpkt;
   1865 			}
   1866 
   1867 			if (rt->rt_gwroute == 0)
   1868 				goto lookup;
   1869 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
   1870 				rtfree(rt); rt = rt0;
   1871 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
   1872 				if ((rt = rt->rt_gwroute) == 0)
   1873 					senderr(EHOSTUNREACH);
   1874 			}
   1875 		}
   1876 		if (rt->rt_flags & RTF_REJECT)
   1877 			senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
   1878 	}
   1879 
   1880 	/*
   1881 	 * Address resolution or Neighbor Unreachability Detection
   1882 	 * for the next hop.
   1883 	 * At this point, the destination of the packet must be a unicast
   1884 	 * or an anycast address(i.e. not a multicast).
   1885 	 */
   1886 
   1887 	/* Look up the neighbor cache for the nexthop */
   1888 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
   1889 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1890 	else {
   1891 		/*
   1892 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
   1893 		 * the condition below is not very efficient. But we believe
   1894 		 * it is tolerable, because this should be a rare case.
   1895 		 */
   1896 		if (nd6_is_addr_neighbor(dst, ifp) &&
   1897 		    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
   1898 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1899 	}
   1900 	if (!ln || !rt) {
   1901 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
   1902 		    !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) {
   1903 			log(LOG_DEBUG,
   1904 			    "nd6_output: can't allocate llinfo for %s "
   1905 			    "(ln=%p, rt=%p)\n",
   1906 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
   1907 			senderr(EIO);	/* XXX: good error? */
   1908 		}
   1909 
   1910 		goto sendpkt;	/* send anyway */
   1911 	}
   1912 
   1913 	/* We don't have to do link-layer address resolution on a p2p link. */
   1914 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
   1915 	    ln->ln_state < ND6_LLINFO_REACHABLE)
   1916 		ln->ln_state = ND6_LLINFO_STALE;
   1917 
   1918 	/*
   1919 	 * The first time we send a packet to a neighbor whose entry is
   1920 	 * STALE, we have to change the state to DELAY and a sets a timer to
   1921 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
   1922 	 * neighbor unreachability detection on expiration.
   1923 	 * (RFC 2461 7.3.3)
   1924 	 */
   1925 	if (ln->ln_state == ND6_LLINFO_STALE) {
   1926 		ln->ln_asked = 0;
   1927 		ln->ln_state = ND6_LLINFO_DELAY;
   1928 		ln->ln_expire = time_second + nd6_delay;
   1929 	}
   1930 
   1931 	/*
   1932 	 * If the neighbor cache entry has a state other than INCOMPLETE
   1933 	 * (i.e. its link-layer address is already reloved), just
   1934 	 * send the packet.
   1935 	 */
   1936 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
   1937 		goto sendpkt;
   1938 
   1939 	/*
   1940 	 * There is a neighbor cache entry, but no ethernet address
   1941 	 * response yet. Replace the held mbuf (if any) with this
   1942 	 * latest one.
   1943 	 *
   1944 	 * XXX Does the code conform to rate-limiting rule?
   1945 	 * (RFC 2461 7.2.2)
   1946 	 */
   1947 	if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
   1948 	    ln->ln_state == ND6_LLINFO_NOSTATE)
   1949 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
   1950 	if (ln->ln_hold)
   1951 		m_freem(ln->ln_hold);
   1952 	ln->ln_hold = m;
   1953 	if (ln->ln_expire) {
   1954 		rt->rt_flags &= ~RTF_REJECT;
   1955 		if (ln->ln_asked < nd6_mmaxtries &&
   1956 		    ln->ln_expire < time_second) {
   1957 			ln->ln_asked++;
   1958 			ln->ln_expire = time_second +
   1959 				nd_ifinfo[ifp->if_index].retrans / 1000;
   1960 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
   1961 		}
   1962 	}
   1963 	return(0);
   1964 
   1965   sendpkt:
   1966 
   1967 #ifdef FAKE_LOOPBACK_IF
   1968 	if (ifp->if_flags & IFF_LOOPBACK) {
   1969 		return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
   1970 					 rt));
   1971 	}
   1972 #endif
   1973 	return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
   1974 
   1975   bad:
   1976 	if (m)
   1977 		m_freem(m);
   1978 	return (error);
   1979 }
   1980 #undef senderr
   1981 
   1982 int
   1983 nd6_storelladdr(ifp, rt, m, dst, desten)
   1984 	struct ifnet *ifp;
   1985 	struct rtentry *rt;
   1986 	struct mbuf *m;
   1987 	struct sockaddr *dst;
   1988 	u_char *desten;
   1989 {
   1990 	struct sockaddr_dl *sdl;
   1991 
   1992 	if (m->m_flags & M_MCAST) {
   1993 		switch (ifp->if_type) {
   1994 		case IFT_ETHER:
   1995 		case IFT_FDDI:
   1996 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
   1997 						 desten);
   1998 			return(1);
   1999 		case IFT_IEEE1394:
   2000 			bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen);
   2001 			return(1);
   2002 		case IFT_ARCNET:
   2003 			*desten = 0;
   2004 			return(1);
   2005 		default:
   2006 			return(0);
   2007 		}
   2008 	}
   2009 
   2010 	if (rt == NULL) {
   2011 		/* this could happen, if we could not allocate memory */
   2012 		return(0);
   2013 	}
   2014 	if (rt->rt_gateway->sa_family != AF_LINK) {
   2015 		printf("nd6_storelladdr: something odd happens\n");
   2016 		return(0);
   2017 	}
   2018 	sdl = SDL(rt->rt_gateway);
   2019 	if (sdl->sdl_alen == 0) {
   2020 		/* this should be impossible, but we bark here for debugging */
   2021 		printf("nd6_storelladdr: sdl_alen == 0\n");
   2022 		return(0);
   2023 	}
   2024 
   2025 	bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
   2026 	return(1);
   2027 }
   2028