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