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