1 /* $NetBSD: nd6.c,v 1.206 2016/08/06 20:00:14 roy 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.206 2016/08/06 20:00:14 roy 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 static int 1304 nd6_purge_entry(struct lltable *llt, struct llentry *ln, void *farg) 1305 { 1306 int *n = farg; 1307 1308 if (*n <= 0) 1309 return 0; 1310 1311 if (ND6_LLINFO_PERMANENT(ln)) 1312 return 0; 1313 1314 LLE_WLOCK(ln); 1315 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 1316 ln->ln_state = ND6_LLINFO_STALE; 1317 else 1318 ln->ln_state = ND6_LLINFO_PURGE; 1319 nd6_llinfo_settimer(ln, 0); 1320 LLE_WUNLOCK(ln); 1321 1322 (*n)--; 1323 return 0; 1324 } 1325 1326 static void 1327 nd6_gc_neighbors(struct lltable *llt) 1328 { 1329 int max_gc_entries = 10; 1330 1331 if (ip6_neighborgcthresh >= 0 && 1332 lltable_get_entry_count(llt) >= ip6_neighborgcthresh) { 1333 /* 1334 * XXX entries that are "less recently used" should be 1335 * freed first. 1336 */ 1337 lltable_foreach_lle(llt, nd6_purge_entry, &max_gc_entries); 1338 } 1339 } 1340 1341 void 1342 nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info) 1343 { 1344 struct sockaddr *gate = rt->rt_gateway; 1345 struct ifnet *ifp = rt->rt_ifp; 1346 uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen; 1347 struct ifaddr *ifa; 1348 1349 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1350 1351 if (req == RTM_LLINFO_UPD) { 1352 int rc; 1353 struct in6_addr *in6; 1354 struct in6_addr in6_all; 1355 int anycast; 1356 1357 if ((ifa = info->rti_ifa) == NULL) 1358 return; 1359 1360 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1361 anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST; 1362 1363 in6_all = in6addr_linklocal_allnodes; 1364 if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) { 1365 log(LOG_ERR, "%s: failed to set scope %s " 1366 "(errno=%d)\n", __func__, if_name(ifp), rc); 1367 return; 1368 } 1369 1370 /* XXX don't set Override for proxy addresses */ 1371 nd6_na_output(ifa->ifa_ifp, &in6_all, in6, 1372 (anycast ? 0 : ND_NA_FLAG_OVERRIDE) 1373 #if 0 1374 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) 1375 #endif 1376 , 1, NULL); 1377 return; 1378 } 1379 1380 if ((rt->rt_flags & RTF_GATEWAY) != 0) 1381 return; 1382 1383 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) { 1384 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1385 /* 1386 * This is probably an interface direct route for a link 1387 * which does not need neighbor caches (e.g. fe80::%lo0/64). 1388 * We do not need special treatment below for such a route. 1389 * Moreover, the RTF_LLINFO flag which would be set below 1390 * would annoy the ndp(8) command. 1391 */ 1392 return; 1393 } 1394 1395 switch (req) { 1396 case RTM_ADD: { 1397 int s; 1398 1399 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1400 /* 1401 * There is no backward compatibility :) 1402 * 1403 * if ((rt->rt_flags & RTF_HOST) == 0 && 1404 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1405 * rt->rt_flags |= RTF_CLONING; 1406 */ 1407 /* XXX should move to route.c? */ 1408 if (rt->rt_flags & (RTF_CONNECTED | RTF_LOCAL)) { 1409 union { 1410 struct sockaddr sa; 1411 struct sockaddr_dl sdl; 1412 struct sockaddr_storage ss; 1413 } u; 1414 /* 1415 * Case 1: This route should come from a route to 1416 * interface (RTF_CLONING case) or the route should be 1417 * treated as on-link but is currently not 1418 * (RTF_LLINFO && ln == NULL case). 1419 */ 1420 if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), 1421 ifp->if_index, ifp->if_type, 1422 NULL, namelen, NULL, addrlen) == NULL) { 1423 printf("%s.%d: sockaddr_dl_init(, %zu, ) " 1424 "failed on %s\n", __func__, __LINE__, 1425 sizeof(u.ss), if_name(ifp)); 1426 } 1427 rt_setgate(rt, &u.sa); 1428 gate = rt->rt_gateway; 1429 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1430 if (gate == NULL) { 1431 log(LOG_ERR, 1432 "%s: rt_setgate failed on %s\n", __func__, 1433 if_name(ifp)); 1434 break; 1435 } 1436 1437 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1438 if ((rt->rt_flags & RTF_CONNECTED) != 0) 1439 break; 1440 } 1441 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1442 /* 1443 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here. 1444 * We don't do that here since llinfo is not ready yet. 1445 * 1446 * There are also couple of other things to be discussed: 1447 * - unsolicited NA code needs improvement beforehand 1448 * - RFC2461 says we MAY send multicast unsolicited NA 1449 * (7.2.6 paragraph 4), however, it also says that we 1450 * SHOULD provide a mechanism to prevent multicast NA storm. 1451 * we don't have anything like it right now. 1452 * note that the mechanism needs a mutual agreement 1453 * between proxies, which means that we need to implement 1454 * a new protocol, or a new kludge. 1455 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA. 1456 * we need to check ip6forwarding before sending it. 1457 * (or should we allow proxy ND configuration only for 1458 * routers? there's no mention about proxy ND from hosts) 1459 */ 1460 #if 0 1461 /* XXX it does not work */ 1462 if (rt->rt_flags & RTF_ANNOUNCE) 1463 nd6_na_output(ifp, 1464 &satocsin6(rt_getkey(rt))->sin6_addr, 1465 &satocsin6(rt_getkey(rt))->sin6_addr, 1466 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0, 1467 1, NULL); 1468 #endif 1469 1470 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { 1471 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1472 /* 1473 * Address resolution isn't necessary for a point to 1474 * point link, so we can skip this test for a p2p link. 1475 */ 1476 if (gate->sa_family != AF_LINK || 1477 gate->sa_len < 1478 sockaddr_dl_measure(namelen, addrlen)) { 1479 log(LOG_DEBUG, 1480 "nd6_rtrequest: bad gateway value: %s\n", 1481 if_name(ifp)); 1482 break; 1483 } 1484 satosdl(gate)->sdl_type = ifp->if_type; 1485 satosdl(gate)->sdl_index = ifp->if_index; 1486 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1487 } 1488 RT_DPRINTF("rt_getkey(rt) = %p\n", rt_getkey(rt)); 1489 1490 /* 1491 * When called from rt_ifa_addlocal, we cannot depend on that 1492 * the address (rt_getkey(rt)) exits in the address list of the 1493 * interface. So check RTF_LOCAL instead. 1494 */ 1495 if (rt->rt_flags & RTF_LOCAL) { 1496 if (nd6_useloopback) 1497 rt->rt_ifp = lo0ifp; /* XXX */ 1498 break; 1499 } 1500 1501 /* 1502 * check if rt_getkey(rt) is an address assigned 1503 * to the interface. 1504 */ 1505 s = pserialize_read_enter(); 1506 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, 1507 &satocsin6(rt_getkey(rt))->sin6_addr); 1508 if (ifa != NULL) { 1509 if (nd6_useloopback) { 1510 rt->rt_ifp = lo0ifp; /* XXX */ 1511 /* 1512 * Make sure rt_ifa be equal to the ifaddr 1513 * corresponding to the address. 1514 * We need this because when we refer 1515 * rt_ifa->ia6_flags in ip6_input, we assume 1516 * that the rt_ifa points to the address instead 1517 * of the loopback address. 1518 */ 1519 if (ifa != rt->rt_ifa) 1520 rt_replace_ifa(rt, ifa); 1521 } 1522 } else if (rt->rt_flags & RTF_ANNOUNCE) { 1523 /* join solicited node multicast for proxy ND */ 1524 if (ifp->if_flags & IFF_MULTICAST) { 1525 struct in6_addr llsol; 1526 int error; 1527 1528 llsol = satocsin6(rt_getkey(rt))->sin6_addr; 1529 llsol.s6_addr32[0] = htonl(0xff020000); 1530 llsol.s6_addr32[1] = 0; 1531 llsol.s6_addr32[2] = htonl(1); 1532 llsol.s6_addr8[12] = 0xff; 1533 if (in6_setscope(&llsol, ifp, NULL)) 1534 goto out; 1535 if (!in6_addmulti(&llsol, ifp, &error, 0)) { 1536 nd6log(LOG_ERR, "%s: failed to join " 1537 "%s (errno=%d)\n", if_name(ifp), 1538 ip6_sprintf(&llsol), error); 1539 } 1540 } 1541 } 1542 out: 1543 pserialize_read_exit(s); 1544 /* 1545 * If we have too many cache entries, initiate immediate 1546 * purging for some entries. 1547 */ 1548 if (rt->rt_ifp != NULL) 1549 nd6_gc_neighbors(LLTABLE6(rt->rt_ifp)); 1550 break; 1551 } 1552 1553 case RTM_DELETE: 1554 /* leave from solicited node multicast for proxy ND */ 1555 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 && 1556 (ifp->if_flags & IFF_MULTICAST) != 0) { 1557 struct in6_addr llsol; 1558 struct in6_multi *in6m; 1559 1560 llsol = satocsin6(rt_getkey(rt))->sin6_addr; 1561 llsol.s6_addr32[0] = htonl(0xff020000); 1562 llsol.s6_addr32[1] = 0; 1563 llsol.s6_addr32[2] = htonl(1); 1564 llsol.s6_addr8[12] = 0xff; 1565 if (in6_setscope(&llsol, ifp, NULL) == 0) { 1566 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 1567 if (in6m) 1568 in6_delmulti(in6m); 1569 } 1570 } 1571 break; 1572 } 1573 } 1574 1575 int 1576 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp) 1577 { 1578 struct in6_drlist *drl = (struct in6_drlist *)data; 1579 struct in6_oprlist *oprl = (struct in6_oprlist *)data; 1580 struct in6_ndireq *ndi = (struct in6_ndireq *)data; 1581 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; 1582 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; 1583 struct nd_defrouter *dr; 1584 struct nd_prefix *pr; 1585 int i = 0, error = 0; 1586 int s; 1587 1588 switch (cmd) { 1589 case SIOCGDRLST_IN6: 1590 /* 1591 * obsolete API, use sysctl under net.inet6.icmp6 1592 */ 1593 memset(drl, 0, sizeof(*drl)); 1594 s = splsoftnet(); 1595 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) { 1596 if (i >= DRLSTSIZ) 1597 break; 1598 drl->defrouter[i].rtaddr = dr->rtaddr; 1599 in6_clearscope(&drl->defrouter[i].rtaddr); 1600 1601 drl->defrouter[i].flags = dr->flags; 1602 drl->defrouter[i].rtlifetime = dr->rtlifetime; 1603 drl->defrouter[i].expire = dr->expire ? 1604 time_mono_to_wall(dr->expire) : 0; 1605 drl->defrouter[i].if_index = dr->ifp->if_index; 1606 i++; 1607 } 1608 splx(s); 1609 break; 1610 case SIOCGPRLST_IN6: 1611 /* 1612 * obsolete API, use sysctl under net.inet6.icmp6 1613 * 1614 * XXX the structure in6_prlist was changed in backward- 1615 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6, 1616 * in6_prlist is used for nd6_sysctl() - fill_prlist(). 1617 */ 1618 /* 1619 * XXX meaning of fields, especialy "raflags", is very 1620 * differnet between RA prefix list and RR/static prefix list. 1621 * how about separating ioctls into two? 1622 */ 1623 memset(oprl, 0, sizeof(*oprl)); 1624 s = splsoftnet(); 1625 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 1626 struct nd_pfxrouter *pfr; 1627 int j; 1628 1629 if (i >= PRLSTSIZ) 1630 break; 1631 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr; 1632 oprl->prefix[i].raflags = pr->ndpr_raf; 1633 oprl->prefix[i].prefixlen = pr->ndpr_plen; 1634 oprl->prefix[i].vltime = pr->ndpr_vltime; 1635 oprl->prefix[i].pltime = pr->ndpr_pltime; 1636 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index; 1637 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1638 oprl->prefix[i].expire = 0; 1639 else { 1640 time_t maxexpire; 1641 1642 /* XXX: we assume time_t is signed. */ 1643 maxexpire = (-1) & 1644 ~((time_t)1 << 1645 ((sizeof(maxexpire) * 8) - 1)); 1646 if (pr->ndpr_vltime < 1647 maxexpire - pr->ndpr_lastupdate) { 1648 time_t expire; 1649 expire = pr->ndpr_lastupdate + 1650 pr->ndpr_vltime; 1651 oprl->prefix[i].expire = expire ? 1652 time_mono_to_wall(expire) : 0; 1653 } else 1654 oprl->prefix[i].expire = maxexpire; 1655 } 1656 1657 j = 0; 1658 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 1659 if (j < DRLSTSIZ) { 1660 #define RTRADDR oprl->prefix[i].advrtr[j] 1661 RTRADDR = pfr->router->rtaddr; 1662 in6_clearscope(&RTRADDR); 1663 #undef RTRADDR 1664 } 1665 j++; 1666 } 1667 oprl->prefix[i].advrtrs = j; 1668 oprl->prefix[i].origin = PR_ORIG_RA; 1669 1670 i++; 1671 } 1672 splx(s); 1673 1674 break; 1675 case OSIOCGIFINFO_IN6: 1676 #define ND ndi->ndi 1677 /* XXX: old ndp(8) assumes a positive value for linkmtu. */ 1678 memset(&ND, 0, sizeof(ND)); 1679 ND.linkmtu = IN6_LINKMTU(ifp); 1680 ND.maxmtu = ND_IFINFO(ifp)->maxmtu; 1681 ND.basereachable = ND_IFINFO(ifp)->basereachable; 1682 ND.reachable = ND_IFINFO(ifp)->reachable; 1683 ND.retrans = ND_IFINFO(ifp)->retrans; 1684 ND.flags = ND_IFINFO(ifp)->flags; 1685 ND.recalctm = ND_IFINFO(ifp)->recalctm; 1686 ND.chlim = ND_IFINFO(ifp)->chlim; 1687 break; 1688 case SIOCGIFINFO_IN6: 1689 ND = *ND_IFINFO(ifp); 1690 break; 1691 case SIOCSIFINFO_IN6: 1692 /* 1693 * used to change host variables from userland. 1694 * intented for a use on router to reflect RA configurations. 1695 */ 1696 /* 0 means 'unspecified' */ 1697 if (ND.linkmtu != 0) { 1698 if (ND.linkmtu < IPV6_MMTU || 1699 ND.linkmtu > IN6_LINKMTU(ifp)) { 1700 error = EINVAL; 1701 break; 1702 } 1703 ND_IFINFO(ifp)->linkmtu = ND.linkmtu; 1704 } 1705 1706 if (ND.basereachable != 0) { 1707 int obasereachable = ND_IFINFO(ifp)->basereachable; 1708 1709 ND_IFINFO(ifp)->basereachable = ND.basereachable; 1710 if (ND.basereachable != obasereachable) 1711 ND_IFINFO(ifp)->reachable = 1712 ND_COMPUTE_RTIME(ND.basereachable); 1713 } 1714 if (ND.retrans != 0) 1715 ND_IFINFO(ifp)->retrans = ND.retrans; 1716 if (ND.chlim != 0) 1717 ND_IFINFO(ifp)->chlim = ND.chlim; 1718 /* FALLTHROUGH */ 1719 case SIOCSIFINFO_FLAGS: 1720 { 1721 struct ifaddr *ifa; 1722 struct in6_ifaddr *ia; 1723 1724 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1725 !(ND.flags & ND6_IFF_IFDISABLED)) 1726 { 1727 /* 1728 * If the interface is marked as ND6_IFF_IFDISABLED and 1729 * has a link-local address with IN6_IFF_DUPLICATED, 1730 * do not clear ND6_IFF_IFDISABLED. 1731 * See RFC 4862, section 5.4.5. 1732 */ 1733 int duplicated_linklocal = 0; 1734 1735 s = pserialize_read_enter(); 1736 IFADDR_READER_FOREACH(ifa, ifp) { 1737 if (ifa->ifa_addr->sa_family != AF_INET6) 1738 continue; 1739 ia = (struct in6_ifaddr *)ifa; 1740 if ((ia->ia6_flags & IN6_IFF_DUPLICATED) && 1741 IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))) 1742 { 1743 duplicated_linklocal = 1; 1744 break; 1745 } 1746 } 1747 pserialize_read_exit(s); 1748 1749 if (duplicated_linklocal) { 1750 ND.flags |= ND6_IFF_IFDISABLED; 1751 log(LOG_ERR, "Cannot enable an interface" 1752 " with a link-local address marked" 1753 " duplicate.\n"); 1754 } else { 1755 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IFDISABLED; 1756 if (ifp->if_flags & IFF_UP) 1757 in6_if_up(ifp); 1758 } 1759 } else if (!(ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED) && 1760 (ND.flags & ND6_IFF_IFDISABLED)) { 1761 int bound = curlwp_bind(); 1762 /* Mark all IPv6 addresses as tentative. */ 1763 1764 ND_IFINFO(ifp)->flags |= ND6_IFF_IFDISABLED; 1765 s = pserialize_read_enter(); 1766 IFADDR_READER_FOREACH(ifa, ifp) { 1767 struct psref psref; 1768 if (ifa->ifa_addr->sa_family != AF_INET6) 1769 continue; 1770 ifa_acquire(ifa, &psref); 1771 pserialize_read_exit(s); 1772 1773 nd6_dad_stop(ifa); 1774 1775 ia = (struct in6_ifaddr *)ifa; 1776 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1777 1778 s = pserialize_read_enter(); 1779 ifa_release(ifa, &psref); 1780 } 1781 pserialize_read_exit(s); 1782 curlwp_bindx(bound); 1783 } 1784 1785 if (ND.flags & ND6_IFF_AUTO_LINKLOCAL) { 1786 if (!(ND_IFINFO(ifp)->flags & ND6_IFF_AUTO_LINKLOCAL)) { 1787 /* auto_linklocal 0->1 transition */ 1788 1789 ND_IFINFO(ifp)->flags |= ND6_IFF_AUTO_LINKLOCAL; 1790 in6_ifattach(ifp, NULL); 1791 } else if (!(ND.flags & ND6_IFF_IFDISABLED) && 1792 ifp->if_flags & IFF_UP) 1793 { 1794 /* 1795 * When the IF already has 1796 * ND6_IFF_AUTO_LINKLOCAL, no link-local 1797 * address is assigned, and IFF_UP, try to 1798 * assign one. 1799 */ 1800 int haslinklocal = 0; 1801 1802 s = pserialize_read_enter(); 1803 IFADDR_READER_FOREACH(ifa, ifp) { 1804 if (ifa->ifa_addr->sa_family !=AF_INET6) 1805 continue; 1806 ia = (struct in6_ifaddr *)ifa; 1807 if (IN6_IS_ADDR_LINKLOCAL(IA6_IN6(ia))){ 1808 haslinklocal = 1; 1809 break; 1810 } 1811 } 1812 pserialize_read_exit(s); 1813 if (!haslinklocal) 1814 in6_ifattach(ifp, NULL); 1815 } 1816 } 1817 } 1818 ND_IFINFO(ifp)->flags = ND.flags; 1819 break; 1820 #undef ND 1821 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ 1822 /* sync kernel routing table with the default router list */ 1823 defrouter_reset(); 1824 defrouter_select(); 1825 break; 1826 case SIOCSPFXFLUSH_IN6: 1827 { 1828 /* flush all the prefix advertised by routers */ 1829 struct nd_prefix *pfx, *next; 1830 1831 s = splsoftnet(); 1832 LIST_FOREACH_SAFE(pfx, &nd_prefix, ndpr_entry, next) { 1833 struct in6_ifaddr *ia, *ia_next; 1834 int _s; 1835 1836 if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr)) 1837 continue; /* XXX */ 1838 1839 /* do we really have to remove addresses as well? */ 1840 restart: 1841 _s = pserialize_read_enter(); 1842 for (ia = IN6_ADDRLIST_READER_FIRST(); ia; 1843 ia = ia_next) { 1844 /* ia might be removed. keep the next ptr. */ 1845 ia_next = IN6_ADDRLIST_READER_NEXT(ia); 1846 1847 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1848 continue; 1849 1850 if (ia->ia6_ndpr == pfx) { 1851 pserialize_read_exit(_s); 1852 /* XXX NOMPSAFE? */ 1853 in6_purgeaddr(&ia->ia_ifa); 1854 goto restart; 1855 } 1856 } 1857 pserialize_read_exit(_s); 1858 prelist_remove(pfx); 1859 } 1860 splx(s); 1861 break; 1862 } 1863 case SIOCSRTRFLUSH_IN6: 1864 { 1865 /* flush all the default routers */ 1866 struct nd_defrouter *drtr, *next; 1867 1868 s = splsoftnet(); 1869 defrouter_reset(); 1870 TAILQ_FOREACH_SAFE(drtr, &nd_defrouter, dr_entry, next) { 1871 defrtrlist_del(drtr, NULL); 1872 } 1873 defrouter_select(); 1874 splx(s); 1875 break; 1876 } 1877 case SIOCGNBRINFO_IN6: 1878 { 1879 struct llentry *ln; 1880 struct in6_addr nb_addr = nbi->addr; /* make local for safety */ 1881 1882 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0) 1883 return error; 1884 1885 ln = nd6_lookup(&nb_addr, ifp, false); 1886 if (ln == NULL) { 1887 error = EINVAL; 1888 break; 1889 } 1890 nbi->state = ln->ln_state; 1891 nbi->asked = ln->ln_asked; 1892 nbi->isrouter = ln->ln_router; 1893 nbi->expire = ln->ln_expire ? 1894 time_mono_to_wall(ln->ln_expire) : 0; 1895 LLE_RUNLOCK(ln); 1896 1897 break; 1898 } 1899 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1900 ndif->ifindex = nd6_defifindex; 1901 break; 1902 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1903 return nd6_setdefaultiface(ndif->ifindex); 1904 } 1905 return error; 1906 } 1907 1908 void 1909 nd6_llinfo_release_pkts(struct llentry *ln, struct ifnet *ifp) 1910 { 1911 struct mbuf *m_hold, *m_hold_next; 1912 struct sockaddr_in6 sin6; 1913 1914 LLE_WLOCK_ASSERT(ln); 1915 1916 sockaddr_in6_init(&sin6, &ln->r_l3addr.addr6, 0, 0, 0); 1917 1918 m_hold = ln->la_hold, ln->la_hold = NULL, ln->la_numheld = 0; 1919 1920 LLE_WUNLOCK(ln); 1921 for (; m_hold != NULL; m_hold = m_hold_next) { 1922 m_hold_next = m_hold->m_nextpkt; 1923 m_hold->m_nextpkt = NULL; 1924 1925 /* 1926 * we assume ifp is not a p2p here, so 1927 * just set the 2nd argument as the 1928 * 1st one. 1929 */ 1930 nd6_output(ifp, ifp, m_hold, &sin6, NULL); 1931 } 1932 LLE_WLOCK(ln); 1933 } 1934 1935 /* 1936 * Create neighbor cache entry and cache link-layer address, 1937 * on reception of inbound ND6 packets. (RS/RA/NS/redirect) 1938 */ 1939 void 1940 nd6_cache_lladdr( 1941 struct ifnet *ifp, 1942 struct in6_addr *from, 1943 char *lladdr, 1944 int lladdrlen, 1945 int type, /* ICMP6 type */ 1946 int code /* type dependent information */ 1947 ) 1948 { 1949 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 1950 struct llentry *ln = NULL; 1951 int is_newentry; 1952 int do_update; 1953 int olladdr; 1954 int llchange; 1955 int newstate = 0; 1956 uint16_t router = 0; 1957 1958 KASSERT(ifp != NULL); 1959 KASSERT(from != NULL); 1960 1961 /* nothing must be updated for unspecified address */ 1962 if (IN6_IS_ADDR_UNSPECIFIED(from)) 1963 return; 1964 1965 /* 1966 * Validation about ifp->if_addrlen and lladdrlen must be done in 1967 * the caller. 1968 * 1969 * XXX If the link does not have link-layer adderss, what should 1970 * we do? (ifp->if_addrlen == 0) 1971 * Spec says nothing in sections for RA, RS and NA. There's small 1972 * description on it in NS section (RFC 2461 7.2.3). 1973 */ 1974 1975 ln = nd6_lookup(from, ifp, true); 1976 if (ln == NULL) { 1977 #if 0 1978 /* nothing must be done if there's no lladdr */ 1979 if (!lladdr || !lladdrlen) 1980 return NULL; 1981 #endif 1982 1983 ln = nd6_create(from, ifp); 1984 is_newentry = 1; 1985 } else { 1986 /* do nothing if static ndp is set */ 1987 if (ln->la_flags & LLE_STATIC) { 1988 LLE_WUNLOCK(ln); 1989 return; 1990 } 1991 is_newentry = 0; 1992 } 1993 1994 if (ln == NULL) 1995 return; 1996 1997 olladdr = (ln->la_flags & LLE_VALID) ? 1 : 0; 1998 if (olladdr && lladdr) { 1999 llchange = memcmp(lladdr, &ln->ll_addr, ifp->if_addrlen); 2000 } else 2001 llchange = 0; 2002 2003 /* 2004 * newentry olladdr lladdr llchange (*=record) 2005 * 0 n n -- (1) 2006 * 0 y n -- (2) 2007 * 0 n y -- (3) * STALE 2008 * 0 y y n (4) * 2009 * 0 y y y (5) * STALE 2010 * 1 -- n -- (6) NOSTATE(= PASSIVE) 2011 * 1 -- y -- (7) * STALE 2012 */ 2013 2014 if (lladdr) { /* (3-5) and (7) */ 2015 /* 2016 * Record source link-layer address 2017 * XXX is it dependent to ifp->if_type? 2018 */ 2019 memcpy(&ln->ll_addr, lladdr, ifp->if_addrlen); 2020 ln->la_flags |= LLE_VALID; 2021 } 2022 2023 if (!is_newentry) { 2024 if ((!olladdr && lladdr) || /* (3) */ 2025 (olladdr && lladdr && llchange)) { /* (5) */ 2026 do_update = 1; 2027 newstate = ND6_LLINFO_STALE; 2028 } else /* (1-2,4) */ 2029 do_update = 0; 2030 } else { 2031 do_update = 1; 2032 if (lladdr == NULL) /* (6) */ 2033 newstate = ND6_LLINFO_NOSTATE; 2034 else /* (7) */ 2035 newstate = ND6_LLINFO_STALE; 2036 } 2037 2038 if (do_update) { 2039 /* 2040 * Update the state of the neighbor cache. 2041 */ 2042 ln->ln_state = newstate; 2043 2044 if (ln->ln_state == ND6_LLINFO_STALE) { 2045 /* 2046 * XXX: since nd6_output() below will cause 2047 * state tansition to DELAY and reset the timer, 2048 * we must set the timer now, although it is actually 2049 * meaningless. 2050 */ 2051 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 2052 2053 nd6_llinfo_release_pkts(ln, ifp); 2054 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 2055 /* probe right away */ 2056 nd6_llinfo_settimer((void *)ln, 0); 2057 } 2058 } 2059 2060 /* 2061 * ICMP6 type dependent behavior. 2062 * 2063 * NS: clear IsRouter if new entry 2064 * RS: clear IsRouter 2065 * RA: set IsRouter if there's lladdr 2066 * redir: clear IsRouter if new entry 2067 * 2068 * RA case, (1): 2069 * The spec says that we must set IsRouter in the following cases: 2070 * - If lladdr exist, set IsRouter. This means (1-5). 2071 * - If it is old entry (!newentry), set IsRouter. This means (7). 2072 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 2073 * A quetion arises for (1) case. (1) case has no lladdr in the 2074 * neighbor cache, this is similar to (6). 2075 * This case is rare but we figured that we MUST NOT set IsRouter. 2076 * 2077 * newentry olladdr lladdr llchange NS RS RA redir 2078 * D R 2079 * 0 n n -- (1) c ? s 2080 * 0 y n -- (2) c s s 2081 * 0 n y -- (3) c s s 2082 * 0 y y n (4) c s s 2083 * 0 y y y (5) c s s 2084 * 1 -- n -- (6) c c c s 2085 * 1 -- y -- (7) c c s c s 2086 * 2087 * (c=clear s=set) 2088 */ 2089 switch (type & 0xff) { 2090 case ND_NEIGHBOR_SOLICIT: 2091 /* 2092 * New entry must have is_router flag cleared. 2093 */ 2094 if (is_newentry) /* (6-7) */ 2095 ln->ln_router = 0; 2096 break; 2097 case ND_REDIRECT: 2098 /* 2099 * If the icmp is a redirect to a better router, always set the 2100 * is_router flag. Otherwise, if the entry is newly created, 2101 * clear the flag. [RFC 2461, sec 8.3] 2102 */ 2103 if (code == ND_REDIRECT_ROUTER) 2104 ln->ln_router = 1; 2105 else if (is_newentry) /* (6-7) */ 2106 ln->ln_router = 0; 2107 break; 2108 case ND_ROUTER_SOLICIT: 2109 /* 2110 * is_router flag must always be cleared. 2111 */ 2112 ln->ln_router = 0; 2113 break; 2114 case ND_ROUTER_ADVERT: 2115 /* 2116 * Mark an entry with lladdr as a router. 2117 */ 2118 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */ 2119 (is_newentry && lladdr)) { /* (7) */ 2120 ln->ln_router = 1; 2121 } 2122 break; 2123 } 2124 2125 #if 0 2126 /* XXX should we send rtmsg as it used to be? */ 2127 if (do_update) 2128 rt_newmsg(RTM_CHANGE, rt); /* tell user process */ 2129 #endif 2130 2131 if (ln != NULL) { 2132 router = ln->ln_router; 2133 LLE_WUNLOCK(ln); 2134 } 2135 2136 /* 2137 * If we have too many cache entries, initiate immediate 2138 * purging for some entries. 2139 */ 2140 if (is_newentry) 2141 nd6_gc_neighbors(LLTABLE6(ifp)); 2142 2143 /* 2144 * When the link-layer address of a router changes, select the 2145 * best router again. In particular, when the neighbor entry is newly 2146 * created, it might affect the selection policy. 2147 * Question: can we restrict the first condition to the "is_newentry" 2148 * case? 2149 * XXX: when we hear an RA from a new router with the link-layer 2150 * address option, defrouter_select() is called twice, since 2151 * defrtrlist_update called the function as well. However, I believe 2152 * we can compromise the overhead, since it only happens the first 2153 * time. 2154 * XXX: although defrouter_select() should not have a bad effect 2155 * for those are not autoconfigured hosts, we explicitly avoid such 2156 * cases for safety. 2157 */ 2158 if (do_update && router && !ip6_forwarding && 2159 nd6_accepts_rtadv(ndi)) 2160 defrouter_select(); 2161 } 2162 2163 static void 2164 nd6_slowtimo(void *ignored_arg) 2165 { 2166 struct nd_ifinfo *nd6if; 2167 struct ifnet *ifp; 2168 int s; 2169 2170 mutex_enter(softnet_lock); 2171 KERNEL_LOCK(1, NULL); 2172 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 2173 nd6_slowtimo, NULL); 2174 2175 s = pserialize_read_enter(); 2176 IFNET_READER_FOREACH(ifp) { 2177 nd6if = ND_IFINFO(ifp); 2178 if (nd6if->basereachable && /* already initialized */ 2179 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 2180 /* 2181 * Since reachable time rarely changes by router 2182 * advertisements, we SHOULD insure that a new random 2183 * value gets recomputed at least once every few hours. 2184 * (RFC 2461, 6.3.4) 2185 */ 2186 nd6if->recalctm = nd6_recalc_reachtm_interval; 2187 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); 2188 } 2189 } 2190 pserialize_read_exit(s); 2191 2192 KERNEL_UNLOCK_ONE(NULL); 2193 mutex_exit(softnet_lock); 2194 } 2195 2196 int 2197 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m, 2198 const struct sockaddr_in6 *dst, struct rtentry *rt) 2199 { 2200 #define senderr(e) { error = (e); goto bad;} 2201 struct llentry *ln = NULL; 2202 int error = 0; 2203 bool created = false; 2204 2205 if (rt != NULL) { 2206 error = rt_check_reject_route(rt, ifp); 2207 if (error != 0) { 2208 m_freem(m); 2209 return error; 2210 } 2211 } 2212 2213 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr)) 2214 goto sendpkt; 2215 2216 if (nd6_need_cache(ifp) == 0) 2217 goto sendpkt; 2218 2219 if (rt != NULL && (rt->rt_flags & RTF_GATEWAY) != 0) { 2220 struct sockaddr_in6 *gw6 = satosin6(rt->rt_gateway); 2221 int s; 2222 2223 /* XXX remain the check to keep the original behavior. */ 2224 /* 2225 * We skip link-layer address resolution and NUD 2226 * if the gateway is not a neighbor from ND point 2227 * of view, regardless of the value of nd_ifinfo.flags. 2228 * The second condition is a bit tricky; we skip 2229 * if the gateway is our own address, which is 2230 * sometimes used to install a route to a p2p link. 2231 */ 2232 s = pserialize_read_enter(); 2233 if (!nd6_is_addr_neighbor(gw6, ifp) || 2234 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) { 2235 /* 2236 * We allow this kind of tricky route only 2237 * when the outgoing interface is p2p. 2238 * XXX: we may need a more generic rule here. 2239 */ 2240 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 2241 senderr(EHOSTUNREACH); 2242 2243 pserialize_read_exit(s); 2244 goto sendpkt; 2245 } 2246 pserialize_read_exit(s); 2247 } 2248 2249 /* 2250 * Address resolution or Neighbor Unreachability Detection 2251 * for the next hop. 2252 * At this point, the destination of the packet must be a unicast 2253 * or an anycast address(i.e. not a multicast). 2254 */ 2255 2256 /* Look up the neighbor cache for the nexthop */ 2257 ln = nd6_lookup(&dst->sin6_addr, ifp, true); 2258 if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp)) { 2259 /* 2260 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 2261 * the condition below is not very efficient. But we believe 2262 * it is tolerable, because this should be a rare case. 2263 */ 2264 ln = nd6_create(&dst->sin6_addr, ifp); 2265 if (ln != NULL) 2266 created = true; 2267 } 2268 2269 if (ln == NULL) { 2270 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 && 2271 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) { 2272 log(LOG_DEBUG, 2273 "nd6_output: can't allocate llinfo for %s " 2274 "(ln=%p, rt=%p)\n", 2275 ip6_sprintf(&dst->sin6_addr), ln, rt); 2276 senderr(EIO); /* XXX: good error? */ 2277 } 2278 goto sendpkt; /* send anyway */ 2279 } 2280 2281 LLE_WLOCK_ASSERT(ln); 2282 2283 /* We don't have to do link-layer address resolution on a p2p link. */ 2284 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 2285 ln->ln_state < ND6_LLINFO_REACHABLE) { 2286 ln->ln_state = ND6_LLINFO_STALE; 2287 nd6_llinfo_settimer(ln, nd6_gctimer * hz); 2288 } 2289 2290 /* 2291 * The first time we send a packet to a neighbor whose entry is 2292 * STALE, we have to change the state to DELAY and a sets a timer to 2293 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 2294 * neighbor unreachability detection on expiration. 2295 * (RFC 2461 7.3.3) 2296 */ 2297 if (ln->ln_state == ND6_LLINFO_STALE) { 2298 ln->ln_asked = 0; 2299 ln->ln_state = ND6_LLINFO_DELAY; 2300 nd6_llinfo_settimer(ln, nd6_delay * hz); 2301 } 2302 2303 /* 2304 * If the neighbor cache entry has a state other than INCOMPLETE 2305 * (i.e. its link-layer address is already resolved), just 2306 * send the packet. 2307 */ 2308 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 2309 goto sendpkt; 2310 2311 /* 2312 * There is a neighbor cache entry, but no ethernet address 2313 * response yet. Append this latest packet to the end of the 2314 * packet queue in the mbuf, unless the number of the packet 2315 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen, 2316 * the oldest packet in the queue will be removed. 2317 */ 2318 if (ln->ln_state == ND6_LLINFO_NOSTATE) 2319 ln->ln_state = ND6_LLINFO_INCOMPLETE; 2320 if (ln->ln_hold) { 2321 struct mbuf *m_hold; 2322 int i; 2323 2324 i = 0; 2325 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) { 2326 i++; 2327 if (m_hold->m_nextpkt == NULL) { 2328 m_hold->m_nextpkt = m; 2329 break; 2330 } 2331 } 2332 while (i >= nd6_maxqueuelen) { 2333 m_hold = ln->ln_hold; 2334 ln->ln_hold = ln->ln_hold->m_nextpkt; 2335 m_freem(m_hold); 2336 i--; 2337 } 2338 } else { 2339 ln->ln_hold = m; 2340 } 2341 2342 /* 2343 * If there has been no NS for the neighbor after entering the 2344 * INCOMPLETE state, send the first solicitation. 2345 */ 2346 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) { 2347 struct in6_addr src, *psrc; 2348 2349 ln->ln_asked++; 2350 nd6_llinfo_settimer(ln, ND_IFINFO(ifp)->retrans * hz / 1000); 2351 psrc = nd6_llinfo_get_holdsrc(ln, &src); 2352 LLE_WUNLOCK(ln); 2353 ln = NULL; 2354 nd6_ns_output(ifp, NULL, &dst->sin6_addr, psrc, 0); 2355 } else { 2356 /* We did the lookup so we need to do the unlock here. */ 2357 LLE_WUNLOCK(ln); 2358 } 2359 2360 error = 0; 2361 goto exit; 2362 2363 sendpkt: 2364 /* discard the packet if IPv6 operation is disabled on the interface */ 2365 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) { 2366 error = ENETDOWN; /* better error? */ 2367 goto bad; 2368 } 2369 2370 if (ln != NULL) 2371 LLE_WUNLOCK(ln); 2372 2373 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2374 error = if_output_lock(ifp, origifp, m, sin6tocsa(dst), rt); 2375 else 2376 error = if_output_lock(ifp, ifp, m, sin6tocsa(dst), rt); 2377 goto exit; 2378 2379 bad: 2380 if (m != NULL) 2381 m_freem(m); 2382 exit: 2383 if (created) 2384 nd6_gc_neighbors(LLTABLE6(ifp)); 2385 2386 return error; 2387 #undef senderr 2388 } 2389 2390 int 2391 nd6_need_cache(struct ifnet *ifp) 2392 { 2393 /* 2394 * XXX: we currently do not make neighbor cache on any interface 2395 * other than ARCnet, Ethernet, FDDI and GIF. 2396 * 2397 * RFC2893 says: 2398 * - unidirectional tunnels needs no ND 2399 */ 2400 switch (ifp->if_type) { 2401 case IFT_ARCNET: 2402 case IFT_ETHER: 2403 case IFT_FDDI: 2404 case IFT_IEEE1394: 2405 case IFT_CARP: 2406 case IFT_GIF: /* XXX need more cases? */ 2407 case IFT_PPP: 2408 case IFT_TUNNEL: 2409 return 1; 2410 default: 2411 return 0; 2412 } 2413 } 2414 2415 /* 2416 * Add pernament ND6 link-layer record for given 2417 * interface address. 2418 * 2419 * Very similar to IPv4 arp_ifinit(), but: 2420 * 1) IPv6 DAD is performed in different place 2421 * 2) It is called by IPv6 protocol stack in contrast to 2422 * arp_ifinit() which is typically called in SIOCSIFADDR 2423 * driver ioctl handler. 2424 * 2425 */ 2426 int 2427 nd6_add_ifa_lle(struct in6_ifaddr *ia) 2428 { 2429 struct ifnet *ifp; 2430 struct llentry *ln; 2431 2432 ifp = ia->ia_ifa.ifa_ifp; 2433 if (nd6_need_cache(ifp) == 0) 2434 return 0; 2435 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 2436 ia->ia_ifa.ifa_flags |= RTF_CONNECTED; 2437 2438 IF_AFDATA_WLOCK(ifp); 2439 ln = lla_create(LLTABLE6(ifp), LLE_IFADDR | LLE_EXCLUSIVE, 2440 sin6tosa(&ia->ia_addr)); 2441 IF_AFDATA_WUNLOCK(ifp); 2442 if (ln == NULL) 2443 return ENOBUFS; 2444 2445 ln->la_expire = 0; /* for IPv6 this means permanent */ 2446 ln->ln_state = ND6_LLINFO_REACHABLE; 2447 2448 LLE_WUNLOCK(ln); 2449 return 0; 2450 } 2451 2452 /* 2453 * Removes ALL lle records for interface address prefix. 2454 * XXXME: That's probably not we really want to do, we need 2455 * to remove address record only and keep other records 2456 * until we determine if given prefix is really going 2457 * to be removed. 2458 */ 2459 void 2460 nd6_rem_ifa_lle(struct in6_ifaddr *ia) 2461 { 2462 struct sockaddr_in6 mask, addr; 2463 2464 memcpy(&addr, &ia->ia_addr, sizeof(ia->ia_addr)); 2465 memcpy(&mask, &ia->ia_prefixmask, sizeof(ia->ia_prefixmask)); 2466 lltable_prefix_free(AF_INET6, sin6tosa(&addr), sin6tosa(&mask), 2467 LLE_STATIC); 2468 } 2469 2470 int 2471 nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt, 2472 struct mbuf *m, const struct sockaddr *dst, uint8_t *lldst, 2473 size_t dstsize) 2474 { 2475 struct llentry *ln; 2476 2477 if (m->m_flags & M_MCAST) { 2478 switch (ifp->if_type) { 2479 case IFT_ETHER: 2480 case IFT_FDDI: 2481 ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr, 2482 lldst); 2483 return 1; 2484 case IFT_IEEE1394: 2485 memcpy(lldst, ifp->if_broadcastaddr, 2486 MIN(dstsize, ifp->if_addrlen)); 2487 return 1; 2488 case IFT_ARCNET: 2489 *lldst = 0; 2490 return 1; 2491 default: 2492 m_freem(m); 2493 return 0; 2494 } 2495 } 2496 2497 /* 2498 * the entry should have been created in nd6_store_lladdr 2499 */ 2500 ln = nd6_lookup(&satocsin6(dst)->sin6_addr, ifp, false); 2501 if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) { 2502 if (ln != NULL) 2503 LLE_RUNLOCK(ln); 2504 /* this could happen, if we could not allocate memory */ 2505 m_freem(m); 2506 return 0; 2507 } 2508 2509 /* XXX llentry should have addrlen? */ 2510 #if 0 2511 sdl = satocsdl(rt->rt_gateway); 2512 if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) { 2513 char sbuf[INET6_ADDRSTRLEN]; 2514 char dbuf[LINK_ADDRSTRLEN]; 2515 /* this should be impossible, but we bark here for debugging */ 2516 printf("%s: sdl_alen == %" PRIu8 ", if=%s, dst=%s, sdl=%s\n", 2517 __func__, sdl->sdl_alen, if_name(ifp), 2518 IN6_PRINT(sbuf, &satocsin6(dst)->sin6_addr), 2519 DL_PRINT(dbuf, &sdl->sdl_addr)); 2520 m_freem(m); 2521 return 0; 2522 } 2523 #endif 2524 2525 memcpy(lldst, &ln->ll_addr, MIN(dstsize, ifp->if_addrlen)); 2526 2527 LLE_RUNLOCK(ln); 2528 2529 return 1; 2530 } 2531 2532 static void 2533 clear_llinfo_pqueue(struct llentry *ln) 2534 { 2535 struct mbuf *m_hold, *m_hold_next; 2536 2537 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) { 2538 m_hold_next = m_hold->m_nextpkt; 2539 m_hold->m_nextpkt = NULL; 2540 m_freem(m_hold); 2541 } 2542 2543 ln->ln_hold = NULL; 2544 return; 2545 } 2546 2547 int 2548 nd6_sysctl( 2549 int name, 2550 void *oldp, /* syscall arg, need copyout */ 2551 size_t *oldlenp, 2552 void *newp, /* syscall arg, need copyin */ 2553 size_t newlen 2554 ) 2555 { 2556 void *p; 2557 size_t ol; 2558 int error; 2559 2560 error = 0; 2561 2562 if (newp) 2563 return EPERM; 2564 if (oldp && !oldlenp) 2565 return EINVAL; 2566 ol = oldlenp ? *oldlenp : 0; 2567 2568 if (oldp) { 2569 p = malloc(*oldlenp, M_TEMP, M_WAITOK); 2570 if (p == NULL) 2571 return ENOMEM; 2572 } else 2573 p = NULL; 2574 switch (name) { 2575 case ICMPV6CTL_ND6_DRLIST: 2576 error = fill_drlist(p, oldlenp, ol); 2577 if (!error && p != NULL && oldp != NULL) 2578 error = copyout(p, oldp, *oldlenp); 2579 break; 2580 2581 case ICMPV6CTL_ND6_PRLIST: 2582 error = fill_prlist(p, oldlenp, ol); 2583 if (!error && p != NULL && oldp != NULL) 2584 error = copyout(p, oldp, *oldlenp); 2585 break; 2586 2587 case ICMPV6CTL_ND6_MAXQLEN: 2588 break; 2589 2590 default: 2591 error = ENOPROTOOPT; 2592 break; 2593 } 2594 if (p) 2595 free(p, M_TEMP); 2596 2597 return error; 2598 } 2599 2600 static int 2601 fill_drlist(void *oldp, size_t *oldlenp, size_t ol) 2602 { 2603 int error = 0, s; 2604 struct in6_defrouter *d = NULL, *de = NULL; 2605 struct nd_defrouter *dr; 2606 size_t l; 2607 2608 s = splsoftnet(); 2609 2610 if (oldp) { 2611 d = (struct in6_defrouter *)oldp; 2612 de = (struct in6_defrouter *)((char *)oldp + *oldlenp); 2613 } 2614 l = 0; 2615 2616 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) { 2617 2618 if (oldp && d + 1 <= de) { 2619 memset(d, 0, sizeof(*d)); 2620 sockaddr_in6_init(&d->rtaddr, &dr->rtaddr, 0, 0, 0); 2621 if (sa6_recoverscope(&d->rtaddr)) { 2622 log(LOG_ERR, 2623 "scope error in router list (%s)\n", 2624 ip6_sprintf(&d->rtaddr.sin6_addr)); 2625 /* XXX: press on... */ 2626 } 2627 d->flags = dr->flags; 2628 d->rtlifetime = dr->rtlifetime; 2629 d->expire = dr->expire ? 2630 time_mono_to_wall(dr->expire) : 0; 2631 d->if_index = dr->ifp->if_index; 2632 } 2633 2634 l += sizeof(*d); 2635 if (d) 2636 d++; 2637 } 2638 2639 if (oldp) { 2640 if (l > ol) 2641 error = ENOMEM; 2642 } 2643 if (oldlenp) 2644 *oldlenp = l; /* (void *)d - (void *)oldp */ 2645 2646 splx(s); 2647 2648 return error; 2649 } 2650 2651 static int 2652 fill_prlist(void *oldp, size_t *oldlenp, size_t ol) 2653 { 2654 int error = 0, s; 2655 struct nd_prefix *pr; 2656 uint8_t *p = NULL, *ps = NULL; 2657 uint8_t *pe = NULL; 2658 size_t l; 2659 2660 s = splsoftnet(); 2661 2662 if (oldp) { 2663 ps = p = (uint8_t*)oldp; 2664 pe = (uint8_t*)oldp + *oldlenp; 2665 } 2666 l = 0; 2667 2668 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 2669 u_short advrtrs; 2670 struct sockaddr_in6 sin6; 2671 struct nd_pfxrouter *pfr; 2672 struct in6_prefix pfx; 2673 2674 if (oldp && p + sizeof(struct in6_prefix) <= pe) 2675 { 2676 memset(&pfx, 0, sizeof(pfx)); 2677 ps = p; 2678 pfx.prefix = pr->ndpr_prefix; 2679 2680 if (sa6_recoverscope(&pfx.prefix)) { 2681 log(LOG_ERR, 2682 "scope error in prefix list (%s)\n", 2683 ip6_sprintf(&pfx.prefix.sin6_addr)); 2684 /* XXX: press on... */ 2685 } 2686 pfx.raflags = pr->ndpr_raf; 2687 pfx.prefixlen = pr->ndpr_plen; 2688 pfx.vltime = pr->ndpr_vltime; 2689 pfx.pltime = pr->ndpr_pltime; 2690 pfx.if_index = pr->ndpr_ifp->if_index; 2691 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2692 pfx.expire = 0; 2693 else { 2694 time_t maxexpire; 2695 2696 /* XXX: we assume time_t is signed. */ 2697 maxexpire = (-1) & 2698 ~((time_t)1 << 2699 ((sizeof(maxexpire) * 8) - 1)); 2700 if (pr->ndpr_vltime < 2701 maxexpire - pr->ndpr_lastupdate) { 2702 pfx.expire = pr->ndpr_lastupdate + 2703 pr->ndpr_vltime; 2704 } else 2705 pfx.expire = maxexpire; 2706 } 2707 pfx.refcnt = pr->ndpr_refcnt; 2708 pfx.flags = pr->ndpr_stateflags; 2709 pfx.origin = PR_ORIG_RA; 2710 2711 p += sizeof(pfx); l += sizeof(pfx); 2712 2713 advrtrs = 0; 2714 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 2715 if (p + sizeof(sin6) > pe) { 2716 advrtrs++; 2717 continue; 2718 } 2719 2720 sockaddr_in6_init(&sin6, &pfr->router->rtaddr, 2721 0, 0, 0); 2722 if (sa6_recoverscope(&sin6)) { 2723 log(LOG_ERR, 2724 "scope error in " 2725 "prefix list (%s)\n", 2726 ip6_sprintf(&pfr->router->rtaddr)); 2727 } 2728 advrtrs++; 2729 memcpy(p, &sin6, sizeof(sin6)); 2730 p += sizeof(sin6); 2731 l += sizeof(sin6); 2732 } 2733 pfx.advrtrs = advrtrs; 2734 memcpy(ps, &pfx, sizeof(pfx)); 2735 } 2736 else { 2737 l += sizeof(pfx); 2738 advrtrs = 0; 2739 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 2740 advrtrs++; 2741 l += sizeof(sin6); 2742 } 2743 } 2744 } 2745 2746 if (oldp) { 2747 *oldlenp = l; /* (void *)d - (void *)oldp */ 2748 if (l > ol) 2749 error = ENOMEM; 2750 } else 2751 *oldlenp = l; 2752 2753 splx(s); 2754 2755 return error; 2756 } 2757