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