nd6_rtr.c revision 121168
1/* $FreeBSD: head/sys/netinet6/nd6_rtr.c 121168 2003-10-17 17:50:09Z ume $ */ 2/* $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei 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#include "opt_inet.h" 34#include "opt_inet6.h" 35 36#include <sys/param.h> 37#include <sys/systm.h> 38#include <sys/malloc.h> 39#include <sys/mbuf.h> 40#include <sys/socket.h> 41#include <sys/sockio.h> 42#include <sys/time.h> 43#include <sys/kernel.h> 44#include <sys/errno.h> 45#include <sys/syslog.h> 46#include <sys/queue.h> 47 48#include <net/if.h> 49#include <net/if_types.h> 50#include <net/if_dl.h> 51#include <net/route.h> 52#include <net/radix.h> 53 54#include <netinet/in.h> 55#include <netinet6/in6_var.h> 56#include <netinet6/in6_ifattach.h> 57#include <netinet/ip6.h> 58#include <netinet6/ip6_var.h> 59#include <netinet6/nd6.h> 60#include <netinet/icmp6.h> 61#include <netinet6/scope6_var.h> 62 63#include <net/net_osdep.h> 64 65#define SDL(s) ((struct sockaddr_dl *)s) 66 67static struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *)); 68static struct in6_ifaddr *in6_ifadd __P((struct nd_prefix *, 69 struct in6_addr *)); 70static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *, 71 struct nd_defrouter *)); 72static void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *)); 73static void pfxrtr_del __P((struct nd_pfxrouter *)); 74static struct nd_pfxrouter *find_pfxlist_reachable_router 75 __P((struct nd_prefix *)); 76static void defrouter_addifreq __P((struct ifnet *)); 77static void nd6_rtmsg __P((int, struct rtentry *)); 78 79static void in6_init_address_ltimes __P((struct nd_prefix *, 80 struct in6_addrlifetime *)); 81 82static int rt6_deleteroute __P((struct radix_node *, void *)); 83 84extern int nd6_recalc_reachtm_interval; 85 86static struct ifnet *nd6_defifp; 87int nd6_defifindex; 88 89int ip6_use_tempaddr = 0; 90 91int ip6_desync_factor; 92u_int32_t ip6_temp_preferred_lifetime = DEF_TEMP_PREFERRED_LIFETIME; 93u_int32_t ip6_temp_valid_lifetime = DEF_TEMP_VALID_LIFETIME; 94/* 95 * shorter lifetimes for debugging purposes. 96int ip6_temp_preferred_lifetime = 800; 97static int ip6_temp_valid_lifetime = 1800; 98*/ 99int ip6_temp_regen_advance = TEMPADDR_REGEN_ADVANCE; 100 101/* 102 * Receive Router Solicitation Message - just for routers. 103 * Router solicitation/advertisement is mostly managed by userland program 104 * (rtadvd) so here we have no function like nd6_ra_output(). 105 * 106 * Based on RFC 2461 107 */ 108void 109nd6_rs_input(m, off, icmp6len) 110 struct mbuf *m; 111 int off, icmp6len; 112{ 113 struct ifnet *ifp = m->m_pkthdr.rcvif; 114 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 115 struct nd_router_solicit *nd_rs; 116 struct in6_addr saddr6 = ip6->ip6_src; 117 char *lladdr = NULL; 118 int lladdrlen = 0; 119#if 0 120 struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL; 121 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL; 122 struct rtentry *rt = NULL; 123 int is_newentry; 124#endif 125 union nd_opts ndopts; 126 127 /* If I'm not a router, ignore it. */ 128 if (ip6_accept_rtadv != 0 || ip6_forwarding != 1) 129 goto freeit; 130 131 /* Sanity checks */ 132 if (ip6->ip6_hlim != 255) { 133 nd6log((LOG_ERR, 134 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 135 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 136 ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); 137 goto bad; 138 } 139 140 /* 141 * Don't update the neighbor cache, if src = ::. 142 * This indicates that the src has no IP address assigned yet. 143 */ 144 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 145 goto freeit; 146 147#ifndef PULLDOWN_TEST 148 IP6_EXTHDR_CHECK(m, off, icmp6len,); 149 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off); 150#else 151 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len); 152 if (nd_rs == NULL) { 153 icmp6stat.icp6s_tooshort++; 154 return; 155 } 156#endif 157 158 icmp6len -= sizeof(*nd_rs); 159 nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 160 if (nd6_options(&ndopts) < 0) { 161 nd6log((LOG_INFO, 162 "nd6_rs_input: invalid ND option, ignored\n")); 163 /* nd6_options have incremented stats */ 164 goto freeit; 165 } 166 167 if (ndopts.nd_opts_src_lladdr) { 168 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 169 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 170 } 171 172 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 173 nd6log((LOG_INFO, 174 "nd6_rs_input: lladdrlen mismatch for %s " 175 "(if %d, RS packet %d)\n", 176 ip6_sprintf(&saddr6), 177 ifp->if_addrlen, lladdrlen - 2)); 178 goto bad; 179 } 180 181 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0); 182 183 freeit: 184 m_freem(m); 185 return; 186 187 bad: 188 icmp6stat.icp6s_badrs++; 189 m_freem(m); 190} 191 192/* 193 * Receive Router Advertisement Message. 194 * 195 * Based on RFC 2461 196 * TODO: on-link bit on prefix information 197 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 198 */ 199void 200nd6_ra_input(m, off, icmp6len) 201 struct mbuf *m; 202 int off, icmp6len; 203{ 204 struct ifnet *ifp = m->m_pkthdr.rcvif; 205 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 206 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 207 struct nd_router_advert *nd_ra; 208 struct in6_addr saddr6 = ip6->ip6_src; 209#if 0 210 struct in6_addr daddr6 = ip6->ip6_dst; 211 int flags; /* = nd_ra->nd_ra_flags_reserved; */ 212 int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0); 213 int is_other = ((flags & ND_RA_FLAG_OTHER) != 0); 214#endif 215 union nd_opts ndopts; 216 struct nd_defrouter *dr; 217 218 /* 219 * We only accept RAs only when 220 * the system-wide variable allows the acceptance, and 221 * per-interface variable allows RAs on the receiving interface. 222 */ 223 if (ip6_accept_rtadv == 0) 224 goto freeit; 225 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV)) 226 goto freeit; 227 228 if (ip6->ip6_hlim != 255) { 229 nd6log((LOG_ERR, 230 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 231 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 232 ip6_sprintf(&ip6->ip6_dst), if_name(ifp))); 233 goto bad; 234 } 235 236 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 237 nd6log((LOG_ERR, 238 "nd6_ra_input: src %s is not link-local\n", 239 ip6_sprintf(&saddr6))); 240 goto bad; 241 } 242 243#ifndef PULLDOWN_TEST 244 IP6_EXTHDR_CHECK(m, off, icmp6len,); 245 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off); 246#else 247 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len); 248 if (nd_ra == NULL) { 249 icmp6stat.icp6s_tooshort++; 250 return; 251 } 252#endif 253 254 icmp6len -= sizeof(*nd_ra); 255 nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 256 if (nd6_options(&ndopts) < 0) { 257 nd6log((LOG_INFO, 258 "nd6_ra_input: invalid ND option, ignored\n")); 259 /* nd6_options have incremented stats */ 260 goto freeit; 261 } 262 263 { 264 struct nd_defrouter dr0; 265 u_int32_t advreachable = nd_ra->nd_ra_reachable; 266 267 dr0.rtaddr = saddr6; 268 dr0.flags = nd_ra->nd_ra_flags_reserved; 269 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime); 270 dr0.expire = time_second + dr0.rtlifetime; 271 dr0.ifp = ifp; 272 dr0.advint = 0; /* Mobile IPv6 */ 273 dr0.advint_expire = 0; /* Mobile IPv6 */ 274 dr0.advints_lost = 0; /* Mobile IPv6 */ 275 /* unspecified or not? (RFC 2461 6.3.4) */ 276 if (advreachable) { 277 advreachable = ntohl(advreachable); 278 if (advreachable <= MAX_REACHABLE_TIME && 279 ndi->basereachable != advreachable) { 280 ndi->basereachable = advreachable; 281 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable); 282 ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */ 283 } 284 } 285 if (nd_ra->nd_ra_retransmit) 286 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit); 287 if (nd_ra->nd_ra_curhoplimit) 288 ndi->chlim = nd_ra->nd_ra_curhoplimit; 289 dr = defrtrlist_update(&dr0); 290 } 291 292 /* 293 * prefix 294 */ 295 if (ndopts.nd_opts_pi) { 296 struct nd_opt_hdr *pt; 297 struct nd_opt_prefix_info *pi = NULL; 298 struct nd_prefix pr; 299 300 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi; 301 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end; 302 pt = (struct nd_opt_hdr *)((caddr_t)pt + 303 (pt->nd_opt_len << 3))) { 304 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) 305 continue; 306 pi = (struct nd_opt_prefix_info *)pt; 307 308 if (pi->nd_opt_pi_len != 4) { 309 nd6log((LOG_INFO, 310 "nd6_ra_input: invalid option " 311 "len %d for prefix information option, " 312 "ignored\n", pi->nd_opt_pi_len)); 313 continue; 314 } 315 316 if (128 < pi->nd_opt_pi_prefix_len) { 317 nd6log((LOG_INFO, 318 "nd6_ra_input: invalid prefix " 319 "len %d for prefix information option, " 320 "ignored\n", pi->nd_opt_pi_prefix_len)); 321 continue; 322 } 323 324 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) 325 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) { 326 nd6log((LOG_INFO, 327 "nd6_ra_input: invalid prefix " 328 "%s, ignored\n", 329 ip6_sprintf(&pi->nd_opt_pi_prefix))); 330 continue; 331 } 332 333 /* aggregatable unicast address, rfc2374 */ 334 if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20 335 && pi->nd_opt_pi_prefix_len != 64) { 336 nd6log((LOG_INFO, 337 "nd6_ra_input: invalid prefixlen " 338 "%d for rfc2374 prefix %s, ignored\n", 339 pi->nd_opt_pi_prefix_len, 340 ip6_sprintf(&pi->nd_opt_pi_prefix))); 341 continue; 342 } 343 344 bzero(&pr, sizeof(pr)); 345 pr.ndpr_prefix.sin6_family = AF_INET6; 346 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix); 347 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix; 348 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif; 349 350 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved & 351 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0; 352 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved & 353 ND_OPT_PI_FLAG_AUTO) ? 1 : 0; 354 pr.ndpr_plen = pi->nd_opt_pi_prefix_len; 355 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time); 356 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time); 357 if (in6_init_prefix_ltimes(&pr)) 358 continue; /* prefix lifetime init failed */ 359 (void)prelist_update(&pr, dr, m); 360 } 361 } 362 363 /* 364 * MTU 365 */ 366 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) { 367 u_int32_t mtu; 368 369 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu); 370 371 /* lower bound */ 372 if (mtu < IPV6_MMTU) { 373 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option " 374 "mtu=%d sent from %s, ignoring\n", 375 mtu, ip6_sprintf(&ip6->ip6_src))); 376 goto skip; 377 } 378 379 /* upper bound */ 380 if (ndi->maxmtu) { 381 if (mtu <= ndi->maxmtu) { 382 int change = (ndi->linkmtu != mtu); 383 384 ndi->linkmtu = mtu; 385 if (change) /* in6_maxmtu may change */ 386 in6_setmaxmtu(); 387 } else { 388 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu " 389 "mtu=%d sent from %s; " 390 "exceeds maxmtu %d, ignoring\n", 391 mtu, ip6_sprintf(&ip6->ip6_src), 392 ndi->maxmtu)); 393 } 394 } else { 395 nd6log((LOG_INFO, "nd6_ra_input: mtu option " 396 "mtu=%d sent from %s; maxmtu unknown, " 397 "ignoring\n", 398 mtu, ip6_sprintf(&ip6->ip6_src))); 399 } 400 } 401 402 skip: 403 404 /* 405 * Source link layer address 406 */ 407 { 408 char *lladdr = NULL; 409 int lladdrlen = 0; 410 411 if (ndopts.nd_opts_src_lladdr) { 412 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 413 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 414 } 415 416 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 417 nd6log((LOG_INFO, 418 "nd6_ra_input: lladdrlen mismatch for %s " 419 "(if %d, RA packet %d)\n", ip6_sprintf(&saddr6), 420 ifp->if_addrlen, lladdrlen - 2)); 421 goto bad; 422 } 423 424 nd6_cache_lladdr(ifp, &saddr6, lladdr, 425 lladdrlen, ND_ROUTER_ADVERT, 0); 426 427 /* 428 * Installing a link-layer address might change the state of the 429 * router's neighbor cache, which might also affect our on-link 430 * detection of adveritsed prefixes. 431 */ 432 pfxlist_onlink_check(); 433 } 434 435 freeit: 436 m_freem(m); 437 return; 438 439 bad: 440 icmp6stat.icp6s_badra++; 441 m_freem(m); 442} 443 444/* 445 * default router list proccessing sub routines 446 */ 447 448/* tell the change to user processes watching the routing socket. */ 449static void 450nd6_rtmsg(cmd, rt) 451 int cmd; 452 struct rtentry *rt; 453{ 454 struct rt_addrinfo info; 455 456 bzero((caddr_t)&info, sizeof(info)); 457 info.rti_info[RTAX_DST] = rt_key(rt); 458 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 459 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 460 info.rti_info[RTAX_IFP] = 461 (struct sockaddr *)TAILQ_FIRST(&rt->rt_ifp->if_addrlist); 462 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 463 464 rt_missmsg(cmd, &info, rt->rt_flags, 0); 465} 466 467void 468defrouter_addreq(new) 469 struct nd_defrouter *new; 470{ 471 struct sockaddr_in6 def, mask, gate; 472 struct rtentry *newrt = NULL; 473 474 Bzero(&def, sizeof(def)); 475 Bzero(&mask, sizeof(mask)); 476 Bzero(&gate, sizeof(gate)); 477 478 def.sin6_len = mask.sin6_len = gate.sin6_len = 479 sizeof(struct sockaddr_in6); 480 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 481 gate.sin6_addr = new->rtaddr; 482 483 (void)rtrequest(RTM_ADD, (struct sockaddr *)&def, 484 (struct sockaddr *)&gate, (struct sockaddr *)&mask, 485 RTF_GATEWAY, &newrt); 486 if (newrt) { 487 RT_LOCK(newrt); 488 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */ 489 newrt->rt_refcnt--; 490 RT_UNLOCK(newrt); 491 } 492 return; 493} 494 495/* Add a route to a given interface as default */ 496void 497defrouter_addifreq(ifp) 498 struct ifnet *ifp; 499{ 500 struct sockaddr_in6 def, mask; 501 struct ifaddr *ifa; 502 struct rtentry *newrt = NULL; 503 int error, flags; 504 505 bzero(&def, sizeof(def)); 506 bzero(&mask, sizeof(mask)); 507 508 def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6); 509 def.sin6_family = mask.sin6_family = AF_INET6; 510 511 /* 512 * Search for an ifaddr beloging to the specified interface. 513 * XXX: An IPv6 address are required to be assigned on the interface. 514 */ 515 if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) { 516 nd6log((LOG_ERR, /* better error? */ 517 "defrouter_addifreq: failed to find an ifaddr " 518 "to install a route to interface %s\n", 519 if_name(ifp))); 520 return; 521 } 522 523 flags = ifa->ifa_flags; 524 error = rtrequest(RTM_ADD, (struct sockaddr *)&def, ifa->ifa_addr, 525 (struct sockaddr *)&mask, flags, &newrt); 526 if (error != 0) { 527 nd6log((LOG_ERR, 528 "defrouter_addifreq: failed to install a route to " 529 "interface %s (errno = %d)\n", 530 if_name(ifp), error)); 531 } else { 532 if (newrt) { 533 RT_LOCK(newrt); 534 nd6_rtmsg(RTM_ADD, newrt); 535 newrt->rt_refcnt--; 536 RT_UNLOCK(newrt); 537 } 538 } 539} 540 541struct nd_defrouter * 542defrouter_lookup(addr, ifp) 543 struct in6_addr *addr; 544 struct ifnet *ifp; 545{ 546 struct nd_defrouter *dr; 547 548 for (dr = TAILQ_FIRST(&nd_defrouter); dr; 549 dr = TAILQ_NEXT(dr, dr_entry)) { 550 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) 551 return (dr); 552 } 553 554 return (NULL); /* search failed */ 555} 556 557void 558defrouter_delreq(dr, dofree) 559 struct nd_defrouter *dr; 560 int dofree; 561{ 562 struct sockaddr_in6 def, mask, gate; 563 struct rtentry *oldrt = NULL; 564 565 Bzero(&def, sizeof(def)); 566 Bzero(&mask, sizeof(mask)); 567 Bzero(&gate, sizeof(gate)); 568 569 def.sin6_len = mask.sin6_len = gate.sin6_len = 570 sizeof(struct sockaddr_in6); 571 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 572 gate.sin6_addr = dr->rtaddr; 573 574 rtrequest(RTM_DELETE, (struct sockaddr *)&def, 575 (struct sockaddr *)&gate, 576 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt); 577 if (oldrt) { 578 nd6_rtmsg(RTM_DELETE, oldrt); 579 RTFREE(oldrt); 580 } 581 582 if (dofree) /* XXX: necessary? */ 583 free(dr, M_IP6NDP); 584} 585 586void 587defrtrlist_del(dr) 588 struct nd_defrouter *dr; 589{ 590 struct nd_defrouter *deldr = NULL; 591 struct nd_prefix *pr; 592 593 /* 594 * Flush all the routing table entries that use the router 595 * as a next hop. 596 */ 597 if (!ip6_forwarding && ip6_accept_rtadv) /* XXX: better condition? */ 598 rt6_flush(&dr->rtaddr, dr->ifp); 599 600 if (dr == TAILQ_FIRST(&nd_defrouter)) 601 deldr = dr; /* The router is primary. */ 602 603 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 604 605 /* 606 * Also delete all the pointers to the router in each prefix lists. 607 */ 608 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 609 struct nd_pfxrouter *pfxrtr; 610 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) 611 pfxrtr_del(pfxrtr); 612 } 613 pfxlist_onlink_check(); 614 615 /* 616 * If the router is the primary one, choose a new one. 617 * Note that defrouter_select() will remove the current gateway 618 * from the routing table. 619 */ 620 if (deldr) 621 defrouter_select(); 622 623 free(dr, M_IP6NDP); 624} 625 626/* 627 * Default Router Selection according to Section 6.3.6 of RFC 2461: 628 * 1) Routers that are reachable or probably reachable should be 629 * preferred. 630 * 2) When no routers on the list are known to be reachable or 631 * probably reachable, routers SHOULD be selected in a round-robin 632 * fashion. 633 * 3) If the Default Router List is empty, assume that all 634 * destinations are on-link. 635 */ 636void 637defrouter_select() 638{ 639 int s = splnet(); 640 struct nd_defrouter *dr, anydr; 641 struct rtentry *rt = NULL; 642 struct llinfo_nd6 *ln = NULL; 643 644 /* 645 * Search for a (probably) reachable router from the list. 646 */ 647 for (dr = TAILQ_FIRST(&nd_defrouter); dr; 648 dr = TAILQ_NEXT(dr, dr_entry)) { 649 if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) && 650 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 651 ND6_IS_LLINFO_PROBREACH(ln)) { 652 /* Got it, and move it to the head */ 653 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 654 TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry); 655 break; 656 } 657 } 658 659 if ((dr = TAILQ_FIRST(&nd_defrouter))) { 660 /* 661 * De-install the previous default gateway and install 662 * a new one. 663 * Note that if there is no reachable router in the list, 664 * the head entry will be used anyway. 665 * XXX: do we have to check the current routing table entry? 666 */ 667 bzero(&anydr, sizeof(anydr)); 668 defrouter_delreq(&anydr, 0); 669 defrouter_addreq(dr); 670 } 671 else { 672 /* 673 * The Default Router List is empty, so install the default 674 * route to an inteface. 675 * XXX: The specification does not say this mechanism should 676 * be restricted to hosts, but this would be not useful 677 * (even harmful) for routers. 678 */ 679 if (!ip6_forwarding) { 680 /* 681 * De-install the current default route 682 * in advance. 683 */ 684 bzero(&anydr, sizeof(anydr)); 685 defrouter_delreq(&anydr, 0); 686 if (nd6_defifp) { 687 /* 688 * Install a route to the default interface 689 * as default route. 690 * XXX: we enable this for host only, because 691 * this may override a default route installed 692 * a user process (e.g. routing daemon) in a 693 * router case. 694 */ 695 defrouter_addifreq(nd6_defifp); 696 } else { 697 nd6log((LOG_INFO, "defrouter_select: " 698 "there's no default router and no default" 699 " interface\n")); 700 } 701 } 702 } 703 704 splx(s); 705 return; 706} 707 708static struct nd_defrouter * 709defrtrlist_update(new) 710 struct nd_defrouter *new; 711{ 712 struct nd_defrouter *dr, *n; 713 int s = splnet(); 714 715 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) { 716 /* entry exists */ 717 if (new->rtlifetime == 0) { 718 defrtrlist_del(dr); 719 dr = NULL; 720 } else { 721 /* override */ 722 dr->flags = new->flags; /* xxx flag check */ 723 dr->rtlifetime = new->rtlifetime; 724 dr->expire = new->expire; 725 } 726 splx(s); 727 return (dr); 728 } 729 730 /* entry does not exist */ 731 if (new->rtlifetime == 0) { 732 splx(s); 733 return (NULL); 734 } 735 736 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT); 737 if (n == NULL) { 738 splx(s); 739 return (NULL); 740 } 741 bzero(n, sizeof(*n)); 742 *n = *new; 743 744 /* 745 * Insert the new router at the end of the Default Router List. 746 * If there is no other router, install it anyway. Otherwise, 747 * just continue to use the current default router. 748 */ 749 TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry); 750 if (TAILQ_FIRST(&nd_defrouter) == n) 751 defrouter_select(); 752 splx(s); 753 754 return (n); 755} 756 757static struct nd_pfxrouter * 758pfxrtr_lookup(pr, dr) 759 struct nd_prefix *pr; 760 struct nd_defrouter *dr; 761{ 762 struct nd_pfxrouter *search; 763 764 for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) { 765 if (search->router == dr) 766 break; 767 } 768 769 return (search); 770} 771 772static void 773pfxrtr_add(pr, dr) 774 struct nd_prefix *pr; 775 struct nd_defrouter *dr; 776{ 777 struct nd_pfxrouter *new; 778 779 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 780 if (new == NULL) 781 return; 782 bzero(new, sizeof(*new)); 783 new->router = dr; 784 785 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry); 786 787 pfxlist_onlink_check(); 788} 789 790static void 791pfxrtr_del(pfr) 792 struct nd_pfxrouter *pfr; 793{ 794 LIST_REMOVE(pfr, pfr_entry); 795 free(pfr, M_IP6NDP); 796} 797 798struct nd_prefix * 799nd6_prefix_lookup(pr) 800 struct nd_prefix *pr; 801{ 802 struct nd_prefix *search; 803 804 for (search = nd_prefix.lh_first; search; search = search->ndpr_next) { 805 if (pr->ndpr_ifp == search->ndpr_ifp && 806 pr->ndpr_plen == search->ndpr_plen && 807 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 808 &search->ndpr_prefix.sin6_addr, pr->ndpr_plen)) { 809 break; 810 } 811 } 812 813 return (search); 814} 815 816int 817nd6_prelist_add(pr, dr, newp) 818 struct nd_prefix *pr, **newp; 819 struct nd_defrouter *dr; 820{ 821 struct nd_prefix *new = NULL; 822 int i, s; 823 824 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 825 if (new == NULL) 826 return(ENOMEM); 827 bzero(new, sizeof(*new)); 828 *new = *pr; 829 if (newp != NULL) 830 *newp = new; 831 832 /* initialization */ 833 LIST_INIT(&new->ndpr_advrtrs); 834 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen); 835 /* make prefix in the canonical form */ 836 for (i = 0; i < 4; i++) 837 new->ndpr_prefix.sin6_addr.s6_addr32[i] &= 838 new->ndpr_mask.s6_addr32[i]; 839 840 s = splnet(); 841 /* link ndpr_entry to nd_prefix list */ 842 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry); 843 splx(s); 844 845 /* ND_OPT_PI_FLAG_ONLINK processing */ 846 if (new->ndpr_raf_onlink) { 847 int e; 848 849 if ((e = nd6_prefix_onlink(new)) != 0) { 850 nd6log((LOG_ERR, "nd6_prelist_add: failed to make " 851 "the prefix %s/%d on-link on %s (errno=%d)\n", 852 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 853 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 854 /* proceed anyway. XXX: is it correct? */ 855 } 856 } 857 858 if (dr) 859 pfxrtr_add(new, dr); 860 861 return 0; 862} 863 864void 865prelist_remove(pr) 866 struct nd_prefix *pr; 867{ 868 struct nd_pfxrouter *pfr, *next; 869 int e, s; 870 871 /* make sure to invalidate the prefix until it is really freed. */ 872 pr->ndpr_vltime = 0; 873 pr->ndpr_pltime = 0; 874#if 0 875 /* 876 * Though these flags are now meaningless, we'd rather keep the value 877 * not to confuse users when executing "ndp -p". 878 */ 879 pr->ndpr_raf_onlink = 0; 880 pr->ndpr_raf_auto = 0; 881#endif 882 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 && 883 (e = nd6_prefix_offlink(pr)) != 0) { 884 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink " 885 "on %s, errno=%d\n", 886 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 887 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 888 /* what should we do? */ 889 } 890 891 if (pr->ndpr_refcnt > 0) 892 return; /* notice here? */ 893 894 s = splnet(); 895 896 /* unlink ndpr_entry from nd_prefix list */ 897 LIST_REMOVE(pr, ndpr_entry); 898 899 /* free list of routers that adversed the prefix */ 900 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) { 901 next = pfr->pfr_next; 902 903 free(pfr, M_IP6NDP); 904 } 905 splx(s); 906 907 free(pr, M_IP6NDP); 908 909 pfxlist_onlink_check(); 910} 911 912int 913prelist_update(new, dr, m) 914 struct nd_prefix *new; 915 struct nd_defrouter *dr; /* may be NULL */ 916 struct mbuf *m; 917{ 918 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL; 919 struct ifaddr *ifa; 920 struct ifnet *ifp = new->ndpr_ifp; 921 struct nd_prefix *pr; 922 int s = splnet(); 923 int error = 0; 924 int newprefix = 0; 925 int auth; 926 struct in6_addrlifetime lt6_tmp; 927 928 auth = 0; 929 if (m) { 930 /* 931 * Authenticity for NA consists authentication for 932 * both IP header and IP datagrams, doesn't it ? 933 */ 934#if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM) 935 auth = ((m->m_flags & M_AUTHIPHDR) && 936 (m->m_flags & M_AUTHIPDGM)); 937#endif 938 } 939 940 if ((pr = nd6_prefix_lookup(new)) != NULL) { 941 /* 942 * nd6_prefix_lookup() ensures that pr and new have the same 943 * prefix on a same interface. 944 */ 945 946 /* 947 * Update prefix information. Note that the on-link (L) bit 948 * and the autonomous (A) bit should NOT be changed from 1 949 * to 0. 950 */ 951 if (new->ndpr_raf_onlink == 1) 952 pr->ndpr_raf_onlink = 1; 953 if (new->ndpr_raf_auto == 1) 954 pr->ndpr_raf_auto = 1; 955 if (new->ndpr_raf_onlink) { 956 pr->ndpr_vltime = new->ndpr_vltime; 957 pr->ndpr_pltime = new->ndpr_pltime; 958 pr->ndpr_preferred = new->ndpr_preferred; 959 pr->ndpr_expire = new->ndpr_expire; 960 } 961 962 if (new->ndpr_raf_onlink && 963 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 964 int e; 965 966 if ((e = nd6_prefix_onlink(pr)) != 0) { 967 nd6log((LOG_ERR, 968 "prelist_update: failed to make " 969 "the prefix %s/%d on-link on %s " 970 "(errno=%d)\n", 971 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 972 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 973 /* proceed anyway. XXX: is it correct? */ 974 } 975 } 976 977 if (dr && pfxrtr_lookup(pr, dr) == NULL) 978 pfxrtr_add(pr, dr); 979 } else { 980 struct nd_prefix *newpr = NULL; 981 982 newprefix = 1; 983 984 if (new->ndpr_vltime == 0) 985 goto end; 986 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0) 987 goto end; 988 989 bzero(&new->ndpr_addr, sizeof(struct in6_addr)); 990 991 error = nd6_prelist_add(new, dr, &newpr); 992 if (error != 0 || newpr == NULL) { 993 nd6log((LOG_NOTICE, "prelist_update: " 994 "nd6_prelist_add failed for %s/%d on %s " 995 "errno=%d, returnpr=%p\n", 996 ip6_sprintf(&new->ndpr_prefix.sin6_addr), 997 new->ndpr_plen, if_name(new->ndpr_ifp), 998 error, newpr)); 999 goto end; /* we should just give up in this case. */ 1000 } 1001 1002 /* 1003 * XXX: from the ND point of view, we can ignore a prefix 1004 * with the on-link bit being zero. However, we need a 1005 * prefix structure for references from autoconfigured 1006 * addresses. Thus, we explicitly make sure that the prefix 1007 * itself expires now. 1008 */ 1009 if (newpr->ndpr_raf_onlink == 0) { 1010 newpr->ndpr_vltime = 0; 1011 newpr->ndpr_pltime = 0; 1012 in6_init_prefix_ltimes(newpr); 1013 } 1014 1015 pr = newpr; 1016 } 1017 1018 /* 1019 * Address autoconfiguration based on Section 5.5.3 of RFC 2462. 1020 * Note that pr must be non NULL at this point. 1021 */ 1022 1023 /* 5.5.3 (a). Ignore the prefix without the A bit set. */ 1024 if (!new->ndpr_raf_auto) 1025 goto afteraddrconf; 1026 1027 /* 1028 * 5.5.3 (b). the link-local prefix should have been ignored in 1029 * nd6_ra_input. 1030 */ 1031 1032 /* 1033 * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. 1034 * This should have been done in nd6_ra_input. 1035 */ 1036 1037 /* 1038 * 5.5.3 (d). If the prefix advertised does not match the prefix of an 1039 * address already in the list, and the Valid Lifetime is not 0, 1040 * form an address. Note that even a manually configured address 1041 * should reject autoconfiguration of a new address. 1042 */ 1043 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1044 struct in6_ifaddr *ifa6; 1045 int ifa_plen; 1046 u_int32_t storedlifetime; 1047 1048 if (ifa->ifa_addr->sa_family != AF_INET6) 1049 continue; 1050 1051 ifa6 = (struct in6_ifaddr *)ifa; 1052 1053 /* 1054 * Spec is not clear here, but I believe we should concentrate 1055 * on unicast (i.e. not anycast) addresses. 1056 * XXX: other ia6_flags? detached or duplicated? 1057 */ 1058 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0) 1059 continue; 1060 1061 ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL); 1062 if (ifa_plen != new->ndpr_plen || 1063 !in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr, 1064 &new->ndpr_prefix.sin6_addr, ifa_plen)) 1065 continue; 1066 1067 if (ia6_match == NULL) /* remember the first one */ 1068 ia6_match = ifa6; 1069 1070 if ((ifa6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1071 continue; 1072 1073 /* 1074 * An already autoconfigured address matched. Now that we 1075 * are sure there is at least one matched address, we can 1076 * proceed to 5.5.3. (e): update the lifetimes according to the 1077 * "two hours" rule and the privacy extension. 1078 */ 1079#define TWOHOUR (120*60) 1080 lt6_tmp = ifa6->ia6_lifetime; 1081 1082 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME) 1083 storedlifetime = ND6_INFINITE_LIFETIME; 1084 else if (IFA6_IS_INVALID(ifa6)) 1085 storedlifetime = 0; 1086 else 1087 storedlifetime = lt6_tmp.ia6t_expire - time_second; 1088 1089 /* when not updating, keep the current stored lifetime. */ 1090 lt6_tmp.ia6t_vltime = storedlifetime; 1091 1092 if (TWOHOUR < new->ndpr_vltime || 1093 storedlifetime < new->ndpr_vltime) { 1094 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1095 } else if (storedlifetime <= TWOHOUR 1096#if 0 1097 /* 1098 * This condition is logically redundant, so we just 1099 * omit it. 1100 * See IPng 6712, 6717, and 6721. 1101 */ 1102 && new->ndpr_vltime <= storedlifetime 1103#endif 1104 ) { 1105 if (auth) { 1106 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1107 } 1108 } else { 1109 /* 1110 * new->ndpr_vltime <= TWOHOUR && 1111 * TWOHOUR < storedlifetime 1112 */ 1113 lt6_tmp.ia6t_vltime = TWOHOUR; 1114 } 1115 1116 /* The 2 hour rule is not imposed for preferred lifetime. */ 1117 lt6_tmp.ia6t_pltime = new->ndpr_pltime; 1118 1119 in6_init_address_ltimes(pr, <6_tmp); 1120 1121 /* 1122 * When adjusting the lifetimes of an existing temporary 1123 * address, only lower the lifetimes. 1124 * RFC 3041 3.3. (1). 1125 * XXX: how should we modify ia6t_[pv]ltime? 1126 */ 1127 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 1128 if (lt6_tmp.ia6t_expire == 0 || /* no expire */ 1129 lt6_tmp.ia6t_expire > 1130 ifa6->ia6_lifetime.ia6t_expire) { 1131 lt6_tmp.ia6t_expire = 1132 ifa6->ia6_lifetime.ia6t_expire; 1133 } 1134 if (lt6_tmp.ia6t_preferred == 0 || /* no expire */ 1135 lt6_tmp.ia6t_preferred > 1136 ifa6->ia6_lifetime.ia6t_preferred) { 1137 lt6_tmp.ia6t_preferred = 1138 ifa6->ia6_lifetime.ia6t_preferred; 1139 } 1140 } 1141 1142 ifa6->ia6_lifetime = lt6_tmp; 1143 } 1144 if (ia6_match == NULL && new->ndpr_vltime) { 1145 /* 1146 * No address matched and the valid lifetime is non-zero. 1147 * Create a new address. 1148 */ 1149 if ((ia6 = in6_ifadd(new, NULL)) != NULL) { 1150 /* 1151 * note that we should use pr (not new) for reference. 1152 */ 1153 pr->ndpr_refcnt++; 1154 ia6->ia6_ndpr = pr; 1155 1156 /* 1157 * RFC 3041 3.3 (2). 1158 * When a new public address is created as described 1159 * in RFC2462, also create a new temporary address. 1160 * 1161 * RFC 3041 3.5. 1162 * When an interface connects to a new link, a new 1163 * randomized interface identifier should be generated 1164 * immediately together with a new set of temporary 1165 * addresses. Thus, we specifiy 1 as the 2nd arg of 1166 * in6_tmpifadd(). 1167 */ 1168 if (ip6_use_tempaddr) { 1169 int e; 1170 if ((e = in6_tmpifadd(ia6, 1)) != 0) { 1171 nd6log((LOG_NOTICE, "prelist_update: " 1172 "failed to create a temporary " 1173 "address, errno=%d\n", 1174 e)); 1175 } 1176 } 1177 1178 /* 1179 * A newly added address might affect the status 1180 * of other addresses, so we check and update it. 1181 * XXX: what if address duplication happens? 1182 */ 1183 pfxlist_onlink_check(); 1184 } else { 1185 /* just set an error. do not bark here. */ 1186 error = EADDRNOTAVAIL; /* XXX: might be unused. */ 1187 } 1188 } 1189 1190 afteraddrconf: 1191 1192 end: 1193 splx(s); 1194 return error; 1195} 1196 1197/* 1198 * A supplement function used in the on-link detection below; 1199 * detect if a given prefix has a (probably) reachable advertising router. 1200 * XXX: lengthy function name... 1201 */ 1202static struct nd_pfxrouter * 1203find_pfxlist_reachable_router(pr) 1204 struct nd_prefix *pr; 1205{ 1206 struct nd_pfxrouter *pfxrtr; 1207 struct rtentry *rt; 1208 struct llinfo_nd6 *ln; 1209 1210 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr; 1211 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) { 1212 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0, 1213 pfxrtr->router->ifp)) && 1214 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 1215 ND6_IS_LLINFO_PROBREACH(ln)) 1216 break; /* found */ 1217 } 1218 1219 return (pfxrtr); 1220} 1221 1222/* 1223 * Check if each prefix in the prefix list has at least one available router 1224 * that advertised the prefix (a router is "available" if its neighbor cache 1225 * entry is reachable or probably reachable). 1226 * If the check fails, the prefix may be off-link, because, for example, 1227 * we have moved from the network but the lifetime of the prefix has not 1228 * expired yet. So we should not use the prefix if there is another prefix 1229 * that has an available router. 1230 * But, if there is no prefix that has an available router, we still regards 1231 * all the prefixes as on-link. This is because we can't tell if all the 1232 * routers are simply dead or if we really moved from the network and there 1233 * is no router around us. 1234 */ 1235void 1236pfxlist_onlink_check() 1237{ 1238 struct nd_prefix *pr; 1239 struct in6_ifaddr *ifa; 1240 1241 /* 1242 * Check if there is a prefix that has a reachable advertising 1243 * router. 1244 */ 1245 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1246 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr)) 1247 break; 1248 } 1249 1250 if (pr) { 1251 /* 1252 * There is at least one prefix that has a reachable router. 1253 * Detach prefixes which have no reachable advertising 1254 * router, and attach other prefixes. 1255 */ 1256 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1257 /* XXX: a link-local prefix should never be detached */ 1258 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1259 continue; 1260 1261 /* 1262 * we aren't interested in prefixes without the L bit 1263 * set. 1264 */ 1265 if (pr->ndpr_raf_onlink == 0) 1266 continue; 1267 1268 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1269 find_pfxlist_reachable_router(pr) == NULL) 1270 pr->ndpr_stateflags |= NDPRF_DETACHED; 1271 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1272 find_pfxlist_reachable_router(pr) != 0) 1273 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1274 } 1275 } else { 1276 /* there is no prefix that has a reachable router */ 1277 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1278 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1279 continue; 1280 1281 if (pr->ndpr_raf_onlink == 0) 1282 continue; 1283 1284 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1285 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1286 } 1287 } 1288 1289 /* 1290 * Remove each interface route associated with a (just) detached 1291 * prefix, and reinstall the interface route for a (just) attached 1292 * prefix. Note that all attempt of reinstallation does not 1293 * necessarily success, when a same prefix is shared among multiple 1294 * interfaces. Such cases will be handled in nd6_prefix_onlink, 1295 * so we don't have to care about them. 1296 */ 1297 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1298 int e; 1299 1300 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1301 continue; 1302 1303 if (pr->ndpr_raf_onlink == 0) 1304 continue; 1305 1306 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1307 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1308 if ((e = nd6_prefix_offlink(pr)) != 0) { 1309 nd6log((LOG_ERR, 1310 "pfxlist_onlink_check: failed to " 1311 "make %s/%d offlink, errno=%d\n", 1312 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1313 pr->ndpr_plen, e)); 1314 } 1315 } 1316 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1317 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 && 1318 pr->ndpr_raf_onlink) { 1319 if ((e = nd6_prefix_onlink(pr)) != 0) { 1320 nd6log((LOG_ERR, 1321 "pfxlist_onlink_check: failed to " 1322 "make %s/%d offlink, errno=%d\n", 1323 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1324 pr->ndpr_plen, e)); 1325 } 1326 } 1327 } 1328 1329 /* 1330 * Changes on the prefix status might affect address status as well. 1331 * Make sure that all addresses derived from an attached prefix are 1332 * attached, and that all addresses derived from a detached prefix are 1333 * detached. Note, however, that a manually configured address should 1334 * always be attached. 1335 * The precise detection logic is same as the one for prefixes. 1336 */ 1337 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 1338 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF)) 1339 continue; 1340 1341 if (ifa->ia6_ndpr == NULL) { 1342 /* 1343 * This can happen when we first configure the address 1344 * (i.e. the address exists, but the prefix does not). 1345 * XXX: complicated relationships... 1346 */ 1347 continue; 1348 } 1349 1350 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 1351 break; 1352 } 1353 if (ifa) { 1354 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 1355 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1356 continue; 1357 1358 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */ 1359 continue; 1360 1361 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 1362 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1363 else 1364 ifa->ia6_flags |= IN6_IFF_DETACHED; 1365 } 1366 } 1367 else { 1368 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 1369 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1370 continue; 1371 1372 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1373 } 1374 } 1375} 1376 1377int 1378nd6_prefix_onlink(pr) 1379 struct nd_prefix *pr; 1380{ 1381 struct ifaddr *ifa; 1382 struct ifnet *ifp = pr->ndpr_ifp; 1383 struct sockaddr_in6 mask6; 1384 struct nd_prefix *opr; 1385 u_long rtflags; 1386 int error = 0; 1387 struct rtentry *rt = NULL; 1388 1389 /* sanity check */ 1390 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1391 nd6log((LOG_ERR, 1392 "nd6_prefix_onlink: %s/%d is already on-link\n", 1393 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen); 1394 return (EEXIST)); 1395 } 1396 1397 /* 1398 * Add the interface route associated with the prefix. Before 1399 * installing the route, check if there's the same prefix on another 1400 * interface, and the prefix has already installed the interface route. 1401 * Although such a configuration is expected to be rare, we explicitly 1402 * allow it. 1403 */ 1404 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) { 1405 if (opr == pr) 1406 continue; 1407 1408 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0) 1409 continue; 1410 1411 if (opr->ndpr_plen == pr->ndpr_plen && 1412 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1413 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) 1414 return (0); 1415 } 1416 1417 /* 1418 * We prefer link-local addresses as the associated interface address. 1419 */ 1420 /* search for a link-local addr */ 1421 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 1422 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST); 1423 if (ifa == NULL) { 1424 /* XXX: freebsd does not have ifa_ifwithaf */ 1425 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1426 if (ifa->ifa_addr->sa_family == AF_INET6) 1427 break; 1428 } 1429 /* should we care about ia6_flags? */ 1430 } 1431 if (ifa == NULL) { 1432 /* 1433 * This can still happen, when, for example, we receive an RA 1434 * containing a prefix with the L bit set and the A bit clear, 1435 * after removing all IPv6 addresses on the receiving 1436 * interface. This should, of course, be rare though. 1437 */ 1438 nd6log((LOG_NOTICE, 1439 "nd6_prefix_onlink: failed to find any ifaddr" 1440 " to add route for a prefix(%s/%d) on %s\n", 1441 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1442 pr->ndpr_plen, if_name(ifp))); 1443 return (0); 1444 } 1445 1446 /* 1447 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs. 1448 * ifa->ifa_rtrequest = nd6_rtrequest; 1449 */ 1450 bzero(&mask6, sizeof(mask6)); 1451 mask6.sin6_len = sizeof(mask6); 1452 mask6.sin6_addr = pr->ndpr_mask; 1453 rtflags = ifa->ifa_flags | RTF_CLONING | RTF_UP; 1454 if (nd6_need_cache(ifp)) { 1455 /* explicitly set in case ifa_flags does not set the flag. */ 1456 rtflags |= RTF_CLONING; 1457 } else { 1458 /* 1459 * explicitly clear the cloning bit in case ifa_flags sets it. 1460 */ 1461 rtflags &= ~RTF_CLONING; 1462 } 1463 error = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix, 1464 ifa->ifa_addr, (struct sockaddr *)&mask6, rtflags, &rt); 1465 if (error == 0) { 1466 if (rt != NULL) /* this should be non NULL, though */ 1467 nd6_rtmsg(RTM_ADD, rt); 1468 pr->ndpr_stateflags |= NDPRF_ONLINK; 1469 } else { 1470 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a" 1471 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx " 1472 "errno = %d\n", 1473 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1474 pr->ndpr_plen, if_name(ifp), 1475 ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr), 1476 ip6_sprintf(&mask6.sin6_addr), rtflags, error)); 1477 } 1478 1479 if (rt != NULL) { 1480 RT_LOCK(rt); 1481 rt->rt_refcnt--; 1482 RT_UNLOCK(rt); 1483 } 1484 1485 return (error); 1486} 1487 1488int 1489nd6_prefix_offlink(pr) 1490 struct nd_prefix *pr; 1491{ 1492 int error = 0; 1493 struct ifnet *ifp = pr->ndpr_ifp; 1494 struct nd_prefix *opr; 1495 struct sockaddr_in6 sa6, mask6; 1496 struct rtentry *rt = NULL; 1497 1498 /* sanity check */ 1499 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1500 nd6log((LOG_ERR, 1501 "nd6_prefix_offlink: %s/%d is already off-link\n", 1502 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen)); 1503 return (EEXIST); 1504 } 1505 1506 bzero(&sa6, sizeof(sa6)); 1507 sa6.sin6_family = AF_INET6; 1508 sa6.sin6_len = sizeof(sa6); 1509 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr, 1510 sizeof(struct in6_addr)); 1511 bzero(&mask6, sizeof(mask6)); 1512 mask6.sin6_family = AF_INET6; 1513 mask6.sin6_len = sizeof(sa6); 1514 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr)); 1515 error = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL, 1516 (struct sockaddr *)&mask6, 0, &rt); 1517 if (error == 0) { 1518 pr->ndpr_stateflags &= ~NDPRF_ONLINK; 1519 1520 /* report the route deletion to the routing socket. */ 1521 if (rt != NULL) 1522 nd6_rtmsg(RTM_DELETE, rt); 1523 1524 /* 1525 * There might be the same prefix on another interface, 1526 * the prefix which could not be on-link just because we have 1527 * the interface route (see comments in nd6_prefix_onlink). 1528 * If there's one, try to make the prefix on-link on the 1529 * interface. 1530 */ 1531 for (opr = nd_prefix.lh_first; opr; opr = opr->ndpr_next) { 1532 if (opr == pr) 1533 continue; 1534 1535 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0) 1536 continue; 1537 1538 /* 1539 * KAME specific: detached prefixes should not be 1540 * on-link. 1541 */ 1542 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1543 continue; 1544 1545 if (opr->ndpr_plen == pr->ndpr_plen && 1546 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1547 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) { 1548 int e; 1549 1550 if ((e = nd6_prefix_onlink(opr)) != 0) { 1551 nd6log((LOG_ERR, 1552 "nd6_prefix_offlink: failed to " 1553 "recover a prefix %s/%d from %s " 1554 "to %s (errno = %d)\n", 1555 ip6_sprintf(&opr->ndpr_prefix.sin6_addr), 1556 opr->ndpr_plen, if_name(ifp), 1557 if_name(opr->ndpr_ifp), e)); 1558 } 1559 } 1560 } 1561 } else { 1562 /* XXX: can we still set the NDPRF_ONLINK flag? */ 1563 nd6log((LOG_ERR, 1564 "nd6_prefix_offlink: failed to delete route: " 1565 "%s/%d on %s (errno = %d)\n", 1566 ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, if_name(ifp), 1567 error)); 1568 } 1569 1570 if (rt != NULL) { 1571 RTFREE(rt); 1572 } 1573 1574 return (error); 1575} 1576 1577static struct in6_ifaddr * 1578in6_ifadd(pr, ifid) 1579 struct nd_prefix *pr; 1580 struct in6_addr *ifid; /* Mobile IPv6 addition */ 1581{ 1582 struct ifnet *ifp = pr->ndpr_ifp; 1583 struct ifaddr *ifa; 1584 struct in6_aliasreq ifra; 1585 struct in6_ifaddr *ia, *ib; 1586 int error, plen0; 1587 struct in6_addr mask; 1588 int prefixlen = pr->ndpr_plen; 1589 1590 in6_prefixlen2mask(&mask, prefixlen); 1591 1592 /* 1593 * find a link-local address (will be interface ID). 1594 * Is it really mandatory? Theoretically, a global or a site-local 1595 * address can be configured without a link-local address, if we 1596 * have a unique interface identifier... 1597 * 1598 * it is not mandatory to have a link-local address, we can generate 1599 * interface identifier on the fly. we do this because: 1600 * (1) it should be the easiest way to find interface identifier. 1601 * (2) RFC2462 5.4 suggesting the use of the same interface identifier 1602 * for multiple addresses on a single interface, and possible shortcut 1603 * of DAD. we omitted DAD for this reason in the past. 1604 * (3) a user can prevent autoconfiguration of global address 1605 * by removing link-local address by hand (this is partly because we 1606 * don't have other way to control the use of IPv6 on an interface. 1607 * this has been our design choice - cf. NRL's "ifconfig auto"). 1608 * (4) it is easier to manage when an interface has addresses 1609 * with the same interface identifier, than to have multiple addresses 1610 * with different interface identifiers. 1611 * 1612 * Mobile IPv6 addition: allow for caller to specify a wished interface 1613 * ID. This is to not break connections when moving addresses between 1614 * interfaces. 1615 */ 1616 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */ 1617 if (ifa) 1618 ib = (struct in6_ifaddr *)ifa; 1619 else 1620 return NULL; 1621 1622#if 0 /* don't care link local addr state, and always do DAD */ 1623 /* if link-local address is not eligible, do not autoconfigure. */ 1624 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) { 1625 printf("in6_ifadd: link-local address not ready\n"); 1626 return NULL; 1627 } 1628#endif 1629 1630 /* prefixlen + ifidlen must be equal to 128 */ 1631 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL); 1632 if (prefixlen != plen0) { 1633 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s " 1634 "(prefix=%d ifid=%d)\n", 1635 if_name(ifp), prefixlen, 128 - plen0)); 1636 return NULL; 1637 } 1638 1639 /* make ifaddr */ 1640 1641 bzero(&ifra, sizeof(ifra)); 1642 /* 1643 * in6_update_ifa() does not use ifra_name, but we accurately set it 1644 * for safety. 1645 */ 1646 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 1647 ifra.ifra_addr.sin6_family = AF_INET6; 1648 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 1649 /* prefix */ 1650 bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr, 1651 sizeof(ifra.ifra_addr.sin6_addr)); 1652 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0]; 1653 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1]; 1654 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2]; 1655 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3]; 1656 1657 /* interface ID */ 1658 if (ifid == NULL || IN6_IS_ADDR_UNSPECIFIED(ifid)) 1659 ifid = &ib->ia_addr.sin6_addr; 1660 ifra.ifra_addr.sin6_addr.s6_addr32[0] |= 1661 (ifid->s6_addr32[0] & ~mask.s6_addr32[0]); 1662 ifra.ifra_addr.sin6_addr.s6_addr32[1] |= 1663 (ifid->s6_addr32[1] & ~mask.s6_addr32[1]); 1664 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 1665 (ifid->s6_addr32[2] & ~mask.s6_addr32[2]); 1666 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 1667 (ifid->s6_addr32[3] & ~mask.s6_addr32[3]); 1668 1669 /* new prefix mask. */ 1670 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1671 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1672 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr, 1673 sizeof(ifra.ifra_prefixmask.sin6_addr)); 1674 1675 /* 1676 * lifetime. 1677 * XXX: in6_init_address_ltimes would override these values later. 1678 * We should reconsider this logic. 1679 */ 1680 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime; 1681 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime; 1682 1683 /* XXX: scope zone ID? */ 1684 1685 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */ 1686 /* 1687 * temporarily set the nopfx flag to avoid conflict. 1688 * XXX: we should reconsider the entire mechanism about prefix 1689 * manipulation. 1690 */ 1691 ifra.ifra_flags |= IN6_IFF_NOPFX; 1692 1693 /* 1694 * keep the new address, regardless of the result of in6_update_ifa. 1695 * XXX: this address is now meaningless. 1696 * We should reconsider its role. 1697 */ 1698 pr->ndpr_addr = ifra.ifra_addr.sin6_addr; 1699 1700 /* allocate ifaddr structure, link into chain, etc. */ 1701 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) { 1702 nd6log((LOG_ERR, 1703 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n", 1704 ip6_sprintf(&ifra.ifra_addr.sin6_addr), if_name(ifp), 1705 error)); 1706 return (NULL); /* ifaddr must not have been allocated. */ 1707 } 1708 1709 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 1710 1711 return (ia); /* this is always non-NULL */ 1712} 1713 1714int 1715in6_tmpifadd(ia0, forcegen) 1716 const struct in6_ifaddr *ia0; /* corresponding public address */ 1717 int forcegen; 1718{ 1719 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp; 1720 struct in6_ifaddr *newia; 1721 struct in6_aliasreq ifra; 1722 int i, error; 1723 int trylimit = 3; /* XXX: adhoc value */ 1724 u_int32_t randid[2]; 1725 time_t vltime0, pltime0; 1726 1727 bzero(&ifra, sizeof(ifra)); 1728 strncpy(ifra.ifra_name, if_name(ifp), sizeof(ifra.ifra_name)); 1729 ifra.ifra_addr = ia0->ia_addr; 1730 /* copy prefix mask */ 1731 ifra.ifra_prefixmask = ia0->ia_prefixmask; 1732 /* clear the old IFID */ 1733 for (i = 0; i < 4; i++) { 1734 ifra.ifra_addr.sin6_addr.s6_addr32[i] &= 1735 ifra.ifra_prefixmask.sin6_addr.s6_addr32[i]; 1736 } 1737 1738 again: 1739 in6_get_tmpifid(ifp, (u_int8_t *)randid, 1740 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen); 1741 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 1742 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2])); 1743 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 1744 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3])); 1745 1746 /* 1747 * If by chance the new temporary address is the same as an address 1748 * already assigned to the interface, generate a new randomized 1749 * interface identifier and repeat this step. 1750 * RFC 3041 3.3 (4). 1751 */ 1752 if (in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr) != NULL) { 1753 if (trylimit-- == 0) { 1754 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find " 1755 "a unique random IFID\n")); 1756 return (EEXIST); 1757 } 1758 forcegen = 1; 1759 goto again; 1760 } 1761 1762 /* 1763 * The Valid Lifetime is the lower of the Valid Lifetime of the 1764 * public address or TEMP_VALID_LIFETIME. 1765 * The Preferred Lifetime is the lower of the Preferred Lifetime 1766 * of the public address or TEMP_PREFERRED_LIFETIME - 1767 * DESYNC_FACTOR. 1768 */ 1769 if (ia0->ia6_lifetime.ia6t_expire != 0) { 1770 vltime0 = IFA6_IS_INVALID(ia0) ? 0 : 1771 (ia0->ia6_lifetime.ia6t_expire - time_second); 1772 if (vltime0 > ip6_temp_valid_lifetime) 1773 vltime0 = ip6_temp_valid_lifetime; 1774 } else 1775 vltime0 = ip6_temp_valid_lifetime; 1776 if (ia0->ia6_lifetime.ia6t_preferred != 0) { 1777 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 : 1778 (ia0->ia6_lifetime.ia6t_preferred - time_second); 1779 if (pltime0 > ip6_temp_preferred_lifetime - ip6_desync_factor){ 1780 pltime0 = ip6_temp_preferred_lifetime - 1781 ip6_desync_factor; 1782 } 1783 } else 1784 pltime0 = ip6_temp_preferred_lifetime - ip6_desync_factor; 1785 ifra.ifra_lifetime.ia6t_vltime = vltime0; 1786 ifra.ifra_lifetime.ia6t_pltime = pltime0; 1787 1788 /* 1789 * A temporary address is created only if this calculated Preferred 1790 * Lifetime is greater than REGEN_ADVANCE time units. 1791 */ 1792 if (ifra.ifra_lifetime.ia6t_pltime <= ip6_temp_regen_advance) 1793 return (0); 1794 1795 /* XXX: scope zone ID? */ 1796 1797 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY); 1798 1799 /* allocate ifaddr structure, link into chain, etc. */ 1800 if ((error = in6_update_ifa(ifp, &ifra, NULL)) != 0) 1801 return (error); 1802 1803 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 1804 if (newia == NULL) { /* XXX: can it happen? */ 1805 nd6log((LOG_ERR, 1806 "in6_tmpifadd: ifa update succeeded, but we got " 1807 "no ifaddr\n")); 1808 return (EINVAL); /* XXX */ 1809 } 1810 newia->ia6_ndpr = ia0->ia6_ndpr; 1811 newia->ia6_ndpr->ndpr_refcnt++; 1812 1813 /* 1814 * A newly added address might affect the status of other addresses. 1815 * XXX: when the temporary address is generated with a new public 1816 * address, the onlink check is redundant. However, it would be safe 1817 * to do the check explicitly everywhere a new address is generated, 1818 * and, in fact, we surely need the check when we create a new 1819 * temporary address due to deprecation of an old temporary address. 1820 */ 1821 pfxlist_onlink_check(); 1822 1823 return (0); 1824} 1825 1826int 1827in6_init_prefix_ltimes(struct nd_prefix *ndpr) 1828{ 1829 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */ 1830 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) { 1831 nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime" 1832 "(%d) is greater than valid lifetime(%d)\n", 1833 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime)); 1834 return (EINVAL); 1835 } 1836 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) 1837 ndpr->ndpr_preferred = 0; 1838 else 1839 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime; 1840 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1841 ndpr->ndpr_expire = 0; 1842 else 1843 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime; 1844 1845 return 0; 1846} 1847 1848static void 1849in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6) 1850{ 1851 /* init ia6t_expire */ 1852 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) 1853 lt6->ia6t_expire = 0; 1854 else { 1855 lt6->ia6t_expire = time_second; 1856 lt6->ia6t_expire += lt6->ia6t_vltime; 1857 } 1858 1859 /* init ia6t_preferred */ 1860 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) 1861 lt6->ia6t_preferred = 0; 1862 else { 1863 lt6->ia6t_preferred = time_second; 1864 lt6->ia6t_preferred += lt6->ia6t_pltime; 1865 } 1866} 1867 1868/* 1869 * Delete all the routing table entries that use the specified gateway. 1870 * XXX: this function causes search through all entries of routing table, so 1871 * it shouldn't be called when acting as a router. 1872 */ 1873void 1874rt6_flush(gateway, ifp) 1875 struct in6_addr *gateway; 1876 struct ifnet *ifp; 1877{ 1878 struct radix_node_head *rnh = rt_tables[AF_INET6]; 1879 int s = splnet(); 1880 1881 /* We'll care only link-local addresses */ 1882 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) { 1883 splx(s); 1884 return; 1885 } 1886 /* XXX: hack for KAME's link-local address kludge */ 1887 gateway->s6_addr16[1] = htons(ifp->if_index); 1888 1889 RADIX_NODE_HEAD_LOCK(rnh); 1890 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway); 1891 RADIX_NODE_HEAD_UNLOCK(rnh); 1892 splx(s); 1893} 1894 1895static int 1896rt6_deleteroute(rn, arg) 1897 struct radix_node *rn; 1898 void *arg; 1899{ 1900#define SIN6(s) ((struct sockaddr_in6 *)s) 1901 struct rtentry *rt = (struct rtentry *)rn; 1902 struct in6_addr *gate = (struct in6_addr *)arg; 1903 1904 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) 1905 return (0); 1906 1907 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) { 1908 return (0); 1909 } 1910 1911 /* 1912 * Do not delete a static route. 1913 * XXX: this seems to be a bit ad-hoc. Should we consider the 1914 * 'cloned' bit instead? 1915 */ 1916 if ((rt->rt_flags & RTF_STATIC) != 0) 1917 return (0); 1918 1919 /* 1920 * We delete only host route. This means, in particular, we don't 1921 * delete default route. 1922 */ 1923 if ((rt->rt_flags & RTF_HOST) == 0) 1924 return (0); 1925 1926 return (rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway, 1927 rt_mask(rt), rt->rt_flags, 0)); 1928#undef SIN6 1929} 1930 1931int 1932nd6_setdefaultiface(ifindex) 1933 int ifindex; 1934{ 1935 int error = 0; 1936 1937 if (ifindex < 0 || if_index < ifindex) 1938 return (EINVAL); 1939 1940 if (nd6_defifindex != ifindex) { 1941 nd6_defifindex = ifindex; 1942 if (nd6_defifindex > 0) 1943 nd6_defifp = ifnet_byindex(nd6_defifindex); 1944 else 1945 nd6_defifp = NULL; 1946 1947 /* 1948 * If the Default Router List is empty, install a route 1949 * to the specified interface as default or remove the default 1950 * route when the default interface becomes canceled. 1951 * The check for the queue is actually redundant, but 1952 * we do this here to avoid re-install the default route 1953 * if the list is NOT empty. 1954 */ 1955 if (TAILQ_FIRST(&nd_defrouter) == NULL) 1956 defrouter_select(); 1957 1958 /* 1959 * Our current implementation assumes one-to-one maping between 1960 * interfaces and links, so it would be natural to use the 1961 * default interface as the default link. 1962 */ 1963 scope6_setdefault(nd6_defifp); 1964 } 1965 1966 return (error); 1967} 1968