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