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