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