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