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