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