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