xref: /openbsd/sys/netinet6/nd6.c (revision c3c56496)
1*c3c56496Sbluhm /*	$OpenBSD: nd6.c,v 1.99 2013/05/31 15:04:24 bluhm 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 SDL(s) ((struct sockaddr_dl *)s)
69287546eaSitojun 
70287546eaSitojun /* timer values */
71287546eaSitojun int	nd6_prune	= 1;	/* walk list every 1 seconds */
72287546eaSitojun int	nd6_delay	= 5;	/* delay first probe time 5 second */
73287546eaSitojun int	nd6_umaxtries	= 3;	/* maximum unicast query */
74287546eaSitojun int	nd6_mmaxtries	= 3;	/* maximum multicast query */
75287546eaSitojun int	nd6_useloopback = 1;	/* use loopback interface for local traffic */
76be4e9e12Sitojun int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
77287546eaSitojun 
78287546eaSitojun /* preventing too many loops in ND option parsing */
79287546eaSitojun int nd6_maxndopt = 10;	/* max # of ND options allowed */
80287546eaSitojun 
81f6e55599Sitojun int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
82f6e55599Sitojun 
83b79da24aSitojun #ifdef ND6_DEBUG
84b79da24aSitojun int nd6_debug = 1;
85b79da24aSitojun #else
86b79da24aSitojun int nd6_debug = 0;
87b79da24aSitojun #endif
88b79da24aSitojun 
89287546eaSitojun static int nd6_inuse, nd6_allocated;
90287546eaSitojun 
91287546eaSitojun struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
92f4f4d166Sitojun struct nd_drhead nd_defrouter;
93287546eaSitojun struct nd_prhead nd_prefix = { 0 };
94287546eaSitojun 
95287546eaSitojun int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
96287546eaSitojun static struct sockaddr_in6 all1_sa;
97287546eaSitojun 
98a0aa363cSjsing void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *);
99a0aa363cSjsing void nd6_slowtimo(void *);
100a0aa363cSjsing struct llinfo_nd6 *nd6_free(struct rtentry *, int);
101a0aa363cSjsing void nd6_llinfo_timer(void *);
102287546eaSitojun 
103b3c1e4c1Sitojun struct timeout nd6_slowtimo_ch;
104b3c1e4c1Sitojun struct timeout nd6_timer_ch;
105b3c1e4c1Sitojun 
106a0aa363cSjsing int fill_drlist(void *, size_t *, size_t);
107a0aa363cSjsing int fill_prlist(void *, size_t *, size_t);
108d8a7e3a7Sitojun 
109f3fcf2f3Smcbride #define LN_DEQUEUE(ln) do { \
110f3fcf2f3Smcbride 	(ln)->ln_next->ln_prev = (ln)->ln_prev; \
111f3fcf2f3Smcbride 	(ln)->ln_prev->ln_next = (ln)->ln_next; \
112f3fcf2f3Smcbride 	} while (0)
113f3fcf2f3Smcbride #define LN_INSERTHEAD(ln) do { \
114f3fcf2f3Smcbride 	(ln)->ln_next = llinfo_nd6.ln_next; \
115f3fcf2f3Smcbride 	llinfo_nd6.ln_next = (ln); \
116f3fcf2f3Smcbride 	(ln)->ln_prev = &llinfo_nd6; \
117f3fcf2f3Smcbride 	(ln)->ln_next->ln_prev = (ln); \
118f3fcf2f3Smcbride 	} while (0)
119f3fcf2f3Smcbride 
120287546eaSitojun void
121a0aa363cSjsing nd6_init(void)
122287546eaSitojun {
123287546eaSitojun 	static int nd6_init_done = 0;
124287546eaSitojun 	int i;
125287546eaSitojun 
126287546eaSitojun 	if (nd6_init_done) {
127287546eaSitojun 		log(LOG_NOTICE, "nd6_init called more than once(ignored)\n");
128287546eaSitojun 		return;
129287546eaSitojun 	}
130287546eaSitojun 
131287546eaSitojun 	all1_sa.sin6_family = AF_INET6;
132287546eaSitojun 	all1_sa.sin6_len = sizeof(struct sockaddr_in6);
133287546eaSitojun 	for (i = 0; i < sizeof(all1_sa.sin6_addr); i++)
134287546eaSitojun 		all1_sa.sin6_addr.s6_addr[i] = 0xff;
135287546eaSitojun 
136f4f4d166Sitojun 	/* initialization of the default router list */
137f4f4d166Sitojun 	TAILQ_INIT(&nd_defrouter);
138f4f4d166Sitojun 
139287546eaSitojun 	nd6_init_done = 1;
140287546eaSitojun 
141287546eaSitojun 	/* start timer */
142b3c1e4c1Sitojun 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
14329e86e5eSblambert 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
144287546eaSitojun }
145287546eaSitojun 
146d6b9e9b9Sitojun struct nd_ifinfo *
147ee37ea65Smcbride nd6_ifattach(struct ifnet *ifp)
148287546eaSitojun {
149d6b9e9b9Sitojun 	struct nd_ifinfo *nd;
150287546eaSitojun 
151393af863Skrw 	nd = malloc(sizeof(*nd), M_IP6NDP, M_WAITOK | M_ZERO);
152287546eaSitojun 
153d6b9e9b9Sitojun 	nd->initialized = 1;
154287546eaSitojun 
155d6b9e9b9Sitojun 	nd->chlim = IPV6_DEFHLIM;
156d6b9e9b9Sitojun 	nd->basereachable = REACHABLE_TIME;
157d6b9e9b9Sitojun 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
158d6b9e9b9Sitojun 	nd->retrans = RETRANS_TIMER;
159d8a7e3a7Sitojun 	/*
160d8a7e3a7Sitojun 	 * Note that the default value of ip6_accept_rtadv is 0, which means
161d8a7e3a7Sitojun 	 * we won't accept RAs by default even if we set ND6_IFF_ACCEPT_RTADV
162d8a7e3a7Sitojun 	 * here.
163d8a7e3a7Sitojun 	 */
164d8a7e3a7Sitojun 	nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
1651b5f410aSitojun 
1661b5f410aSitojun 	/* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */
1671b5f410aSitojun 	nd6_setmtu0(ifp, nd);
168d6b9e9b9Sitojun 
169d6b9e9b9Sitojun 	return nd;
170287546eaSitojun }
171287546eaSitojun 
172d6b9e9b9Sitojun void
173ee37ea65Smcbride nd6_ifdetach(struct nd_ifinfo *nd)
174d6b9e9b9Sitojun {
175d374aaacSitojun 
176d6b9e9b9Sitojun 	free(nd, M_IP6NDP);
177287546eaSitojun }
178287546eaSitojun 
179287546eaSitojun void
180ee37ea65Smcbride nd6_setmtu(struct ifnet *ifp)
1811b5f410aSitojun {
1821b5f410aSitojun 	nd6_setmtu0(ifp, ND_IFINFO(ifp));
1831b5f410aSitojun }
1841b5f410aSitojun 
1851b5f410aSitojun void
186ee37ea65Smcbride nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi)
187287546eaSitojun {
1881b5f410aSitojun 	u_int32_t omaxmtu;
1891b5f410aSitojun 
1901b5f410aSitojun 	omaxmtu = ndi->maxmtu;
191287546eaSitojun 
19240765843Shenning 	if (ifp->if_type == IFT_FDDI)
193d6b9e9b9Sitojun 		ndi->maxmtu = MIN(FDDIMTU, ifp->if_mtu);
19440765843Shenning 	else
195287546eaSitojun 		ndi->maxmtu = ifp->if_mtu;
196287546eaSitojun 
1971b5f410aSitojun 	/*
1981b5f410aSitojun 	 * Decreasing the interface MTU under IPV6 minimum MTU may cause
1991b5f410aSitojun 	 * undesirable situation.  We thus notify the operator of the change
2001b5f410aSitojun 	 * explicitly.  The check for omaxmtu is necessary to restrict the
2011b5f410aSitojun 	 * log to the case of changing the MTU, not initializing it.
2021b5f410aSitojun 	 */
2031b5f410aSitojun 	if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) {
2041b5f410aSitojun 		log(LOG_NOTICE, "nd6_setmtu0: "
2051b5f410aSitojun 		    "new link MTU on %s (%lu) is too small for IPv6\n",
2061b5f410aSitojun 		    ifp->if_xname, (unsigned long)ndi->maxmtu);
207287546eaSitojun 	}
208d6b9e9b9Sitojun 
209d6b9e9b9Sitojun 	if (ndi->maxmtu > in6_maxmtu)
210d6b9e9b9Sitojun 		in6_setmaxmtu(); /* check all interfaces just in case */
211287546eaSitojun }
212287546eaSitojun 
213287546eaSitojun void
214ee37ea65Smcbride nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
215287546eaSitojun {
216287546eaSitojun 	bzero(ndopts, sizeof(*ndopts));
217287546eaSitojun 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
218287546eaSitojun 	ndopts->nd_opts_last
219287546eaSitojun 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
220287546eaSitojun 
221287546eaSitojun 	if (icmp6len == 0) {
222287546eaSitojun 		ndopts->nd_opts_done = 1;
223287546eaSitojun 		ndopts->nd_opts_search = NULL;
224287546eaSitojun 	}
225287546eaSitojun }
226287546eaSitojun 
227287546eaSitojun /*
228287546eaSitojun  * Take one ND option.
229287546eaSitojun  */
230287546eaSitojun struct nd_opt_hdr *
231ee37ea65Smcbride nd6_option(union nd_opts *ndopts)
232287546eaSitojun {
233287546eaSitojun 	struct nd_opt_hdr *nd_opt;
234287546eaSitojun 	int olen;
235287546eaSitojun 
236287546eaSitojun 	if (!ndopts)
237bc84bce2Skrw 		panic("ndopts == NULL in nd6_option");
238287546eaSitojun 	if (!ndopts->nd_opts_last)
239bc84bce2Skrw 		panic("uninitialized ndopts in nd6_option");
240287546eaSitojun 	if (!ndopts->nd_opts_search)
241287546eaSitojun 		return NULL;
242287546eaSitojun 	if (ndopts->nd_opts_done)
243287546eaSitojun 		return NULL;
244287546eaSitojun 
245287546eaSitojun 	nd_opt = ndopts->nd_opts_search;
246287546eaSitojun 
24715bd77d2Sitojun 	/* make sure nd_opt_len is inside the buffer */
24815bd77d2Sitojun 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
24915bd77d2Sitojun 		bzero(ndopts, sizeof(*ndopts));
25015bd77d2Sitojun 		return NULL;
25115bd77d2Sitojun 	}
25215bd77d2Sitojun 
253287546eaSitojun 	olen = nd_opt->nd_opt_len << 3;
254287546eaSitojun 	if (olen == 0) {
255287546eaSitojun 		/*
256287546eaSitojun 		 * Message validation requires that all included
257287546eaSitojun 		 * options have a length that is greater than zero.
258287546eaSitojun 		 */
259287546eaSitojun 		bzero(ndopts, sizeof(*ndopts));
260287546eaSitojun 		return NULL;
261287546eaSitojun 	}
262287546eaSitojun 
263287546eaSitojun 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
26415bd77d2Sitojun 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
26515bd77d2Sitojun 		/* option overruns the end of buffer, invalid */
26615bd77d2Sitojun 		bzero(ndopts, sizeof(*ndopts));
26715bd77d2Sitojun 		return NULL;
26815bd77d2Sitojun 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
26915bd77d2Sitojun 		/* reached the end of options chain */
270287546eaSitojun 		ndopts->nd_opts_done = 1;
271287546eaSitojun 		ndopts->nd_opts_search = NULL;
272287546eaSitojun 	}
273287546eaSitojun 	return nd_opt;
274287546eaSitojun }
275287546eaSitojun 
276287546eaSitojun /*
277287546eaSitojun  * Parse multiple ND options.
278287546eaSitojun  * This function is much easier to use, for ND routines that do not need
279287546eaSitojun  * multiple options of the same type.
280287546eaSitojun  */
281287546eaSitojun int
282ee37ea65Smcbride nd6_options(union nd_opts *ndopts)
283287546eaSitojun {
284287546eaSitojun 	struct nd_opt_hdr *nd_opt;
285287546eaSitojun 	int i = 0;
286287546eaSitojun 
287287546eaSitojun 	if (!ndopts)
288bc84bce2Skrw 		panic("ndopts == NULL in nd6_options");
289287546eaSitojun 	if (!ndopts->nd_opts_last)
290bc84bce2Skrw 		panic("uninitialized ndopts in nd6_options");
291287546eaSitojun 	if (!ndopts->nd_opts_search)
292287546eaSitojun 		return 0;
293287546eaSitojun 
294287546eaSitojun 	while (1) {
295287546eaSitojun 		nd_opt = nd6_option(ndopts);
296287546eaSitojun 		if (!nd_opt && !ndopts->nd_opts_last) {
297287546eaSitojun 			/*
298287546eaSitojun 			 * Message validation requires that all included
299287546eaSitojun 			 * options have a length that is greater than zero.
300287546eaSitojun 			 */
301b79da24aSitojun 			icmp6stat.icp6s_nd_badopt++;
302287546eaSitojun 			bzero(ndopts, sizeof(*ndopts));
303287546eaSitojun 			return -1;
304287546eaSitojun 		}
305287546eaSitojun 
306287546eaSitojun 		if (!nd_opt)
307287546eaSitojun 			goto skip1;
308287546eaSitojun 
309287546eaSitojun 		switch (nd_opt->nd_opt_type) {
310287546eaSitojun 		case ND_OPT_SOURCE_LINKADDR:
311287546eaSitojun 		case ND_OPT_TARGET_LINKADDR:
312287546eaSitojun 		case ND_OPT_MTU:
313287546eaSitojun 		case ND_OPT_REDIRECTED_HEADER:
314287546eaSitojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
31515bd77d2Sitojun 				nd6log((LOG_INFO,
31615bd77d2Sitojun 				    "duplicated ND6 option found (type=%d)\n",
31715bd77d2Sitojun 				    nd_opt->nd_opt_type));
318287546eaSitojun 				/* XXX bark? */
319287546eaSitojun 			} else {
320287546eaSitojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
321287546eaSitojun 					= nd_opt;
322287546eaSitojun 			}
323287546eaSitojun 			break;
324287546eaSitojun 		case ND_OPT_PREFIX_INFORMATION:
325287546eaSitojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
326287546eaSitojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
327287546eaSitojun 					= nd_opt;
328287546eaSitojun 			}
329287546eaSitojun 			ndopts->nd_opts_pi_end =
330287546eaSitojun 				(struct nd_opt_prefix_info *)nd_opt;
331287546eaSitojun 			break;
332287546eaSitojun 		default:
333287546eaSitojun 			/*
334287546eaSitojun 			 * Unknown options must be silently ignored,
335e4d25771Stodd 			 * to accommodate future extension to the protocol.
336287546eaSitojun 			 */
337b79da24aSitojun 			nd6log((LOG_DEBUG,
338287546eaSitojun 			    "nd6_options: unsupported option %d - "
339b79da24aSitojun 			    "option ignored\n", nd_opt->nd_opt_type));
340287546eaSitojun 		}
341287546eaSitojun 
342287546eaSitojun skip1:
343287546eaSitojun 		i++;
344287546eaSitojun 		if (i > nd6_maxndopt) {
345287546eaSitojun 			icmp6stat.icp6s_nd_toomanyopt++;
346b79da24aSitojun 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
347287546eaSitojun 			break;
348287546eaSitojun 		}
349287546eaSitojun 
350287546eaSitojun 		if (ndopts->nd_opts_done)
351287546eaSitojun 			break;
352287546eaSitojun 	}
353287546eaSitojun 
354287546eaSitojun 	return 0;
355287546eaSitojun }
356287546eaSitojun 
357287546eaSitojun /*
3589631a17bSitojun  * ND6 timer routine to handle ND6 entries
359287546eaSitojun  */
360287546eaSitojun void
3619631a17bSitojun nd6_llinfo_settimer(struct llinfo_nd6 *ln, long tick)
3629631a17bSitojun {
3639631a17bSitojun 	int s;
3649631a17bSitojun 
3659631a17bSitojun 	s = splsoftnet();
3669631a17bSitojun 
3679631a17bSitojun 	if (tick < 0) {
3689631a17bSitojun 		ln->ln_expire = 0;
3699631a17bSitojun 		ln->ln_ntick = 0;
3709631a17bSitojun 		timeout_del(&ln->ln_timer_ch);
3719631a17bSitojun 	} else {
3723212dc31Stholo 		ln->ln_expire = time_second + tick / hz;
3739631a17bSitojun 		if (tick > INT_MAX) {
3749631a17bSitojun 			ln->ln_ntick = tick - INT_MAX;
3759631a17bSitojun 			timeout_add(&ln->ln_timer_ch, INT_MAX);
3769631a17bSitojun 		} else {
3779631a17bSitojun 			ln->ln_ntick = 0;
3789631a17bSitojun 			timeout_add(&ln->ln_timer_ch, tick);
3799631a17bSitojun 		}
3809631a17bSitojun 	}
3819631a17bSitojun 
3829631a17bSitojun 	splx(s);
3839631a17bSitojun }
3849631a17bSitojun 
385a0aa363cSjsing void
3869631a17bSitojun nd6_llinfo_timer(void *arg)
387287546eaSitojun {
388287546eaSitojun 	int s;
389b3c1e4c1Sitojun 	struct llinfo_nd6 *ln;
390287546eaSitojun 	struct rtentry *rt;
391287546eaSitojun 	struct sockaddr_in6 *dst;
3929631a17bSitojun 	struct ifnet *ifp;
393d374aaacSitojun 	struct nd_ifinfo *ndi = NULL;
394287546eaSitojun 
3959631a17bSitojun 	s = splsoftnet();
3969631a17bSitojun 
3979631a17bSitojun 	ln = (struct llinfo_nd6 *)arg;
3989631a17bSitojun 
3999631a17bSitojun 	if (ln->ln_ntick > 0) {
4009631a17bSitojun 		if (ln->ln_ntick > INT_MAX) {
4019631a17bSitojun 			ln->ln_ntick -= INT_MAX;
4029631a17bSitojun 			nd6_llinfo_settimer(ln, INT_MAX);
4039631a17bSitojun 		} else {
4049631a17bSitojun 			ln->ln_ntick = 0;
4059631a17bSitojun 			nd6_llinfo_settimer(ln, ln->ln_ntick);
406287546eaSitojun 		}
4079631a17bSitojun 		splx(s);
4089631a17bSitojun 		return;
409287546eaSitojun 	}
4109631a17bSitojun 
4119631a17bSitojun 	if ((rt = ln->ln_rt) == NULL)
4129631a17bSitojun 		panic("ln->ln_rt == NULL");
4139631a17bSitojun 	if ((ifp = rt->rt_ifp) == NULL)
4149631a17bSitojun 		panic("ln->ln_rt->rt_ifp == NULL");
415d6b9e9b9Sitojun 	ndi = ND_IFINFO(ifp);
416*c3c56496Sbluhm 	dst = satosin6(rt_key(rt));
417287546eaSitojun 
418287546eaSitojun 	/* sanity check */
419d374aaacSitojun 	if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
420bc84bce2Skrw 		panic("rt_llinfo(%p) is not equal to ln(%p)",
421d374aaacSitojun 		      rt->rt_llinfo, ln);
422287546eaSitojun 	if (!dst)
423bc84bce2Skrw 		panic("dst=0 in nd6_timer(ln=%p)", ln);
424287546eaSitojun 
425287546eaSitojun 	switch (ln->ln_state) {
426287546eaSitojun 	case ND6_LLINFO_INCOMPLETE:
427287546eaSitojun 		if (ln->ln_asked < nd6_mmaxtries) {
428287546eaSitojun 			ln->ln_asked++;
4299631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
4309631a17bSitojun 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
431287546eaSitojun 		} else {
432287546eaSitojun 			struct mbuf *m = ln->ln_hold;
433287546eaSitojun 			if (m) {
4348294a4dfSitojun 				ln->ln_hold = NULL;
435287546eaSitojun 				/*
436d8a7e3a7Sitojun 				 * Fake rcvif to make the ICMP error
437d8a7e3a7Sitojun 				 * more helpful in diagnosing for the
438d8a7e3a7Sitojun 				 * receiver.
439287546eaSitojun 				 * XXX: should we consider
440287546eaSitojun 				 * older rcvif?
441287546eaSitojun 				 */
442287546eaSitojun 				m->m_pkthdr.rcvif = rt->rt_ifp;
443d8a7e3a7Sitojun 
444287546eaSitojun 				icmp6_error(m, ICMP6_DST_UNREACH,
445287546eaSitojun 				    ICMP6_DST_UNREACH_ADDR, 0);
446e212adedSkrw 				if (ln->ln_hold == m) {
447e212adedSkrw 					/* m is back in ln_hold. Discard. */
448e212adedSkrw 					m_freem(ln->ln_hold);
449e212adedSkrw 					ln->ln_hold = NULL;
450e212adedSkrw 				}
451287546eaSitojun 			}
4529631a17bSitojun 			(void)nd6_free(rt, 0);
4539631a17bSitojun 			ln = NULL;
454287546eaSitojun 		}
455287546eaSitojun 		break;
456287546eaSitojun 	case ND6_LLINFO_REACHABLE:
4579631a17bSitojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
458287546eaSitojun 			ln->ln_state = ND6_LLINFO_STALE;
4599631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
460be4e9e12Sitojun 		}
461287546eaSitojun 		break;
462be4e9e12Sitojun 
463be4e9e12Sitojun 	case ND6_LLINFO_STALE:
464f3fcf2f3Smcbride 	case ND6_LLINFO_PURGE:
465be4e9e12Sitojun 		/* Garbage Collection(RFC 2461 5.3) */
4669631a17bSitojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
4679631a17bSitojun 			(void)nd6_free(rt, 1);
4689631a17bSitojun 			ln = NULL;
4699631a17bSitojun 		}
470be4e9e12Sitojun 		break;
471be4e9e12Sitojun 
472287546eaSitojun 	case ND6_LLINFO_DELAY:
473d374aaacSitojun 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
474d374aaacSitojun 			/* We need NUD */
475287546eaSitojun 			ln->ln_asked = 1;
476287546eaSitojun 			ln->ln_state = ND6_LLINFO_PROBE;
4779631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
478d374aaacSitojun 			nd6_ns_output(ifp, &dst->sin6_addr,
479d8a7e3a7Sitojun 			    &dst->sin6_addr, ln, 0);
480be4e9e12Sitojun 		} else {
481d374aaacSitojun 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
4829631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
483be4e9e12Sitojun 		}
484287546eaSitojun 		break;
485287546eaSitojun 	case ND6_LLINFO_PROBE:
486287546eaSitojun 		if (ln->ln_asked < nd6_umaxtries) {
487287546eaSitojun 			ln->ln_asked++;
4889631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
489287546eaSitojun 			nd6_ns_output(ifp, &dst->sin6_addr,
490287546eaSitojun 			    &dst->sin6_addr, ln, 0);
491d8a7e3a7Sitojun 		} else {
4929631a17bSitojun 			(void)nd6_free(rt, 0);
4939631a17bSitojun 			ln = NULL;
494d8a7e3a7Sitojun 		}
495287546eaSitojun 		break;
496287546eaSitojun 	}
4979631a17bSitojun 
4989631a17bSitojun 	splx(s);
499287546eaSitojun }
500287546eaSitojun 
5019631a17bSitojun /*
5029631a17bSitojun  * ND6 timer routine to expire default route list and prefix list
5039631a17bSitojun  */
5049631a17bSitojun void
505ee37ea65Smcbride nd6_timer(void *ignored_arg)
5069631a17bSitojun {
5079631a17bSitojun 	int s;
508568012e6Sbluhm 	struct nd_defrouter *dr, *ndr;
509119a433cSbluhm 	struct nd_prefix *pr, *npr;
5109631a17bSitojun 	struct in6_ifaddr *ia6, *nia6;
5119631a17bSitojun 
5129631a17bSitojun 	s = splsoftnet();
5139631a17bSitojun 	timeout_set(&nd6_timer_ch, nd6_timer, NULL);
51429e86e5eSblambert 	timeout_add_sec(&nd6_timer_ch, nd6_prune);
5159631a17bSitojun 
5160a2c5741Sitojun 	/* expire default router list */
517568012e6Sbluhm 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr)
518568012e6Sbluhm 		if (dr->expire && dr->expire < time_second)
519287546eaSitojun 			defrtrlist_del(dr);
520287546eaSitojun 
521287546eaSitojun 	/*
522d8a7e3a7Sitojun 	 * expire interface addresses.
523d8a7e3a7Sitojun 	 * in the past the loop was inside prefix expiry processing.
5248b542bbeSpascoe 	 * However, from a stricter spec-conformance standpoint, we should
525d8a7e3a7Sitojun 	 * rather separate address lifetimes and prefix lifetimes.
526d8a7e3a7Sitojun 	 */
527a0fa8079Smpi 	TAILQ_FOREACH_SAFE(ia6, &in6_ifaddr, ia_list, nia6) {
528d8a7e3a7Sitojun 		/* check address lifetime */
529d8a7e3a7Sitojun 		if (IFA6_IS_INVALID(ia6)) {
530d8a7e3a7Sitojun 			in6_purgeaddr(&ia6->ia_ifa);
5310a8b9475Smarkus 		} else if (IFA6_IS_DEPRECATED(ia6)) {
532d8a7e3a7Sitojun 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
533d8a7e3a7Sitojun 		} else {
534d8a7e3a7Sitojun 			/*
535d8a7e3a7Sitojun 			 * A new RA might have made a deprecated address
536d8a7e3a7Sitojun 			 * preferred.
537d8a7e3a7Sitojun 			 */
538d8a7e3a7Sitojun 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
539d8a7e3a7Sitojun 		}
540d8a7e3a7Sitojun 	}
541d8a7e3a7Sitojun 
542d8a7e3a7Sitojun 	/* expire prefix list */
543119a433cSbluhm 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
544d8a7e3a7Sitojun 		/*
545287546eaSitojun 		 * check prefix lifetime.
546287546eaSitojun 		 * since pltime is just for autoconf, pltime processing for
547287546eaSitojun 		 * prefix is not necessary.
548287546eaSitojun 		 */
549d8a7e3a7Sitojun 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
5503212dc31Stholo 		    time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
551287546eaSitojun 			/*
552287546eaSitojun 			 * address expiration and prefix expiration are
553d8a7e3a7Sitojun 			 * separate.  NEVER perform in6_purgeaddr here.
554287546eaSitojun 			 */
555287546eaSitojun 
556287546eaSitojun 			prelist_remove(pr);
557119a433cSbluhm 		}
558287546eaSitojun 	}
559287546eaSitojun 	splx(s);
560287546eaSitojun }
561287546eaSitojun 
56222770369Sitojun /*
56322770369Sitojun  * Nuke neighbor cache/prefix/default router management table, right before
56422770369Sitojun  * ifp goes away.
56522770369Sitojun  */
56622770369Sitojun void
567ee37ea65Smcbride nd6_purge(struct ifnet *ifp)
56822770369Sitojun {
56922770369Sitojun 	struct llinfo_nd6 *ln, *nln;
570d8a7e3a7Sitojun 	struct nd_defrouter *dr, *ndr;
57122770369Sitojun 	struct nd_prefix *pr, *npr;
57222770369Sitojun 
57322770369Sitojun 	/*
574d8a7e3a7Sitojun 	 * Nuke default router list entries toward ifp.
575d8a7e3a7Sitojun 	 * We defer removal of default router list entries that is installed
576d8a7e3a7Sitojun 	 * in the routing table, in order to keep additional side effects as
577d8a7e3a7Sitojun 	 * small as possible.
57822770369Sitojun 	 */
579568012e6Sbluhm 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
580d8a7e3a7Sitojun 		if (dr->installed)
581d8a7e3a7Sitojun 			continue;
582d8a7e3a7Sitojun 
58322770369Sitojun 		if (dr->ifp == ifp)
58422770369Sitojun 			defrtrlist_del(dr);
58522770369Sitojun 	}
586568012e6Sbluhm 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
587d8a7e3a7Sitojun 		if (!dr->installed)
588d8a7e3a7Sitojun 			continue;
589d8a7e3a7Sitojun 
59022770369Sitojun 		if (dr->ifp == ifp)
59122770369Sitojun 			defrtrlist_del(dr);
59222770369Sitojun 	}
59322770369Sitojun 
59422770369Sitojun 	/* Nuke prefix list entries toward ifp */
595119a433cSbluhm 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
59622770369Sitojun 		if (pr->ndpr_ifp == ifp) {
597d8a7e3a7Sitojun 			/*
59808abfc37Sbrad 			 * Because if_detach() does *not* release prefixes
59908abfc37Sbrad 			 * while purging addresses the reference count will
60008abfc37Sbrad 			 * still be above zero. We therefore reset it to
60108abfc37Sbrad 			 * make sure that the prefix really gets purged.
60208abfc37Sbrad 			 */
60308abfc37Sbrad 			pr->ndpr_refcnt = 0;
60408abfc37Sbrad 			/*
605d8a7e3a7Sitojun 			 * Previously, pr->ndpr_addr is removed as well,
606d8a7e3a7Sitojun 			 * but I strongly believe we don't have to do it.
607d8a7e3a7Sitojun 			 * nd6_purge() is only called from in6_ifdetach(),
608d8a7e3a7Sitojun 			 * which removes all the associated interface addresses
609d8a7e3a7Sitojun 			 * by itself.
610d8a7e3a7Sitojun 			 * (jinmei@kame.net 20010129)
611d8a7e3a7Sitojun 			 */
61222770369Sitojun 			prelist_remove(pr);
61322770369Sitojun 		}
61422770369Sitojun 	}
61522770369Sitojun 
616e6e438a4Sitojun 	if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */
61722770369Sitojun 		/* refresh default router list */
618f4f4d166Sitojun 		defrouter_select();
619e6e438a4Sitojun 	}
62022770369Sitojun 
62122770369Sitojun 	/*
62222770369Sitojun 	 * Nuke neighbor cache entries for the ifp.
62322770369Sitojun 	 * Note that rt->rt_ifp may not be the same as ifp,
62422770369Sitojun 	 * due to KAME goto ours hack.  See RTM_RESOLVE case in
62522770369Sitojun 	 * nd6_rtrequest(), and ip6_input().
62622770369Sitojun 	 */
62722770369Sitojun 	ln = llinfo_nd6.ln_next;
62822770369Sitojun 	while (ln && ln != &llinfo_nd6) {
62922770369Sitojun 		struct rtentry *rt;
63022770369Sitojun 		struct sockaddr_dl *sdl;
63122770369Sitojun 
63222770369Sitojun 		nln = ln->ln_next;
63322770369Sitojun 		rt = ln->ln_rt;
63422770369Sitojun 		if (rt && rt->rt_gateway &&
63522770369Sitojun 		    rt->rt_gateway->sa_family == AF_LINK) {
63622770369Sitojun 			sdl = (struct sockaddr_dl *)rt->rt_gateway;
63722770369Sitojun 			if (sdl->sdl_index == ifp->if_index)
638d8a7e3a7Sitojun 				nln = nd6_free(rt, 0);
63922770369Sitojun 		}
64022770369Sitojun 		ln = nln;
64122770369Sitojun 	}
64222770369Sitojun }
64322770369Sitojun 
644287546eaSitojun struct rtentry *
645ee37ea65Smcbride nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp)
646287546eaSitojun {
647287546eaSitojun 	struct rtentry *rt;
648287546eaSitojun 	struct sockaddr_in6 sin6;
649287546eaSitojun 
650287546eaSitojun 	bzero(&sin6, sizeof(sin6));
651287546eaSitojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
652287546eaSitojun 	sin6.sin6_family = AF_INET6;
653287546eaSitojun 	sin6.sin6_addr = *addr6;
654d8a7e3a7Sitojun 
655*c3c56496Sbluhm 	rt = rtalloc1(sin6tosa(&sin6), create, ifp->if_rdomain);
656287546eaSitojun 	if (rt && (rt->rt_flags & RTF_LLINFO) == 0) {
657287546eaSitojun 		/*
658287546eaSitojun 		 * This is the case for the default route.
659287546eaSitojun 		 * If we want to create a neighbor cache for the address, we
660287546eaSitojun 		 * should free the route for the destination and allocate an
661287546eaSitojun 		 * interface route.
662287546eaSitojun 		 */
663287546eaSitojun 		if (create) {
664287546eaSitojun 			RTFREE(rt);
665287546eaSitojun 			rt = 0;
666287546eaSitojun 		}
667287546eaSitojun 	}
668287546eaSitojun 	if (!rt) {
669287546eaSitojun 		if (create && ifp) {
670cb24f5e5Sclaudio 			struct rt_addrinfo info;
671d374aaacSitojun 			int e;
672d374aaacSitojun 
673287546eaSitojun 			/*
674287546eaSitojun 			 * If no route is available and create is set,
675287546eaSitojun 			 * we allocate a host route for the destination
676287546eaSitojun 			 * and treat it like an interface route.
677287546eaSitojun 			 * This hack is necessary for a neighbor which can't
678287546eaSitojun 			 * be covered by our own prefix.
679287546eaSitojun 			 */
680287546eaSitojun 			struct ifaddr *ifa =
681*c3c56496Sbluhm 			    ifaof_ifpforaddr(sin6tosa(&sin6), ifp);
682287546eaSitojun 			if (ifa == NULL)
683287546eaSitojun 				return (NULL);
684287546eaSitojun 
685287546eaSitojun 			/*
686287546eaSitojun 			 * Create a new route.  RTF_LLINFO is necessary
687287546eaSitojun 			 * to create a Neighbor Cache entry for the
688287546eaSitojun 			 * destination in nd6_rtrequest which will be
689cb24f5e5Sclaudio 			 * called in rtrequest1 via ifa->ifa_rtrequest.
690287546eaSitojun 			 */
691cb24f5e5Sclaudio 			bzero(&info, sizeof(info));
692cb24f5e5Sclaudio 			info.rti_flags = (ifa->ifa_flags | RTF_HOST |
693cb24f5e5Sclaudio 			    RTF_LLINFO) & ~RTF_CLONING;
694*c3c56496Sbluhm 			info.rti_info[RTAX_DST] = sin6tosa(&sin6);
695cb24f5e5Sclaudio 			info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr;
696*c3c56496Sbluhm 			info.rti_info[RTAX_NETMASK] = sin6tosa(&all1_sa);
697cb24f5e5Sclaudio 			if ((e = rtrequest1(RTM_ADD, &info, RTP_CONNECTED,
698ba79ddd5Ssperreault 			    &rt, ifp->if_rdomain)) != 0) {
699d8a7e3a7Sitojun #if 0
700287546eaSitojun 				log(LOG_ERR,
701287546eaSitojun 				    "nd6_lookup: failed to add route for a "
702d374aaacSitojun 				    "neighbor(%s), errno=%d\n",
703d374aaacSitojun 				    ip6_sprintf(addr6), e);
704d8a7e3a7Sitojun #endif
705d8a7e3a7Sitojun 				return (NULL);
706d8a7e3a7Sitojun 			}
707287546eaSitojun 			if (rt == NULL)
708287546eaSitojun 				return (NULL);
709287546eaSitojun 			if (rt->rt_llinfo) {
710287546eaSitojun 				struct llinfo_nd6 *ln =
711287546eaSitojun 				    (struct llinfo_nd6 *)rt->rt_llinfo;
712287546eaSitojun 				ln->ln_state = ND6_LLINFO_NOSTATE;
713287546eaSitojun 			}
714f6e55599Sitojun 		} else
715287546eaSitojun 			return (NULL);
716287546eaSitojun 	}
717287546eaSitojun 	rt->rt_refcnt--;
718287546eaSitojun 	/*
719287546eaSitojun 	 * Validation for the entry.
720d8a7e3a7Sitojun 	 * Note that the check for rt_llinfo is necessary because a cloned
721d8a7e3a7Sitojun 	 * route from a parent route that has the L flag (e.g. the default
722d8a7e3a7Sitojun 	 * route to a p2p interface) may have the flag, too, while the
723d8a7e3a7Sitojun 	 * destination is not actually a neighbor.
724287546eaSitojun 	 * XXX: we can't use rt->rt_ifp to check for the interface, since
725287546eaSitojun 	 *      it might be the loopback interface if the entry is for our
726287546eaSitojun 	 *      own address on a non-loopback interface. Instead, we should
727d8a7e3a7Sitojun 	 *      use rt->rt_ifa->ifa_ifp, which would specify the REAL
728d8a7e3a7Sitojun 	 *	interface.
729287546eaSitojun 	 */
730287546eaSitojun 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
731d8a7e3a7Sitojun 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
732287546eaSitojun 	    (ifp && rt->rt_ifa->ifa_ifp != ifp)) {
733287546eaSitojun 		if (create) {
734da592434Sitojun 			nd6log((LOG_DEBUG,
735d8a7e3a7Sitojun 			    "nd6_lookup: failed to lookup %s (if = %s)\n",
736da592434Sitojun 			    ip6_sprintf(addr6),
737da592434Sitojun 			    ifp ? ifp->if_xname : "unspec"));
738287546eaSitojun 		}
739d8a7e3a7Sitojun 		return (NULL);
740287546eaSitojun 	}
741287546eaSitojun 	return (rt);
742287546eaSitojun }
743287546eaSitojun 
744287546eaSitojun /*
745287546eaSitojun  * Detect if a given IPv6 address identifies a neighbor on a given link.
746287546eaSitojun  * XXX: should take care of the destination of a p2p link?
747287546eaSitojun  */
748287546eaSitojun int
749ee37ea65Smcbride nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
750287546eaSitojun {
751d8a7e3a7Sitojun 	struct nd_prefix *pr;
752d8a7e3a7Sitojun 	struct rtentry *rt;
753287546eaSitojun 
754cfb6b8dfSitojun 	/*
755cfb6b8dfSitojun 	 * A link-local address is always a neighbor.
756cfb6b8dfSitojun 	 * XXX: we should use the sin6_scope_id field rather than the embedded
757cfb6b8dfSitojun 	 * interface index.
758d8a7e3a7Sitojun 	 * XXX: a link does not necessarily specify a single interface.
759cfb6b8dfSitojun 	 */
760cfb6b8dfSitojun 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
761cfb6b8dfSitojun 	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
762287546eaSitojun 		return (1);
763287546eaSitojun 
764287546eaSitojun 	/*
765d8a7e3a7Sitojun 	 * If the address matches one of our on-link prefixes, it should be a
766d8a7e3a7Sitojun 	 * neighbor.
767287546eaSitojun 	 */
768545205eaSpyr 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
769d8a7e3a7Sitojun 		if (pr->ndpr_ifp != ifp)
770d8a7e3a7Sitojun 			continue;
771287546eaSitojun 
772d8a7e3a7Sitojun 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
773d8a7e3a7Sitojun 			continue;
774d8a7e3a7Sitojun 
775d8a7e3a7Sitojun 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
776d8a7e3a7Sitojun 		    &addr->sin6_addr, &pr->ndpr_mask))
777d8a7e3a7Sitojun 			return (1);
778287546eaSitojun 	}
779d8a7e3a7Sitojun 
780d8a7e3a7Sitojun 	/*
781287546eaSitojun 	 * Even if the address matches none of our addresses, it might be
782287546eaSitojun 	 * in the neighbor cache.
783287546eaSitojun 	 */
784d8a7e3a7Sitojun 	if ((rt = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL)
785287546eaSitojun 		return (1);
786287546eaSitojun 
787287546eaSitojun 	return (0);
788287546eaSitojun }
789287546eaSitojun 
790287546eaSitojun /*
791287546eaSitojun  * Free an nd6 llinfo entry.
792d8a7e3a7Sitojun  * Since the function would cause significant changes in the kernel, DO NOT
793d8a7e3a7Sitojun  * make it global, unless you have a strong reason for the change, and are sure
794d8a7e3a7Sitojun  * that the change is safe.
795287546eaSitojun  */
796a0aa363cSjsing struct llinfo_nd6 *
797ee37ea65Smcbride nd6_free(struct rtentry *rt, int gc)
798287546eaSitojun {
799cb24f5e5Sclaudio 	struct rt_addrinfo info;
80029760ae1Sitojun 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
801*c3c56496Sbluhm 	struct in6_addr in6 = satosin6(rt_key(rt))->sin6_addr;
802287546eaSitojun 	struct nd_defrouter *dr;
803287546eaSitojun 
804287546eaSitojun 	/*
805d8a7e3a7Sitojun 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
806d8a7e3a7Sitojun 	 * even though it is not harmful, it was not really necessary.
807287546eaSitojun 	 */
808f4f4d166Sitojun 
809492c93a5Sitojun 	if (!ip6_forwarding) {
810f4f4d166Sitojun 		int s;
8116e92dee6Sitojun 		s = splsoftnet();
812*c3c56496Sbluhm 		dr = defrouter_lookup(&satosin6(rt_key(rt))->sin6_addr,
813f4f4d166Sitojun 		    rt->rt_ifp);
814d8a7e3a7Sitojun 
815d8a7e3a7Sitojun 		if (dr != NULL && dr->expire &&
816d8a7e3a7Sitojun 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
817d8a7e3a7Sitojun 			/*
818d8a7e3a7Sitojun 			 * If the reason for the deletion is just garbage
819d8a7e3a7Sitojun 			 * collection, and the neighbor is an active default
820d8a7e3a7Sitojun 			 * router, do not delete it.  Instead, reset the GC
821d8a7e3a7Sitojun 			 * timer using the router's lifetime.
822d8a7e3a7Sitojun 			 * Simply deleting the entry would affect default
823d8a7e3a7Sitojun 			 * router selection, which is not necessarily a good
824d8a7e3a7Sitojun 			 * thing, especially when we're using router preference
825d8a7e3a7Sitojun 			 * values.
826d8a7e3a7Sitojun 			 * XXX: the check for ln_state would be redundant,
827d8a7e3a7Sitojun 			 *      but we intentionally keep it just in case.
828d8a7e3a7Sitojun 			 */
8293212dc31Stholo 			if (dr->expire > time_second * hz) {
8309631a17bSitojun 				nd6_llinfo_settimer(ln,
8313212dc31Stholo 				    dr->expire - time_second * hz);
8329631a17bSitojun 			} else
8339631a17bSitojun 				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
834d8a7e3a7Sitojun 			splx(s);
835d8a7e3a7Sitojun 			return (ln->ln_next);
836d8a7e3a7Sitojun 		}
837d8a7e3a7Sitojun 
838f4f4d166Sitojun 		if (ln->ln_router || dr) {
839f4f4d166Sitojun 			/*
840f4f4d166Sitojun 			 * rt6_flush must be called whether or not the neighbor
841f4f4d166Sitojun 			 * is in the Default Router List.
842f4f4d166Sitojun 			 * See a corresponding comment in nd6_na_input().
843f4f4d166Sitojun 			 */
844f4f4d166Sitojun 			rt6_flush(&in6, rt->rt_ifp);
845f4f4d166Sitojun 		}
846f4f4d166Sitojun 
847f4f4d166Sitojun 		if (dr) {
848f4f4d166Sitojun 			/*
8498b542bbeSpascoe 			 * Unreachability of a router might affect the default
850f4f4d166Sitojun 			 * router selection and on-link detection of advertised
851f4f4d166Sitojun 			 * prefixes.
852f4f4d166Sitojun 			 */
853f4f4d166Sitojun 
854f4f4d166Sitojun 			/*
855f4f4d166Sitojun 			 * Temporarily fake the state to choose a new default
856f4f4d166Sitojun 			 * router and to perform on-link determination of
85734deef1eSitojun 			 * prefixes correctly.
858f4f4d166Sitojun 			 * Below the state will be set correctly,
859f4f4d166Sitojun 			 * or the entry itself will be deleted.
860f4f4d166Sitojun 			 */
861f4f4d166Sitojun 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
862f4f4d166Sitojun 
863f4f4d166Sitojun 			/*
864d8a7e3a7Sitojun 			 * Since defrouter_select() does not affect the
865d8a7e3a7Sitojun 			 * on-link determination and MIP6 needs the check
866d8a7e3a7Sitojun 			 * before the default router selection, we perform
867d8a7e3a7Sitojun 			 * the check now.
868f4f4d166Sitojun 			 */
869f4f4d166Sitojun 			pfxlist_onlink_check();
870d8a7e3a7Sitojun 
871d8a7e3a7Sitojun 			/*
872d8a7e3a7Sitojun 			 * refresh default router list
873d8a7e3a7Sitojun 			 */
874d8a7e3a7Sitojun 			defrouter_select();
875287546eaSitojun 		}
876287546eaSitojun 		splx(s);
877287546eaSitojun 	}
878287546eaSitojun 
87929760ae1Sitojun 	/*
88029760ae1Sitojun 	 * Before deleting the entry, remember the next entry as the
88129760ae1Sitojun 	 * return value.  We need this because pfxlist_onlink_check() above
88229760ae1Sitojun 	 * might have freed other entries (particularly the old next entry) as
88329760ae1Sitojun 	 * a side effect (XXX).
88429760ae1Sitojun 	 */
88529760ae1Sitojun 	next = ln->ln_next;
88629760ae1Sitojun 
88729760ae1Sitojun 	/*
88829760ae1Sitojun 	 * Detach the route from the routing tree and the list of neighbor
88929760ae1Sitojun 	 * caches, and disable the route entry not to be used in already
89029760ae1Sitojun 	 * cached routes.
89129760ae1Sitojun 	 */
892cb24f5e5Sclaudio 	bzero(&info, sizeof(info));
893cb24f5e5Sclaudio 	info.rti_info[RTAX_DST] = rt_key(rt);
894cb24f5e5Sclaudio 	info.rti_info[RTAX_NETMASK] = rt_mask(rt);
895ba79ddd5Ssperreault 	rtrequest1(RTM_DELETE, &info, rt->rt_priority, NULL,
896ba79ddd5Ssperreault 	    rt->rt_ifp->if_rdomain);
89729760ae1Sitojun 
8980a2c5741Sitojun 	return (next);
899287546eaSitojun }
900287546eaSitojun 
901287546eaSitojun /*
902287546eaSitojun  * Upper-layer reachability hint for Neighbor Unreachability Detection.
903287546eaSitojun  *
9048b542bbeSpascoe  * XXX cost-effective methods?
905287546eaSitojun  */
906287546eaSitojun void
907ee37ea65Smcbride nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force)
908287546eaSitojun {
909287546eaSitojun 	struct llinfo_nd6 *ln;
910287546eaSitojun 
911287546eaSitojun 	/*
912287546eaSitojun 	 * If the caller specified "rt", use that.  Otherwise, resolve the
913287546eaSitojun 	 * routing table by supplied "dst6".
914287546eaSitojun 	 */
915287546eaSitojun 	if (!rt) {
916287546eaSitojun 		if (!dst6)
917287546eaSitojun 			return;
918287546eaSitojun 		if (!(rt = nd6_lookup(dst6, 0, NULL)))
919287546eaSitojun 			return;
920287546eaSitojun 	}
921287546eaSitojun 
922f6e55599Sitojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
923f6e55599Sitojun 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
924f6e55599Sitojun 	    !rt->rt_llinfo || !rt->rt_gateway ||
925f6e55599Sitojun 	    rt->rt_gateway->sa_family != AF_LINK) {
926287546eaSitojun 		/* This is not a host route. */
927287546eaSitojun 		return;
928287546eaSitojun 	}
929287546eaSitojun 
930287546eaSitojun 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
931804d8827Sitojun 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
932287546eaSitojun 		return;
933287546eaSitojun 
934f6e55599Sitojun 	/*
935f6e55599Sitojun 	 * if we get upper-layer reachability confirmation many times,
936f6e55599Sitojun 	 * it is possible we have false information.
937f6e55599Sitojun 	 */
938f6e55599Sitojun 	if (!force) {
939f6e55599Sitojun 		ln->ln_byhint++;
940f6e55599Sitojun 		if (ln->ln_byhint > nd6_maxnudhint)
941f6e55599Sitojun 			return;
942f6e55599Sitojun 	}
943f6e55599Sitojun 
944287546eaSitojun 	ln->ln_state = ND6_LLINFO_REACHABLE;
9459631a17bSitojun 	if (!ND6_LLINFO_PERMANENT(ln)) {
9469631a17bSitojun 		nd6_llinfo_settimer(ln,
9479631a17bSitojun 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
9489631a17bSitojun 	}
949287546eaSitojun }
950287546eaSitojun 
951ee37ea65Smcbride /*
952ee37ea65Smcbride  * info - XXX: unused
953ee37ea65Smcbride  */
954ee37ea65Smcbride 
955287546eaSitojun void
956ee37ea65Smcbride nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info)
957287546eaSitojun {
958287546eaSitojun 	struct sockaddr *gate = rt->rt_gateway;
959287546eaSitojun 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
960287546eaSitojun 	static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK};
961287546eaSitojun 	struct ifnet *ifp = rt->rt_ifp;
962287546eaSitojun 	struct ifaddr *ifa;
96370fc31beSrainer 	struct nd_defrouter *dr;
96470fc31beSrainer 
96570fc31beSrainer 	if (req == RTM_DELETE && (rt->rt_flags & RTF_GATEWAY) &&
96670fc31beSrainer 	    (IN6_ARE_ADDR_EQUAL(&(satosin6(rt_key(rt)))->sin6_addr,
96770fc31beSrainer 	    &in6addr_any) && rt_mask(rt) && (rt_mask(rt)->sa_len == 0 ||
96870fc31beSrainer 	    IN6_ARE_ADDR_EQUAL(&(satosin6(rt_mask(rt)))->sin6_addr,
96970fc31beSrainer 	    &in6addr_any)))) {
970*c3c56496Sbluhm 		dr = defrouter_lookup(&satosin6(gate)->sin6_addr, ifp);
97170fc31beSrainer 		if (dr)
97270fc31beSrainer 			dr->installed = 0;
97370fc31beSrainer 	}
974287546eaSitojun 
975d8a7e3a7Sitojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
976287546eaSitojun 		return;
977287546eaSitojun 
978d8a7e3a7Sitojun 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
979d8a7e3a7Sitojun 		/*
980d8a7e3a7Sitojun 		 * This is probably an interface direct route for a link
981d8a7e3a7Sitojun 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
982d8a7e3a7Sitojun 		 * We do not need special treatment below for such a route.
983d8a7e3a7Sitojun 		 * Moreover, the RTF_LLINFO flag which would be set below
984d8a7e3a7Sitojun 		 * would annoy the ndp(8) command.
985d8a7e3a7Sitojun 		 */
986d8a7e3a7Sitojun 		return;
987d8a7e3a7Sitojun 	}
988d8a7e3a7Sitojun 
989af1344beSbluhm 	if (req == RTM_RESOLVE && nd6_need_cache(ifp) == 0) {
990d8a7e3a7Sitojun 		/*
991af1344beSbluhm 		 * For routing daemons like ospf6d we allow neighbor discovery
992af1344beSbluhm 		 * based on the cloning route only.  This allows us to sent
993af1344beSbluhm 		 * packets directly into a network without having an address
994af1344beSbluhm 		 * with matching prefix on the interface.  If the cloning
995af1344beSbluhm 		 * route is used for an stf interface, we would mistakenly
996af1344beSbluhm 		 * make a neighbor cache for the host route, and would see
997af1344beSbluhm 		 * strange neighbor solicitation for the corresponding
998af1344beSbluhm 		 * destination.  In order to avoid confusion, we check if the
999af1344beSbluhm 		 * interface is suitable for neighbor discovery, and stop the
1000d8a7e3a7Sitojun 		 * process if not.  Additionally, we remove the LLINFO flag
1001d8a7e3a7Sitojun 		 * so that ndp(8) will not try to get the neighbor information
1002d8a7e3a7Sitojun 		 * of the destination.
1003d8a7e3a7Sitojun 		 */
1004d8a7e3a7Sitojun 		rt->rt_flags &= ~RTF_LLINFO;
1005d8a7e3a7Sitojun 		return;
1006d8a7e3a7Sitojun 	}
1007d8a7e3a7Sitojun 
1008287546eaSitojun 	switch (req) {
1009287546eaSitojun 	case RTM_ADD:
1010287546eaSitojun 		/*
1011287546eaSitojun 		 * There is no backward compatibility :)
1012287546eaSitojun 		 *
1013287546eaSitojun 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
1014287546eaSitojun 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
1015287546eaSitojun 		 *	   rt->rt_flags |= RTF_CLONING;
1016287546eaSitojun 		 */
101748ebf8e1Sitojun 		if ((rt->rt_flags & RTF_CLONING) ||
101848ebf8e1Sitojun 		    ((rt->rt_flags & RTF_LLINFO) && !ln)) {
1019287546eaSitojun 			/*
102048ebf8e1Sitojun 			 * Case 1: This route should come from a route to
102148ebf8e1Sitojun 			 * interface (RTF_CLONING case) or the route should be
102248ebf8e1Sitojun 			 * treated as on-link but is currently not
102348ebf8e1Sitojun 			 * (RTF_LLINFO && !ln case).
1024287546eaSitojun 			 */
1025287546eaSitojun 			rt_setgate(rt, rt_key(rt),
1026c241aca8Shenning 				   (struct sockaddr *)&null_sdl, 0);
1027287546eaSitojun 			gate = rt->rt_gateway;
1028287546eaSitojun 			SDL(gate)->sdl_type = ifp->if_type;
1029287546eaSitojun 			SDL(gate)->sdl_index = ifp->if_index;
1030287546eaSitojun 			if (ln)
10319631a17bSitojun 				nd6_llinfo_settimer(ln, 0);
1032d8a7e3a7Sitojun 			if ((rt->rt_flags & RTF_CLONING) != 0)
1033287546eaSitojun 				break;
1034287546eaSitojun 		}
1035f4f4d166Sitojun 		/*
10368b542bbeSpascoe 		 * In IPv4 code, we try to announce new RTF_ANNOUNCE entry here.
1037f4f4d166Sitojun 		 * We don't do that here since llinfo is not ready yet.
1038f4f4d166Sitojun 		 *
1039f4f4d166Sitojun 		 * There are also couple of other things to be discussed:
1040f4f4d166Sitojun 		 * - unsolicited NA code needs improvement beforehand
1041f4f4d166Sitojun 		 * - RFC2461 says we MAY send multicast unsolicited NA
1042f4f4d166Sitojun 		 *   (7.2.6 paragraph 4), however, it also says that we
1043f4f4d166Sitojun 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
1044f4f4d166Sitojun 		 *   we don't have anything like it right now.
1045841d7adbSitojun 		 *   note that the mechanism needs a mutual agreement
1046f4f4d166Sitojun 		 *   between proxies, which means that we need to implement
1047841d7adbSitojun 		 *   a new protocol, or a new kludge.
1048841d7adbSitojun 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
1049f4f4d166Sitojun 		 *   we need to check ip6forwarding before sending it.
1050f4f4d166Sitojun 		 *   (or should we allow proxy ND configuration only for
1051f4f4d166Sitojun 		 *   routers?  there's no mention about proxy ND from hosts)
1052f4f4d166Sitojun 		 */
1053f4f4d166Sitojun #if 0
1054f4f4d166Sitojun 		/* XXX it does not work */
1055287546eaSitojun 		if (rt->rt_flags & RTF_ANNOUNCE)
1056287546eaSitojun 			nd6_na_output(ifp,
1057*c3c56496Sbluhm 			      &satosin6(rt_key(rt))->sin6_addr,
1058*c3c56496Sbluhm 			      &satosin6(rt_key(rt))->sin6_addr,
1059287546eaSitojun 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
1060f4f4d166Sitojun 			      1, NULL);
1061f4f4d166Sitojun #endif
1062287546eaSitojun 		/* FALLTHROUGH */
1063287546eaSitojun 	case RTM_RESOLVE:
1064d8a7e3a7Sitojun 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
1065d374aaacSitojun 			/*
1066d374aaacSitojun 			 * Address resolution isn't necessary for a point to
1067d374aaacSitojun 			 * point link, so we can skip this test for a p2p link.
1068d374aaacSitojun 			 */
1069287546eaSitojun 			if (gate->sa_family != AF_LINK ||
1070287546eaSitojun 			    gate->sa_len < sizeof(null_sdl)) {
1071d374aaacSitojun 				log(LOG_DEBUG,
10720a2c5741Sitojun 				    "nd6_rtrequest: bad gateway value: %s\n",
107393036e4eSangelos 				    ifp->if_xname);
1074287546eaSitojun 				break;
1075287546eaSitojun 			}
1076287546eaSitojun 			SDL(gate)->sdl_type = ifp->if_type;
1077287546eaSitojun 			SDL(gate)->sdl_index = ifp->if_index;
1078d374aaacSitojun 		}
1079d374aaacSitojun 		if (ln != NULL)
1080287546eaSitojun 			break;	/* This happens on a route change */
1081287546eaSitojun 		/*
1082287546eaSitojun 		 * Case 2: This route may come from cloning, or a manual route
1083287546eaSitojun 		 * add with a LL address.
1084287546eaSitojun 		 */
1085287546eaSitojun 		R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln));
1086287546eaSitojun 		rt->rt_llinfo = (caddr_t)ln;
1087287546eaSitojun 		if (!ln) {
1088287546eaSitojun 			log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n");
1089287546eaSitojun 			break;
1090287546eaSitojun 		}
1091287546eaSitojun 		nd6_inuse++;
1092287546eaSitojun 		nd6_allocated++;
1093287546eaSitojun 		Bzero(ln, sizeof(*ln));
1094287546eaSitojun 		ln->ln_rt = rt;
10959631a17bSitojun 		timeout_set(&ln->ln_timer_ch, nd6_llinfo_timer, ln);
1096287546eaSitojun 		/* this is required for "ndp" command. - shin */
1097287546eaSitojun 		if (req == RTM_ADD) {
1098287546eaSitojun 		        /*
1099287546eaSitojun 			 * gate should have some valid AF_LINK entry,
1100287546eaSitojun 			 * and ln->ln_expire should have some lifetime
1101287546eaSitojun 			 * which is specified by ndp command.
1102287546eaSitojun 			 */
1103287546eaSitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1104f6e55599Sitojun 			ln->ln_byhint = 0;
1105287546eaSitojun 		} else {
1106287546eaSitojun 		        /*
1107287546eaSitojun 			 * When req == RTM_RESOLVE, rt is created and
1108287546eaSitojun 			 * initialized in rtrequest(), so rt_expire is 0.
1109287546eaSitojun 			 */
1110287546eaSitojun 			ln->ln_state = ND6_LLINFO_NOSTATE;
11119631a17bSitojun 			nd6_llinfo_settimer(ln, 0);
1112287546eaSitojun 		}
1113287546eaSitojun 		rt->rt_flags |= RTF_LLINFO;
1114287546eaSitojun 		ln->ln_next = llinfo_nd6.ln_next;
1115287546eaSitojun 		llinfo_nd6.ln_next = ln;
1116287546eaSitojun 		ln->ln_prev = &llinfo_nd6;
1117287546eaSitojun 		ln->ln_next->ln_prev = ln;
1118287546eaSitojun 
1119287546eaSitojun 		/*
1120f3fcf2f3Smcbride 		 * If we have too many cache entries, initiate immediate
1121f3fcf2f3Smcbride 		 * purging for some "less recently used" entries.  Note that
1122f3fcf2f3Smcbride 		 * we cannot directly call nd6_free() here because it would
1123f3fcf2f3Smcbride 		 * cause re-entering rtable related routines triggering an LOR
1124f3fcf2f3Smcbride 		 * problem for FreeBSD.
1125f3fcf2f3Smcbride 		 */
1126f3fcf2f3Smcbride 		if (ip6_neighborgcthresh >= 0 &&
1127f3fcf2f3Smcbride 		    nd6_inuse >= ip6_neighborgcthresh) {
1128f3fcf2f3Smcbride 			int i;
1129f3fcf2f3Smcbride 
1130f3fcf2f3Smcbride 			for (i = 0; i < 10 && llinfo_nd6.ln_prev != ln; i++) {
1131f3fcf2f3Smcbride 				struct llinfo_nd6 *ln_end = llinfo_nd6.ln_prev;
1132f3fcf2f3Smcbride 
1133f3fcf2f3Smcbride 				/* Move this entry to the head */
1134f3fcf2f3Smcbride 				LN_DEQUEUE(ln_end);
1135f3fcf2f3Smcbride 				LN_INSERTHEAD(ln_end);
1136f3fcf2f3Smcbride 
1137f3fcf2f3Smcbride 				if (ND6_LLINFO_PERMANENT(ln_end))
1138f3fcf2f3Smcbride 					continue;
1139f3fcf2f3Smcbride 
1140f3fcf2f3Smcbride 				if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
1141f3fcf2f3Smcbride 					ln_end->ln_state = ND6_LLINFO_STALE;
1142f3fcf2f3Smcbride 				else
1143f3fcf2f3Smcbride 					ln_end->ln_state = ND6_LLINFO_PURGE;
1144f3fcf2f3Smcbride 				nd6_llinfo_settimer(ln_end, 0);
1145f3fcf2f3Smcbride 			}
1146f3fcf2f3Smcbride 		}
1147f3fcf2f3Smcbride 
1148f3fcf2f3Smcbride 		/*
1149287546eaSitojun 		 * check if rt_key(rt) is one of my address assigned
1150287546eaSitojun 		 * to the interface.
1151287546eaSitojun 		 */
1152fe19dd8dSbluhm 		ifa = &in6ifa_ifpwithaddr(rt->rt_ifp,
1153*c3c56496Sbluhm 		    &satosin6(rt_key(rt))->sin6_addr)->ia_ifa;
1154287546eaSitojun 		if (ifa) {
1155287546eaSitojun 			caddr_t macp = nd6_ifptomac(ifp);
11569631a17bSitojun 			nd6_llinfo_settimer(ln, -1);
1157287546eaSitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1158f6e55599Sitojun 			ln->ln_byhint = 0;
1159287546eaSitojun 			if (macp) {
1160287546eaSitojun 				Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen);
1161287546eaSitojun 				SDL(gate)->sdl_alen = ifp->if_addrlen;
1162287546eaSitojun 			}
1163287546eaSitojun 			if (nd6_useloopback) {
1164fcc641efSmickey 				rt->rt_ifp = lo0ifp;	/*XXX*/
1165287546eaSitojun 				/*
1166287546eaSitojun 				 * Make sure rt_ifa be equal to the ifaddr
1167287546eaSitojun 				 * corresponding to the address.
1168287546eaSitojun 				 * We need this because when we refer
1169287546eaSitojun 				 * rt_ifa->ia6_flags in ip6_input, we assume
1170287546eaSitojun 				 * that the rt_ifa points to the address instead
1171287546eaSitojun 				 * of the loopback address.
1172287546eaSitojun 				 */
1173287546eaSitojun 				if (ifa != rt->rt_ifa) {
1174c405c1b9Smpi 					ifafree(rt->rt_ifa);
1175287546eaSitojun 					ifa->ifa_refcnt++;
1176287546eaSitojun 					rt->rt_ifa = ifa;
1177287546eaSitojun 				}
1178287546eaSitojun 			}
1179f4f4d166Sitojun 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
11809631a17bSitojun 			nd6_llinfo_settimer(ln, -1);
1181f4f4d166Sitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1182f6e55599Sitojun 			ln->ln_byhint = 0;
1183f4f4d166Sitojun 
1184f4f4d166Sitojun 			/* join solicited node multicast for proxy ND */
1185f4f4d166Sitojun 			if (ifp->if_flags & IFF_MULTICAST) {
1186f4f4d166Sitojun 				struct in6_addr llsol;
1187f4f4d166Sitojun 				int error;
1188f4f4d166Sitojun 
1189*c3c56496Sbluhm 				llsol = satosin6(rt_key(rt))->sin6_addr;
1190f4f4d166Sitojun 				llsol.s6_addr16[0] = htons(0xff02);
1191f4f4d166Sitojun 				llsol.s6_addr16[1] = htons(ifp->if_index);
1192f4f4d166Sitojun 				llsol.s6_addr32[1] = 0;
1193f4f4d166Sitojun 				llsol.s6_addr32[2] = htonl(1);
1194f4f4d166Sitojun 				llsol.s6_addr8[12] = 0xff;
1195f4f4d166Sitojun 
1196d8a7e3a7Sitojun 				if (in6_addmulti(&llsol, ifp, &error)) {
1197d8a7e3a7Sitojun 					nd6log((LOG_ERR, "%s: failed to join "
1198d8a7e3a7Sitojun 					    "%s (errno=%d)\n", ifp->if_xname,
1199d8a7e3a7Sitojun 					    ip6_sprintf(&llsol), error));
1200d8a7e3a7Sitojun 				}
1201f4f4d166Sitojun 			}
1202287546eaSitojun 		}
1203287546eaSitojun 		break;
1204287546eaSitojun 
1205287546eaSitojun 	case RTM_DELETE:
1206287546eaSitojun 		if (!ln)
1207287546eaSitojun 			break;
1208f4f4d166Sitojun 		/* leave from solicited node multicast for proxy ND */
1209f4f4d166Sitojun 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1210f4f4d166Sitojun 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1211f4f4d166Sitojun 			struct in6_addr llsol;
1212f4f4d166Sitojun 			struct in6_multi *in6m;
1213f4f4d166Sitojun 
1214*c3c56496Sbluhm 			llsol = satosin6(rt_key(rt))->sin6_addr;
1215f4f4d166Sitojun 			llsol.s6_addr16[0] = htons(0xff02);
1216f4f4d166Sitojun 			llsol.s6_addr16[1] = htons(ifp->if_index);
1217f4f4d166Sitojun 			llsol.s6_addr32[1] = 0;
1218f4f4d166Sitojun 			llsol.s6_addr32[2] = htonl(1);
1219f4f4d166Sitojun 			llsol.s6_addr8[12] = 0xff;
1220f4f4d166Sitojun 
1221f4f4d166Sitojun 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1222f4f4d166Sitojun 			if (in6m)
1223f4f4d166Sitojun 				in6_delmulti(in6m);
1224f4f4d166Sitojun 		}
1225287546eaSitojun 		nd6_inuse--;
1226287546eaSitojun 		ln->ln_next->ln_prev = ln->ln_prev;
1227287546eaSitojun 		ln->ln_prev->ln_next = ln->ln_next;
1228287546eaSitojun 		ln->ln_prev = NULL;
12299631a17bSitojun 		nd6_llinfo_settimer(ln, -1);
1230287546eaSitojun 		rt->rt_llinfo = 0;
1231287546eaSitojun 		rt->rt_flags &= ~RTF_LLINFO;
1232287546eaSitojun 		if (ln->ln_hold)
1233287546eaSitojun 			m_freem(ln->ln_hold);
1234287546eaSitojun 		Free((caddr_t)ln);
1235287546eaSitojun 	}
1236287546eaSitojun }
1237287546eaSitojun 
1238287546eaSitojun int
1239ee37ea65Smcbride nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1240287546eaSitojun {
1241287546eaSitojun 	struct in6_drlist *drl = (struct in6_drlist *)data;
1242d8a7e3a7Sitojun 	struct in6_oprlist *oprl = (struct in6_oprlist *)data;
1243287546eaSitojun 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1244287546eaSitojun 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1245d8a7e3a7Sitojun 	struct nd_defrouter *dr;
1246287546eaSitojun 	struct nd_prefix *pr;
1247287546eaSitojun 	struct rtentry *rt;
1248287546eaSitojun 	int i = 0, error = 0;
1249287546eaSitojun 	int s;
1250287546eaSitojun 
1251287546eaSitojun 	switch (cmd) {
1252287546eaSitojun 	case SIOCGDRLST_IN6:
1253d8a7e3a7Sitojun 		/*
1254d8a7e3a7Sitojun 		 * obsolete API, use sysctl under net.inet6.icmp6
1255d8a7e3a7Sitojun 		 */
1256287546eaSitojun 		bzero(drl, sizeof(*drl));
12576e92dee6Sitojun 		s = splsoftnet();
1258568012e6Sbluhm 		TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
1259568012e6Sbluhm 			if (i >= DRLSTSIZ)
1260568012e6Sbluhm 				break;
1261287546eaSitojun 			drl->defrouter[i].rtaddr = dr->rtaddr;
1262287546eaSitojun 			if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) {
1263287546eaSitojun 				/* XXX: need to this hack for KAME stack */
1264287546eaSitojun 				drl->defrouter[i].rtaddr.s6_addr16[1] = 0;
1265f6e55599Sitojun 			} else
1266287546eaSitojun 				log(LOG_ERR,
1267287546eaSitojun 				    "default router list contains a "
1268287546eaSitojun 				    "non-linklocal address(%s)\n",
1269287546eaSitojun 				    ip6_sprintf(&drl->defrouter[i].rtaddr));
1270287546eaSitojun 
1271287546eaSitojun 			drl->defrouter[i].flags = dr->flags;
1272287546eaSitojun 			drl->defrouter[i].rtlifetime = dr->rtlifetime;
1273287546eaSitojun 			drl->defrouter[i].expire = dr->expire;
1274287546eaSitojun 			drl->defrouter[i].if_index = dr->ifp->if_index;
1275287546eaSitojun 			i++;
1276287546eaSitojun 		}
1277287546eaSitojun 		splx(s);
1278287546eaSitojun 		break;
1279287546eaSitojun 	case SIOCGPRLST_IN6:
1280f4f4d166Sitojun 		/*
1281d8a7e3a7Sitojun 		 * obsolete API, use sysctl under net.inet6.icmp6
1282d8a7e3a7Sitojun 		 *
1283d8a7e3a7Sitojun 		 * XXX the structure in6_prlist was changed in backward-
1284d8a7e3a7Sitojun 		 * incompatible manner.  in6_oprlist is used for SIOCGPRLST_IN6,
1285d8a7e3a7Sitojun 		 * in6_prlist is used for nd6_sysctl() - fill_prlist().
1286d8a7e3a7Sitojun 		 */
1287d8a7e3a7Sitojun 		/*
12888b542bbeSpascoe 		 * XXX meaning of fields, especially "raflags", is very
12898b542bbeSpascoe 		 * different between RA prefix list and RR/static prefix list.
1290f4f4d166Sitojun 		 * how about separating ioctls into two?
1291f4f4d166Sitojun 		 */
1292d8a7e3a7Sitojun 		bzero(oprl, sizeof(*oprl));
12936e92dee6Sitojun 		s = splsoftnet();
1294119a433cSbluhm 		LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1295287546eaSitojun 			struct nd_pfxrouter *pfr;
1296287546eaSitojun 			int j;
1297287546eaSitojun 
1298119a433cSbluhm 			if (i >= PRLSTSIZ)
1299119a433cSbluhm 				break;
1300d8a7e3a7Sitojun 			oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr;
1301d8a7e3a7Sitojun 			oprl->prefix[i].raflags = pr->ndpr_raf;
1302d8a7e3a7Sitojun 			oprl->prefix[i].prefixlen = pr->ndpr_plen;
1303d8a7e3a7Sitojun 			oprl->prefix[i].vltime = pr->ndpr_vltime;
1304d8a7e3a7Sitojun 			oprl->prefix[i].pltime = pr->ndpr_pltime;
1305d8a7e3a7Sitojun 			oprl->prefix[i].if_index = pr->ndpr_ifp->if_index;
1306d8a7e3a7Sitojun 			oprl->prefix[i].expire = pr->ndpr_expire;
1307287546eaSitojun 
1308287546eaSitojun 			j = 0;
1309119a433cSbluhm 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1310287546eaSitojun 				if (j < DRLSTSIZ) {
1311d8a7e3a7Sitojun #define RTRADDR oprl->prefix[i].advrtr[j]
1312287546eaSitojun 					RTRADDR = pfr->router->rtaddr;
1313287546eaSitojun 					if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) {
1314287546eaSitojun 						/* XXX: hack for KAME */
1315287546eaSitojun 						RTRADDR.s6_addr16[1] = 0;
1316f6e55599Sitojun 					} else
1317287546eaSitojun 						log(LOG_ERR,
1318287546eaSitojun 						    "a router(%s) advertises "
1319287546eaSitojun 						    "a prefix with "
1320287546eaSitojun 						    "non-link local address\n",
1321287546eaSitojun 						    ip6_sprintf(&RTRADDR));
1322287546eaSitojun #undef RTRADDR
1323287546eaSitojun 				}
1324287546eaSitojun 				j++;
1325287546eaSitojun 			}
1326d8a7e3a7Sitojun 			oprl->prefix[i].advrtrs = j;
1327d8a7e3a7Sitojun 			oprl->prefix[i].origin = PR_ORIG_RA;
1328287546eaSitojun 
1329287546eaSitojun 			i++;
1330287546eaSitojun 		}
1331f4f4d166Sitojun 		splx(s);
1332287546eaSitojun 
1333287546eaSitojun 		break;
1334d6b9e9b9Sitojun 	case OSIOCGIFINFO_IN6:
1335d6b9e9b9Sitojun 		/* XXX: old ndp(8) assumes a positive value for linkmtu. */
1336191a775dSitojun 		bzero(&ndi->ndi, sizeof(ndi->ndi));
1337d6b9e9b9Sitojun 		ndi->ndi.linkmtu = IN6_LINKMTU(ifp);
1338d6b9e9b9Sitojun 		ndi->ndi.maxmtu = ND_IFINFO(ifp)->maxmtu;
1339d6b9e9b9Sitojun 		ndi->ndi.basereachable = ND_IFINFO(ifp)->basereachable;
1340d6b9e9b9Sitojun 		ndi->ndi.reachable = ND_IFINFO(ifp)->reachable;
1341d6b9e9b9Sitojun 		ndi->ndi.retrans = ND_IFINFO(ifp)->retrans;
1342d6b9e9b9Sitojun 		ndi->ndi.flags = ND_IFINFO(ifp)->flags;
1343d6b9e9b9Sitojun 		ndi->ndi.recalctm = ND_IFINFO(ifp)->recalctm;
1344d6b9e9b9Sitojun 		ndi->ndi.chlim = ND_IFINFO(ifp)->chlim;
1345cfab95f2Sitojun 		break;
1346d6b9e9b9Sitojun 	case SIOCGIFINFO_IN6:
1347d6b9e9b9Sitojun 		ndi->ndi = *ND_IFINFO(ifp);
1348287546eaSitojun 		break;
1349d374aaacSitojun 	case SIOCSIFINFO_FLAGS:
1350d6b9e9b9Sitojun 		ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1351d374aaacSitojun 		break;
1352f4f4d166Sitojun 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1353d8a7e3a7Sitojun 		/* sync kernel routing table with the default router list */
1354d8a7e3a7Sitojun 		defrouter_reset();
1355f4f4d166Sitojun 		defrouter_select();
1356287546eaSitojun 		break;
1357287546eaSitojun 	case SIOCSPFXFLUSH_IN6:
1358287546eaSitojun 	{
1359287546eaSitojun 		/* flush all the prefix advertised by routers */
1360119a433cSbluhm 		struct nd_prefix *pr, *npr;
1361287546eaSitojun 
13626e92dee6Sitojun 		s = splsoftnet();
1363db657d52Sbluhm 		/* First purge the addresses referenced by a prefix. */
1364119a433cSbluhm 		LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
1365d8a7e3a7Sitojun 			struct in6_ifaddr *ia, *ia_next;
1366d8a7e3a7Sitojun 
1367d8a7e3a7Sitojun 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1368d8a7e3a7Sitojun 				continue; /* XXX */
1369d8a7e3a7Sitojun 
1370d8a7e3a7Sitojun 			/* do we really have to remove addresses as well? */
1371a0fa8079Smpi 			TAILQ_FOREACH_SAFE(ia, &in6_ifaddr, ia_list, ia_next) {
1372d8a7e3a7Sitojun 				if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1373d8a7e3a7Sitojun 					continue;
1374d8a7e3a7Sitojun 
1375d8a7e3a7Sitojun 				if (ia->ia6_ndpr == pr)
1376d8a7e3a7Sitojun 					in6_purgeaddr(&ia->ia_ifa);
1377d8a7e3a7Sitojun 			}
1378db657d52Sbluhm 		}
1379db657d52Sbluhm 		/*
1380db657d52Sbluhm 		 * Purging the addresses might remove the prefix as well.
1381db657d52Sbluhm 		 * So run the loop again to access only prefixes that have
1382db657d52Sbluhm 		 * not been freed already.
1383db657d52Sbluhm 		 */
1384119a433cSbluhm 		LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
1385db657d52Sbluhm 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1386db657d52Sbluhm 				continue; /* XXX */
1387db657d52Sbluhm 
1388287546eaSitojun 			prelist_remove(pr);
1389287546eaSitojun 		}
1390287546eaSitojun 		splx(s);
1391287546eaSitojun 		break;
1392287546eaSitojun 	}
1393287546eaSitojun 	case SIOCSRTRFLUSH_IN6:
1394287546eaSitojun 	{
1395287546eaSitojun 		/* flush all the default routers */
1396568012e6Sbluhm 		struct nd_defrouter *dr, *ndr;
1397287546eaSitojun 
13986e92dee6Sitojun 		s = splsoftnet();
1399d8a7e3a7Sitojun 		defrouter_reset();
1400568012e6Sbluhm 		TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr)
1401287546eaSitojun 			defrtrlist_del(dr);
1402d8a7e3a7Sitojun 		defrouter_select();
1403287546eaSitojun 		splx(s);
1404287546eaSitojun 		break;
1405287546eaSitojun 	}
1406287546eaSitojun 	case SIOCGNBRINFO_IN6:
1407287546eaSitojun 	{
1408287546eaSitojun 		struct llinfo_nd6 *ln;
1409287546eaSitojun 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1410287546eaSitojun 
1411287546eaSitojun 		/*
1412287546eaSitojun 		 * XXX: KAME specific hack for scoped addresses
1413287546eaSitojun 		 *      XXXX: for other scopes than link-local?
1414287546eaSitojun 		 */
1415287546eaSitojun 		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1416287546eaSitojun 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1417287546eaSitojun 			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1418287546eaSitojun 
1419287546eaSitojun 			if (*idp == 0)
1420287546eaSitojun 				*idp = htons(ifp->if_index);
1421287546eaSitojun 		}
1422287546eaSitojun 
14236e92dee6Sitojun 		s = splsoftnet();
1424d8a7e3a7Sitojun 		if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL ||
1425d8a7e3a7Sitojun 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1426287546eaSitojun 			error = EINVAL;
142791e69634Sitojun 			splx(s);
1428287546eaSitojun 			break;
1429287546eaSitojun 		}
1430287546eaSitojun 		nbi->state = ln->ln_state;
1431287546eaSitojun 		nbi->asked = ln->ln_asked;
1432287546eaSitojun 		nbi->isrouter = ln->ln_router;
1433287546eaSitojun 		nbi->expire = ln->ln_expire;
1434287546eaSitojun 		splx(s);
1435287546eaSitojun 
1436287546eaSitojun 		break;
1437287546eaSitojun 	}
1438287546eaSitojun 	}
1439287546eaSitojun 	return (error);
1440287546eaSitojun }
1441287546eaSitojun 
1442287546eaSitojun /*
1443287546eaSitojun  * Create neighbor cache entry and cache link-layer address,
1444287546eaSitojun  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1445ee37ea65Smcbride  *
1446ee37ea65Smcbride  * type - ICMP6 type
1447ee37ea65Smcbride  * code - type dependent information
1448287546eaSitojun  */
1449287546eaSitojun struct rtentry *
1450ee37ea65Smcbride nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1451ee37ea65Smcbride     int lladdrlen, int type, int code)
1452287546eaSitojun {
1453287546eaSitojun 	struct rtentry *rt = NULL;
1454287546eaSitojun 	struct llinfo_nd6 *ln = NULL;
1455287546eaSitojun 	int is_newentry;
1456287546eaSitojun 	struct sockaddr_dl *sdl = NULL;
1457287546eaSitojun 	int do_update;
1458287546eaSitojun 	int olladdr;
1459287546eaSitojun 	int llchange;
1460287546eaSitojun 	int newstate = 0;
1461287546eaSitojun 
1462287546eaSitojun 	if (!ifp)
1463287546eaSitojun 		panic("ifp == NULL in nd6_cache_lladdr");
1464287546eaSitojun 	if (!from)
1465287546eaSitojun 		panic("from == NULL in nd6_cache_lladdr");
1466287546eaSitojun 
1467287546eaSitojun 	/* nothing must be updated for unspecified address */
1468287546eaSitojun 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1469287546eaSitojun 		return NULL;
1470287546eaSitojun 
1471287546eaSitojun 	/*
1472287546eaSitojun 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1473287546eaSitojun 	 * the caller.
1474287546eaSitojun 	 *
14758b542bbeSpascoe 	 * XXX If the link does not have link-layer address, what should
1476287546eaSitojun 	 * we do? (ifp->if_addrlen == 0)
1477287546eaSitojun 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1478287546eaSitojun 	 * description on it in NS section (RFC 2461 7.2.3).
1479287546eaSitojun 	 */
1480287546eaSitojun 
1481287546eaSitojun 	rt = nd6_lookup(from, 0, ifp);
1482287546eaSitojun 	if (!rt) {
1483287546eaSitojun #if 0
1484287546eaSitojun 		/* nothing must be done if there's no lladdr */
1485287546eaSitojun 		if (!lladdr || !lladdrlen)
1486287546eaSitojun 			return NULL;
1487287546eaSitojun #endif
1488287546eaSitojun 
14898e084c5aSclaudio 		rt = nd6_lookup(from, RT_REPORT, ifp);
1490287546eaSitojun 		is_newentry = 1;
14910a2c5741Sitojun 	} else {
14920a2c5741Sitojun 		/* do nothing if static ndp is set */
14930a2c5741Sitojun 		if (rt->rt_flags & RTF_STATIC)
14940a2c5741Sitojun 			return NULL;
1495287546eaSitojun 		is_newentry = 0;
14960a2c5741Sitojun 	}
1497287546eaSitojun 
1498287546eaSitojun 	if (!rt)
1499287546eaSitojun 		return NULL;
1500287546eaSitojun 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1501287546eaSitojun fail:
1502d8a7e3a7Sitojun 		(void)nd6_free(rt, 0);
1503287546eaSitojun 		return NULL;
1504287546eaSitojun 	}
1505287546eaSitojun 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1506287546eaSitojun 	if (!ln)
1507287546eaSitojun 		goto fail;
1508287546eaSitojun 	if (!rt->rt_gateway)
1509287546eaSitojun 		goto fail;
1510287546eaSitojun 	if (rt->rt_gateway->sa_family != AF_LINK)
1511287546eaSitojun 		goto fail;
1512287546eaSitojun 	sdl = SDL(rt->rt_gateway);
1513287546eaSitojun 
1514287546eaSitojun 	olladdr = (sdl->sdl_alen) ? 1 : 0;
1515287546eaSitojun 	if (olladdr && lladdr) {
1516287546eaSitojun 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1517287546eaSitojun 			llchange = 1;
1518287546eaSitojun 		else
1519287546eaSitojun 			llchange = 0;
1520287546eaSitojun 	} else
1521287546eaSitojun 		llchange = 0;
1522287546eaSitojun 
1523287546eaSitojun 	/*
1524287546eaSitojun 	 * newentry olladdr  lladdr  llchange	(*=record)
1525287546eaSitojun 	 *	0	n	n	--	(1)
1526287546eaSitojun 	 *	0	y	n	--	(2)
1527287546eaSitojun 	 *	0	n	y	--	(3) * STALE
1528287546eaSitojun 	 *	0	y	y	n	(4) *
1529287546eaSitojun 	 *	0	y	y	y	(5) * STALE
1530287546eaSitojun 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1531287546eaSitojun 	 *	1	--	y	--	(7) * STALE
1532287546eaSitojun 	 */
1533287546eaSitojun 
1534c8a7c9e3Sbluhm 	if (llchange) {
1535c8a7c9e3Sbluhm 		log(LOG_INFO, "ndp info overwritten for %s by %s on %s\n",
1536c8a7c9e3Sbluhm 		    ip6_sprintf(from), ether_sprintf(lladdr), ifp->if_xname);
1537c8a7c9e3Sbluhm 	}
1538287546eaSitojun 	if (lladdr) {		/* (3-5) and (7) */
1539287546eaSitojun 		/*
1540287546eaSitojun 		 * Record source link-layer address
1541287546eaSitojun 		 * XXX is it dependent to ifp->if_type?
1542287546eaSitojun 		 */
1543287546eaSitojun 		sdl->sdl_alen = ifp->if_addrlen;
1544287546eaSitojun 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1545287546eaSitojun 	}
1546287546eaSitojun 
1547287546eaSitojun 	if (!is_newentry) {
1548d8a7e3a7Sitojun 		if ((!olladdr && lladdr) ||		/* (3) */
1549d8a7e3a7Sitojun 		    (olladdr && lladdr && llchange)) {	/* (5) */
1550287546eaSitojun 			do_update = 1;
1551287546eaSitojun 			newstate = ND6_LLINFO_STALE;
1552287546eaSitojun 		} else					/* (1-2,4) */
1553287546eaSitojun 			do_update = 0;
1554287546eaSitojun 	} else {
1555287546eaSitojun 		do_update = 1;
1556287546eaSitojun 		if (!lladdr)				/* (6) */
1557287546eaSitojun 			newstate = ND6_LLINFO_NOSTATE;
1558287546eaSitojun 		else					/* (7) */
1559287546eaSitojun 			newstate = ND6_LLINFO_STALE;
1560287546eaSitojun 	}
1561287546eaSitojun 
1562287546eaSitojun 	if (do_update) {
1563287546eaSitojun 		/*
1564287546eaSitojun 		 * Update the state of the neighbor cache.
1565287546eaSitojun 		 */
1566287546eaSitojun 		ln->ln_state = newstate;
1567287546eaSitojun 
1568287546eaSitojun 		if (ln->ln_state == ND6_LLINFO_STALE) {
15698a7bb304Sitojun 			/*
15708a7bb304Sitojun 			 * XXX: since nd6_output() below will cause
15718b542bbeSpascoe 			 * state transition to DELAY and reset the timer,
15728a7bb304Sitojun 			 * we must set the timer now, although it is actually
15738a7bb304Sitojun 			 * meaningless.
15748a7bb304Sitojun 			 */
15759631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
15768a7bb304Sitojun 
1577287546eaSitojun 			if (ln->ln_hold) {
1578e212adedSkrw 				struct mbuf *n = ln->ln_hold;
1579e212adedSkrw 				ln->ln_hold = NULL;
15806afad192Sitojun 				/*
15816afad192Sitojun 				 * we assume ifp is not a p2p here, so just
15826afad192Sitojun 				 * set the 2nd argument as the 1st one.
15836afad192Sitojun 				 */
1584e212adedSkrw 				nd6_output(ifp, ifp, n,
1585*c3c56496Sbluhm 				    satosin6(rt_key(rt)), rt);
1586e212adedSkrw 				if (ln->ln_hold == n) {
1587e212adedSkrw 					/* n is back in ln_hold. Discard. */
1588e212adedSkrw 					m_freem(ln->ln_hold);
15898a7bb304Sitojun 					ln->ln_hold = NULL;
1590287546eaSitojun 				}
1591e212adedSkrw 			}
1592287546eaSitojun 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1593287546eaSitojun 			/* probe right away */
15949631a17bSitojun 			nd6_llinfo_settimer((void *)ln, 0);
1595287546eaSitojun 		}
1596287546eaSitojun 	}
1597287546eaSitojun 
1598287546eaSitojun 	/*
1599287546eaSitojun 	 * ICMP6 type dependent behavior.
1600287546eaSitojun 	 *
1601287546eaSitojun 	 * NS: clear IsRouter if new entry
1602287546eaSitojun 	 * RS: clear IsRouter
1603287546eaSitojun 	 * RA: set IsRouter if there's lladdr
1604287546eaSitojun 	 * redir: clear IsRouter if new entry
1605287546eaSitojun 	 *
1606287546eaSitojun 	 * RA case, (1):
1607287546eaSitojun 	 * The spec says that we must set IsRouter in the following cases:
1608287546eaSitojun 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1609287546eaSitojun 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1610287546eaSitojun 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
16118b542bbeSpascoe 	 * A question arises for (1) case.  (1) case has no lladdr in the
1612287546eaSitojun 	 * neighbor cache, this is similar to (6).
1613287546eaSitojun 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1614287546eaSitojun 	 *
1615287546eaSitojun 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1616287546eaSitojun 	 *							D R
1617287546eaSitojun 	 *	0	n	n	--	(1)	c   ?     s
1618287546eaSitojun 	 *	0	y	n	--	(2)	c   s     s
1619287546eaSitojun 	 *	0	n	y	--	(3)	c   s     s
1620287546eaSitojun 	 *	0	y	y	n	(4)	c   s     s
1621287546eaSitojun 	 *	0	y	y	y	(5)	c   s     s
1622287546eaSitojun 	 *	1	--	n	--	(6) c	c 	c s
1623287546eaSitojun 	 *	1	--	y	--	(7) c	c   s	c s
1624287546eaSitojun 	 *
1625287546eaSitojun 	 *					(c=clear s=set)
1626287546eaSitojun 	 */
1627287546eaSitojun 	switch (type & 0xff) {
1628287546eaSitojun 	case ND_NEIGHBOR_SOLICIT:
1629287546eaSitojun 		/*
1630287546eaSitojun 		 * New entry must have is_router flag cleared.
1631287546eaSitojun 		 */
1632287546eaSitojun 		if (is_newentry)	/* (6-7) */
1633287546eaSitojun 			ln->ln_router = 0;
1634287546eaSitojun 		break;
1635287546eaSitojun 	case ND_REDIRECT:
1636287546eaSitojun 		/*
1637287546eaSitojun 		 * If the icmp is a redirect to a better router, always set the
1638287546eaSitojun 		 * is_router flag.  Otherwise, if the entry is newly created,
1639287546eaSitojun 		 * clear the flag.  [RFC 2461, sec 8.3]
1640287546eaSitojun 		 */
1641287546eaSitojun 		if (code == ND_REDIRECT_ROUTER)
1642287546eaSitojun 			ln->ln_router = 1;
1643287546eaSitojun 		else if (is_newentry) /* (6-7) */
1644287546eaSitojun 			ln->ln_router = 0;
1645287546eaSitojun 		break;
1646287546eaSitojun 	case ND_ROUTER_SOLICIT:
1647287546eaSitojun 		/*
1648287546eaSitojun 		 * is_router flag must always be cleared.
1649287546eaSitojun 		 */
1650287546eaSitojun 		ln->ln_router = 0;
1651287546eaSitojun 		break;
1652287546eaSitojun 	case ND_ROUTER_ADVERT:
1653287546eaSitojun 		/*
1654287546eaSitojun 		 * Mark an entry with lladdr as a router.
1655287546eaSitojun 		 */
1656d8a7e3a7Sitojun 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1657d8a7e3a7Sitojun 		    (is_newentry && lladdr)) {			/* (7) */
1658287546eaSitojun 			ln->ln_router = 1;
1659287546eaSitojun 		}
1660287546eaSitojun 		break;
1661287546eaSitojun 	}
1662287546eaSitojun 
166350ccc024Sitojun 	/*
166450ccc024Sitojun 	 * When the link-layer address of a router changes, select the
166550ccc024Sitojun 	 * best router again.  In particular, when the neighbor entry is newly
166650ccc024Sitojun 	 * created, it might affect the selection policy.
166750ccc024Sitojun 	 * Question: can we restrict the first condition to the "is_newentry"
166850ccc024Sitojun 	 * case?
166950ccc024Sitojun 	 * XXX: when we hear an RA from a new router with the link-layer
167050ccc024Sitojun 	 * address option, defrouter_select() is called twice, since
167150ccc024Sitojun 	 * defrtrlist_update called the function as well.  However, I believe
167250ccc024Sitojun 	 * we can compromise the overhead, since it only happens the first
167350ccc024Sitojun 	 * time.
1674d8a7e3a7Sitojun 	 * XXX: although defrouter_select() should not have a bad effect
1675d8a7e3a7Sitojun 	 * for those are not autoconfigured hosts, we explicitly avoid such
1676d8a7e3a7Sitojun 	 * cases for safety.
167750ccc024Sitojun 	 */
167829ee75acSitojun 	if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv)
167950ccc024Sitojun 		defrouter_select();
168050ccc024Sitojun 
1681287546eaSitojun 	return rt;
1682287546eaSitojun }
1683287546eaSitojun 
1684a0aa363cSjsing void
1685ee37ea65Smcbride nd6_slowtimo(void *ignored_arg)
1686287546eaSitojun {
16876e92dee6Sitojun 	int s = splsoftnet();
1688b3c1e4c1Sitojun 	struct nd_ifinfo *nd6if;
1689d6b9e9b9Sitojun 	struct ifnet *ifp;
1690287546eaSitojun 
1691b3c1e4c1Sitojun 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
169229e86e5eSblambert 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
1693d814b14cSbluhm 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1694d6b9e9b9Sitojun 		nd6if = ND_IFINFO(ifp);
1695287546eaSitojun 		if (nd6if->basereachable && /* already initialized */
1696287546eaSitojun 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1697287546eaSitojun 			/*
1698287546eaSitojun 			 * Since reachable time rarely changes by router
1699287546eaSitojun 			 * advertisements, we SHOULD insure that a new random
1700287546eaSitojun 			 * value gets recomputed at least once every few hours.
1701287546eaSitojun 			 * (RFC 2461, 6.3.4)
1702287546eaSitojun 			 */
1703287546eaSitojun 			nd6if->recalctm = nd6_recalc_reachtm_interval;
1704287546eaSitojun 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1705287546eaSitojun 		}
1706287546eaSitojun 	}
1707287546eaSitojun 	splx(s);
1708287546eaSitojun }
1709287546eaSitojun 
1710287546eaSitojun #define senderr(e) { error = (e); goto bad;}
1711287546eaSitojun int
1712ee37ea65Smcbride nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0,
1713ee37ea65Smcbride     struct sockaddr_in6 *dst, struct rtentry *rt0)
1714287546eaSitojun {
1715b3c1e4c1Sitojun 	struct mbuf *m = m0;
1716b3c1e4c1Sitojun 	struct rtentry *rt = rt0;
1717cfb6b8dfSitojun 	struct sockaddr_in6 *gw6 = NULL;
1718287546eaSitojun 	struct llinfo_nd6 *ln = NULL;
1719287546eaSitojun 	int error = 0;
1720cb39d30aSangelos #ifdef IPSEC
1721cb39d30aSangelos 	struct m_tag *mtag;
1722cb39d30aSangelos #endif /* IPSEC */
1723287546eaSitojun 
1724287546eaSitojun 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1725287546eaSitojun 		goto sendpkt;
1726287546eaSitojun 
1727d8a7e3a7Sitojun 	if (nd6_need_cache(ifp) == 0)
1728287546eaSitojun 		goto sendpkt;
1729287546eaSitojun 
1730287546eaSitojun 	/*
17318b542bbeSpascoe 	 * next hop determination.  This routine is derived from ether_output.
1732287546eaSitojun 	 */
1733287546eaSitojun 	if (rt) {
1734287546eaSitojun 		if ((rt->rt_flags & RTF_UP) == 0) {
1735*c3c56496Sbluhm 			if ((rt0 = rt = rtalloc1(sin6tosa(dst),
1736ba79ddd5Ssperreault 			    RT_REPORT, m->m_pkthdr.rdomain)) != NULL)
1737287546eaSitojun 			{
1738287546eaSitojun 				rt->rt_refcnt--;
1739fe16f6efSitojun 				if (rt->rt_ifp != ifp)
1740fe16f6efSitojun 					senderr(EHOSTUNREACH);
1741287546eaSitojun 			} else
1742287546eaSitojun 				senderr(EHOSTUNREACH);
1743287546eaSitojun 		}
1744cfb6b8dfSitojun 
1745287546eaSitojun 		if (rt->rt_flags & RTF_GATEWAY) {
1746*c3c56496Sbluhm 			gw6 = satosin6(rt->rt_gateway);
1747cfb6b8dfSitojun 
1748cfb6b8dfSitojun 			/*
1749cfb6b8dfSitojun 			 * We skip link-layer address resolution and NUD
1750cfb6b8dfSitojun 			 * if the gateway is not a neighbor from ND point
1751d8a7e3a7Sitojun 			 * of view, regardless of the value of nd_ifinfo.flags.
175234deef1eSitojun 			 * The second condition is a bit tricky; we skip
1753cfb6b8dfSitojun 			 * if the gateway is our own address, which is
1754cfb6b8dfSitojun 			 * sometimes used to install a route to a p2p link.
1755cfb6b8dfSitojun 			 */
1756cfb6b8dfSitojun 			if (!nd6_is_addr_neighbor(gw6, ifp) ||
1757cfb6b8dfSitojun 			    in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) {
1758cfb6b8dfSitojun 				/*
1759cfb6b8dfSitojun 				 * We allow this kind of tricky route only
1760cfb6b8dfSitojun 				 * when the outgoing interface is p2p.
1761cfb6b8dfSitojun 				 * XXX: we may need a more generic rule here.
1762cfb6b8dfSitojun 				 */
1763cfb6b8dfSitojun 				if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
1764cfb6b8dfSitojun 					senderr(EHOSTUNREACH);
1765cfb6b8dfSitojun 
1766cfb6b8dfSitojun 				goto sendpkt;
1767cfb6b8dfSitojun 			}
1768cfb6b8dfSitojun 
1769287546eaSitojun 			if (rt->rt_gwroute == 0)
1770287546eaSitojun 				goto lookup;
1771287546eaSitojun 			if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) {
1772287546eaSitojun 				rtfree(rt); rt = rt0;
1773d8a7e3a7Sitojun 			lookup:
17748e084c5aSclaudio 				rt->rt_gwroute = rtalloc1(rt->rt_gateway,
1775ba79ddd5Ssperreault 				    RT_REPORT, m->m_pkthdr.rdomain);
1776287546eaSitojun 				if ((rt = rt->rt_gwroute) == 0)
1777287546eaSitojun 					senderr(EHOSTUNREACH);
1778287546eaSitojun 			}
1779287546eaSitojun 		}
1780287546eaSitojun 	}
1781287546eaSitojun 
1782287546eaSitojun 	/*
1783287546eaSitojun 	 * Address resolution or Neighbor Unreachability Detection
1784287546eaSitojun 	 * for the next hop.
1785287546eaSitojun 	 * At this point, the destination of the packet must be a unicast
1786287546eaSitojun 	 * or an anycast address(i.e. not a multicast).
1787287546eaSitojun 	 */
1788287546eaSitojun 
1789287546eaSitojun 	/* Look up the neighbor cache for the nexthop */
1790287546eaSitojun 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1791287546eaSitojun 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1792287546eaSitojun 	else {
1793cfb6b8dfSitojun 		/*
1794cfb6b8dfSitojun 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1795cfb6b8dfSitojun 		 * the condition below is not very efficient.  But we believe
1796cfb6b8dfSitojun 		 * it is tolerable, because this should be a rare case.
1797cfb6b8dfSitojun 		 */
1798cfb6b8dfSitojun 		if (nd6_is_addr_neighbor(dst, ifp) &&
17998e084c5aSclaudio 		    (rt = nd6_lookup(&dst->sin6_addr, RT_REPORT, ifp)) != NULL)
1800287546eaSitojun 			ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1801287546eaSitojun 	}
1802287546eaSitojun 	if (!ln || !rt) {
1803cfb6b8dfSitojun 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1804d6b9e9b9Sitojun 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1805cfb6b8dfSitojun 			log(LOG_DEBUG,
1806cfb6b8dfSitojun 			    "nd6_output: can't allocate llinfo for %s "
1807287546eaSitojun 			    "(ln=%p, rt=%p)\n",
1808287546eaSitojun 			    ip6_sprintf(&dst->sin6_addr), ln, rt);
1809287546eaSitojun 			senderr(EIO);	/* XXX: good error? */
1810287546eaSitojun 		}
1811287546eaSitojun 
1812cfb6b8dfSitojun 		goto sendpkt;	/* send anyway */
1813cfb6b8dfSitojun 	}
1814cfb6b8dfSitojun 
1815f3fcf2f3Smcbride 	/*
1816f3fcf2f3Smcbride 	 * Move this entry to the head of the queue so that it is less likely
1817f3fcf2f3Smcbride 	 * for this entry to be a target of forced garbage collection (see
1818f3fcf2f3Smcbride 	 * nd6_rtrequest()).
1819f3fcf2f3Smcbride 	 */
1820f3fcf2f3Smcbride 	LN_DEQUEUE(ln);
1821f3fcf2f3Smcbride 	LN_INSERTHEAD(ln);
1822f3fcf2f3Smcbride 
1823d374aaacSitojun 	/* We don't have to do link-layer address resolution on a p2p link. */
1824d374aaacSitojun 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1825be4e9e12Sitojun 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
1826d374aaacSitojun 		ln->ln_state = ND6_LLINFO_STALE;
18279631a17bSitojun 		nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1828be4e9e12Sitojun 	}
1829287546eaSitojun 
1830287546eaSitojun 	/*
1831287546eaSitojun 	 * The first time we send a packet to a neighbor whose entry is
1832287546eaSitojun 	 * STALE, we have to change the state to DELAY and a sets a timer to
1833287546eaSitojun 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1834287546eaSitojun 	 * neighbor unreachability detection on expiration.
1835287546eaSitojun 	 * (RFC 2461 7.3.3)
1836287546eaSitojun 	 */
1837287546eaSitojun 	if (ln->ln_state == ND6_LLINFO_STALE) {
1838287546eaSitojun 		ln->ln_asked = 0;
1839287546eaSitojun 		ln->ln_state = ND6_LLINFO_DELAY;
18409631a17bSitojun 		nd6_llinfo_settimer(ln, nd6_delay * hz);
1841287546eaSitojun 	}
1842287546eaSitojun 
1843287546eaSitojun 	/*
1844287546eaSitojun 	 * If the neighbor cache entry has a state other than INCOMPLETE
184534deef1eSitojun 	 * (i.e. its link-layer address is already resolved), just
1846287546eaSitojun 	 * send the packet.
1847287546eaSitojun 	 */
1848287546eaSitojun 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1849287546eaSitojun 		goto sendpkt;
1850287546eaSitojun 
1851287546eaSitojun 	/*
1852287546eaSitojun 	 * There is a neighbor cache entry, but no ethernet address
1853287546eaSitojun 	 * response yet.  Replace the held mbuf (if any) with this
1854287546eaSitojun 	 * latest one.
1855287546eaSitojun 	 */
1856efcf292bSitojun 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1857287546eaSitojun 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1858287546eaSitojun 	if (ln->ln_hold)
1859287546eaSitojun 		m_freem(ln->ln_hold);
1860287546eaSitojun 	ln->ln_hold = m;
186176843262Sitojun 	/*
186276843262Sitojun 	 * If there has been no NS for the neighbor after entering the
186376843262Sitojun 	 * INCOMPLETE state, send the first solicitation.
186476843262Sitojun 	 */
18659631a17bSitojun 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
1866287546eaSitojun 		ln->ln_asked++;
18679631a17bSitojun 		nd6_llinfo_settimer(ln,
18689631a17bSitojun 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1869287546eaSitojun 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1870287546eaSitojun 	}
1871287546eaSitojun 	return (0);
1872287546eaSitojun 
1873287546eaSitojun   sendpkt:
1874cb39d30aSangelos #ifdef IPSEC
1875cb39d30aSangelos 	/*
1876aa22e115Shaesbaert 	 * If we got here and IPsec crypto processing didn't happen, drop it.
1877cb39d30aSangelos 	 */
1878cb39d30aSangelos 	mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL);
1879cb39d30aSangelos #endif /* IPSEC */
18806afad192Sitojun 
18810a2c5741Sitojun 	if ((ifp->if_flags & IFF_LOOPBACK) != 0) {
1882cb39d30aSangelos #ifdef IPSEC
1883aa22e115Shaesbaert 		if (mtag != NULL) {
1884cb39d30aSangelos 			/* Tell IPsec to do its own crypto. */
1885cb39d30aSangelos 			ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
1886cb39d30aSangelos 			error = EACCES;
1887cb39d30aSangelos 			goto bad;
1888cb39d30aSangelos 		}
1889cb39d30aSangelos #endif /* IPSEC */
1890*c3c56496Sbluhm 		return ((*ifp->if_output)(origifp, m, sin6tosa(dst), rt));
18916afad192Sitojun 	}
1892cb39d30aSangelos #ifdef IPSEC
1893aa22e115Shaesbaert 	if (mtag != NULL) {
1894cb39d30aSangelos 		/* Tell IPsec to do its own crypto. */
1895cb39d30aSangelos 		ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1));
1896cb39d30aSangelos 		error = EACCES;
1897cb39d30aSangelos 		goto bad;
1898cb39d30aSangelos 	}
1899cb39d30aSangelos #endif /* IPSEC */
1900*c3c56496Sbluhm 	return ((*ifp->if_output)(ifp, m, sin6tosa(dst), rt));
1901287546eaSitojun 
1902287546eaSitojun   bad:
1903287546eaSitojun 	if (m)
1904287546eaSitojun 		m_freem(m);
1905287546eaSitojun 	return (error);
1906287546eaSitojun }
1907287546eaSitojun #undef senderr
1908287546eaSitojun 
1909287546eaSitojun int
1910ee37ea65Smcbride nd6_need_cache(struct ifnet *ifp)
1911d8a7e3a7Sitojun {
1912d8a7e3a7Sitojun 	/*
1913d8a7e3a7Sitojun 	 * XXX: we currently do not make neighbor cache on any interface
191440765843Shenning 	 * other than Ethernet, FDDI and GIF.
1915d8a7e3a7Sitojun 	 *
1916d8a7e3a7Sitojun 	 * RFC2893 says:
1917d8a7e3a7Sitojun 	 * - unidirectional tunnels needs no ND
1918d8a7e3a7Sitojun 	 */
1919d8a7e3a7Sitojun 	switch (ifp->if_type) {
1920d8a7e3a7Sitojun 	case IFT_ETHER:
1921d8a7e3a7Sitojun 	case IFT_FDDI:
1922d8a7e3a7Sitojun 	case IFT_IEEE1394:
1923d8a7e3a7Sitojun 	case IFT_PROPVIRTUAL:
1924d8a7e3a7Sitojun 	case IFT_L2VLAN:
1925d8a7e3a7Sitojun 	case IFT_IEEE80211:
1926f4433d56Shenning 	case IFT_CARP:
1927d8a7e3a7Sitojun 	case IFT_GIF:		/* XXX need more cases? */
1928d8a7e3a7Sitojun 		return (1);
1929d8a7e3a7Sitojun 	default:
1930d8a7e3a7Sitojun 		return (0);
1931d8a7e3a7Sitojun 	}
1932d8a7e3a7Sitojun }
1933d8a7e3a7Sitojun 
1934d8a7e3a7Sitojun int
1935ee37ea65Smcbride nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m,
1936ee37ea65Smcbride     struct sockaddr *dst, u_char *desten)
1937287546eaSitojun {
1938287546eaSitojun 	struct sockaddr_dl *sdl;
1939287546eaSitojun 
1940287546eaSitojun 	if (m->m_flags & M_MCAST) {
1941287546eaSitojun 		switch (ifp->if_type) {
1942287546eaSitojun 		case IFT_ETHER:
1943287546eaSitojun 		case IFT_FDDI:
1944*c3c56496Sbluhm 			ETHER_MAP_IPV6_MULTICAST(&satosin6(dst)->sin6_addr,
1945287546eaSitojun 						 desten);
1946287546eaSitojun 			return (1);
1947287546eaSitojun 			break;
1948287546eaSitojun 		default:
19490a2c5741Sitojun 			m_freem(m);
1950287546eaSitojun 			return (0);
1951287546eaSitojun 		}
1952287546eaSitojun 	}
1953287546eaSitojun 
195472e30262Sitojun 	if (rt == NULL) {
195572e30262Sitojun 		/* this could happen, if we could not allocate memory */
19560a2c5741Sitojun 		m_freem(m);
195772e30262Sitojun 		return (0);
195872e30262Sitojun 	}
195972e30262Sitojun 	if (rt->rt_gateway->sa_family != AF_LINK) {
1960287546eaSitojun 		printf("nd6_storelladdr: something odd happens\n");
19610a2c5741Sitojun 		m_freem(m);
1962287546eaSitojun 		return (0);
1963287546eaSitojun 	}
1964287546eaSitojun 	sdl = SDL(rt->rt_gateway);
1965b4ee66fcSitojun 	if (sdl->sdl_alen == 0) {
1966bd2806bcSitojun 		/* this should be impossible, but we bark here for debugging */
1967d8a7e3a7Sitojun 		printf("nd6_storelladdr: sdl_alen == 0, dst=%s, if=%s\n",
1968*c3c56496Sbluhm 		    ip6_sprintf(&satosin6(dst)->sin6_addr), ifp->if_xname);
19690a2c5741Sitojun 		m_freem(m);
1970b4ee66fcSitojun 		return (0);
1971b4ee66fcSitojun 	}
1972287546eaSitojun 
1973b4ee66fcSitojun 	bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
1974287546eaSitojun 	return (1);
1975287546eaSitojun }
1976d8a7e3a7Sitojun 
1977ee37ea65Smcbride /*
1978ee37ea65Smcbride  * oldp - syscall arg, need copyout
1979ee37ea65Smcbride  * newp - syscall arg, need copyin
1980ee37ea65Smcbride  */
1981ee37ea65Smcbride 
1982d8a7e3a7Sitojun int
1983a0aa363cSjsing nd6_sysctl(int name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
1984d8a7e3a7Sitojun {
1985db3db419Sitojun 	void *p;
198634223ca4Schl 	size_t ol;
1987d8a7e3a7Sitojun 	int error;
1988d8a7e3a7Sitojun 
1989d8a7e3a7Sitojun 	error = 0;
1990d8a7e3a7Sitojun 
1991d8a7e3a7Sitojun 	if (newp)
1992d8a7e3a7Sitojun 		return EPERM;
1993d8a7e3a7Sitojun 	if (oldp && !oldlenp)
1994d8a7e3a7Sitojun 		return EINVAL;
1995d8a7e3a7Sitojun 	ol = oldlenp ? *oldlenp : 0;
1996d8a7e3a7Sitojun 
1997db3db419Sitojun 	if (oldp) {
1998e6ca3d0dSmk 		p = malloc(*oldlenp, M_TEMP, M_WAITOK | M_CANFAIL);
1999db3db419Sitojun 		if (!p)
2000db3db419Sitojun 			return ENOMEM;
2001db3db419Sitojun 	} else
2002db3db419Sitojun 		p = NULL;
2003d8a7e3a7Sitojun 	switch (name) {
2004d8a7e3a7Sitojun 	case ICMPV6CTL_ND6_DRLIST:
2005db3db419Sitojun 		error = fill_drlist(p, oldlenp, ol);
2006db3db419Sitojun 		if (!error && p && oldp)
2007732568ffSitojun 			error = copyout(p, oldp, *oldlenp);
2008d8a7e3a7Sitojun 		break;
2009d8a7e3a7Sitojun 
2010d8a7e3a7Sitojun 	case ICMPV6CTL_ND6_PRLIST:
2011db3db419Sitojun 		error = fill_prlist(p, oldlenp, ol);
2012db3db419Sitojun 		if (!error && p && oldp)
2013732568ffSitojun 			error = copyout(p, oldp, *oldlenp);
2014d8a7e3a7Sitojun 		break;
2015d8a7e3a7Sitojun 
2016d8a7e3a7Sitojun 	default:
2017d8a7e3a7Sitojun 		error = ENOPROTOOPT;
2018d8a7e3a7Sitojun 		break;
2019d8a7e3a7Sitojun 	}
2020db3db419Sitojun 	if (p)
2021db3db419Sitojun 		free(p, M_TEMP);
2022d8a7e3a7Sitojun 
2023d8a7e3a7Sitojun 	return (error);
2024d8a7e3a7Sitojun }
2025d8a7e3a7Sitojun 
2026a0aa363cSjsing int
2027ee37ea65Smcbride fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
2028d8a7e3a7Sitojun {
2029d8a7e3a7Sitojun 	int error = 0, s;
2030d8a7e3a7Sitojun 	struct in6_defrouter *d = NULL, *de = NULL;
2031d8a7e3a7Sitojun 	struct nd_defrouter *dr;
2032d8a7e3a7Sitojun 	size_t l;
2033d8a7e3a7Sitojun 
20346e92dee6Sitojun 	s = splsoftnet();
2035d8a7e3a7Sitojun 
2036d8a7e3a7Sitojun 	if (oldp) {
2037d8a7e3a7Sitojun 		d = (struct in6_defrouter *)oldp;
2038d8a7e3a7Sitojun 		de = (struct in6_defrouter *)((caddr_t)oldp + *oldlenp);
2039d8a7e3a7Sitojun 	}
2040d8a7e3a7Sitojun 	l = 0;
2041d8a7e3a7Sitojun 
2042568012e6Sbluhm 	TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
2043d8a7e3a7Sitojun 		if (oldp && d + 1 <= de) {
2044d8a7e3a7Sitojun 			bzero(d, sizeof(*d));
2045d8a7e3a7Sitojun 			d->rtaddr.sin6_family = AF_INET6;
2046d8a7e3a7Sitojun 			d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
2047d8a7e3a7Sitojun 			d->rtaddr.sin6_addr = dr->rtaddr;
2048d8a7e3a7Sitojun 			in6_recoverscope(&d->rtaddr, &d->rtaddr.sin6_addr,
2049d8a7e3a7Sitojun 			    dr->ifp);
2050d8a7e3a7Sitojun 			d->flags = dr->flags;
2051d8a7e3a7Sitojun 			d->rtlifetime = dr->rtlifetime;
2052d8a7e3a7Sitojun 			d->expire = dr->expire;
2053d8a7e3a7Sitojun 			d->if_index = dr->ifp->if_index;
2054d8a7e3a7Sitojun 		}
2055d8a7e3a7Sitojun 
2056d8a7e3a7Sitojun 		l += sizeof(*d);
2057d8a7e3a7Sitojun 		if (d)
2058d8a7e3a7Sitojun 			d++;
2059d8a7e3a7Sitojun 	}
2060d8a7e3a7Sitojun 
2061d8a7e3a7Sitojun 	if (oldp) {
2062d8a7e3a7Sitojun 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
2063d8a7e3a7Sitojun 		if (l > ol)
2064d8a7e3a7Sitojun 			error = ENOMEM;
2065d8a7e3a7Sitojun 	} else
2066d8a7e3a7Sitojun 		*oldlenp = l;
2067d8a7e3a7Sitojun 
2068d8a7e3a7Sitojun 	splx(s);
2069d8a7e3a7Sitojun 
2070d8a7e3a7Sitojun 	return (error);
2071d8a7e3a7Sitojun }
2072d8a7e3a7Sitojun 
2073a0aa363cSjsing int
2074ee37ea65Smcbride fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
2075d8a7e3a7Sitojun {
2076d8a7e3a7Sitojun 	int error = 0, s;
2077d8a7e3a7Sitojun 	struct nd_prefix *pr;
2078d8a7e3a7Sitojun 	struct in6_prefix *p = NULL;
2079d8a7e3a7Sitojun 	struct in6_prefix *pe = NULL;
2080d8a7e3a7Sitojun 	size_t l;
2081d8a7e3a7Sitojun 
20826e92dee6Sitojun 	s = splsoftnet();
2083d8a7e3a7Sitojun 
2084d8a7e3a7Sitojun 	if (oldp) {
2085d8a7e3a7Sitojun 		p = (struct in6_prefix *)oldp;
2086d8a7e3a7Sitojun 		pe = (struct in6_prefix *)((caddr_t)oldp + *oldlenp);
2087d8a7e3a7Sitojun 	}
2088d8a7e3a7Sitojun 	l = 0;
2089d8a7e3a7Sitojun 
2090545205eaSpyr 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
2091d8a7e3a7Sitojun 		u_short advrtrs;
2092d8a7e3a7Sitojun 		size_t advance;
2093d8a7e3a7Sitojun 		struct sockaddr_in6 *sin6;
2094d8a7e3a7Sitojun 		struct sockaddr_in6 *s6;
2095d8a7e3a7Sitojun 		struct nd_pfxrouter *pfr;
2096d8a7e3a7Sitojun 
2097d8a7e3a7Sitojun 		if (oldp && p + 1 <= pe)
2098d8a7e3a7Sitojun 		{
2099d8a7e3a7Sitojun 			bzero(p, sizeof(*p));
2100d8a7e3a7Sitojun 			sin6 = (struct sockaddr_in6 *)(p + 1);
2101d8a7e3a7Sitojun 
2102d8a7e3a7Sitojun 			p->prefix = pr->ndpr_prefix;
2103d8a7e3a7Sitojun 			if (in6_recoverscope(&p->prefix,
2104d8a7e3a7Sitojun 			    &p->prefix.sin6_addr, pr->ndpr_ifp) != 0)
2105d8a7e3a7Sitojun 				log(LOG_ERR,
2106d8a7e3a7Sitojun 				    "scope error in prefix list (%s)\n",
2107d8a7e3a7Sitojun 				    ip6_sprintf(&p->prefix.sin6_addr));
2108d8a7e3a7Sitojun 			p->raflags = pr->ndpr_raf;
2109d8a7e3a7Sitojun 			p->prefixlen = pr->ndpr_plen;
2110d8a7e3a7Sitojun 			p->vltime = pr->ndpr_vltime;
2111d8a7e3a7Sitojun 			p->pltime = pr->ndpr_pltime;
2112d8a7e3a7Sitojun 			p->if_index = pr->ndpr_ifp->if_index;
2113d8a7e3a7Sitojun 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2114d8a7e3a7Sitojun 				p->expire = 0;
2115d8a7e3a7Sitojun 			else {
2116d8a7e3a7Sitojun 				time_t maxexpire;
2117d8a7e3a7Sitojun 
2118d8a7e3a7Sitojun 				/* XXX: we assume time_t is signed. */
2119d8a7e3a7Sitojun 				maxexpire = (-1) &
2120d8a7e3a7Sitojun 					~(1 << ((sizeof(maxexpire) * 8) - 1));
2121d8a7e3a7Sitojun 				if (pr->ndpr_vltime <
2122d8a7e3a7Sitojun 				    maxexpire - pr->ndpr_lastupdate) {
2123d8a7e3a7Sitojun 					p->expire = pr->ndpr_lastupdate +
2124d8a7e3a7Sitojun 						pr->ndpr_vltime;
2125d8a7e3a7Sitojun 				} else
2126d8a7e3a7Sitojun 					p->expire = maxexpire;
2127d8a7e3a7Sitojun 			}
2128d8a7e3a7Sitojun 			p->refcnt = pr->ndpr_refcnt;
2129d8a7e3a7Sitojun 			p->flags = pr->ndpr_stateflags;
2130d8a7e3a7Sitojun 			p->origin = PR_ORIG_RA;
2131d8a7e3a7Sitojun 			advrtrs = 0;
2132545205eaSpyr 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2133d8a7e3a7Sitojun 				if ((void *)&sin6[advrtrs + 1] > (void *)pe) {
2134d8a7e3a7Sitojun 					advrtrs++;
2135d8a7e3a7Sitojun 					continue;
2136d8a7e3a7Sitojun 				}
2137d8a7e3a7Sitojun 				s6 = &sin6[advrtrs];
2138d8a7e3a7Sitojun 				s6->sin6_family = AF_INET6;
2139d8a7e3a7Sitojun 				s6->sin6_len = sizeof(struct sockaddr_in6);
2140d8a7e3a7Sitojun 				s6->sin6_addr = pfr->router->rtaddr;
2141d8a7e3a7Sitojun 				in6_recoverscope(s6, &pfr->router->rtaddr,
2142d8a7e3a7Sitojun 				    pfr->router->ifp);
2143d8a7e3a7Sitojun 				advrtrs++;
2144d8a7e3a7Sitojun 			}
2145d8a7e3a7Sitojun 			p->advrtrs = advrtrs;
2146d8a7e3a7Sitojun 		}
2147d8a7e3a7Sitojun 		else {
2148d8a7e3a7Sitojun 			advrtrs = 0;
2149545205eaSpyr 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2150d8a7e3a7Sitojun 				advrtrs++;
2151d8a7e3a7Sitojun 		}
2152d8a7e3a7Sitojun 
2153d8a7e3a7Sitojun 		advance = sizeof(*p) + sizeof(*sin6) * advrtrs;
2154d8a7e3a7Sitojun 		l += advance;
2155d8a7e3a7Sitojun 		if (p)
2156d8a7e3a7Sitojun 			p = (struct in6_prefix *)((caddr_t)p + advance);
2157d8a7e3a7Sitojun 	}
2158d8a7e3a7Sitojun 
2159d8a7e3a7Sitojun 	if (oldp) {
2160d8a7e3a7Sitojun 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
2161d8a7e3a7Sitojun 		if (l > ol)
2162d8a7e3a7Sitojun 			error = ENOMEM;
2163d8a7e3a7Sitojun 	} else
2164d8a7e3a7Sitojun 		*oldlenp = l;
2165d8a7e3a7Sitojun 
2166d8a7e3a7Sitojun 	splx(s);
2167d8a7e3a7Sitojun 
2168d8a7e3a7Sitojun 	return (error);
2169d8a7e3a7Sitojun }
2170