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