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