xref: /openbsd-src/sys/netinet6/nd6.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: nd6.c,v 1.192 2016/09/15 02:00:18 dlg 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/protosw.h>
44 #include <sys/errno.h>
45 #include <sys/ioctl.h>
46 #include <sys/syslog.h>
47 #include <sys/queue.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_prune	= 1;	/* walk list every 1 seconds */
70 int	nd6_delay	= 5;	/* delay first probe time 5 second */
71 int	nd6_umaxtries	= 3;	/* maximum unicast query */
72 int	nd6_mmaxtries	= 3;	/* maximum multicast query */
73 int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
74 
75 /* preventing too many loops in ND option parsing */
76 int nd6_maxndopt = 10;	/* max # of ND options allowed */
77 
78 int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
79 
80 #ifdef ND6_DEBUG
81 int nd6_debug = 1;
82 #else
83 int nd6_debug = 0;
84 #endif
85 
86 TAILQ_HEAD(llinfo_nd6_head, llinfo_nd6) nd6_list;
87 struct	pool nd6_pool;		/* pool for llinfo_nd6 structures */
88 int	nd6_inuse, nd6_allocated;
89 
90 struct nd_drhead nd_defrouter;
91 struct nd_prhead nd_prefix = { 0 };
92 
93 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
94 
95 void nd6_slowtimo(void *);
96 void nd6_invalidate(struct rtentry *);
97 struct llinfo_nd6 *nd6_free(struct rtentry *, int);
98 void nd6_llinfo_timer(void *);
99 
100 struct timeout nd6_slowtimo_ch;
101 struct timeout nd6_timer_ch;
102 struct task nd6_timer_task;
103 void nd6_timer_work(void *);
104 
105 int fill_drlist(void *, size_t *, size_t);
106 int fill_prlist(void *, size_t *, size_t);
107 
108 void
109 nd6_init(void)
110 {
111 	static int nd6_init_done = 0;
112 
113 	if (nd6_init_done) {
114 		log(LOG_NOTICE, "%s called more than once\n", __func__);
115 		return;
116 	}
117 
118 	TAILQ_INIT(&nd6_list);
119 	pool_init(&nd6_pool, sizeof(struct llinfo_nd6), 0,
120 	    IPL_SOFTNET, 0, "nd6", NULL);
121 
122 	/* initialization of the default router list */
123 	TAILQ_INIT(&nd_defrouter);
124 
125 	task_set(&nd6_timer_task, nd6_timer_work, NULL);
126 
127 	nd6_init_done = 1;
128 
129 	/* start timer */
130 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
131 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
132 
133 	nd6_rs_init();
134 }
135 
136 struct nd_ifinfo *
137 nd6_ifattach(struct ifnet *ifp)
138 {
139 	struct nd_ifinfo *nd;
140 
141 	nd = malloc(sizeof(*nd), M_IP6NDP, M_WAITOK | M_ZERO);
142 
143 	nd->initialized = 1;
144 
145 	nd->basereachable = REACHABLE_TIME;
146 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
147 	nd->retrans = RETRANS_TIMER;
148 	/* per-interface IFXF_AUTOCONF6 needs to be set too to accept RAs */
149 	nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
150 
151 	return nd;
152 }
153 
154 void
155 nd6_ifdetach(struct nd_ifinfo *nd)
156 {
157 
158 	free(nd, M_IP6NDP, sizeof(*nd));
159 }
160 
161 void
162 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
163 {
164 	bzero(ndopts, sizeof(*ndopts));
165 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
166 	ndopts->nd_opts_last
167 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
168 
169 	if (icmp6len == 0) {
170 		ndopts->nd_opts_done = 1;
171 		ndopts->nd_opts_search = NULL;
172 	}
173 }
174 
175 /*
176  * Take one ND option.
177  */
178 struct nd_opt_hdr *
179 nd6_option(union nd_opts *ndopts)
180 {
181 	struct nd_opt_hdr *nd_opt;
182 	int olen;
183 
184 	if (!ndopts)
185 		panic("ndopts == NULL in nd6_option");
186 	if (!ndopts->nd_opts_last)
187 		panic("uninitialized ndopts in nd6_option");
188 	if (!ndopts->nd_opts_search)
189 		return NULL;
190 	if (ndopts->nd_opts_done)
191 		return NULL;
192 
193 	nd_opt = ndopts->nd_opts_search;
194 
195 	/* make sure nd_opt_len is inside the buffer */
196 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
197 		bzero(ndopts, sizeof(*ndopts));
198 		return NULL;
199 	}
200 
201 	olen = nd_opt->nd_opt_len << 3;
202 	if (olen == 0) {
203 		/*
204 		 * Message validation requires that all included
205 		 * options have a length that is greater than zero.
206 		 */
207 		bzero(ndopts, sizeof(*ndopts));
208 		return NULL;
209 	}
210 
211 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
212 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
213 		/* option overruns the end of buffer, invalid */
214 		bzero(ndopts, sizeof(*ndopts));
215 		return NULL;
216 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
217 		/* reached the end of options chain */
218 		ndopts->nd_opts_done = 1;
219 		ndopts->nd_opts_search = NULL;
220 	}
221 	return nd_opt;
222 }
223 
224 /*
225  * Parse multiple ND options.
226  * This function is much easier to use, for ND routines that do not need
227  * multiple options of the same type.
228  */
229 int
230 nd6_options(union nd_opts *ndopts)
231 {
232 	struct nd_opt_hdr *nd_opt;
233 	int i = 0;
234 
235 	if (!ndopts)
236 		panic("ndopts == NULL in nd6_options");
237 	if (!ndopts->nd_opts_last)
238 		panic("uninitialized ndopts in nd6_options");
239 	if (!ndopts->nd_opts_search)
240 		return 0;
241 
242 	while (1) {
243 		nd_opt = nd6_option(ndopts);
244 		if (!nd_opt && !ndopts->nd_opts_last) {
245 			/*
246 			 * Message validation requires that all included
247 			 * options have a length that is greater than zero.
248 			 */
249 			icmp6stat.icp6s_nd_badopt++;
250 			bzero(ndopts, sizeof(*ndopts));
251 			return -1;
252 		}
253 
254 		if (!nd_opt)
255 			goto skip1;
256 
257 		switch (nd_opt->nd_opt_type) {
258 		case ND_OPT_SOURCE_LINKADDR:
259 		case ND_OPT_TARGET_LINKADDR:
260 		case ND_OPT_MTU:
261 		case ND_OPT_REDIRECTED_HEADER:
262 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
263 				nd6log((LOG_INFO,
264 				    "duplicated ND6 option found (type=%d)\n",
265 				    nd_opt->nd_opt_type));
266 				/* XXX bark? */
267 			} else {
268 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
269 					= nd_opt;
270 			}
271 			break;
272 		case ND_OPT_PREFIX_INFORMATION:
273 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
274 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
275 					= nd_opt;
276 			}
277 			ndopts->nd_opts_pi_end =
278 				(struct nd_opt_prefix_info *)nd_opt;
279 			break;
280 		default:
281 			/*
282 			 * Unknown options must be silently ignored,
283 			 * to accommodate future extension to the protocol.
284 			 */
285 			nd6log((LOG_DEBUG,
286 			    "nd6_options: unsupported option %d - "
287 			    "option ignored\n", nd_opt->nd_opt_type));
288 		}
289 
290 skip1:
291 		i++;
292 		if (i > nd6_maxndopt) {
293 			icmp6stat.icp6s_nd_toomanyopt++;
294 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
295 			break;
296 		}
297 
298 		if (ndopts->nd_opts_done)
299 			break;
300 	}
301 
302 	return 0;
303 }
304 
305 /*
306  * ND6 timer routine to handle ND6 entries
307  */
308 void
309 nd6_llinfo_settimer(struct llinfo_nd6 *ln, int secs)
310 {
311 	int s;
312 
313 	s = splsoftnet();
314 
315 	if (secs < 0) {
316 		ln->ln_rt->rt_expire = 0;
317 		timeout_del(&ln->ln_timer_ch);
318 	} else {
319 		ln->ln_rt->rt_expire = time_uptime + secs;
320 		timeout_add_sec(&ln->ln_timer_ch, secs);
321 	}
322 
323 	splx(s);
324 }
325 
326 void
327 nd6_llinfo_timer(void *arg)
328 {
329 	int s;
330 	struct llinfo_nd6 *ln;
331 	struct rtentry *rt;
332 	struct sockaddr_in6 *dst;
333 	struct ifnet *ifp;
334 	struct nd_ifinfo *ndi = NULL;
335 
336 	s = splsoftnet();
337 
338 	ln = (struct llinfo_nd6 *)arg;
339 
340 	if ((rt = ln->ln_rt) == NULL)
341 		panic("ln->ln_rt == NULL");
342 	if ((ifp = if_get(rt->rt_ifidx)) == NULL) {
343 		splx(s);
344 		return;
345 	}
346 	ndi = ND_IFINFO(ifp);
347 	dst = satosin6(rt_key(rt));
348 
349 	/* sanity check */
350 	if (rt->rt_llinfo != NULL && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
351 		panic("rt_llinfo(%p) is not equal to ln(%p)",
352 		      rt->rt_llinfo, ln);
353 	if (!dst)
354 		panic("dst=0 in nd6_timer(ln=%p)", ln);
355 
356 	switch (ln->ln_state) {
357 	case ND6_LLINFO_INCOMPLETE:
358 		if (ln->ln_asked < nd6_mmaxtries) {
359 			ln->ln_asked++;
360 			nd6_llinfo_settimer(ln, ndi->retrans / 1000);
361 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
362 		} else {
363 			struct mbuf *m = ln->ln_hold;
364 			if (m) {
365 				ln->ln_hold = NULL;
366 				/*
367 				 * Fake rcvif to make the ICMP error
368 				 * more helpful in diagnosing for the
369 				 * receiver.
370 				 * XXX: should we consider
371 				 * older rcvif?
372 				 */
373 				m->m_pkthdr.ph_ifidx = rt->rt_ifidx;
374 
375 				icmp6_error(m, ICMP6_DST_UNREACH,
376 				    ICMP6_DST_UNREACH_ADDR, 0);
377 				if (ln->ln_hold == m) {
378 					/* m is back in ln_hold. Discard. */
379 					m_freem(ln->ln_hold);
380 					ln->ln_hold = NULL;
381 				}
382 			}
383 			(void)nd6_free(rt, 0);
384 			ln = NULL;
385 		}
386 		break;
387 	case ND6_LLINFO_REACHABLE:
388 		if (!ND6_LLINFO_PERMANENT(ln)) {
389 			ln->ln_state = ND6_LLINFO_STALE;
390 			nd6_llinfo_settimer(ln, nd6_gctimer);
391 		}
392 		break;
393 
394 	case ND6_LLINFO_STALE:
395 	case ND6_LLINFO_PURGE:
396 		/* Garbage Collection(RFC 2461 5.3) */
397 		if (!ND6_LLINFO_PERMANENT(ln)) {
398 			(void)nd6_free(rt, 1);
399 			ln = NULL;
400 		}
401 		break;
402 
403 	case ND6_LLINFO_DELAY:
404 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
405 			/* We need NUD */
406 			ln->ln_asked = 1;
407 			ln->ln_state = ND6_LLINFO_PROBE;
408 			nd6_llinfo_settimer(ln, ndi->retrans / 1000);
409 			nd6_ns_output(ifp, &dst->sin6_addr,
410 			    &dst->sin6_addr, ln, 0);
411 		} else {
412 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
413 			nd6_llinfo_settimer(ln, nd6_gctimer);
414 		}
415 		break;
416 	case ND6_LLINFO_PROBE:
417 		if (ln->ln_asked < nd6_umaxtries) {
418 			ln->ln_asked++;
419 			nd6_llinfo_settimer(ln, ndi->retrans / 1000);
420 			nd6_ns_output(ifp, &dst->sin6_addr,
421 			    &dst->sin6_addr, ln, 0);
422 		} else {
423 			(void)nd6_free(rt, 0);
424 			ln = NULL;
425 		}
426 		break;
427 	}
428 
429 	if_put(ifp);
430 	splx(s);
431 }
432 
433 /*
434  * ND6 timer routine to expire default route list and prefix list
435  */
436 void
437 nd6_timer_work(void *null)
438 {
439 	int s;
440 	struct nd_defrouter *dr, *ndr;
441 	struct nd_prefix *pr, *npr;
442 	struct in6_ifaddr *ia6, *nia6;
443 
444 	s = splsoftnet();
445 	timeout_set(&nd6_timer_ch, nd6_timer, NULL);
446 	timeout_add_sec(&nd6_timer_ch, nd6_prune);
447 
448 	/* expire default router list */
449 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr)
450 		if (dr->expire && dr->expire < time_second)
451 			defrtrlist_del(dr);
452 
453 	/*
454 	 * expire interface addresses.
455 	 * in the past the loop was inside prefix expiry processing.
456 	 * However, from a stricter spec-conformance standpoint, we should
457 	 * rather separate address lifetimes and prefix lifetimes.
458 	 */
459 	TAILQ_FOREACH_SAFE(ia6, &in6_ifaddr, ia_list, nia6) {
460 		/* check address lifetime */
461 		if (IFA6_IS_INVALID(ia6)) {
462 			in6_purgeaddr(&ia6->ia_ifa);
463 		} else if (IFA6_IS_DEPRECATED(ia6)) {
464 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
465 		} else {
466 			/*
467 			 * A new RA might have made a deprecated address
468 			 * preferred.
469 			 */
470 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
471 		}
472 	}
473 
474 	/* expire prefix list */
475 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
476 		/*
477 		 * check prefix lifetime.
478 		 * since pltime is just for autoconf, pltime processing for
479 		 * prefix is not necessary.
480 		 */
481 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
482 		    time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
483 			/*
484 			 * address expiration and prefix expiration are
485 			 * separate.  NEVER perform in6_purgeaddr here.
486 			 */
487 
488 			prelist_remove(pr);
489 		}
490 	}
491 	splx(s);
492 }
493 
494 void
495 nd6_timer(void *ignored_arg)
496 {
497 	task_add(systq, &nd6_timer_task);
498 }
499 
500 /*
501  * Nuke neighbor cache/prefix/default router management table, right before
502  * ifp goes away.
503  */
504 void
505 nd6_purge(struct ifnet *ifp)
506 {
507 	struct llinfo_nd6 *ln, *nln;
508 	struct nd_defrouter *dr, *ndr;
509 	struct nd_prefix *pr, *npr;
510 
511 	/*
512 	 * Nuke default router list entries toward ifp.
513 	 * We defer removal of default router list entries that is installed
514 	 * in the routing table, in order to keep additional side effects as
515 	 * small as possible.
516 	 */
517 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
518 		if (dr->installed)
519 			continue;
520 
521 		if (dr->ifp == ifp)
522 			defrtrlist_del(dr);
523 	}
524 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
525 		if (!dr->installed)
526 			continue;
527 
528 		if (dr->ifp == ifp)
529 			defrtrlist_del(dr);
530 	}
531 
532 	/* Nuke prefix list entries toward ifp */
533 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
534 		if (pr->ndpr_ifp == ifp)
535 			prelist_remove(pr);
536 	}
537 
538 	if (ifp->if_xflags & IFXF_AUTOCONF6) {
539 		/* refresh default router list */
540 		defrouter_select();
541 	}
542 
543 	/*
544 	 * Nuke neighbor cache entries for the ifp.
545 	 */
546 	TAILQ_FOREACH_SAFE(ln, &nd6_list, ln_list, nln) {
547 		struct rtentry *rt;
548 		struct sockaddr_dl *sdl;
549 
550 		rt = ln->ln_rt;
551 		if (rt != NULL && rt->rt_gateway != NULL &&
552 		    rt->rt_gateway->sa_family == AF_LINK) {
553 			sdl = satosdl(rt->rt_gateway);
554 			if (sdl->sdl_index == ifp->if_index)
555 				nln = nd6_free(rt, 0);
556 		}
557 	}
558 }
559 
560 struct rtentry *
561 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp,
562     u_int rtableid)
563 {
564 	struct rtentry *rt;
565 	struct sockaddr_in6 sin6;
566 	int flags;
567 
568 	bzero(&sin6, sizeof(sin6));
569 	sin6.sin6_len = sizeof(struct sockaddr_in6);
570 	sin6.sin6_family = AF_INET6;
571 	sin6.sin6_addr = *addr6;
572 	flags = (create) ? RT_RESOLVE : 0;
573 
574 	rt = rtalloc(sin6tosa(&sin6), flags, rtableid);
575 	if (rt != NULL && (rt->rt_flags & RTF_LLINFO) == 0) {
576 		/*
577 		 * This is the case for the default route.
578 		 * If we want to create a neighbor cache for the address, we
579 		 * should free the route for the destination and allocate an
580 		 * interface route.
581 		 */
582 		if (create) {
583 			rtfree(rt);
584 			rt = NULL;
585 		}
586 	}
587 	if (rt == NULL) {
588 		if (create && ifp) {
589 			struct rt_addrinfo info;
590 			int error;
591 
592 			/*
593 			 * If no route is available and create is set,
594 			 * we allocate a host route for the destination
595 			 * and treat it like an interface route.
596 			 * This hack is necessary for a neighbor which can't
597 			 * be covered by our own prefix.
598 			 */
599 			struct ifaddr *ifa =
600 			    ifaof_ifpforaddr(sin6tosa(&sin6), ifp);
601 			if (ifa == NULL)
602 				return (NULL);
603 
604 			/*
605 			 * Create a new route.  RTF_LLINFO is necessary
606 			 * to create a Neighbor Cache entry for the
607 			 * destination in nd6_rtrequest which will be
608 			 * called in rtrequest.
609 			 */
610 			bzero(&info, sizeof(info));
611 			info.rti_flags = RTF_HOST | RTF_LLINFO;
612 			info.rti_info[RTAX_DST] = sin6tosa(&sin6);
613 			info.rti_info[RTAX_GATEWAY] = sdltosa(ifp->if_sadl);
614 			error = rtrequest(RTM_ADD, &info, RTP_CONNECTED, &rt,
615 			    rtableid);
616 			if (error)
617 				return (NULL);
618 			if (rt->rt_llinfo != NULL) {
619 				struct llinfo_nd6 *ln =
620 				    (struct llinfo_nd6 *)rt->rt_llinfo;
621 				ln->ln_state = ND6_LLINFO_NOSTATE;
622 			}
623 		} else
624 			return (NULL);
625 	}
626 	/*
627 	 * Validation for the entry.
628 	 * Note that the check for rt_llinfo is necessary because a cloned
629 	 * route from a parent route that has the L flag (e.g. the default
630 	 * route to a p2p interface) may have the flag, too, while the
631 	 * destination is not actually a neighbor.
632 	 */
633 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
634 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
635 	    (ifp != NULL && rt->rt_ifidx != ifp->if_index)) {
636 		if (create) {
637 			char addr[INET6_ADDRSTRLEN];
638 			nd6log((LOG_DEBUG, "%s: failed to lookup %s (if=%s)\n",
639 			    __func__,
640 			    inet_ntop(AF_INET6, addr6, addr, sizeof(addr)),
641 			    ifp ? ifp->if_xname : "unspec"));
642 		}
643 		rtfree(rt);
644 		return (NULL);
645 	}
646 	return (rt);
647 }
648 
649 /*
650  * Detect if a given IPv6 address identifies a neighbor on a given link.
651  * XXX: should take care of the destination of a p2p link?
652  */
653 int
654 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
655 {
656 	struct nd_prefix *pr;
657 	struct in6_ifaddr *ia6;
658 	struct ifaddr *ifa;
659 	struct rtentry *rt;
660 
661 	/*
662 	 * A link-local address is always a neighbor.
663 	 * XXX: we should use the sin6_scope_id field rather than the embedded
664 	 * interface index.
665 	 * XXX: a link does not necessarily specify a single interface.
666 	 */
667 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
668 	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
669 		return (1);
670 
671 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
672 		if (ifa->ifa_addr->sa_family != AF_INET6)
673 			continue;
674 
675 		ia6 = ifatoia6(ifa);
676 
677 		/* Prefix check down below. */
678 		if (ia6->ia6_flags & IN6_IFF_AUTOCONF)
679 			continue;
680 
681 		if (IN6_ARE_MASKED_ADDR_EQUAL(&addr->sin6_addr,
682 		    &ia6->ia_addr.sin6_addr,
683 		    &ia6->ia_prefixmask.sin6_addr))
684 			return (1);
685 	}
686 
687 	/*
688 	 * If the address matches one of our on-link prefixes, it should be a
689 	 * neighbor.
690 	 */
691 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
692 		if (pr->ndpr_ifp != ifp)
693 			continue;
694 
695 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
696 			continue;
697 
698 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
699 		    &addr->sin6_addr, &pr->ndpr_mask))
700 			return (1);
701 	}
702 
703 	/*
704 	 * Even if the address matches none of our addresses, it might be
705 	 * in the neighbor cache.
706 	 */
707 	rt = nd6_lookup(&addr->sin6_addr, 0, ifp, ifp->if_rdomain);
708 	if (rt != NULL) {
709 		rtfree(rt);
710 		return (1);
711 	}
712 
713 	return (0);
714 }
715 
716 void
717 nd6_invalidate(struct rtentry *rt)
718 {
719 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
720 
721 	m_freem(ln->ln_hold);
722 	ln->ln_hold = NULL;
723 	ln->ln_state = ND6_LLINFO_INCOMPLETE;
724 	ln->ln_asked = 0;
725 }
726 
727 /*
728  * Free an nd6 llinfo entry.
729  * Since the function would cause significant changes in the kernel, DO NOT
730  * make it global, unless you have a strong reason for the change, and are sure
731  * that the change is safe.
732  */
733 struct llinfo_nd6 *
734 nd6_free(struct rtentry *rt, int gc)
735 {
736 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
737 	struct in6_addr in6 = satosin6(rt_key(rt))->sin6_addr;
738 	struct nd_defrouter *dr;
739 	struct ifnet *ifp;
740 	int s;
741 
742 	/*
743 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
744 	 * even though it is not harmful, it was not really necessary.
745 	 */
746 	ifp = if_get(rt->rt_ifidx);
747 
748 	s = splsoftnet();
749 	if (!ip6_forwarding) {
750 		dr = defrouter_lookup(&satosin6(rt_key(rt))->sin6_addr,
751 		    rt->rt_ifidx);
752 
753 		if (dr != NULL && dr->expire &&
754 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
755 			/*
756 			 * If the reason for the deletion is just garbage
757 			 * collection, and the neighbor is an active default
758 			 * router, do not delete it.  Instead, reset the GC
759 			 * timer using the router's lifetime.
760 			 * Simply deleting the entry would affect default
761 			 * router selection, which is not necessarily a good
762 			 * thing, especially when we're using router preference
763 			 * values.
764 			 * XXX: the check for ln_state would be redundant,
765 			 *      but we intentionally keep it just in case.
766 			 */
767 			if (dr->expire > time_second) {
768 				nd6_llinfo_settimer(ln,
769 				    dr->expire - time_second);
770 			} else
771 				nd6_llinfo_settimer(ln, nd6_gctimer);
772 			splx(s);
773 			if_put(ifp);
774 			return (TAILQ_NEXT(ln, ln_list));
775 		}
776 
777 		if (ln->ln_router || dr) {
778 			/*
779 			 * rt6_flush must be called whether or not the neighbor
780 			 * is in the Default Router List.
781 			 * See a corresponding comment in nd6_na_input().
782 			 */
783 			rt6_flush(&in6, ifp);
784 		}
785 
786 		if (dr) {
787 			/*
788 			 * Unreachability of a router might affect the default
789 			 * router selection and on-link detection of advertised
790 			 * prefixes.
791 			 */
792 
793 			/*
794 			 * Temporarily fake the state to choose a new default
795 			 * router and to perform on-link determination of
796 			 * prefixes correctly.
797 			 * Below the state will be set correctly,
798 			 * or the entry itself will be deleted.
799 			 */
800 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
801 
802 			/*
803 			 * Since defrouter_select() does not affect the
804 			 * on-link determination and MIP6 needs the check
805 			 * before the default router selection, we perform
806 			 * the check now.
807 			 */
808 			pfxlist_onlink_check();
809 
810 			/*
811 			 * refresh default router list
812 			 */
813 			defrouter_select();
814 		}
815 	}
816 
817 	/*
818 	 * Before deleting the entry, remember the next entry as the
819 	 * return value.  We need this because pfxlist_onlink_check() above
820 	 * might have freed other entries (particularly the old next entry) as
821 	 * a side effect (XXX).
822 	 */
823 	next = TAILQ_NEXT(ln, ln_list);
824 
825 	/*
826 	 * Detach the route from the routing tree and the list of neighbor
827 	 * caches, and disable the route entry not to be used in already
828 	 * cached routes.
829 	 */
830 	if (!ISSET(rt->rt_flags, RTF_STATIC|RTF_CACHED))
831 		rtdeletemsg(rt, ifp, ifp->if_rdomain);
832 	splx(s);
833 
834 	if_put(ifp);
835 
836 	return (next);
837 }
838 
839 /*
840  * Upper-layer reachability hint for Neighbor Unreachability Detection.
841  *
842  * XXX cost-effective methods?
843  */
844 void
845 nd6_nud_hint(struct rtentry *rt)
846 {
847 	struct llinfo_nd6 *ln;
848 	struct ifnet *ifp;
849 
850 	ifp = if_get(rt->rt_ifidx);
851 	if (ifp == NULL)
852 		return;
853 
854 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
855 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
856 	    rt->rt_llinfo == NULL || rt->rt_gateway == NULL ||
857 	    rt->rt_gateway->sa_family != AF_LINK) {
858 		/* This is not a host route. */
859 		goto out;
860 	}
861 
862 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
863 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
864 		goto out;
865 
866 	/*
867 	 * if we get upper-layer reachability confirmation many times,
868 	 * it is possible we have false information.
869 	 */
870 	ln->ln_byhint++;
871 	if (ln->ln_byhint > nd6_maxnudhint)
872 		goto out;
873 
874 	ln->ln_state = ND6_LLINFO_REACHABLE;
875 	if (!ND6_LLINFO_PERMANENT(ln))
876 		nd6_llinfo_settimer(ln, ND_IFINFO(ifp)->reachable);
877 out:
878 	if_put(ifp);
879 }
880 
881 void
882 nd6_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt)
883 {
884 	struct sockaddr *gate = rt->rt_gateway;
885 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
886 	struct ifaddr *ifa;
887 	struct nd_defrouter *dr;
888 
889 	if (req == RTM_DELETE && (rt->rt_flags & RTF_GATEWAY) &&
890 	    (IN6_ARE_ADDR_EQUAL(&(satosin6(rt_key(rt)))->sin6_addr,
891 	    &in6addr_any) && rt_plen(rt) == 0)) {
892 		dr = defrouter_lookup(&satosin6(gate)->sin6_addr,
893 		    ifp->if_index);
894 		if (dr)
895 			dr->installed = 0;
896 	}
897 
898 	if (ISSET(rt->rt_flags, RTF_GATEWAY|RTF_MULTICAST))
899 		return;
900 
901 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
902 		/*
903 		 * This is probably an interface direct route for a link
904 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
905 		 * We do not need special treatment below for such a route.
906 		 * Moreover, the RTF_LLINFO flag which would be set below
907 		 * would annoy the ndp(8) command.
908 		 */
909 		return;
910 	}
911 
912 	if (req == RTM_RESOLVE && nd6_need_cache(ifp) == 0) {
913 		/*
914 		 * For routing daemons like ospf6d we allow neighbor discovery
915 		 * based on the cloning route only.  This allows us to sent
916 		 * packets directly into a network without having an address
917 		 * with matching prefix on the interface.  If the cloning
918 		 * route is used for an stf interface, we would mistakenly
919 		 * make a neighbor cache for the host route, and would see
920 		 * strange neighbor solicitation for the corresponding
921 		 * destination.  In order to avoid confusion, we check if the
922 		 * interface is suitable for neighbor discovery, and stop the
923 		 * process if not.  Additionally, we remove the LLINFO flag
924 		 * so that ndp(8) will not try to get the neighbor information
925 		 * of the destination.
926 		 */
927 		rt->rt_flags &= ~RTF_LLINFO;
928 		return;
929 	}
930 
931 	switch (req) {
932 	case RTM_ADD:
933 		if ((rt->rt_flags & RTF_CLONING) ||
934 		    ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && ln == NULL)) {
935 			if (ln != NULL)
936 				nd6_llinfo_settimer(ln, 0);
937 			if ((rt->rt_flags & RTF_CLONING) != 0)
938 				break;
939 		}
940 		/*
941 		 * In IPv4 code, we try to announce new RTF_ANNOUNCE entry here.
942 		 * We don't do that here since llinfo is not ready yet.
943 		 *
944 		 * There are also couple of other things to be discussed:
945 		 * - unsolicited NA code needs improvement beforehand
946 		 * - RFC2461 says we MAY send multicast unsolicited NA
947 		 *   (7.2.6 paragraph 4), however, it also says that we
948 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
949 		 *   we don't have anything like it right now.
950 		 *   note that the mechanism needs a mutual agreement
951 		 *   between proxies, which means that we need to implement
952 		 *   a new protocol, or a new kludge.
953 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
954 		 *   we need to check ip6forwarding before sending it.
955 		 *   (or should we allow proxy ND configuration only for
956 		 *   routers?  there's no mention about proxy ND from hosts)
957 		 */
958 #if 0
959 		/* XXX it does not work */
960 		if (rt->rt_flags & RTF_ANNOUNCE)
961 			nd6_na_output(ifp,
962 			      &satosin6(rt_key(rt))->sin6_addr,
963 			      &satosin6(rt_key(rt))->sin6_addr,
964 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
965 			      1, NULL);
966 #endif
967 		/* FALLTHROUGH */
968 	case RTM_RESOLVE:
969 		if (gate->sa_family != AF_LINK ||
970 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
971 			log(LOG_DEBUG, "%s: bad gateway value: %s\n",
972 			    __func__, ifp->if_xname);
973 			break;
974 		}
975 		satosdl(gate)->sdl_type = ifp->if_type;
976 		satosdl(gate)->sdl_index = ifp->if_index;
977 		if (ln != NULL)
978 			break;	/* This happens on a route change */
979 		/*
980 		 * Case 2: This route may come from cloning, or a manual route
981 		 * add with a LL address.
982 		 */
983 		ln = pool_get(&nd6_pool, PR_NOWAIT | PR_ZERO);
984 		rt->rt_llinfo = (caddr_t)ln;
985 		if (ln == NULL) {
986 			log(LOG_DEBUG, "%s: pool get failed\n", __func__);
987 			break;
988 		}
989 		nd6_inuse++;
990 		nd6_allocated++;
991 		ln->ln_rt = rt;
992 		timeout_set(&ln->ln_timer_ch, nd6_llinfo_timer, ln);
993 		/* this is required for "ndp" command. - shin */
994 		if (req == RTM_ADD) {
995 		        /*
996 			 * gate should have some valid AF_LINK entry,
997 			 * and ln expire should have some lifetime
998 			 * which is specified by ndp command.
999 			 */
1000 			ln->ln_state = ND6_LLINFO_REACHABLE;
1001 			ln->ln_byhint = 0;
1002 		} else {
1003 		        /*
1004 			 * When req == RTM_RESOLVE, rt is created and
1005 			 * initialized in rtrequest(), so rt_expire is 0.
1006 			 */
1007 			ln->ln_state = ND6_LLINFO_NOSTATE;
1008 			nd6_llinfo_settimer(ln, 0);
1009 		}
1010 		rt->rt_flags |= RTF_LLINFO;
1011 		TAILQ_INSERT_HEAD(&nd6_list, ln, ln_list);
1012 
1013 		/*
1014 		 * If we have too many cache entries, initiate immediate
1015 		 * purging for some "less recently used" entries.  Note that
1016 		 * we cannot directly call nd6_free() here because it would
1017 		 * cause re-entering rtable related routines triggering an LOR
1018 		 * problem for FreeBSD.
1019 		 */
1020 		if (ip6_neighborgcthresh >= 0 &&
1021 		    nd6_inuse >= ip6_neighborgcthresh) {
1022 			int i;
1023 
1024 			for (i = 0; i < 10; i++) {
1025 				struct llinfo_nd6 *ln_end;
1026 
1027 				ln_end = TAILQ_LAST(&nd6_list, llinfo_nd6_head);
1028 				if (ln_end == ln)
1029 					break;
1030 
1031 				/* Move this entry to the head */
1032 				TAILQ_REMOVE(&nd6_list, ln_end, ln_list);
1033 				TAILQ_INSERT_HEAD(&nd6_list, ln_end, ln_list);
1034 
1035 				if (ND6_LLINFO_PERMANENT(ln_end))
1036 					continue;
1037 
1038 				if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
1039 					ln_end->ln_state = ND6_LLINFO_STALE;
1040 				else
1041 					ln_end->ln_state = ND6_LLINFO_PURGE;
1042 				nd6_llinfo_settimer(ln_end, 0);
1043 			}
1044 		}
1045 
1046 		/*
1047 		 * check if rt_key(rt) is one of my address assigned
1048 		 * to the interface.
1049 		 */
1050 		ifa = &in6ifa_ifpwithaddr(ifp,
1051 		    &satosin6(rt_key(rt))->sin6_addr)->ia_ifa;
1052 		if (ifa) {
1053 			nd6_llinfo_settimer(ln, -1);
1054 			ln->ln_state = ND6_LLINFO_REACHABLE;
1055 			ln->ln_byhint = 0;
1056 			KASSERT(ifa == rt->rt_ifa);
1057 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
1058 			nd6_llinfo_settimer(ln, -1);
1059 			ln->ln_state = ND6_LLINFO_REACHABLE;
1060 			ln->ln_byhint = 0;
1061 
1062 			/* join solicited node multicast for proxy ND */
1063 			if (ifp->if_flags & IFF_MULTICAST) {
1064 				struct in6_addr llsol;
1065 				int error;
1066 
1067 				llsol = satosin6(rt_key(rt))->sin6_addr;
1068 				llsol.s6_addr16[0] = htons(0xff02);
1069 				llsol.s6_addr16[1] = htons(ifp->if_index);
1070 				llsol.s6_addr32[1] = 0;
1071 				llsol.s6_addr32[2] = htonl(1);
1072 				llsol.s6_addr8[12] = 0xff;
1073 
1074 				if (in6_addmulti(&llsol, ifp, &error)) {
1075 					char addr[INET6_ADDRSTRLEN];
1076 					nd6log((LOG_ERR, "%s: failed to join "
1077 					    "%s (errno=%d)\n", ifp->if_xname,
1078 					    inet_ntop(AF_INET6, &llsol,
1079 						addr, sizeof(addr)),
1080 					    error));
1081 				}
1082 			}
1083 		}
1084 		break;
1085 
1086 	case RTM_DELETE:
1087 		if (ln == NULL)
1088 			break;
1089 		/* leave from solicited node multicast for proxy ND */
1090 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1091 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1092 			struct in6_addr llsol;
1093 			struct in6_multi *in6m;
1094 
1095 			llsol = satosin6(rt_key(rt))->sin6_addr;
1096 			llsol.s6_addr16[0] = htons(0xff02);
1097 			llsol.s6_addr16[1] = htons(ifp->if_index);
1098 			llsol.s6_addr32[1] = 0;
1099 			llsol.s6_addr32[2] = htonl(1);
1100 			llsol.s6_addr8[12] = 0xff;
1101 
1102 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1103 			if (in6m)
1104 				in6_delmulti(in6m);
1105 		}
1106 		nd6_inuse--;
1107 		TAILQ_REMOVE(&nd6_list, ln, ln_list);
1108 		nd6_llinfo_settimer(ln, -1);
1109 		rt->rt_llinfo = NULL;
1110 		rt->rt_flags &= ~RTF_LLINFO;
1111 		m_freem(ln->ln_hold);
1112 		pool_put(&nd6_pool, ln);
1113 		break;
1114 
1115 	case RTM_INVALIDATE:
1116 		nd6_invalidate(rt);
1117 		break;
1118 	}
1119 }
1120 
1121 int
1122 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1123 {
1124 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1125 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1126 	struct rtentry *rt;
1127 	int error = 0;
1128 	int s;
1129 
1130 	switch (cmd) {
1131 	case SIOCGIFINFO_IN6:
1132 		ndi->ndi = *ND_IFINFO(ifp);
1133 		memset(&ndi->ndi.randomseed0, 0, sizeof ndi->ndi.randomseed0);
1134 		memset(&ndi->ndi.randomseed1, 0, sizeof ndi->ndi.randomseed1);
1135 		memset(&ndi->ndi.randomid, 0, sizeof ndi->ndi.randomid);
1136 		break;
1137 	case SIOCSIFINFO_FLAGS:
1138 		ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1139 		break;
1140 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1141 		/* sync kernel routing table with the default router list */
1142 		defrouter_reset();
1143 		defrouter_select();
1144 		break;
1145 	case SIOCSPFXFLUSH_IN6:
1146 	{
1147 		/* flush all the prefix advertised by routers */
1148 		struct nd_prefix *pr, *npr;
1149 
1150 		s = splsoftnet();
1151 		/* First purge the addresses referenced by a prefix. */
1152 		LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
1153 			struct in6_ifaddr *ia6, *ia6_next;
1154 
1155 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1156 				continue; /* XXX */
1157 
1158 			/* do we really have to remove addresses as well? */
1159 			TAILQ_FOREACH_SAFE(ia6, &in6_ifaddr, ia_list, ia6_next) {
1160 				if ((ia6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1161 					continue;
1162 
1163 				if (ia6->ia6_ndpr == pr)
1164 					in6_purgeaddr(&ia6->ia_ifa);
1165 			}
1166 		}
1167 		/*
1168 		 * Purging the addresses might remove the prefix as well.
1169 		 * So run the loop again to access only prefixes that have
1170 		 * not been freed already.
1171 		 */
1172 		LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
1173 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1174 				continue; /* XXX */
1175 
1176 			prelist_remove(pr);
1177 		}
1178 		splx(s);
1179 		break;
1180 	}
1181 	case SIOCSRTRFLUSH_IN6:
1182 	{
1183 		/* flush all the default routers */
1184 		struct nd_defrouter *dr, *ndr;
1185 
1186 		s = splsoftnet();
1187 		defrouter_reset();
1188 		TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr)
1189 			defrtrlist_del(dr);
1190 		defrouter_select();
1191 		splx(s);
1192 		break;
1193 	}
1194 	case SIOCGNBRINFO_IN6:
1195 	{
1196 		struct llinfo_nd6 *ln;
1197 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1198 		time_t expire;
1199 
1200 		/*
1201 		 * XXX: KAME specific hack for scoped addresses
1202 		 *      XXXX: for other scopes than link-local?
1203 		 */
1204 		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1205 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1206 			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1207 
1208 			if (*idp == 0)
1209 				*idp = htons(ifp->if_index);
1210 		}
1211 
1212 		s = splsoftnet();
1213 		rt = nd6_lookup(&nb_addr, 0, ifp, ifp->if_rdomain);
1214 		if (rt == NULL ||
1215 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1216 			error = EINVAL;
1217 			rtfree(rt);
1218 			splx(s);
1219 			break;
1220 		}
1221 		expire = ln->ln_rt->rt_expire;
1222 		if (expire != 0) {
1223 			expire -= time_uptime;
1224 			expire += time_second;
1225 		}
1226 
1227 		nbi->state = ln->ln_state;
1228 		nbi->asked = ln->ln_asked;
1229 		nbi->isrouter = ln->ln_router;
1230 		nbi->expire = expire;
1231 		rtfree(rt);
1232 		splx(s);
1233 
1234 		break;
1235 	}
1236 	}
1237 	return (error);
1238 }
1239 
1240 /*
1241  * Create neighbor cache entry and cache link-layer address,
1242  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1243  *
1244  * type - ICMP6 type
1245  * code - type dependent information
1246  */
1247 void
1248 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1249     int lladdrlen, int type, int code)
1250 {
1251 	struct rtentry *rt = NULL;
1252 	struct llinfo_nd6 *ln = NULL;
1253 	int is_newentry;
1254 	struct sockaddr_dl *sdl = NULL;
1255 	int do_update;
1256 	int olladdr;
1257 	int llchange;
1258 	int newstate = 0;
1259 
1260 	if (!ifp)
1261 		panic("ifp == NULL in nd6_cache_lladdr");
1262 	if (!from)
1263 		panic("from == NULL in nd6_cache_lladdr");
1264 
1265 	/* nothing must be updated for unspecified address */
1266 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1267 		return;
1268 
1269 	/*
1270 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1271 	 * the caller.
1272 	 *
1273 	 * XXX If the link does not have link-layer address, what should
1274 	 * we do? (ifp->if_addrlen == 0)
1275 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1276 	 * description on it in NS section (RFC 2461 7.2.3).
1277 	 */
1278 
1279 	rt = nd6_lookup(from, 0, ifp, ifp->if_rdomain);
1280 	if (rt == NULL) {
1281 #if 0
1282 		/* nothing must be done if there's no lladdr */
1283 		if (!lladdr || !lladdrlen)
1284 			return NULL;
1285 #endif
1286 
1287 		rt = nd6_lookup(from, 1, ifp, ifp->if_rdomain);
1288 		is_newentry = 1;
1289 	} else {
1290 		/* do nothing if static ndp is set */
1291 		if (rt->rt_flags & RTF_STATIC) {
1292 			rtfree(rt);
1293 			return;
1294 		}
1295 		is_newentry = 0;
1296 	}
1297 
1298 	if (!rt)
1299 		return;
1300 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1301 fail:
1302 		(void)nd6_free(rt, 0);
1303 		rtfree(rt);
1304 		return;
1305 	}
1306 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1307 	if (ln == NULL)
1308 		goto fail;
1309 	if (rt->rt_gateway == NULL)
1310 		goto fail;
1311 	if (rt->rt_gateway->sa_family != AF_LINK)
1312 		goto fail;
1313 	sdl = satosdl(rt->rt_gateway);
1314 
1315 	olladdr = (sdl->sdl_alen) ? 1 : 0;
1316 	if (olladdr && lladdr) {
1317 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1318 			llchange = 1;
1319 		else
1320 			llchange = 0;
1321 	} else
1322 		llchange = 0;
1323 
1324 	/*
1325 	 * newentry olladdr  lladdr  llchange	(*=record)
1326 	 *	0	n	n	--	(1)
1327 	 *	0	y	n	--	(2)
1328 	 *	0	n	y	--	(3) * STALE
1329 	 *	0	y	y	n	(4) *
1330 	 *	0	y	y	y	(5) * STALE
1331 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1332 	 *	1	--	y	--	(7) * STALE
1333 	 */
1334 
1335 	if (llchange) {
1336 		char addr[INET6_ADDRSTRLEN];
1337 		log(LOG_INFO, "ndp info overwritten for %s by %s on %s\n",
1338 		    inet_ntop(AF_INET6, from, addr, sizeof(addr)),
1339 		    ether_sprintf(lladdr), ifp->if_xname);
1340 	}
1341 	if (lladdr) {		/* (3-5) and (7) */
1342 		/*
1343 		 * Record source link-layer address
1344 		 * XXX is it dependent to ifp->if_type?
1345 		 */
1346 		sdl->sdl_alen = ifp->if_addrlen;
1347 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1348 	}
1349 
1350 	if (!is_newentry) {
1351 		if ((!olladdr && lladdr) ||		/* (3) */
1352 		    (olladdr && lladdr && llchange)) {	/* (5) */
1353 			do_update = 1;
1354 			newstate = ND6_LLINFO_STALE;
1355 		} else					/* (1-2,4) */
1356 			do_update = 0;
1357 	} else {
1358 		do_update = 1;
1359 		if (!lladdr)				/* (6) */
1360 			newstate = ND6_LLINFO_NOSTATE;
1361 		else					/* (7) */
1362 			newstate = ND6_LLINFO_STALE;
1363 	}
1364 
1365 	if (do_update) {
1366 		/*
1367 		 * Update the state of the neighbor cache.
1368 		 */
1369 		ln->ln_state = newstate;
1370 
1371 		if (ln->ln_state == ND6_LLINFO_STALE) {
1372 			/*
1373 			 * Since nd6_resolve() in ifp->if_output() will cause
1374 			 * state transition to DELAY and reset the timer,
1375 			 * we must set the timer now, although it is actually
1376 			 * meaningless.
1377 			 */
1378 			nd6_llinfo_settimer(ln, nd6_gctimer);
1379 
1380 			if (ln->ln_hold) {
1381 				struct mbuf *n = ln->ln_hold;
1382 				ln->ln_hold = NULL;
1383 				/*
1384 				 * we assume ifp is not a p2p here, so just
1385 				 * set the 2nd argument as the 1st one.
1386 				 */
1387 				ifp->if_output(ifp, n, rt_key(rt), rt);
1388 				if (ln->ln_hold == n) {
1389 					/* n is back in ln_hold. Discard. */
1390 					m_freem(ln->ln_hold);
1391 					ln->ln_hold = NULL;
1392 				}
1393 			}
1394 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1395 			/* probe right away */
1396 			nd6_llinfo_settimer((void *)ln, 0);
1397 		}
1398 	}
1399 
1400 	/*
1401 	 * ICMP6 type dependent behavior.
1402 	 *
1403 	 * NS: clear IsRouter if new entry
1404 	 * RS: clear IsRouter
1405 	 * RA: set IsRouter if there's lladdr
1406 	 * redir: clear IsRouter if new entry
1407 	 *
1408 	 * RA case, (1):
1409 	 * The spec says that we must set IsRouter in the following cases:
1410 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1411 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1412 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1413 	 * A question arises for (1) case.  (1) case has no lladdr in the
1414 	 * neighbor cache, this is similar to (6).
1415 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1416 	 *
1417 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1418 	 *							D R
1419 	 *	0	n	n	--	(1)	c   ?     s
1420 	 *	0	y	n	--	(2)	c   s     s
1421 	 *	0	n	y	--	(3)	c   s     s
1422 	 *	0	y	y	n	(4)	c   s     s
1423 	 *	0	y	y	y	(5)	c   s     s
1424 	 *	1	--	n	--	(6) c	c	c s
1425 	 *	1	--	y	--	(7) c	c   s	c s
1426 	 *
1427 	 *					(c=clear s=set)
1428 	 */
1429 	switch (type & 0xff) {
1430 	case ND_NEIGHBOR_SOLICIT:
1431 		/*
1432 		 * New entry must have is_router flag cleared.
1433 		 */
1434 		if (is_newentry)	/* (6-7) */
1435 			ln->ln_router = 0;
1436 		break;
1437 	case ND_REDIRECT:
1438 		/*
1439 		 * If the icmp is a redirect to a better router, always set the
1440 		 * is_router flag.  Otherwise, if the entry is newly created,
1441 		 * clear the flag.  [RFC 2461, sec 8.3]
1442 		 */
1443 		if (code == ND_REDIRECT_ROUTER)
1444 			ln->ln_router = 1;
1445 		else if (is_newentry) /* (6-7) */
1446 			ln->ln_router = 0;
1447 		break;
1448 	case ND_ROUTER_SOLICIT:
1449 		/*
1450 		 * is_router flag must always be cleared.
1451 		 */
1452 		ln->ln_router = 0;
1453 		break;
1454 	case ND_ROUTER_ADVERT:
1455 		/*
1456 		 * Mark an entry with lladdr as a router.
1457 		 */
1458 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1459 		    (is_newentry && lladdr)) {			/* (7) */
1460 			ln->ln_router = 1;
1461 		}
1462 		break;
1463 	}
1464 
1465 	/*
1466 	 * When the link-layer address of a router changes, select the
1467 	 * best router again.  In particular, when the neighbor entry is newly
1468 	 * created, it might affect the selection policy.
1469 	 * Question: can we restrict the first condition to the "is_newentry"
1470 	 * case?
1471 	 * XXX: when we hear an RA from a new router with the link-layer
1472 	 * address option, defrouter_select() is called twice, since
1473 	 * defrtrlist_update called the function as well.  However, I believe
1474 	 * we can compromise the overhead, since it only happens the first
1475 	 * time.
1476 	 */
1477 	if (do_update && ln->ln_router && (ifp->if_xflags & IFXF_AUTOCONF6))
1478 		defrouter_select();
1479 
1480 	rtfree(rt);
1481 }
1482 
1483 void
1484 nd6_slowtimo(void *ignored_arg)
1485 {
1486 	int s = splsoftnet();
1487 	struct nd_ifinfo *nd6if;
1488 	struct ifnet *ifp;
1489 
1490 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
1491 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
1492 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1493 		nd6if = ND_IFINFO(ifp);
1494 		if (nd6if->basereachable && /* already initialized */
1495 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1496 			/*
1497 			 * Since reachable time rarely changes by router
1498 			 * advertisements, we SHOULD insure that a new random
1499 			 * value gets recomputed at least once every few hours.
1500 			 * (RFC 2461, 6.3.4)
1501 			 */
1502 			nd6if->recalctm = nd6_recalc_reachtm_interval;
1503 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1504 		}
1505 	}
1506 	splx(s);
1507 }
1508 
1509 int
1510 nd6_resolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
1511     struct sockaddr *dst, u_char *desten)
1512 {
1513 	struct sockaddr_dl *sdl;
1514 	struct rtentry *rt;
1515 	struct llinfo_nd6 *ln = NULL;
1516 
1517 	if (m->m_flags & M_MCAST) {
1518 		ETHER_MAP_IPV6_MULTICAST(&satosin6(dst)->sin6_addr, desten);
1519 		return (0);
1520 	}
1521 
1522 	rt = rt_getll(rt0);
1523 
1524 	if (ISSET(rt->rt_flags, RTF_REJECT) &&
1525 	    (rt->rt_expire == 0 || time_uptime < rt->rt_expire)) {
1526 		m_freem(m);
1527 		return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH);
1528 	}
1529 
1530 	/*
1531 	 * Address resolution or Neighbor Unreachability Detection
1532 	 * for the next hop.
1533 	 * At this point, the destination of the packet must be a unicast
1534 	 * or an anycast address(i.e. not a multicast).
1535 	 */
1536 	if (!ISSET(rt->rt_flags, RTF_LLINFO)) {
1537 		char addr[INET6_ADDRSTRLEN];
1538 		log(LOG_DEBUG, "%s: %s: route contains no ND information\n",
1539 		    __func__, inet_ntop(AF_INET6,
1540 		    &satosin6(rt_key(rt))->sin6_addr, addr, sizeof(addr)));
1541 		m_freem(m);
1542 		return (EINVAL);
1543 	}
1544 
1545 	if (rt->rt_gateway->sa_family != AF_LINK) {
1546 		printf("%s: something odd happens\n", __func__);
1547 		m_freem(m);
1548 		return (EINVAL);
1549 	}
1550 
1551 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1552 	KASSERT(ln != NULL);
1553 
1554 	/*
1555 	 * Move this entry to the head of the queue so that it is less likely
1556 	 * for this entry to be a target of forced garbage collection (see
1557 	 * nd6_rtrequest()).
1558 	 */
1559 	TAILQ_REMOVE(&nd6_list, ln, ln_list);
1560 	TAILQ_INSERT_HEAD(&nd6_list, ln, ln_list);
1561 
1562 	/*
1563 	 * The first time we send a packet to a neighbor whose entry is
1564 	 * STALE, we have to change the state to DELAY and a sets a timer to
1565 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1566 	 * neighbor unreachability detection on expiration.
1567 	 * (RFC 2461 7.3.3)
1568 	 */
1569 	if (ln->ln_state == ND6_LLINFO_STALE) {
1570 		ln->ln_asked = 0;
1571 		ln->ln_state = ND6_LLINFO_DELAY;
1572 		nd6_llinfo_settimer(ln, nd6_delay);
1573 	}
1574 
1575 	/*
1576 	 * If the neighbor cache entry has a state other than INCOMPLETE
1577 	 * (i.e. its link-layer address is already resolved), just
1578 	 * send the packet.
1579 	 */
1580 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE) {
1581 		sdl = satosdl(rt->rt_gateway);
1582 		if (sdl->sdl_alen != ETHER_ADDR_LEN) {
1583 			char addr[INET6_ADDRSTRLEN];
1584 			log(LOG_DEBUG, "%s: %s: incorrect nd6 information\n",
1585 			    __func__,
1586 			    inet_ntop(AF_INET6, &satosin6(dst)->sin6_addr,
1587 				addr, sizeof(addr)));
1588 			m_freem(m);
1589 			return (EINVAL);
1590 		}
1591 
1592 		bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1593 		return (0);
1594 	}
1595 
1596 	/*
1597 	 * There is a neighbor cache entry, but no ethernet address
1598 	 * response yet.  Replace the held mbuf (if any) with this
1599 	 * latest one.
1600 	 */
1601 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1602 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1603 	m_freem(ln->ln_hold);
1604 	ln->ln_hold = m;
1605 
1606 	/*
1607 	 * If there has been no NS for the neighbor after entering the
1608 	 * INCOMPLETE state, send the first solicitation.
1609 	 */
1610 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
1611 		ln->ln_asked++;
1612 		nd6_llinfo_settimer(ln, ND_IFINFO(ifp)->retrans / 1000);
1613 		nd6_ns_output(ifp, NULL, &satosin6(dst)->sin6_addr, ln, 0);
1614 	}
1615 	return (EAGAIN);
1616 }
1617 
1618 int
1619 nd6_need_cache(struct ifnet *ifp)
1620 {
1621 	/*
1622 	 * RFC2893 says:
1623 	 * - unidirectional tunnels needs no ND
1624 	 */
1625 	switch (ifp->if_type) {
1626 	case IFT_ETHER:
1627 	case IFT_IEEE80211:
1628 	case IFT_CARP:
1629 		return (1);
1630 	default:
1631 		return (0);
1632 	}
1633 }
1634 
1635 /*
1636  * oldp - syscall arg, need copyout
1637  * newp - syscall arg, need copyin
1638  */
1639 
1640 int
1641 nd6_sysctl(int name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
1642 {
1643 	void *p;
1644 	size_t ol;
1645 	int error;
1646 
1647 	error = 0;
1648 
1649 	if (newp)
1650 		return EPERM;
1651 	if (oldp && !oldlenp)
1652 		return EINVAL;
1653 	ol = oldlenp ? *oldlenp : 0;
1654 
1655 	if (oldp) {
1656 		p = malloc(ol, M_TEMP, M_WAITOK | M_CANFAIL);
1657 		if (!p)
1658 			return ENOMEM;
1659 	} else
1660 		p = NULL;
1661 	switch (name) {
1662 	case ICMPV6CTL_ND6_DRLIST:
1663 		error = fill_drlist(p, oldlenp, ol);
1664 		if (!error && p && oldp)
1665 			error = copyout(p, oldp, *oldlenp);
1666 		break;
1667 
1668 	case ICMPV6CTL_ND6_PRLIST:
1669 		error = fill_prlist(p, oldlenp, ol);
1670 		if (!error && p && oldp)
1671 			error = copyout(p, oldp, *oldlenp);
1672 		break;
1673 
1674 	default:
1675 		error = ENOPROTOOPT;
1676 		break;
1677 	}
1678 	free(p, M_TEMP, ol);
1679 
1680 	return (error);
1681 }
1682 
1683 int
1684 fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
1685 {
1686 	int error = 0, s;
1687 	struct in6_defrouter *d = NULL, *de = NULL;
1688 	struct nd_defrouter *dr;
1689 	size_t l;
1690 
1691 	s = splsoftnet();
1692 
1693 	if (oldp) {
1694 		d = (struct in6_defrouter *)oldp;
1695 		de = (struct in6_defrouter *)((caddr_t)oldp + *oldlenp);
1696 	}
1697 	l = 0;
1698 
1699 	TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
1700 		if (oldp && d + 1 <= de) {
1701 			bzero(d, sizeof(*d));
1702 			d->rtaddr.sin6_family = AF_INET6;
1703 			d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
1704 			in6_recoverscope(&d->rtaddr, &dr->rtaddr);
1705 			d->flags = dr->flags;
1706 			d->rtlifetime = dr->rtlifetime;
1707 			d->expire = dr->expire;
1708 			d->if_index = dr->ifp->if_index;
1709 		}
1710 
1711 		l += sizeof(*d);
1712 		if (d)
1713 			d++;
1714 	}
1715 
1716 	if (oldp) {
1717 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
1718 		if (l > ol)
1719 			error = ENOMEM;
1720 	} else
1721 		*oldlenp = l;
1722 
1723 	splx(s);
1724 
1725 	return (error);
1726 }
1727 
1728 int
1729 fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
1730 {
1731 	int error = 0, s;
1732 	struct nd_prefix *pr;
1733 	char *p = NULL, *ps = NULL;
1734 	char *pe = NULL;
1735 	size_t l;
1736 
1737 	s = splsoftnet();
1738 
1739 	if (oldp) {
1740 		ps = p = (char *)oldp;
1741 		pe = (char *)oldp + *oldlenp;
1742 	}
1743 	l = 0;
1744 
1745 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1746 		u_short advrtrs;
1747 		struct sockaddr_in6 sin6;
1748 		struct nd_pfxrouter *pfr;
1749 		struct in6_prefix pfx;
1750 
1751 		if (oldp && p + sizeof(struct in6_prefix) <= pe) {
1752 			memset(&pfx, 0, sizeof(pfx));
1753 			ps = p;
1754 
1755 			pfx.prefix = pr->ndpr_prefix;
1756 			in6_recoverscope(&pfx.prefix,
1757 			    &pfx.prefix.sin6_addr);
1758 			pfx.raflags = pr->ndpr_raf;
1759 			pfx.prefixlen = pr->ndpr_plen;
1760 			pfx.vltime = pr->ndpr_vltime;
1761 			pfx.pltime = pr->ndpr_pltime;
1762 			pfx.if_index = pr->ndpr_ifp->if_index;
1763 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1764 				pfx.expire = 0;
1765 			else {
1766 				time_t maxexpire;
1767 
1768 				/* XXX: we assume time_t is signed. */
1769 				maxexpire = (time_t)~(1ULL <<
1770 				    ((sizeof(maxexpire) * 8) - 1));
1771 				if (pr->ndpr_vltime <
1772 				    maxexpire - pr->ndpr_lastupdate) {
1773 					pfx.expire = pr->ndpr_lastupdate +
1774 						pr->ndpr_vltime;
1775 				} else
1776 					pfx.expire = maxexpire;
1777 			}
1778 			pfx.refcnt = pr->ndpr_refcnt;
1779 			pfx.flags = pr->ndpr_stateflags;
1780 			pfx.origin = PR_ORIG_RA;
1781 
1782 			p += sizeof(pfx); l += sizeof(pfx);
1783 
1784 			advrtrs = 0;
1785 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1786 				if (p + sizeof(sin6) > pe) {
1787 					advrtrs++;
1788 					continue;
1789 				}
1790 				bzero(&sin6, sizeof(sin6));
1791 				sin6.sin6_family = AF_INET6;
1792 				sin6.sin6_len = sizeof(struct sockaddr_in6);
1793 				in6_recoverscope(&sin6, &pfr->router->rtaddr);
1794 				advrtrs++;
1795 				memcpy(p, &sin6, sizeof(sin6));
1796 				p += sizeof(sin6);
1797 				l += sizeof(sin6);
1798 			}
1799 			pfx.advrtrs = advrtrs;
1800 			memcpy(ps, &pfx, sizeof(pfx));
1801 		}
1802 		else {
1803 			l += sizeof(pfx);
1804 			advrtrs = 0;
1805 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1806 				advrtrs++;
1807 				l += sizeof(sin6);
1808 			}
1809 		}
1810 	}
1811 
1812 	if (oldp) {
1813 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
1814 		if (l > ol)
1815 			error = ENOMEM;
1816 	} else
1817 		*oldlenp = l;
1818 
1819 	splx(s);
1820 
1821 	return (error);
1822 }
1823