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