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