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