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