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