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