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