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 bool 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 */ 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 bool regen = false; 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)) 994 regen = true; 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)) { 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 bool 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 /* ignore detached prefixes */ 1124 if (ia6->ia6_ndpr == NULL || 1125 (ia6->ia6_ndpr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1126 return (false); 1127 1128 ifp = ia6->ia_ifa.ifa_ifp; 1129 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1130 struct in6_ifaddr *it6; 1131 1132 if (ifa->ifa_addr->sa_family != AF_INET6) 1133 continue; 1134 1135 it6 = (struct in6_ifaddr *)ifa; 1136 1137 /* ignore detached or no autoconf addresses. */ 1138 if ((it6->ia6_flags & 1139 (IN6_IFF_AUTOCONF | IN6_IFF_DETACHED)) != IN6_IFF_AUTOCONF) 1140 continue; 1141 1142 /* ignore autoconf addresses with different prefixes. */ 1143 if (it6->ia6_ndpr != ia6->ia6_ndpr) 1144 continue; 1145 1146 /* 1147 * Now we are looking at an autoconf address with the same 1148 * prefix as ours. If the address is temporary and is still 1149 * preferred, do not create another one. It would be rare, but 1150 * could happen, for example, when we resume a laptop PC after 1151 * a long period. 1152 */ 1153 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1154 !IFA6_IS_DEPRECATED(it6)) { 1155 public_ifa6 = NULL; 1156 break; 1157 } 1158 1159 /* 1160 * This is a public autoconf address that has the same prefix 1161 * as ours. If it is preferred, keep it. We can't break the 1162 * loop here, because there may be a still-preferred temporary 1163 * address with the prefix. 1164 */ 1165 if (!IFA6_IS_DEPRECATED(it6)) 1166 public_ifa6 = it6; 1167 } 1168 if (public_ifa6 != NULL) 1169 ifa_ref(&public_ifa6->ia_ifa); 1170 1171 if (public_ifa6 != NULL) { 1172 int e; 1173 1174 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) { 1175 ifa_free(&public_ifa6->ia_ifa); 1176 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" 1177 " tmp addr,errno=%d\n", e); 1178 return (false); 1179 } 1180 ifa_free(&public_ifa6->ia_ifa); 1181 return (true); 1182 } 1183 1184 return (false); 1185 } 1186 1187 /* 1188 * Remove prefix and default router list entries corresponding to ifp. Neighbor 1189 * cache entries are freed in in6_domifdetach(). 1190 */ 1191 void 1192 nd6_purge(struct ifnet *ifp) 1193 { 1194 struct nd_prhead prl; 1195 struct nd_prefix *pr, *npr; 1196 1197 LIST_INIT(&prl); 1198 1199 /* Purge default router list entries toward ifp. */ 1200 nd6_defrouter_purge(ifp); 1201 1202 ND6_WLOCK(); 1203 /* 1204 * Remove prefixes on ifp. We should have already removed addresses on 1205 * this interface, so no addresses should be referencing these prefixes. 1206 */ 1207 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) { 1208 if (pr->ndpr_ifp == ifp) 1209 nd6_prefix_unlink(pr, &prl); 1210 } 1211 ND6_WUNLOCK(); 1212 1213 /* Delete the unlinked prefix objects. */ 1214 while ((pr = LIST_FIRST(&prl)) != NULL) { 1215 LIST_REMOVE(pr, ndpr_entry); 1216 nd6_prefix_del(pr); 1217 } 1218 1219 /* cancel default outgoing interface setting */ 1220 if (V_nd6_defifindex == ifp->if_index) 1221 nd6_setdefaultiface(0); 1222 1223 if (ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) { 1224 /* Refresh default router list. */ 1225 defrouter_select_fib(ifp->if_fib); 1226 } 1227 } 1228 1229 /* 1230 * the caller acquires and releases the lock on the lltbls 1231 * Returns the llentry locked 1232 */ 1233 struct llentry * 1234 nd6_lookup(const struct in6_addr *addr6, int flags, struct ifnet *ifp) 1235 { 1236 struct sockaddr_in6 sin6; 1237 struct llentry *ln; 1238 1239 bzero(&sin6, sizeof(sin6)); 1240 sin6.sin6_len = sizeof(struct sockaddr_in6); 1241 sin6.sin6_family = AF_INET6; 1242 sin6.sin6_addr = *addr6; 1243 1244 LLTABLE_RLOCK_ASSERT(LLTABLE6(ifp)); 1245 1246 ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)&sin6); 1247 1248 return (ln); 1249 } 1250 1251 static struct llentry * 1252 nd6_alloc(const struct in6_addr *addr6, int flags, struct ifnet *ifp) 1253 { 1254 struct sockaddr_in6 sin6; 1255 struct llentry *ln; 1256 1257 bzero(&sin6, sizeof(sin6)); 1258 sin6.sin6_len = sizeof(struct sockaddr_in6); 1259 sin6.sin6_family = AF_INET6; 1260 sin6.sin6_addr = *addr6; 1261 1262 ln = lltable_alloc_entry(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6); 1263 if (ln != NULL) 1264 ln->ln_state = ND6_LLINFO_NOSTATE; 1265 1266 return (ln); 1267 } 1268 1269 /* 1270 * Test whether a given IPv6 address can be a neighbor. 1271 */ 1272 static bool 1273 nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 1274 { 1275 1276 /* 1277 * A link-local address is always a neighbor. 1278 * XXX: a link does not necessarily specify a single interface. 1279 */ 1280 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { 1281 struct sockaddr_in6 sin6_copy; 1282 u_int32_t zone; 1283 1284 /* 1285 * We need sin6_copy since sa6_recoverscope() may modify the 1286 * content (XXX). 1287 */ 1288 sin6_copy = *addr; 1289 if (sa6_recoverscope(&sin6_copy)) 1290 return (0); /* XXX: should be impossible */ 1291 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) 1292 return (0); 1293 if (sin6_copy.sin6_scope_id == zone) 1294 return (1); 1295 else 1296 return (0); 1297 } 1298 /* Checking global unicast */ 1299 1300 /* If an address is directly reachable, it is a neigbor */ 1301 struct nhop_object *nh; 1302 nh = fib6_lookup(ifp->if_fib, &addr->sin6_addr, 0, NHR_NONE, 0); 1303 if (nh != NULL && nh->nh_aifp == ifp && (nh->nh_flags & NHF_GATEWAY) == 0) 1304 return (true); 1305 1306 /* 1307 * Check prefixes with desired on-link state, as some may be not 1308 * installed in the routing table. 1309 */ 1310 bool matched = false; 1311 struct nd_prefix *pr; 1312 ND6_RLOCK(); 1313 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1314 if (pr->ndpr_ifp != ifp) 1315 continue; 1316 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) 1317 continue; 1318 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1319 &addr->sin6_addr, &pr->ndpr_mask)) { 1320 matched = true; 1321 break; 1322 } 1323 } 1324 ND6_RUNLOCK(); 1325 if (matched) 1326 return (true); 1327 1328 /* 1329 * If the address is assigned on the node of the other side of 1330 * a p2p interface, the address should be a neighbor. 1331 */ 1332 if (ifp->if_flags & IFF_POINTOPOINT) { 1333 struct ifaddr *ifa; 1334 1335 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1336 if (ifa->ifa_addr->sa_family != addr->sin6_family) 1337 continue; 1338 if (ifa->ifa_dstaddr != NULL && 1339 sa_equal(addr, ifa->ifa_dstaddr)) { 1340 return (true); 1341 } 1342 } 1343 } 1344 1345 /* 1346 * If the default router list is empty, all addresses are regarded 1347 * as on-link, and thus, as a neighbor. 1348 */ 1349 if (ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV && 1350 nd6_defrouter_list_empty() && 1351 V_nd6_defifindex == ifp->if_index) { 1352 return (1); 1353 } 1354 1355 return (0); 1356 } 1357 1358 /* 1359 * Detect if a given IPv6 address identifies a neighbor on a given link. 1360 * XXX: should take care of the destination of a p2p link? 1361 */ 1362 int 1363 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 1364 { 1365 struct llentry *lle; 1366 int rc = 0; 1367 1368 NET_EPOCH_ASSERT(); 1369 1370 if (nd6_is_new_addr_neighbor(addr, ifp)) 1371 return (1); 1372 1373 /* 1374 * Even if the address matches none of our addresses, it might be 1375 * in the neighbor cache. 1376 */ 1377 if ((lle = nd6_lookup(&addr->sin6_addr, LLE_SF(AF_INET6, 0), ifp)) != NULL) { 1378 LLE_RUNLOCK(lle); 1379 rc = 1; 1380 } 1381 return (rc); 1382 } 1383 1384 static __noinline void 1385 nd6_free_children(struct llentry *lle) 1386 { 1387 struct llentry *child_lle; 1388 1389 NET_EPOCH_ASSERT(); 1390 LLE_WLOCK_ASSERT(lle); 1391 1392 while ((child_lle = CK_SLIST_FIRST(&lle->lle_children)) != NULL) { 1393 LLE_WLOCK(child_lle); 1394 lltable_unlink_child_entry(child_lle); 1395 llentry_free(child_lle); 1396 } 1397 } 1398 1399 /* 1400 * Tries to update @lle address/prepend data with new @lladdr. 1401 * 1402 * Returns true on success. 1403 * In any case, @lle is returned wlocked. 1404 */ 1405 static __noinline bool 1406 nd6_try_set_entry_addr_locked(struct ifnet *ifp, struct llentry *lle, char *lladdr) 1407 { 1408 u_char buf[LLE_MAX_LINKHDR]; 1409 int fam, off; 1410 size_t sz; 1411 1412 sz = sizeof(buf); 1413 if (lltable_calc_llheader(ifp, AF_INET6, lladdr, buf, &sz, &off) != 0) 1414 return (false); 1415 1416 /* Update data */ 1417 lltable_set_entry_addr(ifp, lle, buf, sz, off); 1418 1419 struct llentry *child_lle; 1420 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) { 1421 LLE_WLOCK(child_lle); 1422 fam = child_lle->r_family; 1423 sz = sizeof(buf); 1424 if (lltable_calc_llheader(ifp, fam, lladdr, buf, &sz, &off) == 0) { 1425 /* success */ 1426 lltable_set_entry_addr(ifp, child_lle, buf, sz, off); 1427 child_lle->ln_state = ND6_LLINFO_REACHABLE; 1428 } 1429 LLE_WUNLOCK(child_lle); 1430 } 1431 1432 return (true); 1433 } 1434 1435 bool 1436 nd6_try_set_entry_addr(struct ifnet *ifp, struct llentry *lle, char *lladdr) 1437 { 1438 NET_EPOCH_ASSERT(); 1439 LLE_WLOCK_ASSERT(lle); 1440 1441 if (!lltable_trylock(lle)) 1442 return (false); 1443 bool ret = nd6_try_set_entry_addr_locked(ifp, lle, lladdr); 1444 LLTABLE_UNLOCK(lle->lle_tbl); 1445 1446 return (ret); 1447 } 1448 1449 /* 1450 * Free an nd6 llinfo entry. 1451 * Since the function would cause significant changes in the kernel, DO NOT 1452 * make it global, unless you have a strong reason for the change, and are sure 1453 * that the change is safe. 1454 * 1455 * Set noinline to be dtrace-friendly 1456 */ 1457 static __noinline void 1458 nd6_free(struct llentry **lnp, int gc) 1459 { 1460 struct ifnet *ifp; 1461 struct llentry *ln; 1462 struct nd_defrouter *dr; 1463 1464 ln = *lnp; 1465 *lnp = NULL; 1466 1467 LLE_WLOCK_ASSERT(ln); 1468 ND6_RLOCK_ASSERT(); 1469 1470 KASSERT((ln->la_flags & LLE_CHILD) == 0, ("child lle")); 1471 1472 ifp = lltable_get_ifp(ln->lle_tbl); 1473 if ((ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) != 0) 1474 dr = defrouter_lookup_locked(&ln->r_l3addr.addr6, ifp); 1475 else 1476 dr = NULL; 1477 ND6_RUNLOCK(); 1478 1479 if ((ln->la_flags & LLE_DELETED) == 0) 1480 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED); 1481 1482 /* 1483 * we used to have pfctlinput(PRC_HOSTDEAD) here. 1484 * even though it is not harmful, it was not really necessary. 1485 */ 1486 1487 /* cancel timer */ 1488 nd6_llinfo_settimer_locked(ln, -1); 1489 1490 if (ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) { 1491 if (dr != NULL && dr->expire && 1492 ln->ln_state == ND6_LLINFO_STALE && gc) { 1493 /* 1494 * If the reason for the deletion is just garbage 1495 * collection, and the neighbor is an active default 1496 * router, do not delete it. Instead, reset the GC 1497 * timer using the router's lifetime. 1498 * Simply deleting the entry would affect default 1499 * router selection, which is not necessarily a good 1500 * thing, especially when we're using router preference 1501 * values. 1502 * XXX: the check for ln_state would be redundant, 1503 * but we intentionally keep it just in case. 1504 */ 1505 if (dr->expire > time_uptime) 1506 nd6_llinfo_settimer_locked(ln, 1507 (dr->expire - time_uptime) * hz); 1508 else 1509 nd6_llinfo_settimer_locked(ln, 1510 (long)V_nd6_gctimer * hz); 1511 1512 LLE_REMREF(ln); 1513 LLE_WUNLOCK(ln); 1514 defrouter_rele(dr); 1515 return; 1516 } 1517 1518 if (dr) { 1519 /* 1520 * Unreachability of a router might affect the default 1521 * router selection and on-link detection of advertised 1522 * prefixes. 1523 */ 1524 1525 /* 1526 * Temporarily fake the state to choose a new default 1527 * router and to perform on-link determination of 1528 * prefixes correctly. 1529 * Below the state will be set correctly, 1530 * or the entry itself will be deleted. 1531 */ 1532 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1533 } 1534 1535 if (ln->ln_router || dr) { 1536 /* 1537 * We need to unlock to avoid a LOR with rt6_flush() with the 1538 * rnh and for the calls to pfxlist_onlink_check() and 1539 * defrouter_select_fib() in the block further down for calls 1540 * into nd6_lookup(). We still hold a ref. 1541 */ 1542 LLE_WUNLOCK(ln); 1543 1544 /* 1545 * rt6_flush must be called whether or not the neighbor 1546 * is in the Default Router List. 1547 * See a corresponding comment in nd6_na_input(). 1548 */ 1549 rt6_flush(&ln->r_l3addr.addr6, ifp); 1550 } 1551 1552 if (dr) { 1553 /* 1554 * Since defrouter_select_fib() does not affect the 1555 * on-link determination and MIP6 needs the check 1556 * before the default router selection, we perform 1557 * the check now. 1558 */ 1559 pfxlist_onlink_check(); 1560 1561 /* 1562 * Refresh default router list. 1563 */ 1564 defrouter_select_fib(dr->ifp->if_fib); 1565 } 1566 1567 /* 1568 * If this entry was added by an on-link redirect, remove the 1569 * corresponding host route. 1570 */ 1571 if (ln->la_flags & LLE_REDIRECT) 1572 nd6_free_redirect(ln); 1573 1574 if (ln->ln_router || dr) 1575 LLE_WLOCK(ln); 1576 } 1577 1578 /* 1579 * Save to unlock. We still hold an extra reference and will not 1580 * free(9) in llentry_free() if someone else holds one as well. 1581 */ 1582 LLE_WUNLOCK(ln); 1583 LLTABLE_LOCK(ln->lle_tbl); 1584 LLE_WLOCK(ln); 1585 /* Guard against race with other llentry_free(). */ 1586 if (ln->la_flags & LLE_LINKED) { 1587 /* Remove callout reference */ 1588 LLE_REMREF(ln); 1589 lltable_unlink_entry(ln->lle_tbl, ln); 1590 } 1591 LLTABLE_UNLOCK(ln->lle_tbl); 1592 1593 nd6_free_children(ln); 1594 1595 llentry_free(ln); 1596 if (dr != NULL) 1597 defrouter_rele(dr); 1598 } 1599 1600 static int 1601 nd6_isdynrte(const struct rtentry *rt, const struct nhop_object *nh, void *xap) 1602 { 1603 1604 if (nh->nh_flags & NHF_REDIRECT) 1605 return (1); 1606 1607 return (0); 1608 } 1609 1610 /* 1611 * Remove the rtentry for the given llentry, 1612 * both of which were installed by a redirect. 1613 */ 1614 static void 1615 nd6_free_redirect(const struct llentry *ln) 1616 { 1617 int fibnum; 1618 struct sockaddr_in6 sin6; 1619 struct rib_cmd_info rc; 1620 struct epoch_tracker et; 1621 1622 lltable_fill_sa_entry(ln, (struct sockaddr *)&sin6); 1623 1624 NET_EPOCH_ENTER(et); 1625 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) 1626 rib_del_route_px(fibnum, (struct sockaddr *)&sin6, 128, 1627 nd6_isdynrte, NULL, 0, &rc); 1628 NET_EPOCH_EXIT(et); 1629 } 1630 1631 /* 1632 * Updates status of the default router route. 1633 */ 1634 static void 1635 check_release_defrouter(const struct rib_cmd_info *rc, void *_cbdata) 1636 { 1637 struct nd_defrouter *dr; 1638 struct nhop_object *nh; 1639 1640 nh = rc->rc_nh_old; 1641 if (rc->rc_cmd == RTM_DELETE && (nh->nh_flags & NHF_DEFAULT) != 0) { 1642 dr = defrouter_lookup(&nh->gw6_sa.sin6_addr, nh->nh_ifp); 1643 if (dr != NULL) { 1644 dr->installed = 0; 1645 defrouter_rele(dr); 1646 } 1647 } 1648 } 1649 1650 void 1651 nd6_subscription_cb(struct rib_head *rnh, struct rib_cmd_info *rc, void *arg) 1652 { 1653 1654 rib_decompose_notification(rc, check_release_defrouter, NULL); 1655 if (rc->rc_cmd == RTM_DELETE && !NH_IS_NHGRP(rc->rc_nh_old)) 1656 check_release_defrouter(rc, NULL); 1657 } 1658 1659 int 1660 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp) 1661 { 1662 struct epoch_tracker et; 1663 struct in6_ndireq *ndi = (struct in6_ndireq *)data; 1664 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; 1665 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; 1666 struct in6_ifextra *ext = ifp->if_inet6; 1667 int error = 0; 1668 1669 /* XXXGL: safety against IFT_PFSYNC */ 1670 if (ext == NULL) 1671 return (EPFNOSUPPORT); 1672 #define ND ndi->ndi 1673 switch (cmd) { 1674 case SIOCGIFINFO_IN6: 1675 ND = (struct nd_ifinfo){ 1676 .linkmtu = ext->nd_linkmtu, 1677 .maxmtu = ext->nd_maxmtu, 1678 .basereachable = ext->nd_basereachable, 1679 .reachable = ext->nd_reachable, 1680 .retrans = ext->nd_retrans, 1681 .flags = ext->nd_flags, 1682 .recalctm = ext->nd_recalc_timer, 1683 .chlim = ext->nd_curhoplimit, 1684 .initialized = 1, 1685 }; 1686 break; 1687 case SIOCSIFINFO_IN6: 1688 /* 1689 * used to change host variables from userland. 1690 * intended for a use on router to reflect RA configurations. 1691 */ 1692 /* 0 means 'unspecified' */ 1693 if (ND.linkmtu != 0) { 1694 if (ND.linkmtu < IPV6_MMTU || 1695 ND.linkmtu > in6_ifmtu(ifp)) { 1696 error = EINVAL; 1697 break; 1698 } 1699 ext->nd_linkmtu = ND.linkmtu; 1700 } 1701 1702 if (ND.basereachable != 0) { 1703 uint32_t obasereachable = ext->nd_basereachable; 1704 1705 ext->nd_basereachable = ND.basereachable; 1706 if (ND.basereachable != obasereachable) 1707 ext->nd_reachable = 1708 ND_COMPUTE_RTIME(ND.basereachable); 1709 } 1710 if (ND.retrans != 0) 1711 ext->nd_retrans = ND.retrans; 1712 if (ND.chlim != 0) 1713 ext->nd_curhoplimit = ND.chlim; 1714 /* FALLTHROUGH */ 1715 case SIOCSIFINFO_FLAGS: 1716 { 1717 struct ifaddr *ifa; 1718 struct in6_ifaddr *ia; 1719 1720 if ((ext->nd_flags & ND6_IFF_IFDISABLED) && 1721 !(ND.flags & ND6_IFF_IFDISABLED)) { 1722 /* ifdisabled 1->0 transision */ 1723 1724 /* 1725 * If the interface is marked as ND6_IFF_IFDISABLED and 1726 * has an link-local address with IN6_IFF_DUPLICATED, 1727 * do not clear ND6_IFF_IFDISABLED. 1728 * See RFC 4862, Section 5.4.5. 1729 */ 1730 NET_EPOCH_ENTER(et); 1731 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1732 if (ifa->ifa_addr->sa_family != AF_INET6) 1733 continue; 1734 ia = (struct in6_ifaddr *)ifa; 1735 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) && 1736 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) 1737 break; 1738 } 1739 NET_EPOCH_EXIT(et); 1740 1741 if (ifa != NULL) { 1742 /* LLA is duplicated. */ 1743 ND.flags |= ND6_IFF_IFDISABLED; 1744 log(LOG_ERR, "Cannot enable an interface" 1745 " with a link-local address marked" 1746 " duplicate.\n"); 1747 } else { 1748 ext->nd_flags &= ~ND6_IFF_IFDISABLED; 1749 if (ifp->if_flags & IFF_UP) 1750 in6_if_up(ifp); 1751 } 1752 } else if (!(ext->nd_flags & ND6_IFF_IFDISABLED) && 1753 (ND.flags & ND6_IFF_IFDISABLED)) { 1754 /* ifdisabled 0->1 transision */ 1755 /* Mark all IPv6 address as tentative. */ 1756 1757 ext->nd_flags |= ND6_IFF_IFDISABLED; 1758 if (V_ip6_dad_count > 0 && 1759 (ext->nd_flags & ND6_IFF_NO_DAD) == 0) { 1760 NET_EPOCH_ENTER(et); 1761 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, 1762 ifa_link) { 1763 if (ifa->ifa_addr->sa_family != 1764 AF_INET6) 1765 continue; 1766 ia = (struct in6_ifaddr *)ifa; 1767 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1768 } 1769 NET_EPOCH_EXIT(et); 1770 } 1771 } 1772 1773 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) { 1774 if (!(ext->nd_flags & ND6_IFF_AUTO_LINKLOCAL)) { 1775 /* auto_linklocal 0->1 transision */ 1776 1777 /* If no link-local address on ifp, configure */ 1778 ext->nd_flags |= ND6_IFF_AUTO_LINKLOCAL; 1779 in6_ifattach(ifp, NULL); 1780 } else if (!(ND.flags & ND6_IFF_IFDISABLED) && 1781 ifp->if_flags & IFF_UP) { 1782 /* 1783 * When the IF already has 1784 * ND6_IFF_AUTO_LINKLOCAL, no link-local 1785 * address is assigned, and IFF_UP, try to 1786 * assign one. 1787 */ 1788 NET_EPOCH_ENTER(et); 1789 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, 1790 ifa_link) { 1791 if (ifa->ifa_addr->sa_family != 1792 AF_INET6) 1793 continue; 1794 ia = (struct in6_ifaddr *)ifa; 1795 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) 1796 break; 1797 } 1798 NET_EPOCH_EXIT(et); 1799 if (ifa != NULL) 1800 /* No LLA is configured. */ 1801 in6_ifattach(ifp, NULL); 1802 } 1803 } 1804 ext->nd_flags = ND.flags; 1805 break; 1806 } 1807 #undef ND 1808 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ 1809 /* sync kernel routing table with the default router list */ 1810 defrouter_reset(); 1811 defrouter_select_fib(RT_ALL_FIBS); 1812 break; 1813 case SIOCSPFXFLUSH_IN6: 1814 { 1815 /* flush all the prefix advertised by routers */ 1816 struct in6_ifaddr *ia, *ia_next; 1817 struct nd_prefix *pr, *next; 1818 struct nd_prhead prl; 1819 1820 LIST_INIT(&prl); 1821 1822 ND6_WLOCK(); 1823 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) { 1824 if (ND6_PREFIX_WITH_ROUTER(pr)) 1825 nd6_prefix_unlink(pr, &prl); 1826 } 1827 ND6_WUNLOCK(); 1828 1829 while ((pr = LIST_FIRST(&prl)) != NULL) { 1830 LIST_REMOVE(pr, ndpr_entry); 1831 /* XXXRW: in6_ifaddrhead locking. */ 1832 CK_STAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link, 1833 ia_next) { 1834 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1835 continue; 1836 1837 if (ia->ia6_ndpr == pr) 1838 in6_purgeaddr(&ia->ia_ifa); 1839 } 1840 nd6_prefix_del(pr); 1841 } 1842 break; 1843 } 1844 case SIOCSRTRFLUSH_IN6: 1845 { 1846 /* flush all the default routers */ 1847 1848 defrouter_reset(); 1849 nd6_defrouter_flush_all(); 1850 defrouter_select_fib(RT_ALL_FIBS); 1851 break; 1852 } 1853 case SIOCGNBRINFO_IN6: 1854 { 1855 struct llentry *ln; 1856 struct in6_addr nb_addr = nbi->addr; /* make local for safety */ 1857 1858 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0) 1859 return (error); 1860 1861 NET_EPOCH_ENTER(et); 1862 ln = nd6_lookup(&nb_addr, LLE_SF(AF_INET6, 0), ifp); 1863 NET_EPOCH_EXIT(et); 1864 1865 if (ln == NULL) { 1866 error = EINVAL; 1867 break; 1868 } 1869 nbi->state = ln->ln_state; 1870 nbi->asked = ln->la_asked; 1871 nbi->isrouter = ln->ln_router; 1872 if (ln->la_expire == 0) 1873 nbi->expire = 0; 1874 else 1875 nbi->expire = ln->la_expire + ln->lle_remtime / hz + 1876 (time_second - time_uptime); 1877 LLE_RUNLOCK(ln); 1878 break; 1879 } 1880 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1881 ndif->ifindex = V_nd6_defifindex; 1882 break; 1883 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1884 return (nd6_setdefaultiface(ndif->ifindex)); 1885 } 1886 return (error); 1887 } 1888 1889 /* 1890 * Calculates new isRouter value based on provided parameters and 1891 * returns it. 1892 */ 1893 static int 1894 nd6_is_router(int type, int code, int is_new, int old_addr, int new_addr, 1895 int ln_router) 1896 { 1897 1898 /* 1899 * ICMP6 type dependent behavior. 1900 * 1901 * NS: clear IsRouter if new entry 1902 * RS: clear IsRouter 1903 * RA: set IsRouter if there's lladdr 1904 * redir: clear IsRouter if new entry 1905 * 1906 * RA case, (1): 1907 * The spec says that we must set IsRouter in the following cases: 1908 * - If lladdr exist, set IsRouter. This means (1-5). 1909 * - If it is old entry (!newentry), set IsRouter. This means (7). 1910 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 1911 * A quetion arises for (1) case. (1) case has no lladdr in the 1912 * neighbor cache, this is similar to (6). 1913 * This case is rare but we figured that we MUST NOT set IsRouter. 1914 * 1915 * is_new old_addr new_addr NS RS RA redir 1916 * D R 1917 * 0 n n (1) c ? s 1918 * 0 y n (2) c s s 1919 * 0 n y (3) c s s 1920 * 0 y y (4) c s s 1921 * 0 y y (5) c s s 1922 * 1 -- n (6) c c c s 1923 * 1 -- y (7) c c s c s 1924 * 1925 * (c=clear s=set) 1926 */ 1927 switch (type & 0xff) { 1928 case ND_NEIGHBOR_SOLICIT: 1929 /* 1930 * New entry must have is_router flag cleared. 1931 */ 1932 if (is_new) /* (6-7) */ 1933 ln_router = 0; 1934 break; 1935 case ND_REDIRECT: 1936 /* 1937 * If the icmp is a redirect to a better router, always set the 1938 * is_router flag. Otherwise, if the entry is newly created, 1939 * clear the flag. [RFC 2461, sec 8.3] 1940 */ 1941 if (code == ND_REDIRECT_ROUTER) 1942 ln_router = 1; 1943 else { 1944 if (is_new) /* (6-7) */ 1945 ln_router = 0; 1946 } 1947 break; 1948 case ND_ROUTER_SOLICIT: 1949 /* 1950 * is_router flag must always be cleared. 1951 */ 1952 ln_router = 0; 1953 break; 1954 case ND_ROUTER_ADVERT: 1955 /* 1956 * Mark an entry with lladdr as a router. 1957 */ 1958 if ((!is_new && (old_addr || new_addr)) || /* (2-5) */ 1959 (is_new && new_addr)) { /* (7) */ 1960 ln_router = 1; 1961 } 1962 break; 1963 } 1964 1965 return (ln_router); 1966 } 1967 1968 /* 1969 * Create neighbor cache entry and cache link-layer address, 1970 * on reception of inbound ND6 packets. (RS/RA/NS/redirect) 1971 * 1972 * type - ICMP6 type 1973 * code - type dependent information 1974 * 1975 */ 1976 void 1977 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr, 1978 int lladdrlen, int type, int code) 1979 { 1980 struct llentry *ln = NULL, *ln_tmp; 1981 int is_newentry; 1982 int do_update; 1983 int olladdr; 1984 int llchange; 1985 int flags; 1986 uint16_t router = 0; 1987 struct mbuf *chain = NULL; 1988 u_char linkhdr[LLE_MAX_LINKHDR]; 1989 size_t linkhdrsize; 1990 int lladdr_off; 1991 1992 NET_EPOCH_ASSERT(); 1993 1994 KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__)); 1995 KASSERT(from != NULL, ("%s: from == NULL", __func__)); 1996 1997 /* nothing must be updated for unspecified address */ 1998 if (IN6_IS_ADDR_UNSPECIFIED(from)) 1999 return; 2000 2001 /* 2002 * Validation about ifp->if_addrlen and lladdrlen must be done in 2003 * the caller. 2004 * 2005 * XXX If the link does not have link-layer adderss, what should 2006 * we do? (ifp->if_addrlen == 0) 2007 * Spec says nothing in sections for RA, RS and NA. There's small 2008 * description on it in NS section (RFC 2461 7.2.3). 2009 */ 2010 flags = lladdr ? LLE_EXCLUSIVE : 0; 2011 ln = nd6_lookup(from, LLE_SF(AF_INET6, flags), ifp); 2012 is_newentry = 0; 2013 if (ln == NULL) { 2014 flags |= LLE_EXCLUSIVE; 2015 ln = nd6_alloc(from, 0, ifp); 2016 if (ln == NULL) 2017 return; 2018 2019 /* 2020 * Since we already know all the data for the new entry, 2021 * fill it before insertion. 2022 */ 2023 if (lladdr != NULL) { 2024 linkhdrsize = sizeof(linkhdr); 2025 if (lltable_calc_llheader(ifp, AF_INET6, lladdr, 2026 linkhdr, &linkhdrsize, &lladdr_off) != 0) { 2027 lltable_free_entry(LLTABLE6(ifp), ln); 2028 return; 2029 } 2030 lltable_set_entry_addr(ifp, ln, linkhdr, linkhdrsize, 2031 lladdr_off); 2032 } 2033 2034 LLTABLE_LOCK(LLTABLE6(ifp)); 2035 LLE_WLOCK(ln); 2036 /* Prefer any existing lle over newly-created one */ 2037 ln_tmp = nd6_lookup(from, LLE_SF(AF_INET6, LLE_EXCLUSIVE), ifp); 2038 if (ln_tmp == NULL) 2039 lltable_link_entry(LLTABLE6(ifp), ln); 2040 LLTABLE_UNLOCK(LLTABLE6(ifp)); 2041 if (ln_tmp == NULL) { 2042 /* No existing lle, mark as new entry (6,7) */ 2043 is_newentry = 1; 2044 if (lladdr != NULL) { /* (7) */ 2045 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); 2046 EVENTHANDLER_INVOKE(lle_event, ln, 2047 LLENTRY_RESOLVED); 2048 } 2049 } else { 2050 lltable_free_entry(LLTABLE6(ifp), ln); 2051 ln = ln_tmp; 2052 ln_tmp = NULL; 2053 } 2054 } 2055 /* do nothing if static ndp is set */ 2056 if ((ln->la_flags & LLE_STATIC)) { 2057 if (flags & LLE_EXCLUSIVE) 2058 LLE_WUNLOCK(ln); 2059 else 2060 LLE_RUNLOCK(ln); 2061 return; 2062 } 2063 2064 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0; 2065 if (olladdr && lladdr) { 2066 llchange = bcmp(lladdr, ln->ll_addr, 2067 ifp->if_addrlen); 2068 } else if (!olladdr && lladdr) 2069 llchange = 1; 2070 else 2071 llchange = 0; 2072 2073 /* 2074 * newentry olladdr lladdr llchange (*=record) 2075 * 0 n n -- (1) 2076 * 0 y n -- (2) 2077 * 0 n y y (3) * STALE 2078 * 0 y y n (4) * 2079 * 0 y y y (5) * STALE 2080 * 1 -- n -- (6) NOSTATE(= PASSIVE) 2081 * 1 -- y -- (7) * STALE 2082 */ 2083 2084 do_update = 0; 2085 if (is_newentry == 0 && llchange != 0) { 2086 do_update = 1; /* (3,5) */ 2087 2088 /* 2089 * Record source link-layer address 2090 * XXX is it dependent to ifp->if_type? 2091 */ 2092 if (!nd6_try_set_entry_addr(ifp, ln, lladdr)) { 2093 /* Entry was deleted */ 2094 LLE_WUNLOCK(ln); 2095 return; 2096 } 2097 2098 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); 2099 2100 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED); 2101 2102 if (ln->la_hold != NULL) 2103 chain = nd6_grab_holdchain(ln); 2104 } 2105 2106 /* Calculates new router status */ 2107 router = nd6_is_router(type, code, is_newentry, olladdr, 2108 lladdr != NULL ? 1 : 0, ln->ln_router); 2109 2110 ln->ln_router = router; 2111 /* Mark non-router redirects with special flag */ 2112 if ((type & 0xFF) == ND_REDIRECT && code != ND_REDIRECT_ROUTER) 2113 ln->la_flags |= LLE_REDIRECT; 2114 2115 if (flags & LLE_EXCLUSIVE) 2116 LLE_WUNLOCK(ln); 2117 else 2118 LLE_RUNLOCK(ln); 2119 2120 if (chain != NULL) 2121 nd6_flush_holdchain(ifp, ln, chain); 2122 if (do_update) 2123 nd6_flush_children_holdchain(ifp, ln); 2124 2125 /* 2126 * When the link-layer address of a router changes, select the 2127 * best router again. In particular, when the neighbor entry is newly 2128 * created, it might affect the selection policy. 2129 * Question: can we restrict the first condition to the "is_newentry" 2130 * case? 2131 * XXX: when we hear an RA from a new router with the link-layer 2132 * address option, defrouter_select_fib() is called twice, since 2133 * defrtrlist_update called the function as well. However, I believe 2134 * we can compromise the overhead, since it only happens the first 2135 * time. 2136 * XXX: although defrouter_select_fib() should not have a bad effect 2137 * for those are not autoconfigured hosts, we explicitly avoid such 2138 * cases for safety. 2139 */ 2140 if ((do_update || is_newentry) && router && 2141 ifp->if_inet6->nd_flags & ND6_IFF_ACCEPT_RTADV) { 2142 /* 2143 * guaranteed recursion 2144 */ 2145 defrouter_select_fib(ifp->if_fib); 2146 } 2147 } 2148 2149 static void 2150 nd6_slowtimo(void *arg) 2151 { 2152 struct epoch_tracker et; 2153 CURVNET_SET((struct vnet *) arg); 2154 struct in6_ifextra *nd6if; 2155 struct ifnet *ifp; 2156 2157 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 2158 nd6_slowtimo, curvnet); 2159 NET_EPOCH_ENTER(et); 2160 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2161 if ((nd6if = ifp->if_inet6) == NULL) 2162 continue; 2163 if (nd6if->nd_basereachable && /* already initialized */ 2164 (nd6if->nd_recalc_timer -= ND6_SLOWTIMER_INTERVAL) <= 0) { 2165 /* 2166 * Since reachable time rarely changes by router 2167 * advertisements, we SHOULD insure that a new random 2168 * value gets recomputed at least once every few hours. 2169 * (RFC 2461, 6.3.4) 2170 */ 2171 nd6if->nd_recalc_timer = V_nd6_recalc_reachtm_interval; 2172 nd6if->nd_reachable = 2173 ND_COMPUTE_RTIME(nd6if->nd_basereachable); 2174 } 2175 } 2176 NET_EPOCH_EXIT(et); 2177 CURVNET_RESTORE(); 2178 } 2179 2180 struct mbuf * 2181 nd6_grab_holdchain(struct llentry *ln) 2182 { 2183 struct mbuf *chain; 2184 2185 LLE_WLOCK_ASSERT(ln); 2186 2187 chain = ln->la_hold; 2188 ln->la_hold = NULL; 2189 ln->la_numheld = 0; 2190 2191 if (ln->ln_state == ND6_LLINFO_STALE) { 2192 /* 2193 * The first time we send a packet to a 2194 * neighbor whose entry is STALE, we have 2195 * to change the state to DELAY and a sets 2196 * a timer to expire in DELAY_FIRST_PROBE_TIME 2197 * seconds to ensure do neighbor unreachability 2198 * detection on expiration. 2199 * (RFC 2461 7.3.3) 2200 */ 2201 nd6_llinfo_setstate(ln, ND6_LLINFO_DELAY); 2202 } 2203 2204 return (chain); 2205 } 2206 2207 int 2208 nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m, 2209 struct sockaddr_in6 *dst, struct route *ro) 2210 { 2211 int error; 2212 int ip6len; 2213 struct ip6_hdr *ip6; 2214 struct m_tag *mtag; 2215 2216 #ifdef MAC 2217 mac_netinet6_nd6_send(ifp, m); 2218 #endif 2219 2220 /* 2221 * If called from nd6_ns_output() (NS), nd6_na_output() (NA), 2222 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA 2223 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND 2224 * to be diverted to user space. When re-injected into the kernel, 2225 * send_output() will directly dispatch them to the outgoing interface. 2226 */ 2227 if (send_sendso_input_hook != NULL) { 2228 mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL); 2229 if (mtag != NULL) { 2230 ip6 = mtod(m, struct ip6_hdr *); 2231 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen); 2232 /* Use the SEND socket */ 2233 error = send_sendso_input_hook(m, ifp, SND_OUT, 2234 ip6len); 2235 /* -1 == no app on SEND socket */ 2236 if (error == 0 || error != -1) 2237 return (error); 2238 } 2239 } 2240 2241 m_clrprotoflags(m); /* Avoid confusing lower layers. */ 2242 IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL, 2243 mtod(m, struct ip6_hdr *)); 2244 2245 if ((ifp->if_flags & IFF_LOOPBACK) == 0) 2246 origifp = ifp; 2247 2248 error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, ro); 2249 return (error); 2250 } 2251 2252 /* 2253 * Lookup link headerfor @sa_dst address. Stores found 2254 * data in @desten buffer. Copy of lle ln_flags can be also 2255 * saved in @pflags if @pflags is non-NULL. 2256 * 2257 * If destination LLE does not exists or lle state modification 2258 * is required, call "slow" version. 2259 * 2260 * Return values: 2261 * - 0 on success (address copied to buffer). 2262 * - EWOULDBLOCK (no local error, but address is still unresolved) 2263 * - other errors (alloc failure, etc) 2264 */ 2265 int 2266 nd6_resolve(struct ifnet *ifp, int gw_flags, struct mbuf *m, 2267 const struct sockaddr *sa_dst, u_char *desten, uint32_t *pflags, 2268 struct llentry **plle) 2269 { 2270 struct llentry *ln = NULL; 2271 const struct sockaddr_in6 *dst6; 2272 2273 NET_EPOCH_ASSERT(); 2274 2275 if (pflags != NULL) 2276 *pflags = 0; 2277 2278 dst6 = (const struct sockaddr_in6 *)sa_dst; 2279 2280 /* discard the packet if IPv6 operation is disabled on the interface */ 2281 if ((ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED)) { 2282 m_freem(m); 2283 return (ENETDOWN); /* better error? */ 2284 } 2285 2286 if (m != NULL && m->m_flags & M_MCAST) { 2287 switch (ifp->if_type) { 2288 case IFT_ETHER: 2289 case IFT_L2VLAN: 2290 case IFT_BRIDGE: 2291 ETHER_MAP_IPV6_MULTICAST(&dst6->sin6_addr, 2292 desten); 2293 return (0); 2294 default: 2295 m_freem(m); 2296 return (EAFNOSUPPORT); 2297 } 2298 } 2299 2300 int family = gw_flags >> 16; 2301 int lookup_flags = plle ? LLE_EXCLUSIVE : LLE_UNLOCKED; 2302 ln = nd6_lookup(&dst6->sin6_addr, LLE_SF(family, lookup_flags), ifp); 2303 if (ln != NULL && (ln->r_flags & RLLE_VALID) != 0) { 2304 /* Entry found, let's copy lle info */ 2305 bcopy(ln->r_linkdata, desten, ln->r_hdrlen); 2306 if (pflags != NULL) 2307 *pflags = LLE_VALID | (ln->r_flags & RLLE_IFADDR); 2308 llentry_provide_feedback(ln); 2309 if (plle) { 2310 LLE_ADDREF(ln); 2311 *plle = ln; 2312 LLE_WUNLOCK(ln); 2313 } 2314 return (0); 2315 } else if (plle && ln) 2316 LLE_WUNLOCK(ln); 2317 2318 return (nd6_resolve_slow(ifp, family, 0, m, dst6, desten, pflags, plle)); 2319 } 2320 2321 /* 2322 * Finds or creates a new llentry for @addr and @family. 2323 * Returns wlocked llentry or NULL. 2324 * 2325 * 2326 * Child LLEs. 2327 * 2328 * Do not have their own state machine (gets marked as static) 2329 * settimer bails out for child LLEs just in case. 2330 * 2331 * Locking order: parent lle gets locked first, chen goes the child. 2332 */ 2333 static __noinline struct llentry * 2334 nd6_get_llentry(struct ifnet *ifp, const struct in6_addr *addr, int family) 2335 { 2336 struct llentry *child_lle = NULL; 2337 struct llentry *lle, *lle_tmp; 2338 2339 lle = nd6_alloc(addr, 0, ifp); 2340 if (lle != NULL && family != AF_INET6) { 2341 child_lle = nd6_alloc(addr, 0, ifp); 2342 if (child_lle == NULL) { 2343 lltable_free_entry(LLTABLE6(ifp), lle); 2344 return (NULL); 2345 } 2346 child_lle->r_family = family; 2347 child_lle->la_flags |= LLE_CHILD | LLE_STATIC; 2348 child_lle->ln_state = ND6_LLINFO_INCOMPLETE; 2349 } 2350 2351 if (lle == NULL) { 2352 char ip6buf[INET6_ADDRSTRLEN]; 2353 log(LOG_DEBUG, 2354 "nd6_get_llentry: can't allocate llinfo for %s " 2355 "(ln=%p)\n", 2356 ip6_sprintf(ip6buf, addr), lle); 2357 return (NULL); 2358 } 2359 2360 LLTABLE_LOCK(LLTABLE6(ifp)); 2361 LLE_WLOCK(lle); 2362 /* Prefer any existing entry over newly-created one */ 2363 lle_tmp = nd6_lookup(addr, LLE_SF(AF_INET6, LLE_EXCLUSIVE), ifp); 2364 if (lle_tmp == NULL) 2365 lltable_link_entry(LLTABLE6(ifp), lle); 2366 else { 2367 lltable_free_entry(LLTABLE6(ifp), lle); 2368 lle = lle_tmp; 2369 } 2370 if (child_lle != NULL) { 2371 /* Check if child lle for the same family exists */ 2372 lle_tmp = llentry_lookup_family(lle, child_lle->r_family); 2373 LLE_WLOCK(child_lle); 2374 if (lle_tmp == NULL) { 2375 /* Attach */ 2376 lltable_link_child_entry(lle, child_lle); 2377 } else { 2378 /* child lle already exists, free newly-created one */ 2379 lltable_free_entry(LLTABLE6(ifp), child_lle); 2380 LLE_WLOCK(lle_tmp); 2381 child_lle = lle_tmp; 2382 } 2383 LLE_WUNLOCK(lle); 2384 lle = child_lle; 2385 } 2386 LLTABLE_UNLOCK(LLTABLE6(ifp)); 2387 return (lle); 2388 } 2389 2390 /* 2391 * Do L2 address resolution for @sa_dst address. Stores found 2392 * address in @desten buffer. Copy of lle ln_flags can be also 2393 * saved in @pflags if @pflags is non-NULL. 2394 * 2395 * Heavy version. 2396 * Function assume that destination LLE does not exist, 2397 * is invalid or stale, so LLE_EXCLUSIVE lock needs to be acquired. 2398 * 2399 * Set noinline to be dtrace-friendly 2400 */ 2401 static __noinline int 2402 nd6_resolve_slow(struct ifnet *ifp, int family, int flags, struct mbuf *m, 2403 const struct sockaddr_in6 *dst, u_char *desten, uint32_t *pflags, 2404 struct llentry **plle) 2405 { 2406 struct llentry *lle = NULL; 2407 struct in6_addr *psrc, src; 2408 int send_ns, ll_len; 2409 char *lladdr; 2410 2411 NET_EPOCH_ASSERT(); 2412 2413 /* 2414 * Address resolution or Neighbor Unreachability Detection 2415 * for the next hop. 2416 * At this point, the destination of the packet must be a unicast 2417 * or an anycast address(i.e. not a multicast). 2418 */ 2419 lle = nd6_lookup(&dst->sin6_addr, LLE_SF(family, LLE_EXCLUSIVE), ifp); 2420 if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp)) { 2421 /* 2422 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 2423 * the condition below is not very efficient. But we believe 2424 * it is tolerable, because this should be a rare case. 2425 */ 2426 lle = nd6_get_llentry(ifp, &dst->sin6_addr, family); 2427 } 2428 2429 if (lle == NULL) { 2430 m_freem(m); 2431 return (ENOBUFS); 2432 } 2433 2434 LLE_WLOCK_ASSERT(lle); 2435 2436 /* 2437 * The first time we send a packet to a neighbor whose entry is 2438 * STALE, we have to change the state to DELAY and a sets a timer to 2439 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 2440 * neighbor unreachability detection on expiration. 2441 * (RFC 2461 7.3.3) 2442 */ 2443 if ((!(lle->la_flags & LLE_CHILD)) && (lle->ln_state == ND6_LLINFO_STALE)) 2444 nd6_llinfo_setstate(lle, ND6_LLINFO_DELAY); 2445 2446 /* 2447 * If the neighbor cache entry has a state other than INCOMPLETE 2448 * (i.e. its link-layer address is already resolved), just 2449 * send the packet. 2450 */ 2451 if (lle->ln_state > ND6_LLINFO_INCOMPLETE) { 2452 if (flags & LLE_ADDRONLY) { 2453 lladdr = lle->ll_addr; 2454 ll_len = ifp->if_addrlen; 2455 } else { 2456 lladdr = lle->r_linkdata; 2457 ll_len = lle->r_hdrlen; 2458 } 2459 bcopy(lladdr, desten, ll_len); 2460 if (pflags != NULL) 2461 *pflags = lle->la_flags; 2462 if (plle) { 2463 LLE_ADDREF(lle); 2464 *plle = lle; 2465 } 2466 LLE_WUNLOCK(lle); 2467 return (0); 2468 } 2469 2470 /* 2471 * There is a neighbor cache entry, but no ethernet address 2472 * response yet. Append this latest packet to the end of the 2473 * packet queue in the mbuf. When it exceeds nd6_maxqueuelen, 2474 * the oldest packet in the queue will be removed. 2475 */ 2476 if (m != NULL) { 2477 size_t dropped; 2478 2479 dropped = lltable_append_entry_queue(lle, m, V_nd6_maxqueuelen); 2480 ICMP6STAT_ADD(icp6s_dropped, dropped); 2481 } 2482 2483 /* 2484 * If there has been no NS for the neighbor after entering the 2485 * INCOMPLETE state, send the first solicitation. 2486 * Note that for newly-created lle la_asked will be 0, 2487 * so we will transition from ND6_LLINFO_NOSTATE to 2488 * ND6_LLINFO_INCOMPLETE state here. 2489 */ 2490 psrc = NULL; 2491 send_ns = 0; 2492 2493 /* If we have child lle, switch to the parent to send NS */ 2494 if (lle->la_flags & LLE_CHILD) { 2495 struct llentry *lle_parent = lle->lle_parent; 2496 LLE_WUNLOCK(lle); 2497 lle = lle_parent; 2498 LLE_WLOCK(lle); 2499 } 2500 if (lle->la_asked == 0) { 2501 lle->la_asked++; 2502 send_ns = 1; 2503 psrc = nd6_llinfo_get_holdsrc(lle, &src); 2504 2505 nd6_llinfo_setstate(lle, ND6_LLINFO_INCOMPLETE); 2506 } 2507 LLE_WUNLOCK(lle); 2508 if (send_ns != 0) 2509 nd6_ns_output(ifp, psrc, NULL, &dst->sin6_addr, NULL); 2510 2511 return (EWOULDBLOCK); 2512 } 2513 2514 /* 2515 * Do L2 address resolution for @sa_dst address. Stores found 2516 * address in @desten buffer. Copy of lle ln_flags can be also 2517 * saved in @pflags if @pflags is non-NULL. 2518 * 2519 * Return values: 2520 * - 0 on success (address copied to buffer). 2521 * - EWOULDBLOCK (no local error, but address is still unresolved) 2522 * - other errors (alloc failure, etc) 2523 */ 2524 int 2525 nd6_resolve_addr(struct ifnet *ifp, int flags, const struct sockaddr *dst, 2526 char *desten, uint32_t *pflags) 2527 { 2528 int error; 2529 2530 flags |= LLE_ADDRONLY; 2531 error = nd6_resolve_slow(ifp, AF_INET6, flags, NULL, 2532 (const struct sockaddr_in6 *)dst, desten, pflags, NULL); 2533 return (error); 2534 } 2535 2536 int 2537 nd6_flush_holdchain(struct ifnet *ifp, struct llentry *lle, struct mbuf *chain) 2538 { 2539 struct mbuf *m, *m_head; 2540 struct sockaddr_in6 dst6; 2541 int error = 0; 2542 2543 NET_EPOCH_ASSERT(); 2544 2545 struct route_in6 ro = { 2546 .ro_prepend = lle->r_linkdata, 2547 .ro_plen = lle->r_hdrlen, 2548 }; 2549 2550 lltable_fill_sa_entry(lle, (struct sockaddr *)&dst6); 2551 m_head = chain; 2552 2553 while (m_head) { 2554 m = m_head; 2555 m_head = m_head->m_nextpkt; 2556 m->m_nextpkt = NULL; 2557 error = nd6_output_ifp(ifp, ifp, m, &dst6, (struct route *)&ro); 2558 } 2559 2560 /* 2561 * XXX 2562 * note that intermediate errors are blindly ignored 2563 */ 2564 return (error); 2565 } 2566 2567 __noinline void 2568 nd6_flush_children_holdchain(struct ifnet *ifp, struct llentry *lle) 2569 { 2570 struct llentry *child_lle; 2571 struct mbuf *chain; 2572 2573 NET_EPOCH_ASSERT(); 2574 2575 CK_SLIST_FOREACH(child_lle, &lle->lle_children, lle_child_next) { 2576 LLE_WLOCK(child_lle); 2577 chain = nd6_grab_holdchain(child_lle); 2578 LLE_WUNLOCK(child_lle); 2579 nd6_flush_holdchain(ifp, child_lle, chain); 2580 } 2581 } 2582 2583 static int 2584 nd6_need_cache(struct ifnet *ifp) 2585 { 2586 /* 2587 * XXX: we currently do not make neighbor cache on any interface 2588 * other than Ethernet and GIF. 2589 * 2590 * RFC2893 says: 2591 * - unidirectional tunnels needs no ND 2592 */ 2593 switch (ifp->if_type) { 2594 case IFT_ETHER: 2595 case IFT_IEEE1394: 2596 case IFT_L2VLAN: 2597 case IFT_INFINIBAND: 2598 case IFT_BRIDGE: 2599 case IFT_PROPVIRTUAL: 2600 return (1); 2601 default: 2602 return (0); 2603 } 2604 } 2605 2606 /* 2607 * Add pernament ND6 link-layer record for given 2608 * interface address. 2609 * 2610 * Very similar to IPv4 arp_ifinit(), but: 2611 * 1) IPv6 DAD is performed in different place 2612 * 2) It is called by IPv6 protocol stack in contrast to 2613 * arp_ifinit() which is typically called in SIOCSIFADDR 2614 * driver ioctl handler. 2615 * 2616 */ 2617 int 2618 nd6_add_ifa_lle(struct in6_ifaddr *ia) 2619 { 2620 struct ifnet *ifp; 2621 struct llentry *ln, *ln_tmp; 2622 struct sockaddr *dst; 2623 2624 ifp = ia->ia_ifa.ifa_ifp; 2625 if (nd6_need_cache(ifp) == 0) 2626 return (0); 2627 2628 dst = (struct sockaddr *)&ia->ia_addr; 2629 ln = lltable_alloc_entry(LLTABLE6(ifp), LLE_IFADDR, dst); 2630 if (ln == NULL) 2631 return (ENOBUFS); 2632 2633 LLTABLE_LOCK(LLTABLE6(ifp)); 2634 LLE_WLOCK(ln); 2635 /* Unlink any entry if exists */ 2636 ln_tmp = lla_lookup(LLTABLE6(ifp), LLE_SF(AF_INET6, LLE_EXCLUSIVE), dst); 2637 if (ln_tmp != NULL) 2638 lltable_unlink_entry(LLTABLE6(ifp), ln_tmp); 2639 lltable_link_entry(LLTABLE6(ifp), ln); 2640 LLTABLE_UNLOCK(LLTABLE6(ifp)); 2641 2642 if (ln_tmp != NULL) 2643 EVENTHANDLER_INVOKE(lle_event, ln_tmp, LLENTRY_EXPIRED); 2644 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED); 2645 2646 LLE_WUNLOCK(ln); 2647 if (ln_tmp != NULL) 2648 llentry_free(ln_tmp); 2649 2650 return (0); 2651 } 2652 2653 /* 2654 * Removes either all lle entries for given @ia, or lle 2655 * corresponding to @ia address. 2656 */ 2657 void 2658 nd6_rem_ifa_lle(struct in6_ifaddr *ia, int all) 2659 { 2660 struct sockaddr_in6 mask, addr; 2661 struct sockaddr *saddr, *smask; 2662 struct ifnet *ifp; 2663 2664 ifp = ia->ia_ifa.ifa_ifp; 2665 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 2666 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 2667 saddr = (struct sockaddr *)&addr; 2668 smask = (struct sockaddr *)&mask; 2669 2670 if (all != 0) 2671 lltable_prefix_free(AF_INET6, saddr, smask, LLE_STATIC); 2672 else 2673 lltable_delete_addr(LLTABLE6(ifp), LLE_IFADDR, saddr); 2674 } 2675 2676 static int 2677 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS) 2678 { 2679 struct in6_prefix p; 2680 struct sockaddr_in6 s6; 2681 struct nd_prefix *pr; 2682 struct nd_pfxrouter *pfr; 2683 time_t maxexpire; 2684 int error; 2685 char ip6buf[INET6_ADDRSTRLEN]; 2686 2687 if (req->newptr) 2688 return (EPERM); 2689 2690 error = sysctl_wire_old_buffer(req, 0); 2691 if (error != 0) 2692 return (error); 2693 2694 bzero(&p, sizeof(p)); 2695 p.origin = PR_ORIG_RA; 2696 bzero(&s6, sizeof(s6)); 2697 s6.sin6_family = AF_INET6; 2698 s6.sin6_len = sizeof(s6); 2699 2700 ND6_RLOCK(); 2701 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 2702 p.prefix = pr->ndpr_prefix; 2703 if (sa6_recoverscope(&p.prefix)) { 2704 log(LOG_ERR, "scope error in prefix list (%s)\n", 2705 ip6_sprintf(ip6buf, &p.prefix.sin6_addr)); 2706 /* XXX: press on... */ 2707 } 2708 p.raflags = pr->ndpr_raf; 2709 p.prefixlen = pr->ndpr_plen; 2710 p.vltime = pr->ndpr_vltime; 2711 p.pltime = pr->ndpr_pltime; 2712 p.if_index = pr->ndpr_ifp->if_index; 2713 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2714 p.expire = 0; 2715 else { 2716 /* XXX: we assume time_t is signed. */ 2717 maxexpire = (-1) & 2718 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 2719 if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate) 2720 p.expire = pr->ndpr_lastupdate + 2721 pr->ndpr_vltime + 2722 (time_second - time_uptime); 2723 else 2724 p.expire = maxexpire; 2725 } 2726 p.refcnt = pr->ndpr_addrcnt; 2727 p.flags = pr->ndpr_stateflags; 2728 p.advrtrs = 0; 2729 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) 2730 p.advrtrs++; 2731 error = SYSCTL_OUT(req, &p, sizeof(p)); 2732 if (error != 0) 2733 break; 2734 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 2735 s6.sin6_addr = pfr->router->rtaddr; 2736 if (sa6_recoverscope(&s6)) 2737 log(LOG_ERR, 2738 "scope error in prefix list (%s)\n", 2739 ip6_sprintf(ip6buf, &pfr->router->rtaddr)); 2740 error = SYSCTL_OUT(req, &s6, sizeof(s6)); 2741 if (error != 0) 2742 goto out; 2743 } 2744 } 2745 out: 2746 ND6_RUNLOCK(); 2747 return (error); 2748 } 2749 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist, 2750 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 2751 NULL, 0, nd6_sysctl_prlist, "S,in6_prefix", 2752 "NDP prefix list"); 2753 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen, 2754 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, ""); 2755 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer, 2756 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), ""); 2757