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