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