1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $
32 */
33
34 #include "opt_inet.h"
35 #include "opt_inet6.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/eventhandler.h>
40 #include <sys/callout.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mbuf.h>
44 #include <sys/mutex.h>
45 #include <sys/socket.h>
46 #include <sys/sockio.h>
47 #include <sys/time.h>
48 #include <sys/kernel.h>
49 #include <sys/protosw.h>
50 #include <sys/errno.h>
51 #include <sys/syslog.h>
52 #include <sys/rwlock.h>
53 #include <sys/queue.h>
54 #include <sys/sdt.h>
55 #include <sys/sysctl.h>
56
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/if_private.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 #include <net/route/route_ctl.h>
64 #include <net/route/nhop.h>
65 #include <net/vnet.h>
66
67 #include <netinet/in.h>
68 #include <netinet/in_kdtrace.h>
69 #include <net/if_llatbl.h>
70 #include <netinet/if_ether.h>
71 #include <netinet6/in6_fib.h>
72 #include <netinet6/in6_var.h>
73 #include <netinet/ip6.h>
74 #include <netinet6/ip6_var.h>
75 #include <netinet6/scope6_var.h>
76 #include <netinet6/nd6.h>
77 #include <netinet6/in6_ifattach.h>
78 #include <netinet/icmp6.h>
79 #include <netinet6/send.h>
80
81 #include <sys/limits.h>
82
83 #include <security/mac/mac_framework.h>
84
85 #define ND6_PREFIX_WITH_ROUTER(pr) !LIST_EMPTY(&(pr)->ndpr_advrtrs)
86
87 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */
88 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */
89
90 VNET_DEFINE_STATIC(int, nd6_prune) = 1;
91 #define V_nd6_prune VNET(nd6_prune)
92 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_PRUNE, nd6_prune,
93 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_prune), 0,
94 "Frequency in seconds of checks for expired prefixes and routers");
95
96 VNET_DEFINE_STATIC(int, nd6_delay) = 5;
97 #define V_nd6_delay VNET(nd6_delay)
98 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_DELAY, nd6_delay,
99 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_delay), 0,
100 "Delay in seconds before probing for reachability");
101
102 VNET_DEFINE_STATIC(int, nd6_umaxtries) = 3;
103 #define V_nd6_umaxtries VNET(nd6_umaxtries)
104 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_UMAXTRIES, nd6_umaxtries,
105 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_umaxtries), 0,
106 "Number of ICMPv6 NS messages sent during reachability detection");
107
108 VNET_DEFINE(int, nd6_mmaxtries) = 3;
109 #define V_nd6_mmaxtries VNET(nd6_mmaxtries)
110 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MMAXTRIES, nd6_mmaxtries,
111 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_mmaxtries), 0,
112 "Number of ICMPv6 NS messages sent during address resolution");
113
114 VNET_DEFINE_STATIC(int, nd6_gctimer) = (60 * 60 * 24); /* 1 day: garbage
115 * collection timer */
116 #define V_nd6_gctimer VNET(nd6_gctimer)
117
118 /* preventing too many loops in ND option parsing */
119 VNET_DEFINE_STATIC(int, nd6_maxndopt) = 10; /* max # of ND options allowed */
120
121 VNET_DEFINE_STATIC(int, nd6_maxqueuelen) = 16; /* max pkts cached in unresolved
122 * ND entries */
123 #define V_nd6_maxndopt VNET(nd6_maxndopt)
124 #define V_nd6_maxqueuelen VNET(nd6_maxqueuelen)
125
126 #ifdef ND6_DEBUG
127 VNET_DEFINE(int, nd6_debug) = 1;
128 #else
129 VNET_DEFINE(int, nd6_debug) = 0;
130 #endif
131 #define V_nd6_debug VNET(nd6_debug)
132 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_DEBUG, nd6_debug,
133 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_debug), 0,
134 "Log NDP debug messages");
135
136 static eventhandler_tag lle_event_eh, iflladdr_event_eh, ifnet_link_event_eh;
137
138 VNET_DEFINE(struct nd_prhead, nd_prefix);
139 VNET_DEFINE(struct rwlock, nd6_lock);
140 VNET_DEFINE(uint64_t, nd6_list_genid);
141 VNET_DEFINE(struct mtx, nd6_onlink_mtx);
142
143 VNET_DEFINE(int, nd6_recalc_reachtm_interval) = ND6_RECALC_REACHTM_INTERVAL;
144 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval)
145
146 int (*send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int);
147
148 static bool nd6_is_new_addr_neighbor(const struct sockaddr_in6 *,
149 struct ifnet *);
150 static void nd6_slowtimo(void *);
151 static int regen_tmpaddr(struct in6_ifaddr *);
152 static void nd6_free(struct llentry **, int);
153 static void nd6_free_redirect(const struct llentry *);
154 static void nd6_llinfo_timer(void *);
155 static void nd6_llinfo_settimer_locked(struct llentry *, long);
156 static int nd6_resolve_slow(struct ifnet *, int, int, struct mbuf *,
157 const struct sockaddr_in6 *, u_char *, uint32_t *, struct llentry **);
158 static int nd6_need_cache(struct ifnet *);
159
160 VNET_DEFINE_STATIC(struct callout, nd6_slowtimo_ch);
161 #define V_nd6_slowtimo_ch VNET(nd6_slowtimo_ch)
162
163 VNET_DEFINE_STATIC(struct callout, nd6_timer_ch);
164 #define V_nd6_timer_ch VNET(nd6_timer_ch)
165
166 static void
nd6_lle_event(void * arg __unused,struct llentry * lle,int evt)167 nd6_lle_event(void *arg __unused, struct llentry *lle, int evt)
168 {
169 struct rt_addrinfo rtinfo;
170 struct sockaddr_in6 dst;
171 struct sockaddr_dl gw;
172 struct ifnet *ifp;
173 int type;
174 int fibnum;
175
176 LLE_WLOCK_ASSERT(lle);
177
178 if (lltable_get_af(lle->lle_tbl) != AF_INET6)
179 return;
180
181 switch (evt) {
182 case LLENTRY_RESOLVED:
183 type = RTM_ADD;
184 KASSERT(lle->la_flags & LLE_VALID,
185 ("%s: %p resolved but not valid?", __func__, lle));
186 break;
187 case LLENTRY_EXPIRED:
188 type = RTM_DELETE;
189 break;
190 default:
191 return;
192 }
193
194 ifp = lltable_get_ifp(lle->lle_tbl);
195
196 bzero(&dst, sizeof(dst));
197 bzero(&gw, sizeof(gw));
198 bzero(&rtinfo, sizeof(rtinfo));
199 lltable_fill_sa_entry(lle, (struct sockaddr *)&dst);
200 dst.sin6_scope_id = in6_getscopezone(ifp,
201 in6_addrscope(&dst.sin6_addr));
202 gw.sdl_len = sizeof(struct sockaddr_dl);
203 gw.sdl_family = AF_LINK;
204 gw.sdl_alen = ifp->if_addrlen;
205 gw.sdl_index = ifp->if_index;
206 gw.sdl_type = ifp->if_type;
207 if (evt == LLENTRY_RESOLVED)
208 bcopy(lle->ll_addr, gw.sdl_data, ifp->if_addrlen);
209 rtinfo.rti_info[RTAX_DST] = (struct sockaddr *)&dst;
210 rtinfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gw;
211 rtinfo.rti_addrs = RTA_DST | RTA_GATEWAY;
212 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS : ifp->if_fib;
213 rt_missmsg_fib(type, &rtinfo, RTF_HOST | RTF_LLDATA | (
214 type == RTM_ADD ? RTF_UP: 0), 0, fibnum);
215 }
216
217 /*
218 * A handler for interface link layer address change event.
219 */
220 static void
nd6_iflladdr(void * arg __unused,struct ifnet * ifp)221 nd6_iflladdr(void *arg __unused, struct ifnet *ifp)
222 {
223 struct ifaddr *ifa;
224 struct epoch_tracker et;
225
226 /* XXXGL: ??? */
227 if (ifp->if_inet6 == NULL)
228 return;
229
230 lltable_update_ifaddr(LLTABLE6(ifp));
231
232 if ((ifp->if_flags & IFF_UP) == 0)
233 return;
234
235 /*
236 * Sends gratuitous NAs for each ifaddr to notify other
237 * nodes about the address change.
238 */
239 NET_EPOCH_ENTER(et);
240 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
241 if (ifa->ifa_addr->sa_family == AF_INET6 &&
242 ! IN6_IS_ADDR_MULTICAST(IFA_IN6(ifa)))
243 nd6_grand_start(ifa, ND6_QUEUE_FLAG_LLADDR);
244 }
245 NET_EPOCH_EXIT(et);
246 }
247
248 void
nd6_init(void)249 nd6_init(void)
250 {
251
252 mtx_init(&V_nd6_onlink_mtx, "nd6 onlink", NULL, MTX_DEF);
253 rw_init(&V_nd6_lock, "nd6 list");
254
255 LIST_INIT(&V_nd_prefix);
256 nd6_defrouter_init();
257
258 /* Start timers. */
259 callout_init(&V_nd6_slowtimo_ch, 1);
260 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
261 nd6_slowtimo, curvnet);
262
263 callout_init(&V_nd6_timer_ch, 1);
264 callout_reset(&V_nd6_timer_ch, hz, nd6_timer, curvnet);
265
266 nd6_dad_init();
267 if (IS_DEFAULT_VNET(curvnet)) {
268 lle_event_eh = EVENTHANDLER_REGISTER(lle_event, nd6_lle_event,
269 NULL, EVENTHANDLER_PRI_ANY);
270 iflladdr_event_eh = EVENTHANDLER_REGISTER(iflladdr_event,
271 nd6_iflladdr, NULL, EVENTHANDLER_PRI_ANY);
272 ifnet_link_event_eh = EVENTHANDLER_REGISTER(ifnet_link_event,
273 nd6_ifnet_link_event, NULL, EVENTHANDLER_PRI_ANY);
274 }
275 }
276
277 #ifdef VIMAGE
278 void
nd6_destroy(void)279 nd6_destroy(void)
280 {
281
282 callout_drain(&V_nd6_slowtimo_ch);
283 callout_drain(&V_nd6_timer_ch);
284 if (IS_DEFAULT_VNET(curvnet)) {
285 EVENTHANDLER_DEREGISTER(ifnet_link_event, ifnet_link_event_eh);
286 EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh);
287 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_event_eh);
288 }
289 rw_destroy(&V_nd6_lock);
290 mtx_destroy(&V_nd6_onlink_mtx);
291 }
292 #endif
293
294 void
nd6_ifattach(struct ifnet * ifp)295 nd6_ifattach(struct ifnet *ifp)
296 {
297 struct in6_ifextra *nd = ifp->if_inet6;
298
299 nd->nd_linkmtu = 0;
300 nd->nd_maxmtu = ifp->if_mtu;
301 nd->nd_basereachable = REACHABLE_TIME;
302 nd->nd_reachable = ND_COMPUTE_RTIME(nd->nd_basereachable);
303 nd->nd_retrans = RETRANS_TIMER;
304 nd->nd_recalc_timer = 0;
305 nd->nd_dad_failures = 0;
306 nd->nd_curhoplimit = IPV6_DEFHLIM;
307
308 nd->nd_flags = ND6_IFF_PERFORMNUD;
309
310 /* Set IPv6 disabled on all interfaces but loopback by default. */
311 if ((ifp->if_flags & IFF_LOOPBACK) == 0) {
312 nd->nd_flags |= ND6_IFF_IFDISABLED;
313 if (V_ip6_no_radr)
314 nd->nd_flags |= ND6_IFF_NO_RADR;
315 if (V_ip6_use_stableaddr)
316 nd->nd_flags |= ND6_IFF_STABLEADDR;
317 }
318
319 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL.
320 * XXXHRS: Clear ND6_IFF_AUTO_LINKLOCAL on an IFT_BRIDGE interface by
321 * default regardless of the V_ip6_auto_linklocal configuration to
322 * give a reasonable default behavior.
323 */
324 if ((V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE &&
325 ifp->if_type != IFT_WIREGUARD) || (ifp->if_flags & IFF_LOOPBACK))
326 nd->nd_flags |= ND6_IFF_AUTO_LINKLOCAL;
327 /*
328 * A loopback interface does not need to accept RTADV.
329 * XXXHRS: Clear ND6_IFF_ACCEPT_RTADV on an IFT_BRIDGE interface by
330 * default regardless of the V_ip6_accept_rtadv configuration to
331 * prevent the interface from accepting RA messages arrived
332 * on one of the member interfaces with ND6_IFF_ACCEPT_RTADV.
333 */
334 if (V_ip6_accept_rtadv &&
335 !(ifp->if_flags & IFF_LOOPBACK) &&
336 (ifp->if_type != IFT_BRIDGE)) {
337 nd->nd_flags |= ND6_IFF_ACCEPT_RTADV;
338 /* If we globally accept rtadv, assume IPv6 on. */
339 nd->nd_flags &= ~ND6_IFF_IFDISABLED;
340 }
341
342 /* nd6 queue initialization */
343 TAILQ_INIT(&nd->nd_queue);
344 }
345
346 void
nd6_ifdetach(struct ifnet * ifp)347 nd6_ifdetach(struct ifnet *ifp)
348 {
349 struct epoch_tracker et;
350 struct ifaddr *ifa, *next;
351
352 NET_EPOCH_ENTER(et);
353 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) {
354 if (ifa->ifa_addr->sa_family != AF_INET6)
355 continue;
356
357 /* make sure there are no queued ND6 */
358 nd6_queue_stop(ifa);
359
360 /* stop DAD processing */
361 nd6_dad_stop(ifa);
362 }
363 NET_EPOCH_EXIT(et);
364 }
365
366 /*
367 * Reset ND level link MTU. This function is called when the physical MTU
368 * changes, which means we might have to adjust the ND level MTU.
369 * XXX todo: do not maintain copy of ifp->if_mtu in if_inet6->nd_maxmtu.
370 */
371 void
nd6_setmtu(struct ifnet * ifp)372 nd6_setmtu(struct ifnet *ifp)
373 {
374 struct in6_ifextra *ndi = ifp->if_inet6;
375 uint32_t omaxmtu;
376
377 /* XXXGL: safety against IFT_PFSYNC & IFT_PFLOG */
378 if (ndi == NULL)
379 return;
380
381 omaxmtu = ndi->nd_maxmtu;
382 ndi->nd_maxmtu = ifp->if_mtu;
383
384 /*
385 * Decreasing the interface MTU under IPV6 minimum MTU may cause
386 * undesirable situation. We thus notify the operator of the change
387 * explicitly. The check for omaxmtu is necessary to restrict the
388 * log to the case of changing the MTU, not initializing it.
389 */
390 if (omaxmtu >= IPV6_MMTU && ndi->nd_maxmtu < IPV6_MMTU) {
391 log(LOG_NOTICE, "%s: "
392 "new link MTU on %s (%lu) is too small for IPv6\n",
393 __func__, if_name(ifp), (unsigned long)ndi->nd_maxmtu);
394 }
395 }
396
397 void
nd6_option_init(void * opt,int icmp6len,union nd_opts * ndopts)398 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts)
399 {
400
401 bzero(ndopts, sizeof(*ndopts));
402 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt;
403 ndopts->nd_opts_last
404 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len);
405
406 if (icmp6len == 0) {
407 ndopts->nd_opts_done = 1;
408 ndopts->nd_opts_search = NULL;
409 }
410 }
411
412 /*
413 * Take one ND option.
414 */
415 struct nd_opt_hdr *
nd6_option(union nd_opts * ndopts)416 nd6_option(union nd_opts *ndopts)
417 {
418 struct nd_opt_hdr *nd_opt;
419 int olen;
420
421 KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
422 KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
423 __func__));
424 if (ndopts->nd_opts_search == NULL)
425 return NULL;
426 if (ndopts->nd_opts_done)
427 return NULL;
428
429 nd_opt = ndopts->nd_opts_search;
430
431 /* make sure nd_opt_len is inside the buffer */
432 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) {
433 bzero(ndopts, sizeof(*ndopts));
434 return NULL;
435 }
436
437 olen = nd_opt->nd_opt_len << 3;
438 /*
439 * RFC 4861 section 6.1.2: All included options
440 * must have a length that is greater than zero.
441 */
442 if (olen == 0) {
443 bzero(ndopts, sizeof(*ndopts));
444 return NULL;
445 }
446
447 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen);
448 if (ndopts->nd_opts_search > ndopts->nd_opts_last) {
449 /* option overruns the end of buffer, invalid */
450 bzero(ndopts, sizeof(*ndopts));
451 return NULL;
452 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) {
453 /* reached the end of options chain */
454 ndopts->nd_opts_done = 1;
455 ndopts->nd_opts_search = NULL;
456 }
457 return nd_opt;
458 }
459
460 /*
461 * Parse multiple ND options.
462 * This function is much easier to use, for ND routines that do not need
463 * multiple options of the same type.
464 */
465 int
nd6_options(union nd_opts * ndopts)466 nd6_options(union nd_opts *ndopts)
467 {
468 struct nd_opt_hdr *nd_opt;
469 int i = 0;
470
471 KASSERT(ndopts != NULL, ("%s: ndopts == NULL", __func__));
472 KASSERT(ndopts->nd_opts_last != NULL, ("%s: uninitialized ndopts",
473 __func__));
474 if (ndopts->nd_opts_search == NULL)
475 return 0;
476
477 while (1) {
478 nd_opt = nd6_option(ndopts);
479 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) {
480 /*
481 * Message validation requires that all included
482 * options have a length that is greater than zero.
483 */
484 ICMP6STAT_INC(icp6s_nd_badopt);
485 bzero(ndopts, sizeof(*ndopts));
486 return -1;
487 }
488
489 if (nd_opt == NULL)
490 goto skip1;
491
492 switch (nd_opt->nd_opt_type) {
493 case ND_OPT_SOURCE_LINKADDR:
494 case ND_OPT_TARGET_LINKADDR:
495 case ND_OPT_MTU:
496 case ND_OPT_REDIRECTED_HEADER:
497 case ND_OPT_NONCE:
498 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) {
499 nd6log((LOG_INFO,
500 "duplicated ND6 option found (type=%d)\n",
501 nd_opt->nd_opt_type));
502 /* XXX bark? */
503 } else {
504 ndopts->nd_opt_array[nd_opt->nd_opt_type]
505 = nd_opt;
506 }
507 break;
508 case ND_OPT_PREFIX_INFORMATION:
509 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) {
510 ndopts->nd_opt_array[nd_opt->nd_opt_type]
511 = nd_opt;
512 }
513 ndopts->nd_opts_pi_end =
514 (struct nd_opt_prefix_info *)nd_opt;
515 break;
516 /* What about ND_OPT_ROUTE_INFO? RFC 4191 */
517 case ND_OPT_RDNSS: /* RFC 6106 */
518 case ND_OPT_DNSSL: /* RFC 6106 */
519 /*
520 * Silently ignore options we know and do not care about
521 * in the kernel.
522 */
523 break;
524 default:
525 /*
526 * Unknown options must be silently ignored,
527 * to accommodate future extension to the protocol.
528 */
529 nd6log((LOG_DEBUG,
530 "nd6_options: unsupported option %d - "
531 "option ignored\n", nd_opt->nd_opt_type));
532 }
533
534 skip1:
535 i++;
536 if (i > V_nd6_maxndopt) {
537 ICMP6STAT_INC(icp6s_nd_toomanyopt);
538 nd6log((LOG_INFO, "too many loop in nd opt\n"));
539 break;
540 }
541
542 if (ndopts->nd_opts_done)
543 break;
544 }
545
546 return 0;
547 }
548
549 /*
550 * ND6 timer routine to handle ND6 entries
551 */
552 static void
nd6_llinfo_settimer_locked(struct llentry * ln,long tick)553 nd6_llinfo_settimer_locked(struct llentry *ln, long tick)
554 {
555 int canceled;
556
557 LLE_WLOCK_ASSERT(ln);
558
559 /* Do not schedule timers for child LLEs. */
560 if (ln->la_flags & LLE_CHILD)
561 return;
562
563 if (tick < 0) {
564 ln->la_expire = 0;
565 ln->ln_ntick = 0;
566 canceled = callout_stop(&ln->lle_timer);
567 } else {
568 ln->la_expire = time_uptime + tick / hz;
569 LLE_ADDREF(ln);
570 if (tick > INT_MAX) {
571 ln->ln_ntick = tick - INT_MAX;
572 canceled = callout_reset(&ln->lle_timer, INT_MAX,
573 nd6_llinfo_timer, ln);
574 } else {
575 ln->ln_ntick = 0;
576 canceled = callout_reset(&ln->lle_timer, tick,
577 nd6_llinfo_timer, ln);
578 }
579 }
580 if (canceled > 0)
581 LLE_REMREF(ln);
582 }
583
584 /*
585 * Gets source address of the first packet in hold queue
586 * and stores it in @src.
587 * Returns pointer to @src (if hold queue is not empty) or NULL.
588 *
589 * Set noinline to be dtrace-friendly
590 */
591 static __noinline struct in6_addr *
nd6_llinfo_get_holdsrc(struct llentry * ln,struct in6_addr * src)592 nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src)
593 {
594 struct ip6_hdr hdr;
595 struct mbuf *m;
596
597 if (ln->la_hold == NULL)
598 return (NULL);
599
600 /*
601 * assume every packet in la_hold has the same IP header
602 */
603 m = ln->la_hold;
604 if (sizeof(hdr) > m->m_len)
605 return (NULL);
606
607 m_copydata(m, 0, sizeof(hdr), (caddr_t)&hdr);
608 *src = hdr.ip6_src;
609
610 return (src);
611 }
612
613 /*
614 * Checks if we need to switch from STALE state.
615 *
616 * RFC 4861 requires switching from STALE to DELAY state
617 * on first packet matching entry, waiting V_nd6_delay and
618 * transition to PROBE state (if upper layer confirmation was
619 * not received).
620 *
621 * This code performs a bit differently:
622 * On packet hit we don't change state (but desired state
623 * can be guessed by control plane). However, after V_nd6_delay
624 * seconds code will transition to PROBE state (so DELAY state
625 * is kinda skipped in most situations).
626 *
627 * Typically, V_nd6_gctimer is bigger than V_nd6_delay, so
628 * we perform the following upon entering STALE state:
629 *
630 * 1) Arm timer to run each V_nd6_delay seconds to make sure that
631 * if packet was transmitted at the start of given interval, we
632 * would be able to switch to PROBE state in V_nd6_delay seconds
633 * as user expects.
634 *
635 * 2) Reschedule timer until original V_nd6_gctimer expires keeping
636 * lle in STALE state (remaining timer value stored in lle_remtime).
637 *
638 * 3) Reschedule timer if packet was transmitted less that V_nd6_delay
639 * seconds ago.
640 *
641 * Returns non-zero value if the entry is still STALE (storing
642 * the next timer interval in @pdelay).
643 *
644 * Returns zero value if original timer expired or we need to switch to
645 * PROBE (store that in @do_switch variable).
646 */
647 static int
nd6_is_stale(struct llentry * lle,long * pdelay,int * do_switch)648 nd6_is_stale(struct llentry *lle, long *pdelay, int *do_switch)
649 {
650 int nd_delay, nd_gctimer;
651 time_t lle_hittime;
652 long delay;
653
654 *do_switch = 0;
655 nd_gctimer = V_nd6_gctimer;
656 nd_delay = V_nd6_delay;
657
658 lle_hittime = llentry_get_hittime(lle);
659
660 if (lle_hittime == 0) {
661 /*
662 * Datapath feedback has been requested upon entering
663 * STALE state. No packets has been passed using this lle.
664 * Ask for the timer reschedule and keep STALE state.
665 */
666 delay = (long)(MIN(nd_gctimer, nd_delay));
667 delay *= hz;
668 if (lle->lle_remtime > delay)
669 lle->lle_remtime -= delay;
670 else {
671 delay = lle->lle_remtime;
672 lle->lle_remtime = 0;
673 }
674
675 if (delay == 0) {
676 /*
677 * The original ng6_gctime timeout ended,
678 * no more rescheduling.
679 */
680 return (0);
681 }
682
683 *pdelay = delay;
684 return (1);
685 }
686
687 /*
688 * Packet received. Verify timestamp
689 */
690 delay = (long)(time_uptime - lle_hittime);
691 if (delay < nd_delay) {
692 /*
693 * V_nd6_delay still not passed since the first
694 * hit in STALE state.
695 * Reschedule timer and return.
696 */
697 *pdelay = (long)(nd_delay - delay) * hz;
698 return (1);
699 }
700
701 /* Request switching to probe */
702 *do_switch = 1;
703 return (0);
704 }
705
706 /*
707 * Switch @lle state to new state optionally arming timers.
708 *
709 * Set noinline to be dtrace-friendly
710 */
711 __noinline void
nd6_llinfo_setstate(struct llentry * lle,int newstate)712 nd6_llinfo_setstate(struct llentry *lle, int newstate)
713 {
714 struct ifnet *ifp;
715 int nd_gctimer, nd_delay;
716 long delay, remtime;
717
718 delay = 0;
719 remtime = 0;
720
721 switch (newstate) {
722 case ND6_LLINFO_INCOMPLETE:
723 ifp = lle->lle_tbl->llt_ifp;
724 delay = (long)ifp->if_inet6->nd_retrans * hz / 1000;
725 break;
726 case ND6_LLINFO_REACHABLE:
727 if (!ND6_LLINFO_PERMANENT(lle)) {
728 ifp = lle->lle_tbl->llt_ifp;
729 delay = (long)ifp->if_inet6->nd_reachable * hz;
730 }
731 break;
732 case ND6_LLINFO_STALE:
733
734 llentry_request_feedback(lle);
735 nd_delay = V_nd6_delay;
736 nd_gctimer = V_nd6_gctimer;
737
738 delay = (long)(MIN(nd_gctimer, nd_delay)) * hz;
739 remtime = (long)nd_gctimer * hz - delay;
740 break;
741 case ND6_LLINFO_DELAY:
742 lle->la_asked = 0;
743 delay = (long)V_nd6_delay * hz;
744 break;
745 }
746
747 if (delay > 0)
748 nd6_llinfo_settimer_locked(lle, delay);
749
750 lle->lle_remtime = remtime;
751 lle->ln_state = newstate;
752 }
753
754 /*
755 * Timer-dependent part of nd state machine.
756 *
757 * Set noinline to be dtrace-friendly
758 */
759 static __noinline void
nd6_llinfo_timer(void * arg)760 nd6_llinfo_timer(void *arg)
761 {
762 struct epoch_tracker et;
763 struct llentry *ln;
764 struct in6_addr *dst, *pdst, *psrc, src;
765 struct ifnet *ifp;
766 struct in6_ifextra *ndi;
767 int do_switch, send_ns;
768 long delay;
769
770 KASSERT(arg != NULL, ("%s: arg NULL", __func__));
771 ln = (struct llentry *)arg;
772 ifp = lltable_get_ifp(ln->lle_tbl);
773 CURVNET_SET(ifp->if_vnet);
774
775 ND6_RLOCK();
776 LLE_WLOCK(ln);
777 if (callout_pending(&ln->lle_timer)) {
778 /*
779 * Here we are a bit odd here in the treatment of
780 * active/pending. If the pending bit is set, it got
781 * rescheduled before I ran. The active
782 * bit we ignore, since if it was stopped
783 * in ll_tablefree() and was currently running
784 * it would have return 0 so the code would
785 * not have deleted it since the callout could
786 * not be stopped so we want to go through
787 * with the delete here now. If the callout
788 * was restarted, the pending bit will be back on and
789 * we just want to bail since the callout_reset would
790 * return 1 and our reference would have been removed
791 * by nd6_llinfo_settimer_locked above since canceled
792 * would have been 1.
793 */
794 LLE_WUNLOCK(ln);
795 ND6_RUNLOCK();
796 CURVNET_RESTORE();
797 return;
798 }
799 NET_EPOCH_ENTER(et);
800 ndi = ifp->if_inet6;
801 send_ns = 0;
802 dst = &ln->r_l3addr.addr6;
803 pdst = dst;
804
805 if (ln->ln_ntick > 0) {
806 if (ln->ln_ntick > INT_MAX) {
807 ln->ln_ntick -= INT_MAX;
808 nd6_llinfo_settimer_locked(ln, INT_MAX);
809 } else {
810 ln->ln_ntick = 0;
811 nd6_llinfo_settimer_locked(ln, ln->ln_ntick);
812 }
813 goto done;
814 }
815
816 if (ln->la_flags & LLE_STATIC) {
817 goto done;
818 }
819
820 if (ln->la_flags & LLE_DELETED) {
821 nd6_free(&ln, 0);
822 goto done;
823 }
824
825 switch (ln->ln_state) {
826 case ND6_LLINFO_INCOMPLETE:
827 if (ln->la_asked < V_nd6_mmaxtries) {
828 ln->la_asked++;
829 send_ns = 1;
830 /* Send NS to multicast address */
831 pdst = NULL;
832 } else {
833 struct mbuf *m;
834
835 ICMP6STAT_ADD(icp6s_dropped, ln->la_numheld);
836
837 m = ln->la_hold;
838 if (m != NULL) {
839 /*
840 * assuming every packet in la_hold has the
841 * same IP header. Send error after unlock.
842 */
843 ln->la_hold = m->m_nextpkt;
844 m->m_nextpkt = NULL;
845 ln->la_numheld--;
846 }
847 nd6_free(&ln, 0);
848 if (m != NULL) {
849 struct mbuf *n = m;
850
851 /*
852 * if there are any ummapped mbufs, we
853 * must free them, rather than using
854 * them for an ICMP, as they cannot be
855 * checksummed.
856 */
857 while ((n = n->m_next) != NULL) {
858 if (n->m_flags & M_EXTPG)
859 break;
860 }
861 if (n != NULL) {
862 m_freem(m);
863 m = NULL;
864 } else {
865 icmp6_error2(m, ICMP6_DST_UNREACH,
866 ICMP6_DST_UNREACH_ADDR, 0, ifp);
867 }
868 }
869 }
870 break;
871 case ND6_LLINFO_REACHABLE:
872 if (!ND6_LLINFO_PERMANENT(ln))
873 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
874 break;
875
876 case ND6_LLINFO_STALE:
877 if (nd6_is_stale(ln, &delay, &do_switch) != 0) {
878 /*
879 * No packet has used this entry and GC timeout
880 * has not been passed. Reschedule timer and
881 * return.
882 */
883 nd6_llinfo_settimer_locked(ln, delay);
884 break;
885 }
886
887 if (do_switch == 0) {
888 /*
889 * GC timer has ended and entry hasn't been used.
890 * Run Garbage collector (RFC 4861, 5.3)
891 */
892 if (!ND6_LLINFO_PERMANENT(ln))
893 nd6_free(&ln, 1);
894 break;
895 }
896
897 /* Entry has been used AND delay timer has ended. */
898
899 /* FALLTHROUGH */
900
901 case ND6_LLINFO_DELAY:
902 if ((ndi->nd_flags & ND6_IFF_PERFORMNUD) != 0) {
903 /* We need NUD */
904 ln->la_asked = 1;
905 nd6_llinfo_setstate(ln, ND6_LLINFO_PROBE);
906 send_ns = 1;
907 } else
908 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); /* XXX */
909 break;
910 case ND6_LLINFO_PROBE:
911 if (ln->la_asked < V_nd6_umaxtries) {
912 ln->la_asked++;
913 send_ns = 1;
914 } else {
915 nd6_free(&ln, 0);
916 }
917 break;
918 default:
919 panic("%s: paths in a dark night can be confusing: %d",
920 __func__, ln->ln_state);
921 }
922 done:
923 if (ln != NULL)
924 ND6_RUNLOCK();
925 if (send_ns != 0) {
926 nd6_llinfo_settimer_locked(ln,
927 (long)ndi->nd_retrans * hz / 1000);
928 psrc = nd6_llinfo_get_holdsrc(ln, &src);
929 LLE_FREE_LOCKED(ln);
930 ln = NULL;
931 nd6_ns_output(ifp, psrc, pdst, dst, NULL);
932 }
933
934 if (ln != NULL)
935 LLE_FREE_LOCKED(ln);
936 NET_EPOCH_EXIT(et);
937 CURVNET_RESTORE();
938 }
939
940 /*
941 * ND6 timer routine to expire default route list and prefix list
942 */
943 void
nd6_timer(void * arg)944 nd6_timer(void *arg)
945 {
946 CURVNET_SET((struct vnet *) arg);
947 struct epoch_tracker et;
948 struct nd_prhead prl;
949 struct nd_prefix *pr, *npr;
950 struct ifnet *ifp;
951 struct in6_ifaddr *ia6, *nia6;
952 uint64_t genid;
953
954 LIST_INIT(&prl);
955
956 NET_EPOCH_ENTER(et);
957 nd6_defrouter_timer();
958
959 /*
960 * expire interface addresses.
961 * in the past the loop was inside prefix expiry processing.
962 * However, from a stricter speci-confrmance standpoint, we should
963 * rather separate address lifetimes and prefix lifetimes.
964 *
965 * XXXRW: in6_ifaddrhead locking.
966 */
967 addrloop:
968 CK_STAILQ_FOREACH_SAFE(ia6, &V_in6_ifaddrhead, ia_link, nia6) {
969 /* check address lifetime */
970 if (IFA6_IS_INVALID(ia6)) {
971 int regen = 0;
972
973 /*
974 * If the expiring address is temporary, try
975 * regenerating a new one. This would be useful when
976 * we suspended a laptop PC, then turned it on after a
977 * period that could invalidate all temporary
978 * addresses. Although we may have to restart the
979 * loop (see below), it must be after purging the
980 * address. Otherwise, we'd see an infinite loop of
981 * regeneration.
982 */
983 if (V_ip6_use_tempaddr &&
984 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) {
985 if (regen_tmpaddr(ia6) == 0)
986 regen = 1;
987 }
988
989 in6_purgeaddr(&ia6->ia_ifa);
990
991 if (regen)
992 goto addrloop; /* XXX: see below */
993 } else if (IFA6_IS_DEPRECATED(ia6)) {
994 int oldflags = ia6->ia6_flags;
995
996 ia6->ia6_flags |= IN6_IFF_DEPRECATED;
997
998 /*
999 * If a temporary address has just become deprecated,
1000 * regenerate a new one if possible.
1001 */
1002 if (V_ip6_use_tempaddr &&
1003 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1004 (oldflags & IN6_IFF_DEPRECATED) == 0) {
1005 if (regen_tmpaddr(ia6) == 0) {
1006 /*
1007 * A new temporary address is
1008 * generated.
1009 * XXX: this means the address chain
1010 * has changed while we are still in
1011 * the loop. Although the change
1012 * would not cause disaster (because
1013 * it's not a deletion, but an
1014 * addition,) we'd rather restart the
1015 * loop just for safety. Or does this
1016 * significantly reduce performance??
1017 */
1018 goto addrloop;
1019 }
1020 }
1021 } else if ((ia6->ia6_flags & IN6_IFF_TENTATIVE) != 0) {
1022 /*
1023 * Schedule DAD for a tentative address. This happens
1024 * if the interface was down or not running
1025 * when the address was configured.
1026 */
1027 int delay;
1028
1029 delay = arc4random() %
1030 (MAX_RTR_SOLICITATION_DELAY * hz);
1031 nd6_dad_start((struct ifaddr *)ia6, delay);
1032 } else {
1033 /*
1034 * Check status of the interface. If it is down,
1035 * mark the address as tentative for future DAD.
1036 */
1037 ifp = ia6->ia_ifp;
1038 if ((ifp->if_inet6->nd_flags & ND6_IFF_NO_DAD) == 0 &&
1039 ((ifp->if_flags & IFF_UP) == 0 ||
1040 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 ||
1041 (ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED))){
1042 ia6->ia6_flags &= ~IN6_IFF_DUPLICATED;
1043 ia6->ia6_flags |= IN6_IFF_TENTATIVE;
1044 }
1045
1046 /*
1047 * A new RA might have made a deprecated address
1048 * preferred.
1049 */
1050 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED;
1051 }
1052 }
1053 NET_EPOCH_EXIT(et);
1054
1055 ND6_WLOCK();
1056 restart:
1057 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
1058 /*
1059 * Expire prefixes. Since the pltime is only used for
1060 * autoconfigured addresses, pltime processing for prefixes is
1061 * not necessary.
1062 *
1063 * Only unlink after all derived addresses have expired. This
1064 * may not occur until two hours after the prefix has expired
1065 * per RFC 4862. If the prefix expires before its derived
1066 * addresses, mark it off-link. This will be done automatically
1067 * after unlinking if no address references remain.
1068 */
1069 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME ||
1070 time_uptime - pr->ndpr_lastupdate <= pr->ndpr_vltime)
1071 continue;
1072
1073 if (pr->ndpr_addrcnt == 0) {
1074 nd6_prefix_unlink(pr, &prl);
1075 continue;
1076 }
1077 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) {
1078 genid = V_nd6_list_genid;
1079 nd6_prefix_ref(pr);
1080 ND6_WUNLOCK();
1081 ND6_ONLINK_LOCK();
1082 (void)nd6_prefix_offlink(pr);
1083 ND6_ONLINK_UNLOCK();
1084 ND6_WLOCK();
1085 nd6_prefix_rele(pr);
1086 if (genid != V_nd6_list_genid)
1087 goto restart;
1088 }
1089 }
1090 ND6_WUNLOCK();
1091
1092 while ((pr = LIST_FIRST(&prl)) != NULL) {
1093 LIST_REMOVE(pr, ndpr_entry);
1094 nd6_prefix_del(pr);
1095 }
1096
1097 callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz,
1098 nd6_timer, curvnet);
1099
1100 CURVNET_RESTORE();
1101 }
1102
1103 /*
1104 * ia6 - deprecated/invalidated temporary address
1105 */
1106 static int
regen_tmpaddr(struct in6_ifaddr * ia6)1107 regen_tmpaddr(struct in6_ifaddr *ia6)
1108 {
1109 struct ifaddr *ifa;
1110 struct ifnet *ifp;
1111 struct in6_ifaddr *public_ifa6 = NULL;
1112
1113 NET_EPOCH_ASSERT();
1114
1115 ifp = ia6->ia_ifa.ifa_ifp;
1116 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1117 struct in6_ifaddr *it6;
1118
1119 if (ifa->ifa_addr->sa_family != AF_INET6)
1120 continue;
1121
1122 it6 = (struct in6_ifaddr *)ifa;
1123
1124 /* ignore no autoconf addresses. */
1125 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1126 continue;
1127
1128 /* ignore autoconf addresses with different prefixes. */
1129 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr)
1130 continue;
1131
1132 /*
1133 * Now we are looking at an autoconf address with the same
1134 * prefix as ours. If the address is temporary and is still
1135 * preferred, do not create another one. It would be rare, but
1136 * could happen, for example, when we resume a laptop PC after
1137 * a long period.
1138 */
1139 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1140 !IFA6_IS_DEPRECATED(it6)) {
1141 public_ifa6 = NULL;
1142 break;
1143 }
1144
1145 /*
1146 * This is a public autoconf address that has the same prefix
1147 * as ours. If it is preferred, keep it. We can't break the
1148 * loop here, because there may be a still-preferred temporary
1149 * address with the prefix.
1150 */
1151 if (!IFA6_IS_DEPRECATED(it6))
1152 public_ifa6 = it6;
1153 }
1154 if (public_ifa6 != NULL)
1155 ifa_ref(&public_ifa6->ia_ifa);
1156
1157 if (public_ifa6 != NULL) {
1158 int e;
1159
1160 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) {
1161 ifa_free(&public_ifa6->ia_ifa);
1162 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new"
1163 " tmp addr,errno=%d\n", e);
1164 return (-1);
1165 }
1166 ifa_free(&public_ifa6->ia_ifa);
1167 return (0);
1168 }
1169
1170 return (-1);
1171 }
1172
1173 /*
1174 * Remove prefix and default router list entries corresponding to ifp. Neighbor
1175 * cache entries are freed in in6_domifdetach().
1176 */
1177 void
nd6_purge(struct ifnet * ifp)1178 nd6_purge(struct ifnet *ifp)
1179 {
1180 struct nd_prhead prl;
1181 struct nd_prefix *pr, *npr;
1182
1183 LIST_INIT(&prl);
1184
1185 /* Purge default router list entries toward ifp. */
1186 nd6_defrouter_purge(ifp);
1187
1188 ND6_WLOCK();
1189 /*
1190 * Remove prefixes on ifp. We should have already removed addresses on
1191 * this interface, so no addresses should be referencing these prefixes.
1192 */
1193 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) {
1194 if (pr->ndpr_ifp == ifp)
1195 nd6_prefix_unlink(pr, &prl);
1196 }
1197 ND6_WUNLOCK();
1198
1199 /* Delete the unlinked prefix objects. */
1200 while ((pr = LIST_FIRST(&prl)) != NULL) {
1201 LIST_REMOVE(pr, ndpr_entry);
1202 nd6_prefix_del(pr);
1203 }
1204
1205 /* cancel default outgoing interface setting */
1206 if (V_nd6_defifindex == ifp->if_index)
1207 nd6_setdefaultiface(0);
1208
1209 if (ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) {
1210 /* Refresh default router list. */
1211 defrouter_select_fib(ifp->if_fib);
1212 }
1213 }
1214
1215 /*
1216 * the caller acquires and releases the lock on the lltbls
1217 * Returns the llentry locked
1218 */
1219 struct llentry *
nd6_lookup(const struct in6_addr * addr6,int flags,struct ifnet * ifp)1220 nd6_lookup(const struct in6_addr *addr6, int flags, struct ifnet *ifp)
1221 {
1222 struct sockaddr_in6 sin6;
1223 struct llentry *ln;
1224
1225 bzero(&sin6, sizeof(sin6));
1226 sin6.sin6_len = sizeof(struct sockaddr_in6);
1227 sin6.sin6_family = AF_INET6;
1228 sin6.sin6_addr = *addr6;
1229
1230 LLTABLE_RLOCK_ASSERT(LLTABLE6(ifp));
1231
1232 ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)&sin6);
1233
1234 return (ln);
1235 }
1236
1237 static struct llentry *
nd6_alloc(const struct in6_addr * addr6,int flags,struct ifnet * ifp)1238 nd6_alloc(const struct in6_addr *addr6, int flags, struct ifnet *ifp)
1239 {
1240 struct sockaddr_in6 sin6;
1241 struct llentry *ln;
1242
1243 bzero(&sin6, sizeof(sin6));
1244 sin6.sin6_len = sizeof(struct sockaddr_in6);
1245 sin6.sin6_family = AF_INET6;
1246 sin6.sin6_addr = *addr6;
1247
1248 ln = lltable_alloc_entry(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6);
1249 if (ln != NULL)
1250 ln->ln_state = ND6_LLINFO_NOSTATE;
1251
1252 return (ln);
1253 }
1254
1255 /*
1256 * Test whether a given IPv6 address can be a neighbor.
1257 */
1258 static bool
nd6_is_new_addr_neighbor(const struct sockaddr_in6 * addr,struct ifnet * ifp)1259 nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
1260 {
1261
1262 /*
1263 * A link-local address is always a neighbor.
1264 * XXX: a link does not necessarily specify a single interface.
1265 */
1266 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) {
1267 struct sockaddr_in6 sin6_copy;
1268 u_int32_t zone;
1269
1270 /*
1271 * We need sin6_copy since sa6_recoverscope() may modify the
1272 * content (XXX).
1273 */
1274 sin6_copy = *addr;
1275 if (sa6_recoverscope(&sin6_copy))
1276 return (0); /* XXX: should be impossible */
1277 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone))
1278 return (0);
1279 if (sin6_copy.sin6_scope_id == zone)
1280 return (1);
1281 else
1282 return (0);
1283 }
1284 /* Checking global unicast */
1285
1286 /* If an address is directly reachable, it is a neigbor */
1287 struct nhop_object *nh;
1288 nh = fib6_lookup(ifp->if_fib, &addr->sin6_addr, 0, NHR_NONE, 0);
1289 if (nh != NULL && nh->nh_aifp == ifp && (nh->nh_flags & NHF_GATEWAY) == 0)
1290 return (true);
1291
1292 /*
1293 * Check prefixes with desired on-link state, as some may be not
1294 * installed in the routing table.
1295 */
1296 bool matched = false;
1297 struct nd_prefix *pr;
1298 ND6_RLOCK();
1299 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
1300 if (pr->ndpr_ifp != ifp)
1301 continue;
1302 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0)
1303 continue;
1304 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr,
1305 &addr->sin6_addr, &pr->ndpr_mask)) {
1306 matched = true;
1307 break;
1308 }
1309 }
1310 ND6_RUNLOCK();
1311 if (matched)
1312 return (true);
1313
1314 /*
1315 * If the address is assigned on the node of the other side of
1316 * a p2p interface, the address should be a neighbor.
1317 */
1318 if (ifp->if_flags & IFF_POINTOPOINT) {
1319 struct ifaddr *ifa;
1320
1321 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1322 if (ifa->ifa_addr->sa_family != addr->sin6_family)
1323 continue;
1324 if (ifa->ifa_dstaddr != NULL &&
1325 sa_equal(addr, ifa->ifa_dstaddr)) {
1326 return (true);
1327 }
1328 }
1329 }
1330
1331 /*
1332 * If the default router list is empty, all addresses are regarded
1333 * as on-link, and thus, as a neighbor.
1334 */
1335 if (ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV &&
1336 nd6_defrouter_list_empty() &&
1337 V_nd6_defifindex == ifp->if_index) {
1338 return (1);
1339 }
1340
1341 return (0);
1342 }
1343
1344 /*
1345 * Detect if a given IPv6 address identifies a neighbor on a given link.
1346 * XXX: should take care of the destination of a p2p link?
1347 */
1348 int
nd6_is_addr_neighbor(const struct sockaddr_in6 * addr,struct ifnet * ifp)1349 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp)
1350 {
1351 struct llentry *lle;
1352 int rc = 0;
1353
1354 NET_EPOCH_ASSERT();
1355
1356 if (nd6_is_new_addr_neighbor(addr, ifp))
1357 return (1);
1358
1359 /*
1360 * Even if the address matches none of our addresses, it might be
1361 * in the neighbor cache.
1362 */
1363 if ((lle = nd6_lookup(&addr->sin6_addr, LLE_SF(AF_INET6, 0), ifp)) != NULL) {
1364 LLE_RUNLOCK(lle);
1365 rc = 1;
1366 }
1367 return (rc);
1368 }
1369
1370 static __noinline void
nd6_free_children(struct llentry * lle)1371 nd6_free_children(struct llentry *lle)
1372 {
1373 struct llentry *child_lle;
1374
1375 NET_EPOCH_ASSERT();
1376 LLE_WLOCK_ASSERT(lle);
1377
1378 while ((child_lle = CK_SLIST_FIRST(&lle->lle_children)) != NULL) {
1379 LLE_WLOCK(child_lle);
1380 lltable_unlink_child_entry(child_lle);
1381 llentry_free(child_lle);
1382 }
1383 }
1384
1385 /*
1386 * Tries to update @lle address/prepend data with new @lladdr.
1387 *
1388 * Returns true on success.
1389 * In any case, @lle is returned wlocked.
1390 */
1391 static __noinline bool
nd6_try_set_entry_addr_locked(struct ifnet * ifp,struct llentry * lle,char * lladdr)1392 nd6_try_set_entry_addr_locked(struct ifnet *ifp, struct llentry *lle, char *lladdr)
1393 {
1394 u_char buf[LLE_MAX_LINKHDR];
1395 int fam, off;
1396 size_t sz;
1397
1398 sz = sizeof(buf);
1399 if (lltable_calc_llheader(ifp, AF_INET6, lladdr, buf, &sz, &off) != 0)
1400 return (false);
1401
1402 /* Update data */
1403 lltable_set_entry_addr(ifp, lle, buf, sz, off);
1404
1405 struct llentry *child_lle;
1406 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
1407 LLE_WLOCK(child_lle);
1408 fam = child_lle->r_family;
1409 sz = sizeof(buf);
1410 if (lltable_calc_llheader(ifp, fam, lladdr, buf, &sz, &off) == 0) {
1411 /* success */
1412 lltable_set_entry_addr(ifp, child_lle, buf, sz, off);
1413 child_lle->ln_state = ND6_LLINFO_REACHABLE;
1414 }
1415 LLE_WUNLOCK(child_lle);
1416 }
1417
1418 return (true);
1419 }
1420
1421 bool
nd6_try_set_entry_addr(struct ifnet * ifp,struct llentry * lle,char * lladdr)1422 nd6_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle, char *lladdr)
1423 {
1424 NET_EPOCH_ASSERT();
1425 LLE_WLOCK_ASSERT(lle);
1426
1427 if (!lltable_trylock(lle))
1428 return (false);
1429 bool ret = nd6_try_set_entry_addr_locked(ifp, lle, lladdr);
1430 LLTABLE_UNLOCK(lle->lle_tbl);
1431
1432 return (ret);
1433 }
1434
1435 /*
1436 * Free an nd6 llinfo entry.
1437 * Since the function would cause significant changes in the kernel, DO NOT
1438 * make it global, unless you have a strong reason for the change, and are sure
1439 * that the change is safe.
1440 *
1441 * Set noinline to be dtrace-friendly
1442 */
1443 static __noinline void
nd6_free(struct llentry ** lnp,int gc)1444 nd6_free(struct llentry **lnp, int gc)
1445 {
1446 struct ifnet *ifp;
1447 struct llentry *ln;
1448 struct nd_defrouter *dr;
1449
1450 ln = *lnp;
1451 *lnp = NULL;
1452
1453 LLE_WLOCK_ASSERT(ln);
1454 ND6_RLOCK_ASSERT();
1455
1456 KASSERT((ln->la_flags & LLE_CHILD) == 0, ("child lle"));
1457
1458 ifp = lltable_get_ifp(ln->lle_tbl);
1459 if ((ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) != 0)
1460 dr = defrouter_lookup_locked(&ln->r_l3addr.addr6, ifp);
1461 else
1462 dr = NULL;
1463 ND6_RUNLOCK();
1464
1465 if ((ln->la_flags & LLE_DELETED) == 0)
1466 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED);
1467
1468 /*
1469 * we used to have pfctlinput(PRC_HOSTDEAD) here.
1470 * even though it is not harmful, it was not really necessary.
1471 */
1472
1473 /* cancel timer */
1474 nd6_llinfo_settimer_locked(ln, -1);
1475
1476 if (ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) {
1477 if (dr != NULL && dr->expire &&
1478 ln->ln_state == ND6_LLINFO_STALE && gc) {
1479 /*
1480 * If the reason for the deletion is just garbage
1481 * collection, and the neighbor is an active default
1482 * router, do not delete it. Instead, reset the GC
1483 * timer using the router's lifetime.
1484 * Simply deleting the entry would affect default
1485 * router selection, which is not necessarily a good
1486 * thing, especially when we're using router preference
1487 * values.
1488 * XXX: the check for ln_state would be redundant,
1489 * but we intentionally keep it just in case.
1490 */
1491 if (dr->expire > time_uptime)
1492 nd6_llinfo_settimer_locked(ln,
1493 (dr->expire - time_uptime) * hz);
1494 else
1495 nd6_llinfo_settimer_locked(ln,
1496 (long)V_nd6_gctimer * hz);
1497
1498 LLE_REMREF(ln);
1499 LLE_WUNLOCK(ln);
1500 defrouter_rele(dr);
1501 return;
1502 }
1503
1504 if (dr) {
1505 /*
1506 * Unreachability of a router might affect the default
1507 * router selection and on-link detection of advertised
1508 * prefixes.
1509 */
1510
1511 /*
1512 * Temporarily fake the state to choose a new default
1513 * router and to perform on-link determination of
1514 * prefixes correctly.
1515 * Below the state will be set correctly,
1516 * or the entry itself will be deleted.
1517 */
1518 ln->ln_state = ND6_LLINFO_INCOMPLETE;
1519 }
1520
1521 if (ln->ln_router || dr) {
1522 /*
1523 * We need to unlock to avoid a LOR with rt6_flush() with the
1524 * rnh and for the calls to pfxlist_onlink_check() and
1525 * defrouter_select_fib() in the block further down for calls
1526 * into nd6_lookup(). We still hold a ref.
1527 */
1528 LLE_WUNLOCK(ln);
1529
1530 /*
1531 * rt6_flush must be called whether or not the neighbor
1532 * is in the Default Router List.
1533 * See a corresponding comment in nd6_na_input().
1534 */
1535 rt6_flush(&ln->r_l3addr.addr6, ifp);
1536 }
1537
1538 if (dr) {
1539 /*
1540 * Since defrouter_select_fib() does not affect the
1541 * on-link determination and MIP6 needs the check
1542 * before the default router selection, we perform
1543 * the check now.
1544 */
1545 pfxlist_onlink_check();
1546
1547 /*
1548 * Refresh default router list.
1549 */
1550 defrouter_select_fib(dr->ifp->if_fib);
1551 }
1552
1553 /*
1554 * If this entry was added by an on-link redirect, remove the
1555 * corresponding host route.
1556 */
1557 if (ln->la_flags & LLE_REDIRECT)
1558 nd6_free_redirect(ln);
1559
1560 if (ln->ln_router || dr)
1561 LLE_WLOCK(ln);
1562 }
1563
1564 /*
1565 * Save to unlock. We still hold an extra reference and will not
1566 * free(9) in llentry_free() if someone else holds one as well.
1567 */
1568 LLE_WUNLOCK(ln);
1569 LLTABLE_LOCK(ln->lle_tbl);
1570 LLE_WLOCK(ln);
1571 /* Guard against race with other llentry_free(). */
1572 if (ln->la_flags & LLE_LINKED) {
1573 /* Remove callout reference */
1574 LLE_REMREF(ln);
1575 lltable_unlink_entry(ln->lle_tbl, ln);
1576 }
1577 LLTABLE_UNLOCK(ln->lle_tbl);
1578
1579 nd6_free_children(ln);
1580
1581 llentry_free(ln);
1582 if (dr != NULL)
1583 defrouter_rele(dr);
1584 }
1585
1586 static int
nd6_isdynrte(const struct rtentry * rt,const struct nhop_object * nh,void * xap)1587 nd6_isdynrte(const struct rtentry *rt, const struct nhop_object *nh, void *xap)
1588 {
1589
1590 if (nh->nh_flags & NHF_REDIRECT)
1591 return (1);
1592
1593 return (0);
1594 }
1595
1596 /*
1597 * Remove the rtentry for the given llentry,
1598 * both of which were installed by a redirect.
1599 */
1600 static void
nd6_free_redirect(const struct llentry * ln)1601 nd6_free_redirect(const struct llentry *ln)
1602 {
1603 int fibnum;
1604 struct sockaddr_in6 sin6;
1605 struct rib_cmd_info rc;
1606 struct epoch_tracker et;
1607
1608 lltable_fill_sa_entry(ln, (struct sockaddr *)&sin6);
1609
1610 NET_EPOCH_ENTER(et);
1611 for (fibnum = 0; fibnum < rt_numfibs; fibnum++)
1612 rib_del_route_px(fibnum, (struct sockaddr *)&sin6, 128,
1613 nd6_isdynrte, NULL, 0, &rc);
1614 NET_EPOCH_EXIT(et);
1615 }
1616
1617 /*
1618 * Updates status of the default router route.
1619 */
1620 static void
check_release_defrouter(const struct rib_cmd_info * rc,void * _cbdata)1621 check_release_defrouter(const struct rib_cmd_info *rc, void *_cbdata)
1622 {
1623 struct nd_defrouter *dr;
1624 struct nhop_object *nh;
1625
1626 nh = rc->rc_nh_old;
1627 if (rc->rc_cmd == RTM_DELETE && (nh->nh_flags & NHF_DEFAULT) != 0) {
1628 dr = defrouter_lookup(&nh->gw6_sa.sin6_addr, nh->nh_ifp);
1629 if (dr != NULL) {
1630 dr->installed = 0;
1631 defrouter_rele(dr);
1632 }
1633 }
1634 }
1635
1636 void
nd6_subscription_cb(struct rib_head * rnh,struct rib_cmd_info * rc,void * arg)1637 nd6_subscription_cb(struct rib_head *rnh, struct rib_cmd_info *rc, void *arg)
1638 {
1639
1640 rib_decompose_notification(rc, check_release_defrouter, NULL);
1641 if (rc->rc_cmd == RTM_DELETE && !NH_IS_NHGRP(rc->rc_nh_old))
1642 check_release_defrouter(rc, NULL);
1643 }
1644
1645 int
nd6_ioctl(u_long cmd,caddr_t data,struct ifnet * ifp)1646 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp)
1647 {
1648 struct epoch_tracker et;
1649 struct in6_ndireq *ndi = (struct in6_ndireq *)data;
1650 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data;
1651 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data;
1652 struct in6_ifextra *ext = ifp->if_inet6;
1653 int error = 0;
1654
1655 /* XXXGL: safety against IFT_PFSYNC & IFT_PFLOG */
1656 if (ext == NULL)
1657 return (EPFNOSUPPORT);
1658 #define ND ndi->ndi
1659 switch (cmd) {
1660 case SIOCGIFINFO_IN6:
1661 ND = (struct nd_ifinfo){
1662 .linkmtu = ext->nd_linkmtu,
1663 .maxmtu = ext->nd_maxmtu,
1664 .basereachable = ext->nd_basereachable,
1665 .reachable = ext->nd_reachable,
1666 .retrans = ext->nd_retrans,
1667 .flags = ext->nd_flags,
1668 .recalctm = ext->nd_recalc_timer,
1669 .chlim = ext->nd_curhoplimit,
1670 .initialized = 1,
1671 };
1672 break;
1673 case SIOCSIFINFO_IN6:
1674 /*
1675 * used to change host variables from userland.
1676 * intended for a use on router to reflect RA configurations.
1677 */
1678 /* 0 means 'unspecified' */
1679 if (ND.linkmtu != 0) {
1680 if (ND.linkmtu < IPV6_MMTU ||
1681 ND.linkmtu > in6_ifmtu(ifp)) {
1682 error = EINVAL;
1683 break;
1684 }
1685 ext->nd_linkmtu = ND.linkmtu;
1686 }
1687
1688 if (ND.basereachable != 0) {
1689 uint32_t obasereachable = ext->nd_basereachable;
1690
1691 ext->nd_basereachable = ND.basereachable;
1692 if (ND.basereachable != obasereachable)
1693 ext->nd_reachable =
1694 ND_COMPUTE_RTIME(ND.basereachable);
1695 }
1696 if (ND.retrans != 0)
1697 ext->nd_retrans = ND.retrans;
1698 if (ND.chlim != 0)
1699 ext->nd_curhoplimit = ND.chlim;
1700 /* FALLTHROUGH */
1701 case SIOCSIFINFO_FLAGS:
1702 {
1703 struct ifaddr *ifa;
1704 struct in6_ifaddr *ia;
1705
1706 if ((ext->nd_flags & ND6_IFF_IFDISABLED) &&
1707 !(ND.flags & ND6_IFF_IFDISABLED)) {
1708 /* ifdisabled 1->0 transision */
1709
1710 /*
1711 * If the interface is marked as ND6_IFF_IFDISABLED and
1712 * has an link-local address with IN6_IFF_DUPLICATED,
1713 * do not clear ND6_IFF_IFDISABLED.
1714 * See RFC 4862, Section 5.4.5.
1715 */
1716 NET_EPOCH_ENTER(et);
1717 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1718 if (ifa->ifa_addr->sa_family != AF_INET6)
1719 continue;
1720 ia = (struct in6_ifaddr *)ifa;
1721 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) &&
1722 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1723 break;
1724 }
1725 NET_EPOCH_EXIT(et);
1726
1727 if (ifa != NULL) {
1728 /* LLA is duplicated. */
1729 ND.flags |= ND6_IFF_IFDISABLED;
1730 log(LOG_ERR, "Cannot enable an interface"
1731 " with a link-local address marked"
1732 " duplicate.\n");
1733 } else {
1734 ext->nd_flags &= ~ND6_IFF_IFDISABLED;
1735 if (ifp->if_flags & IFF_UP)
1736 in6_if_up(ifp);
1737 }
1738 } else if (!(ext->nd_flags & ND6_IFF_IFDISABLED) &&
1739 (ND.flags & ND6_IFF_IFDISABLED)) {
1740 /* ifdisabled 0->1 transision */
1741 /* Mark all IPv6 address as tentative. */
1742
1743 ext->nd_flags |= ND6_IFF_IFDISABLED;
1744 if (V_ip6_dad_count > 0 &&
1745 (ext->nd_flags & ND6_IFF_NO_DAD) == 0) {
1746 NET_EPOCH_ENTER(et);
1747 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead,
1748 ifa_link) {
1749 if (ifa->ifa_addr->sa_family !=
1750 AF_INET6)
1751 continue;
1752 ia = (struct in6_ifaddr *)ifa;
1753 ia->ia6_flags |= IN6_IFF_TENTATIVE;
1754 }
1755 NET_EPOCH_EXIT(et);
1756 }
1757 }
1758
1759 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) {
1760 if (!(ext->nd_flags & ND6_IFF_AUTO_LINKLOCAL)) {
1761 /* auto_linklocal 0->1 transision */
1762
1763 /* If no link-local address on ifp, configure */
1764 ext->nd_flags |= ND6_IFF_AUTO_LINKLOCAL;
1765 in6_ifattach(ifp, NULL);
1766 } else if (!(ND.flags & ND6_IFF_IFDISABLED) &&
1767 ifp->if_flags & IFF_UP) {
1768 /*
1769 * When the IF already has
1770 * ND6_IFF_AUTO_LINKLOCAL, no link-local
1771 * address is assigned, and IFF_UP, try to
1772 * assign one.
1773 */
1774 NET_EPOCH_ENTER(et);
1775 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead,
1776 ifa_link) {
1777 if (ifa->ifa_addr->sa_family !=
1778 AF_INET6)
1779 continue;
1780 ia = (struct in6_ifaddr *)ifa;
1781 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia)))
1782 break;
1783 }
1784 NET_EPOCH_EXIT(et);
1785 if (ifa != NULL)
1786 /* No LLA is configured. */
1787 in6_ifattach(ifp, NULL);
1788 }
1789 }
1790 ext->nd_flags = ND.flags;
1791 break;
1792 }
1793 #undef ND
1794 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */
1795 /* sync kernel routing table with the default router list */
1796 defrouter_reset();
1797 defrouter_select_fib(RT_ALL_FIBS);
1798 break;
1799 case SIOCSPFXFLUSH_IN6:
1800 {
1801 /* flush all the prefix advertised by routers */
1802 struct in6_ifaddr *ia, *ia_next;
1803 struct nd_prefix *pr, *next;
1804 struct nd_prhead prl;
1805
1806 LIST_INIT(&prl);
1807
1808 ND6_WLOCK();
1809 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) {
1810 if (ND6_PREFIX_WITH_ROUTER(pr))
1811 nd6_prefix_unlink(pr, &prl);
1812 }
1813 ND6_WUNLOCK();
1814
1815 while ((pr = LIST_FIRST(&prl)) != NULL) {
1816 LIST_REMOVE(pr, ndpr_entry);
1817 /* XXXRW: in6_ifaddrhead locking. */
1818 CK_STAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link,
1819 ia_next) {
1820 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0)
1821 continue;
1822
1823 if (ia->ia6_ndpr == pr)
1824 in6_purgeaddr(&ia->ia_ifa);
1825 }
1826 nd6_prefix_del(pr);
1827 }
1828 break;
1829 }
1830 case SIOCSRTRFLUSH_IN6:
1831 {
1832 /* flush all the default routers */
1833
1834 defrouter_reset();
1835 nd6_defrouter_flush_all();
1836 defrouter_select_fib(RT_ALL_FIBS);
1837 break;
1838 }
1839 case SIOCGNBRINFO_IN6:
1840 {
1841 struct llentry *ln;
1842 struct in6_addr nb_addr = nbi->addr; /* make local for safety */
1843
1844 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0)
1845 return (error);
1846
1847 NET_EPOCH_ENTER(et);
1848 ln = nd6_lookup(&nb_addr, LLE_SF(AF_INET6, 0), ifp);
1849 NET_EPOCH_EXIT(et);
1850
1851 if (ln == NULL) {
1852 error = EINVAL;
1853 break;
1854 }
1855 nbi->state = ln->ln_state;
1856 nbi->asked = ln->la_asked;
1857 nbi->isrouter = ln->ln_router;
1858 if (ln->la_expire == 0)
1859 nbi->expire = 0;
1860 else
1861 nbi->expire = ln->la_expire + ln->lle_remtime / hz +
1862 (time_second - time_uptime);
1863 LLE_RUNLOCK(ln);
1864 break;
1865 }
1866 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1867 ndif->ifindex = V_nd6_defifindex;
1868 break;
1869 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */
1870 return (nd6_setdefaultiface(ndif->ifindex));
1871 }
1872 return (error);
1873 }
1874
1875 /*
1876 * Calculates new isRouter value based on provided parameters and
1877 * returns it.
1878 */
1879 static int
nd6_is_router(int type,int code,int is_new,int old_addr,int new_addr,int ln_router)1880 nd6_is_router(int type, int code, int is_new, int old_addr, int new_addr,
1881 int ln_router)
1882 {
1883
1884 /*
1885 * ICMP6 type dependent behavior.
1886 *
1887 * NS: clear IsRouter if new entry
1888 * RS: clear IsRouter
1889 * RA: set IsRouter if there's lladdr
1890 * redir: clear IsRouter if new entry
1891 *
1892 * RA case, (1):
1893 * The spec says that we must set IsRouter in the following cases:
1894 * - If lladdr exist, set IsRouter. This means (1-5).
1895 * - If it is old entry (!newentry), set IsRouter. This means (7).
1896 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter.
1897 * A quetion arises for (1) case. (1) case has no lladdr in the
1898 * neighbor cache, this is similar to (6).
1899 * This case is rare but we figured that we MUST NOT set IsRouter.
1900 *
1901 * is_new old_addr new_addr NS RS RA redir
1902 * D R
1903 * 0 n n (1) c ? s
1904 * 0 y n (2) c s s
1905 * 0 n y (3) c s s
1906 * 0 y y (4) c s s
1907 * 0 y y (5) c s s
1908 * 1 -- n (6) c c c s
1909 * 1 -- y (7) c c s c s
1910 *
1911 * (c=clear s=set)
1912 */
1913 switch (type & 0xff) {
1914 case ND_NEIGHBOR_SOLICIT:
1915 /*
1916 * New entry must have is_router flag cleared.
1917 */
1918 if (is_new) /* (6-7) */
1919 ln_router = 0;
1920 break;
1921 case ND_REDIRECT:
1922 /*
1923 * If the icmp is a redirect to a better router, always set the
1924 * is_router flag. Otherwise, if the entry is newly created,
1925 * clear the flag. [RFC 2461, sec 8.3]
1926 */
1927 if (code == ND_REDIRECT_ROUTER)
1928 ln_router = 1;
1929 else {
1930 if (is_new) /* (6-7) */
1931 ln_router = 0;
1932 }
1933 break;
1934 case ND_ROUTER_SOLICIT:
1935 /*
1936 * is_router flag must always be cleared.
1937 */
1938 ln_router = 0;
1939 break;
1940 case ND_ROUTER_ADVERT:
1941 /*
1942 * Mark an entry with lladdr as a router.
1943 */
1944 if ((!is_new && (old_addr || new_addr)) || /* (2-5) */
1945 (is_new && new_addr)) { /* (7) */
1946 ln_router = 1;
1947 }
1948 break;
1949 }
1950
1951 return (ln_router);
1952 }
1953
1954 /*
1955 * Create neighbor cache entry and cache link-layer address,
1956 * on reception of inbound ND6 packets. (RS/RA/NS/redirect)
1957 *
1958 * type - ICMP6 type
1959 * code - type dependent information
1960 *
1961 */
1962 void
nd6_cache_lladdr(struct ifnet * ifp,struct in6_addr * from,char * lladdr,int lladdrlen,int type,int code)1963 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr,
1964 int lladdrlen, int type, int code)
1965 {
1966 struct llentry *ln = NULL, *ln_tmp;
1967 int is_newentry;
1968 int do_update;
1969 int olladdr;
1970 int llchange;
1971 int flags;
1972 uint16_t router = 0;
1973 struct mbuf *chain = NULL;
1974 u_char linkhdr[LLE_MAX_LINKHDR];
1975 size_t linkhdrsize;
1976 int lladdr_off;
1977
1978 NET_EPOCH_ASSERT();
1979
1980 KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__));
1981 KASSERT(from != NULL, ("%s: from == NULL", __func__));
1982
1983 /* nothing must be updated for unspecified address */
1984 if (IN6_IS_ADDR_UNSPECIFIED(from))
1985 return;
1986
1987 /*
1988 * Validation about ifp->if_addrlen and lladdrlen must be done in
1989 * the caller.
1990 *
1991 * XXX If the link does not have link-layer adderss, what should
1992 * we do? (ifp->if_addrlen == 0)
1993 * Spec says nothing in sections for RA, RS and NA. There's small
1994 * description on it in NS section (RFC 2461 7.2.3).
1995 */
1996 flags = lladdr ? LLE_EXCLUSIVE : 0;
1997 ln = nd6_lookup(from, LLE_SF(AF_INET6, flags), ifp);
1998 is_newentry = 0;
1999 if (ln == NULL) {
2000 flags |= LLE_EXCLUSIVE;
2001 ln = nd6_alloc(from, 0, ifp);
2002 if (ln == NULL)
2003 return;
2004
2005 /*
2006 * Since we already know all the data for the new entry,
2007 * fill it before insertion.
2008 */
2009 if (lladdr != NULL) {
2010 linkhdrsize = sizeof(linkhdr);
2011 if (lltable_calc_llheader(ifp, AF_INET6, lladdr,
2012 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
2013 lltable_free_entry(LLTABLE6(ifp), ln);
2014 return;
2015 }
2016 lltable_set_entry_addr(ifp, ln, linkhdr, linkhdrsize,
2017 lladdr_off);
2018 }
2019
2020 LLTABLE_LOCK(LLTABLE6(ifp));
2021 LLE_WLOCK(ln);
2022 /* Prefer any existing lle over newly-created one */
2023 ln_tmp = nd6_lookup(from, LLE_SF(AF_INET6, LLE_EXCLUSIVE), ifp);
2024 if (ln_tmp == NULL)
2025 lltable_link_entry(LLTABLE6(ifp), ln);
2026 LLTABLE_UNLOCK(LLTABLE6(ifp));
2027 if (ln_tmp == NULL) {
2028 /* No existing lle, mark as new entry (6,7) */
2029 is_newentry = 1;
2030 if (lladdr != NULL) { /* (7) */
2031 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
2032 EVENTHANDLER_INVOKE(lle_event, ln,
2033 LLENTRY_RESOLVED);
2034 }
2035 } else {
2036 lltable_free_entry(LLTABLE6(ifp), ln);
2037 ln = ln_tmp;
2038 ln_tmp = NULL;
2039 }
2040 }
2041 /* do nothing if static ndp is set */
2042 if ((ln->la_flags & LLE_STATIC)) {
2043 if (flags & LLE_EXCLUSIVE)
2044 LLE_WUNLOCK(ln);
2045 else
2046 LLE_RUNLOCK(ln);
2047 return;
2048 }
2049
2050 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0;
2051 if (olladdr && lladdr) {
2052 llchange = bcmp(lladdr, ln->ll_addr,
2053 ifp->if_addrlen);
2054 } else if (!olladdr && lladdr)
2055 llchange = 1;
2056 else
2057 llchange = 0;
2058
2059 /*
2060 * newentry olladdr lladdr llchange (*=record)
2061 * 0 n n -- (1)
2062 * 0 y n -- (2)
2063 * 0 n y y (3) * STALE
2064 * 0 y y n (4) *
2065 * 0 y y y (5) * STALE
2066 * 1 -- n -- (6) NOSTATE(= PASSIVE)
2067 * 1 -- y -- (7) * STALE
2068 */
2069
2070 do_update = 0;
2071 if (is_newentry == 0 && llchange != 0) {
2072 do_update = 1; /* (3,5) */
2073
2074 /*
2075 * Record source link-layer address
2076 * XXX is it dependent to ifp->if_type?
2077 */
2078 if (!nd6_try_set_entry_addr(ifp, ln, lladdr)) {
2079 /* Entry was deleted */
2080 LLE_WUNLOCK(ln);
2081 return;
2082 }
2083
2084 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE);
2085
2086 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
2087
2088 if (ln->la_hold != NULL)
2089 chain = nd6_grab_holdchain(ln);
2090 }
2091
2092 /* Calculates new router status */
2093 router = nd6_is_router(type, code, is_newentry, olladdr,
2094 lladdr != NULL ? 1 : 0, ln->ln_router);
2095
2096 ln->ln_router = router;
2097 /* Mark non-router redirects with special flag */
2098 if ((type & 0xFF) == ND_REDIRECT && code != ND_REDIRECT_ROUTER)
2099 ln->la_flags |= LLE_REDIRECT;
2100
2101 if (flags & LLE_EXCLUSIVE)
2102 LLE_WUNLOCK(ln);
2103 else
2104 LLE_RUNLOCK(ln);
2105
2106 if (chain != NULL)
2107 nd6_flush_holdchain(ifp, ln, chain);
2108 if (do_update)
2109 nd6_flush_children_holdchain(ifp, ln);
2110
2111 /*
2112 * When the link-layer address of a router changes, select the
2113 * best router again. In particular, when the neighbor entry is newly
2114 * created, it might affect the selection policy.
2115 * Question: can we restrict the first condition to the "is_newentry"
2116 * case?
2117 * XXX: when we hear an RA from a new router with the link-layer
2118 * address option, defrouter_select_fib() is called twice, since
2119 * defrtrlist_update called the function as well. However, I believe
2120 * we can compromise the overhead, since it only happens the first
2121 * time.
2122 * XXX: although defrouter_select_fib() should not have a bad effect
2123 * for those are not autoconfigured hosts, we explicitly avoid such
2124 * cases for safety.
2125 */
2126 if ((do_update || is_newentry) && router &&
2127 ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) {
2128 /*
2129 * guaranteed recursion
2130 */
2131 defrouter_select_fib(ifp->if_fib);
2132 }
2133 }
2134
2135 static void
nd6_slowtimo(void * arg)2136 nd6_slowtimo(void *arg)
2137 {
2138 struct epoch_tracker et;
2139 CURVNET_SET((struct vnet *) arg);
2140 struct in6_ifextra *nd6if;
2141 struct ifnet *ifp;
2142
2143 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz,
2144 nd6_slowtimo, curvnet);
2145 NET_EPOCH_ENTER(et);
2146 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
2147 if ((nd6if = ifp->if_inet6) == NULL)
2148 continue;
2149 if (nd6if->nd_basereachable && /* already initialized */
2150 (nd6if->nd_recalc_timer -= ND6_SLOWTIMER_INTERVAL) <= 0) {
2151 /*
2152 * Since reachable time rarely changes by router
2153 * advertisements, we SHOULD insure that a new random
2154 * value gets recomputed at least once every few hours.
2155 * (RFC 2461, 6.3.4)
2156 */
2157 nd6if->nd_recalc_timer = V_nd6_recalc_reachtm_interval;
2158 nd6if->nd_reachable =
2159 ND_COMPUTE_RTIME(nd6if->nd_basereachable);
2160 }
2161 }
2162 NET_EPOCH_EXIT(et);
2163 CURVNET_RESTORE();
2164 }
2165
2166 struct mbuf *
nd6_grab_holdchain(struct llentry * ln)2167 nd6_grab_holdchain(struct llentry *ln)
2168 {
2169 struct mbuf *chain;
2170
2171 LLE_WLOCK_ASSERT(ln);
2172
2173 chain = ln->la_hold;
2174 ln->la_hold = NULL;
2175 ln->la_numheld = 0;
2176
2177 if (ln->ln_state == ND6_LLINFO_STALE) {
2178 /*
2179 * The first time we send a packet to a
2180 * neighbor whose entry is STALE, we have
2181 * to change the state to DELAY and a sets
2182 * a timer to expire in DELAY_FIRST_PROBE_TIME
2183 * seconds to ensure do neighbor unreachability
2184 * detection on expiration.
2185 * (RFC 2461 7.3.3)
2186 */
2187 nd6_llinfo_setstate(ln, ND6_LLINFO_DELAY);
2188 }
2189
2190 return (chain);
2191 }
2192
2193 int
nd6_output_ifp(struct ifnet * ifp,struct ifnet * origifp,struct mbuf * m,struct sockaddr_in6 * dst,struct route * ro)2194 nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m,
2195 struct sockaddr_in6 *dst, struct route *ro)
2196 {
2197 int error;
2198 int ip6len;
2199 struct ip6_hdr *ip6;
2200 struct m_tag *mtag;
2201
2202 #ifdef MAC
2203 mac_netinet6_nd6_send(ifp, m);
2204 #endif
2205
2206 /*
2207 * If called from nd6_ns_output() (NS), nd6_na_output() (NA),
2208 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA
2209 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND
2210 * to be diverted to user space. When re-injected into the kernel,
2211 * send_output() will directly dispatch them to the outgoing interface.
2212 */
2213 if (send_sendso_input_hook != NULL) {
2214 mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL);
2215 if (mtag != NULL) {
2216 ip6 = mtod(m, struct ip6_hdr *);
2217 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen);
2218 /* Use the SEND socket */
2219 error = send_sendso_input_hook(m, ifp, SND_OUT,
2220 ip6len);
2221 /* -1 == no app on SEND socket */
2222 if (error == 0 || error != -1)
2223 return (error);
2224 }
2225 }
2226
2227 m_clrprotoflags(m); /* Avoid confusing lower layers. */
2228 IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL,
2229 mtod(m, struct ip6_hdr *));
2230
2231 if ((ifp->if_flags & IFF_LOOPBACK) == 0)
2232 origifp = ifp;
2233
2234 error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, ro);
2235 return (error);
2236 }
2237
2238 /*
2239 * Lookup link headerfor @sa_dst address. Stores found
2240 * data in @desten buffer. Copy of lle ln_flags can be also
2241 * saved in @pflags if @pflags is non-NULL.
2242 *
2243 * If destination LLE does not exists or lle state modification
2244 * is required, call "slow" version.
2245 *
2246 * Return values:
2247 * - 0 on success (address copied to buffer).
2248 * - EWOULDBLOCK (no local error, but address is still unresolved)
2249 * - other errors (alloc failure, etc)
2250 */
2251 int
nd6_resolve(struct ifnet * ifp,int gw_flags,struct mbuf * m,const struct sockaddr * sa_dst,u_char * desten,uint32_t * pflags,struct llentry ** plle)2252 nd6_resolve(struct ifnet *ifp, int gw_flags, struct mbuf *m,
2253 const struct sockaddr *sa_dst, u_char *desten, uint32_t *pflags,
2254 struct llentry **plle)
2255 {
2256 struct llentry *ln = NULL;
2257 const struct sockaddr_in6 *dst6;
2258
2259 NET_EPOCH_ASSERT();
2260
2261 if (pflags != NULL)
2262 *pflags = 0;
2263
2264 dst6 = (const struct sockaddr_in6 *)sa_dst;
2265
2266 /* discard the packet if IPv6 operation is disabled on the interface */
2267 if ((ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED)) {
2268 m_freem(m);
2269 return (ENETDOWN); /* better error? */
2270 }
2271
2272 if (m != NULL && m->m_flags & M_MCAST) {
2273 switch (ifp->if_type) {
2274 case IFT_ETHER:
2275 case IFT_L2VLAN:
2276 case IFT_BRIDGE:
2277 ETHER_MAP_IPV6_MULTICAST(&dst6->sin6_addr,
2278 desten);
2279 return (0);
2280 default:
2281 m_freem(m);
2282 return (EAFNOSUPPORT);
2283 }
2284 }
2285
2286 int family = gw_flags >> 16;
2287 int lookup_flags = plle ? LLE_EXCLUSIVE : LLE_UNLOCKED;
2288 ln = nd6_lookup(&dst6->sin6_addr, LLE_SF(family, lookup_flags), ifp);
2289 if (ln != NULL && (ln->r_flags & RLLE_VALID) != 0) {
2290 /* Entry found, let's copy lle info */
2291 bcopy(ln->r_linkdata, desten, ln->r_hdrlen);
2292 if (pflags != NULL)
2293 *pflags = LLE_VALID | (ln->r_flags & RLLE_IFADDR);
2294 llentry_provide_feedback(ln);
2295 if (plle) {
2296 LLE_ADDREF(ln);
2297 *plle = ln;
2298 LLE_WUNLOCK(ln);
2299 }
2300 return (0);
2301 } else if (plle && ln)
2302 LLE_WUNLOCK(ln);
2303
2304 return (nd6_resolve_slow(ifp, family, 0, m, dst6, desten, pflags, plle));
2305 }
2306
2307 /*
2308 * Finds or creates a new llentry for @addr and @family.
2309 * Returns wlocked llentry or NULL.
2310 *
2311 *
2312 * Child LLEs.
2313 *
2314 * Do not have their own state machine (gets marked as static)
2315 * settimer bails out for child LLEs just in case.
2316 *
2317 * Locking order: parent lle gets locked first, chen goes the child.
2318 */
2319 static __noinline struct llentry *
nd6_get_llentry(struct ifnet * ifp,const struct in6_addr * addr,int family)2320 nd6_get_llentry(struct ifnet *ifp, const struct in6_addr *addr, int family)
2321 {
2322 struct llentry *child_lle = NULL;
2323 struct llentry *lle, *lle_tmp;
2324
2325 lle = nd6_alloc(addr, 0, ifp);
2326 if (lle != NULL && family != AF_INET6) {
2327 child_lle = nd6_alloc(addr, 0, ifp);
2328 if (child_lle == NULL) {
2329 lltable_free_entry(LLTABLE6(ifp), lle);
2330 return (NULL);
2331 }
2332 child_lle->r_family = family;
2333 child_lle->la_flags |= LLE_CHILD | LLE_STATIC;
2334 child_lle->ln_state = ND6_LLINFO_INCOMPLETE;
2335 }
2336
2337 if (lle == NULL) {
2338 char ip6buf[INET6_ADDRSTRLEN];
2339 log(LOG_DEBUG,
2340 "nd6_get_llentry: can't allocate llinfo for %s "
2341 "(ln=%p)\n",
2342 ip6_sprintf(ip6buf, addr), lle);
2343 return (NULL);
2344 }
2345
2346 LLTABLE_LOCK(LLTABLE6(ifp));
2347 LLE_WLOCK(lle);
2348 /* Prefer any existing entry over newly-created one */
2349 lle_tmp = nd6_lookup(addr, LLE_SF(AF_INET6, LLE_EXCLUSIVE), ifp);
2350 if (lle_tmp == NULL)
2351 lltable_link_entry(LLTABLE6(ifp), lle);
2352 else {
2353 lltable_free_entry(LLTABLE6(ifp), lle);
2354 lle = lle_tmp;
2355 }
2356 if (child_lle != NULL) {
2357 /* Check if child lle for the same family exists */
2358 lle_tmp = llentry_lookup_family(lle, child_lle->r_family);
2359 LLE_WLOCK(child_lle);
2360 if (lle_tmp == NULL) {
2361 /* Attach */
2362 lltable_link_child_entry(lle, child_lle);
2363 } else {
2364 /* child lle already exists, free newly-created one */
2365 lltable_free_entry(LLTABLE6(ifp), child_lle);
2366 LLE_WLOCK(lle_tmp);
2367 child_lle = lle_tmp;
2368 }
2369 LLE_WUNLOCK(lle);
2370 lle = child_lle;
2371 }
2372 LLTABLE_UNLOCK(LLTABLE6(ifp));
2373 return (lle);
2374 }
2375
2376 /*
2377 * Do L2 address resolution for @sa_dst address. Stores found
2378 * address in @desten buffer. Copy of lle ln_flags can be also
2379 * saved in @pflags if @pflags is non-NULL.
2380 *
2381 * Heavy version.
2382 * Function assume that destination LLE does not exist,
2383 * is invalid or stale, so LLE_EXCLUSIVE lock needs to be acquired.
2384 *
2385 * Set noinline to be dtrace-friendly
2386 */
2387 static __noinline int
nd6_resolve_slow(struct ifnet * ifp,int family,int flags,struct mbuf * m,const struct sockaddr_in6 * dst,u_char * desten,uint32_t * pflags,struct llentry ** plle)2388 nd6_resolve_slow(struct ifnet *ifp, int family, int flags, struct mbuf *m,
2389 const struct sockaddr_in6 *dst, u_char *desten, uint32_t *pflags,
2390 struct llentry **plle)
2391 {
2392 struct llentry *lle = NULL;
2393 struct in6_addr *psrc, src;
2394 int send_ns, ll_len;
2395 char *lladdr;
2396
2397 NET_EPOCH_ASSERT();
2398
2399 /*
2400 * Address resolution or Neighbor Unreachability Detection
2401 * for the next hop.
2402 * At this point, the destination of the packet must be a unicast
2403 * or an anycast address(i.e. not a multicast).
2404 */
2405 lle = nd6_lookup(&dst->sin6_addr, LLE_SF(family, LLE_EXCLUSIVE), ifp);
2406 if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp)) {
2407 /*
2408 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(),
2409 * the condition below is not very efficient. But we believe
2410 * it is tolerable, because this should be a rare case.
2411 */
2412 lle = nd6_get_llentry(ifp, &dst->sin6_addr, family);
2413 }
2414
2415 if (lle == NULL) {
2416 m_freem(m);
2417 return (ENOBUFS);
2418 }
2419
2420 LLE_WLOCK_ASSERT(lle);
2421
2422 /*
2423 * The first time we send a packet to a neighbor whose entry is
2424 * STALE, we have to change the state to DELAY and a sets a timer to
2425 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do
2426 * neighbor unreachability detection on expiration.
2427 * (RFC 2461 7.3.3)
2428 */
2429 if ((!(lle->la_flags & LLE_CHILD)) && (lle->ln_state == ND6_LLINFO_STALE))
2430 nd6_llinfo_setstate(lle, ND6_LLINFO_DELAY);
2431
2432 /*
2433 * If the neighbor cache entry has a state other than INCOMPLETE
2434 * (i.e. its link-layer address is already resolved), just
2435 * send the packet.
2436 */
2437 if (lle->ln_state > ND6_LLINFO_INCOMPLETE) {
2438 if (flags & LLE_ADDRONLY) {
2439 lladdr = lle->ll_addr;
2440 ll_len = ifp->if_addrlen;
2441 } else {
2442 lladdr = lle->r_linkdata;
2443 ll_len = lle->r_hdrlen;
2444 }
2445 bcopy(lladdr, desten, ll_len);
2446 if (pflags != NULL)
2447 *pflags = lle->la_flags;
2448 if (plle) {
2449 LLE_ADDREF(lle);
2450 *plle = lle;
2451 }
2452 LLE_WUNLOCK(lle);
2453 return (0);
2454 }
2455
2456 /*
2457 * There is a neighbor cache entry, but no ethernet address
2458 * response yet. Append this latest packet to the end of the
2459 * packet queue in the mbuf. When it exceeds nd6_maxqueuelen,
2460 * the oldest packet in the queue will be removed.
2461 */
2462 if (m != NULL) {
2463 size_t dropped;
2464
2465 dropped = lltable_append_entry_queue(lle, m, V_nd6_maxqueuelen);
2466 ICMP6STAT_ADD(icp6s_dropped, dropped);
2467 }
2468
2469 /*
2470 * If there has been no NS for the neighbor after entering the
2471 * INCOMPLETE state, send the first solicitation.
2472 * Note that for newly-created lle la_asked will be 0,
2473 * so we will transition from ND6_LLINFO_NOSTATE to
2474 * ND6_LLINFO_INCOMPLETE state here.
2475 */
2476 psrc = NULL;
2477 send_ns = 0;
2478
2479 /* If we have child lle, switch to the parent to send NS */
2480 if (lle->la_flags & LLE_CHILD) {
2481 struct llentry *lle_parent = lle->lle_parent;
2482 LLE_WUNLOCK(lle);
2483 lle = lle_parent;
2484 LLE_WLOCK(lle);
2485 }
2486 if (lle->la_asked == 0) {
2487 lle->la_asked++;
2488 send_ns = 1;
2489 psrc = nd6_llinfo_get_holdsrc(lle, &src);
2490
2491 nd6_llinfo_setstate(lle, ND6_LLINFO_INCOMPLETE);
2492 }
2493 LLE_WUNLOCK(lle);
2494 if (send_ns != 0)
2495 nd6_ns_output(ifp, psrc, NULL, &dst->sin6_addr, NULL);
2496
2497 return (EWOULDBLOCK);
2498 }
2499
2500 /*
2501 * Do L2 address resolution for @sa_dst address. Stores found
2502 * address in @desten buffer. Copy of lle ln_flags can be also
2503 * saved in @pflags if @pflags is non-NULL.
2504 *
2505 * Return values:
2506 * - 0 on success (address copied to buffer).
2507 * - EWOULDBLOCK (no local error, but address is still unresolved)
2508 * - other errors (alloc failure, etc)
2509 */
2510 int
nd6_resolve_addr(struct ifnet * ifp,int flags,const struct sockaddr * dst,char * desten,uint32_t * pflags)2511 nd6_resolve_addr(struct ifnet *ifp, int flags, const struct sockaddr *dst,
2512 char *desten, uint32_t *pflags)
2513 {
2514 int error;
2515
2516 flags |= LLE_ADDRONLY;
2517 error = nd6_resolve_slow(ifp, AF_INET6, flags, NULL,
2518 (const struct sockaddr_in6 *)dst, desten, pflags, NULL);
2519 return (error);
2520 }
2521
2522 int
nd6_flush_holdchain(struct ifnet * ifp,struct llentry * lle,struct mbuf * chain)2523 nd6_flush_holdchain(struct ifnet *ifp, struct llentry *lle, struct mbuf *chain)
2524 {
2525 struct mbuf *m, *m_head;
2526 struct sockaddr_in6 dst6;
2527 int error = 0;
2528
2529 NET_EPOCH_ASSERT();
2530
2531 struct route_in6 ro = {
2532 .ro_prepend = lle->r_linkdata,
2533 .ro_plen = lle->r_hdrlen,
2534 };
2535
2536 lltable_fill_sa_entry(lle, (struct sockaddr *)&dst6);
2537 m_head = chain;
2538
2539 while (m_head) {
2540 m = m_head;
2541 m_head = m_head->m_nextpkt;
2542 m->m_nextpkt = NULL;
2543 error = nd6_output_ifp(ifp, ifp, m, &dst6, (struct route *)&ro);
2544 }
2545
2546 /*
2547 * XXX
2548 * note that intermediate errors are blindly ignored
2549 */
2550 return (error);
2551 }
2552
2553 __noinline void
nd6_flush_children_holdchain(struct ifnet * ifp,struct llentry * lle)2554 nd6_flush_children_holdchain(struct ifnet *ifp, struct llentry *lle)
2555 {
2556 struct llentry *child_lle;
2557 struct mbuf *chain;
2558
2559 NET_EPOCH_ASSERT();
2560
2561 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) {
2562 LLE_WLOCK(child_lle);
2563 chain = nd6_grab_holdchain(child_lle);
2564 LLE_WUNLOCK(child_lle);
2565 nd6_flush_holdchain(ifp, child_lle, chain);
2566 }
2567 }
2568
2569 static int
nd6_need_cache(struct ifnet * ifp)2570 nd6_need_cache(struct ifnet *ifp)
2571 {
2572 /*
2573 * XXX: we currently do not make neighbor cache on any interface
2574 * other than Ethernet and GIF.
2575 *
2576 * RFC2893 says:
2577 * - unidirectional tunnels needs no ND
2578 */
2579 switch (ifp->if_type) {
2580 case IFT_ETHER:
2581 case IFT_IEEE1394:
2582 case IFT_L2VLAN:
2583 case IFT_INFINIBAND:
2584 case IFT_BRIDGE:
2585 case IFT_PROPVIRTUAL:
2586 return (1);
2587 default:
2588 return (0);
2589 }
2590 }
2591
2592 /*
2593 * Add pernament ND6 link-layer record for given
2594 * interface address.
2595 *
2596 * Very similar to IPv4 arp_ifinit(), but:
2597 * 1) IPv6 DAD is performed in different place
2598 * 2) It is called by IPv6 protocol stack in contrast to
2599 * arp_ifinit() which is typically called in SIOCSIFADDR
2600 * driver ioctl handler.
2601 *
2602 */
2603 int
nd6_add_ifa_lle(struct in6_ifaddr * ia)2604 nd6_add_ifa_lle(struct in6_ifaddr *ia)
2605 {
2606 struct ifnet *ifp;
2607 struct llentry *ln, *ln_tmp;
2608 struct sockaddr *dst;
2609
2610 ifp = ia->ia_ifa.ifa_ifp;
2611 if (nd6_need_cache(ifp) == 0)
2612 return (0);
2613
2614 dst = (struct sockaddr *)&ia->ia_addr;
2615 ln = lltable_alloc_entry(LLTABLE6(ifp), LLE_IFADDR, dst);
2616 if (ln == NULL)
2617 return (ENOBUFS);
2618
2619 LLTABLE_LOCK(LLTABLE6(ifp));
2620 LLE_WLOCK(ln);
2621 /* Unlink any entry if exists */
2622 ln_tmp = lla_lookup(LLTABLE6(ifp), LLE_SF(AF_INET6, LLE_EXCLUSIVE), dst);
2623 if (ln_tmp != NULL)
2624 lltable_unlink_entry(LLTABLE6(ifp), ln_tmp);
2625 lltable_link_entry(LLTABLE6(ifp), ln);
2626 LLTABLE_UNLOCK(LLTABLE6(ifp));
2627
2628 if (ln_tmp != NULL)
2629 EVENTHANDLER_INVOKE(lle_event, ln_tmp, LLENTRY_EXPIRED);
2630 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED);
2631
2632 LLE_WUNLOCK(ln);
2633 if (ln_tmp != NULL)
2634 llentry_free(ln_tmp);
2635
2636 return (0);
2637 }
2638
2639 /*
2640 * Removes either all lle entries for given @ia, or lle
2641 * corresponding to @ia address.
2642 */
2643 void
nd6_rem_ifa_lle(struct in6_ifaddr * ia,int all)2644 nd6_rem_ifa_lle(struct in6_ifaddr *ia, int all)
2645 {
2646 struct sockaddr_in6 mask, addr;
2647 struct sockaddr *saddr, *smask;
2648 struct ifnet *ifp;
2649
2650 ifp = ia->ia_ifa.ifa_ifp;
2651 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr));
2652 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask));
2653 saddr = (struct sockaddr *)&addr;
2654 smask = (struct sockaddr *)&mask;
2655
2656 if (all != 0)
2657 lltable_prefix_free(AF_INET6, saddr, smask, LLE_STATIC);
2658 else
2659 lltable_delete_addr(LLTABLE6(ifp), LLE_IFADDR, saddr);
2660 }
2661
2662 static int
nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)2663 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS)
2664 {
2665 struct in6_prefix p;
2666 struct sockaddr_in6 s6;
2667 struct nd_prefix *pr;
2668 struct nd_pfxrouter *pfr;
2669 time_t maxexpire;
2670 int error;
2671 char ip6buf[INET6_ADDRSTRLEN];
2672
2673 if (req->newptr)
2674 return (EPERM);
2675
2676 error = sysctl_wire_old_buffer(req, 0);
2677 if (error != 0)
2678 return (error);
2679
2680 bzero(&p, sizeof(p));
2681 p.origin = PR_ORIG_RA;
2682 bzero(&s6, sizeof(s6));
2683 s6.sin6_family = AF_INET6;
2684 s6.sin6_len = sizeof(s6);
2685
2686 ND6_RLOCK();
2687 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) {
2688 p.prefix = pr->ndpr_prefix;
2689 if (sa6_recoverscope(&p.prefix)) {
2690 log(LOG_ERR, "scope error in prefix list (%s)\n",
2691 ip6_sprintf(ip6buf, &p.prefix.sin6_addr));
2692 /* XXX: press on... */
2693 }
2694 p.raflags = pr->ndpr_raf;
2695 p.prefixlen = pr->ndpr_plen;
2696 p.vltime = pr->ndpr_vltime;
2697 p.pltime = pr->ndpr_pltime;
2698 p.if_index = pr->ndpr_ifp->if_index;
2699 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME)
2700 p.expire = 0;
2701 else {
2702 /* XXX: we assume time_t is signed. */
2703 maxexpire = (-1) &
2704 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1));
2705 if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate)
2706 p.expire = pr->ndpr_lastupdate +
2707 pr->ndpr_vltime +
2708 (time_second - time_uptime);
2709 else
2710 p.expire = maxexpire;
2711 }
2712 p.refcnt = pr->ndpr_addrcnt;
2713 p.flags = pr->ndpr_stateflags;
2714 p.advrtrs = 0;
2715 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry)
2716 p.advrtrs++;
2717 error = SYSCTL_OUT(req, &p, sizeof(p));
2718 if (error != 0)
2719 break;
2720 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) {
2721 s6.sin6_addr = pfr->router->rtaddr;
2722 if (sa6_recoverscope(&s6))
2723 log(LOG_ERR,
2724 "scope error in prefix list (%s)\n",
2725 ip6_sprintf(ip6buf, &pfr->router->rtaddr));
2726 error = SYSCTL_OUT(req, &s6, sizeof(s6));
2727 if (error != 0)
2728 goto out;
2729 }
2730 }
2731 out:
2732 ND6_RUNLOCK();
2733 return (error);
2734 }
2735 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist,
2736 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
2737 NULL, 0, nd6_sysctl_prlist, "S,in6_prefix",
2738 "NDP prefix list");
2739 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen,
2740 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, "");
2741 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer,
2742 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), "");
2743