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