nd6.c revision 191340
1158115Sume/*-
2158115Sume * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3158115Sume * All rights reserved.
4158115Sume *
5158115Sume * Redistribution and use in source and binary forms, with or without
6158115Sume * modification, are permitted provided that the following conditions
7158115Sume * are met:
8158115Sume * 1. Redistributions of source code must retain the above copyright
9158115Sume *    notice, this list of conditions and the following disclaimer.
10158115Sume * 2. Redistributions in binary form must reproduce the above copyright
11158115Sume *    notice, this list of conditions and the following disclaimer in the
12158115Sume *    documentation and/or other materials provided with the distribution.
13158115Sume * 3. Neither the name of the project nor the names of its contributors
14158115Sume *    may be used to endorse or promote products derived from this software
15158115Sume *    without specific prior written permission.
16158115Sume *
17158115Sume * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18158115Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19158115Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20158115Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21158115Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22158115Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23158115Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24158115Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25158115Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26158115Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27158115Sume * SUCH DAMAGE.
28158115Sume *
29171795Sbushman *	$KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $
30171795Sbushman */
31158115Sume
32158115Sume#include <sys/cdefs.h>
33158115Sume__FBSDID("$FreeBSD: head/sys/netinet6/nd6.c 191340 2009-04-20 22:45:21Z rwatson $");
34158115Sume
35158115Sume#include "opt_inet.h"
36158115Sume#include "opt_inet6.h"
37158115Sume#include "opt_mac.h"
38158115Sume#include "opt_route.h"
39158115Sume
40194112Sdes#include <sys/param.h>
41158115Sume#include <sys/systm.h>
42158115Sume#include <sys/callout.h>
43158115Sume#include <sys/malloc.h>
44158115Sume#include <sys/mbuf.h>
45158115Sume#include <sys/socket.h>
46158115Sume#include <sys/sockio.h>
47158115Sume#include <sys/time.h>
48158115Sume#include <sys/kernel.h>
49158115Sume#include <sys/protosw.h>
50194112Sdes#include <sys/errno.h>
51158115Sume#include <sys/syslog.h>
52158115Sume#include <sys/lock.h>
53158115Sume#include <sys/rwlock.h>
54194112Sdes#include <sys/queue.h>
55158115Sume#include <sys/sysctl.h>
56158115Sume#include <sys/vimage.h>
57158115Sume
58158115Sume#include <net/if.h>
59158115Sume#include <net/if_arc.h>
60158115Sume#include <net/if_dl.h>
61158115Sume#include <net/if_types.h>
62194112Sdes#include <net/iso88025.h>
63158115Sume#include <net/fddi.h>
64158115Sume#include <net/route.h>
65158115Sume#include <net/vnet.h>
66158115Sume
67158115Sume#include <netinet/in.h>
68158115Sume#include <net/if_llatbl.h>
69158115Sume#define	L3_ADDR_SIN6(le)	((struct sockaddr_in6 *) L3_ADDR(le))
70228990Suqs#include <netinet/if_ether.h>
71158115Sume#include <netinet6/in6_var.h>
72158115Sume#include <netinet/ip6.h>
73158115Sume#include <netinet6/ip6_var.h>
74194112Sdes#include <netinet6/scope6_var.h>
75158115Sume#include <netinet6/nd6.h>
76158115Sume#include <netinet/icmp6.h>
77158115Sume#include <netinet6/vinet6.h>
78158115Sume
79158115Sume#include <sys/limits.h>
80158115Sume
81194112Sdes#include <security/mac/mac_framework.h>
82158115Sume
83158115Sume#define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
84158115Sume#define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
85158115Sume
86158115Sume#define SIN6(s) ((struct sockaddr_in6 *)s)
87158115Sume
88158115Sume#ifdef VIMAGE_GLOBALS
89158115Sumeint nd6_prune;
90158115Sumeint nd6_delay;
91158115Sumeint nd6_umaxtries;
92158115Sumeint nd6_mmaxtries;
93158115Sumeint nd6_useloopback;
94158115Sumeint nd6_gctimer;
95158115Sume
96158115Sume/* preventing too many loops in ND option parsing */
97158115Sumeint nd6_maxndopt;
98158115Sume
99158115Sumeint nd6_maxnudhint;
100158115Sumeint nd6_maxqueuelen;
101158115Sume
102158115Sumeint nd6_debug;
103158115Sume
104158115Sume/* for debugging? */
105158115Sume#if 0
106158115Sumestatic int nd6_inuse, nd6_allocated;
107158115Sume#endif
108158115Sume
109158115Sumestruct nd_drhead nd_defrouter;
110158115Sumestruct nd_prhead nd_prefix;
111158115Sume
112158115Sumeint nd6_recalc_reachtm_interval;
113158115Sume#endif /* VIMAGE_GLOBALS */
114158115Sume
115158115Sumestatic struct sockaddr_in6 all1_sa;
116158115Sume
117158115Sumestatic int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *,
118158115Sume	struct ifnet *));
119158115Sumestatic void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
120158115Sumestatic void nd6_slowtimo(void *);
121158115Sumestatic int regen_tmpaddr(struct in6_ifaddr *);
122158115Sumestatic struct llentry *nd6_free(struct llentry *, int);
123158115Sumestatic void nd6_llinfo_timer(void *);
124158115Sumestatic void clear_llinfo_pqueue(struct llentry *);
125158115Sume
126158115Sume#ifdef VIMAGE_GLOBALS
127158115Sumestruct callout nd6_slowtimo_ch;
128158115Sumestruct callout nd6_timer_ch;
129158115Sumeextern struct callout in6_tmpaddrtimer_ch;
130158115Sumeextern int dad_ignore_ns;
131158115Sumeextern int dad_maxtry;
132158115Sume#endif
133158115Sume
134158115Sumevoid
135158115Sumend6_init(void)
136158115Sume{
137158115Sume	INIT_VNET_INET6(curvnet);
138158115Sume	static int nd6_init_done = 0;
139158115Sume	int i;
140158115Sume
141158115Sume	if (nd6_init_done) {
142158115Sume		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
143158115Sume		return;
144158115Sume	}
145158115Sume
146194112Sdes	V_nd6_prune	= 1;	/* walk list every 1 seconds */
147158115Sume	V_nd6_delay	= 5;	/* delay first probe time 5 second */
148158115Sume	V_nd6_umaxtries	= 3;	/* maximum unicast query */
149158115Sume	V_nd6_mmaxtries	= 3;	/* maximum multicast query */
150158115Sume	V_nd6_useloopback = 1;	/* use loopback interface for local traffic */
151158115Sume	V_nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
152158115Sume
153158115Sume	/* preventing too many loops in ND option parsing */
154158115Sume	V_nd6_maxndopt = 10;	/* max # of ND options allowed */
155158115Sume
156158115Sume	V_nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
157158115Sume	V_nd6_maxqueuelen = 1;	/* max pkts cached in unresolved ND entries */
158158115Sume
159158115Sume#ifdef ND6_DEBUG
160158115Sume	V_nd6_debug = 1;
161158115Sume#else
162158115Sume	V_nd6_debug = 0;
163194112Sdes#endif
164158115Sume
165158115Sume	V_nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
166158115Sume
167194112Sdes	V_dad_ignore_ns = 0;	/* ignore NS in DAD - specwise incorrect*/
168194112Sdes	V_dad_maxtry = 15;	/* max # of *tries* to transmit DAD packet */
169158115Sume
170158115Sume	/*
171158115Sume	 * XXX just to get this to compile KMM
172158115Sume	 */
173158115Sume#ifdef notyet
174194112Sdes	V_llinfo_nd6.ln_next = &V_llinfo_nd6;
175194112Sdes	V_llinfo_nd6.ln_prev = &V_llinfo_nd6;
176194112Sdes#endif
177158115Sume	LIST_INIT(&V_nd_prefix);
178194112Sdes
179194112Sdes	V_ip6_use_tempaddr = 0;
180194112Sdes	V_ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME;
181158115Sume	V_ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME;
182194112Sdes	V_ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE;
183194112Sdes
184194112Sdes	all1_sa.sin6_family = AF_INET6;
185158115Sume	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
186194112Sdes	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
187194112Sdes		all1_sa.sin6_addr.s6_addr[i] = 0xff;
188194112Sdes
189158115Sume	/* initialization of the default router list */
190194112Sdes	TAILQ_INIT(&V_nd_defrouter);
191194112Sdes	/* start timer */
192194112Sdes	callout_init(&V_nd6_slowtimo_ch, 0);
193158115Sume	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
194158115Sume	    nd6_slowtimo, NULL);
195194112Sdes
196194112Sdes	nd6_init_done = 1;
197194112Sdes
198158115Sume}
199158115Sume
200194112Sdesstruct nd_ifinfo *
201158115Sumend6_ifattach(struct ifnet *ifp)
202194112Sdes{
203158115Sume	struct nd_ifinfo *nd;
204194112Sdes
205194112Sdes	nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK);
206158115Sume	bzero(nd, sizeof(*nd));
207194112Sdes
208158115Sume	nd->initialized = 1;
209194112Sdes
210158115Sume	nd->chlim = IPV6_DEFHLIM;
211194112Sdes	nd->basereachable = REACHABLE_TIME;
212158115Sume	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
213158115Sume	nd->retrans = RETRANS_TIMER;
214194112Sdes	/*
215158115Sume	 * Note that the default value of ip6_accept_rtadv is 0, which means
216194112Sdes	 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
217158115Sume	 * here.
218194112Sdes	 */
219158115Sume	nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
220158115Sume
221194112Sdes	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
222158115Sume	nd6_setmtu0(ifp, nd);
223194112Sdes
224158115Sume	return nd;
225194112Sdes}
226158115Sume
227158115Sumevoid
228194112Sdesnd6_ifdetach(struct nd_ifinfo *nd)
229158115Sume{
230194112Sdes
231158115Sume	free(nd, M_IP6NDP);
232194112Sdes}
233158115Sume
234158115Sume/*
235194112Sdes * Reset ND level link MTU. This function is called when the physical MTU
236158115Sume * changes, which means we might have to adjust the ND level MTU.
237194112Sdes */
238158115Sumevoid
239194112Sdesnd6_setmtu(struct ifnet *ifp)
240194112Sdes{
241158115Sume
242194112Sdes	nd6_setmtu0(ifp, ND_IFINFO(ifp));
243158115Sume}
244194112Sdes
245158115Sume/* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */
246194112Sdesvoid
247158115Sumend6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
248158115Sume{
249158115Sume	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((struct vnet *) arg);
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, NULL);
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	INIT_VNET_INET6(curvnet);
876	struct sockaddr_in6 sin6;
877	struct llentry *ln;
878	int llflags = 0;
879
880	bzero(&sin6, sizeof(sin6));
881	sin6.sin6_len = sizeof(struct sockaddr_in6);
882	sin6.sin6_family = AF_INET6;
883	sin6.sin6_addr = *addr6;
884
885	IF_AFDATA_LOCK_ASSERT(ifp);
886
887	if (flags & ND6_CREATE)
888	    llflags |= LLE_CREATE;
889	if (flags & ND6_EXCLUSIVE)
890	    llflags |= LLE_EXCLUSIVE;
891
892	ln = lla_lookup(LLTABLE6(ifp), llflags, (struct sockaddr *)&sin6);
893	if ((ln != NULL) && (flags & LLE_CREATE)) {
894		ln->ln_state = ND6_LLINFO_NOSTATE;
895		callout_init(&ln->ln_timer_ch, 0);
896	}
897
898	return (ln);
899}
900
901/*
902 * Test whether a given IPv6 address is a neighbor or not, ignoring
903 * the actual neighbor cache.  The neighbor cache is ignored in order
904 * to not reenter the routing code from within itself.
905 */
906static int
907nd6_is_new_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
908{
909	INIT_VNET_INET6(ifp->if_vnet);
910	struct nd_prefix *pr;
911	struct ifaddr *dstaddr;
912
913	/*
914	 * A link-local address is always a neighbor.
915	 * XXX: a link does not necessarily specify a single interface.
916	 */
917	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
918		struct sockaddr_in6 sin6_copy;
919		u_int32_t zone;
920
921		/*
922		 * We need sin6_copy since sa6_recoverscope() may modify the
923		 * content (XXX).
924		 */
925		sin6_copy = *addr;
926		if (sa6_recoverscope(&sin6_copy))
927			return (0); /* XXX: should be impossible */
928		if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
929			return (0);
930		if (sin6_copy.sin6_scope_id == zone)
931			return (1);
932		else
933			return (0);
934	}
935
936	/*
937	 * If the address matches one of our addresses,
938	 * it should be a neighbor.
939	 * If the address matches one of our on-link prefixes, it should be a
940	 * neighbor.
941	 */
942	for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
943		if (pr->ndpr_ifp != ifp)
944			continue;
945
946		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
947			continue;
948
949		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
950		    &addr->sin6_addr, &pr->ndpr_mask))
951			return (1);
952	}
953
954	/*
955	 * If the address is assigned on the node of the other side of
956	 * a p2p interface, the address should be a neighbor.
957	 */
958	dstaddr = ifa_ifwithdstaddr((struct sockaddr *)addr);
959	if ((dstaddr != NULL) && (dstaddr->ifa_ifp == ifp))
960		return (1);
961
962	/*
963	 * If the default router list is empty, all addresses are regarded
964	 * as on-link, and thus, as a neighbor.
965	 * XXX: we restrict the condition to hosts, because routers usually do
966	 * not have the "default router list".
967	 */
968	if (!V_ip6_forwarding && TAILQ_FIRST(&V_nd_defrouter) == NULL &&
969	    V_nd6_defifindex == ifp->if_index) {
970		return (1);
971	}
972
973	return (0);
974}
975
976
977/*
978 * Detect if a given IPv6 address identifies a neighbor on a given link.
979 * XXX: should take care of the destination of a p2p link?
980 */
981int
982nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
983{
984	struct llentry *lle;
985	int rc = 0;
986
987	IF_AFDATA_UNLOCK_ASSERT(ifp);
988	if (nd6_is_new_addr_neighbor(addr, ifp))
989		return (1);
990
991	/*
992	 * Even if the address matches none of our addresses, it might be
993	 * in the neighbor cache.
994	 */
995	IF_AFDATA_LOCK(ifp);
996	if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) {
997		LLE_RUNLOCK(lle);
998		rc = 1;
999	}
1000	IF_AFDATA_UNLOCK(ifp);
1001	return (rc);
1002}
1003
1004/*
1005 * Free an nd6 llinfo entry.
1006 * Since the function would cause significant changes in the kernel, DO NOT
1007 * make it global, unless you have a strong reason for the change, and are sure
1008 * that the change is safe.
1009 */
1010static struct llentry *
1011nd6_free(struct llentry *ln, int gc)
1012{
1013	INIT_VNET_INET6(curvnet);
1014        struct llentry *next;
1015	struct nd_defrouter *dr;
1016	struct ifnet *ifp=NULL;
1017
1018	/*
1019	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1020	 * even though it is not harmful, it was not really necessary.
1021	 */
1022
1023	/* cancel timer */
1024	nd6_llinfo_settimer(ln, -1);
1025
1026	if (!V_ip6_forwarding) {
1027		int s;
1028		s = splnet();
1029		dr = defrouter_lookup(&L3_ADDR_SIN6(ln)->sin6_addr, ln->lle_tbl->llt_ifp);
1030
1031		if (dr != NULL && dr->expire &&
1032		    ln->ln_state == ND6_LLINFO_STALE && gc) {
1033			/*
1034			 * If the reason for the deletion is just garbage
1035			 * collection, and the neighbor is an active default
1036			 * router, do not delete it.  Instead, reset the GC
1037			 * timer using the router's lifetime.
1038			 * Simply deleting the entry would affect default
1039			 * router selection, which is not necessarily a good
1040			 * thing, especially when we're using router preference
1041			 * values.
1042			 * XXX: the check for ln_state would be redundant,
1043			 *      but we intentionally keep it just in case.
1044			 */
1045			if (dr->expire > time_second)
1046				nd6_llinfo_settimer(ln,
1047				    (dr->expire - time_second) * hz);
1048			else
1049				nd6_llinfo_settimer(ln, (long)V_nd6_gctimer * hz);
1050			splx(s);
1051			return (LIST_NEXT(ln, lle_next));
1052		}
1053
1054		if (ln->ln_router || dr) {
1055			/*
1056			 * rt6_flush must be called whether or not the neighbor
1057			 * is in the Default Router List.
1058			 * See a corresponding comment in nd6_na_input().
1059			 */
1060			rt6_flush(&L3_ADDR_SIN6(ln)->sin6_addr, ln->lle_tbl->llt_ifp);
1061		}
1062
1063		if (dr) {
1064			/*
1065			 * Unreachablity of a router might affect the default
1066			 * router selection and on-link detection of advertised
1067			 * prefixes.
1068			 */
1069
1070			/*
1071			 * Temporarily fake the state to choose a new default
1072			 * router and to perform on-link determination of
1073			 * prefixes correctly.
1074			 * Below the state will be set correctly,
1075			 * or the entry itself will be deleted.
1076			 */
1077			ln->ln_state = ND6_LLINFO_INCOMPLETE;
1078
1079			/*
1080			 * Since defrouter_select() does not affect the
1081			 * on-link determination and MIP6 needs the check
1082			 * before the default router selection, we perform
1083			 * the check now.
1084			 */
1085			pfxlist_onlink_check();
1086
1087			/*
1088			 * refresh default router list
1089			 */
1090			defrouter_select();
1091		}
1092		splx(s);
1093	}
1094
1095	/*
1096	 * Before deleting the entry, remember the next entry as the
1097	 * return value.  We need this because pfxlist_onlink_check() above
1098	 * might have freed other entries (particularly the old next entry) as
1099	 * a side effect (XXX).
1100	 */
1101	next = LIST_NEXT(ln, lle_next);
1102
1103	ifp = ln->lle_tbl->llt_ifp;
1104	IF_AFDATA_LOCK(ifp);
1105	LLE_WLOCK(ln);
1106	llentry_free(ln);
1107	IF_AFDATA_UNLOCK(ifp);
1108
1109	return (next);
1110}
1111
1112/*
1113 * Upper-layer reachability hint for Neighbor Unreachability Detection.
1114 *
1115 * XXX cost-effective methods?
1116 */
1117void
1118nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
1119{
1120	INIT_VNET_INET6(curvnet);
1121	struct llentry *ln;
1122	struct ifnet *ifp;
1123
1124	if ((dst6 == NULL) || (rt == NULL))
1125		return;
1126
1127	ifp = rt->rt_ifp;
1128	IF_AFDATA_LOCK(ifp);
1129	ln = nd6_lookup(dst6, ND6_EXCLUSIVE, NULL);
1130	IF_AFDATA_UNLOCK(ifp);
1131	if (ln == NULL)
1132		return;
1133
1134	if (ln->ln_state < ND6_LLINFO_REACHABLE)
1135		goto done;
1136
1137	/*
1138	 * if we get upper-layer reachability confirmation many times,
1139	 * it is possible we have false information.
1140	 */
1141	if (!force) {
1142		ln->ln_byhint++;
1143		if (ln->ln_byhint > V_nd6_maxnudhint) {
1144			goto done;
1145		}
1146	}
1147
1148 	ln->ln_state = ND6_LLINFO_REACHABLE;
1149	if (!ND6_LLINFO_PERMANENT(ln)) {
1150		nd6_llinfo_settimer(ln,
1151		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
1152	}
1153done:
1154	LLE_WUNLOCK(ln);
1155}
1156
1157
1158int
1159nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1160{
1161	INIT_VNET_INET6(ifp->if_vnet);
1162	struct in6_drlist *drl = (struct in6_drlist *)data;
1163	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1164	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1165	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1166	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1167	struct nd_defrouter *dr;
1168	struct nd_prefix *pr;
1169	int i = 0, error = 0;
1170	int s;
1171
1172	switch (cmd) {
1173	case SIOCGDRLST_IN6:
1174		/*
1175		 * obsolete API, use sysctl under net.inet6.icmp6
1176		 */
1177		bzero(drl, sizeof(*drl));
1178		s = splnet();
1179		dr = TAILQ_FIRST(&V_nd_defrouter);
1180		while (dr && i < DRLSTSIZ) {
1181			drl->defrouter[i].rtaddr = dr->rtaddr;
1182			in6_clearscope(&drl->defrouter[i].rtaddr);
1183
1184			drl->defrouter[i].flags = dr->flags;
1185			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1186			drl->defrouter[i].expire = dr->expire;
1187			drl->defrouter[i].if_index = dr->ifp->if_index;
1188			i++;
1189			dr = TAILQ_NEXT(dr, dr_entry);
1190		}
1191		splx(s);
1192		break;
1193	case SIOCGPRLST_IN6:
1194		/*
1195		 * obsolete API, use sysctl under net.inet6.icmp6
1196		 *
1197		 * XXX the structure in6_prlist was changed in backward-
1198		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1199		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1200		 */
1201		/*
1202		 * XXX meaning of fields, especialy "raflags", is very
1203		 * differnet between RA prefix list and RR/static prefix list.
1204		 * how about separating ioctls into two?
1205		 */
1206		bzero(oprl, sizeof(*oprl));
1207		s = splnet();
1208		pr = V_nd_prefix.lh_first;
1209		while (pr && i < PRLSTSIZ) {
1210			struct nd_pfxrouter *pfr;
1211			int j;
1212
1213			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1214			oprl->prefix[i].raflags = pr->ndpr_raf;
1215			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1216			oprl->prefix[i].vltime = pr->ndpr_vltime;
1217			oprl->prefix[i].pltime = pr->ndpr_pltime;
1218			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1219			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
1220				oprl->prefix[i].expire = 0;
1221			else {
1222				time_t maxexpire;
1223
1224				/* XXX: we assume time_t is signed. */
1225				maxexpire = (-1) &
1226				    ~((time_t)1 <<
1227				    ((sizeof(maxexpire) * 8) - 1));
1228				if (pr->ndpr_vltime <
1229				    maxexpire - pr->ndpr_lastupdate) {
1230					oprl->prefix[i].expire =
1231					    pr->ndpr_lastupdate +
1232					    pr->ndpr_vltime;
1233				} else
1234					oprl->prefix[i].expire = maxexpire;
1235			}
1236
1237			pfr = pr->ndpr_advrtrs.lh_first;
1238			j = 0;
1239			while (pfr) {
1240				if (j < DRLSTSIZ) {
1241#define RTRADDR oprl->prefix[i].advrtr[j]
1242					RTRADDR = pfr->router->rtaddr;
1243					in6_clearscope(&RTRADDR);
1244#undef RTRADDR
1245				}
1246				j++;
1247				pfr = pfr->pfr_next;
1248			}
1249			oprl->prefix[i].advrtrs = j;
1250			oprl->prefix[i].origin = PR_ORIG_RA;
1251
1252			i++;
1253			pr = pr->ndpr_next;
1254		}
1255		splx(s);
1256
1257		break;
1258	case OSIOCGIFINFO_IN6:
1259#define ND	ndi->ndi
1260		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1261		bzero(&ND, sizeof(ND));
1262		ND.linkmtu = IN6_LINKMTU(ifp);
1263		ND.maxmtu = ND_IFINFO(ifp)->maxmtu;
1264		ND.basereachable = ND_IFINFO(ifp)->basereachable;
1265		ND.reachable = ND_IFINFO(ifp)->reachable;
1266		ND.retrans = ND_IFINFO(ifp)->retrans;
1267		ND.flags = ND_IFINFO(ifp)->flags;
1268		ND.recalctm = ND_IFINFO(ifp)->recalctm;
1269		ND.chlim = ND_IFINFO(ifp)->chlim;
1270		break;
1271	case SIOCGIFINFO_IN6:
1272		ND = *ND_IFINFO(ifp);
1273		break;
1274	case SIOCSIFINFO_IN6:
1275		/*
1276		 * used to change host variables from userland.
1277		 * intented for a use on router to reflect RA configurations.
1278		 */
1279		/* 0 means 'unspecified' */
1280		if (ND.linkmtu != 0) {
1281			if (ND.linkmtu < IPV6_MMTU ||
1282			    ND.linkmtu > IN6_LINKMTU(ifp)) {
1283				error = EINVAL;
1284				break;
1285			}
1286			ND_IFINFO(ifp)->linkmtu = ND.linkmtu;
1287		}
1288
1289		if (ND.basereachable != 0) {
1290			int obasereachable = ND_IFINFO(ifp)->basereachable;
1291
1292			ND_IFINFO(ifp)->basereachable = ND.basereachable;
1293			if (ND.basereachable != obasereachable)
1294				ND_IFINFO(ifp)->reachable =
1295				    ND_COMPUTE_RTIME(ND.basereachable);
1296		}
1297		if (ND.retrans != 0)
1298			ND_IFINFO(ifp)->retrans = ND.retrans;
1299		if (ND.chlim != 0)
1300			ND_IFINFO(ifp)->chlim = ND.chlim;
1301		/* FALLTHROUGH */
1302	case SIOCSIFINFO_FLAGS:
1303		ND_IFINFO(ifp)->flags = ND.flags;
1304		break;
1305#undef ND
1306	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1307		/* sync kernel routing table with the default router list */
1308		defrouter_reset();
1309		defrouter_select();
1310		break;
1311	case SIOCSPFXFLUSH_IN6:
1312	{
1313		/* flush all the prefix advertised by routers */
1314		struct nd_prefix *pr, *next;
1315
1316		s = splnet();
1317		for (pr = V_nd_prefix.lh_first; pr; pr = next) {
1318			struct in6_ifaddr *ia, *ia_next;
1319
1320			next = pr->ndpr_next;
1321
1322			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1323				continue; /* XXX */
1324
1325			/* do we really have to remove addresses as well? */
1326			for (ia = V_in6_ifaddr; ia; ia = ia_next) {
1327				/* ia might be removed.  keep the next ptr. */
1328				ia_next = ia->ia_next;
1329
1330				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1331					continue;
1332
1333				if (ia->ia6_ndpr == pr)
1334					in6_purgeaddr(&ia->ia_ifa);
1335			}
1336			prelist_remove(pr);
1337		}
1338		splx(s);
1339		break;
1340	}
1341	case SIOCSRTRFLUSH_IN6:
1342	{
1343		/* flush all the default routers */
1344		struct nd_defrouter *dr, *next;
1345
1346		s = splnet();
1347		defrouter_reset();
1348		for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; dr = next) {
1349			next = TAILQ_NEXT(dr, dr_entry);
1350			defrtrlist_del(dr);
1351		}
1352		defrouter_select();
1353		splx(s);
1354		break;
1355	}
1356	case SIOCGNBRINFO_IN6:
1357	{
1358		struct llentry *ln;
1359		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1360
1361		if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1362			return (error);
1363
1364		IF_AFDATA_LOCK(ifp);
1365		ln = nd6_lookup(&nb_addr, 0, ifp);
1366		IF_AFDATA_UNLOCK(ifp);
1367
1368		if (ln == NULL) {
1369			error = EINVAL;
1370			break;
1371		}
1372		nbi->state = ln->ln_state;
1373		nbi->asked = ln->la_asked;
1374		nbi->isrouter = ln->ln_router;
1375		nbi->expire = ln->la_expire;
1376		LLE_RUNLOCK(ln);
1377		break;
1378	}
1379	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1380		ndif->ifindex = V_nd6_defifindex;
1381		break;
1382	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1383		return (nd6_setdefaultiface(ndif->ifindex));
1384	}
1385	return (error);
1386}
1387
1388/*
1389 * Create neighbor cache entry and cache link-layer address,
1390 * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1391 *
1392 * type - ICMP6 type
1393 * code - type dependent information
1394 *
1395 * XXXXX
1396 *  The caller of this function already acquired the ndp
1397 *  cache table lock because the cache entry is returned.
1398 */
1399struct llentry *
1400nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1401    int lladdrlen, int type, int code)
1402{
1403	INIT_VNET_INET6(curvnet);
1404	struct llentry *ln = NULL;
1405	int is_newentry;
1406	int do_update;
1407	int olladdr;
1408	int llchange;
1409	int flags = 0;
1410	int newstate = 0;
1411	uint16_t router = 0;
1412	struct sockaddr_in6 sin6;
1413	struct mbuf *chain = NULL;
1414	int static_route = 0;
1415
1416	IF_AFDATA_UNLOCK_ASSERT(ifp);
1417
1418	if (ifp == NULL)
1419		panic("ifp == NULL in nd6_cache_lladdr");
1420	if (from == NULL)
1421		panic("from == NULL in nd6_cache_lladdr");
1422
1423	/* nothing must be updated for unspecified address */
1424	if (IN6_IS_ADDR_UNSPECIFIED(from))
1425		return NULL;
1426
1427	/*
1428	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1429	 * the caller.
1430	 *
1431	 * XXX If the link does not have link-layer adderss, what should
1432	 * we do? (ifp->if_addrlen == 0)
1433	 * Spec says nothing in sections for RA, RS and NA.  There's small
1434	 * description on it in NS section (RFC 2461 7.2.3).
1435	 */
1436	flags |= lladdr ? ND6_EXCLUSIVE : 0;
1437	IF_AFDATA_LOCK(ifp);
1438	ln = nd6_lookup(from, flags, ifp);
1439
1440	if (ln == NULL) {
1441		flags |= LLE_EXCLUSIVE;
1442		ln = nd6_lookup(from, flags |ND6_CREATE, ifp);
1443		IF_AFDATA_UNLOCK(ifp);
1444		is_newentry = 1;
1445	} else {
1446		IF_AFDATA_UNLOCK(ifp);
1447		/* do nothing if static ndp is set */
1448		if (ln->la_flags & LLE_STATIC) {
1449			static_route = 1;
1450			goto done;
1451		}
1452		is_newentry = 0;
1453	}
1454	if (ln == NULL)
1455		return (NULL);
1456
1457	olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1458	if (olladdr && lladdr) {
1459		llchange = bcmp(lladdr, &ln->ll_addr,
1460		    ifp->if_addrlen);
1461	} else
1462		llchange = 0;
1463
1464	/*
1465	 * newentry olladdr  lladdr  llchange	(*=record)
1466	 *	0	n	n	--	(1)
1467	 *	0	y	n	--	(2)
1468	 *	0	n	y	--	(3) * STALE
1469	 *	0	y	y	n	(4) *
1470	 *	0	y	y	y	(5) * STALE
1471	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1472	 *	1	--	y	--	(7) * STALE
1473	 */
1474
1475	if (lladdr) {		/* (3-5) and (7) */
1476		/*
1477		 * Record source link-layer address
1478		 * XXX is it dependent to ifp->if_type?
1479		 */
1480		bcopy(lladdr, &ln->ll_addr, ifp->if_addrlen);
1481		ln->la_flags |= LLE_VALID;
1482	}
1483
1484	if (!is_newentry) {
1485		if ((!olladdr && lladdr != NULL) ||	/* (3) */
1486		    (olladdr && lladdr != NULL && llchange)) {	/* (5) */
1487			do_update = 1;
1488			newstate = ND6_LLINFO_STALE;
1489		} else					/* (1-2,4) */
1490			do_update = 0;
1491	} else {
1492		do_update = 1;
1493		if (lladdr == NULL)			/* (6) */
1494			newstate = ND6_LLINFO_NOSTATE;
1495		else					/* (7) */
1496			newstate = ND6_LLINFO_STALE;
1497	}
1498
1499	if (do_update) {
1500		/*
1501		 * Update the state of the neighbor cache.
1502		 */
1503		ln->ln_state = newstate;
1504
1505		if (ln->ln_state == ND6_LLINFO_STALE) {
1506			/*
1507			 * XXX: since nd6_output() below will cause
1508			 * state tansition to DELAY and reset the timer,
1509			 * we must set the timer now, although it is actually
1510			 * meaningless.
1511			 */
1512			nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1513
1514			if (ln->la_hold) {
1515				struct mbuf *m_hold, *m_hold_next;
1516
1517				/*
1518				 * reset the la_hold in advance, to explicitly
1519				 * prevent a la_hold lookup in nd6_output()
1520				 * (wouldn't happen, though...)
1521				 */
1522				for (m_hold = ln->la_hold, ln->la_hold = NULL;
1523				    m_hold; m_hold = m_hold_next) {
1524					m_hold_next = m_hold->m_nextpkt;
1525					m_hold->m_nextpkt = NULL;
1526
1527					/*
1528					 * we assume ifp is not a p2p here, so
1529					 * just set the 2nd argument as the
1530					 * 1st one.
1531					 */
1532					nd6_output_lle(ifp, ifp, m_hold, L3_ADDR_SIN6(ln), NULL, ln, &chain);
1533				}
1534				/*
1535				 * If we have mbufs in the chain we need to do
1536				 * deferred transmit. Copy the address from the
1537				 * llentry before dropping the lock down below.
1538				 */
1539				if (chain != NULL)
1540					memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6));
1541			}
1542		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1543			/* probe right away */
1544			nd6_llinfo_settimer_locked((void *)ln, 0);
1545		}
1546	}
1547
1548	/*
1549	 * ICMP6 type dependent behavior.
1550	 *
1551	 * NS: clear IsRouter if new entry
1552	 * RS: clear IsRouter
1553	 * RA: set IsRouter if there's lladdr
1554	 * redir: clear IsRouter if new entry
1555	 *
1556	 * RA case, (1):
1557	 * The spec says that we must set IsRouter in the following cases:
1558	 * - If lladdr exist, set IsRouter.  This means (1-5).
1559	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1560	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1561	 * A quetion arises for (1) case.  (1) case has no lladdr in the
1562	 * neighbor cache, this is similar to (6).
1563	 * This case is rare but we figured that we MUST NOT set IsRouter.
1564	 *
1565	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1566	 *							D R
1567	 *	0	n	n	--	(1)	c   ?     s
1568	 *	0	y	n	--	(2)	c   s     s
1569	 *	0	n	y	--	(3)	c   s     s
1570	 *	0	y	y	n	(4)	c   s     s
1571	 *	0	y	y	y	(5)	c   s     s
1572	 *	1	--	n	--	(6) c	c	c s
1573	 *	1	--	y	--	(7) c	c   s	c s
1574	 *
1575	 *					(c=clear s=set)
1576	 */
1577	switch (type & 0xff) {
1578	case ND_NEIGHBOR_SOLICIT:
1579		/*
1580		 * New entry must have is_router flag cleared.
1581		 */
1582		if (is_newentry)	/* (6-7) */
1583			ln->ln_router = 0;
1584		break;
1585	case ND_REDIRECT:
1586		/*
1587		 * If the icmp is a redirect to a better router, always set the
1588		 * is_router flag.  Otherwise, if the entry is newly created,
1589		 * clear the flag.  [RFC 2461, sec 8.3]
1590		 */
1591		if (code == ND_REDIRECT_ROUTER)
1592			ln->ln_router = 1;
1593		else if (is_newentry) /* (6-7) */
1594			ln->ln_router = 0;
1595		break;
1596	case ND_ROUTER_SOLICIT:
1597		/*
1598		 * is_router flag must always be cleared.
1599		 */
1600		ln->ln_router = 0;
1601		break;
1602	case ND_ROUTER_ADVERT:
1603		/*
1604		 * Mark an entry with lladdr as a router.
1605		 */
1606		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1607		    (is_newentry && lladdr)) {			/* (7) */
1608			ln->ln_router = 1;
1609		}
1610		break;
1611	}
1612
1613	if (ln != NULL) {
1614		static_route = (ln->la_flags & LLE_STATIC);
1615		router = ln->ln_router;
1616
1617		if (flags & ND6_EXCLUSIVE)
1618			LLE_WUNLOCK(ln);
1619		else
1620			LLE_RUNLOCK(ln);
1621		if (static_route)
1622			ln = NULL;
1623	}
1624	if (chain)
1625		nd6_output_flush(ifp, ifp, chain, &sin6, NULL);
1626
1627	/*
1628	 * When the link-layer address of a router changes, select the
1629	 * best router again.  In particular, when the neighbor entry is newly
1630	 * created, it might affect the selection policy.
1631	 * Question: can we restrict the first condition to the "is_newentry"
1632	 * case?
1633	 * XXX: when we hear an RA from a new router with the link-layer
1634	 * address option, defrouter_select() is called twice, since
1635	 * defrtrlist_update called the function as well.  However, I believe
1636	 * we can compromise the overhead, since it only happens the first
1637	 * time.
1638	 * XXX: although defrouter_select() should not have a bad effect
1639	 * for those are not autoconfigured hosts, we explicitly avoid such
1640	 * cases for safety.
1641	 */
1642	if (do_update && router && !V_ip6_forwarding && V_ip6_accept_rtadv) {
1643		/*
1644		 * guaranteed recursion
1645		 */
1646		defrouter_select();
1647	}
1648
1649	return (ln);
1650done:
1651	if (ln != NULL) {
1652		if (flags & ND6_EXCLUSIVE)
1653			LLE_WUNLOCK(ln);
1654		else
1655			LLE_RUNLOCK(ln);
1656		if (static_route)
1657			ln = NULL;
1658	}
1659	return (ln);
1660}
1661
1662static void
1663nd6_slowtimo(void *arg)
1664{
1665	CURVNET_SET((struct vnet *) arg);
1666	INIT_VNET_NET((struct vnet *) arg);
1667	INIT_VNET_INET6((struct vnet *) arg);
1668	struct nd_ifinfo *nd6if;
1669	struct ifnet *ifp;
1670
1671	callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1672	    nd6_slowtimo, NULL);
1673	IFNET_RLOCK();
1674	for (ifp = TAILQ_FIRST(&V_ifnet); ifp;
1675	    ifp = TAILQ_NEXT(ifp, if_list)) {
1676		nd6if = ND_IFINFO(ifp);
1677		if (nd6if->basereachable && /* already initialized */
1678		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1679			/*
1680			 * Since reachable time rarely changes by router
1681			 * advertisements, we SHOULD insure that a new random
1682			 * value gets recomputed at least once every few hours.
1683			 * (RFC 2461, 6.3.4)
1684			 */
1685			nd6if->recalctm = V_nd6_recalc_reachtm_interval;
1686			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1687		}
1688	}
1689	IFNET_RUNLOCK();
1690	CURVNET_RESTORE();
1691}
1692
1693int
1694nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1695    struct sockaddr_in6 *dst, struct rtentry *rt0)
1696{
1697
1698	return (nd6_output_lle(ifp, origifp, m0, dst, rt0, NULL, NULL));
1699}
1700
1701
1702/*
1703 * Note that I'm not enforcing any global serialization
1704 * lle state or asked changes here as the logic is too
1705 * complicated to avoid having to always acquire an exclusive
1706 * lock
1707 * KMM
1708 *
1709 */
1710#define senderr(e) { error = (e); goto bad;}
1711
1712int
1713nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1714    struct sockaddr_in6 *dst, struct rtentry *rt0, struct llentry *lle,
1715	struct mbuf **chain)
1716{
1717	INIT_VNET_INET6(curvnet);
1718	struct mbuf *m = m0;
1719	struct llentry *ln = lle;
1720	int error = 0;
1721	int flags = 0;
1722
1723#ifdef INVARIANTS
1724	if (lle != NULL) {
1725
1726		LLE_WLOCK_ASSERT(lle);
1727
1728		KASSERT(chain != NULL, (" lle locked but no mbuf chain pointer passed"));
1729	}
1730#endif
1731	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1732		goto sendpkt;
1733
1734	if (nd6_need_cache(ifp) == 0)
1735		goto sendpkt;
1736
1737	/*
1738	 * next hop determination.  This routine is derived from ether_output.
1739	 */
1740
1741	/*
1742	 * Address resolution or Neighbor Unreachability Detection
1743	 * for the next hop.
1744	 * At this point, the destination of the packet must be a unicast
1745	 * or an anycast address(i.e. not a multicast).
1746	 */
1747
1748	flags = ((m != NULL) || (lle != NULL)) ? LLE_EXCLUSIVE : 0;
1749	if (ln == NULL) {
1750	retry:
1751		IF_AFDATA_LOCK(ifp);
1752		ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)dst);
1753		IF_AFDATA_UNLOCK(ifp);
1754		if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp))  {
1755			/*
1756			 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1757			 * the condition below is not very efficient.  But we believe
1758			 * it is tolerable, because this should be a rare case.
1759			 */
1760			flags = ND6_CREATE | (m ? ND6_EXCLUSIVE : 0);
1761			IF_AFDATA_LOCK(ifp);
1762			ln = nd6_lookup(&dst->sin6_addr, flags, ifp);
1763			IF_AFDATA_UNLOCK(ifp);
1764		}
1765	}
1766	if (ln == NULL) {
1767		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1768		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1769			char ip6buf[INET6_ADDRSTRLEN];
1770			log(LOG_DEBUG,
1771			    "nd6_output: can't allocate llinfo for %s "
1772			    "(ln=%p)\n",
1773			    ip6_sprintf(ip6buf, &dst->sin6_addr), ln);
1774			senderr(EIO);	/* XXX: good error? */
1775		}
1776		goto sendpkt;	/* send anyway */
1777	}
1778
1779	/* We don't have to do link-layer address resolution on a p2p link. */
1780	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1781	    ln->ln_state < ND6_LLINFO_REACHABLE) {
1782		if ((flags & LLE_EXCLUSIVE) == 0) {
1783			flags |= LLE_EXCLUSIVE;
1784			goto retry;
1785		}
1786		ln->ln_state = ND6_LLINFO_STALE;
1787		nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz);
1788	}
1789
1790	/*
1791	 * The first time we send a packet to a neighbor whose entry is
1792	 * STALE, we have to change the state to DELAY and a sets a timer to
1793	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1794	 * neighbor unreachability detection on expiration.
1795	 * (RFC 2461 7.3.3)
1796	 */
1797	if (ln->ln_state == ND6_LLINFO_STALE) {
1798		if ((flags & LLE_EXCLUSIVE) == 0) {
1799			flags |= LLE_EXCLUSIVE;
1800			LLE_RUNLOCK(ln);
1801			goto retry;
1802		}
1803		ln->la_asked = 0;
1804		ln->ln_state = ND6_LLINFO_DELAY;
1805		nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz);
1806	}
1807
1808	/*
1809	 * If the neighbor cache entry has a state other than INCOMPLETE
1810	 * (i.e. its link-layer address is already resolved), just
1811	 * send the packet.
1812	 */
1813	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1814		goto sendpkt;
1815
1816	/*
1817	 * There is a neighbor cache entry, but no ethernet address
1818	 * response yet.  Append this latest packet to the end of the
1819	 * packet queue in the mbuf, unless the number of the packet
1820	 * does not exceed nd6_maxqueuelen.  When it exceeds nd6_maxqueuelen,
1821	 * the oldest packet in the queue will be removed.
1822	 */
1823	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1824		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1825
1826	if ((flags & LLE_EXCLUSIVE) == 0) {
1827		flags |= LLE_EXCLUSIVE;
1828		LLE_RUNLOCK(ln);
1829		goto retry;
1830	}
1831	if (ln->la_hold) {
1832		struct mbuf *m_hold;
1833		int i;
1834
1835		i = 0;
1836		for (m_hold = ln->la_hold; m_hold; m_hold = m_hold->m_nextpkt) {
1837			i++;
1838			if (m_hold->m_nextpkt == NULL) {
1839				m_hold->m_nextpkt = m;
1840				break;
1841			}
1842		}
1843		while (i >= V_nd6_maxqueuelen) {
1844			m_hold = ln->la_hold;
1845			ln->la_hold = ln->la_hold->m_nextpkt;
1846			m_freem(m_hold);
1847			i--;
1848		}
1849	} else {
1850		ln->la_hold = m;
1851	}
1852	/*
1853	 * We did the lookup (no lle arg) so we
1854	 * need to do the unlock here
1855	 */
1856	if (lle == NULL) {
1857		if (flags & LLE_EXCLUSIVE)
1858			LLE_WUNLOCK(ln);
1859		else
1860			LLE_RUNLOCK(ln);
1861	}
1862
1863	/*
1864	 * If there has been no NS for the neighbor after entering the
1865	 * INCOMPLETE state, send the first solicitation.
1866	 */
1867	if (!ND6_LLINFO_PERMANENT(ln) && ln->la_asked == 0) {
1868		ln->la_asked++;
1869
1870		nd6_llinfo_settimer(ln,
1871		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1872		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1873	}
1874	return (0);
1875
1876  sendpkt:
1877	/* discard the packet if IPv6 operation is disabled on the interface */
1878	if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) {
1879		error = ENETDOWN; /* better error? */
1880		goto bad;
1881	}
1882	/*
1883	 * ln is valid and the caller did not pass in
1884	 * an llentry
1885	 */
1886	if ((ln != NULL) && (lle == NULL)) {
1887		if (flags & LLE_EXCLUSIVE)
1888			LLE_WUNLOCK(ln);
1889		else
1890			LLE_RUNLOCK(ln);
1891	}
1892
1893#ifdef MAC
1894	mac_netinet6_nd6_send(ifp, m);
1895#endif
1896	/*
1897	 * We were passed in a pointer to an lle with the lock held
1898	 * this means that we can't call if_output as we will
1899	 * recurse on the lle lock - so what we do is we create
1900	 * a list of mbufs to send and transmit them in the caller
1901	 * after the lock is dropped
1902	 */
1903	if (lle != NULL) {
1904		if (*chain == NULL)
1905			*chain = m;
1906		else {
1907			struct mbuf *m = *chain;
1908
1909			/*
1910			 * append mbuf to end of deferred chain
1911			 */
1912			while (m->m_nextpkt != NULL)
1913				m = m->m_nextpkt;
1914			m->m_nextpkt = m;
1915		}
1916		return (error);
1917	}
1918	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1919		return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
1920		    NULL));
1921	}
1922	error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, NULL);
1923	return (error);
1924
1925  bad:
1926	/*
1927	 * ln is valid and the caller did not pass in
1928	 * an llentry
1929	 */
1930	if ((ln != NULL) && (lle == NULL)) {
1931		if (flags & LLE_EXCLUSIVE)
1932			LLE_WUNLOCK(ln);
1933		else
1934			LLE_RUNLOCK(ln);
1935	}
1936	if (m)
1937		m_freem(m);
1938	return (error);
1939}
1940#undef senderr
1941
1942
1943int
1944nd6_output_flush(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain,
1945    struct sockaddr_in6 *dst, struct route *ro)
1946{
1947	struct mbuf *m, *m_head;
1948	struct ifnet *outifp;
1949	int error = 0;
1950
1951	m_head = chain;
1952	if ((ifp->if_flags & IFF_LOOPBACK) != 0)
1953		outifp = origifp;
1954	else
1955		outifp = ifp;
1956
1957	while (m_head) {
1958		m = m_head;
1959		m_head = m_head->m_nextpkt;
1960		error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, ro);
1961	}
1962
1963	/*
1964	 * XXX
1965	 * note that intermediate errors are blindly ignored - but this is
1966	 * the same convention as used with nd6_output when called by
1967	 * nd6_cache_lladdr
1968	 */
1969	return (error);
1970}
1971
1972
1973int
1974nd6_need_cache(struct ifnet *ifp)
1975{
1976	/*
1977	 * XXX: we currently do not make neighbor cache on any interface
1978	 * other than ARCnet, Ethernet, FDDI and GIF.
1979	 *
1980	 * RFC2893 says:
1981	 * - unidirectional tunnels needs no ND
1982	 */
1983	switch (ifp->if_type) {
1984	case IFT_ARCNET:
1985	case IFT_ETHER:
1986	case IFT_FDDI:
1987	case IFT_IEEE1394:
1988#ifdef IFT_L2VLAN
1989	case IFT_L2VLAN:
1990#endif
1991#ifdef IFT_IEEE80211
1992	case IFT_IEEE80211:
1993#endif
1994#ifdef IFT_CARP
1995	case IFT_CARP:
1996#endif
1997	case IFT_GIF:		/* XXX need more cases? */
1998	case IFT_PPP:
1999	case IFT_TUNNEL:
2000	case IFT_BRIDGE:
2001	case IFT_PROPVIRTUAL:
2002		return (1);
2003	default:
2004		return (0);
2005	}
2006}
2007
2008/*
2009 * the callers of this function need to be re-worked to drop
2010 * the lle lock, drop here for now
2011 */
2012int
2013nd6_storelladdr(struct ifnet *ifp, struct mbuf *m,
2014    struct sockaddr *dst, u_char *desten, struct llentry **lle)
2015{
2016	struct llentry *ln;
2017
2018	*lle = NULL;
2019	IF_AFDATA_UNLOCK_ASSERT(ifp);
2020	if (m->m_flags & M_MCAST) {
2021		int i;
2022
2023		switch (ifp->if_type) {
2024		case IFT_ETHER:
2025		case IFT_FDDI:
2026#ifdef IFT_L2VLAN
2027		case IFT_L2VLAN:
2028#endif
2029#ifdef IFT_IEEE80211
2030		case IFT_IEEE80211:
2031#endif
2032		case IFT_BRIDGE:
2033		case IFT_ISO88025:
2034			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
2035						 desten);
2036			return (0);
2037		case IFT_IEEE1394:
2038			/*
2039			 * netbsd can use if_broadcastaddr, but we don't do so
2040			 * to reduce # of ifdef.
2041			 */
2042			for (i = 0; i < ifp->if_addrlen; i++)
2043				desten[i] = ~0;
2044			return (0);
2045		case IFT_ARCNET:
2046			*desten = 0;
2047			return (0);
2048		default:
2049			m_freem(m);
2050			return (EAFNOSUPPORT);
2051		}
2052	}
2053
2054
2055	/*
2056	 * the entry should have been created in nd6_store_lladdr
2057	 */
2058	IF_AFDATA_LOCK(ifp);
2059	ln = lla_lookup(LLTABLE6(ifp), 0, dst);
2060	IF_AFDATA_UNLOCK(ifp);
2061	if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) {
2062		if (ln != NULL)
2063			LLE_RUNLOCK(ln);
2064		/* this could happen, if we could not allocate memory */
2065		m_freem(m);
2066		return (1);
2067	}
2068
2069	bcopy(&ln->ll_addr, desten, ifp->if_addrlen);
2070	*lle = ln;
2071	LLE_RUNLOCK(ln);
2072	/*
2073	 * A *small* use after free race exists here
2074	 */
2075	return (0);
2076}
2077
2078static void
2079clear_llinfo_pqueue(struct llentry *ln)
2080{
2081	struct mbuf *m_hold, *m_hold_next;
2082
2083	for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) {
2084		m_hold_next = m_hold->m_nextpkt;
2085		m_hold->m_nextpkt = NULL;
2086		m_freem(m_hold);
2087	}
2088
2089	ln->la_hold = NULL;
2090	return;
2091}
2092
2093static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS);
2094static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS);
2095#ifdef SYSCTL_DECL
2096SYSCTL_DECL(_net_inet6_icmp6);
2097#endif
2098SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist,
2099	CTLFLAG_RD, nd6_sysctl_drlist, "");
2100SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2101	CTLFLAG_RD, nd6_sysctl_prlist, "");
2102SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN,
2103	nd6_maxqueuelen, CTLFLAG_RW, nd6_maxqueuelen, 1, "");
2104
2105static int
2106nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS)
2107{
2108	INIT_VNET_INET6(curvnet);
2109	int error;
2110	char buf[1024] __aligned(4);
2111	struct in6_defrouter *d, *de;
2112	struct nd_defrouter *dr;
2113
2114	if (req->newptr)
2115		return EPERM;
2116	error = 0;
2117
2118	for (dr = TAILQ_FIRST(&V_nd_defrouter); dr;
2119	     dr = TAILQ_NEXT(dr, dr_entry)) {
2120		d = (struct in6_defrouter *)buf;
2121		de = (struct in6_defrouter *)(buf + sizeof(buf));
2122
2123		if (d + 1 <= de) {
2124			bzero(d, sizeof(*d));
2125			d->rtaddr.sin6_family = AF_INET6;
2126			d->rtaddr.sin6_len = sizeof(d->rtaddr);
2127			d->rtaddr.sin6_addr = dr->rtaddr;
2128			error = sa6_recoverscope(&d->rtaddr);
2129			if (error != 0)
2130				return (error);
2131			d->flags = dr->flags;
2132			d->rtlifetime = dr->rtlifetime;
2133			d->expire = dr->expire;
2134			d->if_index = dr->ifp->if_index;
2135		} else
2136			panic("buffer too short");
2137
2138		error = SYSCTL_OUT(req, buf, sizeof(*d));
2139		if (error)
2140			break;
2141	}
2142
2143	return (error);
2144}
2145
2146static int
2147nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2148{
2149	INIT_VNET_INET6(curvnet);
2150	int error;
2151	char buf[1024] __aligned(4);
2152	struct in6_prefix *p, *pe;
2153	struct nd_prefix *pr;
2154	char ip6buf[INET6_ADDRSTRLEN];
2155
2156	if (req->newptr)
2157		return EPERM;
2158	error = 0;
2159
2160	for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) {
2161		u_short advrtrs;
2162		size_t advance;
2163		struct sockaddr_in6 *sin6, *s6;
2164		struct nd_pfxrouter *pfr;
2165
2166		p = (struct in6_prefix *)buf;
2167		pe = (struct in6_prefix *)(buf + sizeof(buf));
2168
2169		if (p + 1 <= pe) {
2170			bzero(p, sizeof(*p));
2171			sin6 = (struct sockaddr_in6 *)(p + 1);
2172
2173			p->prefix = pr->ndpr_prefix;
2174			if (sa6_recoverscope(&p->prefix)) {
2175				log(LOG_ERR,
2176				    "scope error in prefix list (%s)\n",
2177				    ip6_sprintf(ip6buf, &p->prefix.sin6_addr));
2178				/* XXX: press on... */
2179			}
2180			p->raflags = pr->ndpr_raf;
2181			p->prefixlen = pr->ndpr_plen;
2182			p->vltime = pr->ndpr_vltime;
2183			p->pltime = pr->ndpr_pltime;
2184			p->if_index = pr->ndpr_ifp->if_index;
2185			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2186				p->expire = 0;
2187			else {
2188				time_t maxexpire;
2189
2190				/* XXX: we assume time_t is signed. */
2191				maxexpire = (-1) &
2192				    ~((time_t)1 <<
2193				    ((sizeof(maxexpire) * 8) - 1));
2194				if (pr->ndpr_vltime <
2195				    maxexpire - pr->ndpr_lastupdate) {
2196				    p->expire = pr->ndpr_lastupdate +
2197				        pr->ndpr_vltime;
2198				} else
2199					p->expire = maxexpire;
2200			}
2201			p->refcnt = pr->ndpr_refcnt;
2202			p->flags = pr->ndpr_stateflags;
2203			p->origin = PR_ORIG_RA;
2204			advrtrs = 0;
2205			for (pfr = pr->ndpr_advrtrs.lh_first; pfr;
2206			     pfr = pfr->pfr_next) {
2207				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2208					advrtrs++;
2209					continue;
2210				}
2211				s6 = &sin6[advrtrs];
2212				bzero(s6, sizeof(*s6));
2213				s6->sin6_family = AF_INET6;
2214				s6->sin6_len = sizeof(*sin6);
2215				s6->sin6_addr = pfr->router->rtaddr;
2216				if (sa6_recoverscope(s6)) {
2217					log(LOG_ERR,
2218					    "scope error in "
2219					    "prefix list (%s)\n",
2220					    ip6_sprintf(ip6buf,
2221						    &pfr->router->rtaddr));
2222				}
2223				advrtrs++;
2224			}
2225			p->advrtrs = advrtrs;
2226		} else
2227			panic("buffer too short");
2228
2229		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2230		error = SYSCTL_OUT(req, buf, advance);
2231		if (error)
2232			break;
2233	}
2234
2235	return (error);
2236}
2237