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