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