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