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