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