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