1 /* $NetBSD: nd6.c,v 1.139 2011/12/19 11:59:58 drochner 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.139 2011/12/19 11:59:58 drochner Exp $"); 35 36 #include "opt_ipsec.h" 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/callout.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/sockio.h> 46 #include <sys/time.h> 47 #include <sys/kernel.h> 48 #include <sys/protosw.h> 49 #include <sys/errno.h> 50 #include <sys/ioctl.h> 51 #include <sys/syslog.h> 52 #include <sys/queue.h> 53 #include <sys/cprng.h> 54 55 #include <net/if.h> 56 #include <net/if_dl.h> 57 #include <net/if_types.h> 58 #include <net/route.h> 59 #include <net/if_ether.h> 60 #include <net/if_fddi.h> 61 #include <net/if_arc.h> 62 63 #include <netinet/in.h> 64 #include <netinet6/in6_var.h> 65 #include <netinet/ip6.h> 66 #include <netinet6/ip6_var.h> 67 #include <netinet6/scope6_var.h> 68 #include <netinet6/nd6.h> 69 #include <netinet/icmp6.h> 70 #include <netinet6/icmp6_private.h> 71 72 #ifdef KAME_IPSEC 73 #include <netinet6/ipsec.h> 74 #endif 75 76 #include <net/net_osdep.h> 77 78 #define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */ 79 #define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */ 80 81 /* timer values */ 82 int nd6_prune = 1; /* walk list every 1 seconds */ 83 int nd6_delay = 5; /* delay first probe time 5 second */ 84 int nd6_umaxtries = 3; /* maximum unicast query */ 85 int nd6_mmaxtries = 3; /* maximum multicast query */ 86 int nd6_useloopback = 1; /* use loopback interface for local traffic */ 87 int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */ 88 89 /* preventing too many loops in ND option parsing */ 90 int nd6_maxndopt = 10; /* max # of ND options allowed */ 91 92 int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */ 93 94 int nd6_maxqueuelen = 1; /* max # of packets cached in unresolved ND entries */ 95 96 #ifdef ND6_DEBUG 97 int nd6_debug = 1; 98 #else 99 int nd6_debug = 0; 100 #endif 101 102 /* for debugging? */ 103 static int nd6_inuse, nd6_allocated; 104 105 struct llinfo_nd6 llinfo_nd6 = { 106 .ln_prev = &llinfo_nd6, 107 .ln_next = &llinfo_nd6, 108 }; 109 struct nd_drhead nd_defrouter; 110 struct nd_prhead nd_prefix = { 0 }; 111 112 int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL; 113 static const struct sockaddr_in6 all1_sa = { 114 .sin6_family = AF_INET6 115 , .sin6_len = sizeof(struct sockaddr_in6) 116 , .sin6_addr = {.s6_addr = {0xff, 0xff, 0xff, 0xff, 117 0xff, 0xff, 0xff, 0xff, 118 0xff, 0xff, 0xff, 0xff, 119 0xff, 0xff, 0xff, 0xff}} 120 }; 121 122 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *); 123 static void nd6_slowtimo(void *); 124 static int regen_tmpaddr(struct in6_ifaddr *); 125 static struct llinfo_nd6 *nd6_free(struct rtentry *, int); 126 static void nd6_llinfo_timer(void *); 127 static void clear_llinfo_pqueue(struct llinfo_nd6 *); 128 129 callout_t nd6_slowtimo_ch; 130 callout_t nd6_timer_ch; 131 extern callout_t in6_tmpaddrtimer_ch; 132 133 static int fill_drlist(void *, size_t *, size_t); 134 static int fill_prlist(void *, size_t *, size_t); 135 136 MALLOC_DEFINE(M_IP6NDP, "NDP", "IPv6 Neighbour Discovery"); 137 138 void 139 nd6_init(void) 140 { 141 static int nd6_init_done = 0; 142 143 if (nd6_init_done) { 144 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n"); 145 return; 146 } 147 148 /* initialization of the default router list */ 149 TAILQ_INIT(&nd_defrouter); 150 151 nd6_init_done = 1; 152 153 callout_init(&nd6_slowtimo_ch, CALLOUT_MPSAFE); 154 callout_init(&nd6_timer_ch, CALLOUT_MPSAFE); 155 156 /* start timer */ 157 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 158 nd6_slowtimo, NULL); 159 } 160 161 struct nd_ifinfo * 162 nd6_ifattach(struct ifnet *ifp) 163 { 164 struct nd_ifinfo *nd; 165 166 nd = (struct nd_ifinfo *)malloc(sizeof(*nd), M_IP6NDP, M_WAITOK); 167 memset(nd, 0, sizeof(*nd)); 168 169 nd->initialized = 1; 170 171 nd->chlim = IPV6_DEFHLIM; 172 nd->basereachable = REACHABLE_TIME; 173 nd->reachable = ND_COMPUTE_RTIME(nd->basereachable); 174 nd->retrans = RETRANS_TIMER; 175 /* 176 * Note that the default value of ip6_accept_rtadv is 0. 177 * Because we do not set ND6_IFF_OVERRIDE_RTADV here, we won't 178 * accept RAs by default. 179 */ 180 nd->flags = ND6_IFF_PERFORMNUD | ND6_IFF_ACCEPT_RTADV; 181 182 /* XXX: we cannot call nd6_setmtu since ifp is not fully initialized */ 183 nd6_setmtu0(ifp, nd); 184 185 return nd; 186 } 187 188 void 189 nd6_ifdetach(struct nd_ifinfo *nd) 190 { 191 192 free(nd, M_IP6NDP); 193 } 194 195 void 196 nd6_setmtu(struct ifnet *ifp) 197 { 198 nd6_setmtu0(ifp, ND_IFINFO(ifp)); 199 } 200 201 void 202 nd6_setmtu0(struct ifnet *ifp, struct nd_ifinfo *ndi) 203 { 204 u_int32_t omaxmtu; 205 206 omaxmtu = ndi->maxmtu; 207 208 switch (ifp->if_type) { 209 case IFT_ARCNET: 210 ndi->maxmtu = MIN(ARC_PHDS_MAXMTU, ifp->if_mtu); /* RFC2497 */ 211 break; 212 case IFT_FDDI: 213 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); 214 break; 215 default: 216 ndi->maxmtu = ifp->if_mtu; 217 break; 218 } 219 220 /* 221 * Decreasing the interface MTU under IPV6 minimum MTU may cause 222 * undesirable situation. We thus notify the operator of the change 223 * explicitly. The check for omaxmtu is necessary to restrict the 224 * log to the case of changing the MTU, not initializing it. 225 */ 226 if (omaxmtu >= IPV6_MMTU && ndi->maxmtu < IPV6_MMTU) { 227 log(LOG_NOTICE, "nd6_setmtu0: new link MTU on %s (%lu) is too" 228 " small for IPv6 which needs %lu\n", 229 if_name(ifp), (unsigned long)ndi->maxmtu, (unsigned long) 230 IPV6_MMTU); 231 } 232 233 if (ndi->maxmtu > in6_maxmtu) 234 in6_setmaxmtu(); /* check all interfaces just in case */ 235 } 236 237 void 238 nd6_option_init(void *opt, int icmp6len, union nd_opts *ndopts) 239 { 240 241 memset(ndopts, 0, sizeof(*ndopts)); 242 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt; 243 ndopts->nd_opts_last 244 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len); 245 246 if (icmp6len == 0) { 247 ndopts->nd_opts_done = 1; 248 ndopts->nd_opts_search = NULL; 249 } 250 } 251 252 /* 253 * Take one ND option. 254 */ 255 struct nd_opt_hdr * 256 nd6_option(union nd_opts *ndopts) 257 { 258 struct nd_opt_hdr *nd_opt; 259 int olen; 260 261 if (ndopts == NULL) 262 panic("ndopts == NULL in nd6_option"); 263 if (ndopts->nd_opts_last == NULL) 264 panic("uninitialized ndopts in nd6_option"); 265 if (ndopts->nd_opts_search == NULL) 266 return NULL; 267 if (ndopts->nd_opts_done) 268 return NULL; 269 270 nd_opt = ndopts->nd_opts_search; 271 272 /* make sure nd_opt_len is inside the buffer */ 273 if ((void *)&nd_opt->nd_opt_len >= (void *)ndopts->nd_opts_last) { 274 memset(ndopts, 0, sizeof(*ndopts)); 275 return NULL; 276 } 277 278 olen = nd_opt->nd_opt_len << 3; 279 if (olen == 0) { 280 /* 281 * Message validation requires that all included 282 * options have a length that is greater than zero. 283 */ 284 memset(ndopts, 0, sizeof(*ndopts)); 285 return NULL; 286 } 287 288 ndopts->nd_opts_search = (struct nd_opt_hdr *)((char *)nd_opt + olen); 289 if (ndopts->nd_opts_search > ndopts->nd_opts_last) { 290 /* option overruns the end of buffer, invalid */ 291 memset(ndopts, 0, sizeof(*ndopts)); 292 return NULL; 293 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) { 294 /* reached the end of options chain */ 295 ndopts->nd_opts_done = 1; 296 ndopts->nd_opts_search = NULL; 297 } 298 return nd_opt; 299 } 300 301 /* 302 * Parse multiple ND options. 303 * This function is much easier to use, for ND routines that do not need 304 * multiple options of the same type. 305 */ 306 int 307 nd6_options(union nd_opts *ndopts) 308 { 309 struct nd_opt_hdr *nd_opt; 310 int i = 0; 311 312 if (ndopts == NULL) 313 panic("ndopts == NULL in nd6_options"); 314 if (ndopts->nd_opts_last == NULL) 315 panic("uninitialized ndopts in nd6_options"); 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 llinfo_nd6 *ln, long xtick) 387 { 388 int s; 389 390 s = splsoftnet(); 391 392 if (xtick < 0) { 393 ln->ln_expire = 0; 394 ln->ln_ntick = 0; 395 callout_stop(&ln->ln_timer_ch); 396 } else { 397 ln->ln_expire = time_second + xtick / hz; 398 if (xtick > INT_MAX) { 399 ln->ln_ntick = xtick - INT_MAX; 400 callout_reset(&ln->ln_timer_ch, INT_MAX, 401 nd6_llinfo_timer, ln); 402 } else { 403 ln->ln_ntick = 0; 404 callout_reset(&ln->ln_timer_ch, xtick, 405 nd6_llinfo_timer, ln); 406 } 407 } 408 409 splx(s); 410 } 411 412 static void 413 nd6_llinfo_timer(void *arg) 414 { 415 struct llinfo_nd6 *ln; 416 struct rtentry *rt; 417 const struct sockaddr_in6 *dst; 418 struct ifnet *ifp; 419 struct nd_ifinfo *ndi = NULL; 420 421 mutex_enter(softnet_lock); 422 KERNEL_LOCK(1, NULL); 423 424 ln = (struct llinfo_nd6 *)arg; 425 426 if (ln->ln_ntick > 0) { 427 nd6_llinfo_settimer(ln, ln->ln_ntick); 428 KERNEL_UNLOCK_ONE(NULL); 429 mutex_exit(softnet_lock); 430 return; 431 } 432 433 if ((rt = ln->ln_rt) == NULL) 434 panic("ln->ln_rt == NULL"); 435 if ((ifp = rt->rt_ifp) == NULL) 436 panic("ln->ln_rt->rt_ifp == NULL"); 437 ndi = ND_IFINFO(ifp); 438 dst = satocsin6(rt_getkey(rt)); 439 440 /* sanity check */ 441 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln) 442 panic("rt_llinfo(%p) is not equal to ln(%p)", 443 rt->rt_llinfo, ln); 444 if (!dst) 445 panic("dst=0 in nd6_timer(ln=%p)", ln); 446 447 switch (ln->ln_state) { 448 case ND6_LLINFO_INCOMPLETE: 449 if (ln->ln_asked < nd6_mmaxtries) { 450 ln->ln_asked++; 451 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000); 452 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0); 453 } else { 454 struct mbuf *m = ln->ln_hold; 455 if (m) { 456 struct mbuf *m0; 457 458 /* 459 * assuming every packet in ln_hold has 460 * the same IP header 461 */ 462 m0 = m->m_nextpkt; 463 m->m_nextpkt = NULL; 464 icmp6_error2(m, ICMP6_DST_UNREACH, 465 ICMP6_DST_UNREACH_ADDR, 0, rt->rt_ifp); 466 467 ln->ln_hold = m0; 468 clear_llinfo_pqueue(ln); 469 } 470 (void)nd6_free(rt, 0); 471 ln = NULL; 472 } 473 break; 474 case ND6_LLINFO_REACHABLE: 475 if (!ND6_LLINFO_PERMANENT(ln)) { 476 ln->ln_state = ND6_LLINFO_STALE; 477 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz); 478 } 479 break; 480 481 case ND6_LLINFO_STALE: 482 /* Garbage Collection(RFC 2461 5.3) */ 483 if (!ND6_LLINFO_PERMANENT(ln)) { 484 (void)nd6_free(rt, 1); 485 ln = NULL; 486 } 487 break; 488 489 case ND6_LLINFO_DELAY: 490 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) { 491 /* We need NUD */ 492 ln->ln_asked = 1; 493 ln->ln_state = ND6_LLINFO_PROBE; 494 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000); 495 nd6_ns_output(ifp, &dst->sin6_addr, 496 &dst->sin6_addr, ln, 0); 497 } else { 498 ln->ln_state = ND6_LLINFO_STALE; /* XXX */ 499 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz); 500 } 501 break; 502 case ND6_LLINFO_PROBE: 503 if (ln->ln_asked < nd6_umaxtries) { 504 ln->ln_asked++; 505 nd6_llinfo_settimer(ln, (long)ndi->retrans * hz / 1000); 506 nd6_ns_output(ifp, &dst->sin6_addr, 507 &dst->sin6_addr, ln, 0); 508 } else { 509 (void)nd6_free(rt, 0); 510 ln = NULL; 511 } 512 break; 513 } 514 515 KERNEL_UNLOCK_ONE(NULL); 516 mutex_exit(softnet_lock); 517 } 518 519 /* 520 * ND6 timer routine to expire default route list and prefix list 521 */ 522 void 523 nd6_timer(void *ignored_arg) 524 { 525 struct nd_defrouter *next_dr, *dr; 526 struct nd_prefix *next_pr, *pr; 527 struct in6_ifaddr *ia6, *nia6; 528 529 callout_reset(&nd6_timer_ch, nd6_prune * hz, 530 nd6_timer, NULL); 531 532 mutex_enter(softnet_lock); 533 KERNEL_LOCK(1, NULL); 534 535 /* expire default router list */ 536 537 for (dr = TAILQ_FIRST(&nd_defrouter); dr != NULL; dr = next_dr) { 538 next_dr = TAILQ_NEXT(dr, dr_entry); 539 if (dr->expire && dr->expire < time_second) { 540 defrtrlist_del(dr); 541 } 542 } 543 544 /* 545 * expire interface addresses. 546 * in the past the loop was inside prefix expiry processing. 547 * However, from a stricter speci-confrmance standpoint, we should 548 * rather separate address lifetimes and prefix lifetimes. 549 */ 550 addrloop: 551 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) { 552 nia6 = ia6->ia_next; 553 /* check address lifetime */ 554 if (IFA6_IS_INVALID(ia6)) { 555 int regen = 0; 556 557 /* 558 * If the expiring address is temporary, try 559 * regenerating a new one. This would be useful when 560 * we suspended a laptop PC, then turned it on after a 561 * period that could invalidate all temporary 562 * addresses. Although we may have to restart the 563 * loop (see below), it must be after purging the 564 * address. Otherwise, we'd see an infinite loop of 565 * regeneration. 566 */ 567 if (ip6_use_tempaddr && 568 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 569 if (regen_tmpaddr(ia6) == 0) 570 regen = 1; 571 } 572 573 in6_purgeaddr(&ia6->ia_ifa); 574 575 if (regen) 576 goto addrloop; /* XXX: see below */ 577 } else if (IFA6_IS_DEPRECATED(ia6)) { 578 int oldflags = ia6->ia6_flags; 579 580 ia6->ia6_flags |= IN6_IFF_DEPRECATED; 581 582 /* 583 * If a temporary address has just become deprecated, 584 * regenerate a new one if possible. 585 */ 586 if (ip6_use_tempaddr && 587 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 588 (oldflags & IN6_IFF_DEPRECATED) == 0) { 589 590 if (regen_tmpaddr(ia6) == 0) { 591 /* 592 * A new temporary address is 593 * generated. 594 * XXX: this means the address chain 595 * has changed while we are still in 596 * the loop. Although the change 597 * would not cause disaster (because 598 * it's not a deletion, but an 599 * addition,) we'd rather restart the 600 * loop just for safety. Or does this 601 * significantly reduce performance?? 602 */ 603 goto addrloop; 604 } 605 } 606 } else { 607 /* 608 * A new RA might have made a deprecated address 609 * preferred. 610 */ 611 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED; 612 } 613 } 614 615 /* expire prefix list */ 616 for (pr = LIST_FIRST(&nd_prefix); pr != NULL; pr = next_pr) { 617 next_pr = LIST_NEXT(pr, ndpr_entry); 618 /* 619 * check prefix lifetime. 620 * since pltime is just for autoconf, pltime processing for 621 * prefix is not necessary. 622 */ 623 if (pr->ndpr_vltime != ND6_INFINITE_LIFETIME && 624 time_second - pr->ndpr_lastupdate > pr->ndpr_vltime) { 625 626 /* 627 * address expiration and prefix expiration are 628 * separate. NEVER perform in6_purgeaddr here. 629 */ 630 631 prelist_remove(pr); 632 } 633 } 634 635 KERNEL_UNLOCK_ONE(NULL); 636 mutex_exit(softnet_lock); 637 } 638 639 /* ia6: deprecated/invalidated temporary address */ 640 static int 641 regen_tmpaddr(struct in6_ifaddr *ia6) 642 { 643 struct ifaddr *ifa; 644 struct ifnet *ifp; 645 struct in6_ifaddr *public_ifa6 = NULL; 646 647 ifp = ia6->ia_ifa.ifa_ifp; 648 IFADDR_FOREACH(ifa, ifp) { 649 struct in6_ifaddr *it6; 650 651 if (ifa->ifa_addr->sa_family != AF_INET6) 652 continue; 653 654 it6 = (struct in6_ifaddr *)ifa; 655 656 /* ignore no autoconf addresses. */ 657 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 658 continue; 659 660 /* ignore autoconf addresses with different prefixes. */ 661 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) 662 continue; 663 664 /* 665 * Now we are looking at an autoconf address with the same 666 * prefix as ours. If the address is temporary and is still 667 * preferred, do not create another one. It would be rare, but 668 * could happen, for example, when we resume a laptop PC after 669 * a long period. 670 */ 671 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 672 !IFA6_IS_DEPRECATED(it6)) { 673 public_ifa6 = NULL; 674 break; 675 } 676 677 /* 678 * This is a public autoconf address that has the same prefix 679 * as ours. If it is preferred, keep it. We can't break the 680 * loop here, because there may be a still-preferred temporary 681 * address with the prefix. 682 */ 683 if (!IFA6_IS_DEPRECATED(it6)) 684 public_ifa6 = it6; 685 } 686 687 if (public_ifa6 != NULL) { 688 int e; 689 690 /* 691 * Random factor is introduced in the preferred lifetime, so 692 * we do not need additional delay (3rd arg to in6_tmpifadd). 693 */ 694 if ((e = in6_tmpifadd(public_ifa6, 0, 0)) != 0) { 695 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" 696 " tmp addr, errno=%d\n", e); 697 return -1; 698 } 699 return 0; 700 } 701 702 return -1; 703 } 704 705 bool 706 nd6_accepts_rtadv(const struct nd_ifinfo *ndi) 707 { 708 switch (ndi->flags & (ND6_IFF_ACCEPT_RTADV|ND6_IFF_OVERRIDE_RTADV)) { 709 case ND6_IFF_OVERRIDE_RTADV|ND6_IFF_ACCEPT_RTADV: 710 return true; 711 case ND6_IFF_ACCEPT_RTADV: 712 return ip6_accept_rtadv != 0; 713 case ND6_IFF_OVERRIDE_RTADV: 714 case 0: 715 default: 716 return false; 717 } 718 } 719 720 /* 721 * Nuke neighbor cache/prefix/default router management table, right before 722 * ifp goes away. 723 */ 724 void 725 nd6_purge(struct ifnet *ifp) 726 { 727 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 728 struct llinfo_nd6 *ln, *nln; 729 struct nd_defrouter *dr, *ndr; 730 struct nd_prefix *pr, *npr; 731 732 /* 733 * Nuke default router list entries toward ifp. 734 * We defer removal of default router list entries that is installed 735 * in the routing table, in order to keep additional side effects as 736 * small as possible. 737 */ 738 for (dr = TAILQ_FIRST(&nd_defrouter); dr != NULL; dr = ndr) { 739 ndr = TAILQ_NEXT(dr, dr_entry); 740 if (dr->installed) 741 continue; 742 743 if (dr->ifp == ifp) 744 defrtrlist_del(dr); 745 } 746 for (dr = TAILQ_FIRST(&nd_defrouter); dr != NULL; dr = ndr) { 747 ndr = TAILQ_NEXT(dr, dr_entry); 748 if (!dr->installed) 749 continue; 750 751 if (dr->ifp == ifp) 752 defrtrlist_del(dr); 753 } 754 755 /* Nuke prefix list entries toward ifp */ 756 for (pr = LIST_FIRST(&nd_prefix); pr != NULL; pr = npr) { 757 npr = LIST_NEXT(pr, ndpr_entry); 758 if (pr->ndpr_ifp == ifp) { 759 /* 760 * Because if_detach() does *not* release prefixes 761 * while purging addresses the reference count will 762 * still be above zero. We therefore reset it to 763 * make sure that the prefix really gets purged. 764 */ 765 pr->ndpr_refcnt = 0; 766 /* 767 * Previously, pr->ndpr_addr is removed as well, 768 * but I strongly believe we don't have to do it. 769 * nd6_purge() is only called from in6_ifdetach(), 770 * which removes all the associated interface addresses 771 * by itself. 772 * (jinmei@kame.net 20010129) 773 */ 774 prelist_remove(pr); 775 } 776 } 777 778 /* cancel default outgoing interface setting */ 779 if (nd6_defifindex == ifp->if_index) 780 nd6_setdefaultiface(0); 781 782 /* XXX: too restrictive? */ 783 if (!ip6_forwarding && ndi && nd6_accepts_rtadv(ndi)) { 784 /* refresh default router list */ 785 defrouter_select(); 786 } 787 788 /* 789 * Nuke neighbor cache entries for the ifp. 790 * Note that rt->rt_ifp may not be the same as ifp, 791 * due to KAME goto ours hack. See RTM_RESOLVE case in 792 * nd6_rtrequest(), and ip6_input(). 793 */ 794 ln = llinfo_nd6.ln_next; 795 while (ln != NULL && ln != &llinfo_nd6) { 796 struct rtentry *rt; 797 const struct sockaddr_dl *sdl; 798 799 nln = ln->ln_next; 800 rt = ln->ln_rt; 801 if (rt && rt->rt_gateway && 802 rt->rt_gateway->sa_family == AF_LINK) { 803 sdl = satocsdl(rt->rt_gateway); 804 if (sdl->sdl_index == ifp->if_index) 805 nln = nd6_free(rt, 0); 806 } 807 ln = nln; 808 } 809 } 810 811 struct rtentry * 812 nd6_lookup(const struct in6_addr *addr6, int create, struct ifnet *ifp) 813 { 814 struct rtentry *rt; 815 struct sockaddr_in6 sin6; 816 817 sockaddr_in6_init(&sin6, addr6, 0, 0, 0); 818 rt = rtalloc1((struct sockaddr *)&sin6, create); 819 if (rt != NULL && (rt->rt_flags & RTF_LLINFO) == 0) { 820 /* 821 * This is the case for the default route. 822 * If we want to create a neighbor cache for the address, we 823 * should free the route for the destination and allocate an 824 * interface route. 825 */ 826 if (create) { 827 RTFREE(rt); 828 rt = NULL; 829 } 830 } 831 if (rt != NULL) 832 ; 833 else if (create && ifp) { 834 int e; 835 836 /* 837 * If no route is available and create is set, 838 * we allocate a host route for the destination 839 * and treat it like an interface route. 840 * This hack is necessary for a neighbor which can't 841 * be covered by our own prefix. 842 */ 843 struct ifaddr *ifa = 844 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp); 845 if (ifa == NULL) 846 return NULL; 847 848 /* 849 * Create a new route. RTF_LLINFO is necessary 850 * to create a Neighbor Cache entry for the 851 * destination in nd6_rtrequest which will be 852 * called in rtrequest via ifa->ifa_rtrequest. 853 */ 854 if ((e = rtrequest(RTM_ADD, (const struct sockaddr *)&sin6, 855 ifa->ifa_addr, (const struct sockaddr *)&all1_sa, 856 (ifa->ifa_flags | RTF_HOST | RTF_LLINFO) & 857 ~RTF_CLONING, &rt)) != 0) { 858 #if 0 859 log(LOG_ERR, 860 "nd6_lookup: failed to add route for a " 861 "neighbor(%s), errno=%d\n", 862 ip6_sprintf(addr6), e); 863 #endif 864 return NULL; 865 } 866 if (rt == NULL) 867 return NULL; 868 if (rt->rt_llinfo) { 869 struct llinfo_nd6 *ln = 870 (struct llinfo_nd6 *)rt->rt_llinfo; 871 ln->ln_state = ND6_LLINFO_NOSTATE; 872 } 873 } else 874 return NULL; 875 rt->rt_refcnt--; 876 /* 877 * Validation for the entry. 878 * Note that the check for rt_llinfo is necessary because a cloned 879 * route from a parent route that has the L flag (e.g. the default 880 * route to a p2p interface) may have the flag, too, while the 881 * destination is not actually a neighbor. 882 * XXX: we can't use rt->rt_ifp to check for the interface, since 883 * it might be the loopback interface if the entry is for our 884 * own address on a non-loopback interface. Instead, we should 885 * use rt->rt_ifa->ifa_ifp, which would specify the REAL 886 * interface. 887 * Note also that ifa_ifp and ifp may differ when we connect two 888 * interfaces to a same link, install a link prefix to an interface, 889 * and try to install a neighbor cache on an interface that does not 890 * have a route to the prefix. 891 */ 892 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 893 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL || 894 (ifp && rt->rt_ifa->ifa_ifp != ifp)) { 895 if (create) { 896 nd6log((LOG_DEBUG, 897 "nd6_lookup: failed to lookup %s (if = %s)\n", 898 ip6_sprintf(addr6), 899 ifp ? if_name(ifp) : "unspec")); 900 } 901 return NULL; 902 } 903 return rt; 904 } 905 906 /* 907 * Detect if a given IPv6 address identifies a neighbor on a given link. 908 * XXX: should take care of the destination of a p2p link? 909 */ 910 int 911 nd6_is_addr_neighbor(const struct sockaddr_in6 *addr, struct ifnet *ifp) 912 { 913 struct nd_prefix *pr; 914 915 /* 916 * A link-local address is always a neighbor. 917 * XXX: a link does not necessarily specify a single interface. 918 */ 919 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr)) { 920 struct sockaddr_in6 sin6_copy; 921 u_int32_t zone; 922 923 /* 924 * We need sin6_copy since sa6_recoverscope() may modify the 925 * content (XXX). 926 */ 927 sin6_copy = *addr; 928 if (sa6_recoverscope(&sin6_copy)) 929 return 0; /* XXX: should be impossible */ 930 if (in6_setscope(&sin6_copy.sin6_addr, ifp, &zone)) 931 return 0; 932 if (sin6_copy.sin6_scope_id == zone) 933 return 1; 934 else 935 return 0; 936 } 937 938 /* 939 * If the address matches one of our on-link prefixes, it should be a 940 * neighbor. 941 */ 942 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 943 if (pr->ndpr_ifp != ifp) 944 continue; 945 946 if (!(pr->ndpr_stateflags & NDPRF_ONLINK)) 947 continue; 948 949 if (IN6_ARE_MASKED_ADDR_EQUAL(&pr->ndpr_prefix.sin6_addr, 950 &addr->sin6_addr, &pr->ndpr_mask)) 951 return 1; 952 } 953 954 /* 955 * If the default router list is empty, all addresses are regarded 956 * as on-link, and thus, as a neighbor. 957 * XXX: we restrict the condition to hosts, because routers usually do 958 * not have the "default router list". 959 */ 960 if (!ip6_forwarding && TAILQ_FIRST(&nd_defrouter) == NULL && 961 nd6_defifindex == ifp->if_index) { 962 return 1; 963 } 964 965 /* 966 * Even if the address matches none of our addresses, it might be 967 * in the neighbor cache. 968 */ 969 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL) 970 return 1; 971 972 return 0; 973 } 974 975 /* 976 * Free an nd6 llinfo entry. 977 * Since the function would cause significant changes in the kernel, DO NOT 978 * make it global, unless you have a strong reason for the change, and are sure 979 * that the change is safe. 980 */ 981 static struct llinfo_nd6 * 982 nd6_free(struct rtentry *rt, int gc) 983 { 984 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next; 985 struct in6_addr in6 = satocsin6(rt_getkey(rt))->sin6_addr; 986 struct nd_defrouter *dr; 987 988 /* 989 * we used to have pfctlinput(PRC_HOSTDEAD) here. 990 * even though it is not harmful, it was not really necessary. 991 */ 992 993 /* cancel timer */ 994 nd6_llinfo_settimer(ln, -1); 995 996 if (!ip6_forwarding) { 997 int s; 998 s = splsoftnet(); 999 dr = defrouter_lookup(&satocsin6(rt_getkey(rt))->sin6_addr, 1000 rt->rt_ifp); 1001 1002 if (dr != NULL && dr->expire && 1003 ln->ln_state == ND6_LLINFO_STALE && gc) { 1004 /* 1005 * If the reason for the deletion is just garbage 1006 * collection, and the neighbor is an active default 1007 * router, do not delete it. Instead, reset the GC 1008 * timer using the router's lifetime. 1009 * Simply deleting the entry would affect default 1010 * router selection, which is not necessarily a good 1011 * thing, especially when we're using router preference 1012 * values. 1013 * XXX: the check for ln_state would be redundant, 1014 * but we intentionally keep it just in case. 1015 */ 1016 if (dr->expire > time_second) 1017 nd6_llinfo_settimer(ln, 1018 (dr->expire - time_second) * hz); 1019 else 1020 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz); 1021 splx(s); 1022 return ln->ln_next; 1023 } 1024 1025 if (ln->ln_router || dr) { 1026 /* 1027 * rt6_flush must be called whether or not the neighbor 1028 * is in the Default Router List. 1029 * See a corresponding comment in nd6_na_input(). 1030 */ 1031 rt6_flush(&in6, rt->rt_ifp); 1032 } 1033 1034 if (dr) { 1035 /* 1036 * Unreachablity of a router might affect the default 1037 * router selection and on-link detection of advertised 1038 * prefixes. 1039 */ 1040 1041 /* 1042 * Temporarily fake the state to choose a new default 1043 * router and to perform on-link determination of 1044 * prefixes correctly. 1045 * Below the state will be set correctly, 1046 * or the entry itself will be deleted. 1047 */ 1048 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1049 1050 /* 1051 * Since defrouter_select() does not affect the 1052 * on-link determination and MIP6 needs the check 1053 * before the default router selection, we perform 1054 * the check now. 1055 */ 1056 pfxlist_onlink_check(); 1057 1058 /* 1059 * refresh default router list 1060 */ 1061 defrouter_select(); 1062 } 1063 splx(s); 1064 } 1065 1066 /* 1067 * Before deleting the entry, remember the next entry as the 1068 * return value. We need this because pfxlist_onlink_check() above 1069 * might have freed other entries (particularly the old next entry) as 1070 * a side effect (XXX). 1071 */ 1072 next = ln->ln_next; 1073 1074 /* 1075 * Detach the route from the routing tree and the list of neighbor 1076 * caches, and disable the route entry not to be used in already 1077 * cached routes. 1078 */ 1079 rtrequest(RTM_DELETE, rt_getkey(rt), NULL, rt_mask(rt), 0, NULL); 1080 1081 return next; 1082 } 1083 1084 /* 1085 * Upper-layer reachability hint for Neighbor Unreachability Detection. 1086 * 1087 * XXX cost-effective methods? 1088 */ 1089 void 1090 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force) 1091 { 1092 struct llinfo_nd6 *ln; 1093 1094 /* 1095 * If the caller specified "rt", use that. Otherwise, resolve the 1096 * routing table by supplied "dst6". 1097 */ 1098 if (rt == NULL) { 1099 if (dst6 == NULL) 1100 return; 1101 if ((rt = nd6_lookup(dst6, 0, NULL)) == NULL) 1102 return; 1103 } 1104 1105 if ((rt->rt_flags & RTF_GATEWAY) != 0 || 1106 (rt->rt_flags & RTF_LLINFO) == 0 || 1107 !rt->rt_llinfo || !rt->rt_gateway || 1108 rt->rt_gateway->sa_family != AF_LINK) { 1109 /* This is not a host route. */ 1110 return; 1111 } 1112 1113 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1114 if (ln->ln_state < ND6_LLINFO_REACHABLE) 1115 return; 1116 1117 /* 1118 * if we get upper-layer reachability confirmation many times, 1119 * it is possible we have false information. 1120 */ 1121 if (!force) { 1122 ln->ln_byhint++; 1123 if (ln->ln_byhint > nd6_maxnudhint) 1124 return; 1125 } 1126 1127 ln->ln_state = ND6_LLINFO_REACHABLE; 1128 if (!ND6_LLINFO_PERMANENT(ln)) { 1129 nd6_llinfo_settimer(ln, 1130 (long)ND_IFINFO(rt->rt_ifp)->reachable * hz); 1131 } 1132 } 1133 1134 void 1135 nd6_rtrequest(int req, struct rtentry *rt, const struct rt_addrinfo *info) 1136 { 1137 struct sockaddr *gate = rt->rt_gateway; 1138 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1139 struct ifnet *ifp = rt->rt_ifp; 1140 uint8_t namelen = strlen(ifp->if_xname), addrlen = ifp->if_addrlen; 1141 struct ifaddr *ifa; 1142 1143 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1144 1145 if (req == RTM_LLINFO_UPD) { 1146 int rc; 1147 struct in6_addr *in6; 1148 struct in6_addr in6_all; 1149 int anycast; 1150 1151 if ((ifa = info->rti_ifa) == NULL) 1152 return; 1153 1154 in6 = &ifatoia6(ifa)->ia_addr.sin6_addr; 1155 anycast = ifatoia6(ifa)->ia6_flags & IN6_IFF_ANYCAST; 1156 1157 in6_all = in6addr_linklocal_allnodes; 1158 if ((rc = in6_setscope(&in6_all, ifa->ifa_ifp, NULL)) != 0) { 1159 log(LOG_ERR, "%s: failed to set scope %s " 1160 "(errno=%d)\n", __func__, if_name(ifp), rc); 1161 return; 1162 } 1163 1164 /* XXX don't set Override for proxy addresses */ 1165 nd6_na_output(ifa->ifa_ifp, &in6_all, in6, 1166 (anycast ? 0 : ND_NA_FLAG_OVERRIDE) 1167 #if 0 1168 | (ip6_forwarding ? ND_NA_FLAG_ROUTER : 0) 1169 #endif 1170 , 1, NULL); 1171 return; 1172 } 1173 1174 if ((rt->rt_flags & RTF_GATEWAY) != 0) 1175 return; 1176 1177 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) { 1178 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1179 /* 1180 * This is probably an interface direct route for a link 1181 * which does not need neighbor caches (e.g. fe80::%lo0/64). 1182 * We do not need special treatment below for such a route. 1183 * Moreover, the RTF_LLINFO flag which would be set below 1184 * would annoy the ndp(8) command. 1185 */ 1186 return; 1187 } 1188 1189 if (req == RTM_RESOLVE && 1190 (nd6_need_cache(ifp) == 0 || /* stf case */ 1191 !nd6_is_addr_neighbor(satocsin6(rt_getkey(rt)), ifp))) { 1192 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1193 /* 1194 * FreeBSD and BSD/OS often make a cloned host route based 1195 * on a less-specific route (e.g. the default route). 1196 * If the less specific route does not have a "gateway" 1197 * (this is the case when the route just goes to a p2p or an 1198 * stf interface), we'll mistakenly make a neighbor cache for 1199 * the host route, and will see strange neighbor solicitation 1200 * for the corresponding destination. In order to avoid the 1201 * confusion, we check if the destination of the route is 1202 * a neighbor in terms of neighbor discovery, and stop the 1203 * process if not. Additionally, we remove the LLINFO flag 1204 * so that ndp(8) will not try to get the neighbor information 1205 * of the destination. 1206 */ 1207 rt->rt_flags &= ~RTF_LLINFO; 1208 return; 1209 } 1210 1211 switch (req) { 1212 case RTM_ADD: 1213 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1214 /* 1215 * There is no backward compatibility :) 1216 * 1217 * if ((rt->rt_flags & RTF_HOST) == 0 && 1218 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1219 * rt->rt_flags |= RTF_CLONING; 1220 */ 1221 if ((rt->rt_flags & RTF_CLONING) || 1222 ((rt->rt_flags & RTF_LLINFO) && ln == NULL)) { 1223 union { 1224 struct sockaddr sa; 1225 struct sockaddr_dl sdl; 1226 struct sockaddr_storage ss; 1227 } u; 1228 /* 1229 * Case 1: This route should come from a route to 1230 * interface (RTF_CLONING case) or the route should be 1231 * treated as on-link but is currently not 1232 * (RTF_LLINFO && ln == NULL case). 1233 */ 1234 if (sockaddr_dl_init(&u.sdl, sizeof(u.ss), 1235 ifp->if_index, ifp->if_type, 1236 NULL, namelen, NULL, addrlen) == NULL) { 1237 printf("%s.%d: sockaddr_dl_init(, %zu, ) " 1238 "failed on %s\n", __func__, __LINE__, 1239 sizeof(u.ss), if_name(ifp)); 1240 } 1241 rt_setgate(rt, &u.sa); 1242 gate = rt->rt_gateway; 1243 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1244 if (ln != NULL) 1245 nd6_llinfo_settimer(ln, 0); 1246 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1247 if ((rt->rt_flags & RTF_CLONING) != 0) 1248 break; 1249 } 1250 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1251 /* 1252 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here. 1253 * We don't do that here since llinfo is not ready yet. 1254 * 1255 * There are also couple of other things to be discussed: 1256 * - unsolicited NA code needs improvement beforehand 1257 * - RFC2461 says we MAY send multicast unsolicited NA 1258 * (7.2.6 paragraph 4), however, it also says that we 1259 * SHOULD provide a mechanism to prevent multicast NA storm. 1260 * we don't have anything like it right now. 1261 * note that the mechanism needs a mutual agreement 1262 * between proxies, which means that we need to implement 1263 * a new protocol, or a new kludge. 1264 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA. 1265 * we need to check ip6forwarding before sending it. 1266 * (or should we allow proxy ND configuration only for 1267 * routers? there's no mention about proxy ND from hosts) 1268 */ 1269 #if 0 1270 /* XXX it does not work */ 1271 if (rt->rt_flags & RTF_ANNOUNCE) 1272 nd6_na_output(ifp, 1273 &satocsin6(rt_getkey(rt))->sin6_addr, 1274 &satocsin6(rt_getkey(rt))->sin6_addr, 1275 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0, 1276 1, NULL); 1277 #endif 1278 /* FALLTHROUGH */ 1279 case RTM_RESOLVE: 1280 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { 1281 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1282 /* 1283 * Address resolution isn't necessary for a point to 1284 * point link, so we can skip this test for a p2p link. 1285 */ 1286 if (gate->sa_family != AF_LINK || 1287 gate->sa_len < 1288 sockaddr_dl_measure(namelen, addrlen)) { 1289 log(LOG_DEBUG, 1290 "nd6_rtrequest: bad gateway value: %s\n", 1291 if_name(ifp)); 1292 break; 1293 } 1294 satosdl(gate)->sdl_type = ifp->if_type; 1295 satosdl(gate)->sdl_index = ifp->if_index; 1296 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1297 } 1298 if (ln != NULL) 1299 break; /* This happens on a route change */ 1300 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1301 /* 1302 * Case 2: This route may come from cloning, or a manual route 1303 * add with a LL address. 1304 */ 1305 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln)); 1306 rt->rt_llinfo = ln; 1307 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1308 if (ln == NULL) { 1309 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n"); 1310 break; 1311 } 1312 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1313 nd6_inuse++; 1314 nd6_allocated++; 1315 memset(ln, 0, sizeof(*ln)); 1316 ln->ln_rt = rt; 1317 callout_init(&ln->ln_timer_ch, CALLOUT_MPSAFE); 1318 /* this is required for "ndp" command. - shin */ 1319 if (req == RTM_ADD) { 1320 /* 1321 * gate should have some valid AF_LINK entry, 1322 * and ln->ln_expire should have some lifetime 1323 * which is specified by ndp command. 1324 */ 1325 ln->ln_state = ND6_LLINFO_REACHABLE; 1326 ln->ln_byhint = 0; 1327 } else { 1328 /* 1329 * When req == RTM_RESOLVE, rt is created and 1330 * initialized in rtrequest(), so rt_expire is 0. 1331 */ 1332 ln->ln_state = ND6_LLINFO_NOSTATE; 1333 nd6_llinfo_settimer(ln, 0); 1334 } 1335 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1336 rt->rt_flags |= RTF_LLINFO; 1337 ln->ln_next = llinfo_nd6.ln_next; 1338 llinfo_nd6.ln_next = ln; 1339 ln->ln_prev = &llinfo_nd6; 1340 ln->ln_next->ln_prev = ln; 1341 1342 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1343 /* 1344 * check if rt_getkey(rt) is an address assigned 1345 * to the interface. 1346 */ 1347 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, 1348 &satocsin6(rt_getkey(rt))->sin6_addr); 1349 RT_DPRINTF("rt->_rt_key = %p\n", (void *)rt->_rt_key); 1350 if (ifa != NULL) { 1351 const void *mac; 1352 nd6_llinfo_settimer(ln, -1); 1353 ln->ln_state = ND6_LLINFO_REACHABLE; 1354 ln->ln_byhint = 0; 1355 if ((mac = nd6_ifptomac(ifp)) != NULL) { 1356 /* XXX check for error */ 1357 if (sockaddr_dl_setaddr(satosdl(gate), 1358 gate->sa_len, mac, 1359 ifp->if_addrlen) == NULL) { 1360 printf("%s.%d: " 1361 "sockaddr_dl_setaddr(, %d, ) " 1362 "failed on %s\n", __func__, 1363 __LINE__, gate->sa_len, 1364 if_name(ifp)); 1365 } 1366 } 1367 if (nd6_useloopback) { 1368 ifp = rt->rt_ifp = lo0ifp; /* XXX */ 1369 /* 1370 * Make sure rt_ifa be equal to the ifaddr 1371 * corresponding to the address. 1372 * We need this because when we refer 1373 * rt_ifa->ia6_flags in ip6_input, we assume 1374 * that the rt_ifa points to the address instead 1375 * of the loopback address. 1376 */ 1377 if (ifa != rt->rt_ifa) 1378 rt_replace_ifa(rt, ifa); 1379 rt->rt_flags &= ~RTF_CLONED; 1380 } 1381 } else if (rt->rt_flags & RTF_ANNOUNCE) { 1382 nd6_llinfo_settimer(ln, -1); 1383 ln->ln_state = ND6_LLINFO_REACHABLE; 1384 ln->ln_byhint = 0; 1385 1386 /* join solicited node multicast for proxy ND */ 1387 if (ifp->if_flags & IFF_MULTICAST) { 1388 struct in6_addr llsol; 1389 int error; 1390 1391 llsol = satocsin6(rt_getkey(rt))->sin6_addr; 1392 llsol.s6_addr32[0] = htonl(0xff020000); 1393 llsol.s6_addr32[1] = 0; 1394 llsol.s6_addr32[2] = htonl(1); 1395 llsol.s6_addr8[12] = 0xff; 1396 if (in6_setscope(&llsol, ifp, NULL)) 1397 break; 1398 if (!in6_addmulti(&llsol, ifp, &error, 0)) { 1399 nd6log((LOG_ERR, "%s: failed to join " 1400 "%s (errno=%d)\n", if_name(ifp), 1401 ip6_sprintf(&llsol), error)); 1402 } 1403 } 1404 } 1405 break; 1406 1407 case RTM_DELETE: 1408 if (ln == NULL) 1409 break; 1410 /* leave from solicited node multicast for proxy ND */ 1411 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 && 1412 (ifp->if_flags & IFF_MULTICAST) != 0) { 1413 struct in6_addr llsol; 1414 struct in6_multi *in6m; 1415 1416 llsol = satocsin6(rt_getkey(rt))->sin6_addr; 1417 llsol.s6_addr32[0] = htonl(0xff020000); 1418 llsol.s6_addr32[1] = 0; 1419 llsol.s6_addr32[2] = htonl(1); 1420 llsol.s6_addr8[12] = 0xff; 1421 if (in6_setscope(&llsol, ifp, NULL) == 0) { 1422 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 1423 if (in6m) 1424 in6_delmulti(in6m); 1425 } 1426 } 1427 nd6_inuse--; 1428 ln->ln_next->ln_prev = ln->ln_prev; 1429 ln->ln_prev->ln_next = ln->ln_next; 1430 ln->ln_prev = NULL; 1431 nd6_llinfo_settimer(ln, -1); 1432 rt->rt_llinfo = 0; 1433 rt->rt_flags &= ~RTF_LLINFO; 1434 clear_llinfo_pqueue(ln); 1435 Free(ln); 1436 } 1437 } 1438 1439 int 1440 nd6_ioctl(u_long cmd, void *data, struct ifnet *ifp) 1441 { 1442 struct in6_drlist *drl = (struct in6_drlist *)data; 1443 struct in6_oprlist *oprl = (struct in6_oprlist *)data; 1444 struct in6_ndireq *ndi = (struct in6_ndireq *)data; 1445 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; 1446 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; 1447 struct nd_defrouter *dr; 1448 struct nd_prefix *pr; 1449 struct rtentry *rt; 1450 int i = 0, error = 0; 1451 int s; 1452 1453 switch (cmd) { 1454 case SIOCGDRLST_IN6: 1455 /* 1456 * obsolete API, use sysctl under net.inet6.icmp6 1457 */ 1458 memset(drl, 0, sizeof(*drl)); 1459 s = splsoftnet(); 1460 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) { 1461 if (i >= DRLSTSIZ) 1462 break; 1463 drl->defrouter[i].rtaddr = dr->rtaddr; 1464 in6_clearscope(&drl->defrouter[i].rtaddr); 1465 1466 drl->defrouter[i].flags = dr->flags; 1467 drl->defrouter[i].rtlifetime = dr->rtlifetime; 1468 drl->defrouter[i].expire = dr->expire; 1469 drl->defrouter[i].if_index = dr->ifp->if_index; 1470 i++; 1471 } 1472 splx(s); 1473 break; 1474 case SIOCGPRLST_IN6: 1475 /* 1476 * obsolete API, use sysctl under net.inet6.icmp6 1477 * 1478 * XXX the structure in6_prlist was changed in backward- 1479 * incompatible manner. in6_oprlist is used for SIOCGPRLST_IN6, 1480 * in6_prlist is used for nd6_sysctl() - fill_prlist(). 1481 */ 1482 /* 1483 * XXX meaning of fields, especialy "raflags", is very 1484 * differnet between RA prefix list and RR/static prefix list. 1485 * how about separating ioctls into two? 1486 */ 1487 memset(oprl, 0, sizeof(*oprl)); 1488 s = splsoftnet(); 1489 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 1490 struct nd_pfxrouter *pfr; 1491 int j; 1492 1493 if (i >= PRLSTSIZ) 1494 break; 1495 oprl->prefix[i].prefix = pr->ndpr_prefix.sin6_addr; 1496 oprl->prefix[i].raflags = pr->ndpr_raf; 1497 oprl->prefix[i].prefixlen = pr->ndpr_plen; 1498 oprl->prefix[i].vltime = pr->ndpr_vltime; 1499 oprl->prefix[i].pltime = pr->ndpr_pltime; 1500 oprl->prefix[i].if_index = pr->ndpr_ifp->if_index; 1501 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1502 oprl->prefix[i].expire = 0; 1503 else { 1504 time_t maxexpire; 1505 1506 /* XXX: we assume time_t is signed. */ 1507 maxexpire = (-1) & 1508 ~((time_t)1 << 1509 ((sizeof(maxexpire) * 8) - 1)); 1510 if (pr->ndpr_vltime < 1511 maxexpire - pr->ndpr_lastupdate) { 1512 oprl->prefix[i].expire = 1513 pr->ndpr_lastupdate + 1514 pr->ndpr_vltime; 1515 } else 1516 oprl->prefix[i].expire = maxexpire; 1517 } 1518 1519 j = 0; 1520 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 1521 if (j < DRLSTSIZ) { 1522 #define RTRADDR oprl->prefix[i].advrtr[j] 1523 RTRADDR = pfr->router->rtaddr; 1524 in6_clearscope(&RTRADDR); 1525 #undef RTRADDR 1526 } 1527 j++; 1528 } 1529 oprl->prefix[i].advrtrs = j; 1530 oprl->prefix[i].origin = PR_ORIG_RA; 1531 1532 i++; 1533 } 1534 splx(s); 1535 1536 break; 1537 case OSIOCGIFINFO_IN6: 1538 #define ND ndi->ndi 1539 /* XXX: old ndp(8) assumes a positive value for linkmtu. */ 1540 memset(&ND, 0, sizeof(ND)); 1541 ND.linkmtu = IN6_LINKMTU(ifp); 1542 ND.maxmtu = ND_IFINFO(ifp)->maxmtu; 1543 ND.basereachable = ND_IFINFO(ifp)->basereachable; 1544 ND.reachable = ND_IFINFO(ifp)->reachable; 1545 ND.retrans = ND_IFINFO(ifp)->retrans; 1546 ND.flags = ND_IFINFO(ifp)->flags; 1547 ND.recalctm = ND_IFINFO(ifp)->recalctm; 1548 ND.chlim = ND_IFINFO(ifp)->chlim; 1549 break; 1550 case SIOCGIFINFO_IN6: 1551 ND = *ND_IFINFO(ifp); 1552 break; 1553 case SIOCSIFINFO_IN6: 1554 /* 1555 * used to change host variables from userland. 1556 * intented for a use on router to reflect RA configurations. 1557 */ 1558 /* 0 means 'unspecified' */ 1559 if (ND.linkmtu != 0) { 1560 if (ND.linkmtu < IPV6_MMTU || 1561 ND.linkmtu > IN6_LINKMTU(ifp)) { 1562 error = EINVAL; 1563 break; 1564 } 1565 ND_IFINFO(ifp)->linkmtu = ND.linkmtu; 1566 } 1567 1568 if (ND.basereachable != 0) { 1569 int obasereachable = ND_IFINFO(ifp)->basereachable; 1570 1571 ND_IFINFO(ifp)->basereachable = ND.basereachable; 1572 if (ND.basereachable != obasereachable) 1573 ND_IFINFO(ifp)->reachable = 1574 ND_COMPUTE_RTIME(ND.basereachable); 1575 } 1576 if (ND.retrans != 0) 1577 ND_IFINFO(ifp)->retrans = ND.retrans; 1578 if (ND.chlim != 0) 1579 ND_IFINFO(ifp)->chlim = ND.chlim; 1580 /* FALLTHROUGH */ 1581 case SIOCSIFINFO_FLAGS: 1582 ND_IFINFO(ifp)->flags = ND.flags; 1583 break; 1584 #undef ND 1585 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ 1586 /* sync kernel routing table with the default router list */ 1587 defrouter_reset(); 1588 defrouter_select(); 1589 break; 1590 case SIOCSPFXFLUSH_IN6: 1591 { 1592 /* flush all the prefix advertised by routers */ 1593 struct nd_prefix *pfx, *next; 1594 1595 s = splsoftnet(); 1596 for (pfx = LIST_FIRST(&nd_prefix); pfx; pfx = next) { 1597 struct in6_ifaddr *ia, *ia_next; 1598 1599 next = LIST_NEXT(pfx, ndpr_entry); 1600 1601 if (IN6_IS_ADDR_LINKLOCAL(&pfx->ndpr_prefix.sin6_addr)) 1602 continue; /* XXX */ 1603 1604 /* do we really have to remove addresses as well? */ 1605 for (ia = in6_ifaddr; ia; ia = ia_next) { 1606 /* ia might be removed. keep the next ptr. */ 1607 ia_next = ia->ia_next; 1608 1609 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1610 continue; 1611 1612 if (ia->ia6_ndpr == pfx) 1613 in6_purgeaddr(&ia->ia_ifa); 1614 } 1615 prelist_remove(pfx); 1616 } 1617 splx(s); 1618 break; 1619 } 1620 case SIOCSRTRFLUSH_IN6: 1621 { 1622 /* flush all the default routers */ 1623 struct nd_defrouter *drtr, *next; 1624 1625 s = splsoftnet(); 1626 defrouter_reset(); 1627 for (drtr = TAILQ_FIRST(&nd_defrouter); drtr; drtr = next) { 1628 next = TAILQ_NEXT(drtr, dr_entry); 1629 defrtrlist_del(drtr); 1630 } 1631 defrouter_select(); 1632 splx(s); 1633 break; 1634 } 1635 case SIOCGNBRINFO_IN6: 1636 { 1637 struct llinfo_nd6 *ln; 1638 struct in6_addr nb_addr = nbi->addr; /* make local for safety */ 1639 1640 if ((error = in6_setscope(&nb_addr, ifp, NULL)) != 0) 1641 return error; 1642 1643 s = splsoftnet(); 1644 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL || 1645 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) == NULL) { 1646 error = EINVAL; 1647 splx(s); 1648 break; 1649 } 1650 nbi->state = ln->ln_state; 1651 nbi->asked = ln->ln_asked; 1652 nbi->isrouter = ln->ln_router; 1653 nbi->expire = ln->ln_expire; 1654 splx(s); 1655 1656 break; 1657 } 1658 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1659 ndif->ifindex = nd6_defifindex; 1660 break; 1661 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1662 return nd6_setdefaultiface(ndif->ifindex); 1663 } 1664 return error; 1665 } 1666 1667 void 1668 nd6_llinfo_release_pkts(struct llinfo_nd6 *ln, struct ifnet *ifp, 1669 struct rtentry *rt) 1670 { 1671 struct mbuf *m_hold, *m_hold_next; 1672 1673 for (m_hold = ln->ln_hold, ln->ln_hold = NULL; 1674 m_hold != NULL; 1675 m_hold = m_hold_next) { 1676 m_hold_next = m_hold->m_nextpkt; 1677 m_hold->m_nextpkt = NULL; 1678 1679 /* 1680 * we assume ifp is not a p2p here, so 1681 * just set the 2nd argument as the 1682 * 1st one. 1683 */ 1684 nd6_output(ifp, ifp, m_hold, satocsin6(rt_getkey(rt)), rt); 1685 } 1686 } 1687 1688 /* 1689 * Create neighbor cache entry and cache link-layer address, 1690 * on reception of inbound ND6 packets. (RS/RA/NS/redirect) 1691 */ 1692 struct rtentry * 1693 nd6_cache_lladdr( 1694 struct ifnet *ifp, 1695 struct in6_addr *from, 1696 char *lladdr, 1697 int lladdrlen, 1698 int type, /* ICMP6 type */ 1699 int code /* type dependent information */ 1700 ) 1701 { 1702 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 1703 struct rtentry *rt = NULL; 1704 struct llinfo_nd6 *ln = NULL; 1705 int is_newentry; 1706 struct sockaddr_dl *sdl = NULL; 1707 int do_update; 1708 int olladdr; 1709 int llchange; 1710 int newstate = 0; 1711 1712 if (ifp == NULL) 1713 panic("ifp == NULL in nd6_cache_lladdr"); 1714 if (from == NULL) 1715 panic("from == NULL in nd6_cache_lladdr"); 1716 1717 /* nothing must be updated for unspecified address */ 1718 if (IN6_IS_ADDR_UNSPECIFIED(from)) 1719 return NULL; 1720 1721 /* 1722 * Validation about ifp->if_addrlen and lladdrlen must be done in 1723 * the caller. 1724 * 1725 * XXX If the link does not have link-layer adderss, what should 1726 * we do? (ifp->if_addrlen == 0) 1727 * Spec says nothing in sections for RA, RS and NA. There's small 1728 * description on it in NS section (RFC 2461 7.2.3). 1729 */ 1730 1731 rt = nd6_lookup(from, 0, ifp); 1732 if (rt == NULL) { 1733 #if 0 1734 /* nothing must be done if there's no lladdr */ 1735 if (!lladdr || !lladdrlen) 1736 return NULL; 1737 #endif 1738 1739 rt = nd6_lookup(from, 1, ifp); 1740 is_newentry = 1; 1741 } else { 1742 /* do nothing if static ndp is set */ 1743 if (rt->rt_flags & RTF_STATIC) 1744 return NULL; 1745 is_newentry = 0; 1746 } 1747 1748 if (rt == NULL) 1749 return NULL; 1750 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) { 1751 fail: 1752 (void)nd6_free(rt, 0); 1753 return NULL; 1754 } 1755 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1756 if (ln == NULL) 1757 goto fail; 1758 if (rt->rt_gateway == NULL) 1759 goto fail; 1760 if (rt->rt_gateway->sa_family != AF_LINK) 1761 goto fail; 1762 sdl = satosdl(rt->rt_gateway); 1763 1764 olladdr = (sdl->sdl_alen) ? 1 : 0; 1765 if (olladdr && lladdr) { 1766 if (memcmp(lladdr, CLLADDR(sdl), ifp->if_addrlen)) 1767 llchange = 1; 1768 else 1769 llchange = 0; 1770 } else 1771 llchange = 0; 1772 1773 /* 1774 * newentry olladdr lladdr llchange (*=record) 1775 * 0 n n -- (1) 1776 * 0 y n -- (2) 1777 * 0 n y -- (3) * STALE 1778 * 0 y y n (4) * 1779 * 0 y y y (5) * STALE 1780 * 1 -- n -- (6) NOSTATE(= PASSIVE) 1781 * 1 -- y -- (7) * STALE 1782 */ 1783 1784 if (lladdr) { /* (3-5) and (7) */ 1785 /* 1786 * Record source link-layer address 1787 * XXX is it dependent to ifp->if_type? 1788 */ 1789 /* XXX check for error */ 1790 if (sockaddr_dl_setaddr(sdl, sdl->sdl_len, lladdr, 1791 ifp->if_addrlen) == NULL) { 1792 printf("%s.%d: sockaddr_dl_setaddr(, %d, ) " 1793 "failed on %s\n", __func__, __LINE__, 1794 sdl->sdl_len, if_name(ifp)); 1795 } 1796 } 1797 1798 if (!is_newentry) { 1799 if ((!olladdr && lladdr) || /* (3) */ 1800 (olladdr && lladdr && llchange)) { /* (5) */ 1801 do_update = 1; 1802 newstate = ND6_LLINFO_STALE; 1803 } else /* (1-2,4) */ 1804 do_update = 0; 1805 } else { 1806 do_update = 1; 1807 if (lladdr == NULL) /* (6) */ 1808 newstate = ND6_LLINFO_NOSTATE; 1809 else /* (7) */ 1810 newstate = ND6_LLINFO_STALE; 1811 } 1812 1813 if (do_update) { 1814 /* 1815 * Update the state of the neighbor cache. 1816 */ 1817 ln->ln_state = newstate; 1818 1819 if (ln->ln_state == ND6_LLINFO_STALE) { 1820 /* 1821 * XXX: since nd6_output() below will cause 1822 * state tansition to DELAY and reset the timer, 1823 * we must set the timer now, although it is actually 1824 * meaningless. 1825 */ 1826 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz); 1827 1828 nd6_llinfo_release_pkts(ln, ifp, rt); 1829 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 1830 /* probe right away */ 1831 nd6_llinfo_settimer((void *)ln, 0); 1832 } 1833 } 1834 1835 /* 1836 * ICMP6 type dependent behavior. 1837 * 1838 * NS: clear IsRouter if new entry 1839 * RS: clear IsRouter 1840 * RA: set IsRouter if there's lladdr 1841 * redir: clear IsRouter if new entry 1842 * 1843 * RA case, (1): 1844 * The spec says that we must set IsRouter in the following cases: 1845 * - If lladdr exist, set IsRouter. This means (1-5). 1846 * - If it is old entry (!newentry), set IsRouter. This means (7). 1847 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 1848 * A quetion arises for (1) case. (1) case has no lladdr in the 1849 * neighbor cache, this is similar to (6). 1850 * This case is rare but we figured that we MUST NOT set IsRouter. 1851 * 1852 * newentry olladdr lladdr llchange NS RS RA redir 1853 * D R 1854 * 0 n n -- (1) c ? s 1855 * 0 y n -- (2) c s s 1856 * 0 n y -- (3) c s s 1857 * 0 y y n (4) c s s 1858 * 0 y y y (5) c s s 1859 * 1 -- n -- (6) c c c s 1860 * 1 -- y -- (7) c c s c s 1861 * 1862 * (c=clear s=set) 1863 */ 1864 switch (type & 0xff) { 1865 case ND_NEIGHBOR_SOLICIT: 1866 /* 1867 * New entry must have is_router flag cleared. 1868 */ 1869 if (is_newentry) /* (6-7) */ 1870 ln->ln_router = 0; 1871 break; 1872 case ND_REDIRECT: 1873 /* 1874 * If the icmp is a redirect to a better router, always set the 1875 * is_router flag. Otherwise, if the entry is newly created, 1876 * clear the flag. [RFC 2461, sec 8.3] 1877 */ 1878 if (code == ND_REDIRECT_ROUTER) 1879 ln->ln_router = 1; 1880 else if (is_newentry) /* (6-7) */ 1881 ln->ln_router = 0; 1882 break; 1883 case ND_ROUTER_SOLICIT: 1884 /* 1885 * is_router flag must always be cleared. 1886 */ 1887 ln->ln_router = 0; 1888 break; 1889 case ND_ROUTER_ADVERT: 1890 /* 1891 * Mark an entry with lladdr as a router. 1892 */ 1893 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */ 1894 (is_newentry && lladdr)) { /* (7) */ 1895 ln->ln_router = 1; 1896 } 1897 break; 1898 } 1899 1900 /* 1901 * When the link-layer address of a router changes, select the 1902 * best router again. In particular, when the neighbor entry is newly 1903 * created, it might affect the selection policy. 1904 * Question: can we restrict the first condition to the "is_newentry" 1905 * case? 1906 * XXX: when we hear an RA from a new router with the link-layer 1907 * address option, defrouter_select() is called twice, since 1908 * defrtrlist_update called the function as well. However, I believe 1909 * we can compromise the overhead, since it only happens the first 1910 * time. 1911 * XXX: although defrouter_select() should not have a bad effect 1912 * for those are not autoconfigured hosts, we explicitly avoid such 1913 * cases for safety. 1914 */ 1915 if (do_update && ln->ln_router && !ip6_forwarding && 1916 nd6_accepts_rtadv(ndi)) 1917 defrouter_select(); 1918 1919 return rt; 1920 } 1921 1922 static void 1923 nd6_slowtimo(void *ignored_arg) 1924 { 1925 struct nd_ifinfo *nd6if; 1926 struct ifnet *ifp; 1927 1928 mutex_enter(softnet_lock); 1929 KERNEL_LOCK(1, NULL); 1930 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 1931 nd6_slowtimo, NULL); 1932 TAILQ_FOREACH(ifp, &ifnet, if_list) { 1933 nd6if = ND_IFINFO(ifp); 1934 if (nd6if->basereachable && /* already initialized */ 1935 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 1936 /* 1937 * Since reachable time rarely changes by router 1938 * advertisements, we SHOULD insure that a new random 1939 * value gets recomputed at least once every few hours. 1940 * (RFC 2461, 6.3.4) 1941 */ 1942 nd6if->recalctm = nd6_recalc_reachtm_interval; 1943 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); 1944 } 1945 } 1946 KERNEL_UNLOCK_ONE(NULL); 1947 mutex_exit(softnet_lock); 1948 } 1949 1950 #define senderr(e) { error = (e); goto bad;} 1951 int 1952 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0, 1953 const struct sockaddr_in6 *dst, struct rtentry *rt0) 1954 { 1955 struct mbuf *m = m0; 1956 struct rtentry *rt = rt0; 1957 struct sockaddr_in6 *gw6 = NULL; 1958 struct llinfo_nd6 *ln = NULL; 1959 int error = 0; 1960 1961 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr)) 1962 goto sendpkt; 1963 1964 if (nd6_need_cache(ifp) == 0) 1965 goto sendpkt; 1966 1967 /* 1968 * next hop determination. This routine is derived from ether_output. 1969 */ 1970 if (rt) { 1971 if ((rt->rt_flags & RTF_UP) == 0) { 1972 if ((rt0 = rt = rtalloc1(sin6tocsa(dst), 1)) != NULL) { 1973 rt->rt_refcnt--; 1974 if (rt->rt_ifp != ifp) 1975 senderr(EHOSTUNREACH); 1976 } else 1977 senderr(EHOSTUNREACH); 1978 } 1979 1980 if (rt->rt_flags & RTF_GATEWAY) { 1981 gw6 = (struct sockaddr_in6 *)rt->rt_gateway; 1982 1983 /* 1984 * We skip link-layer address resolution and NUD 1985 * if the gateway is not a neighbor from ND point 1986 * of view, regardless of the value of nd_ifinfo.flags. 1987 * The second condition is a bit tricky; we skip 1988 * if the gateway is our own address, which is 1989 * sometimes used to install a route to a p2p link. 1990 */ 1991 if (!nd6_is_addr_neighbor(gw6, ifp) || 1992 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) { 1993 /* 1994 * We allow this kind of tricky route only 1995 * when the outgoing interface is p2p. 1996 * XXX: we may need a more generic rule here. 1997 */ 1998 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1999 senderr(EHOSTUNREACH); 2000 2001 goto sendpkt; 2002 } 2003 2004 if (rt->rt_gwroute == NULL) 2005 goto lookup; 2006 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 2007 rtfree(rt); rt = rt0; 2008 lookup: 2009 rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1); 2010 if ((rt = rt->rt_gwroute) == NULL) 2011 senderr(EHOSTUNREACH); 2012 /* the "G" test below also prevents rt == rt0 */ 2013 if ((rt->rt_flags & RTF_GATEWAY) || 2014 (rt->rt_ifp != ifp)) { 2015 rt->rt_refcnt--; 2016 rt0->rt_gwroute = NULL; 2017 senderr(EHOSTUNREACH); 2018 } 2019 } 2020 } 2021 } 2022 2023 /* 2024 * Address resolution or Neighbor Unreachability Detection 2025 * for the next hop. 2026 * At this point, the destination of the packet must be a unicast 2027 * or an anycast address(i.e. not a multicast). 2028 */ 2029 2030 /* Look up the neighbor cache for the nexthop */ 2031 if (rt != NULL && (rt->rt_flags & RTF_LLINFO) != 0) 2032 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 2033 else { 2034 /* 2035 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 2036 * the condition below is not very efficient. But we believe 2037 * it is tolerable, because this should be a rare case. 2038 */ 2039 if (nd6_is_addr_neighbor(dst, ifp) && 2040 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL) 2041 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 2042 } 2043 if (ln == NULL || rt == NULL) { 2044 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 && 2045 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) { 2046 log(LOG_DEBUG, 2047 "nd6_output: can't allocate llinfo for %s " 2048 "(ln=%p, rt=%p)\n", 2049 ip6_sprintf(&dst->sin6_addr), ln, rt); 2050 senderr(EIO); /* XXX: good error? */ 2051 } 2052 2053 goto sendpkt; /* send anyway */ 2054 } 2055 2056 /* We don't have to do link-layer address resolution on a p2p link. */ 2057 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 2058 ln->ln_state < ND6_LLINFO_REACHABLE) { 2059 ln->ln_state = ND6_LLINFO_STALE; 2060 nd6_llinfo_settimer(ln, (long)nd6_gctimer * hz); 2061 } 2062 2063 /* 2064 * The first time we send a packet to a neighbor whose entry is 2065 * STALE, we have to change the state to DELAY and a sets a timer to 2066 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 2067 * neighbor unreachability detection on expiration. 2068 * (RFC 2461 7.3.3) 2069 */ 2070 if (ln->ln_state == ND6_LLINFO_STALE) { 2071 ln->ln_asked = 0; 2072 ln->ln_state = ND6_LLINFO_DELAY; 2073 nd6_llinfo_settimer(ln, (long)nd6_delay * hz); 2074 } 2075 2076 /* 2077 * If the neighbor cache entry has a state other than INCOMPLETE 2078 * (i.e. its link-layer address is already resolved), just 2079 * send the packet. 2080 */ 2081 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 2082 goto sendpkt; 2083 2084 /* 2085 * There is a neighbor cache entry, but no ethernet address 2086 * response yet. Append this latest packet to the end of the 2087 * packet queue in the mbuf, unless the number of the packet 2088 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen, 2089 * the oldest packet in the queue will be removed. 2090 */ 2091 if (ln->ln_state == ND6_LLINFO_NOSTATE) 2092 ln->ln_state = ND6_LLINFO_INCOMPLETE; 2093 if (ln->ln_hold) { 2094 struct mbuf *m_hold; 2095 int i; 2096 2097 i = 0; 2098 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold->m_nextpkt) { 2099 i++; 2100 if (m_hold->m_nextpkt == NULL) { 2101 m_hold->m_nextpkt = m; 2102 break; 2103 } 2104 } 2105 while (i >= nd6_maxqueuelen) { 2106 m_hold = ln->ln_hold; 2107 ln->ln_hold = ln->ln_hold->m_nextpkt; 2108 m_freem(m_hold); 2109 i--; 2110 } 2111 } else { 2112 ln->ln_hold = m; 2113 } 2114 2115 /* 2116 * If there has been no NS for the neighbor after entering the 2117 * INCOMPLETE state, send the first solicitation. 2118 */ 2119 if (!ND6_LLINFO_PERMANENT(ln) && ln->ln_asked == 0) { 2120 ln->ln_asked++; 2121 nd6_llinfo_settimer(ln, 2122 (long)ND_IFINFO(ifp)->retrans * hz / 1000); 2123 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0); 2124 } 2125 return 0; 2126 2127 sendpkt: 2128 /* discard the packet if IPv6 operation is disabled on the interface */ 2129 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) { 2130 error = ENETDOWN; /* better error? */ 2131 goto bad; 2132 } 2133 2134 #ifdef KAME_IPSEC 2135 /* clean ipsec history once it goes out of the node */ 2136 ipsec_delaux(m); 2137 #endif 2138 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2139 return (*ifp->if_output)(origifp, m, sin6tocsa(dst), rt); 2140 return (*ifp->if_output)(ifp, m, sin6tocsa(dst), rt); 2141 2142 bad: 2143 if (m != NULL) 2144 m_freem(m); 2145 return error; 2146 } 2147 #undef senderr 2148 2149 int 2150 nd6_need_cache(struct ifnet *ifp) 2151 { 2152 /* 2153 * XXX: we currently do not make neighbor cache on any interface 2154 * other than ARCnet, Ethernet, FDDI and GIF. 2155 * 2156 * RFC2893 says: 2157 * - unidirectional tunnels needs no ND 2158 */ 2159 switch (ifp->if_type) { 2160 case IFT_ARCNET: 2161 case IFT_ETHER: 2162 case IFT_FDDI: 2163 case IFT_IEEE1394: 2164 case IFT_CARP: 2165 case IFT_GIF: /* XXX need more cases? */ 2166 case IFT_PPP: 2167 case IFT_TUNNEL: 2168 return 1; 2169 default: 2170 return 0; 2171 } 2172 } 2173 2174 int 2175 nd6_storelladdr(const struct ifnet *ifp, const struct rtentry *rt, 2176 struct mbuf *m, const struct sockaddr *dst, uint8_t *lldst, 2177 size_t dstsize) 2178 { 2179 const struct sockaddr_dl *sdl; 2180 2181 if (m->m_flags & M_MCAST) { 2182 switch (ifp->if_type) { 2183 case IFT_ETHER: 2184 case IFT_FDDI: 2185 ETHER_MAP_IPV6_MULTICAST(&satocsin6(dst)->sin6_addr, 2186 lldst); 2187 return 1; 2188 case IFT_IEEE1394: 2189 memcpy(lldst, ifp->if_broadcastaddr, 2190 MIN(dstsize, ifp->if_addrlen)); 2191 return 1; 2192 case IFT_ARCNET: 2193 *lldst = 0; 2194 return 1; 2195 default: 2196 m_freem(m); 2197 return 0; 2198 } 2199 } 2200 2201 if (rt == NULL) { 2202 /* this could happen, if we could not allocate memory */ 2203 m_freem(m); 2204 return 0; 2205 } 2206 if (rt->rt_gateway->sa_family != AF_LINK) { 2207 printf("%s: something odd happens\n", __func__); 2208 m_freem(m); 2209 return 0; 2210 } 2211 sdl = satocsdl(rt->rt_gateway); 2212 if (sdl->sdl_alen == 0 || sdl->sdl_alen > dstsize) { 2213 /* this should be impossible, but we bark here for debugging */ 2214 printf("%s: sdl_alen == %" PRIu8 ", dst=%s, if=%s\n", __func__, 2215 sdl->sdl_alen, ip6_sprintf(&satocsin6(dst)->sin6_addr), 2216 if_name(ifp)); 2217 m_freem(m); 2218 return 0; 2219 } 2220 2221 memcpy(lldst, CLLADDR(sdl), MIN(dstsize, sdl->sdl_alen)); 2222 return 1; 2223 } 2224 2225 static void 2226 clear_llinfo_pqueue(struct llinfo_nd6 *ln) 2227 { 2228 struct mbuf *m_hold, *m_hold_next; 2229 2230 for (m_hold = ln->ln_hold; m_hold; m_hold = m_hold_next) { 2231 m_hold_next = m_hold->m_nextpkt; 2232 m_hold->m_nextpkt = NULL; 2233 m_freem(m_hold); 2234 } 2235 2236 ln->ln_hold = NULL; 2237 return; 2238 } 2239 2240 int 2241 nd6_sysctl( 2242 int name, 2243 void *oldp, /* syscall arg, need copyout */ 2244 size_t *oldlenp, 2245 void *newp, /* syscall arg, need copyin */ 2246 size_t newlen 2247 ) 2248 { 2249 void *p; 2250 size_t ol; 2251 int error; 2252 2253 error = 0; 2254 2255 if (newp) 2256 return EPERM; 2257 if (oldp && !oldlenp) 2258 return EINVAL; 2259 ol = oldlenp ? *oldlenp : 0; 2260 2261 if (oldp) { 2262 p = malloc(*oldlenp, M_TEMP, M_WAITOK); 2263 if (p == NULL) 2264 return ENOMEM; 2265 } else 2266 p = NULL; 2267 switch (name) { 2268 case ICMPV6CTL_ND6_DRLIST: 2269 error = fill_drlist(p, oldlenp, ol); 2270 if (!error && p != NULL && oldp != NULL) 2271 error = copyout(p, oldp, *oldlenp); 2272 break; 2273 2274 case ICMPV6CTL_ND6_PRLIST: 2275 error = fill_prlist(p, oldlenp, ol); 2276 if (!error && p != NULL && oldp != NULL) 2277 error = copyout(p, oldp, *oldlenp); 2278 break; 2279 2280 case ICMPV6CTL_ND6_MAXQLEN: 2281 break; 2282 2283 default: 2284 error = ENOPROTOOPT; 2285 break; 2286 } 2287 if (p) 2288 free(p, M_TEMP); 2289 2290 return error; 2291 } 2292 2293 static int 2294 fill_drlist(void *oldp, size_t *oldlenp, size_t ol) 2295 { 2296 int error = 0, s; 2297 struct in6_defrouter *d = NULL, *de = NULL; 2298 struct nd_defrouter *dr; 2299 size_t l; 2300 2301 s = splsoftnet(); 2302 2303 if (oldp) { 2304 d = (struct in6_defrouter *)oldp; 2305 de = (struct in6_defrouter *)((char *)oldp + *oldlenp); 2306 } 2307 l = 0; 2308 2309 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) { 2310 2311 if (oldp && d + 1 <= de) { 2312 memset(d, 0, sizeof(*d)); 2313 sockaddr_in6_init(&d->rtaddr, &dr->rtaddr, 0, 0, 0); 2314 if (sa6_recoverscope(&d->rtaddr)) { 2315 log(LOG_ERR, 2316 "scope error in router list (%s)\n", 2317 ip6_sprintf(&d->rtaddr.sin6_addr)); 2318 /* XXX: press on... */ 2319 } 2320 d->flags = dr->flags; 2321 d->rtlifetime = dr->rtlifetime; 2322 d->expire = dr->expire; 2323 d->if_index = dr->ifp->if_index; 2324 } 2325 2326 l += sizeof(*d); 2327 if (d) 2328 d++; 2329 } 2330 2331 if (oldp) { 2332 if (l > ol) 2333 error = ENOMEM; 2334 } 2335 if (oldlenp) 2336 *oldlenp = l; /* (void *)d - (void *)oldp */ 2337 2338 splx(s); 2339 2340 return error; 2341 } 2342 2343 static int 2344 fill_prlist(void *oldp, size_t *oldlenp, size_t ol) 2345 { 2346 int error = 0, s; 2347 struct nd_prefix *pr; 2348 struct in6_prefix *p = NULL; 2349 struct in6_prefix *pe = NULL; 2350 size_t l; 2351 2352 s = splsoftnet(); 2353 2354 if (oldp) { 2355 p = (struct in6_prefix *)oldp; 2356 pe = (struct in6_prefix *)((char *)oldp + *oldlenp); 2357 } 2358 l = 0; 2359 2360 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 2361 u_short advrtrs; 2362 size_t advance; 2363 struct sockaddr_in6 *sin6; 2364 struct sockaddr_in6 *s6; 2365 struct nd_pfxrouter *pfr; 2366 2367 if (oldp && p + 1 <= pe) 2368 { 2369 memset(p, 0, sizeof(*p)); 2370 sin6 = (struct sockaddr_in6 *)(p + 1); 2371 2372 p->prefix = pr->ndpr_prefix; 2373 if (sa6_recoverscope(&p->prefix)) { 2374 log(LOG_ERR, 2375 "scope error in prefix list (%s)\n", 2376 ip6_sprintf(&p->prefix.sin6_addr)); 2377 /* XXX: press on... */ 2378 } 2379 p->raflags = pr->ndpr_raf; 2380 p->prefixlen = pr->ndpr_plen; 2381 p->vltime = pr->ndpr_vltime; 2382 p->pltime = pr->ndpr_pltime; 2383 p->if_index = pr->ndpr_ifp->if_index; 2384 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2385 p->expire = 0; 2386 else { 2387 time_t maxexpire; 2388 2389 /* XXX: we assume time_t is signed. */ 2390 maxexpire = (-1) & 2391 ~((time_t)1 << 2392 ((sizeof(maxexpire) * 8) - 1)); 2393 if (pr->ndpr_vltime < 2394 maxexpire - pr->ndpr_lastupdate) { 2395 p->expire = pr->ndpr_lastupdate + 2396 pr->ndpr_vltime; 2397 } else 2398 p->expire = maxexpire; 2399 } 2400 p->refcnt = pr->ndpr_refcnt; 2401 p->flags = pr->ndpr_stateflags; 2402 p->origin = PR_ORIG_RA; 2403 advrtrs = 0; 2404 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) { 2405 if ((void *)&sin6[advrtrs + 1] > (void *)pe) { 2406 advrtrs++; 2407 continue; 2408 } 2409 s6 = &sin6[advrtrs]; 2410 sockaddr_in6_init(s6, &pfr->router->rtaddr, 2411 0, 0, 0); 2412 if (sa6_recoverscope(s6)) { 2413 log(LOG_ERR, 2414 "scope error in " 2415 "prefix list (%s)\n", 2416 ip6_sprintf(&pfr->router->rtaddr)); 2417 } 2418 advrtrs++; 2419 } 2420 p->advrtrs = advrtrs; 2421 } 2422 else { 2423 advrtrs = 0; 2424 LIST_FOREACH(pfr, &pr->ndpr_advrtrs, pfr_entry) 2425 advrtrs++; 2426 } 2427 2428 advance = sizeof(*p) + sizeof(*sin6) * advrtrs; 2429 l += advance; 2430 if (p) 2431 p = (struct in6_prefix *)((char *)p + advance); 2432 } 2433 2434 if (oldp) { 2435 *oldlenp = l; /* (void *)d - (void *)oldp */ 2436 if (l > ol) 2437 error = ENOMEM; 2438 } else 2439 *oldlenp = l; 2440 2441 splx(s); 2442 2443 return error; 2444 } 2445