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