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