xref: /openbsd/sys/netinet6/nd6.c (revision 7ffb277f)
1*7ffb277fSbluhm /*	$OpenBSD: nd6.c,v 1.167 2015/10/30 09:39:42 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>
47a09574ebSkettenis #include <sys/task.h>
48287546eaSitojun 
49287546eaSitojun #include <net/if.h>
50287546eaSitojun #include <net/if_dl.h>
51287546eaSitojun #include <net/if_types.h>
52287546eaSitojun #include <net/route.h>
53287546eaSitojun 
54287546eaSitojun #include <netinet/in.h>
55287546eaSitojun #include <netinet/if_ether.h>
56cb39d30aSangelos #include <netinet/ip_ipsp.h>
5758aa7d74Sangelos 
58287546eaSitojun #include <netinet6/in6_var.h>
59fa86ee14Sitojun #include <netinet/ip6.h>
60287546eaSitojun #include <netinet6/ip6_var.h>
61287546eaSitojun #include <netinet6/nd6.h>
62fa86ee14Sitojun #include <netinet/icmp6.h>
63287546eaSitojun 
64287546eaSitojun #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
65287546eaSitojun #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
66287546eaSitojun 
67287546eaSitojun /* timer values */
68287546eaSitojun int	nd6_prune	= 1;	/* walk list every 1 seconds */
69287546eaSitojun int	nd6_delay	= 5;	/* delay first probe time 5 second */
70287546eaSitojun int	nd6_umaxtries	= 3;	/* maximum unicast query */
71287546eaSitojun int	nd6_mmaxtries	= 3;	/* maximum multicast query */
72be4e9e12Sitojun int	nd6_gctimer	= (60 * 60 * 24); /* 1 day: garbage collection timer */
73287546eaSitojun 
74287546eaSitojun /* preventing too many loops in ND option parsing */
75287546eaSitojun int nd6_maxndopt = 10;	/* max # of ND options allowed */
76287546eaSitojun 
77f6e55599Sitojun int nd6_maxnudhint = 0;	/* max # of subsequent upper layer hints */
78f6e55599Sitojun 
79b79da24aSitojun #ifdef ND6_DEBUG
80b79da24aSitojun int nd6_debug = 1;
81b79da24aSitojun #else
82b79da24aSitojun int nd6_debug = 0;
83b79da24aSitojun #endif
84b79da24aSitojun 
85287546eaSitojun static int nd6_inuse, nd6_allocated;
86287546eaSitojun 
87287546eaSitojun struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6};
88f4f4d166Sitojun struct nd_drhead nd_defrouter;
89287546eaSitojun struct nd_prhead nd_prefix = { 0 };
90287546eaSitojun 
91287546eaSitojun int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
92287546eaSitojun 
93a0aa363cSjsing void nd6_slowtimo(void *);
94a0aa363cSjsing struct llinfo_nd6 *nd6_free(struct rtentry *, int);
95a0aa363cSjsing void nd6_llinfo_timer(void *);
96287546eaSitojun 
97b3c1e4c1Sitojun struct timeout nd6_slowtimo_ch;
98b3c1e4c1Sitojun struct timeout nd6_timer_ch;
99a09574ebSkettenis struct task nd6_timer_task;
100e4195480Sdlg void nd6_timer_work(void *);
101b3c1e4c1Sitojun 
102a0aa363cSjsing int fill_drlist(void *, size_t *, size_t);
103a0aa363cSjsing int fill_prlist(void *, size_t *, size_t);
104d8a7e3a7Sitojun 
105f3fcf2f3Smcbride #define LN_DEQUEUE(ln) do { \
106f3fcf2f3Smcbride 	(ln)->ln_next->ln_prev = (ln)->ln_prev; \
107f3fcf2f3Smcbride 	(ln)->ln_prev->ln_next = (ln)->ln_next; \
108f3fcf2f3Smcbride 	} while (0)
109f3fcf2f3Smcbride #define LN_INSERTHEAD(ln) do { \
110f3fcf2f3Smcbride 	(ln)->ln_next = llinfo_nd6.ln_next; \
111f3fcf2f3Smcbride 	llinfo_nd6.ln_next = (ln); \
112f3fcf2f3Smcbride 	(ln)->ln_prev = &llinfo_nd6; \
113f3fcf2f3Smcbride 	(ln)->ln_next->ln_prev = (ln); \
114f3fcf2f3Smcbride 	} while (0)
115f3fcf2f3Smcbride 
116287546eaSitojun void
117a0aa363cSjsing nd6_init(void)
118287546eaSitojun {
119287546eaSitojun 	static int nd6_init_done = 0;
120287546eaSitojun 
121287546eaSitojun 	if (nd6_init_done) {
12235075f95Smpi 		log(LOG_NOTICE, "%s called more than once\n", __func__);
123287546eaSitojun 		return;
124287546eaSitojun 	}
125287546eaSitojun 
126f4f4d166Sitojun 	/* initialization of the default router list */
127f4f4d166Sitojun 	TAILQ_INIT(&nd_defrouter);
128f4f4d166Sitojun 
129e4195480Sdlg 	task_set(&nd6_timer_task, nd6_timer_work, NULL);
130a09574ebSkettenis 
131287546eaSitojun 	nd6_init_done = 1;
132287546eaSitojun 
133287546eaSitojun 	/* start timer */
134b3c1e4c1Sitojun 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
13529e86e5eSblambert 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
136110585f2Sflorian 
13760968cbeSmpi 	nd6_rs_init();
138287546eaSitojun }
139287546eaSitojun 
140d6b9e9b9Sitojun struct nd_ifinfo *
141ee37ea65Smcbride nd6_ifattach(struct ifnet *ifp)
142287546eaSitojun {
143d6b9e9b9Sitojun 	struct nd_ifinfo *nd;
144287546eaSitojun 
145393af863Skrw 	nd = malloc(sizeof(*nd), M_IP6NDP, M_WAITOK | M_ZERO);
146287546eaSitojun 
147d6b9e9b9Sitojun 	nd->initialized = 1;
148287546eaSitojun 
149d6b9e9b9Sitojun 	nd->basereachable = REACHABLE_TIME;
150d6b9e9b9Sitojun 	nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
151d6b9e9b9Sitojun 	nd->retrans = RETRANS_TIMER;
1522a4a63f1Shenning 	/* per-interface IFXF_AUTOCONF6 needs to be set too to accept RAs */
153d8a7e3a7Sitojun 	nd->flags = (ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV);
1541b5f410aSitojun 
155d6b9e9b9Sitojun 	return nd;
156287546eaSitojun }
157287546eaSitojun 
158d6b9e9b9Sitojun void
159ee37ea65Smcbride nd6_ifdetach(struct nd_ifinfo *nd)
160d6b9e9b9Sitojun {
161d374aaacSitojun 
162dd168dc2Stedu 	free(nd, M_IP6NDP, 0);
163287546eaSitojun }
164287546eaSitojun 
165287546eaSitojun void
166ee37ea65Smcbride nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
167287546eaSitojun {
168287546eaSitojun 	bzero(ndopts, sizeof(*ndopts));
169287546eaSitojun 	ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
170287546eaSitojun 	ndopts->nd_opts_last
171287546eaSitojun 		= (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
172287546eaSitojun 
173287546eaSitojun 	if (icmp6len == 0) {
174287546eaSitojun 		ndopts->nd_opts_done = 1;
175287546eaSitojun 		ndopts->nd_opts_search = NULL;
176287546eaSitojun 	}
177287546eaSitojun }
178287546eaSitojun 
179287546eaSitojun /*
180287546eaSitojun  * Take one ND option.
181287546eaSitojun  */
182287546eaSitojun struct nd_opt_hdr *
183ee37ea65Smcbride nd6_option(union nd_opts *ndopts)
184287546eaSitojun {
185287546eaSitojun 	struct nd_opt_hdr *nd_opt;
186287546eaSitojun 	int olen;
187287546eaSitojun 
188287546eaSitojun 	if (!ndopts)
189bc84bce2Skrw 		panic("ndopts == NULL in nd6_option");
190287546eaSitojun 	if (!ndopts->nd_opts_last)
191bc84bce2Skrw 		panic("uninitialized ndopts in nd6_option");
192287546eaSitojun 	if (!ndopts->nd_opts_search)
193287546eaSitojun 		return NULL;
194287546eaSitojun 	if (ndopts->nd_opts_done)
195287546eaSitojun 		return NULL;
196287546eaSitojun 
197287546eaSitojun 	nd_opt = ndopts->nd_opts_search;
198287546eaSitojun 
19915bd77d2Sitojun 	/* make sure nd_opt_len is inside the buffer */
20015bd77d2Sitojun 	if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
20115bd77d2Sitojun 		bzero(ndopts, sizeof(*ndopts));
20215bd77d2Sitojun 		return NULL;
20315bd77d2Sitojun 	}
20415bd77d2Sitojun 
205287546eaSitojun 	olen = nd_opt->nd_opt_len << 3;
206287546eaSitojun 	if (olen == 0) {
207287546eaSitojun 		/*
208287546eaSitojun 		 * Message validation requires that all included
209287546eaSitojun 		 * options have a length that is greater than zero.
210287546eaSitojun 		 */
211287546eaSitojun 		bzero(ndopts, sizeof(*ndopts));
212287546eaSitojun 		return NULL;
213287546eaSitojun 	}
214287546eaSitojun 
215287546eaSitojun 	ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
21615bd77d2Sitojun 	if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
21715bd77d2Sitojun 		/* option overruns the end of buffer, invalid */
21815bd77d2Sitojun 		bzero(ndopts, sizeof(*ndopts));
21915bd77d2Sitojun 		return NULL;
22015bd77d2Sitojun 	} else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
22115bd77d2Sitojun 		/* reached the end of options chain */
222287546eaSitojun 		ndopts->nd_opts_done = 1;
223287546eaSitojun 		ndopts->nd_opts_search = NULL;
224287546eaSitojun 	}
225287546eaSitojun 	return nd_opt;
226287546eaSitojun }
227287546eaSitojun 
228287546eaSitojun /*
229287546eaSitojun  * Parse multiple ND options.
230287546eaSitojun  * This function is much easier to use, for ND routines that do not need
231287546eaSitojun  * multiple options of the same type.
232287546eaSitojun  */
233287546eaSitojun int
234ee37ea65Smcbride nd6_options(union nd_opts *ndopts)
235287546eaSitojun {
236287546eaSitojun 	struct nd_opt_hdr *nd_opt;
237287546eaSitojun 	int i = 0;
238287546eaSitojun 
239287546eaSitojun 	if (!ndopts)
240bc84bce2Skrw 		panic("ndopts == NULL in nd6_options");
241287546eaSitojun 	if (!ndopts->nd_opts_last)
242bc84bce2Skrw 		panic("uninitialized ndopts in nd6_options");
243287546eaSitojun 	if (!ndopts->nd_opts_search)
244287546eaSitojun 		return 0;
245287546eaSitojun 
246287546eaSitojun 	while (1) {
247287546eaSitojun 		nd_opt = nd6_option(ndopts);
248287546eaSitojun 		if (!nd_opt && !ndopts->nd_opts_last) {
249287546eaSitojun 			/*
250287546eaSitojun 			 * Message validation requires that all included
251287546eaSitojun 			 * options have a length that is greater than zero.
252287546eaSitojun 			 */
253b79da24aSitojun 			icmp6stat.icp6s_nd_badopt++;
254287546eaSitojun 			bzero(ndopts, sizeof(*ndopts));
255287546eaSitojun 			return -1;
256287546eaSitojun 		}
257287546eaSitojun 
258287546eaSitojun 		if (!nd_opt)
259287546eaSitojun 			goto skip1;
260287546eaSitojun 
261287546eaSitojun 		switch (nd_opt->nd_opt_type) {
262287546eaSitojun 		case ND_OPT_SOURCE_LINKADDR:
263287546eaSitojun 		case ND_OPT_TARGET_LINKADDR:
264287546eaSitojun 		case ND_OPT_MTU:
265287546eaSitojun 		case ND_OPT_REDIRECTED_HEADER:
266287546eaSitojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
26715bd77d2Sitojun 				nd6log((LOG_INFO,
26815bd77d2Sitojun 				    "duplicated ND6 option found (type=%d)\n",
26915bd77d2Sitojun 				    nd_opt->nd_opt_type));
270287546eaSitojun 				/* XXX bark? */
271287546eaSitojun 			} else {
272287546eaSitojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
273287546eaSitojun 					= nd_opt;
274287546eaSitojun 			}
275287546eaSitojun 			break;
276287546eaSitojun 		case ND_OPT_PREFIX_INFORMATION:
277287546eaSitojun 			if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
278287546eaSitojun 				ndopts->nd_opt_array[nd_opt->nd_opt_type]
279287546eaSitojun 					= nd_opt;
280287546eaSitojun 			}
281287546eaSitojun 			ndopts->nd_opts_pi_end =
282287546eaSitojun 				(struct nd_opt_prefix_info *)nd_opt;
283287546eaSitojun 			break;
284287546eaSitojun 		default:
285287546eaSitojun 			/*
286287546eaSitojun 			 * Unknown options must be silently ignored,
287e4d25771Stodd 			 * to accommodate future extension to the protocol.
288287546eaSitojun 			 */
289b79da24aSitojun 			nd6log((LOG_DEBUG,
290287546eaSitojun 			    "nd6_options: unsupported option %d - "
291b79da24aSitojun 			    "option ignored\n", nd_opt->nd_opt_type));
292287546eaSitojun 		}
293287546eaSitojun 
294287546eaSitojun skip1:
295287546eaSitojun 		i++;
296287546eaSitojun 		if (i > nd6_maxndopt) {
297287546eaSitojun 			icmp6stat.icp6s_nd_toomanyopt++;
298b79da24aSitojun 			nd6log((LOG_INFO, "too many loop in nd opt\n"));
299287546eaSitojun 			break;
300287546eaSitojun 		}
301287546eaSitojun 
302287546eaSitojun 		if (ndopts->nd_opts_done)
303287546eaSitojun 			break;
304287546eaSitojun 	}
305287546eaSitojun 
306287546eaSitojun 	return 0;
307287546eaSitojun }
308287546eaSitojun 
309287546eaSitojun /*
3109631a17bSitojun  * ND6 timer routine to handle ND6 entries
311287546eaSitojun  */
312287546eaSitojun void
3139631a17bSitojun nd6_llinfo_settimer(struct llinfo_nd6 *ln, long tick)
3149631a17bSitojun {
3159631a17bSitojun 	int s;
3169631a17bSitojun 
3179631a17bSitojun 	s = splsoftnet();
3189631a17bSitojun 
3199631a17bSitojun 	if (tick < 0) {
3209631a17bSitojun 		ln->ln_expire = 0;
3219631a17bSitojun 		ln->ln_ntick = 0;
3229631a17bSitojun 		timeout_del(&ln->ln_timer_ch);
3239631a17bSitojun 	} else {
3243212dc31Stholo 		ln->ln_expire = time_second + tick / hz;
3259631a17bSitojun 		if (tick > INT_MAX) {
3269631a17bSitojun 			ln->ln_ntick = tick - INT_MAX;
3279631a17bSitojun 			timeout_add(&ln->ln_timer_ch, INT_MAX);
3289631a17bSitojun 		} else {
3299631a17bSitojun 			ln->ln_ntick = 0;
3309631a17bSitojun 			timeout_add(&ln->ln_timer_ch, tick);
3319631a17bSitojun 		}
3329631a17bSitojun 	}
3339631a17bSitojun 
3349631a17bSitojun 	splx(s);
3359631a17bSitojun }
3369631a17bSitojun 
337a0aa363cSjsing void
3389631a17bSitojun nd6_llinfo_timer(void *arg)
339287546eaSitojun {
340287546eaSitojun 	int s;
341b3c1e4c1Sitojun 	struct llinfo_nd6 *ln;
342287546eaSitojun 	struct rtentry *rt;
343287546eaSitojun 	struct sockaddr_in6 *dst;
3449631a17bSitojun 	struct ifnet *ifp;
345d374aaacSitojun 	struct nd_ifinfo *ndi = NULL;
346287546eaSitojun 
3479631a17bSitojun 	s = splsoftnet();
3489631a17bSitojun 
3499631a17bSitojun 	ln = (struct llinfo_nd6 *)arg;
3509631a17bSitojun 
3519631a17bSitojun 	if (ln->ln_ntick > 0) {
3529631a17bSitojun 		if (ln->ln_ntick > INT_MAX) {
3539631a17bSitojun 			ln->ln_ntick -= INT_MAX;
3549631a17bSitojun 			nd6_llinfo_settimer(ln, INT_MAX);
3559631a17bSitojun 		} else {
3569631a17bSitojun 			ln->ln_ntick = 0;
3579631a17bSitojun 			nd6_llinfo_settimer(ln, ln->ln_ntick);
358287546eaSitojun 		}
3599631a17bSitojun 		splx(s);
3609631a17bSitojun 		return;
361287546eaSitojun 	}
3629631a17bSitojun 
3639631a17bSitojun 	if ((rt = ln->ln_rt) == NULL)
3649631a17bSitojun 		panic("ln->ln_rt == NULL");
36547773228Sjsg 	if ((ifp = if_get(rt->rt_ifidx)) == NULL) {
36647773228Sjsg 		splx(s);
367f5e0f62bSmpi 		return;
36847773228Sjsg 	}
369d6b9e9b9Sitojun 	ndi = ND_IFINFO(ifp);
370c3c56496Sbluhm 	dst = satosin6(rt_key(rt));
371287546eaSitojun 
372287546eaSitojun 	/* sanity check */
373d374aaacSitojun 	if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln)
374bc84bce2Skrw 		panic("rt_llinfo(%p) is not equal to ln(%p)",
375d374aaacSitojun 		      rt->rt_llinfo, ln);
376287546eaSitojun 	if (!dst)
377bc84bce2Skrw 		panic("dst=0 in nd6_timer(ln=%p)", ln);
378287546eaSitojun 
379287546eaSitojun 	switch (ln->ln_state) {
380287546eaSitojun 	case ND6_LLINFO_INCOMPLETE:
381287546eaSitojun 		if (ln->ln_asked < nd6_mmaxtries) {
382287546eaSitojun 			ln->ln_asked++;
3839631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
3849631a17bSitojun 			nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
385287546eaSitojun 		} else {
386287546eaSitojun 			struct mbuf *m = ln->ln_hold;
387287546eaSitojun 			if (m) {
3888294a4dfSitojun 				ln->ln_hold = NULL;
389287546eaSitojun 				/*
390d8a7e3a7Sitojun 				 * Fake rcvif to make the ICMP error
391d8a7e3a7Sitojun 				 * more helpful in diagnosing for the
392d8a7e3a7Sitojun 				 * receiver.
393287546eaSitojun 				 * XXX: should we consider
394287546eaSitojun 				 * older rcvif?
395287546eaSitojun 				 */
39628112c65Smpi 				m->m_pkthdr.ph_ifidx = rt->rt_ifidx;
397d8a7e3a7Sitojun 
398287546eaSitojun 				icmp6_error(m, ICMP6_DST_UNREACH,
399287546eaSitojun 				    ICMP6_DST_UNREACH_ADDR, 0);
400e212adedSkrw 				if (ln->ln_hold == m) {
401e212adedSkrw 					/* m is back in ln_hold. Discard. */
402e212adedSkrw 					m_freem(ln->ln_hold);
403e212adedSkrw 					ln->ln_hold = NULL;
404e212adedSkrw 				}
405287546eaSitojun 			}
4069631a17bSitojun 			(void)nd6_free(rt, 0);
4079631a17bSitojun 			ln = NULL;
408287546eaSitojun 		}
409287546eaSitojun 		break;
410287546eaSitojun 	case ND6_LLINFO_REACHABLE:
4119631a17bSitojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
412287546eaSitojun 			ln->ln_state = ND6_LLINFO_STALE;
4139631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
414be4e9e12Sitojun 		}
415287546eaSitojun 		break;
416be4e9e12Sitojun 
417be4e9e12Sitojun 	case ND6_LLINFO_STALE:
418f3fcf2f3Smcbride 	case ND6_LLINFO_PURGE:
419be4e9e12Sitojun 		/* Garbage Collection(RFC 2461 5.3) */
4209631a17bSitojun 		if (!ND6_LLINFO_PERMANENT(ln)) {
4219631a17bSitojun 			(void)nd6_free(rt, 1);
4229631a17bSitojun 			ln = NULL;
4239631a17bSitojun 		}
424be4e9e12Sitojun 		break;
425be4e9e12Sitojun 
426287546eaSitojun 	case ND6_LLINFO_DELAY:
427d374aaacSitojun 		if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) {
428d374aaacSitojun 			/* We need NUD */
429287546eaSitojun 			ln->ln_asked = 1;
430287546eaSitojun 			ln->ln_state = ND6_LLINFO_PROBE;
4319631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
432d374aaacSitojun 			nd6_ns_output(ifp, &dst->sin6_addr,
433d8a7e3a7Sitojun 			    &dst->sin6_addr, ln, 0);
434be4e9e12Sitojun 		} else {
435d374aaacSitojun 			ln->ln_state = ND6_LLINFO_STALE; /* XXX */
4369631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
437be4e9e12Sitojun 		}
438287546eaSitojun 		break;
439287546eaSitojun 	case ND6_LLINFO_PROBE:
440287546eaSitojun 		if (ln->ln_asked < nd6_umaxtries) {
441287546eaSitojun 			ln->ln_asked++;
4429631a17bSitojun 			nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000);
443287546eaSitojun 			nd6_ns_output(ifp, &dst->sin6_addr,
444287546eaSitojun 			    &dst->sin6_addr, ln, 0);
445d8a7e3a7Sitojun 		} else {
4469631a17bSitojun 			(void)nd6_free(rt, 0);
4479631a17bSitojun 			ln = NULL;
448d8a7e3a7Sitojun 		}
449287546eaSitojun 		break;
450287546eaSitojun 	}
4519631a17bSitojun 
452f5e0f62bSmpi 	if_put(ifp);
4539631a17bSitojun 	splx(s);
454287546eaSitojun }
455287546eaSitojun 
4569631a17bSitojun /*
4579631a17bSitojun  * ND6 timer routine to expire default route list and prefix list
4589631a17bSitojun  */
4599631a17bSitojun void
460e4195480Sdlg nd6_timer_work(void *null)
4619631a17bSitojun {
4629631a17bSitojun 	int s;
463568012e6Sbluhm 	struct nd_defrouter *dr, *ndr;
464119a433cSbluhm 	struct nd_prefix *pr, *npr;
4659631a17bSitojun 	struct in6_ifaddr *ia6, *nia6;
4669631a17bSitojun 
4679631a17bSitojun 	s = splsoftnet();
4689631a17bSitojun 	timeout_set(&nd6_timer_ch, nd6_timer, NULL);
46929e86e5eSblambert 	timeout_add_sec(&nd6_timer_ch, nd6_prune);
4709631a17bSitojun 
4710a2c5741Sitojun 	/* expire default router list */
472568012e6Sbluhm 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr)
473568012e6Sbluhm 		if (dr->expire && dr->expire < time_second)
474287546eaSitojun 			defrtrlist_del(dr);
475287546eaSitojun 
476287546eaSitojun 	/*
477d8a7e3a7Sitojun 	 * expire interface addresses.
478d8a7e3a7Sitojun 	 * in the past the loop was inside prefix expiry processing.
4798b542bbeSpascoe 	 * However, from a stricter spec-conformance standpoint, we should
480d8a7e3a7Sitojun 	 * rather separate address lifetimes and prefix lifetimes.
481d8a7e3a7Sitojun 	 */
482a0fa8079Smpi 	TAILQ_FOREACH_SAFE(ia6, &in6_ifaddr, ia_list, nia6) {
483d8a7e3a7Sitojun 		/* check address lifetime */
484d8a7e3a7Sitojun 		if (IFA6_IS_INVALID(ia6)) {
485d8a7e3a7Sitojun 			in6_purgeaddr(&ia6->ia_ifa);
4860a8b9475Smarkus 		} else if (IFA6_IS_DEPRECATED(ia6)) {
487d8a7e3a7Sitojun 			ia6->ia6_flags |= IN6_IFF_DEPRECATED;
488d8a7e3a7Sitojun 		} else {
489d8a7e3a7Sitojun 			/*
490d8a7e3a7Sitojun 			 * A new RA might have made a deprecated address
491d8a7e3a7Sitojun 			 * preferred.
492d8a7e3a7Sitojun 			 */
493d8a7e3a7Sitojun 			ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
494d8a7e3a7Sitojun 		}
495d8a7e3a7Sitojun 	}
496d8a7e3a7Sitojun 
497d8a7e3a7Sitojun 	/* expire prefix list */
498119a433cSbluhm 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
499d8a7e3a7Sitojun 		/*
500287546eaSitojun 		 * check prefix lifetime.
501287546eaSitojun 		 * since pltime is just for autoconf, pltime processing for
502287546eaSitojun 		 * prefix is not necessary.
503287546eaSitojun 		 */
504d8a7e3a7Sitojun 		if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME &&
5053212dc31Stholo 		    time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) {
506287546eaSitojun 			/*
507287546eaSitojun 			 * address expiration and prefix expiration are
508d8a7e3a7Sitojun 			 * separate.  NEVER perform in6_purgeaddr here.
509287546eaSitojun 			 */
510287546eaSitojun 
511287546eaSitojun 			prelist_remove(pr);
512119a433cSbluhm 		}
513287546eaSitojun 	}
514287546eaSitojun 	splx(s);
515287546eaSitojun }
516287546eaSitojun 
517a09574ebSkettenis void
518a09574ebSkettenis nd6_timer(void *ignored_arg)
519a09574ebSkettenis {
520a09574ebSkettenis 	task_add(systq, &nd6_timer_task);
521a09574ebSkettenis }
522a09574ebSkettenis 
52322770369Sitojun /*
52422770369Sitojun  * Nuke neighbor cache/prefix/default router management table, right before
52522770369Sitojun  * ifp goes away.
52622770369Sitojun  */
52722770369Sitojun void
528ee37ea65Smcbride nd6_purge(struct ifnet *ifp)
52922770369Sitojun {
53022770369Sitojun 	struct llinfo_nd6 *ln, *nln;
531d8a7e3a7Sitojun 	struct nd_defrouter *dr, *ndr;
53222770369Sitojun 	struct nd_prefix *pr, *npr;
53322770369Sitojun 
53422770369Sitojun 	/*
535d8a7e3a7Sitojun 	 * Nuke default router list entries toward ifp.
536d8a7e3a7Sitojun 	 * We defer removal of default router list entries that is installed
537d8a7e3a7Sitojun 	 * in the routing table, in order to keep additional side effects as
538d8a7e3a7Sitojun 	 * small as possible.
53922770369Sitojun 	 */
540568012e6Sbluhm 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
541d8a7e3a7Sitojun 		if (dr->installed)
542d8a7e3a7Sitojun 			continue;
543d8a7e3a7Sitojun 
54422770369Sitojun 		if (dr->ifp == ifp)
54522770369Sitojun 			defrtrlist_del(dr);
54622770369Sitojun 	}
547568012e6Sbluhm 	TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr) {
548d8a7e3a7Sitojun 		if (!dr->installed)
549d8a7e3a7Sitojun 			continue;
550d8a7e3a7Sitojun 
55122770369Sitojun 		if (dr->ifp == ifp)
55222770369Sitojun 			defrtrlist_del(dr);
55322770369Sitojun 	}
55422770369Sitojun 
55522770369Sitojun 	/* Nuke prefix list entries toward ifp */
556119a433cSbluhm 	LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
557e520a0ecSmpi 		if (pr->ndpr_ifp == ifp)
55822770369Sitojun 			prelist_remove(pr);
55922770369Sitojun 	}
56022770369Sitojun 
561d3b1c214Sflorian 	if (ifp->if_xflags & IFXF_AUTOCONF6) {
56222770369Sitojun 		/* refresh default router list */
563f4f4d166Sitojun 		defrouter_select();
564e6e438a4Sitojun 	}
56522770369Sitojun 
56622770369Sitojun 	/*
56722770369Sitojun 	 * Nuke neighbor cache entries for the ifp.
56822770369Sitojun 	 */
56922770369Sitojun 	ln = llinfo_nd6.ln_next;
57022770369Sitojun 	while (ln && ln != &llinfo_nd6) {
57122770369Sitojun 		struct rtentry *rt;
57222770369Sitojun 		struct sockaddr_dl *sdl;
57322770369Sitojun 
57422770369Sitojun 		nln = ln->ln_next;
57522770369Sitojun 		rt = ln->ln_rt;
57622770369Sitojun 		if (rt && rt->rt_gateway &&
57722770369Sitojun 		    rt->rt_gateway->sa_family == AF_LINK) {
578c7b7b779Sbluhm 			sdl = satosdl(rt->rt_gateway);
57922770369Sitojun 			if (sdl->sdl_index == ifp->if_index)
580d8a7e3a7Sitojun 				nln = nd6_free(rt, 0);
58122770369Sitojun 		}
58222770369Sitojun 		ln = nln;
58322770369Sitojun 	}
58422770369Sitojun }
58522770369Sitojun 
586287546eaSitojun struct rtentry *
587f4d1af37Smikeb nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp,
588f4d1af37Smikeb     u_int rtableid)
589287546eaSitojun {
590287546eaSitojun 	struct rtentry *rt;
591287546eaSitojun 	struct sockaddr_in6 sin6;
5925148b194Smpi 	int flags;
593287546eaSitojun 
594287546eaSitojun 	bzero(&sin6, sizeof(sin6));
595287546eaSitojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
596287546eaSitojun 	sin6.sin6_family = AF_INET6;
597287546eaSitojun 	sin6.sin6_addr = *addr6;
5985148b194Smpi 	flags = (create) ? (RT_REPORT|RT_RESOLVE) : 0;
599d8a7e3a7Sitojun 
6005148b194Smpi 	rt = rtalloc(sin6tosa(&sin6), flags, rtableid);
60116119bbfSmpi 	if (rt != NULL && (rt->rt_flags & RTF_LLINFO) == 0) {
602287546eaSitojun 		/*
603287546eaSitojun 		 * This is the case for the default route.
604287546eaSitojun 		 * If we want to create a neighbor cache for the address, we
605287546eaSitojun 		 * should free the route for the destination and allocate an
606287546eaSitojun 		 * interface route.
607287546eaSitojun 		 */
608287546eaSitojun 		if (create) {
60927ae666cSmpi 			rtfree(rt);
61027ae666cSmpi 			rt = NULL;
611287546eaSitojun 		}
612287546eaSitojun 	}
61316119bbfSmpi 	if (rt == NULL) {
614287546eaSitojun 		if (create && ifp) {
615cb24f5e5Sclaudio 			struct rt_addrinfo info;
6160ffd01d4Sbluhm 			int error;
617d374aaacSitojun 
618287546eaSitojun 			/*
619287546eaSitojun 			 * If no route is available and create is set,
620287546eaSitojun 			 * we allocate a host route for the destination
621287546eaSitojun 			 * and treat it like an interface route.
622287546eaSitojun 			 * This hack is necessary for a neighbor which can't
623287546eaSitojun 			 * be covered by our own prefix.
624287546eaSitojun 			 */
625287546eaSitojun 			struct ifaddr *ifa =
626c3c56496Sbluhm 			    ifaof_ifpforaddr(sin6tosa(&sin6), ifp);
627287546eaSitojun 			if (ifa == NULL)
628287546eaSitojun 				return (NULL);
629287546eaSitojun 
630287546eaSitojun 			/*
631287546eaSitojun 			 * Create a new route.  RTF_LLINFO is necessary
632287546eaSitojun 			 * to create a Neighbor Cache entry for the
633287546eaSitojun 			 * destination in nd6_rtrequest which will be
634*7ffb277fSbluhm 			 * called in rtrequest.
635287546eaSitojun 			 */
636cb24f5e5Sclaudio 			bzero(&info, sizeof(info));
637c29fc46aSmpi 			info.rti_flags = RTF_HOST | RTF_LLINFO;
638c3c56496Sbluhm 			info.rti_info[RTAX_DST] = sin6tosa(&sin6);
639c7b7b779Sbluhm 			info.rti_info[RTAX_GATEWAY] = sdltosa(ifp->if_sadl);
640*7ffb277fSbluhm 			error = rtrequest(RTM_ADD, &info, RTP_CONNECTED, &rt,
6410ffd01d4Sbluhm 			    rtableid);
6420ffd01d4Sbluhm 			if (error)
643287546eaSitojun 				return (NULL);
644287546eaSitojun 			if (rt->rt_llinfo) {
645287546eaSitojun 				struct llinfo_nd6 *ln =
646287546eaSitojun 				    (struct llinfo_nd6 *)rt->rt_llinfo;
647287546eaSitojun 				ln->ln_state = ND6_LLINFO_NOSTATE;
648287546eaSitojun 			}
649f6e55599Sitojun 		} else
650287546eaSitojun 			return (NULL);
651287546eaSitojun 	}
652287546eaSitojun 	/*
653287546eaSitojun 	 * Validation for the entry.
654d8a7e3a7Sitojun 	 * Note that the check for rt_llinfo is necessary because a cloned
655d8a7e3a7Sitojun 	 * route from a parent route that has the L flag (e.g. the default
656d8a7e3a7Sitojun 	 * route to a p2p interface) may have the flag, too, while the
657d8a7e3a7Sitojun 	 * destination is not actually a neighbor.
658287546eaSitojun 	 */
659287546eaSitojun 	if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 ||
660d8a7e3a7Sitojun 	    rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL ||
66109743a3aSmpi 	    (ifp != NULL && rt->rt_ifp != ifp)) {
662287546eaSitojun 		if (create) {
663bbcf0337Smpi 			char addr[INET6_ADDRSTRLEN];
66435075f95Smpi 			nd6log((LOG_DEBUG, "%s: failed to lookup %s (if=%s)\n",
66535075f95Smpi 			    __func__,
666bbcf0337Smpi 			    inet_ntop(AF_INET6, addr6, addr, sizeof(addr)),
667da592434Sitojun 			    ifp ? ifp->if_xname : "unspec"));
668287546eaSitojun 		}
66916119bbfSmpi 		rtfree(rt);
670d8a7e3a7Sitojun 		return (NULL);
671287546eaSitojun 	}
672287546eaSitojun 	return (rt);
673287546eaSitojun }
674287546eaSitojun 
675287546eaSitojun /*
676287546eaSitojun  * Detect if a given IPv6 address identifies a neighbor on a given link.
677287546eaSitojun  * XXX: should take care of the destination of a p2p link?
678287546eaSitojun  */
679287546eaSitojun int
680ee37ea65Smcbride nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp)
681287546eaSitojun {
682d8a7e3a7Sitojun 	struct nd_prefix *pr;
683ef6620bcSmpi 	struct in6_ifaddr *ia6;
684ef6620bcSmpi 	struct ifaddr *ifa;
685d8a7e3a7Sitojun 	struct rtentry *rt;
686287546eaSitojun 
687cfb6b8dfSitojun 	/*
688cfb6b8dfSitojun 	 * A link-local address is always a neighbor.
689cfb6b8dfSitojun 	 * XXX: we should use the sin6_scope_id field rather than the embedded
690cfb6b8dfSitojun 	 * interface index.
691d8a7e3a7Sitojun 	 * XXX: a link does not necessarily specify a single interface.
692cfb6b8dfSitojun 	 */
693cfb6b8dfSitojun 	if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) &&
694cfb6b8dfSitojun 	    ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index)
695287546eaSitojun 		return (1);
696287546eaSitojun 
697ef6620bcSmpi 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
698ef6620bcSmpi 		if (ifa->ifa_addr->sa_family != AF_INET6)
699ef6620bcSmpi 			continue;
700ef6620bcSmpi 
701ef6620bcSmpi 		ia6 = ifatoia6(ifa);
702ef6620bcSmpi 
703ef6620bcSmpi 		/* Prefix check down below. */
704ef6620bcSmpi 		if (ia6->ia6_flags & IN6_IFF_AUTOCONF)
705ef6620bcSmpi 			continue;
706ef6620bcSmpi 
707ef6620bcSmpi 		if (IN6_ARE_MASKED_ADDR_EQUAL(&addr->sin6_addr,
708ef6620bcSmpi 		    &ia6->ia_addr.sin6_addr,
709ef6620bcSmpi 		    &ia6->ia_prefixmask.sin6_addr))
710ef6620bcSmpi 			return (1);
711ef6620bcSmpi 	}
712ef6620bcSmpi 
713287546eaSitojun 	/*
714d8a7e3a7Sitojun 	 * If the address matches one of our on-link prefixes, it should be a
715d8a7e3a7Sitojun 	 * neighbor.
716287546eaSitojun 	 */
717545205eaSpyr 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
718d8a7e3a7Sitojun 		if (pr->ndpr_ifp != ifp)
719d8a7e3a7Sitojun 			continue;
720287546eaSitojun 
721d8a7e3a7Sitojun 		if (!(pr->ndpr_stateflags & NDPRF_ONLINK))
722d8a7e3a7Sitojun 			continue;
723d8a7e3a7Sitojun 
724d8a7e3a7Sitojun 		if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
725d8a7e3a7Sitojun 		    &addr->sin6_addr, &pr->ndpr_mask))
726d8a7e3a7Sitojun 			return (1);
727287546eaSitojun 	}
728d8a7e3a7Sitojun 
729d8a7e3a7Sitojun 	/*
730287546eaSitojun 	 * Even if the address matches none of our addresses, it might be
731287546eaSitojun 	 * in the neighbor cache.
732287546eaSitojun 	 */
73316119bbfSmpi 	rt = nd6_lookup(&addr->sin6_addr, 0, ifp, ifp->if_rdomain);
73416119bbfSmpi 	if (rt != NULL) {
73516119bbfSmpi 		rtfree(rt);
736287546eaSitojun 		return (1);
73716119bbfSmpi 	}
738287546eaSitojun 
739287546eaSitojun 	return (0);
740287546eaSitojun }
741287546eaSitojun 
742287546eaSitojun /*
743287546eaSitojun  * Free an nd6 llinfo entry.
744d8a7e3a7Sitojun  * Since the function would cause significant changes in the kernel, DO NOT
745d8a7e3a7Sitojun  * make it global, unless you have a strong reason for the change, and are sure
746d8a7e3a7Sitojun  * that the change is safe.
747287546eaSitojun  */
748a0aa363cSjsing struct llinfo_nd6 *
749ee37ea65Smcbride nd6_free(struct rtentry *rt, int gc)
750287546eaSitojun {
75129760ae1Sitojun 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next;
752c3c56496Sbluhm 	struct in6_addr in6 = satosin6(rt_key(rt))->sin6_addr;
753287546eaSitojun 	struct nd_defrouter *dr;
75481d38878Smpi 	int s;
755287546eaSitojun 
756287546eaSitojun 	/*
757d8a7e3a7Sitojun 	 * we used to have pfctlinput(PRC_HOSTDEAD) here.
758d8a7e3a7Sitojun 	 * even though it is not harmful, it was not really necessary.
759287546eaSitojun 	 */
760f4f4d166Sitojun 
7616e92dee6Sitojun 	s = splsoftnet();
76281d38878Smpi 	if (!ip6_forwarding) {
763c3c56496Sbluhm 		dr = defrouter_lookup(&satosin6(rt_key(rt))->sin6_addr,
764f4f4d166Sitojun 		    rt->rt_ifp);
765d8a7e3a7Sitojun 
766d8a7e3a7Sitojun 		if (dr != NULL && dr->expire &&
767d8a7e3a7Sitojun 		    ln->ln_state == ND6_LLINFO_STALE && gc) {
768d8a7e3a7Sitojun 			/*
769d8a7e3a7Sitojun 			 * If the reason for the deletion is just garbage
770d8a7e3a7Sitojun 			 * collection, and the neighbor is an active default
771d8a7e3a7Sitojun 			 * router, do not delete it.  Instead, reset the GC
772d8a7e3a7Sitojun 			 * timer using the router's lifetime.
773d8a7e3a7Sitojun 			 * Simply deleting the entry would affect default
774d8a7e3a7Sitojun 			 * router selection, which is not necessarily a good
775d8a7e3a7Sitojun 			 * thing, especially when we're using router preference
776d8a7e3a7Sitojun 			 * values.
777d8a7e3a7Sitojun 			 * XXX: the check for ln_state would be redundant,
778d8a7e3a7Sitojun 			 *      but we intentionally keep it just in case.
779d8a7e3a7Sitojun 			 */
7803212dc31Stholo 			if (dr->expire > time_second * hz) {
7819631a17bSitojun 				nd6_llinfo_settimer(ln,
7823212dc31Stholo 				    dr->expire - time_second * hz);
7839631a17bSitojun 			} else
7849631a17bSitojun 				nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
785d8a7e3a7Sitojun 			splx(s);
786d8a7e3a7Sitojun 			return (ln->ln_next);
787d8a7e3a7Sitojun 		}
788d8a7e3a7Sitojun 
789f4f4d166Sitojun 		if (ln->ln_router || dr) {
790f4f4d166Sitojun 			/*
791f4f4d166Sitojun 			 * rt6_flush must be called whether or not the neighbor
792f4f4d166Sitojun 			 * is in the Default Router List.
793f4f4d166Sitojun 			 * See a corresponding comment in nd6_na_input().
794f4f4d166Sitojun 			 */
795f4f4d166Sitojun 			rt6_flush(&in6, rt->rt_ifp);
796f4f4d166Sitojun 		}
797f4f4d166Sitojun 
798f4f4d166Sitojun 		if (dr) {
799f4f4d166Sitojun 			/*
8008b542bbeSpascoe 			 * Unreachability of a router might affect the default
801f4f4d166Sitojun 			 * router selection and on-link detection of advertised
802f4f4d166Sitojun 			 * prefixes.
803f4f4d166Sitojun 			 */
804f4f4d166Sitojun 
805f4f4d166Sitojun 			/*
806f4f4d166Sitojun 			 * Temporarily fake the state to choose a new default
807f4f4d166Sitojun 			 * router and to perform on-link determination of
80834deef1eSitojun 			 * prefixes correctly.
809f4f4d166Sitojun 			 * Below the state will be set correctly,
810f4f4d166Sitojun 			 * or the entry itself will be deleted.
811f4f4d166Sitojun 			 */
812f4f4d166Sitojun 			ln->ln_state = ND6_LLINFO_INCOMPLETE;
813f4f4d166Sitojun 
814f4f4d166Sitojun 			/*
815d8a7e3a7Sitojun 			 * Since defrouter_select() does not affect the
816d8a7e3a7Sitojun 			 * on-link determination and MIP6 needs the check
817d8a7e3a7Sitojun 			 * before the default router selection, we perform
818d8a7e3a7Sitojun 			 * the check now.
819f4f4d166Sitojun 			 */
820f4f4d166Sitojun 			pfxlist_onlink_check();
821d8a7e3a7Sitojun 
822d8a7e3a7Sitojun 			/*
823d8a7e3a7Sitojun 			 * refresh default router list
824d8a7e3a7Sitojun 			 */
825d8a7e3a7Sitojun 			defrouter_select();
826287546eaSitojun 		}
827287546eaSitojun 	}
828287546eaSitojun 
82929760ae1Sitojun 	/*
83029760ae1Sitojun 	 * Before deleting the entry, remember the next entry as the
83129760ae1Sitojun 	 * return value.  We need this because pfxlist_onlink_check() above
83229760ae1Sitojun 	 * might have freed other entries (particularly the old next entry) as
83329760ae1Sitojun 	 * a side effect (XXX).
83429760ae1Sitojun 	 */
83529760ae1Sitojun 	next = ln->ln_next;
83629760ae1Sitojun 
83729760ae1Sitojun 	/*
83829760ae1Sitojun 	 * Detach the route from the routing tree and the list of neighbor
83929760ae1Sitojun 	 * caches, and disable the route entry not to be used in already
84029760ae1Sitojun 	 * cached routes.
84129760ae1Sitojun 	 */
8427aa9499aSmpi 	rtdeletemsg(rt, rt->rt_ifp->if_rdomain);
84381d38878Smpi 	splx(s);
84429760ae1Sitojun 
8450a2c5741Sitojun 	return (next);
846287546eaSitojun }
847287546eaSitojun 
848287546eaSitojun /*
849287546eaSitojun  * Upper-layer reachability hint for Neighbor Unreachability Detection.
850287546eaSitojun  *
8518b542bbeSpascoe  * XXX cost-effective methods?
852287546eaSitojun  */
853287546eaSitojun void
85460f4930eSmpi nd6_nud_hint(struct rtentry *rt, u_int rtableid)
855287546eaSitojun {
856287546eaSitojun 	struct llinfo_nd6 *ln;
857287546eaSitojun 
85860f4930eSmpi 	if (rt == NULL) {
859287546eaSitojun 		return;
860287546eaSitojun 	}
861287546eaSitojun 
862f6e55599Sitojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0 ||
863f6e55599Sitojun 	    (rt->rt_flags & RTF_LLINFO) == 0 ||
864f6e55599Sitojun 	    !rt->rt_llinfo || !rt->rt_gateway ||
865f6e55599Sitojun 	    rt->rt_gateway->sa_family != AF_LINK) {
866287546eaSitojun 		/* This is not a host route. */
867287546eaSitojun 		return;
868287546eaSitojun 	}
869287546eaSitojun 
870287546eaSitojun 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
871804d8827Sitojun 	if (ln->ln_state < ND6_LLINFO_REACHABLE)
872287546eaSitojun 		return;
873287546eaSitojun 
874f6e55599Sitojun 	/*
875f6e55599Sitojun 	 * if we get upper-layer reachability confirmation many times,
876f6e55599Sitojun 	 * it is possible we have false information.
877f6e55599Sitojun 	 */
878f6e55599Sitojun 	ln->ln_byhint++;
879f6e55599Sitojun 	if (ln->ln_byhint > nd6_maxnudhint)
880f6e55599Sitojun 		return;
881f6e55599Sitojun 
882287546eaSitojun 	ln->ln_state = ND6_LLINFO_REACHABLE;
8839631a17bSitojun 	if (!ND6_LLINFO_PERMANENT(ln)) {
8849631a17bSitojun 		nd6_llinfo_settimer(ln,
8859631a17bSitojun 		    (long)ND_IFINFO(rt->rt_ifp)->reachable * hz);
8869631a17bSitojun 	}
887287546eaSitojun }
888287546eaSitojun 
889287546eaSitojun void
890dcb17c31Smpi nd6_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt)
891287546eaSitojun {
892287546eaSitojun 	struct sockaddr *gate = rt->rt_gateway;
893287546eaSitojun 	struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo;
894287546eaSitojun 	struct ifaddr *ifa;
89570fc31beSrainer 	struct nd_defrouter *dr;
89670fc31beSrainer 
89770fc31beSrainer 	if (req == RTM_DELETE && (rt->rt_flags & RTF_GATEWAY) &&
89870fc31beSrainer 	    (IN6_ARE_ADDR_EQUAL(&(satosin6(rt_key(rt)))->sin6_addr,
89970fc31beSrainer 	    &in6addr_any) && rt_mask(rt) && (rt_mask(rt)->sa_len == 0 ||
90070fc31beSrainer 	    IN6_ARE_ADDR_EQUAL(&(satosin6(rt_mask(rt)))->sin6_addr,
90170fc31beSrainer 	    &in6addr_any)))) {
902c3c56496Sbluhm 		dr = defrouter_lookup(&satosin6(gate)->sin6_addr, ifp);
90370fc31beSrainer 		if (dr)
90470fc31beSrainer 			dr->installed = 0;
90570fc31beSrainer 	}
906287546eaSitojun 
907d8a7e3a7Sitojun 	if ((rt->rt_flags & RTF_GATEWAY) != 0)
908287546eaSitojun 		return;
909287546eaSitojun 
910d8a7e3a7Sitojun 	if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
911d8a7e3a7Sitojun 		/*
912d8a7e3a7Sitojun 		 * This is probably an interface direct route for a link
913d8a7e3a7Sitojun 		 * which does not need neighbor caches (e.g. fe80::%lo0/64).
914d8a7e3a7Sitojun 		 * We do not need special treatment below for such a route.
915d8a7e3a7Sitojun 		 * Moreover, the RTF_LLINFO flag which would be set below
916d8a7e3a7Sitojun 		 * would annoy the ndp(8) command.
917d8a7e3a7Sitojun 		 */
918d8a7e3a7Sitojun 		return;
919d8a7e3a7Sitojun 	}
920d8a7e3a7Sitojun 
921af1344beSbluhm 	if (req == RTM_RESOLVE && nd6_need_cache(ifp) == 0) {
922d8a7e3a7Sitojun 		/*
923af1344beSbluhm 		 * For routing daemons like ospf6d we allow neighbor discovery
924af1344beSbluhm 		 * based on the cloning route only.  This allows us to sent
925af1344beSbluhm 		 * packets directly into a network without having an address
926af1344beSbluhm 		 * with matching prefix on the interface.  If the cloning
927af1344beSbluhm 		 * route is used for an stf interface, we would mistakenly
928af1344beSbluhm 		 * make a neighbor cache for the host route, and would see
929af1344beSbluhm 		 * strange neighbor solicitation for the corresponding
930af1344beSbluhm 		 * destination.  In order to avoid confusion, we check if the
931af1344beSbluhm 		 * interface is suitable for neighbor discovery, and stop the
932d8a7e3a7Sitojun 		 * process if not.  Additionally, we remove the LLINFO flag
933d8a7e3a7Sitojun 		 * so that ndp(8) will not try to get the neighbor information
934d8a7e3a7Sitojun 		 * of the destination.
935d8a7e3a7Sitojun 		 */
936d8a7e3a7Sitojun 		rt->rt_flags &= ~RTF_LLINFO;
937d8a7e3a7Sitojun 		return;
938d8a7e3a7Sitojun 	}
939d8a7e3a7Sitojun 
940287546eaSitojun 	switch (req) {
941287546eaSitojun 	case RTM_ADD:
942287546eaSitojun 		/*
943287546eaSitojun 		 * There is no backward compatibility :)
944287546eaSitojun 		 *
945287546eaSitojun 		 * if ((rt->rt_flags & RTF_HOST) == 0 &&
946287546eaSitojun 		 *     SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
947287546eaSitojun 		 *	   rt->rt_flags |= RTF_CLONING;
948287546eaSitojun 		 */
94948ebf8e1Sitojun 		if ((rt->rt_flags & RTF_CLONING) ||
9505f3e87fcSmpi 		    ((rt->rt_flags & (RTF_LLINFO | RTF_LOCAL)) && !ln)) {
951287546eaSitojun 			if (ln)
9529631a17bSitojun 				nd6_llinfo_settimer(ln, 0);
953d8a7e3a7Sitojun 			if ((rt->rt_flags & RTF_CLONING) != 0)
954287546eaSitojun 				break;
955287546eaSitojun 		}
956f4f4d166Sitojun 		/*
9578b542bbeSpascoe 		 * In IPv4 code, we try to announce new RTF_ANNOUNCE entry here.
958f4f4d166Sitojun 		 * We don't do that here since llinfo is not ready yet.
959f4f4d166Sitojun 		 *
960f4f4d166Sitojun 		 * There are also couple of other things to be discussed:
961f4f4d166Sitojun 		 * - unsolicited NA code needs improvement beforehand
962f4f4d166Sitojun 		 * - RFC2461 says we MAY send multicast unsolicited NA
963f4f4d166Sitojun 		 *   (7.2.6 paragraph 4), however, it also says that we
964f4f4d166Sitojun 		 *   SHOULD provide a mechanism to prevent multicast NA storm.
965f4f4d166Sitojun 		 *   we don't have anything like it right now.
966841d7adbSitojun 		 *   note that the mechanism needs a mutual agreement
967f4f4d166Sitojun 		 *   between proxies, which means that we need to implement
968841d7adbSitojun 		 *   a new protocol, or a new kludge.
969841d7adbSitojun 		 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
970f4f4d166Sitojun 		 *   we need to check ip6forwarding before sending it.
971f4f4d166Sitojun 		 *   (or should we allow proxy ND configuration only for
972f4f4d166Sitojun 		 *   routers?  there's no mention about proxy ND from hosts)
973f4f4d166Sitojun 		 */
974f4f4d166Sitojun #if 0
975f4f4d166Sitojun 		/* XXX it does not work */
976287546eaSitojun 		if (rt->rt_flags & RTF_ANNOUNCE)
977287546eaSitojun 			nd6_na_output(ifp,
978c3c56496Sbluhm 			      &satosin6(rt_key(rt))->sin6_addr,
979c3c56496Sbluhm 			      &satosin6(rt_key(rt))->sin6_addr,
980287546eaSitojun 			      ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
981f4f4d166Sitojun 			      1, NULL);
982f4f4d166Sitojun #endif
983287546eaSitojun 		/* FALLTHROUGH */
984287546eaSitojun 	case RTM_RESOLVE:
985287546eaSitojun 		if (gate->sa_family != AF_LINK ||
986cfc71c8bSclaudio 		    gate->sa_len < sizeof(struct sockaddr_dl)) {
98786b61919Smpi 			log(LOG_DEBUG, "%s: bad gateway value: %s\n",
98886b61919Smpi 			    __func__, ifp->if_xname);
989287546eaSitojun 			break;
990287546eaSitojun 		}
991c7b7b779Sbluhm 		satosdl(gate)->sdl_type = ifp->if_type;
992c7b7b779Sbluhm 		satosdl(gate)->sdl_index = ifp->if_index;
993d374aaacSitojun 		if (ln != NULL)
994287546eaSitojun 			break;	/* This happens on a route change */
995287546eaSitojun 		/*
996287546eaSitojun 		 * Case 2: This route may come from cloning, or a manual route
997287546eaSitojun 		 * add with a LL address.
998287546eaSitojun 		 */
9997371a15dStedu 		ln = malloc(sizeof(*ln), M_RTABLE, M_NOWAIT | M_ZERO);
1000287546eaSitojun 		rt->rt_llinfo = (caddr_t)ln;
1001287546eaSitojun 		if (!ln) {
100286b61919Smpi 			log(LOG_DEBUG, "%s: malloc failed\n", __func__);
1003287546eaSitojun 			break;
1004287546eaSitojun 		}
1005287546eaSitojun 		nd6_inuse++;
1006287546eaSitojun 		nd6_allocated++;
1007287546eaSitojun 		ln->ln_rt = rt;
10089631a17bSitojun 		timeout_set(&ln->ln_timer_ch, nd6_llinfo_timer, ln);
1009287546eaSitojun 		/* this is required for "ndp" command. - shin */
1010287546eaSitojun 		if (req == RTM_ADD) {
1011287546eaSitojun 		        /*
1012287546eaSitojun 			 * gate should have some valid AF_LINK entry,
1013287546eaSitojun 			 * and ln->ln_expire should have some lifetime
1014287546eaSitojun 			 * which is specified by ndp command.
1015287546eaSitojun 			 */
1016287546eaSitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1017f6e55599Sitojun 			ln->ln_byhint = 0;
1018287546eaSitojun 		} else {
1019287546eaSitojun 		        /*
1020287546eaSitojun 			 * When req == RTM_RESOLVE, rt is created and
1021287546eaSitojun 			 * initialized in rtrequest(), so rt_expire is 0.
1022287546eaSitojun 			 */
1023287546eaSitojun 			ln->ln_state = ND6_LLINFO_NOSTATE;
10249631a17bSitojun 			nd6_llinfo_settimer(ln, 0);
1025287546eaSitojun 		}
1026287546eaSitojun 		rt->rt_flags |= RTF_LLINFO;
1027287546eaSitojun 		ln->ln_next = llinfo_nd6.ln_next;
1028287546eaSitojun 		llinfo_nd6.ln_next = ln;
1029287546eaSitojun 		ln->ln_prev = &llinfo_nd6;
1030287546eaSitojun 		ln->ln_next->ln_prev = ln;
1031287546eaSitojun 
1032287546eaSitojun 		/*
1033f3fcf2f3Smcbride 		 * If we have too many cache entries, initiate immediate
1034f3fcf2f3Smcbride 		 * purging for some "less recently used" entries.  Note that
1035f3fcf2f3Smcbride 		 * we cannot directly call nd6_free() here because it would
1036f3fcf2f3Smcbride 		 * cause re-entering rtable related routines triggering an LOR
1037f3fcf2f3Smcbride 		 * problem for FreeBSD.
1038f3fcf2f3Smcbride 		 */
1039f3fcf2f3Smcbride 		if (ip6_neighborgcthresh >= 0 &&
1040f3fcf2f3Smcbride 		    nd6_inuse >= ip6_neighborgcthresh) {
1041f3fcf2f3Smcbride 			int i;
1042f3fcf2f3Smcbride 
1043f3fcf2f3Smcbride 			for (i = 0; i < 10 && llinfo_nd6.ln_prev != ln; i++) {
1044f3fcf2f3Smcbride 				struct llinfo_nd6 *ln_end = llinfo_nd6.ln_prev;
1045f3fcf2f3Smcbride 
1046f3fcf2f3Smcbride 				/* Move this entry to the head */
1047f3fcf2f3Smcbride 				LN_DEQUEUE(ln_end);
1048f3fcf2f3Smcbride 				LN_INSERTHEAD(ln_end);
1049f3fcf2f3Smcbride 
1050f3fcf2f3Smcbride 				if (ND6_LLINFO_PERMANENT(ln_end))
1051f3fcf2f3Smcbride 					continue;
1052f3fcf2f3Smcbride 
1053f3fcf2f3Smcbride 				if (ln_end->ln_state > ND6_LLINFO_INCOMPLETE)
1054f3fcf2f3Smcbride 					ln_end->ln_state = ND6_LLINFO_STALE;
1055f3fcf2f3Smcbride 				else
1056f3fcf2f3Smcbride 					ln_end->ln_state = ND6_LLINFO_PURGE;
1057f3fcf2f3Smcbride 				nd6_llinfo_settimer(ln_end, 0);
1058f3fcf2f3Smcbride 			}
1059f3fcf2f3Smcbride 		}
1060f3fcf2f3Smcbride 
1061f3fcf2f3Smcbride 		/*
1062287546eaSitojun 		 * check if rt_key(rt) is one of my address assigned
1063287546eaSitojun 		 * to the interface.
1064287546eaSitojun 		 */
106586b61919Smpi 		ifa = &in6ifa_ifpwithaddr(ifp,
1066c3c56496Sbluhm 		    &satosin6(rt_key(rt))->sin6_addr)->ia_ifa;
1067287546eaSitojun 		if (ifa) {
10689631a17bSitojun 			nd6_llinfo_settimer(ln, -1);
1069287546eaSitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1070f6e55599Sitojun 			ln->ln_byhint = 0;
1071fac399ceSmpi 			KASSERT(ifa == rt->rt_ifa);
1072f4f4d166Sitojun 		} else if (rt->rt_flags & RTF_ANNOUNCE) {
10739631a17bSitojun 			nd6_llinfo_settimer(ln, -1);
1074f4f4d166Sitojun 			ln->ln_state = ND6_LLINFO_REACHABLE;
1075f6e55599Sitojun 			ln->ln_byhint = 0;
1076f4f4d166Sitojun 
1077f4f4d166Sitojun 			/* join solicited node multicast for proxy ND */
1078f4f4d166Sitojun 			if (ifp->if_flags & IFF_MULTICAST) {
1079f4f4d166Sitojun 				struct in6_addr llsol;
1080f4f4d166Sitojun 				int error;
1081f4f4d166Sitojun 
1082c3c56496Sbluhm 				llsol = satosin6(rt_key(rt))->sin6_addr;
1083f4f4d166Sitojun 				llsol.s6_addr16[0] = htons(0xff02);
1084f4f4d166Sitojun 				llsol.s6_addr16[1] = htons(ifp->if_index);
1085f4f4d166Sitojun 				llsol.s6_addr32[1] = 0;
1086f4f4d166Sitojun 				llsol.s6_addr32[2] = htonl(1);
1087f4f4d166Sitojun 				llsol.s6_addr8[12] = 0xff;
1088f4f4d166Sitojun 
1089d8a7e3a7Sitojun 				if (in6_addmulti(&llsol, ifp, &error)) {
1090bbcf0337Smpi 					char addr[INET6_ADDRSTRLEN];
1091d8a7e3a7Sitojun 					nd6log((LOG_ERR, "%s: failed to join "
1092d8a7e3a7Sitojun 					    "%s (errno=%d)\n", ifp->if_xname,
1093bbcf0337Smpi 					    inet_ntop(AF_INET6, &llsol,
1094bbcf0337Smpi 						addr, sizeof(addr)),
1095bbcf0337Smpi 					    error));
1096d8a7e3a7Sitojun 				}
1097f4f4d166Sitojun 			}
1098287546eaSitojun 		}
1099287546eaSitojun 		break;
1100287546eaSitojun 
1101287546eaSitojun 	case RTM_DELETE:
1102287546eaSitojun 		if (!ln)
1103287546eaSitojun 			break;
1104f4f4d166Sitojun 		/* leave from solicited node multicast for proxy ND */
1105f4f4d166Sitojun 		if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1106f4f4d166Sitojun 		    (ifp->if_flags & IFF_MULTICAST) != 0) {
1107f4f4d166Sitojun 			struct in6_addr llsol;
1108f4f4d166Sitojun 			struct in6_multi *in6m;
1109f4f4d166Sitojun 
1110c3c56496Sbluhm 			llsol = satosin6(rt_key(rt))->sin6_addr;
1111f4f4d166Sitojun 			llsol.s6_addr16[0] = htons(0xff02);
1112f4f4d166Sitojun 			llsol.s6_addr16[1] = htons(ifp->if_index);
1113f4f4d166Sitojun 			llsol.s6_addr32[1] = 0;
1114f4f4d166Sitojun 			llsol.s6_addr32[2] = htonl(1);
1115f4f4d166Sitojun 			llsol.s6_addr8[12] = 0xff;
1116f4f4d166Sitojun 
1117f4f4d166Sitojun 			IN6_LOOKUP_MULTI(llsol, ifp, in6m);
1118f4f4d166Sitojun 			if (in6m)
1119f4f4d166Sitojun 				in6_delmulti(in6m);
1120f4f4d166Sitojun 		}
1121287546eaSitojun 		nd6_inuse--;
1122287546eaSitojun 		ln->ln_next->ln_prev = ln->ln_prev;
1123287546eaSitojun 		ln->ln_prev->ln_next = ln->ln_next;
1124287546eaSitojun 		ln->ln_prev = NULL;
11259631a17bSitojun 		nd6_llinfo_settimer(ln, -1);
1126287546eaSitojun 		rt->rt_llinfo = 0;
1127287546eaSitojun 		rt->rt_flags &= ~RTF_LLINFO;
1128287546eaSitojun 		m_freem(ln->ln_hold);
1129dd168dc2Stedu 		free(ln, M_RTABLE, 0);
1130287546eaSitojun 	}
1131287546eaSitojun }
1132287546eaSitojun 
1133287546eaSitojun int
1134ee37ea65Smcbride nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1135287546eaSitojun {
1136287546eaSitojun 	struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1137287546eaSitojun 	struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1138287546eaSitojun 	struct rtentry *rt;
1139ee3c93eeSbluhm 	int error = 0;
1140287546eaSitojun 	int s;
1141287546eaSitojun 
1142287546eaSitojun 	switch (cmd) {
1143d6b9e9b9Sitojun 	case SIOCGIFINFO_IN6:
1144d6b9e9b9Sitojun 		ndi->ndi = *ND_IFINFO(ifp);
114502dac871Sderaadt 		memset(&ndi->ndi.randomseed0, 0, sizeof ndi->ndi.randomseed0);
114602dac871Sderaadt 		memset(&ndi->ndi.randomseed1, 0, sizeof ndi->ndi.randomseed1);
114702dac871Sderaadt 		memset(&ndi->ndi.randomid, 0, sizeof ndi->ndi.randomid);
1148287546eaSitojun 		break;
1149d374aaacSitojun 	case SIOCSIFINFO_FLAGS:
1150d6b9e9b9Sitojun 		ND_IFINFO(ifp)->flags = ndi->ndi.flags;
1151d374aaacSitojun 		break;
1152f4f4d166Sitojun 	case SIOCSNDFLUSH_IN6:	/* XXX: the ioctl name is confusing... */
1153d8a7e3a7Sitojun 		/* sync kernel routing table with the default router list */
1154d8a7e3a7Sitojun 		defrouter_reset();
1155f4f4d166Sitojun 		defrouter_select();
1156287546eaSitojun 		break;
1157287546eaSitojun 	case SIOCSPFXFLUSH_IN6:
1158287546eaSitojun 	{
1159287546eaSitojun 		/* flush all the prefix advertised by routers */
1160119a433cSbluhm 		struct nd_prefix *pr, *npr;
1161287546eaSitojun 
11626e92dee6Sitojun 		s = splsoftnet();
1163db657d52Sbluhm 		/* First purge the addresses referenced by a prefix. */
1164119a433cSbluhm 		LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
1165b9e83c60Sbluhm 			struct in6_ifaddr *ia6, *ia6_next;
1166d8a7e3a7Sitojun 
1167d8a7e3a7Sitojun 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1168d8a7e3a7Sitojun 				continue; /* XXX */
1169d8a7e3a7Sitojun 
1170d8a7e3a7Sitojun 			/* do we really have to remove addresses as well? */
1171b9e83c60Sbluhm 			TAILQ_FOREACH_SAFE(ia6, &in6_ifaddr, ia_list, ia6_next) {
1172b9e83c60Sbluhm 				if ((ia6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1173d8a7e3a7Sitojun 					continue;
1174d8a7e3a7Sitojun 
1175b9e83c60Sbluhm 				if (ia6->ia6_ndpr == pr)
1176b9e83c60Sbluhm 					in6_purgeaddr(&ia6->ia_ifa);
1177d8a7e3a7Sitojun 			}
1178db657d52Sbluhm 		}
1179db657d52Sbluhm 		/*
1180db657d52Sbluhm 		 * Purging the addresses might remove the prefix as well.
1181db657d52Sbluhm 		 * So run the loop again to access only prefixes that have
1182db657d52Sbluhm 		 * not been freed already.
1183db657d52Sbluhm 		 */
1184119a433cSbluhm 		LIST_FOREACH_SAFE(pr, &nd_prefix, ndpr_entry, npr) {
1185db657d52Sbluhm 			if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr))
1186db657d52Sbluhm 				continue; /* XXX */
1187db657d52Sbluhm 
1188287546eaSitojun 			prelist_remove(pr);
1189287546eaSitojun 		}
1190287546eaSitojun 		splx(s);
1191287546eaSitojun 		break;
1192287546eaSitojun 	}
1193287546eaSitojun 	case SIOCSRTRFLUSH_IN6:
1194287546eaSitojun 	{
1195287546eaSitojun 		/* flush all the default routers */
1196568012e6Sbluhm 		struct nd_defrouter *dr, *ndr;
1197287546eaSitojun 
11986e92dee6Sitojun 		s = splsoftnet();
1199d8a7e3a7Sitojun 		defrouter_reset();
1200568012e6Sbluhm 		TAILQ_FOREACH_SAFE(dr, &nd_defrouter, dr_entry, ndr)
1201287546eaSitojun 			defrtrlist_del(dr);
1202d8a7e3a7Sitojun 		defrouter_select();
1203287546eaSitojun 		splx(s);
1204287546eaSitojun 		break;
1205287546eaSitojun 	}
1206287546eaSitojun 	case SIOCGNBRINFO_IN6:
1207287546eaSitojun 	{
1208287546eaSitojun 		struct llinfo_nd6 *ln;
1209287546eaSitojun 		struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1210287546eaSitojun 
1211287546eaSitojun 		/*
1212287546eaSitojun 		 * XXX: KAME specific hack for scoped addresses
1213287546eaSitojun 		 *      XXXX: for other scopes than link-local?
1214287546eaSitojun 		 */
1215287546eaSitojun 		if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) ||
1216287546eaSitojun 		    IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) {
1217287546eaSitojun 			u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2];
1218287546eaSitojun 
1219287546eaSitojun 			if (*idp == 0)
1220287546eaSitojun 				*idp = htons(ifp->if_index);
1221287546eaSitojun 		}
1222287546eaSitojun 
12236e92dee6Sitojun 		s = splsoftnet();
122416119bbfSmpi 		rt = nd6_lookup(&nb_addr, 0, ifp, ifp->if_rdomain);
122516119bbfSmpi 		if (rt == NULL ||
1226d8a7e3a7Sitojun 		    (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) {
1227287546eaSitojun 			error = EINVAL;
122816119bbfSmpi 			rtfree(rt);
122991e69634Sitojun 			splx(s);
1230287546eaSitojun 			break;
1231287546eaSitojun 		}
1232287546eaSitojun 		nbi->state = ln->ln_state;
1233287546eaSitojun 		nbi->asked = ln->ln_asked;
1234287546eaSitojun 		nbi->isrouter = ln->ln_router;
1235287546eaSitojun 		nbi->expire = ln->ln_expire;
123616119bbfSmpi 		rtfree(rt);
1237287546eaSitojun 		splx(s);
1238287546eaSitojun 
1239287546eaSitojun 		break;
1240287546eaSitojun 	}
1241287546eaSitojun 	}
1242287546eaSitojun 	return (error);
1243287546eaSitojun }
1244287546eaSitojun 
1245287546eaSitojun /*
1246287546eaSitojun  * Create neighbor cache entry and cache link-layer address,
1247287546eaSitojun  * on reception of inbound ND6 packets.  (RS/RA/NS/redirect)
1248ee37ea65Smcbride  *
1249ee37ea65Smcbride  * type - ICMP6 type
1250ee37ea65Smcbride  * code - type dependent information
1251287546eaSitojun  */
1252db435b2aSmpi void
1253ee37ea65Smcbride nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1254ee37ea65Smcbride     int lladdrlen, int type, int code)
1255287546eaSitojun {
1256287546eaSitojun 	struct rtentry *rt = NULL;
1257287546eaSitojun 	struct llinfo_nd6 *ln = NULL;
1258287546eaSitojun 	int is_newentry;
1259287546eaSitojun 	struct sockaddr_dl *sdl = NULL;
1260287546eaSitojun 	int do_update;
1261287546eaSitojun 	int olladdr;
1262287546eaSitojun 	int llchange;
1263287546eaSitojun 	int newstate = 0;
1264287546eaSitojun 
1265287546eaSitojun 	if (!ifp)
1266287546eaSitojun 		panic("ifp == NULL in nd6_cache_lladdr");
1267287546eaSitojun 	if (!from)
1268287546eaSitojun 		panic("from == NULL in nd6_cache_lladdr");
1269287546eaSitojun 
1270287546eaSitojun 	/* nothing must be updated for unspecified address */
1271287546eaSitojun 	if (IN6_IS_ADDR_UNSPECIFIED(from))
1272db435b2aSmpi 		return;
1273287546eaSitojun 
1274287546eaSitojun 	/*
1275287546eaSitojun 	 * Validation about ifp->if_addrlen and lladdrlen must be done in
1276287546eaSitojun 	 * the caller.
1277287546eaSitojun 	 *
12788b542bbeSpascoe 	 * XXX If the link does not have link-layer address, what should
1279287546eaSitojun 	 * we do? (ifp->if_addrlen == 0)
1280287546eaSitojun 	 * Spec says nothing in sections for RA, RS and NA.  There's small
1281287546eaSitojun 	 * description on it in NS section (RFC 2461 7.2.3).
1282287546eaSitojun 	 */
1283287546eaSitojun 
1284f4d1af37Smikeb 	rt = nd6_lookup(from, 0, ifp, ifp->if_rdomain);
128516119bbfSmpi 	if (rt == NULL) {
1286287546eaSitojun #if 0
1287287546eaSitojun 		/* nothing must be done if there's no lladdr */
1288287546eaSitojun 		if (!lladdr || !lladdrlen)
1289287546eaSitojun 			return NULL;
1290287546eaSitojun #endif
1291287546eaSitojun 
12925148b194Smpi 		rt = nd6_lookup(from, 1, ifp, ifp->if_rdomain);
1293287546eaSitojun 		is_newentry = 1;
12940a2c5741Sitojun 	} else {
12950a2c5741Sitojun 		/* do nothing if static ndp is set */
129616119bbfSmpi 		if (rt->rt_flags & RTF_STATIC) {
129716119bbfSmpi 			rtfree(rt);
1298db435b2aSmpi 			return;
129916119bbfSmpi 		}
1300287546eaSitojun 		is_newentry = 0;
13010a2c5741Sitojun 	}
1302287546eaSitojun 
1303287546eaSitojun 	if (!rt)
1304db435b2aSmpi 		return;
1305287546eaSitojun 	if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) {
1306287546eaSitojun fail:
1307d8a7e3a7Sitojun 		(void)nd6_free(rt, 0);
130816119bbfSmpi 		rtfree(rt);
1309db435b2aSmpi 		return;
1310287546eaSitojun 	}
1311287546eaSitojun 	ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1312287546eaSitojun 	if (!ln)
1313287546eaSitojun 		goto fail;
1314287546eaSitojun 	if (!rt->rt_gateway)
1315287546eaSitojun 		goto fail;
1316287546eaSitojun 	if (rt->rt_gateway->sa_family != AF_LINK)
1317287546eaSitojun 		goto fail;
1318c7b7b779Sbluhm 	sdl = satosdl(rt->rt_gateway);
1319287546eaSitojun 
1320287546eaSitojun 	olladdr = (sdl->sdl_alen) ? 1 : 0;
1321287546eaSitojun 	if (olladdr && lladdr) {
1322287546eaSitojun 		if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen))
1323287546eaSitojun 			llchange = 1;
1324287546eaSitojun 		else
1325287546eaSitojun 			llchange = 0;
1326287546eaSitojun 	} else
1327287546eaSitojun 		llchange = 0;
1328287546eaSitojun 
1329287546eaSitojun 	/*
1330287546eaSitojun 	 * newentry olladdr  lladdr  llchange	(*=record)
1331287546eaSitojun 	 *	0	n	n	--	(1)
1332287546eaSitojun 	 *	0	y	n	--	(2)
1333287546eaSitojun 	 *	0	n	y	--	(3) * STALE
1334287546eaSitojun 	 *	0	y	y	n	(4) *
1335287546eaSitojun 	 *	0	y	y	y	(5) * STALE
1336287546eaSitojun 	 *	1	--	n	--	(6)   NOSTATE(= PASSIVE)
1337287546eaSitojun 	 *	1	--	y	--	(7) * STALE
1338287546eaSitojun 	 */
1339287546eaSitojun 
1340c8a7c9e3Sbluhm 	if (llchange) {
1341bbcf0337Smpi 		char addr[INET6_ADDRSTRLEN];
1342c8a7c9e3Sbluhm 		log(LOG_INFO, "ndp info overwritten for %s by %s on %s\n",
1343bbcf0337Smpi 		    inet_ntop(AF_INET6, from, addr, sizeof(addr)),
1344bbcf0337Smpi 		    ether_sprintf(lladdr), ifp->if_xname);
1345c8a7c9e3Sbluhm 	}
1346287546eaSitojun 	if (lladdr) {		/* (3-5) and (7) */
1347287546eaSitojun 		/*
1348287546eaSitojun 		 * Record source link-layer address
1349287546eaSitojun 		 * XXX is it dependent to ifp->if_type?
1350287546eaSitojun 		 */
1351287546eaSitojun 		sdl->sdl_alen = ifp->if_addrlen;
1352287546eaSitojun 		bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen);
1353287546eaSitojun 	}
1354287546eaSitojun 
1355287546eaSitojun 	if (!is_newentry) {
1356d8a7e3a7Sitojun 		if ((!olladdr && lladdr) ||		/* (3) */
1357d8a7e3a7Sitojun 		    (olladdr && lladdr && llchange)) {	/* (5) */
1358287546eaSitojun 			do_update = 1;
1359287546eaSitojun 			newstate = ND6_LLINFO_STALE;
1360287546eaSitojun 		} else					/* (1-2,4) */
1361287546eaSitojun 			do_update = 0;
1362287546eaSitojun 	} else {
1363287546eaSitojun 		do_update = 1;
1364287546eaSitojun 		if (!lladdr)				/* (6) */
1365287546eaSitojun 			newstate = ND6_LLINFO_NOSTATE;
1366287546eaSitojun 		else					/* (7) */
1367287546eaSitojun 			newstate = ND6_LLINFO_STALE;
1368287546eaSitojun 	}
1369287546eaSitojun 
1370287546eaSitojun 	if (do_update) {
1371287546eaSitojun 		/*
1372287546eaSitojun 		 * Update the state of the neighbor cache.
1373287546eaSitojun 		 */
1374287546eaSitojun 		ln->ln_state = newstate;
1375287546eaSitojun 
1376287546eaSitojun 		if (ln->ln_state == ND6_LLINFO_STALE) {
13778a7bb304Sitojun 			/*
13788a7bb304Sitojun 			 * XXX: since nd6_output() below will cause
13798b542bbeSpascoe 			 * state transition to DELAY and reset the timer,
13808a7bb304Sitojun 			 * we must set the timer now, although it is actually
13818a7bb304Sitojun 			 * meaningless.
13828a7bb304Sitojun 			 */
13839631a17bSitojun 			nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
13848a7bb304Sitojun 
1385287546eaSitojun 			if (ln->ln_hold) {
1386e212adedSkrw 				struct mbuf *n = ln->ln_hold;
1387e212adedSkrw 				ln->ln_hold = NULL;
13886afad192Sitojun 				/*
13896afad192Sitojun 				 * we assume ifp is not a p2p here, so just
13906afad192Sitojun 				 * set the 2nd argument as the 1st one.
13916afad192Sitojun 				 */
1392bc3c7896Smpi 				nd6_output(ifp, n, satosin6(rt_key(rt)), rt);
1393e212adedSkrw 				if (ln->ln_hold == n) {
1394e212adedSkrw 					/* n is back in ln_hold. Discard. */
1395e212adedSkrw 					m_freem(ln->ln_hold);
13968a7bb304Sitojun 					ln->ln_hold = NULL;
1397287546eaSitojun 				}
1398e212adedSkrw 			}
1399287546eaSitojun 		} else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) {
1400287546eaSitojun 			/* probe right away */
14019631a17bSitojun 			nd6_llinfo_settimer((void *)ln, 0);
1402287546eaSitojun 		}
1403287546eaSitojun 	}
1404287546eaSitojun 
1405287546eaSitojun 	/*
1406287546eaSitojun 	 * ICMP6 type dependent behavior.
1407287546eaSitojun 	 *
1408287546eaSitojun 	 * NS: clear IsRouter if new entry
1409287546eaSitojun 	 * RS: clear IsRouter
1410287546eaSitojun 	 * RA: set IsRouter if there's lladdr
1411287546eaSitojun 	 * redir: clear IsRouter if new entry
1412287546eaSitojun 	 *
1413287546eaSitojun 	 * RA case, (1):
1414287546eaSitojun 	 * The spec says that we must set IsRouter in the following cases:
1415287546eaSitojun 	 * - If lladdr exist, set IsRouter.  This means (1-5).
1416287546eaSitojun 	 * - If it is old entry (!newentry), set IsRouter.  This means (7).
1417287546eaSitojun 	 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
14188b542bbeSpascoe 	 * A question arises for (1) case.  (1) case has no lladdr in the
1419287546eaSitojun 	 * neighbor cache, this is similar to (6).
1420287546eaSitojun 	 * This case is rare but we figured that we MUST NOT set IsRouter.
1421287546eaSitojun 	 *
1422287546eaSitojun 	 * newentry olladdr  lladdr  llchange	    NS  RS  RA	redir
1423287546eaSitojun 	 *							D R
1424287546eaSitojun 	 *	0	n	n	--	(1)	c   ?     s
1425287546eaSitojun 	 *	0	y	n	--	(2)	c   s     s
1426287546eaSitojun 	 *	0	n	y	--	(3)	c   s     s
1427287546eaSitojun 	 *	0	y	y	n	(4)	c   s     s
1428287546eaSitojun 	 *	0	y	y	y	(5)	c   s     s
1429287546eaSitojun 	 *	1	--	n	--	(6) c	c	c s
1430287546eaSitojun 	 *	1	--	y	--	(7) c	c   s	c s
1431287546eaSitojun 	 *
1432287546eaSitojun 	 *					(c=clear s=set)
1433287546eaSitojun 	 */
1434287546eaSitojun 	switch (type & 0xff) {
1435287546eaSitojun 	case ND_NEIGHBOR_SOLICIT:
1436287546eaSitojun 		/*
1437287546eaSitojun 		 * New entry must have is_router flag cleared.
1438287546eaSitojun 		 */
1439287546eaSitojun 		if (is_newentry)	/* (6-7) */
1440287546eaSitojun 			ln->ln_router = 0;
1441287546eaSitojun 		break;
1442287546eaSitojun 	case ND_REDIRECT:
1443287546eaSitojun 		/*
1444287546eaSitojun 		 * If the icmp is a redirect to a better router, always set the
1445287546eaSitojun 		 * is_router flag.  Otherwise, if the entry is newly created,
1446287546eaSitojun 		 * clear the flag.  [RFC 2461, sec 8.3]
1447287546eaSitojun 		 */
1448287546eaSitojun 		if (code == ND_REDIRECT_ROUTER)
1449287546eaSitojun 			ln->ln_router = 1;
1450287546eaSitojun 		else if (is_newentry) /* (6-7) */
1451287546eaSitojun 			ln->ln_router = 0;
1452287546eaSitojun 		break;
1453287546eaSitojun 	case ND_ROUTER_SOLICIT:
1454287546eaSitojun 		/*
1455287546eaSitojun 		 * is_router flag must always be cleared.
1456287546eaSitojun 		 */
1457287546eaSitojun 		ln->ln_router = 0;
1458287546eaSitojun 		break;
1459287546eaSitojun 	case ND_ROUTER_ADVERT:
1460287546eaSitojun 		/*
1461287546eaSitojun 		 * Mark an entry with lladdr as a router.
1462287546eaSitojun 		 */
1463d8a7e3a7Sitojun 		if ((!is_newentry && (olladdr || lladdr)) ||	/* (2-5) */
1464d8a7e3a7Sitojun 		    (is_newentry && lladdr)) {			/* (7) */
1465287546eaSitojun 			ln->ln_router = 1;
1466287546eaSitojun 		}
1467287546eaSitojun 		break;
1468287546eaSitojun 	}
1469287546eaSitojun 
147050ccc024Sitojun 	/*
147150ccc024Sitojun 	 * When the link-layer address of a router changes, select the
147250ccc024Sitojun 	 * best router again.  In particular, when the neighbor entry is newly
147350ccc024Sitojun 	 * created, it might affect the selection policy.
147450ccc024Sitojun 	 * Question: can we restrict the first condition to the "is_newentry"
147550ccc024Sitojun 	 * case?
147650ccc024Sitojun 	 * XXX: when we hear an RA from a new router with the link-layer
147750ccc024Sitojun 	 * address option, defrouter_select() is called twice, since
147850ccc024Sitojun 	 * defrtrlist_update called the function as well.  However, I believe
147950ccc024Sitojun 	 * we can compromise the overhead, since it only happens the first
148050ccc024Sitojun 	 * time.
148150ccc024Sitojun 	 */
1482d3b1c214Sflorian 	if (do_update && ln->ln_router && (ifp->if_xflags & IFXF_AUTOCONF6))
148350ccc024Sitojun 		defrouter_select();
148416119bbfSmpi 
148516119bbfSmpi 	rtfree(rt);
1486287546eaSitojun }
1487287546eaSitojun 
1488a0aa363cSjsing void
1489ee37ea65Smcbride nd6_slowtimo(void *ignored_arg)
1490287546eaSitojun {
14916e92dee6Sitojun 	int s = splsoftnet();
1492b3c1e4c1Sitojun 	struct nd_ifinfo *nd6if;
1493d6b9e9b9Sitojun 	struct ifnet *ifp;
1494287546eaSitojun 
1495b3c1e4c1Sitojun 	timeout_set(&nd6_slowtimo_ch, nd6_slowtimo, NULL);
149629e86e5eSblambert 	timeout_add_sec(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL);
1497d814b14cSbluhm 	TAILQ_FOREACH(ifp, &ifnet, if_list) {
1498d6b9e9b9Sitojun 		nd6if = ND_IFINFO(ifp);
1499287546eaSitojun 		if (nd6if->basereachable && /* already initialized */
1500287546eaSitojun 		    (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1501287546eaSitojun 			/*
1502287546eaSitojun 			 * Since reachable time rarely changes by router
1503287546eaSitojun 			 * advertisements, we SHOULD insure that a new random
1504287546eaSitojun 			 * value gets recomputed at least once every few hours.
1505287546eaSitojun 			 * (RFC 2461, 6.3.4)
1506287546eaSitojun 			 */
1507287546eaSitojun 			nd6if->recalctm = nd6_recalc_reachtm_interval;
1508287546eaSitojun 			nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable);
1509287546eaSitojun 		}
1510287546eaSitojun 	}
1511287546eaSitojun 	splx(s);
1512287546eaSitojun }
1513287546eaSitojun 
1514287546eaSitojun int
1515bc3c7896Smpi nd6_output(struct ifnet *ifp, struct mbuf *m0, struct sockaddr_in6 *dst,
1516bc3c7896Smpi     struct rtentry *rt0)
1517287546eaSitojun {
1518b3c1e4c1Sitojun 	struct mbuf *m = m0;
1519b3c1e4c1Sitojun 	struct rtentry *rt = rt0;
1520287546eaSitojun 	struct llinfo_nd6 *ln = NULL;
152116119bbfSmpi 	int created = 0, error = 0;
1522287546eaSitojun 
1523287546eaSitojun 	if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr))
1524287546eaSitojun 		goto sendpkt;
1525287546eaSitojun 
1526d8a7e3a7Sitojun 	if (nd6_need_cache(ifp) == 0)
1527287546eaSitojun 		goto sendpkt;
1528287546eaSitojun 
1529287546eaSitojun 	/*
15308c023157Smpi 	 * next hop determination.
1531287546eaSitojun 	 */
15328c023157Smpi 	if (rt0 != NULL) {
15338c023157Smpi 		error = rt_checkgate(ifp, rt0, sin6tosa(dst),
15348c023157Smpi 		    m->m_pkthdr.ph_rtableid, &rt);
15358c023157Smpi 		if (error) {
15368c023157Smpi 			m_freem(m);
15378c023157Smpi 			return (error);
1538287546eaSitojun 		}
1539cfb6b8dfSitojun 
1540cfb6b8dfSitojun 		/*
1541cfb6b8dfSitojun 		 * We skip link-layer address resolution and NUD
1542cfb6b8dfSitojun 		 * if the gateway is not a neighbor from ND point
1543d8a7e3a7Sitojun 		 * of view, regardless of the value of nd_ifinfo.flags.
154434deef1eSitojun 		 * The second condition is a bit tricky; we skip
1545cfb6b8dfSitojun 		 * if the gateway is our own address, which is
1546cfb6b8dfSitojun 		 * sometimes used to install a route to a p2p link.
1547cfb6b8dfSitojun 		 */
15488c023157Smpi 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
15498c023157Smpi 		    ((nd6_is_addr_neighbor(satosin6(rt_key(rt)), ifp) == 0) ||
15508c023157Smpi 		    in6ifa_ifpwithaddr(ifp, &satosin6(rt_key(rt))->sin6_addr)))
1551cfb6b8dfSitojun 			goto sendpkt;
1552cfb6b8dfSitojun 	}
1553cfb6b8dfSitojun 
1554287546eaSitojun 	/*
1555287546eaSitojun 	 * Address resolution or Neighbor Unreachability Detection
1556287546eaSitojun 	 * for the next hop.
1557287546eaSitojun 	 * At this point, the destination of the packet must be a unicast
1558287546eaSitojun 	 * or an anycast address(i.e. not a multicast).
1559287546eaSitojun 	 */
1560287546eaSitojun 
1561287546eaSitojun 	/* Look up the neighbor cache for the nexthop */
1562287546eaSitojun 	if (rt && (rt->rt_flags & RTF_LLINFO) != 0)
1563287546eaSitojun 		ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1564287546eaSitojun 	else {
1565cfb6b8dfSitojun 		/*
1566cfb6b8dfSitojun 		 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1567cfb6b8dfSitojun 		 * the condition below is not very efficient.  But we believe
1568cfb6b8dfSitojun 		 * it is tolerable, because this should be a rare case.
1569cfb6b8dfSitojun 		 */
157016119bbfSmpi 		if (nd6_is_addr_neighbor(dst, ifp)) {
157116119bbfSmpi 			rt = nd6_lookup(&dst->sin6_addr, 1, ifp,
157216119bbfSmpi 			    ifp->if_rdomain);
157316119bbfSmpi 			if (rt != NULL) {
157416119bbfSmpi 				created = 1;
1575287546eaSitojun 				ln = (struct llinfo_nd6 *)rt->rt_llinfo;
1576287546eaSitojun 			}
157716119bbfSmpi 		}
157816119bbfSmpi 	}
1579287546eaSitojun 	if (!ln || !rt) {
1580cfb6b8dfSitojun 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0 &&
1581d6b9e9b9Sitojun 		    !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) {
1582bbcf0337Smpi 			char addr[INET6_ADDRSTRLEN];
1583bbcf0337Smpi 
158435075f95Smpi 			log(LOG_DEBUG, "%s: can't allocate llinfo for %s "
158535075f95Smpi 			    "(ln=%p, rt=%p)\n", __func__,
1586bbcf0337Smpi 			    inet_ntop(AF_INET6, &dst->sin6_addr,
1587bbcf0337Smpi 				addr, sizeof(addr)),
1588bbcf0337Smpi 			    ln, rt);
158916119bbfSmpi 			m_freem(m);
159016119bbfSmpi 			if (created)
159116119bbfSmpi 				rtfree(rt);
159216119bbfSmpi 			return (EIO);	/* XXX: good error? */
1593287546eaSitojun 		}
1594287546eaSitojun 
1595cfb6b8dfSitojun 		goto sendpkt;	/* send anyway */
1596cfb6b8dfSitojun 	}
1597cfb6b8dfSitojun 
1598f3fcf2f3Smcbride 	/*
1599f3fcf2f3Smcbride 	 * Move this entry to the head of the queue so that it is less likely
1600f3fcf2f3Smcbride 	 * for this entry to be a target of forced garbage collection (see
1601f3fcf2f3Smcbride 	 * nd6_rtrequest()).
1602f3fcf2f3Smcbride 	 */
1603f3fcf2f3Smcbride 	LN_DEQUEUE(ln);
1604f3fcf2f3Smcbride 	LN_INSERTHEAD(ln);
1605f3fcf2f3Smcbride 
1606d374aaacSitojun 	/* We don't have to do link-layer address resolution on a p2p link. */
1607d374aaacSitojun 	if ((ifp->if_flags & IFF_POINTOPOINT) != 0 &&
1608be4e9e12Sitojun 	    ln->ln_state < ND6_LLINFO_REACHABLE) {
1609d374aaacSitojun 		ln->ln_state = ND6_LLINFO_STALE;
16109631a17bSitojun 		nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz);
1611be4e9e12Sitojun 	}
1612287546eaSitojun 
1613287546eaSitojun 	/*
1614287546eaSitojun 	 * The first time we send a packet to a neighbor whose entry is
1615287546eaSitojun 	 * STALE, we have to change the state to DELAY and a sets a timer to
1616287546eaSitojun 	 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
1617287546eaSitojun 	 * neighbor unreachability detection on expiration.
1618287546eaSitojun 	 * (RFC 2461 7.3.3)
1619287546eaSitojun 	 */
1620287546eaSitojun 	if (ln->ln_state == ND6_LLINFO_STALE) {
1621287546eaSitojun 		ln->ln_asked = 0;
1622287546eaSitojun 		ln->ln_state = ND6_LLINFO_DELAY;
16239631a17bSitojun 		nd6_llinfo_settimer(ln, nd6_delay * hz);
1624287546eaSitojun 	}
1625287546eaSitojun 
1626287546eaSitojun 	/*
1627287546eaSitojun 	 * If the neighbor cache entry has a state other than INCOMPLETE
162834deef1eSitojun 	 * (i.e. its link-layer address is already resolved), just
1629287546eaSitojun 	 * send the packet.
1630287546eaSitojun 	 */
1631287546eaSitojun 	if (ln->ln_state > ND6_LLINFO_INCOMPLETE)
1632287546eaSitojun 		goto sendpkt;
1633287546eaSitojun 
1634287546eaSitojun 	/*
1635287546eaSitojun 	 * There is a neighbor cache entry, but no ethernet address
1636287546eaSitojun 	 * response yet.  Replace the held mbuf (if any) with this
1637287546eaSitojun 	 * latest one.
1638287546eaSitojun 	 */
1639efcf292bSitojun 	if (ln->ln_state == ND6_LLINFO_NOSTATE)
1640287546eaSitojun 		ln->ln_state = ND6_LLINFO_INCOMPLETE;
1641287546eaSitojun 	m_freem(ln->ln_hold);
1642287546eaSitojun 	ln->ln_hold = m;
164376843262Sitojun 	/*
164476843262Sitojun 	 * If there has been no NS for the neighbor after entering the
164576843262Sitojun 	 * INCOMPLETE state, send the first solicitation.
164676843262Sitojun 	 */
16479631a17bSitojun 	if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) {
1648287546eaSitojun 		ln->ln_asked++;
16499631a17bSitojun 		nd6_llinfo_settimer(ln,
16509631a17bSitojun 		    (long)ND_IFINFO(ifp)->retrans * hz / 1000);
1651287546eaSitojun 		nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0);
1652287546eaSitojun 	}
165316119bbfSmpi 	if (created)
165416119bbfSmpi 		rtfree(rt);
1655287546eaSitojun 	return (0);
1656287546eaSitojun 
1657287546eaSitojun   sendpkt:
165816119bbfSmpi 	error = ifp->if_output(ifp, m, sin6tosa(dst), rt);
165916119bbfSmpi 	if (created)
166016119bbfSmpi 		rtfree(rt);
1661287546eaSitojun 	return (error);
1662287546eaSitojun }
1663287546eaSitojun 
1664287546eaSitojun int
1665ee37ea65Smcbride nd6_need_cache(struct ifnet *ifp)
1666d8a7e3a7Sitojun {
1667d8a7e3a7Sitojun 	/*
1668d8a7e3a7Sitojun 	 * RFC2893 says:
1669d8a7e3a7Sitojun 	 * - unidirectional tunnels needs no ND
1670d8a7e3a7Sitojun 	 */
1671d8a7e3a7Sitojun 	switch (ifp->if_type) {
1672d8a7e3a7Sitojun 	case IFT_ETHER:
1673d8a7e3a7Sitojun 	case IFT_IEEE1394:
1674d8a7e3a7Sitojun 	case IFT_PROPVIRTUAL:
1675d8a7e3a7Sitojun 	case IFT_IEEE80211:
1676f4433d56Shenning 	case IFT_CARP:
1677d8a7e3a7Sitojun 	case IFT_GIF:		/* XXX need more cases? */
1678d8a7e3a7Sitojun 		return (1);
1679d8a7e3a7Sitojun 	default:
1680d8a7e3a7Sitojun 		return (0);
1681d8a7e3a7Sitojun 	}
1682d8a7e3a7Sitojun }
1683d8a7e3a7Sitojun 
1684d8a7e3a7Sitojun int
16858c023157Smpi nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m,
1686ee37ea65Smcbride     struct sockaddr *dst, u_char *desten)
1687287546eaSitojun {
1688287546eaSitojun 	struct sockaddr_dl *sdl;
16898c023157Smpi 	struct rtentry *rt;
16908c023157Smpi 	int error;
1691287546eaSitojun 
1692287546eaSitojun 	if (m->m_flags & M_MCAST) {
1693287546eaSitojun 		switch (ifp->if_type) {
1694287546eaSitojun 		case IFT_ETHER:
169565d1c24fSmpi 		case IFT_CARP:
1696c3c56496Sbluhm 			ETHER_MAP_IPV6_MULTICAST(&satosin6(dst)->sin6_addr,
1697287546eaSitojun 						 desten);
16988c023157Smpi 			return (0);
1699287546eaSitojun 			break;
1700287546eaSitojun 		default:
17010a2c5741Sitojun 			m_freem(m);
17028c023157Smpi 			return (EINVAL);
1703287546eaSitojun 		}
1704287546eaSitojun 	}
1705287546eaSitojun 
17068c023157Smpi 	if (rt0 == NULL) {
170772e30262Sitojun 		/* this could happen, if we could not allocate memory */
17080a2c5741Sitojun 		m_freem(m);
17098c023157Smpi 		return (ENOMEM);
171072e30262Sitojun 	}
17118c023157Smpi 
17128c023157Smpi 	error = rt_checkgate(ifp, rt0, dst, m->m_pkthdr.ph_rtableid, &rt);
17138c023157Smpi 	if (error) {
17148c023157Smpi 		m_freem(m);
17158c023157Smpi 		return (error);
17168c023157Smpi 	}
17178c023157Smpi 
171872e30262Sitojun 	if (rt->rt_gateway->sa_family != AF_LINK) {
171935075f95Smpi 		printf("%s: something odd happens\n", __func__);
17200a2c5741Sitojun 		m_freem(m);
17218c023157Smpi 		return (EINVAL);
1722287546eaSitojun 	}
1723c7b7b779Sbluhm 	sdl = satosdl(rt->rt_gateway);
1724dbae7f73Smpi 	if (sdl->sdl_alen != ETHER_ADDR_LEN) {
1725bbcf0337Smpi 		char addr[INET6_ADDRSTRLEN];
1726dbae7f73Smpi 		log(LOG_DEBUG, "%s: %s: incorrect nd6 information\n", __func__,
1727bbcf0337Smpi 		    inet_ntop(AF_INET6, &satosin6(dst)->sin6_addr,
1728dbae7f73Smpi 			addr, sizeof(addr)));
17290a2c5741Sitojun 		m_freem(m);
17308c023157Smpi 		return (EINVAL);
1731b4ee66fcSitojun 	}
1732287546eaSitojun 
1733b4ee66fcSitojun 	bcopy(LLADDR(sdl), desten, sdl->sdl_alen);
17348c023157Smpi 	return (0);
1735287546eaSitojun }
1736d8a7e3a7Sitojun 
1737ee37ea65Smcbride /*
1738ee37ea65Smcbride  * oldp - syscall arg, need copyout
1739ee37ea65Smcbride  * newp - syscall arg, need copyin
1740ee37ea65Smcbride  */
1741ee37ea65Smcbride 
1742d8a7e3a7Sitojun int
1743a0aa363cSjsing nd6_sysctl(int name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
1744d8a7e3a7Sitojun {
1745db3db419Sitojun 	void *p;
174634223ca4Schl 	size_t ol;
1747d8a7e3a7Sitojun 	int error;
1748d8a7e3a7Sitojun 
1749d8a7e3a7Sitojun 	error = 0;
1750d8a7e3a7Sitojun 
1751d8a7e3a7Sitojun 	if (newp)
1752d8a7e3a7Sitojun 		return EPERM;
1753d8a7e3a7Sitojun 	if (oldp && !oldlenp)
1754d8a7e3a7Sitojun 		return EINVAL;
1755d8a7e3a7Sitojun 	ol = oldlenp ? *oldlenp : 0;
1756d8a7e3a7Sitojun 
1757db3db419Sitojun 	if (oldp) {
1758e6ca3d0dSmk 		p = malloc(*oldlenp, M_TEMP, M_WAITOK | M_CANFAIL);
1759db3db419Sitojun 		if (!p)
1760db3db419Sitojun 			return ENOMEM;
1761db3db419Sitojun 	} else
1762db3db419Sitojun 		p = NULL;
1763d8a7e3a7Sitojun 	switch (name) {
1764d8a7e3a7Sitojun 	case ICMPV6CTL_ND6_DRLIST:
1765db3db419Sitojun 		error = fill_drlist(p, oldlenp, ol);
1766db3db419Sitojun 		if (!error && p && oldp)
1767732568ffSitojun 			error = copyout(p, oldp, *oldlenp);
1768d8a7e3a7Sitojun 		break;
1769d8a7e3a7Sitojun 
1770d8a7e3a7Sitojun 	case ICMPV6CTL_ND6_PRLIST:
1771db3db419Sitojun 		error = fill_prlist(p, oldlenp, ol);
1772db3db419Sitojun 		if (!error && p && oldp)
1773732568ffSitojun 			error = copyout(p, oldp, *oldlenp);
1774d8a7e3a7Sitojun 		break;
1775d8a7e3a7Sitojun 
1776d8a7e3a7Sitojun 	default:
1777d8a7e3a7Sitojun 		error = ENOPROTOOPT;
1778d8a7e3a7Sitojun 		break;
1779d8a7e3a7Sitojun 	}
1780db3db419Sitojun 	if (p)
1781dd168dc2Stedu 		free(p, M_TEMP, 0);
1782d8a7e3a7Sitojun 
1783d8a7e3a7Sitojun 	return (error);
1784d8a7e3a7Sitojun }
1785d8a7e3a7Sitojun 
1786a0aa363cSjsing int
1787ee37ea65Smcbride fill_drlist(void *oldp, size_t *oldlenp, size_t ol)
1788d8a7e3a7Sitojun {
1789d8a7e3a7Sitojun 	int error = 0, s;
1790d8a7e3a7Sitojun 	struct in6_defrouter *d = NULL, *de = NULL;
1791d8a7e3a7Sitojun 	struct nd_defrouter *dr;
1792d8a7e3a7Sitojun 	size_t l;
1793d8a7e3a7Sitojun 
17946e92dee6Sitojun 	s = splsoftnet();
1795d8a7e3a7Sitojun 
1796d8a7e3a7Sitojun 	if (oldp) {
1797d8a7e3a7Sitojun 		d = (struct in6_defrouter *)oldp;
1798d8a7e3a7Sitojun 		de = (struct in6_defrouter *)((caddr_t)oldp + *oldlenp);
1799d8a7e3a7Sitojun 	}
1800d8a7e3a7Sitojun 	l = 0;
1801d8a7e3a7Sitojun 
1802568012e6Sbluhm 	TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) {
1803d8a7e3a7Sitojun 		if (oldp && d + 1 <= de) {
1804d8a7e3a7Sitojun 			bzero(d, sizeof(*d));
1805d8a7e3a7Sitojun 			d->rtaddr.sin6_family = AF_INET6;
1806d8a7e3a7Sitojun 			d->rtaddr.sin6_len = sizeof(struct sockaddr_in6);
18076c8dd0e9Sclaudio 			in6_recoverscope(&d->rtaddr, &dr->rtaddr);
1808d8a7e3a7Sitojun 			d->flags = dr->flags;
1809d8a7e3a7Sitojun 			d->rtlifetime = dr->rtlifetime;
1810d8a7e3a7Sitojun 			d->expire = dr->expire;
1811d8a7e3a7Sitojun 			d->if_index = dr->ifp->if_index;
1812d8a7e3a7Sitojun 		}
1813d8a7e3a7Sitojun 
1814d8a7e3a7Sitojun 		l += sizeof(*d);
1815d8a7e3a7Sitojun 		if (d)
1816d8a7e3a7Sitojun 			d++;
1817d8a7e3a7Sitojun 	}
1818d8a7e3a7Sitojun 
1819d8a7e3a7Sitojun 	if (oldp) {
1820d8a7e3a7Sitojun 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
1821d8a7e3a7Sitojun 		if (l > ol)
1822d8a7e3a7Sitojun 			error = ENOMEM;
1823d8a7e3a7Sitojun 	} else
1824d8a7e3a7Sitojun 		*oldlenp = l;
1825d8a7e3a7Sitojun 
1826d8a7e3a7Sitojun 	splx(s);
1827d8a7e3a7Sitojun 
1828d8a7e3a7Sitojun 	return (error);
1829d8a7e3a7Sitojun }
1830d8a7e3a7Sitojun 
1831a0aa363cSjsing int
1832ee37ea65Smcbride fill_prlist(void *oldp, size_t *oldlenp, size_t ol)
1833d8a7e3a7Sitojun {
1834d8a7e3a7Sitojun 	int error = 0, s;
1835d8a7e3a7Sitojun 	struct nd_prefix *pr;
183608247436Snaddy 	char *p = NULL, *ps = NULL;
183708247436Snaddy 	char *pe = NULL;
1838d8a7e3a7Sitojun 	size_t l;
1839d8a7e3a7Sitojun 
18406e92dee6Sitojun 	s = splsoftnet();
1841d8a7e3a7Sitojun 
1842d8a7e3a7Sitojun 	if (oldp) {
184308247436Snaddy 		ps = p = (char *)oldp;
184408247436Snaddy 		pe = (char *)oldp + *oldlenp;
1845d8a7e3a7Sitojun 	}
1846d8a7e3a7Sitojun 	l = 0;
1847d8a7e3a7Sitojun 
1848545205eaSpyr 	LIST_FOREACH(pr, &nd_prefix, ndpr_entry) {
1849d8a7e3a7Sitojun 		u_short advrtrs;
185008247436Snaddy 		struct sockaddr_in6 sin6;
1851d8a7e3a7Sitojun 		struct nd_pfxrouter *pfr;
185208247436Snaddy 		struct in6_prefix pfx;
1853d8a7e3a7Sitojun 
185408247436Snaddy 		if (oldp && p + sizeof(struct in6_prefix) <= pe) {
185508247436Snaddy 			memset(&pfx, 0, sizeof(pfx));
185608247436Snaddy 			ps = p;
1857d8a7e3a7Sitojun 
185808247436Snaddy 			pfx.prefix = pr->ndpr_prefix;
18596c8dd0e9Sclaudio 			in6_recoverscope(&pfx.prefix,
18606c8dd0e9Sclaudio 			    &pfx.prefix.sin6_addr);
186108247436Snaddy 			pfx.raflags = pr->ndpr_raf;
186208247436Snaddy 			pfx.prefixlen = pr->ndpr_plen;
186308247436Snaddy 			pfx.vltime = pr->ndpr_vltime;
186408247436Snaddy 			pfx.pltime = pr->ndpr_pltime;
186508247436Snaddy 			pfx.if_index = pr->ndpr_ifp->if_index;
1866d8a7e3a7Sitojun 			if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
186708247436Snaddy 				pfx.expire = 0;
1868d8a7e3a7Sitojun 			else {
1869d8a7e3a7Sitojun 				time_t maxexpire;
1870d8a7e3a7Sitojun 
1871d8a7e3a7Sitojun 				/* XXX: we assume time_t is signed. */
187291a535ffSguenther 				maxexpire = (time_t)~(1ULL <<
187391a535ffSguenther 				    ((sizeof(maxexpire) * 8) - 1));
1874d8a7e3a7Sitojun 				if (pr->ndpr_vltime <
1875d8a7e3a7Sitojun 				    maxexpire - pr->ndpr_lastupdate) {
187608247436Snaddy 					pfx.expire = pr->ndpr_lastupdate +
1877d8a7e3a7Sitojun 						pr->ndpr_vltime;
1878d8a7e3a7Sitojun 				} else
187908247436Snaddy 					pfx.expire = maxexpire;
1880d8a7e3a7Sitojun 			}
188108247436Snaddy 			pfx.refcnt = pr->ndpr_refcnt;
188208247436Snaddy 			pfx.flags = pr->ndpr_stateflags;
188308247436Snaddy 			pfx.origin = PR_ORIG_RA;
188408247436Snaddy 
188508247436Snaddy 			p += sizeof(pfx); l += sizeof(pfx);
188608247436Snaddy 
1887d8a7e3a7Sitojun 			advrtrs = 0;
1888545205eaSpyr 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
188908247436Snaddy 				if (p + sizeof(sin6) > pe) {
1890d8a7e3a7Sitojun 					advrtrs++;
1891d8a7e3a7Sitojun 					continue;
1892d8a7e3a7Sitojun 				}
18939c794535Sbluhm 				bzero(&sin6, sizeof(sin6));
189408247436Snaddy 				sin6.sin6_family = AF_INET6;
189508247436Snaddy 				sin6.sin6_len = sizeof(struct sockaddr_in6);
18966c8dd0e9Sclaudio 				in6_recoverscope(&sin6, &pfr->router->rtaddr);
1897d8a7e3a7Sitojun 				advrtrs++;
189808247436Snaddy 				memcpy(p, &sin6, sizeof(sin6));
189908247436Snaddy 				p += sizeof(sin6);
190008247436Snaddy 				l += sizeof(sin6);
1901d8a7e3a7Sitojun 			}
190208247436Snaddy 			pfx.advrtrs = advrtrs;
190308247436Snaddy 			memcpy(ps, &pfx, sizeof(pfx));
1904d8a7e3a7Sitojun 		}
1905d8a7e3a7Sitojun 		else {
190608247436Snaddy 			l += sizeof(pfx);
1907d8a7e3a7Sitojun 			advrtrs = 0;
190808247436Snaddy 			LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
1909d8a7e3a7Sitojun 				advrtrs++;
191008247436Snaddy 				l += sizeof(sin6);
1911d8a7e3a7Sitojun 			}
191208247436Snaddy 		}
1913d8a7e3a7Sitojun 	}
1914d8a7e3a7Sitojun 
1915d8a7e3a7Sitojun 	if (oldp) {
1916d8a7e3a7Sitojun 		*oldlenp = l;	/* (caddr_t)d - (caddr_t)oldp */
1917d8a7e3a7Sitojun 		if (l > ol)
1918d8a7e3a7Sitojun 			error = ENOMEM;
1919d8a7e3a7Sitojun 	} else
1920d8a7e3a7Sitojun 		*oldlenp = l;
1921d8a7e3a7Sitojun 
1922d8a7e3a7Sitojun 	splx(s);
1923d8a7e3a7Sitojun 
1924d8a7e3a7Sitojun 	return (error);
1925d8a7e3a7Sitojun }
1926