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