1 /*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_inet.h" 36 #include "opt_inet6.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.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_arc.h> 60 #include <net/if_dl.h> 61 #include <net/if_types.h> 62 #include <net/iso88025.h> 63 #include <net/fddi.h> 64 #include <net/route.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_var.h> 72 #include <netinet/ip6.h> 73 #include <netinet6/ip6_var.h> 74 #include <netinet6/scope6_var.h> 75 #include <netinet6/nd6.h> 76 #include <netinet6/in6_ifattach.h> 77 #include <netinet/icmp6.h> 78 #include <netinet6/send.h> 79 80 #include <sys/limits.h> 81 82 #include <security/mac/mac_framework.h> 83 84 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */ 85 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */ 86 87 #define SIN6(s) ((const struct sockaddr_in6 *)(s)) 88 89 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); 90 91 /* timer values */ 92 VNET_DEFINE(int, nd6_prune) = 1; /* walk list every 1 seconds */ 93 VNET_DEFINE(int, nd6_delay) = 5; /* delay first probe time 5 second */ 94 VNET_DEFINE(int, nd6_umaxtries) = 3; /* maximum unicast query */ 95 VNET_DEFINE(int, nd6_mmaxtries) = 3; /* maximum multicast query */ 96 VNET_DEFINE(int, nd6_useloopback) = 1; /* use loopback interface for 97 * local traffic */ 98 VNET_DEFINE(int, nd6_gctimer) = (60 * 60 * 24); /* 1 day: garbage 99 * collection timer */ 100 101 /* preventing too many loops in ND option parsing */ 102 static VNET_DEFINE(int, nd6_maxndopt) = 10; /* max # of ND options allowed */ 103 104 VNET_DEFINE(int, nd6_maxnudhint) = 0; /* max # of subsequent upper 105 * layer hints */ 106 static VNET_DEFINE(int, nd6_maxqueuelen) = 1; /* max pkts cached in unresolved 107 * ND entries */ 108 #define V_nd6_maxndopt VNET(nd6_maxndopt) 109 #define V_nd6_maxqueuelen VNET(nd6_maxqueuelen) 110 111 #ifdef ND6_DEBUG 112 VNET_DEFINE(int, nd6_debug) = 1; 113 #else 114 VNET_DEFINE(int, nd6_debug) = 0; 115 #endif 116 117 static eventhandler_tag lle_event_eh, iflladdr_event_eh; 118 119 VNET_DEFINE(struct nd_drhead, nd_defrouter); 120 VNET_DEFINE(struct nd_prhead, nd_prefix); 121 VNET_DEFINE(struct rwlock, nd6_lock); 122 VNET_DEFINE(uint64_t, nd6_list_genid); 123 VNET_DEFINE(struct mtx, nd6_onlink_mtx); 124 125 VNET_DEFINE(int, nd6_recalc_reachtm_interval) = ND6_RECALC_REACHTM_INTERVAL; 126 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval) 127 128 int (*send_sendso_input_hook)(struct mbuf *, struct ifnet *, int, int); 129 130 static int nd6_is_new_addr_neighbor(const struct sockaddr_in6 *, 131 struct ifnet *); 132 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *); 133 static void nd6_slowtimo(void *); 134 static int regen_tmpaddr(struct in6_ifaddr *); 135 static void nd6_free(struct llentry **, int); 136 static void nd6_free_redirect(const struct llentry *); 137 static void nd6_llinfo_timer(void *); 138 static void nd6_llinfo_settimer_locked(struct llentry *, long); 139 static void clear_llinfo_pqueue(struct llentry *); 140 static void nd6_rtrequest(int, struct rtentry *, struct rt_addrinfo *); 141 static int nd6_resolve_slow(struct ifnet *, int, struct mbuf *, 142 const struct sockaddr_in6 *, u_char *, uint32_t *, struct llentry **); 143 static int nd6_need_cache(struct ifnet *); 144 145 146 static VNET_DEFINE(struct callout, nd6_slowtimo_ch); 147 #define V_nd6_slowtimo_ch VNET(nd6_slowtimo_ch) 148 149 VNET_DEFINE(struct callout, nd6_timer_ch); 150 #define V_nd6_timer_ch VNET(nd6_timer_ch) 151 152 static void 153 nd6_lle_event(void *arg __unused, struct llentry *lle, int evt) 154 { 155 struct rt_addrinfo rtinfo; 156 struct sockaddr_in6 dst; 157 struct sockaddr_dl gw; 158 struct ifnet *ifp; 159 int type; 160 161 LLE_WLOCK_ASSERT(lle); 162 163 if (lltable_get_af(lle->lle_tbl) != AF_INET6) 164 return; 165 166 switch (evt) { 167 case LLENTRY_RESOLVED: 168 type = RTM_ADD; 169 KASSERT(lle->la_flags & LLE_VALID, 170 ("%s: %p resolved but not valid?", __func__, lle)); 171 break; 172 case LLENTRY_EXPIRED: 173 type = RTM_DELETE; 174 break; 175 default: 176 return; 177 } 178 179 ifp = lltable_get_ifp(lle->lle_tbl); 180 181 bzero(&dst, sizeof(dst)); 182 bzero(&gw, sizeof(gw)); 183 bzero(&rtinfo, sizeof(rtinfo)); 184 lltable_fill_sa_entry(lle, (struct sockaddr *)&dst); 185 dst.sin6_scope_id = in6_getscopezone(ifp, 186 in6_addrscope(&dst.sin6_addr)); 187 gw.sdl_len = sizeof(struct sockaddr_dl); 188 gw.sdl_family = AF_LINK; 189 gw.sdl_alen = ifp->if_addrlen; 190 gw.sdl_index = ifp->if_index; 191 gw.sdl_type = ifp->if_type; 192 if (evt == LLENTRY_RESOLVED) 193 bcopy(lle->ll_addr, gw.sdl_data, ifp->if_addrlen); 194 rtinfo.rti_info[RTAX_DST] = (struct sockaddr *)&dst; 195 rtinfo.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gw; 196 rtinfo.rti_addrs = RTA_DST | RTA_GATEWAY; 197 rt_missmsg_fib(type, &rtinfo, RTF_HOST | RTF_LLDATA | ( 198 type == RTM_ADD ? RTF_UP: 0), 0, RT_DEFAULT_FIB); 199 } 200 201 /* 202 * A handler for interface link layer address change event. 203 */ 204 static void 205 nd6_iflladdr(void *arg __unused, struct ifnet *ifp) 206 { 207 208 lltable_update_ifaddr(LLTABLE6(ifp)); 209 } 210 211 void 212 nd6_init(void) 213 { 214 215 mtx_init(&V_nd6_onlink_mtx, "nd6 onlink", NULL, MTX_DEF); 216 rw_init(&V_nd6_lock, "nd6 list"); 217 218 LIST_INIT(&V_nd_prefix); 219 TAILQ_INIT(&V_nd_defrouter); 220 221 /* Start timers. */ 222 callout_init(&V_nd6_slowtimo_ch, 0); 223 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 224 nd6_slowtimo, curvnet); 225 226 callout_init(&V_nd6_timer_ch, 0); 227 callout_reset(&V_nd6_timer_ch, hz, nd6_timer, curvnet); 228 229 nd6_dad_init(); 230 if (IS_DEFAULT_VNET(curvnet)) { 231 lle_event_eh = EVENTHANDLER_REGISTER(lle_event, nd6_lle_event, 232 NULL, EVENTHANDLER_PRI_ANY); 233 iflladdr_event_eh = EVENTHANDLER_REGISTER(iflladdr_event, 234 nd6_iflladdr, NULL, EVENTHANDLER_PRI_ANY); 235 } 236 } 237 238 #ifdef VIMAGE 239 void 240 nd6_destroy() 241 { 242 243 callout_drain(&V_nd6_slowtimo_ch); 244 callout_drain(&V_nd6_timer_ch); 245 if (IS_DEFAULT_VNET(curvnet)) { 246 EVENTHANDLER_DEREGISTER(lle_event, lle_event_eh); 247 EVENTHANDLER_DEREGISTER(iflladdr_event, iflladdr_event_eh); 248 } 249 rw_destroy(&V_nd6_lock); 250 mtx_destroy(&V_nd6_onlink_mtx); 251 } 252 #endif 253 254 struct nd_ifinfo * 255 nd6_ifattach(struct ifnet *ifp) 256 { 257 struct nd_ifinfo *nd; 258 259 nd = malloc(sizeof(*nd), M_IP6NDP, M_WAITOK | M_ZERO); 260 nd->initialized = 1; 261 262 nd->chlim = IPV6_DEFHLIM; 263 nd->basereachable = REACHABLE_TIME; 264 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable); 265 nd->retrans = RETRANS_TIMER; 266 267 nd->flags = ND6_IFF_PERFORMNUD; 268 269 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL. 270 * XXXHRS: Clear ND6_IFF_AUTO_LINKLOCAL on an IFT_BRIDGE interface by 271 * default regardless of the V_ip6_auto_linklocal configuration to 272 * give a reasonable default behavior. 273 */ 274 if ((V_ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) || 275 (ifp->if_flags & IFF_LOOPBACK)) 276 nd->flags |= ND6_IFF_AUTO_LINKLOCAL; 277 /* 278 * A loopback interface does not need to accept RTADV. 279 * XXXHRS: Clear ND6_IFF_ACCEPT_RTADV on an IFT_BRIDGE interface by 280 * default regardless of the V_ip6_accept_rtadv configuration to 281 * prevent the interface from accepting RA messages arrived 282 * on one of the member interfaces with ND6_IFF_ACCEPT_RTADV. 283 */ 284 if (V_ip6_accept_rtadv && 285 !(ifp->if_flags & IFF_LOOPBACK) && 286 (ifp->if_type != IFT_BRIDGE)) 287 nd->flags |= ND6_IFF_ACCEPT_RTADV; 288 if (V_ip6_no_radr && !(ifp->if_flags & IFF_LOOPBACK)) 289 nd->flags |= ND6_IFF_NO_RADR; 290 291 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */ 292 nd6_setmtu0(ifp, nd); 293 294 return nd; 295 } 296 297 void 298 nd6_ifdetach(struct ifnet *ifp, struct nd_ifinfo *nd) 299 { 300 struct ifaddr *ifa, *next; 301 302 IF_ADDR_RLOCK(ifp); 303 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, next) { 304 if (ifa->ifa_addr->sa_family != AF_INET6) 305 continue; 306 307 /* stop DAD processing */ 308 nd6_dad_stop(ifa); 309 } 310 IF_ADDR_RUNLOCK(ifp); 311 312 free(nd, M_IP6NDP); 313 } 314 315 /* 316 * Reset ND level link MTU. This function is called when the physical MTU 317 * changes, which means we might have to adjust the ND level MTU. 318 */ 319 void 320 nd6_setmtu(struct ifnet *ifp) 321 { 322 if (ifp->if_afdata[AF_INET6] == NULL) 323 return; 324 325 nd6_setmtu0(ifp, ND_IFINFO(ifp)); 326 } 327 328 /* XXX todo: do not maintain copy of ifp->if_mtu in ndi->maxmtu */ 329 void 330 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi) 331 { 332 u_int32_t omaxmtu; 333 334 omaxmtu = ndi->maxmtu; 335 336 switch (ifp->if_type) { 337 case IFT_ARCNET: 338 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */ 339 break; 340 case IFT_FDDI: 341 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); /* RFC2467 */ 342 break; 343 case IFT_ISO88025: 344 ndi->maxmtu = MIN(ISO88025_MAX_MTU, ifp->if_mtu); 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 bool onlink_locked; 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 onlink_locked = false; 1026 restart: 1027 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) { 1028 /* 1029 * Expire prefixes. Since the pltime is only used for 1030 * autoconfigured addresses, pltime processing for prefixes is 1031 * not necessary. 1032 * 1033 * Only unlink after all derived addresses have expired. This 1034 * may not occur until two hours after the prefix has expired 1035 * per RFC 4862. If the prefix expires before its derived 1036 * addresses, mark it off-link. This will be done automatically 1037 * after unlinking if no address references remain. 1038 */ 1039 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME || 1040 time_uptime - pr->ndpr_lastupdate <= pr->ndpr_vltime) 1041 continue; 1042 1043 if (pr->ndpr_addrcnt == 0) { 1044 nd6_prefix_unlink(pr, &prl); 1045 continue; 1046 } 1047 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1048 if (!onlink_locked) { 1049 onlink_locked = ND6_ONLINK_TRYLOCK(); 1050 if (!onlink_locked) { 1051 ND6_WUNLOCK(); 1052 ND6_ONLINK_LOCK(); 1053 onlink_locked = true; 1054 ND6_WLOCK(); 1055 goto restart; 1056 } 1057 } 1058 (void)nd6_prefix_offlink(pr); 1059 } 1060 } 1061 ND6_WUNLOCK(); 1062 if (onlink_locked) 1063 ND6_ONLINK_UNLOCK(); 1064 1065 while ((pr = LIST_FIRST(&prl)) != NULL) { 1066 LIST_REMOVE(pr, ndpr_entry); 1067 nd6_prefix_del(pr); 1068 } 1069 1070 callout_reset(&V_nd6_timer_ch, V_nd6_prune * hz, 1071 nd6_timer, curvnet); 1072 1073 CURVNET_RESTORE(); 1074 } 1075 1076 /* 1077 * ia6 - deprecated/invalidated temporary address 1078 */ 1079 static int 1080 regen_tmpaddr(struct in6_ifaddr *ia6) 1081 { 1082 struct ifaddr *ifa; 1083 struct ifnet *ifp; 1084 struct in6_ifaddr *public_ifa6 = NULL; 1085 1086 ifp = ia6->ia_ifa.ifa_ifp; 1087 IF_ADDR_RLOCK(ifp); 1088 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1089 struct in6_ifaddr *it6; 1090 1091 if (ifa->ifa_addr->sa_family != AF_INET6) 1092 continue; 1093 1094 it6 = (struct in6_ifaddr *)ifa; 1095 1096 /* ignore no autoconf addresses. */ 1097 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1098 continue; 1099 1100 /* ignore autoconf addresses with different prefixes. */ 1101 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) 1102 continue; 1103 1104 /* 1105 * Now we are looking at an autoconf address with the same 1106 * prefix as ours. If the address is temporary and is still 1107 * preferred, do not create another one. It would be rare, but 1108 * could happen, for example, when we resume a laptop PC after 1109 * a long period. 1110 */ 1111 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 1112 !IFA6_IS_DEPRECATED(it6)) { 1113 public_ifa6 = NULL; 1114 break; 1115 } 1116 1117 /* 1118 * This is a public autoconf address that has the same prefix 1119 * as ours. If it is preferred, keep it. We can't break the 1120 * loop here, because there may be a still-preferred temporary 1121 * address with the prefix. 1122 */ 1123 if (!IFA6_IS_DEPRECATED(it6)) 1124 public_ifa6 = it6; 1125 } 1126 if (public_ifa6 != NULL) 1127 ifa_ref(&public_ifa6->ia_ifa); 1128 IF_ADDR_RUNLOCK(ifp); 1129 1130 if (public_ifa6 != NULL) { 1131 int e; 1132 1133 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) { 1134 ifa_free(&public_ifa6->ia_ifa); 1135 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" 1136 " tmp addr,errno=%d\n", e); 1137 return (-1); 1138 } 1139 ifa_free(&public_ifa6->ia_ifa); 1140 return (0); 1141 } 1142 1143 return (-1); 1144 } 1145 1146 /* 1147 * Remove prefix and default router list entries corresponding to ifp. Neighbor 1148 * cache entries are freed in in6_domifdetach(). 1149 */ 1150 void 1151 nd6_purge(struct ifnet *ifp) 1152 { 1153 struct nd_drhead drq; 1154 struct nd_prhead prl; 1155 struct nd_defrouter *dr, *ndr; 1156 struct nd_prefix *pr, *npr; 1157 1158 TAILQ_INIT(&drq); 1159 LIST_INIT(&prl); 1160 1161 /* 1162 * Nuke default router list entries toward ifp. 1163 * We defer removal of default router list entries that is installed 1164 * in the routing table, in order to keep additional side effects as 1165 * small as possible. 1166 */ 1167 ND6_WLOCK(); 1168 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) { 1169 if (dr->installed) 1170 continue; 1171 if (dr->ifp == ifp) 1172 defrouter_unlink(dr, &drq); 1173 } 1174 TAILQ_FOREACH_SAFE(dr, &V_nd_defrouter, dr_entry, ndr) { 1175 if (!dr->installed) 1176 continue; 1177 if (dr->ifp == ifp) 1178 defrouter_unlink(dr, &drq); 1179 } 1180 1181 /* 1182 * Remove prefixes on ifp. We should have already removed addresses on 1183 * this interface, so no addresses should be referencing these prefixes. 1184 */ 1185 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, npr) { 1186 if (pr->ndpr_ifp == ifp) 1187 nd6_prefix_unlink(pr, &prl); 1188 } 1189 ND6_WUNLOCK(); 1190 1191 /* Delete the unlinked router and prefix objects. */ 1192 while ((dr = TAILQ_FIRST(&drq)) != NULL) { 1193 TAILQ_REMOVE(&drq, dr, dr_entry); 1194 defrouter_del(dr); 1195 } 1196 while ((pr = LIST_FIRST(&prl)) != NULL) { 1197 LIST_REMOVE(pr, ndpr_entry); 1198 nd6_prefix_del(pr); 1199 } 1200 1201 /* cancel default outgoing interface setting */ 1202 if (V_nd6_defifindex == ifp->if_index) 1203 nd6_setdefaultiface(0); 1204 1205 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) { 1206 /* Refresh default router list. */ 1207 defrouter_select(); 1208 } 1209 } 1210 1211 /* 1212 * the caller acquires and releases the lock on the lltbls 1213 * Returns the llentry locked 1214 */ 1215 struct llentry * 1216 nd6_lookup(const struct in6_addr *addr6, int flags, struct ifnet *ifp) 1217 { 1218 struct sockaddr_in6 sin6; 1219 struct llentry *ln; 1220 1221 bzero(&sin6, sizeof(sin6)); 1222 sin6.sin6_len = sizeof(struct sockaddr_in6); 1223 sin6.sin6_family = AF_INET6; 1224 sin6.sin6_addr = *addr6; 1225 1226 IF_AFDATA_LOCK_ASSERT(ifp); 1227 1228 ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)&sin6); 1229 1230 return (ln); 1231 } 1232 1233 struct llentry * 1234 nd6_alloc(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 ln = lltable_alloc_entry(LLTABLE6(ifp), 0, (struct sockaddr *)&sin6); 1245 if (ln != NULL) 1246 ln->ln_state = ND6_LLINFO_NOSTATE; 1247 1248 return (ln); 1249 } 1250 1251 /* 1252 * Test whether a given IPv6 address is a neighbor or not, ignoring 1253 * the actual neighbor cache. The neighbor cache is ignored in order 1254 * to not reenter the routing code from within itself. 1255 */ 1256 static int 1257 nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 1258 { 1259 struct nd_prefix *pr; 1260 struct ifaddr *dstaddr; 1261 struct rt_addrinfo info; 1262 struct sockaddr_in6 rt_key; 1263 const struct sockaddr *dst6; 1264 uint64_t genid; 1265 int error, fibnum; 1266 1267 /* 1268 * A link-local address is always a neighbor. 1269 * XXX: a link does not necessarily specify a single interface. 1270 */ 1271 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { 1272 struct sockaddr_in6 sin6_copy; 1273 u_int32_t zone; 1274 1275 /* 1276 * We need sin6_copy since sa6_recoverscope() may modify the 1277 * content (XXX). 1278 */ 1279 sin6_copy = *addr; 1280 if (sa6_recoverscope(&sin6_copy)) 1281 return (0); /* XXX: should be impossible */ 1282 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) 1283 return (0); 1284 if (sin6_copy.sin6_scope_id == zone) 1285 return (1); 1286 else 1287 return (0); 1288 } 1289 1290 bzero(&rt_key, sizeof(rt_key)); 1291 bzero(&info, sizeof(info)); 1292 info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key; 1293 1294 /* Always use the default FIB here. XXME - why? */ 1295 fibnum = RT_DEFAULT_FIB; 1296 1297 /* 1298 * If the address matches one of our addresses, 1299 * it should be a neighbor. 1300 * If the address matches one of our on-link prefixes, it should be a 1301 * neighbor. 1302 */ 1303 ND6_RLOCK(); 1304 restart: 1305 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1306 if (pr->ndpr_ifp != ifp) 1307 continue; 1308 1309 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1310 /* Always use the default FIB here. */ 1311 dst6 = (const struct sockaddr *)&pr->ndpr_prefix; 1312 1313 genid = V_nd6_list_genid; 1314 ND6_RUNLOCK(); 1315 1316 /* Restore length field before retrying lookup */ 1317 rt_key.sin6_len = sizeof(rt_key); 1318 error = rib_lookup_info(fibnum, dst6, 0, 0, &info); 1319 1320 ND6_RLOCK(); 1321 if (genid != V_nd6_list_genid) 1322 goto restart; 1323 if (error != 0) 1324 continue; 1325 1326 /* 1327 * This is the case where multiple interfaces 1328 * have the same prefix, but only one is installed 1329 * into the routing table and that prefix entry 1330 * is not the one being examined here. In the case 1331 * where RADIX_MPATH is enabled, multiple route 1332 * entries (of the same rt_key value) will be 1333 * installed because the interface addresses all 1334 * differ. 1335 */ 1336 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1337 &rt_key.sin6_addr)) 1338 continue; 1339 } 1340 1341 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1342 &addr->sin6_addr, &pr->ndpr_mask)) { 1343 ND6_RUNLOCK(); 1344 return (1); 1345 } 1346 } 1347 ND6_RUNLOCK(); 1348 1349 /* 1350 * If the address is assigned on the node of the other side of 1351 * a p2p interface, the address should be a neighbor. 1352 */ 1353 dstaddr = ifa_ifwithdstaddr((const struct sockaddr *)addr, RT_ALL_FIBS); 1354 if (dstaddr != NULL) { 1355 if (dstaddr->ifa_ifp == ifp) { 1356 ifa_free(dstaddr); 1357 return (1); 1358 } 1359 ifa_free(dstaddr); 1360 } 1361 1362 /* 1363 * If the default router list is empty, all addresses are regarded 1364 * as on-link, and thus, as a neighbor. 1365 */ 1366 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && 1367 TAILQ_EMPTY(&V_nd_defrouter) && 1368 V_nd6_defifindex == ifp->if_index) { 1369 return (1); 1370 } 1371 1372 return (0); 1373 } 1374 1375 1376 /* 1377 * Detect if a given IPv6 address identifies a neighbor on a given link. 1378 * XXX: should take care of the destination of a p2p link? 1379 */ 1380 int 1381 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 1382 { 1383 struct llentry *lle; 1384 int rc = 0; 1385 1386 IF_AFDATA_UNLOCK_ASSERT(ifp); 1387 if (nd6_is_new_addr_neighbor(addr, ifp)) 1388 return (1); 1389 1390 /* 1391 * Even if the address matches none of our addresses, it might be 1392 * in the neighbor cache. 1393 */ 1394 IF_AFDATA_RLOCK(ifp); 1395 if ((lle = nd6_lookup(&addr->sin6_addr, 0, ifp)) != NULL) { 1396 LLE_RUNLOCK(lle); 1397 rc = 1; 1398 } 1399 IF_AFDATA_RUNLOCK(ifp); 1400 return (rc); 1401 } 1402 1403 /* 1404 * Free an nd6 llinfo entry. 1405 * Since the function would cause significant changes in the kernel, DO NOT 1406 * make it global, unless you have a strong reason for the change, and are sure 1407 * that the change is safe. 1408 * 1409 * Set noinline to be dtrace-friendly 1410 */ 1411 static __noinline void 1412 nd6_free(struct llentry **lnp, int gc) 1413 { 1414 struct ifnet *ifp; 1415 struct llentry *ln; 1416 struct nd_defrouter *dr; 1417 1418 ln = *lnp; 1419 *lnp = NULL; 1420 1421 LLE_WLOCK_ASSERT(ln); 1422 ND6_RLOCK_ASSERT(); 1423 1424 ifp = lltable_get_ifp(ln->lle_tbl); 1425 if ((ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) != 0) 1426 dr = defrouter_lookup_locked(&ln->r_l3addr.addr6, ifp); 1427 else 1428 dr = NULL; 1429 ND6_RUNLOCK(); 1430 1431 if ((ln->la_flags & LLE_DELETED) == 0) 1432 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_EXPIRED); 1433 1434 /* 1435 * we used to have pfctlinput(PRC_HOSTDEAD) here. 1436 * even though it is not harmful, it was not really necessary. 1437 */ 1438 1439 /* cancel timer */ 1440 nd6_llinfo_settimer_locked(ln, -1); 1441 1442 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) { 1443 if (dr != NULL && dr->expire && 1444 ln->ln_state == ND6_LLINFO_STALE && gc) { 1445 /* 1446 * If the reason for the deletion is just garbage 1447 * collection, and the neighbor is an active default 1448 * router, do not delete it. Instead, reset the GC 1449 * timer using the router's lifetime. 1450 * Simply deleting the entry would affect default 1451 * router selection, which is not necessarily a good 1452 * thing, especially when we're using router preference 1453 * values. 1454 * XXX: the check for ln_state would be redundant, 1455 * but we intentionally keep it just in case. 1456 */ 1457 if (dr->expire > time_uptime) 1458 nd6_llinfo_settimer_locked(ln, 1459 (dr->expire - time_uptime) * hz); 1460 else 1461 nd6_llinfo_settimer_locked(ln, 1462 (long)V_nd6_gctimer * hz); 1463 1464 LLE_REMREF(ln); 1465 LLE_WUNLOCK(ln); 1466 defrouter_rele(dr); 1467 return; 1468 } 1469 1470 if (dr) { 1471 /* 1472 * Unreachablity of a router might affect the default 1473 * router selection and on-link detection of advertised 1474 * prefixes. 1475 */ 1476 1477 /* 1478 * Temporarily fake the state to choose a new default 1479 * router and to perform on-link determination of 1480 * prefixes correctly. 1481 * Below the state will be set correctly, 1482 * or the entry itself will be deleted. 1483 */ 1484 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1485 } 1486 1487 if (ln->ln_router || dr) { 1488 1489 /* 1490 * We need to unlock to avoid a LOR with rt6_flush() with the 1491 * rnh and for the calls to pfxlist_onlink_check() and 1492 * defrouter_select() in the block further down for calls 1493 * into nd6_lookup(). We still hold a ref. 1494 */ 1495 LLE_WUNLOCK(ln); 1496 1497 /* 1498 * rt6_flush must be called whether or not the neighbor 1499 * is in the Default Router List. 1500 * See a corresponding comment in nd6_na_input(). 1501 */ 1502 rt6_flush(&ln->r_l3addr.addr6, ifp); 1503 } 1504 1505 if (dr) { 1506 /* 1507 * Since defrouter_select() does not affect the 1508 * on-link determination and MIP6 needs the check 1509 * before the default router selection, we perform 1510 * the check now. 1511 */ 1512 pfxlist_onlink_check(); 1513 1514 /* 1515 * Refresh default router list. 1516 */ 1517 defrouter_select(); 1518 } 1519 1520 /* 1521 * If this entry was added by an on-link redirect, remove the 1522 * corresponding host route. 1523 */ 1524 if (ln->la_flags & LLE_REDIRECT) 1525 nd6_free_redirect(ln); 1526 1527 if (ln->ln_router || dr) 1528 LLE_WLOCK(ln); 1529 } 1530 1531 /* 1532 * Save to unlock. We still hold an extra reference and will not 1533 * free(9) in llentry_free() if someone else holds one as well. 1534 */ 1535 LLE_WUNLOCK(ln); 1536 IF_AFDATA_LOCK(ifp); 1537 LLE_WLOCK(ln); 1538 /* Guard against race with other llentry_free(). */ 1539 if (ln->la_flags & LLE_LINKED) { 1540 /* Remove callout reference */ 1541 LLE_REMREF(ln); 1542 lltable_unlink_entry(ln->lle_tbl, ln); 1543 } 1544 IF_AFDATA_UNLOCK(ifp); 1545 1546 llentry_free(ln); 1547 if (dr != NULL) 1548 defrouter_rele(dr); 1549 } 1550 1551 static int 1552 nd6_isdynrte(const struct rtentry *rt, void *xap) 1553 { 1554 1555 if (rt->rt_flags == (RTF_UP | RTF_HOST | RTF_DYNAMIC)) 1556 return (1); 1557 1558 return (0); 1559 } 1560 /* 1561 * Remove the rtentry for the given llentry, 1562 * both of which were installed by a redirect. 1563 */ 1564 static void 1565 nd6_free_redirect(const struct llentry *ln) 1566 { 1567 int fibnum; 1568 struct sockaddr_in6 sin6; 1569 struct rt_addrinfo info; 1570 1571 lltable_fill_sa_entry(ln, (struct sockaddr *)&sin6); 1572 memset(&info, 0, sizeof(info)); 1573 info.rti_info[RTAX_DST] = (struct sockaddr *)&sin6; 1574 info.rti_filter = nd6_isdynrte; 1575 1576 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) 1577 rtrequest1_fib(RTM_DELETE, &info, NULL, fibnum); 1578 } 1579 1580 /* 1581 * Rejuvenate this function for routing operations related 1582 * processing. 1583 */ 1584 void 1585 nd6_rtrequest(int req, struct rtentry *rt, struct rt_addrinfo *info) 1586 { 1587 struct sockaddr_in6 *gateway; 1588 struct nd_defrouter *dr; 1589 struct ifnet *ifp; 1590 1591 gateway = (struct sockaddr_in6 *)rt->rt_gateway; 1592 ifp = rt->rt_ifp; 1593 1594 switch (req) { 1595 case RTM_ADD: 1596 break; 1597 1598 case RTM_DELETE: 1599 if (!ifp) 1600 return; 1601 /* 1602 * Only indirect routes are interesting. 1603 */ 1604 if ((rt->rt_flags & RTF_GATEWAY) == 0) 1605 return; 1606 /* 1607 * check for default route 1608 */ 1609 if (IN6_ARE_ADDR_EQUAL(&in6addr_any, 1610 &SIN6(rt_key(rt))->sin6_addr)) { 1611 dr = defrouter_lookup(&gateway->sin6_addr, ifp); 1612 if (dr != NULL) { 1613 dr->installed = 0; 1614 defrouter_rele(dr); 1615 } 1616 } 1617 break; 1618 } 1619 } 1620 1621 1622 int 1623 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp) 1624 { 1625 struct in6_ndireq *ndi = (struct in6_ndireq *)data; 1626 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; 1627 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; 1628 int error = 0; 1629 1630 if (ifp->if_afdata[AF_INET6] == NULL) 1631 return (EPFNOSUPPORT); 1632 switch (cmd) { 1633 case OSIOCGIFINFO_IN6: 1634 #define ND ndi->ndi 1635 /* XXX: old ndp(8) assumes a positive value for linkmtu. */ 1636 bzero(&ND, sizeof(ND)); 1637 ND.linkmtu = IN6_LINKMTU(ifp); 1638 ND.maxmtu = ND_IFINFO(ifp)->maxmtu; 1639 ND.basereachable = ND_IFINFO(ifp)->basereachable; 1640 ND.reachable = ND_IFINFO(ifp)->reachable; 1641 ND.retrans = ND_IFINFO(ifp)->retrans; 1642 ND.flags = ND_IFINFO(ifp)->flags; 1643 ND.recalctm = ND_IFINFO(ifp)->recalctm; 1644 ND.chlim = ND_IFINFO(ifp)->chlim; 1645 break; 1646 case SIOCGIFINFO_IN6: 1647 ND = *ND_IFINFO(ifp); 1648 break; 1649 case SIOCSIFINFO_IN6: 1650 /* 1651 * used to change host variables from userland. 1652 * intended for a use on router to reflect RA configurations. 1653 */ 1654 /* 0 means 'unspecified' */ 1655 if (ND.linkmtu != 0) { 1656 if (ND.linkmtu < IPV6_MMTU || 1657 ND.linkmtu > IN6_LINKMTU(ifp)) { 1658 error = EINVAL; 1659 break; 1660 } 1661 ND_IFINFO(ifp)->linkmtu = ND.linkmtu; 1662 } 1663 1664 if (ND.basereachable != 0) { 1665 int obasereachable = ND_IFINFO(ifp)->basereachable; 1666 1667 ND_IFINFO(ifp)->basereachable = ND.basereachable; 1668 if (ND.basereachable != obasereachable) 1669 ND_IFINFO(ifp)->reachable = 1670 ND_COMPUTE_RTIME(ND.basereachable); 1671 } 1672 if (ND.retrans != 0) 1673 ND_IFINFO(ifp)->retrans = ND.retrans; 1674 if (ND.chlim != 0) 1675 ND_IFINFO(ifp)->chlim = ND.chlim; 1676 /* FALLTHROUGH */ 1677 case SIOCSIFINFO_FLAGS: 1678 { 1679 struct ifaddr *ifa; 1680 struct in6_ifaddr *ia; 1681 1682 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1683 !(ND.flags & ND6_IFF_IFDISABLED)) { 1684 /* ifdisabled 1->0 transision */ 1685 1686 /* 1687 * If the interface is marked as ND6_IFF_IFDISABLED and 1688 * has an link-local address with IN6_IFF_DUPLICATED, 1689 * do not clear ND6_IFF_IFDISABLED. 1690 * See RFC 4862, Section 5.4.5. 1691 */ 1692 IF_ADDR_RLOCK(ifp); 1693 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1694 if (ifa->ifa_addr->sa_family != AF_INET6) 1695 continue; 1696 ia = (struct in6_ifaddr *)ifa; 1697 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) && 1698 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) 1699 break; 1700 } 1701 IF_ADDR_RUNLOCK(ifp); 1702 1703 if (ifa != NULL) { 1704 /* LLA is duplicated. */ 1705 ND.flags |= ND6_IFF_IFDISABLED; 1706 log(LOG_ERR, "Cannot enable an interface" 1707 " with a link-local address marked" 1708 " duplicate.\n"); 1709 } else { 1710 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED; 1711 if (ifp->if_flags & IFF_UP) 1712 in6_if_up(ifp); 1713 } 1714 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1715 (ND.flags & ND6_IFF_IFDISABLED)) { 1716 /* ifdisabled 0->1 transision */ 1717 /* Mark all IPv6 address as tentative. */ 1718 1719 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 1720 if (V_ip6_dad_count > 0 && 1721 (ND_IFINFO(ifp)->flags & ND6_IFF_NO_DAD) == 0) { 1722 IF_ADDR_RLOCK(ifp); 1723 TAILQ_FOREACH(ifa, &ifp->if_addrhead, 1724 ifa_link) { 1725 if (ifa->ifa_addr->sa_family != 1726 AF_INET6) 1727 continue; 1728 ia = (struct in6_ifaddr *)ifa; 1729 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1730 } 1731 IF_ADDR_RUNLOCK(ifp); 1732 } 1733 } 1734 1735 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) { 1736 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) { 1737 /* auto_linklocal 0->1 transision */ 1738 1739 /* If no link-local address on ifp, configure */ 1740 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL; 1741 in6_ifattach(ifp, NULL); 1742 } else if (!(ND.flags & ND6_IFF_IFDISABLED) && 1743 ifp->if_flags & IFF_UP) { 1744 /* 1745 * When the IF already has 1746 * ND6_IFF_AUTO_LINKLOCAL, no link-local 1747 * address is assigned, and IFF_UP, try to 1748 * assign one. 1749 */ 1750 IF_ADDR_RLOCK(ifp); 1751 TAILQ_FOREACH(ifa, &ifp->if_addrhead, 1752 ifa_link) { 1753 if (ifa->ifa_addr->sa_family != 1754 AF_INET6) 1755 continue; 1756 ia = (struct in6_ifaddr *)ifa; 1757 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) 1758 break; 1759 } 1760 IF_ADDR_RUNLOCK(ifp); 1761 if (ifa != NULL) 1762 /* No LLA is configured. */ 1763 in6_ifattach(ifp, NULL); 1764 } 1765 } 1766 } 1767 ND_IFINFO(ifp)->flags = ND.flags; 1768 break; 1769 #undef ND 1770 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ 1771 /* sync kernel routing table with the default router list */ 1772 defrouter_reset(); 1773 defrouter_select(); 1774 break; 1775 case SIOCSPFXFLUSH_IN6: 1776 { 1777 /* flush all the prefix advertised by routers */ 1778 struct in6_ifaddr *ia, *ia_next; 1779 struct nd_prefix *pr, *next; 1780 struct nd_prhead prl; 1781 1782 LIST_INIT(&prl); 1783 1784 ND6_WLOCK(); 1785 LIST_FOREACH_SAFE(pr, &V_nd_prefix, ndpr_entry, next) { 1786 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1787 continue; /* XXX */ 1788 nd6_prefix_unlink(pr, &prl); 1789 } 1790 ND6_WUNLOCK(); 1791 1792 while ((pr = LIST_FIRST(&prl)) != NULL) { 1793 LIST_REMOVE(pr, ndpr_entry); 1794 /* XXXRW: in6_ifaddrhead locking. */ 1795 TAILQ_FOREACH_SAFE(ia, &V_in6_ifaddrhead, ia_link, 1796 ia_next) { 1797 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1798 continue; 1799 1800 if (ia->ia6_ndpr == pr) 1801 in6_purgeaddr(&ia->ia_ifa); 1802 } 1803 nd6_prefix_del(pr); 1804 } 1805 break; 1806 } 1807 case SIOCSRTRFLUSH_IN6: 1808 { 1809 /* flush all the default routers */ 1810 struct nd_drhead drq; 1811 struct nd_defrouter *dr; 1812 1813 TAILQ_INIT(&drq); 1814 1815 defrouter_reset(); 1816 1817 ND6_WLOCK(); 1818 while ((dr = TAILQ_FIRST(&V_nd_defrouter)) != NULL) 1819 defrouter_unlink(dr, &drq); 1820 ND6_WUNLOCK(); 1821 while ((dr = TAILQ_FIRST(&drq)) != NULL) { 1822 TAILQ_REMOVE(&drq, dr, dr_entry); 1823 defrouter_del(dr); 1824 } 1825 1826 defrouter_select(); 1827 break; 1828 } 1829 case SIOCGNBRINFO_IN6: 1830 { 1831 struct llentry *ln; 1832 struct in6_addr nb_addr = nbi->addr; /* make local for safety */ 1833 1834 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0) 1835 return (error); 1836 1837 IF_AFDATA_RLOCK(ifp); 1838 ln = nd6_lookup(&nb_addr, 0, ifp); 1839 IF_AFDATA_RUNLOCK(ifp); 1840 1841 if (ln == NULL) { 1842 error = EINVAL; 1843 break; 1844 } 1845 nbi->state = ln->ln_state; 1846 nbi->asked = ln->la_asked; 1847 nbi->isrouter = ln->ln_router; 1848 if (ln->la_expire == 0) 1849 nbi->expire = 0; 1850 else 1851 nbi->expire = ln->la_expire + ln->lle_remtime / hz + 1852 (time_second - time_uptime); 1853 LLE_RUNLOCK(ln); 1854 break; 1855 } 1856 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1857 ndif->ifindex = V_nd6_defifindex; 1858 break; 1859 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1860 return (nd6_setdefaultiface(ndif->ifindex)); 1861 } 1862 return (error); 1863 } 1864 1865 /* 1866 * Calculates new isRouter value based on provided parameters and 1867 * returns it. 1868 */ 1869 static int 1870 nd6_is_router(int type, int code, int is_new, int old_addr, int new_addr, 1871 int ln_router) 1872 { 1873 1874 /* 1875 * ICMP6 type dependent behavior. 1876 * 1877 * NS: clear IsRouter if new entry 1878 * RS: clear IsRouter 1879 * RA: set IsRouter if there's lladdr 1880 * redir: clear IsRouter if new entry 1881 * 1882 * RA case, (1): 1883 * The spec says that we must set IsRouter in the following cases: 1884 * - If lladdr exist, set IsRouter. This means (1-5). 1885 * - If it is old entry (!newentry), set IsRouter. This means (7). 1886 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 1887 * A quetion arises for (1) case. (1) case has no lladdr in the 1888 * neighbor cache, this is similar to (6). 1889 * This case is rare but we figured that we MUST NOT set IsRouter. 1890 * 1891 * is_new old_addr new_addr NS RS RA redir 1892 * D R 1893 * 0 n n (1) c ? s 1894 * 0 y n (2) c s s 1895 * 0 n y (3) c s s 1896 * 0 y y (4) c s s 1897 * 0 y y (5) c s s 1898 * 1 -- n (6) c c c s 1899 * 1 -- y (7) c c s c s 1900 * 1901 * (c=clear s=set) 1902 */ 1903 switch (type & 0xff) { 1904 case ND_NEIGHBOR_SOLICIT: 1905 /* 1906 * New entry must have is_router flag cleared. 1907 */ 1908 if (is_new) /* (6-7) */ 1909 ln_router = 0; 1910 break; 1911 case ND_REDIRECT: 1912 /* 1913 * If the icmp is a redirect to a better router, always set the 1914 * is_router flag. Otherwise, if the entry is newly created, 1915 * clear the flag. [RFC 2461, sec 8.3] 1916 */ 1917 if (code == ND_REDIRECT_ROUTER) 1918 ln_router = 1; 1919 else { 1920 if (is_new) /* (6-7) */ 1921 ln_router = 0; 1922 } 1923 break; 1924 case ND_ROUTER_SOLICIT: 1925 /* 1926 * is_router flag must always be cleared. 1927 */ 1928 ln_router = 0; 1929 break; 1930 case ND_ROUTER_ADVERT: 1931 /* 1932 * Mark an entry with lladdr as a router. 1933 */ 1934 if ((!is_new && (old_addr || new_addr)) || /* (2-5) */ 1935 (is_new && new_addr)) { /* (7) */ 1936 ln_router = 1; 1937 } 1938 break; 1939 } 1940 1941 return (ln_router); 1942 } 1943 1944 /* 1945 * Create neighbor cache entry and cache link-layer address, 1946 * on reception of inbound ND6 packets. (RS/RA/NS/redirect) 1947 * 1948 * type - ICMP6 type 1949 * code - type dependent information 1950 * 1951 */ 1952 void 1953 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr, 1954 int lladdrlen, int type, int code) 1955 { 1956 struct llentry *ln = NULL, *ln_tmp; 1957 int is_newentry; 1958 int do_update; 1959 int olladdr; 1960 int llchange; 1961 int flags; 1962 uint16_t router = 0; 1963 struct sockaddr_in6 sin6; 1964 struct mbuf *chain = NULL; 1965 u_char linkhdr[LLE_MAX_LINKHDR]; 1966 size_t linkhdrsize; 1967 int lladdr_off; 1968 1969 IF_AFDATA_UNLOCK_ASSERT(ifp); 1970 1971 KASSERT(ifp != NULL, ("%s: ifp == NULL", __func__)); 1972 KASSERT(from != NULL, ("%s: from == NULL", __func__)); 1973 1974 /* nothing must be updated for unspecified address */ 1975 if (IN6_IS_ADDR_UNSPECIFIED(from)) 1976 return; 1977 1978 /* 1979 * Validation about ifp->if_addrlen and lladdrlen must be done in 1980 * the caller. 1981 * 1982 * XXX If the link does not have link-layer adderss, what should 1983 * we do? (ifp->if_addrlen == 0) 1984 * Spec says nothing in sections for RA, RS and NA. There's small 1985 * description on it in NS section (RFC 2461 7.2.3). 1986 */ 1987 flags = lladdr ? LLE_EXCLUSIVE : 0; 1988 IF_AFDATA_RLOCK(ifp); 1989 ln = nd6_lookup(from, flags, ifp); 1990 IF_AFDATA_RUNLOCK(ifp); 1991 is_newentry = 0; 1992 if (ln == NULL) { 1993 flags |= LLE_EXCLUSIVE; 1994 ln = nd6_alloc(from, 0, ifp); 1995 if (ln == NULL) 1996 return; 1997 1998 /* 1999 * Since we already know all the data for the new entry, 2000 * fill it before insertion. 2001 */ 2002 if (lladdr != NULL) { 2003 linkhdrsize = sizeof(linkhdr); 2004 if (lltable_calc_llheader(ifp, AF_INET6, lladdr, 2005 linkhdr, &linkhdrsize, &lladdr_off) != 0) 2006 return; 2007 lltable_set_entry_addr(ifp, ln, linkhdr, linkhdrsize, 2008 lladdr_off); 2009 } 2010 2011 IF_AFDATA_WLOCK(ifp); 2012 LLE_WLOCK(ln); 2013 /* Prefer any existing lle over newly-created one */ 2014 ln_tmp = nd6_lookup(from, LLE_EXCLUSIVE, ifp); 2015 if (ln_tmp == NULL) 2016 lltable_link_entry(LLTABLE6(ifp), ln); 2017 IF_AFDATA_WUNLOCK(ifp); 2018 if (ln_tmp == NULL) { 2019 /* No existing lle, mark as new entry (6,7) */ 2020 is_newentry = 1; 2021 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); 2022 if (lladdr != NULL) /* (7) */ 2023 EVENTHANDLER_INVOKE(lle_event, ln, 2024 LLENTRY_RESOLVED); 2025 } else { 2026 lltable_free_entry(LLTABLE6(ifp), ln); 2027 ln = ln_tmp; 2028 ln_tmp = NULL; 2029 } 2030 } 2031 /* do nothing if static ndp is set */ 2032 if ((ln->la_flags & LLE_STATIC)) { 2033 if (flags & LLE_EXCLUSIVE) 2034 LLE_WUNLOCK(ln); 2035 else 2036 LLE_RUNLOCK(ln); 2037 return; 2038 } 2039 2040 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0; 2041 if (olladdr && lladdr) { 2042 llchange = bcmp(lladdr, ln->ll_addr, 2043 ifp->if_addrlen); 2044 } else if (!olladdr && lladdr) 2045 llchange = 1; 2046 else 2047 llchange = 0; 2048 2049 /* 2050 * newentry olladdr lladdr llchange (*=record) 2051 * 0 n n -- (1) 2052 * 0 y n -- (2) 2053 * 0 n y y (3) * STALE 2054 * 0 y y n (4) * 2055 * 0 y y y (5) * STALE 2056 * 1 -- n -- (6) NOSTATE(= PASSIVE) 2057 * 1 -- y -- (7) * STALE 2058 */ 2059 2060 do_update = 0; 2061 if (is_newentry == 0 && llchange != 0) { 2062 do_update = 1; /* (3,5) */ 2063 2064 /* 2065 * Record source link-layer address 2066 * XXX is it dependent to ifp->if_type? 2067 */ 2068 linkhdrsize = sizeof(linkhdr); 2069 if (lltable_calc_llheader(ifp, AF_INET6, lladdr, 2070 linkhdr, &linkhdrsize, &lladdr_off) != 0) 2071 return; 2072 2073 if (lltable_try_set_entry_addr(ifp, ln, linkhdr, linkhdrsize, 2074 lladdr_off) == 0) { 2075 /* Entry was deleted */ 2076 return; 2077 } 2078 2079 nd6_llinfo_setstate(ln, ND6_LLINFO_STALE); 2080 2081 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED); 2082 2083 if (ln->la_hold != NULL) 2084 nd6_grab_holdchain(ln, &chain, &sin6); 2085 } 2086 2087 /* Calculates new router status */ 2088 router = nd6_is_router(type, code, is_newentry, olladdr, 2089 lladdr != NULL ? 1 : 0, ln->ln_router); 2090 2091 ln->ln_router = router; 2092 /* Mark non-router redirects with special flag */ 2093 if ((type & 0xFF) == ND_REDIRECT && code != ND_REDIRECT_ROUTER) 2094 ln->la_flags |= LLE_REDIRECT; 2095 2096 if (flags & LLE_EXCLUSIVE) 2097 LLE_WUNLOCK(ln); 2098 else 2099 LLE_RUNLOCK(ln); 2100 2101 if (chain != NULL) 2102 nd6_flush_holdchain(ifp, ifp, chain, &sin6); 2103 2104 /* 2105 * When the link-layer address of a router changes, select the 2106 * best router again. In particular, when the neighbor entry is newly 2107 * created, it might affect the selection policy. 2108 * Question: can we restrict the first condition to the "is_newentry" 2109 * case? 2110 * XXX: when we hear an RA from a new router with the link-layer 2111 * address option, defrouter_select() is called twice, since 2112 * defrtrlist_update called the function as well. However, I believe 2113 * we can compromise the overhead, since it only happens the first 2114 * time. 2115 * XXX: although defrouter_select() should not have a bad effect 2116 * for those are not autoconfigured hosts, we explicitly avoid such 2117 * cases for safety. 2118 */ 2119 if ((do_update || is_newentry) && router && 2120 ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) { 2121 /* 2122 * guaranteed recursion 2123 */ 2124 defrouter_select(); 2125 } 2126 } 2127 2128 static void 2129 nd6_slowtimo(void *arg) 2130 { 2131 CURVNET_SET((struct vnet *) arg); 2132 struct nd_ifinfo *nd6if; 2133 struct ifnet *ifp; 2134 2135 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 2136 nd6_slowtimo, curvnet); 2137 IFNET_RLOCK_NOSLEEP(); 2138 TAILQ_FOREACH(ifp, &V_ifnet, if_link) { 2139 if (ifp->if_afdata[AF_INET6] == NULL) 2140 continue; 2141 nd6if = ND_IFINFO(ifp); 2142 if (nd6if->basereachable && /* already initialized */ 2143 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 2144 /* 2145 * Since reachable time rarely changes by router 2146 * advertisements, we SHOULD insure that a new random 2147 * value gets recomputed at least once every few hours. 2148 * (RFC 2461, 6.3.4) 2149 */ 2150 nd6if->recalctm = V_nd6_recalc_reachtm_interval; 2151 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); 2152 } 2153 } 2154 IFNET_RUNLOCK_NOSLEEP(); 2155 CURVNET_RESTORE(); 2156 } 2157 2158 void 2159 nd6_grab_holdchain(struct llentry *ln, struct mbuf **chain, 2160 struct sockaddr_in6 *sin6) 2161 { 2162 2163 LLE_WLOCK_ASSERT(ln); 2164 2165 *chain = ln->la_hold; 2166 ln->la_hold = NULL; 2167 lltable_fill_sa_entry(ln, (struct sockaddr *)sin6); 2168 2169 if (ln->ln_state == ND6_LLINFO_STALE) { 2170 2171 /* 2172 * The first time we send a packet to a 2173 * neighbor whose entry is STALE, we have 2174 * to change the state to DELAY and a sets 2175 * a timer to expire in DELAY_FIRST_PROBE_TIME 2176 * seconds to ensure do neighbor unreachability 2177 * detection on expiration. 2178 * (RFC 2461 7.3.3) 2179 */ 2180 nd6_llinfo_setstate(ln, ND6_LLINFO_DELAY); 2181 } 2182 } 2183 2184 int 2185 nd6_output_ifp(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m, 2186 struct sockaddr_in6 *dst, struct route *ro) 2187 { 2188 int error; 2189 int ip6len; 2190 struct ip6_hdr *ip6; 2191 struct m_tag *mtag; 2192 2193 #ifdef MAC 2194 mac_netinet6_nd6_send(ifp, m); 2195 #endif 2196 2197 /* 2198 * If called from nd6_ns_output() (NS), nd6_na_output() (NA), 2199 * icmp6_redirect_output() (REDIRECT) or from rip6_output() (RS, RA 2200 * as handled by rtsol and rtadvd), mbufs will be tagged for SeND 2201 * to be diverted to user space. When re-injected into the kernel, 2202 * send_output() will directly dispatch them to the outgoing interface. 2203 */ 2204 if (send_sendso_input_hook != NULL) { 2205 mtag = m_tag_find(m, PACKET_TAG_ND_OUTGOING, NULL); 2206 if (mtag != NULL) { 2207 ip6 = mtod(m, struct ip6_hdr *); 2208 ip6len = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen); 2209 /* Use the SEND socket */ 2210 error = send_sendso_input_hook(m, ifp, SND_OUT, 2211 ip6len); 2212 /* -1 == no app on SEND socket */ 2213 if (error == 0 || error != -1) 2214 return (error); 2215 } 2216 } 2217 2218 m_clrprotoflags(m); /* Avoid confusing lower layers. */ 2219 IP_PROBE(send, NULL, NULL, mtod(m, struct ip6_hdr *), ifp, NULL, 2220 mtod(m, struct ip6_hdr *)); 2221 2222 if ((ifp->if_flags & IFF_LOOPBACK) == 0) 2223 origifp = ifp; 2224 2225 error = (*ifp->if_output)(origifp, m, (struct sockaddr *)dst, ro); 2226 return (error); 2227 } 2228 2229 /* 2230 * Lookup link headerfor @sa_dst address. Stores found 2231 * data in @desten buffer. Copy of lle ln_flags can be also 2232 * saved in @pflags if @pflags is non-NULL. 2233 * 2234 * If destination LLE does not exists or lle state modification 2235 * is required, call "slow" version. 2236 * 2237 * Return values: 2238 * - 0 on success (address copied to buffer). 2239 * - EWOULDBLOCK (no local error, but address is still unresolved) 2240 * - other errors (alloc failure, etc) 2241 */ 2242 int 2243 nd6_resolve(struct ifnet *ifp, int is_gw, struct mbuf *m, 2244 const struct sockaddr *sa_dst, u_char *desten, uint32_t *pflags, 2245 struct llentry **plle) 2246 { 2247 struct llentry *ln = NULL; 2248 const struct sockaddr_in6 *dst6; 2249 2250 if (pflags != NULL) 2251 *pflags = 0; 2252 2253 dst6 = (const struct sockaddr_in6 *)sa_dst; 2254 2255 /* discard the packet if IPv6 operation is disabled on the interface */ 2256 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) { 2257 m_freem(m); 2258 return (ENETDOWN); /* better error? */ 2259 } 2260 2261 if (m != NULL && m->m_flags & M_MCAST) { 2262 switch (ifp->if_type) { 2263 case IFT_ETHER: 2264 case IFT_FDDI: 2265 case IFT_L2VLAN: 2266 case IFT_IEEE80211: 2267 case IFT_BRIDGE: 2268 case IFT_ISO88025: 2269 ETHER_MAP_IPV6_MULTICAST(&dst6->sin6_addr, 2270 desten); 2271 return (0); 2272 default: 2273 m_freem(m); 2274 return (EAFNOSUPPORT); 2275 } 2276 } 2277 2278 IF_AFDATA_RLOCK(ifp); 2279 ln = nd6_lookup(&dst6->sin6_addr, plle ? LLE_EXCLUSIVE : LLE_UNLOCKED, 2280 ifp); 2281 if (ln != NULL && (ln->r_flags & RLLE_VALID) != 0) { 2282 /* Entry found, let's copy lle info */ 2283 bcopy(ln->r_linkdata, desten, ln->r_hdrlen); 2284 if (pflags != NULL) 2285 *pflags = LLE_VALID | (ln->r_flags & RLLE_IFADDR); 2286 /* Check if we have feedback request from nd6 timer */ 2287 if (ln->r_skip_req != 0) { 2288 LLE_REQ_LOCK(ln); 2289 ln->r_skip_req = 0; /* Notify that entry was used */ 2290 ln->lle_hittime = time_uptime; 2291 LLE_REQ_UNLOCK(ln); 2292 } 2293 if (plle) { 2294 LLE_ADDREF(ln); 2295 *plle = ln; 2296 LLE_WUNLOCK(ln); 2297 } 2298 IF_AFDATA_RUNLOCK(ifp); 2299 return (0); 2300 } else if (plle && ln) 2301 LLE_WUNLOCK(ln); 2302 IF_AFDATA_RUNLOCK(ifp); 2303 2304 return (nd6_resolve_slow(ifp, 0, m, dst6, desten, pflags, plle)); 2305 } 2306 2307 2308 /* 2309 * Do L2 address resolution for @sa_dst address. Stores found 2310 * address in @desten buffer. Copy of lle ln_flags can be also 2311 * saved in @pflags if @pflags is non-NULL. 2312 * 2313 * Heavy version. 2314 * Function assume that destination LLE does not exist, 2315 * is invalid or stale, so LLE_EXCLUSIVE lock needs to be acquired. 2316 * 2317 * Set noinline to be dtrace-friendly 2318 */ 2319 static __noinline int 2320 nd6_resolve_slow(struct ifnet *ifp, int flags, struct mbuf *m, 2321 const struct sockaddr_in6 *dst, u_char *desten, uint32_t *pflags, 2322 struct llentry **plle) 2323 { 2324 struct llentry *lle = NULL, *lle_tmp; 2325 struct in6_addr *psrc, src; 2326 int send_ns, ll_len; 2327 char *lladdr; 2328 2329 /* 2330 * Address resolution or Neighbor Unreachability Detection 2331 * for the next hop. 2332 * At this point, the destination of the packet must be a unicast 2333 * or an anycast address(i.e. not a multicast). 2334 */ 2335 if (lle == NULL) { 2336 IF_AFDATA_RLOCK(ifp); 2337 lle = nd6_lookup(&dst->sin6_addr, LLE_EXCLUSIVE, ifp); 2338 IF_AFDATA_RUNLOCK(ifp); 2339 if ((lle == NULL) && nd6_is_addr_neighbor(dst, ifp)) { 2340 /* 2341 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 2342 * the condition below is not very efficient. But we believe 2343 * it is tolerable, because this should be a rare case. 2344 */ 2345 lle = nd6_alloc(&dst->sin6_addr, 0, ifp); 2346 if (lle == NULL) { 2347 char ip6buf[INET6_ADDRSTRLEN]; 2348 log(LOG_DEBUG, 2349 "nd6_output: can't allocate llinfo for %s " 2350 "(ln=%p)\n", 2351 ip6_sprintf(ip6buf, &dst->sin6_addr), lle); 2352 m_freem(m); 2353 return (ENOBUFS); 2354 } 2355 2356 IF_AFDATA_WLOCK(ifp); 2357 LLE_WLOCK(lle); 2358 /* Prefer any existing entry over newly-created one */ 2359 lle_tmp = nd6_lookup(&dst->sin6_addr, LLE_EXCLUSIVE, ifp); 2360 if (lle_tmp == NULL) 2361 lltable_link_entry(LLTABLE6(ifp), lle); 2362 IF_AFDATA_WUNLOCK(ifp); 2363 if (lle_tmp != NULL) { 2364 lltable_free_entry(LLTABLE6(ifp), lle); 2365 lle = lle_tmp; 2366 lle_tmp = NULL; 2367 } 2368 } 2369 } 2370 if (lle == NULL) { 2371 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) { 2372 m_freem(m); 2373 return (ENOBUFS); 2374 } 2375 2376 if (m != NULL) 2377 m_freem(m); 2378 return (ENOBUFS); 2379 } 2380 2381 LLE_WLOCK_ASSERT(lle); 2382 2383 /* 2384 * The first time we send a packet to a neighbor whose entry is 2385 * STALE, we have to change the state to DELAY and a sets a timer to 2386 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 2387 * neighbor unreachability detection on expiration. 2388 * (RFC 2461 7.3.3) 2389 */ 2390 if (lle->ln_state == ND6_LLINFO_STALE) 2391 nd6_llinfo_setstate(lle, ND6_LLINFO_DELAY); 2392 2393 /* 2394 * If the neighbor cache entry has a state other than INCOMPLETE 2395 * (i.e. its link-layer address is already resolved), just 2396 * send the packet. 2397 */ 2398 if (lle->ln_state > ND6_LLINFO_INCOMPLETE) { 2399 if (flags & LLE_ADDRONLY) { 2400 lladdr = lle->ll_addr; 2401 ll_len = ifp->if_addrlen; 2402 } else { 2403 lladdr = lle->r_linkdata; 2404 ll_len = lle->r_hdrlen; 2405 } 2406 bcopy(lladdr, desten, ll_len); 2407 if (pflags != NULL) 2408 *pflags = lle->la_flags; 2409 if (plle) { 2410 LLE_ADDREF(lle); 2411 *plle = lle; 2412 } 2413 LLE_WUNLOCK(lle); 2414 return (0); 2415 } 2416 2417 /* 2418 * There is a neighbor cache entry, but no ethernet address 2419 * response yet. Append this latest packet to the end of the 2420 * packet queue in the mbuf. When it exceeds nd6_maxqueuelen, 2421 * the oldest packet in the queue will be removed. 2422 */ 2423 2424 if (lle->la_hold != NULL) { 2425 struct mbuf *m_hold; 2426 int i; 2427 2428 i = 0; 2429 for (m_hold = lle->la_hold; m_hold; m_hold = m_hold->m_nextpkt){ 2430 i++; 2431 if (m_hold->m_nextpkt == NULL) { 2432 m_hold->m_nextpkt = m; 2433 break; 2434 } 2435 } 2436 while (i >= V_nd6_maxqueuelen) { 2437 m_hold = lle->la_hold; 2438 lle->la_hold = lle->la_hold->m_nextpkt; 2439 m_freem(m_hold); 2440 i--; 2441 } 2442 } else { 2443 lle->la_hold = m; 2444 } 2445 2446 /* 2447 * If there has been no NS for the neighbor after entering the 2448 * INCOMPLETE state, send the first solicitation. 2449 * Note that for newly-created lle la_asked will be 0, 2450 * so we will transition from ND6_LLINFO_NOSTATE to 2451 * ND6_LLINFO_INCOMPLETE state here. 2452 */ 2453 psrc = NULL; 2454 send_ns = 0; 2455 if (lle->la_asked == 0) { 2456 lle->la_asked++; 2457 send_ns = 1; 2458 psrc = nd6_llinfo_get_holdsrc(lle, &src); 2459 2460 nd6_llinfo_setstate(lle, ND6_LLINFO_INCOMPLETE); 2461 } 2462 LLE_WUNLOCK(lle); 2463 if (send_ns != 0) 2464 nd6_ns_output(ifp, psrc, NULL, &dst->sin6_addr, NULL); 2465 2466 return (EWOULDBLOCK); 2467 } 2468 2469 /* 2470 * Do L2 address resolution for @sa_dst address. Stores found 2471 * address in @desten buffer. Copy of lle ln_flags can be also 2472 * saved in @pflags if @pflags is non-NULL. 2473 * 2474 * Return values: 2475 * - 0 on success (address copied to buffer). 2476 * - EWOULDBLOCK (no local error, but address is still unresolved) 2477 * - other errors (alloc failure, etc) 2478 */ 2479 int 2480 nd6_resolve_addr(struct ifnet *ifp, int flags, const struct sockaddr *dst, 2481 char *desten, uint32_t *pflags) 2482 { 2483 int error; 2484 2485 flags |= LLE_ADDRONLY; 2486 error = nd6_resolve_slow(ifp, flags, NULL, 2487 (const struct sockaddr_in6 *)dst, desten, pflags, NULL); 2488 return (error); 2489 } 2490 2491 int 2492 nd6_flush_holdchain(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain, 2493 struct sockaddr_in6 *dst) 2494 { 2495 struct mbuf *m, *m_head; 2496 struct ifnet *outifp; 2497 int error = 0; 2498 2499 m_head = chain; 2500 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2501 outifp = origifp; 2502 else 2503 outifp = ifp; 2504 2505 while (m_head) { 2506 m = m_head; 2507 m_head = m_head->m_nextpkt; 2508 error = nd6_output_ifp(ifp, origifp, m, dst, NULL); 2509 } 2510 2511 /* 2512 * XXX 2513 * note that intermediate errors are blindly ignored 2514 */ 2515 return (error); 2516 } 2517 2518 static int 2519 nd6_need_cache(struct ifnet *ifp) 2520 { 2521 /* 2522 * XXX: we currently do not make neighbor cache on any interface 2523 * other than ARCnet, Ethernet, FDDI and GIF. 2524 * 2525 * RFC2893 says: 2526 * - unidirectional tunnels needs no ND 2527 */ 2528 switch (ifp->if_type) { 2529 case IFT_ARCNET: 2530 case IFT_ETHER: 2531 case IFT_FDDI: 2532 case IFT_IEEE1394: 2533 case IFT_L2VLAN: 2534 case IFT_IEEE80211: 2535 case IFT_INFINIBAND: 2536 case IFT_BRIDGE: 2537 case IFT_PROPVIRTUAL: 2538 return (1); 2539 default: 2540 return (0); 2541 } 2542 } 2543 2544 /* 2545 * Add pernament ND6 link-layer record for given 2546 * interface address. 2547 * 2548 * Very similar to IPv4 arp_ifinit(), but: 2549 * 1) IPv6 DAD is performed in different place 2550 * 2) It is called by IPv6 protocol stack in contrast to 2551 * arp_ifinit() which is typically called in SIOCSIFADDR 2552 * driver ioctl handler. 2553 * 2554 */ 2555 int 2556 nd6_add_ifa_lle(struct in6_ifaddr *ia) 2557 { 2558 struct ifnet *ifp; 2559 struct llentry *ln, *ln_tmp; 2560 struct sockaddr *dst; 2561 2562 ifp = ia->ia_ifa.ifa_ifp; 2563 if (nd6_need_cache(ifp) == 0) 2564 return (0); 2565 2566 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 2567 dst = (struct sockaddr *)&ia->ia_addr; 2568 ln = lltable_alloc_entry(LLTABLE6(ifp), LLE_IFADDR, dst); 2569 if (ln == NULL) 2570 return (ENOBUFS); 2571 2572 IF_AFDATA_WLOCK(ifp); 2573 LLE_WLOCK(ln); 2574 /* Unlink any entry if exists */ 2575 ln_tmp = lla_lookup(LLTABLE6(ifp), LLE_EXCLUSIVE, dst); 2576 if (ln_tmp != NULL) 2577 lltable_unlink_entry(LLTABLE6(ifp), ln_tmp); 2578 lltable_link_entry(LLTABLE6(ifp), ln); 2579 IF_AFDATA_WUNLOCK(ifp); 2580 2581 if (ln_tmp != NULL) 2582 EVENTHANDLER_INVOKE(lle_event, ln_tmp, LLENTRY_EXPIRED); 2583 EVENTHANDLER_INVOKE(lle_event, ln, LLENTRY_RESOLVED); 2584 2585 LLE_WUNLOCK(ln); 2586 if (ln_tmp != NULL) 2587 llentry_free(ln_tmp); 2588 2589 return (0); 2590 } 2591 2592 /* 2593 * Removes either all lle entries for given @ia, or lle 2594 * corresponding to @ia address. 2595 */ 2596 void 2597 nd6_rem_ifa_lle(struct in6_ifaddr *ia, int all) 2598 { 2599 struct sockaddr_in6 mask, addr; 2600 struct sockaddr *saddr, *smask; 2601 struct ifnet *ifp; 2602 2603 ifp = ia->ia_ifa.ifa_ifp; 2604 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 2605 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 2606 saddr = (struct sockaddr *)&addr; 2607 smask = (struct sockaddr *)&mask; 2608 2609 if (all != 0) 2610 lltable_prefix_free(AF_INET6, saddr, smask, LLE_STATIC); 2611 else 2612 lltable_delete_addr(LLTABLE6(ifp), LLE_IFADDR, saddr); 2613 } 2614 2615 static void 2616 clear_llinfo_pqueue(struct llentry *ln) 2617 { 2618 struct mbuf *m_hold, *m_hold_next; 2619 2620 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) { 2621 m_hold_next = m_hold->m_nextpkt; 2622 m_freem(m_hold); 2623 } 2624 2625 ln->la_hold = NULL; 2626 } 2627 2628 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS); 2629 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS); 2630 2631 SYSCTL_DECL(_net_inet6_icmp6); 2632 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist, 2633 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 2634 NULL, 0, nd6_sysctl_drlist, "S,in6_defrouter", 2635 "NDP default router list"); 2636 SYSCTL_PROC(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist, 2637 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, 2638 NULL, 0, nd6_sysctl_prlist, "S,in6_prefix", 2639 "NDP prefix list"); 2640 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, nd6_maxqueuelen, 2641 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_maxqueuelen), 1, ""); 2642 SYSCTL_INT(_net_inet6_icmp6, OID_AUTO, nd6_gctimer, 2643 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_gctimer), (60 * 60 * 24), ""); 2644 2645 static int 2646 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS) 2647 { 2648 struct in6_defrouter d; 2649 struct nd_defrouter *dr; 2650 int error; 2651 2652 if (req->newptr != NULL) 2653 return (EPERM); 2654 2655 error = sysctl_wire_old_buffer(req, 0); 2656 if (error != 0) 2657 return (error); 2658 2659 bzero(&d, sizeof(d)); 2660 d.rtaddr.sin6_family = AF_INET6; 2661 d.rtaddr.sin6_len = sizeof(d.rtaddr); 2662 2663 ND6_RLOCK(); 2664 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 2665 d.rtaddr.sin6_addr = dr->rtaddr; 2666 error = sa6_recoverscope(&d.rtaddr); 2667 if (error != 0) 2668 break; 2669 d.flags = dr->raflags; 2670 d.rtlifetime = dr->rtlifetime; 2671 d.expire = dr->expire + (time_second - time_uptime); 2672 d.if_index = dr->ifp->if_index; 2673 error = SYSCTL_OUT(req, &d, sizeof(d)); 2674 if (error != 0) 2675 break; 2676 } 2677 ND6_RUNLOCK(); 2678 return (error); 2679 } 2680 2681 static int 2682 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS) 2683 { 2684 struct in6_prefix p; 2685 struct sockaddr_in6 s6; 2686 struct nd_prefix *pr; 2687 struct nd_pfxrouter *pfr; 2688 time_t maxexpire; 2689 int error; 2690 char ip6buf[INET6_ADDRSTRLEN]; 2691 2692 if (req->newptr) 2693 return (EPERM); 2694 2695 error = sysctl_wire_old_buffer(req, 0); 2696 if (error != 0) 2697 return (error); 2698 2699 bzero(&p, sizeof(p)); 2700 p.origin = PR_ORIG_RA; 2701 bzero(&s6, sizeof(s6)); 2702 s6.sin6_family = AF_INET6; 2703 s6.sin6_len = sizeof(s6); 2704 2705 ND6_RLOCK(); 2706 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 2707 p.prefix = pr->ndpr_prefix; 2708 if (sa6_recoverscope(&p.prefix)) { 2709 log(LOG_ERR, "scope error in prefix list (%s)\n", 2710 ip6_sprintf(ip6buf, &p.prefix.sin6_addr)); 2711 /* XXX: press on... */ 2712 } 2713 p.raflags = pr->ndpr_raf; 2714 p.prefixlen = pr->ndpr_plen; 2715 p.vltime = pr->ndpr_vltime; 2716 p.pltime = pr->ndpr_pltime; 2717 p.if_index = pr->ndpr_ifp->if_index; 2718 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2719 p.expire = 0; 2720 else { 2721 /* XXX: we assume time_t is signed. */ 2722 maxexpire = (-1) & 2723 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 2724 if (pr->ndpr_vltime < maxexpire - pr->ndpr_lastupdate) 2725 p.expire = pr->ndpr_lastupdate + 2726 pr->ndpr_vltime + 2727 (time_second - time_uptime); 2728 else 2729 p.expire = maxexpire; 2730 } 2731 p.refcnt = pr->ndpr_addrcnt; 2732 p.flags = pr->ndpr_stateflags; 2733 p.advrtrs = 0; 2734 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) 2735 p.advrtrs++; 2736 error = SYSCTL_OUT(req, &p, sizeof(p)); 2737 if (error != 0) 2738 break; 2739 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 2740 s6.sin6_addr = pfr->router->rtaddr; 2741 if (sa6_recoverscope(&s6)) 2742 log(LOG_ERR, 2743 "scope error in prefix list (%s)\n", 2744 ip6_sprintf(ip6buf, &pfr->router->rtaddr)); 2745 error = SYSCTL_OUT(req, &s6, sizeof(s6)); 2746 if (error != 0) 2747 goto out; 2748 } 2749 } 2750 out: 2751 ND6_RUNLOCK(); 2752 return (error); 2753 } 2754