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