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