nd6.c revision 78468
1/* $FreeBSD: head/sys/netinet6/nd6.c 78468 2001-06-19 14:48:02Z sumikawa $ */ 2/* $KAME: nd6.c,v 1.144 2001/05/24 07:44:00 itojun Exp $ */ 3 4/* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33/* 34 * XXX 35 * KAME 970409 note: 36 * BSD/OS version heavily modifies this code, related to llinfo. 37 * Since we don't have BSD/OS version of net/route.c in our hand, 38 * I left the code mostly as it was in 970310. -- itojun 39 */ 40 41#include "opt_inet.h" 42#include "opt_inet6.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/callout.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/socket.h> 50#include <sys/sockio.h> 51#include <sys/time.h> 52#include <sys/kernel.h> 53#include <sys/protosw.h> 54#include <sys/errno.h> 55#include <sys/syslog.h> 56#include <sys/queue.h> 57#include <sys/sysctl.h> 58 59#include <net/if.h> 60#include <net/if_dl.h> 61#include <net/if_types.h> 62#include <net/if_atm.h> 63#include <net/route.h> 64 65#include <netinet/in.h> 66#include <netinet/if_ether.h> 67#include <netinet/if_fddi.h> 68#include <netinet6/in6_var.h> 69#include <netinet/ip6.h> 70#include <netinet6/ip6_var.h> 71#include <netinet6/nd6.h> 72#include <netinet6/in6_prefix.h> 73#include <netinet/icmp6.h> 74 75#include <net/net_osdep.h> 76 77#define ND6_SLOWTIMER_INTERVAL (60 * 60) /* 1 hour */ 78#define ND6_RECALC_REACHTM_INTERVAL (60 * 120) /* 2 hours */ 79 80#define SIN6(s) ((struct sockaddr_in6 *)s) 81#define SDL(s) ((struct sockaddr_dl *)s) 82 83/* timer values */ 84int nd6_prune = 1; /* walk list every 1 seconds */ 85int nd6_delay = 5; /* delay first probe time 5 second */ 86int nd6_umaxtries = 3; /* maximum unicast query */ 87int nd6_mmaxtries = 3; /* maximum multicast query */ 88int nd6_useloopback = 1; /* use loopback interface for local traffic */ 89int nd6_gctimer = (60 * 60 * 24); /* 1 day: garbage collection timer */ 90 91/* preventing too many loops in ND option parsing */ 92int nd6_maxndopt = 10; /* max # of ND options allowed */ 93 94int nd6_maxnudhint = 0; /* max # of subsequent upper layer hints */ 95 96#ifdef ND6_DEBUG 97int nd6_debug = 1; 98#else 99int nd6_debug = 0; 100#endif 101 102/* for debugging? */ 103static int nd6_inuse, nd6_allocated; 104 105struct llinfo_nd6 llinfo_nd6 = {&llinfo_nd6, &llinfo_nd6}; 106static size_t nd_ifinfo_indexlim = 8; 107struct nd_ifinfo *nd_ifinfo = NULL; 108struct nd_drhead nd_defrouter; 109struct nd_prhead nd_prefix = { 0 }; 110 111int nd6_recalc_reachtm_interval = ND6_RECALC_REACHTM_INTERVAL; 112static struct sockaddr_in6 all1_sa; 113 114static void nd6_slowtimo __P((void *)); 115static int regen_tmpaddr __P((struct in6_ifaddr *)); 116 117struct callout nd6_slowtimo_ch; 118struct callout nd6_timer_ch; 119extern struct callout in6_tmpaddrtimer_ch; 120 121void 122nd6_init() 123{ 124 static int nd6_init_done = 0; 125 int i; 126 127 if (nd6_init_done) { 128 log(LOG_NOTICE, "nd6_init called more than once(ignored)\n"); 129 return; 130 } 131 132 all1_sa.sin6_family = AF_INET6; 133 all1_sa.sin6_len = sizeof(struct sockaddr_in6); 134 for (i = 0; i < sizeof(all1_sa.sin6_addr); i++) 135 all1_sa.sin6_addr.s6_addr[i] = 0xff; 136 137 /* initialization of the default router list */ 138 TAILQ_INIT(&nd_defrouter); 139 140 nd6_init_done = 1; 141 142 /* start timer */ 143 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 144 nd6_slowtimo, NULL); 145} 146 147void 148nd6_ifattach(ifp) 149 struct ifnet *ifp; 150{ 151 152 /* 153 * We have some arrays that should be indexed by if_index. 154 * since if_index will grow dynamically, they should grow too. 155 */ 156 if (nd_ifinfo == NULL || if_index >= nd_ifinfo_indexlim) { 157 size_t n; 158 caddr_t q; 159 160 while (if_index >= nd_ifinfo_indexlim) 161 nd_ifinfo_indexlim <<= 1; 162 163 /* grow nd_ifinfo */ 164 n = nd_ifinfo_indexlim * sizeof(struct nd_ifinfo); 165 q = (caddr_t)malloc(n, M_IP6NDP, M_WAITOK); 166 bzero(q, n); 167 if (nd_ifinfo) { 168 bcopy((caddr_t)nd_ifinfo, q, n/2); 169 free((caddr_t)nd_ifinfo, M_IP6NDP); 170 } 171 nd_ifinfo = (struct nd_ifinfo *)q; 172 } 173 174#define ND nd_ifinfo[ifp->if_index] 175 176 /* 177 * Don't initialize if called twice. 178 * XXX: to detect this, we should choose a member that is never set 179 * before initialization of the ND structure itself. We formaly used 180 * the linkmtu member, which was not suitable because it could be 181 * initialized via "ifconfig mtu". 182 */ 183 if (ND.basereachable) 184 return; 185 186 ND.linkmtu = ifindex2ifnet[ifp->if_index]->if_mtu; 187 ND.chlim = IPV6_DEFHLIM; 188 ND.basereachable = REACHABLE_TIME; 189 ND.reachable = ND_COMPUTE_RTIME(ND.basereachable); 190 ND.retrans = RETRANS_TIMER; 191 ND.receivedra = 0; 192 ND.flags = ND6_IFF_PERFORMNUD; 193 nd6_setmtu(ifp); 194#undef ND 195} 196 197/* 198 * Reset ND level link MTU. This function is called when the physical MTU 199 * changes, which means we might have to adjust the ND level MTU. 200 */ 201void 202nd6_setmtu(ifp) 203 struct ifnet *ifp; 204{ 205#define MIN(a,b) ((a) < (b) ? (a) : (b)) 206 struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index]; 207 u_long oldmaxmtu = ndi->maxmtu; 208 u_long oldlinkmtu = ndi->linkmtu; 209 210 switch (ifp->if_type) { 211 case IFT_ARCNET: /* XXX MTU handling needs more work */ 212 ndi->maxmtu = MIN(60480, ifp->if_mtu); 213 break; 214 case IFT_ETHER: 215 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu); 216 break; 217 case IFT_FDDI: 218 ndi->maxmtu = MIN(FDDIIPMTU, ifp->if_mtu); 219 break; 220 case IFT_ATM: 221 ndi->maxmtu = MIN(ATMMTU, ifp->if_mtu); 222 break; 223 case IFT_IEEE1394: /* XXX should be IEEE1394MTU(1500) */ 224 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu); 225 break; 226#ifdef IFT_IEEE80211 227 case IFT_IEEE80211: /* XXX should be IEEE80211MTU(1500) */ 228 ndi->maxmtu = MIN(ETHERMTU, ifp->if_mtu); 229 break; 230#endif 231 default: 232 ndi->maxmtu = ifp->if_mtu; 233 break; 234 } 235 236 if (oldmaxmtu != ndi->maxmtu) { 237 /* 238 * If the ND level MTU is not set yet, or if the maxmtu 239 * is reset to a smaller value than the ND level MTU, 240 * also reset the ND level MTU. 241 */ 242 if (ndi->linkmtu == 0 || 243 ndi->maxmtu < ndi->linkmtu) { 244 ndi->linkmtu = ndi->maxmtu; 245 /* also adjust in6_maxmtu if necessary. */ 246 if (oldlinkmtu == 0) { 247 /* 248 * XXX: the case analysis is grotty, but 249 * it is not efficient to call in6_setmaxmtu() 250 * here when we are during the initialization 251 * procedure. 252 */ 253 if (in6_maxmtu < ndi->linkmtu) 254 in6_maxmtu = ndi->linkmtu; 255 } else 256 in6_setmaxmtu(); 257 } 258 } 259#undef MIN 260} 261 262void 263nd6_option_init(opt, icmp6len, ndopts) 264 void *opt; 265 int icmp6len; 266 union nd_opts *ndopts; 267{ 268 bzero(ndopts, sizeof(*ndopts)); 269 ndopts->nd_opts_search = (struct nd_opt_hdr *)opt; 270 ndopts->nd_opts_last 271 = (struct nd_opt_hdr *)(((u_char *)opt) + icmp6len); 272 273 if (icmp6len == 0) { 274 ndopts->nd_opts_done = 1; 275 ndopts->nd_opts_search = NULL; 276 } 277} 278 279/* 280 * Take one ND option. 281 */ 282struct nd_opt_hdr * 283nd6_option(ndopts) 284 union nd_opts *ndopts; 285{ 286 struct nd_opt_hdr *nd_opt; 287 int olen; 288 289 if (!ndopts) 290 panic("ndopts == NULL in nd6_option\n"); 291 if (!ndopts->nd_opts_last) 292 panic("uninitialized ndopts in nd6_option\n"); 293 if (!ndopts->nd_opts_search) 294 return NULL; 295 if (ndopts->nd_opts_done) 296 return NULL; 297 298 nd_opt = ndopts->nd_opts_search; 299 300 /* make sure nd_opt_len is inside the buffer */ 301 if ((caddr_t)&nd_opt->nd_opt_len >= (caddr_t)ndopts->nd_opts_last) { 302 bzero(ndopts, sizeof(*ndopts)); 303 return NULL; 304 } 305 306 olen = nd_opt->nd_opt_len << 3; 307 if (olen == 0) { 308 /* 309 * Message validation requires that all included 310 * options have a length that is greater than zero. 311 */ 312 bzero(ndopts, sizeof(*ndopts)); 313 return NULL; 314 } 315 316 ndopts->nd_opts_search = (struct nd_opt_hdr *)((caddr_t)nd_opt + olen); 317 if (ndopts->nd_opts_search > ndopts->nd_opts_last) { 318 /* option overruns the end of buffer, invalid */ 319 bzero(ndopts, sizeof(*ndopts)); 320 return NULL; 321 } else if (ndopts->nd_opts_search == ndopts->nd_opts_last) { 322 /* reached the end of options chain */ 323 ndopts->nd_opts_done = 1; 324 ndopts->nd_opts_search = NULL; 325 } 326 return nd_opt; 327} 328 329/* 330 * Parse multiple ND options. 331 * This function is much easier to use, for ND routines that do not need 332 * multiple options of the same type. 333 */ 334int 335nd6_options(ndopts) 336 union nd_opts *ndopts; 337{ 338 struct nd_opt_hdr *nd_opt; 339 int i = 0; 340 341 if (!ndopts) 342 panic("ndopts == NULL in nd6_options\n"); 343 if (!ndopts->nd_opts_last) 344 panic("uninitialized ndopts in nd6_options\n"); 345 if (!ndopts->nd_opts_search) 346 return 0; 347 348 while (1) { 349 nd_opt = nd6_option(ndopts); 350 if (!nd_opt && !ndopts->nd_opts_last) { 351 /* 352 * Message validation requires that all included 353 * options have a length that is greater than zero. 354 */ 355 icmp6stat.icp6s_nd_badopt++; 356 bzero(ndopts, sizeof(*ndopts)); 357 return -1; 358 } 359 360 if (!nd_opt) 361 goto skip1; 362 363 switch (nd_opt->nd_opt_type) { 364 case ND_OPT_SOURCE_LINKADDR: 365 case ND_OPT_TARGET_LINKADDR: 366 case ND_OPT_MTU: 367 case ND_OPT_REDIRECTED_HEADER: 368 if (ndopts->nd_opt_array[nd_opt->nd_opt_type]) { 369 nd6log((LOG_INFO, 370 "duplicated ND6 option found (type=%d)\n", 371 nd_opt->nd_opt_type)); 372 /* XXX bark? */ 373 } else { 374 ndopts->nd_opt_array[nd_opt->nd_opt_type] 375 = nd_opt; 376 } 377 break; 378 case ND_OPT_PREFIX_INFORMATION: 379 if (ndopts->nd_opt_array[nd_opt->nd_opt_type] == 0) { 380 ndopts->nd_opt_array[nd_opt->nd_opt_type] 381 = nd_opt; 382 } 383 ndopts->nd_opts_pi_end = 384 (struct nd_opt_prefix_info *)nd_opt; 385 break; 386 default: 387 /* 388 * Unknown options must be silently ignored, 389 * to accomodate future extension to the protocol. 390 */ 391 nd6log((LOG_DEBUG, 392 "nd6_options: unsupported option %d - " 393 "option ignored\n", nd_opt->nd_opt_type)); 394 } 395 396skip1: 397 i++; 398 if (i > nd6_maxndopt) { 399 icmp6stat.icp6s_nd_toomanyopt++; 400 nd6log((LOG_INFO, "too many loop in nd opt\n")); 401 break; 402 } 403 404 if (ndopts->nd_opts_done) 405 break; 406 } 407 408 return 0; 409} 410 411/* 412 * ND6 timer routine to expire default route list and prefix list 413 */ 414void 415nd6_timer(ignored_arg) 416 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 /* XXX BSD/OS separates this code -- itojun */ 432 while (ln && ln != &llinfo_nd6) { 433 struct rtentry *rt; 434 struct sockaddr_in6 *dst; 435 struct llinfo_nd6 *next = ln->ln_next; 436 /* XXX: used for the DELAY case only: */ 437 struct nd_ifinfo *ndi = NULL; 438 439 if ((rt = ln->ln_rt) == NULL) { 440 ln = next; 441 continue; 442 } 443 if ((ifp = rt->rt_ifp) == NULL) { 444 ln = next; 445 continue; 446 } 447 ndi = &nd_ifinfo[ifp->if_index]; 448 dst = (struct sockaddr_in6 *)rt_key(rt); 449 450 if (ln->ln_expire > time_second) { 451 ln = next; 452 continue; 453 } 454 455 /* sanity check */ 456 if (!rt) 457 panic("rt=0 in nd6_timer(ln=%p)\n", ln); 458 if (rt->rt_llinfo && (struct llinfo_nd6 *)rt->rt_llinfo != ln) 459 panic("rt_llinfo(%p) is not equal to ln(%p)\n", 460 rt->rt_llinfo, ln); 461 if (!dst) 462 panic("dst=0 in nd6_timer(ln=%p)\n", ln); 463 464 switch (ln->ln_state) { 465 case ND6_LLINFO_INCOMPLETE: 466 if (ln->ln_asked < nd6_mmaxtries) { 467 ln->ln_asked++; 468 ln->ln_expire = time_second + 469 nd_ifinfo[ifp->if_index].retrans / 1000; 470 nd6_ns_output(ifp, NULL, &dst->sin6_addr, 471 ln, 0); 472 } else { 473 struct mbuf *m = ln->ln_hold; 474 if (m) { 475 if (rt->rt_ifp) { 476 /* 477 * Fake rcvif to make ICMP error 478 * more helpful in diagnosing 479 * for the receiver. 480 * XXX: should we consider 481 * older rcvif? 482 */ 483 m->m_pkthdr.rcvif = rt->rt_ifp; 484 } 485 icmp6_error(m, ICMP6_DST_UNREACH, 486 ICMP6_DST_UNREACH_ADDR, 0); 487 ln->ln_hold = NULL; 488 } 489 next = nd6_free(rt); 490 } 491 break; 492 case ND6_LLINFO_REACHABLE: 493 if (ln->ln_expire) { 494 ln->ln_state = ND6_LLINFO_STALE; 495 ln->ln_expire = time_second + nd6_gctimer; 496 } 497 break; 498 499 case ND6_LLINFO_STALE: 500 /* Garbage Collection(RFC 2461 5.3) */ 501 if (ln->ln_expire) 502 next = nd6_free(rt); 503 break; 504 505 case ND6_LLINFO_DELAY: 506 if (ndi && (ndi->flags & ND6_IFF_PERFORMNUD) != 0) { 507 /* We need NUD */ 508 ln->ln_asked = 1; 509 ln->ln_state = ND6_LLINFO_PROBE; 510 ln->ln_expire = time_second + 511 ndi->retrans / 1000; 512 nd6_ns_output(ifp, &dst->sin6_addr, 513 &dst->sin6_addr, 514 ln, 0); 515 } else { 516 ln->ln_state = ND6_LLINFO_STALE; /* XXX */ 517 ln->ln_expire = time_second + nd6_gctimer; 518 } 519 break; 520 case ND6_LLINFO_PROBE: 521 if (ln->ln_asked < nd6_umaxtries) { 522 ln->ln_asked++; 523 ln->ln_expire = time_second + 524 nd_ifinfo[ifp->if_index].retrans / 1000; 525 nd6_ns_output(ifp, &dst->sin6_addr, 526 &dst->sin6_addr, ln, 0); 527 } else { 528 next = nd6_free(rt); 529 } 530 break; 531 } 532 ln = next; 533 } 534 535 /* expire default router list */ 536 dr = TAILQ_FIRST(&nd_defrouter); 537 while (dr) { 538 if (dr->expire && dr->expire < time_second) { 539 struct nd_defrouter *t; 540 t = TAILQ_NEXT(dr, dr_entry); 541 defrtrlist_del(dr); 542 dr = t; 543 } else { 544 dr = TAILQ_NEXT(dr, dr_entry); 545 } 546 } 547 548 /* 549 * expire interface addresses. 550 * in the past the loop was inside prefix expiry processing. 551 * However, from a stricter speci-confrmance standpoint, we should 552 * rather separate address lifetimes and prefix lifetimes. 553 */ 554 addrloop: 555 for (ia6 = in6_ifaddr; ia6; ia6 = nia6) { 556 nia6 = ia6->ia_next; 557 /* check address lifetime */ 558 lt6 = &ia6->ia6_lifetime; 559 if (IFA6_IS_INVALID(ia6)) { 560 int regen = 0; 561 562 /* 563 * If the expiring address is temporary, try 564 * regenerating a new one. This would be useful when 565 * we suspended a laptop PC, then turned on after a 566 * period that could invalidate all temporary 567 * addresses. Although we may have to restart the 568 * loop (see below), it must be after purging the 569 * address. Otherwise, we'd see an infinite loop of 570 * regeneration. 571 */ 572 if (ip6_use_tempaddr && 573 (ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 574 if (regen_tmpaddr(ia6) == 0) 575 regen = 1; 576 } 577 578 in6_purgeaddr(&ia6->ia_ifa); 579 580 if (regen) 581 goto addrloop; /* XXX: see below */ 582 } else 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 an addition, but a 604 * deletion,) 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 if (IFA6_IS_DEPRECATED(ia6)) { 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 * we offset expire time by NDPR_KEEP_EXPIRE, so that we 629 * can use the old prefix information to validate the 630 * next prefix information to come. See prelist_update() 631 * for actual validation. 632 * 633 * I don't think such an offset is necessary. 634 * (jinmei@kame.net, 20010130). 635 */ 636 if (pr->ndpr_expire && pr->ndpr_expire < time_second) { 637 struct nd_prefix *t; 638 t = pr->ndpr_next; 639 640 /* 641 * address expiration and prefix expiration are 642 * separate. NEVER perform in6_purgeaddr here. 643 */ 644 645 prelist_remove(pr); 646 pr = t; 647 } else 648 pr = pr->ndpr_next; 649 } 650 splx(s); 651} 652 653static int 654regen_tmpaddr(ia6) 655 struct in6_ifaddr *ia6; /* deprecated/invalidated temporary address */ 656{ 657 struct ifaddr *ifa; 658 struct ifnet *ifp; 659 struct in6_ifaddr *public_ifa6 = NULL; 660 661 ifp = ia6->ia_ifa.ifa_ifp; 662 for (ifa = ifp->if_addrlist.tqh_first; ifa; 663 ifa = ifa->ifa_list.tqe_next) 664 { 665 struct in6_ifaddr *it6; 666 667 if (ifa->ifa_addr->sa_family != AF_INET6) 668 continue; 669 670 it6 = (struct in6_ifaddr *)ifa; 671 672 /* ignore no autoconf addresses. */ 673 if ((it6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 674 continue; 675 676 /* ignore autoconf addresses with different prefixes. */ 677 if (it6->ia6_ndpr == NULL || it6->ia6_ndpr != ia6->ia6_ndpr) 678 continue; 679 680 /* 681 * Now we are looking at an autoconf address with the same 682 * prefix as ours. If the address is temporary and is still 683 * preferred, do not create another one. It would be rare, but 684 * could happen, for example, when we resume a laptop PC after 685 * a long period. 686 */ 687 if ((it6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && 688 !IFA6_IS_DEPRECATED(it6)) { 689 public_ifa6 = NULL; 690 break; 691 } 692 693 /* 694 * This is a public autoconf address that has the same prefix 695 * as ours. If it is preferred, keep it. We can't break the 696 * loop here, because there may be a still-preferred temporary 697 * address with the prefix. 698 */ 699 if (!IFA6_IS_DEPRECATED(it6)) 700 public_ifa6 = it6; 701 } 702 703 if (public_ifa6 != NULL) { 704 int e; 705 706 if ((e = in6_tmpifadd(public_ifa6, 0)) != 0) { 707 log(LOG_NOTICE, "regen_tmpaddr: failed to create a new" 708 " tmp addr,errno=%d\n", e); 709 return(-1); 710 } 711 return(0); 712 } 713 714 return(-1); 715} 716 717/* 718 * Nuke neighbor cache/prefix/default router management table, right before 719 * ifp goes away. 720 */ 721void 722nd6_purge(ifp) 723 struct ifnet *ifp; 724{ 725 struct llinfo_nd6 *ln, *nln; 726 struct nd_defrouter *dr, *ndr, drany; 727 struct nd_prefix *pr, *npr; 728 729 /* Nuke default router list entries toward ifp */ 730 if ((dr = TAILQ_FIRST(&nd_defrouter)) != NULL) { 731 /* 732 * The first entry of the list may be stored in 733 * the routing table, so we'll delete it later. 734 */ 735 for (dr = TAILQ_NEXT(dr, dr_entry); dr; dr = ndr) { 736 ndr = TAILQ_NEXT(dr, dr_entry); 737 if (dr->ifp == ifp) 738 defrtrlist_del(dr); 739 } 740 dr = TAILQ_FIRST(&nd_defrouter); 741 if (dr->ifp == ifp) 742 defrtrlist_del(dr); 743 } 744 745 /* Nuke prefix list entries toward ifp */ 746 for (pr = nd_prefix.lh_first; pr; pr = npr) { 747 npr = pr->ndpr_next; 748 if (pr->ndpr_ifp == ifp) { 749 /* 750 * Previously, pr->ndpr_addr is removed as well, 751 * but I strongly believe we don't have to do it. 752 * nd6_purge() is only called from in6_ifdetach(), 753 * which removes all the associated interface addresses 754 * by itself. 755 * (jinmei@kame.net 20010129) 756 */ 757 prelist_remove(pr); 758 } 759 } 760 761 /* cancel default outgoing interface setting */ 762 if (nd6_defifindex == ifp->if_index) 763 nd6_setdefaultiface(0); 764 765 /* refresh default router list */ 766 bzero(&drany, sizeof(drany)); 767 defrouter_delreq(&drany, 0); 768 defrouter_select(); 769 770 /* 771 * Nuke neighbor cache entries for the ifp. 772 * Note that rt->rt_ifp may not be the same as ifp, 773 * due to KAME goto ours hack. See RTM_RESOLVE case in 774 * nd6_rtrequest(), and ip6_input(). 775 */ 776 ln = llinfo_nd6.ln_next; 777 while (ln && ln != &llinfo_nd6) { 778 struct rtentry *rt; 779 struct sockaddr_dl *sdl; 780 781 nln = ln->ln_next; 782 rt = ln->ln_rt; 783 if (rt && rt->rt_gateway && 784 rt->rt_gateway->sa_family == AF_LINK) { 785 sdl = (struct sockaddr_dl *)rt->rt_gateway; 786 if (sdl->sdl_index == ifp->if_index) 787 nln = nd6_free(rt); 788 } 789 ln = nln; 790 } 791} 792 793struct rtentry * 794nd6_lookup(addr6, create, ifp) 795 struct in6_addr *addr6; 796 int create; 797 struct ifnet *ifp; 798{ 799 struct rtentry *rt; 800 struct sockaddr_in6 sin6; 801 802 bzero(&sin6, sizeof(sin6)); 803 sin6.sin6_len = sizeof(struct sockaddr_in6); 804 sin6.sin6_family = AF_INET6; 805 sin6.sin6_addr = *addr6; 806#ifdef SCOPEDROUTING 807 sin6.sin6_scope_id = in6_addr2scopeid(ifp, addr6); 808#endif 809 rt = rtalloc1((struct sockaddr *)&sin6, create, 0UL); 810 if (rt && (rt->rt_flags & RTF_LLINFO) == 0) { 811 /* 812 * This is the case for the default route. 813 * If we want to create a neighbor cache for the address, we 814 * should free the route for the destination and allocate an 815 * interface route. 816 */ 817 if (create) { 818 RTFREE(rt); 819 rt = 0; 820 } 821 } 822 if (!rt) { 823 if (create && ifp) { 824 int e; 825 826 /* 827 * If no route is available and create is set, 828 * we allocate a host route for the destination 829 * and treat it like an interface route. 830 * This hack is necessary for a neighbor which can't 831 * be covered by our own prefix. 832 */ 833 struct ifaddr *ifa = 834 ifaof_ifpforaddr((struct sockaddr *)&sin6, ifp); 835 if (ifa == NULL) 836 return(NULL); 837 838 /* 839 * Create a new route. RTF_LLINFO is necessary 840 * to create a Neighbor Cache entry for the 841 * destination in nd6_rtrequest which will be 842 * called in rtequest via ifa->ifa_rtrequest. 843 */ 844 if ((e = rtrequest(RTM_ADD, (struct sockaddr *)&sin6, 845 ifa->ifa_addr, 846 (struct sockaddr *)&all1_sa, 847 (ifa->ifa_flags | 848 RTF_HOST | RTF_LLINFO) & 849 ~RTF_CLONING, 850 &rt)) != 0) 851 log(LOG_ERR, 852 "nd6_lookup: failed to add route for a " 853 "neighbor(%s), errno=%d\n", 854 ip6_sprintf(addr6), e); 855 if (rt == NULL) 856 return(NULL); 857 if (rt->rt_llinfo) { 858 struct llinfo_nd6 *ln = 859 (struct llinfo_nd6 *)rt->rt_llinfo; 860 ln->ln_state = ND6_LLINFO_NOSTATE; 861 } 862 } else 863 return(NULL); 864 } 865 rt->rt_refcnt--; 866 /* 867 * Validation for the entry. 868 * XXX: we can't use rt->rt_ifp to check for the interface, since 869 * it might be the loopback interface if the entry is for our 870 * own address on a non-loopback interface. Instead, we should 871 * use rt->rt_ifa->ifa_ifp, which would specify the REAL interface. 872 */ 873 if ((rt->rt_flags & RTF_GATEWAY) || (rt->rt_flags & RTF_LLINFO) == 0 || 874 rt->rt_gateway->sa_family != AF_LINK || 875 (ifp && rt->rt_ifa->ifa_ifp != ifp)) { 876 if (create) { 877 log(LOG_DEBUG, "nd6_lookup: failed to lookup %s (if = %s)\n", 878 ip6_sprintf(addr6), ifp ? if_name(ifp) : "unspec"); 879 /* xxx more logs... kazu */ 880 } 881 return(0); 882 } 883 return(rt); 884} 885 886/* 887 * Detect if a given IPv6 address identifies a neighbor on a given link. 888 * XXX: should take care of the destination of a p2p link? 889 */ 890int 891nd6_is_addr_neighbor(addr, ifp) 892 struct sockaddr_in6 *addr; 893 struct ifnet *ifp; 894{ 895 struct ifaddr *ifa; 896 int i; 897 898#define IFADDR6(a) ((((struct in6_ifaddr *)(a))->ia_addr).sin6_addr) 899#define IFMASK6(a) ((((struct in6_ifaddr *)(a))->ia_prefixmask).sin6_addr) 900 901 /* 902 * A link-local address is always a neighbor. 903 * XXX: we should use the sin6_scope_id field rather than the embedded 904 * interface index. 905 */ 906 if (IN6_IS_ADDR_LINKLOCAL(&addr->sin6_addr) && 907 ntohs(*(u_int16_t *)&addr->sin6_addr.s6_addr[2]) == ifp->if_index) 908 return(1); 909 910 /* 911 * If the address matches one of our addresses, 912 * it should be a neighbor. 913 */ 914 for (ifa = ifp->if_addrlist.tqh_first; 915 ifa; 916 ifa = ifa->ifa_list.tqe_next) 917 { 918 if (ifa->ifa_addr->sa_family != AF_INET6) 919 next: continue; 920 921 for (i = 0; i < 4; i++) { 922 if ((IFADDR6(ifa).s6_addr32[i] ^ 923 addr->sin6_addr.s6_addr32[i]) & 924 IFMASK6(ifa).s6_addr32[i]) 925 goto next; 926 } 927 return(1); 928 } 929 930 /* 931 * Even if the address matches none of our addresses, it might be 932 * in the neighbor cache. 933 */ 934 if (nd6_lookup(&addr->sin6_addr, 0, ifp)) 935 return(1); 936 937 return(0); 938#undef IFADDR6 939#undef IFMASK6 940} 941 942/* 943 * Free an nd6 llinfo entry. 944 */ 945struct llinfo_nd6 * 946nd6_free(rt) 947 struct rtentry *rt; 948{ 949 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo, *next; 950 struct in6_addr in6 = ((struct sockaddr_in6 *)rt_key(rt))->sin6_addr; 951 struct nd_defrouter *dr; 952 953 /* 954 * we used to have pfctlinput(PRC_HOSTDEAD) here. 955 * even though it is not harmful, it was not really necessary. 956 */ 957 958 if (!ip6_forwarding && ip6_accept_rtadv) { /* XXX: too restrictive? */ 959 int s; 960 s = splnet(); 961 dr = defrouter_lookup(&((struct sockaddr_in6 *)rt_key(rt))->sin6_addr, 962 rt->rt_ifp); 963 964 if (ln->ln_router || dr) { 965 /* 966 * rt6_flush must be called whether or not the neighbor 967 * is in the Default Router List. 968 * See a corresponding comment in nd6_na_input(). 969 */ 970 rt6_flush(&in6, rt->rt_ifp); 971 } 972 973 if (dr) { 974 /* 975 * Unreachablity of a router might affect the default 976 * router selection and on-link detection of advertised 977 * prefixes. 978 */ 979 980 /* 981 * Temporarily fake the state to choose a new default 982 * router and to perform on-link determination of 983 * prefixes coreectly. 984 * Below the state will be set correctly, 985 * or the entry itself will be deleted. 986 */ 987 ln->ln_state = ND6_LLINFO_INCOMPLETE; 988 989 /* 990 * Since defrouter_select() does not affect the 991 * on-link determination and MIP6 needs the check 992 * before the default router selection, we perform 993 * the check now. 994 */ 995 pfxlist_onlink_check(); 996 997 if (dr == TAILQ_FIRST(&nd_defrouter)) { 998 /* 999 * It is used as the current default router, 1000 * so we have to move it to the end of the 1001 * list and choose a new one. 1002 * XXX: it is not very efficient if this is 1003 * the only router. 1004 */ 1005 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 1006 TAILQ_INSERT_TAIL(&nd_defrouter, dr, dr_entry); 1007 1008 defrouter_select(); 1009 } 1010 } 1011 splx(s); 1012 } 1013 1014 /* 1015 * Before deleting the entry, remember the next entry as the 1016 * return value. We need this because pfxlist_onlink_check() above 1017 * might have freed other entries (particularly the old next entry) as 1018 * a side effect (XXX). 1019 */ 1020 next = ln->ln_next; 1021 1022 /* 1023 * Detach the route from the routing tree and the list of neighbor 1024 * caches, and disable the route entry not to be used in already 1025 * cached routes. 1026 */ 1027 rtrequest(RTM_DELETE, rt_key(rt), (struct sockaddr *)0, 1028 rt_mask(rt), 0, (struct rtentry **)0); 1029 1030 return(next); 1031} 1032 1033/* 1034 * Upper-layer reachability hint for Neighbor Unreachability Detection. 1035 * 1036 * XXX cost-effective metods? 1037 */ 1038void 1039nd6_nud_hint(rt, dst6, force) 1040 struct rtentry *rt; 1041 struct in6_addr *dst6; 1042 int force; 1043{ 1044 struct llinfo_nd6 *ln; 1045 1046 /* 1047 * If the caller specified "rt", use that. Otherwise, resolve the 1048 * routing table by supplied "dst6". 1049 */ 1050 if (!rt) { 1051 if (!dst6) 1052 return; 1053 if (!(rt = nd6_lookup(dst6, 0, NULL))) 1054 return; 1055 } 1056 1057 if ((rt->rt_flags & RTF_GATEWAY) != 0 || 1058 (rt->rt_flags & RTF_LLINFO) == 0 || 1059 !rt->rt_llinfo || !rt->rt_gateway || 1060 rt->rt_gateway->sa_family != AF_LINK) { 1061 /* This is not a host route. */ 1062 return; 1063 } 1064 1065 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1066 if (ln->ln_state < ND6_LLINFO_REACHABLE) 1067 return; 1068 1069 /* 1070 * if we get upper-layer reachability confirmation many times, 1071 * it is possible we have false information. 1072 */ 1073 if (!force) { 1074 ln->ln_byhint++; 1075 if (ln->ln_byhint > nd6_maxnudhint) 1076 return; 1077 } 1078 1079 ln->ln_state = ND6_LLINFO_REACHABLE; 1080 if (ln->ln_expire) 1081 ln->ln_expire = time_second + 1082 nd_ifinfo[rt->rt_ifp->if_index].reachable; 1083} 1084 1085void 1086nd6_rtrequest(req, rt, sa) 1087 int req; 1088 struct rtentry *rt; 1089 struct sockaddr *sa; /* xxx unused */ 1090{ 1091 struct sockaddr *gate = rt->rt_gateway; 1092 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1093 static struct sockaddr_dl null_sdl = {sizeof(null_sdl), AF_LINK}; 1094 struct ifnet *ifp = rt->rt_ifp; 1095 struct ifaddr *ifa; 1096 1097 if (rt->rt_flags & RTF_GATEWAY) 1098 return; 1099 1100 if (nd6_need_cache(ifp) == 0 && (rt->rt_flags & RTF_HOST) == 0) { 1101 /* 1102 * This is probably an interface direct route for a link 1103 * which does not need neighbor caches (e.g. fe80::%lo0/64). 1104 * We do not need special treatment below for such a route. 1105 * Moreover, the RTF_LLINFO flag which would be set below 1106 * would annoy the ndp(8) command. 1107 */ 1108 return; 1109 } 1110 1111 switch (req) { 1112 case RTM_ADD: 1113 /* 1114 * There is no backward compatibility :) 1115 * 1116 * if ((rt->rt_flags & RTF_HOST) == 0 && 1117 * SIN(rt_mask(rt))->sin_addr.s_addr != 0xffffffff) 1118 * rt->rt_flags |= RTF_CLONING; 1119 */ 1120 if (rt->rt_flags & (RTF_CLONING | RTF_LLINFO)) { 1121 /* 1122 * Case 1: This route should come from 1123 * a route to interface. RTF_LLINFO flag is set 1124 * for a host route whose destination should be 1125 * treated as on-link. 1126 */ 1127 rt_setgate(rt, rt_key(rt), 1128 (struct sockaddr *)&null_sdl); 1129 gate = rt->rt_gateway; 1130 SDL(gate)->sdl_type = ifp->if_type; 1131 SDL(gate)->sdl_index = ifp->if_index; 1132 if (ln) 1133 ln->ln_expire = time_second; 1134#if 1 1135 if (ln && ln->ln_expire == 0) { 1136 /* kludge for desktops */ 1137#if 0 1138 printf("nd6_request: time.tv_sec is zero; " 1139 "treat it as 1\n"); 1140#endif 1141 ln->ln_expire = 1; 1142 } 1143#endif 1144 if (rt->rt_flags & RTF_CLONING) 1145 break; 1146 } 1147 /* 1148 * In IPv4 code, we try to annonuce new RTF_ANNOUNCE entry here. 1149 * We don't do that here since llinfo is not ready yet. 1150 * 1151 * There are also couple of other things to be discussed: 1152 * - unsolicited NA code needs improvement beforehand 1153 * - RFC2461 says we MAY send multicast unsolicited NA 1154 * (7.2.6 paragraph 4), however, it also says that we 1155 * SHOULD provide a mechanism to prevent multicast NA storm. 1156 * we don't have anything like it right now. 1157 * note that the mechanism needs a mutual agreement 1158 * between proxies, which means that we need to implement 1159 * a new protocol, or a new kludge. 1160 * - from RFC2461 6.2.4, host MUST NOT send an unsolicited NA. 1161 * we need to check ip6forwarding before sending it. 1162 * (or should we allow proxy ND configuration only for 1163 * routers? there's no mention about proxy ND from hosts) 1164 */ 1165#if 0 1166 /* XXX it does not work */ 1167 if (rt->rt_flags & RTF_ANNOUNCE) 1168 nd6_na_output(ifp, 1169 &SIN6(rt_key(rt))->sin6_addr, 1170 &SIN6(rt_key(rt))->sin6_addr, 1171 ip6_forwarding ? ND_NA_FLAG_ROUTER : 0, 1172 1, NULL); 1173#endif 1174 /* FALLTHROUGH */ 1175 case RTM_RESOLVE: 1176 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) { 1177 /* 1178 * Address resolution isn't necessary for a point to 1179 * point link, so we can skip this test for a p2p link. 1180 */ 1181 if (gate->sa_family != AF_LINK || 1182 gate->sa_len < sizeof(null_sdl)) { 1183 log(LOG_DEBUG, 1184 "nd6_rtrequest: bad gateway value: %s\n", 1185 if_name(ifp)); 1186 break; 1187 } 1188 SDL(gate)->sdl_type = ifp->if_type; 1189 SDL(gate)->sdl_index = ifp->if_index; 1190 } 1191 if (ln != NULL) 1192 break; /* This happens on a route change */ 1193 /* 1194 * Case 2: This route may come from cloning, or a manual route 1195 * add with a LL address. 1196 */ 1197 R_Malloc(ln, struct llinfo_nd6 *, sizeof(*ln)); 1198 rt->rt_llinfo = (caddr_t)ln; 1199 if (!ln) { 1200 log(LOG_DEBUG, "nd6_rtrequest: malloc failed\n"); 1201 break; 1202 } 1203 nd6_inuse++; 1204 nd6_allocated++; 1205 Bzero(ln, sizeof(*ln)); 1206 ln->ln_rt = rt; 1207 /* this is required for "ndp" command. - shin */ 1208 if (req == RTM_ADD) { 1209 /* 1210 * gate should have some valid AF_LINK entry, 1211 * and ln->ln_expire should have some lifetime 1212 * which is specified by ndp command. 1213 */ 1214 ln->ln_state = ND6_LLINFO_REACHABLE; 1215 ln->ln_byhint = 0; 1216 } else { 1217 /* 1218 * When req == RTM_RESOLVE, rt is created and 1219 * initialized in rtrequest(), so rt_expire is 0. 1220 */ 1221 ln->ln_state = ND6_LLINFO_NOSTATE; 1222 ln->ln_expire = time_second; 1223 } 1224 rt->rt_flags |= RTF_LLINFO; 1225 ln->ln_next = llinfo_nd6.ln_next; 1226 llinfo_nd6.ln_next = ln; 1227 ln->ln_prev = &llinfo_nd6; 1228 ln->ln_next->ln_prev = ln; 1229 1230 /* 1231 * check if rt_key(rt) is one of my address assigned 1232 * to the interface. 1233 */ 1234 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(rt->rt_ifp, 1235 &SIN6(rt_key(rt))->sin6_addr); 1236 if (ifa) { 1237 caddr_t macp = nd6_ifptomac(ifp); 1238 ln->ln_expire = 0; 1239 ln->ln_state = ND6_LLINFO_REACHABLE; 1240 ln->ln_byhint = 0; 1241 if (macp) { 1242 Bcopy(macp, LLADDR(SDL(gate)), ifp->if_addrlen); 1243 SDL(gate)->sdl_alen = ifp->if_addrlen; 1244 } 1245 if (nd6_useloopback) { 1246 rt->rt_ifp = &loif[0]; /*XXX*/ 1247 /* 1248 * Make sure rt_ifa be equal to the ifaddr 1249 * corresponding to the address. 1250 * We need this because when we refer 1251 * rt_ifa->ia6_flags in ip6_input, we assume 1252 * that the rt_ifa points to the address instead 1253 * of the loopback address. 1254 */ 1255 if (ifa != rt->rt_ifa) { 1256 IFAFREE(rt->rt_ifa); 1257 IFAREF(ifa); 1258 rt->rt_ifa = ifa; 1259 } 1260 } 1261 } else if (rt->rt_flags & RTF_ANNOUNCE) { 1262 ln->ln_expire = 0; 1263 ln->ln_state = ND6_LLINFO_REACHABLE; 1264 ln->ln_byhint = 0; 1265 1266 /* join solicited node multicast for proxy ND */ 1267 if (ifp->if_flags & IFF_MULTICAST) { 1268 struct in6_addr llsol; 1269 int error; 1270 1271 llsol = SIN6(rt_key(rt))->sin6_addr; 1272 llsol.s6_addr16[0] = htons(0xff02); 1273 llsol.s6_addr16[1] = htons(ifp->if_index); 1274 llsol.s6_addr32[1] = 0; 1275 llsol.s6_addr32[2] = htonl(1); 1276 llsol.s6_addr8[12] = 0xff; 1277 1278 if (!in6_addmulti(&llsol, ifp, &error)) { 1279 nd6log((LOG_ERR, "%s: failed to join " 1280 "%s (errno=%d)\n", if_name(ifp), 1281 ip6_sprintf(&llsol), error)); 1282 } 1283 } 1284 } 1285 break; 1286 1287 case RTM_DELETE: 1288 if (!ln) 1289 break; 1290 /* leave from solicited node multicast for proxy ND */ 1291 if ((rt->rt_flags & RTF_ANNOUNCE) != 0 && 1292 (ifp->if_flags & IFF_MULTICAST) != 0) { 1293 struct in6_addr llsol; 1294 struct in6_multi *in6m; 1295 1296 llsol = SIN6(rt_key(rt))->sin6_addr; 1297 llsol.s6_addr16[0] = htons(0xff02); 1298 llsol.s6_addr16[1] = htons(ifp->if_index); 1299 llsol.s6_addr32[1] = 0; 1300 llsol.s6_addr32[2] = htonl(1); 1301 llsol.s6_addr8[12] = 0xff; 1302 1303 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 1304 if (in6m) 1305 in6_delmulti(in6m); 1306 } 1307 nd6_inuse--; 1308 ln->ln_next->ln_prev = ln->ln_prev; 1309 ln->ln_prev->ln_next = ln->ln_next; 1310 ln->ln_prev = NULL; 1311 rt->rt_llinfo = 0; 1312 rt->rt_flags &= ~RTF_LLINFO; 1313 if (ln->ln_hold) 1314 m_freem(ln->ln_hold); 1315 Free((caddr_t)ln); 1316 } 1317} 1318 1319int 1320nd6_ioctl(cmd, data, ifp) 1321 u_long cmd; 1322 caddr_t data; 1323 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 */ 1576struct rtentry * 1577nd6_cache_lladdr(ifp, from, lladdr, lladdrlen, type, code) 1578 struct ifnet *ifp; 1579 struct in6_addr *from; 1580 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) { 1633fail: 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 return rt; 1787} 1788 1789static void 1790nd6_slowtimo(ignored_arg) 1791 void *ignored_arg; 1792{ 1793 int s = splnet(); 1794 int i; 1795 struct nd_ifinfo *nd6if; 1796 1797 callout_reset(&nd6_slowtimo_ch, ND6_SLOWTIMER_INTERVAL * hz, 1798 nd6_slowtimo, NULL); 1799 for (i = 1; i < if_index + 1; i++) { 1800 if (!nd_ifinfo || i >= nd_ifinfo_indexlim) 1801 continue; 1802 nd6if = &nd_ifinfo[i]; 1803 if (nd6if->basereachable && /* already initialized */ 1804 (nd6if->recalctm -= ND6_SLOWTIMER_INTERVAL) <= 0) { 1805 /* 1806 * Since reachable time rarely changes by router 1807 * advertisements, we SHOULD insure that a new random 1808 * value gets recomputed at least once every few hours. 1809 * (RFC 2461, 6.3.4) 1810 */ 1811 nd6if->recalctm = nd6_recalc_reachtm_interval; 1812 nd6if->reachable = ND_COMPUTE_RTIME(nd6if->basereachable); 1813 } 1814 } 1815 splx(s); 1816} 1817 1818#define senderr(e) { error = (e); goto bad;} 1819int 1820nd6_output(ifp, origifp, m0, dst, rt0) 1821 struct ifnet *ifp; 1822 struct ifnet *origifp; 1823 struct mbuf *m0; 1824 struct sockaddr_in6 *dst; 1825 struct rtentry *rt0; 1826{ 1827 struct mbuf *m = m0; 1828 struct rtentry *rt = rt0; 1829 struct sockaddr_in6 *gw6 = NULL; 1830 struct llinfo_nd6 *ln = NULL; 1831 int error = 0; 1832 1833 if (IN6_IS_ADDR_MULTICAST(&dst->sin6_addr)) 1834 goto sendpkt; 1835 1836 if (nd6_need_cache(ifp) == 0) 1837 goto sendpkt; 1838 1839 /* 1840 * next hop determination. This routine is derived from ether_outpout. 1841 */ 1842 if (rt) { 1843 if ((rt->rt_flags & RTF_UP) == 0) { 1844 if ((rt0 = rt = rtalloc1((struct sockaddr *)dst, 1, 0UL)) != 1845 NULL) 1846 { 1847 rt->rt_refcnt--; 1848 if (rt->rt_ifp != ifp) { 1849 /* XXX: loop care? */ 1850 return nd6_output(ifp, origifp, m0, 1851 dst, rt); 1852 } 1853 } else 1854 senderr(EHOSTUNREACH); 1855 } 1856 1857 if (rt->rt_flags & RTF_GATEWAY) { 1858 gw6 = (struct sockaddr_in6 *)rt->rt_gateway; 1859 1860 /* 1861 * We skip link-layer address resolution and NUD 1862 * if the gateway is not a neighbor from ND point 1863 * of view, regardless the value of the 1864 * nd_ifinfo.flags. 1865 * The second condition is a bit tricky: we skip 1866 * if the gateway is our own address, which is 1867 * sometimes used to install a route to a p2p link. 1868 */ 1869 if (!nd6_is_addr_neighbor(gw6, ifp) || 1870 in6ifa_ifpwithaddr(ifp, &gw6->sin6_addr)) { 1871 /* 1872 * We allow this kind of tricky route only 1873 * when the outgoing interface is p2p. 1874 * XXX: we may need a more generic rule here. 1875 */ 1876 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 1877 senderr(EHOSTUNREACH); 1878 1879 goto sendpkt; 1880 } 1881 1882 if (rt->rt_gwroute == 0) 1883 goto lookup; 1884 if (((rt = rt->rt_gwroute)->rt_flags & RTF_UP) == 0) { 1885 rtfree(rt); rt = rt0; 1886 lookup: rt->rt_gwroute = rtalloc1(rt->rt_gateway, 1, 0UL); 1887 if ((rt = rt->rt_gwroute) == 0) 1888 senderr(EHOSTUNREACH); 1889 } 1890 } 1891 } 1892 1893 /* 1894 * Address resolution or Neighbor Unreachability Detection 1895 * for the next hop. 1896 * At this point, the destination of the packet must be a unicast 1897 * or an anycast address(i.e. not a multicast). 1898 */ 1899 1900 /* Look up the neighbor cache for the nexthop */ 1901 if (rt && (rt->rt_flags & RTF_LLINFO) != 0) 1902 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1903 else { 1904 /* 1905 * Since nd6_is_addr_neighbor() internally calls nd6_lookup(), 1906 * the condition below is not very efficient. But we believe 1907 * it is tolerable, because this should be a rare case. 1908 */ 1909 if (nd6_is_addr_neighbor(dst, ifp) && 1910 (rt = nd6_lookup(&dst->sin6_addr, 1, ifp)) != NULL) 1911 ln = (struct llinfo_nd6 *)rt->rt_llinfo; 1912 } 1913 if (!ln || !rt) { 1914 if ((ifp->if_flags & IFF_POINTOPOINT) == 0 && 1915 !(nd_ifinfo[ifp->if_index].flags & ND6_IFF_PERFORMNUD)) { 1916 log(LOG_DEBUG, 1917 "nd6_output: can't allocate llinfo for %s " 1918 "(ln=%p, rt=%p)\n", 1919 ip6_sprintf(&dst->sin6_addr), ln, rt); 1920 senderr(EIO); /* XXX: good error? */ 1921 } 1922 1923 goto sendpkt; /* send anyway */ 1924 } 1925 1926 /* We don't have to do link-layer address resolution on a p2p link. */ 1927 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 1928 ln->ln_state < ND6_LLINFO_REACHABLE) { 1929 ln->ln_state = ND6_LLINFO_STALE; 1930 ln->ln_expire = time_second + nd6_gctimer; 1931 } 1932 1933 /* 1934 * The first time we send a packet to a neighbor whose entry is 1935 * STALE, we have to change the state to DELAY and a sets a timer to 1936 * expire in DELAY_FIRST_PROBE_TIME seconds to ensure do 1937 * neighbor unreachability detection on expiration. 1938 * (RFC 2461 7.3.3) 1939 */ 1940 if (ln->ln_state == ND6_LLINFO_STALE) { 1941 ln->ln_asked = 0; 1942 ln->ln_state = ND6_LLINFO_DELAY; 1943 ln->ln_expire = time_second + nd6_delay; 1944 } 1945 1946 /* 1947 * If the neighbor cache entry has a state other than INCOMPLETE 1948 * (i.e. its link-layer address is already reloved), just 1949 * send the packet. 1950 */ 1951 if (ln->ln_state > ND6_LLINFO_INCOMPLETE) 1952 goto sendpkt; 1953 1954 /* 1955 * There is a neighbor cache entry, but no ethernet address 1956 * response yet. Replace the held mbuf (if any) with this 1957 * latest one. 1958 * 1959 * XXX Does the code conform to rate-limiting rule? 1960 * (RFC 2461 7.2.2) 1961 */ 1962 if (ln->ln_state == ND6_LLINFO_NOSTATE) 1963 ln->ln_state = ND6_LLINFO_INCOMPLETE; 1964 if (ln->ln_hold) 1965 m_freem(ln->ln_hold); 1966 ln->ln_hold = m; 1967 if (ln->ln_expire) { 1968 if (ln->ln_asked < nd6_mmaxtries && 1969 ln->ln_expire < time_second) { 1970 ln->ln_asked++; 1971 ln->ln_expire = time_second + 1972 nd_ifinfo[ifp->if_index].retrans / 1000; 1973 nd6_ns_output(ifp, NULL, &dst->sin6_addr, ln, 0); 1974 } 1975 } 1976 return(0); 1977 1978 sendpkt: 1979 1980 if ((ifp->if_flags & IFF_LOOPBACK) != 0) { 1981 return((*ifp->if_output)(origifp, m, (struct sockaddr *)dst, 1982 rt)); 1983 } 1984 return((*ifp->if_output)(ifp, m, (struct sockaddr *)dst, rt)); 1985 1986 bad: 1987 if (m) 1988 m_freem(m); 1989 return (error); 1990} 1991#undef senderr 1992 1993int 1994nd6_need_cache(ifp) 1995 struct ifnet *ifp; 1996{ 1997 /* 1998 * XXX: we currently do not make neighbor cache on any interface 1999 * other than ARCnet, Ethernet, FDDI and GIF. 2000 * 2001 * RFC2893 says: 2002 * - unidirectional tunnels needs no ND 2003 */ 2004 switch (ifp->if_type) { 2005 case IFT_ARCNET: 2006 case IFT_ETHER: 2007 case IFT_FDDI: 2008 case IFT_IEEE1394: 2009#ifdef IFT_L2VLAN 2010 case IFT_L2VLAN: 2011#endif 2012#ifdef IFT_IEEE80211 2013 case IFT_IEEE80211: 2014#endif 2015 case IFT_GIF: /* XXX need more cases? */ 2016 return(1); 2017 default: 2018 return(0); 2019 } 2020} 2021 2022int 2023nd6_storelladdr(ifp, rt, m, dst, desten) 2024 struct ifnet *ifp; 2025 struct rtentry *rt; 2026 struct mbuf *m; 2027 struct sockaddr *dst; 2028 u_char *desten; 2029{ 2030 int i; 2031 struct sockaddr_dl *sdl; 2032 2033 if (m->m_flags & M_MCAST) { 2034 switch (ifp->if_type) { 2035 case IFT_ETHER: 2036 case IFT_FDDI: 2037#ifdef IFT_L2VLAN 2038 case IFT_L2VLAN: 2039#endif 2040#ifdef IFT_IEEE80211 2041 case IFT_IEEE80211: 2042#endif 2043 ETHER_MAP_IPV6_MULTICAST(&SIN6(dst)->sin6_addr, 2044 desten); 2045 return(1); 2046 case IFT_IEEE1394: 2047 for (i = 0; i < ifp->if_addrlen; i++) 2048 desten[i] = ~0; 2049 return(1); 2050 case IFT_ARCNET: 2051 *desten = 0; 2052 return(1); 2053 default: 2054 m_freem(m); 2055 return(0); 2056 } 2057 } 2058 2059 if (rt == NULL) { 2060 /* this could happen, if we could not allocate memory */ 2061 m_freem(m); 2062 return(0); 2063 } 2064 if (rt->rt_gateway->sa_family != AF_LINK) { 2065 printf("nd6_storelladdr: something odd happens\n"); 2066 m_freem(m); 2067 return(0); 2068 } 2069 sdl = SDL(rt->rt_gateway); 2070 if (sdl->sdl_alen == 0) { 2071 /* this should be impossible, but we bark here for debugging */ 2072 printf("nd6_storelladdr: sdl_alen == 0\n"); 2073 m_freem(m); 2074 return(0); 2075 } 2076 2077 bcopy(LLADDR(sdl), desten, sdl->sdl_alen); 2078 return(1); 2079} 2080 2081static int nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS); 2082static int nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS); 2083#ifdef SYSCTL_DECL 2084SYSCTL_DECL(_net_inet6_icmp6); 2085#endif 2086SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_DRLIST, nd6_drlist, 2087 CTLFLAG_RD, nd6_sysctl_drlist, ""); 2088SYSCTL_NODE(_net_inet6_icmp6, ICMPV6CTL_ND6_PRLIST, nd6_prlist, 2089 CTLFLAG_RD, nd6_sysctl_prlist, ""); 2090 2091static int 2092nd6_sysctl_drlist(SYSCTL_HANDLER_ARGS) 2093{ 2094 int error; 2095 char buf[1024]; 2096 struct in6_defrouter *d, *de; 2097 struct nd_defrouter *dr; 2098 2099 if (req->newptr) 2100 return EPERM; 2101 error = 0; 2102 2103 for (dr = TAILQ_FIRST(&nd_defrouter); 2104 dr; 2105 dr = TAILQ_NEXT(dr, dr_entry)) { 2106 d = (struct in6_defrouter *)buf; 2107 de = (struct in6_defrouter *)(buf + sizeof(buf)); 2108 2109 if (d + 1 <= de) { 2110 bzero(d, sizeof(*d)); 2111 d->rtaddr.sin6_family = AF_INET6; 2112 d->rtaddr.sin6_len = sizeof(d->rtaddr); 2113 if (in6_recoverscope(&d->rtaddr, &dr->rtaddr, 2114 dr->ifp) != 0) 2115 log(LOG_ERR, 2116 "scope error in " 2117 "default router list (%s)\n", 2118 ip6_sprintf(&dr->rtaddr)); 2119 d->flags = dr->flags; 2120 d->rtlifetime = dr->rtlifetime; 2121 d->expire = dr->expire; 2122 d->if_index = dr->ifp->if_index; 2123 } else 2124 panic("buffer too short"); 2125 2126 error = SYSCTL_OUT(req, buf, sizeof(*d)); 2127 if (error) 2128 break; 2129 } 2130 return error; 2131} 2132 2133static int 2134nd6_sysctl_prlist(SYSCTL_HANDLER_ARGS) 2135{ 2136 int error; 2137 char buf[1024]; 2138 struct in6_prefix *p, *pe; 2139 struct nd_prefix *pr; 2140 2141 if (req->newptr) 2142 return EPERM; 2143 error = 0; 2144 2145 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 2146 u_short advrtrs; 2147 size_t advance; 2148 struct sockaddr_in6 *sin6, *s6; 2149 struct nd_pfxrouter *pfr; 2150 2151 p = (struct in6_prefix *)buf; 2152 pe = (struct in6_prefix *)(buf + sizeof(buf)); 2153 2154 if (p + 1 <= pe) { 2155 bzero(p, sizeof(*p)); 2156 sin6 = (struct sockaddr_in6 *)(p + 1); 2157 2158 p->prefix = pr->ndpr_prefix; 2159 if (in6_recoverscope(&p->prefix, 2160 &p->prefix.sin6_addr, pr->ndpr_ifp) != 0) 2161 log(LOG_ERR, 2162 "scope error in prefix list (%s)\n", 2163 ip6_sprintf(&p->prefix.sin6_addr)); 2164 p->raflags = pr->ndpr_raf; 2165 p->prefixlen = pr->ndpr_plen; 2166 p->vltime = pr->ndpr_vltime; 2167 p->pltime = pr->ndpr_pltime; 2168 p->if_index = pr->ndpr_ifp->if_index; 2169 p->expire = pr->ndpr_expire; 2170 p->refcnt = pr->ndpr_refcnt; 2171 p->flags = pr->ndpr_stateflags; 2172 p->origin = PR_ORIG_RA; 2173 advrtrs = 0; 2174 for (pfr = pr->ndpr_advrtrs.lh_first; 2175 pfr; 2176 pfr = pfr->pfr_next) { 2177 if ((void *)&sin6[advrtrs + 1] > 2178 (void *)pe) { 2179 advrtrs++; 2180 continue; 2181 } 2182 s6 = &sin6[advrtrs]; 2183 bzero(s6, sizeof(*s6)); 2184 s6->sin6_family = AF_INET6; 2185 s6->sin6_len = sizeof(*sin6); 2186 if (in6_recoverscope(s6, 2187 &pfr->router->rtaddr, 2188 pfr->router->ifp) != 0) 2189 log(LOG_ERR, 2190 "scope error in " 2191 "prefix list (%s)\n", 2192 ip6_sprintf(&pfr->router->rtaddr)); 2193 advrtrs++; 2194 } 2195 p->advrtrs = advrtrs; 2196 } else 2197 panic("buffer too short"); 2198 2199 advance = sizeof(*p) + sizeof(*sin6) * advrtrs; 2200 error = SYSCTL_OUT(req, buf, advance); 2201 if (error) 2202 break; 2203 } 2204 return error; 2205} 2206