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