xref: /freebsd/sys/netinet6/nd6.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
1 /*-
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_inet.h"
36 #include "opt_inet6.h"
37 #include "opt_mac.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/callout.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/sockio.h>
46 #include <sys/time.h>
47 #include <sys/kernel.h>
48 #include <sys/protosw.h>
49 #include <sys/errno.h>
50 #include <sys/syslog.h>
51 #include <sys/lock.h>
52 #include <sys/rwlock.h>
53 #include <sys/queue.h>
54 #include <sys/sysctl.h>
55 
56 #include <net/if.h>
57 #include <net/if_arc.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/iso88025.h>
61 #include <net/fddi.h>
62 #include <net/route.h>
63 #include <net/vnet.h>
64 
65 #include <netinet/in.h>
66 #include <net/if_llatbl.h>
67 #define	L3_ADDR_SIN6(le)	((struct sockaddr_in6 *) L3_ADDR(le))
68 #include <netinet/if_ether.h>
69 #include <netinet6/in6_var.h>
70 #include <netinet/ip6.h>
71 #include <netinet6/ip6_var.h>
72 #include <netinet6/scope6_var.h>
73 #include <netinet6/nd6.h>
74 #include <netinet/icmp6.h>
75 #include <netinet6/vinet6.h>
76 
77 #include <sys/limits.h>
78 #include <sys/vimage.h>
79 
80 #include <security/mac/mac_framework.h>
81 
82 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
83 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
84 
85 #define SIN6(s) ((struct sockaddr_in6 *)s)
86 
87 #ifdef VIMAGE_GLOBALS
88 int nd6_prune;
89 int nd6_delay;
90 int nd6_umaxtries;
91 int nd6_mmaxtries;
92 int nd6_useloopback;
93 int nd6_gctimer;
94 
95 /* preventing too many loops in ND option parsing */
96 int nd6_maxndopt;
97 
98 int nd6_maxnudhint;
99 int nd6_maxqueuelen;
100 
101 int nd6_debug;
102 
103 /* for debugging? */
104 #if 0
105 static int nd6_inuse, nd6_allocated;
106 #endif
107 
108 struct nd_drhead nd_defrouter;
109 struct nd_prhead nd_prefix;
110 
111 int nd6_recalc_reachtm_interval;
112 #endif /* VIMAGE_GLOBALS */
113 
114 static struct sockaddr_in6 all1_sa;
115 
116 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
117 	struct ifnet *));
118 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
119 static void nd6_slowtimo(void *);
120 static int regen_tmpaddr(struct in6_ifaddr *);
121 static struct llentry *nd6_free(struct llentry *, int);
122 static void nd6_llinfo_timer(void *);
123 static void clear_llinfo_pqueue(struct llentry *);
124 
125 #ifdef VIMAGE_GLOBALS
126 struct callout nd6_slowtimo_ch;
127 struct callout nd6_timer_ch;
128 extern struct callout in6_tmpaddrtimer_ch;
129 extern int dad_ignore_ns;
130 extern int dad_maxtry;
131 #endif
132 
133 void
134 nd6_init(void)
135 {
136 	INIT_VNET_INET6(curvnet);
137 	static int nd6_init_done = 0;
138 	int i;
139 
140 	if (nd6_init_done) {
141 		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
142 		return;
143 	}
144 
145 	V_nd6_prune	= 1;	/* walk list every 1 seconds */
146 	V_nd6_delay	= 5;	/* delay first probe time 5 second */
147 	V_nd6_umaxtries	= 3;	/* maximum unicast query */
148 	V_nd6_mmaxtries	= 3;	/* maximum multicast query */
149 	V_nd6_useloopback = 1;	/* use loopback interface for local traffic */
150 	V_nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
151 
152 	/* preventing too many loops in ND option parsing */
153 	V_nd6_maxndopt = 10;	/* max # of ND options allowed */
154 
155 	V_nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
156 	V_nd6_maxqueuelen = 1;	/* max pkts cached in unresolved ND entries */
157 
158 #ifdef ND6_DEBUG
159 	V_nd6_debug = 1;
160 #else
161 	V_nd6_debug = 0;
162 #endif
163 
164 	V_nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
165 
166 	V_dad_ignore_ns = 0;	/* ignore NS in DAD - specwise incorrect*/
167 	V_dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
168 
169 	/*
170 	 * XXX just to get this to compile KMM
171 	 */
172 #ifdef notyet
173 	V_llinfo_nd6.ln_next = &V_llinfo_nd6;
174 	V_llinfo_nd6.ln_prev = &V_llinfo_nd6;
175 #endif
176 	LIST_INIT(&V_nd_prefix);
177 
178 	V_ip6_use_tempaddr = 0;
179 	V_ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME;
180 	V_ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME;
181 	V_ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE;
182 
183 	all1_sa.sin6_family = AF_INET6;
184 	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
185 	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
186 		all1_sa.sin6_addr.s6_addr[i] = 0xff;
187 
188 	/* initialization of the default router list */
189 	TAILQ_INIT(&V_nd_defrouter);
190 	/* start timer */
191 	callout_init(&V_nd6_slowtimo_ch, 0);
192 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
193 	    nd6_slowtimo, NULL);
194 
195 	nd6_init_done = 1;
196 
197 }
198 
199 struct nd_ifinfo *
200 nd6_ifattach(struct ifnet *ifp)
201 {
202 	struct nd_ifinfo *nd;
203 
204 	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
205 	bzero(nd, sizeof(*nd));
206 
207 	nd->initialized = 1;
208 
209 	nd->chlim = IPV6_DEFHLIM;
210 	nd->basereachable = REACHABLE_TIME;
211 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
212 	nd->retrans = RETRANS_TIMER;
213 	/*
214 	 * Note that the default value of ip6_accept_rtadv is 0, which means
215 	 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
216 	 * here.
217 	 */
218 	nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
219 
220 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
221 	nd6_setmtu0(ifp, nd);
222 
223 	return nd;
224 }
225 
226 void
227 nd6_ifdetach(struct nd_ifinfo *nd)
228 {
229 
230 	free(nd, M_IP6NDP);
231 }
232 
233 /*
234  * Reset ND level link MTU. This function is called when the physical MTU
235  * changes, which means we might have to adjust the ND level MTU.
236  */
237 void
238 nd6_setmtu(struct ifnet *ifp)
239 {
240 
241 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
242 }
243 
244 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
245 void
246 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
247 {
248 	INIT_VNET_INET6(ifp->if_vnet);
249 	u_int32_t omaxmtu;
250 
251 	omaxmtu = ndi->maxmtu;
252 
253 	switch (ifp->if_type) {
254 	case IFT_ARCNET:
255 		ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */
256 		break;
257 	case IFT_FDDI:
258 		ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */
259 		break;
260 	case IFT_ISO88025:
261 		 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu);
262 		 break;
263 	default:
264 		ndi->maxmtu = ifp->if_mtu;
265 		break;
266 	}
267 
268 	/*
269 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
270 	 * undesirable situation.  We thus notify the operator of the change
271 	 * explicitly.  The check for omaxmtu is necessary to restrict the
272 	 * log to the case of changing the MTU, not initializing it.
273 	 */
274 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
275 		log(LOG_NOTICE, "nd6_setmtu0: "
276 		    "new link MTU on %s (%lu) is too small for IPv6\n",
277 		    if_name(ifp), (unsigned long)ndi->maxmtu);
278 	}
279 
280 	if (ndi->maxmtu > V_in6_maxmtu)
281 		in6_setmaxmtu(); /* check all interfaces just in case */
282 
283 #undef MIN
284 }
285 
286 void
287 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
288 {
289 
290 	bzero(ndopts, sizeof(*ndopts));
291 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
292 	ndopts->nd_opts_last
293 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
294 
295 	if (icmp6len == 0) {
296 		ndopts->nd_opts_done = 1;
297 		ndopts->nd_opts_search = NULL;
298 	}
299 }
300 
301 /*
302  * Take one ND option.
303  */
304 struct nd_opt_hdr *
305 nd6_option(union nd_opts *ndopts)
306 {
307 	struct nd_opt_hdr *nd_opt;
308 	int olen;
309 
310 	if (ndopts == NULL)
311 		panic("ndopts == NULL in nd6_option");
312 	if (ndopts->nd_opts_last == NULL)
313 		panic("uninitialized ndopts in nd6_option");
314 	if (ndopts->nd_opts_search == NULL)
315 		return NULL;
316 	if (ndopts->nd_opts_done)
317 		return NULL;
318 
319 	nd_opt = ndopts->nd_opts_search;
320 
321 	/* make sure nd_opt_len is inside the buffer */
322 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
323 		bzero(ndopts, sizeof(*ndopts));
324 		return NULL;
325 	}
326 
327 	olen = nd_opt->nd_opt_len << 3;
328 	if (olen == 0) {
329 		/*
330 		 * Message validation requires that all included
331 		 * options have a length that is greater than zero.
332 		 */
333 		bzero(ndopts, sizeof(*ndopts));
334 		return NULL;
335 	}
336 
337 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
338 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
339 		/* option overruns the end of buffer, invalid */
340 		bzero(ndopts, sizeof(*ndopts));
341 		return NULL;
342 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
343 		/* reached the end of options chain */
344 		ndopts->nd_opts_done = 1;
345 		ndopts->nd_opts_search = NULL;
346 	}
347 	return nd_opt;
348 }
349 
350 /*
351  * Parse multiple ND options.
352  * This function is much easier to use, for ND routines that do not need
353  * multiple options of the same type.
354  */
355 int
356 nd6_options(union nd_opts *ndopts)
357 {
358 	INIT_VNET_INET6(curvnet);
359 	struct nd_opt_hdr *nd_opt;
360 	int i = 0;
361 
362 	if (ndopts == NULL)
363 		panic("ndopts == NULL in nd6_options");
364 	if (ndopts->nd_opts_last == NULL)
365 		panic("uninitialized ndopts in nd6_options");
366 	if (ndopts->nd_opts_search == NULL)
367 		return 0;
368 
369 	while (1) {
370 		nd_opt = nd6_option(ndopts);
371 		if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
372 			/*
373 			 * Message validation requires that all included
374 			 * options have a length that is greater than zero.
375 			 */
376 			V_icmp6stat.icp6s_nd_badopt++;
377 			bzero(ndopts, sizeof(*ndopts));
378 			return -1;
379 		}
380 
381 		if (nd_opt == NULL)
382 			goto skip1;
383 
384 		switch (nd_opt->nd_opt_type) {
385 		case ND_OPT_SOURCE_LINKADDR:
386 		case ND_OPT_TARGET_LINKADDR:
387 		case ND_OPT_MTU:
388 		case ND_OPT_REDIRECTED_HEADER:
389 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
390 				nd6log((LOG_INFO,
391 				    "duplicated ND6 option found (type=%d)\n",
392 				    nd_opt->nd_opt_type));
393 				/* XXX bark? */
394 			} else {
395 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
396 					= nd_opt;
397 			}
398 			break;
399 		case ND_OPT_PREFIX_INFORMATION:
400 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
401 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
402 					= nd_opt;
403 			}
404 			ndopts->nd_opts_pi_end =
405 				(struct nd_opt_prefix_info *)nd_opt;
406 			break;
407 		default:
408 			/*
409 			 * Unknown options must be silently ignored,
410 			 * to accomodate future extension to the protocol.
411 			 */
412 			nd6log((LOG_DEBUG,
413 			    "nd6_options: unsupported option %d - "
414 			    "option ignored\n", nd_opt->nd_opt_type));
415 		}
416 
417 skip1:
418 		i++;
419 		if (i > V_nd6_maxndopt) {
420 			V_icmp6stat.icp6s_nd_toomanyopt++;
421 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
422 			break;
423 		}
424 
425 		if (ndopts->nd_opts_done)
426 			break;
427 	}
428 
429 	return 0;
430 }
431 
432 /*
433  * ND6 timer routine to handle ND6 entries
434  */
435 void
436 nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
437 {
438 	if (tick < 0) {
439 		ln->la_expire = 0;
440 		ln->ln_ntick = 0;
441 		callout_stop(&ln->ln_timer_ch);
442 		/*
443 		 * XXX - do we know that there is
444 		 * callout installed? i.e. are we
445 		 * guaranteed that we're not dropping
446 		 * a reference that we did not add?
447 		 * KMM
448 		 */
449 		LLE_REMREF(ln);
450 	} else {
451 		ln->la_expire = time_second + tick / hz;
452 		LLE_ADDREF(ln);
453 		if (tick > INT_MAX) {
454 			ln->ln_ntick = tick - INT_MAX;
455 			callout_reset(&ln->ln_timer_ch, INT_MAX,
456 			    nd6_llinfo_timer, ln);
457 		} else {
458 			ln->ln_ntick = 0;
459 			callout_reset(&ln->ln_timer_ch, tick,
460 			    nd6_llinfo_timer, ln);
461 		}
462 	}
463 }
464 
465 void
466 nd6_llinfo_settimer(struct llentry *ln, long tick)
467 {
468 
469 	LLE_WLOCK(ln);
470 	nd6_llinfo_settimer_locked(ln, tick);
471 	LLE_WUNLOCK(ln);
472 }
473 
474 static void
475 nd6_llinfo_timer(void *arg)
476 {
477 	struct llentry *ln;
478 	struct in6_addr *dst;
479 	struct ifnet *ifp;
480 	struct nd_ifinfo *ndi = NULL;
481 
482 	ln = (struct llentry *)arg;
483 	if (ln == NULL) {
484 		panic("%s: NULL entry!\n", __func__);
485 		return;
486 	}
487 
488 	if ((ifp = ((ln->lle_tbl != NULL) ? ln->lle_tbl->llt_ifp : NULL)) == NULL)
489 		panic("ln ifp == NULL");
490 
491 	CURVNET_SET(ifp->if_vnet);
492 	INIT_VNET_INET6(curvnet);
493 
494 	if (ln->ln_ntick > 0) {
495 		if (ln->ln_ntick > INT_MAX) {
496 			ln->ln_ntick -= INT_MAX;
497 			nd6_llinfo_settimer(ln, INT_MAX);
498 		} else {
499 			ln->ln_ntick = 0;
500 			nd6_llinfo_settimer(ln, ln->ln_ntick);
501 		}
502 		goto done;
503 	}
504 
505 	ndi = ND_IFINFO(ifp);
506 	dst = &L3_ADDR_SIN6(ln)->sin6_addr;
507 	if ((ln->la_flags & LLE_STATIC) || (ln->la_expire > time_second)) {
508 		goto done;
509 	}
510 
511 	if (ln->la_flags & LLE_DELETED) {
512 		(void)nd6_free(ln, 0);
513 		goto done;
514 	}
515 
516 	switch (ln->ln_state) {
517 	case ND6_LLINFO_INCOMPLETE:
518 		if (ln->la_asked < V_nd6_mmaxtries) {
519 			ln->la_asked++;
520 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
521 			nd6_ns_output(ifp, NULL, dst, ln, 0);
522 		} else {
523 			struct mbuf *m = ln->la_hold;
524 			if (m) {
525 				struct mbuf *m0;
526 
527 				/*
528 				 * assuming every packet in la_hold has the
529 				 * same IP header
530 				 */
531 				m0 = m->m_nextpkt;
532 				m->m_nextpkt = NULL;
533 				icmp6_error2(m, ICMP6_DST_UNREACH,
534 				    ICMP6_DST_UNREACH_ADDR, 0, ifp);
535 
536 				ln->la_hold = m0;
537 				clear_llinfo_pqueue(ln);
538 			}
539 			(void)nd6_free(ln, 0);
540 			ln = NULL;
541 		}
542 		break;
543 	case ND6_LLINFO_REACHABLE:
544 		if (!ND6_LLINFO_PERMANENT(ln)) {
545 			ln->ln_state = ND6_LLINFO_STALE;
546 			nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
547 		}
548 		break;
549 
550 	case ND6_LLINFO_STALE:
551 		/* Garbage Collection(RFC 2461 5.3) */
552 		if (!ND6_LLINFO_PERMANENT(ln)) {
553 			(void)nd6_free(ln, 1);
554 			ln = NULL;
555 		}
556 		break;
557 
558 	case ND6_LLINFO_DELAY:
559 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
560 			/* We need NUD */
561 			ln->la_asked = 1;
562 			ln->ln_state = ND6_LLINFO_PROBE;
563 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
564 			nd6_ns_output(ifp, dst, dst, ln, 0);
565 		} else {
566 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
567 			nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
568 		}
569 		break;
570 	case ND6_LLINFO_PROBE:
571 		if (ln->la_asked < V_nd6_umaxtries) {
572 			ln->la_asked++;
573 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
574 			nd6_ns_output(ifp, dst, dst, ln, 0);
575 		} else {
576 			(void)nd6_free(ln, 0);
577 			ln = NULL;
578 		}
579 		break;
580 	}
581 	CURVNET_RESTORE();
582 done:
583 	if (ln != NULL)
584 		LLE_FREE(ln);
585 }
586 
587 
588 /*
589  * ND6 timer routine to expire default route list and prefix list
590  */
591 void
592 nd6_timer(void *arg)
593 {
594 	CURVNET_SET_QUIET((struct vnet *) arg);
595 	INIT_VNET_INET6((struct vnet *) arg);
596 	int s;
597 	struct nd_defrouter *dr;
598 	struct nd_prefix *pr;
599 	struct in6_ifaddr *ia6, *nia6;
600 	struct in6_addrlifetime *lt6;
601 
602 	callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
603 	    nd6_timer, NULL);
604 
605 	/* expire default router list */
606 	s = splnet();
607 	dr = TAILQ_FIRST(&V_nd_defrouter);
608 	while (dr) {
609 		if (dr->expire && dr->expire < time_second) {
610 			struct nd_defrouter *t;
611 			t = TAILQ_NEXT(dr, dr_entry);
612 			defrtrlist_del(dr);
613 			dr = t;
614 		} else {
615 			dr = TAILQ_NEXT(dr, dr_entry);
616 		}
617 	}
618 
619 	/*
620 	 * expire interface addresses.
621 	 * in the past the loop was inside prefix expiry processing.
622 	 * However, from a stricter speci-confrmance standpoint, we should
623 	 * rather separate address lifetimes and prefix lifetimes.
624 	 */
625   addrloop:
626 	for (ia6 = V_in6_ifaddr; ia6; ia6 = nia6) {
627 		nia6 = ia6->ia_next;
628 		/* check address lifetime */
629 		lt6 = &ia6->ia6_lifetime;
630 		if (IFA6_IS_INVALID(ia6)) {
631 			int regen = 0;
632 
633 			/*
634 			 * If the expiring address is temporary, try
635 			 * regenerating a new one.  This would be useful when
636 			 * we suspended a laptop PC, then turned it on after a
637 			 * period that could invalidate all temporary
638 			 * addresses.  Although we may have to restart the
639 			 * loop (see below), it must be after purging the
640 			 * address.  Otherwise, we'd see an infinite loop of
641 			 * regeneration.
642 			 */
643 			if (V_ip6_use_tempaddr &&
644 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
645 				if (regen_tmpaddr(ia6) == 0)
646 					regen = 1;
647 			}
648 
649 			in6_purgeaddr(&ia6->ia_ifa);
650 
651 			if (regen)
652 				goto addrloop; /* XXX: see below */
653 		} else if (IFA6_IS_DEPRECATED(ia6)) {
654 			int oldflags = ia6->ia6_flags;
655 
656 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
657 
658 			/*
659 			 * If a temporary address has just become deprecated,
660 			 * regenerate a new one if possible.
661 			 */
662 			if (V_ip6_use_tempaddr &&
663 			    (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
664 			    (oldflags & IN6_IFF_DEPRECATED) == 0) {
665 
666 				if (regen_tmpaddr(ia6) == 0) {
667 					/*
668 					 * A new temporary address is
669 					 * generated.
670 					 * XXX: this means the address chain
671 					 * has changed while we are still in
672 					 * the loop.  Although the change
673 					 * would not cause disaster (because
674 					 * it's not a deletion, but an
675 					 * addition,) we'd rather restart the
676 					 * loop just for safety.  Or does this
677 					 * significantly reduce performance??
678 					 */
679 					goto addrloop;
680 				}
681 			}
682 		} else {
683 			/*
684 			 * A new RA might have made a deprecated address
685 			 * preferred.
686 			 */
687 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
688 		}
689 	}
690 
691 	/* expire prefix list */
692 	pr = V_nd_prefix.lh_first;
693 	while (pr) {
694 		/*
695 		 * check prefix lifetime.
696 		 * since pltime is just for autoconf, pltime processing for
697 		 * prefix is not necessary.
698 		 */
699 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
700 		    time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
701 			struct nd_prefix *t;
702 			t = pr->ndpr_next;
703 
704 			/*
705 			 * address expiration and prefix expiration are
706 			 * separate.  NEVER perform in6_purgeaddr here.
707 			 */
708 
709 			prelist_remove(pr);
710 			pr = t;
711 		} else
712 			pr = pr->ndpr_next;
713 	}
714 	splx(s);
715 	CURVNET_RESTORE();
716 }
717 
718 /*
719  * ia6 - deprecated/invalidated temporary address
720  */
721 static int
722 regen_tmpaddr(struct in6_ifaddr *ia6)
723 {
724 	struct ifaddr *ifa;
725 	struct ifnet *ifp;
726 	struct in6_ifaddr *public_ifa6 = NULL;
727 
728 	ifp = ia6->ia_ifa.ifa_ifp;
729 	for (ifa = ifp->if_addrlist.tqh_first; ifa;
730 	     ifa = ifa->ifa_list.tqe_next) {
731 		struct in6_ifaddr *it6;
732 
733 		if (ifa->ifa_addr->sa_family != AF_INET6)
734 			continue;
735 
736 		it6 = (struct in6_ifaddr *)ifa;
737 
738 		/* ignore no autoconf addresses. */
739 		if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
740 			continue;
741 
742 		/* ignore autoconf addresses with different prefixes. */
743 		if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
744 			continue;
745 
746 		/*
747 		 * Now we are looking at an autoconf address with the same
748 		 * prefix as ours.  If the address is temporary and is still
749 		 * preferred, do not create another one.  It would be rare, but
750 		 * could happen, for example, when we resume a laptop PC after
751 		 * a long period.
752 		 */
753 		if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
754 		    !IFA6_IS_DEPRECATED(it6)) {
755 			public_ifa6 = NULL;
756 			break;
757 		}
758 
759 		/*
760 		 * This is a public autoconf address that has the same prefix
761 		 * as ours.  If it is preferred, keep it.  We can't break the
762 		 * loop here, because there may be a still-preferred temporary
763 		 * address with the prefix.
764 		 */
765 		if (!IFA6_IS_DEPRECATED(it6))
766 		    public_ifa6 = it6;
767 	}
768 
769 	if (public_ifa6 != NULL) {
770 		int e;
771 
772 		if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
773 			log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
774 			    " tmp addr,errno=%d\n", e);
775 			return (-1);
776 		}
777 		return (0);
778 	}
779 
780 	return (-1);
781 }
782 
783 /*
784  * Nuke neighbor cache/prefix/default router management table, right before
785  * ifp goes away.
786  */
787 void
788 nd6_purge(struct ifnet *ifp)
789 {
790 	INIT_VNET_INET6(ifp->if_vnet);
791 	struct nd_defrouter *dr, *ndr;
792 	struct nd_prefix *pr, *npr;
793 
794 	/*
795 	 * Nuke default router list entries toward ifp.
796 	 * We defer removal of default router list entries that is installed
797 	 * in the routing table, in order to keep additional side effects as
798 	 * small as possible.
799 	 */
800 	for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) {
801 		ndr = TAILQ_NEXT(dr, dr_entry);
802 		if (dr->installed)
803 			continue;
804 
805 		if (dr->ifp == ifp)
806 			defrtrlist_del(dr);
807 	}
808 
809 	for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = ndr) {
810 		ndr = TAILQ_NEXT(dr, dr_entry);
811 		if (!dr->installed)
812 			continue;
813 
814 		if (dr->ifp == ifp)
815 			defrtrlist_del(dr);
816 	}
817 
818 	/* Nuke prefix list entries toward ifp */
819 	for (pr = V_nd_prefix.lh_first; pr; pr = npr) {
820 		npr = pr->ndpr_next;
821 		if (pr->ndpr_ifp == ifp) {
822 			/*
823 			 * Because if_detach() does *not* release prefixes
824 			 * while purging addresses the reference count will
825 			 * still be above zero. We therefore reset it to
826 			 * make sure that the prefix really gets purged.
827 			 */
828 			pr->ndpr_refcnt = 0;
829 
830 			/*
831 			 * Previously, pr->ndpr_addr is removed as well,
832 			 * but I strongly believe we don't have to do it.
833 			 * nd6_purge() is only called from in6_ifdetach(),
834 			 * which removes all the associated interface addresses
835 			 * by itself.
836 			 * (jinmei@kame.net 20010129)
837 			 */
838 			prelist_remove(pr);
839 		}
840 	}
841 
842 	/* cancel default outgoing interface setting */
843 	if (V_nd6_defifindex == ifp->if_index)
844 		nd6_setdefaultiface(0);
845 
846 	if (!V_ip6_forwarding && V_ip6_accept_rtadv) { /* XXX: too restrictive? */
847 		/* refresh default router list
848 		 *
849 		 *
850 		 */
851 		defrouter_select();
852 
853 	}
854 
855 	/* XXXXX
856 	 * We do not nuke the neighbor cache entries here any more
857 	 * because the neighbor cache is kept in if_afdata[AF_INET6].
858 	 * nd6_purge() is invoked by in6_ifdetach() which is called
859 	 * from if_detach() where everything gets purged. So let
860 	 * in6_domifdetach() do the actual L2 table purging work.
861 	 */
862 }
863 
864 /*
865  * the caller acquires and releases the lock on the lltbls
866  * Returns the llentry locked
867  */
868 struct llentry *
869 nd6_lookup(struct in6_addr *addr6, int flags, struct ifnet *ifp)
870 {
871 	INIT_VNET_INET6(curvnet);
872 	struct sockaddr_in6 sin6;
873 	struct llentry *ln;
874 	int llflags = 0;
875 
876 	bzero(&sin6, sizeof(sin6));
877 	sin6.sin6_len = sizeof(struct sockaddr_in6);
878 	sin6.sin6_family = AF_INET6;
879 	sin6.sin6_addr = *addr6;
880 
881 	IF_AFDATA_LOCK_ASSERT(ifp);
882 
883 	if (flags & ND6_CREATE)
884 	    llflags |= LLE_CREATE;
885 	if (flags & ND6_EXCLUSIVE)
886 	    llflags |= LLE_EXCLUSIVE;
887 
888 	ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
889 	if ((ln != NULL) && (flags & LLE_CREATE)) {
890 		ln->ln_state = ND6_LLINFO_NOSTATE;
891 		callout_init(&ln->ln_timer_ch, 0);
892 	}
893 
894 	return (ln);
895 }
896 
897 /*
898  * Test whether a given IPv6 address is a neighbor or not, ignoring
899  * the actual neighbor cache.  The neighbor cache is ignored in order
900  * to not reenter the routing code from within itself.
901  */
902 static int
903 nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
904 {
905 	INIT_VNET_INET6(ifp->if_vnet);
906 	struct nd_prefix *pr;
907 	struct ifaddr *dstaddr;
908 
909 	/*
910 	 * A link-local address is always a neighbor.
911 	 * XXX: a link does not necessarily specify a single interface.
912 	 */
913 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
914 		struct sockaddr_in6 sin6_copy;
915 		u_int32_t zone;
916 
917 		/*
918 		 * We need sin6_copy since sa6_recoverscope() may modify the
919 		 * content (XXX).
920 		 */
921 		sin6_copy = *addr;
922 		if (sa6_recoverscope(&sin6_copy))
923 			return (0); /* XXX: should be impossible */
924 		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
925 			return (0);
926 		if (sin6_copy.sin6_scope_id == zone)
927 			return (1);
928 		else
929 			return (0);
930 	}
931 
932 	/*
933 	 * If the address matches one of our addresses,
934 	 * it should be a neighbor.
935 	 * If the address matches one of our on-link prefixes, it should be a
936 	 * neighbor.
937 	 */
938 	for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
939 		if (pr->ndpr_ifp != ifp)
940 			continue;
941 
942 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
943 			continue;
944 
945 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
946 		    &addr->sin6_addr, &pr->ndpr_mask))
947 			return (1);
948 	}
949 
950 	/*
951 	 * If the address is assigned on the node of the other side of
952 	 * a p2p interface, the address should be a neighbor.
953 	 */
954 	dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr);
955 	if ((dstaddr != NULL) && (dstaddr->ifa_ifp == ifp))
956 		return (1);
957 
958 	/*
959 	 * If the default router list is empty, all addresses are regarded
960 	 * as on-link, and thus, as a neighbor.
961 	 * XXX: we restrict the condition to hosts, because routers usually do
962 	 * not have the "default router list".
963 	 */
964 	if (!V_ip6_forwarding && TAILQ_FIRST(&V_nd_defrouter) == NULL &&
965 	    V_nd6_defifindex == ifp->if_index) {
966 		return (1);
967 	}
968 
969 	return (0);
970 }
971 
972 
973 /*
974  * Detect if a given IPv6 address identifies a neighbor on a given link.
975  * XXX: should take care of the destination of a p2p link?
976  */
977 int
978 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
979 {
980 	struct llentry *lle;
981 	int rc = 0;
982 
983 	IF_AFDATA_UNLOCK_ASSERT(ifp);
984 	if (nd6_is_new_addr_neighbor(addr, ifp))
985 		return (1);
986 
987 	/*
988 	 * Even if the address matches none of our addresses, it might be
989 	 * in the neighbor cache.
990 	 */
991 	IF_AFDATA_LOCK(ifp);
992 	if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
993 		LLE_RUNLOCK(lle);
994 		rc = 1;
995 	}
996 	IF_AFDATA_UNLOCK(ifp);
997 	return (rc);
998 }
999 
1000 /*
1001  * Free an nd6 llinfo entry.
1002  * Since the function would cause significant changes in the kernel, DO NOT
1003  * make it global, unless you have a strong reason for the change, and are sure
1004  * that the change is safe.
1005  */
1006 static struct llentry *
1007 nd6_free(struct llentry *ln, int gc)
1008 {
1009 	INIT_VNET_INET6(curvnet);
1010         struct llentry *next;
1011 	struct nd_defrouter *dr;
1012 	struct ifnet *ifp=NULL;
1013 
1014 	/*
1015 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1016 	 * even though it is not harmful, it was not really necessary.
1017 	 */
1018 
1019 	/* cancel timer */
1020 	nd6_llinfo_settimer(ln, -1);
1021 
1022 	if (!V_ip6_forwarding) {
1023 		int s;
1024 		s = splnet();
1025 		dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ln->lle_tbl->llt_ifp);
1026 
1027 		if (dr != NULL && dr->expire &&
1028 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
1029 			/*
1030 			 * If the reason for the deletion is just garbage
1031 			 * collection, and the neighbor is an active default
1032 			 * router, do not delete it.  Instead, reset the GC
1033 			 * timer using the router's lifetime.
1034 			 * Simply deleting the entry would affect default
1035 			 * router selection, which is not necessarily a good
1036 			 * thing, especially when we're using router preference
1037 			 * values.
1038 			 * XXX: the check for ln_state would be redundant,
1039 			 *      but we intentionally keep it just in case.
1040 			 */
1041 			if (dr->expire > time_second)
1042 				nd6_llinfo_settimer(ln,
1043 				    (dr->expire - time_second) * hz);
1044 			else
1045 				nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
1046 			splx(s);
1047 			return (LIST_NEXT(ln, lle_next));
1048 		}
1049 
1050 		if (ln->ln_router || dr) {
1051 			/*
1052 			 * rt6_flush must be called whether or not the neighbor
1053 			 * is in the Default Router List.
1054 			 * See a corresponding comment in nd6_na_input().
1055 			 */
1056 			rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ln->lle_tbl->llt_ifp);
1057 		}
1058 
1059 		if (dr) {
1060 			/*
1061 			 * Unreachablity of a router might affect the default
1062 			 * router selection and on-link detection of advertised
1063 			 * prefixes.
1064 			 */
1065 
1066 			/*
1067 			 * Temporarily fake the state to choose a new default
1068 			 * router and to perform on-link determination of
1069 			 * prefixes correctly.
1070 			 * Below the state will be set correctly,
1071 			 * or the entry itself will be deleted.
1072 			 */
1073 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
1074 
1075 			/*
1076 			 * Since defrouter_select() does not affect the
1077 			 * on-link determination and MIP6 needs the check
1078 			 * before the default router selection, we perform
1079 			 * the check now.
1080 			 */
1081 			pfxlist_onlink_check();
1082 
1083 			/*
1084 			 * refresh default router list
1085 			 */
1086 			defrouter_select();
1087 		}
1088 		splx(s);
1089 	}
1090 
1091 	/*
1092 	 * Before deleting the entry, remember the next entry as the
1093 	 * return value.  We need this because pfxlist_onlink_check() above
1094 	 * might have freed other entries (particularly the old next entry) as
1095 	 * a side effect (XXX).
1096 	 */
1097 	next = LIST_NEXT(ln, lle_next);
1098 
1099 	ifp = ln->lle_tbl->llt_ifp;
1100 	IF_AFDATA_LOCK(ifp);
1101 	LLE_WLOCK(ln);
1102 	llentry_free(ln);
1103 	IF_AFDATA_UNLOCK(ifp);
1104 
1105 	return (next);
1106 }
1107 
1108 /*
1109  * Upper-layer reachability hint for Neighbor Unreachability Detection.
1110  *
1111  * XXX cost-effective methods?
1112  */
1113 void
1114 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1115 {
1116 	INIT_VNET_INET6(curvnet);
1117 	struct llentry *ln;
1118 	struct ifnet *ifp;
1119 
1120 	if ((dst6 == NULL) || (rt == NULL))
1121 		return;
1122 
1123 	ifp = rt->rt_ifp;
1124 	IF_AFDATA_LOCK(ifp);
1125 	ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1126 	IF_AFDATA_UNLOCK(ifp);
1127 	if (ln == NULL)
1128 		return;
1129 
1130 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
1131 		goto done;
1132 
1133 	/*
1134 	 * if we get upper-layer reachability confirmation many times,
1135 	 * it is possible we have false information.
1136 	 */
1137 	if (!force) {
1138 		ln->ln_byhint++;
1139 		if (ln->ln_byhint > V_nd6_maxnudhint) {
1140 			goto done;
1141 		}
1142 	}
1143 
1144  	ln->ln_state = ND6_LLINFO_REACHABLE;
1145 	if (!ND6_LLINFO_PERMANENT(ln)) {
1146 		nd6_llinfo_settimer(ln,
1147 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1148 	}
1149 done:
1150 	LLE_WUNLOCK(ln);
1151 }
1152 
1153 
1154 int
1155 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1156 {
1157 	INIT_VNET_INET6(ifp->if_vnet);
1158 	struct in6_drlist *drl = (struct in6_drlist *)data;
1159 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1160 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1161 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1162 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1163 	struct nd_defrouter *dr;
1164 	struct nd_prefix *pr;
1165 	int i = 0, error = 0;
1166 	int s;
1167 
1168 	switch (cmd) {
1169 	case SIOCGDRLST_IN6:
1170 		/*
1171 		 * obsolete API, use sysctl under net.inet6.icmp6
1172 		 */
1173 		bzero(drl, sizeof(*drl));
1174 		s = splnet();
1175 		dr = TAILQ_FIRST(&V_nd_defrouter);
1176 		while (dr && i < DRLSTSIZ) {
1177 			drl->defrouter[i].rtaddr = dr->rtaddr;
1178 			in6_clearscope(&drl->defrouter[i].rtaddr);
1179 
1180 			drl->defrouter[i].flags = dr->flags;
1181 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1182 			drl->defrouter[i].expire = dr->expire;
1183 			drl->defrouter[i].if_index = dr->ifp->if_index;
1184 			i++;
1185 			dr = TAILQ_NEXT(dr, dr_entry);
1186 		}
1187 		splx(s);
1188 		break;
1189 	case SIOCGPRLST_IN6:
1190 		/*
1191 		 * obsolete API, use sysctl under net.inet6.icmp6
1192 		 *
1193 		 * XXX the structure in6_prlist was changed in backward-
1194 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1195 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1196 		 */
1197 		/*
1198 		 * XXX meaning of fields, especialy "raflags", is very
1199 		 * differnet between RA prefix list and RR/static prefix list.
1200 		 * how about separating ioctls into two?
1201 		 */
1202 		bzero(oprl, sizeof(*oprl));
1203 		s = splnet();
1204 		pr = V_nd_prefix.lh_first;
1205 		while (pr && i < PRLSTSIZ) {
1206 			struct nd_pfxrouter *pfr;
1207 			int j;
1208 
1209 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1210 			oprl->prefix[i].raflags = pr->ndpr_raf;
1211 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1212 			oprl->prefix[i].vltime = pr->ndpr_vltime;
1213 			oprl->prefix[i].pltime = pr->ndpr_pltime;
1214 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1215 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1216 				oprl->prefix[i].expire = 0;
1217 			else {
1218 				time_t maxexpire;
1219 
1220 				/* XXX: we assume time_t is signed. */
1221 				maxexpire = (-1) &
1222 				    ~((time_t)1 <<
1223 				    ((sizeof(maxexpire) * 8) - 1));
1224 				if (pr->ndpr_vltime <
1225 				    maxexpire - pr->ndpr_lastupdate) {
1226 					oprl->prefix[i].expire =
1227 					    pr->ndpr_lastupdate +
1228 					    pr->ndpr_vltime;
1229 				} else
1230 					oprl->prefix[i].expire = maxexpire;
1231 			}
1232 
1233 			pfr = pr->ndpr_advrtrs.lh_first;
1234 			j = 0;
1235 			while (pfr) {
1236 				if (j < DRLSTSIZ) {
1237 #define RTRADDR oprl->prefix[i].advrtr[j]
1238 					RTRADDR = pfr->router->rtaddr;
1239 					in6_clearscope(&RTRADDR);
1240 #undef RTRADDR
1241 				}
1242 				j++;
1243 				pfr = pfr->pfr_next;
1244 			}
1245 			oprl->prefix[i].advrtrs = j;
1246 			oprl->prefix[i].origin = PR_ORIG_RA;
1247 
1248 			i++;
1249 			pr = pr->ndpr_next;
1250 		}
1251 		splx(s);
1252 
1253 		break;
1254 	case OSIOCGIFINFO_IN6:
1255 #define ND	ndi->ndi
1256 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1257 		bzero(&ND, sizeof(ND));
1258 		ND.linkmtu = IN6_LINKMTU(ifp);
1259 		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1260 		ND.basereachable = ND_IFINFO(ifp)->basereachable;
1261 		ND.reachable = ND_IFINFO(ifp)->reachable;
1262 		ND.retrans = ND_IFINFO(ifp)->retrans;
1263 		ND.flags = ND_IFINFO(ifp)->flags;
1264 		ND.recalctm = ND_IFINFO(ifp)->recalctm;
1265 		ND.chlim = ND_IFINFO(ifp)->chlim;
1266 		break;
1267 	case SIOCGIFINFO_IN6:
1268 		ND = *ND_IFINFO(ifp);
1269 		break;
1270 	case SIOCSIFINFO_IN6:
1271 		/*
1272 		 * used to change host variables from userland.
1273 		 * intented for a use on router to reflect RA configurations.
1274 		 */
1275 		/* 0 means 'unspecified' */
1276 		if (ND.linkmtu != 0) {
1277 			if (ND.linkmtu < IPV6_MMTU ||
1278 			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1279 				error = EINVAL;
1280 				break;
1281 			}
1282 			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1283 		}
1284 
1285 		if (ND.basereachable != 0) {
1286 			int obasereachable = ND_IFINFO(ifp)->basereachable;
1287 
1288 			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1289 			if (ND.basereachable != obasereachable)
1290 				ND_IFINFO(ifp)->reachable =
1291 				    ND_COMPUTE_RTIME(ND.basereachable);
1292 		}
1293 		if (ND.retrans != 0)
1294 			ND_IFINFO(ifp)->retrans = ND.retrans;
1295 		if (ND.chlim != 0)
1296 			ND_IFINFO(ifp)->chlim = ND.chlim;
1297 		/* FALLTHROUGH */
1298 	case SIOCSIFINFO_FLAGS:
1299 		ND_IFINFO(ifp)->flags = ND.flags;
1300 		break;
1301 #undef ND
1302 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1303 		/* sync kernel routing table with the default router list */
1304 		defrouter_reset();
1305 		defrouter_select();
1306 		break;
1307 	case SIOCSPFXFLUSH_IN6:
1308 	{
1309 		/* flush all the prefix advertised by routers */
1310 		struct nd_prefix *pr, *next;
1311 
1312 		s = splnet();
1313 		for (pr = V_nd_prefix.lh_first; pr; pr = next) {
1314 			struct in6_ifaddr *ia, *ia_next;
1315 
1316 			next = pr->ndpr_next;
1317 
1318 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1319 				continue; /* XXX */
1320 
1321 			/* do we really have to remove addresses as well? */
1322 			for (ia = V_in6_ifaddr; ia; ia = ia_next) {
1323 				/* ia might be removed.  keep the next ptr. */
1324 				ia_next = ia->ia_next;
1325 
1326 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1327 					continue;
1328 
1329 				if (ia->ia6_ndpr == pr)
1330 					in6_purgeaddr(&ia->ia_ifa);
1331 			}
1332 			prelist_remove(pr);
1333 		}
1334 		splx(s);
1335 		break;
1336 	}
1337 	case SIOCSRTRFLUSH_IN6:
1338 	{
1339 		/* flush all the default routers */
1340 		struct nd_defrouter *dr, *next;
1341 
1342 		s = splnet();
1343 		defrouter_reset();
1344 		for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = next) {
1345 			next = TAILQ_NEXT(dr, dr_entry);
1346 			defrtrlist_del(dr);
1347 		}
1348 		defrouter_select();
1349 		splx(s);
1350 		break;
1351 	}
1352 	case SIOCGNBRINFO_IN6:
1353 	{
1354 		struct llentry *ln;
1355 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1356 
1357 		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1358 			return (error);
1359 
1360 		IF_AFDATA_LOCK(ifp);
1361 		ln = nd6_lookup(&nb_addr, 0, ifp);
1362 		IF_AFDATA_UNLOCK(ifp);
1363 
1364 		if (ln == NULL) {
1365 			error = EINVAL;
1366 			break;
1367 		}
1368 		nbi->state = ln->ln_state;
1369 		nbi->asked = ln->la_asked;
1370 		nbi->isrouter = ln->ln_router;
1371 		nbi->expire = ln->la_expire;
1372 		LLE_RUNLOCK(ln);
1373 		break;
1374 	}
1375 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1376 		ndif->ifindex = V_nd6_defifindex;
1377 		break;
1378 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1379 		return (nd6_setdefaultiface(ndif->ifindex));
1380 	}
1381 	return (error);
1382 }
1383 
1384 /*
1385  * Create neighbor cache entry and cache link-layer address,
1386  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1387  *
1388  * type - ICMP6 type
1389  * code - type dependent information
1390  *
1391  * XXXXX
1392  *  The caller of this function already acquired the ndp
1393  *  cache table lock because the cache entry is returned.
1394  */
1395 struct llentry *
1396 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1397     int lladdrlen, int type, int code)
1398 {
1399 	INIT_VNET_INET6(curvnet);
1400 	struct llentry *ln = NULL;
1401 	int is_newentry;
1402 	int do_update;
1403 	int olladdr;
1404 	int llchange;
1405 	int flags = 0;
1406 	int newstate = 0;
1407 	uint16_t router = 0;
1408 	struct sockaddr_in6 sin6;
1409 	struct mbuf *chain = NULL;
1410 	int static_route = 0;
1411 
1412 	IF_AFDATA_UNLOCK_ASSERT(ifp);
1413 
1414 	if (ifp == NULL)
1415 		panic("ifp == NULL in nd6_cache_lladdr");
1416 	if (from == NULL)
1417 		panic("from == NULL in nd6_cache_lladdr");
1418 
1419 	/* nothing must be updated for unspecified address */
1420 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1421 		return NULL;
1422 
1423 	/*
1424 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1425 	 * the caller.
1426 	 *
1427 	 * XXX If the link does not have link-layer adderss, what should
1428 	 * we do? (ifp->if_addrlen == 0)
1429 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1430 	 * description on it in NS section (RFC 2461 7.2.3).
1431 	 */
1432 	flags |= lladdr ? ND6_EXCLUSIVE : 0;
1433 	IF_AFDATA_LOCK(ifp);
1434 	ln = nd6_lookup(from, flags, ifp);
1435 
1436 	if (ln == NULL) {
1437 		flags |= LLE_EXCLUSIVE;
1438 		ln = nd6_lookup(from, flags |ND6_CREATE, ifp);
1439 		IF_AFDATA_UNLOCK(ifp);
1440 		is_newentry = 1;
1441 	} else {
1442 		IF_AFDATA_UNLOCK(ifp);
1443 		/* do nothing if static ndp is set */
1444 		if (ln->la_flags & LLE_STATIC) {
1445 			static_route = 1;
1446 			goto done;
1447 		}
1448 		is_newentry = 0;
1449 	}
1450 	if (ln == NULL)
1451 		return (NULL);
1452 
1453 	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1454 	if (olladdr && lladdr) {
1455 		llchange = bcmp(lladdr, &ln->ll_addr,
1456 		    ifp->if_addrlen);
1457 	} else
1458 		llchange = 0;
1459 
1460 	/*
1461 	 * newentry olladdr  lladdr  llchange	(*=record)
1462 	 *	0	n	n	--	(1)
1463 	 *	0	y	n	--	(2)
1464 	 *	0	n	y	--	(3) * STALE
1465 	 *	0	y	y	n	(4) *
1466 	 *	0	y	y	y	(5) * STALE
1467 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1468 	 *	1	--	y	--	(7) * STALE
1469 	 */
1470 
1471 	if (lladdr) {		/* (3-5) and (7) */
1472 		/*
1473 		 * Record source link-layer address
1474 		 * XXX is it dependent to ifp->if_type?
1475 		 */
1476 		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
1477 		ln->la_flags |= LLE_VALID;
1478 	}
1479 
1480 	if (!is_newentry) {
1481 		if ((!olladdr && lladdr != NULL) ||	/* (3) */
1482 		    (olladdr && lladdr != NULL && llchange)) {	/* (5) */
1483 			do_update = 1;
1484 			newstate = ND6_LLINFO_STALE;
1485 		} else					/* (1-2,4) */
1486 			do_update = 0;
1487 	} else {
1488 		do_update = 1;
1489 		if (lladdr == NULL)			/* (6) */
1490 			newstate = ND6_LLINFO_NOSTATE;
1491 		else					/* (7) */
1492 			newstate = ND6_LLINFO_STALE;
1493 	}
1494 
1495 	if (do_update) {
1496 		/*
1497 		 * Update the state of the neighbor cache.
1498 		 */
1499 		ln->ln_state = newstate;
1500 
1501 		if (ln->ln_state == ND6_LLINFO_STALE) {
1502 			/*
1503 			 * XXX: since nd6_output() below will cause
1504 			 * state tansition to DELAY and reset the timer,
1505 			 * we must set the timer now, although it is actually
1506 			 * meaningless.
1507 			 */
1508 			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1509 
1510 			if (ln->la_hold) {
1511 				struct mbuf *m_hold, *m_hold_next;
1512 
1513 				/*
1514 				 * reset the la_hold in advance, to explicitly
1515 				 * prevent a la_hold lookup in nd6_output()
1516 				 * (wouldn't happen, though...)
1517 				 */
1518 				for (m_hold = ln->la_hold, ln->la_hold = NULL;
1519 				    m_hold; m_hold = m_hold_next) {
1520 					m_hold_next = m_hold->m_nextpkt;
1521 					m_hold->m_nextpkt = NULL;
1522 
1523 					/*
1524 					 * we assume ifp is not a p2p here, so
1525 					 * just set the 2nd argument as the
1526 					 * 1st one.
1527 					 */
1528 					nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
1529 				}
1530 				/*
1531 				 * If we have mbufs in the chain we need to do
1532 				 * deferred transmit. Copy the address from the
1533 				 * llentry before dropping the lock down below.
1534 				 */
1535 				if (chain != NULL)
1536 					memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
1537 			}
1538 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1539 			/* probe right away */
1540 			nd6_llinfo_settimer_locked((void *)ln, 0);
1541 		}
1542 	}
1543 
1544 	/*
1545 	 * ICMP6 type dependent behavior.
1546 	 *
1547 	 * NS: clear IsRouter if new entry
1548 	 * RS: clear IsRouter
1549 	 * RA: set IsRouter if there's lladdr
1550 	 * redir: clear IsRouter if new entry
1551 	 *
1552 	 * RA case, (1):
1553 	 * The spec says that we must set IsRouter in the following cases:
1554 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1555 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1556 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1557 	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1558 	 * neighbor cache, this is similar to (6).
1559 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1560 	 *
1561 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1562 	 *							D R
1563 	 *	0	n	n	--	(1)	c   ?     s
1564 	 *	0	y	n	--	(2)	c   s     s
1565 	 *	0	n	y	--	(3)	c   s     s
1566 	 *	0	y	y	n	(4)	c   s     s
1567 	 *	0	y	y	y	(5)	c   s     s
1568 	 *	1	--	n	--	(6) c	c	c s
1569 	 *	1	--	y	--	(7) c	c   s	c s
1570 	 *
1571 	 *					(c=clear s=set)
1572 	 */
1573 	switch (type & 0xff) {
1574 	case ND_NEIGHBOR_SOLICIT:
1575 		/*
1576 		 * New entry must have is_router flag cleared.
1577 		 */
1578 		if (is_newentry)	/* (6-7) */
1579 			ln->ln_router = 0;
1580 		break;
1581 	case ND_REDIRECT:
1582 		/*
1583 		 * If the icmp is a redirect to a better router, always set the
1584 		 * is_router flag.  Otherwise, if the entry is newly created,
1585 		 * clear the flag.  [RFC 2461, sec 8.3]
1586 		 */
1587 		if (code == ND_REDIRECT_ROUTER)
1588 			ln->ln_router = 1;
1589 		else if (is_newentry) /* (6-7) */
1590 			ln->ln_router = 0;
1591 		break;
1592 	case ND_ROUTER_SOLICIT:
1593 		/*
1594 		 * is_router flag must always be cleared.
1595 		 */
1596 		ln->ln_router = 0;
1597 		break;
1598 	case ND_ROUTER_ADVERT:
1599 		/*
1600 		 * Mark an entry with lladdr as a router.
1601 		 */
1602 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1603 		    (is_newentry && lladdr)) {			/* (7) */
1604 			ln->ln_router = 1;
1605 		}
1606 		break;
1607 	}
1608 
1609 	if (ln != NULL) {
1610 		static_route = (ln->la_flags & LLE_STATIC);
1611 		router = ln->ln_router;
1612 
1613 		if (flags & ND6_EXCLUSIVE)
1614 			LLE_WUNLOCK(ln);
1615 		else
1616 			LLE_RUNLOCK(ln);
1617 		if (static_route)
1618 			ln = NULL;
1619 	}
1620 	if (chain)
1621 		nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
1622 
1623 	/*
1624 	 * When the link-layer address of a router changes, select the
1625 	 * best router again.  In particular, when the neighbor entry is newly
1626 	 * created, it might affect the selection policy.
1627 	 * Question: can we restrict the first condition to the "is_newentry"
1628 	 * case?
1629 	 * XXX: when we hear an RA from a new router with the link-layer
1630 	 * address option, defrouter_select() is called twice, since
1631 	 * defrtrlist_update called the function as well.  However, I believe
1632 	 * we can compromise the overhead, since it only happens the first
1633 	 * time.
1634 	 * XXX: although defrouter_select() should not have a bad effect
1635 	 * for those are not autoconfigured hosts, we explicitly avoid such
1636 	 * cases for safety.
1637 	 */
1638 	if (do_update && router && !V_ip6_forwarding && V_ip6_accept_rtadv) {
1639 		/*
1640 		 * guaranteed recursion
1641 		 */
1642 		defrouter_select();
1643 	}
1644 
1645 	return (ln);
1646 done:
1647 	if (ln != NULL) {
1648 		if (flags & ND6_EXCLUSIVE)
1649 			LLE_WUNLOCK(ln);
1650 		else
1651 			LLE_RUNLOCK(ln);
1652 		if (static_route)
1653 			ln = NULL;
1654 	}
1655 	return (ln);
1656 }
1657 
1658 static void
1659 nd6_slowtimo(void *arg)
1660 {
1661 	CURVNET_SET((struct vnet *) arg);
1662 	INIT_VNET_NET((struct vnet *) arg);
1663 	INIT_VNET_INET6((struct vnet *) arg);
1664 	struct nd_ifinfo *nd6if;
1665 	struct ifnet *ifp;
1666 
1667 	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1668 	    nd6_slowtimo, NULL);
1669 	IFNET_RLOCK();
1670 	for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
1671 	    ifp = TAILQ_NEXT(ifp, if_list)) {
1672 		nd6if = ND_IFINFO(ifp);
1673 		if (nd6if->basereachable && /* already initialized */
1674 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1675 			/*
1676 			 * Since reachable time rarely changes by router
1677 			 * advertisements, we SHOULD insure that a new random
1678 			 * value gets recomputed at least once every few hours.
1679 			 * (RFC 2461, 6.3.4)
1680 			 */
1681 			nd6if->recalctm = V_nd6_recalc_reachtm_interval;
1682 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1683 		}
1684 	}
1685 	IFNET_RUNLOCK();
1686 	CURVNET_RESTORE();
1687 }
1688 
1689 int
1690 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1691     struct sockaddr_in6 *dst, struct rtentry *rt0)
1692 {
1693 
1694 	return (nd6_output_lle(ifp, origifp, m0, dst, rt0, NULL, NULL));
1695 }
1696 
1697 
1698 /*
1699  * Note that I'm not enforcing any global serialization
1700  * lle state or asked changes here as the logic is too
1701  * complicated to avoid having to always acquire an exclusive
1702  * lock
1703  * KMM
1704  *
1705  */
1706 #define senderr(e) { error = (e); goto bad;}
1707 
1708 int
1709 nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1710     struct sockaddr_in6 *dst, struct rtentry *rt0, struct llentry *lle,
1711 	struct mbuf **chain)
1712 {
1713 	INIT_VNET_INET6(curvnet);
1714 	struct mbuf *m = m0;
1715 	struct llentry *ln = lle;
1716 	int error = 0;
1717 	int flags = 0;
1718 
1719 #ifdef INVARIANTS
1720 	if (lle != NULL) {
1721 
1722 		LLE_WLOCK_ASSERT(lle);
1723 
1724 		KASSERT(chain != NULL, (" lle locked but no mbuf chain pointer passed"));
1725 	}
1726 #endif
1727 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1728 		goto sendpkt;
1729 
1730 	if (nd6_need_cache(ifp) == 0)
1731 		goto sendpkt;
1732 
1733 	/*
1734 	 * next hop determination.  This routine is derived from ether_output.
1735 	 */
1736 
1737 	/*
1738 	 * Address resolution or Neighbor Unreachability Detection
1739 	 * for the next hop.
1740 	 * At this point, the destination of the packet must be a unicast
1741 	 * or an anycast address(i.e. not a multicast).
1742 	 */
1743 
1744 	flags = ((m != NULL) || (lle != NULL)) ? LLE_EXCLUSIVE : 0;
1745 	if (ln == NULL) {
1746 	retry:
1747 		IF_AFDATA_LOCK(ifp);
1748 		ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)dst);
1749 		IF_AFDATA_UNLOCK(ifp);
1750 		if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp))  {
1751 			/*
1752 			 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1753 			 * the condition below is not very efficient.  But we believe
1754 			 * it is tolerable, because this should be a rare case.
1755 			 */
1756 			flags = ND6_CREATE | (m ? ND6_EXCLUSIVE : 0);
1757 			IF_AFDATA_LOCK(ifp);
1758 			ln = nd6_lookup(&dst->sin6_addr, flags, ifp);
1759 			IF_AFDATA_UNLOCK(ifp);
1760 		}
1761 	}
1762 	if (ln == NULL) {
1763 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1764 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1765 			char ip6buf[INET6_ADDRSTRLEN];
1766 			log(LOG_DEBUG,
1767 			    "nd6_output: can't allocate llinfo for %s "
1768 			    "(ln=%p)\n",
1769 			    ip6_sprintf(ip6buf, &dst->sin6_addr), ln);
1770 			senderr(EIO);	/* XXX: good error? */
1771 		}
1772 		goto sendpkt;	/* send anyway */
1773 	}
1774 
1775 	/* We don't have to do link-layer address resolution on a p2p link. */
1776 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1777 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
1778 		if ((flags & LLE_EXCLUSIVE) == 0) {
1779 			flags |= LLE_EXCLUSIVE;
1780 			goto retry;
1781 		}
1782 		ln->ln_state = ND6_LLINFO_STALE;
1783 		nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1784 	}
1785 
1786 	/*
1787 	 * The first time we send a packet to a neighbor whose entry is
1788 	 * STALE, we have to change the state to DELAY and a sets a timer to
1789 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1790 	 * neighbor unreachability detection on expiration.
1791 	 * (RFC 2461 7.3.3)
1792 	 */
1793 	if (ln->ln_state == ND6_LLINFO_STALE) {
1794 		if ((flags & LLE_EXCLUSIVE) == 0) {
1795 			flags |= LLE_EXCLUSIVE;
1796 			LLE_RUNLOCK(ln);
1797 			goto retry;
1798 		}
1799 		ln->la_asked = 0;
1800 		ln->ln_state = ND6_LLINFO_DELAY;
1801 		nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz);
1802 	}
1803 
1804 	/*
1805 	 * If the neighbor cache entry has a state other than INCOMPLETE
1806 	 * (i.e. its link-layer address is already resolved), just
1807 	 * send the packet.
1808 	 */
1809 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1810 		goto sendpkt;
1811 
1812 	/*
1813 	 * There is a neighbor cache entry, but no ethernet address
1814 	 * response yet.  Append this latest packet to the end of the
1815 	 * packet queue in the mbuf, unless the number of the packet
1816 	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
1817 	 * the oldest packet in the queue will be removed.
1818 	 */
1819 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1820 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1821 
1822 	if ((flags & LLE_EXCLUSIVE) == 0) {
1823 		flags |= LLE_EXCLUSIVE;
1824 		LLE_RUNLOCK(ln);
1825 		goto retry;
1826 	}
1827 	if (ln->la_hold) {
1828 		struct mbuf *m_hold;
1829 		int i;
1830 
1831 		i = 0;
1832 		for (m_hold = ln->la_hold; m_hold; m_hold = m_hold->m_nextpkt) {
1833 			i++;
1834 			if (m_hold->m_nextpkt == NULL) {
1835 				m_hold->m_nextpkt = m;
1836 				break;
1837 			}
1838 		}
1839 		while (i >= V_nd6_maxqueuelen) {
1840 			m_hold = ln->la_hold;
1841 			ln->la_hold = ln->la_hold->m_nextpkt;
1842 			m_freem(m_hold);
1843 			i--;
1844 		}
1845 	} else {
1846 		ln->la_hold = m;
1847 	}
1848 	/*
1849 	 * We did the lookup (no lle arg) so we
1850 	 * need to do the unlock here
1851 	 */
1852 	if (lle == NULL) {
1853 		if (flags & LLE_EXCLUSIVE)
1854 			LLE_WUNLOCK(ln);
1855 		else
1856 			LLE_RUNLOCK(ln);
1857 	}
1858 
1859 	/*
1860 	 * If there has been no NS for the neighbor after entering the
1861 	 * INCOMPLETE state, send the first solicitation.
1862 	 */
1863 	if (!ND6_LLINFO_PERMANENT(ln) && ln->la_asked == 0) {
1864 		ln->la_asked++;
1865 
1866 		nd6_llinfo_settimer(ln,
1867 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1868 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1869 	}
1870 	return (0);
1871 
1872   sendpkt:
1873 	/* discard the packet if IPv6 operation is disabled on the interface */
1874 	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1875 		error = ENETDOWN; /* better error? */
1876 		goto bad;
1877 	}
1878 	/*
1879 	 * ln is valid and the caller did not pass in
1880 	 * an llentry
1881 	 */
1882 	if ((ln != NULL) && (lle == NULL)) {
1883 		if (flags & LLE_EXCLUSIVE)
1884 			LLE_WUNLOCK(ln);
1885 		else
1886 			LLE_RUNLOCK(ln);
1887 	}
1888 
1889 #ifdef MAC
1890 	mac_netinet6_nd6_send(ifp, m);
1891 #endif
1892 	/*
1893 	 * We were passed in a pointer to an lle with the lock held
1894 	 * this means that we can't call if_output as we will
1895 	 * recurse on the lle lock - so what we do is we create
1896 	 * a list of mbufs to send and transmit them in the caller
1897 	 * after the lock is dropped
1898 	 */
1899 	if (lle != NULL) {
1900 		if (*chain == NULL)
1901 			*chain = m;
1902 		else {
1903 			struct mbuf *m = *chain;
1904 
1905 			/*
1906 			 * append mbuf to end of deferred chain
1907 			 */
1908 			while (m->m_nextpkt != NULL)
1909 				m = m->m_nextpkt;
1910 			m->m_nextpkt = m;
1911 		}
1912 		return (error);
1913 	}
1914 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1915 		return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1916 		    NULL));
1917 	}
1918 	error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, NULL);
1919 	return (error);
1920 
1921   bad:
1922 	/*
1923 	 * ln is valid and the caller did not pass in
1924 	 * an llentry
1925 	 */
1926 	if ((ln != NULL) && (lle == NULL)) {
1927 		if (flags & LLE_EXCLUSIVE)
1928 			LLE_WUNLOCK(ln);
1929 		else
1930 			LLE_RUNLOCK(ln);
1931 	}
1932 	if (m)
1933 		m_freem(m);
1934 	return (error);
1935 }
1936 #undef senderr
1937 
1938 
1939 int
1940 nd6_output_flush(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
1941     struct sockaddr_in6 *dst, struct rtentry *rt)
1942 {
1943 	struct mbuf *m, *m_head;
1944 	struct ifnet *outifp;
1945 	int error = 0;
1946 
1947 	m_head = chain;
1948 	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1949 		outifp = origifp;
1950 	else
1951 		outifp = ifp;
1952 
1953 	while (m_head) {
1954 		m = m_head;
1955 		m_head = m_head->m_nextpkt;
1956 		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt);
1957 	}
1958 
1959 	/*
1960 	 * XXX
1961 	 * note that intermediate errors are blindly ignored - but this is
1962 	 * the same convention as used with nd6_output when called by
1963 	 * nd6_cache_lladdr
1964 	 */
1965 	return (error);
1966 }
1967 
1968 
1969 int
1970 nd6_need_cache(struct ifnet *ifp)
1971 {
1972 	/*
1973 	 * XXX: we currently do not make neighbor cache on any interface
1974 	 * other than ARCnet, Ethernet, FDDI and GIF.
1975 	 *
1976 	 * RFC2893 says:
1977 	 * - unidirectional tunnels needs no ND
1978 	 */
1979 	switch (ifp->if_type) {
1980 	case IFT_ARCNET:
1981 	case IFT_ETHER:
1982 	case IFT_FDDI:
1983 	case IFT_IEEE1394:
1984 #ifdef IFT_L2VLAN
1985 	case IFT_L2VLAN:
1986 #endif
1987 #ifdef IFT_IEEE80211
1988 	case IFT_IEEE80211:
1989 #endif
1990 #ifdef IFT_CARP
1991 	case IFT_CARP:
1992 #endif
1993 	case IFT_GIF:		/* XXX need more cases? */
1994 	case IFT_PPP:
1995 	case IFT_TUNNEL:
1996 	case IFT_BRIDGE:
1997 	case IFT_PROPVIRTUAL:
1998 		return (1);
1999 	default:
2000 		return (0);
2001 	}
2002 }
2003 
2004 /*
2005  * the callers of this function need to be re-worked to drop
2006  * the lle lock, drop here for now
2007  */
2008 int
2009 nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
2010     struct sockaddr *dst, u_char *desten, struct llentry **lle)
2011 {
2012 	struct llentry *ln;
2013 
2014 	*lle = NULL;
2015 	IF_AFDATA_UNLOCK_ASSERT(ifp);
2016 	if (m->m_flags & M_MCAST) {
2017 		int i;
2018 
2019 		switch (ifp->if_type) {
2020 		case IFT_ETHER:
2021 		case IFT_FDDI:
2022 #ifdef IFT_L2VLAN
2023 		case IFT_L2VLAN:
2024 #endif
2025 #ifdef IFT_IEEE80211
2026 		case IFT_IEEE80211:
2027 #endif
2028 		case IFT_BRIDGE:
2029 		case IFT_ISO88025:
2030 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2031 						 desten);
2032 			return (0);
2033 		case IFT_IEEE1394:
2034 			/*
2035 			 * netbsd can use if_broadcastaddr, but we don't do so
2036 			 * to reduce # of ifdef.
2037 			 */
2038 			for (i = 0; i < ifp->if_addrlen; i++)
2039 				desten[i] = ~0;
2040 			return (0);
2041 		case IFT_ARCNET:
2042 			*desten = 0;
2043 			return (0);
2044 		default:
2045 			m_freem(m);
2046 			return (EAFNOSUPPORT);
2047 		}
2048 	}
2049 
2050 
2051 	/*
2052 	 * the entry should have been created in nd6_store_lladdr
2053 	 */
2054 	IF_AFDATA_LOCK(ifp);
2055 	ln = lla_lookup(LLTABLE6(ifp), 0, dst);
2056 	IF_AFDATA_UNLOCK(ifp);
2057 	if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
2058 		if (ln != NULL)
2059 			LLE_RUNLOCK(ln);
2060 		/* this could happen, if we could not allocate memory */
2061 		m_freem(m);
2062 		return (1);
2063 	}
2064 
2065 	bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
2066 	*lle = ln;
2067 	LLE_RUNLOCK(ln);
2068 	/*
2069 	 * A *small* use after free race exists here
2070 	 */
2071 	return (0);
2072 }
2073 
2074 static void
2075 clear_llinfo_pqueue(struct llentry *ln)
2076 {
2077 	struct mbuf *m_hold, *m_hold_next;
2078 
2079 	for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2080 		m_hold_next = m_hold->m_nextpkt;
2081 		m_hold->m_nextpkt = NULL;
2082 		m_freem(m_hold);
2083 	}
2084 
2085 	ln->la_hold = NULL;
2086 	return;
2087 }
2088 
2089 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2090 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2091 #ifdef SYSCTL_DECL
2092 SYSCTL_DECL(_net_inet6_icmp6);
2093 #endif
2094 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2095 	CTLFLAG_RD, nd6_sysctl_drlist, "");
2096 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2097 	CTLFLAG_RD, nd6_sysctl_prlist, "");
2098 SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN,
2099 	nd6_maxqueuelen, CTLFLAG_RW, nd6_maxqueuelen, 1, "");
2100 
2101 static int
2102 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2103 {
2104 	INIT_VNET_INET6(curvnet);
2105 	int error;
2106 	char buf[1024] __aligned(4);
2107 	struct in6_defrouter *d, *de;
2108 	struct nd_defrouter *dr;
2109 
2110 	if (req->newptr)
2111 		return EPERM;
2112 	error = 0;
2113 
2114 	for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
2115 	     dr = TAILQ_NEXT(dr, dr_entry)) {
2116 		d = (struct in6_defrouter *)buf;
2117 		de = (struct in6_defrouter *)(buf + sizeof(buf));
2118 
2119 		if (d + 1 <= de) {
2120 			bzero(d, sizeof(*d));
2121 			d->rtaddr.sin6_family = AF_INET6;
2122 			d->rtaddr.sin6_len = sizeof(d->rtaddr);
2123 			d->rtaddr.sin6_addr = dr->rtaddr;
2124 			error = sa6_recoverscope(&d->rtaddr);
2125 			if (error != 0)
2126 				return (error);
2127 			d->flags = dr->flags;
2128 			d->rtlifetime = dr->rtlifetime;
2129 			d->expire = dr->expire;
2130 			d->if_index = dr->ifp->if_index;
2131 		} else
2132 			panic("buffer too short");
2133 
2134 		error = SYSCTL_OUT(req, buf, sizeof(*d));
2135 		if (error)
2136 			break;
2137 	}
2138 
2139 	return (error);
2140 }
2141 
2142 static int
2143 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2144 {
2145 	INIT_VNET_INET6(curvnet);
2146 	int error;
2147 	char buf[1024] __aligned(4);
2148 	struct in6_prefix *p, *pe;
2149 	struct nd_prefix *pr;
2150 	char ip6buf[INET6_ADDRSTRLEN];
2151 
2152 	if (req->newptr)
2153 		return EPERM;
2154 	error = 0;
2155 
2156 	for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2157 		u_short advrtrs;
2158 		size_t advance;
2159 		struct sockaddr_in6 *sin6, *s6;
2160 		struct nd_pfxrouter *pfr;
2161 
2162 		p = (struct in6_prefix *)buf;
2163 		pe = (struct in6_prefix *)(buf + sizeof(buf));
2164 
2165 		if (p + 1 <= pe) {
2166 			bzero(p, sizeof(*p));
2167 			sin6 = (struct sockaddr_in6 *)(p + 1);
2168 
2169 			p->prefix = pr->ndpr_prefix;
2170 			if (sa6_recoverscope(&p->prefix)) {
2171 				log(LOG_ERR,
2172 				    "scope error in prefix list (%s)\n",
2173 				    ip6_sprintf(ip6buf, &p->prefix.sin6_addr));
2174 				/* XXX: press on... */
2175 			}
2176 			p->raflags = pr->ndpr_raf;
2177 			p->prefixlen = pr->ndpr_plen;
2178 			p->vltime = pr->ndpr_vltime;
2179 			p->pltime = pr->ndpr_pltime;
2180 			p->if_index = pr->ndpr_ifp->if_index;
2181 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2182 				p->expire = 0;
2183 			else {
2184 				time_t maxexpire;
2185 
2186 				/* XXX: we assume time_t is signed. */
2187 				maxexpire = (-1) &
2188 				    ~((time_t)1 <<
2189 				    ((sizeof(maxexpire) * 8) - 1));
2190 				if (pr->ndpr_vltime <
2191 				    maxexpire - pr->ndpr_lastupdate) {
2192 				    p->expire = pr->ndpr_lastupdate +
2193 				        pr->ndpr_vltime;
2194 				} else
2195 					p->expire = maxexpire;
2196 			}
2197 			p->refcnt = pr->ndpr_refcnt;
2198 			p->flags = pr->ndpr_stateflags;
2199 			p->origin = PR_ORIG_RA;
2200 			advrtrs = 0;
2201 			for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2202 			     pfr = pfr->pfr_next) {
2203 				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2204 					advrtrs++;
2205 					continue;
2206 				}
2207 				s6 = &sin6[advrtrs];
2208 				bzero(s6, sizeof(*s6));
2209 				s6->sin6_family = AF_INET6;
2210 				s6->sin6_len = sizeof(*sin6);
2211 				s6->sin6_addr = pfr->router->rtaddr;
2212 				if (sa6_recoverscope(s6)) {
2213 					log(LOG_ERR,
2214 					    "scope error in "
2215 					    "prefix list (%s)\n",
2216 					    ip6_sprintf(ip6buf,
2217 						    &pfr->router->rtaddr));
2218 				}
2219 				advrtrs++;
2220 			}
2221 			p->advrtrs = advrtrs;
2222 		} else
2223 			panic("buffer too short");
2224 
2225 		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2226 		error = SYSCTL_OUT(req, buf, advance);
2227 		if (error)
2228 			break;
2229 	}
2230 
2231 	return (error);
2232 }
2233