xref: /openbsd/sys/netinet6/nd6.c (revision a0aa363c)
1*a0aa363cSjsing /*	$OpenBSD: nd6.c,v 1.83 2010/02/08 11:56:09 jsing 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 
99*a0aa363cSjsing void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
100*a0aa363cSjsing void nd6_slowtimo(void *);
101*a0aa363cSjsing struct llinfo_nd6 *nd6_free(struct rtentry *, int);
102*a0aa363cSjsing 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 
108*a0aa363cSjsing int fill_drlist(void *, size_t *, size_t);
109*a0aa363cSjsing 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
123*a0aa363cSjsing nd6_init(void)
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);
14529e86e5eSblambert 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
146287546eaSitojun }
147287546eaSitojun 
148d6b9e9b9Sitojun struct nd_ifinfo *
149ee37ea65Smcbride 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
175ee37ea65Smcbride nd6_ifdetach(struct nd_ifinfo *nd)
176d6b9e9b9Sitojun {
177d374aaacSitojun 
178d6b9e9b9Sitojun 	free(nd, M_IP6NDP);
179287546eaSitojun }
180287546eaSitojun 
181287546eaSitojun void
182ee37ea65Smcbride nd6_setmtu(struct ifnet *ifp)
1831b5f410aSitojun {
1841b5f410aSitojun 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
1851b5f410aSitojun }
1861b5f410aSitojun 
1871b5f410aSitojun void
188ee37ea65Smcbride 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
216ee37ea65Smcbride 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 *
233ee37ea65Smcbride 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
284ee37ea65Smcbride 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 
387*a0aa363cSjsing 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
507ee37ea65Smcbride 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 
5149631a17bSitojun 	s = splsoftnet();
5159631a17bSitojun 	timeout_set(&nd6_timer_ch, nd6_timer, NULL);
51629e86e5eSblambert 	timeout_add_sec(&nd6_timer_ch, nd6_prune);
5179631a17bSitojun 
5180a2c5741Sitojun 	/* expire default router list */
519f4f4d166Sitojun 	dr = TAILQ_FIRST(&nd_defrouter);
520287546eaSitojun 	while (dr) {
5213212dc31Stholo 		if (dr->expire && dr->expire < time_second) {
522287546eaSitojun 			struct nd_defrouter *t;
523f4f4d166Sitojun 			t = TAILQ_NEXT(dr, dr_entry);
524287546eaSitojun 			defrtrlist_del(dr);
525287546eaSitojun 			dr = t;
526d8a7e3a7Sitojun 		} else {
527f4f4d166Sitojun 			dr = TAILQ_NEXT(dr, dr_entry);
528287546eaSitojun 		}
529287546eaSitojun 	}
530287546eaSitojun 
531287546eaSitojun 	/*
532d8a7e3a7Sitojun 	 * expire interface addresses.
533d8a7e3a7Sitojun 	 * in the past the loop was inside prefix expiry processing.
5348b542bbeSpascoe 	 * However, from a stricter spec-conformance standpoint, we should
535d8a7e3a7Sitojun 	 * rather separate address lifetimes and prefix lifetimes.
536d8a7e3a7Sitojun 	 */
537d8a7e3a7Sitojun 	for (ia6 = in6_ifaddr; ia6; ia6 = nia6) {
538d8a7e3a7Sitojun 		nia6 = ia6->ia_next;
539d8a7e3a7Sitojun 		/* check address lifetime */
540d8a7e3a7Sitojun 		if (IFA6_IS_INVALID(ia6)) {
541d8a7e3a7Sitojun 			in6_purgeaddr(&ia6->ia_ifa);
5420a8b9475Smarkus 		} else if (IFA6_IS_DEPRECATED(ia6)) {
543d8a7e3a7Sitojun 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
544d8a7e3a7Sitojun 		} else {
545d8a7e3a7Sitojun 			/*
546d8a7e3a7Sitojun 			 * A new RA might have made a deprecated address
547d8a7e3a7Sitojun 			 * preferred.
548d8a7e3a7Sitojun 			 */
549d8a7e3a7Sitojun 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
550d8a7e3a7Sitojun 		}
551d8a7e3a7Sitojun 	}
552d8a7e3a7Sitojun 
553d8a7e3a7Sitojun 	/* expire prefix list */
554545205eaSpyr 	pr = LIST_FIRST(&nd_prefix);
555545205eaSpyr 	while (pr != NULL) {
556d8a7e3a7Sitojun 		/*
557287546eaSitojun 		 * check prefix lifetime.
558287546eaSitojun 		 * since pltime is just for autoconf, pltime processing for
559287546eaSitojun 		 * prefix is not necessary.
560287546eaSitojun 		 */
561d8a7e3a7Sitojun 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
5623212dc31Stholo 		    time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
563287546eaSitojun 			struct nd_prefix *t;
564545205eaSpyr 			t = LIST_NEXT(pr, ndpr_entry);
565287546eaSitojun 
566287546eaSitojun 			/*
567287546eaSitojun 			 * address expiration and prefix expiration are
568d8a7e3a7Sitojun 			 * separate.  NEVER perform in6_purgeaddr here.
569287546eaSitojun 			 */
570287546eaSitojun 
571287546eaSitojun 			prelist_remove(pr);
572287546eaSitojun 			pr = t;
573287546eaSitojun 		} else
574545205eaSpyr 			pr = LIST_NEXT(pr, ndpr_entry);
575287546eaSitojun 	}
576287546eaSitojun 	splx(s);
577287546eaSitojun }
578287546eaSitojun 
57922770369Sitojun /*
58022770369Sitojun  * Nuke neighbor cache/prefix/default router management table, right before
58122770369Sitojun  * ifp goes away.
58222770369Sitojun  */
58322770369Sitojun void
584ee37ea65Smcbride nd6_purge(struct ifnet *ifp)
58522770369Sitojun {
58622770369Sitojun 	struct llinfo_nd6 *ln, *nln;
587d8a7e3a7Sitojun 	struct nd_defrouter *dr, *ndr;
58822770369Sitojun 	struct nd_prefix *pr, *npr;
58922770369Sitojun 
59022770369Sitojun 	/*
591d8a7e3a7Sitojun 	 * Nuke default router list entries toward ifp.
592d8a7e3a7Sitojun 	 * We defer removal of default router list entries that is installed
593d8a7e3a7Sitojun 	 * in the routing table, in order to keep additional side effects as
594d8a7e3a7Sitojun 	 * small as possible.
59522770369Sitojun 	 */
596d8a7e3a7Sitojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
597f4f4d166Sitojun 		ndr = TAILQ_NEXT(dr, dr_entry);
598d8a7e3a7Sitojun 		if (dr->installed)
599d8a7e3a7Sitojun 			continue;
600d8a7e3a7Sitojun 
60122770369Sitojun 		if (dr->ifp == ifp)
60222770369Sitojun 			defrtrlist_del(dr);
60322770369Sitojun 	}
604d8a7e3a7Sitojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = ndr) {
605d8a7e3a7Sitojun 		ndr = TAILQ_NEXT(dr, dr_entry);
606d8a7e3a7Sitojun 		if (!dr->installed)
607d8a7e3a7Sitojun 			continue;
608d8a7e3a7Sitojun 
60922770369Sitojun 		if (dr->ifp == ifp)
61022770369Sitojun 			defrtrlist_del(dr);
61122770369Sitojun 	}
61222770369Sitojun 
61322770369Sitojun 	/* Nuke prefix list entries toward ifp */
614545205eaSpyr 	for (pr = LIST_FIRST(&nd_prefix); pr != NULL; pr = npr) {
615545205eaSpyr 		npr = LIST_NEXT(pr, ndpr_entry);
61622770369Sitojun 		if (pr->ndpr_ifp == ifp) {
617d8a7e3a7Sitojun 			/*
61808abfc37Sbrad 			 * Because if_detach() does *not* release prefixes
61908abfc37Sbrad 			 * while purging addresses the reference count will
62008abfc37Sbrad 			 * still be above zero. We therefore reset it to
62108abfc37Sbrad 			 * make sure that the prefix really gets purged.
62208abfc37Sbrad 			 */
62308abfc37Sbrad 			pr->ndpr_refcnt = 0;
62408abfc37Sbrad 			/*
625d8a7e3a7Sitojun 			 * Previously, pr->ndpr_addr is removed as well,
626d8a7e3a7Sitojun 			 * but I strongly believe we don't have to do it.
627d8a7e3a7Sitojun 			 * nd6_purge() is only called from in6_ifdetach(),
628d8a7e3a7Sitojun 			 * which removes all the associated interface addresses
629d8a7e3a7Sitojun 			 * by itself.
630d8a7e3a7Sitojun 			 * (jinmei@kame.net 20010129)
631d8a7e3a7Sitojun 			 */
63222770369Sitojun 			prelist_remove(pr);
63322770369Sitojun 		}
63422770369Sitojun 	}
63522770369Sitojun 
636f4f4d166Sitojun 	/* cancel default outgoing interface setting */
637f4f4d166Sitojun 	if (nd6_defifindex == ifp->if_index)
638f4f4d166Sitojun 		nd6_setdefaultiface(0);
639f4f4d166Sitojun 
640e6e438a4Sitojun 	if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
64122770369Sitojun 		/* refresh default router list */
642f4f4d166Sitojun 		defrouter_select();
643e6e438a4Sitojun 	}
64422770369Sitojun 
64522770369Sitojun 	/*
64622770369Sitojun 	 * Nuke neighbor cache entries for the ifp.
64722770369Sitojun 	 * Note that rt->rt_ifp may not be the same as ifp,
64822770369Sitojun 	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
64922770369Sitojun 	 * nd6_rtrequest(), and ip6_input().
65022770369Sitojun 	 */
65122770369Sitojun 	ln = llinfo_nd6.ln_next;
65222770369Sitojun 	while (ln && ln != &llinfo_nd6) {
65322770369Sitojun 		struct rtentry *rt;
65422770369Sitojun 		struct sockaddr_dl *sdl;
65522770369Sitojun 
65622770369Sitojun 		nln = ln->ln_next;
65722770369Sitojun 		rt = ln->ln_rt;
65822770369Sitojun 		if (rt && rt->rt_gateway &&
65922770369Sitojun 		    rt->rt_gateway->sa_family == AF_LINK) {
66022770369Sitojun 			sdl = (struct sockaddr_dl *)rt->rt_gateway;
66122770369Sitojun 			if (sdl->sdl_index == ifp->if_index)
662d8a7e3a7Sitojun 				nln = nd6_free(rt, 0);
66322770369Sitojun 		}
66422770369Sitojun 		ln = nln;
66522770369Sitojun 	}
66622770369Sitojun }
66722770369Sitojun 
668287546eaSitojun struct rtentry *
669ee37ea65Smcbride nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
670287546eaSitojun {
671287546eaSitojun 	struct rtentry *rt;
672287546eaSitojun 	struct sockaddr_in6 sin6;
673287546eaSitojun 
674287546eaSitojun 	bzero(&sin6, sizeof(sin6));
675287546eaSitojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
676287546eaSitojun 	sin6.sin6_family = AF_INET6;
677287546eaSitojun 	sin6.sin6_addr = *addr6;
678d8a7e3a7Sitojun 
679c241aca8Shenning 	rt = rtalloc1((struct sockaddr *)&sin6, create, 0);
680287546eaSitojun 	if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
681287546eaSitojun 		/*
682287546eaSitojun 		 * This is the case for the default route.
683287546eaSitojun 		 * If we want to create a neighbor cache for the address, we
684287546eaSitojun 		 * should free the route for the destination and allocate an
685287546eaSitojun 		 * interface route.
686287546eaSitojun 		 */
687287546eaSitojun 		if (create) {
688287546eaSitojun 			RTFREE(rt);
689287546eaSitojun 			rt = 0;
690287546eaSitojun 		}
691287546eaSitojun 	}
692287546eaSitojun 	if (!rt) {
693287546eaSitojun 		if (create && ifp) {
694cb24f5e5Sclaudio 			struct rt_addrinfo info;
695d374aaacSitojun 			int e;
696d374aaacSitojun 
697287546eaSitojun 			/*
698287546eaSitojun 			 * If no route is available and create is set,
699287546eaSitojun 			 * we allocate a host route for the destination
700287546eaSitojun 			 * and treat it like an interface route.
701287546eaSitojun 			 * This hack is necessary for a neighbor which can't
702287546eaSitojun 			 * be covered by our own prefix.
703287546eaSitojun 			 */
704287546eaSitojun 			struct ifaddr *ifa =
705287546eaSitojun 			    ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp);
706287546eaSitojun 			if (ifa == NULL)
707287546eaSitojun 				return (NULL);
708287546eaSitojun 
709287546eaSitojun 			/*
710287546eaSitojun 			 * Create a new route.  RTF_LLINFO is necessary
711287546eaSitojun 			 * to create a Neighbor Cache entry for the
712287546eaSitojun 			 * destination in nd6_rtrequest which will be
713cb24f5e5Sclaudio 			 * called in rtrequest1 via ifa->ifa_rtrequest.
714287546eaSitojun 			 */
715cb24f5e5Sclaudio 			bzero(&info, sizeof(info));
716cb24f5e5Sclaudio 			info.rti_flags = (ifa->ifa_flags | RTF_HOST |
717cb24f5e5Sclaudio 			    RTF_LLINFO) & ~RTF_CLONING;
718cb24f5e5Sclaudio 			info.rti_info[RTAX_DST] = (struct sockaddr *)&sin6;
719cb24f5e5Sclaudio 			info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
720cb24f5e5Sclaudio 			info.rti_info[RTAX_NETMASK] =
721cb24f5e5Sclaudio 			    (struct sockaddr *)&all1_sa;
722cb24f5e5Sclaudio 			if ((e = rtrequest1(RTM_ADD, &info, RTP_CONNECTED,
723cb24f5e5Sclaudio 			    &rt, 0)) != 0) {
724d8a7e3a7Sitojun #if 0
725287546eaSitojun 				log(LOG_ERR,
726287546eaSitojun 				    "nd6_lookup: failed to add route for a "
727d374aaacSitojun 				    "neighbor(%s), errno=%d\n",
728d374aaacSitojun 				    ip6_sprintf(addr6), e);
729d8a7e3a7Sitojun #endif
730d8a7e3a7Sitojun 				return (NULL);
731d8a7e3a7Sitojun 			}
732287546eaSitojun 			if (rt == NULL)
733287546eaSitojun 				return (NULL);
734287546eaSitojun 			if (rt->rt_llinfo) {
735287546eaSitojun 				struct llinfo_nd6 *ln =
736287546eaSitojun 				    (struct llinfo_nd6 *)rt->rt_llinfo;
737287546eaSitojun 				ln->ln_state = ND6_LLINFO_NOSTATE;
738287546eaSitojun 			}
739f6e55599Sitojun 		} else
740287546eaSitojun 			return (NULL);
741287546eaSitojun 	}
742287546eaSitojun 	rt->rt_refcnt--;
743287546eaSitojun 	/*
744287546eaSitojun 	 * Validation for the entry.
745d8a7e3a7Sitojun 	 * Note that the check for rt_llinfo is necessary because a cloned
746d8a7e3a7Sitojun 	 * route from a parent route that has the L flag (e.g. the default
747d8a7e3a7Sitojun 	 * route to a p2p interface) may have the flag, too, while the
748d8a7e3a7Sitojun 	 * destination is not actually a neighbor.
749287546eaSitojun 	 * XXX: we can't use rt->rt_ifp to check for the interface, since
750287546eaSitojun 	 *      it might be the loopback interface if the entry is for our
751287546eaSitojun 	 *      own address on a non-loopback interface. Instead, we should
752d8a7e3a7Sitojun 	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL
753d8a7e3a7Sitojun 	 *	interface.
754287546eaSitojun 	 */
755287546eaSitojun 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
756d8a7e3a7Sitojun 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
757287546eaSitojun 	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
758287546eaSitojun 		if (create) {
759da592434Sitojun 			nd6log((LOG_DEBUG,
760d8a7e3a7Sitojun 			    "nd6_lookup: failed to lookup %s (if = %s)\n",
761da592434Sitojun 			    ip6_sprintf(addr6),
762da592434Sitojun 			    ifp ? ifp->if_xname : "unspec"));
763287546eaSitojun 		}
764d8a7e3a7Sitojun 		return (NULL);
765287546eaSitojun 	}
766287546eaSitojun 	return (rt);
767287546eaSitojun }
768287546eaSitojun 
769287546eaSitojun /*
770287546eaSitojun  * Detect if a given IPv6 address identifies a neighbor on a given link.
771287546eaSitojun  * XXX: should take care of the destination of a p2p link?
772287546eaSitojun  */
773287546eaSitojun int
774ee37ea65Smcbride nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
775287546eaSitojun {
776d8a7e3a7Sitojun 	struct nd_prefix *pr;
777d8a7e3a7Sitojun 	struct rtentry *rt;
778287546eaSitojun 
779cfb6b8dfSitojun 	/*
780cfb6b8dfSitojun 	 * A link-local address is always a neighbor.
781cfb6b8dfSitojun 	 * XXX: we should use the sin6_scope_id field rather than the embedded
782cfb6b8dfSitojun 	 * interface index.
783d8a7e3a7Sitojun 	 * XXX: a link does not necessarily specify a single interface.
784cfb6b8dfSitojun 	 */
785cfb6b8dfSitojun 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
786cfb6b8dfSitojun 	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
787287546eaSitojun 		return (1);
788287546eaSitojun 
789287546eaSitojun 	/*
790d8a7e3a7Sitojun 	 * If the address matches one of our on-link prefixes, it should be a
791d8a7e3a7Sitojun 	 * neighbor.
792287546eaSitojun 	 */
793545205eaSpyr 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
794d8a7e3a7Sitojun 		if (pr->ndpr_ifp != ifp)
795d8a7e3a7Sitojun 			continue;
796287546eaSitojun 
797d8a7e3a7Sitojun 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
798d8a7e3a7Sitojun 			continue;
799d8a7e3a7Sitojun 
800d8a7e3a7Sitojun 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
801d8a7e3a7Sitojun 		    &addr->sin6_addr, &pr->ndpr_mask))
802d8a7e3a7Sitojun 			return (1);
803287546eaSitojun 	}
804d8a7e3a7Sitojun 
805d8a7e3a7Sitojun 	/*
806d8a7e3a7Sitojun 	 * If the default router list is empty, all addresses are regarded
807d8a7e3a7Sitojun 	 * as on-link, and thus, as a neighbor.
808d8a7e3a7Sitojun 	 * XXX: we restrict the condition to hosts, because routers usually do
809d8a7e3a7Sitojun 	 * not have the "default router list".
810d8a7e3a7Sitojun 	 */
811d8a7e3a7Sitojun 	if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL &&
812d8a7e3a7Sitojun 	    nd6_defifindex == ifp->if_index) {
813287546eaSitojun 		return (1);
814287546eaSitojun 	}
815287546eaSitojun 
816287546eaSitojun 	/*
817287546eaSitojun 	 * Even if the address matches none of our addresses, it might be
818287546eaSitojun 	 * in the neighbor cache.
819287546eaSitojun 	 */
820d8a7e3a7Sitojun 	if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL)
821287546eaSitojun 		return (1);
822287546eaSitojun 
823287546eaSitojun 	return (0);
824287546eaSitojun }
825287546eaSitojun 
826287546eaSitojun /*
827287546eaSitojun  * Free an nd6 llinfo entry.
828d8a7e3a7Sitojun  * Since the function would cause significant changes in the kernel, DO NOT
829d8a7e3a7Sitojun  * make it global, unless you have a strong reason for the change, and are sure
830d8a7e3a7Sitojun  * that the change is safe.
831287546eaSitojun  */
832*a0aa363cSjsing struct llinfo_nd6 *
833ee37ea65Smcbride nd6_free(struct rtentry *rt, int gc)
834287546eaSitojun {
835cb24f5e5Sclaudio 	struct rt_addrinfo info;
83629760ae1Sitojun 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
837f4f4d166Sitojun 	struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr;
838287546eaSitojun 	struct nd_defrouter *dr;
839287546eaSitojun 
840287546eaSitojun 	/*
841d8a7e3a7Sitojun 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
842d8a7e3a7Sitojun 	 * even though it is not harmful, it was not really necessary.
843287546eaSitojun 	 */
844f4f4d166Sitojun 
845492c93a5Sitojun 	if (!ip6_forwarding) {
846f4f4d166Sitojun 		int s;
8476e92dee6Sitojun 		s = splsoftnet();
848f4f4d166Sitojun 		dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr,
849f4f4d166Sitojun 		    rt->rt_ifp);
850d8a7e3a7Sitojun 
851d8a7e3a7Sitojun 		if (dr != NULL && dr->expire &&
852d8a7e3a7Sitojun 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
853d8a7e3a7Sitojun 			/*
854d8a7e3a7Sitojun 			 * If the reason for the deletion is just garbage
855d8a7e3a7Sitojun 			 * collection, and the neighbor is an active default
856d8a7e3a7Sitojun 			 * router, do not delete it.  Instead, reset the GC
857d8a7e3a7Sitojun 			 * timer using the router's lifetime.
858d8a7e3a7Sitojun 			 * Simply deleting the entry would affect default
859d8a7e3a7Sitojun 			 * router selection, which is not necessarily a good
860d8a7e3a7Sitojun 			 * thing, especially when we're using router preference
861d8a7e3a7Sitojun 			 * values.
862d8a7e3a7Sitojun 			 * XXX: the check for ln_state would be redundant,
863d8a7e3a7Sitojun 			 *      but we intentionally keep it just in case.
864d8a7e3a7Sitojun 			 */
8653212dc31Stholo 			if (dr->expire > time_second * hz) {
8669631a17bSitojun 				nd6_llinfo_settimer(ln,
8673212dc31Stholo 				    dr->expire - time_second * hz);
8689631a17bSitojun 			} else
8699631a17bSitojun 				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
870d8a7e3a7Sitojun 			splx(s);
871d8a7e3a7Sitojun 			return (ln->ln_next);
872d8a7e3a7Sitojun 		}
873d8a7e3a7Sitojun 
874f4f4d166Sitojun 		if (ln->ln_router || dr) {
875f4f4d166Sitojun 			/*
876f4f4d166Sitojun 			 * rt6_flush must be called whether or not the neighbor
877f4f4d166Sitojun 			 * is in the Default Router List.
878f4f4d166Sitojun 			 * See a corresponding comment in nd6_na_input().
879f4f4d166Sitojun 			 */
880f4f4d166Sitojun 			rt6_flush(&in6, rt->rt_ifp);
881f4f4d166Sitojun 		}
882f4f4d166Sitojun 
883f4f4d166Sitojun 		if (dr) {
884f4f4d166Sitojun 			/*
8858b542bbeSpascoe 			 * Unreachability of a router might affect the default
886f4f4d166Sitojun 			 * router selection and on-link detection of advertised
887f4f4d166Sitojun 			 * prefixes.
888f4f4d166Sitojun 			 */
889f4f4d166Sitojun 
890f4f4d166Sitojun 			/*
891f4f4d166Sitojun 			 * Temporarily fake the state to choose a new default
892f4f4d166Sitojun 			 * router and to perform on-link determination of
89334deef1eSitojun 			 * prefixes correctly.
894f4f4d166Sitojun 			 * Below the state will be set correctly,
895f4f4d166Sitojun 			 * or the entry itself will be deleted.
896f4f4d166Sitojun 			 */
897f4f4d166Sitojun 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
898f4f4d166Sitojun 
899f4f4d166Sitojun 			/*
900d8a7e3a7Sitojun 			 * Since defrouter_select() does not affect the
901d8a7e3a7Sitojun 			 * on-link determination and MIP6 needs the check
902d8a7e3a7Sitojun 			 * before the default router selection, we perform
903d8a7e3a7Sitojun 			 * the check now.
904f4f4d166Sitojun 			 */
905f4f4d166Sitojun 			pfxlist_onlink_check();
906d8a7e3a7Sitojun 
907d8a7e3a7Sitojun 			/*
908d8a7e3a7Sitojun 			 * refresh default router list
909d8a7e3a7Sitojun 			 */
910d8a7e3a7Sitojun 			defrouter_select();
911287546eaSitojun 		}
912287546eaSitojun 		splx(s);
913287546eaSitojun 	}
914287546eaSitojun 
91529760ae1Sitojun 	/*
91629760ae1Sitojun 	 * Before deleting the entry, remember the next entry as the
91729760ae1Sitojun 	 * return value.  We need this because pfxlist_onlink_check() above
91829760ae1Sitojun 	 * might have freed other entries (particularly the old next entry) as
91929760ae1Sitojun 	 * a side effect (XXX).
92029760ae1Sitojun 	 */
92129760ae1Sitojun 	next = ln->ln_next;
92229760ae1Sitojun 
92329760ae1Sitojun 	/*
92429760ae1Sitojun 	 * Detach the route from the routing tree and the list of neighbor
92529760ae1Sitojun 	 * caches, and disable the route entry not to be used in already
92629760ae1Sitojun 	 * cached routes.
92729760ae1Sitojun 	 */
928cb24f5e5Sclaudio 	bzero(&info, sizeof(info));
929cb24f5e5Sclaudio 	info.rti_info[RTAX_DST] = rt_key(rt);
930cb24f5e5Sclaudio 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
931cb24f5e5Sclaudio 	rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL, 0);
93229760ae1Sitojun 
9330a2c5741Sitojun 	return (next);
934287546eaSitojun }
935287546eaSitojun 
936287546eaSitojun /*
937287546eaSitojun  * Upper-layer reachability hint for Neighbor Unreachability Detection.
938287546eaSitojun  *
9398b542bbeSpascoe  * XXX cost-effective methods?
940287546eaSitojun  */
941287546eaSitojun void
942ee37ea65Smcbride nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
943287546eaSitojun {
944287546eaSitojun 	struct llinfo_nd6 *ln;
945287546eaSitojun 
946287546eaSitojun 	/*
947287546eaSitojun 	 * If the caller specified "rt", use that.  Otherwise, resolve the
948287546eaSitojun 	 * routing table by supplied "dst6".
949287546eaSitojun 	 */
950287546eaSitojun 	if (!rt) {
951287546eaSitojun 		if (!dst6)
952287546eaSitojun 			return;
953287546eaSitojun 		if (!(rt = nd6_lookup(dst6, 0, NULL)))
954287546eaSitojun 			return;
955287546eaSitojun 	}
956287546eaSitojun 
957f6e55599Sitojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
958f6e55599Sitojun 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
959f6e55599Sitojun 	    !rt->rt_llinfo || !rt->rt_gateway ||
960f6e55599Sitojun 	    rt->rt_gateway->sa_family != AF_LINK) {
961287546eaSitojun 		/* This is not a host route. */
962287546eaSitojun 		return;
963287546eaSitojun 	}
964287546eaSitojun 
965287546eaSitojun 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
966804d8827Sitojun 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
967287546eaSitojun 		return;
968287546eaSitojun 
969f6e55599Sitojun 	/*
970f6e55599Sitojun 	 * if we get upper-layer reachability confirmation many times,
971f6e55599Sitojun 	 * it is possible we have false information.
972f6e55599Sitojun 	 */
973f6e55599Sitojun 	if (!force) {
974f6e55599Sitojun 		ln->ln_byhint++;
975f6e55599Sitojun 		if (ln->ln_byhint > nd6_maxnudhint)
976f6e55599Sitojun 			return;
977f6e55599Sitojun 	}
978f6e55599Sitojun 
979287546eaSitojun 	ln->ln_state = ND6_LLINFO_REACHABLE;
9809631a17bSitojun 	if (!ND6_LLINFO_PERMANENT(ln)) {
9819631a17bSitojun 		nd6_llinfo_settimer(ln,
9829631a17bSitojun 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
9839631a17bSitojun 	}
984287546eaSitojun }
985287546eaSitojun 
986ee37ea65Smcbride /*
987ee37ea65Smcbride  * info - XXX: unused
988ee37ea65Smcbride  */
989ee37ea65Smcbride 
990287546eaSitojun void
991ee37ea65Smcbride nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
992287546eaSitojun {
993287546eaSitojun 	struct sockaddr *gate = rt->rt_gateway;
994287546eaSitojun 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
995287546eaSitojun 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
996287546eaSitojun 	struct ifnet *ifp = rt->rt_ifp;
997287546eaSitojun 	struct ifaddr *ifa;
99870fc31beSrainer 	struct nd_defrouter *dr;
99970fc31beSrainer 
100070fc31beSrainer 	if (req == RTM_DELETE && (rt->rt_flags & RTF_GATEWAY) &&
100170fc31beSrainer 	    (IN6_ARE_ADDR_EQUAL(&(satosin6(rt_key(rt)))->sin6_addr,
100270fc31beSrainer 	    &in6addr_any) && rt_mask(rt) && (rt_mask(rt)->sa_len == 0 ||
100370fc31beSrainer 	    IN6_ARE_ADDR_EQUAL(&(satosin6(rt_mask(rt)))->sin6_addr,
100470fc31beSrainer 	    &in6addr_any)))) {
100570fc31beSrainer 		dr = defrouter_lookup(&SIN6(gate)->sin6_addr, ifp);
100670fc31beSrainer 		if (dr)
100770fc31beSrainer 			dr->installed = 0;
100870fc31beSrainer 	}
1009287546eaSitojun 
1010d8a7e3a7Sitojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
1011287546eaSitojun 		return;
1012287546eaSitojun 
1013d8a7e3a7Sitojun 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
1014d8a7e3a7Sitojun 		/*
1015d8a7e3a7Sitojun 		 * This is probably an interface direct route for a link
1016d8a7e3a7Sitojun 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
1017d8a7e3a7Sitojun 		 * We do not need special treatment below for such a route.
1018d8a7e3a7Sitojun 		 * Moreover, the RTF_LLINFO flag which would be set below
1019d8a7e3a7Sitojun 		 * would annoy the ndp(8) command.
1020d8a7e3a7Sitojun 		 */
1021d8a7e3a7Sitojun 		return;
1022d8a7e3a7Sitojun 	}
1023d8a7e3a7Sitojun 
1024d8a7e3a7Sitojun 	if (req == RTM_RESOLVE &&
1025d8a7e3a7Sitojun 	    (nd6_need_cache(ifp) == 0 || /* stf case */
1026d8a7e3a7Sitojun 	     !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) {
1027d8a7e3a7Sitojun 		/*
1028d8a7e3a7Sitojun 		 * FreeBSD and BSD/OS often make a cloned host route based
1029d8a7e3a7Sitojun 		 * on a less-specific route (e.g. the default route).
1030d8a7e3a7Sitojun 		 * If the less specific route does not have a "gateway"
1031d8a7e3a7Sitojun 		 * (this is the case when the route just goes to a p2p or an
1032d8a7e3a7Sitojun 		 * stf interface), we'll mistakenly make a neighbor cache for
1033d8a7e3a7Sitojun 		 * the host route, and will see strange neighbor solicitation
1034d8a7e3a7Sitojun 		 * for the corresponding destination.  In order to avoid the
1035d8a7e3a7Sitojun 		 * confusion, we check if the destination of the route is
1036d8a7e3a7Sitojun 		 * a neighbor in terms of neighbor discovery, and stop the
1037d8a7e3a7Sitojun 		 * process if not.  Additionally, we remove the LLINFO flag
1038d8a7e3a7Sitojun 		 * so that ndp(8) will not try to get the neighbor information
1039d8a7e3a7Sitojun 		 * of the destination.
1040d8a7e3a7Sitojun 		 */
1041d8a7e3a7Sitojun 		rt->rt_flags &= ~RTF_LLINFO;
1042d8a7e3a7Sitojun 		return;
1043d8a7e3a7Sitojun 	}
1044d8a7e3a7Sitojun 
1045287546eaSitojun 	switch (req) {
1046287546eaSitojun 	case RTM_ADD:
1047287546eaSitojun 		/*
1048287546eaSitojun 		 * There is no backward compatibility :)
1049287546eaSitojun 		 *
1050287546eaSitojun 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1051287546eaSitojun 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1052287546eaSitojun 		 *	   rt->rt_flags |= RTF_CLONING;
1053287546eaSitojun 		 */
105448ebf8e1Sitojun 		if ((rt->rt_flags & RTF_CLONING) ||
105548ebf8e1Sitojun 		    ((rt->rt_flags & RTF_LLINFO) && !ln)) {
1056287546eaSitojun 			/*
105748ebf8e1Sitojun 			 * Case 1: This route should come from a route to
105848ebf8e1Sitojun 			 * interface (RTF_CLONING case) or the route should be
105948ebf8e1Sitojun 			 * treated as on-link but is currently not
106048ebf8e1Sitojun 			 * (RTF_LLINFO && !ln case).
1061287546eaSitojun 			 */
1062287546eaSitojun 			rt_setgate(rt, rt_key(rt),
1063c241aca8Shenning 				   (struct sockaddr *)&null_sdl, 0);
1064287546eaSitojun 			gate = rt->rt_gateway;
1065287546eaSitojun 			SDL(gate)->sdl_type = ifp->if_type;
1066287546eaSitojun 			SDL(gate)->sdl_index = ifp->if_index;
1067287546eaSitojun 			if (ln)
10689631a17bSitojun 				nd6_llinfo_settimer(ln, 0);
1069d8a7e3a7Sitojun 			if ((rt->rt_flags & RTF_CLONING) != 0)
1070287546eaSitojun 				break;
1071287546eaSitojun 		}
1072f4f4d166Sitojun 		/*
10738b542bbeSpascoe 		 * In IPv4 code, we try to announce new RTF_ANNOUNCE entry here.
1074f4f4d166Sitojun 		 * We don't do that here since llinfo is not ready yet.
1075f4f4d166Sitojun 		 *
1076f4f4d166Sitojun 		 * There are also couple of other things to be discussed:
1077f4f4d166Sitojun 		 * - unsolicited NA code needs improvement beforehand
1078f4f4d166Sitojun 		 * - RFC2461 says we MAY send multicast unsolicited NA
1079f4f4d166Sitojun 		 *   (7.2.6 paragraph 4), however, it also says that we
1080f4f4d166Sitojun 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
1081f4f4d166Sitojun 		 *   we don't have anything like it right now.
1082841d7adbSitojun 		 *   note that the mechanism needs a mutual agreement
1083f4f4d166Sitojun 		 *   between proxies, which means that we need to implement
1084841d7adbSitojun 		 *   a new protocol, or a new kludge.
1085841d7adbSitojun 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1086f4f4d166Sitojun 		 *   we need to check ip6forwarding before sending it.
1087f4f4d166Sitojun 		 *   (or should we allow proxy ND configuration only for
1088f4f4d166Sitojun 		 *   routers?  there's no mention about proxy ND from hosts)
1089f4f4d166Sitojun 		 */
1090f4f4d166Sitojun #if 0
1091f4f4d166Sitojun 		/* XXX it does not work */
1092287546eaSitojun 		if (rt->rt_flags & RTF_ANNOUNCE)
1093287546eaSitojun 			nd6_na_output(ifp,
1094287546eaSitojun 			      &SIN6(rt_key(rt))->sin6_addr,
1095287546eaSitojun 			      &SIN6(rt_key(rt))->sin6_addr,
1096287546eaSitojun 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1097f4f4d166Sitojun 			      1, NULL);
1098f4f4d166Sitojun #endif
1099287546eaSitojun 		/* FALLTHROUGH */
1100287546eaSitojun 	case RTM_RESOLVE:
1101d8a7e3a7Sitojun 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1102d374aaacSitojun 			/*
1103d374aaacSitojun 			 * Address resolution isn't necessary for a point to
1104d374aaacSitojun 			 * point link, so we can skip this test for a p2p link.
1105d374aaacSitojun 			 */
1106287546eaSitojun 			if (gate->sa_family != AF_LINK ||
1107287546eaSitojun 			    gate->sa_len < sizeof(null_sdl)) {
1108d374aaacSitojun 				log(LOG_DEBUG,
11090a2c5741Sitojun 				    "nd6_rtrequest: bad gateway value: %s\n",
111093036e4eSangelos 				    ifp->if_xname);
1111287546eaSitojun 				break;
1112287546eaSitojun 			}
1113287546eaSitojun 			SDL(gate)->sdl_type = ifp->if_type;
1114287546eaSitojun 			SDL(gate)->sdl_index = ifp->if_index;
1115d374aaacSitojun 		}
1116d374aaacSitojun 		if (ln != NULL)
1117287546eaSitojun 			break;	/* This happens on a route change */
1118287546eaSitojun 		/*
1119287546eaSitojun 		 * Case 2: This route may come from cloning, or a manual route
1120287546eaSitojun 		 * add with a LL address.
1121287546eaSitojun 		 */
1122287546eaSitojun 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1123287546eaSitojun 		rt->rt_llinfo = (caddr_t)ln;
1124287546eaSitojun 		if (!ln) {
1125287546eaSitojun 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1126287546eaSitojun 			break;
1127287546eaSitojun 		}
1128287546eaSitojun 		nd6_inuse++;
1129287546eaSitojun 		nd6_allocated++;
1130287546eaSitojun 		Bzero(ln, sizeof(*ln));
1131287546eaSitojun 		ln->ln_rt = rt;
11329631a17bSitojun 		timeout_set(&ln->ln_timer_ch, nd6_llinfo_timer, ln);
1133287546eaSitojun 		/* this is required for "ndp" command. - shin */
1134287546eaSitojun 		if (req == RTM_ADD) {
1135287546eaSitojun 		        /*
1136287546eaSitojun 			 * gate should have some valid AF_LINK entry,
1137287546eaSitojun 			 * and ln->ln_expire should have some lifetime
1138287546eaSitojun 			 * which is specified by ndp command.
1139287546eaSitojun 			 */
1140287546eaSitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1141f6e55599Sitojun 			ln->ln_byhint = 0;
1142287546eaSitojun 		} else {
1143287546eaSitojun 		        /*
1144287546eaSitojun 			 * When req == RTM_RESOLVE, rt is created and
1145287546eaSitojun 			 * initialized in rtrequest(), so rt_expire is 0.
1146287546eaSitojun 			 */
1147287546eaSitojun 			ln->ln_state = ND6_LLINFO_NOSTATE;
11489631a17bSitojun 			nd6_llinfo_settimer(ln, 0);
1149287546eaSitojun 		}
1150287546eaSitojun 		rt->rt_flags |= RTF_LLINFO;
1151287546eaSitojun 		ln->ln_next = llinfo_nd6.ln_next;
1152287546eaSitojun 		llinfo_nd6.ln_next = ln;
1153287546eaSitojun 		ln->ln_prev = &llinfo_nd6;
1154287546eaSitojun 		ln->ln_next->ln_prev = ln;
1155287546eaSitojun 
1156287546eaSitojun 		/*
1157f3fcf2f3Smcbride 		 * If we have too many cache entries, initiate immediate
1158f3fcf2f3Smcbride 		 * purging for some "less recently used" entries.  Note that
1159f3fcf2f3Smcbride 		 * we cannot directly call nd6_free() here because it would
1160f3fcf2f3Smcbride 		 * cause re-entering rtable related routines triggering an LOR
1161f3fcf2f3Smcbride 		 * problem for FreeBSD.
1162f3fcf2f3Smcbride 		 */
1163f3fcf2f3Smcbride 		if (ip6_neighborgcthresh >= 0 &&
1164f3fcf2f3Smcbride 		    nd6_inuse >= ip6_neighborgcthresh) {
1165f3fcf2f3Smcbride 			int i;
1166f3fcf2f3Smcbride 
1167f3fcf2f3Smcbride 			for (i = 0; i < 10 && llinfo_nd6.ln_prev != ln; i++) {
1168f3fcf2f3Smcbride 				struct llinfo_nd6 *ln_end = llinfo_nd6.ln_prev;
1169f3fcf2f3Smcbride 
1170f3fcf2f3Smcbride 				/* Move this entry to the head */
1171f3fcf2f3Smcbride 				LN_DEQUEUE(ln_end);
1172f3fcf2f3Smcbride 				LN_INSERTHEAD(ln_end);
1173f3fcf2f3Smcbride 
1174f3fcf2f3Smcbride 				if (ND6_LLINFO_PERMANENT(ln_end))
1175f3fcf2f3Smcbride 					continue;
1176f3fcf2f3Smcbride 
1177f3fcf2f3Smcbride 				if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
1178f3fcf2f3Smcbride 					ln_end->ln_state = ND6_LLINFO_STALE;
1179f3fcf2f3Smcbride 				else
1180f3fcf2f3Smcbride 					ln_end->ln_state = ND6_LLINFO_PURGE;
1181f3fcf2f3Smcbride 				nd6_llinfo_settimer(ln_end, 0);
1182f3fcf2f3Smcbride 			}
1183f3fcf2f3Smcbride 		}
1184f3fcf2f3Smcbride 
1185f3fcf2f3Smcbride 		/*
1186287546eaSitojun 		 * check if rt_key(rt) is one of my address assigned
1187287546eaSitojun 		 * to the interface.
1188287546eaSitojun 		 */
1189287546eaSitojun 		ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp,
1190287546eaSitojun 		    &SIN6(rt_key(rt))->sin6_addr);
1191287546eaSitojun 		if (ifa) {
1192287546eaSitojun 			caddr_t macp = nd6_ifptomac(ifp);
11939631a17bSitojun 			nd6_llinfo_settimer(ln, -1);
1194287546eaSitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1195f6e55599Sitojun 			ln->ln_byhint = 0;
1196287546eaSitojun 			if (macp) {
1197287546eaSitojun 				Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1198287546eaSitojun 				SDL(gate)->sdl_alen = ifp->if_addrlen;
1199287546eaSitojun 			}
1200287546eaSitojun 			if (nd6_useloopback) {
1201fcc641efSmickey 				rt->rt_ifp = lo0ifp;	/*XXX*/
1202287546eaSitojun 				/*
1203287546eaSitojun 				 * Make sure rt_ifa be equal to the ifaddr
1204287546eaSitojun 				 * corresponding to the address.
1205287546eaSitojun 				 * We need this because when we refer
1206287546eaSitojun 				 * rt_ifa->ia6_flags in ip6_input, we assume
1207287546eaSitojun 				 * that the rt_ifa points to the address instead
1208287546eaSitojun 				 * of the loopback address.
1209287546eaSitojun 				 */
1210287546eaSitojun 				if (ifa != rt->rt_ifa) {
12114f587652Sitojun 					IFAFREE(rt->rt_ifa);
1212287546eaSitojun 					ifa->ifa_refcnt++;
1213287546eaSitojun 					rt->rt_ifa = ifa;
1214287546eaSitojun 				}
1215287546eaSitojun 			}
1216f4f4d166Sitojun 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
12179631a17bSitojun 			nd6_llinfo_settimer(ln, -1);
1218f4f4d166Sitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1219f6e55599Sitojun 			ln->ln_byhint = 0;
1220f4f4d166Sitojun 
1221f4f4d166Sitojun 			/* join solicited node multicast for proxy ND */
1222f4f4d166Sitojun 			if (ifp->if_flags & IFF_MULTICAST) {
1223f4f4d166Sitojun 				struct in6_addr llsol;
1224f4f4d166Sitojun 				int error;
1225f4f4d166Sitojun 
1226f4f4d166Sitojun 				llsol = SIN6(rt_key(rt))->sin6_addr;
1227f4f4d166Sitojun 				llsol.s6_addr16[0] = htons(0xff02);
1228f4f4d166Sitojun 				llsol.s6_addr16[1] = htons(ifp->if_index);
1229f4f4d166Sitojun 				llsol.s6_addr32[1] = 0;
1230f4f4d166Sitojun 				llsol.s6_addr32[2] = htonl(1);
1231f4f4d166Sitojun 				llsol.s6_addr8[12] = 0xff;
1232f4f4d166Sitojun 
1233d8a7e3a7Sitojun 				if (in6_addmulti(&llsol, ifp, &error)) {
1234d8a7e3a7Sitojun 					nd6log((LOG_ERR, "%s: failed to join "
1235d8a7e3a7Sitojun 					    "%s (errno=%d)\n", ifp->if_xname,
1236d8a7e3a7Sitojun 					    ip6_sprintf(&llsol), error));
1237d8a7e3a7Sitojun 				}
1238f4f4d166Sitojun 			}
1239287546eaSitojun 		}
1240287546eaSitojun 		break;
1241287546eaSitojun 
1242287546eaSitojun 	case RTM_DELETE:
1243287546eaSitojun 		if (!ln)
1244287546eaSitojun 			break;
1245f4f4d166Sitojun 		/* leave from solicited node multicast for proxy ND */
1246f4f4d166Sitojun 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1247f4f4d166Sitojun 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1248f4f4d166Sitojun 			struct in6_addr llsol;
1249f4f4d166Sitojun 			struct in6_multi *in6m;
1250f4f4d166Sitojun 
1251f4f4d166Sitojun 			llsol = SIN6(rt_key(rt))->sin6_addr;
1252f4f4d166Sitojun 			llsol.s6_addr16[0] = htons(0xff02);
1253f4f4d166Sitojun 			llsol.s6_addr16[1] = htons(ifp->if_index);
1254f4f4d166Sitojun 			llsol.s6_addr32[1] = 0;
1255f4f4d166Sitojun 			llsol.s6_addr32[2] = htonl(1);
1256f4f4d166Sitojun 			llsol.s6_addr8[12] = 0xff;
1257f4f4d166Sitojun 
1258f4f4d166Sitojun 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1259f4f4d166Sitojun 			if (in6m)
1260f4f4d166Sitojun 				in6_delmulti(in6m);
1261f4f4d166Sitojun 		}
1262287546eaSitojun 		nd6_inuse--;
1263287546eaSitojun 		ln->ln_next->ln_prev = ln->ln_prev;
1264287546eaSitojun 		ln->ln_prev->ln_next = ln->ln_next;
1265287546eaSitojun 		ln->ln_prev = NULL;
12669631a17bSitojun 		nd6_llinfo_settimer(ln, -1);
1267287546eaSitojun 		rt->rt_llinfo = 0;
1268287546eaSitojun 		rt->rt_flags &= ~RTF_LLINFO;
1269287546eaSitojun 		if (ln->ln_hold)
1270287546eaSitojun 			m_freem(ln->ln_hold);
1271287546eaSitojun 		Free((caddr_t)ln);
1272287546eaSitojun 	}
1273287546eaSitojun }
1274287546eaSitojun 
1275287546eaSitojun int
1276ee37ea65Smcbride nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1277287546eaSitojun {
1278287546eaSitojun 	struct in6_drlist *drl = (struct in6_drlist *)data;
1279d8a7e3a7Sitojun 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1280287546eaSitojun 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1281287546eaSitojun 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1282f4f4d166Sitojun 	struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1283d8a7e3a7Sitojun 	struct nd_defrouter *dr;
1284287546eaSitojun 	struct nd_prefix *pr;
1285287546eaSitojun 	struct rtentry *rt;
1286287546eaSitojun 	int i = 0, error = 0;
1287287546eaSitojun 	int s;
1288287546eaSitojun 
1289287546eaSitojun 	switch (cmd) {
1290287546eaSitojun 	case SIOCGDRLST_IN6:
1291d8a7e3a7Sitojun 		/*
1292d8a7e3a7Sitojun 		 * obsolete API, use sysctl under net.inet6.icmp6
1293d8a7e3a7Sitojun 		 */
1294287546eaSitojun 		bzero(drl, sizeof(*drl));
12956e92dee6Sitojun 		s = splsoftnet();
1296f4f4d166Sitojun 		dr = TAILQ_FIRST(&nd_defrouter);
1297287546eaSitojun 		while (dr && i < DRLSTSIZ) {
1298287546eaSitojun 			drl->defrouter[i].rtaddr = dr->rtaddr;
1299287546eaSitojun 			if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1300287546eaSitojun 				/* XXX: need to this hack for KAME stack */
1301287546eaSitojun 				drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1302f6e55599Sitojun 			} else
1303287546eaSitojun 				log(LOG_ERR,
1304287546eaSitojun 				    "default router list contains a "
1305287546eaSitojun 				    "non-linklocal address(%s)\n",
1306287546eaSitojun 				    ip6_sprintf(&drl->defrouter[i].rtaddr));
1307287546eaSitojun 
1308287546eaSitojun 			drl->defrouter[i].flags = dr->flags;
1309287546eaSitojun 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1310287546eaSitojun 			drl->defrouter[i].expire = dr->expire;
1311287546eaSitojun 			drl->defrouter[i].if_index = dr->ifp->if_index;
1312287546eaSitojun 			i++;
1313f4f4d166Sitojun 			dr = TAILQ_NEXT(dr, dr_entry);
1314287546eaSitojun 		}
1315287546eaSitojun 		splx(s);
1316287546eaSitojun 		break;
1317287546eaSitojun 	case SIOCGPRLST_IN6:
1318f4f4d166Sitojun 		/*
1319d8a7e3a7Sitojun 		 * obsolete API, use sysctl under net.inet6.icmp6
1320d8a7e3a7Sitojun 		 *
1321d8a7e3a7Sitojun 		 * XXX the structure in6_prlist was changed in backward-
1322d8a7e3a7Sitojun 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1323d8a7e3a7Sitojun 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1324d8a7e3a7Sitojun 		 */
1325d8a7e3a7Sitojun 		/*
13268b542bbeSpascoe 		 * XXX meaning of fields, especially "raflags", is very
13278b542bbeSpascoe 		 * different between RA prefix list and RR/static prefix list.
1328f4f4d166Sitojun 		 * how about separating ioctls into two?
1329f4f4d166Sitojun 		 */
1330d8a7e3a7Sitojun 		bzero(oprl, sizeof(*oprl));
13316e92dee6Sitojun 		s = splsoftnet();
1332545205eaSpyr 		pr = LIST_FIRST(&nd_prefix);
1333287546eaSitojun 		while (pr && i < PRLSTSIZ) {
1334287546eaSitojun 			struct nd_pfxrouter *pfr;
1335287546eaSitojun 			int j;
1336287546eaSitojun 
1337d8a7e3a7Sitojun 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1338d8a7e3a7Sitojun 			oprl->prefix[i].raflags = pr->ndpr_raf;
1339d8a7e3a7Sitojun 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1340d8a7e3a7Sitojun 			oprl->prefix[i].vltime = pr->ndpr_vltime;
1341d8a7e3a7Sitojun 			oprl->prefix[i].pltime = pr->ndpr_pltime;
1342d8a7e3a7Sitojun 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1343d8a7e3a7Sitojun 			oprl->prefix[i].expire = pr->ndpr_expire;
1344287546eaSitojun 
1345545205eaSpyr 			pfr = LIST_FIRST(&pr->ndpr_advrtrs);
1346287546eaSitojun 			j = 0;
1347287546eaSitojun 			while(pfr) {
1348287546eaSitojun 				if (j < DRLSTSIZ) {
1349d8a7e3a7Sitojun #define RTRADDR oprl->prefix[i].advrtr[j]
1350287546eaSitojun 					RTRADDR = pfr->router->rtaddr;
1351287546eaSitojun 					if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1352287546eaSitojun 						/* XXX: hack for KAME */
1353287546eaSitojun 						RTRADDR.s6_addr16[1] = 0;
1354f6e55599Sitojun 					} else
1355287546eaSitojun 						log(LOG_ERR,
1356287546eaSitojun 						    "a router(%s) advertises "
1357287546eaSitojun 						    "a prefix with "
1358287546eaSitojun 						    "non-link local address\n",
1359287546eaSitojun 						    ip6_sprintf(&RTRADDR));
1360287546eaSitojun #undef RTRADDR
1361287546eaSitojun 				}
1362287546eaSitojun 				j++;
1363545205eaSpyr 				pfr = LIST_NEXT(pfr, pfr_entry);
1364287546eaSitojun 			}
1365d8a7e3a7Sitojun 			oprl->prefix[i].advrtrs = j;
1366d8a7e3a7Sitojun 			oprl->prefix[i].origin = PR_ORIG_RA;
1367287546eaSitojun 
1368287546eaSitojun 			i++;
1369545205eaSpyr 			pr = LIST_NEXT(pr, ndpr_entry);
1370287546eaSitojun 		}
1371f4f4d166Sitojun 		splx(s);
1372287546eaSitojun 
1373287546eaSitojun 		break;
1374d6b9e9b9Sitojun 	case OSIOCGIFINFO_IN6:
1375d6b9e9b9Sitojun 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1376191a775dSitojun 		bzero(&ndi->ndi, sizeof(ndi->ndi));
1377d6b9e9b9Sitojun 		ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1378d6b9e9b9Sitojun 		ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1379d6b9e9b9Sitojun 		ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1380d6b9e9b9Sitojun 		ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1381d6b9e9b9Sitojun 		ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1382d6b9e9b9Sitojun 		ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1383d6b9e9b9Sitojun 		ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1384d6b9e9b9Sitojun 		ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1385cfab95f2Sitojun 		break;
1386d6b9e9b9Sitojun 	case SIOCGIFINFO_IN6:
1387d6b9e9b9Sitojun 		ndi->ndi = *ND_IFINFO(ifp);
1388287546eaSitojun 		break;
1389d374aaacSitojun 	case SIOCSIFINFO_FLAGS:
1390d6b9e9b9Sitojun 		ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1391d374aaacSitojun 		break;
1392f4f4d166Sitojun 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1393d8a7e3a7Sitojun 		/* sync kernel routing table with the default router list */
1394d8a7e3a7Sitojun 		defrouter_reset();
1395f4f4d166Sitojun 		defrouter_select();
1396287546eaSitojun 		break;
1397287546eaSitojun 	case SIOCSPFXFLUSH_IN6:
1398287546eaSitojun 	{
1399287546eaSitojun 		/* flush all the prefix advertised by routers */
1400287546eaSitojun 		struct nd_prefix *pr, *next;
1401287546eaSitojun 
14026e92dee6Sitojun 		s = splsoftnet();
1403545205eaSpyr 		for (pr = LIST_FIRST(&nd_prefix); pr; pr = next) {
1404d8a7e3a7Sitojun 			struct in6_ifaddr *ia, *ia_next;
1405d8a7e3a7Sitojun 
1406545205eaSpyr 			next = LIST_NEXT(pr, ndpr_entry);
1407d8a7e3a7Sitojun 
1408d8a7e3a7Sitojun 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1409d8a7e3a7Sitojun 				continue; /* XXX */
1410d8a7e3a7Sitojun 
1411d8a7e3a7Sitojun 			/* do we really have to remove addresses as well? */
1412d8a7e3a7Sitojun 			for (ia = in6_ifaddr; ia; ia = ia_next) {
1413d8a7e3a7Sitojun 				/* ia might be removed.  keep the next ptr. */
1414d8a7e3a7Sitojun 				ia_next = ia->ia_next;
1415d8a7e3a7Sitojun 
1416d8a7e3a7Sitojun 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1417d8a7e3a7Sitojun 					continue;
1418d8a7e3a7Sitojun 
1419d8a7e3a7Sitojun 				if (ia->ia6_ndpr == pr)
1420d8a7e3a7Sitojun 					in6_purgeaddr(&ia->ia_ifa);
1421d8a7e3a7Sitojun 			}
1422287546eaSitojun 			prelist_remove(pr);
1423287546eaSitojun 		}
1424287546eaSitojun 		splx(s);
1425287546eaSitojun 		break;
1426287546eaSitojun 	}
1427287546eaSitojun 	case SIOCSRTRFLUSH_IN6:
1428287546eaSitojun 	{
1429287546eaSitojun 		/* flush all the default routers */
1430287546eaSitojun 		struct nd_defrouter *dr, *next;
1431287546eaSitojun 
14326e92dee6Sitojun 		s = splsoftnet();
1433d8a7e3a7Sitojun 		defrouter_reset();
1434d8a7e3a7Sitojun 		for (dr = TAILQ_FIRST(&nd_defrouter); dr; dr = next) {
1435f4f4d166Sitojun 			next = TAILQ_NEXT(dr, dr_entry);
1436287546eaSitojun 			defrtrlist_del(dr);
1437287546eaSitojun 		}
1438d8a7e3a7Sitojun 		defrouter_select();
1439287546eaSitojun 		splx(s);
1440287546eaSitojun 		break;
1441287546eaSitojun 	}
1442287546eaSitojun 	case SIOCGNBRINFO_IN6:
1443287546eaSitojun 	{
1444287546eaSitojun 		struct llinfo_nd6 *ln;
1445287546eaSitojun 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1446287546eaSitojun 
1447287546eaSitojun 		/*
1448287546eaSitojun 		 * XXX: KAME specific hack for scoped addresses
1449287546eaSitojun 		 *      XXXX: for other scopes than link-local?
1450287546eaSitojun 		 */
1451287546eaSitojun 		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1452287546eaSitojun 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1453287546eaSitojun 			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1454287546eaSitojun 
1455287546eaSitojun 			if (*idp == 0)
1456287546eaSitojun 				*idp = htons(ifp->if_index);
1457287546eaSitojun 		}
1458287546eaSitojun 
14596e92dee6Sitojun 		s = splsoftnet();
1460d8a7e3a7Sitojun 		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1461d8a7e3a7Sitojun 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1462287546eaSitojun 			error = EINVAL;
146391e69634Sitojun 			splx(s);
1464287546eaSitojun 			break;
1465287546eaSitojun 		}
1466287546eaSitojun 		nbi->state = ln->ln_state;
1467287546eaSitojun 		nbi->asked = ln->ln_asked;
1468287546eaSitojun 		nbi->isrouter = ln->ln_router;
1469287546eaSitojun 		nbi->expire = ln->ln_expire;
1470287546eaSitojun 		splx(s);
1471287546eaSitojun 
1472287546eaSitojun 		break;
1473287546eaSitojun 	}
1474f4f4d166Sitojun 	case SIOCGDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1475f4f4d166Sitojun 		ndif->ifindex = nd6_defifindex;
1476f4f4d166Sitojun 		break;
1477f4f4d166Sitojun 	case SIOCSDEFIFACE_IN6:	/* XXX: should be implemented as a sysctl? */
1478f4f4d166Sitojun 		return (nd6_setdefaultiface(ndif->ifindex));
1479f4f4d166Sitojun 		break;
1480287546eaSitojun 	}
1481287546eaSitojun 	return (error);
1482287546eaSitojun }
1483287546eaSitojun 
1484287546eaSitojun /*
1485287546eaSitojun  * Create neighbor cache entry and cache link-layer address,
1486287546eaSitojun  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1487ee37ea65Smcbride  *
1488ee37ea65Smcbride  * type - ICMP6 type
1489ee37ea65Smcbride  * code - type dependent information
1490287546eaSitojun  */
1491287546eaSitojun struct rtentry *
1492ee37ea65Smcbride nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1493ee37ea65Smcbride     int lladdrlen, int type, int code)
1494287546eaSitojun {
1495287546eaSitojun 	struct rtentry *rt = NULL;
1496287546eaSitojun 	struct llinfo_nd6 *ln = NULL;
1497287546eaSitojun 	int is_newentry;
1498287546eaSitojun 	struct sockaddr_dl *sdl = NULL;
1499287546eaSitojun 	int do_update;
1500287546eaSitojun 	int olladdr;
1501287546eaSitojun 	int llchange;
1502287546eaSitojun 	int newstate = 0;
1503287546eaSitojun 
1504287546eaSitojun 	if (!ifp)
1505287546eaSitojun 		panic("ifp == NULL in nd6_cache_lladdr");
1506287546eaSitojun 	if (!from)
1507287546eaSitojun 		panic("from == NULL in nd6_cache_lladdr");
1508287546eaSitojun 
1509287546eaSitojun 	/* nothing must be updated for unspecified address */
1510287546eaSitojun 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1511287546eaSitojun 		return NULL;
1512287546eaSitojun 
1513287546eaSitojun 	/*
1514287546eaSitojun 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1515287546eaSitojun 	 * the caller.
1516287546eaSitojun 	 *
15178b542bbeSpascoe 	 * XXX If the link does not have link-layer address, what should
1518287546eaSitojun 	 * we do? (ifp->if_addrlen == 0)
1519287546eaSitojun 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1520287546eaSitojun 	 * description on it in NS section (RFC 2461 7.2.3).
1521287546eaSitojun 	 */
1522287546eaSitojun 
1523287546eaSitojun 	rt = nd6_lookup(from, 0, ifp);
1524287546eaSitojun 	if (!rt) {
1525287546eaSitojun #if 0
1526287546eaSitojun 		/* nothing must be done if there's no lladdr */
1527287546eaSitojun 		if (!lladdr || !lladdrlen)
1528287546eaSitojun 			return NULL;
1529287546eaSitojun #endif
1530287546eaSitojun 
1531287546eaSitojun 		rt = nd6_lookup(from, 1, ifp);
1532287546eaSitojun 		is_newentry = 1;
15330a2c5741Sitojun 	} else {
15340a2c5741Sitojun 		/* do nothing if static ndp is set */
15350a2c5741Sitojun 		if (rt->rt_flags & RTF_STATIC)
15360a2c5741Sitojun 			return NULL;
1537287546eaSitojun 		is_newentry = 0;
15380a2c5741Sitojun 	}
1539287546eaSitojun 
1540287546eaSitojun 	if (!rt)
1541287546eaSitojun 		return NULL;
1542287546eaSitojun 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1543287546eaSitojun fail:
1544d8a7e3a7Sitojun 		(void)nd6_free(rt, 0);
1545287546eaSitojun 		return NULL;
1546287546eaSitojun 	}
1547287546eaSitojun 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1548287546eaSitojun 	if (!ln)
1549287546eaSitojun 		goto fail;
1550287546eaSitojun 	if (!rt->rt_gateway)
1551287546eaSitojun 		goto fail;
1552287546eaSitojun 	if (rt->rt_gateway->sa_family != AF_LINK)
1553287546eaSitojun 		goto fail;
1554287546eaSitojun 	sdl = SDL(rt->rt_gateway);
1555287546eaSitojun 
1556287546eaSitojun 	olladdr = (sdl->sdl_alen) ? 1 : 0;
1557287546eaSitojun 	if (olladdr && lladdr) {
1558287546eaSitojun 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1559287546eaSitojun 			llchange = 1;
1560287546eaSitojun 		else
1561287546eaSitojun 			llchange = 0;
1562287546eaSitojun 	} else
1563287546eaSitojun 		llchange = 0;
1564287546eaSitojun 
1565287546eaSitojun 	/*
1566287546eaSitojun 	 * newentry olladdr  lladdr  llchange	(*=record)
1567287546eaSitojun 	 *	0	n	n	--	(1)
1568287546eaSitojun 	 *	0	y	n	--	(2)
1569287546eaSitojun 	 *	0	n	y	--	(3) * STALE
1570287546eaSitojun 	 *	0	y	y	n	(4) *
1571287546eaSitojun 	 *	0	y	y	y	(5) * STALE
1572287546eaSitojun 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1573287546eaSitojun 	 *	1	--	y	--	(7) * STALE
1574287546eaSitojun 	 */
1575287546eaSitojun 
1576287546eaSitojun 	if (lladdr) {		/* (3-5) and (7) */
1577287546eaSitojun 		/*
1578287546eaSitojun 		 * Record source link-layer address
1579287546eaSitojun 		 * XXX is it dependent to ifp->if_type?
1580287546eaSitojun 		 */
1581287546eaSitojun 		sdl->sdl_alen = ifp->if_addrlen;
1582287546eaSitojun 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1583287546eaSitojun 	}
1584287546eaSitojun 
1585287546eaSitojun 	if (!is_newentry) {
1586d8a7e3a7Sitojun 		if ((!olladdr && lladdr) ||		/* (3) */
1587d8a7e3a7Sitojun 		    (olladdr && lladdr && llchange)) {	/* (5) */
1588287546eaSitojun 			do_update = 1;
1589287546eaSitojun 			newstate = ND6_LLINFO_STALE;
1590287546eaSitojun 		} else					/* (1-2,4) */
1591287546eaSitojun 			do_update = 0;
1592287546eaSitojun 	} else {
1593287546eaSitojun 		do_update = 1;
1594287546eaSitojun 		if (!lladdr)				/* (6) */
1595287546eaSitojun 			newstate = ND6_LLINFO_NOSTATE;
1596287546eaSitojun 		else					/* (7) */
1597287546eaSitojun 			newstate = ND6_LLINFO_STALE;
1598287546eaSitojun 	}
1599287546eaSitojun 
1600287546eaSitojun 	if (do_update) {
1601287546eaSitojun 		/*
1602287546eaSitojun 		 * Update the state of the neighbor cache.
1603287546eaSitojun 		 */
1604287546eaSitojun 		ln->ln_state = newstate;
1605287546eaSitojun 
1606287546eaSitojun 		if (ln->ln_state == ND6_LLINFO_STALE) {
16078a7bb304Sitojun 			/*
16088a7bb304Sitojun 			 * XXX: since nd6_output() below will cause
16098b542bbeSpascoe 			 * state transition to DELAY and reset the timer,
16108a7bb304Sitojun 			 * we must set the timer now, although it is actually
16118a7bb304Sitojun 			 * meaningless.
16128a7bb304Sitojun 			 */
16139631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
16148a7bb304Sitojun 
1615287546eaSitojun 			if (ln->ln_hold) {
1616e212adedSkrw 				struct mbuf *n = ln->ln_hold;
1617e212adedSkrw 				ln->ln_hold = NULL;
16186afad192Sitojun 				/*
16196afad192Sitojun 				 * we assume ifp is not a p2p here, so just
16206afad192Sitojun 				 * set the 2nd argument as the 1st one.
16216afad192Sitojun 				 */
1622e212adedSkrw 				nd6_output(ifp, ifp, n,
1623d8a7e3a7Sitojun 				    (struct sockaddr_in6 *)rt_key(rt), rt);
1624e212adedSkrw 				if (ln->ln_hold == n) {
1625e212adedSkrw 					/* n is back in ln_hold. Discard. */
1626e212adedSkrw 					m_freem(ln->ln_hold);
16278a7bb304Sitojun 					ln->ln_hold = NULL;
1628287546eaSitojun 				}
1629e212adedSkrw 			}
1630287546eaSitojun 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1631287546eaSitojun 			/* probe right away */
16329631a17bSitojun 			nd6_llinfo_settimer((void *)ln, 0);
1633287546eaSitojun 		}
1634287546eaSitojun 	}
1635287546eaSitojun 
1636287546eaSitojun 	/*
1637287546eaSitojun 	 * ICMP6 type dependent behavior.
1638287546eaSitojun 	 *
1639287546eaSitojun 	 * NS: clear IsRouter if new entry
1640287546eaSitojun 	 * RS: clear IsRouter
1641287546eaSitojun 	 * RA: set IsRouter if there's lladdr
1642287546eaSitojun 	 * redir: clear IsRouter if new entry
1643287546eaSitojun 	 *
1644287546eaSitojun 	 * RA case, (1):
1645287546eaSitojun 	 * The spec says that we must set IsRouter in the following cases:
1646287546eaSitojun 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1647287546eaSitojun 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1648287546eaSitojun 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
16498b542bbeSpascoe 	 * A question arises for (1) case.  (1) case has no lladdr in the
1650287546eaSitojun 	 * neighbor cache, this is similar to (6).
1651287546eaSitojun 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1652287546eaSitojun 	 *
1653287546eaSitojun 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1654287546eaSitojun 	 *							D R
1655287546eaSitojun 	 *	0	n	n	--	(1)	c   ?     s
1656287546eaSitojun 	 *	0	y	n	--	(2)	c   s     s
1657287546eaSitojun 	 *	0	n	y	--	(3)	c   s     s
1658287546eaSitojun 	 *	0	y	y	n	(4)	c   s     s
1659287546eaSitojun 	 *	0	y	y	y	(5)	c   s     s
1660287546eaSitojun 	 *	1	--	n	--	(6) c	c 	c s
1661287546eaSitojun 	 *	1	--	y	--	(7) c	c   s	c s
1662287546eaSitojun 	 *
1663287546eaSitojun 	 *					(c=clear s=set)
1664287546eaSitojun 	 */
1665287546eaSitojun 	switch (type & 0xff) {
1666287546eaSitojun 	case ND_NEIGHBOR_SOLICIT:
1667287546eaSitojun 		/*
1668287546eaSitojun 		 * New entry must have is_router flag cleared.
1669287546eaSitojun 		 */
1670287546eaSitojun 		if (is_newentry)	/* (6-7) */
1671287546eaSitojun 			ln->ln_router = 0;
1672287546eaSitojun 		break;
1673287546eaSitojun 	case ND_REDIRECT:
1674287546eaSitojun 		/*
1675287546eaSitojun 		 * If the icmp is a redirect to a better router, always set the
1676287546eaSitojun 		 * is_router flag.  Otherwise, if the entry is newly created,
1677287546eaSitojun 		 * clear the flag.  [RFC 2461, sec 8.3]
1678287546eaSitojun 		 */
1679287546eaSitojun 		if (code == ND_REDIRECT_ROUTER)
1680287546eaSitojun 			ln->ln_router = 1;
1681287546eaSitojun 		else if (is_newentry) /* (6-7) */
1682287546eaSitojun 			ln->ln_router = 0;
1683287546eaSitojun 		break;
1684287546eaSitojun 	case ND_ROUTER_SOLICIT:
1685287546eaSitojun 		/*
1686287546eaSitojun 		 * is_router flag must always be cleared.
1687287546eaSitojun 		 */
1688287546eaSitojun 		ln->ln_router = 0;
1689287546eaSitojun 		break;
1690287546eaSitojun 	case ND_ROUTER_ADVERT:
1691287546eaSitojun 		/*
1692287546eaSitojun 		 * Mark an entry with lladdr as a router.
1693287546eaSitojun 		 */
1694d8a7e3a7Sitojun 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1695d8a7e3a7Sitojun 		    (is_newentry && lladdr)) {			/* (7) */
1696287546eaSitojun 			ln->ln_router = 1;
1697287546eaSitojun 		}
1698287546eaSitojun 		break;
1699287546eaSitojun 	}
1700287546eaSitojun 
170150ccc024Sitojun 	/*
170250ccc024Sitojun 	 * When the link-layer address of a router changes, select the
170350ccc024Sitojun 	 * best router again.  In particular, when the neighbor entry is newly
170450ccc024Sitojun 	 * created, it might affect the selection policy.
170550ccc024Sitojun 	 * Question: can we restrict the first condition to the "is_newentry"
170650ccc024Sitojun 	 * case?
170750ccc024Sitojun 	 * XXX: when we hear an RA from a new router with the link-layer
170850ccc024Sitojun 	 * address option, defrouter_select() is called twice, since
170950ccc024Sitojun 	 * defrtrlist_update called the function as well.  However, I believe
171050ccc024Sitojun 	 * we can compromise the overhead, since it only happens the first
171150ccc024Sitojun 	 * time.
1712d8a7e3a7Sitojun 	 * XXX: although defrouter_select() should not have a bad effect
1713d8a7e3a7Sitojun 	 * for those are not autoconfigured hosts, we explicitly avoid such
1714d8a7e3a7Sitojun 	 * cases for safety.
171550ccc024Sitojun 	 */
171629ee75acSitojun 	if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
171750ccc024Sitojun 		defrouter_select();
171850ccc024Sitojun 
1719287546eaSitojun 	return rt;
1720287546eaSitojun }
1721287546eaSitojun 
1722*a0aa363cSjsing void
1723ee37ea65Smcbride nd6_slowtimo(void *ignored_arg)
1724287546eaSitojun {
17256e92dee6Sitojun 	int s = splsoftnet();
1726b3c1e4c1Sitojun 	struct nd_ifinfo *nd6if;
1727d6b9e9b9Sitojun 	struct ifnet *ifp;
1728287546eaSitojun 
1729b3c1e4c1Sitojun 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
173029e86e5eSblambert 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
1731d6b9e9b9Sitojun 	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1732d6b9e9b9Sitojun 	{
1733d6b9e9b9Sitojun 		nd6if = ND_IFINFO(ifp);
1734287546eaSitojun 		if (nd6if->basereachable && /* already initialized */
1735287546eaSitojun 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1736287546eaSitojun 			/*
1737287546eaSitojun 			 * Since reachable time rarely changes by router
1738287546eaSitojun 			 * advertisements, we SHOULD insure that a new random
1739287546eaSitojun 			 * value gets recomputed at least once every few hours.
1740287546eaSitojun 			 * (RFC 2461, 6.3.4)
1741287546eaSitojun 			 */
1742287546eaSitojun 			nd6if->recalctm = nd6_recalc_reachtm_interval;
1743287546eaSitojun 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1744287546eaSitojun 		}
1745287546eaSitojun 	}
1746287546eaSitojun 	splx(s);
1747287546eaSitojun }
1748287546eaSitojun 
1749287546eaSitojun #define senderr(e) { error = (e); goto bad;}
1750287546eaSitojun int
1751ee37ea65Smcbride nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1752ee37ea65Smcbride     struct sockaddr_in6 *dst, struct rtentry *rt0)
1753287546eaSitojun {
1754b3c1e4c1Sitojun 	struct mbuf *m = m0;
1755b3c1e4c1Sitojun 	struct rtentry *rt = rt0;
1756cfb6b8dfSitojun 	struct sockaddr_in6 *gw6 = NULL;
1757287546eaSitojun 	struct llinfo_nd6 *ln = NULL;
1758287546eaSitojun 	int error = 0;
1759cb39d30aSangelos #ifdef IPSEC
1760cb39d30aSangelos 	struct m_tag *mtag;
1761cb39d30aSangelos #endif /* IPSEC */
1762287546eaSitojun 
1763287546eaSitojun 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1764287546eaSitojun 		goto sendpkt;
1765287546eaSitojun 
1766d8a7e3a7Sitojun 	if (nd6_need_cache(ifp) == 0)
1767287546eaSitojun 		goto sendpkt;
1768287546eaSitojun 
1769287546eaSitojun 	/*
17708b542bbeSpascoe 	 * next hop determination.  This routine is derived from ether_output.
1771287546eaSitojun 	 */
1772287546eaSitojun 	if (rt) {
1773287546eaSitojun 		if ((rt->rt_flags & RTF_UP) == 0) {
1774d8a7e3a7Sitojun 			if ((rt0 = rt = rtalloc1((struct sockaddr *)dst,
1775c241aca8Shenning 			    1, 0)) != NULL)
1776287546eaSitojun 			{
1777287546eaSitojun 				rt->rt_refcnt--;
1778fe16f6efSitojun 				if (rt->rt_ifp != ifp)
1779fe16f6efSitojun 					senderr(EHOSTUNREACH);
1780287546eaSitojun 			} else
1781287546eaSitojun 				senderr(EHOSTUNREACH);
1782287546eaSitojun 		}
1783cfb6b8dfSitojun 
1784287546eaSitojun 		if (rt->rt_flags & RTF_GATEWAY) {
1785cfb6b8dfSitojun 			gw6 = (struct sockaddr_in6 *)rt->rt_gateway;
1786cfb6b8dfSitojun 
1787cfb6b8dfSitojun 			/*
1788cfb6b8dfSitojun 			 * We skip link-layer address resolution and NUD
1789cfb6b8dfSitojun 			 * if the gateway is not a neighbor from ND point
1790d8a7e3a7Sitojun 			 * of view, regardless of the value of nd_ifinfo.flags.
179134deef1eSitojun 			 * The second condition is a bit tricky; we skip
1792cfb6b8dfSitojun 			 * if the gateway is our own address, which is
1793cfb6b8dfSitojun 			 * sometimes used to install a route to a p2p link.
1794cfb6b8dfSitojun 			 */
1795cfb6b8dfSitojun 			if (!nd6_is_addr_neighbor(gw6, ifp) ||
1796cfb6b8dfSitojun 			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1797cfb6b8dfSitojun 				/*
1798cfb6b8dfSitojun 				 * We allow this kind of tricky route only
1799cfb6b8dfSitojun 				 * when the outgoing interface is p2p.
1800cfb6b8dfSitojun 				 * XXX: we may need a more generic rule here.
1801cfb6b8dfSitojun 				 */
1802cfb6b8dfSitojun 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1803cfb6b8dfSitojun 					senderr(EHOSTUNREACH);
1804cfb6b8dfSitojun 
1805cfb6b8dfSitojun 				goto sendpkt;
1806cfb6b8dfSitojun 			}
1807cfb6b8dfSitojun 
1808287546eaSitojun 			if (rt->rt_gwroute == 0)
1809287546eaSitojun 				goto lookup;
1810287546eaSitojun 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1811287546eaSitojun 				rtfree(rt); rt = rt0;
1812d8a7e3a7Sitojun 			lookup:
1813c241aca8Shenning 				rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0);
1814287546eaSitojun 				if ((rt = rt->rt_gwroute) == 0)
1815287546eaSitojun 					senderr(EHOSTUNREACH);
1816287546eaSitojun 			}
1817287546eaSitojun 		}
1818287546eaSitojun 	}
1819287546eaSitojun 
1820287546eaSitojun 	/*
1821287546eaSitojun 	 * Address resolution or Neighbor Unreachability Detection
1822287546eaSitojun 	 * for the next hop.
1823287546eaSitojun 	 * At this point, the destination of the packet must be a unicast
1824287546eaSitojun 	 * or an anycast address(i.e. not a multicast).
1825287546eaSitojun 	 */
1826287546eaSitojun 
1827287546eaSitojun 	/* Look up the neighbor cache for the nexthop */
1828287546eaSitojun 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1829287546eaSitojun 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1830287546eaSitojun 	else {
1831cfb6b8dfSitojun 		/*
1832cfb6b8dfSitojun 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1833cfb6b8dfSitojun 		 * the condition below is not very efficient.  But we believe
1834cfb6b8dfSitojun 		 * it is tolerable, because this should be a rare case.
1835cfb6b8dfSitojun 		 */
1836cfb6b8dfSitojun 		if (nd6_is_addr_neighbor(dst, ifp) &&
1837cfb6b8dfSitojun 		    (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL)
1838287546eaSitojun 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1839287546eaSitojun 	}
1840287546eaSitojun 	if (!ln || !rt) {
1841cfb6b8dfSitojun 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1842d6b9e9b9Sitojun 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1843cfb6b8dfSitojun 			log(LOG_DEBUG,
1844cfb6b8dfSitojun 			    "nd6_output: can't allocate llinfo for %s "
1845287546eaSitojun 			    "(ln=%p, rt=%p)\n",
1846287546eaSitojun 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
1847287546eaSitojun 			senderr(EIO);	/* XXX: good error? */
1848287546eaSitojun 		}
1849287546eaSitojun 
1850cfb6b8dfSitojun 		goto sendpkt;	/* send anyway */
1851cfb6b8dfSitojun 	}
1852cfb6b8dfSitojun 
1853f3fcf2f3Smcbride 	/*
1854f3fcf2f3Smcbride 	 * Move this entry to the head of the queue so that it is less likely
1855f3fcf2f3Smcbride 	 * for this entry to be a target of forced garbage collection (see
1856f3fcf2f3Smcbride 	 * nd6_rtrequest()).
1857f3fcf2f3Smcbride 	 */
1858f3fcf2f3Smcbride 	LN_DEQUEUE(ln);
1859f3fcf2f3Smcbride 	LN_INSERTHEAD(ln);
1860f3fcf2f3Smcbride 
1861d374aaacSitojun 	/* We don't have to do link-layer address resolution on a p2p link. */
1862d374aaacSitojun 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1863be4e9e12Sitojun 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
1864d374aaacSitojun 		ln->ln_state = ND6_LLINFO_STALE;
18659631a17bSitojun 		nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1866be4e9e12Sitojun 	}
1867287546eaSitojun 
1868287546eaSitojun 	/*
1869287546eaSitojun 	 * The first time we send a packet to a neighbor whose entry is
1870287546eaSitojun 	 * STALE, we have to change the state to DELAY and a sets a timer to
1871287546eaSitojun 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1872287546eaSitojun 	 * neighbor unreachability detection on expiration.
1873287546eaSitojun 	 * (RFC 2461 7.3.3)
1874287546eaSitojun 	 */
1875287546eaSitojun 	if (ln->ln_state == ND6_LLINFO_STALE) {
1876287546eaSitojun 		ln->ln_asked = 0;
1877287546eaSitojun 		ln->ln_state = ND6_LLINFO_DELAY;
18789631a17bSitojun 		nd6_llinfo_settimer(ln, nd6_delay * hz);
1879287546eaSitojun 	}
1880287546eaSitojun 
1881287546eaSitojun 	/*
1882287546eaSitojun 	 * If the neighbor cache entry has a state other than INCOMPLETE
188334deef1eSitojun 	 * (i.e. its link-layer address is already resolved), just
1884287546eaSitojun 	 * send the packet.
1885287546eaSitojun 	 */
1886287546eaSitojun 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1887287546eaSitojun 		goto sendpkt;
1888287546eaSitojun 
1889287546eaSitojun 	/*
1890287546eaSitojun 	 * There is a neighbor cache entry, but no ethernet address
1891287546eaSitojun 	 * response yet.  Replace the held mbuf (if any) with this
1892287546eaSitojun 	 * latest one.
1893287546eaSitojun 	 */
1894efcf292bSitojun 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1895287546eaSitojun 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1896287546eaSitojun 	if (ln->ln_hold)
1897287546eaSitojun 		m_freem(ln->ln_hold);
1898287546eaSitojun 	ln->ln_hold = m;
189976843262Sitojun 	/*
190076843262Sitojun 	 * If there has been no NS for the neighbor after entering the
190176843262Sitojun 	 * INCOMPLETE state, send the first solicitation.
190276843262Sitojun 	 */
19039631a17bSitojun 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
1904287546eaSitojun 		ln->ln_asked++;
19059631a17bSitojun 		nd6_llinfo_settimer(ln,
19069631a17bSitojun 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1907287546eaSitojun 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1908287546eaSitojun 	}
1909287546eaSitojun 	return (0);
1910287546eaSitojun 
1911287546eaSitojun   sendpkt:
1912cb39d30aSangelos #ifdef IPSEC
1913cb39d30aSangelos 	/*
1914cb39d30aSangelos 	 * If the packet needs outgoing IPsec crypto processing and the
1915cb39d30aSangelos 	 * interface doesn't support it, drop it.
1916cb39d30aSangelos 	 */
1917cb39d30aSangelos 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
1918cb39d30aSangelos #endif /* IPSEC */
19196afad192Sitojun 
19200a2c5741Sitojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1921cb39d30aSangelos #ifdef IPSEC
1922cb39d30aSangelos 		if (mtag != NULL &&
1923cb39d30aSangelos 		    (origifp->if_capabilities & IFCAP_IPSEC) == 0) {
1924cb39d30aSangelos 			/* Tell IPsec to do its own crypto. */
1925cb39d30aSangelos 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
1926cb39d30aSangelos 			error = EACCES;
1927cb39d30aSangelos 			goto bad;
1928cb39d30aSangelos 		}
1929cb39d30aSangelos #endif /* IPSEC */
19306afad192Sitojun 		return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst,
19316afad192Sitojun 		    rt));
19326afad192Sitojun 	}
1933cb39d30aSangelos #ifdef IPSEC
1934cb39d30aSangelos 	if (mtag != NULL &&
1935cb39d30aSangelos 	    (ifp->if_capabilities & IFCAP_IPSEC) == 0) {
1936cb39d30aSangelos 		/* Tell IPsec to do its own crypto. */
1937cb39d30aSangelos 		ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
1938cb39d30aSangelos 		error = EACCES;
1939cb39d30aSangelos 		goto bad;
1940cb39d30aSangelos 	}
1941cb39d30aSangelos #endif /* IPSEC */
1942287546eaSitojun 	return ((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt));
1943287546eaSitojun 
1944287546eaSitojun   bad:
1945287546eaSitojun 	if (m)
1946287546eaSitojun 		m_freem(m);
1947287546eaSitojun 	return (error);
1948287546eaSitojun }
1949287546eaSitojun #undef senderr
1950287546eaSitojun 
1951287546eaSitojun int
1952ee37ea65Smcbride nd6_need_cache(struct ifnet *ifp)
1953d8a7e3a7Sitojun {
1954d8a7e3a7Sitojun 	/*
1955d8a7e3a7Sitojun 	 * XXX: we currently do not make neighbor cache on any interface
195640765843Shenning 	 * other than Ethernet, FDDI and GIF.
1957d8a7e3a7Sitojun 	 *
1958d8a7e3a7Sitojun 	 * RFC2893 says:
1959d8a7e3a7Sitojun 	 * - unidirectional tunnels needs no ND
1960d8a7e3a7Sitojun 	 */
1961d8a7e3a7Sitojun 	switch (ifp->if_type) {
1962d8a7e3a7Sitojun 	case IFT_ETHER:
1963d8a7e3a7Sitojun 	case IFT_FDDI:
1964d8a7e3a7Sitojun 	case IFT_IEEE1394:
1965d8a7e3a7Sitojun 	case IFT_PROPVIRTUAL:
1966d8a7e3a7Sitojun 	case IFT_L2VLAN:
1967d8a7e3a7Sitojun 	case IFT_IEEE80211:
1968f4433d56Shenning 	case IFT_CARP:
1969d8a7e3a7Sitojun 	case IFT_GIF:		/* XXX need more cases? */
1970d8a7e3a7Sitojun 		return (1);
1971d8a7e3a7Sitojun 	default:
1972d8a7e3a7Sitojun 		return (0);
1973d8a7e3a7Sitojun 	}
1974d8a7e3a7Sitojun }
1975d8a7e3a7Sitojun 
1976d8a7e3a7Sitojun int
1977ee37ea65Smcbride nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
1978ee37ea65Smcbride     struct sockaddr *dst, u_char *desten)
1979287546eaSitojun {
1980287546eaSitojun 	struct sockaddr_dl *sdl;
1981287546eaSitojun 
1982287546eaSitojun 	if (m->m_flags & M_MCAST) {
1983287546eaSitojun 		switch (ifp->if_type) {
1984287546eaSitojun 		case IFT_ETHER:
1985287546eaSitojun 		case IFT_FDDI:
1986287546eaSitojun 			ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr,
1987287546eaSitojun 						 desten);
1988287546eaSitojun 			return (1);
1989287546eaSitojun 			break;
1990287546eaSitojun 		default:
19910a2c5741Sitojun 			m_freem(m);
1992287546eaSitojun 			return (0);
1993287546eaSitojun 		}
1994287546eaSitojun 	}
1995287546eaSitojun 
199672e30262Sitojun 	if (rt == NULL) {
199772e30262Sitojun 		/* this could happen, if we could not allocate memory */
19980a2c5741Sitojun 		m_freem(m);
199972e30262Sitojun 		return (0);
200072e30262Sitojun 	}
200172e30262Sitojun 	if (rt->rt_gateway->sa_family != AF_LINK) {
2002287546eaSitojun 		printf("nd6_storelladdr: something odd happens\n");
20030a2c5741Sitojun 		m_freem(m);
2004287546eaSitojun 		return (0);
2005287546eaSitojun 	}
2006287546eaSitojun 	sdl = SDL(rt->rt_gateway);
2007b4ee66fcSitojun 	if (sdl->sdl_alen == 0) {
2008bd2806bcSitojun 		/* this should be impossible, but we bark here for debugging */
2009d8a7e3a7Sitojun 		printf("nd6_storelladdr: sdl_alen == 0, dst=%s, if=%s\n",
2010d8a7e3a7Sitojun 		    ip6_sprintf(&SIN6(dst)->sin6_addr), ifp->if_xname);
20110a2c5741Sitojun 		m_freem(m);
2012b4ee66fcSitojun 		return (0);
2013b4ee66fcSitojun 	}
2014287546eaSitojun 
2015b4ee66fcSitojun 	bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
2016287546eaSitojun 	return (1);
2017287546eaSitojun }
2018d8a7e3a7Sitojun 
2019ee37ea65Smcbride /*
2020ee37ea65Smcbride  * oldp - syscall arg, need copyout
2021ee37ea65Smcbride  * newp - syscall arg, need copyin
2022ee37ea65Smcbride  */
2023ee37ea65Smcbride 
2024d8a7e3a7Sitojun int
2025*a0aa363cSjsing nd6_sysctl(int name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
2026d8a7e3a7Sitojun {
2027db3db419Sitojun 	void *p;
202834223ca4Schl 	size_t ol;
2029d8a7e3a7Sitojun 	int error;
2030d8a7e3a7Sitojun 
2031d8a7e3a7Sitojun 	error = 0;
2032d8a7e3a7Sitojun 
2033d8a7e3a7Sitojun 	if (newp)
2034d8a7e3a7Sitojun 		return EPERM;
2035d8a7e3a7Sitojun 	if (oldp && !oldlenp)
2036d8a7e3a7Sitojun 		return EINVAL;
2037d8a7e3a7Sitojun 	ol = oldlenp ? *oldlenp : 0;
2038d8a7e3a7Sitojun 
2039db3db419Sitojun 	if (oldp) {
2040db3db419Sitojun 		p = malloc(*oldlenp, M_TEMP, M_WAITOK);
2041db3db419Sitojun 		if (!p)
2042db3db419Sitojun 			return ENOMEM;
2043db3db419Sitojun 	} else
2044db3db419Sitojun 		p = NULL;
2045d8a7e3a7Sitojun 	switch (name) {
2046d8a7e3a7Sitojun 	case ICMPV6CTL_ND6_DRLIST:
2047db3db419Sitojun 		error = fill_drlist(p, oldlenp, ol);
2048db3db419Sitojun 		if (!error && p && oldp)
2049732568ffSitojun 			error = copyout(p, oldp, *oldlenp);
2050d8a7e3a7Sitojun 		break;
2051d8a7e3a7Sitojun 
2052d8a7e3a7Sitojun 	case ICMPV6CTL_ND6_PRLIST:
2053db3db419Sitojun 		error = fill_prlist(p, oldlenp, ol);
2054db3db419Sitojun 		if (!error && p && oldp)
2055732568ffSitojun 			error = copyout(p, oldp, *oldlenp);
2056d8a7e3a7Sitojun 		break;
2057d8a7e3a7Sitojun 
2058d8a7e3a7Sitojun 	default:
2059d8a7e3a7Sitojun 		error = ENOPROTOOPT;
2060d8a7e3a7Sitojun 		break;
2061d8a7e3a7Sitojun 	}
2062db3db419Sitojun 	if (p)
2063db3db419Sitojun 		free(p, M_TEMP);
2064d8a7e3a7Sitojun 
2065d8a7e3a7Sitojun 	return (error);
2066d8a7e3a7Sitojun }
2067d8a7e3a7Sitojun 
2068*a0aa363cSjsing int
2069ee37ea65Smcbride fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
2070d8a7e3a7Sitojun {
2071d8a7e3a7Sitojun 	int error = 0, s;
2072d8a7e3a7Sitojun 	struct in6_defrouter *d = NULL, *de = NULL;
2073d8a7e3a7Sitojun 	struct nd_defrouter *dr;
2074d8a7e3a7Sitojun 	size_t l;
2075d8a7e3a7Sitojun 
20766e92dee6Sitojun 	s = splsoftnet();
2077d8a7e3a7Sitojun 
2078d8a7e3a7Sitojun 	if (oldp) {
2079d8a7e3a7Sitojun 		d = (struct in6_defrouter *)oldp;
2080d8a7e3a7Sitojun 		de = (struct in6_defrouter *)((caddr_t)oldp + *oldlenp);
2081d8a7e3a7Sitojun 	}
2082d8a7e3a7Sitojun 	l = 0;
2083d8a7e3a7Sitojun 
2084d8a7e3a7Sitojun 	for (dr = TAILQ_FIRST(&nd_defrouter); dr;
2085d8a7e3a7Sitojun 	     dr = TAILQ_NEXT(dr, dr_entry)) {
2086d8a7e3a7Sitojun 
2087d8a7e3a7Sitojun 		if (oldp && d + 1 <= de) {
2088d8a7e3a7Sitojun 			bzero(d, sizeof(*d));
2089d8a7e3a7Sitojun 			d->rtaddr.sin6_family = AF_INET6;
2090d8a7e3a7Sitojun 			d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
2091d8a7e3a7Sitojun 			d->rtaddr.sin6_addr = dr->rtaddr;
2092d8a7e3a7Sitojun 			in6_recoverscope(&d->rtaddr, &d->rtaddr.sin6_addr,
2093d8a7e3a7Sitojun 			    dr->ifp);
2094d8a7e3a7Sitojun 			d->flags = dr->flags;
2095d8a7e3a7Sitojun 			d->rtlifetime = dr->rtlifetime;
2096d8a7e3a7Sitojun 			d->expire = dr->expire;
2097d8a7e3a7Sitojun 			d->if_index = dr->ifp->if_index;
2098d8a7e3a7Sitojun 		}
2099d8a7e3a7Sitojun 
2100d8a7e3a7Sitojun 		l += sizeof(*d);
2101d8a7e3a7Sitojun 		if (d)
2102d8a7e3a7Sitojun 			d++;
2103d8a7e3a7Sitojun 	}
2104d8a7e3a7Sitojun 
2105d8a7e3a7Sitojun 	if (oldp) {
2106d8a7e3a7Sitojun 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
2107d8a7e3a7Sitojun 		if (l > ol)
2108d8a7e3a7Sitojun 			error = ENOMEM;
2109d8a7e3a7Sitojun 	} else
2110d8a7e3a7Sitojun 		*oldlenp = l;
2111d8a7e3a7Sitojun 
2112d8a7e3a7Sitojun 	splx(s);
2113d8a7e3a7Sitojun 
2114d8a7e3a7Sitojun 	return (error);
2115d8a7e3a7Sitojun }
2116d8a7e3a7Sitojun 
2117*a0aa363cSjsing int
2118ee37ea65Smcbride fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
2119d8a7e3a7Sitojun {
2120d8a7e3a7Sitojun 	int error = 0, s;
2121d8a7e3a7Sitojun 	struct nd_prefix *pr;
2122d8a7e3a7Sitojun 	struct in6_prefix *p = NULL;
2123d8a7e3a7Sitojun 	struct in6_prefix *pe = NULL;
2124d8a7e3a7Sitojun 	size_t l;
2125d8a7e3a7Sitojun 
21266e92dee6Sitojun 	s = splsoftnet();
2127d8a7e3a7Sitojun 
2128d8a7e3a7Sitojun 	if (oldp) {
2129d8a7e3a7Sitojun 		p = (struct in6_prefix *)oldp;
2130d8a7e3a7Sitojun 		pe = (struct in6_prefix *)((caddr_t)oldp + *oldlenp);
2131d8a7e3a7Sitojun 	}
2132d8a7e3a7Sitojun 	l = 0;
2133d8a7e3a7Sitojun 
2134545205eaSpyr 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
2135d8a7e3a7Sitojun 		u_short advrtrs;
2136d8a7e3a7Sitojun 		size_t advance;
2137d8a7e3a7Sitojun 		struct sockaddr_in6 *sin6;
2138d8a7e3a7Sitojun 		struct sockaddr_in6 *s6;
2139d8a7e3a7Sitojun 		struct nd_pfxrouter *pfr;
2140d8a7e3a7Sitojun 
2141d8a7e3a7Sitojun 		if (oldp && p + 1 <= pe)
2142d8a7e3a7Sitojun 		{
2143d8a7e3a7Sitojun 			bzero(p, sizeof(*p));
2144d8a7e3a7Sitojun 			sin6 = (struct sockaddr_in6 *)(p + 1);
2145d8a7e3a7Sitojun 
2146d8a7e3a7Sitojun 			p->prefix = pr->ndpr_prefix;
2147d8a7e3a7Sitojun 			if (in6_recoverscope(&p->prefix,
2148d8a7e3a7Sitojun 			    &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2149d8a7e3a7Sitojun 				log(LOG_ERR,
2150d8a7e3a7Sitojun 				    "scope error in prefix list (%s)\n",
2151d8a7e3a7Sitojun 				    ip6_sprintf(&p->prefix.sin6_addr));
2152d8a7e3a7Sitojun 			p->raflags = pr->ndpr_raf;
2153d8a7e3a7Sitojun 			p->prefixlen = pr->ndpr_plen;
2154d8a7e3a7Sitojun 			p->vltime = pr->ndpr_vltime;
2155d8a7e3a7Sitojun 			p->pltime = pr->ndpr_pltime;
2156d8a7e3a7Sitojun 			p->if_index = pr->ndpr_ifp->if_index;
2157d8a7e3a7Sitojun 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2158d8a7e3a7Sitojun 				p->expire = 0;
2159d8a7e3a7Sitojun 			else {
2160d8a7e3a7Sitojun 				time_t maxexpire;
2161d8a7e3a7Sitojun 
2162d8a7e3a7Sitojun 				/* XXX: we assume time_t is signed. */
2163d8a7e3a7Sitojun 				maxexpire = (-1) &
2164d8a7e3a7Sitojun 					~(1 << ((sizeof(maxexpire) * 8) - 1));
2165d8a7e3a7Sitojun 				if (pr->ndpr_vltime <
2166d8a7e3a7Sitojun 				    maxexpire - pr->ndpr_lastupdate) {
2167d8a7e3a7Sitojun 					p->expire = pr->ndpr_lastupdate +
2168d8a7e3a7Sitojun 						pr->ndpr_vltime;
2169d8a7e3a7Sitojun 				} else
2170d8a7e3a7Sitojun 					p->expire = maxexpire;
2171d8a7e3a7Sitojun 			}
2172d8a7e3a7Sitojun 			p->refcnt = pr->ndpr_refcnt;
2173d8a7e3a7Sitojun 			p->flags = pr->ndpr_stateflags;
2174d8a7e3a7Sitojun 			p->origin = PR_ORIG_RA;
2175d8a7e3a7Sitojun 			advrtrs = 0;
2176545205eaSpyr 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2177d8a7e3a7Sitojun 				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2178d8a7e3a7Sitojun 					advrtrs++;
2179d8a7e3a7Sitojun 					continue;
2180d8a7e3a7Sitojun 				}
2181d8a7e3a7Sitojun 				s6 = &sin6[advrtrs];
2182d8a7e3a7Sitojun 				s6->sin6_family = AF_INET6;
2183d8a7e3a7Sitojun 				s6->sin6_len = sizeof(struct sockaddr_in6);
2184d8a7e3a7Sitojun 				s6->sin6_addr = pfr->router->rtaddr;
2185d8a7e3a7Sitojun 				in6_recoverscope(s6, &pfr->router->rtaddr,
2186d8a7e3a7Sitojun 				    pfr->router->ifp);
2187d8a7e3a7Sitojun 				advrtrs++;
2188d8a7e3a7Sitojun 			}
2189d8a7e3a7Sitojun 			p->advrtrs = advrtrs;
2190d8a7e3a7Sitojun 		}
2191d8a7e3a7Sitojun 		else {
2192d8a7e3a7Sitojun 			advrtrs = 0;
2193545205eaSpyr 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2194d8a7e3a7Sitojun 				advrtrs++;
2195d8a7e3a7Sitojun 		}
2196d8a7e3a7Sitojun 
2197d8a7e3a7Sitojun 		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2198d8a7e3a7Sitojun 		l += advance;
2199d8a7e3a7Sitojun 		if (p)
2200d8a7e3a7Sitojun 			p = (struct in6_prefix *)((caddr_t)p + advance);
2201d8a7e3a7Sitojun 	}
2202d8a7e3a7Sitojun 
2203d8a7e3a7Sitojun 	if (oldp) {
2204d8a7e3a7Sitojun 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
2205d8a7e3a7Sitojun 		if (l > ol)
2206d8a7e3a7Sitojun 			error = ENOMEM;
2207d8a7e3a7Sitojun 	} else
2208d8a7e3a7Sitojun 		*oldlenp = l;
2209d8a7e3a7Sitojun 
2210d8a7e3a7Sitojun 	splx(s);
2211d8a7e3a7Sitojun 
2212d8a7e3a7Sitojun 	return (error);
2213d8a7e3a7Sitojun }
2214