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