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