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