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