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