nd6.c revision 1.274 1 1.274 roy /* $NetBSD: nd6.c,v 1.274 2020/09/15 10:05:36 roy Exp $ */
2 1.65 itojun /* $KAME: nd6.c,v 1.279 2002/06/08 11:16:51 itojun Exp $ */
3 1.4 thorpej
4 1.2 itojun /*
5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.2 itojun * All rights reserved.
7 1.25 itojun *
8 1.2 itojun * Redistribution and use in source and binary forms, with or without
9 1.2 itojun * modification, are permitted provided that the following conditions
10 1.2 itojun * are met:
11 1.2 itojun * 1. Redistributions of source code must retain the above copyright
12 1.2 itojun * notice, this list of conditions and the following disclaimer.
13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 itojun * notice, this list of conditions and the following disclaimer in the
15 1.2 itojun * documentation and/or other materials provided with the distribution.
16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.2 itojun * may be used to endorse or promote products derived from this software
18 1.2 itojun * without specific prior written permission.
19 1.25 itojun *
20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2 itojun * SUCH DAMAGE.
31 1.2 itojun */
32 1.2 itojun
33 1.55 lukem #include <sys/cdefs.h>
34 1.274 roy __KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.274 2020/09/15 10:05:36 roy Exp $");
35 1.162 ozaki
36 1.169 pooka #ifdef _KERNEL_OPT
37 1.271 roy #include "opt_compat_netbsd.h"
38 1.162 ozaki #include "opt_net_mpsafe.h"
39 1.169 pooka #endif
40 1.87 itojun
41 1.153 roy #include "bridge.h"
42 1.153 roy #include "carp.h"
43 1.2 itojun
44 1.2 itojun #include <sys/param.h>
45 1.2 itojun #include <sys/systm.h>
46 1.20 thorpej #include <sys/callout.h>
47 1.229 ozaki #include <sys/kmem.h>
48 1.2 itojun #include <sys/mbuf.h>
49 1.2 itojun #include <sys/socket.h>
50 1.126 ad #include <sys/socketvar.h>
51 1.2 itojun #include <sys/sockio.h>
52 1.2 itojun #include <sys/time.h>
53 1.2 itojun #include <sys/kernel.h>
54 1.2 itojun #include <sys/errno.h>
55 1.2 itojun #include <sys/ioctl.h>
56 1.2 itojun #include <sys/syslog.h>
57 1.2 itojun #include <sys/queue.h>
58 1.138 tls #include <sys/cprng.h>
59 1.203 ozaki #include <sys/workqueue.h>
60 1.2 itojun
61 1.2 itojun #include <net/if.h>
62 1.2 itojun #include <net/if_dl.h>
63 1.181 ozaki #include <net/if_llatbl.h>
64 1.2 itojun #include <net/if_types.h>
65 1.272 roy #include <net/nd.h>
66 1.2 itojun #include <net/route.h>
67 1.62 itojun #include <net/if_ether.h>
68 1.62 itojun #include <net/if_arc.h>
69 1.2 itojun
70 1.2 itojun #include <netinet/in.h>
71 1.2 itojun #include <netinet6/in6_var.h>
72 1.17 itojun #include <netinet/ip6.h>
73 1.2 itojun #include <netinet6/ip6_var.h>
74 1.96 rpaulo #include <netinet6/scope6_var.h>
75 1.2 itojun #include <netinet6/nd6.h>
76 1.151 roy #include <netinet6/in6_ifattach.h>
77 1.17 itojun #include <netinet/icmp6.h>
78 1.125 thorpej #include <netinet6/icmp6_private.h>
79 1.87 itojun
80 1.271 roy #ifdef COMPAT_90
81 1.271 roy #include <compat/netinet6/in6_var.h>
82 1.271 roy #include <compat/netinet6/nd6.h>
83 1.271 roy #endif
84 1.271 roy
85 1.2 itojun #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
86 1.2 itojun #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
87 1.2 itojun
88 1.2 itojun /* timer values */
89 1.2 itojun int nd6_prune = 1; /* walk list every 1 seconds */
90 1.2 itojun int nd6_useloopback = 1; /* use loopback interface for local traffic */
91 1.2 itojun
92 1.12 itojun /* preventing too many loops in ND option parsing */
93 1.12 itojun int nd6_maxndopt = 10; /* max # of ND options allowed */
94 1.12 itojun
95 1.36 itojun #ifdef ND6_DEBUG
96 1.36 itojun int nd6_debug = 1;
97 1.36 itojun #else
98 1.36 itojun int nd6_debug = 0;
99 1.36 itojun #endif
100 1.36 itojun
101 1.220 ozaki krwlock_t nd6_lock __cacheline_aligned;
102 1.220 ozaki
103 1.2 itojun int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL;
104 1.2 itojun
105 1.121 dyoung static void nd6_slowtimo(void *);
106 1.188 ozaki static void nd6_free(struct llentry *, int);
107 1.272 roy static bool nd6_nud_enabled(struct ifnet *);
108 1.272 roy static unsigned int nd6_llinfo_reachable(struct ifnet *);
109 1.272 roy static unsigned int nd6_llinfo_retrans(struct ifnet *);
110 1.273 roy static union l3addr *nd6_llinfo_holdsrc(struct llentry *, union l3addr *);
111 1.273 roy static void nd6_llinfo_output(struct ifnet *, const union l3addr *,
112 1.273 roy const union l3addr *, const uint8_t *, const union l3addr *);
113 1.273 roy static void nd6_llinfo_missed(struct ifnet *, const union l3addr *,
114 1.274 roy int16_t, struct mbuf *);
115 1.186 ozaki static void nd6_timer(void *);
116 1.203 ozaki static void nd6_timer_work(struct work *, void *);
117 1.217 ozaki static struct nd_opt_hdr *nd6_option(union nd_opts *);
118 1.2 itojun
119 1.186 ozaki static callout_t nd6_slowtimo_ch;
120 1.186 ozaki static callout_t nd6_timer_ch;
121 1.203 ozaki static struct workqueue *nd6_timer_wq;
122 1.203 ozaki static struct work nd6_timer_wk;
123 1.20 thorpej
124 1.272 roy struct nd_domain nd6_nd_domain = {
125 1.272 roy .nd_family = AF_INET6,
126 1.272 roy .nd_delay = 5, /* delay first probe time 5 second */
127 1.272 roy .nd_mmaxtries = 3, /* maximum unicast query */
128 1.272 roy .nd_umaxtries = 3, /* maximum multicast query */
129 1.274 roy .nd_retransmultiple = BACKOFF_MULTIPLE,
130 1.274 roy .nd_maxretrans = MAX_RETRANS_TIMER,
131 1.272 roy .nd_maxnudhint = 0, /* max # of subsequent upper layer hints */
132 1.272 roy .nd_maxqueuelen = 1, /* max # of packets in unresolved ND entries */
133 1.272 roy .nd_nud_enabled = nd6_nud_enabled,
134 1.272 roy .nd_reachable = nd6_llinfo_reachable,
135 1.272 roy .nd_retrans = nd6_llinfo_retrans,
136 1.272 roy .nd_holdsrc = nd6_llinfo_holdsrc,
137 1.272 roy .nd_output = nd6_llinfo_output,
138 1.272 roy .nd_missed = nd6_llinfo_missed,
139 1.272 roy .nd_free = nd6_free,
140 1.272 roy };
141 1.272 roy
142 1.79 thorpej MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery");
143 1.65 itojun
144 1.2 itojun void
145 1.112 dyoung nd6_init(void)
146 1.2 itojun {
147 1.203 ozaki int error;
148 1.12 itojun
149 1.272 roy nd_attach_domain(&nd6_nd_domain);
150 1.264 ozaki nd6_nbr_init();
151 1.264 ozaki
152 1.220 ozaki rw_init(&nd6_lock);
153 1.220 ozaki
154 1.126 ad callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE);
155 1.126 ad callout_init(&nd6_timer_ch, CALLOUT_MPSAFE);
156 1.116 ad
157 1.203 ozaki error = workqueue_create(&nd6_timer_wq, "nd6_timer",
158 1.203 ozaki nd6_timer_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
159 1.203 ozaki if (error)
160 1.203 ozaki panic("%s: workqueue_create failed (%d)\n", __func__, error);
161 1.203 ozaki
162 1.2 itojun /* start timer */
163 1.20 thorpej callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
164 1.20 thorpej nd6_slowtimo, NULL);
165 1.186 ozaki callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL);
166 1.2 itojun }
167 1.2 itojun
168 1.271 roy struct nd_kifinfo *
169 1.112 dyoung nd6_ifattach(struct ifnet *ifp)
170 1.2 itojun {
171 1.271 roy struct nd_kifinfo *nd;
172 1.2 itojun
173 1.229 ozaki nd = kmem_zalloc(sizeof(*nd), KM_SLEEP);
174 1.2 itojun
175 1.58 itojun nd->chlim = IPV6_DEFHLIM;
176 1.58 itojun nd->basereachable = REACHABLE_TIME;
177 1.58 itojun nd->reachable = ND_COMPUTE_RTIME(nd->basereachable);
178 1.58 itojun nd->retrans = RETRANS_TIMER;
179 1.151 roy
180 1.271 roy nd->flags = ND6_IFF_PERFORMNUD;
181 1.61 itojun
182 1.151 roy /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
183 1.151 roy * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL
184 1.154 snj * because one of its members should. */
185 1.151 roy if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) ||
186 1.151 roy (ifp->if_flags & IFF_LOOPBACK))
187 1.151 roy nd->flags |= ND6_IFF_AUTO_LINKLOCAL;
188 1.151 roy
189 1.58 itojun return nd;
190 1.58 itojun }
191 1.21 itojun
192 1.58 itojun void
193 1.158 martin nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext)
194 1.58 itojun {
195 1.21 itojun
196 1.219 ozaki /* Ensure all IPv6 addresses are purged before calling nd6_purge */
197 1.219 ozaki if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr);
198 1.158 martin nd6_purge(ifp, ext);
199 1.271 roy kmem_free(ext->nd_ifinfo, sizeof(struct nd_kifinfo));
200 1.2 itojun }
201 1.2 itojun
202 1.2 itojun void
203 1.112 dyoung nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
204 1.2 itojun {
205 1.58 itojun
206 1.129 dyoung memset(ndopts, 0, sizeof(*ndopts));
207 1.2 itojun ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
208 1.2 itojun ndopts->nd_opts_last
209 1.2 itojun = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
210 1.2 itojun
211 1.2 itojun if (icmp6len == 0) {
212 1.2 itojun ndopts->nd_opts_done = 1;
213 1.2 itojun ndopts->nd_opts_search = NULL;
214 1.2 itojun }
215 1.2 itojun }
216 1.2 itojun
217 1.2 itojun /*
218 1.2 itojun * Take one ND option.
219 1.2 itojun */
220 1.217 ozaki static struct nd_opt_hdr *
221 1.112 dyoung nd6_option(union nd_opts *ndopts)
222 1.2 itojun {
223 1.2 itojun struct nd_opt_hdr *nd_opt;
224 1.2 itojun int olen;
225 1.2 itojun
226 1.163 ozaki KASSERT(ndopts != NULL);
227 1.163 ozaki KASSERT(ndopts->nd_opts_last != NULL);
228 1.163 ozaki
229 1.99 rpaulo if (ndopts->nd_opts_search == NULL)
230 1.2 itojun return NULL;
231 1.2 itojun if (ndopts->nd_opts_done)
232 1.2 itojun return NULL;
233 1.2 itojun
234 1.2 itojun nd_opt = ndopts->nd_opts_search;
235 1.2 itojun
236 1.40 itojun /* make sure nd_opt_len is inside the buffer */
237 1.111 christos if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) {
238 1.129 dyoung memset(ndopts, 0, sizeof(*ndopts));
239 1.40 itojun return NULL;
240 1.40 itojun }
241 1.40 itojun
242 1.2 itojun olen = nd_opt->nd_opt_len << 3;
243 1.2 itojun if (olen == 0) {
244 1.2 itojun /*
245 1.2 itojun * Message validation requires that all included
246 1.2 itojun * options have a length that is greater than zero.
247 1.2 itojun */
248 1.129 dyoung memset(ndopts, 0, sizeof(*ndopts));
249 1.2 itojun return NULL;
250 1.2 itojun }
251 1.2 itojun
252 1.111 christos ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen);
253 1.40 itojun if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
254 1.40 itojun /* option overruns the end of buffer, invalid */
255 1.129 dyoung memset(ndopts, 0, sizeof(*ndopts));
256 1.40 itojun return NULL;
257 1.40 itojun } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
258 1.40 itojun /* reached the end of options chain */
259 1.2 itojun ndopts->nd_opts_done = 1;
260 1.2 itojun ndopts->nd_opts_search = NULL;
261 1.2 itojun }
262 1.2 itojun return nd_opt;
263 1.2 itojun }
264 1.2 itojun
265 1.2 itojun /*
266 1.2 itojun * Parse multiple ND options.
267 1.2 itojun * This function is much easier to use, for ND routines that do not need
268 1.2 itojun * multiple options of the same type.
269 1.2 itojun */
270 1.2 itojun int
271 1.112 dyoung nd6_options(union nd_opts *ndopts)
272 1.2 itojun {
273 1.2 itojun struct nd_opt_hdr *nd_opt;
274 1.2 itojun int i = 0;
275 1.2 itojun
276 1.163 ozaki KASSERT(ndopts != NULL);
277 1.163 ozaki KASSERT(ndopts->nd_opts_last != NULL);
278 1.163 ozaki
279 1.99 rpaulo if (ndopts->nd_opts_search == NULL)
280 1.2 itojun return 0;
281 1.99 rpaulo
282 1.2 itojun while (1) {
283 1.2 itojun nd_opt = nd6_option(ndopts);
284 1.99 rpaulo if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
285 1.2 itojun /*
286 1.2 itojun * Message validation requires that all included
287 1.2 itojun * options have a length that is greater than zero.
288 1.2 itojun */
289 1.125 thorpej ICMP6_STATINC(ICMP6_STAT_ND_BADOPT);
290 1.129 dyoung memset(ndopts, 0, sizeof(*ndopts));
291 1.2 itojun return -1;
292 1.2 itojun }
293 1.2 itojun
294 1.99 rpaulo if (nd_opt == NULL)
295 1.2 itojun goto skip1;
296 1.2 itojun
297 1.2 itojun switch (nd_opt->nd_opt_type) {
298 1.2 itojun case ND_OPT_SOURCE_LINKADDR:
299 1.2 itojun case ND_OPT_TARGET_LINKADDR:
300 1.2 itojun case ND_OPT_MTU:
301 1.2 itojun case ND_OPT_REDIRECTED_HEADER:
302 1.247 roy case ND_OPT_NONCE:
303 1.2 itojun if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
304 1.187 ozaki nd6log(LOG_INFO,
305 1.36 itojun "duplicated ND6 option found (type=%d)\n",
306 1.187 ozaki nd_opt->nd_opt_type);
307 1.2 itojun /* XXX bark? */
308 1.2 itojun } else {
309 1.2 itojun ndopts->nd_opt_array[nd_opt->nd_opt_type]
310 1.2 itojun = nd_opt;
311 1.2 itojun }
312 1.2 itojun break;
313 1.2 itojun case ND_OPT_PREFIX_INFORMATION:
314 1.2 itojun if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
315 1.2 itojun ndopts->nd_opt_array[nd_opt->nd_opt_type]
316 1.2 itojun = nd_opt;
317 1.2 itojun }
318 1.2 itojun ndopts->nd_opts_pi_end =
319 1.2 itojun (struct nd_opt_prefix_info *)nd_opt;
320 1.2 itojun break;
321 1.2 itojun default:
322 1.2 itojun /*
323 1.2 itojun * Unknown options must be silently ignored,
324 1.109 christos * to accommodate future extension to the protocol.
325 1.2 itojun */
326 1.187 ozaki nd6log(LOG_DEBUG,
327 1.2 itojun "nd6_options: unsupported option %d - "
328 1.187 ozaki "option ignored\n", nd_opt->nd_opt_type);
329 1.2 itojun }
330 1.2 itojun
331 1.2 itojun skip1:
332 1.2 itojun i++;
333 1.12 itojun if (i > nd6_maxndopt) {
334 1.125 thorpej ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT);
335 1.187 ozaki nd6log(LOG_INFO, "too many loop in nd opt\n");
336 1.2 itojun break;
337 1.2 itojun }
338 1.2 itojun
339 1.2 itojun if (ndopts->nd_opts_done)
340 1.2 itojun break;
341 1.2 itojun }
342 1.2 itojun
343 1.2 itojun return 0;
344 1.2 itojun }
345 1.2 itojun
346 1.2 itojun /*
347 1.179 ozaki * Gets source address of the first packet in hold queue
348 1.179 ozaki * and stores it in @src.
349 1.179 ozaki * Returns pointer to @src (if hold queue is not empty) or NULL.
350 1.179 ozaki */
351 1.179 ozaki static struct in6_addr *
352 1.181 ozaki nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
353 1.179 ozaki {
354 1.179 ozaki struct ip6_hdr *hip6;
355 1.179 ozaki
356 1.179 ozaki if (ln == NULL || ln->ln_hold == NULL)
357 1.179 ozaki return NULL;
358 1.179 ozaki
359 1.179 ozaki /*
360 1.179 ozaki * assuming every packet in ln_hold has the same IP header
361 1.179 ozaki */
362 1.179 ozaki hip6 = mtod(ln->ln_hold, struct ip6_hdr *);
363 1.179 ozaki /* XXX pullup? */
364 1.179 ozaki if (sizeof(*hip6) < ln->ln_hold->m_len)
365 1.179 ozaki *src = hip6->ip6_src;
366 1.179 ozaki else
367 1.179 ozaki src = NULL;
368 1.179 ozaki
369 1.179 ozaki return src;
370 1.179 ozaki }
371 1.179 ozaki
372 1.273 roy static union l3addr *
373 1.273 roy nd6_llinfo_holdsrc(struct llentry *ln, union l3addr *src)
374 1.2 itojun {
375 1.63 itojun
376 1.273 roy if (nd6_llinfo_get_holdsrc(ln, &src->addr6) == NULL)
377 1.272 roy return NULL;
378 1.272 roy return src;
379 1.272 roy }
380 1.246 ozaki
381 1.272 roy static void
382 1.273 roy nd6_llinfo_output(struct ifnet *ifp, const union l3addr *daddr,
383 1.273 roy const union l3addr *taddr, __unused const uint8_t *tlladdr,
384 1.273 roy const union l3addr *hsrc)
385 1.272 roy {
386 1.86 itojun
387 1.273 roy nd6_ns_output(ifp, &daddr->addr6, &taddr->addr6,
388 1.273 roy &hsrc->addr6, NULL);
389 1.272 roy }
390 1.173 ozaki
391 1.272 roy static bool
392 1.272 roy nd6_nud_enabled(struct ifnet *ifp)
393 1.272 roy {
394 1.272 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
395 1.86 itojun
396 1.272 roy return ndi->flags & ND6_IFF_PERFORMNUD;
397 1.272 roy }
398 1.263 roy
399 1.272 roy static unsigned int
400 1.272 roy nd6_llinfo_reachable(struct ifnet *ifp)
401 1.272 roy {
402 1.272 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
403 1.262 roy
404 1.272 roy return ndi->reachable;
405 1.272 roy }
406 1.269 roy
407 1.272 roy static unsigned int
408 1.272 roy nd6_llinfo_retrans(struct ifnet *ifp)
409 1.272 roy {
410 1.272 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
411 1.268 christos
412 1.272 roy return ndi->retrans;
413 1.272 roy }
414 1.262 roy
415 1.272 roy static void
416 1.274 roy nd6_llinfo_missed(struct ifnet *ifp, const union l3addr *taddr,
417 1.274 roy int16_t type, struct mbuf *m)
418 1.272 roy {
419 1.272 roy struct in6_addr mdaddr6 = zeroin6_addr;
420 1.272 roy struct sockaddr_in6 dsin6, tsin6;
421 1.272 roy struct sockaddr *sa;
422 1.262 roy
423 1.274 roy if (m != NULL) {
424 1.274 roy if (type == ND_LLINFO_PROBE) {
425 1.274 roy struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
426 1.274 roy
427 1.274 roy /* XXX pullup? */
428 1.274 roy if (sizeof(*ip6) < m->m_len)
429 1.274 roy mdaddr6 = ip6->ip6_src;
430 1.274 roy m_freem(m);
431 1.274 roy } else
432 1.274 roy icmp6_error2(m, ICMP6_DST_UNREACH,
433 1.274 roy ICMP6_DST_UNREACH_ADDR, 0, ifp, &mdaddr6);
434 1.274 roy }
435 1.272 roy if (!IN6_IS_ADDR_UNSPECIFIED(&mdaddr6)) {
436 1.272 roy sockaddr_in6_init(&dsin6, &mdaddr6, 0, 0, 0);
437 1.272 roy sa = sin6tosa(&dsin6);
438 1.272 roy } else
439 1.272 roy sa = NULL;
440 1.2 itojun
441 1.273 roy sockaddr_in6_init(&tsin6, &taddr->addr6, 0, 0, 0);
442 1.272 roy rt_clonedmsg(RTM_MISS, sa, sin6tosa(&tsin6), NULL, ifp);
443 1.86 itojun }
444 1.42 itojun
445 1.86 itojun /*
446 1.86 itojun * ND6 timer routine to expire default route list and prefix list
447 1.86 itojun */
448 1.186 ozaki static void
449 1.203 ozaki nd6_timer_work(struct work *wk, void *arg)
450 1.86 itojun {
451 1.86 itojun struct in6_ifaddr *ia6, *nia6;
452 1.205 ozaki int s, bound;
453 1.205 ozaki struct psref psref;
454 1.42 itojun
455 1.86 itojun callout_reset(&nd6_timer_ch, nd6_prune * hz,
456 1.86 itojun nd6_timer, NULL);
457 1.63 itojun
458 1.239 ozaki SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
459 1.126 ad
460 1.271 roy /* expire interface addresses */
461 1.205 ozaki bound = curlwp_bind();
462 1.205 ozaki s = pserialize_read_enter();
463 1.205 ozaki for (ia6 = IN6_ADDRLIST_READER_FIRST(); ia6; ia6 = nia6) {
464 1.205 ozaki nia6 = IN6_ADDRLIST_READER_NEXT(ia6);
465 1.205 ozaki
466 1.205 ozaki ia6_acquire(ia6, &psref);
467 1.205 ozaki pserialize_read_exit(s);
468 1.205 ozaki
469 1.65 itojun /* check address lifetime */
470 1.65 itojun if (IFA6_IS_INVALID(ia6)) {
471 1.240 ozaki struct ifnet *ifp;
472 1.99 rpaulo
473 1.240 ozaki ifp = ia6->ia_ifa.ifa_ifp;
474 1.240 ozaki IFNET_LOCK(ifp);
475 1.240 ozaki /*
476 1.240 ozaki * Need to take the lock first to prevent if_detach
477 1.240 ozaki * from running in6_purgeaddr concurrently.
478 1.240 ozaki */
479 1.240 ozaki if (!if_is_deactivated(ifp)) {
480 1.240 ozaki ia6_release(ia6, &psref);
481 1.240 ozaki in6_purgeaddr(&ia6->ia_ifa);
482 1.240 ozaki } else {
483 1.240 ozaki /*
484 1.240 ozaki * ifp is being destroyed, ia6 will be destroyed
485 1.240 ozaki * by if_detach.
486 1.240 ozaki */
487 1.240 ozaki ia6_release(ia6, &psref);
488 1.240 ozaki }
489 1.205 ozaki ia6 = NULL;
490 1.240 ozaki IFNET_UNLOCK(ifp);
491 1.99 rpaulo } else if (IFA6_IS_DEPRECATED(ia6)) {
492 1.99 rpaulo int oldflags = ia6->ia6_flags;
493 1.99 rpaulo
494 1.145 roy if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
495 1.145 roy ia6->ia6_flags |= IN6_IFF_DEPRECATED;
496 1.253 roy rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
497 1.145 roy }
498 1.65 itojun } else {
499 1.65 itojun /*
500 1.65 itojun * A new RA might have made a deprecated address
501 1.65 itojun * preferred.
502 1.65 itojun */
503 1.145 roy if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
504 1.145 roy ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
505 1.253 roy rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
506 1.145 roy }
507 1.2 itojun }
508 1.205 ozaki s = pserialize_read_enter();
509 1.205 ozaki ia6_release(ia6, &psref);
510 1.65 itojun }
511 1.205 ozaki pserialize_read_exit(s);
512 1.205 ozaki curlwp_bindx(bound);
513 1.2 itojun
514 1.239 ozaki SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
515 1.16 itojun }
516 1.16 itojun
517 1.203 ozaki static void
518 1.203 ozaki nd6_timer(void *ignored_arg)
519 1.203 ozaki {
520 1.203 ozaki
521 1.203 ozaki workqueue_enqueue(nd6_timer_wq, &nd6_timer_wk, NULL);
522 1.203 ozaki }
523 1.203 ozaki
524 1.16 itojun /*
525 1.16 itojun * Nuke neighbor cache/prefix/default router management table, right before
526 1.16 itojun * ifp goes away.
527 1.16 itojun */
528 1.16 itojun void
529 1.158 martin nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext)
530 1.16 itojun {
531 1.16 itojun
532 1.65 itojun /*
533 1.158 martin * During detach, the ND info might be already removed, but
534 1.158 martin * then is explitly passed as argument.
535 1.158 martin * Otherwise get it from ifp->if_afdata.
536 1.158 martin */
537 1.158 martin if (ext == NULL)
538 1.158 martin ext = ifp->if_afdata[AF_INET6];
539 1.158 martin if (ext == NULL)
540 1.158 martin return;
541 1.158 martin
542 1.16 itojun /*
543 1.181 ozaki * We may not need to nuke the neighbor cache entries here
544 1.181 ozaki * because the neighbor cache is kept in if_afdata[AF_INET6].
545 1.181 ozaki * nd6_purge() is invoked by in6_ifdetach() which is called
546 1.181 ozaki * from if_detach() where everything gets purged. However
547 1.181 ozaki * in6_ifdetach is directly called from vlan(4), so we still
548 1.181 ozaki * need to purge entries here.
549 1.16 itojun */
550 1.181 ozaki if (ext->lltable != NULL)
551 1.181 ozaki lltable_purge_entries(ext->lltable);
552 1.2 itojun }
553 1.2 itojun
554 1.188 ozaki struct llentry *
555 1.188 ozaki nd6_lookup(const struct in6_addr *addr6, const struct ifnet *ifp, bool wlock)
556 1.188 ozaki {
557 1.188 ozaki struct sockaddr_in6 sin6;
558 1.188 ozaki struct llentry *ln;
559 1.188 ozaki
560 1.188 ozaki sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
561 1.188 ozaki
562 1.188 ozaki IF_AFDATA_RLOCK(ifp);
563 1.188 ozaki ln = lla_lookup(LLTABLE6(ifp), wlock ? LLE_EXCLUSIVE : 0,
564 1.188 ozaki sin6tosa(&sin6));
565 1.188 ozaki IF_AFDATA_RUNLOCK(ifp);
566 1.188 ozaki
567 1.188 ozaki return ln;
568 1.188 ozaki }
569 1.188 ozaki
570 1.188 ozaki struct llentry *
571 1.188 ozaki nd6_create(const struct in6_addr *addr6, const struct ifnet *ifp)
572 1.2 itojun {
573 1.2 itojun struct sockaddr_in6 sin6;
574 1.188 ozaki struct llentry *ln;
575 1.237 ozaki struct rtentry *rt;
576 1.2 itojun
577 1.122 dyoung sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
578 1.237 ozaki rt = rtalloc1(sin6tosa(&sin6), 0);
579 1.188 ozaki
580 1.188 ozaki IF_AFDATA_WLOCK(ifp);
581 1.237 ozaki ln = lla_create(LLTABLE6(ifp), LLE_EXCLUSIVE, sin6tosa(&sin6), rt);
582 1.188 ozaki IF_AFDATA_WUNLOCK(ifp);
583 1.188 ozaki
584 1.237 ozaki if (rt != NULL)
585 1.237 ozaki rt_unref(rt);
586 1.188 ozaki if (ln != NULL)
587 1.272 roy ln->ln_state = ND_LLINFO_NOSTATE;
588 1.188 ozaki
589 1.188 ozaki return ln;
590 1.188 ozaki }
591 1.188 ozaki
592 1.188 ozaki /*
593 1.188 ozaki * Test whether a given IPv6 address is a neighbor or not, ignoring
594 1.188 ozaki * the actual neighbor cache. The neighbor cache is ignored in order
595 1.188 ozaki * to not reenter the routing code from within itself.
596 1.188 ozaki */
597 1.188 ozaki static int
598 1.188 ozaki nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
599 1.188 ozaki {
600 1.188 ozaki struct ifaddr *dstaddr;
601 1.205 ozaki int s;
602 1.188 ozaki
603 1.188 ozaki /*
604 1.188 ozaki * A link-local address is always a neighbor.
605 1.188 ozaki * XXX: a link does not necessarily specify a single interface.
606 1.188 ozaki */
607 1.188 ozaki if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
608 1.188 ozaki struct sockaddr_in6 sin6_copy;
609 1.188 ozaki u_int32_t zone;
610 1.188 ozaki
611 1.2 itojun /*
612 1.188 ozaki * We need sin6_copy since sa6_recoverscope() may modify the
613 1.188 ozaki * content (XXX).
614 1.188 ozaki */
615 1.188 ozaki sin6_copy = *addr;
616 1.188 ozaki if (sa6_recoverscope(&sin6_copy))
617 1.188 ozaki return 0; /* XXX: should be impossible */
618 1.188 ozaki if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
619 1.188 ozaki return 0;
620 1.188 ozaki if (sin6_copy.sin6_scope_id == zone)
621 1.188 ozaki return 1;
622 1.188 ozaki else
623 1.188 ozaki return 0;
624 1.188 ozaki }
625 1.188 ozaki
626 1.188 ozaki /*
627 1.188 ozaki * If the address is assigned on the node of the other side of
628 1.188 ozaki * a p2p interface, the address should be a neighbor.
629 1.188 ozaki */
630 1.205 ozaki s = pserialize_read_enter();
631 1.204 ozaki dstaddr = ifa_ifwithdstaddr(sin6tocsa(addr));
632 1.188 ozaki if (dstaddr != NULL) {
633 1.188 ozaki if (dstaddr->ifa_ifp == ifp) {
634 1.205 ozaki pserialize_read_exit(s);
635 1.188 ozaki return 1;
636 1.188 ozaki }
637 1.188 ozaki }
638 1.205 ozaki pserialize_read_exit(s);
639 1.147 roy
640 1.188 ozaki return 0;
641 1.147 roy }
642 1.147 roy
643 1.6 itojun /*
644 1.6 itojun * Detect if a given IPv6 address identifies a neighbor on a given link.
645 1.25 itojun * XXX: should take care of the destination of a p2p link?
646 1.6 itojun */
647 1.6 itojun int
648 1.110 dyoung nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
649 1.6 itojun {
650 1.188 ozaki struct llentry *ln;
651 1.165 ozaki struct rtentry *rt;
652 1.6 itojun
653 1.29 itojun /*
654 1.29 itojun * A link-local address is always a neighbor.
655 1.52 itojun * XXX: a link does not necessarily specify a single interface.
656 1.29 itojun */
657 1.96 rpaulo if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
658 1.96 rpaulo struct sockaddr_in6 sin6_copy;
659 1.96 rpaulo u_int32_t zone;
660 1.96 rpaulo
661 1.96 rpaulo /*
662 1.96 rpaulo * We need sin6_copy since sa6_recoverscope() may modify the
663 1.96 rpaulo * content (XXX).
664 1.96 rpaulo */
665 1.96 rpaulo sin6_copy = *addr;
666 1.96 rpaulo if (sa6_recoverscope(&sin6_copy))
667 1.112 dyoung return 0; /* XXX: should be impossible */
668 1.96 rpaulo if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
669 1.112 dyoung return 0;
670 1.96 rpaulo if (sin6_copy.sin6_scope_id == zone)
671 1.112 dyoung return 1;
672 1.96 rpaulo else
673 1.112 dyoung return 0;
674 1.96 rpaulo }
675 1.6 itojun
676 1.188 ozaki if (nd6_is_new_addr_neighbor(addr, ifp))
677 1.188 ozaki return 1;
678 1.188 ozaki
679 1.6 itojun /*
680 1.188 ozaki * Even if the address matches none of our addresses, it might be
681 1.188 ozaki * in the neighbor cache or a connected route.
682 1.6 itojun */
683 1.188 ozaki ln = nd6_lookup(&addr->sin6_addr, ifp, false);
684 1.188 ozaki if (ln != NULL) {
685 1.188 ozaki LLE_RUNLOCK(ln);
686 1.188 ozaki return 1;
687 1.188 ozaki }
688 1.188 ozaki
689 1.188 ozaki rt = rtalloc1(sin6tocsa(addr), 0);
690 1.188 ozaki if (rt == NULL)
691 1.188 ozaki return 0;
692 1.188 ozaki
693 1.188 ozaki if ((rt->rt_flags & RTF_CONNECTED) && (rt->rt_ifp == ifp
694 1.188 ozaki #if NBRIDGE > 0
695 1.188 ozaki || rt->rt_ifp->if_bridge == ifp->if_bridge
696 1.188 ozaki #endif
697 1.188 ozaki #if NCARP > 0
698 1.188 ozaki || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) ||
699 1.188 ozaki (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)||
700 1.188 ozaki (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP &&
701 1.188 ozaki rt->rt_ifp->if_carpdev == ifp->if_carpdev)
702 1.188 ozaki #endif
703 1.188 ozaki )) {
704 1.216 ozaki rt_unref(rt);
705 1.112 dyoung return 1;
706 1.165 ozaki }
707 1.216 ozaki rt_unref(rt);
708 1.6 itojun
709 1.112 dyoung return 0;
710 1.2 itojun }
711 1.2 itojun
712 1.2 itojun /*
713 1.2 itojun * Free an nd6 llinfo entry.
714 1.54 itojun * Since the function would cause significant changes in the kernel, DO NOT
715 1.54 itojun * make it global, unless you have a strong reason for the change, and are sure
716 1.54 itojun * that the change is safe.
717 1.2 itojun */
718 1.181 ozaki static void
719 1.188 ozaki nd6_free(struct llentry *ln, int gc)
720 1.2 itojun {
721 1.188 ozaki struct ifnet *ifp;
722 1.2 itojun
723 1.181 ozaki KASSERT(ln != NULL);
724 1.181 ozaki LLE_WLOCK_ASSERT(ln);
725 1.181 ozaki
726 1.18 itojun /*
727 1.271 roy * If the reason for the deletion is just garbage collection,
728 1.271 roy * and the neighbor is an active router, do not delete it.
729 1.271 roy * Instead, reset the GC timer using the router's lifetime.
730 1.271 roy * XXX: the check for ln_state should be redundant,
731 1.271 roy * but we intentionally keep it just in case.
732 1.18 itojun */
733 1.271 roy if (!ip6_forwarding && ln->ln_router &&
734 1.272 roy ln->ln_state == ND_LLINFO_STALE && gc)
735 1.271 roy {
736 1.272 roy nd_set_timer(ln, ND_TIMER_EXPIRE);
737 1.260 roy LLE_WUNLOCK(ln);
738 1.271 roy return;
739 1.271 roy }
740 1.12 itojun
741 1.271 roy ifp = ln->lle_tbl->llt_ifp;
742 1.12 itojun
743 1.262 roy if (ln->la_flags & LLE_VALID || gc) {
744 1.262 roy struct sockaddr_in6 sin6;
745 1.262 roy const char *lladdr;
746 1.262 roy
747 1.271 roy sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
748 1.262 roy lladdr = ln->la_flags & LLE_VALID ?
749 1.262 roy (const char *)&ln->ll_addr : NULL;
750 1.267 roy rt_clonedmsg(RTM_DELETE, NULL, sin6tosa(&sin6), lladdr, ifp);
751 1.262 roy }
752 1.259 roy
753 1.188 ozaki /*
754 1.188 ozaki * Save to unlock. We still hold an extra reference and will not
755 1.188 ozaki * free(9) in llentry_free() if someone else holds one as well.
756 1.188 ozaki */
757 1.181 ozaki LLE_WUNLOCK(ln);
758 1.188 ozaki IF_AFDATA_LOCK(ifp);
759 1.188 ozaki LLE_WLOCK(ln);
760 1.188 ozaki
761 1.222 ozaki lltable_free_entry(LLTABLE6(ifp), ln);
762 1.188 ozaki
763 1.188 ozaki IF_AFDATA_UNLOCK(ifp);
764 1.2 itojun }
765 1.2 itojun
766 1.2 itojun /*
767 1.2 itojun * Upper-layer reachability hint for Neighbor Unreachability Detection.
768 1.2 itojun *
769 1.98 rpaulo * XXX cost-effective methods?
770 1.2 itojun */
771 1.2 itojun void
772 1.171 ozaki nd6_nud_hint(struct rtentry *rt)
773 1.2 itojun {
774 1.181 ozaki struct llentry *ln;
775 1.188 ozaki struct ifnet *ifp;
776 1.2 itojun
777 1.164 ozaki if (rt == NULL)
778 1.164 ozaki return;
779 1.2 itojun
780 1.188 ozaki ifp = rt->rt_ifp;
781 1.188 ozaki ln = nd6_lookup(&(satocsin6(rt_getkey(rt)))->sin6_addr, ifp, true);
782 1.272 roy nd_nud_hint(ln);
783 1.2 itojun }
784 1.2 itojun
785 1.207 ozaki struct gc_args {
786 1.207 ozaki int gc_entries;
787 1.207 ozaki const struct in6_addr *skip_in6;
788 1.207 ozaki };
789 1.207 ozaki
790 1.181 ozaki static int
791 1.181 ozaki nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg)
792 1.181 ozaki {
793 1.207 ozaki struct gc_args *args = farg;
794 1.207 ozaki int *n = &args->gc_entries;
795 1.207 ozaki const struct in6_addr *skip_in6 = args->skip_in6;
796 1.181 ozaki
797 1.181 ozaki if (*n <= 0)
798 1.181 ozaki return 0;
799 1.181 ozaki
800 1.272 roy if (ND_IS_LLINFO_PERMANENT(ln))
801 1.181 ozaki return 0;
802 1.181 ozaki
803 1.207 ozaki if (IN6_ARE_ADDR_EQUAL(&ln->r_l3addr.addr6, skip_in6))
804 1.207 ozaki return 0;
805 1.207 ozaki
806 1.181 ozaki LLE_WLOCK(ln);
807 1.272 roy if (ln->ln_state > ND_LLINFO_INCOMPLETE)
808 1.272 roy ln->ln_state = ND_LLINFO_STALE;
809 1.181 ozaki else
810 1.272 roy ln->ln_state = ND_LLINFO_PURGE;
811 1.272 roy nd_set_timer(ln, ND_TIMER_IMMEDIATE);
812 1.181 ozaki LLE_WUNLOCK(ln);
813 1.181 ozaki
814 1.181 ozaki (*n)--;
815 1.181 ozaki return 0;
816 1.181 ozaki }
817 1.181 ozaki
818 1.181 ozaki static void
819 1.207 ozaki nd6_gc_neighbors(struct lltable *llt, const struct in6_addr *in6)
820 1.181 ozaki {
821 1.181 ozaki
822 1.181 ozaki if (ip6_neighborgcthresh >= 0 &&
823 1.181 ozaki lltable_get_entry_count(llt) >= ip6_neighborgcthresh) {
824 1.207 ozaki struct gc_args gc_args = {10, in6};
825 1.181 ozaki /*
826 1.181 ozaki * XXX entries that are "less recently used" should be
827 1.181 ozaki * freed first.
828 1.181 ozaki */
829 1.207 ozaki lltable_foreach_lle(llt, nd6_purge_entry, &gc_args);
830 1.181 ozaki }
831 1.181 ozaki }
832 1.181 ozaki
833 1.2 itojun void
834 1.130 dyoung nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
835 1.2 itojun {
836 1.2 itojun struct sockaddr *gate = rt->rt_gateway;
837 1.2 itojun struct ifnet *ifp = rt->rt_ifp;
838 1.119 dyoung uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
839 1.2 itojun struct ifaddr *ifa;
840 1.2 itojun
841 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
842 1.117 dyoung
843 1.131 dyoung if (req == RTM_LLINFO_UPD) {
844 1.131 dyoung int rc;
845 1.131 dyoung struct in6_addr *in6;
846 1.131 dyoung struct in6_addr in6_all;
847 1.131 dyoung int anycast;
848 1.131 dyoung
849 1.131 dyoung if ((ifa = info->rti_ifa) == NULL)
850 1.131 dyoung return;
851 1.131 dyoung
852 1.131 dyoung in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
853 1.131 dyoung anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
854 1.131 dyoung
855 1.131 dyoung in6_all = in6addr_linklocal_allnodes;
856 1.131 dyoung if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
857 1.131 dyoung log(LOG_ERR, "%s: failed to set scope %s "
858 1.131 dyoung "(errno=%d)\n", __func__, if_name(ifp), rc);
859 1.131 dyoung return;
860 1.131 dyoung }
861 1.131 dyoung
862 1.131 dyoung /* XXX don't set Override for proxy addresses */
863 1.131 dyoung nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
864 1.131 dyoung (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
865 1.131 dyoung #if 0
866 1.131 dyoung | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
867 1.131 dyoung #endif
868 1.131 dyoung , 1, NULL);
869 1.131 dyoung return;
870 1.131 dyoung }
871 1.131 dyoung
872 1.270 roy if ((rt->rt_flags & RTF_GATEWAY) != 0) {
873 1.270 roy if (req != RTM_ADD)
874 1.270 roy return;
875 1.270 roy /*
876 1.270 roy * linklayers with particular MTU limitation.
877 1.270 roy */
878 1.270 roy switch(ifp->if_type) {
879 1.270 roy #if NARCNET > 0
880 1.270 roy case IFT_ARCNET:
881 1.270 roy if (rt->rt_rmx.rmx_mtu > ARC_PHDS_MAXMTU) /* RFC2497 */
882 1.270 roy rt->rt_rmx.rmx_mtu = ARC_PHDS_MAXMTU;
883 1.270 roy break;
884 1.270 roy #endif
885 1.270 roy }
886 1.2 itojun return;
887 1.270 roy }
888 1.2 itojun
889 1.54 itojun if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
890 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
891 1.54 itojun /*
892 1.54 itojun * This is probably an interface direct route for a link
893 1.54 itojun * which does not need neighbor caches (e.g. fe80::%lo0/64).
894 1.54 itojun * We do not need special treatment below for such a route.
895 1.54 itojun * Moreover, the RTF_LLINFO flag which would be set below
896 1.54 itojun * would annoy the ndp(8) command.
897 1.54 itojun */
898 1.54 itojun return;
899 1.54 itojun }
900 1.54 itojun
901 1.2 itojun switch (req) {
902 1.205 ozaki case RTM_ADD: {
903 1.238 ozaki struct psref psref;
904 1.205 ozaki
905 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
906 1.2 itojun /*
907 1.2 itojun * There is no backward compatibility :)
908 1.2 itojun *
909 1.2 itojun * if ((rt->rt_flags & RTF_HOST) == 0 &&
910 1.2 itojun * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
911 1.2 itojun * rt->rt_flags |= RTF_CLONING;
912 1.2 itojun */
913 1.188 ozaki /* XXX should move to route.c? */
914 1.188 ozaki if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) {
915 1.120 dyoung union {
916 1.120 dyoung struct sockaddr sa;
917 1.120 dyoung struct sockaddr_dl sdl;
918 1.120 dyoung struct sockaddr_storage ss;
919 1.120 dyoung } u;
920 1.2 itojun /*
921 1.84 itojun * Case 1: This route should come from a route to
922 1.84 itojun * interface (RTF_CLONING case) or the route should be
923 1.84 itojun * treated as on-link but is currently not
924 1.129 dyoung * (RTF_LLINFO && ln == NULL case).
925 1.2 itojun */
926 1.136 dyoung if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
927 1.120 dyoung ifp->if_index, ifp->if_type,
928 1.136 dyoung NULL, namelen, NULL, addrlen) == NULL) {
929 1.136 dyoung printf("%s.%d: sockaddr_dl_init(, %zu, ) "
930 1.136 dyoung "failed on %s\n", __func__, __LINE__,
931 1.136 dyoung sizeof(u.ss), if_name(ifp));
932 1.136 dyoung }
933 1.120 dyoung rt_setgate(rt, &u.sa);
934 1.119 dyoung gate = rt->rt_gateway;
935 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
936 1.192 ozaki if (gate == NULL) {
937 1.192 ozaki log(LOG_ERR,
938 1.192 ozaki "%s: rt_setgate failed on %s\n", __func__,
939 1.192 ozaki if_name(ifp));
940 1.192 ozaki break;
941 1.192 ozaki }
942 1.188 ozaki
943 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
944 1.188 ozaki if ((rt->rt_flags & RTF_CONNECTED) != 0)
945 1.2 itojun break;
946 1.2 itojun }
947 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
948 1.18 itojun /*
949 1.18 itojun * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
950 1.18 itojun * We don't do that here since llinfo is not ready yet.
951 1.18 itojun *
952 1.18 itojun * There are also couple of other things to be discussed:
953 1.18 itojun * - unsolicited NA code needs improvement beforehand
954 1.18 itojun * - RFC2461 says we MAY send multicast unsolicited NA
955 1.18 itojun * (7.2.6 paragraph 4), however, it also says that we
956 1.18 itojun * SHOULD provide a mechanism to prevent multicast NA storm.
957 1.18 itojun * we don't have anything like it right now.
958 1.38 itojun * note that the mechanism needs a mutual agreement
959 1.18 itojun * between proxies, which means that we need to implement
960 1.38 itojun * a new protocol, or a new kludge.
961 1.38 itojun * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
962 1.18 itojun * we need to check ip6forwarding before sending it.
963 1.18 itojun * (or should we allow proxy ND configuration only for
964 1.18 itojun * routers? there's no mention about proxy ND from hosts)
965 1.18 itojun */
966 1.18 itojun #if 0
967 1.18 itojun /* XXX it does not work */
968 1.2 itojun if (rt->rt_flags & RTF_ANNOUNCE)
969 1.2 itojun nd6_na_output(ifp,
970 1.118 dyoung &satocsin6(rt_getkey(rt))->sin6_addr,
971 1.118 dyoung &satocsin6(rt_getkey(rt))->sin6_addr,
972 1.18 itojun ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
973 1.18 itojun 1, NULL);
974 1.18 itojun #endif
975 1.188 ozaki
976 1.65 itojun if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
977 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
978 1.26 itojun /*
979 1.26 itojun * Address resolution isn't necessary for a point to
980 1.26 itojun * point link, so we can skip this test for a p2p link.
981 1.26 itojun */
982 1.26 itojun if (gate->sa_family != AF_LINK ||
983 1.119 dyoung gate->sa_len <
984 1.119 dyoung sockaddr_dl_measure(namelen, addrlen)) {
985 1.26 itojun log(LOG_DEBUG,
986 1.36 itojun "nd6_rtrequest: bad gateway value: %s\n",
987 1.36 itojun if_name(ifp));
988 1.26 itojun break;
989 1.26 itojun }
990 1.118 dyoung satosdl(gate)->sdl_type = ifp->if_type;
991 1.118 dyoung satosdl(gate)->sdl_index = ifp->if_index;
992 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
993 1.2 itojun }
994 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
995 1.2 itojun
996 1.143 christos /*
997 1.198 ozaki * When called from rt_ifa_addlocal, we cannot depend on that
998 1.198 ozaki * the address (rt_getkey(rt)) exits in the address list of the
999 1.198 ozaki * interface. So check RTF_LOCAL instead.
1000 1.198 ozaki */
1001 1.198 ozaki if (rt->rt_flags & RTF_LOCAL) {
1002 1.198 ozaki if (nd6_useloopback)
1003 1.198 ozaki rt->rt_ifp = lo0ifp; /* XXX */
1004 1.198 ozaki break;
1005 1.198 ozaki }
1006 1.198 ozaki
1007 1.198 ozaki /*
1008 1.117 dyoung * check if rt_getkey(rt) is an address assigned
1009 1.2 itojun * to the interface.
1010 1.2 itojun */
1011 1.238 ozaki ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp,
1012 1.238 ozaki &satocsin6(rt_getkey(rt))->sin6_addr, &psref);
1013 1.129 dyoung if (ifa != NULL) {
1014 1.2 itojun if (nd6_useloopback) {
1015 1.195 ozaki rt->rt_ifp = lo0ifp; /* XXX */
1016 1.2 itojun /*
1017 1.2 itojun * Make sure rt_ifa be equal to the ifaddr
1018 1.2 itojun * corresponding to the address.
1019 1.2 itojun * We need this because when we refer
1020 1.2 itojun * rt_ifa->ia6_flags in ip6_input, we assume
1021 1.2 itojun * that the rt_ifa points to the address instead
1022 1.2 itojun * of the loopback address.
1023 1.2 itojun */
1024 1.251 ozaki if (!ISSET(info->rti_flags, RTF_DONTCHANGEIFA)
1025 1.251 ozaki && ifa != rt->rt_ifa)
1026 1.106 dyoung rt_replace_ifa(rt, ifa);
1027 1.2 itojun }
1028 1.18 itojun } else if (rt->rt_flags & RTF_ANNOUNCE) {
1029 1.18 itojun /* join solicited node multicast for proxy ND */
1030 1.18 itojun if (ifp->if_flags & IFF_MULTICAST) {
1031 1.18 itojun struct in6_addr llsol;
1032 1.18 itojun int error;
1033 1.18 itojun
1034 1.118 dyoung llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1035 1.96 rpaulo llsol.s6_addr32[0] = htonl(0xff020000);
1036 1.18 itojun llsol.s6_addr32[1] = 0;
1037 1.18 itojun llsol.s6_addr32[2] = htonl(1);
1038 1.18 itojun llsol.s6_addr8[12] = 0xff;
1039 1.96 rpaulo if (in6_setscope(&llsol, ifp, NULL))
1040 1.188 ozaki goto out;
1041 1.99 rpaulo if (!in6_addmulti(&llsol, ifp, &error, 0)) {
1042 1.225 ryo char ip6buf[INET6_ADDRSTRLEN];
1043 1.187 ozaki nd6log(LOG_ERR, "%s: failed to join "
1044 1.54 itojun "%s (errno=%d)\n", if_name(ifp),
1045 1.226 christos IN6_PRINT(ip6buf, &llsol), error);
1046 1.46 itojun }
1047 1.18 itojun }
1048 1.2 itojun }
1049 1.188 ozaki out:
1050 1.238 ozaki ifa_release(ifa, &psref);
1051 1.181 ozaki /*
1052 1.181 ozaki * If we have too many cache entries, initiate immediate
1053 1.181 ozaki * purging for some entries.
1054 1.181 ozaki */
1055 1.188 ozaki if (rt->rt_ifp != NULL)
1056 1.207 ozaki nd6_gc_neighbors(LLTABLE6(rt->rt_ifp), NULL);
1057 1.2 itojun break;
1058 1.205 ozaki }
1059 1.2 itojun
1060 1.2 itojun case RTM_DELETE:
1061 1.18 itojun /* leave from solicited node multicast for proxy ND */
1062 1.18 itojun if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1063 1.18 itojun (ifp->if_flags & IFF_MULTICAST) != 0) {
1064 1.18 itojun struct in6_addr llsol;
1065 1.18 itojun
1066 1.118 dyoung llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1067 1.96 rpaulo llsol.s6_addr32[0] = htonl(0xff020000);
1068 1.18 itojun llsol.s6_addr32[1] = 0;
1069 1.18 itojun llsol.s6_addr32[2] = htonl(1);
1070 1.18 itojun llsol.s6_addr8[12] = 0xff;
1071 1.249 ozaki if (in6_setscope(&llsol, ifp, NULL) == 0)
1072 1.249 ozaki in6_lookup_and_delete_multi(&llsol, ifp);
1073 1.18 itojun }
1074 1.188 ozaki break;
1075 1.2 itojun }
1076 1.2 itojun }
1077 1.2 itojun
1078 1.271 roy static void
1079 1.271 roy nd6_setifflags(struct ifnet *ifp, uint32_t flags)
1080 1.2 itojun {
1081 1.271 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
1082 1.271 roy struct ifaddr *ifa;
1083 1.271 roy struct in6_ifaddr *ia;
1084 1.271 roy int s;
1085 1.2 itojun
1086 1.271 roy if (ndi->flags & ND6_IFF_IFDISABLED && !(flags & ND6_IFF_IFDISABLED)) {
1087 1.65 itojun /*
1088 1.271 roy * If the interface is marked as ND6_IFF_IFDISABLED and
1089 1.271 roy * has a link-local address with IN6_IFF_DUPLICATED,
1090 1.271 roy * do not clear ND6_IFF_IFDISABLED.
1091 1.271 roy * See RFC 4862, section 5.4.5.
1092 1.65 itojun */
1093 1.271 roy bool duplicated_linklocal = false;
1094 1.271 roy
1095 1.271 roy s = pserialize_read_enter();
1096 1.271 roy IFADDR_READER_FOREACH(ifa, ifp) {
1097 1.271 roy if (ifa->ifa_addr->sa_family != AF_INET6)
1098 1.271 roy continue;
1099 1.271 roy ia = (struct in6_ifaddr *)ifa;
1100 1.271 roy if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1101 1.271 roy IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1102 1.271 roy {
1103 1.271 roy duplicated_linklocal = true;
1104 1.108 dyoung break;
1105 1.271 roy }
1106 1.271 roy }
1107 1.271 roy pserialize_read_exit(s);
1108 1.2 itojun
1109 1.271 roy if (duplicated_linklocal) {
1110 1.271 roy flags |= ND6_IFF_IFDISABLED;
1111 1.271 roy log(LOG_ERR, "%s: Cannot enable an interface"
1112 1.271 roy " with a link-local address marked"
1113 1.271 roy " duplicate.\n", if_name(ifp));
1114 1.271 roy } else {
1115 1.271 roy ndi->flags &= ~ND6_IFF_IFDISABLED;
1116 1.271 roy if (ifp->if_flags & IFF_UP)
1117 1.271 roy in6_if_up(ifp);
1118 1.2 itojun }
1119 1.271 roy } else if (!(ndi->flags & ND6_IFF_IFDISABLED) &&
1120 1.271 roy (flags & ND6_IFF_IFDISABLED))
1121 1.271 roy {
1122 1.271 roy struct psref psref;
1123 1.271 roy int bound = curlwp_bind();
1124 1.271 roy
1125 1.271 roy /* Mark all IPv6 addresses as tentative. */
1126 1.2 itojun
1127 1.271 roy ndi->flags |= ND6_IFF_IFDISABLED;
1128 1.271 roy s = pserialize_read_enter();
1129 1.271 roy IFADDR_READER_FOREACH(ifa, ifp) {
1130 1.271 roy if (ifa->ifa_addr->sa_family != AF_INET6)
1131 1.271 roy continue;
1132 1.271 roy ifa_acquire(ifa, &psref);
1133 1.271 roy pserialize_read_exit(s);
1134 1.2 itojun
1135 1.271 roy nd6_dad_stop(ifa);
1136 1.2 itojun
1137 1.271 roy ia = (struct in6_ifaddr *)ifa;
1138 1.271 roy ia->ia6_flags |= IN6_IFF_TENTATIVE;
1139 1.12 itojun
1140 1.271 roy s = pserialize_read_enter();
1141 1.271 roy ifa_release(ifa, &psref);
1142 1.99 rpaulo }
1143 1.271 roy pserialize_read_exit(s);
1144 1.271 roy curlwp_bindx(bound);
1145 1.271 roy }
1146 1.99 rpaulo
1147 1.271 roy if (flags & ND6_IFF_AUTO_LINKLOCAL) {
1148 1.271 roy if (!(ndi->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1149 1.271 roy /* auto_linklocal 0->1 transition */
1150 1.148 roy
1151 1.271 roy ndi->flags |= ND6_IFF_AUTO_LINKLOCAL;
1152 1.271 roy in6_ifattach(ifp, NULL);
1153 1.271 roy } else if (!(flags & ND6_IFF_IFDISABLED) &&
1154 1.271 roy ifp->if_flags & IFF_UP)
1155 1.148 roy {
1156 1.148 roy /*
1157 1.271 roy * When the IF already has
1158 1.271 roy * ND6_IFF_AUTO_LINKLOCAL, no link-local
1159 1.271 roy * address is assigned, and IFF_UP, try to
1160 1.271 roy * assign one.
1161 1.148 roy */
1162 1.271 roy bool haslinklocal = 0;
1163 1.148 roy
1164 1.205 ozaki s = pserialize_read_enter();
1165 1.202 ozaki IFADDR_READER_FOREACH(ifa, ifp) {
1166 1.271 roy if (ifa->ifa_addr->sa_family !=AF_INET6)
1167 1.148 roy continue;
1168 1.148 roy ia = (struct in6_ifaddr *)ifa;
1169 1.271 roy if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){
1170 1.271 roy haslinklocal = true;
1171 1.148 roy break;
1172 1.148 roy }
1173 1.148 roy }
1174 1.205 ozaki pserialize_read_exit(s);
1175 1.271 roy if (!haslinklocal)
1176 1.271 roy in6_ifattach(ifp, NULL);
1177 1.148 roy }
1178 1.271 roy }
1179 1.151 roy
1180 1.271 roy ndi->flags = flags;
1181 1.271 roy }
1182 1.151 roy
1183 1.271 roy int
1184 1.271 roy nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
1185 1.271 roy {
1186 1.271 roy #ifdef OSIOCGIFINFO_IN6_90
1187 1.271 roy struct in6_ndireq90 *ondi = (struct in6_ndireq90 *)data;
1188 1.271 roy struct in6_ndifreq90 *ndif = (struct in6_ndifreq90 *)data;
1189 1.271 roy #define OND ondi->ndi
1190 1.271 roy #endif
1191 1.271 roy struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1192 1.271 roy struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1193 1.271 roy struct nd_kifinfo *ifndi = ND_IFINFO(ifp);
1194 1.271 roy int error = 0;
1195 1.271 roy #define ND ndi->ndi
1196 1.151 roy
1197 1.271 roy switch (cmd) {
1198 1.271 roy #ifdef OSIOCSRTRFLUSH_IN6
1199 1.271 roy case OSIOCGDRLST_IN6: /* FALLTHROUGH */
1200 1.271 roy case OSIOCGPRLST_IN6: /* FALLTHROUGH */
1201 1.271 roy case OSIOCSNDFLUSH_IN6: /* FALLTHROUGH */
1202 1.271 roy case OSIOCSPFXFLUSH_IN6: /* FALLTHROUGH */
1203 1.271 roy case OSIOCSRTRFLUSH_IN6: /* FALLTHROUGH */
1204 1.271 roy break;
1205 1.271 roy case OSIOCGDEFIFACE_IN6:
1206 1.271 roy ndif->ifindex = 0;
1207 1.26 itojun break;
1208 1.271 roy case OSIOCSDEFIFACE_IN6:
1209 1.271 roy error = ENOTSUP;
1210 1.2 itojun break;
1211 1.271 roy #endif
1212 1.271 roy #ifdef OSIOCGIFINFO_IN6
1213 1.271 roy case OSIOCGIFINFO_IN6: /* FALLTHROUGH */
1214 1.271 roy #endif
1215 1.271 roy #ifdef OSIOCGIFINFO_IN6_90
1216 1.271 roy case OSIOCGIFINFO_IN6_90:
1217 1.271 roy memset(&OND, 0, sizeof(OND));
1218 1.271 roy OND.initialized = 1;
1219 1.271 roy OND.chlim = ifndi->chlim;
1220 1.271 roy OND.basereachable = ifndi->basereachable;
1221 1.271 roy OND.retrans = ifndi->retrans;
1222 1.271 roy OND.flags = ifndi->flags;
1223 1.271 roy break;
1224 1.271 roy case OSIOCSIFINFO_IN6_90:
1225 1.271 roy /* Allow userland to set Neighour Unreachability Detection
1226 1.271 roy * timers. */
1227 1.271 roy if (OND.chlim != 0)
1228 1.271 roy ifndi->chlim = OND.chlim;
1229 1.271 roy if (OND.basereachable != 0 &&
1230 1.271 roy OND.basereachable != ifndi->basereachable)
1231 1.271 roy {
1232 1.271 roy ifndi->basereachable = OND.basereachable;
1233 1.271 roy ifndi->reachable = ND_COMPUTE_RTIME(OND.basereachable);
1234 1.2 itojun }
1235 1.271 roy if (OND.retrans != 0)
1236 1.271 roy ifndi->retrans = OND.retrans;
1237 1.271 roy /* Retain the old behaviour .... */
1238 1.271 roy /* FALLTHROUGH */
1239 1.271 roy case OSIOCSIFINFO_FLAGS_90:
1240 1.271 roy nd6_setifflags(ifp, OND.flags);
1241 1.2 itojun break;
1242 1.271 roy #undef OND
1243 1.252 roy #endif
1244 1.271 roy case SIOCGIFINFO_IN6:
1245 1.271 roy ND.chlim = ifndi->chlim;
1246 1.271 roy ND.basereachable = ifndi->basereachable;
1247 1.271 roy ND.retrans = ifndi->retrans;
1248 1.271 roy ND.flags = ifndi->flags;
1249 1.271 roy break;
1250 1.271 roy case SIOCSIFINFO_IN6:
1251 1.271 roy /* Allow userland to set Neighour Unreachability Detection
1252 1.271 roy * timers. */
1253 1.271 roy if (ND.chlim != 0)
1254 1.271 roy ifndi->chlim = ND.chlim;
1255 1.271 roy if (ND.basereachable != 0 &&
1256 1.271 roy ND.basereachable != ifndi->basereachable)
1257 1.271 roy {
1258 1.271 roy ifndi->basereachable = ND.basereachable;
1259 1.271 roy ifndi->reachable = ND_COMPUTE_RTIME(ND.basereachable);
1260 1.2 itojun }
1261 1.271 roy if (ND.retrans != 0)
1262 1.271 roy ifndi->retrans = ND.retrans;
1263 1.271 roy break;
1264 1.271 roy case SIOCSIFINFO_FLAGS:
1265 1.271 roy nd6_setifflags(ifp, ND.flags);
1266 1.2 itojun break;
1267 1.271 roy #undef ND
1268 1.2 itojun case SIOCGNBRINFO_IN6:
1269 1.99 rpaulo {
1270 1.181 ozaki struct llentry *ln;
1271 1.12 itojun struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1272 1.12 itojun
1273 1.96 rpaulo if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1274 1.112 dyoung return error;
1275 1.2 itojun
1276 1.188 ozaki ln = nd6_lookup(&nb_addr, ifp, false);
1277 1.165 ozaki if (ln == NULL) {
1278 1.12 itojun error = EINVAL;
1279 1.12 itojun break;
1280 1.12 itojun }
1281 1.12 itojun nbi->state = ln->ln_state;
1282 1.12 itojun nbi->asked = ln->ln_asked;
1283 1.12 itojun nbi->isrouter = ln->ln_router;
1284 1.166 ozaki nbi->expire = ln->ln_expire ?
1285 1.166 ozaki time_mono_to_wall(ln->ln_expire) : 0;
1286 1.188 ozaki LLE_RUNLOCK(ln);
1287 1.63 itojun
1288 1.12 itojun break;
1289 1.65 itojun }
1290 1.2 itojun }
1291 1.112 dyoung return error;
1292 1.2 itojun }
1293 1.2 itojun
1294 1.115 dyoung void
1295 1.188 ozaki nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp)
1296 1.115 dyoung {
1297 1.115 dyoung struct mbuf *m_hold, *m_hold_next;
1298 1.188 ozaki struct sockaddr_in6 sin6;
1299 1.188 ozaki
1300 1.188 ozaki LLE_WLOCK_ASSERT(ln);
1301 1.188 ozaki
1302 1.188 ozaki sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
1303 1.115 dyoung
1304 1.188 ozaki m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0;
1305 1.188 ozaki
1306 1.188 ozaki LLE_WUNLOCK(ln);
1307 1.188 ozaki for (; m_hold != NULL; m_hold = m_hold_next) {
1308 1.115 dyoung m_hold_next = m_hold->m_nextpkt;
1309 1.115 dyoung m_hold->m_nextpkt = NULL;
1310 1.115 dyoung
1311 1.115 dyoung /*
1312 1.115 dyoung * we assume ifp is not a p2p here, so
1313 1.115 dyoung * just set the 2nd argument as the
1314 1.115 dyoung * 1st one.
1315 1.115 dyoung */
1316 1.227 ozaki ip6_if_output(ifp, ifp, m_hold, &sin6, NULL);
1317 1.115 dyoung }
1318 1.188 ozaki LLE_WLOCK(ln);
1319 1.115 dyoung }
1320 1.115 dyoung
1321 1.2 itojun /*
1322 1.2 itojun * Create neighbor cache entry and cache link-layer address,
1323 1.52 itojun * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1324 1.2 itojun */
1325 1.165 ozaki void
1326 1.105 christos nd6_cache_lladdr(
1327 1.105 christos struct ifnet *ifp,
1328 1.105 christos struct in6_addr *from,
1329 1.105 christos char *lladdr,
1330 1.107 christos int lladdrlen,
1331 1.105 christos int type, /* ICMP6 type */
1332 1.105 christos int code /* type dependent information */
1333 1.105 christos )
1334 1.2 itojun {
1335 1.181 ozaki struct llentry *ln = NULL;
1336 1.2 itojun int is_newentry;
1337 1.2 itojun int do_update;
1338 1.2 itojun int olladdr;
1339 1.2 itojun int llchange;
1340 1.2 itojun int newstate = 0;
1341 1.2 itojun
1342 1.163 ozaki KASSERT(ifp != NULL);
1343 1.163 ozaki KASSERT(from != NULL);
1344 1.2 itojun
1345 1.2 itojun /* nothing must be updated for unspecified address */
1346 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(from))
1347 1.165 ozaki return;
1348 1.2 itojun
1349 1.2 itojun /*
1350 1.2 itojun * Validation about ifp->if_addrlen and lladdrlen must be done in
1351 1.2 itojun * the caller.
1352 1.2 itojun *
1353 1.2 itojun * XXX If the link does not have link-layer adderss, what should
1354 1.2 itojun * we do? (ifp->if_addrlen == 0)
1355 1.2 itojun * Spec says nothing in sections for RA, RS and NA. There's small
1356 1.2 itojun * description on it in NS section (RFC 2461 7.2.3).
1357 1.2 itojun */
1358 1.2 itojun
1359 1.188 ozaki ln = nd6_lookup(from, ifp, true);
1360 1.188 ozaki if (ln == NULL) {
1361 1.2 itojun #if 0
1362 1.2 itojun /* nothing must be done if there's no lladdr */
1363 1.2 itojun if (!lladdr || !lladdrlen)
1364 1.2 itojun return NULL;
1365 1.2 itojun #endif
1366 1.2 itojun
1367 1.188 ozaki ln = nd6_create(from, ifp);
1368 1.2 itojun is_newentry = 1;
1369 1.43 itojun } else {
1370 1.43 itojun /* do nothing if static ndp is set */
1371 1.188 ozaki if (ln->la_flags & LLE_STATIC) {
1372 1.188 ozaki LLE_WUNLOCK(ln);
1373 1.165 ozaki return;
1374 1.165 ozaki }
1375 1.2 itojun is_newentry = 0;
1376 1.43 itojun }
1377 1.2 itojun
1378 1.188 ozaki if (ln == NULL)
1379 1.165 ozaki return;
1380 1.2 itojun
1381 1.188 ozaki olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1382 1.2 itojun if (olladdr && lladdr) {
1383 1.188 ozaki llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen);
1384 1.2 itojun } else
1385 1.2 itojun llchange = 0;
1386 1.2 itojun
1387 1.2 itojun /*
1388 1.2 itojun * newentry olladdr lladdr llchange (*=record)
1389 1.2 itojun * 0 n n -- (1)
1390 1.2 itojun * 0 y n -- (2)
1391 1.2 itojun * 0 n y -- (3) * STALE
1392 1.2 itojun * 0 y y n (4) *
1393 1.2 itojun * 0 y y y (5) * STALE
1394 1.2 itojun * 1 -- n -- (6) NOSTATE(= PASSIVE)
1395 1.2 itojun * 1 -- y -- (7) * STALE
1396 1.2 itojun */
1397 1.2 itojun
1398 1.52 itojun if (lladdr) { /* (3-5) and (7) */
1399 1.2 itojun /*
1400 1.2 itojun * Record source link-layer address
1401 1.2 itojun * XXX is it dependent to ifp->if_type?
1402 1.2 itojun */
1403 1.188 ozaki memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
1404 1.188 ozaki ln->la_flags |= LLE_VALID;
1405 1.2 itojun }
1406 1.2 itojun
1407 1.2 itojun if (!is_newentry) {
1408 1.65 itojun if ((!olladdr && lladdr) || /* (3) */
1409 1.65 itojun (olladdr && lladdr && llchange)) { /* (5) */
1410 1.2 itojun do_update = 1;
1411 1.272 roy newstate = ND_LLINFO_STALE;
1412 1.52 itojun } else /* (1-2,4) */
1413 1.2 itojun do_update = 0;
1414 1.2 itojun } else {
1415 1.2 itojun do_update = 1;
1416 1.99 rpaulo if (lladdr == NULL) /* (6) */
1417 1.272 roy newstate = ND_LLINFO_NOSTATE;
1418 1.52 itojun else /* (7) */
1419 1.272 roy newstate = ND_LLINFO_STALE;
1420 1.2 itojun }
1421 1.2 itojun
1422 1.2 itojun if (do_update) {
1423 1.2 itojun /*
1424 1.2 itojun * Update the state of the neighbor cache.
1425 1.2 itojun */
1426 1.2 itojun ln->ln_state = newstate;
1427 1.2 itojun
1428 1.272 roy if (ln->ln_state == ND_LLINFO_STALE) {
1429 1.44 itojun /*
1430 1.44 itojun * XXX: since nd6_output() below will cause
1431 1.44 itojun * state tansition to DELAY and reset the timer,
1432 1.44 itojun * we must set the timer now, although it is actually
1433 1.44 itojun * meaningless.
1434 1.44 itojun */
1435 1.272 roy nd_set_timer(ln, ND_TIMER_GC);
1436 1.44 itojun
1437 1.188 ozaki nd6_llinfo_release_pkts(ln, ifp);
1438 1.272 roy } else if (ln->ln_state == ND_LLINFO_INCOMPLETE) {
1439 1.2 itojun /* probe right away */
1440 1.272 roy nd_set_timer(ln, ND_TIMER_IMMEDIATE);
1441 1.2 itojun }
1442 1.2 itojun }
1443 1.2 itojun
1444 1.2 itojun /*
1445 1.2 itojun * ICMP6 type dependent behavior.
1446 1.2 itojun *
1447 1.2 itojun * NS: clear IsRouter if new entry
1448 1.2 itojun * RS: clear IsRouter
1449 1.2 itojun * RA: set IsRouter if there's lladdr
1450 1.2 itojun * redir: clear IsRouter if new entry
1451 1.2 itojun *
1452 1.2 itojun * RA case, (1):
1453 1.2 itojun * The spec says that we must set IsRouter in the following cases:
1454 1.2 itojun * - If lladdr exist, set IsRouter. This means (1-5).
1455 1.2 itojun * - If it is old entry (!newentry), set IsRouter. This means (7).
1456 1.2 itojun * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1457 1.2 itojun * A quetion arises for (1) case. (1) case has no lladdr in the
1458 1.2 itojun * neighbor cache, this is similar to (6).
1459 1.2 itojun * This case is rare but we figured that we MUST NOT set IsRouter.
1460 1.2 itojun *
1461 1.2 itojun * newentry olladdr lladdr llchange NS RS RA redir
1462 1.8 itojun * D R
1463 1.8 itojun * 0 n n -- (1) c ? s
1464 1.8 itojun * 0 y n -- (2) c s s
1465 1.8 itojun * 0 n y -- (3) c s s
1466 1.8 itojun * 0 y y n (4) c s s
1467 1.8 itojun * 0 y y y (5) c s s
1468 1.8 itojun * 1 -- n -- (6) c c c s
1469 1.8 itojun * 1 -- y -- (7) c c s c s
1470 1.2 itojun *
1471 1.2 itojun * (c=clear s=set)
1472 1.2 itojun */
1473 1.2 itojun switch (type & 0xff) {
1474 1.2 itojun case ND_NEIGHBOR_SOLICIT:
1475 1.2 itojun /*
1476 1.2 itojun * New entry must have is_router flag cleared.
1477 1.2 itojun */
1478 1.52 itojun if (is_newentry) /* (6-7) */
1479 1.8 itojun ln->ln_router = 0;
1480 1.8 itojun break;
1481 1.8 itojun case ND_REDIRECT:
1482 1.8 itojun /*
1483 1.8 itojun * If the icmp is a redirect to a better router, always set the
1484 1.52 itojun * is_router flag. Otherwise, if the entry is newly created,
1485 1.52 itojun * clear the flag. [RFC 2461, sec 8.3]
1486 1.8 itojun */
1487 1.8 itojun if (code == ND_REDIRECT_ROUTER)
1488 1.8 itojun ln->ln_router = 1;
1489 1.52 itojun else if (is_newentry) /* (6-7) */
1490 1.2 itojun ln->ln_router = 0;
1491 1.2 itojun break;
1492 1.2 itojun case ND_ROUTER_SOLICIT:
1493 1.2 itojun /*
1494 1.2 itojun * is_router flag must always be cleared.
1495 1.2 itojun */
1496 1.2 itojun ln->ln_router = 0;
1497 1.2 itojun break;
1498 1.2 itojun case ND_ROUTER_ADVERT:
1499 1.2 itojun /*
1500 1.2 itojun * Mark an entry with lladdr as a router.
1501 1.2 itojun */
1502 1.65 itojun if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1503 1.65 itojun (is_newentry && lladdr)) { /* (7) */
1504 1.2 itojun ln->ln_router = 1;
1505 1.2 itojun }
1506 1.2 itojun break;
1507 1.2 itojun }
1508 1.47 itojun
1509 1.262 roy if (do_update && lladdr != NULL) {
1510 1.259 roy struct sockaddr_in6 sin6;
1511 1.259 roy
1512 1.259 roy sockaddr_in6_init(&sin6, from, 0, 0, 0);
1513 1.259 roy rt_clonedmsg(is_newentry ? RTM_ADD : RTM_CHANGE,
1514 1.267 roy NULL, sin6tosa(&sin6), lladdr, ifp);
1515 1.259 roy }
1516 1.188 ozaki
1517 1.271 roy if (ln != NULL)
1518 1.188 ozaki LLE_WUNLOCK(ln);
1519 1.188 ozaki
1520 1.188 ozaki /*
1521 1.188 ozaki * If we have too many cache entries, initiate immediate
1522 1.188 ozaki * purging for some entries.
1523 1.188 ozaki */
1524 1.188 ozaki if (is_newentry)
1525 1.207 ozaki nd6_gc_neighbors(LLTABLE6(ifp), &ln->r_l3addr.addr6);
1526 1.2 itojun }
1527 1.2 itojun
1528 1.2 itojun static void
1529 1.107 christos nd6_slowtimo(void *ignored_arg)
1530 1.2 itojun {
1531 1.271 roy struct nd_kifinfo *ndi;
1532 1.58 itojun struct ifnet *ifp;
1533 1.194 ozaki int s;
1534 1.2 itojun
1535 1.239 ozaki SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
1536 1.170 ozaki callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1537 1.20 thorpej nd6_slowtimo, NULL);
1538 1.194 ozaki
1539 1.194 ozaki s = pserialize_read_enter();
1540 1.194 ozaki IFNET_READER_FOREACH(ifp) {
1541 1.271 roy ndi = ND_IFINFO(ifp);
1542 1.271 roy if (ndi->basereachable && /* already initialized */
1543 1.271 roy (ndi->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1544 1.2 itojun /*
1545 1.2 itojun * Since reachable time rarely changes by router
1546 1.2 itojun * advertisements, we SHOULD insure that a new random
1547 1.2 itojun * value gets recomputed at least once every few hours.
1548 1.2 itojun * (RFC 2461, 6.3.4)
1549 1.2 itojun */
1550 1.271 roy ndi->recalctm = nd6_recalc_reachtm_interval;
1551 1.271 roy ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
1552 1.2 itojun }
1553 1.2 itojun }
1554 1.194 ozaki pserialize_read_exit(s);
1555 1.194 ozaki
1556 1.239 ozaki SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1557 1.2 itojun }
1558 1.2 itojun
1559 1.227 ozaki /*
1560 1.227 ozaki * Return 0 if a neighbor cache is found. Return EWOULDBLOCK if a cache is not
1561 1.227 ozaki * found and trying to resolve a neighbor; in this case the mbuf is queued in
1562 1.227 ozaki * the list. Otherwise return errno after freeing the mbuf.
1563 1.227 ozaki */
1564 1.188 ozaki int
1565 1.227 ozaki nd6_resolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m,
1566 1.227 ozaki const struct sockaddr *_dst, uint8_t *lldst, size_t dstsize)
1567 1.176 ozaki {
1568 1.181 ozaki struct llentry *ln = NULL;
1569 1.188 ozaki bool created = false;
1570 1.227 ozaki const struct sockaddr_in6 *dst = satocsin6(_dst);
1571 1.263 roy int error;
1572 1.271 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
1573 1.193 ozaki
1574 1.227 ozaki /* discard the packet if IPv6 operation is disabled on the interface */
1575 1.271 roy if (ndi->flags & ND6_IFF_IFDISABLED) {
1576 1.227 ozaki m_freem(m);
1577 1.227 ozaki return ENETDOWN; /* better error? */
1578 1.193 ozaki }
1579 1.193 ozaki
1580 1.2 itojun /*
1581 1.2 itojun * Address resolution or Neighbor Unreachability Detection
1582 1.2 itojun * for the next hop.
1583 1.2 itojun * At this point, the destination of the packet must be a unicast
1584 1.2 itojun * or an anycast address(i.e. not a multicast).
1585 1.2 itojun */
1586 1.2 itojun
1587 1.2 itojun /* Look up the neighbor cache for the nexthop */
1588 1.227 ozaki ln = nd6_lookup(&dst->sin6_addr, ifp, false);
1589 1.227 ozaki
1590 1.255 ozaki if (ln != NULL && (ln->la_flags & LLE_VALID) != 0 &&
1591 1.272 roy ln->ln_state == ND_LLINFO_REACHABLE) {
1592 1.227 ozaki /* Fast path */
1593 1.227 ozaki memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
1594 1.227 ozaki LLE_RUNLOCK(ln);
1595 1.227 ozaki return 0;
1596 1.227 ozaki }
1597 1.227 ozaki if (ln != NULL)
1598 1.227 ozaki LLE_RUNLOCK(ln);
1599 1.227 ozaki
1600 1.227 ozaki /* Slow path */
1601 1.188 ozaki ln = nd6_lookup(&dst->sin6_addr, ifp, true);
1602 1.227 ozaki if (ln == NULL && nd6_is_addr_neighbor(dst, ifp)) {
1603 1.29 itojun /*
1604 1.29 itojun * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1605 1.52 itojun * the condition below is not very efficient. But we believe
1606 1.29 itojun * it is tolerable, because this should be a rare case.
1607 1.29 itojun */
1608 1.188 ozaki ln = nd6_create(&dst->sin6_addr, ifp);
1609 1.227 ozaki if (ln == NULL) {
1610 1.225 ryo char ip6buf[INET6_ADDRSTRLEN];
1611 1.29 itojun log(LOG_DEBUG,
1612 1.227 ozaki "%s: can't allocate llinfo for %s "
1613 1.227 ozaki "(ln=%p, rt=%p)\n", __func__,
1614 1.226 christos IN6_PRINT(ip6buf, &dst->sin6_addr), ln, rt);
1615 1.227 ozaki m_freem(m);
1616 1.227 ozaki return ENOBUFS;
1617 1.29 itojun }
1618 1.227 ozaki created = true;
1619 1.2 itojun }
1620 1.2 itojun
1621 1.236 ozaki if (ln == NULL) {
1622 1.236 ozaki m_freem(m);
1623 1.236 ozaki return ENETDOWN; /* better error? */
1624 1.236 ozaki }
1625 1.236 ozaki
1626 1.272 roy error = nd_resolve(ln, rt, m, lldst, dstsize);
1627 1.188 ozaki
1628 1.188 ozaki if (created)
1629 1.207 ozaki nd6_gc_neighbors(LLTABLE6(ifp), &dst->sin6_addr);
1630 1.165 ozaki
1631 1.263 roy return error;
1632 1.63 itojun }
1633 1.54 itojun
1634 1.54 itojun int
1635 1.112 dyoung nd6_need_cache(struct ifnet *ifp)
1636 1.54 itojun {
1637 1.54 itojun /*
1638 1.54 itojun * XXX: we currently do not make neighbor cache on any interface
1639 1.266 thorpej * other than ARCnet, Ethernet, and GIF.
1640 1.54 itojun *
1641 1.54 itojun * RFC2893 says:
1642 1.54 itojun * - unidirectional tunnels needs no ND
1643 1.54 itojun */
1644 1.54 itojun switch (ifp->if_type) {
1645 1.54 itojun case IFT_ARCNET:
1646 1.54 itojun case IFT_ETHER:
1647 1.54 itojun case IFT_IEEE1394:
1648 1.102 liamjfoy case IFT_CARP:
1649 1.54 itojun case IFT_GIF: /* XXX need more cases? */
1650 1.99 rpaulo case IFT_PPP:
1651 1.99 rpaulo case IFT_TUNNEL:
1652 1.112 dyoung return 1;
1653 1.54 itojun default:
1654 1.112 dyoung return 0;
1655 1.54 itojun }
1656 1.54 itojun }
1657 1.2 itojun
1658 1.65 itojun int
1659 1.105 christos nd6_sysctl(
1660 1.105 christos int name,
1661 1.105 christos void *oldp, /* syscall arg, need copyout */
1662 1.105 christos size_t *oldlenp,
1663 1.105 christos void *newp, /* syscall arg, need copyin */
1664 1.107 christos size_t newlen
1665 1.105 christos )
1666 1.65 itojun {
1667 1.65 itojun
1668 1.65 itojun if (newp)
1669 1.65 itojun return EPERM;
1670 1.241 pgoyette
1671 1.65 itojun switch (name) {
1672 1.271 roy #ifdef COMPAT_90
1673 1.271 roy case OICMPV6CTL_ND6_DRLIST: /* FALLTHROUGH */
1674 1.271 roy case OICMPV6CTL_ND6_PRLIST:
1675 1.271 roy *oldlenp = 0;
1676 1.271 roy return 0;
1677 1.271 roy #endif
1678 1.99 rpaulo case ICMPV6CTL_ND6_MAXQLEN:
1679 1.242 pgoyette return 0;
1680 1.65 itojun default:
1681 1.242 pgoyette return ENOPROTOOPT;
1682 1.65 itojun }
1683 1.212 ozaki }
1684