xref: /openbsd/sys/netinet6/nd6.c (revision ee37ea65)
1*ee37ea65Smcbride /*	$OpenBSD: nd6.c,v 1.79 2008/06/11 19:00:50 mcbride Exp $	*/
2d8a7e3a7Sitojun /*	$KAME: nd6.c,v 1.280 2002/06/08 19:52:07 itojun Exp $	*/
3287546eaSitojun 
4287546eaSitojun /*
5287546eaSitojun  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6287546eaSitojun  * All rights reserved.
7287546eaSitojun  *
8287546eaSitojun  * Redistribution and use in source and binary forms, with or without
9287546eaSitojun  * modification, are permitted provided that the following conditions
10287546eaSitojun  * are met:
11287546eaSitojun  * 1. Redistributions of source code must retain the above copyright
12287546eaSitojun  *    notice, this list of conditions and the following disclaimer.
13287546eaSitojun  * 2. Redistributions in binary form must reproduce the above copyright
14287546eaSitojun  *    notice, this list of conditions and the following disclaimer in the
15287546eaSitojun  *    documentation and/or other materials provided with the distribution.
16287546eaSitojun  * 3. Neither the name of the project nor the names of its contributors
17287546eaSitojun  *    may be used to endorse or promote products derived from this software
18287546eaSitojun  *    without specific prior written permission.
19287546eaSitojun  *
20287546eaSitojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21287546eaSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22287546eaSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23287546eaSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24287546eaSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25287546eaSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26287546eaSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27287546eaSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28287546eaSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29287546eaSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30287546eaSitojun  * SUCH DAMAGE.
31287546eaSitojun  */
32287546eaSitojun 
33287546eaSitojun #include <sys/param.h>
34287546eaSitojun #include <sys/systm.h>
35b3c1e4c1Sitojun #include <sys/timeout.h>
36287546eaSitojun #include <sys/malloc.h>
37287546eaSitojun #include <sys/mbuf.h>
38287546eaSitojun #include <sys/socket.h>
39287546eaSitojun #include <sys/sockio.h>
40287546eaSitojun #include <sys/time.h>
41287546eaSitojun #include <sys/kernel.h>
42f4f4d166Sitojun #include <sys/protosw.h>
43287546eaSitojun #include <sys/errno.h>
44287546eaSitojun #include <sys/ioctl.h>
45287546eaSitojun #include <sys/syslog.h>
46287546eaSitojun #include <sys/queue.h>
473c4c637fSangelos #include <dev/rndvar.h>
48287546eaSitojun 
49287546eaSitojun #include <net/if.h>
50287546eaSitojun #include <net/if_dl.h>
51287546eaSitojun #include <net/if_types.h>
52d6b9e9b9Sitojun #include <net/if_fddi.h>
53287546eaSitojun #include <net/route.h>
54287546eaSitojun 
55287546eaSitojun #include <netinet/in.h>
56287546eaSitojun #include <netinet/if_ether.h>
57cb39d30aSangelos #include <netinet/ip_ipsp.h>
5858aa7d74Sangelos 
59287546eaSitojun #include <netinet6/in6_var.h>
60fa86ee14Sitojun #include <netinet/ip6.h>
61287546eaSitojun #include <netinet6/ip6_var.h>
62287546eaSitojun #include <netinet6/nd6.h>
63fa86ee14Sitojun #include <netinet/icmp6.h>
64287546eaSitojun 
65287546eaSitojun #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
66287546eaSitojun #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
67287546eaSitojun 
68287546eaSitojun #define SIN6(s) ((struct sockaddr_in6 *)s)
69287546eaSitojun #define SDL(s) ((struct sockaddr_dl *)s)
70287546eaSitojun 
71287546eaSitojun /* timer values */
72287546eaSitojun int	nd6_prune	= 1;	/* walk list every 1 seconds */
73287546eaSitojun int	nd6_delay	= 5;	/* delay first probe time 5 second */
74287546eaSitojun int	nd6_umaxtries	= 3;	/* maximum unicast query */
75287546eaSitojun int	nd6_mmaxtries	= 3;	/* maximum multicast query */
76287546eaSitojun int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
77be4e9e12Sitojun int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
78287546eaSitojun 
79287546eaSitojun /* preventing too many loops in ND option parsing */
80287546eaSitojun int nd6_maxndopt = 10;	/* max # of ND options allowed */
81287546eaSitojun 
82f6e55599Sitojun int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
83f6e55599Sitojun 
84b79da24aSitojun #ifdef ND6_DEBUG
85b79da24aSitojun int nd6_debug = 1;
86b79da24aSitojun #else
87b79da24aSitojun int nd6_debug = 0;
88b79da24aSitojun #endif
89b79da24aSitojun 
90287546eaSitojun static int nd6_inuse, nd6_allocated;
91287546eaSitojun 
92287546eaSitojun struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
93f4f4d166Sitojun struct nd_drhead nd_defrouter;
94287546eaSitojun struct nd_prhead nd_prefix = { 0 };
95287546eaSitojun 
96287546eaSitojun int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
97287546eaSitojun static struct sockaddr_in6 all1_sa;
98287546eaSitojun 
991b5f410aSitojun static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
100c4071fd1Smillert static void nd6_slowtimo(void *);
101d8a7e3a7Sitojun static struct llinfo_nd6 *nd6_free(struct rtentry *, int);
1029631a17bSitojun static void nd6_llinfo_timer(void *);
103287546eaSitojun 
104b3c1e4c1Sitojun struct timeout nd6_slowtimo_ch;
105b3c1e4c1Sitojun struct timeout nd6_timer_ch;
106b3c1e4c1Sitojun extern struct timeout in6_tmpaddrtimer_ch;
107b3c1e4c1Sitojun 
108d8a7e3a7Sitojun static int fill_drlist(void *, size_t *, size_t);
109d8a7e3a7Sitojun static int fill_prlist(void *, size_t *, size_t);
110d8a7e3a7Sitojun 
111f3fcf2f3Smcbride #define LN_DEQUEUE(ln) do { \
112f3fcf2f3Smcbride 	(ln)->ln_next->ln_prev = (ln)->ln_prev; \
113f3fcf2f3Smcbride 	(ln)->ln_prev->ln_next = (ln)->ln_next; \
114f3fcf2f3Smcbride 	} while (0)
115f3fcf2f3Smcbride #define LN_INSERTHEAD(ln) do { \
116f3fcf2f3Smcbride 	(ln)->ln_next = llinfo_nd6.ln_next; \
117f3fcf2f3Smcbride 	llinfo_nd6.ln_next = (ln); \
118f3fcf2f3Smcbride 	(ln)->ln_prev = &llinfo_nd6; \
119f3fcf2f3Smcbride 	(ln)->ln_next->ln_prev = (ln); \
120f3fcf2f3Smcbride 	} while (0)
121f3fcf2f3Smcbride 
122287546eaSitojun void
123287546eaSitojun nd6_init()
124287546eaSitojun {
125287546eaSitojun 	static int nd6_init_done = 0;
126287546eaSitojun 	int i;
127287546eaSitojun 
128287546eaSitojun 	if (nd6_init_done) {
129287546eaSitojun 		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
130287546eaSitojun 		return;
131287546eaSitojun 	}
132287546eaSitojun 
133287546eaSitojun 	all1_sa.sin6_family = AF_INET6;
134287546eaSitojun 	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
135287546eaSitojun 	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
136287546eaSitojun 		all1_sa.sin6_addr.s6_addr[i] = 0xff;
137287546eaSitojun 
138f4f4d166Sitojun 	/* initialization of the default router list */
139f4f4d166Sitojun 	TAILQ_INIT(&nd_defrouter);
140f4f4d166Sitojun 
141287546eaSitojun 	nd6_init_done = 1;
142287546eaSitojun 
143287546eaSitojun 	/* start timer */
144b3c1e4c1Sitojun 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
145b3c1e4c1Sitojun 	timeout_add(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz);
146287546eaSitojun }
147287546eaSitojun 
148d6b9e9b9Sitojun struct nd_ifinfo *
149*ee37ea65Smcbride nd6_ifattach(struct ifnet *ifp)
150287546eaSitojun {
151d6b9e9b9Sitojun 	struct nd_ifinfo *nd;
152287546eaSitojun 
153393af863Skrw 	nd = malloc(sizeof(*nd), M_IP6NDP, M_WAITOK | M_ZERO);
154287546eaSitojun 
155d6b9e9b9Sitojun 	nd->initialized = 1;
156287546eaSitojun 
157d6b9e9b9Sitojun 	nd->chlim = IPV6_DEFHLIM;
158d6b9e9b9Sitojun 	nd->basereachable = REACHABLE_TIME;
159d6b9e9b9Sitojun 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
160d6b9e9b9Sitojun 	nd->retrans = RETRANS_TIMER;
161d8a7e3a7Sitojun 	/*
162d8a7e3a7Sitojun 	 * Note that the default value of ip6_accept_rtadv is 0, which means
163d8a7e3a7Sitojun 	 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
164d8a7e3a7Sitojun 	 * here.
165d8a7e3a7Sitojun 	 */
166d8a7e3a7Sitojun 	nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
1671b5f410aSitojun 
1681b5f410aSitojun 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
1691b5f410aSitojun 	nd6_setmtu0(ifp, nd);
170d6b9e9b9Sitojun 
171d6b9e9b9Sitojun 	return nd;
172287546eaSitojun }
173287546eaSitojun 
174d6b9e9b9Sitojun void
175*ee37ea65Smcbride nd6_ifdetach(struct nd_ifinfo *nd)
176d6b9e9b9Sitojun {
177d374aaacSitojun 
178d6b9e9b9Sitojun 	free(nd, M_IP6NDP);
179287546eaSitojun }
180287546eaSitojun 
181287546eaSitojun void
182*ee37ea65Smcbride nd6_setmtu(struct ifnet *ifp)
1831b5f410aSitojun {
1841b5f410aSitojun 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
1851b5f410aSitojun }
1861b5f410aSitojun 
1871b5f410aSitojun void
188*ee37ea65Smcbride nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
189287546eaSitojun {
1901b5f410aSitojun 	u_int32_t omaxmtu;
1911b5f410aSitojun 
1921b5f410aSitojun 	omaxmtu = ndi->maxmtu;
193287546eaSitojun 
19440765843Shenning 	if (ifp->if_type == IFT_FDDI)
195d6b9e9b9Sitojun 		ndi->maxmtu = MIN(FDDIMTU, ifp->if_mtu);
19640765843Shenning 	else
197287546eaSitojun 		ndi->maxmtu = ifp->if_mtu;
198287546eaSitojun 
1991b5f410aSitojun 	/*
2001b5f410aSitojun 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
2011b5f410aSitojun 	 * undesirable situation.  We thus notify the operator of the change
2021b5f410aSitojun 	 * explicitly.  The check for omaxmtu is necessary to restrict the
2031b5f410aSitojun 	 * log to the case of changing the MTU, not initializing it.
2041b5f410aSitojun 	 */
2051b5f410aSitojun 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
2061b5f410aSitojun 		log(LOG_NOTICE, "nd6_setmtu0: "
2071b5f410aSitojun 		    "new link MTU on %s (%lu) is too small for IPv6\n",
2081b5f410aSitojun 		    ifp->if_xname, (unsigned long)ndi->maxmtu);
209287546eaSitojun 	}
210d6b9e9b9Sitojun 
211d6b9e9b9Sitojun 	if (ndi->maxmtu > in6_maxmtu)
212d6b9e9b9Sitojun 		in6_setmaxmtu(); /* check all interfaces just in case */
213287546eaSitojun }
214287546eaSitojun 
215287546eaSitojun void
216*ee37ea65Smcbride nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
217287546eaSitojun {
218287546eaSitojun 	bzero(ndopts, sizeof(*ndopts));
219287546eaSitojun 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
220287546eaSitojun 	ndopts->nd_opts_last
221287546eaSitojun 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
222287546eaSitojun 
223287546eaSitojun 	if (icmp6len == 0) {
224287546eaSitojun 		ndopts->nd_opts_done = 1;
225287546eaSitojun 		ndopts->nd_opts_search = NULL;
226287546eaSitojun 	}
227287546eaSitojun }
228287546eaSitojun 
229287546eaSitojun /*
230287546eaSitojun  * Take one ND option.
231287546eaSitojun  */
232287546eaSitojun struct nd_opt_hdr *
233*ee37ea65Smcbride nd6_option(union nd_opts *ndopts)
234287546eaSitojun {
235287546eaSitojun 	struct nd_opt_hdr *nd_opt;
236287546eaSitojun 	int olen;
237287546eaSitojun 
238287546eaSitojun 	if (!ndopts)
239bc84bce2Skrw 		panic("ndopts == NULL in nd6_option");
240287546eaSitojun 	if (!ndopts->nd_opts_last)
241bc84bce2Skrw 		panic("uninitialized ndopts in nd6_option");
242287546eaSitojun 	if (!ndopts->nd_opts_search)
243287546eaSitojun 		return NULL;
244287546eaSitojun 	if (ndopts->nd_opts_done)
245287546eaSitojun 		return NULL;
246287546eaSitojun 
247287546eaSitojun 	nd_opt = ndopts->nd_opts_search;
248287546eaSitojun 
24915bd77d2Sitojun 	/* make sure nd_opt_len is inside the buffer */
25015bd77d2Sitojun 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
25115bd77d2Sitojun 		bzero(ndopts, sizeof(*ndopts));
25215bd77d2Sitojun 		return NULL;
25315bd77d2Sitojun 	}
25415bd77d2Sitojun 
255287546eaSitojun 	olen = nd_opt->nd_opt_len << 3;
256287546eaSitojun 	if (olen == 0) {
257287546eaSitojun 		/*
258287546eaSitojun 		 * Message validation requires that all included
259287546eaSitojun 		 * options have a length that is greater than zero.
260287546eaSitojun 		 */
261287546eaSitojun 		bzero(ndopts, sizeof(*ndopts));
262287546eaSitojun 		return NULL;
263287546eaSitojun 	}
264287546eaSitojun 
265287546eaSitojun 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
26615bd77d2Sitojun 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
26715bd77d2Sitojun 		/* option overruns the end of buffer, invalid */
26815bd77d2Sitojun 		bzero(ndopts, sizeof(*ndopts));
26915bd77d2Sitojun 		return NULL;
27015bd77d2Sitojun 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
27115bd77d2Sitojun 		/* reached the end of options chain */
272287546eaSitojun 		ndopts->nd_opts_done = 1;
273287546eaSitojun 		ndopts->nd_opts_search = NULL;
274287546eaSitojun 	}
275287546eaSitojun 	return nd_opt;
276287546eaSitojun }
277287546eaSitojun 
278287546eaSitojun /*
279287546eaSitojun  * Parse multiple ND options.
280287546eaSitojun  * This function is much easier to use, for ND routines that do not need
281287546eaSitojun  * multiple options of the same type.
282287546eaSitojun  */
283287546eaSitojun int
284*ee37ea65Smcbride nd6_options(union nd_opts *ndopts)
285287546eaSitojun {
286287546eaSitojun 	struct nd_opt_hdr *nd_opt;
287287546eaSitojun 	int i = 0;
288287546eaSitojun 
289287546eaSitojun 	if (!ndopts)
290bc84bce2Skrw 		panic("ndopts == NULL in nd6_options");
291287546eaSitojun 	if (!ndopts->nd_opts_last)
292bc84bce2Skrw 		panic("uninitialized ndopts in nd6_options");
293287546eaSitojun 	if (!ndopts->nd_opts_search)
294287546eaSitojun 		return 0;
295287546eaSitojun 
296287546eaSitojun 	while (1) {
297287546eaSitojun 		nd_opt = nd6_option(ndopts);
298287546eaSitojun 		if (!nd_opt && !ndopts->nd_opts_last) {
299287546eaSitojun 			/*
300287546eaSitojun 			 * Message validation requires that all included
301287546eaSitojun 			 * options have a length that is greater than zero.
302287546eaSitojun 			 */
303b79da24aSitojun 			icmp6stat.icp6s_nd_badopt++;
304287546eaSitojun 			bzero(ndopts, sizeof(*ndopts));
305287546eaSitojun 			return -1;
306287546eaSitojun 		}
307287546eaSitojun 
308287546eaSitojun 		if (!nd_opt)
309287546eaSitojun 			goto skip1;
310287546eaSitojun 
311287546eaSitojun 		switch (nd_opt->nd_opt_type) {
312287546eaSitojun 		case ND_OPT_SOURCE_LINKADDR:
313287546eaSitojun 		case ND_OPT_TARGET_LINKADDR:
314287546eaSitojun 		case ND_OPT_MTU:
315287546eaSitojun 		case ND_OPT_REDIRECTED_HEADER:
316287546eaSitojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
31715bd77d2Sitojun 				nd6log((LOG_INFO,
31815bd77d2Sitojun 				    "duplicated ND6 option found (type=%d)\n",
31915bd77d2Sitojun 				    nd_opt->nd_opt_type));
320287546eaSitojun 				/* XXX bark? */
321287546eaSitojun 			} else {
322287546eaSitojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
323287546eaSitojun 					= nd_opt;
324287546eaSitojun 			}
325287546eaSitojun 			break;
326287546eaSitojun 		case ND_OPT_PREFIX_INFORMATION:
327287546eaSitojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
328287546eaSitojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
329287546eaSitojun 					= nd_opt;
330287546eaSitojun 			}
331287546eaSitojun 			ndopts->nd_opts_pi_end =
332287546eaSitojun 				(struct nd_opt_prefix_info *)nd_opt;
333287546eaSitojun 			break;
334287546eaSitojun 		default:
335287546eaSitojun 			/*
336287546eaSitojun 			 * Unknown options must be silently ignored,
337e4d25771Stodd 			 * to accommodate future extension to the protocol.
338287546eaSitojun 			 */
339b79da24aSitojun 			nd6log((LOG_DEBUG,
340287546eaSitojun 			    "nd6_options: unsupported option %d - "
341b79da24aSitojun 			    "option ignored\n", nd_opt->nd_opt_type));
342287546eaSitojun 		}
343287546eaSitojun 
344287546eaSitojun skip1:
345287546eaSitojun 		i++;
346287546eaSitojun 		if (i > nd6_maxndopt) {
347287546eaSitojun 			icmp6stat.icp6s_nd_toomanyopt++;
348b79da24aSitojun 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
349287546eaSitojun 			break;
350287546eaSitojun 		}
351287546eaSitojun 
352287546eaSitojun 		if (ndopts->nd_opts_done)
353287546eaSitojun 			break;
354287546eaSitojun 	}
355287546eaSitojun 
356287546eaSitojun 	return 0;
357287546eaSitojun }
358287546eaSitojun 
359287546eaSitojun /*
3609631a17bSitojun  * ND6 timer routine to handle ND6 entries
361287546eaSitojun  */
362287546eaSitojun void
3639631a17bSitojun nd6_llinfo_settimer(struct llinfo_nd6 *ln, long tick)
3649631a17bSitojun {
3659631a17bSitojun 	int s;
3669631a17bSitojun 
3679631a17bSitojun 	s = splsoftnet();
3689631a17bSitojun 
3699631a17bSitojun 	if (tick < 0) {
3709631a17bSitojun 		ln->ln_expire = 0;
3719631a17bSitojun 		ln->ln_ntick = 0;
3729631a17bSitojun 		timeout_del(&ln->ln_timer_ch);
3739631a17bSitojun 	} else {
3743212dc31Stholo 		ln->ln_expire = time_second + tick / hz;
3759631a17bSitojun 		if (tick > INT_MAX) {
3769631a17bSitojun 			ln->ln_ntick = tick - INT_MAX;
3779631a17bSitojun 			timeout_add(&ln->ln_timer_ch, INT_MAX);
3789631a17bSitojun 		} else {
3799631a17bSitojun 			ln->ln_ntick = 0;
3809631a17bSitojun 			timeout_add(&ln->ln_timer_ch, tick);
3819631a17bSitojun 		}
3829631a17bSitojun 	}
3839631a17bSitojun 
3849631a17bSitojun 	splx(s);
3859631a17bSitojun }
3869631a17bSitojun 
3879631a17bSitojun static void
3889631a17bSitojun nd6_llinfo_timer(void *arg)
389287546eaSitojun {
390287546eaSitojun 	int s;
391b3c1e4c1Sitojun 	struct llinfo_nd6 *ln;
392287546eaSitojun 	struct rtentry *rt;
393287546eaSitojun 	struct sockaddr_in6 *dst;
3949631a17bSitojun 	struct ifnet *ifp;
395d374aaacSitojun 	struct nd_ifinfo *ndi = NULL;
396287546eaSitojun 
3979631a17bSitojun 	s = splsoftnet();
3989631a17bSitojun 
3999631a17bSitojun 	ln = (struct llinfo_nd6 *)arg;
4009631a17bSitojun 
4019631a17bSitojun 	if (ln->ln_ntick > 0) {
4029631a17bSitojun 		if (ln->ln_ntick > INT_MAX) {
4039631a17bSitojun 			ln->ln_ntick -= INT_MAX;
4049631a17bSitojun 			nd6_llinfo_settimer(ln, INT_MAX);
4059631a17bSitojun 		} else {
4069631a17bSitojun 			ln->ln_ntick = 0;
4079631a17bSitojun 			nd6_llinfo_settimer(ln, ln->ln_ntick);
408287546eaSitojun 		}
4099631a17bSitojun 		splx(s);
4109631a17bSitojun 		return;
411287546eaSitojun 	}
4129631a17bSitojun 
4139631a17bSitojun 	if ((rt = ln->ln_rt) == NULL)
4149631a17bSitojun 		panic("ln->ln_rt == NULL");
4159631a17bSitojun 	if ((ifp = rt->rt_ifp) == NULL)
4169631a17bSitojun 		panic("ln->ln_rt->rt_ifp == NULL");
417d6b9e9b9Sitojun 	ndi = ND_IFINFO(ifp);
418287546eaSitojun 	dst = (struct sockaddr_in6 *)rt_key(rt);
419287546eaSitojun 
420287546eaSitojun 	/* sanity check */
421d374aaacSitojun 	if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
422bc84bce2Skrw 		panic("rt_llinfo(%p) is not equal to ln(%p)",
423d374aaacSitojun 		      rt->rt_llinfo, ln);
424287546eaSitojun 	if (!dst)
425bc84bce2Skrw 		panic("dst=0 in nd6_timer(ln=%p)", ln);
426287546eaSitojun 
427287546eaSitojun 	switch (ln->ln_state) {
428287546eaSitojun 	case ND6_LLINFO_INCOMPLETE:
429287546eaSitojun 		if (ln->ln_asked < nd6_mmaxtries) {
430287546eaSitojun 			ln->ln_asked++;
4319631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
4329631a17bSitojun 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
433287546eaSitojun 		} else {
434287546eaSitojun 			struct mbuf *m = ln->ln_hold;
435287546eaSitojun 			if (m) {
4368294a4dfSitojun 				ln->ln_hold = NULL;
437287546eaSitojun 				/*
438d8a7e3a7Sitojun 				 * Fake rcvif to make the ICMP error
439d8a7e3a7Sitojun 				 * more helpful in diagnosing for the
440d8a7e3a7Sitojun 				 * receiver.
441287546eaSitojun 				 * XXX: should we consider
442287546eaSitojun 				 * older rcvif?
443287546eaSitojun 				 */
444287546eaSitojun 				m->m_pkthdr.rcvif = rt->rt_ifp;
445d8a7e3a7Sitojun 
446287546eaSitojun 				icmp6_error(m, ICMP6_DST_UNREACH,
447287546eaSitojun 				    ICMP6_DST_UNREACH_ADDR, 0);
448e212adedSkrw 				if (ln->ln_hold == m) {
449e212adedSkrw 					/* m is back in ln_hold. Discard. */
450e212adedSkrw 					m_freem(ln->ln_hold);
451e212adedSkrw 					ln->ln_hold = NULL;
452e212adedSkrw 				}
453287546eaSitojun 			}
4549631a17bSitojun 			(void)nd6_free(rt, 0);
4559631a17bSitojun 			ln = NULL;
456287546eaSitojun 		}
457287546eaSitojun 		break;
458287546eaSitojun 	case ND6_LLINFO_REACHABLE:
4599631a17bSitojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
460287546eaSitojun 			ln->ln_state = ND6_LLINFO_STALE;
4619631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
462be4e9e12Sitojun 		}
463287546eaSitojun 		break;
464be4e9e12Sitojun 
465be4e9e12Sitojun 	case ND6_LLINFO_STALE:
466f3fcf2f3Smcbride 	case ND6_LLINFO_PURGE:
467be4e9e12Sitojun 		/* Garbage Collection(RFC 2461 5.3) */
4689631a17bSitojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
4699631a17bSitojun 			(void)nd6_free(rt, 1);
4709631a17bSitojun 			ln = NULL;
4719631a17bSitojun 		}
472be4e9e12Sitojun 		break;
473be4e9e12Sitojun 
474287546eaSitojun 	case ND6_LLINFO_DELAY:
475d374aaacSitojun 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
476d374aaacSitojun 			/* We need NUD */
477287546eaSitojun 			ln->ln_asked = 1;
478287546eaSitojun 			ln->ln_state = ND6_LLINFO_PROBE;
4799631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
480d374aaacSitojun 			nd6_ns_output(ifp, &dst->sin6_addr,
481d8a7e3a7Sitojun 			    &dst->sin6_addr, ln, 0);
482be4e9e12Sitojun 		} else {
483d374aaacSitojun 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
4849631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
485be4e9e12Sitojun 		}
486287546eaSitojun 		break;
487287546eaSitojun 	case ND6_LLINFO_PROBE:
488287546eaSitojun 		if (ln->ln_asked < nd6_umaxtries) {
489287546eaSitojun 			ln->ln_asked++;
4909631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
491287546eaSitojun 			nd6_ns_output(ifp, &dst->sin6_addr,
492287546eaSitojun 			    &dst->sin6_addr, ln, 0);
493d8a7e3a7Sitojun 		} else {
4949631a17bSitojun 			(void)nd6_free(rt, 0);
4959631a17bSitojun 			ln = NULL;
496d8a7e3a7Sitojun 		}
497287546eaSitojun 		break;
498287546eaSitojun 	}
4999631a17bSitojun 
5009631a17bSitojun 	splx(s);
501287546eaSitojun }
502287546eaSitojun 
5039631a17bSitojun /*
5049631a17bSitojun  * ND6 timer routine to expire default route list and prefix list
5059631a17bSitojun  */
5069631a17bSitojun void
507*ee37ea65Smcbride nd6_timer(void *ignored_arg)
5089631a17bSitojun {
5099631a17bSitojun 	int s;
5109631a17bSitojun 	struct nd_defrouter *dr;
5119631a17bSitojun 	struct nd_prefix *pr;
5129631a17bSitojun 	struct in6_ifaddr *ia6, *nia6;
5139631a17bSitojun 	struct in6_addrlifetime *lt6;
5149631a17bSitojun 
5159631a17bSitojun 	s = splsoftnet();
5169631a17bSitojun 	timeout_set(&nd6_timer_ch, nd6_timer, NULL);
5179631a17bSitojun 	timeout_add(&nd6_timer_ch, nd6_prune * hz);
5189631a17bSitojun 
5190a2c5741Sitojun 	/* expire default router list */
520f4f4d166Sitojun 	dr = TAILQ_FIRST(&nd_defrouter);
521287546eaSitojun 	while (dr) {
5223212dc31Stholo 		if (dr->expire && dr->expire < time_second) {
523287546eaSitojun 			struct nd_defrouter *t;
524f4f4d166Sitojun 			t = TAILQ_NEXT(dr, dr_entry);
525287546eaSitojun 			defrtrlist_del(dr);
526287546eaSitojun 			dr = t;
527d8a7e3a7Sitojun 		} else {
528f4f4d166Sitojun 			dr = TAILQ_NEXT(dr, dr_entry);
529287546eaSitojun 		}
530287546eaSitojun 	}
531287546eaSitojun 
532287546eaSitojun 	/*
533d8a7e3a7Sitojun 	 * expire interface addresses.
534d8a7e3a7Sitojun 	 * in the past the loop was inside prefix expiry processing.
5358b542bbeSpascoe 	 * However, from a stricter spec-conformance standpoint, we should
536d8a7e3a7Sitojun 	 * rather separate address lifetimes and prefix lifetimes.
537d8a7e3a7Sitojun 	 */
538d8a7e3a7Sitojun 	for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
539d8a7e3a7Sitojun 		nia6 = ia6->ia_next;
540d8a7e3a7Sitojun 		/* check address lifetime */
541d8a7e3a7Sitojun 		lt6 = &ia6->ia6_lifetime;
542d8a7e3a7Sitojun 		if (IFA6_IS_INVALID(ia6)) {
543d8a7e3a7Sitojun 			in6_purgeaddr(&ia6->ia_ifa);
5440a8b9475Smarkus 		} else if (IFA6_IS_DEPRECATED(ia6)) {
545d8a7e3a7Sitojun 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
546d8a7e3a7Sitojun 		} else {
547d8a7e3a7Sitojun 			/*
548d8a7e3a7Sitojun 			 * A new RA might have made a deprecated address
549d8a7e3a7Sitojun 			 * preferred.
550d8a7e3a7Sitojun 			 */
551d8a7e3a7Sitojun 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
552d8a7e3a7Sitojun 		}
553d8a7e3a7Sitojun 	}
554d8a7e3a7Sitojun 
555d8a7e3a7Sitojun 	/* expire prefix list */
556545205eaSpyr 	pr = LIST_FIRST(&nd_prefix);
557545205eaSpyr 	while (pr != NULL) {
558d8a7e3a7Sitojun 		/*
559287546eaSitojun 		 * check prefix lifetime.
560287546eaSitojun 		 * since pltime is just for autoconf, pltime processing for
561287546eaSitojun 		 * prefix is not necessary.
562287546eaSitojun 		 */
563d8a7e3a7Sitojun 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
5643212dc31Stholo 		    time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
565287546eaSitojun 			struct nd_prefix *t;
566545205eaSpyr 			t = LIST_NEXT(pr, ndpr_entry);
567287546eaSitojun 
568287546eaSitojun 			/*
569287546eaSitojun 			 * address expiration and prefix expiration are
570d8a7e3a7Sitojun 			 * separate.  NEVER perform in6_purgeaddr here.
571287546eaSitojun 			 */
572287546eaSitojun 
573287546eaSitojun 			prelist_remove(pr);
574287546eaSitojun 			pr = t;
575287546eaSitojun 		} else
576545205eaSpyr 			pr = LIST_NEXT(pr, ndpr_entry);
577287546eaSitojun 	}
578287546eaSitojun 	splx(s);
579287546eaSitojun }
580287546eaSitojun 
58122770369Sitojun /*
58222770369Sitojun  * Nuke neighbor cache/prefix/default router management table, right before
58322770369Sitojun  * ifp goes away.
58422770369Sitojun  */
58522770369Sitojun void
586*ee37ea65Smcbride nd6_purge(struct ifnet *ifp)
58722770369Sitojun {
58822770369Sitojun 	struct llinfo_nd6 *ln, *nln;
589d8a7e3a7Sitojun 	struct nd_defrouter *dr, *ndr;
59022770369Sitojun 	struct nd_prefix *pr, *npr;
59122770369Sitojun 
59222770369Sitojun 	/*
593d8a7e3a7Sitojun 	 * Nuke default router list entries toward ifp.
594d8a7e3a7Sitojun 	 * We defer removal of default router list entries that is installed
595d8a7e3a7Sitojun 	 * in the routing table, in order to keep additional side effects as
596d8a7e3a7Sitojun 	 * small as possible.
59722770369Sitojun 	 */
598d8a7e3a7Sitojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
599f4f4d166Sitojun 		ndr = TAILQ_NEXT(dr, dr_entry);
600d8a7e3a7Sitojun 		if (dr->installed)
601d8a7e3a7Sitojun 			continue;
602d8a7e3a7Sitojun 
60322770369Sitojun 		if (dr->ifp == ifp)
60422770369Sitojun 			defrtrlist_del(dr);
60522770369Sitojun 	}
606d8a7e3a7Sitojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
607d8a7e3a7Sitojun 		ndr = TAILQ_NEXT(dr, dr_entry);
608d8a7e3a7Sitojun 		if (!dr->installed)
609d8a7e3a7Sitojun 			continue;
610d8a7e3a7Sitojun 
61122770369Sitojun 		if (dr->ifp == ifp)
61222770369Sitojun 			defrtrlist_del(dr);
61322770369Sitojun 	}
61422770369Sitojun 
61522770369Sitojun 	/* Nuke prefix list entries toward ifp */
616545205eaSpyr 	for (pr = LIST_FIRST(&nd_prefix); pr != NULL; pr = npr) {
617545205eaSpyr 		npr = LIST_NEXT(pr, ndpr_entry);
61822770369Sitojun 		if (pr->ndpr_ifp == ifp) {
619d8a7e3a7Sitojun 			/*
62008abfc37Sbrad 			 * Because if_detach() does *not* release prefixes
62108abfc37Sbrad 			 * while purging addresses the reference count will
62208abfc37Sbrad 			 * still be above zero. We therefore reset it to
62308abfc37Sbrad 			 * make sure that the prefix really gets purged.
62408abfc37Sbrad 			 */
62508abfc37Sbrad 			pr->ndpr_refcnt = 0;
62608abfc37Sbrad 			/*
627d8a7e3a7Sitojun 			 * Previously, pr->ndpr_addr is removed as well,
628d8a7e3a7Sitojun 			 * but I strongly believe we don't have to do it.
629d8a7e3a7Sitojun 			 * nd6_purge() is only called from in6_ifdetach(),
630d8a7e3a7Sitojun 			 * which removes all the associated interface addresses
631d8a7e3a7Sitojun 			 * by itself.
632d8a7e3a7Sitojun 			 * (jinmei@kame.net 20010129)
633d8a7e3a7Sitojun 			 */
63422770369Sitojun 			prelist_remove(pr);
63522770369Sitojun 		}
63622770369Sitojun 	}
63722770369Sitojun 
638f4f4d166Sitojun 	/* cancel default outgoing interface setting */
639f4f4d166Sitojun 	if (nd6_defifindex == ifp->if_index)
640f4f4d166Sitojun 		nd6_setdefaultiface(0);
641f4f4d166Sitojun 
642e6e438a4Sitojun 	if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
64322770369Sitojun 		/* refresh default router list */
644f4f4d166Sitojun 		defrouter_select();
645e6e438a4Sitojun 	}
64622770369Sitojun 
64722770369Sitojun 	/*
64822770369Sitojun 	 * Nuke neighbor cache entries for the ifp.
64922770369Sitojun 	 * Note that rt->rt_ifp may not be the same as ifp,
65022770369Sitojun 	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
65122770369Sitojun 	 * nd6_rtrequest(), and ip6_input().
65222770369Sitojun 	 */
65322770369Sitojun 	ln = llinfo_nd6.ln_next;
65422770369Sitojun 	while (ln && ln != &llinfo_nd6) {
65522770369Sitojun 		struct rtentry *rt;
65622770369Sitojun 		struct sockaddr_dl *sdl;
65722770369Sitojun 
65822770369Sitojun 		nln = ln->ln_next;
65922770369Sitojun 		rt = ln->ln_rt;
66022770369Sitojun 		if (rt && rt->rt_gateway &&
66122770369Sitojun 		    rt->rt_gateway->sa_family == AF_LINK) {
66222770369Sitojun 			sdl = (struct sockaddr_dl *)rt->rt_gateway;
66322770369Sitojun 			if (sdl->sdl_index == ifp->if_index)
664d8a7e3a7Sitojun 				nln = nd6_free(rt, 0);
66522770369Sitojun 		}
66622770369Sitojun 		ln = nln;
66722770369Sitojun 	}
66822770369Sitojun }
66922770369Sitojun 
670287546eaSitojun struct rtentry *
671*ee37ea65Smcbride nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
672287546eaSitojun {
673287546eaSitojun 	struct rtentry *rt;
674287546eaSitojun 	struct sockaddr_in6 sin6;
675287546eaSitojun 
676287546eaSitojun 	bzero(&sin6, sizeof(sin6));
677287546eaSitojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
678287546eaSitojun 	sin6.sin6_family = AF_INET6;
679287546eaSitojun 	sin6.sin6_addr = *addr6;
680d8a7e3a7Sitojun 
681c241aca8Shenning 	rt = rtalloc1((struct sockaddr *)&sin6, create, 0);
682287546eaSitojun 	if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
683287546eaSitojun 		/*
684287546eaSitojun 		 * This is the case for the default route.
685287546eaSitojun 		 * If we want to create a neighbor cache for the address, we
686287546eaSitojun 		 * should free the route for the destination and allocate an
687287546eaSitojun 		 * interface route.
688287546eaSitojun 		 */
689287546eaSitojun 		if (create) {
690287546eaSitojun 			RTFREE(rt);
691287546eaSitojun 			rt = 0;
692287546eaSitojun 		}
693287546eaSitojun 	}
694287546eaSitojun 	if (!rt) {
695287546eaSitojun 		if (create && ifp) {
696cb24f5e5Sclaudio 			struct rt_addrinfo info;
697d374aaacSitojun 			int e;
698d374aaacSitojun 
699287546eaSitojun 			/*
700287546eaSitojun 			 * If no route is available and create is set,
701287546eaSitojun 			 * we allocate a host route for the destination
702287546eaSitojun 			 * and treat it like an interface route.
703287546eaSitojun 			 * This hack is necessary for a neighbor which can't
704287546eaSitojun 			 * be covered by our own prefix.
705287546eaSitojun 			 */
706287546eaSitojun 			struct ifaddr *ifa =
707287546eaSitojun 			    ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
708287546eaSitojun 			if (ifa == NULL)
709287546eaSitojun 				return (NULL);
710287546eaSitojun 
711287546eaSitojun 			/*
712287546eaSitojun 			 * Create a new route.  RTF_LLINFO is necessary
713287546eaSitojun 			 * to create a Neighbor Cache entry for the
714287546eaSitojun 			 * destination in nd6_rtrequest which will be
715cb24f5e5Sclaudio 			 * called in rtrequest1 via ifa->ifa_rtrequest.
716287546eaSitojun 			 */
717cb24f5e5Sclaudio 			bzero(&info, sizeof(info));
718cb24f5e5Sclaudio 			info.rti_flags = (ifa->ifa_flags | RTF_HOST |
719cb24f5e5Sclaudio 			    RTF_LLINFO) & ~RTF_CLONING;
720cb24f5e5Sclaudio 			info.rti_info[RTAX_DST] = (struct sockaddr *)&sin6;
721cb24f5e5Sclaudio 			info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
722cb24f5e5Sclaudio 			info.rti_info[RTAX_NETMASK] =
723cb24f5e5Sclaudio 			    (struct sockaddr *)&all1_sa;
724cb24f5e5Sclaudio 			if ((e = rtrequest1(RTM_ADD, &info, RTP_CONNECTED,
725cb24f5e5Sclaudio 			    &rt, 0)) != 0) {
726d8a7e3a7Sitojun #if 0
727287546eaSitojun 				log(LOG_ERR,
728287546eaSitojun 				    "nd6_lookup: failed to add route for a "
729d374aaacSitojun 				    "neighbor(%s), errno=%d\n",
730d374aaacSitojun 				    ip6_sprintf(addr6), e);
731d8a7e3a7Sitojun #endif
732d8a7e3a7Sitojun 				return (NULL);
733d8a7e3a7Sitojun 			}
734287546eaSitojun 			if (rt == NULL)
735287546eaSitojun 				return (NULL);
736287546eaSitojun 			if (rt->rt_llinfo) {
737287546eaSitojun 				struct llinfo_nd6 *ln =
738287546eaSitojun 				    (struct llinfo_nd6 *)rt->rt_llinfo;
739287546eaSitojun 				ln->ln_state = ND6_LLINFO_NOSTATE;
740287546eaSitojun 			}
741f6e55599Sitojun 		} else
742287546eaSitojun 			return (NULL);
743287546eaSitojun 	}
744287546eaSitojun 	rt->rt_refcnt--;
745287546eaSitojun 	/*
746287546eaSitojun 	 * Validation for the entry.
747d8a7e3a7Sitojun 	 * Note that the check for rt_llinfo is necessary because a cloned
748d8a7e3a7Sitojun 	 * route from a parent route that has the L flag (e.g. the default
749d8a7e3a7Sitojun 	 * route to a p2p interface) may have the flag, too, while the
750d8a7e3a7Sitojun 	 * destination is not actually a neighbor.
751287546eaSitojun 	 * XXX: we can't use rt->rt_ifp to check for the interface, since
752287546eaSitojun 	 *      it might be the loopback interface if the entry is for our
753287546eaSitojun 	 *      own address on a non-loopback interface. Instead, we should
754d8a7e3a7Sitojun 	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL
755d8a7e3a7Sitojun 	 *	interface.
756287546eaSitojun 	 */
757287546eaSitojun 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
758d8a7e3a7Sitojun 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
759287546eaSitojun 	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
760287546eaSitojun 		if (create) {
761da592434Sitojun 			nd6log((LOG_DEBUG,
762d8a7e3a7Sitojun 			    "nd6_lookup: failed to lookup %s (if = %s)\n",
763da592434Sitojun 			    ip6_sprintf(addr6),
764da592434Sitojun 			    ifp ? ifp->if_xname : "unspec"));
765287546eaSitojun 		}
766d8a7e3a7Sitojun 		return (NULL);
767287546eaSitojun 	}
768287546eaSitojun 	return (rt);
769287546eaSitojun }
770287546eaSitojun 
771287546eaSitojun /*
772287546eaSitojun  * Detect if a given IPv6 address identifies a neighbor on a given link.
773287546eaSitojun  * XXX: should take care of the destination of a p2p link?
774287546eaSitojun  */
775287546eaSitojun int
776*ee37ea65Smcbride nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
777287546eaSitojun {
778d8a7e3a7Sitojun 	struct nd_prefix *pr;
779d8a7e3a7Sitojun 	struct rtentry *rt;
780287546eaSitojun 
781cfb6b8dfSitojun 	/*
782cfb6b8dfSitojun 	 * A link-local address is always a neighbor.
783cfb6b8dfSitojun 	 * XXX: we should use the sin6_scope_id field rather than the embedded
784cfb6b8dfSitojun 	 * interface index.
785d8a7e3a7Sitojun 	 * XXX: a link does not necessarily specify a single interface.
786cfb6b8dfSitojun 	 */
787cfb6b8dfSitojun 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
788cfb6b8dfSitojun 	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
789287546eaSitojun 		return (1);
790287546eaSitojun 
791287546eaSitojun 	/*
792d8a7e3a7Sitojun 	 * If the address matches one of our on-link prefixes, it should be a
793d8a7e3a7Sitojun 	 * neighbor.
794287546eaSitojun 	 */
795545205eaSpyr 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
796d8a7e3a7Sitojun 		if (pr->ndpr_ifp != ifp)
797d8a7e3a7Sitojun 			continue;
798287546eaSitojun 
799d8a7e3a7Sitojun 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
800d8a7e3a7Sitojun 			continue;
801d8a7e3a7Sitojun 
802d8a7e3a7Sitojun 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
803d8a7e3a7Sitojun 		    &addr->sin6_addr, &pr->ndpr_mask))
804d8a7e3a7Sitojun 			return (1);
805287546eaSitojun 	}
806d8a7e3a7Sitojun 
807d8a7e3a7Sitojun 	/*
808d8a7e3a7Sitojun 	 * If the default router list is empty, all addresses are regarded
809d8a7e3a7Sitojun 	 * as on-link, and thus, as a neighbor.
810d8a7e3a7Sitojun 	 * XXX: we restrict the condition to hosts, because routers usually do
811d8a7e3a7Sitojun 	 * not have the "default router list".
812d8a7e3a7Sitojun 	 */
813d8a7e3a7Sitojun 	if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
814d8a7e3a7Sitojun 	    nd6_defifindex == ifp->if_index) {
815287546eaSitojun 		return (1);
816287546eaSitojun 	}
817287546eaSitojun 
818287546eaSitojun 	/*
819287546eaSitojun 	 * Even if the address matches none of our addresses, it might be
820287546eaSitojun 	 * in the neighbor cache.
821287546eaSitojun 	 */
822d8a7e3a7Sitojun 	if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL)
823287546eaSitojun 		return (1);
824287546eaSitojun 
825287546eaSitojun 	return (0);
826287546eaSitojun }
827287546eaSitojun 
828287546eaSitojun /*
829287546eaSitojun  * Free an nd6 llinfo entry.
830d8a7e3a7Sitojun  * Since the function would cause significant changes in the kernel, DO NOT
831d8a7e3a7Sitojun  * make it global, unless you have a strong reason for the change, and are sure
832d8a7e3a7Sitojun  * that the change is safe.
833287546eaSitojun  */
834d8a7e3a7Sitojun static struct llinfo_nd6 *
835*ee37ea65Smcbride nd6_free(struct rtentry *rt, int gc)
836287546eaSitojun {
837cb24f5e5Sclaudio 	struct rt_addrinfo info;
83829760ae1Sitojun 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
839f4f4d166Sitojun 	struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
840287546eaSitojun 	struct nd_defrouter *dr;
841287546eaSitojun 
842287546eaSitojun 	/*
843d8a7e3a7Sitojun 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
844d8a7e3a7Sitojun 	 * even though it is not harmful, it was not really necessary.
845287546eaSitojun 	 */
846f4f4d166Sitojun 
847492c93a5Sitojun 	if (!ip6_forwarding) {
848f4f4d166Sitojun 		int s;
8496e92dee6Sitojun 		s = splsoftnet();
850f4f4d166Sitojun 		dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
851f4f4d166Sitojun 		    rt->rt_ifp);
852d8a7e3a7Sitojun 
853d8a7e3a7Sitojun 		if (dr != NULL && dr->expire &&
854d8a7e3a7Sitojun 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
855d8a7e3a7Sitojun 			/*
856d8a7e3a7Sitojun 			 * If the reason for the deletion is just garbage
857d8a7e3a7Sitojun 			 * collection, and the neighbor is an active default
858d8a7e3a7Sitojun 			 * router, do not delete it.  Instead, reset the GC
859d8a7e3a7Sitojun 			 * timer using the router's lifetime.
860d8a7e3a7Sitojun 			 * Simply deleting the entry would affect default
861d8a7e3a7Sitojun 			 * router selection, which is not necessarily a good
862d8a7e3a7Sitojun 			 * thing, especially when we're using router preference
863d8a7e3a7Sitojun 			 * values.
864d8a7e3a7Sitojun 			 * XXX: the check for ln_state would be redundant,
865d8a7e3a7Sitojun 			 *      but we intentionally keep it just in case.
866d8a7e3a7Sitojun 			 */
8673212dc31Stholo 			if (dr->expire > time_second * hz) {
8689631a17bSitojun 				nd6_llinfo_settimer(ln,
8693212dc31Stholo 				    dr->expire - time_second * hz);
8709631a17bSitojun 			} else
8719631a17bSitojun 				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
872d8a7e3a7Sitojun 			splx(s);
873d8a7e3a7Sitojun 			return (ln->ln_next);
874d8a7e3a7Sitojun 		}
875d8a7e3a7Sitojun 
876f4f4d166Sitojun 		if (ln->ln_router || dr) {
877f4f4d166Sitojun 			/*
878f4f4d166Sitojun 			 * rt6_flush must be called whether or not the neighbor
879f4f4d166Sitojun 			 * is in the Default Router List.
880f4f4d166Sitojun 			 * See a corresponding comment in nd6_na_input().
881f4f4d166Sitojun 			 */
882f4f4d166Sitojun 			rt6_flush(&in6, rt->rt_ifp);
883f4f4d166Sitojun 		}
884f4f4d166Sitojun 
885f4f4d166Sitojun 		if (dr) {
886f4f4d166Sitojun 			/*
8878b542bbeSpascoe 			 * Unreachability of a router might affect the default
888f4f4d166Sitojun 			 * router selection and on-link detection of advertised
889f4f4d166Sitojun 			 * prefixes.
890f4f4d166Sitojun 			 */
891f4f4d166Sitojun 
892f4f4d166Sitojun 			/*
893f4f4d166Sitojun 			 * Temporarily fake the state to choose a new default
894f4f4d166Sitojun 			 * router and to perform on-link determination of
89534deef1eSitojun 			 * prefixes correctly.
896f4f4d166Sitojun 			 * Below the state will be set correctly,
897f4f4d166Sitojun 			 * or the entry itself will be deleted.
898f4f4d166Sitojun 			 */
899f4f4d166Sitojun 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
900f4f4d166Sitojun 
901f4f4d166Sitojun 			/*
902d8a7e3a7Sitojun 			 * Since defrouter_select() does not affect the
903d8a7e3a7Sitojun 			 * on-link determination and MIP6 needs the check
904d8a7e3a7Sitojun 			 * before the default router selection, we perform
905d8a7e3a7Sitojun 			 * the check now.
906f4f4d166Sitojun 			 */
907f4f4d166Sitojun 			pfxlist_onlink_check();
908d8a7e3a7Sitojun 
909d8a7e3a7Sitojun 			/*
910d8a7e3a7Sitojun 			 * refresh default router list
911d8a7e3a7Sitojun 			 */
912d8a7e3a7Sitojun 			defrouter_select();
913287546eaSitojun 		}
914287546eaSitojun 		splx(s);
915287546eaSitojun 	}
916287546eaSitojun 
91729760ae1Sitojun 	/*
91829760ae1Sitojun 	 * Before deleting the entry, remember the next entry as the
91929760ae1Sitojun 	 * return value.  We need this because pfxlist_onlink_check() above
92029760ae1Sitojun 	 * might have freed other entries (particularly the old next entry) as
92129760ae1Sitojun 	 * a side effect (XXX).
92229760ae1Sitojun 	 */
92329760ae1Sitojun 	next = ln->ln_next;
92429760ae1Sitojun 
92529760ae1Sitojun 	/*
92629760ae1Sitojun 	 * Detach the route from the routing tree and the list of neighbor
92729760ae1Sitojun 	 * caches, and disable the route entry not to be used in already
92829760ae1Sitojun 	 * cached routes.
92929760ae1Sitojun 	 */
930cb24f5e5Sclaudio 	bzero(&info, sizeof(info));
931cb24f5e5Sclaudio 	info.rti_info[RTAX_DST] = rt_key(rt);
932cb24f5e5Sclaudio 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
933cb24f5e5Sclaudio 	rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, 0);
93429760ae1Sitojun 
9350a2c5741Sitojun 	return (next);
936287546eaSitojun }
937287546eaSitojun 
938287546eaSitojun /*
939287546eaSitojun  * Upper-layer reachability hint for Neighbor Unreachability Detection.
940287546eaSitojun  *
9418b542bbeSpascoe  * XXX cost-effective methods?
942287546eaSitojun  */
943287546eaSitojun void
944*ee37ea65Smcbride nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
945287546eaSitojun {
946287546eaSitojun 	struct llinfo_nd6 *ln;
947287546eaSitojun 
948287546eaSitojun 	/*
949287546eaSitojun 	 * If the caller specified "rt", use that.  Otherwise, resolve the
950287546eaSitojun 	 * routing table by supplied "dst6".
951287546eaSitojun 	 */
952287546eaSitojun 	if (!rt) {
953287546eaSitojun 		if (!dst6)
954287546eaSitojun 			return;
955287546eaSitojun 		if (!(rt = nd6_lookup(dst6, 0, NULL)))
956287546eaSitojun 			return;
957287546eaSitojun 	}
958287546eaSitojun 
959f6e55599Sitojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
960f6e55599Sitojun 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
961f6e55599Sitojun 	    !rt->rt_llinfo || !rt->rt_gateway ||
962f6e55599Sitojun 	    rt->rt_gateway->sa_family != AF_LINK) {
963287546eaSitojun 		/* This is not a host route. */
964287546eaSitojun 		return;
965287546eaSitojun 	}
966287546eaSitojun 
967287546eaSitojun 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
968804d8827Sitojun 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
969287546eaSitojun 		return;
970287546eaSitojun 
971f6e55599Sitojun 	/*
972f6e55599Sitojun 	 * if we get upper-layer reachability confirmation many times,
973f6e55599Sitojun 	 * it is possible we have false information.
974f6e55599Sitojun 	 */
975f6e55599Sitojun 	if (!force) {
976f6e55599Sitojun 		ln->ln_byhint++;
977f6e55599Sitojun 		if (ln->ln_byhint > nd6_maxnudhint)
978f6e55599Sitojun 			return;
979f6e55599Sitojun 	}
980f6e55599Sitojun 
981287546eaSitojun 	ln->ln_state = ND6_LLINFO_REACHABLE;
9829631a17bSitojun 	if (!ND6_LLINFO_PERMANENT(ln)) {
9839631a17bSitojun 		nd6_llinfo_settimer(ln,
9849631a17bSitojun 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
9859631a17bSitojun 	}
986287546eaSitojun }
987287546eaSitojun 
988*ee37ea65Smcbride /*
989*ee37ea65Smcbride  * info - XXX: unused
990*ee37ea65Smcbride  */
991*ee37ea65Smcbride 
992287546eaSitojun void
993*ee37ea65Smcbride nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
994287546eaSitojun {
995287546eaSitojun 	struct sockaddr *gate = rt->rt_gateway;
996287546eaSitojun 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
997287546eaSitojun 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
998287546eaSitojun 	struct ifnet *ifp = rt->rt_ifp;
999287546eaSitojun 	struct ifaddr *ifa;
1000d8a7e3a7Sitojun 	int mine = 0;
1001287546eaSitojun 
1002d8a7e3a7Sitojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
1003287546eaSitojun 		return;
1004287546eaSitojun 
1005d8a7e3a7Sitojun 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1006d8a7e3a7Sitojun 		/*
1007d8a7e3a7Sitojun 		 * This is probably an interface direct route for a link
1008d8a7e3a7Sitojun 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1009d8a7e3a7Sitojun 		 * We do not need special treatment below for such a route.
1010d8a7e3a7Sitojun 		 * Moreover, the RTF_LLINFO flag which would be set below
1011d8a7e3a7Sitojun 		 * would annoy the ndp(8) command.
1012d8a7e3a7Sitojun 		 */
1013d8a7e3a7Sitojun 		return;
1014d8a7e3a7Sitojun 	}
1015d8a7e3a7Sitojun 
1016d8a7e3a7Sitojun 	if (req == RTM_RESOLVE &&
1017d8a7e3a7Sitojun 	    (nd6_need_cache(ifp) == 0 || /* stf case */
1018d8a7e3a7Sitojun 	     !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1019d8a7e3a7Sitojun 		/*
1020d8a7e3a7Sitojun 		 * FreeBSD and BSD/OS often make a cloned host route based
1021d8a7e3a7Sitojun 		 * on a less-specific route (e.g. the default route).
1022d8a7e3a7Sitojun 		 * If the less specific route does not have a "gateway"
1023d8a7e3a7Sitojun 		 * (this is the case when the route just goes to a p2p or an
1024d8a7e3a7Sitojun 		 * stf interface), we'll mistakenly make a neighbor cache for
1025d8a7e3a7Sitojun 		 * the host route, and will see strange neighbor solicitation
1026d8a7e3a7Sitojun 		 * for the corresponding destination.  In order to avoid the
1027d8a7e3a7Sitojun 		 * confusion, we check if the destination of the route is
1028d8a7e3a7Sitojun 		 * a neighbor in terms of neighbor discovery, and stop the
1029d8a7e3a7Sitojun 		 * process if not.  Additionally, we remove the LLINFO flag
1030d8a7e3a7Sitojun 		 * so that ndp(8) will not try to get the neighbor information
1031d8a7e3a7Sitojun 		 * of the destination.
1032d8a7e3a7Sitojun 		 */
1033d8a7e3a7Sitojun 		rt->rt_flags &= ~RTF_LLINFO;
1034d8a7e3a7Sitojun 		return;
1035d8a7e3a7Sitojun 	}
1036d8a7e3a7Sitojun 
1037287546eaSitojun 	switch (req) {
1038287546eaSitojun 	case RTM_ADD:
1039287546eaSitojun 		/*
1040287546eaSitojun 		 * There is no backward compatibility :)
1041287546eaSitojun 		 *
1042287546eaSitojun 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1043287546eaSitojun 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1044287546eaSitojun 		 *	   rt->rt_flags |= RTF_CLONING;
1045287546eaSitojun 		 */
104648ebf8e1Sitojun 		if ((rt->rt_flags & RTF_CLONING) ||
104748ebf8e1Sitojun 		    ((rt->rt_flags & RTF_LLINFO) && !ln)) {
1048287546eaSitojun 			/*
104948ebf8e1Sitojun 			 * Case 1: This route should come from a route to
105048ebf8e1Sitojun 			 * interface (RTF_CLONING case) or the route should be
105148ebf8e1Sitojun 			 * treated as on-link but is currently not
105248ebf8e1Sitojun 			 * (RTF_LLINFO && !ln case).
1053287546eaSitojun 			 */
1054287546eaSitojun 			rt_setgate(rt, rt_key(rt),
1055c241aca8Shenning 				   (struct sockaddr *)&null_sdl, 0);
1056287546eaSitojun 			gate = rt->rt_gateway;
1057287546eaSitojun 			SDL(gate)->sdl_type = ifp->if_type;
1058287546eaSitojun 			SDL(gate)->sdl_index = ifp->if_index;
1059287546eaSitojun 			if (ln)
10609631a17bSitojun 				nd6_llinfo_settimer(ln, 0);
1061d8a7e3a7Sitojun 			if ((rt->rt_flags & RTF_CLONING) != 0)
1062287546eaSitojun 				break;
1063287546eaSitojun 		}
1064f4f4d166Sitojun 		/*
10658b542bbeSpascoe 		 * In IPv4 code, we try to announce new RTF_ANNOUNCE entry here.
1066f4f4d166Sitojun 		 * We don't do that here since llinfo is not ready yet.
1067f4f4d166Sitojun 		 *
1068f4f4d166Sitojun 		 * There are also couple of other things to be discussed:
1069f4f4d166Sitojun 		 * - unsolicited NA code needs improvement beforehand
1070f4f4d166Sitojun 		 * - RFC2461 says we MAY send multicast unsolicited NA
1071f4f4d166Sitojun 		 *   (7.2.6 paragraph 4), however, it also says that we
1072f4f4d166Sitojun 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
1073f4f4d166Sitojun 		 *   we don't have anything like it right now.
1074841d7adbSitojun 		 *   note that the mechanism needs a mutual agreement
1075f4f4d166Sitojun 		 *   between proxies, which means that we need to implement
1076841d7adbSitojun 		 *   a new protocol, or a new kludge.
1077841d7adbSitojun 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1078f4f4d166Sitojun 		 *   we need to check ip6forwarding before sending it.
1079f4f4d166Sitojun 		 *   (or should we allow proxy ND configuration only for
1080f4f4d166Sitojun 		 *   routers?  there's no mention about proxy ND from hosts)
1081f4f4d166Sitojun 		 */
1082f4f4d166Sitojun #if 0
1083f4f4d166Sitojun 		/* XXX it does not work */
1084287546eaSitojun 		if (rt->rt_flags & RTF_ANNOUNCE)
1085287546eaSitojun 			nd6_na_output(ifp,
1086287546eaSitojun 			      &SIN6(rt_key(rt))->sin6_addr,
1087287546eaSitojun 			      &SIN6(rt_key(rt))->sin6_addr,
1088287546eaSitojun 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1089f4f4d166Sitojun 			      1, NULL);
1090f4f4d166Sitojun #endif
1091287546eaSitojun 		/* FALLTHROUGH */
1092287546eaSitojun 	case RTM_RESOLVE:
1093d8a7e3a7Sitojun 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1094d374aaacSitojun 			/*
1095d374aaacSitojun 			 * Address resolution isn't necessary for a point to
1096d374aaacSitojun 			 * point link, so we can skip this test for a p2p link.
1097d374aaacSitojun 			 */
1098287546eaSitojun 			if (gate->sa_family != AF_LINK ||
1099287546eaSitojun 			    gate->sa_len < sizeof(null_sdl)) {
1100d374aaacSitojun 				log(LOG_DEBUG,
11010a2c5741Sitojun 				    "nd6_rtrequest: bad gateway value: %s\n",
110293036e4eSangelos 				    ifp->if_xname);
1103287546eaSitojun 				break;
1104287546eaSitojun 			}
1105287546eaSitojun 			SDL(gate)->sdl_type = ifp->if_type;
1106287546eaSitojun 			SDL(gate)->sdl_index = ifp->if_index;
1107d374aaacSitojun 		}
1108d374aaacSitojun 		if (ln != NULL)
1109287546eaSitojun 			break;	/* This happens on a route change */
1110287546eaSitojun 		/*
1111287546eaSitojun 		 * Case 2: This route may come from cloning, or a manual route
1112287546eaSitojun 		 * add with a LL address.
1113287546eaSitojun 		 */
1114287546eaSitojun 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1115287546eaSitojun 		rt->rt_llinfo = (caddr_t)ln;
1116287546eaSitojun 		if (!ln) {
1117287546eaSitojun 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1118287546eaSitojun 			break;
1119287546eaSitojun 		}
1120287546eaSitojun 		nd6_inuse++;
1121287546eaSitojun 		nd6_allocated++;
1122287546eaSitojun 		Bzero(ln, sizeof(*ln));
1123287546eaSitojun 		ln->ln_rt = rt;
11249631a17bSitojun 		timeout_set(&ln->ln_timer_ch, nd6_llinfo_timer, ln);
1125287546eaSitojun 		/* this is required for "ndp" command. - shin */
1126287546eaSitojun 		if (req == RTM_ADD) {
1127287546eaSitojun 		        /*
1128287546eaSitojun 			 * gate should have some valid AF_LINK entry,
1129287546eaSitojun 			 * and ln->ln_expire should have some lifetime
1130287546eaSitojun 			 * which is specified by ndp command.
1131287546eaSitojun 			 */
1132287546eaSitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1133f6e55599Sitojun 			ln->ln_byhint = 0;
1134287546eaSitojun 		} else {
1135287546eaSitojun 		        /*
1136287546eaSitojun 			 * When req == RTM_RESOLVE, rt is created and
1137287546eaSitojun 			 * initialized in rtrequest(), so rt_expire is 0.
1138287546eaSitojun 			 */
1139287546eaSitojun 			ln->ln_state = ND6_LLINFO_NOSTATE;
11409631a17bSitojun 			nd6_llinfo_settimer(ln, 0);
1141287546eaSitojun 		}
1142287546eaSitojun 		rt->rt_flags |= RTF_LLINFO;
1143287546eaSitojun 		ln->ln_next = llinfo_nd6.ln_next;
1144287546eaSitojun 		llinfo_nd6.ln_next = ln;
1145287546eaSitojun 		ln->ln_prev = &llinfo_nd6;
1146287546eaSitojun 		ln->ln_next->ln_prev = ln;
1147287546eaSitojun 
1148287546eaSitojun 		/*
1149f3fcf2f3Smcbride 		 * If we have too many cache entries, initiate immediate
1150f3fcf2f3Smcbride 		 * purging for some "less recently used" entries.  Note that
1151f3fcf2f3Smcbride 		 * we cannot directly call nd6_free() here because it would
1152f3fcf2f3Smcbride 		 * cause re-entering rtable related routines triggering an LOR
1153f3fcf2f3Smcbride 		 * problem for FreeBSD.
1154f3fcf2f3Smcbride 		 */
1155f3fcf2f3Smcbride 		if (ip6_neighborgcthresh >= 0 &&
1156f3fcf2f3Smcbride 		    nd6_inuse >= ip6_neighborgcthresh) {
1157f3fcf2f3Smcbride 			int i;
1158f3fcf2f3Smcbride 
1159f3fcf2f3Smcbride 			for (i = 0; i < 10 && llinfo_nd6.ln_prev != ln; i++) {
1160f3fcf2f3Smcbride 				struct llinfo_nd6 *ln_end = llinfo_nd6.ln_prev;
1161f3fcf2f3Smcbride 
1162f3fcf2f3Smcbride 				/* Move this entry to the head */
1163f3fcf2f3Smcbride 				LN_DEQUEUE(ln_end);
1164f3fcf2f3Smcbride 				LN_INSERTHEAD(ln_end);
1165f3fcf2f3Smcbride 
1166f3fcf2f3Smcbride 				if (ND6_LLINFO_PERMANENT(ln_end))
1167f3fcf2f3Smcbride 					continue;
1168f3fcf2f3Smcbride 
1169f3fcf2f3Smcbride 				if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
1170f3fcf2f3Smcbride 					ln_end->ln_state = ND6_LLINFO_STALE;
1171f3fcf2f3Smcbride 				else
1172f3fcf2f3Smcbride 					ln_end->ln_state = ND6_LLINFO_PURGE;
1173f3fcf2f3Smcbride 				nd6_llinfo_settimer(ln_end, 0);
1174f3fcf2f3Smcbride 			}
1175f3fcf2f3Smcbride 		}
1176f3fcf2f3Smcbride 
1177f3fcf2f3Smcbride 		/*
1178287546eaSitojun 		 * check if rt_key(rt) is one of my address assigned
1179287546eaSitojun 		 * to the interface.
1180287546eaSitojun 		 */
1181287546eaSitojun 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1182287546eaSitojun 		    &SIN6(rt_key(rt))->sin6_addr);
1183287546eaSitojun 		if (ifa) {
1184287546eaSitojun 			caddr_t macp = nd6_ifptomac(ifp);
11859631a17bSitojun 			nd6_llinfo_settimer(ln, -1);
1186287546eaSitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1187f6e55599Sitojun 			ln->ln_byhint = 0;
1188d8a7e3a7Sitojun 			mine = 1;
1189287546eaSitojun 			if (macp) {
1190287546eaSitojun 				Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1191287546eaSitojun 				SDL(gate)->sdl_alen = ifp->if_addrlen;
1192287546eaSitojun 			}
1193287546eaSitojun 			if (nd6_useloopback) {
1194fcc641efSmickey 				rt->rt_ifp = lo0ifp;	/*XXX*/
1195287546eaSitojun 				/*
1196287546eaSitojun 				 * Make sure rt_ifa be equal to the ifaddr
1197287546eaSitojun 				 * corresponding to the address.
1198287546eaSitojun 				 * We need this because when we refer
1199287546eaSitojun 				 * rt_ifa->ia6_flags in ip6_input, we assume
1200287546eaSitojun 				 * that the rt_ifa points to the address instead
1201287546eaSitojun 				 * of the loopback address.
1202287546eaSitojun 				 */
1203287546eaSitojun 				if (ifa != rt->rt_ifa) {
12044f587652Sitojun 					IFAFREE(rt->rt_ifa);
1205287546eaSitojun 					ifa->ifa_refcnt++;
1206287546eaSitojun 					rt->rt_ifa = ifa;
1207287546eaSitojun 				}
1208287546eaSitojun 			}
1209f4f4d166Sitojun 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
12109631a17bSitojun 			nd6_llinfo_settimer(ln, -1);
1211f4f4d166Sitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1212f6e55599Sitojun 			ln->ln_byhint = 0;
1213f4f4d166Sitojun 
1214f4f4d166Sitojun 			/* join solicited node multicast for proxy ND */
1215f4f4d166Sitojun 			if (ifp->if_flags & IFF_MULTICAST) {
1216f4f4d166Sitojun 				struct in6_addr llsol;
1217f4f4d166Sitojun 				int error;
1218f4f4d166Sitojun 
1219f4f4d166Sitojun 				llsol = SIN6(rt_key(rt))->sin6_addr;
1220f4f4d166Sitojun 				llsol.s6_addr16[0] = htons(0xff02);
1221f4f4d166Sitojun 				llsol.s6_addr16[1] = htons(ifp->if_index);
1222f4f4d166Sitojun 				llsol.s6_addr32[1] = 0;
1223f4f4d166Sitojun 				llsol.s6_addr32[2] = htonl(1);
1224f4f4d166Sitojun 				llsol.s6_addr8[12] = 0xff;
1225f4f4d166Sitojun 
1226d8a7e3a7Sitojun 				if (in6_addmulti(&llsol, ifp, &error)) {
1227d8a7e3a7Sitojun 					nd6log((LOG_ERR, "%s: failed to join "
1228d8a7e3a7Sitojun 					    "%s (errno=%d)\n", ifp->if_xname,
1229d8a7e3a7Sitojun 					    ip6_sprintf(&llsol), error));
1230d8a7e3a7Sitojun 				}
1231f4f4d166Sitojun 			}
1232287546eaSitojun 		}
1233287546eaSitojun 		break;
1234287546eaSitojun 
1235287546eaSitojun 	case RTM_DELETE:
1236287546eaSitojun 		if (!ln)
1237287546eaSitojun 			break;
1238f4f4d166Sitojun 		/* leave from solicited node multicast for proxy ND */
1239f4f4d166Sitojun 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1240f4f4d166Sitojun 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1241f4f4d166Sitojun 			struct in6_addr llsol;
1242f4f4d166Sitojun 			struct in6_multi *in6m;
1243f4f4d166Sitojun 
1244f4f4d166Sitojun 			llsol = SIN6(rt_key(rt))->sin6_addr;
1245f4f4d166Sitojun 			llsol.s6_addr16[0] = htons(0xff02);
1246f4f4d166Sitojun 			llsol.s6_addr16[1] = htons(ifp->if_index);
1247f4f4d166Sitojun 			llsol.s6_addr32[1] = 0;
1248f4f4d166Sitojun 			llsol.s6_addr32[2] = htonl(1);
1249f4f4d166Sitojun 			llsol.s6_addr8[12] = 0xff;
1250f4f4d166Sitojun 
1251f4f4d166Sitojun 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1252f4f4d166Sitojun 			if (in6m)
1253f4f4d166Sitojun 				in6_delmulti(in6m);
1254f4f4d166Sitojun 		}
1255287546eaSitojun 		nd6_inuse--;
1256287546eaSitojun 		ln->ln_next->ln_prev = ln->ln_prev;
1257287546eaSitojun 		ln->ln_prev->ln_next = ln->ln_next;
1258287546eaSitojun 		ln->ln_prev = NULL;
12599631a17bSitojun 		nd6_llinfo_settimer(ln, -1);
1260287546eaSitojun 		rt->rt_llinfo = 0;
1261287546eaSitojun 		rt->rt_flags &= ~RTF_LLINFO;
1262287546eaSitojun 		if (ln->ln_hold)
1263287546eaSitojun 			m_freem(ln->ln_hold);
1264287546eaSitojun 		Free((caddr_t)ln);
1265287546eaSitojun 	}
1266287546eaSitojun }
1267287546eaSitojun 
1268287546eaSitojun int
1269*ee37ea65Smcbride nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1270287546eaSitojun {
1271287546eaSitojun 	struct in6_drlist *drl = (struct in6_drlist *)data;
1272d8a7e3a7Sitojun 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1273287546eaSitojun 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1274287546eaSitojun 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1275f4f4d166Sitojun 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1276d8a7e3a7Sitojun 	struct nd_defrouter *dr;
1277287546eaSitojun 	struct nd_prefix *pr;
1278287546eaSitojun 	struct rtentry *rt;
1279287546eaSitojun 	int i = 0, error = 0;
1280287546eaSitojun 	int s;
1281287546eaSitojun 
1282287546eaSitojun 	switch (cmd) {
1283287546eaSitojun 	case SIOCGDRLST_IN6:
1284d8a7e3a7Sitojun 		/*
1285d8a7e3a7Sitojun 		 * obsolete API, use sysctl under net.inet6.icmp6
1286d8a7e3a7Sitojun 		 */
1287287546eaSitojun 		bzero(drl, sizeof(*drl));
12886e92dee6Sitojun 		s = splsoftnet();
1289f4f4d166Sitojun 		dr = TAILQ_FIRST(&nd_defrouter);
1290287546eaSitojun 		while (dr && i < DRLSTSIZ) {
1291287546eaSitojun 			drl->defrouter[i].rtaddr = dr->rtaddr;
1292287546eaSitojun 			if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1293287546eaSitojun 				/* XXX: need to this hack for KAME stack */
1294287546eaSitojun 				drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1295f6e55599Sitojun 			} else
1296287546eaSitojun 				log(LOG_ERR,
1297287546eaSitojun 				    "default router list contains a "
1298287546eaSitojun 				    "non-linklocal address(%s)\n",
1299287546eaSitojun 				    ip6_sprintf(&drl->defrouter[i].rtaddr));
1300287546eaSitojun 
1301287546eaSitojun 			drl->defrouter[i].flags = dr->flags;
1302287546eaSitojun 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1303287546eaSitojun 			drl->defrouter[i].expire = dr->expire;
1304287546eaSitojun 			drl->defrouter[i].if_index = dr->ifp->if_index;
1305287546eaSitojun 			i++;
1306f4f4d166Sitojun 			dr = TAILQ_NEXT(dr, dr_entry);
1307287546eaSitojun 		}
1308287546eaSitojun 		splx(s);
1309287546eaSitojun 		break;
1310287546eaSitojun 	case SIOCGPRLST_IN6:
1311f4f4d166Sitojun 		/*
1312d8a7e3a7Sitojun 		 * obsolete API, use sysctl under net.inet6.icmp6
1313d8a7e3a7Sitojun 		 *
1314d8a7e3a7Sitojun 		 * XXX the structure in6_prlist was changed in backward-
1315d8a7e3a7Sitojun 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1316d8a7e3a7Sitojun 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1317d8a7e3a7Sitojun 		 */
1318d8a7e3a7Sitojun 		/*
13198b542bbeSpascoe 		 * XXX meaning of fields, especially "raflags", is very
13208b542bbeSpascoe 		 * different between RA prefix list and RR/static prefix list.
1321f4f4d166Sitojun 		 * how about separating ioctls into two?
1322f4f4d166Sitojun 		 */
1323d8a7e3a7Sitojun 		bzero(oprl, sizeof(*oprl));
13246e92dee6Sitojun 		s = splsoftnet();
1325545205eaSpyr 		pr = LIST_FIRST(&nd_prefix);
1326287546eaSitojun 		while (pr && i < PRLSTSIZ) {
1327287546eaSitojun 			struct nd_pfxrouter *pfr;
1328287546eaSitojun 			int j;
1329287546eaSitojun 
1330d8a7e3a7Sitojun 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1331d8a7e3a7Sitojun 			oprl->prefix[i].raflags = pr->ndpr_raf;
1332d8a7e3a7Sitojun 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1333d8a7e3a7Sitojun 			oprl->prefix[i].vltime = pr->ndpr_vltime;
1334d8a7e3a7Sitojun 			oprl->prefix[i].pltime = pr->ndpr_pltime;
1335d8a7e3a7Sitojun 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1336d8a7e3a7Sitojun 			oprl->prefix[i].expire = pr->ndpr_expire;
1337287546eaSitojun 
1338545205eaSpyr 			pfr = LIST_FIRST(&pr->ndpr_advrtrs);
1339287546eaSitojun 			j = 0;
1340287546eaSitojun 			while(pfr) {
1341287546eaSitojun 				if (j < DRLSTSIZ) {
1342d8a7e3a7Sitojun #define RTRADDR oprl->prefix[i].advrtr[j]
1343287546eaSitojun 					RTRADDR = pfr->router->rtaddr;
1344287546eaSitojun 					if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1345287546eaSitojun 						/* XXX: hack for KAME */
1346287546eaSitojun 						RTRADDR.s6_addr16[1] = 0;
1347f6e55599Sitojun 					} else
1348287546eaSitojun 						log(LOG_ERR,
1349287546eaSitojun 						    "a router(%s) advertises "
1350287546eaSitojun 						    "a prefix with "
1351287546eaSitojun 						    "non-link local address\n",
1352287546eaSitojun 						    ip6_sprintf(&RTRADDR));
1353287546eaSitojun #undef RTRADDR
1354287546eaSitojun 				}
1355287546eaSitojun 				j++;
1356545205eaSpyr 				pfr = LIST_NEXT(pfr, pfr_entry);
1357287546eaSitojun 			}
1358d8a7e3a7Sitojun 			oprl->prefix[i].advrtrs = j;
1359d8a7e3a7Sitojun 			oprl->prefix[i].origin = PR_ORIG_RA;
1360287546eaSitojun 
1361287546eaSitojun 			i++;
1362545205eaSpyr 			pr = LIST_NEXT(pr, ndpr_entry);
1363287546eaSitojun 		}
1364f4f4d166Sitojun 		splx(s);
1365287546eaSitojun 
1366287546eaSitojun 		break;
1367d6b9e9b9Sitojun 	case OSIOCGIFINFO_IN6:
1368d6b9e9b9Sitojun 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1369191a775dSitojun 		bzero(&ndi->ndi, sizeof(ndi->ndi));
1370d6b9e9b9Sitojun 		ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1371d6b9e9b9Sitojun 		ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1372d6b9e9b9Sitojun 		ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1373d6b9e9b9Sitojun 		ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1374d6b9e9b9Sitojun 		ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1375d6b9e9b9Sitojun 		ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1376d6b9e9b9Sitojun 		ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1377d6b9e9b9Sitojun 		ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1378cfab95f2Sitojun 		break;
1379d6b9e9b9Sitojun 	case SIOCGIFINFO_IN6:
1380d6b9e9b9Sitojun 		ndi->ndi = *ND_IFINFO(ifp);
1381287546eaSitojun 		break;
1382d374aaacSitojun 	case SIOCSIFINFO_FLAGS:
1383d6b9e9b9Sitojun 		ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1384d374aaacSitojun 		break;
1385f4f4d166Sitojun 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1386d8a7e3a7Sitojun 		/* sync kernel routing table with the default router list */
1387d8a7e3a7Sitojun 		defrouter_reset();
1388f4f4d166Sitojun 		defrouter_select();
1389287546eaSitojun 		break;
1390287546eaSitojun 	case SIOCSPFXFLUSH_IN6:
1391287546eaSitojun 	{
1392287546eaSitojun 		/* flush all the prefix advertised by routers */
1393287546eaSitojun 		struct nd_prefix *pr, *next;
1394287546eaSitojun 
13956e92dee6Sitojun 		s = splsoftnet();
1396545205eaSpyr 		for (pr = LIST_FIRST(&nd_prefix); pr; pr = next) {
1397d8a7e3a7Sitojun 			struct in6_ifaddr *ia, *ia_next;
1398d8a7e3a7Sitojun 
1399545205eaSpyr 			next = LIST_NEXT(pr, ndpr_entry);
1400d8a7e3a7Sitojun 
1401d8a7e3a7Sitojun 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1402d8a7e3a7Sitojun 				continue; /* XXX */
1403d8a7e3a7Sitojun 
1404d8a7e3a7Sitojun 			/* do we really have to remove addresses as well? */
1405d8a7e3a7Sitojun 			for (ia = in6_ifaddr; ia; ia = ia_next) {
1406d8a7e3a7Sitojun 				/* ia might be removed.  keep the next ptr. */
1407d8a7e3a7Sitojun 				ia_next = ia->ia_next;
1408d8a7e3a7Sitojun 
1409d8a7e3a7Sitojun 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1410d8a7e3a7Sitojun 					continue;
1411d8a7e3a7Sitojun 
1412d8a7e3a7Sitojun 				if (ia->ia6_ndpr == pr)
1413d8a7e3a7Sitojun 					in6_purgeaddr(&ia->ia_ifa);
1414d8a7e3a7Sitojun 			}
1415287546eaSitojun 			prelist_remove(pr);
1416287546eaSitojun 		}
1417287546eaSitojun 		splx(s);
1418287546eaSitojun 		break;
1419287546eaSitojun 	}
1420287546eaSitojun 	case SIOCSRTRFLUSH_IN6:
1421287546eaSitojun 	{
1422287546eaSitojun 		/* flush all the default routers */
1423287546eaSitojun 		struct nd_defrouter *dr, *next;
1424287546eaSitojun 
14256e92dee6Sitojun 		s = splsoftnet();
1426d8a7e3a7Sitojun 		defrouter_reset();
1427d8a7e3a7Sitojun 		for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = next) {
1428f4f4d166Sitojun 			next = TAILQ_NEXT(dr, dr_entry);
1429287546eaSitojun 			defrtrlist_del(dr);
1430287546eaSitojun 		}
1431d8a7e3a7Sitojun 		defrouter_select();
1432287546eaSitojun 		splx(s);
1433287546eaSitojun 		break;
1434287546eaSitojun 	}
1435287546eaSitojun 	case SIOCGNBRINFO_IN6:
1436287546eaSitojun 	{
1437287546eaSitojun 		struct llinfo_nd6 *ln;
1438287546eaSitojun 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1439287546eaSitojun 
1440287546eaSitojun 		/*
1441287546eaSitojun 		 * XXX: KAME specific hack for scoped addresses
1442287546eaSitojun 		 *      XXXX: for other scopes than link-local?
1443287546eaSitojun 		 */
1444287546eaSitojun 		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1445287546eaSitojun 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1446287546eaSitojun 			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1447287546eaSitojun 
1448287546eaSitojun 			if (*idp == 0)
1449287546eaSitojun 				*idp = htons(ifp->if_index);
1450287546eaSitojun 		}
1451287546eaSitojun 
14526e92dee6Sitojun 		s = splsoftnet();
1453d8a7e3a7Sitojun 		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1454d8a7e3a7Sitojun 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1455287546eaSitojun 			error = EINVAL;
145691e69634Sitojun 			splx(s);
1457287546eaSitojun 			break;
1458287546eaSitojun 		}
1459287546eaSitojun 		nbi->state = ln->ln_state;
1460287546eaSitojun 		nbi->asked = ln->ln_asked;
1461287546eaSitojun 		nbi->isrouter = ln->ln_router;
1462287546eaSitojun 		nbi->expire = ln->ln_expire;
1463287546eaSitojun 		splx(s);
1464287546eaSitojun 
1465287546eaSitojun 		break;
1466287546eaSitojun 	}
1467f4f4d166Sitojun 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1468f4f4d166Sitojun 		ndif->ifindex = nd6_defifindex;
1469f4f4d166Sitojun 		break;
1470f4f4d166Sitojun 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1471f4f4d166Sitojun 		return (nd6_setdefaultiface(ndif->ifindex));
1472f4f4d166Sitojun 		break;
1473287546eaSitojun 	}
1474287546eaSitojun 	return (error);
1475287546eaSitojun }
1476287546eaSitojun 
1477287546eaSitojun /*
1478287546eaSitojun  * Create neighbor cache entry and cache link-layer address,
1479287546eaSitojun  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1480*ee37ea65Smcbride  *
1481*ee37ea65Smcbride  * type - ICMP6 type
1482*ee37ea65Smcbride  * code - type dependent information
1483287546eaSitojun  */
1484287546eaSitojun struct rtentry *
1485*ee37ea65Smcbride nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1486*ee37ea65Smcbride 	int lladdrlen, int type, int code)
1487287546eaSitojun {
1488287546eaSitojun 	struct rtentry *rt = NULL;
1489287546eaSitojun 	struct llinfo_nd6 *ln = NULL;
1490287546eaSitojun 	int is_newentry;
1491287546eaSitojun 	struct sockaddr_dl *sdl = NULL;
1492287546eaSitojun 	int do_update;
1493287546eaSitojun 	int olladdr;
1494287546eaSitojun 	int llchange;
1495287546eaSitojun 	int newstate = 0;
1496287546eaSitojun 
1497287546eaSitojun 	if (!ifp)
1498287546eaSitojun 		panic("ifp == NULL in nd6_cache_lladdr");
1499287546eaSitojun 	if (!from)
1500287546eaSitojun 		panic("from == NULL in nd6_cache_lladdr");
1501287546eaSitojun 
1502287546eaSitojun 	/* nothing must be updated for unspecified address */
1503287546eaSitojun 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1504287546eaSitojun 		return NULL;
1505287546eaSitojun 
1506287546eaSitojun 	/*
1507287546eaSitojun 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1508287546eaSitojun 	 * the caller.
1509287546eaSitojun 	 *
15108b542bbeSpascoe 	 * XXX If the link does not have link-layer address, what should
1511287546eaSitojun 	 * we do? (ifp->if_addrlen == 0)
1512287546eaSitojun 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1513287546eaSitojun 	 * description on it in NS section (RFC 2461 7.2.3).
1514287546eaSitojun 	 */
1515287546eaSitojun 
1516287546eaSitojun 	rt = nd6_lookup(from, 0, ifp);
1517287546eaSitojun 	if (!rt) {
1518287546eaSitojun #if 0
1519287546eaSitojun 		/* nothing must be done if there's no lladdr */
1520287546eaSitojun 		if (!lladdr || !lladdrlen)
1521287546eaSitojun 			return NULL;
1522287546eaSitojun #endif
1523287546eaSitojun 
1524287546eaSitojun 		rt = nd6_lookup(from, 1, ifp);
1525287546eaSitojun 		is_newentry = 1;
15260a2c5741Sitojun 	} else {
15270a2c5741Sitojun 		/* do nothing if static ndp is set */
15280a2c5741Sitojun 		if (rt->rt_flags & RTF_STATIC)
15290a2c5741Sitojun 			return NULL;
1530287546eaSitojun 		is_newentry = 0;
15310a2c5741Sitojun 	}
1532287546eaSitojun 
1533287546eaSitojun 	if (!rt)
1534287546eaSitojun 		return NULL;
1535287546eaSitojun 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1536287546eaSitojun fail:
1537d8a7e3a7Sitojun 		(void)nd6_free(rt, 0);
1538287546eaSitojun 		return NULL;
1539287546eaSitojun 	}
1540287546eaSitojun 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1541287546eaSitojun 	if (!ln)
1542287546eaSitojun 		goto fail;
1543287546eaSitojun 	if (!rt->rt_gateway)
1544287546eaSitojun 		goto fail;
1545287546eaSitojun 	if (rt->rt_gateway->sa_family != AF_LINK)
1546287546eaSitojun 		goto fail;
1547287546eaSitojun 	sdl = SDL(rt->rt_gateway);
1548287546eaSitojun 
1549287546eaSitojun 	olladdr = (sdl->sdl_alen) ? 1 : 0;
1550287546eaSitojun 	if (olladdr && lladdr) {
1551287546eaSitojun 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1552287546eaSitojun 			llchange = 1;
1553287546eaSitojun 		else
1554287546eaSitojun 			llchange = 0;
1555287546eaSitojun 	} else
1556287546eaSitojun 		llchange = 0;
1557287546eaSitojun 
1558287546eaSitojun 	/*
1559287546eaSitojun 	 * newentry olladdr  lladdr  llchange	(*=record)
1560287546eaSitojun 	 *	0	n	n	--	(1)
1561287546eaSitojun 	 *	0	y	n	--	(2)
1562287546eaSitojun 	 *	0	n	y	--	(3) * STALE
1563287546eaSitojun 	 *	0	y	y	n	(4) *
1564287546eaSitojun 	 *	0	y	y	y	(5) * STALE
1565287546eaSitojun 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1566287546eaSitojun 	 *	1	--	y	--	(7) * STALE
1567287546eaSitojun 	 */
1568287546eaSitojun 
1569287546eaSitojun 	if (lladdr) {		/* (3-5) and (7) */
1570287546eaSitojun 		/*
1571287546eaSitojun 		 * Record source link-layer address
1572287546eaSitojun 		 * XXX is it dependent to ifp->if_type?
1573287546eaSitojun 		 */
1574287546eaSitojun 		sdl->sdl_alen = ifp->if_addrlen;
1575287546eaSitojun 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1576287546eaSitojun 	}
1577287546eaSitojun 
1578287546eaSitojun 	if (!is_newentry) {
1579d8a7e3a7Sitojun 		if ((!olladdr && lladdr) ||		/* (3) */
1580d8a7e3a7Sitojun 		    (olladdr && lladdr && llchange)) {	/* (5) */
1581287546eaSitojun 			do_update = 1;
1582287546eaSitojun 			newstate = ND6_LLINFO_STALE;
1583287546eaSitojun 		} else					/* (1-2,4) */
1584287546eaSitojun 			do_update = 0;
1585287546eaSitojun 	} else {
1586287546eaSitojun 		do_update = 1;
1587287546eaSitojun 		if (!lladdr)				/* (6) */
1588287546eaSitojun 			newstate = ND6_LLINFO_NOSTATE;
1589287546eaSitojun 		else					/* (7) */
1590287546eaSitojun 			newstate = ND6_LLINFO_STALE;
1591287546eaSitojun 	}
1592287546eaSitojun 
1593287546eaSitojun 	if (do_update) {
1594287546eaSitojun 		/*
1595287546eaSitojun 		 * Update the state of the neighbor cache.
1596287546eaSitojun 		 */
1597287546eaSitojun 		ln->ln_state = newstate;
1598287546eaSitojun 
1599287546eaSitojun 		if (ln->ln_state == ND6_LLINFO_STALE) {
16008a7bb304Sitojun 			/*
16018a7bb304Sitojun 			 * XXX: since nd6_output() below will cause
16028b542bbeSpascoe 			 * state transition to DELAY and reset the timer,
16038a7bb304Sitojun 			 * we must set the timer now, although it is actually
16048a7bb304Sitojun 			 * meaningless.
16058a7bb304Sitojun 			 */
16069631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
16078a7bb304Sitojun 
1608287546eaSitojun 			if (ln->ln_hold) {
1609e212adedSkrw 				struct mbuf *n = ln->ln_hold;
1610e212adedSkrw 				ln->ln_hold = NULL;
16116afad192Sitojun 				/*
16126afad192Sitojun 				 * we assume ifp is not a p2p here, so just
16136afad192Sitojun 				 * set the 2nd argument as the 1st one.
16146afad192Sitojun 				 */
1615e212adedSkrw 				nd6_output(ifp, ifp, n,
1616d8a7e3a7Sitojun 				    (struct sockaddr_in6 *)rt_key(rt), rt);
1617e212adedSkrw 				if (ln->ln_hold == n) {
1618e212adedSkrw 					/* n is back in ln_hold. Discard. */
1619e212adedSkrw 					m_freem(ln->ln_hold);
16208a7bb304Sitojun 					ln->ln_hold = NULL;
1621287546eaSitojun 				}
1622e212adedSkrw 			}
1623287546eaSitojun 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1624287546eaSitojun 			/* probe right away */
16259631a17bSitojun 			nd6_llinfo_settimer((void *)ln, 0);
1626287546eaSitojun 		}
1627287546eaSitojun 	}
1628287546eaSitojun 
1629287546eaSitojun 	/*
1630287546eaSitojun 	 * ICMP6 type dependent behavior.
1631287546eaSitojun 	 *
1632287546eaSitojun 	 * NS: clear IsRouter if new entry
1633287546eaSitojun 	 * RS: clear IsRouter
1634287546eaSitojun 	 * RA: set IsRouter if there's lladdr
1635287546eaSitojun 	 * redir: clear IsRouter if new entry
1636287546eaSitojun 	 *
1637287546eaSitojun 	 * RA case, (1):
1638287546eaSitojun 	 * The spec says that we must set IsRouter in the following cases:
1639287546eaSitojun 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1640287546eaSitojun 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1641287546eaSitojun 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
16428b542bbeSpascoe 	 * A question arises for (1) case.  (1) case has no lladdr in the
1643287546eaSitojun 	 * neighbor cache, this is similar to (6).
1644287546eaSitojun 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1645287546eaSitojun 	 *
1646287546eaSitojun 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1647287546eaSitojun 	 *							D R
1648287546eaSitojun 	 *	0	n	n	--	(1)	c   ?     s
1649287546eaSitojun 	 *	0	y	n	--	(2)	c   s     s
1650287546eaSitojun 	 *	0	n	y	--	(3)	c   s     s
1651287546eaSitojun 	 *	0	y	y	n	(4)	c   s     s
1652287546eaSitojun 	 *	0	y	y	y	(5)	c   s     s
1653287546eaSitojun 	 *	1	--	n	--	(6) c	c 	c s
1654287546eaSitojun 	 *	1	--	y	--	(7) c	c   s	c s
1655287546eaSitojun 	 *
1656287546eaSitojun 	 *					(c=clear s=set)
1657287546eaSitojun 	 */
1658287546eaSitojun 	switch (type & 0xff) {
1659287546eaSitojun 	case ND_NEIGHBOR_SOLICIT:
1660287546eaSitojun 		/*
1661287546eaSitojun 		 * New entry must have is_router flag cleared.
1662287546eaSitojun 		 */
1663287546eaSitojun 		if (is_newentry)	/* (6-7) */
1664287546eaSitojun 			ln->ln_router = 0;
1665287546eaSitojun 		break;
1666287546eaSitojun 	case ND_REDIRECT:
1667287546eaSitojun 		/*
1668287546eaSitojun 		 * If the icmp is a redirect to a better router, always set the
1669287546eaSitojun 		 * is_router flag.  Otherwise, if the entry is newly created,
1670287546eaSitojun 		 * clear the flag.  [RFC 2461, sec 8.3]
1671287546eaSitojun 		 */
1672287546eaSitojun 		if (code == ND_REDIRECT_ROUTER)
1673287546eaSitojun 			ln->ln_router = 1;
1674287546eaSitojun 		else if (is_newentry) /* (6-7) */
1675287546eaSitojun 			ln->ln_router = 0;
1676287546eaSitojun 		break;
1677287546eaSitojun 	case ND_ROUTER_SOLICIT:
1678287546eaSitojun 		/*
1679287546eaSitojun 		 * is_router flag must always be cleared.
1680287546eaSitojun 		 */
1681287546eaSitojun 		ln->ln_router = 0;
1682287546eaSitojun 		break;
1683287546eaSitojun 	case ND_ROUTER_ADVERT:
1684287546eaSitojun 		/*
1685287546eaSitojun 		 * Mark an entry with lladdr as a router.
1686287546eaSitojun 		 */
1687d8a7e3a7Sitojun 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1688d8a7e3a7Sitojun 		    (is_newentry && lladdr)) {			/* (7) */
1689287546eaSitojun 			ln->ln_router = 1;
1690287546eaSitojun 		}
1691287546eaSitojun 		break;
1692287546eaSitojun 	}
1693287546eaSitojun 
169450ccc024Sitojun 	/*
169550ccc024Sitojun 	 * When the link-layer address of a router changes, select the
169650ccc024Sitojun 	 * best router again.  In particular, when the neighbor entry is newly
169750ccc024Sitojun 	 * created, it might affect the selection policy.
169850ccc024Sitojun 	 * Question: can we restrict the first condition to the "is_newentry"
169950ccc024Sitojun 	 * case?
170050ccc024Sitojun 	 * XXX: when we hear an RA from a new router with the link-layer
170150ccc024Sitojun 	 * address option, defrouter_select() is called twice, since
170250ccc024Sitojun 	 * defrtrlist_update called the function as well.  However, I believe
170350ccc024Sitojun 	 * we can compromise the overhead, since it only happens the first
170450ccc024Sitojun 	 * time.
1705d8a7e3a7Sitojun 	 * XXX: although defrouter_select() should not have a bad effect
1706d8a7e3a7Sitojun 	 * for those are not autoconfigured hosts, we explicitly avoid such
1707d8a7e3a7Sitojun 	 * cases for safety.
170850ccc024Sitojun 	 */
170929ee75acSitojun 	if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
171050ccc024Sitojun 		defrouter_select();
171150ccc024Sitojun 
1712287546eaSitojun 	return rt;
1713287546eaSitojun }
1714287546eaSitojun 
1715287546eaSitojun static void
1716*ee37ea65Smcbride nd6_slowtimo(void *ignored_arg)
1717287546eaSitojun {
17186e92dee6Sitojun 	int s = splsoftnet();
1719b3c1e4c1Sitojun 	struct nd_ifinfo *nd6if;
1720d6b9e9b9Sitojun 	struct ifnet *ifp;
1721287546eaSitojun 
1722b3c1e4c1Sitojun 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
1723b3c1e4c1Sitojun 	timeout_add(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz);
1724d6b9e9b9Sitojun 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1725d6b9e9b9Sitojun 	{
1726d6b9e9b9Sitojun 		nd6if = ND_IFINFO(ifp);
1727287546eaSitojun 		if (nd6if->basereachable && /* already initialized */
1728287546eaSitojun 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1729287546eaSitojun 			/*
1730287546eaSitojun 			 * Since reachable time rarely changes by router
1731287546eaSitojun 			 * advertisements, we SHOULD insure that a new random
1732287546eaSitojun 			 * value gets recomputed at least once every few hours.
1733287546eaSitojun 			 * (RFC 2461, 6.3.4)
1734287546eaSitojun 			 */
1735287546eaSitojun 			nd6if->recalctm = nd6_recalc_reachtm_interval;
1736287546eaSitojun 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1737287546eaSitojun 		}
1738287546eaSitojun 	}
1739287546eaSitojun 	splx(s);
1740287546eaSitojun }
1741287546eaSitojun 
1742287546eaSitojun #define senderr(e) { error = (e); goto bad;}
1743287546eaSitojun int
1744*ee37ea65Smcbride nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1745*ee37ea65Smcbride 	struct sockaddr_in6 *dst, struct rtentry *rt0)
1746287546eaSitojun {
1747b3c1e4c1Sitojun 	struct mbuf *m = m0;
1748b3c1e4c1Sitojun 	struct rtentry *rt = rt0;
1749cfb6b8dfSitojun 	struct sockaddr_in6 *gw6 = NULL;
1750287546eaSitojun 	struct llinfo_nd6 *ln = NULL;
1751287546eaSitojun 	int error = 0;
1752cb39d30aSangelos #ifdef IPSEC
1753cb39d30aSangelos 	struct m_tag *mtag;
1754cb39d30aSangelos #endif /* IPSEC */
1755287546eaSitojun 
1756287546eaSitojun 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1757287546eaSitojun 		goto sendpkt;
1758287546eaSitojun 
1759d8a7e3a7Sitojun 	if (nd6_need_cache(ifp) == 0)
1760287546eaSitojun 		goto sendpkt;
1761287546eaSitojun 
1762287546eaSitojun 	/*
17638b542bbeSpascoe 	 * next hop determination.  This routine is derived from ether_output.
1764287546eaSitojun 	 */
1765287546eaSitojun 	if (rt) {
1766287546eaSitojun 		if ((rt->rt_flags & RTF_UP) == 0) {
1767d8a7e3a7Sitojun 			if ((rt0 = rt = rtalloc1((struct sockaddr *)dst,
1768c241aca8Shenning 			    1, 0)) != NULL)
1769287546eaSitojun 			{
1770287546eaSitojun 				rt->rt_refcnt--;
1771fe16f6efSitojun 				if (rt->rt_ifp != ifp)
1772fe16f6efSitojun 					senderr(EHOSTUNREACH);
1773287546eaSitojun 			} else
1774287546eaSitojun 				senderr(EHOSTUNREACH);
1775287546eaSitojun 		}
1776cfb6b8dfSitojun 
1777287546eaSitojun 		if (rt->rt_flags & RTF_GATEWAY) {
1778cfb6b8dfSitojun 			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1779cfb6b8dfSitojun 
1780cfb6b8dfSitojun 			/*
1781cfb6b8dfSitojun 			 * We skip link-layer address resolution and NUD
1782cfb6b8dfSitojun 			 * if the gateway is not a neighbor from ND point
1783d8a7e3a7Sitojun 			 * of view, regardless of the value of nd_ifinfo.flags.
178434deef1eSitojun 			 * The second condition is a bit tricky; we skip
1785cfb6b8dfSitojun 			 * if the gateway is our own address, which is
1786cfb6b8dfSitojun 			 * sometimes used to install a route to a p2p link.
1787cfb6b8dfSitojun 			 */
1788cfb6b8dfSitojun 			if (!nd6_is_addr_neighbor(gw6, ifp) ||
1789cfb6b8dfSitojun 			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1790cfb6b8dfSitojun 				/*
1791cfb6b8dfSitojun 				 * We allow this kind of tricky route only
1792cfb6b8dfSitojun 				 * when the outgoing interface is p2p.
1793cfb6b8dfSitojun 				 * XXX: we may need a more generic rule here.
1794cfb6b8dfSitojun 				 */
1795cfb6b8dfSitojun 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1796cfb6b8dfSitojun 					senderr(EHOSTUNREACH);
1797cfb6b8dfSitojun 
1798cfb6b8dfSitojun 				goto sendpkt;
1799cfb6b8dfSitojun 			}
1800cfb6b8dfSitojun 
1801287546eaSitojun 			if (rt->rt_gwroute == 0)
1802287546eaSitojun 				goto lookup;
1803287546eaSitojun 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1804287546eaSitojun 				rtfree(rt); rt = rt0;
1805d8a7e3a7Sitojun 			lookup:
1806c241aca8Shenning 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0);
1807287546eaSitojun 				if ((rt = rt->rt_gwroute) == 0)
1808287546eaSitojun 					senderr(EHOSTUNREACH);
1809287546eaSitojun 			}
1810287546eaSitojun 		}
1811287546eaSitojun 	}
1812287546eaSitojun 
1813287546eaSitojun 	/*
1814287546eaSitojun 	 * Address resolution or Neighbor Unreachability Detection
1815287546eaSitojun 	 * for the next hop.
1816287546eaSitojun 	 * At this point, the destination of the packet must be a unicast
1817287546eaSitojun 	 * or an anycast address(i.e. not a multicast).
1818287546eaSitojun 	 */
1819287546eaSitojun 
1820287546eaSitojun 	/* Look up the neighbor cache for the nexthop */
1821287546eaSitojun 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1822287546eaSitojun 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1823287546eaSitojun 	else {
1824cfb6b8dfSitojun 		/*
1825cfb6b8dfSitojun 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1826cfb6b8dfSitojun 		 * the condition below is not very efficient.  But we believe
1827cfb6b8dfSitojun 		 * it is tolerable, because this should be a rare case.
1828cfb6b8dfSitojun 		 */
1829cfb6b8dfSitojun 		if (nd6_is_addr_neighbor(dst, ifp) &&
1830cfb6b8dfSitojun 		    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1831287546eaSitojun 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1832287546eaSitojun 	}
1833287546eaSitojun 	if (!ln || !rt) {
1834cfb6b8dfSitojun 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1835d6b9e9b9Sitojun 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1836cfb6b8dfSitojun 			log(LOG_DEBUG,
1837cfb6b8dfSitojun 			    "nd6_output: can't allocate llinfo for %s "
1838287546eaSitojun 			    "(ln=%p, rt=%p)\n",
1839287546eaSitojun 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
1840287546eaSitojun 			senderr(EIO);	/* XXX: good error? */
1841287546eaSitojun 		}
1842287546eaSitojun 
1843cfb6b8dfSitojun 		goto sendpkt;	/* send anyway */
1844cfb6b8dfSitojun 	}
1845cfb6b8dfSitojun 
1846f3fcf2f3Smcbride 	/*
1847f3fcf2f3Smcbride 	 * Move this entry to the head of the queue so that it is less likely
1848f3fcf2f3Smcbride 	 * for this entry to be a target of forced garbage collection (see
1849f3fcf2f3Smcbride 	 * nd6_rtrequest()).
1850f3fcf2f3Smcbride 	 */
1851f3fcf2f3Smcbride 	LN_DEQUEUE(ln);
1852f3fcf2f3Smcbride 	LN_INSERTHEAD(ln);
1853f3fcf2f3Smcbride 
1854d374aaacSitojun 	/* We don't have to do link-layer address resolution on a p2p link. */
1855d374aaacSitojun 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1856be4e9e12Sitojun 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
1857d374aaacSitojun 		ln->ln_state = ND6_LLINFO_STALE;
18589631a17bSitojun 		nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1859be4e9e12Sitojun 	}
1860287546eaSitojun 
1861287546eaSitojun 	/*
1862287546eaSitojun 	 * The first time we send a packet to a neighbor whose entry is
1863287546eaSitojun 	 * STALE, we have to change the state to DELAY and a sets a timer to
1864287546eaSitojun 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1865287546eaSitojun 	 * neighbor unreachability detection on expiration.
1866287546eaSitojun 	 * (RFC 2461 7.3.3)
1867287546eaSitojun 	 */
1868287546eaSitojun 	if (ln->ln_state == ND6_LLINFO_STALE) {
1869287546eaSitojun 		ln->ln_asked = 0;
1870287546eaSitojun 		ln->ln_state = ND6_LLINFO_DELAY;
18719631a17bSitojun 		nd6_llinfo_settimer(ln, nd6_delay * hz);
1872287546eaSitojun 	}
1873287546eaSitojun 
1874287546eaSitojun 	/*
1875287546eaSitojun 	 * If the neighbor cache entry has a state other than INCOMPLETE
187634deef1eSitojun 	 * (i.e. its link-layer address is already resolved), just
1877287546eaSitojun 	 * send the packet.
1878287546eaSitojun 	 */
1879287546eaSitojun 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1880287546eaSitojun 		goto sendpkt;
1881287546eaSitojun 
1882287546eaSitojun 	/*
1883287546eaSitojun 	 * There is a neighbor cache entry, but no ethernet address
1884287546eaSitojun 	 * response yet.  Replace the held mbuf (if any) with this
1885287546eaSitojun 	 * latest one.
1886287546eaSitojun 	 */
1887efcf292bSitojun 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1888287546eaSitojun 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1889287546eaSitojun 	if (ln->ln_hold)
1890287546eaSitojun 		m_freem(ln->ln_hold);
1891287546eaSitojun 	ln->ln_hold = m;
189276843262Sitojun 	/*
189376843262Sitojun 	 * If there has been no NS for the neighbor after entering the
189476843262Sitojun 	 * INCOMPLETE state, send the first solicitation.
189576843262Sitojun 	 */
18969631a17bSitojun 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
1897287546eaSitojun 		ln->ln_asked++;
18989631a17bSitojun 		nd6_llinfo_settimer(ln,
18999631a17bSitojun 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1900287546eaSitojun 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1901287546eaSitojun 	}
1902287546eaSitojun 	return (0);
1903287546eaSitojun 
1904287546eaSitojun   sendpkt:
1905cb39d30aSangelos #ifdef IPSEC
1906cb39d30aSangelos 	/*
1907cb39d30aSangelos 	 * If the packet needs outgoing IPsec crypto processing and the
1908cb39d30aSangelos 	 * interface doesn't support it, drop it.
1909cb39d30aSangelos 	 */
1910cb39d30aSangelos 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
1911cb39d30aSangelos #endif /* IPSEC */
19126afad192Sitojun 
19130a2c5741Sitojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1914cb39d30aSangelos #ifdef IPSEC
1915cb39d30aSangelos 		if (mtag != NULL &&
1916cb39d30aSangelos 		    (origifp->if_capabilities & IFCAP_IPSEC) == 0) {
1917cb39d30aSangelos 			/* Tell IPsec to do its own crypto. */
1918cb39d30aSangelos 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
1919cb39d30aSangelos 			error = EACCES;
1920cb39d30aSangelos 			goto bad;
1921cb39d30aSangelos 		}
1922cb39d30aSangelos #endif /* IPSEC */
19236afad192Sitojun 		return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
19246afad192Sitojun 		    rt));
19256afad192Sitojun 	}
1926cb39d30aSangelos #ifdef IPSEC
1927cb39d30aSangelos 	if (mtag != NULL &&
1928cb39d30aSangelos 	    (ifp->if_capabilities & IFCAP_IPSEC) == 0) {
1929cb39d30aSangelos 		/* Tell IPsec to do its own crypto. */
1930cb39d30aSangelos 		ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
1931cb39d30aSangelos 		error = EACCES;
1932cb39d30aSangelos 		goto bad;
1933cb39d30aSangelos 	}
1934cb39d30aSangelos #endif /* IPSEC */
1935287546eaSitojun 	return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1936287546eaSitojun 
1937287546eaSitojun   bad:
1938287546eaSitojun 	if (m)
1939287546eaSitojun 		m_freem(m);
1940287546eaSitojun 	return (error);
1941287546eaSitojun }
1942287546eaSitojun #undef senderr
1943287546eaSitojun 
1944287546eaSitojun int
1945*ee37ea65Smcbride nd6_need_cache(struct ifnet *ifp)
1946d8a7e3a7Sitojun {
1947d8a7e3a7Sitojun 	/*
1948d8a7e3a7Sitojun 	 * XXX: we currently do not make neighbor cache on any interface
194940765843Shenning 	 * other than Ethernet, FDDI and GIF.
1950d8a7e3a7Sitojun 	 *
1951d8a7e3a7Sitojun 	 * RFC2893 says:
1952d8a7e3a7Sitojun 	 * - unidirectional tunnels needs no ND
1953d8a7e3a7Sitojun 	 */
1954d8a7e3a7Sitojun 	switch (ifp->if_type) {
1955d8a7e3a7Sitojun 	case IFT_ETHER:
1956d8a7e3a7Sitojun 	case IFT_FDDI:
1957d8a7e3a7Sitojun 	case IFT_IEEE1394:
1958d8a7e3a7Sitojun 	case IFT_PROPVIRTUAL:
1959d8a7e3a7Sitojun 	case IFT_L2VLAN:
1960d8a7e3a7Sitojun 	case IFT_IEEE80211:
1961f4433d56Shenning 	case IFT_CARP:
1962d8a7e3a7Sitojun 	case IFT_GIF:		/* XXX need more cases? */
1963d8a7e3a7Sitojun 		return (1);
1964d8a7e3a7Sitojun 	default:
1965d8a7e3a7Sitojun 		return (0);
1966d8a7e3a7Sitojun 	}
1967d8a7e3a7Sitojun }
1968d8a7e3a7Sitojun 
1969d8a7e3a7Sitojun int
1970*ee37ea65Smcbride nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
1971*ee37ea65Smcbride 	struct sockaddr *dst, u_char *desten)
1972287546eaSitojun {
1973287546eaSitojun 	struct sockaddr_dl *sdl;
1974287546eaSitojun 
1975287546eaSitojun 	if (m->m_flags & M_MCAST) {
1976287546eaSitojun 		switch (ifp->if_type) {
1977287546eaSitojun 		case IFT_ETHER:
1978287546eaSitojun 		case IFT_FDDI:
1979287546eaSitojun 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1980287546eaSitojun 						 desten);
1981287546eaSitojun 			return (1);
1982287546eaSitojun 			break;
1983287546eaSitojun 		default:
19840a2c5741Sitojun 			m_freem(m);
1985287546eaSitojun 			return (0);
1986287546eaSitojun 		}
1987287546eaSitojun 	}
1988287546eaSitojun 
198972e30262Sitojun 	if (rt == NULL) {
199072e30262Sitojun 		/* this could happen, if we could not allocate memory */
19910a2c5741Sitojun 		m_freem(m);
199272e30262Sitojun 		return (0);
199372e30262Sitojun 	}
199472e30262Sitojun 	if (rt->rt_gateway->sa_family != AF_LINK) {
1995287546eaSitojun 		printf("nd6_storelladdr: something odd happens\n");
19960a2c5741Sitojun 		m_freem(m);
1997287546eaSitojun 		return (0);
1998287546eaSitojun 	}
1999287546eaSitojun 	sdl = SDL(rt->rt_gateway);
2000b4ee66fcSitojun 	if (sdl->sdl_alen == 0) {
2001bd2806bcSitojun 		/* this should be impossible, but we bark here for debugging */
2002d8a7e3a7Sitojun 		printf("nd6_storelladdr: sdl_alen == 0, dst=%s, if=%s\n",
2003d8a7e3a7Sitojun 		    ip6_sprintf(&SIN6(dst)->sin6_addr), ifp->if_xname);
20040a2c5741Sitojun 		m_freem(m);
2005b4ee66fcSitojun 		return (0);
2006b4ee66fcSitojun 	}
2007287546eaSitojun 
2008b4ee66fcSitojun 	bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2009287546eaSitojun 	return (1);
2010287546eaSitojun }
2011d8a7e3a7Sitojun 
2012*ee37ea65Smcbride /*
2013*ee37ea65Smcbride  * oldp - syscall arg, need copyout
2014*ee37ea65Smcbride  * newp - syscall arg, need copyin
2015*ee37ea65Smcbride  */
2016*ee37ea65Smcbride 
2017d8a7e3a7Sitojun int
2018*ee37ea65Smcbride nd6_sysctl(int name, void *oldp, size_t *oldlenp, void *newp,
2019*ee37ea65Smcbride 	size_t newlen)
2020d8a7e3a7Sitojun {
2021db3db419Sitojun 	void *p;
2022d8a7e3a7Sitojun 	size_t ol, l;
2023d8a7e3a7Sitojun 	int error;
2024d8a7e3a7Sitojun 
2025d8a7e3a7Sitojun 	error = 0;
2026d8a7e3a7Sitojun 	l = 0;
2027d8a7e3a7Sitojun 
2028d8a7e3a7Sitojun 	if (newp)
2029d8a7e3a7Sitojun 		return EPERM;
2030d8a7e3a7Sitojun 	if (oldp && !oldlenp)
2031d8a7e3a7Sitojun 		return EINVAL;
2032d8a7e3a7Sitojun 	ol = oldlenp ? *oldlenp : 0;
2033d8a7e3a7Sitojun 
2034db3db419Sitojun 	if (oldp) {
2035db3db419Sitojun 		p = malloc(*oldlenp, M_TEMP, M_WAITOK);
2036db3db419Sitojun 		if (!p)
2037db3db419Sitojun 			return ENOMEM;
2038db3db419Sitojun 	} else
2039db3db419Sitojun 		p = NULL;
2040d8a7e3a7Sitojun 	switch (name) {
2041d8a7e3a7Sitojun 	case ICMPV6CTL_ND6_DRLIST:
2042db3db419Sitojun 		error = fill_drlist(p, oldlenp, ol);
2043db3db419Sitojun 		if (!error && p && oldp)
2044732568ffSitojun 			error = copyout(p, oldp, *oldlenp);
2045d8a7e3a7Sitojun 		break;
2046d8a7e3a7Sitojun 
2047d8a7e3a7Sitojun 	case ICMPV6CTL_ND6_PRLIST:
2048db3db419Sitojun 		error = fill_prlist(p, oldlenp, ol);
2049db3db419Sitojun 		if (!error && p && oldp)
2050732568ffSitojun 			error = copyout(p, oldp, *oldlenp);
2051d8a7e3a7Sitojun 		break;
2052d8a7e3a7Sitojun 
2053d8a7e3a7Sitojun 	default:
2054d8a7e3a7Sitojun 		error = ENOPROTOOPT;
2055d8a7e3a7Sitojun 		break;
2056d8a7e3a7Sitojun 	}
2057db3db419Sitojun 	if (p)
2058db3db419Sitojun 		free(p, M_TEMP);
2059d8a7e3a7Sitojun 
2060d8a7e3a7Sitojun 	return (error);
2061d8a7e3a7Sitojun }
2062d8a7e3a7Sitojun 
2063d8a7e3a7Sitojun static int
2064*ee37ea65Smcbride fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
2065d8a7e3a7Sitojun {
2066d8a7e3a7Sitojun 	int error = 0, s;
2067d8a7e3a7Sitojun 	struct in6_defrouter *d = NULL, *de = NULL;
2068d8a7e3a7Sitojun 	struct nd_defrouter *dr;
2069d8a7e3a7Sitojun 	size_t l;
2070d8a7e3a7Sitojun 
20716e92dee6Sitojun 	s = splsoftnet();
2072d8a7e3a7Sitojun 
2073d8a7e3a7Sitojun 	if (oldp) {
2074d8a7e3a7Sitojun 		d = (struct in6_defrouter *)oldp;
2075d8a7e3a7Sitojun 		de = (struct in6_defrouter *)((caddr_t)oldp + *oldlenp);
2076d8a7e3a7Sitojun 	}
2077d8a7e3a7Sitojun 	l = 0;
2078d8a7e3a7Sitojun 
2079d8a7e3a7Sitojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2080d8a7e3a7Sitojun 	     dr = TAILQ_NEXT(dr, dr_entry)) {
2081d8a7e3a7Sitojun 
2082d8a7e3a7Sitojun 		if (oldp && d + 1 <= de) {
2083d8a7e3a7Sitojun 			bzero(d, sizeof(*d));
2084d8a7e3a7Sitojun 			d->rtaddr.sin6_family = AF_INET6;
2085d8a7e3a7Sitojun 			d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
2086d8a7e3a7Sitojun 			d->rtaddr.sin6_addr = dr->rtaddr;
2087d8a7e3a7Sitojun 			in6_recoverscope(&d->rtaddr, &d->rtaddr.sin6_addr,
2088d8a7e3a7Sitojun 			    dr->ifp);
2089d8a7e3a7Sitojun 			d->flags = dr->flags;
2090d8a7e3a7Sitojun 			d->rtlifetime = dr->rtlifetime;
2091d8a7e3a7Sitojun 			d->expire = dr->expire;
2092d8a7e3a7Sitojun 			d->if_index = dr->ifp->if_index;
2093d8a7e3a7Sitojun 		}
2094d8a7e3a7Sitojun 
2095d8a7e3a7Sitojun 		l += sizeof(*d);
2096d8a7e3a7Sitojun 		if (d)
2097d8a7e3a7Sitojun 			d++;
2098d8a7e3a7Sitojun 	}
2099d8a7e3a7Sitojun 
2100d8a7e3a7Sitojun 	if (oldp) {
2101d8a7e3a7Sitojun 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
2102d8a7e3a7Sitojun 		if (l > ol)
2103d8a7e3a7Sitojun 			error = ENOMEM;
2104d8a7e3a7Sitojun 	} else
2105d8a7e3a7Sitojun 		*oldlenp = l;
2106d8a7e3a7Sitojun 
2107d8a7e3a7Sitojun 	splx(s);
2108d8a7e3a7Sitojun 
2109d8a7e3a7Sitojun 	return (error);
2110d8a7e3a7Sitojun }
2111d8a7e3a7Sitojun 
2112d8a7e3a7Sitojun static int
2113*ee37ea65Smcbride fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
2114d8a7e3a7Sitojun {
2115d8a7e3a7Sitojun 	int error = 0, s;
2116d8a7e3a7Sitojun 	struct nd_prefix *pr;
2117d8a7e3a7Sitojun 	struct in6_prefix *p = NULL;
2118d8a7e3a7Sitojun 	struct in6_prefix *pe = NULL;
2119d8a7e3a7Sitojun 	size_t l;
2120d8a7e3a7Sitojun 
21216e92dee6Sitojun 	s = splsoftnet();
2122d8a7e3a7Sitojun 
2123d8a7e3a7Sitojun 	if (oldp) {
2124d8a7e3a7Sitojun 		p = (struct in6_prefix *)oldp;
2125d8a7e3a7Sitojun 		pe = (struct in6_prefix *)((caddr_t)oldp + *oldlenp);
2126d8a7e3a7Sitojun 	}
2127d8a7e3a7Sitojun 	l = 0;
2128d8a7e3a7Sitojun 
2129545205eaSpyr 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
2130d8a7e3a7Sitojun 		u_short advrtrs;
2131d8a7e3a7Sitojun 		size_t advance;
2132d8a7e3a7Sitojun 		struct sockaddr_in6 *sin6;
2133d8a7e3a7Sitojun 		struct sockaddr_in6 *s6;
2134d8a7e3a7Sitojun 		struct nd_pfxrouter *pfr;
2135d8a7e3a7Sitojun 
2136d8a7e3a7Sitojun 		if (oldp && p + 1 <= pe)
2137d8a7e3a7Sitojun 		{
2138d8a7e3a7Sitojun 			bzero(p, sizeof(*p));
2139d8a7e3a7Sitojun 			sin6 = (struct sockaddr_in6 *)(p + 1);
2140d8a7e3a7Sitojun 
2141d8a7e3a7Sitojun 			p->prefix = pr->ndpr_prefix;
2142d8a7e3a7Sitojun 			if (in6_recoverscope(&p->prefix,
2143d8a7e3a7Sitojun 			    &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2144d8a7e3a7Sitojun 				log(LOG_ERR,
2145d8a7e3a7Sitojun 				    "scope error in prefix list (%s)\n",
2146d8a7e3a7Sitojun 				    ip6_sprintf(&p->prefix.sin6_addr));
2147d8a7e3a7Sitojun 			p->raflags = pr->ndpr_raf;
2148d8a7e3a7Sitojun 			p->prefixlen = pr->ndpr_plen;
2149d8a7e3a7Sitojun 			p->vltime = pr->ndpr_vltime;
2150d8a7e3a7Sitojun 			p->pltime = pr->ndpr_pltime;
2151d8a7e3a7Sitojun 			p->if_index = pr->ndpr_ifp->if_index;
2152d8a7e3a7Sitojun 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2153d8a7e3a7Sitojun 				p->expire = 0;
2154d8a7e3a7Sitojun 			else {
2155d8a7e3a7Sitojun 				time_t maxexpire;
2156d8a7e3a7Sitojun 
2157d8a7e3a7Sitojun 				/* XXX: we assume time_t is signed. */
2158d8a7e3a7Sitojun 				maxexpire = (-1) &
2159d8a7e3a7Sitojun 					~(1 << ((sizeof(maxexpire) * 8) - 1));
2160d8a7e3a7Sitojun 				if (pr->ndpr_vltime <
2161d8a7e3a7Sitojun 				    maxexpire - pr->ndpr_lastupdate) {
2162d8a7e3a7Sitojun 					p->expire = pr->ndpr_lastupdate +
2163d8a7e3a7Sitojun 						pr->ndpr_vltime;
2164d8a7e3a7Sitojun 				} else
2165d8a7e3a7Sitojun 					p->expire = maxexpire;
2166d8a7e3a7Sitojun 			}
2167d8a7e3a7Sitojun 			p->refcnt = pr->ndpr_refcnt;
2168d8a7e3a7Sitojun 			p->flags = pr->ndpr_stateflags;
2169d8a7e3a7Sitojun 			p->origin = PR_ORIG_RA;
2170d8a7e3a7Sitojun 			advrtrs = 0;
2171545205eaSpyr 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2172d8a7e3a7Sitojun 				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2173d8a7e3a7Sitojun 					advrtrs++;
2174d8a7e3a7Sitojun 					continue;
2175d8a7e3a7Sitojun 				}
2176d8a7e3a7Sitojun 				s6 = &sin6[advrtrs];
2177d8a7e3a7Sitojun 				s6->sin6_family = AF_INET6;
2178d8a7e3a7Sitojun 				s6->sin6_len = sizeof(struct sockaddr_in6);
2179d8a7e3a7Sitojun 				s6->sin6_addr = pfr->router->rtaddr;
2180d8a7e3a7Sitojun 				in6_recoverscope(s6, &pfr->router->rtaddr,
2181d8a7e3a7Sitojun 				    pfr->router->ifp);
2182d8a7e3a7Sitojun 				advrtrs++;
2183d8a7e3a7Sitojun 			}
2184d8a7e3a7Sitojun 			p->advrtrs = advrtrs;
2185d8a7e3a7Sitojun 		}
2186d8a7e3a7Sitojun 		else {
2187d8a7e3a7Sitojun 			advrtrs = 0;
2188545205eaSpyr 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2189d8a7e3a7Sitojun 				advrtrs++;
2190d8a7e3a7Sitojun 		}
2191d8a7e3a7Sitojun 
2192d8a7e3a7Sitojun 		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2193d8a7e3a7Sitojun 		l += advance;
2194d8a7e3a7Sitojun 		if (p)
2195d8a7e3a7Sitojun 			p = (struct in6_prefix *)((caddr_t)p + advance);
2196d8a7e3a7Sitojun 	}
2197d8a7e3a7Sitojun 
2198d8a7e3a7Sitojun 	if (oldp) {
2199d8a7e3a7Sitojun 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
2200d8a7e3a7Sitojun 		if (l > ol)
2201d8a7e3a7Sitojun 			error = ENOMEM;
2202d8a7e3a7Sitojun 	} else
2203d8a7e3a7Sitojun 		*oldlenp = l;
2204d8a7e3a7Sitojun 
2205d8a7e3a7Sitojun 	splx(s);
2206d8a7e3a7Sitojun 
2207d8a7e3a7Sitojun 	return (error);
2208d8a7e3a7Sitojun }
2209