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