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.9 2004/08/13 14:27:29 drhodus 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 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_list) { 649 struct in6_ifaddr *it6; 650 651 if (ifa->ifa_addr->sa_family != AF_INET6) 652 continue; 653 654 it6 = (struct in6_ifaddr *)ifa; 655 656 /* ignore no autoconf addresses. */ 657 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 658 continue; 659 660 /* ignore autoconf addresses with different prefixes. */ 661 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) 662 continue; 663 664 /* 665 * Now we are looking at an autoconf address with the same 666 * prefix as ours. If the address is temporary and is still 667 * preferred, do not create another one. It would be rare, but 668 * could happen, for example, when we resume a laptop PC after 669 * a long period. 670 */ 671 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 672 !IFA6_IS_DEPRECATED(it6)) { 673 public_ifa6 = NULL; 674 break; 675 } 676 677 /* 678 * This is a public autoconf address that has the same prefix 679 * as ours. If it is preferred, keep it. We can't break the 680 * loop here, because there may be a still-preferred temporary 681 * address with the prefix. 682 */ 683 if (!IFA6_IS_DEPRECATED(it6)) 684 public_ifa6 = it6; 685 } 686 687 if (public_ifa6 != NULL) { 688 int e; 689 690 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) { 691 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" 692 " tmp addr,errno=%d\n", e); 693 return(-1); 694 } 695 return(0); 696 } 697 698 return(-1); 699 } 700 701 /* 702 * Nuke neighbor cache/prefix/default router management table, right before 703 * ifp goes away. 704 */ 705 void 706 nd6_purge(struct ifnet *ifp) 707 { 708 struct llinfo_nd6 *ln, *nln; 709 struct nd_defrouter *dr, *ndr, drany; 710 struct nd_prefix *pr, *npr; 711 712 /* Nuke default router list entries toward ifp */ 713 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) { 714 /* 715 * The first entry of the list may be stored in 716 * the routing table, so we'll delete it later. 717 */ 718 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) { 719 ndr = TAILQ_NEXT(dr, dr_entry); 720 if (dr->ifp == ifp) 721 defrtrlist_del(dr); 722 } 723 dr = TAILQ_FIRST(&nd_defrouter); 724 if (dr->ifp == ifp) 725 defrtrlist_del(dr); 726 } 727 728 /* Nuke prefix list entries toward ifp */ 729 for (pr = nd_prefix.lh_first; pr; pr = npr) { 730 npr = pr->ndpr_next; 731 if (pr->ndpr_ifp == ifp) { 732 /* 733 * Previously, pr->ndpr_addr is removed as well, 734 * but I strongly believe we don't have to do it. 735 * nd6_purge() is only called from in6_ifdetach(), 736 * which removes all the associated interface addresses 737 * by itself. 738 * (jinmei@kame.net 20010129) 739 */ 740 prelist_remove(pr); 741 } 742 } 743 744 /* cancel default outgoing interface setting */ 745 if (nd6_defifindex == ifp->if_index) 746 nd6_setdefaultiface(0); 747 748 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */ 749 /* refresh default router list */ 750 bzero(&drany, sizeof(drany)); 751 defrouter_delreq(&drany, 0); 752 defrouter_select(); 753 } 754 755 /* 756 * Nuke neighbor cache entries for the ifp. 757 * Note that rt->rt_ifp may not be the same as ifp, 758 * due to KAME goto ours hack. See RTM_RESOLVE case in 759 * nd6_rtrequest(), and ip6_input(). 760 */ 761 ln = llinfo_nd6.ln_next; 762 while (ln && ln != &llinfo_nd6) { 763 struct rtentry *rt; 764 struct sockaddr_dl *sdl; 765 766 nln = ln->ln_next; 767 rt = ln->ln_rt; 768 if (rt && rt->rt_gateway && 769 rt->rt_gateway->sa_family == AF_LINK) { 770 sdl = (struct sockaddr_dl *)rt->rt_gateway; 771 if (sdl->sdl_index == ifp->if_index) 772 nln = nd6_free(rt); 773 } 774 ln = nln; 775 } 776 } 777 778 struct rtentry * 779 nd6_lookup(struct in6_addr *addr6, int create, struct ifnet *ifp) 780 { 781 struct rtentry *rt; 782 struct sockaddr_in6 sin6; 783 784 bzero(&sin6, sizeof(sin6)); 785 sin6.sin6_len = sizeof(struct sockaddr_in6); 786 sin6.sin6_family = AF_INET6; 787 sin6.sin6_addr = *addr6; 788 #ifdef SCOPEDROUTING 789 sin6.sin6_scope_id = in6_addr2scopeid(ifp, addr6); 790 #endif 791 rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL); 792 if (rt && (rt->rt_flags & RTF_LLINFO) == 0) { 793 /* 794 * This is the case for the default route. 795 * If we want to create a neighbor cache for the address, we 796 * should free the route for the destination and allocate an 797 * interface route. 798 */ 799 if (create) { 800 RTFREE(rt); 801 rt = 0; 802 } 803 } 804 if (!rt) { 805 if (create && ifp) { 806 int e; 807 808 /* 809 * If no route is available and create is set, 810 * we allocate a host route for the destination 811 * and treat it like an interface route. 812 * This hack is necessary for a neighbor which can't 813 * be covered by our own prefix. 814 */ 815 struct ifaddr *ifa = 816 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp); 817 if (ifa == NULL) 818 return(NULL); 819 820 /* 821 * Create a new route. RTF_LLINFO is necessary 822 * to create a Neighbor Cache entry for the 823 * destination in nd6_rtrequest which will be 824 * called in rtrequest via ifa->ifa_rtrequest. 825 */ 826 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6, 827 ifa->ifa_addr, 828 (struct sockaddr *)&all1_sa, 829 (ifa->ifa_flags | 830 RTF_HOST | RTF_LLINFO) & 831 ~RTF_CLONING, 832 &rt)) != 0) 833 log(LOG_ERR, 834 "nd6_lookup: failed to add route for a " 835 "neighbor(%s), errno=%d\n", 836 ip6_sprintf(addr6), e); 837 if (rt == NULL) 838 return(NULL); 839 if (rt->rt_llinfo) { 840 struct llinfo_nd6 *ln = 841 (struct llinfo_nd6 *)rt->rt_llinfo; 842 ln->ln_state = ND6_LLINFO_NOSTATE; 843 } 844 } else 845 return(NULL); 846 } 847 rt->rt_refcnt--; 848 /* 849 * Validation for the entry. 850 * Note that the check for rt_llinfo is necessary because a cloned 851 * route from a parent route that has the L flag (e.g. the default 852 * route to a p2p interface) may have the flag, too, while the 853 * destination is not actually a neighbor. 854 * XXX: we can't use rt->rt_ifp to check for the interface, since 855 * it might be the loopback interface if the entry is for our 856 * own address on a non-loopback interface. Instead, we should 857 * use rt->rt_ifa->ifa_ifp, which would specify the REAL 858 * interface. 859 */ 860 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 861 rt->rt_gateway->sa_family != AF_LINK || rt->rt_llinfo == NULL || 862 (ifp && rt->rt_ifa->ifa_ifp != ifp)) { 863 if (create) { 864 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n", 865 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec"); 866 /* xxx more logs... kazu */ 867 } 868 return(NULL); 869 } 870 return(rt); 871 } 872 873 /* 874 * Detect if a given IPv6 address identifies a neighbor on a given link. 875 * XXX: should take care of the destination of a p2p link? 876 */ 877 int 878 nd6_is_addr_neighbor(struct sockaddr_in6 *addr, struct ifnet *ifp) 879 { 880 struct ifaddr *ifa; 881 int i; 882 883 #define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr) 884 #define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr) 885 886 /* 887 * A link-local address is always a neighbor. 888 * XXX: we should use the sin6_scope_id field rather than the embedded 889 * interface index. 890 */ 891 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) && 892 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index) 893 return(1); 894 895 /* 896 * If the address matches one of our addresses, 897 * it should be a neighbor. 898 */ 899 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 900 if (ifa->ifa_addr->sa_family != AF_INET6) 901 next: continue; 902 903 for (i = 0; i < 4; i++) { 904 if ((IFADDR6(ifa).s6_addr32[i] ^ 905 addr->sin6_addr.s6_addr32[i]) & 906 IFMASK6(ifa).s6_addr32[i]) 907 goto next; 908 } 909 return(1); 910 } 911 912 /* 913 * Even if the address matches none of our addresses, it might be 914 * in the neighbor cache. 915 */ 916 if (nd6_lookup(&addr->sin6_addr, 0, ifp) != NULL) 917 return(1); 918 919 return(0); 920 #undef IFADDR6 921 #undef IFMASK6 922 } 923 924 /* 925 * Free an nd6 llinfo entry. 926 */ 927 struct llinfo_nd6 * 928 nd6_free(struct rtentry *rt) 929 { 930 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next; 931 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr; 932 struct nd_defrouter *dr; 933 934 /* 935 * we used to have pfctlinput(PRC_HOSTDEAD) here. 936 * even though it is not harmful, it was not really necessary. 937 */ 938 939 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */ 940 int s; 941 s = splnet(); 942 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 943 rt->rt_ifp); 944 945 if (ln->ln_router || dr) { 946 /* 947 * rt6_flush must be called whether or not the neighbor 948 * is in the Default Router List. 949 * See a corresponding comment in nd6_na_input(). 950 */ 951 rt6_flush(&in6, rt->rt_ifp); 952 } 953 954 if (dr) { 955 /* 956 * Unreachablity of a router might affect the default 957 * router selection and on-link detection of advertised 958 * prefixes. 959 */ 960 961 /* 962 * Temporarily fake the state to choose a new default 963 * router and to perform on-link determination of 964 * prefixes correctly. 965 * Below the state will be set correctly, 966 * or the entry itself will be deleted. 967 */ 968 ln->ln_state = ND6_LLINFO_INCOMPLETE; 969 970 /* 971 * Since defrouter_select() does not affect the 972 * on-link determination and MIP6 needs the check 973 * before the default router selection, we perform 974 * the check now. 975 */ 976 pfxlist_onlink_check(); 977 978 if (dr == TAILQ_FIRST(&nd_defrouter)) { 979 /* 980 * It is used as the current default router, 981 * so we have to move it to the end of the 982 * list and choose a new one. 983 * XXX: it is not very efficient if this is 984 * the only router. 985 */ 986 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 987 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry); 988 989 defrouter_select(); 990 } 991 } 992 splx(s); 993 } 994 995 /* 996 * Before deleting the entry, remember the next entry as the 997 * return value. We need this because pfxlist_onlink_check() above 998 * might have freed other entries (particularly the old next entry) as 999 * a side effect (XXX). 1000 */ 1001 next = ln->ln_next; 1002 1003 /* 1004 * Detach the route from the routing tree and the list of neighbor 1005 * caches, and disable the route entry not to be used in already 1006 * cached routes. 1007 */ 1008 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, 1009 rt_mask(rt), 0, (struct rtentry **)0); 1010 1011 return(next); 1012 } 1013 1014 /* 1015 * Upper-layer reachability hint for Neighbor Unreachability Detection. 1016 * 1017 * XXX cost-effective metods? 1018 */ 1019 void 1020 nd6_nud_hint(struct rtentry *rt, struct in6_addr *dst6, int force) 1021 { 1022 struct llinfo_nd6 *ln; 1023 1024 /* 1025 * If the caller specified "rt", use that. Otherwise, resolve the 1026 * routing table by supplied "dst6". 1027 */ 1028 if (!rt) { 1029 if (!dst6) 1030 return; 1031 if (!(rt = nd6_lookup(dst6, 0, NULL))) 1032 return; 1033 } 1034 1035 if ((rt->rt_flags & RTF_GATEWAY) != 0 || 1036 (rt->rt_flags & RTF_LLINFO) == 0 || 1037 !rt->rt_llinfo || !rt->rt_gateway || 1038 rt->rt_gateway->sa_family != AF_LINK) { 1039 /* This is not a host route. */ 1040 return; 1041 } 1042 1043 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1044 if (ln->ln_state < ND6_LLINFO_REACHABLE) 1045 return; 1046 1047 /* 1048 * if we get upper-layer reachability confirmation many times, 1049 * it is possible we have false information. 1050 */ 1051 if (!force) { 1052 ln->ln_byhint++; 1053 if (ln->ln_byhint > nd6_maxnudhint) 1054 return; 1055 } 1056 1057 ln->ln_state = ND6_LLINFO_REACHABLE; 1058 if (ln->ln_expire) 1059 ln->ln_expire = time_second + 1060 nd_ifinfo[rt->rt_ifp->if_index].reachable; 1061 } 1062 1063 void 1064 nd6_rtrequest(int req, struct rtentry *rt, 1065 struct rt_addrinfo *info) /* xxx unused */ 1066 { 1067 struct sockaddr *gate = rt->rt_gateway; 1068 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1069 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1070 struct ifnet *ifp = rt->rt_ifp; 1071 struct ifaddr *ifa; 1072 1073 if ((rt->rt_flags & RTF_GATEWAY)) 1074 return; 1075 1076 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) { 1077 /* 1078 * This is probably an interface direct route for a link 1079 * which does not need neighbor caches (e.g. fe80::%lo0/64). 1080 * We do not need special treatment below for such a route. 1081 * Moreover, the RTF_LLINFO flag which would be set below 1082 * would annoy the ndp(8) command. 1083 */ 1084 return; 1085 } 1086 1087 if (req == RTM_RESOLVE && 1088 (nd6_need_cache(ifp) == 0 || /* stf case */ 1089 !nd6_is_addr_neighbor((struct sockaddr_in6 *)rt_key(rt), ifp))) { 1090 /* 1091 * FreeBSD and BSD/OS often make a cloned host route based 1092 * on a less-specific route (e.g. the default route). 1093 * If the less specific route does not have a "gateway" 1094 * (this is the case when the route just goes to a p2p or an 1095 * stf interface), we'll mistakenly make a neighbor cache for 1096 * the host route, and will see strange neighbor solicitation 1097 * for the corresponding destination. In order to avoid the 1098 * confusion, we check if the destination of the route is 1099 * a neighbor in terms of neighbor discovery, and stop the 1100 * process if not. Additionally, we remove the LLINFO flag 1101 * so that ndp(8) will not try to get the neighbor information 1102 * of the destination. 1103 */ 1104 rt->rt_flags &= ~RTF_LLINFO; 1105 return; 1106 } 1107 1108 switch (req) { 1109 case RTM_ADD: 1110 /* 1111 * There is no backward compatibility :) 1112 * 1113 * if ((rt->rt_flags & RTF_HOST) == 0 && 1114 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1115 * rt->rt_flags |= RTF_CLONING; 1116 */ 1117 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) { 1118 /* 1119 * Case 1: This route should come from 1120 * a route to interface. RTF_LLINFO flag is set 1121 * for a host route whose destination should be 1122 * treated as on-link. 1123 */ 1124 rt_setgate(rt, rt_key(rt), 1125 (struct sockaddr *)&null_sdl); 1126 gate = rt->rt_gateway; 1127 SDL(gate)->sdl_type = ifp->if_type; 1128 SDL(gate)->sdl_index = ifp->if_index; 1129 if (ln) 1130 ln->ln_expire = time_second; 1131 #if 1 1132 if (ln && ln->ln_expire == 0) { 1133 /* kludge for desktops */ 1134 #if 0 1135 printf("nd6_rtequest: time.tv_sec is zero; " 1136 "treat it as 1\n"); 1137 #endif 1138 ln->ln_expire = 1; 1139 } 1140 #endif 1141 if ((rt->rt_flags & RTF_CLONING)) 1142 break; 1143 } 1144 /* 1145 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here. 1146 * We don't do that here since llinfo is not ready yet. 1147 * 1148 * There are also couple of other things to be discussed: 1149 * - unsolicited NA code needs improvement beforehand 1150 * - RFC2461 says we MAY send multicast unsolicited NA 1151 * (7.2.6 paragraph 4), however, it also says that we 1152 * SHOULD provide a mechanism to prevent multicast NA storm. 1153 * we don't have anything like it right now. 1154 * note that the mechanism needs a mutual agreement 1155 * between proxies, which means that we need to implement 1156 * a new protocol, or a new kludge. 1157 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA. 1158 * we need to check ip6forwarding before sending it. 1159 * (or should we allow proxy ND configuration only for 1160 * routers? there's no mention about proxy ND from hosts) 1161 */ 1162 #if 0 1163 /* XXX it does not work */ 1164 if (rt->rt_flags & RTF_ANNOUNCE) 1165 nd6_na_output(ifp, 1166 &SIN6(rt_key(rt))->sin6_addr, 1167 &SIN6(rt_key(rt))->sin6_addr, 1168 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0, 1169 1, NULL); 1170 #endif 1171 /* FALLTHROUGH */ 1172 case RTM_RESOLVE: 1173 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { 1174 /* 1175 * Address resolution isn't necessary for a point to 1176 * point link, so we can skip this test for a p2p link. 1177 */ 1178 if (gate->sa_family != AF_LINK || 1179 gate->sa_len < sizeof(null_sdl)) { 1180 log(LOG_DEBUG, 1181 "nd6_rtrequest: bad gateway value: %s\n", 1182 if_name(ifp)); 1183 break; 1184 } 1185 SDL(gate)->sdl_type = ifp->if_type; 1186 SDL(gate)->sdl_index = ifp->if_index; 1187 } 1188 if (ln != NULL) 1189 break; /* This happens on a route change */ 1190 /* 1191 * Case 2: This route may come from cloning, or a manual route 1192 * add with a LL address. 1193 */ 1194 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln)); 1195 rt->rt_llinfo = (caddr_t)ln; 1196 if (!ln) { 1197 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n"); 1198 break; 1199 } 1200 nd6_inuse++; 1201 nd6_allocated++; 1202 Bzero(ln, sizeof(*ln)); 1203 ln->ln_rt = rt; 1204 /* this is required for "ndp" command. - shin */ 1205 if (req == RTM_ADD) { 1206 /* 1207 * gate should have some valid AF_LINK entry, 1208 * and ln->ln_expire should have some lifetime 1209 * which is specified by ndp command. 1210 */ 1211 ln->ln_state = ND6_LLINFO_REACHABLE; 1212 ln->ln_byhint = 0; 1213 } else { 1214 /* 1215 * When req == RTM_RESOLVE, rt is created and 1216 * initialized in rtrequest(), so rt_expire is 0. 1217 */ 1218 ln->ln_state = ND6_LLINFO_NOSTATE; 1219 ln->ln_expire = time_second; 1220 } 1221 rt->rt_flags |= RTF_LLINFO; 1222 ln->ln_next = llinfo_nd6.ln_next; 1223 llinfo_nd6.ln_next = ln; 1224 ln->ln_prev = &llinfo_nd6; 1225 ln->ln_next->ln_prev = ln; 1226 1227 /* 1228 * check if rt_key(rt) is one of my address assigned 1229 * to the interface. 1230 */ 1231 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp, 1232 &SIN6(rt_key(rt))->sin6_addr); 1233 if (ifa) { 1234 caddr_t macp = nd6_ifptomac(ifp); 1235 ln->ln_expire = 0; 1236 ln->ln_state = ND6_LLINFO_REACHABLE; 1237 ln->ln_byhint = 0; 1238 if (macp) { 1239 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen); 1240 SDL(gate)->sdl_alen = ifp->if_addrlen; 1241 } 1242 if (nd6_useloopback) { 1243 rt->rt_ifp = &loif[0]; /* XXX */ 1244 /* 1245 * Make sure rt_ifa be equal to the ifaddr 1246 * corresponding to the address. 1247 * We need this because when we refer 1248 * rt_ifa->ia6_flags in ip6_input, we assume 1249 * that the rt_ifa points to the address instead 1250 * of the loopback address. 1251 */ 1252 if (ifa != rt->rt_ifa) { 1253 IFAFREE(rt->rt_ifa); 1254 IFAREF(ifa); 1255 rt->rt_ifa = ifa; 1256 } 1257 } 1258 } else if (rt->rt_flags & RTF_ANNOUNCE) { 1259 ln->ln_expire = 0; 1260 ln->ln_state = ND6_LLINFO_REACHABLE; 1261 ln->ln_byhint = 0; 1262 1263 /* join solicited node multicast for proxy ND */ 1264 if (ifp->if_flags & IFF_MULTICAST) { 1265 struct in6_addr llsol; 1266 int error; 1267 1268 llsol = SIN6(rt_key(rt))->sin6_addr; 1269 llsol.s6_addr16[0] = htons(0xff02); 1270 llsol.s6_addr16[1] = htons(ifp->if_index); 1271 llsol.s6_addr32[1] = 0; 1272 llsol.s6_addr32[2] = htonl(1); 1273 llsol.s6_addr8[12] = 0xff; 1274 1275 if (!in6_addmulti(&llsol, ifp, &error)) { 1276 nd6log((LOG_ERR, "%s: failed to join " 1277 "%s (errno=%d)\n", if_name(ifp), 1278 ip6_sprintf(&llsol), error)); 1279 } 1280 } 1281 } 1282 break; 1283 1284 case RTM_DELETE: 1285 if (!ln) 1286 break; 1287 /* leave from solicited node multicast for proxy ND */ 1288 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 && 1289 (ifp->if_flags & IFF_MULTICAST) != 0) { 1290 struct in6_addr llsol; 1291 struct in6_multi *in6m; 1292 1293 llsol = SIN6(rt_key(rt))->sin6_addr; 1294 llsol.s6_addr16[0] = htons(0xff02); 1295 llsol.s6_addr16[1] = htons(ifp->if_index); 1296 llsol.s6_addr32[1] = 0; 1297 llsol.s6_addr32[2] = htonl(1); 1298 llsol.s6_addr8[12] = 0xff; 1299 1300 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 1301 if (in6m) 1302 in6_delmulti(in6m); 1303 } 1304 nd6_inuse--; 1305 ln->ln_next->ln_prev = ln->ln_prev; 1306 ln->ln_prev->ln_next = ln->ln_next; 1307 ln->ln_prev = NULL; 1308 rt->rt_llinfo = 0; 1309 rt->rt_flags &= ~RTF_LLINFO; 1310 if (ln->ln_hold) 1311 m_freem(ln->ln_hold); 1312 Free((caddr_t)ln); 1313 } 1314 } 1315 1316 int 1317 nd6_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp) 1318 { 1319 struct in6_drlist *drl = (struct in6_drlist *)data; 1320 struct in6_prlist *prl = (struct in6_prlist *)data; 1321 struct in6_ndireq *ndi = (struct in6_ndireq *)data; 1322 struct in6_nbrinfo *nbi = (struct in6_nbrinfo *)data; 1323 struct in6_ndifreq *ndif = (struct in6_ndifreq *)data; 1324 struct nd_defrouter *dr, any; 1325 struct nd_prefix *pr; 1326 struct rtentry *rt; 1327 int i = 0, error = 0; 1328 int s; 1329 1330 switch (cmd) { 1331 case SIOCGDRLST_IN6: 1332 /* 1333 * obsolete API, use sysctl under net.inet6.icmp6 1334 */ 1335 bzero(drl, sizeof(*drl)); 1336 s = splnet(); 1337 dr = TAILQ_FIRST(&nd_defrouter); 1338 while (dr && i < DRLSTSIZ) { 1339 drl->defrouter[i].rtaddr = dr->rtaddr; 1340 if (IN6_IS_ADDR_LINKLOCAL(&drl->defrouter[i].rtaddr)) { 1341 /* XXX: need to this hack for KAME stack */ 1342 drl->defrouter[i].rtaddr.s6_addr16[1] = 0; 1343 } else 1344 log(LOG_ERR, 1345 "default router list contains a " 1346 "non-linklocal address(%s)\n", 1347 ip6_sprintf(&drl->defrouter[i].rtaddr)); 1348 1349 drl->defrouter[i].flags = dr->flags; 1350 drl->defrouter[i].rtlifetime = dr->rtlifetime; 1351 drl->defrouter[i].expire = dr->expire; 1352 drl->defrouter[i].if_index = dr->ifp->if_index; 1353 i++; 1354 dr = TAILQ_NEXT(dr, dr_entry); 1355 } 1356 splx(s); 1357 break; 1358 case SIOCGPRLST_IN6: 1359 /* 1360 * obsolete API, use sysctl under net.inet6.icmp6 1361 */ 1362 /* 1363 * XXX meaning of fields, especialy "raflags", is very 1364 * differnet between RA prefix list and RR/static prefix list. 1365 * how about separating ioctls into two? 1366 */ 1367 bzero(prl, sizeof(*prl)); 1368 s = splnet(); 1369 pr = nd_prefix.lh_first; 1370 while (pr && i < PRLSTSIZ) { 1371 struct nd_pfxrouter *pfr; 1372 int j; 1373 1374 (void)in6_embedscope(&prl->prefix[i].prefix, 1375 &pr->ndpr_prefix, NULL, NULL); 1376 prl->prefix[i].raflags = pr->ndpr_raf; 1377 prl->prefix[i].prefixlen = pr->ndpr_plen; 1378 prl->prefix[i].vltime = pr->ndpr_vltime; 1379 prl->prefix[i].pltime = pr->ndpr_pltime; 1380 prl->prefix[i].if_index = pr->ndpr_ifp->if_index; 1381 prl->prefix[i].expire = pr->ndpr_expire; 1382 1383 pfr = pr->ndpr_advrtrs.lh_first; 1384 j = 0; 1385 while (pfr) { 1386 if (j < DRLSTSIZ) { 1387 #define RTRADDR prl->prefix[i].advrtr[j] 1388 RTRADDR = pfr->router->rtaddr; 1389 if (IN6_IS_ADDR_LINKLOCAL(&RTRADDR)) { 1390 /* XXX: hack for KAME */ 1391 RTRADDR.s6_addr16[1] = 0; 1392 } else 1393 log(LOG_ERR, 1394 "a router(%s) advertises " 1395 "a prefix with " 1396 "non-link local address\n", 1397 ip6_sprintf(&RTRADDR)); 1398 #undef RTRADDR 1399 } 1400 j++; 1401 pfr = pfr->pfr_next; 1402 } 1403 prl->prefix[i].advrtrs = j; 1404 prl->prefix[i].origin = PR_ORIG_RA; 1405 1406 i++; 1407 pr = pr->ndpr_next; 1408 } 1409 { 1410 struct rr_prefix *rpp; 1411 1412 for (rpp = LIST_FIRST(&rr_prefix); rpp; 1413 rpp = LIST_NEXT(rpp, rp_entry)) { 1414 if (i >= PRLSTSIZ) 1415 break; 1416 (void)in6_embedscope(&prl->prefix[i].prefix, 1417 &pr->ndpr_prefix, NULL, NULL); 1418 prl->prefix[i].raflags = rpp->rp_raf; 1419 prl->prefix[i].prefixlen = rpp->rp_plen; 1420 prl->prefix[i].vltime = rpp->rp_vltime; 1421 prl->prefix[i].pltime = rpp->rp_pltime; 1422 prl->prefix[i].if_index = rpp->rp_ifp->if_index; 1423 prl->prefix[i].expire = rpp->rp_expire; 1424 prl->prefix[i].advrtrs = 0; 1425 prl->prefix[i].origin = rpp->rp_origin; 1426 i++; 1427 } 1428 } 1429 splx(s); 1430 1431 break; 1432 case OSIOCGIFINFO_IN6: 1433 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) { 1434 error = EINVAL; 1435 break; 1436 } 1437 ndi->ndi.linkmtu = nd_ifinfo[ifp->if_index].linkmtu; 1438 ndi->ndi.maxmtu = nd_ifinfo[ifp->if_index].maxmtu; 1439 ndi->ndi.basereachable = 1440 nd_ifinfo[ifp->if_index].basereachable; 1441 ndi->ndi.reachable = nd_ifinfo[ifp->if_index].reachable; 1442 ndi->ndi.retrans = nd_ifinfo[ifp->if_index].retrans; 1443 ndi->ndi.flags = nd_ifinfo[ifp->if_index].flags; 1444 ndi->ndi.recalctm = nd_ifinfo[ifp->if_index].recalctm; 1445 ndi->ndi.chlim = nd_ifinfo[ifp->if_index].chlim; 1446 ndi->ndi.receivedra = nd_ifinfo[ifp->if_index].receivedra; 1447 break; 1448 case SIOCGIFINFO_IN6: 1449 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) { 1450 error = EINVAL; 1451 break; 1452 } 1453 ndi->ndi = nd_ifinfo[ifp->if_index]; 1454 break; 1455 case SIOCSIFINFO_FLAGS: 1456 /* XXX: almost all other fields of ndi->ndi is unused */ 1457 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) { 1458 error = EINVAL; 1459 break; 1460 } 1461 nd_ifinfo[ifp->if_index].flags = ndi->ndi.flags; 1462 break; 1463 case SIOCSNDFLUSH_IN6: /* XXX: the ioctl name is confusing... */ 1464 /* flush default router list */ 1465 /* 1466 * xxx sumikawa: should not delete route if default 1467 * route equals to the top of default router list 1468 */ 1469 bzero(&any, sizeof(any)); 1470 defrouter_delreq(&any, 0); 1471 defrouter_select(); 1472 /* xxx sumikawa: flush prefix list */ 1473 break; 1474 case SIOCSPFXFLUSH_IN6: 1475 { 1476 /* flush all the prefix advertised by routers */ 1477 struct nd_prefix *pr, *next; 1478 1479 s = splnet(); 1480 for (pr = nd_prefix.lh_first; pr; pr = next) { 1481 struct in6_ifaddr *ia, *ia_next; 1482 1483 next = pr->ndpr_next; 1484 1485 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1486 continue; /* XXX */ 1487 1488 /* do we really have to remove addresses as well? */ 1489 for (ia = in6_ifaddr; ia; ia = ia_next) { 1490 /* ia might be removed. keep the next ptr. */ 1491 ia_next = ia->ia_next; 1492 1493 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1494 continue; 1495 1496 if (ia->ia6_ndpr == pr) 1497 in6_purgeaddr(&ia->ia_ifa); 1498 } 1499 prelist_remove(pr); 1500 } 1501 splx(s); 1502 break; 1503 } 1504 case SIOCSRTRFLUSH_IN6: 1505 { 1506 /* flush all the default routers */ 1507 struct nd_defrouter *dr, *next; 1508 1509 s = splnet(); 1510 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) { 1511 /* 1512 * The first entry of the list may be stored in 1513 * the routing table, so we'll delete it later. 1514 */ 1515 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = next) { 1516 next = TAILQ_NEXT(dr, dr_entry); 1517 defrtrlist_del(dr); 1518 } 1519 defrtrlist_del(TAILQ_FIRST(&nd_defrouter)); 1520 } 1521 splx(s); 1522 break; 1523 } 1524 case SIOCGNBRINFO_IN6: 1525 { 1526 struct llinfo_nd6 *ln; 1527 struct in6_addr nb_addr = nbi->addr; /* make local for safety */ 1528 1529 /* 1530 * XXX: KAME specific hack for scoped addresses 1531 * XXXX: for other scopes than link-local? 1532 */ 1533 if (IN6_IS_ADDR_LINKLOCAL(&nbi->addr) || 1534 IN6_IS_ADDR_MC_LINKLOCAL(&nbi->addr)) { 1535 u_int16_t *idp = (u_int16_t *)&nb_addr.s6_addr[2]; 1536 1537 if (*idp == 0) 1538 *idp = htons(ifp->if_index); 1539 } 1540 1541 s = splnet(); 1542 if ((rt = nd6_lookup(&nb_addr, 0, ifp)) == NULL) { 1543 error = EINVAL; 1544 splx(s); 1545 break; 1546 } 1547 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1548 nbi->state = ln->ln_state; 1549 nbi->asked = ln->ln_asked; 1550 nbi->isrouter = ln->ln_router; 1551 nbi->expire = ln->ln_expire; 1552 splx(s); 1553 1554 break; 1555 } 1556 case SIOCGDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1557 ndif->ifindex = nd6_defifindex; 1558 break; 1559 case SIOCSDEFIFACE_IN6: /* XXX: should be implemented as a sysctl? */ 1560 return(nd6_setdefaultiface(ndif->ifindex)); 1561 break; 1562 } 1563 return(error); 1564 } 1565 1566 /* 1567 * Create neighbor cache entry and cache link-layer address, 1568 * on reception of inbound ND6 packets. (RS/RA/NS/redirect) 1569 */ 1570 struct rtentry * 1571 nd6_cache_lladdr(struct ifnet *ifp, struct in6_addr *from, char *lladdr, 1572 int lladdrlen, 1573 int type, /* ICMP6 type */ 1574 int code /* type dependent information */) 1575 { 1576 struct rtentry *rt = NULL; 1577 struct llinfo_nd6 *ln = NULL; 1578 int is_newentry; 1579 struct sockaddr_dl *sdl = NULL; 1580 int do_update; 1581 int olladdr; 1582 int llchange; 1583 int newstate = 0; 1584 1585 if (!ifp) 1586 panic("ifp == NULL in nd6_cache_lladdr"); 1587 if (!from) 1588 panic("from == NULL in nd6_cache_lladdr"); 1589 1590 /* nothing must be updated for unspecified address */ 1591 if (IN6_IS_ADDR_UNSPECIFIED(from)) 1592 return NULL; 1593 1594 /* 1595 * Validation about ifp->if_addrlen and lladdrlen must be done in 1596 * the caller. 1597 * 1598 * XXX If the link does not have link-layer adderss, what should 1599 * we do? (ifp->if_addrlen == 0) 1600 * Spec says nothing in sections for RA, RS and NA. There's small 1601 * description on it in NS section (RFC 2461 7.2.3). 1602 */ 1603 1604 rt = nd6_lookup(from, 0, ifp); 1605 if (!rt) { 1606 #if 0 1607 /* nothing must be done if there's no lladdr */ 1608 if (!lladdr || !lladdrlen) 1609 return NULL; 1610 #endif 1611 1612 rt = nd6_lookup(from, 1, ifp); 1613 is_newentry = 1; 1614 } else { 1615 /* do nothing if static ndp is set */ 1616 if (rt->rt_flags & RTF_STATIC) 1617 return NULL; 1618 is_newentry = 0; 1619 } 1620 1621 if (!rt) 1622 return NULL; 1623 if ((rt->rt_flags & (RTF_GATEWAY | RTF_LLINFO)) != RTF_LLINFO) { 1624 fail: 1625 (void)nd6_free(rt); 1626 return NULL; 1627 } 1628 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1629 if (!ln) 1630 goto fail; 1631 if (!rt->rt_gateway) 1632 goto fail; 1633 if (rt->rt_gateway->sa_family != AF_LINK) 1634 goto fail; 1635 sdl = SDL(rt->rt_gateway); 1636 1637 olladdr = (sdl->sdl_alen) ? 1 : 0; 1638 if (olladdr && lladdr) { 1639 if (bcmp(lladdr, LLADDR(sdl), ifp->if_addrlen)) 1640 llchange = 1; 1641 else 1642 llchange = 0; 1643 } else 1644 llchange = 0; 1645 1646 /* 1647 * newentry olladdr lladdr llchange (*=record) 1648 * 0 n n -- (1) 1649 * 0 y n -- (2) 1650 * 0 n y -- (3) * STALE 1651 * 0 y y n (4) * 1652 * 0 y y y (5) * STALE 1653 * 1 -- n -- (6) NOSTATE(= PASSIVE) 1654 * 1 -- y -- (7) * STALE 1655 */ 1656 1657 if (lladdr) { /* (3-5) and (7) */ 1658 /* 1659 * Record source link-layer address 1660 * XXX is it dependent to ifp->if_type? 1661 */ 1662 sdl->sdl_alen = ifp->if_addrlen; 1663 bcopy(lladdr, LLADDR(sdl), ifp->if_addrlen); 1664 } 1665 1666 if (!is_newentry) { 1667 if ((!olladdr && lladdr) /* (3) */ 1668 || (olladdr && lladdr && llchange)) { /* (5) */ 1669 do_update = 1; 1670 newstate = ND6_LLINFO_STALE; 1671 } else /* (1-2,4) */ 1672 do_update = 0; 1673 } else { 1674 do_update = 1; 1675 if (!lladdr) /* (6) */ 1676 newstate = ND6_LLINFO_NOSTATE; 1677 else /* (7) */ 1678 newstate = ND6_LLINFO_STALE; 1679 } 1680 1681 if (do_update) { 1682 /* 1683 * Update the state of the neighbor cache. 1684 */ 1685 ln->ln_state = newstate; 1686 1687 if (ln->ln_state == ND6_LLINFO_STALE) { 1688 /* 1689 * XXX: since nd6_output() below will cause 1690 * state tansition to DELAY and reset the timer, 1691 * we must set the timer now, although it is actually 1692 * meaningless. 1693 */ 1694 ln->ln_expire = time_second + nd6_gctimer; 1695 1696 if (ln->ln_hold) { 1697 /* 1698 * we assume ifp is not a p2p here, so just 1699 * set the 2nd argument as the 1st one. 1700 */ 1701 nd6_output(ifp, ifp, ln->ln_hold, 1702 (struct sockaddr_in6 *)rt_key(rt), 1703 rt); 1704 ln->ln_hold = NULL; 1705 } 1706 } else if (ln->ln_state == ND6_LLINFO_INCOMPLETE) { 1707 /* probe right away */ 1708 ln->ln_expire = time_second; 1709 } 1710 } 1711 1712 /* 1713 * ICMP6 type dependent behavior. 1714 * 1715 * NS: clear IsRouter if new entry 1716 * RS: clear IsRouter 1717 * RA: set IsRouter if there's lladdr 1718 * redir: clear IsRouter if new entry 1719 * 1720 * RA case, (1): 1721 * The spec says that we must set IsRouter in the following cases: 1722 * - If lladdr exist, set IsRouter. This means (1-5). 1723 * - If it is old entry (!newentry), set IsRouter. This means (7). 1724 * So, based on the spec, in (1-5) and (7) cases we must set IsRouter. 1725 * A quetion arises for (1) case. (1) case has no lladdr in the 1726 * neighbor cache, this is similar to (6). 1727 * This case is rare but we figured that we MUST NOT set IsRouter. 1728 * 1729 * newentry olladdr lladdr llchange NS RS RA redir 1730 * D R 1731 * 0 n n -- (1) c ? s 1732 * 0 y n -- (2) c s s 1733 * 0 n y -- (3) c s s 1734 * 0 y y n (4) c s s 1735 * 0 y y y (5) c s s 1736 * 1 -- n -- (6) c c c s 1737 * 1 -- y -- (7) c c s c s 1738 * 1739 * (c=clear s=set) 1740 */ 1741 switch (type & 0xff) { 1742 case ND_NEIGHBOR_SOLICIT: 1743 /* 1744 * New entry must have is_router flag cleared. 1745 */ 1746 if (is_newentry) /* (6-7) */ 1747 ln->ln_router = 0; 1748 break; 1749 case ND_REDIRECT: 1750 /* 1751 * If the icmp is a redirect to a better router, always set the 1752 * is_router flag. Otherwise, if the entry is newly created, 1753 * clear the flag. [RFC 2461, sec 8.3] 1754 */ 1755 if (code == ND_REDIRECT_ROUTER) 1756 ln->ln_router = 1; 1757 else if (is_newentry) /* (6-7) */ 1758 ln->ln_router = 0; 1759 break; 1760 case ND_ROUTER_SOLICIT: 1761 /* 1762 * is_router flag must always be cleared. 1763 */ 1764 ln->ln_router = 0; 1765 break; 1766 case ND_ROUTER_ADVERT: 1767 /* 1768 * Mark an entry with lladdr as a router. 1769 */ 1770 if ((!is_newentry && (olladdr || lladdr)) /* (2-5) */ 1771 || (is_newentry && lladdr)) { /* (7) */ 1772 ln->ln_router = 1; 1773 } 1774 break; 1775 } 1776 1777 /* 1778 * When the link-layer address of a router changes, select the 1779 * best router again. In particular, when the neighbor entry is newly 1780 * created, it might affect the selection policy. 1781 * Question: can we restrict the first condition to the "is_newentry" 1782 * case? 1783 * XXX: when we hear an RA from a new router with the link-layer 1784 * address option, defrouter_select() is called twice, since 1785 * defrtrlist_update called the function as well. However, I believe 1786 * we can compromise the overhead, since it only happens the first 1787 * time. 1788 * XXX: although defrouter_select() should not have a bad effect 1789 * for those are not autoconfigured hosts, we explicitly avoid such 1790 * cases for safety. 1791 */ 1792 if (do_update && ln->ln_router && !ip6_forwarding && ip6_accept_rtadv) 1793 defrouter_select(); 1794 1795 return rt; 1796 } 1797 1798 static void 1799 nd6_slowtimo(void *ignored_arg) 1800 { 1801 int s = splnet(); 1802 int i; 1803 struct nd_ifinfo *nd6if; 1804 1805 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 1806 nd6_slowtimo, NULL); 1807 for (i = 1; i < if_index + 1; i++) { 1808 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) 1809 continue; 1810 nd6if = &nd_ifinfo[i]; 1811 if (nd6if->basereachable && /* already initialized */ 1812 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 1813 /* 1814 * Since reachable time rarely changes by router 1815 * advertisements, we SHOULD insure that a new random 1816 * value gets recomputed at least once every few hours. 1817 * (RFC 2461, 6.3.4) 1818 */ 1819 nd6if->recalctm = nd6_recalc_reachtm_interval; 1820 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); 1821 } 1822 } 1823 splx(s); 1824 } 1825 1826 #define senderr(e) { error = (e); goto bad;} 1827 int 1828 nd6_output(struct ifnet *ifp, struct ifnet *origifp, struct mbuf *m0, 1829 struct sockaddr_in6 *dst, struct rtentry *rt0) 1830 { 1831 struct mbuf *m = m0; 1832 struct rtentry *rt = rt0; 1833 struct sockaddr_in6 *gw6 = NULL; 1834 struct llinfo_nd6 *ln = NULL; 1835 int error = 0; 1836 1837 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr)) 1838 goto sendpkt; 1839 1840 if (nd6_need_cache(ifp) == 0) 1841 goto sendpkt; 1842 1843 /* 1844 * next hop determination. This routine is derived from ether_outpout. 1845 */ 1846 if (rt) { 1847 if ((rt->rt_flags & RTF_UP) == 0) { 1848 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) != 1849 NULL) 1850 { 1851 rt->rt_refcnt--; 1852 if (rt->rt_ifp != ifp) { 1853 /* XXX: loop care? */ 1854 return nd6_output(ifp, origifp, m0, 1855 dst, rt); 1856 } 1857 } else 1858 senderr(EHOSTUNREACH); 1859 } 1860 1861 if (rt->rt_flags & RTF_GATEWAY) { 1862 gw6 = (struct sockaddr_in6 *)rt->rt_gateway; 1863 1864 /* 1865 * We skip link-layer address resolution and NUD 1866 * if the gateway is not a neighbor from ND point 1867 * of view, regardless of the value of nd_ifinfo.flags. 1868 * The second condition is a bit tricky; we skip 1869 * if the gateway is our own address, which is 1870 * sometimes used to install a route to a p2p link. 1871 */ 1872 if (!nd6_is_addr_neighbor(gw6, ifp) || 1873 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) { 1874 /* 1875 * We allow this kind of tricky route only 1876 * when the outgoing interface is p2p. 1877 * XXX: we may need a more generic rule here. 1878 */ 1879 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1880 senderr(EHOSTUNREACH); 1881 1882 goto sendpkt; 1883 } 1884 1885 if (rt->rt_gwroute == 0) 1886 goto lookup; 1887 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 1888 rtfree(rt); rt = rt0; 1889 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL); 1890 if ((rt = rt->rt_gwroute) == 0) 1891 senderr(EHOSTUNREACH); 1892 } 1893 } 1894 } 1895 1896 /* 1897 * Address resolution or Neighbor Unreachability Detection 1898 * for the next hop. 1899 * At this point, the destination of the packet must be a unicast 1900 * or an anycast address(i.e. not a multicast). 1901 */ 1902 1903 /* Look up the neighbor cache for the nexthop */ 1904 if (rt && (rt->rt_flags & RTF_LLINFO) != 0) 1905 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1906 else { 1907 /* 1908 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 1909 * the condition below is not very efficient. But we believe 1910 * it is tolerable, because this should be a rare case. 1911 */ 1912 if (nd6_is_addr_neighbor(dst, ifp) && 1913 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL) 1914 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1915 } 1916 if (!ln || !rt) { 1917 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 && 1918 !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) { 1919 log(LOG_DEBUG, 1920 "nd6_output: can't allocate llinfo for %s " 1921 "(ln=%p, rt=%p)\n", 1922 ip6_sprintf(&dst->sin6_addr), ln, rt); 1923 senderr(EIO); /* XXX: good error? */ 1924 } 1925 1926 goto sendpkt; /* send anyway */ 1927 } 1928 1929 /* We don't have to do link-layer address resolution on a p2p link. */ 1930 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 1931 ln->ln_state < ND6_LLINFO_REACHABLE) { 1932 ln->ln_state = ND6_LLINFO_STALE; 1933 ln->ln_expire = time_second + nd6_gctimer; 1934 } 1935 1936 /* 1937 * The first time we send a packet to a neighbor whose entry is 1938 * STALE, we have to change the state to DELAY and a sets a timer to 1939 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 1940 * neighbor unreachability detection on expiration. 1941 * (RFC 2461 7.3.3) 1942 */ 1943 if (ln->ln_state == ND6_LLINFO_STALE) { 1944 ln->ln_asked = 0; 1945 ln->ln_state = ND6_LLINFO_DELAY; 1946 ln->ln_expire = time_second + nd6_delay; 1947 } 1948 1949 /* 1950 * If the neighbor cache entry has a state other than INCOMPLETE 1951 * (i.e. its link-layer address is already resolved), just 1952 * send the packet. 1953 */ 1954 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 1955 goto sendpkt; 1956 1957 /* 1958 * There is a neighbor cache entry, but no ethernet address 1959 * response yet. Replace the held mbuf (if any) with this 1960 * latest one. 1961 * 1962 * This code conforms to the rate-limiting rule described in Section 1963 * 7.2.2 of RFC 2461, because the timer is set correctly after sending 1964 * an NS below. 1965 */ 1966 if (ln->ln_state == ND6_LLINFO_NOSTATE) 1967 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1968 if (ln->ln_hold) 1969 m_freem(ln->ln_hold); 1970 ln->ln_hold = m; 1971 if (ln->ln_expire) { 1972 if (ln->ln_asked < nd6_mmaxtries && 1973 ln->ln_expire < time_second) { 1974 ln->ln_asked++; 1975 ln->ln_expire = time_second + 1976 nd_ifinfo[ifp->if_index].retrans / 1000; 1977 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0); 1978 } 1979 } 1980 return(0); 1981 1982 sendpkt: 1983 1984 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 1985 return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst, 1986 rt)); 1987 } 1988 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt)); 1989 1990 bad: 1991 if (m) 1992 m_freem(m); 1993 return (error); 1994 } 1995 #undef senderr 1996 1997 int 1998 nd6_need_cache(struct ifnet *ifp) 1999 { 2000 /* 2001 * XXX: we currently do not make neighbor cache on any interface 2002 * other than ARCnet, Ethernet, FDDI and GIF. 2003 * 2004 * RFC2893 says: 2005 * - unidirectional tunnels needs no ND 2006 */ 2007 switch (ifp->if_type) { 2008 case IFT_ARCNET: 2009 case IFT_ETHER: 2010 case IFT_FDDI: 2011 case IFT_IEEE1394: 2012 #ifdef IFT_L2VLAN 2013 case IFT_L2VLAN: 2014 #endif 2015 #ifdef IFT_IEEE80211 2016 case IFT_IEEE80211: 2017 #endif 2018 case IFT_GIF: /* XXX need more cases? */ 2019 return(1); 2020 default: 2021 return(0); 2022 } 2023 } 2024 2025 int 2026 nd6_storelladdr(struct ifnet *ifp, struct rtentry *rt, struct mbuf *m, 2027 struct sockaddr *dst, u_char *desten) 2028 { 2029 struct sockaddr_dl *sdl; 2030 2031 if (m->m_flags & M_MCAST) { 2032 switch (ifp->if_type) { 2033 case IFT_ETHER: 2034 case IFT_FDDI: 2035 #ifdef IFT_L2VLAN 2036 case IFT_L2VLAN: 2037 #endif 2038 #ifdef IFT_IEEE80211 2039 case IFT_IEEE80211: 2040 #endif 2041 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, 2042 desten); 2043 return(1); 2044 case IFT_IEEE1394: 2045 bcopy(ifp->if_broadcastaddr, desten, ifp->if_addrlen); 2046 return(1); 2047 case IFT_ARCNET: 2048 *desten = 0; 2049 return(1); 2050 default: 2051 m_freem(m); 2052 return(0); 2053 } 2054 } 2055 2056 if (rt == NULL) { 2057 /* this could happen, if we could not allocate memory */ 2058 m_freem(m); 2059 return(0); 2060 } 2061 if (rt->rt_gateway->sa_family != AF_LINK) { 2062 printf("nd6_storelladdr: something odd happens\n"); 2063 m_freem(m); 2064 return(0); 2065 } 2066 sdl = SDL(rt->rt_gateway); 2067 if (sdl->sdl_alen == 0) { 2068 /* this should be impossible, but we bark here for debugging */ 2069 printf("nd6_storelladdr: sdl_alen == 0\n"); 2070 m_freem(m); 2071 return(0); 2072 } 2073 2074 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 2075 return(1); 2076 } 2077 2078 static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS); 2079 static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS); 2080 #ifdef SYSCTL_DECL 2081 SYSCTL_DECL(_net_inet6_icmp6); 2082 #endif 2083 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist, 2084 CTLFLAG_RD, nd6_sysctl_drlist, ""); 2085 SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist, 2086 CTLFLAG_RD, nd6_sysctl_prlist, ""); 2087 2088 static int 2089 nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS) 2090 { 2091 int error; 2092 char buf[1024]; 2093 struct in6_defrouter *d, *de; 2094 struct nd_defrouter *dr; 2095 2096 if (req->newptr) 2097 return EPERM; 2098 error = 0; 2099 2100 for (dr = TAILQ_FIRST(&nd_defrouter); 2101 dr; 2102 dr = TAILQ_NEXT(dr, dr_entry)) { 2103 d = (struct in6_defrouter *)buf; 2104 de = (struct in6_defrouter *)(buf + sizeof(buf)); 2105 2106 if (d + 1 <= de) { 2107 bzero(d, sizeof(*d)); 2108 d->rtaddr.sin6_family = AF_INET6; 2109 d->rtaddr.sin6_len = sizeof(d->rtaddr); 2110 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr, 2111 dr->ifp) != 0) 2112 log(LOG_ERR, 2113 "scope error in " 2114 "default router list (%s)\n", 2115 ip6_sprintf(&dr->rtaddr)); 2116 d->flags = dr->flags; 2117 d->rtlifetime = dr->rtlifetime; 2118 d->expire = dr->expire; 2119 d->if_index = dr->ifp->if_index; 2120 } else 2121 panic("buffer too short"); 2122 2123 error = SYSCTL_OUT(req, buf, sizeof(*d)); 2124 if (error) 2125 break; 2126 } 2127 return error; 2128 } 2129 2130 static int 2131 nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS) 2132 { 2133 int error; 2134 char buf[1024]; 2135 struct in6_prefix *p, *pe; 2136 struct nd_prefix *pr; 2137 2138 if (req->newptr) 2139 return EPERM; 2140 error = 0; 2141 2142 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 2143 u_short advrtrs; 2144 size_t advance; 2145 struct sockaddr_in6 *sin6, *s6; 2146 struct nd_pfxrouter *pfr; 2147 2148 p = (struct in6_prefix *)buf; 2149 pe = (struct in6_prefix *)(buf + sizeof(buf)); 2150 2151 if (p + 1 <= pe) { 2152 bzero(p, sizeof(*p)); 2153 sin6 = (struct sockaddr_in6 *)(p + 1); 2154 2155 p->prefix = pr->ndpr_prefix; 2156 if (in6_recoverscope(&p->prefix, 2157 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0) 2158 log(LOG_ERR, 2159 "scope error in prefix list (%s)\n", 2160 ip6_sprintf(&p->prefix.sin6_addr)); 2161 p->raflags = pr->ndpr_raf; 2162 p->prefixlen = pr->ndpr_plen; 2163 p->vltime = pr->ndpr_vltime; 2164 p->pltime = pr->ndpr_pltime; 2165 p->if_index = pr->ndpr_ifp->if_index; 2166 p->expire = pr->ndpr_expire; 2167 p->refcnt = pr->ndpr_refcnt; 2168 p->flags = pr->ndpr_stateflags; 2169 p->origin = PR_ORIG_RA; 2170 advrtrs = 0; 2171 for (pfr = pr->ndpr_advrtrs.lh_first; 2172 pfr; 2173 pfr = pfr->pfr_next) { 2174 if ((void *)&sin6[advrtrs + 1] > 2175 (void *)pe) { 2176 advrtrs++; 2177 continue; 2178 } 2179 s6 = &sin6[advrtrs]; 2180 bzero(s6, sizeof(*s6)); 2181 s6->sin6_family = AF_INET6; 2182 s6->sin6_len = sizeof(*sin6); 2183 if (in6_recoverscope(s6, 2184 &pfr->router->rtaddr, 2185 pfr->router->ifp) != 0) 2186 log(LOG_ERR, 2187 "scope error in " 2188 "prefix list (%s)\n", 2189 ip6_sprintf(&pfr->router->rtaddr)); 2190 advrtrs++; 2191 } 2192 p->advrtrs = advrtrs; 2193 } else 2194 panic("buffer too short"); 2195 2196 advance = sizeof(*p) + sizeof(*sin6) * advrtrs; 2197 error = SYSCTL_OUT(req, buf, advance); 2198 if (error) 2199 break; 2200 } 2201 return error; 2202 } 2203