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