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