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