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