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