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