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