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