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