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