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