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