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