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