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