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$"); 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 89 int nd6_prune; 90 int nd6_delay; 91 int nd6_umaxtries; 92 int nd6_mmaxtries; 93 int nd6_useloopback; 94 int nd6_gctimer; 95 96 /* preventing too many loops in ND option parsing */ 97 int nd6_maxndopt; 98 99 int nd6_maxnudhint; 100 int nd6_maxqueuelen; 101 102 int nd6_debug; 103 104 /* for debugging? */ 105 #if 0 106 static int nd6_inuse, nd6_allocated; 107 #endif 108 109 struct nd_drhead nd_defrouter; 110 struct nd_prhead nd_prefix; 111 112 int nd6_recalc_reachtm_interval; 113 #endif /* VIMAGE_GLOBALS */ 114 115 static struct sockaddr_in6 all1_sa; 116 117 static int nd6_is_new_addr_neighbor __P((struct sockaddr_in6 *, 118 struct ifnet *)); 119 static void nd6_setmtu0(struct ifnet *, struct nd_ifinfo *); 120 static void nd6_slowtimo(void *); 121 static int regen_tmpaddr(struct in6_ifaddr *); 122 static struct llentry *nd6_free(struct llentry *, int); 123 static void nd6_llinfo_timer(void *); 124 static void clear_llinfo_pqueue(struct llentry *); 125 126 #ifdef VIMAGE_GLOBALS 127 struct callout nd6_slowtimo_ch; 128 struct callout nd6_timer_ch; 129 extern struct callout in6_tmpaddrtimer_ch; 130 extern int dad_ignore_ns; 131 extern int dad_maxtry; 132 #endif 133 134 void 135 nd6_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 200 struct nd_ifinfo * 201 nd6_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 227 void 228 nd6_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 */ 238 void 239 nd6_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 */ 246 void 247 nd6_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 287 void 288 nd6_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 */ 305 struct nd_opt_hdr * 306 nd6_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 */ 356 int 357 nd6_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 418 skip1: 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 */ 436 void 437 nd6_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 466 void 467 nd6_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 475 static void 476 nd6_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(); 583 done: 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 */ 592 void 593 nd6_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 */ 722 static int 723 regen_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 */ 788 void 789 nd6_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 */ 869 struct llentry * 870 nd6_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 */ 903 static int 904 nd6_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 */ 978 int 979 nd6_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 */ 1007 static struct llentry * 1008 nd6_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 */ 1114 void 1115 nd6_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 } 1150 done: 1151 LLE_WUNLOCK(ln); 1152 } 1153 1154 1155 int 1156 nd6_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 */ 1396 struct llentry * 1397 nd6_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 *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, &chain); 1530 } 1531 /* 1532 * If we have mbufs in the chain we need to do 1533 * deferred transmit. Copy the address from the 1534 * llentry before dropping the lock down below. 1535 */ 1536 if (chain != NULL) 1537 memcpy(&sin6, L3_ADDR_SIN6(ln), sizeof(sin6)); 1538 } 1539 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 1540 /* probe right away */ 1541 nd6_llinfo_settimer_locked((void *)ln, 0); 1542 } 1543 } 1544 1545 /* 1546 * ICMP6 type dependent behavior. 1547 * 1548 * NS: clear IsRouter if new entry 1549 * RS: clear IsRouter 1550 * RA: set IsRouter if there's lladdr 1551 * redir: clear IsRouter if new entry 1552 * 1553 * RA case, (1): 1554 * The spec says that we must set IsRouter in the following cases: 1555 * - If lladdr exist, set IsRouter. This means (1-5). 1556 * - If it is old entry (!newentry), set IsRouter. This means (7). 1557 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 1558 * A quetion arises for (1) case. (1) case has no lladdr in the 1559 * neighbor cache, this is similar to (6). 1560 * This case is rare but we figured that we MUST NOT set IsRouter. 1561 * 1562 * newentry olladdr lladdr llchange NS RS RA redir 1563 * D R 1564 * 0 n n -- (1) c ? s 1565 * 0 y n -- (2) c s s 1566 * 0 n y -- (3) c s s 1567 * 0 y y n (4) c s s 1568 * 0 y y y (5) c s s 1569 * 1 -- n -- (6) c c c s 1570 * 1 -- y -- (7) c c s c s 1571 * 1572 * (c=clear s=set) 1573 */ 1574 switch (type & 0xff) { 1575 case ND_NEIGHBOR_SOLICIT: 1576 /* 1577 * New entry must have is_router flag cleared. 1578 */ 1579 if (is_newentry) /* (6-7) */ 1580 ln->ln_router = 0; 1581 break; 1582 case ND_REDIRECT: 1583 /* 1584 * If the icmp is a redirect to a better router, always set the 1585 * is_router flag. Otherwise, if the entry is newly created, 1586 * clear the flag. [RFC 2461, sec 8.3] 1587 */ 1588 if (code == ND_REDIRECT_ROUTER) 1589 ln->ln_router = 1; 1590 else if (is_newentry) /* (6-7) */ 1591 ln->ln_router = 0; 1592 break; 1593 case ND_ROUTER_SOLICIT: 1594 /* 1595 * is_router flag must always be cleared. 1596 */ 1597 ln->ln_router = 0; 1598 break; 1599 case ND_ROUTER_ADVERT: 1600 /* 1601 * Mark an entry with lladdr as a router. 1602 */ 1603 if ((!is_newentry && (olladdr || lladdr)) || /* (2-5) */ 1604 (is_newentry && lladdr)) { /* (7) */ 1605 ln->ln_router = 1; 1606 } 1607 break; 1608 } 1609 1610 if (ln != NULL) { 1611 static_route = (ln->la_flags & LLE_STATIC); 1612 router = ln->ln_router; 1613 1614 if (flags & ND6_EXCLUSIVE) 1615 LLE_WUNLOCK(ln); 1616 else 1617 LLE_RUNLOCK(ln); 1618 if (static_route) 1619 ln = NULL; 1620 } 1621 if (chain) 1622 nd6_output_flush(ifp, ifp, chain, &sin6, NULL); 1623 1624 /* 1625 * When the link-layer address of a router changes, select the 1626 * best router again. In particular, when the neighbor entry is newly 1627 * created, it might affect the selection policy. 1628 * Question: can we restrict the first condition to the "is_newentry" 1629 * case? 1630 * XXX: when we hear an RA from a new router with the link-layer 1631 * address option, defrouter_select() is called twice, since 1632 * defrtrlist_update called the function as well. However, I believe 1633 * we can compromise the overhead, since it only happens the first 1634 * time. 1635 * XXX: although defrouter_select() should not have a bad effect 1636 * for those are not autoconfigured hosts, we explicitly avoid such 1637 * cases for safety. 1638 */ 1639 if (do_update && router && !V_ip6_forwarding && V_ip6_accept_rtadv) { 1640 /* 1641 * guaranteed recursion 1642 */ 1643 defrouter_select(); 1644 } 1645 1646 return (ln); 1647 done: 1648 if (ln != NULL) { 1649 if (flags & ND6_EXCLUSIVE) 1650 LLE_WUNLOCK(ln); 1651 else 1652 LLE_RUNLOCK(ln); 1653 if (static_route) 1654 ln = NULL; 1655 } 1656 return (ln); 1657 } 1658 1659 static void 1660 nd6_slowtimo(void *arg) 1661 { 1662 CURVNET_SET((struct vnet *) arg); 1663 INIT_VNET_NET((struct vnet *) arg); 1664 INIT_VNET_INET6((struct vnet *) arg); 1665 struct nd_ifinfo *nd6if; 1666 struct ifnet *ifp; 1667 1668 callout_reset(&V_nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 1669 nd6_slowtimo, NULL); 1670 IFNET_RLOCK(); 1671 for (ifp = TAILQ_FIRST(&V_ifnet); ifp; 1672 ifp = TAILQ_NEXT(ifp, if_list)) { 1673 nd6if = ND_IFINFO(ifp); 1674 if (nd6if->basereachable && /* already initialized */ 1675 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 1676 /* 1677 * Since reachable time rarely changes by router 1678 * advertisements, we SHOULD insure that a new random 1679 * value gets recomputed at least once every few hours. 1680 * (RFC 2461, 6.3.4) 1681 */ 1682 nd6if->recalctm = V_nd6_recalc_reachtm_interval; 1683 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); 1684 } 1685 } 1686 IFNET_RUNLOCK(); 1687 CURVNET_RESTORE(); 1688 } 1689 1690 int 1691 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0, 1692 struct sockaddr_in6 *dst, struct rtentry *rt0) 1693 { 1694 1695 return (nd6_output_lle(ifp, origifp, m0, dst, rt0, NULL, NULL)); 1696 } 1697 1698 1699 /* 1700 * Note that I'm not enforcing any global serialization 1701 * lle state or asked changes here as the logic is too 1702 * complicated to avoid having to always acquire an exclusive 1703 * lock 1704 * KMM 1705 * 1706 */ 1707 #define senderr(e) { error = (e); goto bad;} 1708 1709 int 1710 nd6_output_lle(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0, 1711 struct sockaddr_in6 *dst, struct rtentry *rt0, struct llentry *lle, 1712 struct mbuf **chain) 1713 { 1714 INIT_VNET_INET6(curvnet); 1715 struct mbuf *m = m0; 1716 struct llentry *ln = lle; 1717 int error = 0; 1718 int flags = 0; 1719 1720 #ifdef INVARIANTS 1721 if (lle != NULL) { 1722 1723 LLE_WLOCK_ASSERT(lle); 1724 1725 KASSERT(chain != NULL, (" lle locked but no mbuf chain pointer passed")); 1726 } 1727 #endif 1728 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr)) 1729 goto sendpkt; 1730 1731 if (nd6_need_cache(ifp) == 0) 1732 goto sendpkt; 1733 1734 /* 1735 * next hop determination. This routine is derived from ether_output. 1736 */ 1737 1738 /* 1739 * Address resolution or Neighbor Unreachability Detection 1740 * for the next hop. 1741 * At this point, the destination of the packet must be a unicast 1742 * or an anycast address(i.e. not a multicast). 1743 */ 1744 1745 flags = ((m != NULL) || (lle != NULL)) ? LLE_EXCLUSIVE : 0; 1746 if (ln == NULL) { 1747 retry: 1748 IF_AFDATA_LOCK(ifp); 1749 ln = lla_lookup(LLTABLE6(ifp), flags, (struct sockaddr *)dst); 1750 IF_AFDATA_UNLOCK(ifp); 1751 if ((ln == NULL) && nd6_is_addr_neighbor(dst, ifp)) { 1752 /* 1753 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 1754 * the condition below is not very efficient. But we believe 1755 * it is tolerable, because this should be a rare case. 1756 */ 1757 flags = ND6_CREATE | (m ? ND6_EXCLUSIVE : 0); 1758 IF_AFDATA_LOCK(ifp); 1759 ln = nd6_lookup(&dst->sin6_addr, flags, ifp); 1760 IF_AFDATA_UNLOCK(ifp); 1761 } 1762 } 1763 if (ln == NULL) { 1764 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 && 1765 !(ND_IFINFO(ifp)->flags & ND6_IFF_PERFORMNUD)) { 1766 char ip6buf[INET6_ADDRSTRLEN]; 1767 log(LOG_DEBUG, 1768 "nd6_output: can't allocate llinfo for %s " 1769 "(ln=%p)\n", 1770 ip6_sprintf(ip6buf, &dst->sin6_addr), ln); 1771 senderr(EIO); /* XXX: good error? */ 1772 } 1773 goto sendpkt; /* send anyway */ 1774 } 1775 1776 /* We don't have to do link-layer address resolution on a p2p link. */ 1777 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 1778 ln->ln_state < ND6_LLINFO_REACHABLE) { 1779 if ((flags & LLE_EXCLUSIVE) == 0) { 1780 flags |= LLE_EXCLUSIVE; 1781 goto retry; 1782 } 1783 ln->ln_state = ND6_LLINFO_STALE; 1784 nd6_llinfo_settimer_locked(ln, (long)V_nd6_gctimer * hz); 1785 } 1786 1787 /* 1788 * The first time we send a packet to a neighbor whose entry is 1789 * STALE, we have to change the state to DELAY and a sets a timer to 1790 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 1791 * neighbor unreachability detection on expiration. 1792 * (RFC 2461 7.3.3) 1793 */ 1794 if (ln->ln_state == ND6_LLINFO_STALE) { 1795 if ((flags & LLE_EXCLUSIVE) == 0) { 1796 flags |= LLE_EXCLUSIVE; 1797 LLE_RUNLOCK(ln); 1798 goto retry; 1799 } 1800 ln->la_asked = 0; 1801 ln->ln_state = ND6_LLINFO_DELAY; 1802 nd6_llinfo_settimer_locked(ln, (long)V_nd6_delay * hz); 1803 } 1804 1805 /* 1806 * If the neighbor cache entry has a state other than INCOMPLETE 1807 * (i.e. its link-layer address is already resolved), just 1808 * send the packet. 1809 */ 1810 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 1811 goto sendpkt; 1812 1813 /* 1814 * There is a neighbor cache entry, but no ethernet address 1815 * response yet. Append this latest packet to the end of the 1816 * packet queue in the mbuf, unless the number of the packet 1817 * does not exceed nd6_maxqueuelen. When it exceeds nd6_maxqueuelen, 1818 * the oldest packet in the queue will be removed. 1819 */ 1820 if (ln->ln_state == ND6_LLINFO_NOSTATE) 1821 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1822 1823 if ((flags & LLE_EXCLUSIVE) == 0) { 1824 flags |= LLE_EXCLUSIVE; 1825 LLE_RUNLOCK(ln); 1826 goto retry; 1827 } 1828 if (ln->la_hold) { 1829 struct mbuf *m_hold; 1830 int i; 1831 1832 i = 0; 1833 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold->m_nextpkt) { 1834 i++; 1835 if (m_hold->m_nextpkt == NULL) { 1836 m_hold->m_nextpkt = m; 1837 break; 1838 } 1839 } 1840 while (i >= V_nd6_maxqueuelen) { 1841 m_hold = ln->la_hold; 1842 ln->la_hold = ln->la_hold->m_nextpkt; 1843 m_freem(m_hold); 1844 i--; 1845 } 1846 } else { 1847 ln->la_hold = m; 1848 } 1849 /* 1850 * We did the lookup (no lle arg) so we 1851 * need to do the unlock here 1852 */ 1853 if (lle == NULL) { 1854 if (flags & LLE_EXCLUSIVE) 1855 LLE_WUNLOCK(ln); 1856 else 1857 LLE_RUNLOCK(ln); 1858 } 1859 1860 /* 1861 * If there has been no NS for the neighbor after entering the 1862 * INCOMPLETE state, send the first solicitation. 1863 */ 1864 if (!ND6_LLINFO_PERMANENT(ln) && ln->la_asked == 0) { 1865 ln->la_asked++; 1866 1867 nd6_llinfo_settimer(ln, 1868 (long)ND_IFINFO(ifp)->retrans * hz / 1000); 1869 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0); 1870 } 1871 return (0); 1872 1873 sendpkt: 1874 /* discard the packet if IPv6 operation is disabled on the interface */ 1875 if ((ND_IFINFO(ifp)->flags & ND6_IFF_IFDISABLED)) { 1876 error = ENETDOWN; /* better error? */ 1877 goto bad; 1878 } 1879 /* 1880 * ln is valid and the caller did not pass in 1881 * an llentry 1882 */ 1883 if ((ln != NULL) && (lle == NULL)) { 1884 if (flags & LLE_EXCLUSIVE) 1885 LLE_WUNLOCK(ln); 1886 else 1887 LLE_RUNLOCK(ln); 1888 } 1889 1890 #ifdef MAC 1891 mac_netinet6_nd6_send(ifp, m); 1892 #endif 1893 /* 1894 * We were passed in a pointer to an lle with the lock held 1895 * this means that we can't call if_output as we will 1896 * recurse on the lle lock - so what we do is we create 1897 * a list of mbufs to send and transmit them in the caller 1898 * after the lock is dropped 1899 */ 1900 if (lle != NULL) { 1901 if (*chain == NULL) 1902 *chain = m; 1903 else { 1904 struct mbuf *m = *chain; 1905 1906 /* 1907 * append mbuf to end of deferred chain 1908 */ 1909 while (m->m_nextpkt != NULL) 1910 m = m->m_nextpkt; 1911 m->m_nextpkt = m; 1912 } 1913 return (error); 1914 } 1915 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 1916 return ((*ifp->if_output)(origifp, m, (struct sockaddr *)dst, 1917 NULL)); 1918 } 1919 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, NULL); 1920 return (error); 1921 1922 bad: 1923 /* 1924 * ln is valid and the caller did not pass in 1925 * an llentry 1926 */ 1927 if ((ln != NULL) && (lle == NULL)) { 1928 if (flags & LLE_EXCLUSIVE) 1929 LLE_WUNLOCK(ln); 1930 else 1931 LLE_RUNLOCK(ln); 1932 } 1933 if (m) 1934 m_freem(m); 1935 return (error); 1936 } 1937 #undef senderr 1938 1939 1940 int 1941 nd6_output_flush(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *chain, 1942 struct sockaddr_in6 *dst, struct rtentry *rt) 1943 { 1944 struct mbuf *m, *m_head; 1945 struct ifnet *outifp; 1946 int error = 0; 1947 1948 m_head = chain; 1949 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 1950 outifp = origifp; 1951 else 1952 outifp = ifp; 1953 1954 while (m_head) { 1955 m = m_head; 1956 m_head = m_head->m_nextpkt; 1957 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt); 1958 } 1959 1960 /* 1961 * XXX 1962 * note that intermediate errors are blindly ignored - but this is 1963 * the same convention as used with nd6_output when called by 1964 * nd6_cache_lladdr 1965 */ 1966 return (error); 1967 } 1968 1969 1970 int 1971 nd6_need_cache(struct ifnet *ifp) 1972 { 1973 /* 1974 * XXX: we currently do not make neighbor cache on any interface 1975 * other than ARCnet, Ethernet, FDDI and GIF. 1976 * 1977 * RFC2893 says: 1978 * - unidirectional tunnels needs no ND 1979 */ 1980 switch (ifp->if_type) { 1981 case IFT_ARCNET: 1982 case IFT_ETHER: 1983 case IFT_FDDI: 1984 case IFT_IEEE1394: 1985 #ifdef IFT_L2VLAN 1986 case IFT_L2VLAN: 1987 #endif 1988 #ifdef IFT_IEEE80211 1989 case IFT_IEEE80211: 1990 #endif 1991 #ifdef IFT_CARP 1992 case IFT_CARP: 1993 #endif 1994 case IFT_GIF: /* XXX need more cases? */ 1995 case IFT_PPP: 1996 case IFT_TUNNEL: 1997 case IFT_BRIDGE: 1998 case IFT_PROPVIRTUAL: 1999 return (1); 2000 default: 2001 return (0); 2002 } 2003 } 2004 2005 /* 2006 * the callers of this function need to be re-worked to drop 2007 * the lle lock, drop here for now 2008 */ 2009 int 2010 nd6_storelladdr(struct ifnet *ifp, struct mbuf *m, 2011 struct sockaddr *dst, u_char *desten, struct llentry **lle) 2012 { 2013 struct llentry *ln; 2014 2015 *lle = NULL; 2016 IF_AFDATA_UNLOCK_ASSERT(ifp); 2017 if (m->m_flags & M_MCAST) { 2018 int i; 2019 2020 switch (ifp->if_type) { 2021 case IFT_ETHER: 2022 case IFT_FDDI: 2023 #ifdef IFT_L2VLAN 2024 case IFT_L2VLAN: 2025 #endif 2026 #ifdef IFT_IEEE80211 2027 case IFT_IEEE80211: 2028 #endif 2029 case IFT_BRIDGE: 2030 case IFT_ISO88025: 2031 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, 2032 desten); 2033 return (0); 2034 case IFT_IEEE1394: 2035 /* 2036 * netbsd can use if_broadcastaddr, but we don't do so 2037 * to reduce # of ifdef. 2038 */ 2039 for (i = 0; i < ifp->if_addrlen; i++) 2040 desten[i] = ~0; 2041 return (0); 2042 case IFT_ARCNET: 2043 *desten = 0; 2044 return (0); 2045 default: 2046 m_freem(m); 2047 return (EAFNOSUPPORT); 2048 } 2049 } 2050 2051 2052 /* 2053 * the entry should have been created in nd6_store_lladdr 2054 */ 2055 IF_AFDATA_LOCK(ifp); 2056 ln = lla_lookup(LLTABLE6(ifp), 0, dst); 2057 IF_AFDATA_UNLOCK(ifp); 2058 if ((ln == NULL) || !(ln->la_flags & LLE_VALID)) { 2059 if (ln != NULL) 2060 LLE_RUNLOCK(ln); 2061 /* this could happen, if we could not allocate memory */ 2062 m_freem(m); 2063 return (1); 2064 } 2065 2066 bcopy(&ln->ll_addr, desten, ifp->if_addrlen); 2067 *lle = ln; 2068 LLE_RUNLOCK(ln); 2069 /* 2070 * A *small* use after free race exists here 2071 */ 2072 return (0); 2073 } 2074 2075 static void 2076 clear_llinfo_pqueue(struct llentry *ln) 2077 { 2078 struct mbuf *m_hold, *m_hold_next; 2079 2080 for (m_hold = ln->la_hold; m_hold; m_hold = m_hold_next) { 2081 m_hold_next = m_hold->m_nextpkt; 2082 m_hold->m_nextpkt = NULL; 2083 m_freem(m_hold); 2084 } 2085 2086 ln->la_hold = NULL; 2087 return; 2088 } 2089 2090 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS); 2091 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS); 2092 #ifdef SYSCTL_DECL 2093 SYSCTL_DECL(_net_inet6_icmp6); 2094 #endif 2095 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist, 2096 CTLFLAG_RD, nd6_sysctl_drlist, ""); 2097 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist, 2098 CTLFLAG_RD, nd6_sysctl_prlist, ""); 2099 SYSCTL_V_INT(V_NET, vnet_inet6, _net_inet6_icmp6, ICMPV6CTL_ND6_MAXQLEN, 2100 nd6_maxqueuelen, CTLFLAG_RW, nd6_maxqueuelen, 1, ""); 2101 2102 static int 2103 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS) 2104 { 2105 INIT_VNET_INET6(curvnet); 2106 int error; 2107 char buf[1024] __aligned(4); 2108 struct in6_defrouter *d, *de; 2109 struct nd_defrouter *dr; 2110 2111 if (req->newptr) 2112 return EPERM; 2113 error = 0; 2114 2115 for (dr = TAILQ_FIRST(&V_nd_defrouter); dr; 2116 dr = TAILQ_NEXT(dr, dr_entry)) { 2117 d = (struct in6_defrouter *)buf; 2118 de = (struct in6_defrouter *)(buf + sizeof(buf)); 2119 2120 if (d + 1 <= de) { 2121 bzero(d, sizeof(*d)); 2122 d->rtaddr.sin6_family = AF_INET6; 2123 d->rtaddr.sin6_len = sizeof(d->rtaddr); 2124 d->rtaddr.sin6_addr = dr->rtaddr; 2125 error = sa6_recoverscope(&d->rtaddr); 2126 if (error != 0) 2127 return (error); 2128 d->flags = dr->flags; 2129 d->rtlifetime = dr->rtlifetime; 2130 d->expire = dr->expire; 2131 d->if_index = dr->ifp->if_index; 2132 } else 2133 panic("buffer too short"); 2134 2135 error = SYSCTL_OUT(req, buf, sizeof(*d)); 2136 if (error) 2137 break; 2138 } 2139 2140 return (error); 2141 } 2142 2143 static int 2144 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS) 2145 { 2146 INIT_VNET_INET6(curvnet); 2147 int error; 2148 char buf[1024] __aligned(4); 2149 struct in6_prefix *p, *pe; 2150 struct nd_prefix *pr; 2151 char ip6buf[INET6_ADDRSTRLEN]; 2152 2153 if (req->newptr) 2154 return EPERM; 2155 error = 0; 2156 2157 for (pr = V_nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 2158 u_short advrtrs; 2159 size_t advance; 2160 struct sockaddr_in6 *sin6, *s6; 2161 struct nd_pfxrouter *pfr; 2162 2163 p = (struct in6_prefix *)buf; 2164 pe = (struct in6_prefix *)(buf + sizeof(buf)); 2165 2166 if (p + 1 <= pe) { 2167 bzero(p, sizeof(*p)); 2168 sin6 = (struct sockaddr_in6 *)(p + 1); 2169 2170 p->prefix = pr->ndpr_prefix; 2171 if (sa6_recoverscope(&p->prefix)) { 2172 log(LOG_ERR, 2173 "scope error in prefix list (%s)\n", 2174 ip6_sprintf(ip6buf, &p->prefix.sin6_addr)); 2175 /* XXX: press on... */ 2176 } 2177 p->raflags = pr->ndpr_raf; 2178 p->prefixlen = pr->ndpr_plen; 2179 p->vltime = pr->ndpr_vltime; 2180 p->pltime = pr->ndpr_pltime; 2181 p->if_index = pr->ndpr_ifp->if_index; 2182 if (pr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2183 p->expire = 0; 2184 else { 2185 time_t maxexpire; 2186 2187 /* XXX: we assume time_t is signed. */ 2188 maxexpire = (-1) & 2189 ~((time_t)1 << 2190 ((sizeof(maxexpire) * 8) - 1)); 2191 if (pr->ndpr_vltime < 2192 maxexpire - pr->ndpr_lastupdate) { 2193 p->expire = pr->ndpr_lastupdate + 2194 pr->ndpr_vltime; 2195 } else 2196 p->expire = maxexpire; 2197 } 2198 p->refcnt = pr->ndpr_refcnt; 2199 p->flags = pr->ndpr_stateflags; 2200 p->origin = PR_ORIG_RA; 2201 advrtrs = 0; 2202 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; 2203 pfr = pfr->pfr_next) { 2204 if ((void *)&sin6[advrtrs + 1] > (void *)pe) { 2205 advrtrs++; 2206 continue; 2207 } 2208 s6 = &sin6[advrtrs]; 2209 bzero(s6, sizeof(*s6)); 2210 s6->sin6_family = AF_INET6; 2211 s6->sin6_len = sizeof(*sin6); 2212 s6->sin6_addr = pfr->router->rtaddr; 2213 if (sa6_recoverscope(s6)) { 2214 log(LOG_ERR, 2215 "scope error in " 2216 "prefix list (%s)\n", 2217 ip6_sprintf(ip6buf, 2218 &pfr->router->rtaddr)); 2219 } 2220 advrtrs++; 2221 } 2222 p->advrtrs = advrtrs; 2223 } else 2224 panic("buffer too short"); 2225 2226 advance = sizeof(*p) + sizeof(*sin6) * advrtrs; 2227 error = SYSCTL_OUT(req, buf, advance); 2228 if (error) 2229 break; 2230 } 2231 2232 return (error); 2233 } 2234