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