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