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