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