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