Home | History | Annotate | Line # | Download | only in netinet6
nd6.c revision 1.10
      1 /*	$NetBSD: nd6.c,v 1.10 1999/09/20 02:35:44 itojun Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * XXX
     34  * KAME 970409 note:
     35  * BSD/OS version heavily modifies this code, related to llinfo.
     36  * Since we don't have BSD/OS version of net/route.c in our hand,
     37  * I left the code mostly as it was in 970310.  -- itojun
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/malloc.h>
     43 #include <sys/mbuf.h>
     44 #include <sys/socket.h>
     45 #include <sys/sockio.h>
     46 #include <sys/time.h>
     47 #include <sys/kernel.h>
     48 #include <sys/errno.h>
     49 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     50 #include <sys/ioctl.h>
     51 #endif
     52 #include <sys/syslog.h>
     53 #include <sys/queue.h>
     54 
     55 #include <net/if.h>
     56 #include <net/if_dl.h>
     57 #include <net/if_types.h>
     58 #include <net/if_atm.h>
     59 #include <net/route.h>
     60 
     61 #include <netinet/in.h>
     62 #ifndef __NetBSD__
     63 #include <netinet/if_ether.h>
     64 #ifdef __FreeBSD__
     65 #include <netinet/if_fddi.h>
     66 #endif
     67 #ifdef __bsdi__
     68 #include <net/if_fddi.h>
     69 #endif
     70 #else /* __NetBSD__ */
     71 #include <net/if_ether.h>
     72 #include <netinet/if_inarp.h>
     73 #include <net/if_fddi.h>
     74 #endif /* __NetBSD__ */
     75 #include <netinet6/in6_var.h>
     76 #include <netinet6/ip6.h>
     77 #include <netinet6/ip6_var.h>
     78 #include <netinet6/nd6.h>
     79 #include <netinet6/icmp6.h>
     80 
     81 #include "loop.h"
     82 #ifdef __NetBSD__
     83 extern struct ifnet loif[NLOOP];
     84 #endif
     85 
     86 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
     87 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
     88 
     89 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
     90 #define time_second time.tv_sec
     91 #endif
     92 
     93 #define SIN6(s) ((struct sockaddr_in6 *)s)
     94 #define SDL(s) ((struct sockaddr_dl *)s)
     95 
     96 /* timer values */
     97 int	nd6_prune	= 1;	/* walk list every 1 seconds */
     98 int	nd6_delay	= 5;	/* delay first probe time 5 second */
     99 int	nd6_umaxtries	= 3;	/* maximum unicast query */
    100 int	nd6_mmaxtries	= 3;	/* maximum multicast query */
    101 int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
    102 int	nd6_proxyall	= 0;	/* enable Proxy Neighbor Advertisement */
    103 
    104 /* for debugging? */
    105 static int nd6_inuse, nd6_allocated;
    106 
    107 struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
    108 struct nd_ifinfo *nd_ifinfo;
    109 struct nd_drhead nd_defrouter = { 0 };
    110 struct nd_prhead nd_prefix = { 0 };
    111 
    112 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
    113 #if 0
    114 extern	int ip6_forwarding;
    115 #endif
    116 
    117 static void nd6_slowtimo __P((void *));
    118 
    119 void
    120 nd6_init()
    121 {
    122 	static int nd6_init_done = 0;
    123 
    124 	if (nd6_init_done) {
    125 		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
    126 		return;
    127 	}
    128 	nd6_init_done = 1;
    129 
    130 	/* start timer */
    131 	timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
    132 }
    133 
    134 void
    135 nd6_ifattach(ifp)
    136 	struct ifnet *ifp;
    137 {
    138 	static size_t if_indexlim = 8;
    139 
    140 	/*
    141 	 * We have some arrays that should be indexed by if_index.
    142 	 * since if_index will grow dynamically, they should grow too.
    143 	 */
    144 	if (nd_ifinfo == NULL || if_index >= if_indexlim) {
    145 		size_t n;
    146 		caddr_t q;
    147 
    148 		while (if_index >= if_indexlim)
    149 			if_indexlim <<= 1;
    150 
    151 		/* grow nd_ifinfo */
    152 		n = if_indexlim * sizeof(struct nd_ifinfo);
    153 		q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK);
    154 		bzero(q, n);
    155 		if (nd_ifinfo) {
    156 			bcopy((caddr_t)nd_ifinfo, q, n/2);
    157 			free((caddr_t)nd_ifinfo, M_IP6NDP);
    158 		}
    159 		nd_ifinfo = (struct nd_ifinfo *)q;
    160 	}
    161 
    162 #define ND nd_ifinfo[ifp->if_index]
    163 	ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu;
    164 	ND.chlim = IPV6_DEFHLIM;
    165 	ND.basereachable = REACHABLE_TIME;
    166 	ND.reachable = ND_COMPUTE_RTIME(ND.basereachable);
    167 	ND.retrans = RETRANS_TIMER;
    168 	ND.receivedra = 0;
    169 	nd6_setmtu(ifp);
    170 #undef ND
    171 }
    172 
    173 /*
    174  * Reset ND level link MTU. This function is called when the physical MTU
    175  * changes, which means we might have to adjust the ND level MTU.
    176  */
    177 void
    178 nd6_setmtu(ifp)
    179 	struct ifnet *ifp;
    180 {
    181 #define MIN(a,b) ((a) < (b) ? (a) : (b))
    182 	struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index];
    183 	u_long oldmaxmtu = ndi->maxmtu;
    184 	u_long oldlinkmtu = ndi->linkmtu;
    185 
    186 	switch(ifp->if_type) {
    187 	 case IFT_ARCNET:	/* XXX MTU handling needs more work */
    188 		 ndi->maxmtu = MIN(60480, ifp->if_mtu);
    189 		 break;
    190 	 case IFT_ETHER:
    191 		 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu);
    192 		 break;
    193 #if defined(__FreeBSD__) || defined(__bsdi__)
    194 	 case IFT_FDDI:
    195 		 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu);
    196 		 break;
    197 #endif
    198 	 case IFT_ATM:
    199 		 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu);
    200 		 break;
    201 	 default:
    202 		 ndi->maxmtu = ifp->if_mtu;
    203 		 break;
    204 	}
    205 
    206 	if (oldmaxmtu != ndi->maxmtu) {
    207 		/*
    208 		 * If the ND level MTU is not set yet, or if the maxmtu
    209 		 * is reset to a smaller value than the ND level MTU,
    210 		 * also reset the ND level MTU.
    211 		 */
    212 		if (ndi->linkmtu == 0 ||
    213 		    ndi->maxmtu < ndi->linkmtu) {
    214 			ndi->linkmtu = ndi->maxmtu;
    215 			/* also adjust in6_maxmtu if necessary. */
    216 			if (oldlinkmtu == 0) {
    217 				/*
    218 				 * XXX: the case analysis is grotty, but
    219 				 * it is not efficient to call in6_setmaxmtu()
    220 				 * here when we are during the initialization
    221 				 * procedure.
    222 				 */
    223 				if (in6_maxmtu < ndi->linkmtu)
    224 					in6_maxmtu = ndi->linkmtu;
    225 			}
    226 			else
    227 				in6_setmaxmtu();
    228 		}
    229 	}
    230 #undef MIN
    231 }
    232 
    233 void
    234 nd6_option_init(opt, icmp6len, ndopts)
    235 	void *opt;
    236 	int icmp6len;
    237 	union nd_opts *ndopts;
    238 {
    239 	bzero(ndopts, sizeof(*ndopts));
    240 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
    241 	ndopts->nd_opts_last
    242 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
    243 
    244 	if (icmp6len == 0) {
    245 		ndopts->nd_opts_done = 1;
    246 		ndopts->nd_opts_search = NULL;
    247 	}
    248 }
    249 
    250 /*
    251  * Take one ND option.
    252  */
    253 struct nd_opt_hdr *
    254 nd6_option(ndopts)
    255 	union nd_opts *ndopts;
    256 {
    257 	struct nd_opt_hdr *nd_opt;
    258 	int olen;
    259 
    260 	if (!ndopts)
    261 		panic("ndopts == NULL in nd6_option\n");
    262 	if (!ndopts->nd_opts_last)
    263 		panic("uninitialized ndopts in nd6_option\n");
    264 	if (!ndopts->nd_opts_search)
    265 		return NULL;
    266 	if (ndopts->nd_opts_done)
    267 		return NULL;
    268 
    269 	nd_opt = ndopts->nd_opts_search;
    270 
    271 	olen = nd_opt->nd_opt_len << 3;
    272 	if (olen == 0) {
    273 		/*
    274 		 * Message validation requires that all included
    275 		 * options have a length that is greater than zero.
    276 		 */
    277 		bzero(ndopts, sizeof(*ndopts));
    278 		return NULL;
    279 	}
    280 
    281 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
    282 	if (!(ndopts->nd_opts_search < ndopts->nd_opts_last)) {
    283 		ndopts->nd_opts_done = 1;
    284 		ndopts->nd_opts_search = NULL;
    285 	}
    286 	return nd_opt;
    287 }
    288 
    289 /*
    290  * Parse multiple ND options.
    291  * This function is much easier to use, for ND routines that do not need
    292  * multiple options of the same type.
    293  */
    294 int
    295 nd6_options(ndopts)
    296 	union nd_opts *ndopts;
    297 {
    298 	struct nd_opt_hdr *nd_opt;
    299 	int i = 0;
    300 
    301 	if (!ndopts)
    302 		panic("ndopts == NULL in nd6_options\n");
    303 	if (!ndopts->nd_opts_last)
    304 		panic("uninitialized ndopts in nd6_options\n");
    305 	if (!ndopts->nd_opts_search)
    306 		return 0;
    307 
    308 	while (1) {
    309 		nd_opt = nd6_option(ndopts);
    310 		if (!nd_opt && !ndopts->nd_opts_last) {
    311 			/*
    312 			 * Message validation requires that all included
    313 			 * options have a length that is greater than zero.
    314 			 */
    315 			bzero(ndopts, sizeof(*ndopts));
    316 			return -1;
    317 		}
    318 
    319 		if (!nd_opt)
    320 			goto skip1;
    321 
    322 		switch (nd_opt->nd_opt_type) {
    323 		case ND_OPT_SOURCE_LINKADDR:
    324 		case ND_OPT_TARGET_LINKADDR:
    325 		case ND_OPT_MTU:
    326 		case ND_OPT_REDIRECTED_HEADER:
    327 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
    328 				printf("duplicated ND6 option found "
    329 					"(type=%d)\n", nd_opt->nd_opt_type);
    330 				/* XXX bark? */
    331 			} else {
    332 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
    333 					= nd_opt;
    334 			}
    335 			break;
    336 		case ND_OPT_PREFIX_INFORMATION:
    337 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
    338 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
    339 					= nd_opt;
    340 			}
    341 			ndopts->nd_opts_pi_end =
    342 				(struct nd_opt_prefix_info *)nd_opt;
    343 			break;
    344 		default:
    345 			/*
    346 			 * Unknown options must be silently ignored,
    347 			 * to accomodate future extension to the protocol.
    348 			 */
    349 			log(LOG_INFO,
    350 			    "nd6_options: unsupported option %d - "
    351 			    "option ignored\n", nd_opt->nd_opt_type);
    352 		}
    353 
    354 skip1:
    355 		i++;
    356 		if (i > 10) {
    357 			printf("too many loop in nd opt\n");
    358 			break;
    359 		}
    360 
    361 		if (ndopts->nd_opts_done)
    362 			break;
    363 	}
    364 
    365 	return 0;
    366 }
    367 
    368 /*
    369  * ND6 timer routine to expire default route list and prefix list
    370  */
    371 void
    372 nd6_timer(ignored_arg)
    373 	void	*ignored_arg;
    374 {
    375 	int s;
    376 	register struct llinfo_nd6 *ln;
    377 	register struct nd_defrouter *dr;
    378 	register struct nd_prefix *pr;
    379 
    380 	s = splsoftnet();
    381 	timeout(nd6_timer, (caddr_t)0, nd6_prune * hz);
    382 
    383 	ln = llinfo_nd6.ln_next;
    384 	/* XXX BSD/OS separates this code -- itojun */
    385 	while (ln && ln != &llinfo_nd6) {
    386 		struct rtentry *rt;
    387 		struct ifnet *ifp;
    388 		struct sockaddr_in6 *dst;
    389 		struct llinfo_nd6 *next = ln->ln_next;
    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 		dst = (struct sockaddr_in6 *)rt_key(rt);
    400 
    401 		if (ln->ln_expire > time_second) {
    402 			ln = next;
    403 			continue;
    404 		}
    405 
    406 		/* sanity check */
    407 		if (!rt)
    408 			panic("rt=0 in nd6_timer(ln=%p)\n", 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 					nd_ifinfo[ifp->if_index].retrans / 1000;
    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 				nd6_free(rt);
    438 			}
    439 			break;
    440 		case ND6_LLINFO_REACHABLE:
    441 			if (ln->ln_expire) {
    442 				ln->ln_state = ND6_LLINFO_STALE;
    443 			}
    444 			break;
    445 		/*
    446 		 * ND6_LLINFO_STALE state requires nothing for timer
    447 		 * routine.
    448 		 */
    449 		case ND6_LLINFO_DELAY:
    450 			ln->ln_asked = 1;
    451 			ln->ln_state = ND6_LLINFO_PROBE;
    452 			ln->ln_expire = time_second +
    453 				nd_ifinfo[ifp->if_index].retrans / 1000;
    454 			nd6_ns_output(ifp, &dst->sin6_addr, &dst->sin6_addr,
    455 				ln, 0);
    456 			break;
    457 
    458 		case ND6_LLINFO_PROBE:
    459 			if (ln->ln_asked < nd6_umaxtries) {
    460 				ln->ln_asked++;
    461 				ln->ln_expire = time_second +
    462 					nd_ifinfo[ifp->if_index].retrans / 1000;
    463 				nd6_ns_output(ifp, &dst->sin6_addr,
    464 					       &dst->sin6_addr, ln, 0);
    465 			} else {
    466 				nd6_free(rt);
    467 			}
    468 			break;
    469 		case ND6_LLINFO_WAITDELETE:
    470 			nd6_free(rt);
    471 			break;
    472 		}
    473 		ln = next;
    474 	}
    475 
    476 	/* expire */
    477 	dr = nd_defrouter.lh_first;
    478 	while (dr) {
    479 		if (dr->expire && dr->expire < time_second) {
    480 			struct nd_defrouter *t;
    481 			t = dr->dr_next;
    482 			defrtrlist_del(dr);
    483 			dr = t;
    484 		} else
    485 			dr = dr->dr_next;
    486 	}
    487 	pr = nd_prefix.lh_first;
    488 	while (pr) {
    489 		struct in6_ifaddr *ia6;
    490 		struct in6_addrlifetime *lt6;
    491 
    492 		if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
    493 			ia6 = NULL;
    494 		else
    495 			ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr);
    496 
    497 		if (ia6) {
    498 			/* check address lifetime */
    499 			lt6 = &ia6->ia6_lifetime;
    500 			if (lt6->ia6t_preferred && lt6->ia6t_preferred < time_second)
    501 				ia6->ia6_flags |= IN6_IFF_DEPRECATED;
    502 			if (lt6->ia6t_expire && lt6->ia6t_expire < time_second) {
    503 				if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
    504 					in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
    505 				/* xxx ND_OPT_PI_FLAG_ONLINK processing */
    506 			}
    507 		}
    508 
    509 		/*
    510 		 * check prefix lifetime.
    511 		 * since pltime is just for autoconf, pltime processing for
    512 		 * prefix is not necessary.
    513 		 *
    514 		 * we offset expire time by NDPR_KEEP_EXPIRE, so that we
    515 		 * can use the old prefix information to validate the
    516 		 * next prefix information to come.  See prelist_update()
    517 		 * for actual validation.
    518 		 */
    519 		if (pr->ndpr_expire
    520 		 && pr->ndpr_expire + NDPR_KEEP_EXPIRED < time_second) {
    521 			struct nd_prefix *t;
    522 			t = pr->ndpr_next;
    523 
    524 			/*
    525 			 * address expiration and prefix expiration are
    526 			 * separate.  NEVER perform in6_ifdel here.
    527 			 */
    528 
    529 			prelist_remove(pr);
    530 			pr = t;
    531 		} else
    532 			pr = pr->ndpr_next;
    533 	}
    534 	splx(s);
    535 }
    536 
    537 static struct sockaddr_in6 all1_sa = {
    538 	sizeof(struct sockaddr_in6), AF_INET6, 0, 0,
    539 	{{{0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff}}}, 0};
    540 
    541 struct rtentry *
    542 nd6_lookup(addr6, create, ifp)
    543 	struct in6_addr *addr6;
    544 	int create;
    545 	struct ifnet *ifp;
    546 {
    547 	struct rtentry *rt;
    548 	struct sockaddr_in6 sin6;
    549 
    550 	bzero(&sin6, sizeof(sin6));
    551 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    552 	sin6.sin6_family = AF_INET6;
    553 	sin6.sin6_addr = *addr6;
    554 	rt = rtalloc1((struct sockaddr *)&sin6, create
    555 #ifdef __FreeBSD__
    556 		      , 0UL
    557 #endif /*__FreeBSD__*/
    558 		      );
    559 	if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
    560 		/*
    561 		 * This is the case for the default route.
    562 		 * If we want to create a neighbor cache for the address, we
    563 		 * should free the route for the destination and allocate an
    564 		 * interface route.
    565 		 */
    566 		if (create) {
    567 			RTFREE(rt);
    568 			rt = 0;
    569 		}
    570 	}
    571 	if (!rt) {
    572 		if (create && ifp) {
    573 			/*
    574 			 * If no route is available and create is set,
    575 			 * we allocate a host route for the destination
    576 			 * and treat it like an interface route.
    577 			 * This hack is necessary for a neighbor which can't
    578 			 * be covered by our own prefix.
    579 			 */
    580 			struct ifaddr *ifa =
    581 				ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
    582 			if (ifa == NULL)
    583 				return(NULL);
    584 
    585 			/*
    586 			 * Create a new route. RTF_LLINFO is necessary
    587 			 * to create a Neighbor Cache entry for the
    588 			 * destination in nd6_rtrequest which will be
    589 			 * called in rtequest via ifa->ifa_rtrequest.
    590 			 */
    591 			if (rtrequest(RTM_ADD, (struct sockaddr *)&sin6,
    592 				      ifa->ifa_addr,
    593 				      (struct sockaddr *)&all1_sa,
    594 				      (ifa->ifa_flags |
    595 				       RTF_HOST | RTF_LLINFO) & ~RTF_CLONING,
    596 				      &rt))
    597 				log(LOG_ERR,
    598 				    "nd6_lookup: failed to add route for a "
    599 				    "neighbor(%s)\n", ip6_sprintf(addr6));
    600 			if (rt == NULL)
    601 				return(NULL);
    602 			if (rt->rt_llinfo) {
    603 				struct llinfo_nd6 *ln =
    604 					(struct llinfo_nd6 *)rt->rt_llinfo;
    605 				ln->ln_state = ND6_LLINFO_NOSTATE;
    606 			}
    607 		}
    608 		else
    609 			return(NULL);
    610 	}
    611 	rt->rt_refcnt--;
    612 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
    613 	   rt->rt_gateway->sa_family != AF_LINK) {
    614 		if (create) {
    615 			log(LOG_DEBUG, "nd6_lookup: failed to lookup %s\n",
    616 			    ip6_sprintf(addr6));
    617 			/* xxx more logs... kazu */
    618 		}
    619 		return(0);
    620 	}
    621 	return(rt);
    622 }
    623 
    624 /*
    625  * Detect if a given IPv6 address identifies a neighbor on a given link.
    626  * XXX: should take care of the destination of a p2p link?
    627  */
    628 int
    629 nd6_is_addr_neighbor(addr, ifp)
    630 	struct in6_addr *addr;
    631 	struct ifnet *ifp;
    632 {
    633 	register struct ifaddr *ifa;
    634 	int i;
    635 
    636 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr)
    637 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr)
    638 
    639 	/* A link-local address is always a neighbor. */
    640 	if (IN6_IS_ADDR_LINKLOCAL(addr))
    641 		return(1);
    642 
    643 	/*
    644 	 * If the address matches one of our addresses,
    645 	 * it should be a neighbor.
    646 	 */
    647 #ifdef __bsdi__
    648 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
    649 #else
    650 	for (ifa = ifp->if_addrlist.tqh_first;
    651 	     ifa;
    652 	     ifa = ifa->ifa_list.tqe_next)
    653 #endif
    654 	{
    655 		if (ifa->ifa_addr->sa_family != AF_INET6)
    656 			next: continue;
    657 
    658 		for (i = 0; i < 4; i++) {
    659 			if ((IFADDR6(ifa).s6_addr32[i] ^ addr->s6_addr32[i]) &
    660 			    IFMASK6(ifa).s6_addr32[i])
    661 				goto next;
    662 		}
    663 		return(1);
    664 	}
    665 
    666 	/*
    667 	 * Even if the address matches none of our addresses, it might be
    668 	 * in the neighbor cache.
    669 	 */
    670 	if (nd6_lookup(addr, 0, ifp))
    671 		return(1);
    672 
    673 	return(0);
    674 #undef IFADDR6
    675 #undef IFMASK6
    676 }
    677 
    678 /*
    679  * Free an nd6 llinfo entry.
    680  */
    681 void
    682 nd6_free(rt)
    683 	struct rtentry *rt;
    684 {
    685 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
    686 	struct sockaddr_dl *sdl;
    687 
    688 	if (ln->ln_router) {
    689 		/* remove from default router list */
    690 		struct nd_defrouter *dr;
    691 		struct in6_addr *in6;
    692 		int s;
    693 		in6 = &((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
    694 
    695 		s = splsoftnet();
    696 		dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->
    697 				      sin6_addr,
    698 				      rt->rt_ifp);
    699 		if (dr)
    700 			defrtrlist_del(dr);
    701 		else if (!ip6_forwarding && ip6_accept_rtadv) {
    702 			/*
    703 			 * rt6_flush must be called in any case.
    704 			 * see the comment in nd6_na_input().
    705 			 */
    706 			rt6_flush(in6, rt->rt_ifp);
    707 		}
    708 		splx(s);
    709 	}
    710 
    711 	if (rt->rt_refcnt > 0 && (sdl = SDL(rt->rt_gateway)) &&
    712 	   sdl->sdl_family == AF_LINK) {
    713 		sdl->sdl_alen = 0;
    714 		ln->ln_state = ND6_LLINFO_WAITDELETE;
    715 		ln->ln_asked = 0;
    716 		rt->rt_flags &= ~RTF_REJECT;
    717 		return;
    718 	}
    719 	rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, rt_mask(rt),
    720 		  0, (struct rtentry **)0);
    721 }
    722 
    723 /*
    724  * Upper-layer reachability hint for Neighbor Unreachability Detection.
    725  *
    726  * XXX cost-effective metods?
    727  */
    728 void
    729 nd6_nud_hint(rt, dst6)
    730 	struct rtentry *rt;
    731 	struct in6_addr *dst6;
    732 {
    733 	struct llinfo_nd6 *ln;
    734 
    735 	/*
    736 	 * If the caller specified "rt", use that.  Otherwise, resolve the
    737 	 * routing table by supplied "dst6".
    738 	 */
    739 	if (!rt) {
    740 		if (!dst6)
    741 			return;
    742 		if (!(rt = nd6_lookup(dst6, 0, NULL)))
    743 			return;
    744 	}
    745 
    746 	if ((rt->rt_flags & RTF_GATEWAY)
    747 	 || (rt->rt_flags & RTF_LLINFO) == 0
    748 	 || !rt->rt_llinfo
    749 	 || !rt->rt_gateway
    750 	 || rt->rt_gateway->sa_family != AF_LINK) {
    751 		/* This is not a host route. */
    752 		return;
    753 	}
    754 
    755 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
    756 	if (ln->ln_state == ND6_LLINFO_INCOMPLETE)
    757 		return;
    758 
    759 	ln->ln_state = ND6_LLINFO_REACHABLE;
    760 	if (ln->ln_expire)
    761 		ln->ln_expire = time_second +
    762 			nd_ifinfo[rt->rt_ifp->if_index].reachable;
    763 }
    764 
    765 /*
    766  * Resolve an IP6 address into an ethernet address. If success,
    767  * desten is filled in. If there is no entry in ndptab,
    768  * set one up and multicast a solicitation for the IP6 address.
    769  * Hold onto this mbuf and resend it once the address
    770  * is finally resolved. A return value of 1 indicates
    771  * that desten has been filled in and the packet should be sent
    772  * normally; a 0 return indicates that the packet has been
    773  * taken over here, either now or for later transmission.
    774  */
    775 int
    776 nd6_resolve(ifp, rt, m, dst, desten)
    777 	struct ifnet *ifp;
    778 	struct rtentry *rt;
    779 	struct mbuf *m;
    780 	struct sockaddr *dst;
    781 	u_char *desten;
    782 {
    783 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL;
    784 	struct sockaddr_dl *sdl;
    785 
    786 	if (m->m_flags & M_MCAST) {
    787 		switch (ifp->if_type) {
    788 		case IFT_ETHER:
    789 		case IFT_FDDI:
    790 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
    791 						 desten);
    792 			return(1);
    793 			break;
    794 		case IFT_ARCNET:
    795 			*desten = 0;
    796 			return(1);
    797 		default:
    798 			return(0);
    799 		}
    800 	}
    801 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
    802 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
    803 	else {
    804 		if ((rt = nd6_lookup(&(SIN6(dst)->sin6_addr), 1, ifp)) != NULL)
    805 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
    806 	}
    807 	if (!ln || !rt) {
    808 		log(LOG_DEBUG, "nd6_resolve: can't allocate llinfo for %s\n",
    809 			ip6_sprintf(&(SIN6(dst)->sin6_addr)));
    810 		m_freem(m);
    811 		return(0);
    812 	}
    813 	sdl = SDL(rt->rt_gateway);
    814 	/*
    815 	 * Ckeck the address family and length is valid, the address
    816 	 * is resolved; otherwise, try to resolve.
    817 	 */
    818 	if (ln->ln_state >= ND6_LLINFO_REACHABLE
    819 	   && sdl->sdl_family == AF_LINK
    820 	   && sdl->sdl_alen != 0) {
    821 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
    822 		if (ln->ln_state == ND6_LLINFO_STALE) {
    823 			ln->ln_asked = 0;
    824 			ln->ln_state = ND6_LLINFO_DELAY;
    825 			ln->ln_expire = time_second + nd6_delay;
    826 		}
    827 		return(1);
    828 	}
    829 	/*
    830 	 * There is an ndp entry, but no ethernet address
    831 	 * response yet. Replace the held mbuf with this
    832 	 * latest one.
    833 	 *
    834 	 * XXX Does the code conform to rate-limiting rule?
    835 	 * (RFC 2461 7.2.2)
    836 	 */
    837 	if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
    838 	    ln->ln_state == ND6_LLINFO_NOSTATE)
    839 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
    840 	if (ln->ln_hold)
    841 		m_freem(ln->ln_hold);
    842 	ln->ln_hold = m;
    843 	if (ln->ln_expire) {
    844 		rt->rt_flags &= ~RTF_REJECT;
    845 		if (ln->ln_asked < nd6_mmaxtries &&
    846 		    ln->ln_expire < time_second) {
    847 			ln->ln_asked++;
    848 			ln->ln_expire = time_second +
    849 				nd_ifinfo[ifp->if_index].retrans / 1000;
    850 			nd6_ns_output(ifp, NULL, &(SIN6(dst)->sin6_addr),
    851 				ln, 0);
    852 		}
    853 	}
    854 	return(0);
    855 }
    856 
    857 void
    858 nd6_rtrequest(req, rt, sa)
    859 	int	req;
    860 	struct rtentry *rt;
    861 	struct sockaddr *sa; /* xxx unused */
    862 {
    863 	struct sockaddr *gate = rt->rt_gateway;
    864 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
    865 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
    866 	struct ifnet *ifp = rt->rt_ifp;
    867 	struct ifaddr *ifa;
    868 
    869 	if (rt->rt_flags & RTF_GATEWAY)
    870 		return;
    871 
    872 	switch (req) {
    873 	case RTM_ADD:
    874 		/*
    875 		 * There is no backward compatibility :)
    876 		 *
    877 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
    878 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
    879 		 *	   rt->rt_flags |= RTF_CLONING;
    880 		 */
    881 		if (rt->rt_flags & RTF_CLONING || rt->rt_flags & RTF_LLINFO) {
    882 			/*
    883 			 * Case 1: This route should come from
    884 			 * a route to interface. RTF_LLINFO flag is set
    885 			 * for a host route whose destination should be
    886 			 * treated as on-link.
    887 			 */
    888 			rt_setgate(rt, rt_key(rt),
    889 				   (struct sockaddr *)&null_sdl);
    890 			gate = rt->rt_gateway;
    891 			SDL(gate)->sdl_type = ifp->if_type;
    892 			SDL(gate)->sdl_index = ifp->if_index;
    893 			if (ln)
    894 				ln->ln_expire = time_second;
    895 #if 1
    896 			if (ln && ln->ln_expire == 0) {
    897 				/* cludge for desktops */
    898 #if 0
    899 				printf("nd6_request: time.tv_sec is zero; "
    900 				       "treat it as 1\n");
    901 #endif
    902 				ln->ln_expire = 1;
    903 			}
    904 #endif
    905 			if (rt->rt_flags & RTF_CLONING)
    906 				break;
    907 		}
    908 		/* Announce a new entry if requested. */
    909 		if (rt->rt_flags & RTF_ANNOUNCE)
    910 			nd6_na_output(ifp,
    911 				      &SIN6(rt_key(rt))->sin6_addr,
    912 				      &SIN6(rt_key(rt))->sin6_addr,
    913 				      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
    914 				      1);
    915 		/* FALLTHROUGH */
    916 	case RTM_RESOLVE:
    917 		if (gate->sa_family != AF_LINK ||
    918 		   gate->sa_len < sizeof(null_sdl)) {
    919 			log(LOG_DEBUG, "nd6_rtrequest: bad gateway value\n");
    920 			break;
    921 		}
    922 		SDL(gate)->sdl_type = ifp->if_type;
    923 		SDL(gate)->sdl_index = ifp->if_index;
    924 		if (ln != 0)
    925 			break;	/* This happens on a route change */
    926 		/*
    927 		 * Case 2: This route may come from cloning, or a manual route
    928 		 * add with a LL address.
    929 		 */
    930 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
    931 		rt->rt_llinfo = (caddr_t)ln;
    932 		if (!ln) {
    933 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
    934 			break;
    935 		}
    936 		nd6_inuse++;
    937 		nd6_allocated++;
    938 		Bzero(ln, sizeof(*ln));
    939 		ln->ln_rt = rt;
    940 		/* this is required for "ndp" command. - shin */
    941 		if (req == RTM_ADD) {
    942 		        /*
    943 			 * gate should have some valid AF_LINK entry,
    944 			 * and ln->ln_expire should have some lifetime
    945 			 * which is specified by ndp command.
    946 			 */
    947 			ln->ln_state = ND6_LLINFO_REACHABLE;
    948 		} else {
    949 		        /*
    950 			 * When req == RTM_RESOLVE, rt is created and
    951 			 * initialized in rtrequest(), so rt_expire is 0.
    952 			 */
    953 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
    954 			ln->ln_expire = time_second;
    955 		}
    956 		rt->rt_flags |= RTF_LLINFO;
    957 #if 0
    958 		insque(ln, &llinfo_nd6);
    959 #else
    960 		ln->ln_next = llinfo_nd6.ln_next;
    961 		llinfo_nd6.ln_next = ln;
    962 		ln->ln_prev = &llinfo_nd6;
    963 		ln->ln_next->ln_prev = ln;
    964 #endif
    965 
    966 		/*
    967 		 * check if rt_key(rt) is one of my address assigned
    968 		 * to the interface.
    969 		 */
    970 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
    971 					  &SIN6(rt_key(rt))->sin6_addr);
    972 		if (ifa) {
    973 			caddr_t macp = nd6_ifptomac(ifp);
    974 			ln->ln_expire = 0;
    975 			ln->ln_state = ND6_LLINFO_REACHABLE;
    976 			if (macp) {
    977 				Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
    978 				SDL(gate)->sdl_alen = ifp->if_addrlen;
    979 			}
    980 			if (nd6_useloopback) {
    981 #ifdef __bsdi__
    982 				extern struct ifnet loif;
    983 				rt->rt_ifp = &loif;	/*XXX*/
    984 #endif /*__bsdi__*/
    985 #if defined(__FreeBSD__) || defined(__NetBSD__)
    986 				rt->rt_ifp = &loif[0];	/*XXX*/
    987 #endif
    988 				/*
    989 				 * Make sure rt_ifa be equal to the ifaddr
    990 				 * corresponding to the address.
    991 				 * We need this because when we refer
    992 				 * rt_ifa->ia6_flags in ip6_input, we assume
    993 				 * that the rt_ifa points to the address instead
    994 				 * of the loopback address.
    995 				 */
    996 				if (ifa != rt->rt_ifa) {
    997 					rt->rt_ifa->ifa_refcnt--;
    998 					ifa->ifa_refcnt++;
    999 					rt->rt_ifa = ifa;
   1000 				}
   1001 			}
   1002 		}
   1003 		break;
   1004 
   1005 	case RTM_DELETE:
   1006 		if (!ln)
   1007 			break;
   1008 		nd6_inuse--;
   1009 #if 0
   1010 		remque(ln);
   1011 #else
   1012 		ln->ln_next->ln_prev = ln->ln_prev;
   1013 		ln->ln_prev->ln_next = ln->ln_next;
   1014 		ln->ln_prev = NULL;
   1015 #endif
   1016 		rt->rt_llinfo = 0;
   1017 		rt->rt_flags &= ~RTF_LLINFO;
   1018 		if (ln->ln_hold)
   1019 			m_freem(ln->ln_hold);
   1020 		Free((caddr_t)ln);
   1021 	}
   1022 }
   1023 
   1024 void
   1025 nd6_p2p_rtrequest(req, rt, sa)
   1026 	int	req;
   1027 	struct rtentry *rt;
   1028 	struct sockaddr *sa; /* xxx unused */
   1029 {
   1030 	struct sockaddr *gate = rt->rt_gateway;
   1031 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
   1032 	struct ifnet *ifp = rt->rt_ifp;
   1033 	struct ifaddr *ifa;
   1034 
   1035 	if (rt->rt_flags & RTF_GATEWAY)
   1036 		return;
   1037 
   1038 	switch (req) {
   1039 	case RTM_ADD:
   1040 		/*
   1041 		 * There is no backward compatibility :)
   1042 		 *
   1043 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
   1044 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
   1045 		 *	   rt->rt_flags |= RTF_CLONING;
   1046 		 */
   1047 		if (rt->rt_flags & RTF_CLONING) {
   1048 			/*
   1049 			 * Case 1: This route should come from
   1050 			 * a route to interface.
   1051 			 */
   1052 			rt_setgate(rt, rt_key(rt),
   1053 				   (struct sockaddr *)&null_sdl);
   1054 			gate = rt->rt_gateway;
   1055 			SDL(gate)->sdl_type = ifp->if_type;
   1056 			SDL(gate)->sdl_index = ifp->if_index;
   1057 			break;
   1058 		}
   1059 		/* Announce a new entry if rqquested. */
   1060 		if (rt->rt_flags & RTF_ANNOUNCE)
   1061 			nd6_na_output(ifp,
   1062 				      &SIN6(rt_key(rt))->sin6_addr,
   1063 				      &SIN6(rt_key(rt))->sin6_addr,
   1064 				      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
   1065 				      1);
   1066 		/* FALLTHROUGH */
   1067 	case RTM_RESOLVE:
   1068 		/*
   1069 		 * check if rt_key(rt) is one of my address assigned
   1070 		 * to the interface.
   1071 		 */
   1072  		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
   1073 					  &SIN6(rt_key(rt))->sin6_addr);
   1074 		if (ifa) {
   1075 			if (nd6_useloopback) {
   1076 #ifdef __bsdi__
   1077 				extern struct ifnet loif;
   1078 				rt->rt_ifp = &loif;	/*XXX*/
   1079 #endif /*__bsdi__*/
   1080 #if defined(__FreeBSD__) || defined(__NetBSD__)
   1081 				rt->rt_ifp = &loif[0];	/*XXX*/
   1082 #endif
   1083 			}
   1084 		}
   1085 		break;
   1086 	}
   1087 }
   1088 
   1089 int
   1090 nd6_ioctl(cmd, data, ifp)
   1091 	u_long cmd;
   1092 	caddr_t	data;
   1093 	struct ifnet *ifp;
   1094 {
   1095 	struct in6_drlist *drl = (struct in6_drlist *)data;
   1096 	struct in6_prlist *prl = (struct in6_prlist *)data;
   1097 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
   1098 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
   1099 	struct nd_defrouter *dr, any;
   1100 	struct nd_prefix *pr;
   1101 	struct rtentry *rt;
   1102 	int i = 0, error = 0;
   1103 	int s;
   1104 
   1105 	switch (cmd) {
   1106 	case SIOCGDRLST_IN6:
   1107 		bzero(drl, sizeof(*drl));
   1108 		s = splsoftnet();
   1109 		dr = nd_defrouter.lh_first;
   1110 		while (dr && i < DRLSTSIZ) {
   1111 			drl->defrouter[i].rtaddr = dr->rtaddr;
   1112 			if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
   1113 				/* XXX: need to this hack for KAME stack */
   1114 				drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
   1115 			}
   1116 			else
   1117 				log(LOG_ERR,
   1118 				    "default router list contains a "
   1119 				    "non-linklocal address(%s)\n",
   1120 				    ip6_sprintf(&drl->defrouter[i].rtaddr));
   1121 
   1122 			drl->defrouter[i].flags = dr->flags;
   1123 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
   1124 			drl->defrouter[i].expire = dr->expire;
   1125 			drl->defrouter[i].if_index = dr->ifp->if_index;
   1126 			i++;
   1127 			dr = dr->dr_next;
   1128 		}
   1129 		splx(s);
   1130 		break;
   1131 	case SIOCGPRLST_IN6:
   1132 		bzero(prl, sizeof(*prl));
   1133 		s = splsoftnet();
   1134 		pr = nd_prefix.lh_first;
   1135 		while (pr && i < PRLSTSIZ) {
   1136 			struct nd_pfxrouter *pfr;
   1137 			int j;
   1138 
   1139 			prl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
   1140 			prl->prefix[i].raflags = pr->ndpr_raf;
   1141 			prl->prefix[i].prefixlen = pr->ndpr_plen;
   1142 			prl->prefix[i].vltime = pr->ndpr_vltime;
   1143 			prl->prefix[i].pltime = pr->ndpr_pltime;
   1144 			prl->prefix[i].if_index = pr->ndpr_ifp->if_index;
   1145 			prl->prefix[i].expire = pr->ndpr_expire;
   1146 
   1147 			pfr = pr->ndpr_advrtrs.lh_first;
   1148 			j = 0;
   1149 			while(pfr) {
   1150 				if (j < DRLSTSIZ) {
   1151 #define RTRADDR prl->prefix[i].advrtr[j]
   1152 					RTRADDR = pfr->router->rtaddr;
   1153 					if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
   1154 						/* XXX: hack for KAME */
   1155 						RTRADDR.s6_addr16[1] = 0;
   1156 					}
   1157 					else
   1158 						log(LOG_ERR,
   1159 						    "a router(%s) advertises "
   1160 						    "a prefix with "
   1161 						    "non-link local address\n",
   1162 						    ip6_sprintf(&RTRADDR));
   1163 #undef RTRADDR
   1164 				}
   1165 				j++;
   1166 				pfr = pfr->pfr_next;
   1167 			}
   1168 			prl->prefix[i].advrtrs = j;
   1169 
   1170 			i++;
   1171 			pr = pr->ndpr_next;
   1172 		}
   1173 		splx(s);
   1174 		break;
   1175 	case SIOCGIFINFO_IN6:
   1176 		ndi->ndi = nd_ifinfo[ifp->if_index];
   1177 		break;
   1178 	case SIOCSNDFLUSH_IN6:
   1179 		/* flush default router list */
   1180 		/*
   1181 		 * xxx sumikawa: should not delete route if default
   1182 		 * route equals to the top of default router list
   1183 		 */
   1184 		bzero(&any, sizeof(any));
   1185 		defrouter_delreq(&any, 0);
   1186 		/* xxx sumikawa: flush prefix list */
   1187 		break;
   1188 	case SIOCSPFXFLUSH_IN6:
   1189 	    {
   1190 		/* flush all the prefix advertised by routers */
   1191 		struct nd_prefix *pr, *next;
   1192 
   1193 		s = splsoftnet();
   1194 		for (pr = nd_prefix.lh_first; pr; pr = next) {
   1195 			next = pr->ndpr_next;
   1196 			if (!IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr))
   1197 				in6_ifdel(pr->ndpr_ifp, &pr->ndpr_addr);
   1198 			prelist_remove(pr);
   1199 		}
   1200 		splx(s);
   1201 		break;
   1202 	    }
   1203 	case SIOCSRTRFLUSH_IN6:
   1204 	    {
   1205 		/* flush all the default routers */
   1206 		struct nd_defrouter *dr, *next;
   1207 
   1208 		s = splsoftnet();
   1209 		if ((dr = nd_defrouter.lh_first) != NULL) {
   1210 			/*
   1211 			 * The first entry of the list may be stored in
   1212 			 * the routing table, so we'll delete it later.
   1213 			 */
   1214 			for (dr = dr->dr_next; dr; dr = next) {
   1215 				next = dr->dr_next;
   1216 				defrtrlist_del(dr);
   1217 			}
   1218 			defrtrlist_del(nd_defrouter.lh_first);
   1219 		}
   1220 		splx(s);
   1221 		break;
   1222 	    }
   1223 	case SIOCGNBRINFO_IN6:
   1224 	    {
   1225 		  struct llinfo_nd6 *ln;
   1226 
   1227 		  s = splsoftnet();
   1228 		  if ((rt = nd6_lookup(&nbi->addr, 0, ifp)) == NULL) {
   1229 			  error = EINVAL;
   1230 			  break;
   1231 		  }
   1232 		  ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1233 		  nbi->state = ln->ln_state;
   1234 		  nbi->asked = ln->ln_asked;
   1235 		  nbi->isrouter = ln->ln_router;
   1236 		  nbi->expire = ln->ln_expire;
   1237 		  splx(s);
   1238 
   1239 		  break;
   1240 	    }
   1241 	}
   1242 	return(error);
   1243 }
   1244 
   1245 /*
   1246  * Create neighbor cache entry and cache link-layer address,
   1247  * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
   1248  */
   1249 struct rtentry *
   1250 nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code)
   1251 	struct ifnet *ifp;
   1252 	struct in6_addr *from;
   1253 	char *lladdr;
   1254 	int lladdrlen;
   1255 	int type;	/* ICMP6 type */
   1256 	int code;	/* type dependent information */
   1257 {
   1258 	struct rtentry *rt = NULL;
   1259 	struct llinfo_nd6 *ln = NULL;
   1260 	int is_newentry;
   1261 	struct sockaddr_dl *sdl = NULL;
   1262 	int do_update;
   1263 	int olladdr;
   1264 	int llchange;
   1265 	int newstate = 0;
   1266 
   1267 	if (!ifp)
   1268 		panic("ifp == NULL in nd6_cache_lladdr");
   1269 	if (!from)
   1270 		panic("from == NULL in nd6_cache_lladdr");
   1271 
   1272 	/* nothing must be updated for unspecified address */
   1273 	if (IN6_IS_ADDR_UNSPECIFIED(from))
   1274 		return NULL;
   1275 
   1276 	/*
   1277 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
   1278 	 * the caller.
   1279 	 *
   1280 	 * XXX If the link does not have link-layer adderss, what should
   1281 	 * we do? (ifp->if_addrlen == 0)
   1282 	 * Spec says nothing in sections for RA, RS and NA.  There's small
   1283 	 * description on it in NS section (RFC 2461 7.2.3).
   1284 	 */
   1285 
   1286 	rt = nd6_lookup(from, 0, ifp);
   1287 	if (!rt) {
   1288 #if 0
   1289 		/* nothing must be done if there's no lladdr */
   1290 		if (!lladdr || !lladdrlen)
   1291 			return NULL;
   1292 #endif
   1293 
   1294 		rt = nd6_lookup(from, 1, ifp);
   1295 		is_newentry = 1;
   1296 	} else
   1297 		is_newentry = 0;
   1298 
   1299 	if (!rt)
   1300 		return NULL;
   1301 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
   1302 fail:
   1303 		nd6_free(rt);
   1304 		return NULL;
   1305 	}
   1306 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1307 	if (!ln)
   1308 		goto fail;
   1309 	if (!rt->rt_gateway)
   1310 		goto fail;
   1311 	if (rt->rt_gateway->sa_family != AF_LINK)
   1312 		goto fail;
   1313 	sdl = SDL(rt->rt_gateway);
   1314 
   1315 	olladdr = (sdl->sdl_alen) ? 1 : 0;
   1316 	if (olladdr && lladdr) {
   1317 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
   1318 			llchange = 1;
   1319 		else
   1320 			llchange = 0;
   1321 	} else
   1322 		llchange = 0;
   1323 
   1324 	/*
   1325 	 * newentry olladdr  lladdr  llchange	(*=record)
   1326 	 *	0	n	n	--	(1)
   1327 	 *	0	y	n	--	(2)
   1328 	 *	0	n	y	--	(3) * STALE
   1329 	 *	0	y	y	n	(4) *
   1330 	 *	0	y	y	y	(5) * STALE
   1331 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
   1332 	 *	1	--	y	--	(7) * STALE
   1333 	 */
   1334 
   1335 	if (lladdr) {		/*(3-5) and (7)*/
   1336 		/*
   1337 		 * Record source link-layer address
   1338 		 * XXX is it dependent to ifp->if_type?
   1339 		 */
   1340 		sdl->sdl_alen = ifp->if_addrlen;
   1341 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
   1342 	}
   1343 
   1344 	if (!is_newentry) {
   1345 		if ((!olladdr && lladdr)		/*(3)*/
   1346 		 || (olladdr && lladdr && llchange)) {	/*(5)*/
   1347 			do_update = 1;
   1348 			newstate = ND6_LLINFO_STALE;
   1349 		} else					/*(1-2,4)*/
   1350 			do_update = 0;
   1351 	} else {
   1352 		do_update = 1;
   1353 		if (!lladdr)				/*(6)*/
   1354 			newstate = ND6_LLINFO_NOSTATE;
   1355 		else					/*(7)*/
   1356 			newstate = ND6_LLINFO_STALE;
   1357 	}
   1358 
   1359 	if (do_update) {
   1360 		/*
   1361 		 * Update the state of the neighbor cache.
   1362 		 */
   1363 		ln->ln_state = newstate;
   1364 
   1365 		if (ln->ln_state == ND6_LLINFO_STALE) {
   1366 			rt->rt_flags &= ~RTF_REJECT;
   1367 			if (ln->ln_hold) {
   1368 				(*ifp->if_output)(ifp, ln->ln_hold,
   1369 						  rt_key(rt), rt);
   1370 				ln->ln_hold = 0;
   1371 			}
   1372 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
   1373 			/* probe right away */
   1374 			ln->ln_expire = time_second;
   1375 		}
   1376 	}
   1377 
   1378 	/*
   1379 	 * ICMP6 type dependent behavior.
   1380 	 *
   1381 	 * NS: clear IsRouter if new entry
   1382 	 * RS: clear IsRouter
   1383 	 * RA: set IsRouter if there's lladdr
   1384 	 * redir: clear IsRouter if new entry
   1385 	 *
   1386 	 * RA case, (1):
   1387 	 * The spec says that we must set IsRouter in the following cases:
   1388 	 * - If lladdr exist, set IsRouter.  This means (1-5).
   1389 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
   1390 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
   1391 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
   1392 	 * neighbor cache, this is similar to (6).
   1393 	 * This case is rare but we figured that we MUST NOT set IsRouter.
   1394 	 *
   1395 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
   1396 	 *							D R
   1397 	 *	0	n	n	--	(1)	c   ?     s
   1398 	 *	0	y	n	--	(2)	c   s     s
   1399 	 *	0	n	y	--	(3)	c   s     s
   1400 	 *	0	y	y	n	(4)	c   s     s
   1401 	 *	0	y	y	y	(5)	c   s     s
   1402 	 *	1	--	n	--	(6) c	c 	c s
   1403 	 *	1	--	y	--	(7) c	c   s	c s
   1404 	 *
   1405 	 *					(c=clear s=set)
   1406 	 */
   1407 	switch (type & 0xff) {
   1408 	case ND_NEIGHBOR_SOLICIT:
   1409 		/*
   1410 		 * New entry must have is_router flag cleared.
   1411 		 */
   1412 		if (is_newentry)	/*(6-7)*/
   1413 			ln->ln_router = 0;
   1414 		break;
   1415 	case ND_REDIRECT:
   1416 		/*
   1417 		 * If the icmp is a redirect to a better router, always set the
   1418 		 * is_router flag. Otherwise, if the entry is newly created,
   1419 		 * clear the flag. [RFC 2461, sec 8.3]
   1420 		 *
   1421 		 */
   1422 		if (code == ND_REDIRECT_ROUTER)
   1423 			ln->ln_router = 1;
   1424 		else if (is_newentry) /*(6-7)*/
   1425 			ln->ln_router = 0;
   1426 		break;
   1427 	case ND_ROUTER_SOLICIT:
   1428 		/*
   1429 		 * is_router flag must always be cleared.
   1430 		 */
   1431 		ln->ln_router = 0;
   1432 		break;
   1433 	case ND_ROUTER_ADVERT:
   1434 		/*
   1435 		 * Mark an entry with lladdr as a router.
   1436 		 */
   1437 		if ((!is_newentry && (olladdr || lladdr))	/*(2-5)*/
   1438 		 || (is_newentry && lladdr)) {			/*(7)*/
   1439 			ln->ln_router = 1;
   1440 		}
   1441 		break;
   1442 	}
   1443 
   1444 	return rt;
   1445 }
   1446 
   1447 static void
   1448 nd6_slowtimo(ignored_arg)
   1449     void *ignored_arg;
   1450 {
   1451 	int s = splsoftnet();
   1452 	register int i;
   1453 	register struct nd_ifinfo *nd6if;
   1454 
   1455 	timeout(nd6_slowtimo, (caddr_t)0, ND6_SLOWTIMER_INTERVAL * hz);
   1456 	for (i = 1; i < if_index + 1; i++) {
   1457 		nd6if = &nd_ifinfo[i];
   1458 		if (nd6if->basereachable && /* already initialized */
   1459 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
   1460 			/*
   1461 			 * Since reachable time rarely changes by router
   1462 			 * advertisements, we SHOULD insure that a new random
   1463 			 * value gets recomputed at least once every few hours.
   1464 			 * (RFC 2461, 6.3.4)
   1465 			 */
   1466 			nd6if->recalctm = nd6_recalc_reachtm_interval;
   1467 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
   1468 		}
   1469 	}
   1470 	splx(s);
   1471 }
   1472 
   1473 #ifdef NEWIP6OUTPUT
   1474 /* for experimental */
   1475 #define senderr(e) { error = (e); goto bad;}
   1476 int
   1477 nd6_output(ifp, m0, dst, rt0)
   1478 	register struct ifnet *ifp;
   1479 	struct mbuf *m0;
   1480 	struct sockaddr_in6 *dst;
   1481 	struct rtentry *rt0;
   1482 {
   1483 	register struct mbuf *m = m0;
   1484 	register struct rtentry *rt = rt0;
   1485 	struct llinfo_nd6 *ln = NULL;
   1486 	int error = 0;
   1487 
   1488 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
   1489 		goto sendpkt;
   1490 
   1491 	/*
   1492 	 * XXX: we currently do not make neighbor cache on any interface
   1493 	 * other than ARCnet, Ethernet and FDDI.
   1494 	 */
   1495 	switch (ifp->if_type) {
   1496 	case IFT_ARCNET:
   1497 	case IFT_ETHER:
   1498 	case IFT_FDDI:
   1499 		break;
   1500 	default:
   1501 		goto sendpkt;
   1502 	}
   1503 
   1504 	/*
   1505 	 * next hop determination. This routine is derived from ether_outpout.
   1506 	 */
   1507 	if (rt) {
   1508 		if ((rt->rt_flags & RTF_UP) == 0) {
   1509 #ifdef __FreeBSD__
   1510 			if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) !=
   1511 				NULL) {
   1512 #else
   1513 			if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1)) !=
   1514 				NULL) {
   1515 #endif
   1516 				rt->rt_refcnt--;
   1517 				if (rt->rt_ifp != ifp)
   1518 					return nd6_output(ifp, m0, dst, rt); /* XXX: loop care? */
   1519 			} else
   1520 				senderr(EHOSTUNREACH);
   1521 		}
   1522 		if (rt->rt_flags & RTF_GATEWAY) {
   1523 			if (rt->rt_gwroute == 0)
   1524 				goto lookup;
   1525 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
   1526 				rtfree(rt); rt = rt0;
   1527 #ifdef __FreeBSD__
   1528 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL);
   1529 #else
   1530 			lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1);
   1531 #endif
   1532 				if ((rt = rt->rt_gwroute) == 0)
   1533 					senderr(EHOSTUNREACH);
   1534 #ifdef __bsdi__
   1535 				/* the "G" test below also prevents rt == rt0 */
   1536 				if ((rt->rt_flags & RTF_GATEWAY) ||
   1537 				    (rt->rt_ifp != ifp)) {
   1538 					rt->rt_refcnt--;
   1539 					rt0->rt_gwroute = 0;
   1540 					senderr(EHOSTUNREACH);
   1541 				}
   1542 #endif
   1543 			}
   1544 		}
   1545 		if (rt->rt_flags & RTF_REJECT)
   1546 			senderr(rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
   1547 	}
   1548 
   1549 	/*
   1550 	 * Address resolution or Neighbor Unreachability Detection
   1551 	 * for the next hop.
   1552 	 * At this point, the destination of the packet must be a unicast
   1553 	 * or an anycast address(i.e. not a multicast).
   1554 	 */
   1555 
   1556 	/* Look up the neighbor cache for the nexthop */
   1557 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
   1558 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1559 	else {
   1560 		if ((rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
   1561 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
   1562 	}
   1563 	if (!ln || !rt) {
   1564 		log(LOG_DEBUG, "nd6_output: can't allocate llinfo for %s "
   1565 		    "(ln=%p, rt=%p)\n",
   1566 		    ip6_sprintf(&dst->sin6_addr), ln, rt);
   1567 		senderr(EIO);	/* XXX: good error? */
   1568 	}
   1569 
   1570 
   1571 	/*
   1572 	 * The first time we send a packet to a neighbor whose entry is
   1573 	 * STALE, we have to change the state to DELAY and a sets a timer to
   1574 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
   1575 	 * neighbor unreachability detection on expiration.
   1576 	 * (RFC 2461 7.3.3)
   1577 	 */
   1578 	if (ln->ln_state == ND6_LLINFO_STALE) {
   1579 		ln->ln_asked = 0;
   1580 		ln->ln_state = ND6_LLINFO_DELAY;
   1581 		ln->ln_expire = time_second + nd6_delay;
   1582 	}
   1583 
   1584 	/*
   1585 	 * If the neighbor cache entry has a state other than INCOMPLETE
   1586 	 * (i.e. its link-layer address is already reloved), just
   1587 	 * send the packet.
   1588 	 */
   1589 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
   1590 		goto sendpkt;
   1591 
   1592 	/*
   1593 	 * There is a neighbor cache entry, but no ethernet address
   1594 	 * response yet. Replace the held mbuf (if any) with this
   1595 	 * latest one.
   1596 	 *
   1597 	 * XXX Does the code conform to rate-limiting rule?
   1598 	 * (RFC 2461 7.2.2)
   1599 	 */
   1600 	if (ln->ln_state == ND6_LLINFO_WAITDELETE ||
   1601 	    ln->ln_state == ND6_LLINFO_NOSTATE)
   1602 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
   1603 	if (ln->ln_hold)
   1604 		m_freem(ln->ln_hold);
   1605 	ln->ln_hold = m;
   1606 	if (ln->ln_expire) {
   1607 		rt->rt_flags &= ~RTF_REJECT;
   1608 		if (ln->ln_asked < nd6_mmaxtries &&
   1609 		    ln->ln_expire < time_second) {
   1610 			ln->ln_asked++;
   1611 			ln->ln_expire = time_second +
   1612 				nd_ifinfo[ifp->if_index].retrans / 1000;
   1613 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
   1614 		}
   1615 	}
   1616 	return(0);
   1617 
   1618   sendpkt:
   1619 	return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
   1620 
   1621   bad:
   1622 	if (m)
   1623 		m_freem(m);
   1624 	return (error);
   1625 }
   1626 #undef senderr
   1627 
   1628 int
   1629 nd6_storelladdr(ifp, rt, m, dst, desten)
   1630 	struct ifnet *ifp;
   1631 	struct rtentry *rt;
   1632 	struct mbuf *m;
   1633 	struct sockaddr *dst;
   1634 	u_char *desten;
   1635 {
   1636 	struct sockaddr_dl *sdl;
   1637 
   1638 	if (m->m_flags & M_MCAST) {
   1639 		switch (ifp->if_type) {
   1640 		case IFT_ETHER:
   1641 		case IFT_FDDI:
   1642 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
   1643 						 desten);
   1644 			return(1);
   1645 			break;
   1646 		case IFT_ARCNET:
   1647 			*desten = 0;
   1648 			return(1);
   1649 			break;
   1650 		default:
   1651 			return(0);
   1652 		}
   1653 	}
   1654 
   1655 	if (rt == NULL ||
   1656 	    rt->rt_gateway->sa_family != AF_LINK) {
   1657 		printf("nd6_storelladdr: something odd happens\n");
   1658 		return(0);
   1659 	}
   1660 	sdl = SDL(rt->rt_gateway);
   1661 	if (sdl->sdl_alen != 0)
   1662 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
   1663 
   1664 	return(1);
   1665 }
   1666 #endif /* NEWIP6OUTPUT */
   1667