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