1 /* $NetBSD: nd6.c,v 1.223 2016/12/22 03:46:51 ozaki-r Exp $ */ 2 /* $KAME: nd6.c,v 1.279 2002/06/08 11:16:51 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: nd6.c,v 1.223 2016/12/22 03:46:51 ozaki-r Exp $"); 35 36 #ifdef _KERNEL_OPT 37 #include "opt_net_mpsafe.h" 38 #endif 39 40 #include "bridge.h" 41 #include "carp.h" 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/callout.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/sockio.h> 51 #include <sys/time.h> 52 #include <sys/kernel.h> 53 #include <sys/protosw.h> 54 #include <sys/errno.h> 55 #include <sys/ioctl.h> 56 #include <sys/syslog.h> 57 #include <sys/queue.h> 58 #include <sys/cprng.h> 59 #include <sys/workqueue.h> 60 61 #include <net/if.h> 62 #include <net/if_dl.h> 63 #include <net/if_llatbl.h> 64 #include <net/if_types.h> 65 #include <net/route.h> 66 #include <net/if_ether.h> 67 #include <net/if_fddi.h> 68 #include <net/if_arc.h> 69 70 #include <netinet/in.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/icmp6_private.h> 79 80 #include <net/net_osdep.h> 81 82 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */ 83 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */ 84 85 /* timer values */ 86 int nd6_prune = 1; /* walk list every 1 seconds */ 87 int nd6_delay = 5; /* delay first probe time 5 second */ 88 int nd6_umaxtries = 3; /* maximum unicast query */ 89 int nd6_mmaxtries = 3; /* maximum multicast query */ 90 int nd6_useloopback = 1; /* use loopback interface for local traffic */ 91 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */ 92 93 /* preventing too many loops in ND option parsing */ 94 int nd6_maxndopt = 10; /* max # of ND options allowed */ 95 96 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */ 97 98 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */ 99 100 #ifdef ND6_DEBUG 101 int nd6_debug = 1; 102 #else 103 int nd6_debug = 0; 104 #endif 105 106 krwlock_t nd6_lock __cacheline_aligned; 107 108 struct nd_drhead nd_defrouter; 109 struct nd_prhead nd_prefix = { 0 }; 110 111 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL; 112 113 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *); 114 static void nd6_slowtimo(void *); 115 static int regen_tmpaddr(const struct in6_ifaddr *); 116 static void nd6_free(struct llentry *, int); 117 static void nd6_llinfo_timer(void *); 118 static void nd6_timer(void *); 119 static void nd6_timer_work(struct work *, void *); 120 static void clear_llinfo_pqueue(struct llentry *); 121 static struct nd_opt_hdr *nd6_option(union nd_opts *); 122 123 static callout_t nd6_slowtimo_ch; 124 static callout_t nd6_timer_ch; 125 static struct workqueue *nd6_timer_wq; 126 static struct work nd6_timer_wk; 127 128 static int fill_drlist(void *, size_t *, size_t); 129 static int fill_prlist(void *, size_t *, size_t); 130 131 static struct ifnet *nd6_defifp; 132 static int nd6_defifindex; 133 134 static int nd6_setdefaultiface(int); 135 136 MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery"); 137 138 void 139 nd6_init(void) 140 { 141 int error; 142 143 rw_init(&nd6_lock); 144 145 /* initialization of the default router list */ 146 ND_DEFROUTER_LIST_INIT(); 147 148 callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE); 149 callout_init(&nd6_timer_ch, CALLOUT_MPSAFE); 150 151 error = workqueue_create(&nd6_timer_wq, "nd6_timer", 152 nd6_timer_work, NULL, PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE); 153 if (error) 154 panic("%s: workqueue_create failed (%d)\n", __func__, error); 155 156 /* start timer */ 157 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 158 nd6_slowtimo, NULL); 159 callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); 160 } 161 162 struct nd_ifinfo * 163 nd6_ifattach(struct ifnet *ifp) 164 { 165 struct nd_ifinfo *nd; 166 167 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK|M_ZERO); 168 169 nd->initialized = 1; 170 171 nd->chlim = IPV6_DEFHLIM; 172 nd->basereachable = REACHABLE_TIME; 173 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable); 174 nd->retrans = RETRANS_TIMER; 175 176 nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV; 177 178 /* A loopback interface always has ND6_IFF_AUTO_LINKLOCAL. 179 * A bridge interface should not have ND6_IFF_AUTO_LINKLOCAL 180 * because one of its members should. */ 181 if ((ip6_auto_linklocal && ifp->if_type != IFT_BRIDGE) || 182 (ifp->if_flags & IFF_LOOPBACK)) 183 nd->flags |= ND6_IFF_AUTO_LINKLOCAL; 184 185 /* A loopback interface does not need to accept RTADV. 186 * A bridge interface should not accept RTADV 187 * because one of its members should. */ 188 if (ip6_accept_rtadv && 189 !(ifp->if_flags & IFF_LOOPBACK) && 190 !(ifp->if_type != IFT_BRIDGE)) 191 nd->flags |= ND6_IFF_ACCEPT_RTADV; 192 193 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */ 194 nd6_setmtu0(ifp, nd); 195 196 return nd; 197 } 198 199 void 200 nd6_ifdetach(struct ifnet *ifp, struct in6_ifextra *ext) 201 { 202 203 /* Ensure all IPv6 addresses are purged before calling nd6_purge */ 204 if_purgeaddrs(ifp, AF_INET6, in6_purgeaddr); 205 nd6_purge(ifp, ext); 206 free(ext->nd_ifinfo, M_IP6NDP); 207 } 208 209 void 210 nd6_setmtu(struct ifnet *ifp) 211 { 212 nd6_setmtu0(ifp, ND_IFINFO(ifp)); 213 } 214 215 void 216 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi) 217 { 218 u_int32_t omaxmtu; 219 220 omaxmtu = ndi->maxmtu; 221 222 switch (ifp->if_type) { 223 case IFT_ARCNET: 224 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */ 225 break; 226 case IFT_FDDI: 227 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); 228 break; 229 default: 230 ndi->maxmtu = ifp->if_mtu; 231 break; 232 } 233 234 /* 235 * Decreasing the interface MTU under IPV6 minimum MTU may cause 236 * undesirable situation. We thus notify the operator of the change 237 * explicitly. The check for omaxmtu is necessary to restrict the 238 * log to the case of changing the MTU, not initializing it. 239 */ 240 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) { 241 log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too" 242 " small for IPv6 which needs %lu\n", 243 if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long) 244 IPV6_MMTU); 245 } 246 247 if (ndi->maxmtu > in6_maxmtu) 248 in6_setmaxmtu(); /* check all interfaces just in case */ 249 } 250 251 void 252 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts) 253 { 254 255 memset(ndopts, 0, sizeof(*ndopts)); 256 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt; 257 ndopts->nd_opts_last 258 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len); 259 260 if (icmp6len == 0) { 261 ndopts->nd_opts_done = 1; 262 ndopts->nd_opts_search = NULL; 263 } 264 } 265 266 /* 267 * Take one ND option. 268 */ 269 static struct nd_opt_hdr * 270 nd6_option(union nd_opts *ndopts) 271 { 272 struct nd_opt_hdr *nd_opt; 273 int olen; 274 275 KASSERT(ndopts != NULL); 276 KASSERT(ndopts->nd_opts_last != NULL); 277 278 if (ndopts->nd_opts_search == NULL) 279 return NULL; 280 if (ndopts->nd_opts_done) 281 return NULL; 282 283 nd_opt = ndopts->nd_opts_search; 284 285 /* make sure nd_opt_len is inside the buffer */ 286 if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) { 287 memset(ndopts, 0, sizeof(*ndopts)); 288 return NULL; 289 } 290 291 olen = nd_opt->nd_opt_len << 3; 292 if (olen == 0) { 293 /* 294 * Message validation requires that all included 295 * options have a length that is greater than zero. 296 */ 297 memset(ndopts, 0, sizeof(*ndopts)); 298 return NULL; 299 } 300 301 ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen); 302 if (ndopts->nd_opts_search > ndopts->nd_opts_last) { 303 /* option overruns the end of buffer, invalid */ 304 memset(ndopts, 0, sizeof(*ndopts)); 305 return NULL; 306 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) { 307 /* reached the end of options chain */ 308 ndopts->nd_opts_done = 1; 309 ndopts->nd_opts_search = NULL; 310 } 311 return nd_opt; 312 } 313 314 /* 315 * Parse multiple ND options. 316 * This function is much easier to use, for ND routines that do not need 317 * multiple options of the same type. 318 */ 319 int 320 nd6_options(union nd_opts *ndopts) 321 { 322 struct nd_opt_hdr *nd_opt; 323 int i = 0; 324 325 KASSERT(ndopts != NULL); 326 KASSERT(ndopts->nd_opts_last != NULL); 327 328 if (ndopts->nd_opts_search == NULL) 329 return 0; 330 331 while (1) { 332 nd_opt = nd6_option(ndopts); 333 if (nd_opt == NULL && ndopts->nd_opts_last == NULL) { 334 /* 335 * Message validation requires that all included 336 * options have a length that is greater than zero. 337 */ 338 ICMP6_STATINC(ICMP6_STAT_ND_BADOPT); 339 memset(ndopts, 0, sizeof(*ndopts)); 340 return -1; 341 } 342 343 if (nd_opt == NULL) 344 goto skip1; 345 346 switch (nd_opt->nd_opt_type) { 347 case ND_OPT_SOURCE_LINKADDR: 348 case ND_OPT_TARGET_LINKADDR: 349 case ND_OPT_MTU: 350 case ND_OPT_REDIRECTED_HEADER: 351 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) { 352 nd6log(LOG_INFO, 353 "duplicated ND6 option found (type=%d)\n", 354 nd_opt->nd_opt_type); 355 /* XXX bark? */ 356 } else { 357 ndopts->nd_opt_array[nd_opt->nd_opt_type] 358 = nd_opt; 359 } 360 break; 361 case ND_OPT_PREFIX_INFORMATION: 362 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) { 363 ndopts->nd_opt_array[nd_opt->nd_opt_type] 364 = nd_opt; 365 } 366 ndopts->nd_opts_pi_end = 367 (struct nd_opt_prefix_info *)nd_opt; 368 break; 369 default: 370 /* 371 * Unknown options must be silently ignored, 372 * to accommodate future extension to the protocol. 373 */ 374 nd6log(LOG_DEBUG, 375 "nd6_options: unsupported option %d - " 376 "option ignored\n", nd_opt->nd_opt_type); 377 } 378 379 skip1: 380 i++; 381 if (i > nd6_maxndopt) { 382 ICMP6_STATINC(ICMP6_STAT_ND_TOOMANYOPT); 383 nd6log(LOG_INFO, "too many loop in nd opt\n"); 384 break; 385 } 386 387 if (ndopts->nd_opts_done) 388 break; 389 } 390 391 return 0; 392 } 393 394 /* 395 * ND6 timer routine to handle ND6 entries 396 */ 397 void 398 nd6_llinfo_settimer(struct llentry *ln, time_t xtick) 399 { 400 401 CTASSERT(sizeof(time_t) > sizeof(int)); 402 LLE_WLOCK_ASSERT(ln); 403 404 KASSERT(xtick >= 0); 405 406 ln->ln_expire = time_uptime + xtick / hz; 407 LLE_ADDREF(ln); 408 if (xtick > INT_MAX) { 409 ln->ln_ntick = xtick - INT_MAX; 410 callout_reset(&ln->ln_timer_ch, INT_MAX, 411 nd6_llinfo_timer, ln); 412 } else { 413 ln->ln_ntick = 0; 414 callout_reset(&ln->ln_timer_ch, xtick, 415 nd6_llinfo_timer, ln); 416 } 417 } 418 419 /* 420 * Gets source address of the first packet in hold queue 421 * and stores it in @src. 422 * Returns pointer to @src (if hold queue is not empty) or NULL. 423 */ 424 static struct in6_addr * 425 nd6_llinfo_get_holdsrc(struct llentry *ln, struct in6_addr *src) 426 { 427 struct ip6_hdr *hip6; 428 429 if (ln == NULL || ln->ln_hold == NULL) 430 return NULL; 431 432 /* 433 * assuming every packet in ln_hold has the same IP header 434 */ 435 hip6 = mtod(ln->ln_hold, struct ip6_hdr *); 436 /* XXX pullup? */ 437 if (sizeof(*hip6) < ln->ln_hold->m_len) 438 *src = hip6->ip6_src; 439 else 440 src = NULL; 441 442 return src; 443 } 444 445 static void 446 nd6_llinfo_timer(void *arg) 447 { 448 struct llentry *ln = arg; 449 struct ifnet *ifp; 450 struct nd_ifinfo *ndi = NULL; 451 bool send_ns = false; 452 const struct in6_addr *daddr6 = NULL; 453 454 LLE_WLOCK(ln); 455 if ((ln->la_flags & LLE_LINKED) == 0) 456 goto out; 457 if (ln->ln_ntick > 0) { 458 nd6_llinfo_settimer(ln, ln->ln_ntick); 459 goto out; 460 } 461 462 if (callout_pending(&ln->la_timer)) { 463 /* 464 * Here we are a bit odd here in the treatment of 465 * active/pending. If the pending bit is set, it got 466 * rescheduled before I ran. The active 467 * bit we ignore, since if it was stopped 468 * in ll_tablefree() and was currently running 469 * it would have return 0 so the code would 470 * not have deleted it since the callout could 471 * not be stopped so we want to go through 472 * with the delete here now. If the callout 473 * was restarted, the pending bit will be back on and 474 * we just want to bail since the callout_reset would 475 * return 1 and our reference would have been removed 476 * by nd6_llinfo_settimer above since canceled 477 * would have been 1. 478 */ 479 goto out; 480 } 481 482 ifp = ln->lle_tbl->llt_ifp; 483 484 KASSERT(ifp != NULL); 485 486 ndi = ND_IFINFO(ifp); 487 488 switch (ln->ln_state) { 489 case ND6_LLINFO_INCOMPLETE: 490 if (ln->ln_asked < nd6_mmaxtries) { 491 ln->ln_asked++; 492 send_ns = true; 493 } else { 494 struct mbuf *m = ln->ln_hold; 495 if (m) { 496 struct mbuf *m0; 497 498 /* 499 * assuming every packet in ln_hold has 500 * the same IP header 501 */ 502 m0 = m->m_nextpkt; 503 m->m_nextpkt = NULL; 504 ln->ln_hold = m0; 505 clear_llinfo_pqueue(ln); 506 } 507 nd6_free(ln, 0); 508 ln = NULL; 509 if (m != NULL) { 510 #ifndef NET_MPSAFE 511 mutex_enter(softnet_lock); 512 KERNEL_LOCK(1, NULL); 513 #endif 514 icmp6_error2(m, ICMP6_DST_UNREACH, 515 ICMP6_DST_UNREACH_ADDR, 0, ifp); 516 #ifndef NET_MPSAFE 517 KERNEL_UNLOCK_ONE(NULL); 518 mutex_exit(softnet_lock); 519 #endif 520 } 521 } 522 break; 523 case ND6_LLINFO_REACHABLE: 524 if (!ND6_LLINFO_PERMANENT(ln)) { 525 ln->ln_state = ND6_LLINFO_STALE; 526 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 527 } 528 break; 529 530 case ND6_LLINFO_PURGE: 531 case ND6_LLINFO_STALE: 532 /* Garbage Collection(RFC 2461 5.3) */ 533 if (!ND6_LLINFO_PERMANENT(ln)) { 534 nd6_free(ln, 1); 535 ln = NULL; 536 } 537 break; 538 539 case ND6_LLINFO_DELAY: 540 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) { 541 /* We need NUD */ 542 ln->ln_asked = 1; 543 ln->ln_state = ND6_LLINFO_PROBE; 544 daddr6 = &ln->r_l3addr.addr6; 545 send_ns = true; 546 } else { 547 ln->ln_state = ND6_LLINFO_STALE; /* XXX */ 548 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 549 } 550 break; 551 case ND6_LLINFO_PROBE: 552 if (ln->ln_asked < nd6_umaxtries) { 553 ln->ln_asked++; 554 daddr6 = &ln->r_l3addr.addr6; 555 send_ns = true; 556 } else { 557 nd6_free(ln, 0); 558 ln = NULL; 559 } 560 break; 561 } 562 563 if (send_ns) { 564 struct in6_addr src, *psrc; 565 const struct in6_addr *taddr6 = &ln->r_l3addr.addr6; 566 567 nd6_llinfo_settimer(ln, ndi->retrans * hz / 1000); 568 psrc = nd6_llinfo_get_holdsrc(ln, &src); 569 LLE_FREE_LOCKED(ln); 570 ln = NULL; 571 #ifndef NET_MPSAFE 572 mutex_enter(softnet_lock); 573 KERNEL_LOCK(1, NULL); 574 #endif 575 nd6_ns_output(ifp, daddr6, taddr6, psrc, 0); 576 #ifndef NET_MPSAFE 577 KERNEL_UNLOCK_ONE(NULL); 578 mutex_exit(softnet_lock); 579 #endif 580 } 581 582 out: 583 if (ln != NULL) 584 LLE_FREE_LOCKED(ln); 585 } 586 587 /* 588 * ND6 timer routine to expire default route list and prefix list 589 */ 590 static void 591 nd6_timer_work(struct work *wk, void *arg) 592 { 593 struct nd_defrouter *next_dr, *dr; 594 struct nd_prefix *next_pr, *pr; 595 struct in6_ifaddr *ia6, *nia6; 596 int s, bound; 597 struct psref psref; 598 599 callout_reset(&nd6_timer_ch, nd6_prune * hz, 600 nd6_timer, NULL); 601 602 #ifndef NET_MPSAFE 603 mutex_enter(softnet_lock); 604 KERNEL_LOCK(1, NULL); 605 #endif 606 607 /* expire default router list */ 608 609 ND6_WLOCK(); 610 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, next_dr) { 611 if (dr->expire && dr->expire < time_uptime) { 612 nd6_defrtrlist_del(dr, NULL); 613 } 614 } 615 ND6_UNLOCK(); 616 617 /* 618 * expire interface addresses. 619 * in the past the loop was inside prefix expiry processing. 620 * However, from a stricter speci-confrmance standpoint, we should 621 * rather separate address lifetimes and prefix lifetimes. 622 */ 623 bound = curlwp_bind(); 624 addrloop: 625 s = pserialize_read_enter(); 626 for (ia6 = IN6_ADDRLIST_READER_FIRST(); ia6; ia6 = nia6) { 627 nia6 = IN6_ADDRLIST_READER_NEXT(ia6); 628 629 ia6_acquire(ia6, &psref); 630 pserialize_read_exit(s); 631 632 /* check address lifetime */ 633 if (IFA6_IS_INVALID(ia6)) { 634 int regen = 0; 635 636 /* 637 * If the expiring address is temporary, try 638 * regenerating a new one. This would be useful when 639 * we suspended a laptop PC, then turned it on after a 640 * period that could invalidate all temporary 641 * addresses. Although we may have to restart the 642 * loop (see below), it must be after purging the 643 * address. Otherwise, we'd see an infinite loop of 644 * regeneration. 645 */ 646 if (ip6_use_tempaddr && 647 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 648 if (regen_tmpaddr(ia6) == 0) 649 regen = 1; 650 } 651 652 ia6_release(ia6, &psref); 653 in6_purgeaddr(&ia6->ia_ifa); 654 ia6 = NULL; 655 656 if (regen) 657 goto addrloop; /* XXX: see below */ 658 } else if (IFA6_IS_DEPRECATED(ia6)) { 659 int oldflags = ia6->ia6_flags; 660 661 if ((oldflags & IN6_IFF_DEPRECATED) == 0) { 662 ia6->ia6_flags |= IN6_IFF_DEPRECATED; 663 rt_newaddrmsg(RTM_NEWADDR, 664 (struct ifaddr *)ia6, 0, NULL); 665 } 666 667 /* 668 * If a temporary address has just become deprecated, 669 * regenerate a new one if possible. 670 */ 671 if (ip6_use_tempaddr && 672 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 673 (oldflags & IN6_IFF_DEPRECATED) == 0) { 674 675 if (regen_tmpaddr(ia6) == 0) { 676 /* 677 * A new temporary address is 678 * generated. 679 * XXX: this means the address chain 680 * has changed while we are still in 681 * the loop. Although the change 682 * would not cause disaster (because 683 * it's not a deletion, but an 684 * addition,) we'd rather restart the 685 * loop just for safety. Or does this 686 * significantly reduce performance?? 687 */ 688 ia6_release(ia6, &psref); 689 goto addrloop; 690 } 691 } 692 } else { 693 /* 694 * A new RA might have made a deprecated address 695 * preferred. 696 */ 697 if (ia6->ia6_flags & IN6_IFF_DEPRECATED) { 698 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED; 699 rt_newaddrmsg(RTM_NEWADDR, 700 (struct ifaddr *)ia6, 0, NULL); 701 } 702 } 703 s = pserialize_read_enter(); 704 ia6_release(ia6, &psref); 705 } 706 pserialize_read_exit(s); 707 curlwp_bindx(bound); 708 709 /* expire prefix list */ 710 ND6_WLOCK(); 711 ND_PREFIX_LIST_FOREACH_SAFE(pr, next_pr) { 712 /* 713 * check prefix lifetime. 714 * since pltime is just for autoconf, pltime processing for 715 * prefix is not necessary. 716 */ 717 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME && 718 time_uptime - pr->ndpr_lastupdate > pr->ndpr_vltime) { 719 720 /* 721 * address expiration and prefix expiration are 722 * separate. NEVER perform in6_purgeaddr here. 723 */ 724 725 nd6_prelist_remove(pr); 726 } 727 } 728 ND6_UNLOCK(); 729 730 #ifndef NET_MPSAFE 731 KERNEL_UNLOCK_ONE(NULL); 732 mutex_exit(softnet_lock); 733 #endif 734 } 735 736 static void 737 nd6_timer(void *ignored_arg) 738 { 739 740 workqueue_enqueue(nd6_timer_wq, &nd6_timer_wk, NULL); 741 } 742 743 /* ia6: deprecated/invalidated temporary address */ 744 static int 745 regen_tmpaddr(const struct in6_ifaddr *ia6) 746 { 747 struct ifaddr *ifa; 748 struct ifnet *ifp; 749 struct in6_ifaddr *public_ifa6 = NULL; 750 int s; 751 752 ifp = ia6->ia_ifa.ifa_ifp; 753 s = pserialize_read_enter(); 754 IFADDR_READER_FOREACH(ifa, ifp) { 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 793 if (public_ifa6 != NULL) { 794 int e; 795 struct psref psref; 796 797 ia6_acquire(public_ifa6, &psref); 798 pserialize_read_exit(s); 799 /* 800 * Random factor is introduced in the preferred lifetime, so 801 * we do not need additional delay (3rd arg to in6_tmpifadd). 802 */ 803 ND6_WLOCK(); 804 e = in6_tmpifadd(public_ifa6, 0, 0); 805 ND6_UNLOCK(); 806 if (e != 0) { 807 ia6_release(public_ifa6, &psref); 808 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" 809 " tmp addr, errno=%d\n", e); 810 return -1; 811 } 812 ia6_release(public_ifa6, &psref); 813 return 0; 814 } 815 pserialize_read_exit(s); 816 817 return -1; 818 } 819 820 bool 821 nd6_accepts_rtadv(const struct nd_ifinfo *ndi) 822 { 823 switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) { 824 case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV: 825 return true; 826 case ND6_IFF_ACCEPT_RTADV: 827 return ip6_accept_rtadv != 0; 828 case ND6_IFF_OVERRIDE_RTADV: 829 case 0: 830 default: 831 return false; 832 } 833 } 834 835 /* 836 * Nuke neighbor cache/prefix/default router management table, right before 837 * ifp goes away. 838 */ 839 void 840 nd6_purge(struct ifnet *ifp, struct in6_ifextra *ext) 841 { 842 struct nd_defrouter *dr, *ndr; 843 struct nd_prefix *pr, *npr; 844 845 /* 846 * During detach, the ND info might be already removed, but 847 * then is explitly passed as argument. 848 * Otherwise get it from ifp->if_afdata. 849 */ 850 if (ext == NULL) 851 ext = ifp->if_afdata[AF_INET6]; 852 if (ext == NULL) 853 return; 854 855 ND6_WLOCK(); 856 /* 857 * Nuke default router list entries toward ifp. 858 * We defer removal of default router list entries that is installed 859 * in the routing table, in order to keep additional side effects as 860 * small as possible. 861 */ 862 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, ndr) { 863 if (dr->installed) 864 continue; 865 866 if (dr->ifp == ifp) { 867 KASSERT(ext != NULL); 868 nd6_defrtrlist_del(dr, ext); 869 } 870 } 871 872 ND_DEFROUTER_LIST_FOREACH_SAFE(dr, ndr) { 873 if (!dr->installed) 874 continue; 875 876 if (dr->ifp == ifp) { 877 KASSERT(ext != NULL); 878 nd6_defrtrlist_del(dr, ext); 879 } 880 } 881 882 /* Nuke prefix list entries toward ifp */ 883 ND_PREFIX_LIST_FOREACH_SAFE(pr, npr) { 884 if (pr->ndpr_ifp == ifp) { 885 /* 886 * All addresses referencing pr should be already freed. 887 */ 888 KASSERT(pr->ndpr_refcnt == 0); 889 nd6_prelist_remove(pr); 890 } 891 } 892 893 /* cancel default outgoing interface setting */ 894 if (nd6_defifindex == ifp->if_index) 895 nd6_setdefaultiface(0); 896 897 /* XXX: too restrictive? */ 898 if (!ip6_forwarding && ifp->if_afdata[AF_INET6]) { 899 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 900 if (ndi && nd6_accepts_rtadv(ndi)) { 901 /* refresh default router list */ 902 nd6_defrouter_select(); 903 } 904 } 905 ND6_UNLOCK(); 906 907 /* 908 * We may not need to nuke the neighbor cache entries here 909 * because the neighbor cache is kept in if_afdata[AF_INET6]. 910 * nd6_purge() is invoked by in6_ifdetach() which is called 911 * from if_detach() where everything gets purged. However 912 * in6_ifdetach is directly called from vlan(4), so we still 913 * need to purge entries here. 914 */ 915 if (ext->lltable != NULL) 916 lltable_purge_entries(ext->lltable); 917 } 918 919 void 920 nd6_assert_purged(struct ifnet *ifp) 921 { 922 struct nd_defrouter *dr; 923 struct nd_prefix *pr; 924 925 ND6_RLOCK(); 926 ND_DEFROUTER_LIST_FOREACH(dr) { 927 KASSERTMSG(dr->ifp != ifp, 928 "defrouter %s remains on %s", 929 ip6_sprintf(&dr->rtaddr), ifp->if_xname); 930 } 931 932 ND_PREFIX_LIST_FOREACH(pr) { 933 KASSERTMSG(pr->ndpr_ifp != ifp, 934 "prefix %s/%d remains on %s", 935 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 936 pr->ndpr_plen, ifp->if_xname); 937 } 938 ND6_UNLOCK(); 939 } 940 941 struct llentry * 942 nd6_lookup(const struct in6_addr *addr6, const struct ifnet *ifp, bool wlock) 943 { 944 struct sockaddr_in6 sin6; 945 struct llentry *ln; 946 947 sockaddr_in6_init(&sin6, addr6, 0, 0, 0); 948 949 IF_AFDATA_RLOCK(ifp); 950 ln = lla_lookup(LLTABLE6(ifp), wlock ? LLE_EXCLUSIVE : 0, 951 sin6tosa(&sin6)); 952 IF_AFDATA_RUNLOCK(ifp); 953 954 return ln; 955 } 956 957 struct llentry * 958 nd6_create(const struct in6_addr *addr6, const struct ifnet *ifp) 959 { 960 struct sockaddr_in6 sin6; 961 struct llentry *ln; 962 963 sockaddr_in6_init(&sin6, addr6, 0, 0, 0); 964 965 IF_AFDATA_WLOCK(ifp); 966 ln = lla_create(LLTABLE6(ifp), LLE_EXCLUSIVE, 967 sin6tosa(&sin6)); 968 IF_AFDATA_WUNLOCK(ifp); 969 970 if (ln != NULL) 971 ln->ln_state = ND6_LLINFO_NOSTATE; 972 973 return ln; 974 } 975 976 /* 977 * Test whether a given IPv6 address is a neighbor or not, ignoring 978 * the actual neighbor cache. The neighbor cache is ignored in order 979 * to not reenter the routing code from within itself. 980 */ 981 static int 982 nd6_is_new_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 983 { 984 struct nd_prefix *pr; 985 struct ifaddr *dstaddr; 986 int s; 987 988 /* 989 * A link-local address is always a neighbor. 990 * XXX: a link does not necessarily specify a single interface. 991 */ 992 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { 993 struct sockaddr_in6 sin6_copy; 994 u_int32_t zone; 995 996 /* 997 * We need sin6_copy since sa6_recoverscope() may modify the 998 * content (XXX). 999 */ 1000 sin6_copy = *addr; 1001 if (sa6_recoverscope(&sin6_copy)) 1002 return 0; /* XXX: should be impossible */ 1003 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) 1004 return 0; 1005 if (sin6_copy.sin6_scope_id == zone) 1006 return 1; 1007 else 1008 return 0; 1009 } 1010 1011 /* 1012 * If the address matches one of our addresses, 1013 * it should be a neighbor. 1014 * If the address matches one of our on-link prefixes, it should be a 1015 * neighbor. 1016 */ 1017 ND6_RLOCK(); 1018 ND_PREFIX_LIST_FOREACH(pr) { 1019 if (pr->ndpr_ifp != ifp) 1020 continue; 1021 1022 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) { 1023 struct rtentry *rt; 1024 1025 rt = rtalloc1(sin6tosa(&pr->ndpr_prefix), 0); 1026 if (rt == NULL) 1027 continue; 1028 /* 1029 * This is the case where multiple interfaces 1030 * have the same prefix, but only one is installed 1031 * into the routing table and that prefix entry 1032 * is not the one being examined here. In the case 1033 * where RADIX_MPATH is enabled, multiple route 1034 * entries (of the same rt_key value) will be 1035 * installed because the interface addresses all 1036 * differ. 1037 */ 1038 if (!IN6_ARE_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1039 &satocsin6(rt_getkey(rt))->sin6_addr)) { 1040 rt_unref(rt); 1041 continue; 1042 } 1043 rt_unref(rt); 1044 } 1045 1046 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1047 &addr->sin6_addr, &pr->ndpr_mask)) { 1048 ND6_UNLOCK(); 1049 return 1; 1050 } 1051 } 1052 ND6_UNLOCK(); 1053 1054 /* 1055 * If the address is assigned on the node of the other side of 1056 * a p2p interface, the address should be a neighbor. 1057 */ 1058 s = pserialize_read_enter(); 1059 dstaddr = ifa_ifwithdstaddr(sin6tocsa(addr)); 1060 if (dstaddr != NULL) { 1061 if (dstaddr->ifa_ifp == ifp) { 1062 pserialize_read_exit(s); 1063 return 1; 1064 } 1065 } 1066 pserialize_read_exit(s); 1067 1068 /* 1069 * If the default router list is empty, all addresses are regarded 1070 * as on-link, and thus, as a neighbor. 1071 */ 1072 ND6_RLOCK(); 1073 if (ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV && 1074 ND_DEFROUTER_LIST_EMPTY() && nd6_defifindex == ifp->if_index) { 1075 ND6_UNLOCK(); 1076 return 1; 1077 } 1078 ND6_UNLOCK(); 1079 1080 return 0; 1081 } 1082 1083 /* 1084 * Detect if a given IPv6 address identifies a neighbor on a given link. 1085 * XXX: should take care of the destination of a p2p link? 1086 */ 1087 int 1088 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 1089 { 1090 struct nd_prefix *pr; 1091 struct llentry *ln; 1092 struct rtentry *rt; 1093 1094 /* 1095 * A link-local address is always a neighbor. 1096 * XXX: a link does not necessarily specify a single interface. 1097 */ 1098 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { 1099 struct sockaddr_in6 sin6_copy; 1100 u_int32_t zone; 1101 1102 /* 1103 * We need sin6_copy since sa6_recoverscope() may modify the 1104 * content (XXX). 1105 */ 1106 sin6_copy = *addr; 1107 if (sa6_recoverscope(&sin6_copy)) 1108 return 0; /* XXX: should be impossible */ 1109 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) 1110 return 0; 1111 if (sin6_copy.sin6_scope_id == zone) 1112 return 1; 1113 else 1114 return 0; 1115 } 1116 1117 /* 1118 * If the address matches one of our on-link prefixes, it should be a 1119 * neighbor. 1120 */ 1121 ND6_RLOCK(); 1122 ND_PREFIX_LIST_FOREACH(pr) { 1123 if (pr->ndpr_ifp != ifp) 1124 continue; 1125 1126 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) 1127 continue; 1128 1129 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 1130 &addr->sin6_addr, &pr->ndpr_mask)) { 1131 ND6_UNLOCK(); 1132 return 1; 1133 } 1134 } 1135 1136 /* 1137 * If the default router list is empty, all addresses are regarded 1138 * as on-link, and thus, as a neighbor. 1139 * XXX: we restrict the condition to hosts, because routers usually do 1140 * not have the "default router list". 1141 */ 1142 if (!ip6_forwarding && ND_DEFROUTER_LIST_EMPTY() && 1143 nd6_defifindex == ifp->if_index) { 1144 ND6_UNLOCK(); 1145 return 1; 1146 } 1147 ND6_UNLOCK(); 1148 1149 if (nd6_is_new_addr_neighbor(addr, ifp)) 1150 return 1; 1151 1152 /* 1153 * Even if the address matches none of our addresses, it might be 1154 * in the neighbor cache or a connected route. 1155 */ 1156 ln = nd6_lookup(&addr->sin6_addr, ifp, false); 1157 if (ln != NULL) { 1158 LLE_RUNLOCK(ln); 1159 return 1; 1160 } 1161 1162 rt = rtalloc1(sin6tocsa(addr), 0); 1163 if (rt == NULL) 1164 return 0; 1165 1166 if ((rt->rt_flags & RTF_CONNECTED) && (rt->rt_ifp == ifp 1167 #if NBRIDGE > 0 1168 || rt->rt_ifp->if_bridge == ifp->if_bridge 1169 #endif 1170 #if NCARP > 0 1171 || (ifp->if_type == IFT_CARP && rt->rt_ifp == ifp->if_carpdev) || 1172 (rt->rt_ifp->if_type == IFT_CARP && rt->rt_ifp->if_carpdev == ifp)|| 1173 (ifp->if_type == IFT_CARP && rt->rt_ifp->if_type == IFT_CARP && 1174 rt->rt_ifp->if_carpdev == ifp->if_carpdev) 1175 #endif 1176 )) { 1177 rt_unref(rt); 1178 return 1; 1179 } 1180 rt_unref(rt); 1181 1182 return 0; 1183 } 1184 1185 /* 1186 * Free an nd6 llinfo entry. 1187 * Since the function would cause significant changes in the kernel, DO NOT 1188 * make it global, unless you have a strong reason for the change, and are sure 1189 * that the change is safe. 1190 */ 1191 static void 1192 nd6_free(struct llentry *ln, int gc) 1193 { 1194 struct nd_defrouter *dr; 1195 struct ifnet *ifp; 1196 struct in6_addr *in6; 1197 1198 KASSERT(ln != NULL); 1199 LLE_WLOCK_ASSERT(ln); 1200 1201 ifp = ln->lle_tbl->llt_ifp; 1202 in6 = &ln->r_l3addr.addr6; 1203 /* 1204 * we used to have pfctlinput(PRC_HOSTDEAD) here. 1205 * even though it is not harmful, it was not really necessary. 1206 */ 1207 1208 if (!ip6_forwarding) { 1209 ND6_WLOCK(); 1210 dr = nd6_defrouter_lookup(in6, ifp); 1211 1212 if (dr != NULL && dr->expire && 1213 ln->ln_state == ND6_LLINFO_STALE && gc) { 1214 /* 1215 * If the reason for the deletion is just garbage 1216 * collection, and the neighbor is an active default 1217 * router, do not delete it. Instead, reset the GC 1218 * timer using the router's lifetime. 1219 * Simply deleting the entry would affect default 1220 * router selection, which is not necessarily a good 1221 * thing, especially when we're using router preference 1222 * values. 1223 * XXX: the check for ln_state would be redundant, 1224 * but we intentionally keep it just in case. 1225 */ 1226 if (dr->expire > time_uptime) 1227 nd6_llinfo_settimer(ln, 1228 (dr->expire - time_uptime) * hz); 1229 else 1230 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 1231 ND6_UNLOCK(); 1232 LLE_WUNLOCK(ln); 1233 return; 1234 } 1235 1236 if (ln->ln_router || dr) { 1237 /* 1238 * We need to unlock to avoid a LOR with nd6_rt_flush() 1239 * with the rnh and for the calls to 1240 * nd6_pfxlist_onlink_check() and nd6_defrouter_select() in the 1241 * block further down for calls into nd6_lookup(). 1242 * We still hold a ref. 1243 */ 1244 LLE_WUNLOCK(ln); 1245 1246 /* 1247 * nd6_rt_flush must be called whether or not the neighbor 1248 * is in the Default Router List. 1249 * See a corresponding comment in nd6_na_input(). 1250 */ 1251 nd6_rt_flush(in6, ifp); 1252 } 1253 1254 if (dr) { 1255 /* 1256 * Unreachablity of a router might affect the default 1257 * router selection and on-link detection of advertised 1258 * prefixes. 1259 */ 1260 1261 /* 1262 * Temporarily fake the state to choose a new default 1263 * router and to perform on-link determination of 1264 * prefixes correctly. 1265 * Below the state will be set correctly, 1266 * or the entry itself will be deleted. 1267 */ 1268 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1269 1270 /* 1271 * Since nd6_defrouter_select() does not affect the 1272 * on-link determination and MIP6 needs the check 1273 * before the default router selection, we perform 1274 * the check now. 1275 */ 1276 nd6_pfxlist_onlink_check(); 1277 1278 /* 1279 * refresh default router list 1280 */ 1281 nd6_defrouter_select(); 1282 } 1283 1284 #ifdef __FreeBSD__ 1285 /* 1286 * If this entry was added by an on-link redirect, remove the 1287 * corresponding host route. 1288 */ 1289 if (ln->la_flags & LLE_REDIRECT) 1290 nd6_free_redirect(ln); 1291 #endif 1292 ND6_UNLOCK(); 1293 1294 if (ln->ln_router || dr) 1295 LLE_WLOCK(ln); 1296 } 1297 1298 /* 1299 * Save to unlock. We still hold an extra reference and will not 1300 * free(9) in llentry_free() if someone else holds one as well. 1301 */ 1302 LLE_WUNLOCK(ln); 1303 IF_AFDATA_LOCK(ifp); 1304 LLE_WLOCK(ln); 1305 1306 lltable_free_entry(LLTABLE6(ifp), ln); 1307 1308 IF_AFDATA_UNLOCK(ifp); 1309 } 1310 1311 /* 1312 * Upper-layer reachability hint for Neighbor Unreachability Detection. 1313 * 1314 * XXX cost-effective methods? 1315 */ 1316 void 1317 nd6_nud_hint(struct rtentry *rt) 1318 { 1319 struct llentry *ln; 1320 struct ifnet *ifp; 1321 1322 if (rt == NULL) 1323 return; 1324 1325 ifp = rt->rt_ifp; 1326 ln = nd6_lookup(&(satocsin6(rt_getkey(rt)))->sin6_addr, ifp, true); 1327 if (ln == NULL) 1328 return; 1329 1330 if (ln->ln_state < ND6_LLINFO_REACHABLE) 1331 goto done; 1332 1333 /* 1334 * if we get upper-layer reachability confirmation many times, 1335 * it is possible we have false information. 1336 */ 1337 ln->ln_byhint++; 1338 if (ln->ln_byhint > nd6_maxnudhint) 1339 goto done; 1340 1341 ln->ln_state = ND6_LLINFO_REACHABLE; 1342 if (!ND6_LLINFO_PERMANENT(ln)) 1343 nd6_llinfo_settimer(ln, ND_IFINFO(rt->rt_ifp)->reachable * hz); 1344 1345 done: 1346 LLE_WUNLOCK(ln); 1347 1348 return; 1349 } 1350 1351 struct gc_args { 1352 int gc_entries; 1353 const struct in6_addr *skip_in6; 1354 }; 1355 1356 static int 1357 nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg) 1358 { 1359 struct gc_args *args = farg; 1360 int *n = &args->gc_entries; 1361 const struct in6_addr *skip_in6 = args->skip_in6; 1362 1363 if (*n <= 0) 1364 return 0; 1365 1366 if (ND6_LLINFO_PERMANENT(ln)) 1367 return 0; 1368 1369 if (IN6_ARE_ADDR_EQUAL(&ln->r_l3addr.addr6, skip_in6)) 1370 return 0; 1371 1372 LLE_WLOCK(ln); 1373 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 1374 ln->ln_state = ND6_LLINFO_STALE; 1375 else 1376 ln->ln_state = ND6_LLINFO_PURGE; 1377 nd6_llinfo_settimer(ln, 0); 1378 LLE_WUNLOCK(ln); 1379 1380 (*n)--; 1381 return 0; 1382 } 1383 1384 static void 1385 nd6_gc_neighbors(struct lltable *llt, const struct in6_addr *in6) 1386 { 1387 1388 if (ip6_neighborgcthresh >= 0 && 1389 lltable_get_entry_count(llt) >= ip6_neighborgcthresh) { 1390 struct gc_args gc_args = {10, in6}; 1391 /* 1392 * XXX entries that are "less recently used" should be 1393 * freed first. 1394 */ 1395 lltable_foreach_lle(llt, nd6_purge_entry, &gc_args); 1396 } 1397 } 1398 1399 void 1400 nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info) 1401 { 1402 struct sockaddr *gate = rt->rt_gateway; 1403 struct ifnet *ifp = rt->rt_ifp; 1404 uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen; 1405 struct ifaddr *ifa; 1406 1407 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1408 1409 if (req == RTM_LLINFO_UPD) { 1410 int rc; 1411 struct in6_addr *in6; 1412 struct in6_addr in6_all; 1413 int anycast; 1414 1415 if ((ifa = info->rti_ifa) == NULL) 1416 return; 1417 1418 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1419 anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST; 1420 1421 in6_all = in6addr_linklocal_allnodes; 1422 if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) { 1423 log(LOG_ERR, "%s: failed to set scope %s " 1424 "(errno=%d)\n", __func__, if_name(ifp), rc); 1425 return; 1426 } 1427 1428 /* XXX don't set Override for proxy addresses */ 1429 nd6_na_output(ifa->ifa_ifp, &in6_all, in6, 1430 (anycast ? 0 : ND_NA_FLAG_OVERRIDE) 1431 #if 0 1432 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) 1433 #endif 1434 , 1, NULL); 1435 return; 1436 } 1437 1438 if ((rt->rt_flags & RTF_GATEWAY) != 0) 1439 return; 1440 1441 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) { 1442 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1443 /* 1444 * This is probably an interface direct route for a link 1445 * which does not need neighbor caches (e.g. fe80::%lo0/64). 1446 * We do not need special treatment below for such a route. 1447 * Moreover, the RTF_LLINFO flag which would be set below 1448 * would annoy the ndp(8) command. 1449 */ 1450 return; 1451 } 1452 1453 switch (req) { 1454 case RTM_ADD: { 1455 int s; 1456 1457 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1458 /* 1459 * There is no backward compatibility :) 1460 * 1461 * if ((rt->rt_flags & RTF_HOST) == 0 && 1462 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1463 * rt->rt_flags |= RTF_CLONING; 1464 */ 1465 /* XXX should move to route.c? */ 1466 if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) { 1467 union { 1468 struct sockaddr sa; 1469 struct sockaddr_dl sdl; 1470 struct sockaddr_storage ss; 1471 } u; 1472 /* 1473 * Case 1: This route should come from a route to 1474 * interface (RTF_CLONING case) or the route should be 1475 * treated as on-link but is currently not 1476 * (RTF_LLINFO && ln == NULL case). 1477 */ 1478 if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), 1479 ifp->if_index, ifp->if_type, 1480 NULL, namelen, NULL, addrlen) == NULL) { 1481 printf("%s.%d: sockaddr_dl_init(, %zu, ) " 1482 "failed on %s\n", __func__, __LINE__, 1483 sizeof(u.ss), if_name(ifp)); 1484 } 1485 rt_setgate(rt, &u.sa); 1486 gate = rt->rt_gateway; 1487 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1488 if (gate == NULL) { 1489 log(LOG_ERR, 1490 "%s: rt_setgate failed on %s\n", __func__, 1491 if_name(ifp)); 1492 break; 1493 } 1494 1495 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1496 if ((rt->rt_flags & RTF_CONNECTED) != 0) 1497 break; 1498 } 1499 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1500 /* 1501 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here. 1502 * We don't do that here since llinfo is not ready yet. 1503 * 1504 * There are also couple of other things to be discussed: 1505 * - unsolicited NA code needs improvement beforehand 1506 * - RFC2461 says we MAY send multicast unsolicited NA 1507 * (7.2.6 paragraph 4), however, it also says that we 1508 * SHOULD provide a mechanism to prevent multicast NA storm. 1509 * we don't have anything like it right now. 1510 * note that the mechanism needs a mutual agreement 1511 * between proxies, which means that we need to implement 1512 * a new protocol, or a new kludge. 1513 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA. 1514 * we need to check ip6forwarding before sending it. 1515 * (or should we allow proxy ND configuration only for 1516 * routers? there's no mention about proxy ND from hosts) 1517 */ 1518 #if 0 1519 /* XXX it does not work */ 1520 if (rt->rt_flags & RTF_ANNOUNCE) 1521 nd6_na_output(ifp, 1522 &satocsin6(rt_getkey(rt))->sin6_addr, 1523 &satocsin6(rt_getkey(rt))->sin6_addr, 1524 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0, 1525 1, NULL); 1526 #endif 1527 1528 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { 1529 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1530 /* 1531 * Address resolution isn't necessary for a point to 1532 * point link, so we can skip this test for a p2p link. 1533 */ 1534 if (gate->sa_family != AF_LINK || 1535 gate->sa_len < 1536 sockaddr_dl_measure(namelen, addrlen)) { 1537 log(LOG_DEBUG, 1538 "nd6_rtrequest: bad gateway value: %s\n", 1539 if_name(ifp)); 1540 break; 1541 } 1542 satosdl(gate)->sdl_type = ifp->if_type; 1543 satosdl(gate)->sdl_index = ifp->if_index; 1544 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1545 } 1546 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1547 1548 /* 1549 * When called from rt_ifa_addlocal, we cannot depend on that 1550 * the address (rt_getkey(rt)) exits in the address list of the 1551 * interface. So check RTF_LOCAL instead. 1552 */ 1553 if (rt->rt_flags & RTF_LOCAL) { 1554 if (nd6_useloopback) 1555 rt->rt_ifp = lo0ifp; /* XXX */ 1556 break; 1557 } 1558 1559 /* 1560 * check if rt_getkey(rt) is an address assigned 1561 * to the interface. 1562 */ 1563 s = pserialize_read_enter(); 1564 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, 1565 &satocsin6(rt_getkey(rt))->sin6_addr); 1566 if (ifa != NULL) { 1567 if (nd6_useloopback) { 1568 rt->rt_ifp = lo0ifp; /* XXX */ 1569 /* 1570 * Make sure rt_ifa be equal to the ifaddr 1571 * corresponding to the address. 1572 * We need this because when we refer 1573 * rt_ifa->ia6_flags in ip6_input, we assume 1574 * that the rt_ifa points to the address instead 1575 * of the loopback address. 1576 */ 1577 if (ifa != rt->rt_ifa) 1578 rt_replace_ifa(rt, ifa); 1579 } 1580 } else if (rt->rt_flags & RTF_ANNOUNCE) { 1581 /* join solicited node multicast for proxy ND */ 1582 if (ifp->if_flags & IFF_MULTICAST) { 1583 struct in6_addr llsol; 1584 int error; 1585 1586 llsol = satocsin6(rt_getkey(rt))->sin6_addr; 1587 llsol.s6_addr32[0] = htonl(0xff020000); 1588 llsol.s6_addr32[1] = 0; 1589 llsol.s6_addr32[2] = htonl(1); 1590 llsol.s6_addr8[12] = 0xff; 1591 if (in6_setscope(&llsol, ifp, NULL)) 1592 goto out; 1593 if (!in6_addmulti(&llsol, ifp, &error, 0)) { 1594 nd6log(LOG_ERR, "%s: failed to join " 1595 "%s (errno=%d)\n", if_name(ifp), 1596 ip6_sprintf(&llsol), error); 1597 } 1598 } 1599 } 1600 out: 1601 pserialize_read_exit(s); 1602 /* 1603 * If we have too many cache entries, initiate immediate 1604 * purging for some entries. 1605 */ 1606 if (rt->rt_ifp != NULL) 1607 nd6_gc_neighbors(LLTABLE6(rt->rt_ifp), NULL); 1608 break; 1609 } 1610 1611 case RTM_DELETE: 1612 /* leave from solicited node multicast for proxy ND */ 1613 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 && 1614 (ifp->if_flags & IFF_MULTICAST) != 0) { 1615 struct in6_addr llsol; 1616 struct in6_multi *in6m; 1617 1618 llsol = satocsin6(rt_getkey(rt))->sin6_addr; 1619 llsol.s6_addr32[0] = htonl(0xff020000); 1620 llsol.s6_addr32[1] = 0; 1621 llsol.s6_addr32[2] = htonl(1); 1622 llsol.s6_addr8[12] = 0xff; 1623 if (in6_setscope(&llsol, ifp, NULL) == 0) { 1624 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 1625 if (in6m) 1626 in6_delmulti(in6m); 1627 } 1628 } 1629 break; 1630 } 1631 } 1632 1633 int 1634 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp) 1635 { 1636 struct in6_drlist *drl = (struct in6_drlist *)data; 1637 struct in6_oprlist *oprl = (struct in6_oprlist *)data; 1638 struct in6_ndireq *ndi = (struct in6_ndireq *)data; 1639 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; 1640 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; 1641 struct nd_defrouter *dr; 1642 struct nd_prefix *pr; 1643 int i = 0, error = 0; 1644 1645 switch (cmd) { 1646 case SIOCGDRLST_IN6: 1647 /* 1648 * obsolete API, use sysctl under net.inet6.icmp6 1649 */ 1650 memset(drl, 0, sizeof(*drl)); 1651 ND6_RLOCK(); 1652 ND_DEFROUTER_LIST_FOREACH(dr) { 1653 if (i >= DRLSTSIZ) 1654 break; 1655 drl->defrouter[i].rtaddr = dr->rtaddr; 1656 in6_clearscope(&drl->defrouter[i].rtaddr); 1657 1658 drl->defrouter[i].flags = dr->flags; 1659 drl->defrouter[i].rtlifetime = dr->rtlifetime; 1660 drl->defrouter[i].expire = dr->expire ? 1661 time_mono_to_wall(dr->expire) : 0; 1662 drl->defrouter[i].if_index = dr->ifp->if_index; 1663 i++; 1664 } 1665 ND6_UNLOCK(); 1666 break; 1667 case SIOCGPRLST_IN6: 1668 /* 1669 * obsolete API, use sysctl under net.inet6.icmp6 1670 * 1671 * XXX the structure in6_prlist was changed in backward- 1672 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6, 1673 * in6_prlist is used for nd6_sysctl() - fill_prlist(). 1674 */ 1675 /* 1676 * XXX meaning of fields, especialy "raflags", is very 1677 * differnet between RA prefix list and RR/static prefix list. 1678 * how about separating ioctls into two? 1679 */ 1680 memset(oprl, 0, sizeof(*oprl)); 1681 ND6_RLOCK(); 1682 ND_PREFIX_LIST_FOREACH(pr) { 1683 struct nd_pfxrouter *pfr; 1684 int j; 1685 1686 if (i >= PRLSTSIZ) 1687 break; 1688 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr; 1689 oprl->prefix[i].raflags = pr->ndpr_raf; 1690 oprl->prefix[i].prefixlen = pr->ndpr_plen; 1691 oprl->prefix[i].vltime = pr->ndpr_vltime; 1692 oprl->prefix[i].pltime = pr->ndpr_pltime; 1693 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index; 1694 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1695 oprl->prefix[i].expire = 0; 1696 else { 1697 time_t maxexpire; 1698 1699 /* XXX: we assume time_t is signed. */ 1700 maxexpire = (-1) & 1701 ~((time_t)1 << 1702 ((sizeof(maxexpire) * 8) - 1)); 1703 if (pr->ndpr_vltime < 1704 maxexpire - pr->ndpr_lastupdate) { 1705 time_t expire; 1706 expire = pr->ndpr_lastupdate + 1707 pr->ndpr_vltime; 1708 oprl->prefix[i].expire = expire ? 1709 time_mono_to_wall(expire) : 0; 1710 } else 1711 oprl->prefix[i].expire = maxexpire; 1712 } 1713 1714 j = 0; 1715 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 1716 if (j < DRLSTSIZ) { 1717 #define RTRADDR oprl->prefix[i].advrtr[j] 1718 RTRADDR = pfr->router->rtaddr; 1719 in6_clearscope(&RTRADDR); 1720 #undef RTRADDR 1721 } 1722 j++; 1723 } 1724 oprl->prefix[i].advrtrs = j; 1725 oprl->prefix[i].origin = PR_ORIG_RA; 1726 1727 i++; 1728 } 1729 ND6_UNLOCK(); 1730 1731 break; 1732 case OSIOCGIFINFO_IN6: 1733 #define ND ndi->ndi 1734 /* XXX: old ndp(8) assumes a positive value for linkmtu. */ 1735 memset(&ND, 0, sizeof(ND)); 1736 ND.linkmtu = IN6_LINKMTU(ifp); 1737 ND.maxmtu = ND_IFINFO(ifp)->maxmtu; 1738 ND.basereachable = ND_IFINFO(ifp)->basereachable; 1739 ND.reachable = ND_IFINFO(ifp)->reachable; 1740 ND.retrans = ND_IFINFO(ifp)->retrans; 1741 ND.flags = ND_IFINFO(ifp)->flags; 1742 ND.recalctm = ND_IFINFO(ifp)->recalctm; 1743 ND.chlim = ND_IFINFO(ifp)->chlim; 1744 break; 1745 case SIOCGIFINFO_IN6: 1746 ND = *ND_IFINFO(ifp); 1747 break; 1748 case SIOCSIFINFO_IN6: 1749 /* 1750 * used to change host variables from userland. 1751 * intented for a use on router to reflect RA configurations. 1752 */ 1753 /* 0 means 'unspecified' */ 1754 if (ND.linkmtu != 0) { 1755 if (ND.linkmtu < IPV6_MMTU || 1756 ND.linkmtu > IN6_LINKMTU(ifp)) { 1757 error = EINVAL; 1758 break; 1759 } 1760 ND_IFINFO(ifp)->linkmtu = ND.linkmtu; 1761 } 1762 1763 if (ND.basereachable != 0) { 1764 int obasereachable = ND_IFINFO(ifp)->basereachable; 1765 1766 ND_IFINFO(ifp)->basereachable = ND.basereachable; 1767 if (ND.basereachable != obasereachable) 1768 ND_IFINFO(ifp)->reachable = 1769 ND_COMPUTE_RTIME(ND.basereachable); 1770 } 1771 if (ND.retrans != 0) 1772 ND_IFINFO(ifp)->retrans = ND.retrans; 1773 if (ND.chlim != 0) 1774 ND_IFINFO(ifp)->chlim = ND.chlim; 1775 /* FALLTHROUGH */ 1776 case SIOCSIFINFO_FLAGS: 1777 { 1778 struct ifaddr *ifa; 1779 struct in6_ifaddr *ia; 1780 int s; 1781 1782 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1783 !(ND.flags & ND6_IFF_IFDISABLED)) 1784 { 1785 /* 1786 * If the interface is marked as ND6_IFF_IFDISABLED and 1787 * has a link-local address with IN6_IFF_DUPLICATED, 1788 * do not clear ND6_IFF_IFDISABLED. 1789 * See RFC 4862, section 5.4.5. 1790 */ 1791 int duplicated_linklocal = 0; 1792 1793 s = pserialize_read_enter(); 1794 IFADDR_READER_FOREACH(ifa, ifp) { 1795 if (ifa->ifa_addr->sa_family != AF_INET6) 1796 continue; 1797 ia = (struct in6_ifaddr *)ifa; 1798 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) && 1799 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) 1800 { 1801 duplicated_linklocal = 1; 1802 break; 1803 } 1804 } 1805 pserialize_read_exit(s); 1806 1807 if (duplicated_linklocal) { 1808 ND.flags |= ND6_IFF_IFDISABLED; 1809 log(LOG_ERR, "Cannot enable an interface" 1810 " with a link-local address marked" 1811 " duplicate.\n"); 1812 } else { 1813 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED; 1814 if (ifp->if_flags & IFF_UP) 1815 in6_if_up(ifp); 1816 } 1817 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1818 (ND.flags & ND6_IFF_IFDISABLED)) { 1819 int bound = curlwp_bind(); 1820 /* Mark all IPv6 addresses as tentative. */ 1821 1822 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 1823 s = pserialize_read_enter(); 1824 IFADDR_READER_FOREACH(ifa, ifp) { 1825 struct psref psref; 1826 if (ifa->ifa_addr->sa_family != AF_INET6) 1827 continue; 1828 ifa_acquire(ifa, &psref); 1829 pserialize_read_exit(s); 1830 1831 nd6_dad_stop(ifa); 1832 1833 ia = (struct in6_ifaddr *)ifa; 1834 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1835 1836 s = pserialize_read_enter(); 1837 ifa_release(ifa, &psref); 1838 } 1839 pserialize_read_exit(s); 1840 curlwp_bindx(bound); 1841 } 1842 1843 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) { 1844 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) { 1845 /* auto_linklocal 0->1 transition */ 1846 1847 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL; 1848 in6_ifattach(ifp, NULL); 1849 } else if (!(ND.flags & ND6_IFF_IFDISABLED) && 1850 ifp->if_flags & IFF_UP) 1851 { 1852 /* 1853 * When the IF already has 1854 * ND6_IFF_AUTO_LINKLOCAL, no link-local 1855 * address is assigned, and IFF_UP, try to 1856 * assign one. 1857 */ 1858 int haslinklocal = 0; 1859 1860 s = pserialize_read_enter(); 1861 IFADDR_READER_FOREACH(ifa, ifp) { 1862 if (ifa->ifa_addr->sa_family !=AF_INET6) 1863 continue; 1864 ia = (struct in6_ifaddr *)ifa; 1865 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){ 1866 haslinklocal = 1; 1867 break; 1868 } 1869 } 1870 pserialize_read_exit(s); 1871 if (!haslinklocal) 1872 in6_ifattach(ifp, NULL); 1873 } 1874 } 1875 } 1876 ND_IFINFO(ifp)->flags = ND.flags; 1877 break; 1878 #undef ND 1879 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ 1880 /* sync kernel routing table with the default router list */ 1881 ND6_WLOCK(); 1882 nd6_defrouter_reset(); 1883 nd6_defrouter_select(); 1884 ND6_UNLOCK(); 1885 break; 1886 case SIOCSPFXFLUSH_IN6: 1887 { 1888 /* flush all the prefix advertised by routers */ 1889 struct nd_prefix *pfx, *next; 1890 1891 restart: 1892 ND6_WLOCK(); 1893 ND_PREFIX_LIST_FOREACH_SAFE(pfx, next) { 1894 struct in6_ifaddr *ia, *ia_next; 1895 int _s; 1896 1897 if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr)) 1898 continue; /* XXX */ 1899 1900 /* do we really have to remove addresses as well? */ 1901 _s = pserialize_read_enter(); 1902 for (ia = IN6_ADDRLIST_READER_FIRST(); ia; 1903 ia = ia_next) { 1904 /* ia might be removed. keep the next ptr. */ 1905 ia_next = IN6_ADDRLIST_READER_NEXT(ia); 1906 1907 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1908 continue; 1909 1910 if (ia->ia6_ndpr == pfx) { 1911 pserialize_read_exit(_s); 1912 ND6_UNLOCK(); 1913 /* XXX NOMPSAFE? */ 1914 /* in6_purgeaddr may destroy pfx. */ 1915 in6_purgeaddr(&ia->ia_ifa); 1916 goto restart; 1917 } 1918 } 1919 pserialize_read_exit(_s); 1920 1921 KASSERT(pfx->ndpr_refcnt == 0); 1922 nd6_prelist_remove(pfx); 1923 } 1924 ND6_UNLOCK(); 1925 break; 1926 } 1927 case SIOCSRTRFLUSH_IN6: 1928 { 1929 /* flush all the default routers */ 1930 struct nd_defrouter *drtr, *next; 1931 1932 ND6_WLOCK(); 1933 nd6_defrouter_reset(); 1934 ND_DEFROUTER_LIST_FOREACH_SAFE(drtr, next) { 1935 nd6_defrtrlist_del(drtr, NULL); 1936 } 1937 nd6_defrouter_select(); 1938 ND6_UNLOCK(); 1939 break; 1940 } 1941 case SIOCGNBRINFO_IN6: 1942 { 1943 struct llentry *ln; 1944 struct in6_addr nb_addr = nbi->addr; /* make local for safety */ 1945 1946 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0) 1947 return error; 1948 1949 ln = nd6_lookup(&nb_addr, ifp, false); 1950 if (ln == NULL) { 1951 error = EINVAL; 1952 break; 1953 } 1954 nbi->state = ln->ln_state; 1955 nbi->asked = ln->ln_asked; 1956 nbi->isrouter = ln->ln_router; 1957 nbi->expire = ln->ln_expire ? 1958 time_mono_to_wall(ln->ln_expire) : 0; 1959 LLE_RUNLOCK(ln); 1960 1961 break; 1962 } 1963 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1964 ndif->ifindex = nd6_defifindex; 1965 break; 1966 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1967 return nd6_setdefaultiface(ndif->ifindex); 1968 } 1969 return error; 1970 } 1971 1972 void 1973 nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp) 1974 { 1975 struct mbuf *m_hold, *m_hold_next; 1976 struct sockaddr_in6 sin6; 1977 1978 LLE_WLOCK_ASSERT(ln); 1979 1980 sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0); 1981 1982 m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0; 1983 1984 LLE_WUNLOCK(ln); 1985 for (; m_hold != NULL; m_hold = m_hold_next) { 1986 m_hold_next = m_hold->m_nextpkt; 1987 m_hold->m_nextpkt = NULL; 1988 1989 /* 1990 * we assume ifp is not a p2p here, so 1991 * just set the 2nd argument as the 1992 * 1st one. 1993 */ 1994 nd6_output(ifp, ifp, m_hold, &sin6, NULL); 1995 } 1996 LLE_WLOCK(ln); 1997 } 1998 1999 /* 2000 * Create neighbor cache entry and cache link-layer address, 2001 * on reception of inbound ND6 packets. (RS/RA/NS/redirect) 2002 */ 2003 void 2004 nd6_cache_lladdr( 2005 struct ifnet *ifp, 2006 struct in6_addr *from, 2007 char *lladdr, 2008 int lladdrlen, 2009 int type, /* ICMP6 type */ 2010 int code /* type dependent information */ 2011 ) 2012 { 2013 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 2014 struct llentry *ln = NULL; 2015 int is_newentry; 2016 int do_update; 2017 int olladdr; 2018 int llchange; 2019 int newstate = 0; 2020 uint16_t router = 0; 2021 2022 KASSERT(ifp != NULL); 2023 KASSERT(from != NULL); 2024 2025 /* nothing must be updated for unspecified address */ 2026 if (IN6_IS_ADDR_UNSPECIFIED(from)) 2027 return; 2028 2029 /* 2030 * Validation about ifp->if_addrlen and lladdrlen must be done in 2031 * the caller. 2032 * 2033 * XXX If the link does not have link-layer adderss, what should 2034 * we do? (ifp->if_addrlen == 0) 2035 * Spec says nothing in sections for RA, RS and NA. There's small 2036 * description on it in NS section (RFC 2461 7.2.3). 2037 */ 2038 2039 ln = nd6_lookup(from, ifp, true); 2040 if (ln == NULL) { 2041 #if 0 2042 /* nothing must be done if there's no lladdr */ 2043 if (!lladdr || !lladdrlen) 2044 return NULL; 2045 #endif 2046 2047 ln = nd6_create(from, ifp); 2048 is_newentry = 1; 2049 } else { 2050 /* do nothing if static ndp is set */ 2051 if (ln->la_flags & LLE_STATIC) { 2052 LLE_WUNLOCK(ln); 2053 return; 2054 } 2055 is_newentry = 0; 2056 } 2057 2058 if (ln == NULL) 2059 return; 2060 2061 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0; 2062 if (olladdr && lladdr) { 2063 llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen); 2064 } else 2065 llchange = 0; 2066 2067 /* 2068 * newentry olladdr lladdr llchange (*=record) 2069 * 0 n n -- (1) 2070 * 0 y n -- (2) 2071 * 0 n y -- (3) * STALE 2072 * 0 y y n (4) * 2073 * 0 y y y (5) * STALE 2074 * 1 -- n -- (6) NOSTATE(= PASSIVE) 2075 * 1 -- y -- (7) * STALE 2076 */ 2077 2078 if (lladdr) { /* (3-5) and (7) */ 2079 /* 2080 * Record source link-layer address 2081 * XXX is it dependent to ifp->if_type? 2082 */ 2083 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen); 2084 ln->la_flags |= LLE_VALID; 2085 } 2086 2087 if (!is_newentry) { 2088 if ((!olladdr && lladdr) || /* (3) */ 2089 (olladdr && lladdr && llchange)) { /* (5) */ 2090 do_update = 1; 2091 newstate = ND6_LLINFO_STALE; 2092 } else /* (1-2,4) */ 2093 do_update = 0; 2094 } else { 2095 do_update = 1; 2096 if (lladdr == NULL) /* (6) */ 2097 newstate = ND6_LLINFO_NOSTATE; 2098 else /* (7) */ 2099 newstate = ND6_LLINFO_STALE; 2100 } 2101 2102 if (do_update) { 2103 /* 2104 * Update the state of the neighbor cache. 2105 */ 2106 ln->ln_state = newstate; 2107 2108 if (ln->ln_state == ND6_LLINFO_STALE) { 2109 /* 2110 * XXX: since nd6_output() below will cause 2111 * state tansition to DELAY and reset the timer, 2112 * we must set the timer now, although it is actually 2113 * meaningless. 2114 */ 2115 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 2116 2117 nd6_llinfo_release_pkts(ln, ifp); 2118 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 2119 /* probe right away */ 2120 nd6_llinfo_settimer((void *)ln, 0); 2121 } 2122 } 2123 2124 /* 2125 * ICMP6 type dependent behavior. 2126 * 2127 * NS: clear IsRouter if new entry 2128 * RS: clear IsRouter 2129 * RA: set IsRouter if there's lladdr 2130 * redir: clear IsRouter if new entry 2131 * 2132 * RA case, (1): 2133 * The spec says that we must set IsRouter in the following cases: 2134 * - If lladdr exist, set IsRouter. This means (1-5). 2135 * - If it is old entry (!newentry), set IsRouter. This means (7). 2136 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 2137 * A quetion arises for (1) case. (1) case has no lladdr in the 2138 * neighbor cache, this is similar to (6). 2139 * This case is rare but we figured that we MUST NOT set IsRouter. 2140 * 2141 * newentry olladdr lladdr llchange NS RS RA redir 2142 * D R 2143 * 0 n n -- (1) c ? s 2144 * 0 y n -- (2) c s s 2145 * 0 n y -- (3) c s s 2146 * 0 y y n (4) c s s 2147 * 0 y y y (5) c s s 2148 * 1 -- n -- (6) c c c s 2149 * 1 -- y -- (7) c c s c s 2150 * 2151 * (c=clear s=set) 2152 */ 2153 switch (type & 0xff) { 2154 case ND_NEIGHBOR_SOLICIT: 2155 /* 2156 * New entry must have is_router flag cleared. 2157 */ 2158 if (is_newentry) /* (6-7) */ 2159 ln->ln_router = 0; 2160 break; 2161 case ND_REDIRECT: 2162 /* 2163 * If the icmp is a redirect to a better router, always set the 2164 * is_router flag. Otherwise, if the entry is newly created, 2165 * clear the flag. [RFC 2461, sec 8.3] 2166 */ 2167 if (code == ND_REDIRECT_ROUTER) 2168 ln->ln_router = 1; 2169 else if (is_newentry) /* (6-7) */ 2170 ln->ln_router = 0; 2171 break; 2172 case ND_ROUTER_SOLICIT: 2173 /* 2174 * is_router flag must always be cleared. 2175 */ 2176 ln->ln_router = 0; 2177 break; 2178 case ND_ROUTER_ADVERT: 2179 /* 2180 * Mark an entry with lladdr as a router. 2181 */ 2182 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */ 2183 (is_newentry && lladdr)) { /* (7) */ 2184 ln->ln_router = 1; 2185 } 2186 break; 2187 } 2188 2189 #if 0 2190 /* XXX should we send rtmsg as it used to be? */ 2191 if (do_update) 2192 rt_newmsg(RTM_CHANGE, rt); /* tell user process */ 2193 #endif 2194 2195 if (ln != NULL) { 2196 router = ln->ln_router; 2197 LLE_WUNLOCK(ln); 2198 } 2199 2200 /* 2201 * If we have too many cache entries, initiate immediate 2202 * purging for some entries. 2203 */ 2204 if (is_newentry) 2205 nd6_gc_neighbors(LLTABLE6(ifp), &ln->r_l3addr.addr6); 2206 2207 /* 2208 * When the link-layer address of a router changes, select the 2209 * best router again. In particular, when the neighbor entry is newly 2210 * created, it might affect the selection policy. 2211 * Question: can we restrict the first condition to the "is_newentry" 2212 * case? 2213 * XXX: when we hear an RA from a new router with the link-layer 2214 * address option, nd6_defrouter_select() is called twice, since 2215 * defrtrlist_update called the function as well. However, I believe 2216 * we can compromise the overhead, since it only happens the first 2217 * time. 2218 * XXX: although nd6_defrouter_select() should not have a bad effect 2219 * for those are not autoconfigured hosts, we explicitly avoid such 2220 * cases for safety. 2221 */ 2222 if (do_update && router && !ip6_forwarding && 2223 nd6_accepts_rtadv(ndi)) { 2224 ND6_WLOCK(); 2225 nd6_defrouter_select(); 2226 ND6_UNLOCK(); 2227 } 2228 } 2229 2230 static void 2231 nd6_slowtimo(void *ignored_arg) 2232 { 2233 struct nd_ifinfo *nd6if; 2234 struct ifnet *ifp; 2235 int s; 2236 2237 #ifndef NET_MPSAFE 2238 mutex_enter(softnet_lock); 2239 KERNEL_LOCK(1, NULL); 2240 #endif 2241 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 2242 nd6_slowtimo, NULL); 2243 2244 s = pserialize_read_enter(); 2245 IFNET_READER_FOREACH(ifp) { 2246 nd6if = ND_IFINFO(ifp); 2247 if (nd6if->basereachable && /* already initialized */ 2248 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 2249 /* 2250 * Since reachable time rarely changes by router 2251 * advertisements, we SHOULD insure that a new random 2252 * value gets recomputed at least once every few hours. 2253 * (RFC 2461, 6.3.4) 2254 */ 2255 nd6if->recalctm = nd6_recalc_reachtm_interval; 2256 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); 2257 } 2258 } 2259 pserialize_read_exit(s); 2260 2261 #ifndef NET_MPSAFE 2262 KERNEL_UNLOCK_ONE(NULL); 2263 mutex_exit(softnet_lock); 2264 #endif 2265 } 2266 2267 int 2268 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m, 2269 const struct sockaddr_in6 *dst, struct rtentry *rt) 2270 { 2271 #define senderr(e) { error = (e); goto bad;} 2272 struct llentry *ln = NULL; 2273 int error = 0; 2274 bool created = false; 2275 2276 if (rt != NULL) { 2277 error = rt_check_reject_route(rt, ifp); 2278 if (error != 0) { 2279 m_freem(m); 2280 return error; 2281 } 2282 } 2283 2284 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr)) 2285 goto sendpkt; 2286 2287 if (nd6_need_cache(ifp) == 0) 2288 goto sendpkt; 2289 2290 if (rt != NULL && (rt->rt_flags & RTF_GATEWAY) != 0) { 2291 struct sockaddr_in6 *gw6 = satosin6(rt->rt_gateway); 2292 int s; 2293 2294 /* XXX remain the check to keep the original behavior. */ 2295 /* 2296 * We skip link-layer address resolution and NUD 2297 * if the gateway is not a neighbor from ND point 2298 * of view, regardless of the value of nd_ifinfo.flags. 2299 * The second condition is a bit tricky; we skip 2300 * if the gateway is our own address, which is 2301 * sometimes used to install a route to a p2p link. 2302 */ 2303 s = pserialize_read_enter(); 2304 if (!nd6_is_addr_neighbor(gw6, ifp) || 2305 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) { 2306 /* 2307 * We allow this kind of tricky route only 2308 * when the outgoing interface is p2p. 2309 * XXX: we may need a more generic rule here. 2310 */ 2311 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 2312 pserialize_read_exit(s); 2313 senderr(EHOSTUNREACH); 2314 } 2315 2316 pserialize_read_exit(s); 2317 goto sendpkt; 2318 } 2319 pserialize_read_exit(s); 2320 } 2321 2322 /* 2323 * Address resolution or Neighbor Unreachability Detection 2324 * for the next hop. 2325 * At this point, the destination of the packet must be a unicast 2326 * or an anycast address(i.e. not a multicast). 2327 */ 2328 2329 /* Look up the neighbor cache for the nexthop */ 2330 ln = nd6_lookup(&dst->sin6_addr, ifp, true); 2331 if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp)) { 2332 /* 2333 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 2334 * the condition below is not very efficient. But we believe 2335 * it is tolerable, because this should be a rare case. 2336 */ 2337 ln = nd6_create(&dst->sin6_addr, ifp); 2338 if (ln != NULL) 2339 created = true; 2340 } 2341 2342 if (ln == NULL) { 2343 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 && 2344 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) { 2345 log(LOG_DEBUG, 2346 "nd6_output: can't allocate llinfo for %s " 2347 "(ln=%p, rt=%p)\n", 2348 ip6_sprintf(&dst->sin6_addr), ln, rt); 2349 senderr(EIO); /* XXX: good error? */ 2350 } 2351 goto sendpkt; /* send anyway */ 2352 } 2353 2354 LLE_WLOCK_ASSERT(ln); 2355 2356 /* We don't have to do link-layer address resolution on a p2p link. */ 2357 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 2358 ln->ln_state < ND6_LLINFO_REACHABLE) { 2359 ln->ln_state = ND6_LLINFO_STALE; 2360 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 2361 } 2362 2363 /* 2364 * The first time we send a packet to a neighbor whose entry is 2365 * STALE, we have to change the state to DELAY and a sets a timer to 2366 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 2367 * neighbor unreachability detection on expiration. 2368 * (RFC 2461 7.3.3) 2369 */ 2370 if (ln->ln_state == ND6_LLINFO_STALE) { 2371 ln->ln_asked = 0; 2372 ln->ln_state = ND6_LLINFO_DELAY; 2373 nd6_llinfo_settimer(ln, nd6_delay * hz); 2374 } 2375 2376 /* 2377 * If the neighbor cache entry has a state other than INCOMPLETE 2378 * (i.e. its link-layer address is already resolved), just 2379 * send the packet. 2380 */ 2381 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 2382 goto sendpkt; 2383 2384 /* 2385 * There is a neighbor cache entry, but no ethernet address 2386 * response yet. Append this latest packet to the end of the 2387 * packet queue in the mbuf, unless the number of the packet 2388 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen, 2389 * the oldest packet in the queue will be removed. 2390 */ 2391 if (ln->ln_state == ND6_LLINFO_NOSTATE) 2392 ln->ln_state = ND6_LLINFO_INCOMPLETE; 2393 if (ln->ln_hold) { 2394 struct mbuf *m_hold; 2395 int i; 2396 2397 i = 0; 2398 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) { 2399 i++; 2400 if (m_hold->m_nextpkt == NULL) { 2401 m_hold->m_nextpkt = m; 2402 break; 2403 } 2404 } 2405 while (i >= nd6_maxqueuelen) { 2406 m_hold = ln->ln_hold; 2407 ln->ln_hold = ln->ln_hold->m_nextpkt; 2408 m_freem(m_hold); 2409 i--; 2410 } 2411 } else { 2412 ln->ln_hold = m; 2413 } 2414 2415 /* 2416 * If there has been no NS for the neighbor after entering the 2417 * INCOMPLETE state, send the first solicitation. 2418 */ 2419 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) { 2420 struct in6_addr src, *psrc; 2421 2422 ln->ln_asked++; 2423 nd6_llinfo_settimer(ln, ND_IFINFO(ifp)->retrans * hz / 1000); 2424 psrc = nd6_llinfo_get_holdsrc(ln, &src); 2425 LLE_WUNLOCK(ln); 2426 ln = NULL; 2427 nd6_ns_output(ifp, NULL, &dst->sin6_addr, psrc, 0); 2428 } else { 2429 /* We did the lookup so we need to do the unlock here. */ 2430 LLE_WUNLOCK(ln); 2431 } 2432 2433 error = 0; 2434 goto exit; 2435 2436 sendpkt: 2437 /* discard the packet if IPv6 operation is disabled on the interface */ 2438 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) { 2439 error = ENETDOWN; /* better error? */ 2440 goto bad; 2441 } 2442 2443 if (ln != NULL) 2444 LLE_WUNLOCK(ln); 2445 2446 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2447 error = if_output_lock(ifp, origifp, m, sin6tocsa(dst), rt); 2448 else 2449 error = if_output_lock(ifp, ifp, m, sin6tocsa(dst), rt); 2450 goto exit; 2451 2452 bad: 2453 if (m != NULL) 2454 m_freem(m); 2455 exit: 2456 if (created) 2457 nd6_gc_neighbors(LLTABLE6(ifp), &dst->sin6_addr); 2458 2459 return error; 2460 #undef senderr 2461 } 2462 2463 int 2464 nd6_need_cache(struct ifnet *ifp) 2465 { 2466 /* 2467 * XXX: we currently do not make neighbor cache on any interface 2468 * other than ARCnet, Ethernet, FDDI and GIF. 2469 * 2470 * RFC2893 says: 2471 * - unidirectional tunnels needs no ND 2472 */ 2473 switch (ifp->if_type) { 2474 case IFT_ARCNET: 2475 case IFT_ETHER: 2476 case IFT_FDDI: 2477 case IFT_IEEE1394: 2478 case IFT_CARP: 2479 case IFT_GIF: /* XXX need more cases? */ 2480 case IFT_PPP: 2481 case IFT_TUNNEL: 2482 return 1; 2483 default: 2484 return 0; 2485 } 2486 } 2487 2488 /* 2489 * Add pernament ND6 link-layer record for given 2490 * interface address. 2491 * 2492 * Very similar to IPv4 arp_ifinit(), but: 2493 * 1) IPv6 DAD is performed in different place 2494 * 2) It is called by IPv6 protocol stack in contrast to 2495 * arp_ifinit() which is typically called in SIOCSIFADDR 2496 * driver ioctl handler. 2497 * 2498 */ 2499 int 2500 nd6_add_ifa_lle(struct in6_ifaddr *ia) 2501 { 2502 struct ifnet *ifp; 2503 struct llentry *ln; 2504 2505 ifp = ia->ia_ifa.ifa_ifp; 2506 if (nd6_need_cache(ifp) == 0) 2507 return 0; 2508 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 2509 ia->ia_ifa.ifa_flags |= RTF_CONNECTED; 2510 2511 IF_AFDATA_WLOCK(ifp); 2512 ln = lla_create(LLTABLE6(ifp), LLE_IFADDR | LLE_EXCLUSIVE, 2513 sin6tosa(&ia->ia_addr)); 2514 IF_AFDATA_WUNLOCK(ifp); 2515 if (ln == NULL) 2516 return ENOBUFS; 2517 2518 ln->la_expire = 0; /* for IPv6 this means permanent */ 2519 ln->ln_state = ND6_LLINFO_REACHABLE; 2520 2521 LLE_WUNLOCK(ln); 2522 return 0; 2523 } 2524 2525 /* 2526 * Removes ALL lle records for interface address prefix. 2527 * XXXME: That's probably not we really want to do, we need 2528 * to remove address record only and keep other records 2529 * until we determine if given prefix is really going 2530 * to be removed. 2531 */ 2532 void 2533 nd6_rem_ifa_lle(struct in6_ifaddr *ia) 2534 { 2535 struct sockaddr_in6 mask, addr; 2536 2537 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 2538 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 2539 lltable_prefix_free(AF_INET6, sin6tosa(&addr), sin6tosa(&mask), 2540 LLE_STATIC); 2541 } 2542 2543 int 2544 nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt, 2545 struct mbuf *m, const struct sockaddr *dst, uint8_t *lldst, 2546 size_t dstsize) 2547 { 2548 struct llentry *ln; 2549 2550 if (m->m_flags & M_MCAST) { 2551 switch (ifp->if_type) { 2552 case IFT_ETHER: 2553 case IFT_FDDI: 2554 ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr, 2555 lldst); 2556 return 1; 2557 case IFT_IEEE1394: 2558 memcpy(lldst, ifp->if_broadcastaddr, 2559 MIN(dstsize, ifp->if_addrlen)); 2560 return 1; 2561 case IFT_ARCNET: 2562 *lldst = 0; 2563 return 1; 2564 default: 2565 m_freem(m); 2566 return 0; 2567 } 2568 } 2569 2570 /* 2571 * the entry should have been created in nd6_store_lladdr 2572 */ 2573 ln = nd6_lookup(&satocsin6(dst)->sin6_addr, ifp, false); 2574 if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) { 2575 if (ln != NULL) 2576 LLE_RUNLOCK(ln); 2577 /* this could happen, if we could not allocate memory */ 2578 m_freem(m); 2579 return 0; 2580 } 2581 2582 /* XXX llentry should have addrlen? */ 2583 #if 0 2584 sdl = satocsdl(rt->rt_gateway); 2585 if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) { 2586 char sbuf[INET6_ADDRSTRLEN]; 2587 char dbuf[LINK_ADDRSTRLEN]; 2588 /* this should be impossible, but we bark here for debugging */ 2589 printf("%s: sdl_alen == %" PRIu8 ", if=%s, dst=%s, sdl=%s\n", 2590 __func__, sdl->sdl_alen, if_name(ifp), 2591 IN6_PRINT(sbuf, &satocsin6(dst)->sin6_addr), 2592 DL_PRINT(dbuf, &sdl->sdl_addr)); 2593 m_freem(m); 2594 return 0; 2595 } 2596 #endif 2597 2598 memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen)); 2599 2600 LLE_RUNLOCK(ln); 2601 2602 return 1; 2603 } 2604 2605 static void 2606 clear_llinfo_pqueue(struct llentry *ln) 2607 { 2608 struct mbuf *m_hold, *m_hold_next; 2609 2610 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) { 2611 m_hold_next = m_hold->m_nextpkt; 2612 m_hold->m_nextpkt = NULL; 2613 m_freem(m_hold); 2614 } 2615 2616 ln->ln_hold = NULL; 2617 return; 2618 } 2619 2620 int 2621 nd6_sysctl( 2622 int name, 2623 void *oldp, /* syscall arg, need copyout */ 2624 size_t *oldlenp, 2625 void *newp, /* syscall arg, need copyin */ 2626 size_t newlen 2627 ) 2628 { 2629 void *p; 2630 size_t ol; 2631 int error; 2632 2633 error = 0; 2634 2635 if (newp) 2636 return EPERM; 2637 if (oldp && !oldlenp) 2638 return EINVAL; 2639 ol = oldlenp ? *oldlenp : 0; 2640 2641 if (oldp) { 2642 p = malloc(*oldlenp, M_TEMP, M_WAITOK); 2643 if (p == NULL) 2644 return ENOMEM; 2645 } else 2646 p = NULL; 2647 switch (name) { 2648 case ICMPV6CTL_ND6_DRLIST: 2649 error = fill_drlist(p, oldlenp, ol); 2650 if (!error && p != NULL && oldp != NULL) 2651 error = copyout(p, oldp, *oldlenp); 2652 break; 2653 2654 case ICMPV6CTL_ND6_PRLIST: 2655 error = fill_prlist(p, oldlenp, ol); 2656 if (!error && p != NULL && oldp != NULL) 2657 error = copyout(p, oldp, *oldlenp); 2658 break; 2659 2660 case ICMPV6CTL_ND6_MAXQLEN: 2661 break; 2662 2663 default: 2664 error = ENOPROTOOPT; 2665 break; 2666 } 2667 if (p) 2668 free(p, M_TEMP); 2669 2670 return error; 2671 } 2672 2673 static int 2674 fill_drlist(void *oldp, size_t *oldlenp, size_t ol) 2675 { 2676 int error = 0; 2677 struct in6_defrouter *d = NULL, *de = NULL; 2678 struct nd_defrouter *dr; 2679 size_t l; 2680 2681 if (oldp) { 2682 d = (struct in6_defrouter *)oldp; 2683 de = (struct in6_defrouter *)((char *)oldp + *oldlenp); 2684 } 2685 l = 0; 2686 2687 ND6_RLOCK(); 2688 ND_DEFROUTER_LIST_FOREACH(dr) { 2689 2690 if (oldp && d + 1 <= de) { 2691 memset(d, 0, sizeof(*d)); 2692 sockaddr_in6_init(&d->rtaddr, &dr->rtaddr, 0, 0, 0); 2693 if (sa6_recoverscope(&d->rtaddr)) { 2694 log(LOG_ERR, 2695 "scope error in router list (%s)\n", 2696 ip6_sprintf(&d->rtaddr.sin6_addr)); 2697 /* XXX: press on... */ 2698 } 2699 d->flags = dr->flags; 2700 d->rtlifetime = dr->rtlifetime; 2701 d->expire = dr->expire ? 2702 time_mono_to_wall(dr->expire) : 0; 2703 d->if_index = dr->ifp->if_index; 2704 } 2705 2706 l += sizeof(*d); 2707 if (d) 2708 d++; 2709 } 2710 ND6_UNLOCK(); 2711 2712 if (oldp) { 2713 if (l > ol) 2714 error = ENOMEM; 2715 } 2716 if (oldlenp) 2717 *oldlenp = l; /* (void *)d - (void *)oldp */ 2718 2719 return error; 2720 } 2721 2722 static int 2723 fill_prlist(void *oldp, size_t *oldlenp, size_t ol) 2724 { 2725 int error = 0; 2726 struct nd_prefix *pr; 2727 uint8_t *p = NULL, *ps = NULL; 2728 uint8_t *pe = NULL; 2729 size_t l; 2730 2731 if (oldp) { 2732 ps = p = (uint8_t*)oldp; 2733 pe = (uint8_t*)oldp + *oldlenp; 2734 } 2735 l = 0; 2736 2737 ND6_RLOCK(); 2738 ND_PREFIX_LIST_FOREACH(pr) { 2739 u_short advrtrs; 2740 struct sockaddr_in6 sin6; 2741 struct nd_pfxrouter *pfr; 2742 struct in6_prefix pfx; 2743 2744 if (oldp && p + sizeof(struct in6_prefix) <= pe) 2745 { 2746 memset(&pfx, 0, sizeof(pfx)); 2747 ps = p; 2748 pfx.prefix = pr->ndpr_prefix; 2749 2750 if (sa6_recoverscope(&pfx.prefix)) { 2751 log(LOG_ERR, 2752 "scope error in prefix list (%s)\n", 2753 ip6_sprintf(&pfx.prefix.sin6_addr)); 2754 /* XXX: press on... */ 2755 } 2756 pfx.raflags = pr->ndpr_raf; 2757 pfx.prefixlen = pr->ndpr_plen; 2758 pfx.vltime = pr->ndpr_vltime; 2759 pfx.pltime = pr->ndpr_pltime; 2760 pfx.if_index = pr->ndpr_ifp->if_index; 2761 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2762 pfx.expire = 0; 2763 else { 2764 time_t maxexpire; 2765 2766 /* XXX: we assume time_t is signed. */ 2767 maxexpire = (-1) & 2768 ~((time_t)1 << 2769 ((sizeof(maxexpire) * 8) - 1)); 2770 if (pr->ndpr_vltime < 2771 maxexpire - pr->ndpr_lastupdate) { 2772 pfx.expire = pr->ndpr_lastupdate + 2773 pr->ndpr_vltime; 2774 } else 2775 pfx.expire = maxexpire; 2776 } 2777 pfx.refcnt = pr->ndpr_refcnt; 2778 pfx.flags = pr->ndpr_stateflags; 2779 pfx.origin = PR_ORIG_RA; 2780 2781 p += sizeof(pfx); l += sizeof(pfx); 2782 2783 advrtrs = 0; 2784 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 2785 if (p + sizeof(sin6) > pe) { 2786 advrtrs++; 2787 continue; 2788 } 2789 2790 sockaddr_in6_init(&sin6, &pfr->router->rtaddr, 2791 0, 0, 0); 2792 if (sa6_recoverscope(&sin6)) { 2793 log(LOG_ERR, 2794 "scope error in " 2795 "prefix list (%s)\n", 2796 ip6_sprintf(&pfr->router->rtaddr)); 2797 } 2798 advrtrs++; 2799 memcpy(p, &sin6, sizeof(sin6)); 2800 p += sizeof(sin6); 2801 l += sizeof(sin6); 2802 } 2803 pfx.advrtrs = advrtrs; 2804 memcpy(ps, &pfx, sizeof(pfx)); 2805 } 2806 else { 2807 l += sizeof(pfx); 2808 advrtrs = 0; 2809 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 2810 advrtrs++; 2811 l += sizeof(sin6); 2812 } 2813 } 2814 } 2815 ND6_UNLOCK(); 2816 2817 if (oldp) { 2818 *oldlenp = l; /* (void *)d - (void *)oldp */ 2819 if (l > ol) 2820 error = ENOMEM; 2821 } else 2822 *oldlenp = l; 2823 2824 return error; 2825 } 2826 2827 static int 2828 nd6_setdefaultiface(int ifindex) 2829 { 2830 ifnet_t *ifp; 2831 int error = 0; 2832 int s; 2833 2834 s = pserialize_read_enter(); 2835 ifp = if_byindex(ifindex); 2836 if (ifp == NULL) { 2837 pserialize_read_exit(s); 2838 return EINVAL; 2839 } 2840 if (nd6_defifindex != ifindex) { 2841 nd6_defifindex = ifindex; 2842 nd6_defifp = nd6_defifindex > 0 ? ifp : NULL; 2843 2844 /* 2845 * Our current implementation assumes one-to-one maping between 2846 * interfaces and links, so it would be natural to use the 2847 * default interface as the default link. 2848 */ 2849 scope6_setdefault(nd6_defifp); 2850 } 2851 pserialize_read_exit(s); 2852 2853 return (error); 2854 } 2855