xref: /openbsd-src/sys/netinet6/nd6.c (revision 3374c67d44f9b75b98444cbf63020f777792342e)
1 /*	$OpenBSD: nd6.c,v 1.264 2023/01/06 14:35:34 kn Exp $	*/
2 /*	$KAME: nd6.c,v 1.280 2002/06/08 19:52:07 itojun Exp $	*/
3 
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/timeout.h>
36 #include <sys/malloc.h>
37 #include <sys/mbuf.h>
38 #include <sys/socket.h>
39 #include <sys/sockio.h>
40 #include <sys/time.h>
41 #include <sys/kernel.h>
42 #include <sys/pool.h>
43 #include <sys/errno.h>
44 #include <sys/ioctl.h>
45 #include <sys/syslog.h>
46 #include <sys/queue.h>
47 #include <sys/stdint.h>
48 #include <sys/task.h>
49 
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/if_ether.h>
57 #include <netinet/ip_ipsp.h>
58 
59 #include <netinet6/in6_var.h>
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #include <netinet6/nd6.h>
63 #include <netinet/icmp6.h>
64 
65 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
66 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
67 
68 /* timer values */
69 int	nd6_timer_next	= -1;	/* at which uptime nd6_timer runs */
70 time_t	nd6_expire_next	= -1;	/* at which uptime nd6_expire runs */
71 int	nd6_delay	= 5;	/* delay first probe time 5 second */
72 int	nd6_umaxtries	= 3;	/* maximum unicast query */
73 int	nd6_mmaxtries	= 3;	/* maximum multicast query */
74 int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
75 
76 /* preventing too many loops in ND option parsing */
77 int nd6_maxndopt = 10;	/* max # of ND options allowed */
78 
79 int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
80 
81 #ifdef ND6_DEBUG
82 int nd6_debug = 1;
83 #else
84 int nd6_debug = 0;
85 #endif
86 
87 TAILQ_HEAD(llinfo_nd6_head, llinfo_nd6) nd6_list;
88 struct	pool nd6_pool;		/* pool for llinfo_nd6 structures */
89 int	nd6_inuse;
90 
91 void nd6_timer(void *);
92 void nd6_slowtimo(void *);
93 void nd6_expire(void *);
94 void nd6_expire_timer(void *);
95 void nd6_invalidate(struct rtentry *);
96 void nd6_free(struct rtentry *);
97 int nd6_llinfo_timer(struct rtentry *);
98 
99 struct timeout nd6_timer_to;
100 struct timeout nd6_slowtimo_ch;
101 struct timeout nd6_expire_timeout;
102 struct task nd6_expire_task;
103 
104 void
105 nd6_init(void)
106 {
107 	TAILQ_INIT(&nd6_list);
108 	pool_init(&nd6_pool, sizeof(struct llinfo_nd6), 0,
109 	    IPL_SOFTNET, 0, "nd6", NULL);
110 
111 	task_set(&nd6_expire_task, nd6_expire, NULL);
112 
113 	/* start timer */
114 	timeout_set_proc(&nd6_timer_to, nd6_timer, NULL);
115 	timeout_set_proc(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
116 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
117 	timeout_set(&nd6_expire_timeout, nd6_expire_timer, NULL);
118 }
119 
120 void
121 nd6_ifattach(struct ifnet *ifp)
122 {
123 	struct nd_ifinfo *nd;
124 
125 	nd = malloc(sizeof(*nd), M_IP6NDP, M_WAITOK | M_ZERO);
126 
127 	nd->reachable = ND_COMPUTE_RTIME(REACHABLE_TIME);
128 
129 	ifp->if_nd = nd;
130 }
131 
132 void
133 nd6_ifdetach(struct ifnet *ifp)
134 {
135 	struct nd_ifinfo *nd = ifp->if_nd;
136 
137 	free(nd, M_IP6NDP, sizeof(*nd));
138 }
139 
140 /*
141  * Parse multiple ND options.
142  * This function is much easier to use, for ND routines that do not need
143  * multiple options of the same type.
144  */
145 int
146 nd6_options(void *opt, int icmp6len, struct nd_opts *ndopts)
147 {
148 	struct nd_opt_hdr *nd_opt = opt, *next_opt, *last_opt;
149 	int i = 0;
150 
151 	bzero(ndopts, sizeof(*ndopts));
152 
153 	if (icmp6len == 0)
154 		return 0;
155 
156 	next_opt = nd_opt;
157 	last_opt = (struct nd_opt_hdr *)((u_char *)nd_opt + icmp6len);
158 
159 	while (1) {
160 		int olen;
161 
162 		if (next_opt == NULL)
163 			goto skip1;
164 
165 		nd_opt = next_opt;
166 
167 		/* make sure nd_opt_len is inside the buffer */
168 		if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)last_opt)
169 			goto invalid;
170 
171 		/* every option must have a length greater than zero */
172 		olen = nd_opt->nd_opt_len << 3;
173 		if (olen == 0)
174 			goto invalid;
175 
176 		next_opt = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
177 		if (next_opt > last_opt) {
178 			/* option overruns the end of buffer */
179 			goto invalid;
180 		} else if (next_opt == last_opt) {
181 			/* reached the end of options chain */
182 			next_opt = NULL;
183 		}
184 
185 		switch (nd_opt->nd_opt_type) {
186 		case ND_OPT_SOURCE_LINKADDR:
187 			if (ndopts->nd_opts_src_lladdr != NULL)
188 				nd6log((LOG_INFO, "duplicated ND6 option found "
189 				    "(type=%d)\n", nd_opt->nd_opt_type));
190 			else
191 				ndopts->nd_opts_src_lladdr = nd_opt;
192 			break;
193 		case ND_OPT_TARGET_LINKADDR:
194 			if (ndopts->nd_opts_tgt_lladdr != NULL)
195 				nd6log((LOG_INFO, "duplicated ND6 option found "
196 				    "(type=%d)\n", nd_opt->nd_opt_type));
197 			else
198 				ndopts->nd_opts_tgt_lladdr = nd_opt;
199 			break;
200 		case ND_OPT_MTU:
201 		case ND_OPT_REDIRECTED_HEADER:
202 		case ND_OPT_PREFIX_INFORMATION:
203 		case ND_OPT_DNSSL:
204 		case ND_OPT_RDNSS:
205 			/* Don't warn, not used by kernel */
206 			break;
207 		default:
208 			/*
209 			 * Unknown options must be silently ignored,
210 			 * to accommodate future extension to the protocol.
211 			 */
212 			nd6log((LOG_DEBUG,
213 			    "nd6_options: unsupported option %d - "
214 			    "option ignored\n", nd_opt->nd_opt_type));
215 			break;
216 		}
217 
218 skip1:
219 		i++;
220 		if (i > nd6_maxndopt) {
221 			icmp6stat_inc(icp6s_nd_toomanyopt);
222 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
223 			break;
224 		}
225 
226 		if (next_opt == NULL)
227 			break;
228 	}
229 
230 	return 0;
231 
232 invalid:
233 	bzero(ndopts, sizeof(*ndopts));
234 	icmp6stat_inc(icp6s_nd_badopt);
235 	return -1;
236 }
237 
238 /*
239  * ND6 timer routine to handle ND6 entries
240  */
241 void
242 nd6_llinfo_settimer(const struct llinfo_nd6 *ln, unsigned int secs)
243 {
244 	time_t expire = getuptime() + secs;
245 
246 	NET_ASSERT_LOCKED();
247 	KASSERT(!ISSET(ln->ln_rt->rt_flags, RTF_LOCAL));
248 
249 	ln->ln_rt->rt_expire = expire;
250 	if (!timeout_pending(&nd6_timer_to) || expire < nd6_timer_next) {
251 		nd6_timer_next = expire;
252 		timeout_add_sec(&nd6_timer_to, secs);
253 	}
254 }
255 
256 void
257 nd6_timer(void *unused)
258 {
259 	struct llinfo_nd6 *ln, *nln;
260 	time_t expire = getuptime() + nd6_gctimer;
261 	int secs;
262 
263 	NET_LOCK();
264 	TAILQ_FOREACH_SAFE(ln, &nd6_list, ln_list, nln) {
265 		struct rtentry *rt = ln->ln_rt;
266 
267 		if (rt->rt_expire && rt->rt_expire <= getuptime())
268 			if (nd6_llinfo_timer(rt))
269 				continue;
270 
271 		if (rt->rt_expire && rt->rt_expire < expire)
272 			expire = rt->rt_expire;
273 	}
274 
275 	secs = expire - getuptime();
276 	if (secs < 0)
277 		secs = 0;
278 	if (!TAILQ_EMPTY(&nd6_list)) {
279 		nd6_timer_next = getuptime() + secs;
280 		timeout_add_sec(&nd6_timer_to, secs);
281 	}
282 
283 	NET_UNLOCK();
284 }
285 
286 /*
287  * ND timer state handling.
288  *
289  * Returns 1 if `rt' should no longer be used, 0 otherwise.
290  */
291 int
292 nd6_llinfo_timer(struct rtentry *rt)
293 {
294 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
295 	struct sockaddr_in6 *dst = satosin6(rt_key(rt));
296 	struct ifnet *ifp;
297 
298 	NET_ASSERT_LOCKED();
299 
300 	if ((ifp = if_get(rt->rt_ifidx)) == NULL)
301 		return 1;
302 
303 	switch (ln->ln_state) {
304 	case ND6_LLINFO_INCOMPLETE:
305 		if (ln->ln_asked < nd6_mmaxtries) {
306 			ln->ln_asked++;
307 			nd6_llinfo_settimer(ln, RETRANS_TIMER / 1000);
308 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
309 		} else {
310 			struct mbuf *m = ln->ln_hold;
311 			if (m) {
312 				ln->ln_hold = NULL;
313 				/*
314 				 * Fake rcvif to make the ICMP error
315 				 * more helpful in diagnosing for the
316 				 * receiver.
317 				 * XXX: should we consider
318 				 * older rcvif?
319 				 */
320 				m->m_pkthdr.ph_ifidx = rt->rt_ifidx;
321 
322 				icmp6_error(m, ICMP6_DST_UNREACH,
323 				    ICMP6_DST_UNREACH_ADDR, 0);
324 				if (ln->ln_hold == m) {
325 					/* m is back in ln_hold. Discard. */
326 					m_freem(ln->ln_hold);
327 					ln->ln_hold = NULL;
328 				}
329 			}
330 			nd6_free(rt);
331 			ln = NULL;
332 		}
333 		break;
334 	case ND6_LLINFO_REACHABLE:
335 		if (!ND6_LLINFO_PERMANENT(ln)) {
336 			ln->ln_state = ND6_LLINFO_STALE;
337 			nd6_llinfo_settimer(ln, nd6_gctimer);
338 		}
339 		break;
340 
341 	case ND6_LLINFO_STALE:
342 	case ND6_LLINFO_PURGE:
343 		/* Garbage Collection(RFC 2461 5.3) */
344 		if (!ND6_LLINFO_PERMANENT(ln)) {
345 			nd6_free(rt);
346 			ln = NULL;
347 		}
348 		break;
349 
350 	case ND6_LLINFO_DELAY:
351 		/* We need NUD */
352 		ln->ln_asked = 1;
353 		ln->ln_state = ND6_LLINFO_PROBE;
354 		nd6_llinfo_settimer(ln, RETRANS_TIMER / 1000);
355 		nd6_ns_output(ifp, &dst->sin6_addr, &dst->sin6_addr, ln, 0);
356 		break;
357 	case ND6_LLINFO_PROBE:
358 		if (ln->ln_asked < nd6_umaxtries) {
359 			ln->ln_asked++;
360 			nd6_llinfo_settimer(ln, RETRANS_TIMER / 1000);
361 			nd6_ns_output(ifp, &dst->sin6_addr,
362 			    &dst->sin6_addr, ln, 0);
363 		} else {
364 			nd6_free(rt);
365 			ln = NULL;
366 		}
367 		break;
368 	}
369 
370 	if_put(ifp);
371 
372 	return (ln == NULL);
373 }
374 
375 void
376 nd6_expire_timer_update(struct in6_ifaddr *ia6)
377 {
378 	time_t expire_time = INT64_MAX;
379 	int secs;
380 
381 	if (ia6->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME)
382 		expire_time = ia6->ia6_lifetime.ia6t_expire;
383 
384 	if (!(ia6->ia6_flags & IN6_IFF_DEPRECATED) &&
385 	    ia6->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME &&
386 	    expire_time > ia6->ia6_lifetime.ia6t_preferred)
387 		expire_time = ia6->ia6_lifetime.ia6t_preferred;
388 
389 	if (expire_time == INT64_MAX)
390 		return;
391 
392 	/*
393 	 * IFA6_IS_INVALID() and IFA6_IS_DEPRECATED() check for uptime
394 	 * greater than ia6t_expire or ia6t_preferred, not greater or equal.
395 	 * Schedule timeout one second later so that either IFA6_IS_INVALID()
396 	 * or IFA6_IS_DEPRECATED() is true.
397 	 */
398 	expire_time++;
399 
400 	if (!timeout_pending(&nd6_expire_timeout) ||
401 	    nd6_expire_next > expire_time) {
402 		secs = expire_time - getuptime();
403 		if (secs < 0)
404 			secs = 0;
405 
406 		timeout_add_sec(&nd6_expire_timeout, secs);
407 		nd6_expire_next = expire_time;
408 	}
409 }
410 
411 /*
412  * Expire interface addresses.
413  */
414 void
415 nd6_expire(void *unused)
416 {
417 	struct ifnet *ifp;
418 
419 	NET_LOCK();
420 
421 	TAILQ_FOREACH(ifp, &ifnetlist, if_list) {
422 		struct ifaddr *ifa, *nifa;
423 		struct in6_ifaddr *ia6;
424 
425 		TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrlist, ifa_list, nifa) {
426 			if (ifa->ifa_addr->sa_family != AF_INET6)
427 				continue;
428 			ia6 = ifatoia6(ifa);
429 			/* check address lifetime */
430 			if (IFA6_IS_INVALID(ia6)) {
431 				in6_purgeaddr(&ia6->ia_ifa);
432 			} else {
433 				if (IFA6_IS_DEPRECATED(ia6))
434 					ia6->ia6_flags |= IN6_IFF_DEPRECATED;
435 				nd6_expire_timer_update(ia6);
436 			}
437 		}
438 	}
439 
440 	NET_UNLOCK();
441 }
442 
443 void
444 nd6_expire_timer(void *unused)
445 {
446 	task_add(net_tq(0), &nd6_expire_task);
447 }
448 
449 /*
450  * Nuke neighbor cache/prefix/default router management table, right before
451  * ifp goes away.
452  */
453 void
454 nd6_purge(struct ifnet *ifp)
455 {
456 	struct llinfo_nd6 *ln, *nln;
457 
458 	NET_ASSERT_LOCKED();
459 
460 	/*
461 	 * Nuke neighbor cache entries for the ifp.
462 	 */
463 	TAILQ_FOREACH_SAFE(ln, &nd6_list, ln_list, nln) {
464 		struct rtentry *rt;
465 		struct sockaddr_dl *sdl;
466 
467 		rt = ln->ln_rt;
468 		if (rt != NULL && rt->rt_gateway != NULL &&
469 		    rt->rt_gateway->sa_family == AF_LINK) {
470 			sdl = satosdl(rt->rt_gateway);
471 			if (sdl->sdl_index == ifp->if_index)
472 				nd6_free(rt);
473 		}
474 	}
475 }
476 
477 struct rtentry *
478 nd6_lookup(const struct in6_addr *addr6, int create, struct ifnet *ifp,
479     u_int rtableid)
480 {
481 	struct rtentry *rt;
482 	struct sockaddr_in6 sin6;
483 	int flags;
484 
485 	bzero(&sin6, sizeof(sin6));
486 	sin6.sin6_len = sizeof(struct sockaddr_in6);
487 	sin6.sin6_family = AF_INET6;
488 	sin6.sin6_addr = *addr6;
489 	flags = (create) ? RT_RESOLVE : 0;
490 
491 	rt = rtalloc(sin6tosa(&sin6), flags, rtableid);
492 	if (rt != NULL && (rt->rt_flags & RTF_LLINFO) == 0) {
493 		/*
494 		 * This is the case for the default route.
495 		 * If we want to create a neighbor cache for the address, we
496 		 * should free the route for the destination and allocate an
497 		 * interface route.
498 		 */
499 		if (create) {
500 			rtfree(rt);
501 			rt = NULL;
502 		}
503 	}
504 	if (rt == NULL) {
505 		if (create && ifp) {
506 			struct rt_addrinfo info;
507 			struct ifaddr *ifa;
508 			int error;
509 
510 			/*
511 			 * If no route is available and create is set,
512 			 * we allocate a host route for the destination
513 			 * and treat it like an interface route.
514 			 * This hack is necessary for a neighbor which can't
515 			 * be covered by our own prefix.
516 			 */
517 			ifa = ifaof_ifpforaddr(sin6tosa(&sin6), ifp);
518 			if (ifa == NULL)
519 				return (NULL);
520 
521 			/*
522 			 * Create a new route.  RTF_LLINFO is necessary
523 			 * to create a Neighbor Cache entry for the
524 			 * destination in nd6_rtrequest which will be
525 			 * called in rtrequest.
526 			 */
527 			bzero(&info, sizeof(info));
528 			info.rti_ifa = ifa;
529 			info.rti_flags = RTF_HOST | RTF_LLINFO;
530 			info.rti_info[RTAX_DST] = sin6tosa(&sin6);
531 			info.rti_info[RTAX_GATEWAY] = sdltosa(ifp->if_sadl);
532 			error = rtrequest(RTM_ADD, &info, RTP_CONNECTED, &rt,
533 			    rtableid);
534 			if (error)
535 				return (NULL);
536 			if (rt->rt_llinfo != NULL) {
537 				struct llinfo_nd6 *ln =
538 				    (struct llinfo_nd6 *)rt->rt_llinfo;
539 				ln->ln_state = ND6_LLINFO_NOSTATE;
540 			}
541 		} else
542 			return (NULL);
543 	}
544 	/*
545 	 * Validation for the entry.
546 	 * Note that the check for rt_llinfo is necessary because a cloned
547 	 * route from a parent route that has the L flag (e.g. the default
548 	 * route to a p2p interface) may have the flag, too, while the
549 	 * destination is not actually a neighbor.
550 	 */
551 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
552 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
553 	    (ifp != NULL && rt->rt_ifidx != ifp->if_index)) {
554 		if (create) {
555 			char addr[INET6_ADDRSTRLEN];
556 			nd6log((LOG_DEBUG, "%s: failed to lookup %s (if=%s)\n",
557 			    __func__,
558 			    inet_ntop(AF_INET6, addr6, addr, sizeof(addr)),
559 			    ifp ? ifp->if_xname : "unspec"));
560 		}
561 		rtfree(rt);
562 		return (NULL);
563 	}
564 	return (rt);
565 }
566 
567 /*
568  * Detect if a given IPv6 address identifies a neighbor on a given link.
569  * XXX: should take care of the destination of a p2p link?
570  */
571 int
572 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
573 {
574 	struct in6_ifaddr *ia6;
575 	struct ifaddr *ifa;
576 	struct rtentry *rt;
577 
578 	/*
579 	 * A link-local address is always a neighbor.
580 	 * XXX: we should use the sin6_scope_id field rather than the embedded
581 	 * interface index.
582 	 * XXX: a link does not necessarily specify a single interface.
583 	 */
584 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
585 	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
586 		return (1);
587 
588 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
589 		if (ifa->ifa_addr->sa_family != AF_INET6)
590 			continue;
591 
592 		ia6 = ifatoia6(ifa);
593 
594 		/* Prefix check down below. */
595 		if (ia6->ia6_flags & IN6_IFF_AUTOCONF)
596 			continue;
597 
598 		if (IN6_ARE_MASKED_ADDR_EQUAL(&addr->sin6_addr,
599 		    &ia6->ia_addr.sin6_addr,
600 		    &ia6->ia_prefixmask.sin6_addr))
601 			return (1);
602 	}
603 
604 	/*
605 	 * Even if the address matches none of our addresses, it might be
606 	 * in the neighbor cache.
607 	 */
608 	rt = nd6_lookup(&addr->sin6_addr, 0, ifp, ifp->if_rdomain);
609 	if (rt != NULL) {
610 		rtfree(rt);
611 		return (1);
612 	}
613 
614 	return (0);
615 }
616 
617 void
618 nd6_invalidate(struct rtentry *rt)
619 {
620 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
621 	struct sockaddr_dl *sdl = satosdl(rt->rt_gateway);
622 
623 	m_freem(ln->ln_hold);
624 	sdl->sdl_alen = 0;
625 	ln->ln_hold = NULL;
626 	ln->ln_state = ND6_LLINFO_INCOMPLETE;
627 	ln->ln_asked = 0;
628 }
629 
630 /*
631  * Free an nd6 llinfo entry.
632  */
633 void
634 nd6_free(struct rtentry *rt)
635 {
636 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
637 	struct in6_addr in6 = satosin6(rt_key(rt))->sin6_addr;
638 	struct ifnet *ifp;
639 
640 	NET_ASSERT_LOCKED();
641 
642 	ifp = if_get(rt->rt_ifidx);
643 
644 	if (!ip6_forwarding) {
645 		if (ln->ln_router) {
646 			/*
647 			 * rt6_flush must be called whether or not the neighbor
648 			 * is in the Default Router List.
649 			 * See a corresponding comment in nd6_na_input().
650 			 */
651 			rt6_flush(&in6, ifp);
652 		}
653 	}
654 
655 	KASSERT(!ISSET(rt->rt_flags, RTF_LOCAL));
656 	nd6_invalidate(rt);
657 
658 	/*
659 	 * Detach the route from the routing tree and the list of neighbor
660 	 * caches, and disable the route entry not to be used in already
661 	 * cached routes.
662 	 */
663 	if (!ISSET(rt->rt_flags, RTF_STATIC|RTF_CACHED))
664 		rtdeletemsg(rt, ifp, ifp->if_rdomain);
665 
666 	if_put(ifp);
667 }
668 
669 /*
670  * Upper-layer reachability hint for Neighbor Unreachability Detection.
671  *
672  * XXX cost-effective methods?
673  */
674 void
675 nd6_nud_hint(struct rtentry *rt)
676 {
677 	struct llinfo_nd6 *ln;
678 	struct ifnet *ifp;
679 
680 	ifp = if_get(rt->rt_ifidx);
681 	if (ifp == NULL)
682 		return;
683 
684 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
685 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
686 	    rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
687 	    rt->rt_gateway->sa_family != AF_LINK) {
688 		/* This is not a host route. */
689 		goto out;
690 	}
691 
692 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
693 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
694 		goto out;
695 
696 	/*
697 	 * if we get upper-layer reachability confirmation many times,
698 	 * it is possible we have false information.
699 	 */
700 	ln->ln_byhint++;
701 	if (ln->ln_byhint > nd6_maxnudhint)
702 		goto out;
703 
704 	ln->ln_state = ND6_LLINFO_REACHABLE;
705 	if (!ND6_LLINFO_PERMANENT(ln))
706 		nd6_llinfo_settimer(ln, ifp->if_nd->reachable);
707 out:
708 	if_put(ifp);
709 }
710 
711 void
712 nd6_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt)
713 {
714 	struct sockaddr *gate = rt->rt_gateway;
715 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
716 	struct ifaddr *ifa;
717 	struct in6_ifaddr *ifa6;
718 
719 	if (ISSET(rt->rt_flags, RTF_GATEWAY|RTF_MULTICAST|RTF_MPLS))
720 		return;
721 
722 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
723 		/*
724 		 * This is probably an interface direct route for a link
725 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
726 		 * We do not need special treatment below for such a route.
727 		 * Moreover, the RTF_LLINFO flag which would be set below
728 		 * would annoy the ndp(8) command.
729 		 */
730 		return;
731 	}
732 
733 	if (req == RTM_RESOLVE && nd6_need_cache(ifp) == 0) {
734 		/*
735 		 * For routing daemons like ospf6d we allow neighbor discovery
736 		 * based on the cloning route only.  This allows us to send
737 		 * packets directly into a network without having an address
738 		 * with matching prefix on the interface.  If the cloning
739 		 * route is used for an 6to4 interface, we would mistakenly
740 		 * make a neighbor cache for the host route, and would see
741 		 * strange neighbor solicitation for the corresponding
742 		 * destination.  In order to avoid confusion, we check if the
743 		 * interface is suitable for neighbor discovery, and stop the
744 		 * process if not.  Additionally, we remove the LLINFO flag
745 		 * so that ndp(8) will not try to get the neighbor information
746 		 * of the destination.
747 		 */
748 		rt->rt_flags &= ~RTF_LLINFO;
749 		return;
750 	}
751 
752 	switch (req) {
753 	case RTM_ADD:
754 		if ((rt->rt_flags & RTF_CLONING) ||
755 		    ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && ln == NULL)) {
756 			if (ln != NULL)
757 				nd6_llinfo_settimer(ln, 0);
758 			if ((rt->rt_flags & RTF_CLONING) != 0)
759 				break;
760 		}
761 		/*
762 		 * In IPv4 code, we try to announce new RTF_ANNOUNCE entry here.
763 		 * We don't do that here since llinfo is not ready yet.
764 		 *
765 		 * There are also couple of other things to be discussed:
766 		 * - unsolicited NA code needs improvement beforehand
767 		 * - RFC2461 says we MAY send multicast unsolicited NA
768 		 *   (7.2.6 paragraph 4), however, it also says that we
769 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
770 		 *   we don't have anything like it right now.
771 		 *   note that the mechanism needs a mutual agreement
772 		 *   between proxies, which means that we need to implement
773 		 *   a new protocol, or a new kludge.
774 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
775 		 *   we need to check ip6forwarding before sending it.
776 		 *   (or should we allow proxy ND configuration only for
777 		 *   routers?  there's no mention about proxy ND from hosts)
778 		 */
779 #if 0
780 		/* XXX it does not work */
781 		if (rt->rt_flags & RTF_ANNOUNCE)
782 			nd6_na_output(ifp,
783 			      &satosin6(rt_key(rt))->sin6_addr,
784 			      &satosin6(rt_key(rt))->sin6_addr,
785 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
786 			      1, NULL);
787 #endif
788 		/* FALLTHROUGH */
789 	case RTM_RESOLVE:
790 		if (gate->sa_family != AF_LINK ||
791 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
792 			log(LOG_DEBUG, "%s: bad gateway value: %s\n",
793 			    __func__, ifp->if_xname);
794 			break;
795 		}
796 		satosdl(gate)->sdl_type = ifp->if_type;
797 		satosdl(gate)->sdl_index = ifp->if_index;
798 		if (ln != NULL)
799 			break;	/* This happens on a route change */
800 		/*
801 		 * Case 2: This route may come from cloning, or a manual route
802 		 * add with a LL address.
803 		 */
804 		ln = pool_get(&nd6_pool, PR_NOWAIT | PR_ZERO);
805 		rt->rt_llinfo = (caddr_t)ln;
806 		if (ln == NULL) {
807 			log(LOG_DEBUG, "%s: pool get failed\n", __func__);
808 			break;
809 		}
810 		nd6_inuse++;
811 		ln->ln_rt = rt;
812 		/* this is required for "ndp" command. - shin */
813 		if (req == RTM_ADD) {
814 		        /*
815 			 * gate should have some valid AF_LINK entry,
816 			 * and ln expire should have some lifetime
817 			 * which is specified by ndp command.
818 			 */
819 			ln->ln_state = ND6_LLINFO_REACHABLE;
820 			ln->ln_byhint = 0;
821 		} else {
822 		        /*
823 			 * When req == RTM_RESOLVE, rt is created and
824 			 * initialized in rtrequest(), so rt_expire is 0.
825 			 */
826 			ln->ln_state = ND6_LLINFO_NOSTATE;
827 			nd6_llinfo_settimer(ln, 0);
828 		}
829 		rt->rt_flags |= RTF_LLINFO;
830 		TAILQ_INSERT_HEAD(&nd6_list, ln, ln_list);
831 
832 		/*
833 		 * If we have too many cache entries, initiate immediate
834 		 * purging for some "less recently used" entries.  Note that
835 		 * we cannot directly call nd6_free() here because it would
836 		 * cause re-entering rtable related routines triggering
837 		 * lock-order-reversal problems.
838 		 */
839 		if (ip6_neighborgcthresh >= 0 &&
840 		    nd6_inuse >= ip6_neighborgcthresh) {
841 			int i;
842 
843 			for (i = 0; i < 10; i++) {
844 				struct llinfo_nd6 *ln_end;
845 
846 				ln_end = TAILQ_LAST(&nd6_list, llinfo_nd6_head);
847 				if (ln_end == ln)
848 					break;
849 
850 				/* Move this entry to the head */
851 				TAILQ_REMOVE(&nd6_list, ln_end, ln_list);
852 				TAILQ_INSERT_HEAD(&nd6_list, ln_end, ln_list);
853 
854 				if (ND6_LLINFO_PERMANENT(ln_end))
855 					continue;
856 
857 				if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
858 					ln_end->ln_state = ND6_LLINFO_STALE;
859 				else
860 					ln_end->ln_state = ND6_LLINFO_PURGE;
861 				nd6_llinfo_settimer(ln_end, 0);
862 			}
863 		}
864 
865 		/*
866 		 * check if rt_key(rt) is one of my address assigned
867 		 * to the interface.
868 		 */
869 		ifa6 = in6ifa_ifpwithaddr(ifp,
870 		    &satosin6(rt_key(rt))->sin6_addr);
871 		ifa = ifa6 ? &ifa6->ia_ifa : NULL;
872 		if (ifa) {
873 			ln->ln_state = ND6_LLINFO_REACHABLE;
874 			ln->ln_byhint = 0;
875 			rt->rt_expire = 0;
876 			KASSERT(ifa == rt->rt_ifa);
877 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
878 			ln->ln_state = ND6_LLINFO_REACHABLE;
879 			ln->ln_byhint = 0;
880 			rt->rt_expire = 0;
881 
882 			/* join solicited node multicast for proxy ND */
883 			if (ifp->if_flags & IFF_MULTICAST) {
884 				struct in6_addr llsol;
885 				int error;
886 
887 				llsol = satosin6(rt_key(rt))->sin6_addr;
888 				llsol.s6_addr16[0] = htons(0xff02);
889 				llsol.s6_addr16[1] = htons(ifp->if_index);
890 				llsol.s6_addr32[1] = 0;
891 				llsol.s6_addr32[2] = htonl(1);
892 				llsol.s6_addr8[12] = 0xff;
893 
894 				if (in6_addmulti(&llsol, ifp, &error)) {
895 					char addr[INET6_ADDRSTRLEN];
896 					nd6log((LOG_ERR, "%s: failed to join "
897 					    "%s (errno=%d)\n", ifp->if_xname,
898 					    inet_ntop(AF_INET6, &llsol,
899 						addr, sizeof(addr)),
900 					    error));
901 				}
902 			}
903 		}
904 		break;
905 
906 	case RTM_DELETE:
907 		if (ln == NULL)
908 			break;
909 		/* leave from solicited node multicast for proxy ND */
910 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
911 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
912 			struct in6_addr llsol;
913 			struct in6_multi *in6m;
914 
915 			llsol = satosin6(rt_key(rt))->sin6_addr;
916 			llsol.s6_addr16[0] = htons(0xff02);
917 			llsol.s6_addr16[1] = htons(ifp->if_index);
918 			llsol.s6_addr32[1] = 0;
919 			llsol.s6_addr32[2] = htonl(1);
920 			llsol.s6_addr8[12] = 0xff;
921 
922 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
923 			if (in6m)
924 				in6_delmulti(in6m);
925 		}
926 		nd6_inuse--;
927 		TAILQ_REMOVE(&nd6_list, ln, ln_list);
928 		rt->rt_expire = 0;
929 		rt->rt_llinfo = NULL;
930 		rt->rt_flags &= ~RTF_LLINFO;
931 		m_freem(ln->ln_hold);
932 		pool_put(&nd6_pool, ln);
933 		break;
934 
935 	case RTM_INVALIDATE:
936 		if (ln == NULL)
937 			break;
938 		if (!ISSET(rt->rt_flags, RTF_LOCAL))
939 			nd6_invalidate(rt);
940 		break;
941 	}
942 }
943 
944 int
945 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
946 {
947 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
948 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
949 	struct rtentry *rt;
950 
951 	switch (cmd) {
952 	case SIOCGIFINFO_IN6:
953 		NET_LOCK_SHARED();
954 		ndi->ndi = *ifp->if_nd;
955 		NET_UNLOCK_SHARED();
956 		return (0);
957 	case SIOCGNBRINFO_IN6:
958 	{
959 		struct llinfo_nd6 *ln;
960 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
961 		time_t expire;
962 
963 		NET_LOCK_SHARED();
964 		/*
965 		 * XXX: KAME specific hack for scoped addresses
966 		 *      XXXX: for other scopes than link-local?
967 		 */
968 		if (IN6_IS_ADDR_LINKLOCAL(&nb_addr) ||
969 		    IN6_IS_ADDR_MC_LINKLOCAL(&nb_addr)) {
970 			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
971 
972 			if (*idp == 0)
973 				*idp = htons(ifp->if_index);
974 		}
975 
976 		rt = nd6_lookup(&nb_addr, 0, ifp, ifp->if_rdomain);
977 		if (rt == NULL ||
978 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
979 			rtfree(rt);
980 			NET_UNLOCK_SHARED();
981 			return (EINVAL);
982 		}
983 		expire = ln->ln_rt->rt_expire;
984 		if (expire != 0) {
985 			expire -= getuptime();
986 			expire += gettime();
987 		}
988 
989 		nbi->state = ln->ln_state;
990 		nbi->asked = ln->ln_asked;
991 		nbi->isrouter = ln->ln_router;
992 		nbi->expire = expire;
993 
994 		rtfree(rt);
995 		NET_UNLOCK_SHARED();
996 		return (0);
997 	}
998 	}
999 	return (0);
1000 }
1001 
1002 /*
1003  * Create neighbor cache entry and cache link-layer address,
1004  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1005  *
1006  * type - ICMP6 type
1007  * code - type dependent information
1008  */
1009 void
1010 nd6_cache_lladdr(struct ifnet *ifp, const struct in6_addr *from, char *lladdr,
1011     int lladdrlen, int type, int code)
1012 {
1013 	struct rtentry *rt = NULL;
1014 	struct llinfo_nd6 *ln = NULL;
1015 	int is_newentry;
1016 	struct sockaddr_dl *sdl = NULL;
1017 	int do_update;
1018 	int olladdr;
1019 	int llchange;
1020 	int newstate = 0;
1021 
1022 	if (!ifp)
1023 		panic("%s: ifp == NULL", __func__);
1024 	if (!from)
1025 		panic("%s: from == NULL", __func__);
1026 
1027 	/* nothing must be updated for unspecified address */
1028 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1029 		return;
1030 
1031 	/*
1032 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1033 	 * the caller.
1034 	 *
1035 	 * XXX If the link does not have link-layer address, what should
1036 	 * we do? (ifp->if_addrlen == 0)
1037 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1038 	 * description on it in NS section (RFC 2461 7.2.3).
1039 	 */
1040 
1041 	rt = nd6_lookup(from, 0, ifp, ifp->if_rdomain);
1042 	if (rt == NULL) {
1043 		rt = nd6_lookup(from, 1, ifp, ifp->if_rdomain);
1044 		is_newentry = 1;
1045 	} else {
1046 		/* do not overwrite local or static entry */
1047 		if (ISSET(rt->rt_flags, RTF_STATIC|RTF_LOCAL)) {
1048 			rtfree(rt);
1049 			return;
1050 		}
1051 		is_newentry = 0;
1052 	}
1053 
1054 	if (!rt)
1055 		return;
1056 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1057 fail:
1058 		nd6_free(rt);
1059 		rtfree(rt);
1060 		return;
1061 	}
1062 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1063 	if (ln == NULL)
1064 		goto fail;
1065 	if (rt->rt_gateway == NULL)
1066 		goto fail;
1067 	if (rt->rt_gateway->sa_family != AF_LINK)
1068 		goto fail;
1069 	sdl = satosdl(rt->rt_gateway);
1070 
1071 	olladdr = (sdl->sdl_alen) ? 1 : 0;
1072 	if (olladdr && lladdr) {
1073 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1074 			llchange = 1;
1075 		else
1076 			llchange = 0;
1077 	} else
1078 		llchange = 0;
1079 
1080 	/*
1081 	 * newentry olladdr  lladdr  llchange	(*=record)
1082 	 *	0	n	n	--	(1)
1083 	 *	0	y	n	--	(2)
1084 	 *	0	n	y	--	(3) * STALE
1085 	 *	0	y	y	n	(4) *
1086 	 *	0	y	y	y	(5) * STALE
1087 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1088 	 *	1	--	y	--	(7) * STALE
1089 	 */
1090 
1091 	if (llchange) {
1092 		char addr[INET6_ADDRSTRLEN];
1093 		log(LOG_INFO, "ndp info overwritten for %s by %s on %s\n",
1094 		    inet_ntop(AF_INET6, from, addr, sizeof(addr)),
1095 		    ether_sprintf(lladdr), ifp->if_xname);
1096 	}
1097 	if (lladdr) {		/* (3-5) and (7) */
1098 		/*
1099 		 * Record source link-layer address
1100 		 * XXX is it dependent to ifp->if_type?
1101 		 */
1102 		sdl->sdl_alen = ifp->if_addrlen;
1103 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1104 	}
1105 
1106 	if (!is_newentry) {
1107 		if ((!olladdr && lladdr) ||		/* (3) */
1108 		    (olladdr && lladdr && llchange)) {	/* (5) */
1109 			do_update = 1;
1110 			newstate = ND6_LLINFO_STALE;
1111 		} else					/* (1-2,4) */
1112 			do_update = 0;
1113 	} else {
1114 		do_update = 1;
1115 		if (!lladdr)				/* (6) */
1116 			newstate = ND6_LLINFO_NOSTATE;
1117 		else					/* (7) */
1118 			newstate = ND6_LLINFO_STALE;
1119 	}
1120 
1121 	if (do_update) {
1122 		/*
1123 		 * Update the state of the neighbor cache.
1124 		 */
1125 		ln->ln_state = newstate;
1126 
1127 		if (ln->ln_state == ND6_LLINFO_STALE) {
1128 			/*
1129 			 * Since nd6_resolve() in ifp->if_output() will cause
1130 			 * state transition to DELAY and reset the timer,
1131 			 * we must set the timer now, although it is actually
1132 			 * meaningless.
1133 			 */
1134 			nd6_llinfo_settimer(ln, nd6_gctimer);
1135 
1136 			if (ln->ln_hold) {
1137 				struct mbuf *n = ln->ln_hold;
1138 				ln->ln_hold = NULL;
1139 				/*
1140 				 * we assume ifp is not a p2p here, so just
1141 				 * set the 2nd argument as the 1st one.
1142 				 */
1143 				ifp->if_output(ifp, n, rt_key(rt), rt);
1144 				if (ln->ln_hold == n) {
1145 					/* n is back in ln_hold. Discard. */
1146 					m_freem(ln->ln_hold);
1147 					ln->ln_hold = NULL;
1148 				}
1149 			}
1150 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1151 			/* probe right away */
1152 			nd6_llinfo_settimer(ln, 0);
1153 		}
1154 	}
1155 
1156 	/*
1157 	 * ICMP6 type dependent behavior.
1158 	 *
1159 	 * NS: clear IsRouter if new entry
1160 	 * RS: clear IsRouter
1161 	 * RA: set IsRouter if there's lladdr
1162 	 * redir: clear IsRouter if new entry
1163 	 *
1164 	 * RA case, (1):
1165 	 * The spec says that we must set IsRouter in the following cases:
1166 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1167 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1168 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1169 	 * A question arises for (1) case.  (1) case has no lladdr in the
1170 	 * neighbor cache, this is similar to (6).
1171 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1172 	 *
1173 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1174 	 *							D R
1175 	 *	0	n	n	--	(1)	c   ?     s
1176 	 *	0	y	n	--	(2)	c   s     s
1177 	 *	0	n	y	--	(3)	c   s     s
1178 	 *	0	y	y	n	(4)	c   s     s
1179 	 *	0	y	y	y	(5)	c   s     s
1180 	 *	1	--	n	--	(6) c	c	c s
1181 	 *	1	--	y	--	(7) c	c   s	c s
1182 	 *
1183 	 *					(c=clear s=set)
1184 	 */
1185 	switch (type & 0xff) {
1186 	case ND_NEIGHBOR_SOLICIT:
1187 		/*
1188 		 * New entry must have is_router flag cleared.
1189 		 */
1190 		if (is_newentry)	/* (6-7) */
1191 			ln->ln_router = 0;
1192 		break;
1193 	case ND_REDIRECT:
1194 		/*
1195 		 * If the icmp is a redirect to a better router, always set the
1196 		 * is_router flag.  Otherwise, if the entry is newly created,
1197 		 * clear the flag.  [RFC 2461, sec 8.3]
1198 		 */
1199 		if (code == ND_REDIRECT_ROUTER)
1200 			ln->ln_router = 1;
1201 		else if (is_newentry) /* (6-7) */
1202 			ln->ln_router = 0;
1203 		break;
1204 	case ND_ROUTER_SOLICIT:
1205 		/*
1206 		 * is_router flag must always be cleared.
1207 		 */
1208 		ln->ln_router = 0;
1209 		break;
1210 	case ND_ROUTER_ADVERT:
1211 		/*
1212 		 * Mark an entry with lladdr as a router.
1213 		 */
1214 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1215 		    (is_newentry && lladdr)) {			/* (7) */
1216 			ln->ln_router = 1;
1217 		}
1218 		break;
1219 	}
1220 
1221 	rtfree(rt);
1222 }
1223 
1224 void
1225 nd6_slowtimo(void *ignored_arg)
1226 {
1227 	struct nd_ifinfo *nd6if;
1228 	struct ifnet *ifp;
1229 
1230 	NET_LOCK();
1231 
1232 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
1233 
1234 	TAILQ_FOREACH(ifp, &ifnetlist, if_list) {
1235 		nd6if = ifp->if_nd;
1236 		if ((nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1237 			/*
1238 			 * Since reachable time rarely changes by router
1239 			 * advertisements, we SHOULD insure that a new random
1240 			 * value gets recomputed at least once every few hours.
1241 			 * (RFC 2461, 6.3.4)
1242 			 */
1243 			nd6if->recalctm = ND6_RECALC_REACHTM_INTERVAL;
1244 			nd6if->reachable = ND_COMPUTE_RTIME(REACHABLE_TIME);
1245 		}
1246 	}
1247 	NET_UNLOCK();
1248 }
1249 
1250 int
1251 nd6_resolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
1252     struct sockaddr *dst, u_char *desten)
1253 {
1254 	struct sockaddr_dl *sdl;
1255 	struct rtentry *rt;
1256 	struct llinfo_nd6 *ln = NULL;
1257 
1258 	if (m->m_flags & M_MCAST) {
1259 		ETHER_MAP_IPV6_MULTICAST(&satosin6(dst)->sin6_addr, desten);
1260 		return (0);
1261 	}
1262 
1263 	rt = rt_getll(rt0);
1264 
1265 	if (ISSET(rt->rt_flags, RTF_REJECT) &&
1266 	    (rt->rt_expire == 0 || getuptime() < rt->rt_expire)) {
1267 		m_freem(m);
1268 		return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1269 	}
1270 
1271 	/*
1272 	 * Address resolution or Neighbor Unreachability Detection
1273 	 * for the next hop.
1274 	 * At this point, the destination of the packet must be a unicast
1275 	 * or an anycast address(i.e. not a multicast).
1276 	 */
1277 	if (!ISSET(rt->rt_flags, RTF_LLINFO)) {
1278 		char addr[INET6_ADDRSTRLEN];
1279 		log(LOG_DEBUG, "%s: %s: route contains no ND information\n",
1280 		    __func__, inet_ntop(AF_INET6,
1281 		    &satosin6(rt_key(rt))->sin6_addr, addr, sizeof(addr)));
1282 		m_freem(m);
1283 		return (EINVAL);
1284 	}
1285 
1286 	if (rt->rt_gateway->sa_family != AF_LINK) {
1287 		printf("%s: something odd happens\n", __func__);
1288 		m_freem(m);
1289 		return (EINVAL);
1290 	}
1291 
1292 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1293 	KASSERT(ln != NULL);
1294 
1295 	/*
1296 	 * Move this entry to the head of the queue so that it is less likely
1297 	 * for this entry to be a target of forced garbage collection (see
1298 	 * nd6_rtrequest()).
1299 	 */
1300 	TAILQ_REMOVE(&nd6_list, ln, ln_list);
1301 	TAILQ_INSERT_HEAD(&nd6_list, ln, ln_list);
1302 
1303 	/*
1304 	 * The first time we send a packet to a neighbor whose entry is
1305 	 * STALE, we have to change the state to DELAY and set a timer to
1306 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure we do
1307 	 * neighbor unreachability detection on expiration.
1308 	 * (RFC 2461 7.3.3)
1309 	 */
1310 	if (ln->ln_state == ND6_LLINFO_STALE) {
1311 		ln->ln_asked = 0;
1312 		ln->ln_state = ND6_LLINFO_DELAY;
1313 		nd6_llinfo_settimer(ln, nd6_delay);
1314 	}
1315 
1316 	/*
1317 	 * If the neighbor cache entry has a state other than INCOMPLETE
1318 	 * (i.e. its link-layer address is already resolved), just
1319 	 * send the packet.
1320 	 */
1321 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
1322 		sdl = satosdl(rt->rt_gateway);
1323 		if (sdl->sdl_alen != ETHER_ADDR_LEN) {
1324 			char addr[INET6_ADDRSTRLEN];
1325 			log(LOG_DEBUG, "%s: %s: incorrect nd6 information\n",
1326 			    __func__,
1327 			    inet_ntop(AF_INET6, &satosin6(dst)->sin6_addr,
1328 				addr, sizeof(addr)));
1329 			m_freem(m);
1330 			return (EINVAL);
1331 		}
1332 
1333 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1334 		return (0);
1335 	}
1336 
1337 	/*
1338 	 * There is a neighbor cache entry, but no ethernet address
1339 	 * response yet.  Replace the held mbuf (if any) with this
1340 	 * latest one.
1341 	 */
1342 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1343 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1344 	m_freem(ln->ln_hold);
1345 	ln->ln_hold = m;
1346 
1347 	/*
1348 	 * If there has been no NS for the neighbor after entering the
1349 	 * INCOMPLETE state, send the first solicitation.
1350 	 */
1351 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
1352 		ln->ln_asked++;
1353 		nd6_llinfo_settimer(ln, RETRANS_TIMER / 1000);
1354 		nd6_ns_output(ifp, NULL, &satosin6(dst)->sin6_addr, ln, 0);
1355 	}
1356 	return (EAGAIN);
1357 }
1358 
1359 int
1360 nd6_need_cache(struct ifnet *ifp)
1361 {
1362 	/*
1363 	 * RFC2893 says:
1364 	 * - unidirectional tunnels needs no ND
1365 	 */
1366 	switch (ifp->if_type) {
1367 	case IFT_ETHER:
1368 	case IFT_IEEE80211:
1369 	case IFT_CARP:
1370 		return (1);
1371 	default:
1372 		return (0);
1373 	}
1374 }
1375