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