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