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