nd6.c revision 1.279 1 1.279 riastrad /* $NetBSD: nd6.c,v 1.279 2022/09/01 18:32:17 riastradh 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.279 riastrad __KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.279 2022/09/01 18:32:17 riastradh 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.276 nia nd6_ns_output(ifp,
388 1.276 nia daddr != NULL ? &daddr->addr6 : NULL,
389 1.276 nia taddr != NULL ? &taddr->addr6 : NULL,
390 1.276 nia hsrc != NULL ? &hsrc->addr6 : NULL, NULL);
391 1.272 roy }
392 1.173 ozaki
393 1.272 roy static bool
394 1.272 roy nd6_nud_enabled(struct ifnet *ifp)
395 1.272 roy {
396 1.272 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
397 1.86 itojun
398 1.272 roy return ndi->flags & ND6_IFF_PERFORMNUD;
399 1.272 roy }
400 1.263 roy
401 1.272 roy static unsigned int
402 1.272 roy nd6_llinfo_reachable(struct ifnet *ifp)
403 1.272 roy {
404 1.272 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
405 1.262 roy
406 1.272 roy return ndi->reachable;
407 1.272 roy }
408 1.269 roy
409 1.272 roy static unsigned int
410 1.272 roy nd6_llinfo_retrans(struct ifnet *ifp)
411 1.272 roy {
412 1.272 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
413 1.268 christos
414 1.272 roy return ndi->retrans;
415 1.272 roy }
416 1.262 roy
417 1.272 roy static void
418 1.274 roy nd6_llinfo_missed(struct ifnet *ifp, const union l3addr *taddr,
419 1.274 roy int16_t type, struct mbuf *m)
420 1.272 roy {
421 1.272 roy struct in6_addr mdaddr6 = zeroin6_addr;
422 1.272 roy struct sockaddr_in6 dsin6, tsin6;
423 1.272 roy struct sockaddr *sa;
424 1.262 roy
425 1.274 roy if (m != NULL) {
426 1.274 roy if (type == ND_LLINFO_PROBE) {
427 1.274 roy struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
428 1.274 roy
429 1.274 roy /* XXX pullup? */
430 1.274 roy if (sizeof(*ip6) < m->m_len)
431 1.274 roy mdaddr6 = ip6->ip6_src;
432 1.274 roy m_freem(m);
433 1.274 roy } else
434 1.274 roy icmp6_error2(m, ICMP6_DST_UNREACH,
435 1.274 roy ICMP6_DST_UNREACH_ADDR, 0, ifp, &mdaddr6);
436 1.274 roy }
437 1.272 roy if (!IN6_IS_ADDR_UNSPECIFIED(&mdaddr6)) {
438 1.272 roy sockaddr_in6_init(&dsin6, &mdaddr6, 0, 0, 0);
439 1.272 roy sa = sin6tosa(&dsin6);
440 1.272 roy } else
441 1.272 roy sa = NULL;
442 1.2 itojun
443 1.273 roy sockaddr_in6_init(&tsin6, &taddr->addr6, 0, 0, 0);
444 1.272 roy rt_clonedmsg(RTM_MISS, sa, sin6tosa(&tsin6), NULL, ifp);
445 1.86 itojun }
446 1.42 itojun
447 1.86 itojun /*
448 1.86 itojun * ND6 timer routine to expire default route list and prefix list
449 1.86 itojun */
450 1.186 ozaki static void
451 1.203 ozaki nd6_timer_work(struct work *wk, void *arg)
452 1.86 itojun {
453 1.86 itojun struct in6_ifaddr *ia6, *nia6;
454 1.205 ozaki int s, bound;
455 1.205 ozaki struct psref psref;
456 1.42 itojun
457 1.86 itojun callout_reset(&nd6_timer_ch, nd6_prune * hz,
458 1.86 itojun nd6_timer, NULL);
459 1.63 itojun
460 1.239 ozaki SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
461 1.126 ad
462 1.271 roy /* expire interface addresses */
463 1.205 ozaki bound = curlwp_bind();
464 1.205 ozaki s = pserialize_read_enter();
465 1.205 ozaki for (ia6 = IN6_ADDRLIST_READER_FIRST(); ia6; ia6 = nia6) {
466 1.205 ozaki nia6 = IN6_ADDRLIST_READER_NEXT(ia6);
467 1.205 ozaki
468 1.205 ozaki ia6_acquire(ia6, &psref);
469 1.205 ozaki pserialize_read_exit(s);
470 1.205 ozaki
471 1.65 itojun /* check address lifetime */
472 1.65 itojun if (IFA6_IS_INVALID(ia6)) {
473 1.240 ozaki struct ifnet *ifp;
474 1.99 rpaulo
475 1.240 ozaki ifp = ia6->ia_ifa.ifa_ifp;
476 1.240 ozaki IFNET_LOCK(ifp);
477 1.240 ozaki /*
478 1.240 ozaki * Need to take the lock first to prevent if_detach
479 1.240 ozaki * from running in6_purgeaddr concurrently.
480 1.240 ozaki */
481 1.240 ozaki if (!if_is_deactivated(ifp)) {
482 1.240 ozaki ia6_release(ia6, &psref);
483 1.240 ozaki in6_purgeaddr(&ia6->ia_ifa);
484 1.240 ozaki } else {
485 1.240 ozaki /*
486 1.240 ozaki * ifp is being destroyed, ia6 will be destroyed
487 1.240 ozaki * by if_detach.
488 1.240 ozaki */
489 1.240 ozaki ia6_release(ia6, &psref);
490 1.240 ozaki }
491 1.205 ozaki ia6 = NULL;
492 1.240 ozaki IFNET_UNLOCK(ifp);
493 1.99 rpaulo } else if (IFA6_IS_DEPRECATED(ia6)) {
494 1.99 rpaulo int oldflags = ia6->ia6_flags;
495 1.99 rpaulo
496 1.145 roy if ((oldflags & IN6_IFF_DEPRECATED) == 0) {
497 1.145 roy ia6->ia6_flags |= IN6_IFF_DEPRECATED;
498 1.253 roy rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
499 1.145 roy }
500 1.65 itojun } else {
501 1.65 itojun /*
502 1.65 itojun * A new RA might have made a deprecated address
503 1.65 itojun * preferred.
504 1.65 itojun */
505 1.145 roy if (ia6->ia6_flags & IN6_IFF_DEPRECATED) {
506 1.145 roy ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
507 1.253 roy rt_addrmsg(RTM_NEWADDR, (struct ifaddr *)ia6);
508 1.145 roy }
509 1.2 itojun }
510 1.205 ozaki s = pserialize_read_enter();
511 1.205 ozaki ia6_release(ia6, &psref);
512 1.65 itojun }
513 1.205 ozaki pserialize_read_exit(s);
514 1.205 ozaki curlwp_bindx(bound);
515 1.2 itojun
516 1.239 ozaki SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
517 1.16 itojun }
518 1.16 itojun
519 1.203 ozaki static void
520 1.203 ozaki nd6_timer(void *ignored_arg)
521 1.203 ozaki {
522 1.203 ozaki
523 1.203 ozaki workqueue_enqueue(nd6_timer_wq, &nd6_timer_wk, NULL);
524 1.203 ozaki }
525 1.203 ozaki
526 1.16 itojun /*
527 1.16 itojun * Nuke neighbor cache/prefix/default router management table, right before
528 1.16 itojun * ifp goes away.
529 1.16 itojun */
530 1.16 itojun void
531 1.158 martin nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext)
532 1.16 itojun {
533 1.16 itojun
534 1.65 itojun /*
535 1.158 martin * During detach, the ND info might be already removed, but
536 1.158 martin * then is explitly passed as argument.
537 1.158 martin * Otherwise get it from ifp->if_afdata.
538 1.158 martin */
539 1.158 martin if (ext == NULL)
540 1.158 martin ext = ifp->if_afdata[AF_INET6];
541 1.158 martin if (ext == NULL)
542 1.158 martin return;
543 1.158 martin
544 1.16 itojun /*
545 1.181 ozaki * We may not need to nuke the neighbor cache entries here
546 1.181 ozaki * because the neighbor cache is kept in if_afdata[AF_INET6].
547 1.181 ozaki * nd6_purge() is invoked by in6_ifdetach() which is called
548 1.181 ozaki * from if_detach() where everything gets purged. However
549 1.181 ozaki * in6_ifdetach is directly called from vlan(4), so we still
550 1.181 ozaki * need to purge entries here.
551 1.16 itojun */
552 1.181 ozaki if (ext->lltable != NULL)
553 1.181 ozaki lltable_purge_entries(ext->lltable);
554 1.2 itojun }
555 1.2 itojun
556 1.188 ozaki struct llentry *
557 1.188 ozaki nd6_lookup(const struct in6_addr *addr6, const struct ifnet *ifp, bool wlock)
558 1.188 ozaki {
559 1.188 ozaki struct sockaddr_in6 sin6;
560 1.188 ozaki struct llentry *ln;
561 1.188 ozaki
562 1.188 ozaki sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
563 1.188 ozaki
564 1.188 ozaki IF_AFDATA_RLOCK(ifp);
565 1.188 ozaki ln = lla_lookup(LLTABLE6(ifp), wlock ? LLE_EXCLUSIVE : 0,
566 1.188 ozaki sin6tosa(&sin6));
567 1.188 ozaki IF_AFDATA_RUNLOCK(ifp);
568 1.188 ozaki
569 1.188 ozaki return ln;
570 1.188 ozaki }
571 1.188 ozaki
572 1.188 ozaki struct llentry *
573 1.188 ozaki nd6_create(const struct in6_addr *addr6, const struct ifnet *ifp)
574 1.2 itojun {
575 1.2 itojun struct sockaddr_in6 sin6;
576 1.188 ozaki struct llentry *ln;
577 1.237 ozaki struct rtentry *rt;
578 1.2 itojun
579 1.122 dyoung sockaddr_in6_init(&sin6, addr6, 0, 0, 0);
580 1.237 ozaki rt = rtalloc1(sin6tosa(&sin6), 0);
581 1.188 ozaki
582 1.188 ozaki IF_AFDATA_WLOCK(ifp);
583 1.237 ozaki ln = lla_create(LLTABLE6(ifp), LLE_EXCLUSIVE, sin6tosa(&sin6), rt);
584 1.188 ozaki IF_AFDATA_WUNLOCK(ifp);
585 1.188 ozaki
586 1.237 ozaki if (rt != NULL)
587 1.237 ozaki rt_unref(rt);
588 1.188 ozaki if (ln != NULL)
589 1.272 roy ln->ln_state = ND_LLINFO_NOSTATE;
590 1.188 ozaki
591 1.188 ozaki return ln;
592 1.188 ozaki }
593 1.188 ozaki
594 1.188 ozaki /*
595 1.188 ozaki * Test whether a given IPv6 address is a neighbor or not, ignoring
596 1.188 ozaki * the actual neighbor cache. The neighbor cache is ignored in order
597 1.188 ozaki * to not reenter the routing code from within itself.
598 1.188 ozaki */
599 1.188 ozaki static int
600 1.188 ozaki nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
601 1.188 ozaki {
602 1.188 ozaki struct ifaddr *dstaddr;
603 1.205 ozaki int s;
604 1.188 ozaki
605 1.188 ozaki /*
606 1.188 ozaki * A link-local address is always a neighbor.
607 1.188 ozaki * XXX: a link does not necessarily specify a single interface.
608 1.188 ozaki */
609 1.188 ozaki if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
610 1.188 ozaki struct sockaddr_in6 sin6_copy;
611 1.188 ozaki u_int32_t zone;
612 1.188 ozaki
613 1.2 itojun /*
614 1.188 ozaki * We need sin6_copy since sa6_recoverscope() may modify the
615 1.188 ozaki * content (XXX).
616 1.188 ozaki */
617 1.188 ozaki sin6_copy = *addr;
618 1.188 ozaki if (sa6_recoverscope(&sin6_copy))
619 1.188 ozaki return 0; /* XXX: should be impossible */
620 1.188 ozaki if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
621 1.188 ozaki return 0;
622 1.188 ozaki if (sin6_copy.sin6_scope_id == zone)
623 1.188 ozaki return 1;
624 1.188 ozaki else
625 1.188 ozaki return 0;
626 1.188 ozaki }
627 1.188 ozaki
628 1.188 ozaki /*
629 1.188 ozaki * If the address is assigned on the node of the other side of
630 1.188 ozaki * a p2p interface, the address should be a neighbor.
631 1.188 ozaki */
632 1.205 ozaki s = pserialize_read_enter();
633 1.204 ozaki dstaddr = ifa_ifwithdstaddr(sin6tocsa(addr));
634 1.188 ozaki if (dstaddr != NULL) {
635 1.188 ozaki if (dstaddr->ifa_ifp == ifp) {
636 1.205 ozaki pserialize_read_exit(s);
637 1.188 ozaki return 1;
638 1.188 ozaki }
639 1.188 ozaki }
640 1.205 ozaki pserialize_read_exit(s);
641 1.147 roy
642 1.188 ozaki return 0;
643 1.147 roy }
644 1.147 roy
645 1.6 itojun /*
646 1.6 itojun * Detect if a given IPv6 address identifies a neighbor on a given link.
647 1.25 itojun * XXX: should take care of the destination of a p2p link?
648 1.6 itojun */
649 1.6 itojun int
650 1.110 dyoung nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
651 1.6 itojun {
652 1.188 ozaki struct llentry *ln;
653 1.165 ozaki struct rtentry *rt;
654 1.6 itojun
655 1.29 itojun /*
656 1.29 itojun * A link-local address is always a neighbor.
657 1.52 itojun * XXX: a link does not necessarily specify a single interface.
658 1.29 itojun */
659 1.96 rpaulo if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
660 1.96 rpaulo struct sockaddr_in6 sin6_copy;
661 1.96 rpaulo u_int32_t zone;
662 1.96 rpaulo
663 1.96 rpaulo /*
664 1.96 rpaulo * We need sin6_copy since sa6_recoverscope() may modify the
665 1.96 rpaulo * content (XXX).
666 1.96 rpaulo */
667 1.96 rpaulo sin6_copy = *addr;
668 1.96 rpaulo if (sa6_recoverscope(&sin6_copy))
669 1.112 dyoung return 0; /* XXX: should be impossible */
670 1.96 rpaulo if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
671 1.112 dyoung return 0;
672 1.96 rpaulo if (sin6_copy.sin6_scope_id == zone)
673 1.112 dyoung return 1;
674 1.96 rpaulo else
675 1.112 dyoung return 0;
676 1.96 rpaulo }
677 1.6 itojun
678 1.188 ozaki if (nd6_is_new_addr_neighbor(addr, ifp))
679 1.188 ozaki return 1;
680 1.188 ozaki
681 1.6 itojun /*
682 1.188 ozaki * Even if the address matches none of our addresses, it might be
683 1.188 ozaki * in the neighbor cache or a connected route.
684 1.6 itojun */
685 1.188 ozaki ln = nd6_lookup(&addr->sin6_addr, ifp, false);
686 1.188 ozaki if (ln != NULL) {
687 1.188 ozaki LLE_RUNLOCK(ln);
688 1.188 ozaki return 1;
689 1.188 ozaki }
690 1.188 ozaki
691 1.188 ozaki rt = rtalloc1(sin6tocsa(addr), 0);
692 1.188 ozaki if (rt == NULL)
693 1.188 ozaki return 0;
694 1.188 ozaki
695 1.188 ozaki if ((rt->rt_flags & RTF_CONNECTED) && (rt->rt_ifp == ifp
696 1.188 ozaki #if NBRIDGE > 0
697 1.188 ozaki || rt->rt_ifp->if_bridge == ifp->if_bridge
698 1.188 ozaki #endif
699 1.188 ozaki #if NCARP > 0
700 1.188 ozaki || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) ||
701 1.188 ozaki (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)||
702 1.188 ozaki (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP &&
703 1.188 ozaki rt->rt_ifp->if_carpdev == ifp->if_carpdev)
704 1.188 ozaki #endif
705 1.188 ozaki )) {
706 1.216 ozaki rt_unref(rt);
707 1.112 dyoung return 1;
708 1.165 ozaki }
709 1.216 ozaki rt_unref(rt);
710 1.6 itojun
711 1.112 dyoung return 0;
712 1.2 itojun }
713 1.2 itojun
714 1.2 itojun /*
715 1.2 itojun * Free an nd6 llinfo entry.
716 1.54 itojun * Since the function would cause significant changes in the kernel, DO NOT
717 1.54 itojun * make it global, unless you have a strong reason for the change, and are sure
718 1.54 itojun * that the change is safe.
719 1.2 itojun */
720 1.181 ozaki static void
721 1.188 ozaki nd6_free(struct llentry *ln, int gc)
722 1.2 itojun {
723 1.188 ozaki struct ifnet *ifp;
724 1.2 itojun
725 1.181 ozaki KASSERT(ln != NULL);
726 1.181 ozaki LLE_WLOCK_ASSERT(ln);
727 1.181 ozaki
728 1.18 itojun /*
729 1.271 roy * If the reason for the deletion is just garbage collection,
730 1.271 roy * and the neighbor is an active router, do not delete it.
731 1.271 roy * Instead, reset the GC timer using the router's lifetime.
732 1.271 roy * XXX: the check for ln_state should be redundant,
733 1.271 roy * but we intentionally keep it just in case.
734 1.18 itojun */
735 1.271 roy if (!ip6_forwarding && ln->ln_router &&
736 1.272 roy ln->ln_state == ND_LLINFO_STALE && gc)
737 1.271 roy {
738 1.272 roy nd_set_timer(ln, ND_TIMER_EXPIRE);
739 1.260 roy LLE_WUNLOCK(ln);
740 1.271 roy return;
741 1.271 roy }
742 1.12 itojun
743 1.271 roy ifp = ln->lle_tbl->llt_ifp;
744 1.12 itojun
745 1.262 roy if (ln->la_flags & LLE_VALID || gc) {
746 1.262 roy struct sockaddr_in6 sin6;
747 1.262 roy const char *lladdr;
748 1.262 roy
749 1.271 roy sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
750 1.262 roy lladdr = ln->la_flags & LLE_VALID ?
751 1.262 roy (const char *)&ln->ll_addr : NULL;
752 1.267 roy rt_clonedmsg(RTM_DELETE, NULL, sin6tosa(&sin6), lladdr, ifp);
753 1.262 roy }
754 1.259 roy
755 1.188 ozaki /*
756 1.188 ozaki * Save to unlock. We still hold an extra reference and will not
757 1.188 ozaki * free(9) in llentry_free() if someone else holds one as well.
758 1.188 ozaki */
759 1.181 ozaki LLE_WUNLOCK(ln);
760 1.188 ozaki IF_AFDATA_LOCK(ifp);
761 1.188 ozaki LLE_WLOCK(ln);
762 1.188 ozaki
763 1.222 ozaki lltable_free_entry(LLTABLE6(ifp), ln);
764 1.188 ozaki
765 1.188 ozaki IF_AFDATA_UNLOCK(ifp);
766 1.2 itojun }
767 1.2 itojun
768 1.2 itojun /*
769 1.2 itojun * Upper-layer reachability hint for Neighbor Unreachability Detection.
770 1.2 itojun *
771 1.98 rpaulo * XXX cost-effective methods?
772 1.2 itojun */
773 1.2 itojun void
774 1.171 ozaki nd6_nud_hint(struct rtentry *rt)
775 1.2 itojun {
776 1.181 ozaki struct llentry *ln;
777 1.188 ozaki struct ifnet *ifp;
778 1.2 itojun
779 1.164 ozaki if (rt == NULL)
780 1.164 ozaki return;
781 1.2 itojun
782 1.188 ozaki ifp = rt->rt_ifp;
783 1.188 ozaki ln = nd6_lookup(&(satocsin6(rt_getkey(rt)))->sin6_addr, ifp, true);
784 1.272 roy nd_nud_hint(ln);
785 1.2 itojun }
786 1.2 itojun
787 1.207 ozaki struct gc_args {
788 1.207 ozaki int gc_entries;
789 1.207 ozaki const struct in6_addr *skip_in6;
790 1.207 ozaki };
791 1.207 ozaki
792 1.181 ozaki static int
793 1.181 ozaki nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg)
794 1.181 ozaki {
795 1.207 ozaki struct gc_args *args = farg;
796 1.207 ozaki int *n = &args->gc_entries;
797 1.207 ozaki const struct in6_addr *skip_in6 = args->skip_in6;
798 1.181 ozaki
799 1.181 ozaki if (*n <= 0)
800 1.181 ozaki return 0;
801 1.181 ozaki
802 1.272 roy if (ND_IS_LLINFO_PERMANENT(ln))
803 1.181 ozaki return 0;
804 1.181 ozaki
805 1.207 ozaki if (IN6_ARE_ADDR_EQUAL(&ln->r_l3addr.addr6, skip_in6))
806 1.207 ozaki return 0;
807 1.207 ozaki
808 1.181 ozaki LLE_WLOCK(ln);
809 1.272 roy if (ln->ln_state > ND_LLINFO_INCOMPLETE)
810 1.272 roy ln->ln_state = ND_LLINFO_STALE;
811 1.181 ozaki else
812 1.272 roy ln->ln_state = ND_LLINFO_PURGE;
813 1.272 roy nd_set_timer(ln, ND_TIMER_IMMEDIATE);
814 1.181 ozaki LLE_WUNLOCK(ln);
815 1.181 ozaki
816 1.181 ozaki (*n)--;
817 1.181 ozaki return 0;
818 1.181 ozaki }
819 1.181 ozaki
820 1.181 ozaki static void
821 1.207 ozaki nd6_gc_neighbors(struct lltable *llt, const struct in6_addr *in6)
822 1.181 ozaki {
823 1.181 ozaki
824 1.181 ozaki if (ip6_neighborgcthresh >= 0 &&
825 1.181 ozaki lltable_get_entry_count(llt) >= ip6_neighborgcthresh) {
826 1.207 ozaki struct gc_args gc_args = {10, in6};
827 1.181 ozaki /*
828 1.181 ozaki * XXX entries that are "less recently used" should be
829 1.181 ozaki * freed first.
830 1.181 ozaki */
831 1.207 ozaki lltable_foreach_lle(llt, nd6_purge_entry, &gc_args);
832 1.181 ozaki }
833 1.181 ozaki }
834 1.181 ozaki
835 1.2 itojun void
836 1.130 dyoung nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info)
837 1.2 itojun {
838 1.2 itojun struct sockaddr *gate = rt->rt_gateway;
839 1.2 itojun struct ifnet *ifp = rt->rt_ifp;
840 1.119 dyoung uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen;
841 1.2 itojun struct ifaddr *ifa;
842 1.2 itojun
843 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
844 1.117 dyoung
845 1.131 dyoung if (req == RTM_LLINFO_UPD) {
846 1.131 dyoung int rc;
847 1.131 dyoung struct in6_addr *in6;
848 1.131 dyoung struct in6_addr in6_all;
849 1.131 dyoung int anycast;
850 1.131 dyoung
851 1.131 dyoung if ((ifa = info->rti_ifa) == NULL)
852 1.131 dyoung return;
853 1.131 dyoung
854 1.131 dyoung in6 = &ifatoia6(ifa)->ia_addr.sin6_addr;
855 1.131 dyoung anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST;
856 1.131 dyoung
857 1.131 dyoung in6_all = in6addr_linklocal_allnodes;
858 1.131 dyoung if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) {
859 1.131 dyoung log(LOG_ERR, "%s: failed to set scope %s "
860 1.131 dyoung "(errno=%d)\n", __func__, if_name(ifp), rc);
861 1.131 dyoung return;
862 1.131 dyoung }
863 1.131 dyoung
864 1.131 dyoung /* XXX don't set Override for proxy addresses */
865 1.131 dyoung nd6_na_output(ifa->ifa_ifp, &in6_all, in6,
866 1.131 dyoung (anycast ? 0 : ND_NA_FLAG_OVERRIDE)
867 1.131 dyoung #if 0
868 1.131 dyoung | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0)
869 1.131 dyoung #endif
870 1.131 dyoung , 1, NULL);
871 1.131 dyoung return;
872 1.131 dyoung }
873 1.131 dyoung
874 1.270 roy if ((rt->rt_flags & RTF_GATEWAY) != 0) {
875 1.270 roy if (req != RTM_ADD)
876 1.270 roy return;
877 1.270 roy /*
878 1.270 roy * linklayers with particular MTU limitation.
879 1.270 roy */
880 1.270 roy switch(ifp->if_type) {
881 1.270 roy #if NARCNET > 0
882 1.270 roy case IFT_ARCNET:
883 1.270 roy if (rt->rt_rmx.rmx_mtu > ARC_PHDS_MAXMTU) /* RFC2497 */
884 1.270 roy rt->rt_rmx.rmx_mtu = ARC_PHDS_MAXMTU;
885 1.270 roy break;
886 1.270 roy #endif
887 1.270 roy }
888 1.2 itojun return;
889 1.270 roy }
890 1.2 itojun
891 1.54 itojun if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) {
892 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
893 1.54 itojun /*
894 1.54 itojun * This is probably an interface direct route for a link
895 1.54 itojun * which does not need neighbor caches (e.g. fe80::%lo0/64).
896 1.54 itojun * We do not need special treatment below for such a route.
897 1.54 itojun * Moreover, the RTF_LLINFO flag which would be set below
898 1.54 itojun * would annoy the ndp(8) command.
899 1.54 itojun */
900 1.54 itojun return;
901 1.54 itojun }
902 1.54 itojun
903 1.2 itojun switch (req) {
904 1.205 ozaki case RTM_ADD: {
905 1.238 ozaki struct psref psref;
906 1.205 ozaki
907 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
908 1.2 itojun /*
909 1.2 itojun * There is no backward compatibility :)
910 1.2 itojun *
911 1.2 itojun * if ((rt->rt_flags & RTF_HOST) == 0 &&
912 1.2 itojun * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff)
913 1.2 itojun * rt->rt_flags |= RTF_CLONING;
914 1.2 itojun */
915 1.188 ozaki /* XXX should move to route.c? */
916 1.188 ozaki if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) {
917 1.120 dyoung union {
918 1.120 dyoung struct sockaddr sa;
919 1.120 dyoung struct sockaddr_dl sdl;
920 1.120 dyoung struct sockaddr_storage ss;
921 1.120 dyoung } u;
922 1.2 itojun /*
923 1.84 itojun * Case 1: This route should come from a route to
924 1.84 itojun * interface (RTF_CLONING case) or the route should be
925 1.84 itojun * treated as on-link but is currently not
926 1.129 dyoung * (RTF_LLINFO && ln == NULL case).
927 1.2 itojun */
928 1.136 dyoung if (sockaddr_dl_init(&u.sdl, sizeof(u.ss),
929 1.120 dyoung ifp->if_index, ifp->if_type,
930 1.136 dyoung NULL, namelen, NULL, addrlen) == NULL) {
931 1.136 dyoung printf("%s.%d: sockaddr_dl_init(, %zu, ) "
932 1.136 dyoung "failed on %s\n", __func__, __LINE__,
933 1.136 dyoung sizeof(u.ss), if_name(ifp));
934 1.136 dyoung }
935 1.120 dyoung rt_setgate(rt, &u.sa);
936 1.119 dyoung gate = rt->rt_gateway;
937 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
938 1.192 ozaki if (gate == NULL) {
939 1.192 ozaki log(LOG_ERR,
940 1.192 ozaki "%s: rt_setgate failed on %s\n", __func__,
941 1.192 ozaki if_name(ifp));
942 1.192 ozaki break;
943 1.192 ozaki }
944 1.188 ozaki
945 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
946 1.188 ozaki if ((rt->rt_flags & RTF_CONNECTED) != 0)
947 1.2 itojun break;
948 1.2 itojun }
949 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
950 1.18 itojun /*
951 1.18 itojun * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here.
952 1.18 itojun * We don't do that here since llinfo is not ready yet.
953 1.18 itojun *
954 1.18 itojun * There are also couple of other things to be discussed:
955 1.18 itojun * - unsolicited NA code needs improvement beforehand
956 1.18 itojun * - RFC2461 says we MAY send multicast unsolicited NA
957 1.18 itojun * (7.2.6 paragraph 4), however, it also says that we
958 1.18 itojun * SHOULD provide a mechanism to prevent multicast NA storm.
959 1.18 itojun * we don't have anything like it right now.
960 1.38 itojun * note that the mechanism needs a mutual agreement
961 1.18 itojun * between proxies, which means that we need to implement
962 1.38 itojun * a new protocol, or a new kludge.
963 1.38 itojun * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA.
964 1.18 itojun * we need to check ip6forwarding before sending it.
965 1.18 itojun * (or should we allow proxy ND configuration only for
966 1.18 itojun * routers? there's no mention about proxy ND from hosts)
967 1.18 itojun */
968 1.18 itojun #if 0
969 1.18 itojun /* XXX it does not work */
970 1.2 itojun if (rt->rt_flags & RTF_ANNOUNCE)
971 1.2 itojun nd6_na_output(ifp,
972 1.118 dyoung &satocsin6(rt_getkey(rt))->sin6_addr,
973 1.118 dyoung &satocsin6(rt_getkey(rt))->sin6_addr,
974 1.18 itojun ip6_forwarding ? ND_NA_FLAG_ROUTER : 0,
975 1.18 itojun 1, NULL);
976 1.18 itojun #endif
977 1.188 ozaki
978 1.65 itojun if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) {
979 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
980 1.26 itojun /*
981 1.26 itojun * Address resolution isn't necessary for a point to
982 1.26 itojun * point link, so we can skip this test for a p2p link.
983 1.26 itojun */
984 1.26 itojun if (gate->sa_family != AF_LINK ||
985 1.119 dyoung gate->sa_len <
986 1.119 dyoung sockaddr_dl_measure(namelen, addrlen)) {
987 1.26 itojun log(LOG_DEBUG,
988 1.36 itojun "nd6_rtrequest: bad gateway value: %s\n",
989 1.36 itojun if_name(ifp));
990 1.26 itojun break;
991 1.26 itojun }
992 1.118 dyoung satosdl(gate)->sdl_type = ifp->if_type;
993 1.118 dyoung satosdl(gate)->sdl_index = ifp->if_index;
994 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
995 1.2 itojun }
996 1.144 joerg RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt));
997 1.2 itojun
998 1.143 christos /*
999 1.198 ozaki * When called from rt_ifa_addlocal, we cannot depend on that
1000 1.198 ozaki * the address (rt_getkey(rt)) exits in the address list of the
1001 1.198 ozaki * interface. So check RTF_LOCAL instead.
1002 1.198 ozaki */
1003 1.198 ozaki if (rt->rt_flags & RTF_LOCAL) {
1004 1.198 ozaki if (nd6_useloopback)
1005 1.198 ozaki rt->rt_ifp = lo0ifp; /* XXX */
1006 1.198 ozaki break;
1007 1.198 ozaki }
1008 1.198 ozaki
1009 1.198 ozaki /*
1010 1.117 dyoung * check if rt_getkey(rt) is an address assigned
1011 1.2 itojun * to the interface.
1012 1.2 itojun */
1013 1.238 ozaki ifa = (struct ifaddr *)in6ifa_ifpwithaddr_psref(ifp,
1014 1.238 ozaki &satocsin6(rt_getkey(rt))->sin6_addr, &psref);
1015 1.129 dyoung if (ifa != NULL) {
1016 1.2 itojun if (nd6_useloopback) {
1017 1.195 ozaki rt->rt_ifp = lo0ifp; /* XXX */
1018 1.2 itojun /*
1019 1.2 itojun * Make sure rt_ifa be equal to the ifaddr
1020 1.2 itojun * corresponding to the address.
1021 1.2 itojun * We need this because when we refer
1022 1.2 itojun * rt_ifa->ia6_flags in ip6_input, we assume
1023 1.2 itojun * that the rt_ifa points to the address instead
1024 1.2 itojun * of the loopback address.
1025 1.2 itojun */
1026 1.251 ozaki if (!ISSET(info->rti_flags, RTF_DONTCHANGEIFA)
1027 1.251 ozaki && ifa != rt->rt_ifa)
1028 1.106 dyoung rt_replace_ifa(rt, ifa);
1029 1.2 itojun }
1030 1.18 itojun } else if (rt->rt_flags & RTF_ANNOUNCE) {
1031 1.18 itojun /* join solicited node multicast for proxy ND */
1032 1.18 itojun if (ifp->if_flags & IFF_MULTICAST) {
1033 1.18 itojun struct in6_addr llsol;
1034 1.18 itojun int error;
1035 1.18 itojun
1036 1.118 dyoung llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1037 1.96 rpaulo llsol.s6_addr32[0] = htonl(0xff020000);
1038 1.18 itojun llsol.s6_addr32[1] = 0;
1039 1.18 itojun llsol.s6_addr32[2] = htonl(1);
1040 1.18 itojun llsol.s6_addr8[12] = 0xff;
1041 1.96 rpaulo if (in6_setscope(&llsol, ifp, NULL))
1042 1.188 ozaki goto out;
1043 1.99 rpaulo if (!in6_addmulti(&llsol, ifp, &error, 0)) {
1044 1.225 ryo char ip6buf[INET6_ADDRSTRLEN];
1045 1.187 ozaki nd6log(LOG_ERR, "%s: failed to join "
1046 1.54 itojun "%s (errno=%d)\n", if_name(ifp),
1047 1.226 christos IN6_PRINT(ip6buf, &llsol), error);
1048 1.46 itojun }
1049 1.18 itojun }
1050 1.2 itojun }
1051 1.188 ozaki out:
1052 1.238 ozaki ifa_release(ifa, &psref);
1053 1.181 ozaki /*
1054 1.181 ozaki * If we have too many cache entries, initiate immediate
1055 1.181 ozaki * purging for some entries.
1056 1.181 ozaki */
1057 1.188 ozaki if (rt->rt_ifp != NULL)
1058 1.207 ozaki nd6_gc_neighbors(LLTABLE6(rt->rt_ifp), NULL);
1059 1.2 itojun break;
1060 1.205 ozaki }
1061 1.2 itojun
1062 1.2 itojun case RTM_DELETE:
1063 1.18 itojun /* leave from solicited node multicast for proxy ND */
1064 1.18 itojun if ((rt->rt_flags & RTF_ANNOUNCE) != 0 &&
1065 1.18 itojun (ifp->if_flags & IFF_MULTICAST) != 0) {
1066 1.18 itojun struct in6_addr llsol;
1067 1.18 itojun
1068 1.118 dyoung llsol = satocsin6(rt_getkey(rt))->sin6_addr;
1069 1.96 rpaulo llsol.s6_addr32[0] = htonl(0xff020000);
1070 1.18 itojun llsol.s6_addr32[1] = 0;
1071 1.18 itojun llsol.s6_addr32[2] = htonl(1);
1072 1.18 itojun llsol.s6_addr8[12] = 0xff;
1073 1.249 ozaki if (in6_setscope(&llsol, ifp, NULL) == 0)
1074 1.249 ozaki in6_lookup_and_delete_multi(&llsol, ifp);
1075 1.18 itojun }
1076 1.188 ozaki break;
1077 1.2 itojun }
1078 1.2 itojun }
1079 1.2 itojun
1080 1.271 roy static void
1081 1.271 roy nd6_setifflags(struct ifnet *ifp, uint32_t flags)
1082 1.2 itojun {
1083 1.271 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
1084 1.271 roy struct ifaddr *ifa;
1085 1.271 roy struct in6_ifaddr *ia;
1086 1.271 roy int s;
1087 1.2 itojun
1088 1.271 roy if (ndi->flags & ND6_IFF_IFDISABLED && !(flags & ND6_IFF_IFDISABLED)) {
1089 1.65 itojun /*
1090 1.271 roy * If the interface is marked as ND6_IFF_IFDISABLED and
1091 1.271 roy * has a link-local address with IN6_IFF_DUPLICATED,
1092 1.271 roy * do not clear ND6_IFF_IFDISABLED.
1093 1.271 roy * See RFC 4862, section 5.4.5.
1094 1.65 itojun */
1095 1.271 roy bool duplicated_linklocal = false;
1096 1.271 roy
1097 1.271 roy s = pserialize_read_enter();
1098 1.271 roy IFADDR_READER_FOREACH(ifa, ifp) {
1099 1.271 roy if (ifa->ifa_addr->sa_family != AF_INET6)
1100 1.271 roy continue;
1101 1.271 roy ia = (struct in6_ifaddr *)ifa;
1102 1.271 roy if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1103 1.271 roy IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1104 1.271 roy {
1105 1.271 roy duplicated_linklocal = true;
1106 1.108 dyoung break;
1107 1.271 roy }
1108 1.271 roy }
1109 1.271 roy pserialize_read_exit(s);
1110 1.2 itojun
1111 1.271 roy if (duplicated_linklocal) {
1112 1.271 roy flags |= ND6_IFF_IFDISABLED;
1113 1.271 roy log(LOG_ERR, "%s: Cannot enable an interface"
1114 1.271 roy " with a link-local address marked"
1115 1.271 roy " duplicate.\n", if_name(ifp));
1116 1.271 roy } else {
1117 1.271 roy ndi->flags &= ~ND6_IFF_IFDISABLED;
1118 1.271 roy if (ifp->if_flags & IFF_UP)
1119 1.271 roy in6_if_up(ifp);
1120 1.2 itojun }
1121 1.271 roy } else if (!(ndi->flags & ND6_IFF_IFDISABLED) &&
1122 1.271 roy (flags & ND6_IFF_IFDISABLED))
1123 1.271 roy {
1124 1.271 roy struct psref psref;
1125 1.271 roy int bound = curlwp_bind();
1126 1.271 roy
1127 1.271 roy /* Mark all IPv6 addresses as tentative. */
1128 1.2 itojun
1129 1.271 roy ndi->flags |= ND6_IFF_IFDISABLED;
1130 1.271 roy s = pserialize_read_enter();
1131 1.271 roy IFADDR_READER_FOREACH(ifa, ifp) {
1132 1.271 roy if (ifa->ifa_addr->sa_family != AF_INET6)
1133 1.271 roy continue;
1134 1.271 roy ifa_acquire(ifa, &psref);
1135 1.271 roy pserialize_read_exit(s);
1136 1.2 itojun
1137 1.271 roy nd6_dad_stop(ifa);
1138 1.2 itojun
1139 1.271 roy ia = (struct in6_ifaddr *)ifa;
1140 1.271 roy ia->ia6_flags |= IN6_IFF_TENTATIVE;
1141 1.12 itojun
1142 1.271 roy s = pserialize_read_enter();
1143 1.271 roy ifa_release(ifa, &psref);
1144 1.99 rpaulo }
1145 1.271 roy pserialize_read_exit(s);
1146 1.271 roy curlwp_bindx(bound);
1147 1.271 roy }
1148 1.99 rpaulo
1149 1.271 roy if (flags & ND6_IFF_AUTO_LINKLOCAL) {
1150 1.271 roy if (!(ndi->flags & ND6_IFF_AUTO_LINKLOCAL)) {
1151 1.271 roy /* auto_linklocal 0->1 transition */
1152 1.148 roy
1153 1.271 roy ndi->flags |= ND6_IFF_AUTO_LINKLOCAL;
1154 1.271 roy in6_ifattach(ifp, NULL);
1155 1.271 roy } else if (!(flags & ND6_IFF_IFDISABLED) &&
1156 1.271 roy ifp->if_flags & IFF_UP)
1157 1.148 roy {
1158 1.148 roy /*
1159 1.271 roy * When the IF already has
1160 1.271 roy * ND6_IFF_AUTO_LINKLOCAL, no link-local
1161 1.271 roy * address is assigned, and IFF_UP, try to
1162 1.271 roy * assign one.
1163 1.148 roy */
1164 1.271 roy bool haslinklocal = 0;
1165 1.148 roy
1166 1.205 ozaki s = pserialize_read_enter();
1167 1.202 ozaki IFADDR_READER_FOREACH(ifa, ifp) {
1168 1.271 roy if (ifa->ifa_addr->sa_family !=AF_INET6)
1169 1.148 roy continue;
1170 1.148 roy ia = (struct in6_ifaddr *)ifa;
1171 1.271 roy if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){
1172 1.271 roy haslinklocal = true;
1173 1.148 roy break;
1174 1.148 roy }
1175 1.148 roy }
1176 1.205 ozaki pserialize_read_exit(s);
1177 1.271 roy if (!haslinklocal)
1178 1.271 roy in6_ifattach(ifp, NULL);
1179 1.148 roy }
1180 1.271 roy }
1181 1.151 roy
1182 1.271 roy ndi->flags = flags;
1183 1.271 roy }
1184 1.151 roy
1185 1.271 roy int
1186 1.271 roy nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp)
1187 1.271 roy {
1188 1.271 roy #ifdef OSIOCGIFINFO_IN6_90
1189 1.271 roy struct in6_ndireq90 *ondi = (struct in6_ndireq90 *)data;
1190 1.271 roy struct in6_ndifreq90 *ndif = (struct in6_ndifreq90 *)data;
1191 1.271 roy #define OND ondi->ndi
1192 1.271 roy #endif
1193 1.271 roy struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1194 1.271 roy struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1195 1.271 roy struct nd_kifinfo *ifndi = ND_IFINFO(ifp);
1196 1.271 roy int error = 0;
1197 1.271 roy #define ND ndi->ndi
1198 1.151 roy
1199 1.271 roy switch (cmd) {
1200 1.271 roy #ifdef OSIOCSRTRFLUSH_IN6
1201 1.271 roy case OSIOCGDRLST_IN6: /* FALLTHROUGH */
1202 1.271 roy case OSIOCGPRLST_IN6: /* FALLTHROUGH */
1203 1.271 roy case OSIOCSNDFLUSH_IN6: /* FALLTHROUGH */
1204 1.271 roy case OSIOCSPFXFLUSH_IN6: /* FALLTHROUGH */
1205 1.271 roy case OSIOCSRTRFLUSH_IN6: /* FALLTHROUGH */
1206 1.271 roy break;
1207 1.271 roy case OSIOCGDEFIFACE_IN6:
1208 1.271 roy ndif->ifindex = 0;
1209 1.26 itojun break;
1210 1.271 roy case OSIOCSDEFIFACE_IN6:
1211 1.271 roy error = ENOTSUP;
1212 1.2 itojun break;
1213 1.271 roy #endif
1214 1.271 roy #ifdef OSIOCGIFINFO_IN6
1215 1.271 roy case OSIOCGIFINFO_IN6: /* FALLTHROUGH */
1216 1.271 roy #endif
1217 1.271 roy #ifdef OSIOCGIFINFO_IN6_90
1218 1.271 roy case OSIOCGIFINFO_IN6_90:
1219 1.271 roy memset(&OND, 0, sizeof(OND));
1220 1.271 roy OND.initialized = 1;
1221 1.271 roy OND.chlim = ifndi->chlim;
1222 1.271 roy OND.basereachable = ifndi->basereachable;
1223 1.271 roy OND.retrans = ifndi->retrans;
1224 1.271 roy OND.flags = ifndi->flags;
1225 1.271 roy break;
1226 1.271 roy case OSIOCSIFINFO_IN6_90:
1227 1.271 roy /* Allow userland to set Neighour Unreachability Detection
1228 1.271 roy * timers. */
1229 1.271 roy if (OND.chlim != 0)
1230 1.271 roy ifndi->chlim = OND.chlim;
1231 1.271 roy if (OND.basereachable != 0 &&
1232 1.271 roy OND.basereachable != ifndi->basereachable)
1233 1.271 roy {
1234 1.271 roy ifndi->basereachable = OND.basereachable;
1235 1.271 roy ifndi->reachable = ND_COMPUTE_RTIME(OND.basereachable);
1236 1.2 itojun }
1237 1.271 roy if (OND.retrans != 0)
1238 1.271 roy ifndi->retrans = OND.retrans;
1239 1.271 roy /* Retain the old behaviour .... */
1240 1.271 roy /* FALLTHROUGH */
1241 1.271 roy case OSIOCSIFINFO_FLAGS_90:
1242 1.271 roy nd6_setifflags(ifp, OND.flags);
1243 1.2 itojun break;
1244 1.271 roy #undef OND
1245 1.252 roy #endif
1246 1.271 roy case SIOCGIFINFO_IN6:
1247 1.271 roy ND.chlim = ifndi->chlim;
1248 1.271 roy ND.basereachable = ifndi->basereachable;
1249 1.271 roy ND.retrans = ifndi->retrans;
1250 1.271 roy ND.flags = ifndi->flags;
1251 1.271 roy break;
1252 1.271 roy case SIOCSIFINFO_IN6:
1253 1.271 roy /* Allow userland to set Neighour Unreachability Detection
1254 1.271 roy * timers. */
1255 1.271 roy if (ND.chlim != 0)
1256 1.271 roy ifndi->chlim = ND.chlim;
1257 1.271 roy if (ND.basereachable != 0 &&
1258 1.271 roy ND.basereachable != ifndi->basereachable)
1259 1.271 roy {
1260 1.271 roy ifndi->basereachable = ND.basereachable;
1261 1.271 roy ifndi->reachable = ND_COMPUTE_RTIME(ND.basereachable);
1262 1.2 itojun }
1263 1.271 roy if (ND.retrans != 0)
1264 1.271 roy ifndi->retrans = ND.retrans;
1265 1.271 roy break;
1266 1.271 roy case SIOCSIFINFO_FLAGS:
1267 1.271 roy nd6_setifflags(ifp, ND.flags);
1268 1.2 itojun break;
1269 1.271 roy #undef ND
1270 1.2 itojun case SIOCGNBRINFO_IN6:
1271 1.99 rpaulo {
1272 1.181 ozaki struct llentry *ln;
1273 1.12 itojun struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1274 1.12 itojun
1275 1.96 rpaulo if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1276 1.112 dyoung return error;
1277 1.2 itojun
1278 1.188 ozaki ln = nd6_lookup(&nb_addr, ifp, false);
1279 1.165 ozaki if (ln == NULL) {
1280 1.12 itojun error = EINVAL;
1281 1.12 itojun break;
1282 1.12 itojun }
1283 1.12 itojun nbi->state = ln->ln_state;
1284 1.12 itojun nbi->asked = ln->ln_asked;
1285 1.12 itojun nbi->isrouter = ln->ln_router;
1286 1.166 ozaki nbi->expire = ln->ln_expire ?
1287 1.166 ozaki time_mono_to_wall(ln->ln_expire) : 0;
1288 1.188 ozaki LLE_RUNLOCK(ln);
1289 1.63 itojun
1290 1.12 itojun break;
1291 1.65 itojun }
1292 1.2 itojun }
1293 1.112 dyoung return error;
1294 1.2 itojun }
1295 1.2 itojun
1296 1.115 dyoung void
1297 1.188 ozaki nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp)
1298 1.115 dyoung {
1299 1.115 dyoung struct mbuf *m_hold, *m_hold_next;
1300 1.188 ozaki struct sockaddr_in6 sin6;
1301 1.188 ozaki
1302 1.188 ozaki LLE_WLOCK_ASSERT(ln);
1303 1.188 ozaki
1304 1.188 ozaki sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0);
1305 1.115 dyoung
1306 1.188 ozaki m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0;
1307 1.188 ozaki
1308 1.277 ozaki LLE_ADDREF(ln);
1309 1.188 ozaki LLE_WUNLOCK(ln);
1310 1.188 ozaki for (; m_hold != NULL; m_hold = m_hold_next) {
1311 1.115 dyoung m_hold_next = m_hold->m_nextpkt;
1312 1.115 dyoung m_hold->m_nextpkt = NULL;
1313 1.115 dyoung
1314 1.115 dyoung /*
1315 1.115 dyoung * we assume ifp is not a p2p here, so
1316 1.115 dyoung * just set the 2nd argument as the
1317 1.115 dyoung * 1st one.
1318 1.115 dyoung */
1319 1.227 ozaki ip6_if_output(ifp, ifp, m_hold, &sin6, NULL);
1320 1.115 dyoung }
1321 1.188 ozaki LLE_WLOCK(ln);
1322 1.277 ozaki LLE_REMREF(ln);
1323 1.115 dyoung }
1324 1.115 dyoung
1325 1.2 itojun /*
1326 1.2 itojun * Create neighbor cache entry and cache link-layer address,
1327 1.52 itojun * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1328 1.2 itojun */
1329 1.165 ozaki void
1330 1.105 christos nd6_cache_lladdr(
1331 1.105 christos struct ifnet *ifp,
1332 1.105 christos struct in6_addr *from,
1333 1.105 christos char *lladdr,
1334 1.107 christos int lladdrlen,
1335 1.105 christos int type, /* ICMP6 type */
1336 1.105 christos int code /* type dependent information */
1337 1.105 christos )
1338 1.2 itojun {
1339 1.181 ozaki struct llentry *ln = NULL;
1340 1.2 itojun int is_newentry;
1341 1.2 itojun int do_update;
1342 1.2 itojun int olladdr;
1343 1.2 itojun int llchange;
1344 1.2 itojun int newstate = 0;
1345 1.2 itojun
1346 1.163 ozaki KASSERT(ifp != NULL);
1347 1.163 ozaki KASSERT(from != NULL);
1348 1.2 itojun
1349 1.2 itojun /* nothing must be updated for unspecified address */
1350 1.2 itojun if (IN6_IS_ADDR_UNSPECIFIED(from))
1351 1.165 ozaki return;
1352 1.2 itojun
1353 1.2 itojun /*
1354 1.2 itojun * Validation about ifp->if_addrlen and lladdrlen must be done in
1355 1.2 itojun * the caller.
1356 1.2 itojun *
1357 1.2 itojun * XXX If the link does not have link-layer adderss, what should
1358 1.2 itojun * we do? (ifp->if_addrlen == 0)
1359 1.2 itojun * Spec says nothing in sections for RA, RS and NA. There's small
1360 1.2 itojun * description on it in NS section (RFC 2461 7.2.3).
1361 1.2 itojun */
1362 1.2 itojun
1363 1.188 ozaki ln = nd6_lookup(from, ifp, true);
1364 1.188 ozaki if (ln == NULL) {
1365 1.2 itojun #if 0
1366 1.2 itojun /* nothing must be done if there's no lladdr */
1367 1.2 itojun if (!lladdr || !lladdrlen)
1368 1.2 itojun return NULL;
1369 1.2 itojun #endif
1370 1.2 itojun
1371 1.188 ozaki ln = nd6_create(from, ifp);
1372 1.2 itojun is_newentry = 1;
1373 1.43 itojun } else {
1374 1.43 itojun /* do nothing if static ndp is set */
1375 1.188 ozaki if (ln->la_flags & LLE_STATIC) {
1376 1.188 ozaki LLE_WUNLOCK(ln);
1377 1.165 ozaki return;
1378 1.165 ozaki }
1379 1.2 itojun is_newentry = 0;
1380 1.43 itojun }
1381 1.2 itojun
1382 1.188 ozaki if (ln == NULL)
1383 1.165 ozaki return;
1384 1.2 itojun
1385 1.188 ozaki olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
1386 1.2 itojun if (olladdr && lladdr) {
1387 1.188 ozaki llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen);
1388 1.2 itojun } else
1389 1.2 itojun llchange = 0;
1390 1.2 itojun
1391 1.2 itojun /*
1392 1.2 itojun * newentry olladdr lladdr llchange (*=record)
1393 1.2 itojun * 0 n n -- (1)
1394 1.2 itojun * 0 y n -- (2)
1395 1.2 itojun * 0 n y -- (3) * STALE
1396 1.2 itojun * 0 y y n (4) *
1397 1.2 itojun * 0 y y y (5) * STALE
1398 1.2 itojun * 1 -- n -- (6) NOSTATE(= PASSIVE)
1399 1.2 itojun * 1 -- y -- (7) * STALE
1400 1.2 itojun */
1401 1.2 itojun
1402 1.52 itojun if (lladdr) { /* (3-5) and (7) */
1403 1.2 itojun /*
1404 1.2 itojun * Record source link-layer address
1405 1.2 itojun * XXX is it dependent to ifp->if_type?
1406 1.2 itojun */
1407 1.188 ozaki memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen);
1408 1.188 ozaki ln->la_flags |= LLE_VALID;
1409 1.2 itojun }
1410 1.2 itojun
1411 1.2 itojun if (!is_newentry) {
1412 1.65 itojun if ((!olladdr && lladdr) || /* (3) */
1413 1.65 itojun (olladdr && lladdr && llchange)) { /* (5) */
1414 1.2 itojun do_update = 1;
1415 1.272 roy newstate = ND_LLINFO_STALE;
1416 1.52 itojun } else /* (1-2,4) */
1417 1.2 itojun do_update = 0;
1418 1.2 itojun } else {
1419 1.2 itojun do_update = 1;
1420 1.99 rpaulo if (lladdr == NULL) /* (6) */
1421 1.272 roy newstate = ND_LLINFO_NOSTATE;
1422 1.52 itojun else /* (7) */
1423 1.272 roy newstate = ND_LLINFO_STALE;
1424 1.2 itojun }
1425 1.2 itojun
1426 1.2 itojun if (do_update) {
1427 1.2 itojun /*
1428 1.2 itojun * Update the state of the neighbor cache.
1429 1.2 itojun */
1430 1.2 itojun ln->ln_state = newstate;
1431 1.2 itojun
1432 1.272 roy if (ln->ln_state == ND_LLINFO_STALE) {
1433 1.44 itojun /*
1434 1.44 itojun * XXX: since nd6_output() below will cause
1435 1.44 itojun * state tansition to DELAY and reset the timer,
1436 1.44 itojun * we must set the timer now, although it is actually
1437 1.44 itojun * meaningless.
1438 1.44 itojun */
1439 1.272 roy nd_set_timer(ln, ND_TIMER_GC);
1440 1.44 itojun
1441 1.188 ozaki nd6_llinfo_release_pkts(ln, ifp);
1442 1.272 roy } else if (ln->ln_state == ND_LLINFO_INCOMPLETE) {
1443 1.2 itojun /* probe right away */
1444 1.272 roy nd_set_timer(ln, ND_TIMER_IMMEDIATE);
1445 1.2 itojun }
1446 1.2 itojun }
1447 1.2 itojun
1448 1.2 itojun /*
1449 1.2 itojun * ICMP6 type dependent behavior.
1450 1.2 itojun *
1451 1.2 itojun * NS: clear IsRouter if new entry
1452 1.2 itojun * RS: clear IsRouter
1453 1.2 itojun * RA: set IsRouter if there's lladdr
1454 1.2 itojun * redir: clear IsRouter if new entry
1455 1.2 itojun *
1456 1.2 itojun * RA case, (1):
1457 1.2 itojun * The spec says that we must set IsRouter in the following cases:
1458 1.2 itojun * - If lladdr exist, set IsRouter. This means (1-5).
1459 1.2 itojun * - If it is old entry (!newentry), set IsRouter. This means (7).
1460 1.2 itojun * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1461 1.278 andvar * A question arises for (1) case. (1) case has no lladdr in the
1462 1.2 itojun * neighbor cache, this is similar to (6).
1463 1.2 itojun * This case is rare but we figured that we MUST NOT set IsRouter.
1464 1.2 itojun *
1465 1.2 itojun * newentry olladdr lladdr llchange NS RS RA redir
1466 1.8 itojun * D R
1467 1.8 itojun * 0 n n -- (1) c ? s
1468 1.8 itojun * 0 y n -- (2) c s s
1469 1.8 itojun * 0 n y -- (3) c s s
1470 1.8 itojun * 0 y y n (4) c s s
1471 1.8 itojun * 0 y y y (5) c s s
1472 1.8 itojun * 1 -- n -- (6) c c c s
1473 1.8 itojun * 1 -- y -- (7) c c s c s
1474 1.2 itojun *
1475 1.2 itojun * (c=clear s=set)
1476 1.2 itojun */
1477 1.2 itojun switch (type & 0xff) {
1478 1.2 itojun case ND_NEIGHBOR_SOLICIT:
1479 1.2 itojun /*
1480 1.2 itojun * New entry must have is_router flag cleared.
1481 1.2 itojun */
1482 1.52 itojun if (is_newentry) /* (6-7) */
1483 1.8 itojun ln->ln_router = 0;
1484 1.8 itojun break;
1485 1.8 itojun case ND_REDIRECT:
1486 1.8 itojun /*
1487 1.8 itojun * If the icmp is a redirect to a better router, always set the
1488 1.52 itojun * is_router flag. Otherwise, if the entry is newly created,
1489 1.52 itojun * clear the flag. [RFC 2461, sec 8.3]
1490 1.8 itojun */
1491 1.8 itojun if (code == ND_REDIRECT_ROUTER)
1492 1.8 itojun ln->ln_router = 1;
1493 1.52 itojun else if (is_newentry) /* (6-7) */
1494 1.2 itojun ln->ln_router = 0;
1495 1.2 itojun break;
1496 1.2 itojun case ND_ROUTER_SOLICIT:
1497 1.2 itojun /*
1498 1.2 itojun * is_router flag must always be cleared.
1499 1.2 itojun */
1500 1.2 itojun ln->ln_router = 0;
1501 1.2 itojun break;
1502 1.2 itojun case ND_ROUTER_ADVERT:
1503 1.2 itojun /*
1504 1.2 itojun * Mark an entry with lladdr as a router.
1505 1.2 itojun */
1506 1.65 itojun if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */
1507 1.65 itojun (is_newentry && lladdr)) { /* (7) */
1508 1.2 itojun ln->ln_router = 1;
1509 1.2 itojun }
1510 1.2 itojun break;
1511 1.2 itojun }
1512 1.47 itojun
1513 1.262 roy if (do_update && lladdr != NULL) {
1514 1.259 roy struct sockaddr_in6 sin6;
1515 1.259 roy
1516 1.259 roy sockaddr_in6_init(&sin6, from, 0, 0, 0);
1517 1.259 roy rt_clonedmsg(is_newentry ? RTM_ADD : RTM_CHANGE,
1518 1.267 roy NULL, sin6tosa(&sin6), lladdr, ifp);
1519 1.259 roy }
1520 1.188 ozaki
1521 1.271 roy if (ln != NULL)
1522 1.188 ozaki LLE_WUNLOCK(ln);
1523 1.188 ozaki
1524 1.188 ozaki /*
1525 1.188 ozaki * If we have too many cache entries, initiate immediate
1526 1.188 ozaki * purging for some entries.
1527 1.188 ozaki */
1528 1.188 ozaki if (is_newentry)
1529 1.207 ozaki nd6_gc_neighbors(LLTABLE6(ifp), &ln->r_l3addr.addr6);
1530 1.2 itojun }
1531 1.2 itojun
1532 1.2 itojun static void
1533 1.107 christos nd6_slowtimo(void *ignored_arg)
1534 1.2 itojun {
1535 1.271 roy struct nd_kifinfo *ndi;
1536 1.58 itojun struct ifnet *ifp;
1537 1.279 riastrad struct psref psref;
1538 1.194 ozaki int s;
1539 1.2 itojun
1540 1.239 ozaki SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
1541 1.170 ozaki callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
1542 1.20 thorpej nd6_slowtimo, NULL);
1543 1.194 ozaki
1544 1.194 ozaki s = pserialize_read_enter();
1545 1.194 ozaki IFNET_READER_FOREACH(ifp) {
1546 1.271 roy ndi = ND_IFINFO(ifp);
1547 1.271 roy if (ndi->basereachable && /* already initialized */
1548 1.271 roy (ndi->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) {
1549 1.279 riastrad if_acquire(ifp, &psref);
1550 1.279 riastrad pserialize_read_exit(s);
1551 1.2 itojun /*
1552 1.2 itojun * Since reachable time rarely changes by router
1553 1.2 itojun * advertisements, we SHOULD insure that a new random
1554 1.2 itojun * value gets recomputed at least once every few hours.
1555 1.2 itojun * (RFC 2461, 6.3.4)
1556 1.2 itojun */
1557 1.271 roy ndi->recalctm = nd6_recalc_reachtm_interval;
1558 1.271 roy ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable);
1559 1.279 riastrad s = pserialize_read_enter();
1560 1.279 riastrad if_release(ifp, &psref);
1561 1.2 itojun }
1562 1.2 itojun }
1563 1.194 ozaki pserialize_read_exit(s);
1564 1.194 ozaki
1565 1.239 ozaki SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
1566 1.2 itojun }
1567 1.2 itojun
1568 1.227 ozaki /*
1569 1.227 ozaki * Return 0 if a neighbor cache is found. Return EWOULDBLOCK if a cache is not
1570 1.227 ozaki * found and trying to resolve a neighbor; in this case the mbuf is queued in
1571 1.227 ozaki * the list. Otherwise return errno after freeing the mbuf.
1572 1.227 ozaki */
1573 1.188 ozaki int
1574 1.227 ozaki nd6_resolve(struct ifnet *ifp, const struct rtentry *rt, struct mbuf *m,
1575 1.227 ozaki const struct sockaddr *_dst, uint8_t *lldst, size_t dstsize)
1576 1.176 ozaki {
1577 1.181 ozaki struct llentry *ln = NULL;
1578 1.188 ozaki bool created = false;
1579 1.227 ozaki const struct sockaddr_in6 *dst = satocsin6(_dst);
1580 1.263 roy int error;
1581 1.271 roy struct nd_kifinfo *ndi = ND_IFINFO(ifp);
1582 1.193 ozaki
1583 1.227 ozaki /* discard the packet if IPv6 operation is disabled on the interface */
1584 1.271 roy if (ndi->flags & ND6_IFF_IFDISABLED) {
1585 1.227 ozaki m_freem(m);
1586 1.227 ozaki return ENETDOWN; /* better error? */
1587 1.193 ozaki }
1588 1.193 ozaki
1589 1.2 itojun /*
1590 1.2 itojun * Address resolution or Neighbor Unreachability Detection
1591 1.2 itojun * for the next hop.
1592 1.2 itojun * At this point, the destination of the packet must be a unicast
1593 1.2 itojun * or an anycast address(i.e. not a multicast).
1594 1.2 itojun */
1595 1.2 itojun
1596 1.2 itojun /* Look up the neighbor cache for the nexthop */
1597 1.227 ozaki ln = nd6_lookup(&dst->sin6_addr, ifp, false);
1598 1.227 ozaki
1599 1.255 ozaki if (ln != NULL && (ln->la_flags & LLE_VALID) != 0 &&
1600 1.272 roy ln->ln_state == ND_LLINFO_REACHABLE) {
1601 1.227 ozaki /* Fast path */
1602 1.227 ozaki memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen));
1603 1.227 ozaki LLE_RUNLOCK(ln);
1604 1.227 ozaki return 0;
1605 1.227 ozaki }
1606 1.227 ozaki if (ln != NULL)
1607 1.227 ozaki LLE_RUNLOCK(ln);
1608 1.227 ozaki
1609 1.227 ozaki /* Slow path */
1610 1.188 ozaki ln = nd6_lookup(&dst->sin6_addr, ifp, true);
1611 1.227 ozaki if (ln == NULL && nd6_is_addr_neighbor(dst, ifp)) {
1612 1.29 itojun /*
1613 1.29 itojun * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
1614 1.52 itojun * the condition below is not very efficient. But we believe
1615 1.29 itojun * it is tolerable, because this should be a rare case.
1616 1.29 itojun */
1617 1.188 ozaki ln = nd6_create(&dst->sin6_addr, ifp);
1618 1.227 ozaki if (ln == NULL) {
1619 1.225 ryo char ip6buf[INET6_ADDRSTRLEN];
1620 1.29 itojun log(LOG_DEBUG,
1621 1.227 ozaki "%s: can't allocate llinfo for %s "
1622 1.227 ozaki "(ln=%p, rt=%p)\n", __func__,
1623 1.226 christos IN6_PRINT(ip6buf, &dst->sin6_addr), ln, rt);
1624 1.227 ozaki m_freem(m);
1625 1.227 ozaki return ENOBUFS;
1626 1.29 itojun }
1627 1.227 ozaki created = true;
1628 1.2 itojun }
1629 1.2 itojun
1630 1.236 ozaki if (ln == NULL) {
1631 1.236 ozaki m_freem(m);
1632 1.236 ozaki return ENETDOWN; /* better error? */
1633 1.236 ozaki }
1634 1.236 ozaki
1635 1.272 roy error = nd_resolve(ln, rt, m, lldst, dstsize);
1636 1.188 ozaki
1637 1.188 ozaki if (created)
1638 1.207 ozaki nd6_gc_neighbors(LLTABLE6(ifp), &dst->sin6_addr);
1639 1.165 ozaki
1640 1.263 roy return error;
1641 1.63 itojun }
1642 1.54 itojun
1643 1.54 itojun int
1644 1.112 dyoung nd6_need_cache(struct ifnet *ifp)
1645 1.54 itojun {
1646 1.54 itojun /*
1647 1.54 itojun * XXX: we currently do not make neighbor cache on any interface
1648 1.266 thorpej * other than ARCnet, Ethernet, and GIF.
1649 1.54 itojun *
1650 1.54 itojun * RFC2893 says:
1651 1.54 itojun * - unidirectional tunnels needs no ND
1652 1.54 itojun */
1653 1.54 itojun switch (ifp->if_type) {
1654 1.54 itojun case IFT_ARCNET:
1655 1.54 itojun case IFT_ETHER:
1656 1.54 itojun case IFT_IEEE1394:
1657 1.102 liamjfoy case IFT_CARP:
1658 1.54 itojun case IFT_GIF: /* XXX need more cases? */
1659 1.99 rpaulo case IFT_PPP:
1660 1.99 rpaulo case IFT_TUNNEL:
1661 1.112 dyoung return 1;
1662 1.54 itojun default:
1663 1.112 dyoung return 0;
1664 1.54 itojun }
1665 1.54 itojun }
1666 1.2 itojun
1667 1.65 itojun int
1668 1.105 christos nd6_sysctl(
1669 1.105 christos int name,
1670 1.105 christos void *oldp, /* syscall arg, need copyout */
1671 1.105 christos size_t *oldlenp,
1672 1.105 christos void *newp, /* syscall arg, need copyin */
1673 1.107 christos size_t newlen
1674 1.105 christos )
1675 1.65 itojun {
1676 1.65 itojun
1677 1.65 itojun if (newp)
1678 1.65 itojun return EPERM;
1679 1.241 pgoyette
1680 1.65 itojun switch (name) {
1681 1.271 roy #ifdef COMPAT_90
1682 1.271 roy case OICMPV6CTL_ND6_DRLIST: /* FALLTHROUGH */
1683 1.271 roy case OICMPV6CTL_ND6_PRLIST:
1684 1.271 roy *oldlenp = 0;
1685 1.271 roy return 0;
1686 1.271 roy #endif
1687 1.99 rpaulo case ICMPV6CTL_ND6_MAXQLEN:
1688 1.242 pgoyette return 0;
1689 1.65 itojun default:
1690 1.242 pgoyette return ENOPROTOOPT;
1691 1.65 itojun }
1692 1.212 ozaki }
1693