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