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