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