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