1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $KAME: nd6_rtr.c,v 1.111 2001/04/27 01:37:15 jinmei Exp $ 32 */ 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include "opt_inet.h" 38 #include "opt_inet6.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/malloc.h> 43 #include <sys/mbuf.h> 44 #include <sys/refcount.h> 45 #include <sys/socket.h> 46 #include <sys/sockio.h> 47 #include <sys/time.h> 48 #include <sys/kernel.h> 49 #include <sys/lock.h> 50 #include <sys/errno.h> 51 #include <sys/rmlock.h> 52 #include <sys/rwlock.h> 53 #include <sys/syslog.h> 54 #include <sys/queue.h> 55 56 #include <net/if.h> 57 #include <net/if_var.h> 58 #include <net/if_types.h> 59 #include <net/if_dl.h> 60 #include <net/route.h> 61 #include <net/route_var.h> 62 #include <net/radix.h> 63 #include <net/vnet.h> 64 65 #include <netinet/in.h> 66 #include <net/if_llatbl.h> 67 #include <netinet6/in6_var.h> 68 #include <netinet6/in6_ifattach.h> 69 #include <netinet/ip6.h> 70 #include <netinet6/ip6_var.h> 71 #include <netinet6/nd6.h> 72 #include <netinet/icmp6.h> 73 #include <netinet6/scope6_var.h> 74 75 static int rtpref(struct nd_defrouter *); 76 static struct nd_defrouter *defrtrlist_update(struct nd_defrouter *); 77 static int prelist_update(struct nd_prefixctl *, struct nd_defrouter *, 78 struct mbuf *, int); 79 static struct in6_ifaddr *in6_ifadd(struct nd_prefixctl *, int); 80 static struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *, 81 struct nd_defrouter *); 82 static void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *); 83 static void pfxrtr_del(struct nd_pfxrouter *); 84 static struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *); 85 static void defrouter_delreq(struct nd_defrouter *); 86 static void nd6_rtmsg(int, struct rtentry *); 87 88 static int in6_init_prefix_ltimes(struct nd_prefix *); 89 static void in6_init_address_ltimes(struct nd_prefix *, 90 struct in6_addrlifetime *); 91 92 static int rt6_deleteroute(const struct rtentry *, void *); 93 94 VNET_DECLARE(int, nd6_recalc_reachtm_interval); 95 #define V_nd6_recalc_reachtm_interval VNET(nd6_recalc_reachtm_interval) 96 97 VNET_DEFINE_STATIC(struct ifnet *, nd6_defifp); 98 VNET_DEFINE(int, nd6_defifindex); 99 #define V_nd6_defifp VNET(nd6_defifp) 100 101 VNET_DEFINE(int, ip6_use_tempaddr) = 0; 102 103 VNET_DEFINE(int, ip6_desync_factor); 104 VNET_DEFINE(u_int32_t, ip6_temp_preferred_lifetime) = DEF_TEMP_PREFERRED_LIFETIME; 105 VNET_DEFINE(u_int32_t, ip6_temp_valid_lifetime) = DEF_TEMP_VALID_LIFETIME; 106 107 VNET_DEFINE(int, ip6_temp_regen_advance) = TEMPADDR_REGEN_ADVANCE; 108 109 #ifdef EXPERIMENTAL 110 VNET_DEFINE(int, nd6_ignore_ipv6_only_ra) = 1; 111 #endif 112 113 /* RTPREF_MEDIUM has to be 0! */ 114 #define RTPREF_HIGH 1 115 #define RTPREF_MEDIUM 0 116 #define RTPREF_LOW (-1) 117 #define RTPREF_RESERVED (-2) 118 #define RTPREF_INVALID (-3) /* internal */ 119 120 /* 121 * Receive Router Solicitation Message - just for routers. 122 * Router solicitation/advertisement is mostly managed by userland program 123 * (rtadvd) so here we have no function like nd6_ra_output(). 124 * 125 * Based on RFC 2461 126 */ 127 void 128 nd6_rs_input(struct mbuf *m, int off, int icmp6len) 129 { 130 struct ifnet *ifp = m->m_pkthdr.rcvif; 131 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 132 struct nd_router_solicit *nd_rs; 133 struct in6_addr saddr6 = ip6->ip6_src; 134 char *lladdr = NULL; 135 int lladdrlen = 0; 136 union nd_opts ndopts; 137 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 138 139 /* 140 * Accept RS only when V_ip6_forwarding=1 and the interface has 141 * no ND6_IFF_ACCEPT_RTADV. 142 */ 143 if (!V_ip6_forwarding || ND_IFINFO(ifp)->flags & ND6_IFF_ACCEPT_RTADV) 144 goto freeit; 145 146 /* RFC 6980: Nodes MUST silently ignore fragments */ 147 if(m->m_flags & M_FRAGMENTED) 148 goto freeit; 149 150 /* Sanity checks */ 151 if (ip6->ip6_hlim != 255) { 152 nd6log((LOG_ERR, 153 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 154 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 155 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 156 goto bad; 157 } 158 159 /* 160 * Don't update the neighbor cache, if src = ::. 161 * This indicates that the src has no IP address assigned yet. 162 */ 163 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 164 goto freeit; 165 166 #ifndef PULLDOWN_TEST 167 IP6_EXTHDR_CHECK(m, off, icmp6len,); 168 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off); 169 #else 170 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len); 171 if (nd_rs == NULL) { 172 ICMP6STAT_INC(icp6s_tooshort); 173 return; 174 } 175 #endif 176 177 icmp6len -= sizeof(*nd_rs); 178 nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 179 if (nd6_options(&ndopts) < 0) { 180 nd6log((LOG_INFO, 181 "nd6_rs_input: invalid ND option, ignored\n")); 182 /* nd6_options have incremented stats */ 183 goto freeit; 184 } 185 186 if (ndopts.nd_opts_src_lladdr) { 187 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 188 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 189 } 190 191 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 192 nd6log((LOG_INFO, 193 "nd6_rs_input: lladdrlen mismatch for %s " 194 "(if %d, RS packet %d)\n", 195 ip6_sprintf(ip6bufs, &saddr6), 196 ifp->if_addrlen, lladdrlen - 2)); 197 goto bad; 198 } 199 200 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0); 201 202 freeit: 203 m_freem(m); 204 return; 205 206 bad: 207 ICMP6STAT_INC(icp6s_badrs); 208 m_freem(m); 209 } 210 211 #ifdef EXPERIMENTAL 212 /* 213 * An initial update routine for draft-ietf-6man-ipv6only-flag. 214 * We need to iterate over all default routers for the given 215 * interface to see whether they are all advertising the "S" 216 * (IPv6-Only) flag. If they do set, otherwise unset, the 217 * interface flag we later use to filter on. 218 */ 219 static void 220 defrtr_ipv6_only_ifp(struct ifnet *ifp) 221 { 222 struct nd_defrouter *dr; 223 bool ipv6_only, ipv6_only_old; 224 #ifdef INET 225 struct epoch_tracker et; 226 struct ifaddr *ifa; 227 bool has_ipv4_addr; 228 #endif 229 230 if (V_nd6_ignore_ipv6_only_ra != 0) 231 return; 232 233 ipv6_only = true; 234 ND6_RLOCK(); 235 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) 236 if (dr->ifp == ifp && 237 (dr->raflags & ND_RA_FLAG_IPV6_ONLY) == 0) 238 ipv6_only = false; 239 ND6_RUNLOCK(); 240 241 IF_AFDATA_WLOCK(ifp); 242 ipv6_only_old = ND_IFINFO(ifp)->flags & ND6_IFF_IPV6_ONLY; 243 IF_AFDATA_WUNLOCK(ifp); 244 245 /* If nothing changed, we have an early exit. */ 246 if (ipv6_only == ipv6_only_old) 247 return; 248 249 #ifdef INET 250 /* 251 * Should we want to set the IPV6-ONLY flag, check if the 252 * interface has a non-0/0 and non-link-local IPv4 address 253 * configured on it. If it has we will assume working 254 * IPv4 operations and will clear the interface flag. 255 */ 256 has_ipv4_addr = false; 257 if (ipv6_only) { 258 NET_EPOCH_ENTER(et); 259 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 260 if (ifa->ifa_addr->sa_family != AF_INET) 261 continue; 262 if (in_canforward( 263 satosin(ifa->ifa_addr)->sin_addr)) { 264 has_ipv4_addr = true; 265 break; 266 } 267 } 268 NET_EPOCH_EXIT(et); 269 } 270 if (ipv6_only && has_ipv4_addr) { 271 log(LOG_NOTICE, "%s rcvd RA w/ IPv6-Only flag set but has IPv4 " 272 "configured, ignoring IPv6-Only flag.\n", ifp->if_xname); 273 ipv6_only = false; 274 } 275 #endif 276 277 IF_AFDATA_WLOCK(ifp); 278 if (ipv6_only) 279 ND_IFINFO(ifp)->flags |= ND6_IFF_IPV6_ONLY; 280 else 281 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY; 282 IF_AFDATA_WUNLOCK(ifp); 283 284 #ifdef notyet 285 /* Send notification of flag change. */ 286 #endif 287 } 288 289 static void 290 defrtr_ipv6_only_ipf_down(struct ifnet *ifp) 291 { 292 293 IF_AFDATA_WLOCK(ifp); 294 ND_IFINFO(ifp)->flags &= ~ND6_IFF_IPV6_ONLY; 295 IF_AFDATA_WUNLOCK(ifp); 296 } 297 #endif /* EXPERIMENTAL */ 298 299 void 300 nd6_ifnet_link_event(void *arg __unused, struct ifnet *ifp, int linkstate) 301 { 302 303 /* 304 * XXX-BZ we might want to trigger re-evaluation of our default router 305 * availability. E.g., on link down the default router might be 306 * unreachable but a different interface might still have connectivity. 307 */ 308 309 #ifdef EXPERIMENTAL 310 if (linkstate == LINK_STATE_DOWN) 311 defrtr_ipv6_only_ipf_down(ifp); 312 #endif 313 } 314 315 /* 316 * Receive Router Advertisement Message. 317 * 318 * Based on RFC 2461 319 * TODO: on-link bit on prefix information 320 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 321 */ 322 void 323 nd6_ra_input(struct mbuf *m, int off, int icmp6len) 324 { 325 struct ifnet *ifp = m->m_pkthdr.rcvif; 326 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 327 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 328 struct nd_router_advert *nd_ra; 329 struct in6_addr saddr6 = ip6->ip6_src; 330 int mcast = 0; 331 union nd_opts ndopts; 332 struct nd_defrouter *dr; 333 char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 334 335 dr = NULL; 336 337 /* 338 * We only accept RAs only when the per-interface flag 339 * ND6_IFF_ACCEPT_RTADV is on the receiving interface. 340 */ 341 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV)) 342 goto freeit; 343 344 /* RFC 6980: Nodes MUST silently ignore fragments */ 345 if(m->m_flags & M_FRAGMENTED) 346 goto freeit; 347 348 if (ip6->ip6_hlim != 255) { 349 nd6log((LOG_ERR, 350 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 351 ip6->ip6_hlim, ip6_sprintf(ip6bufs, &ip6->ip6_src), 352 ip6_sprintf(ip6bufd, &ip6->ip6_dst), if_name(ifp))); 353 goto bad; 354 } 355 356 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 357 nd6log((LOG_ERR, 358 "nd6_ra_input: src %s is not link-local\n", 359 ip6_sprintf(ip6bufs, &saddr6))); 360 goto bad; 361 } 362 363 #ifndef PULLDOWN_TEST 364 IP6_EXTHDR_CHECK(m, off, icmp6len,); 365 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off); 366 #else 367 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len); 368 if (nd_ra == NULL) { 369 ICMP6STAT_INC(icp6s_tooshort); 370 return; 371 } 372 #endif 373 374 icmp6len -= sizeof(*nd_ra); 375 nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 376 if (nd6_options(&ndopts) < 0) { 377 nd6log((LOG_INFO, 378 "nd6_ra_input: invalid ND option, ignored\n")); 379 /* nd6_options have incremented stats */ 380 goto freeit; 381 } 382 383 { 384 struct nd_defrouter dr0; 385 u_int32_t advreachable = nd_ra->nd_ra_reachable; 386 387 /* remember if this is a multicasted advertisement */ 388 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) 389 mcast = 1; 390 391 bzero(&dr0, sizeof(dr0)); 392 dr0.rtaddr = saddr6; 393 dr0.raflags = nd_ra->nd_ra_flags_reserved; 394 /* 395 * Effectively-disable routes from RA messages when 396 * ND6_IFF_NO_RADR enabled on the receiving interface or 397 * (ip6.forwarding == 1 && ip6.rfc6204w3 != 1). 398 */ 399 if (ndi->flags & ND6_IFF_NO_RADR) 400 dr0.rtlifetime = 0; 401 else if (V_ip6_forwarding && !V_ip6_rfc6204w3) 402 dr0.rtlifetime = 0; 403 else 404 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime); 405 dr0.expire = time_uptime + dr0.rtlifetime; 406 dr0.ifp = ifp; 407 /* unspecified or not? (RFC 2461 6.3.4) */ 408 if (advreachable) { 409 advreachable = ntohl(advreachable); 410 if (advreachable <= MAX_REACHABLE_TIME && 411 ndi->basereachable != advreachable) { 412 ndi->basereachable = advreachable; 413 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable); 414 ndi->recalctm = V_nd6_recalc_reachtm_interval; /* reset */ 415 } 416 } 417 if (nd_ra->nd_ra_retransmit) 418 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit); 419 if (nd_ra->nd_ra_curhoplimit) { 420 if (ndi->chlim < nd_ra->nd_ra_curhoplimit) 421 ndi->chlim = nd_ra->nd_ra_curhoplimit; 422 else if (ndi->chlim != nd_ra->nd_ra_curhoplimit) { 423 log(LOG_ERR, "RA with a lower CurHopLimit sent from " 424 "%s on %s (current = %d, received = %d). " 425 "Ignored.\n", ip6_sprintf(ip6bufs, &ip6->ip6_src), 426 if_name(ifp), ndi->chlim, nd_ra->nd_ra_curhoplimit); 427 } 428 } 429 dr = defrtrlist_update(&dr0); 430 #ifdef EXPERIMENTAL 431 defrtr_ipv6_only_ifp(ifp); 432 #endif 433 } 434 435 /* 436 * prefix 437 */ 438 if (ndopts.nd_opts_pi) { 439 struct nd_opt_hdr *pt; 440 struct nd_opt_prefix_info *pi = NULL; 441 struct nd_prefixctl pr; 442 443 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi; 444 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end; 445 pt = (struct nd_opt_hdr *)((caddr_t)pt + 446 (pt->nd_opt_len << 3))) { 447 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) 448 continue; 449 pi = (struct nd_opt_prefix_info *)pt; 450 451 if (pi->nd_opt_pi_len != 4) { 452 nd6log((LOG_INFO, 453 "nd6_ra_input: invalid option " 454 "len %d for prefix information option, " 455 "ignored\n", pi->nd_opt_pi_len)); 456 continue; 457 } 458 459 if (128 < pi->nd_opt_pi_prefix_len) { 460 nd6log((LOG_INFO, 461 "nd6_ra_input: invalid prefix " 462 "len %d for prefix information option, " 463 "ignored\n", pi->nd_opt_pi_prefix_len)); 464 continue; 465 } 466 467 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) 468 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) { 469 nd6log((LOG_INFO, 470 "nd6_ra_input: invalid prefix " 471 "%s, ignored\n", 472 ip6_sprintf(ip6bufs, 473 &pi->nd_opt_pi_prefix))); 474 continue; 475 } 476 477 bzero(&pr, sizeof(pr)); 478 pr.ndpr_prefix.sin6_family = AF_INET6; 479 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix); 480 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix; 481 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif; 482 483 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved & 484 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0; 485 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved & 486 ND_OPT_PI_FLAG_AUTO) ? 1 : 0; 487 pr.ndpr_plen = pi->nd_opt_pi_prefix_len; 488 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time); 489 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time); 490 (void)prelist_update(&pr, dr, m, mcast); 491 } 492 } 493 if (dr != NULL) { 494 defrouter_rele(dr); 495 dr = NULL; 496 } 497 498 /* 499 * MTU 500 */ 501 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) { 502 u_long mtu; 503 u_long maxmtu; 504 505 mtu = (u_long)ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu); 506 507 /* lower bound */ 508 if (mtu < IPV6_MMTU) { 509 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option " 510 "mtu=%lu sent from %s, ignoring\n", 511 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src))); 512 goto skip; 513 } 514 515 /* upper bound */ 516 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu) 517 ? ndi->maxmtu : ifp->if_mtu; 518 if (mtu <= maxmtu) { 519 int change = (ndi->linkmtu != mtu); 520 521 ndi->linkmtu = mtu; 522 if (change) { 523 /* in6_maxmtu may change */ 524 in6_setmaxmtu(); 525 rt_updatemtu(ifp); 526 } 527 } else { 528 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu " 529 "mtu=%lu sent from %s; " 530 "exceeds maxmtu %lu, ignoring\n", 531 mtu, ip6_sprintf(ip6bufs, &ip6->ip6_src), maxmtu)); 532 } 533 } 534 535 skip: 536 537 /* 538 * Source link layer address 539 */ 540 { 541 char *lladdr = NULL; 542 int lladdrlen = 0; 543 544 if (ndopts.nd_opts_src_lladdr) { 545 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 546 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 547 } 548 549 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 550 nd6log((LOG_INFO, 551 "nd6_ra_input: lladdrlen mismatch for %s " 552 "(if %d, RA packet %d)\n", ip6_sprintf(ip6bufs, &saddr6), 553 ifp->if_addrlen, lladdrlen - 2)); 554 goto bad; 555 } 556 557 nd6_cache_lladdr(ifp, &saddr6, lladdr, 558 lladdrlen, ND_ROUTER_ADVERT, 0); 559 560 /* 561 * Installing a link-layer address might change the state of the 562 * router's neighbor cache, which might also affect our on-link 563 * detection of adveritsed prefixes. 564 */ 565 pfxlist_onlink_check(); 566 } 567 568 freeit: 569 m_freem(m); 570 return; 571 572 bad: 573 ICMP6STAT_INC(icp6s_badra); 574 m_freem(m); 575 } 576 577 /* tell the change to user processes watching the routing socket. */ 578 static void 579 nd6_rtmsg(int cmd, struct rtentry *rt) 580 { 581 struct rt_addrinfo info; 582 struct ifnet *ifp; 583 struct ifaddr *ifa; 584 585 bzero((caddr_t)&info, sizeof(info)); 586 info.rti_info[RTAX_DST] = rt_key(rt); 587 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 588 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 589 ifp = rt->rt_ifp; 590 if (ifp != NULL) { 591 struct epoch_tracker et; 592 593 NET_EPOCH_ENTER(et); 594 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead); 595 info.rti_info[RTAX_IFP] = ifa->ifa_addr; 596 ifa_ref(ifa); 597 NET_EPOCH_EXIT(et); 598 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 599 } else 600 ifa = NULL; 601 602 rt_missmsg_fib(cmd, &info, rt->rt_flags, 0, rt->rt_fibnum); 603 if (ifa != NULL) 604 ifa_free(ifa); 605 } 606 607 /* 608 * default router list processing sub routines 609 */ 610 611 static void 612 defrouter_addreq(struct nd_defrouter *new) 613 { 614 struct sockaddr_in6 def, mask, gate; 615 struct rtentry *newrt = NULL; 616 int error; 617 618 bzero(&def, sizeof(def)); 619 bzero(&mask, sizeof(mask)); 620 bzero(&gate, sizeof(gate)); 621 622 def.sin6_len = mask.sin6_len = gate.sin6_len = 623 sizeof(struct sockaddr_in6); 624 def.sin6_family = gate.sin6_family = AF_INET6; 625 gate.sin6_addr = new->rtaddr; 626 627 error = in6_rtrequest(RTM_ADD, (struct sockaddr *)&def, 628 (struct sockaddr *)&gate, (struct sockaddr *)&mask, 629 RTF_GATEWAY, &newrt, new->ifp->if_fib); 630 if (newrt) { 631 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */ 632 RTFREE(newrt); 633 } 634 if (error == 0) 635 new->installed = 1; 636 } 637 638 struct nd_defrouter * 639 defrouter_lookup_locked(struct in6_addr *addr, struct ifnet *ifp) 640 { 641 struct nd_defrouter *dr; 642 643 ND6_LOCK_ASSERT(); 644 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) 645 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) { 646 defrouter_ref(dr); 647 return (dr); 648 } 649 return (NULL); 650 } 651 652 struct nd_defrouter * 653 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp) 654 { 655 struct nd_defrouter *dr; 656 657 ND6_RLOCK(); 658 dr = defrouter_lookup_locked(addr, ifp); 659 ND6_RUNLOCK(); 660 return (dr); 661 } 662 663 void 664 defrouter_ref(struct nd_defrouter *dr) 665 { 666 667 refcount_acquire(&dr->refcnt); 668 } 669 670 void 671 defrouter_rele(struct nd_defrouter *dr) 672 { 673 674 if (refcount_release(&dr->refcnt)) 675 free(dr, M_IP6NDP); 676 } 677 678 /* 679 * Remove the default route for a given router. 680 * This is just a subroutine function for defrouter_select_fib(), and 681 * should not be called from anywhere else. 682 */ 683 static void 684 defrouter_delreq(struct nd_defrouter *dr) 685 { 686 struct sockaddr_in6 def, mask, gate; 687 struct rtentry *oldrt = NULL; 688 689 bzero(&def, sizeof(def)); 690 bzero(&mask, sizeof(mask)); 691 bzero(&gate, sizeof(gate)); 692 693 def.sin6_len = mask.sin6_len = gate.sin6_len = 694 sizeof(struct sockaddr_in6); 695 def.sin6_family = gate.sin6_family = AF_INET6; 696 gate.sin6_addr = dr->rtaddr; 697 698 in6_rtrequest(RTM_DELETE, (struct sockaddr *)&def, 699 (struct sockaddr *)&gate, 700 (struct sockaddr *)&mask, RTF_GATEWAY, &oldrt, dr->ifp->if_fib); 701 if (oldrt) { 702 nd6_rtmsg(RTM_DELETE, oldrt); 703 RTFREE(oldrt); 704 } 705 706 dr->installed = 0; 707 } 708 709 /* 710 * Remove all default routes from default router list. 711 */ 712 void 713 defrouter_reset(void) 714 { 715 struct nd_defrouter *dr, **dra; 716 int count, i; 717 718 count = i = 0; 719 720 /* 721 * We can't delete routes with the ND lock held, so make a copy of the 722 * current default router list and use that when deleting routes. 723 */ 724 ND6_RLOCK(); 725 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) 726 count++; 727 ND6_RUNLOCK(); 728 729 dra = malloc(count * sizeof(*dra), M_TEMP, M_WAITOK | M_ZERO); 730 731 ND6_RLOCK(); 732 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 733 if (i == count) 734 break; 735 defrouter_ref(dr); 736 dra[i++] = dr; 737 } 738 ND6_RUNLOCK(); 739 740 for (i = 0; i < count && dra[i] != NULL; i++) { 741 defrouter_delreq(dra[i]); 742 defrouter_rele(dra[i]); 743 } 744 free(dra, M_TEMP); 745 746 /* 747 * XXX should we also nuke any default routers in the kernel, by 748 * going through them by rtalloc1()? 749 */ 750 } 751 752 /* 753 * Look up a matching default router list entry and remove it. Returns true if a 754 * matching entry was found, false otherwise. 755 */ 756 bool 757 defrouter_remove(struct in6_addr *addr, struct ifnet *ifp) 758 { 759 struct nd_defrouter *dr; 760 761 ND6_WLOCK(); 762 dr = defrouter_lookup_locked(addr, ifp); 763 if (dr == NULL) { 764 ND6_WUNLOCK(); 765 return (false); 766 } 767 768 defrouter_unlink(dr, NULL); 769 ND6_WUNLOCK(); 770 defrouter_del(dr); 771 defrouter_rele(dr); 772 return (true); 773 } 774 775 /* 776 * Remove a router from the global list and optionally stash it in a 777 * caller-supplied queue. 778 * 779 * The ND lock must be held. 780 */ 781 void 782 defrouter_unlink(struct nd_defrouter *dr, struct nd_drhead *drq) 783 { 784 785 ND6_WLOCK_ASSERT(); 786 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry); 787 V_nd6_list_genid++; 788 if (drq != NULL) 789 TAILQ_INSERT_TAIL(drq, dr, dr_entry); 790 } 791 792 void 793 defrouter_del(struct nd_defrouter *dr) 794 { 795 struct nd_defrouter *deldr = NULL; 796 struct nd_prefix *pr; 797 struct nd_pfxrouter *pfxrtr; 798 799 ND6_UNLOCK_ASSERT(); 800 801 /* 802 * Flush all the routing table entries that use the router 803 * as a next hop. 804 */ 805 if (ND_IFINFO(dr->ifp)->flags & ND6_IFF_ACCEPT_RTADV) 806 rt6_flush(&dr->rtaddr, dr->ifp); 807 808 #ifdef EXPERIMENTAL 809 defrtr_ipv6_only_ifp(dr->ifp); 810 #endif 811 812 if (dr->installed) { 813 deldr = dr; 814 defrouter_delreq(dr); 815 } 816 817 /* 818 * Also delete all the pointers to the router in each prefix lists. 819 */ 820 ND6_WLOCK(); 821 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 822 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) 823 pfxrtr_del(pfxrtr); 824 } 825 ND6_WUNLOCK(); 826 827 pfxlist_onlink_check(); 828 829 /* 830 * If the router is the primary one, choose a new one. 831 * Note that defrouter_select_fib() will remove the current 832 * gateway from the routing table. 833 */ 834 if (deldr) 835 defrouter_select_fib(deldr->ifp->if_fib); 836 837 /* 838 * Release the list reference. 839 */ 840 defrouter_rele(dr); 841 } 842 843 /* 844 * Default Router Selection according to Section 6.3.6 of RFC 2461 and 845 * draft-ietf-ipngwg-router-selection: 846 * 1) Routers that are reachable or probably reachable should be preferred. 847 * If we have more than one (probably) reachable router, prefer ones 848 * with the highest router preference. 849 * 2) When no routers on the list are known to be reachable or 850 * probably reachable, routers SHOULD be selected in a round-robin 851 * fashion, regardless of router preference values. 852 * 3) If the Default Router List is empty, assume that all 853 * destinations are on-link. 854 * 855 * We assume nd_defrouter is sorted by router preference value. 856 * Since the code below covers both with and without router preference cases, 857 * we do not need to classify the cases by ifdef. 858 * 859 * At this moment, we do not try to install more than one default router, 860 * even when the multipath routing is available, because we're not sure about 861 * the benefits for stub hosts comparing to the risk of making the code 862 * complicated and the possibility of introducing bugs. 863 * 864 * We maintain a single list of routers for multiple FIBs, only considering one 865 * at a time based on the receiving interface's FIB. If @fibnum is RT_ALL_FIBS, 866 * we do the whole thing multiple times. 867 */ 868 void 869 defrouter_select_fib(int fibnum) 870 { 871 struct epoch_tracker et; 872 struct nd_defrouter *dr, *selected_dr, *installed_dr; 873 struct llentry *ln = NULL; 874 875 if (fibnum == RT_ALL_FIBS) { 876 for (fibnum = 0; fibnum < rt_numfibs; fibnum++) { 877 defrouter_select_fib(fibnum); 878 } 879 } 880 881 ND6_RLOCK(); 882 /* 883 * Let's handle easy case (3) first: 884 * If default router list is empty, there's nothing to be done. 885 */ 886 if (TAILQ_EMPTY(&V_nd_defrouter)) { 887 ND6_RUNLOCK(); 888 return; 889 } 890 891 /* 892 * Search for a (probably) reachable router from the list. 893 * We just pick up the first reachable one (if any), assuming that 894 * the ordering rule of the list described in defrtrlist_update(). 895 */ 896 selected_dr = installed_dr = NULL; 897 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 898 NET_EPOCH_ENTER(et); 899 if (selected_dr == NULL && dr->ifp->if_fib == fibnum && 900 (ln = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) && 901 ND6_IS_LLINFO_PROBREACH(ln)) { 902 selected_dr = dr; 903 defrouter_ref(selected_dr); 904 } 905 NET_EPOCH_EXIT(et); 906 if (ln != NULL) { 907 LLE_RUNLOCK(ln); 908 ln = NULL; 909 } 910 911 if (dr->installed && dr->ifp->if_fib == fibnum) { 912 if (installed_dr == NULL) { 913 installed_dr = dr; 914 defrouter_ref(installed_dr); 915 } else { 916 /* 917 * this should not happen. 918 * warn for diagnosis. 919 */ 920 log(LOG_ERR, "defrouter_select_fib: more than " 921 "one router is installed\n"); 922 } 923 } 924 } 925 /* 926 * If none of the default routers was found to be reachable, 927 * round-robin the list regardless of preference. 928 * Otherwise, if we have an installed router, check if the selected 929 * (reachable) router should really be preferred to the installed one. 930 * We only prefer the new router when the old one is not reachable 931 * or when the new one has a really higher preference value. 932 */ 933 if (selected_dr == NULL) { 934 if (installed_dr == NULL || 935 TAILQ_NEXT(installed_dr, dr_entry) == NULL) 936 dr = TAILQ_FIRST(&V_nd_defrouter); 937 else 938 dr = TAILQ_NEXT(installed_dr, dr_entry); 939 940 /* Ensure we select a router for this FIB. */ 941 TAILQ_FOREACH_FROM(dr, &V_nd_defrouter, dr_entry) { 942 if (dr->ifp->if_fib == fibnum) { 943 selected_dr = dr; 944 defrouter_ref(selected_dr); 945 break; 946 } 947 } 948 } else if (installed_dr != NULL) { 949 NET_EPOCH_ENTER(et); 950 if ((ln = nd6_lookup(&installed_dr->rtaddr, 0, 951 installed_dr->ifp)) && 952 ND6_IS_LLINFO_PROBREACH(ln) && 953 installed_dr->ifp->if_fib == fibnum && 954 rtpref(selected_dr) <= rtpref(installed_dr)) { 955 defrouter_rele(selected_dr); 956 selected_dr = installed_dr; 957 } 958 NET_EPOCH_EXIT(et); 959 if (ln != NULL) 960 LLE_RUNLOCK(ln); 961 } 962 ND6_RUNLOCK(); 963 964 /* 965 * If we selected a router for this FIB and it's different 966 * than the installed one, remove the installed router and 967 * install the selected one in its place. 968 */ 969 if (installed_dr != selected_dr) { 970 if (installed_dr != NULL) { 971 defrouter_delreq(installed_dr); 972 defrouter_rele(installed_dr); 973 } 974 if (selected_dr != NULL) 975 defrouter_addreq(selected_dr); 976 } 977 if (selected_dr != NULL) 978 defrouter_rele(selected_dr); 979 } 980 981 /* 982 * Maintain old KPI for default router selection. 983 * If unspecified, we can re-select routers for all FIBs. 984 */ 985 void 986 defrouter_select(void) 987 { 988 defrouter_select_fib(RT_ALL_FIBS); 989 } 990 991 /* 992 * for default router selection 993 * regards router-preference field as a 2-bit signed integer 994 */ 995 static int 996 rtpref(struct nd_defrouter *dr) 997 { 998 switch (dr->raflags & ND_RA_FLAG_RTPREF_MASK) { 999 case ND_RA_FLAG_RTPREF_HIGH: 1000 return (RTPREF_HIGH); 1001 case ND_RA_FLAG_RTPREF_MEDIUM: 1002 case ND_RA_FLAG_RTPREF_RSV: 1003 return (RTPREF_MEDIUM); 1004 case ND_RA_FLAG_RTPREF_LOW: 1005 return (RTPREF_LOW); 1006 default: 1007 /* 1008 * This case should never happen. If it did, it would mean a 1009 * serious bug of kernel internal. We thus always bark here. 1010 * Or, can we even panic? 1011 */ 1012 log(LOG_ERR, "rtpref: impossible RA flag %x\n", dr->raflags); 1013 return (RTPREF_INVALID); 1014 } 1015 /* NOTREACHED */ 1016 } 1017 1018 static struct nd_defrouter * 1019 defrtrlist_update(struct nd_defrouter *new) 1020 { 1021 struct nd_defrouter *dr, *n; 1022 uint64_t genid; 1023 int oldpref; 1024 bool writelocked; 1025 1026 if (new->rtlifetime == 0) { 1027 defrouter_remove(&new->rtaddr, new->ifp); 1028 return (NULL); 1029 } 1030 1031 ND6_RLOCK(); 1032 writelocked = false; 1033 restart: 1034 dr = defrouter_lookup_locked(&new->rtaddr, new->ifp); 1035 if (dr != NULL) { 1036 oldpref = rtpref(dr); 1037 1038 /* override */ 1039 dr->raflags = new->raflags; /* XXX flag check */ 1040 dr->rtlifetime = new->rtlifetime; 1041 dr->expire = new->expire; 1042 1043 /* 1044 * If the preference does not change, there's no need 1045 * to sort the entries. Also make sure the selected 1046 * router is still installed in the kernel. 1047 */ 1048 if (dr->installed && rtpref(new) == oldpref) { 1049 if (writelocked) 1050 ND6_WUNLOCK(); 1051 else 1052 ND6_RUNLOCK(); 1053 return (dr); 1054 } 1055 } 1056 1057 /* 1058 * The router needs to be reinserted into the default router 1059 * list, so upgrade to a write lock. If that fails and the list 1060 * has potentially changed while the lock was dropped, we'll 1061 * redo the lookup with the write lock held. 1062 */ 1063 if (!writelocked) { 1064 writelocked = true; 1065 if (!ND6_TRY_UPGRADE()) { 1066 genid = V_nd6_list_genid; 1067 ND6_RUNLOCK(); 1068 ND6_WLOCK(); 1069 if (genid != V_nd6_list_genid) 1070 goto restart; 1071 } 1072 } 1073 1074 if (dr != NULL) { 1075 /* 1076 * The preferred router may have changed, so relocate this 1077 * router. 1078 */ 1079 TAILQ_REMOVE(&V_nd_defrouter, dr, dr_entry); 1080 n = dr; 1081 } else { 1082 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO); 1083 if (n == NULL) { 1084 ND6_WUNLOCK(); 1085 return (NULL); 1086 } 1087 memcpy(n, new, sizeof(*n)); 1088 /* Initialize with an extra reference for the caller. */ 1089 refcount_init(&n->refcnt, 2); 1090 } 1091 1092 /* 1093 * Insert the new router in the Default Router List; 1094 * The Default Router List should be in the descending order 1095 * of router-preferece. Routers with the same preference are 1096 * sorted in the arriving time order. 1097 */ 1098 1099 /* insert at the end of the group */ 1100 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 1101 if (rtpref(n) > rtpref(dr)) 1102 break; 1103 } 1104 if (dr != NULL) 1105 TAILQ_INSERT_BEFORE(dr, n, dr_entry); 1106 else 1107 TAILQ_INSERT_TAIL(&V_nd_defrouter, n, dr_entry); 1108 V_nd6_list_genid++; 1109 ND6_WUNLOCK(); 1110 1111 defrouter_select_fib(new->ifp->if_fib); 1112 1113 return (n); 1114 } 1115 1116 static struct nd_pfxrouter * 1117 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr) 1118 { 1119 struct nd_pfxrouter *search; 1120 1121 ND6_LOCK_ASSERT(); 1122 1123 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) { 1124 if (search->router == dr) 1125 break; 1126 } 1127 return (search); 1128 } 1129 1130 static void 1131 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr) 1132 { 1133 struct nd_pfxrouter *new; 1134 bool update; 1135 1136 ND6_UNLOCK_ASSERT(); 1137 1138 ND6_RLOCK(); 1139 if (pfxrtr_lookup(pr, dr) != NULL) { 1140 ND6_RUNLOCK(); 1141 return; 1142 } 1143 ND6_RUNLOCK(); 1144 1145 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO); 1146 if (new == NULL) 1147 return; 1148 defrouter_ref(dr); 1149 new->router = dr; 1150 1151 ND6_WLOCK(); 1152 if (pfxrtr_lookup(pr, dr) == NULL) { 1153 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry); 1154 update = true; 1155 } else { 1156 /* We lost a race to add the reference. */ 1157 defrouter_rele(dr); 1158 free(new, M_IP6NDP); 1159 update = false; 1160 } 1161 ND6_WUNLOCK(); 1162 1163 if (update) 1164 pfxlist_onlink_check(); 1165 } 1166 1167 static void 1168 pfxrtr_del(struct nd_pfxrouter *pfr) 1169 { 1170 1171 ND6_WLOCK_ASSERT(); 1172 1173 LIST_REMOVE(pfr, pfr_entry); 1174 defrouter_rele(pfr->router); 1175 free(pfr, M_IP6NDP); 1176 } 1177 1178 static struct nd_prefix * 1179 nd6_prefix_lookup_locked(struct nd_prefixctl *key) 1180 { 1181 struct nd_prefix *search; 1182 1183 ND6_LOCK_ASSERT(); 1184 1185 LIST_FOREACH(search, &V_nd_prefix, ndpr_entry) { 1186 if (key->ndpr_ifp == search->ndpr_ifp && 1187 key->ndpr_plen == search->ndpr_plen && 1188 in6_are_prefix_equal(&key->ndpr_prefix.sin6_addr, 1189 &search->ndpr_prefix.sin6_addr, key->ndpr_plen)) { 1190 nd6_prefix_ref(search); 1191 break; 1192 } 1193 } 1194 return (search); 1195 } 1196 1197 struct nd_prefix * 1198 nd6_prefix_lookup(struct nd_prefixctl *key) 1199 { 1200 struct nd_prefix *search; 1201 1202 ND6_RLOCK(); 1203 search = nd6_prefix_lookup_locked(key); 1204 ND6_RUNLOCK(); 1205 return (search); 1206 } 1207 1208 void 1209 nd6_prefix_ref(struct nd_prefix *pr) 1210 { 1211 1212 refcount_acquire(&pr->ndpr_refcnt); 1213 } 1214 1215 void 1216 nd6_prefix_rele(struct nd_prefix *pr) 1217 { 1218 1219 if (refcount_release(&pr->ndpr_refcnt)) { 1220 KASSERT(LIST_EMPTY(&pr->ndpr_advrtrs), 1221 ("prefix %p has advertising routers", pr)); 1222 free(pr, M_IP6NDP); 1223 } 1224 } 1225 1226 int 1227 nd6_prelist_add(struct nd_prefixctl *pr, struct nd_defrouter *dr, 1228 struct nd_prefix **newp) 1229 { 1230 struct nd_prefix *new; 1231 char ip6buf[INET6_ADDRSTRLEN]; 1232 int error; 1233 1234 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO); 1235 if (new == NULL) 1236 return (ENOMEM); 1237 refcount_init(&new->ndpr_refcnt, newp != NULL ? 2 : 1); 1238 new->ndpr_ifp = pr->ndpr_ifp; 1239 new->ndpr_prefix = pr->ndpr_prefix; 1240 new->ndpr_plen = pr->ndpr_plen; 1241 new->ndpr_vltime = pr->ndpr_vltime; 1242 new->ndpr_pltime = pr->ndpr_pltime; 1243 new->ndpr_flags = pr->ndpr_flags; 1244 if ((error = in6_init_prefix_ltimes(new)) != 0) { 1245 free(new, M_IP6NDP); 1246 return (error); 1247 } 1248 new->ndpr_lastupdate = time_uptime; 1249 1250 /* initialization */ 1251 LIST_INIT(&new->ndpr_advrtrs); 1252 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen); 1253 /* make prefix in the canonical form */ 1254 IN6_MASK_ADDR(&new->ndpr_prefix.sin6_addr, &new->ndpr_mask); 1255 1256 ND6_WLOCK(); 1257 LIST_INSERT_HEAD(&V_nd_prefix, new, ndpr_entry); 1258 V_nd6_list_genid++; 1259 ND6_WUNLOCK(); 1260 1261 /* ND_OPT_PI_FLAG_ONLINK processing */ 1262 if (new->ndpr_raf_onlink) { 1263 struct epoch_tracker et; 1264 1265 ND6_ONLINK_LOCK(); 1266 NET_EPOCH_ENTER(et); 1267 if ((error = nd6_prefix_onlink(new)) != 0) { 1268 nd6log((LOG_ERR, "nd6_prelist_add: failed to make " 1269 "the prefix %s/%d on-link on %s (errno=%d)\n", 1270 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1271 pr->ndpr_plen, if_name(pr->ndpr_ifp), error)); 1272 /* proceed anyway. XXX: is it correct? */ 1273 } 1274 NET_EPOCH_EXIT(et); 1275 ND6_ONLINK_UNLOCK(); 1276 } 1277 1278 if (dr != NULL) 1279 pfxrtr_add(new, dr); 1280 if (newp != NULL) 1281 *newp = new; 1282 return (0); 1283 } 1284 1285 /* 1286 * Remove a prefix from the prefix list and optionally stash it in a 1287 * caller-provided list. 1288 * 1289 * The ND6 lock must be held. 1290 */ 1291 void 1292 nd6_prefix_unlink(struct nd_prefix *pr, struct nd_prhead *list) 1293 { 1294 1295 ND6_WLOCK_ASSERT(); 1296 1297 LIST_REMOVE(pr, ndpr_entry); 1298 V_nd6_list_genid++; 1299 if (list != NULL) 1300 LIST_INSERT_HEAD(list, pr, ndpr_entry); 1301 } 1302 1303 /* 1304 * Free an unlinked prefix, first marking it off-link if necessary. 1305 */ 1306 void 1307 nd6_prefix_del(struct nd_prefix *pr) 1308 { 1309 struct nd_pfxrouter *pfr, *next; 1310 int e; 1311 char ip6buf[INET6_ADDRSTRLEN]; 1312 1313 KASSERT(pr->ndpr_addrcnt == 0, 1314 ("prefix %p has referencing addresses", pr)); 1315 ND6_UNLOCK_ASSERT(); 1316 1317 /* 1318 * Though these flags are now meaningless, we'd rather keep the value 1319 * of pr->ndpr_raf_onlink and pr->ndpr_raf_auto not to confuse users 1320 * when executing "ndp -p". 1321 */ 1322 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1323 ND6_ONLINK_LOCK(); 1324 if ((e = nd6_prefix_offlink(pr)) != 0) { 1325 nd6log((LOG_ERR, 1326 "nd6_prefix_del: failed to make %s/%d offlink " 1327 "on %s, errno=%d\n", 1328 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1329 pr->ndpr_plen, if_name(pr->ndpr_ifp), e)); 1330 /* what should we do? */ 1331 } 1332 ND6_ONLINK_UNLOCK(); 1333 } 1334 1335 /* Release references to routers that have advertised this prefix. */ 1336 ND6_WLOCK(); 1337 LIST_FOREACH_SAFE(pfr, &pr->ndpr_advrtrs, pfr_entry, next) 1338 pfxrtr_del(pfr); 1339 ND6_WUNLOCK(); 1340 1341 nd6_prefix_rele(pr); 1342 1343 pfxlist_onlink_check(); 1344 } 1345 1346 static int 1347 prelist_update(struct nd_prefixctl *new, struct nd_defrouter *dr, 1348 struct mbuf *m, int mcast) 1349 { 1350 struct in6_ifaddr *ia6 = NULL, *ia6_match = NULL; 1351 struct ifaddr *ifa; 1352 struct ifnet *ifp = new->ndpr_ifp; 1353 struct nd_prefix *pr; 1354 int error = 0; 1355 int auth; 1356 struct in6_addrlifetime lt6_tmp; 1357 char ip6buf[INET6_ADDRSTRLEN]; 1358 1359 NET_EPOCH_ASSERT(); 1360 1361 auth = 0; 1362 if (m) { 1363 /* 1364 * Authenticity for NA consists authentication for 1365 * both IP header and IP datagrams, doesn't it ? 1366 */ 1367 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM) 1368 auth = ((m->m_flags & M_AUTHIPHDR) && 1369 (m->m_flags & M_AUTHIPDGM)); 1370 #endif 1371 } 1372 1373 if ((pr = nd6_prefix_lookup(new)) != NULL) { 1374 /* 1375 * nd6_prefix_lookup() ensures that pr and new have the same 1376 * prefix on a same interface. 1377 */ 1378 1379 /* 1380 * Update prefix information. Note that the on-link (L) bit 1381 * and the autonomous (A) bit should NOT be changed from 1 1382 * to 0. 1383 */ 1384 if (new->ndpr_raf_onlink == 1) 1385 pr->ndpr_raf_onlink = 1; 1386 if (new->ndpr_raf_auto == 1) 1387 pr->ndpr_raf_auto = 1; 1388 if (new->ndpr_raf_onlink) { 1389 pr->ndpr_vltime = new->ndpr_vltime; 1390 pr->ndpr_pltime = new->ndpr_pltime; 1391 (void)in6_init_prefix_ltimes(pr); /* XXX error case? */ 1392 pr->ndpr_lastupdate = time_uptime; 1393 } 1394 1395 if (new->ndpr_raf_onlink && 1396 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1397 ND6_ONLINK_LOCK(); 1398 if ((error = nd6_prefix_onlink(pr)) != 0) { 1399 nd6log((LOG_ERR, 1400 "prelist_update: failed to make " 1401 "the prefix %s/%d on-link on %s " 1402 "(errno=%d)\n", 1403 ip6_sprintf(ip6buf, 1404 &pr->ndpr_prefix.sin6_addr), 1405 pr->ndpr_plen, if_name(pr->ndpr_ifp), 1406 error)); 1407 /* proceed anyway. XXX: is it correct? */ 1408 } 1409 ND6_ONLINK_UNLOCK(); 1410 } 1411 1412 if (dr != NULL) 1413 pfxrtr_add(pr, dr); 1414 } else { 1415 if (new->ndpr_vltime == 0) 1416 goto end; 1417 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0) 1418 goto end; 1419 1420 error = nd6_prelist_add(new, dr, &pr); 1421 if (error != 0) { 1422 nd6log((LOG_NOTICE, "prelist_update: " 1423 "nd6_prelist_add failed for %s/%d on %s errno=%d\n", 1424 ip6_sprintf(ip6buf, &new->ndpr_prefix.sin6_addr), 1425 new->ndpr_plen, if_name(new->ndpr_ifp), error)); 1426 goto end; /* we should just give up in this case. */ 1427 } 1428 1429 /* 1430 * XXX: from the ND point of view, we can ignore a prefix 1431 * with the on-link bit being zero. However, we need a 1432 * prefix structure for references from autoconfigured 1433 * addresses. Thus, we explicitly make sure that the prefix 1434 * itself expires now. 1435 */ 1436 if (pr->ndpr_raf_onlink == 0) { 1437 pr->ndpr_vltime = 0; 1438 pr->ndpr_pltime = 0; 1439 in6_init_prefix_ltimes(pr); 1440 } 1441 } 1442 1443 /* 1444 * Address autoconfiguration based on Section 5.5.3 of RFC 2462. 1445 * Note that pr must be non NULL at this point. 1446 */ 1447 1448 /* 5.5.3 (a). Ignore the prefix without the A bit set. */ 1449 if (!new->ndpr_raf_auto) 1450 goto end; 1451 1452 /* 1453 * 5.5.3 (b). the link-local prefix should have been ignored in 1454 * nd6_ra_input. 1455 */ 1456 1457 /* 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. */ 1458 if (new->ndpr_pltime > new->ndpr_vltime) { 1459 error = EINVAL; /* XXX: won't be used */ 1460 goto end; 1461 } 1462 1463 /* 1464 * 5.5.3 (d). If the prefix advertised is not equal to the prefix of 1465 * an address configured by stateless autoconfiguration already in the 1466 * list of addresses associated with the interface, and the Valid 1467 * Lifetime is not 0, form an address. We first check if we have 1468 * a matching prefix. 1469 * Note: we apply a clarification in rfc2462bis-02 here. We only 1470 * consider autoconfigured addresses while RFC2462 simply said 1471 * "address". 1472 */ 1473 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1474 struct in6_ifaddr *ifa6; 1475 u_int32_t remaininglifetime; 1476 1477 if (ifa->ifa_addr->sa_family != AF_INET6) 1478 continue; 1479 1480 ifa6 = (struct in6_ifaddr *)ifa; 1481 1482 /* 1483 * We only consider autoconfigured addresses as per rfc2462bis. 1484 */ 1485 if (!(ifa6->ia6_flags & IN6_IFF_AUTOCONF)) 1486 continue; 1487 1488 /* 1489 * Spec is not clear here, but I believe we should concentrate 1490 * on unicast (i.e. not anycast) addresses. 1491 * XXX: other ia6_flags? detached or duplicated? 1492 */ 1493 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0) 1494 continue; 1495 1496 /* 1497 * Ignore the address if it is not associated with a prefix 1498 * or is associated with a prefix that is different from this 1499 * one. (pr is never NULL here) 1500 */ 1501 if (ifa6->ia6_ndpr != pr) 1502 continue; 1503 1504 if (ia6_match == NULL) /* remember the first one */ 1505 ia6_match = ifa6; 1506 1507 /* 1508 * An already autoconfigured address matched. Now that we 1509 * are sure there is at least one matched address, we can 1510 * proceed to 5.5.3. (e): update the lifetimes according to the 1511 * "two hours" rule and the privacy extension. 1512 * We apply some clarifications in rfc2462bis: 1513 * - use remaininglifetime instead of storedlifetime as a 1514 * variable name 1515 * - remove the dead code in the "two-hour" rule 1516 */ 1517 #define TWOHOUR (120*60) 1518 lt6_tmp = ifa6->ia6_lifetime; 1519 1520 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME) 1521 remaininglifetime = ND6_INFINITE_LIFETIME; 1522 else if (time_uptime - ifa6->ia6_updatetime > 1523 lt6_tmp.ia6t_vltime) { 1524 /* 1525 * The case of "invalid" address. We should usually 1526 * not see this case. 1527 */ 1528 remaininglifetime = 0; 1529 } else 1530 remaininglifetime = lt6_tmp.ia6t_vltime - 1531 (time_uptime - ifa6->ia6_updatetime); 1532 1533 /* when not updating, keep the current stored lifetime. */ 1534 lt6_tmp.ia6t_vltime = remaininglifetime; 1535 1536 if (TWOHOUR < new->ndpr_vltime || 1537 remaininglifetime < new->ndpr_vltime) { 1538 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1539 } else if (remaininglifetime <= TWOHOUR) { 1540 if (auth) { 1541 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1542 } 1543 } else { 1544 /* 1545 * new->ndpr_vltime <= TWOHOUR && 1546 * TWOHOUR < remaininglifetime 1547 */ 1548 lt6_tmp.ia6t_vltime = TWOHOUR; 1549 } 1550 1551 /* The 2 hour rule is not imposed for preferred lifetime. */ 1552 lt6_tmp.ia6t_pltime = new->ndpr_pltime; 1553 1554 in6_init_address_ltimes(pr, <6_tmp); 1555 1556 /* 1557 * We need to treat lifetimes for temporary addresses 1558 * differently, according to 1559 * draft-ietf-ipv6-privacy-addrs-v2-01.txt 3.3 (1); 1560 * we only update the lifetimes when they are in the maximum 1561 * intervals. 1562 */ 1563 if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0) { 1564 u_int32_t maxvltime, maxpltime; 1565 1566 if (V_ip6_temp_valid_lifetime > 1567 (u_int32_t)((time_uptime - ifa6->ia6_createtime) + 1568 V_ip6_desync_factor)) { 1569 maxvltime = V_ip6_temp_valid_lifetime - 1570 (time_uptime - ifa6->ia6_createtime) - 1571 V_ip6_desync_factor; 1572 } else 1573 maxvltime = 0; 1574 if (V_ip6_temp_preferred_lifetime > 1575 (u_int32_t)((time_uptime - ifa6->ia6_createtime) + 1576 V_ip6_desync_factor)) { 1577 maxpltime = V_ip6_temp_preferred_lifetime - 1578 (time_uptime - ifa6->ia6_createtime) - 1579 V_ip6_desync_factor; 1580 } else 1581 maxpltime = 0; 1582 1583 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME || 1584 lt6_tmp.ia6t_vltime > maxvltime) { 1585 lt6_tmp.ia6t_vltime = maxvltime; 1586 } 1587 if (lt6_tmp.ia6t_pltime == ND6_INFINITE_LIFETIME || 1588 lt6_tmp.ia6t_pltime > maxpltime) { 1589 lt6_tmp.ia6t_pltime = maxpltime; 1590 } 1591 } 1592 ifa6->ia6_lifetime = lt6_tmp; 1593 ifa6->ia6_updatetime = time_uptime; 1594 } 1595 if (ia6_match == NULL && new->ndpr_vltime) { 1596 int ifidlen; 1597 1598 /* 1599 * 5.5.3 (d) (continued) 1600 * No address matched and the valid lifetime is non-zero. 1601 * Create a new address. 1602 */ 1603 1604 /* 1605 * Prefix Length check: 1606 * If the sum of the prefix length and interface identifier 1607 * length does not equal 128 bits, the Prefix Information 1608 * option MUST be ignored. The length of the interface 1609 * identifier is defined in a separate link-type specific 1610 * document. 1611 */ 1612 ifidlen = in6_if2idlen(ifp); 1613 if (ifidlen < 0) { 1614 /* this should not happen, so we always log it. */ 1615 log(LOG_ERR, "prelist_update: IFID undefined (%s)\n", 1616 if_name(ifp)); 1617 goto end; 1618 } 1619 if (ifidlen + pr->ndpr_plen != 128) { 1620 nd6log((LOG_INFO, 1621 "prelist_update: invalid prefixlen " 1622 "%d for %s, ignored\n", 1623 pr->ndpr_plen, if_name(ifp))); 1624 goto end; 1625 } 1626 1627 if ((ia6 = in6_ifadd(new, mcast)) != NULL) { 1628 /* 1629 * note that we should use pr (not new) for reference. 1630 */ 1631 pr->ndpr_addrcnt++; 1632 ia6->ia6_ndpr = pr; 1633 1634 /* 1635 * RFC 3041 3.3 (2). 1636 * When a new public address is created as described 1637 * in RFC2462, also create a new temporary address. 1638 * 1639 * RFC 3041 3.5. 1640 * When an interface connects to a new link, a new 1641 * randomized interface identifier should be generated 1642 * immediately together with a new set of temporary 1643 * addresses. Thus, we specifiy 1 as the 2nd arg of 1644 * in6_tmpifadd(). 1645 */ 1646 if (V_ip6_use_tempaddr) { 1647 int e; 1648 if ((e = in6_tmpifadd(ia6, 1, 1)) != 0) { 1649 nd6log((LOG_NOTICE, "prelist_update: " 1650 "failed to create a temporary " 1651 "address, errno=%d\n", 1652 e)); 1653 } 1654 } 1655 ifa_free(&ia6->ia_ifa); 1656 1657 /* 1658 * A newly added address might affect the status 1659 * of other addresses, so we check and update it. 1660 * XXX: what if address duplication happens? 1661 */ 1662 pfxlist_onlink_check(); 1663 } else { 1664 /* just set an error. do not bark here. */ 1665 error = EADDRNOTAVAIL; /* XXX: might be unused. */ 1666 } 1667 } 1668 1669 end: 1670 if (pr != NULL) 1671 nd6_prefix_rele(pr); 1672 return (error); 1673 } 1674 1675 /* 1676 * A supplement function used in the on-link detection below; 1677 * detect if a given prefix has a (probably) reachable advertising router. 1678 * XXX: lengthy function name... 1679 */ 1680 static struct nd_pfxrouter * 1681 find_pfxlist_reachable_router(struct nd_prefix *pr) 1682 { 1683 struct epoch_tracker et; 1684 struct nd_pfxrouter *pfxrtr; 1685 struct llentry *ln; 1686 int canreach; 1687 1688 ND6_LOCK_ASSERT(); 1689 1690 NET_EPOCH_ENTER(et); 1691 LIST_FOREACH(pfxrtr, &pr->ndpr_advrtrs, pfr_entry) { 1692 ln = nd6_lookup(&pfxrtr->router->rtaddr, 0, pfxrtr->router->ifp); 1693 if (ln == NULL) 1694 continue; 1695 canreach = ND6_IS_LLINFO_PROBREACH(ln); 1696 LLE_RUNLOCK(ln); 1697 if (canreach) 1698 break; 1699 } 1700 NET_EPOCH_EXIT(et); 1701 return (pfxrtr); 1702 } 1703 1704 /* 1705 * Check if each prefix in the prefix list has at least one available router 1706 * that advertised the prefix (a router is "available" if its neighbor cache 1707 * entry is reachable or probably reachable). 1708 * If the check fails, the prefix may be off-link, because, for example, 1709 * we have moved from the network but the lifetime of the prefix has not 1710 * expired yet. So we should not use the prefix if there is another prefix 1711 * that has an available router. 1712 * But, if there is no prefix that has an available router, we still regard 1713 * all the prefixes as on-link. This is because we can't tell if all the 1714 * routers are simply dead or if we really moved from the network and there 1715 * is no router around us. 1716 */ 1717 void 1718 pfxlist_onlink_check(void) 1719 { 1720 struct nd_prefix *pr; 1721 struct in6_ifaddr *ifa; 1722 struct nd_defrouter *dr; 1723 struct nd_pfxrouter *pfxrtr = NULL; 1724 struct rm_priotracker in6_ifa_tracker; 1725 uint64_t genid; 1726 uint32_t flags; 1727 1728 ND6_ONLINK_LOCK(); 1729 ND6_RLOCK(); 1730 1731 /* 1732 * Check if there is a prefix that has a reachable advertising 1733 * router. 1734 */ 1735 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1736 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr)) 1737 break; 1738 } 1739 1740 /* 1741 * If we have no such prefix, check whether we still have a router 1742 * that does not advertise any prefixes. 1743 */ 1744 if (pr == NULL) { 1745 TAILQ_FOREACH(dr, &V_nd_defrouter, dr_entry) { 1746 struct nd_prefix *pr0; 1747 1748 LIST_FOREACH(pr0, &V_nd_prefix, ndpr_entry) { 1749 if ((pfxrtr = pfxrtr_lookup(pr0, dr)) != NULL) 1750 break; 1751 } 1752 if (pfxrtr != NULL) 1753 break; 1754 } 1755 } 1756 if (pr != NULL || (!TAILQ_EMPTY(&V_nd_defrouter) && pfxrtr == NULL)) { 1757 /* 1758 * There is at least one prefix that has a reachable router, 1759 * or at least a router which probably does not advertise 1760 * any prefixes. The latter would be the case when we move 1761 * to a new link where we have a router that does not provide 1762 * prefixes and we configure an address by hand. 1763 * Detach prefixes which have no reachable advertising 1764 * router, and attach other prefixes. 1765 */ 1766 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1767 /* XXX: a link-local prefix should never be detached */ 1768 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) || 1769 pr->ndpr_raf_onlink == 0 || 1770 pr->ndpr_raf_auto == 0) 1771 continue; 1772 1773 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1774 find_pfxlist_reachable_router(pr) == NULL) 1775 pr->ndpr_stateflags |= NDPRF_DETACHED; 1776 else if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1777 find_pfxlist_reachable_router(pr) != NULL) 1778 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1779 } 1780 } else { 1781 /* there is no prefix that has a reachable router */ 1782 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1783 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) || 1784 pr->ndpr_raf_onlink == 0 || 1785 pr->ndpr_raf_auto == 0) 1786 continue; 1787 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1788 } 1789 } 1790 1791 /* 1792 * Remove each interface route associated with a (just) detached 1793 * prefix, and reinstall the interface route for a (just) attached 1794 * prefix. Note that all attempt of reinstallation does not 1795 * necessarily success, when a same prefix is shared among multiple 1796 * interfaces. Such cases will be handled in nd6_prefix_onlink, 1797 * so we don't have to care about them. 1798 */ 1799 restart: 1800 LIST_FOREACH(pr, &V_nd_prefix, ndpr_entry) { 1801 char ip6buf[INET6_ADDRSTRLEN]; 1802 int e; 1803 1804 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) || 1805 pr->ndpr_raf_onlink == 0 || 1806 pr->ndpr_raf_auto == 0) 1807 continue; 1808 1809 flags = pr->ndpr_stateflags & (NDPRF_DETACHED | NDPRF_ONLINK); 1810 if (flags == 0 || flags == (NDPRF_DETACHED | NDPRF_ONLINK)) { 1811 genid = V_nd6_list_genid; 1812 ND6_RUNLOCK(); 1813 if ((flags & NDPRF_ONLINK) != 0 && 1814 (e = nd6_prefix_offlink(pr)) != 0) { 1815 nd6log((LOG_ERR, 1816 "pfxlist_onlink_check: failed to " 1817 "make %s/%d offlink, errno=%d\n", 1818 ip6_sprintf(ip6buf, 1819 &pr->ndpr_prefix.sin6_addr), 1820 pr->ndpr_plen, e)); 1821 } else if ((flags & NDPRF_ONLINK) == 0 && 1822 (e = nd6_prefix_onlink(pr)) != 0) { 1823 nd6log((LOG_ERR, 1824 "pfxlist_onlink_check: failed to " 1825 "make %s/%d onlink, errno=%d\n", 1826 ip6_sprintf(ip6buf, 1827 &pr->ndpr_prefix.sin6_addr), 1828 pr->ndpr_plen, e)); 1829 } 1830 ND6_RLOCK(); 1831 if (genid != V_nd6_list_genid) 1832 goto restart; 1833 } 1834 } 1835 1836 /* 1837 * Changes on the prefix status might affect address status as well. 1838 * Make sure that all addresses derived from an attached prefix are 1839 * attached, and that all addresses derived from a detached prefix are 1840 * detached. Note, however, that a manually configured address should 1841 * always be attached. 1842 * The precise detection logic is same as the one for prefixes. 1843 */ 1844 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 1845 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1846 if (!(ifa->ia6_flags & IN6_IFF_AUTOCONF)) 1847 continue; 1848 1849 if (ifa->ia6_ndpr == NULL) { 1850 /* 1851 * This can happen when we first configure the address 1852 * (i.e. the address exists, but the prefix does not). 1853 * XXX: complicated relationships... 1854 */ 1855 continue; 1856 } 1857 1858 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) 1859 break; 1860 } 1861 if (ifa) { 1862 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1863 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1864 continue; 1865 1866 if (ifa->ia6_ndpr == NULL) /* XXX: see above. */ 1867 continue; 1868 1869 if (find_pfxlist_reachable_router(ifa->ia6_ndpr)) { 1870 if (ifa->ia6_flags & IN6_IFF_DETACHED) { 1871 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1872 ifa->ia6_flags |= IN6_IFF_TENTATIVE; 1873 nd6_dad_start((struct ifaddr *)ifa, 0); 1874 } 1875 } else { 1876 ifa->ia6_flags |= IN6_IFF_DETACHED; 1877 } 1878 } 1879 } else { 1880 CK_STAILQ_FOREACH(ifa, &V_in6_ifaddrhead, ia_link) { 1881 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1882 continue; 1883 1884 if (ifa->ia6_flags & IN6_IFF_DETACHED) { 1885 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1886 ifa->ia6_flags |= IN6_IFF_TENTATIVE; 1887 /* Do we need a delay in this case? */ 1888 nd6_dad_start((struct ifaddr *)ifa, 0); 1889 } 1890 } 1891 } 1892 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1893 ND6_RUNLOCK(); 1894 ND6_ONLINK_UNLOCK(); 1895 } 1896 1897 static int 1898 nd6_prefix_onlink_rtrequest(struct nd_prefix *pr, struct ifaddr *ifa) 1899 { 1900 struct sockaddr_dl sdl; 1901 struct rtentry *rt; 1902 struct sockaddr_in6 mask6; 1903 u_long rtflags; 1904 int error, a_failure, fibnum, maxfib; 1905 1906 /* 1907 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs. 1908 * ifa->ifa_rtrequest = nd6_rtrequest; 1909 */ 1910 bzero(&mask6, sizeof(mask6)); 1911 mask6.sin6_len = sizeof(mask6); 1912 mask6.sin6_addr = pr->ndpr_mask; 1913 rtflags = (ifa->ifa_flags & ~IFA_RTSELF) | RTF_UP; 1914 1915 bzero(&sdl, sizeof(struct sockaddr_dl)); 1916 sdl.sdl_len = sizeof(struct sockaddr_dl); 1917 sdl.sdl_family = AF_LINK; 1918 sdl.sdl_type = ifa->ifa_ifp->if_type; 1919 sdl.sdl_index = ifa->ifa_ifp->if_index; 1920 1921 if(V_rt_add_addr_allfibs) { 1922 fibnum = 0; 1923 maxfib = rt_numfibs; 1924 } else { 1925 fibnum = ifa->ifa_ifp->if_fib; 1926 maxfib = fibnum + 1; 1927 } 1928 a_failure = 0; 1929 for (; fibnum < maxfib; fibnum++) { 1930 1931 rt = NULL; 1932 error = in6_rtrequest(RTM_ADD, 1933 (struct sockaddr *)&pr->ndpr_prefix, (struct sockaddr *)&sdl, 1934 (struct sockaddr *)&mask6, rtflags, &rt, fibnum); 1935 if (error == 0) { 1936 KASSERT(rt != NULL, ("%s: in6_rtrequest return no " 1937 "error(%d) but rt is NULL, pr=%p, ifa=%p", __func__, 1938 error, pr, ifa)); 1939 RT_LOCK(rt); 1940 nd6_rtmsg(RTM_ADD, rt); 1941 RT_UNLOCK(rt); 1942 pr->ndpr_stateflags |= NDPRF_ONLINK; 1943 } else { 1944 char ip6buf[INET6_ADDRSTRLEN]; 1945 char ip6bufg[INET6_ADDRSTRLEN]; 1946 char ip6bufm[INET6_ADDRSTRLEN]; 1947 struct sockaddr_in6 *sin6; 1948 1949 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 1950 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add " 1951 "route for a prefix (%s/%d) on %s, gw=%s, mask=%s, " 1952 "flags=%lx errno = %d\n", 1953 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 1954 pr->ndpr_plen, if_name(pr->ndpr_ifp), 1955 ip6_sprintf(ip6bufg, &sin6->sin6_addr), 1956 ip6_sprintf(ip6bufm, &mask6.sin6_addr), 1957 rtflags, error)); 1958 1959 /* Save last error to return, see rtinit(). */ 1960 a_failure = error; 1961 } 1962 1963 if (rt != NULL) { 1964 RT_LOCK(rt); 1965 RT_REMREF(rt); 1966 RT_UNLOCK(rt); 1967 } 1968 } 1969 1970 /* Return the last error we got. */ 1971 return (a_failure); 1972 } 1973 1974 int 1975 nd6_prefix_onlink(struct nd_prefix *pr) 1976 { 1977 struct ifaddr *ifa; 1978 struct ifnet *ifp = pr->ndpr_ifp; 1979 struct nd_prefix *opr; 1980 char ip6buf[INET6_ADDRSTRLEN]; 1981 int error; 1982 1983 ND6_ONLINK_LOCK_ASSERT(); 1984 ND6_UNLOCK_ASSERT(); 1985 1986 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) 1987 return (EEXIST); 1988 1989 /* 1990 * Add the interface route associated with the prefix. Before 1991 * installing the route, check if there's the same prefix on another 1992 * interface, and the prefix has already installed the interface route. 1993 * Although such a configuration is expected to be rare, we explicitly 1994 * allow it. 1995 */ 1996 ND6_RLOCK(); 1997 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) { 1998 if (opr == pr) 1999 continue; 2000 2001 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0) 2002 continue; 2003 2004 if (!V_rt_add_addr_allfibs && 2005 opr->ndpr_ifp->if_fib != pr->ndpr_ifp->if_fib) 2006 continue; 2007 2008 if (opr->ndpr_plen == pr->ndpr_plen && 2009 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 2010 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) { 2011 ND6_RUNLOCK(); 2012 return (0); 2013 } 2014 } 2015 ND6_RUNLOCK(); 2016 2017 /* 2018 * We prefer link-local addresses as the associated interface address. 2019 */ 2020 /* search for a link-local addr */ 2021 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 2022 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST); 2023 if (ifa == NULL) { 2024 struct epoch_tracker et; 2025 2026 /* XXX: freebsd does not have ifa_ifwithaf */ 2027 NET_EPOCH_ENTER(et); 2028 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2029 if (ifa->ifa_addr->sa_family == AF_INET6) { 2030 ifa_ref(ifa); 2031 break; 2032 } 2033 } 2034 NET_EPOCH_EXIT(et); 2035 /* should we care about ia6_flags? */ 2036 } 2037 if (ifa == NULL) { 2038 /* 2039 * This can still happen, when, for example, we receive an RA 2040 * containing a prefix with the L bit set and the A bit clear, 2041 * after removing all IPv6 addresses on the receiving 2042 * interface. This should, of course, be rare though. 2043 */ 2044 nd6log((LOG_NOTICE, 2045 "nd6_prefix_onlink: failed to find any ifaddr" 2046 " to add route for a prefix(%s/%d) on %s\n", 2047 ip6_sprintf(ip6buf, &pr->ndpr_prefix.sin6_addr), 2048 pr->ndpr_plen, if_name(ifp))); 2049 return (0); 2050 } 2051 2052 error = nd6_prefix_onlink_rtrequest(pr, ifa); 2053 2054 if (ifa != NULL) 2055 ifa_free(ifa); 2056 2057 return (error); 2058 } 2059 2060 int 2061 nd6_prefix_offlink(struct nd_prefix *pr) 2062 { 2063 int error = 0; 2064 struct ifnet *ifp = pr->ndpr_ifp; 2065 struct nd_prefix *opr; 2066 struct sockaddr_in6 sa6, mask6; 2067 struct rtentry *rt; 2068 char ip6buf[INET6_ADDRSTRLEN]; 2069 uint64_t genid; 2070 int fibnum, maxfib, a_failure; 2071 2072 ND6_ONLINK_LOCK_ASSERT(); 2073 ND6_UNLOCK_ASSERT(); 2074 2075 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) 2076 return (EEXIST); 2077 2078 bzero(&sa6, sizeof(sa6)); 2079 sa6.sin6_family = AF_INET6; 2080 sa6.sin6_len = sizeof(sa6); 2081 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr, 2082 sizeof(struct in6_addr)); 2083 bzero(&mask6, sizeof(mask6)); 2084 mask6.sin6_family = AF_INET6; 2085 mask6.sin6_len = sizeof(sa6); 2086 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr)); 2087 2088 if (V_rt_add_addr_allfibs) { 2089 fibnum = 0; 2090 maxfib = rt_numfibs; 2091 } else { 2092 fibnum = ifp->if_fib; 2093 maxfib = fibnum + 1; 2094 } 2095 2096 a_failure = 0; 2097 for (; fibnum < maxfib; fibnum++) { 2098 rt = NULL; 2099 error = in6_rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL, 2100 (struct sockaddr *)&mask6, 0, &rt, fibnum); 2101 if (error == 0) { 2102 /* report the route deletion to the routing socket. */ 2103 if (rt != NULL) 2104 nd6_rtmsg(RTM_DELETE, rt); 2105 } else { 2106 /* Save last error to return, see rtinit(). */ 2107 a_failure = error; 2108 } 2109 if (rt != NULL) { 2110 RTFREE(rt); 2111 } 2112 } 2113 error = a_failure; 2114 a_failure = 1; 2115 if (error == 0) { 2116 pr->ndpr_stateflags &= ~NDPRF_ONLINK; 2117 2118 /* 2119 * There might be the same prefix on another interface, 2120 * the prefix which could not be on-link just because we have 2121 * the interface route (see comments in nd6_prefix_onlink). 2122 * If there's one, try to make the prefix on-link on the 2123 * interface. 2124 */ 2125 ND6_RLOCK(); 2126 restart: 2127 LIST_FOREACH(opr, &V_nd_prefix, ndpr_entry) { 2128 /* 2129 * KAME specific: detached prefixes should not be 2130 * on-link. 2131 */ 2132 if (opr == pr || (opr->ndpr_stateflags & 2133 (NDPRF_ONLINK | NDPRF_DETACHED)) != 0) 2134 continue; 2135 2136 if (opr->ndpr_plen == pr->ndpr_plen && 2137 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 2138 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) { 2139 int e; 2140 2141 genid = V_nd6_list_genid; 2142 ND6_RUNLOCK(); 2143 if ((e = nd6_prefix_onlink(opr)) != 0) { 2144 nd6log((LOG_ERR, 2145 "nd6_prefix_offlink: failed to " 2146 "recover a prefix %s/%d from %s " 2147 "to %s (errno = %d)\n", 2148 ip6_sprintf(ip6buf, 2149 &opr->ndpr_prefix.sin6_addr), 2150 opr->ndpr_plen, if_name(ifp), 2151 if_name(opr->ndpr_ifp), e)); 2152 } else 2153 a_failure = 0; 2154 ND6_RLOCK(); 2155 if (genid != V_nd6_list_genid) 2156 goto restart; 2157 } 2158 } 2159 ND6_RUNLOCK(); 2160 } else { 2161 /* XXX: can we still set the NDPRF_ONLINK flag? */ 2162 nd6log((LOG_ERR, 2163 "nd6_prefix_offlink: failed to delete route: " 2164 "%s/%d on %s (errno = %d)\n", 2165 ip6_sprintf(ip6buf, &sa6.sin6_addr), pr->ndpr_plen, 2166 if_name(ifp), error)); 2167 } 2168 2169 if (a_failure) 2170 lltable_prefix_free(AF_INET6, (struct sockaddr *)&sa6, 2171 (struct sockaddr *)&mask6, LLE_STATIC); 2172 2173 return (error); 2174 } 2175 2176 static struct in6_ifaddr * 2177 in6_ifadd(struct nd_prefixctl *pr, int mcast) 2178 { 2179 struct ifnet *ifp = pr->ndpr_ifp; 2180 struct ifaddr *ifa; 2181 struct in6_aliasreq ifra; 2182 struct in6_ifaddr *ia, *ib; 2183 int error, plen0; 2184 struct in6_addr mask; 2185 int prefixlen = pr->ndpr_plen; 2186 int updateflags; 2187 char ip6buf[INET6_ADDRSTRLEN]; 2188 2189 in6_prefixlen2mask(&mask, prefixlen); 2190 2191 /* 2192 * find a link-local address (will be interface ID). 2193 * Is it really mandatory? Theoretically, a global or a site-local 2194 * address can be configured without a link-local address, if we 2195 * have a unique interface identifier... 2196 * 2197 * it is not mandatory to have a link-local address, we can generate 2198 * interface identifier on the fly. we do this because: 2199 * (1) it should be the easiest way to find interface identifier. 2200 * (2) RFC2462 5.4 suggesting the use of the same interface identifier 2201 * for multiple addresses on a single interface, and possible shortcut 2202 * of DAD. we omitted DAD for this reason in the past. 2203 * (3) a user can prevent autoconfiguration of global address 2204 * by removing link-local address by hand (this is partly because we 2205 * don't have other way to control the use of IPv6 on an interface. 2206 * this has been our design choice - cf. NRL's "ifconfig auto"). 2207 * (4) it is easier to manage when an interface has addresses 2208 * with the same interface identifier, than to have multiple addresses 2209 * with different interface identifiers. 2210 */ 2211 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0); /* 0 is OK? */ 2212 if (ifa) 2213 ib = (struct in6_ifaddr *)ifa; 2214 else 2215 return NULL; 2216 2217 /* prefixlen + ifidlen must be equal to 128 */ 2218 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL); 2219 if (prefixlen != plen0) { 2220 ifa_free(ifa); 2221 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s " 2222 "(prefix=%d ifid=%d)\n", 2223 if_name(ifp), prefixlen, 128 - plen0)); 2224 return NULL; 2225 } 2226 2227 /* make ifaddr */ 2228 in6_prepare_ifra(&ifra, &pr->ndpr_prefix.sin6_addr, &mask); 2229 2230 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, &mask); 2231 /* interface ID */ 2232 ifra.ifra_addr.sin6_addr.s6_addr32[0] |= 2233 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]); 2234 ifra.ifra_addr.sin6_addr.s6_addr32[1] |= 2235 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]); 2236 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 2237 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]); 2238 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 2239 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]); 2240 ifa_free(ifa); 2241 2242 /* lifetimes. */ 2243 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime; 2244 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime; 2245 2246 /* XXX: scope zone ID? */ 2247 2248 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */ 2249 2250 /* 2251 * Make sure that we do not have this address already. This should 2252 * usually not happen, but we can still see this case, e.g., if we 2253 * have manually configured the exact address to be configured. 2254 */ 2255 ifa = (struct ifaddr *)in6ifa_ifpwithaddr(ifp, 2256 &ifra.ifra_addr.sin6_addr); 2257 if (ifa != NULL) { 2258 ifa_free(ifa); 2259 /* this should be rare enough to make an explicit log */ 2260 log(LOG_INFO, "in6_ifadd: %s is already configured\n", 2261 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr)); 2262 return (NULL); 2263 } 2264 2265 /* 2266 * Allocate ifaddr structure, link into chain, etc. 2267 * If we are going to create a new address upon receiving a multicasted 2268 * RA, we need to impose a random delay before starting DAD. 2269 * [draft-ietf-ipv6-rfc2462bis-02.txt, Section 5.4.2] 2270 */ 2271 updateflags = 0; 2272 if (mcast) 2273 updateflags |= IN6_IFAUPDATE_DADDELAY; 2274 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) { 2275 nd6log((LOG_ERR, 2276 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n", 2277 ip6_sprintf(ip6buf, &ifra.ifra_addr.sin6_addr), 2278 if_name(ifp), error)); 2279 return (NULL); /* ifaddr must not have been allocated. */ 2280 } 2281 2282 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 2283 /* 2284 * XXXRW: Assumption of non-NULLness here might not be true with 2285 * fine-grained locking -- should we validate it? Or just return 2286 * earlier ifa rather than looking it up again? 2287 */ 2288 return (ia); /* this is always non-NULL and referenced. */ 2289 } 2290 2291 /* 2292 * ia0 - corresponding public address 2293 */ 2294 int 2295 in6_tmpifadd(const struct in6_ifaddr *ia0, int forcegen, int delay) 2296 { 2297 struct ifnet *ifp = ia0->ia_ifa.ifa_ifp; 2298 struct in6_ifaddr *newia; 2299 struct in6_aliasreq ifra; 2300 int error; 2301 int trylimit = 3; /* XXX: adhoc value */ 2302 int updateflags; 2303 u_int32_t randid[2]; 2304 time_t vltime0, pltime0; 2305 2306 in6_prepare_ifra(&ifra, &ia0->ia_addr.sin6_addr, 2307 &ia0->ia_prefixmask.sin6_addr); 2308 2309 ifra.ifra_addr = ia0->ia_addr; /* XXX: do we need this ? */ 2310 /* clear the old IFID */ 2311 IN6_MASK_ADDR(&ifra.ifra_addr.sin6_addr, 2312 &ifra.ifra_prefixmask.sin6_addr); 2313 2314 again: 2315 if (in6_get_tmpifid(ifp, (u_int8_t *)randid, 2316 (const u_int8_t *)&ia0->ia_addr.sin6_addr.s6_addr[8], forcegen)) { 2317 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to find a good " 2318 "random IFID\n")); 2319 return (EINVAL); 2320 } 2321 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 2322 (randid[0] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[2])); 2323 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 2324 (randid[1] & ~(ifra.ifra_prefixmask.sin6_addr.s6_addr32[3])); 2325 2326 /* 2327 * in6_get_tmpifid() quite likely provided a unique interface ID. 2328 * However, we may still have a chance to see collision, because 2329 * there may be a time lag between generation of the ID and generation 2330 * of the address. So, we'll do one more sanity check. 2331 */ 2332 2333 if (in6_localip(&ifra.ifra_addr.sin6_addr) != 0) { 2334 if (trylimit-- > 0) { 2335 forcegen = 1; 2336 goto again; 2337 } 2338 2339 /* Give up. Something strange should have happened. */ 2340 nd6log((LOG_NOTICE, "in6_tmpifadd: failed to " 2341 "find a unique random IFID\n")); 2342 return (EEXIST); 2343 } 2344 2345 /* 2346 * The Valid Lifetime is the lower of the Valid Lifetime of the 2347 * public address or TEMP_VALID_LIFETIME. 2348 * The Preferred Lifetime is the lower of the Preferred Lifetime 2349 * of the public address or TEMP_PREFERRED_LIFETIME - 2350 * DESYNC_FACTOR. 2351 */ 2352 if (ia0->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 2353 vltime0 = IFA6_IS_INVALID(ia0) ? 0 : 2354 (ia0->ia6_lifetime.ia6t_vltime - 2355 (time_uptime - ia0->ia6_updatetime)); 2356 if (vltime0 > V_ip6_temp_valid_lifetime) 2357 vltime0 = V_ip6_temp_valid_lifetime; 2358 } else 2359 vltime0 = V_ip6_temp_valid_lifetime; 2360 if (ia0->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 2361 pltime0 = IFA6_IS_DEPRECATED(ia0) ? 0 : 2362 (ia0->ia6_lifetime.ia6t_pltime - 2363 (time_uptime - ia0->ia6_updatetime)); 2364 if (pltime0 > V_ip6_temp_preferred_lifetime - V_ip6_desync_factor){ 2365 pltime0 = V_ip6_temp_preferred_lifetime - 2366 V_ip6_desync_factor; 2367 } 2368 } else 2369 pltime0 = V_ip6_temp_preferred_lifetime - V_ip6_desync_factor; 2370 ifra.ifra_lifetime.ia6t_vltime = vltime0; 2371 ifra.ifra_lifetime.ia6t_pltime = pltime0; 2372 2373 /* 2374 * A temporary address is created only if this calculated Preferred 2375 * Lifetime is greater than REGEN_ADVANCE time units. 2376 */ 2377 if (ifra.ifra_lifetime.ia6t_pltime <= V_ip6_temp_regen_advance) 2378 return (0); 2379 2380 /* XXX: scope zone ID? */ 2381 2382 ifra.ifra_flags |= (IN6_IFF_AUTOCONF|IN6_IFF_TEMPORARY); 2383 2384 /* allocate ifaddr structure, link into chain, etc. */ 2385 updateflags = 0; 2386 if (delay) 2387 updateflags |= IN6_IFAUPDATE_DADDELAY; 2388 if ((error = in6_update_ifa(ifp, &ifra, NULL, updateflags)) != 0) 2389 return (error); 2390 2391 newia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 2392 if (newia == NULL) { /* XXX: can it happen? */ 2393 nd6log((LOG_ERR, 2394 "in6_tmpifadd: ifa update succeeded, but we got " 2395 "no ifaddr\n")); 2396 return (EINVAL); /* XXX */ 2397 } 2398 newia->ia6_ndpr = ia0->ia6_ndpr; 2399 newia->ia6_ndpr->ndpr_addrcnt++; 2400 ifa_free(&newia->ia_ifa); 2401 2402 /* 2403 * A newly added address might affect the status of other addresses. 2404 * XXX: when the temporary address is generated with a new public 2405 * address, the onlink check is redundant. However, it would be safe 2406 * to do the check explicitly everywhere a new address is generated, 2407 * and, in fact, we surely need the check when we create a new 2408 * temporary address due to deprecation of an old temporary address. 2409 */ 2410 pfxlist_onlink_check(); 2411 2412 return (0); 2413 } 2414 2415 static int 2416 in6_init_prefix_ltimes(struct nd_prefix *ndpr) 2417 { 2418 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) 2419 ndpr->ndpr_preferred = 0; 2420 else 2421 ndpr->ndpr_preferred = time_uptime + ndpr->ndpr_pltime; 2422 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) 2423 ndpr->ndpr_expire = 0; 2424 else 2425 ndpr->ndpr_expire = time_uptime + ndpr->ndpr_vltime; 2426 2427 return 0; 2428 } 2429 2430 static void 2431 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6) 2432 { 2433 /* init ia6t_expire */ 2434 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) 2435 lt6->ia6t_expire = 0; 2436 else { 2437 lt6->ia6t_expire = time_uptime; 2438 lt6->ia6t_expire += lt6->ia6t_vltime; 2439 } 2440 2441 /* init ia6t_preferred */ 2442 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) 2443 lt6->ia6t_preferred = 0; 2444 else { 2445 lt6->ia6t_preferred = time_uptime; 2446 lt6->ia6t_preferred += lt6->ia6t_pltime; 2447 } 2448 } 2449 2450 /* 2451 * Delete all the routing table entries that use the specified gateway. 2452 * XXX: this function causes search through all entries of routing table, so 2453 * it shouldn't be called when acting as a router. 2454 */ 2455 void 2456 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp) 2457 { 2458 2459 /* We'll care only link-local addresses */ 2460 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) 2461 return; 2462 2463 /* XXX Do we really need to walk any but the default FIB? */ 2464 rt_foreach_fib_walk_del(AF_INET6, rt6_deleteroute, (void *)gateway); 2465 } 2466 2467 static int 2468 rt6_deleteroute(const struct rtentry *rt, void *arg) 2469 { 2470 #define SIN6(s) ((struct sockaddr_in6 *)s) 2471 struct in6_addr *gate = (struct in6_addr *)arg; 2472 2473 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) 2474 return (0); 2475 2476 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) { 2477 return (0); 2478 } 2479 2480 /* 2481 * Do not delete a static route. 2482 * XXX: this seems to be a bit ad-hoc. Should we consider the 2483 * 'cloned' bit instead? 2484 */ 2485 if ((rt->rt_flags & RTF_STATIC) != 0) 2486 return (0); 2487 2488 /* 2489 * We delete only host route. This means, in particular, we don't 2490 * delete default route. 2491 */ 2492 if ((rt->rt_flags & RTF_HOST) == 0) 2493 return (0); 2494 2495 return (1); 2496 #undef SIN6 2497 } 2498 2499 int 2500 nd6_setdefaultiface(int ifindex) 2501 { 2502 int error = 0; 2503 2504 if (ifindex < 0 || V_if_index < ifindex) 2505 return (EINVAL); 2506 if (ifindex != 0 && !ifnet_byindex(ifindex)) 2507 return (EINVAL); 2508 2509 if (V_nd6_defifindex != ifindex) { 2510 V_nd6_defifindex = ifindex; 2511 if (V_nd6_defifindex > 0) 2512 V_nd6_defifp = ifnet_byindex(V_nd6_defifindex); 2513 else 2514 V_nd6_defifp = NULL; 2515 2516 /* 2517 * Our current implementation assumes one-to-one maping between 2518 * interfaces and links, so it would be natural to use the 2519 * default interface as the default link. 2520 */ 2521 scope6_setdefault(V_nd6_defifp); 2522 } 2523 2524 return (error); 2525 } 2526