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