1 /* $OpenBSD: nd6_rtr.c,v 1.59 2012/05/18 10:50:07 mikeb Exp $ */ 2 /* $KAME: nd6_rtr.c,v 1.97 2001/02/07 11:09:13 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/socket.h> 38 #include <sys/sockio.h> 39 #include <sys/time.h> 40 #include <sys/kernel.h> 41 #include <sys/errno.h> 42 #include <sys/ioctl.h> 43 #include <sys/syslog.h> 44 #include <sys/queue.h> 45 #include <sys/workq.h> 46 #include <dev/rndvar.h> 47 48 #include <net/if.h> 49 #include <net/if_types.h> 50 #include <net/if_dl.h> 51 #include <net/route.h> 52 #include <net/radix.h> 53 54 #include <netinet/in.h> 55 #include <netinet6/in6_var.h> 56 #include <netinet/ip6.h> 57 #include <netinet6/ip6_var.h> 58 #include <netinet6/nd6.h> 59 #include <netinet/icmp6.h> 60 61 #include <dev/rndvar.h> 62 63 #define SDL(s) ((struct sockaddr_dl *)s) 64 65 int rtpref(struct nd_defrouter *); 66 struct nd_defrouter *defrtrlist_update(struct nd_defrouter *); 67 struct in6_ifaddr *in6_ifadd(struct nd_prefix *); 68 struct nd_pfxrouter *pfxrtr_lookup(struct nd_prefix *, struct nd_defrouter *); 69 void pfxrtr_add(struct nd_prefix *, struct nd_defrouter *); 70 void pfxrtr_del(struct nd_pfxrouter *); 71 struct nd_pfxrouter *find_pfxlist_reachable_router(struct nd_prefix *); 72 void defrouter_delreq(struct nd_defrouter *); 73 void nd6_rtmsg(int, struct rtentry *); 74 void purge_detached(struct ifnet *); 75 76 void in6_init_address_ltimes(struct nd_prefix *, struct in6_addrlifetime *); 77 78 int rt6_deleteroute(struct radix_node *, void *, u_int); 79 80 void nd6_addr_add(void *, void *); 81 82 extern int nd6_recalc_reachtm_interval; 83 84 static struct ifnet *nd6_defifp; 85 int nd6_defifindex; 86 87 /* 88 * Receive Router Solicitation Message - just for routers. 89 * Router solicitation/advertisement is mostly managed by userland program 90 * (rtadvd) so here we have no function like nd6_ra_output(). 91 * 92 * Based on RFC 2461 93 */ 94 void 95 nd6_rs_input(struct mbuf *m, int off, int icmp6len) 96 { 97 struct ifnet *ifp = m->m_pkthdr.rcvif; 98 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 99 struct nd_router_solicit *nd_rs; 100 struct in6_addr saddr6 = ip6->ip6_src; 101 #if 0 102 struct in6_addr daddr6 = ip6->ip6_dst; 103 #endif 104 char *lladdr = NULL; 105 int lladdrlen = 0; 106 #if 0 107 struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL; 108 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL; 109 struct rtentry *rt = NULL; 110 int is_newentry; 111 #endif 112 union nd_opts ndopts; 113 114 /* If I'm not a router, ignore it. */ 115 if (ip6_accept_rtadv != 0 || !ip6_forwarding) 116 goto freeit; 117 118 /* Sanity checks */ 119 if (ip6->ip6_hlim != 255) { 120 nd6log((LOG_ERR, 121 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 122 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 123 ip6_sprintf(&ip6->ip6_dst), ifp->if_xname)); 124 goto bad; 125 } 126 127 /* 128 * Don't update the neighbor cache, if src = ::. 129 * This indicates that the src has no IP address assigned yet. 130 */ 131 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 132 goto freeit; 133 134 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len); 135 if (nd_rs == NULL) { 136 icmp6stat.icp6s_tooshort++; 137 return; 138 } 139 140 icmp6len -= sizeof(*nd_rs); 141 nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 142 if (nd6_options(&ndopts) < 0) { 143 nd6log((LOG_INFO, 144 "nd6_rs_input: invalid ND option, ignored\n")); 145 /* nd6_options have incremented stats */ 146 goto freeit; 147 } 148 149 if (ndopts.nd_opts_src_lladdr) { 150 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 151 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 152 } 153 154 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 155 nd6log((LOG_INFO, 156 "nd6_rs_input: lladdrlen mismatch for %s " 157 "(if %d, RS packet %d)\n", 158 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2)); 159 goto bad; 160 } 161 162 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0); 163 164 freeit: 165 m_freem(m); 166 return; 167 168 bad: 169 icmp6stat.icp6s_badrs++; 170 m_freem(m); 171 } 172 173 /* 174 * Receive Router Advertisement Message. 175 * 176 * Based on RFC 2461 177 * TODO: on-link bit on prefix information 178 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 179 */ 180 void 181 nd6_ra_input(struct mbuf *m, int off, int icmp6len) 182 { 183 struct ifnet *ifp = m->m_pkthdr.rcvif; 184 struct nd_ifinfo *ndi = ND_IFINFO(ifp); 185 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 186 struct nd_router_advert *nd_ra; 187 struct in6_addr saddr6 = ip6->ip6_src; 188 #if 0 189 struct in6_addr daddr6 = ip6->ip6_dst; 190 int flags; /* = nd_ra->nd_ra_flags_reserved; */ 191 int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0); 192 int is_other = ((flags & ND_RA_FLAG_OTHER) != 0); 193 #endif 194 union nd_opts ndopts; 195 struct nd_defrouter *dr; 196 197 /* 198 * We only accept RAs only when 199 * the system-wide variable allows the acceptance, and 200 * per-interface variable allows RAs on the receiving interface. 201 */ 202 if (ip6_accept_rtadv == 0) 203 goto freeit; 204 if (!(ndi->flags & ND6_IFF_ACCEPT_RTADV)) 205 goto freeit; 206 207 if (ip6->ip6_hlim != 255) { 208 nd6log((LOG_ERR, 209 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 210 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 211 ip6_sprintf(&ip6->ip6_dst), ifp->if_xname)); 212 goto bad; 213 } 214 215 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 216 nd6log((LOG_ERR, 217 "nd6_ra_input: src %s is not link-local\n", 218 ip6_sprintf(&saddr6))); 219 goto bad; 220 } 221 222 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len); 223 if (nd_ra == NULL) { 224 icmp6stat.icp6s_tooshort++; 225 return; 226 } 227 228 icmp6len -= sizeof(*nd_ra); 229 nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 230 if (nd6_options(&ndopts) < 0) { 231 nd6log((LOG_INFO, 232 "nd6_ra_input: invalid ND option, ignored\n")); 233 /* nd6_options have incremented stats */ 234 goto freeit; 235 } 236 237 { 238 struct nd_defrouter dr0; 239 u_int32_t advreachable = nd_ra->nd_ra_reachable; 240 241 Bzero(&dr0, sizeof(dr0)); 242 dr0.rtaddr = saddr6; 243 dr0.flags = nd_ra->nd_ra_flags_reserved; 244 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime); 245 dr0.expire = time_second + dr0.rtlifetime; 246 dr0.ifp = ifp; 247 /* unspecified or not? (RFC 2461 6.3.4) */ 248 if (advreachable) { 249 NTOHL(advreachable); 250 if (advreachable <= MAX_REACHABLE_TIME && 251 ndi->basereachable != advreachable) { 252 ndi->basereachable = advreachable; 253 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable); 254 ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */ 255 } 256 } 257 if (nd_ra->nd_ra_retransmit) 258 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit); 259 if (nd_ra->nd_ra_curhoplimit) 260 ndi->chlim = nd_ra->nd_ra_curhoplimit; 261 dr = defrtrlist_update(&dr0); 262 } 263 264 /* 265 * prefix 266 */ 267 if (ndopts.nd_opts_pi) { 268 struct nd_opt_hdr *pt; 269 struct nd_opt_prefix_info *pi = NULL; 270 struct nd_prefix pr; 271 272 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi; 273 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end; 274 pt = (struct nd_opt_hdr *)((caddr_t)pt + 275 (pt->nd_opt_len << 3))) { 276 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) 277 continue; 278 pi = (struct nd_opt_prefix_info *)pt; 279 280 if (pi->nd_opt_pi_len != 4) { 281 nd6log((LOG_INFO, 282 "nd6_ra_input: invalid option " 283 "len %d for prefix information option, " 284 "ignored\n", pi->nd_opt_pi_len)); 285 continue; 286 } 287 288 if (128 < pi->nd_opt_pi_prefix_len) { 289 nd6log((LOG_INFO, 290 "nd6_ra_input: invalid prefix " 291 "len %d for prefix information option, " 292 "ignored\n", pi->nd_opt_pi_prefix_len)); 293 continue; 294 } 295 296 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) 297 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) { 298 nd6log((LOG_INFO, 299 "nd6_ra_input: invalid prefix " 300 "%s, ignored\n", 301 ip6_sprintf(&pi->nd_opt_pi_prefix))); 302 continue; 303 } 304 305 /* aggregatable unicast address, rfc2374 */ 306 if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20 307 && pi->nd_opt_pi_prefix_len != 64) { 308 nd6log((LOG_INFO, 309 "nd6_ra_input: invalid prefixlen " 310 "%d for rfc2374 prefix %s, ignored\n", 311 pi->nd_opt_pi_prefix_len, 312 ip6_sprintf(&pi->nd_opt_pi_prefix))); 313 continue; 314 } 315 316 bzero(&pr, sizeof(pr)); 317 pr.ndpr_prefix.sin6_family = AF_INET6; 318 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix); 319 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix; 320 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif; 321 322 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved & 323 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0; 324 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved & 325 ND_OPT_PI_FLAG_AUTO) ? 1 : 0; 326 pr.ndpr_plen = pi->nd_opt_pi_prefix_len; 327 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time); 328 pr.ndpr_pltime = ntohl(pi->nd_opt_pi_preferred_time); 329 pr.ndpr_lastupdate = time_second; 330 331 if (in6_init_prefix_ltimes(&pr)) 332 continue; /* prefix lifetime init failed */ 333 334 (void)prelist_update(&pr, dr, m); 335 } 336 } 337 338 /* 339 * MTU 340 */ 341 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) { 342 u_long mtu; 343 u_long maxmtu; 344 345 mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu); 346 347 /* lower bound */ 348 if (mtu < IPV6_MMTU) { 349 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option " 350 "mtu=%lu sent from %s, ignoring\n", 351 mtu, ip6_sprintf(&ip6->ip6_src))); 352 goto skip; 353 } 354 355 /* upper bound */ 356 maxmtu = (ndi->maxmtu && ndi->maxmtu < ifp->if_mtu) 357 ? ndi->maxmtu : ifp->if_mtu; 358 if (mtu <= maxmtu) { 359 int change = (ndi->linkmtu != mtu); 360 361 ndi->linkmtu = mtu; 362 if (change) /* in6_maxmtu may change */ 363 in6_setmaxmtu(); 364 } else { 365 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu " 366 "mtu=%lu sent from %s; " 367 "exceeds maxmtu %lu, ignoring\n", 368 mtu, ip6_sprintf(&ip6->ip6_src), maxmtu)); 369 } 370 } 371 372 skip: 373 374 /* 375 * Source link layer address 376 */ 377 { 378 char *lladdr = NULL; 379 int lladdrlen = 0; 380 381 if (ndopts.nd_opts_src_lladdr) { 382 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 383 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 384 } 385 386 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 387 nd6log((LOG_INFO, 388 "nd6_ra_input: lladdrlen mismatch for %s " 389 "(if %d, RA packet %d)\n", ip6_sprintf(&saddr6), 390 ifp->if_addrlen, lladdrlen - 2)); 391 goto bad; 392 } 393 394 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0); 395 396 /* 397 * Installing a link-layer address might change the state of the 398 * router's neighbor cache, which might also affect our on-link 399 * detection of adveritsed prefixes. 400 */ 401 pfxlist_onlink_check(); 402 } 403 404 freeit: 405 m_freem(m); 406 return; 407 408 bad: 409 icmp6stat.icp6s_badra++; 410 m_freem(m); 411 } 412 413 /* 414 * default router list processing sub routines 415 */ 416 417 /* tell the change to user processes watching the routing socket. */ 418 void 419 nd6_rtmsg(int cmd, struct rtentry *rt) 420 { 421 struct rt_addrinfo info; 422 423 bzero((caddr_t)&info, sizeof(info)); 424 info.rti_info[RTAX_DST] = rt_key(rt); 425 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 426 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 427 if (rt->rt_ifp) { 428 info.rti_info[RTAX_IFP] = 429 TAILQ_FIRST(&rt->rt_ifp->if_addrlist)->ifa_addr; 430 info.rti_info[RTAX_IFA] = rt->rt_ifa->ifa_addr; 431 } 432 433 rt_missmsg(cmd, &info, rt->rt_flags, rt->rt_ifp, 0, 0); 434 } 435 436 void 437 defrouter_addreq(struct nd_defrouter *new) 438 { 439 struct rt_addrinfo info; 440 struct sockaddr_in6 def, mask, gate; 441 struct rtentry *newrt = NULL; 442 int s; 443 int error; 444 445 Bzero(&def, sizeof(def)); 446 Bzero(&mask, sizeof(mask)); 447 Bzero(&gate, sizeof(gate)); /* for safety */ 448 Bzero(&info, sizeof(info)); 449 450 def.sin6_len = mask.sin6_len = gate.sin6_len = 451 sizeof(struct sockaddr_in6); 452 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 453 gate.sin6_addr = new->rtaddr; 454 gate.sin6_scope_id = 0; /* XXX */ 455 456 info.rti_flags = RTF_GATEWAY; 457 info.rti_info[RTAX_DST] = (struct sockaddr *)&def; 458 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gate; 459 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask; 460 461 s = splsoftnet(); 462 error = rtrequest1(RTM_ADD, &info, RTP_DEFAULT, &newrt, 463 new->ifp->if_rdomain); 464 if (newrt) { 465 nd6_rtmsg(RTM_ADD, newrt); /* tell user process */ 466 newrt->rt_refcnt--; 467 } 468 if (error == 0) 469 new->installed = 1; 470 splx(s); 471 return; 472 } 473 474 struct nd_defrouter * 475 defrouter_lookup(struct in6_addr *addr, struct ifnet *ifp) 476 { 477 struct nd_defrouter *dr; 478 479 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) 480 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) 481 return (dr); 482 483 return (NULL); /* search failed */ 484 } 485 486 void 487 defrtrlist_del(struct nd_defrouter *dr) 488 { 489 struct nd_defrouter *deldr = NULL; 490 struct in6_ifextra *ext = dr->ifp->if_afdata[AF_INET6]; 491 struct nd_prefix *pr; 492 493 /* 494 * Flush all the routing table entries that use the router 495 * as a next hop. 496 */ 497 if (!ip6_forwarding && ip6_accept_rtadv) /* XXX: better condition? */ 498 rt6_flush(&dr->rtaddr, dr->ifp); 499 500 if (dr->installed) { 501 deldr = dr; 502 defrouter_delreq(dr); 503 } 504 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 505 506 /* 507 * Also delete all the pointers to the router in each prefix lists. 508 */ 509 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 510 struct nd_pfxrouter *pfxrtr; 511 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) 512 pfxrtr_del(pfxrtr); 513 } 514 pfxlist_onlink_check(); 515 516 /* 517 * If the router is the primary one, choose a new one. 518 * Note that defrouter_select() will remove the current gateway 519 * from the routing table. 520 */ 521 if (deldr) 522 defrouter_select(); 523 524 ext->ndefrouters--; 525 if (ext->ndefrouters < 0) { 526 log(LOG_WARNING, "defrtrlist_del: negative count on %s\n", 527 dr->ifp->if_xname); 528 } 529 530 free(dr, M_IP6NDP); 531 } 532 533 /* 534 * Remove the default route for a given router. 535 * This is just a subroutine function for defrouter_select(), and should 536 * not be called from anywhere else. 537 */ 538 void 539 defrouter_delreq(struct nd_defrouter *dr) 540 { 541 struct rt_addrinfo info; 542 struct sockaddr_in6 def, mask, gw; 543 struct rtentry *oldrt = NULL; 544 545 #ifdef DIAGNOSTIC 546 if (!dr) 547 panic("dr == NULL in defrouter_delreq"); 548 #endif 549 550 Bzero(&info, sizeof(info)); 551 Bzero(&def, sizeof(def)); 552 Bzero(&mask, sizeof(mask)); 553 Bzero(&gw, sizeof(gw)); /* for safety */ 554 555 def.sin6_len = mask.sin6_len = gw.sin6_len = 556 sizeof(struct sockaddr_in6); 557 def.sin6_family = mask.sin6_family = gw.sin6_family = AF_INET6; 558 gw.sin6_addr = dr->rtaddr; 559 gw.sin6_scope_id = 0; /* XXX */ 560 561 info.rti_flags = RTF_GATEWAY; 562 info.rti_info[RTAX_DST] = (struct sockaddr *)&def; 563 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gw; 564 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask; 565 566 rtrequest1(RTM_DELETE, &info, RTP_DEFAULT, &oldrt, 567 dr->ifp->if_rdomain); 568 if (oldrt) { 569 nd6_rtmsg(RTM_DELETE, oldrt); 570 if (oldrt->rt_refcnt <= 0) { 571 /* 572 * XXX: borrowed from the RTM_DELETE case of 573 * rtrequest1(). 574 */ 575 oldrt->rt_refcnt++; 576 rtfree(oldrt); 577 } 578 } 579 580 dr->installed = 0; 581 } 582 583 /* 584 * remove all default routes from default router list 585 */ 586 void 587 defrouter_reset(void) 588 { 589 struct nd_defrouter *dr; 590 591 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) 592 defrouter_delreq(dr); 593 594 /* 595 * XXX should we also nuke any default routers in the kernel, by 596 * going through them by rtalloc1()? 597 */ 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 */ 621 void 622 defrouter_select(void) 623 { 624 int s = splsoftnet(); 625 struct nd_defrouter *dr, *selected_dr = NULL, *installed_dr = NULL; 626 struct rtentry *rt = NULL; 627 struct llinfo_nd6 *ln = NULL; 628 629 /* 630 * This function should be called only when acting as an autoconfigured 631 * host. Although the remaining part of this function is not effective 632 * if the node is not an autoconfigured host, we explicitly exclude 633 * such cases here for safety. 634 */ 635 if (ip6_forwarding || !ip6_accept_rtadv) { 636 nd6log((LOG_WARNING, 637 "defrouter_select: called unexpectedly (forwarding=%d, " 638 "accept_rtadv=%d)\n", ip6_forwarding, ip6_accept_rtadv)); 639 splx(s); 640 return; 641 } 642 643 /* 644 * Let's handle easy case (3) first: 645 * If default router list is empty, there's nothing to be done. 646 */ 647 if (TAILQ_EMPTY(&nd_defrouter)) { 648 splx(s); 649 return; 650 } 651 652 /* 653 * Search for a (probably) reachable router from the list. 654 * We just pick up the first reachable one (if any), assuming that 655 * the ordering rule of the list described in defrtrlist_update(). 656 */ 657 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) { 658 if (!selected_dr && 659 (rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) && 660 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 661 ND6_IS_LLINFO_PROBREACH(ln)) { 662 selected_dr = dr; 663 } 664 665 if (dr->installed && !installed_dr) 666 installed_dr = dr; 667 else if (dr->installed && installed_dr) { 668 /* this should not happen. warn for diagnosis. */ 669 log(LOG_ERR, "defrouter_select: more than one router" 670 " is installed\n"); 671 } 672 } 673 /* 674 * If none of the default routers was found to be reachable, 675 * round-robin the list regardless of preference. 676 * Otherwise, if we have an installed router, check if the selected 677 * (reachable) router should really be preferred to the installed one. 678 * We only prefer the new router when the old one is not reachable 679 * or when the new one has a really higher preference value. 680 */ 681 if (!selected_dr) { 682 if (!installed_dr || !TAILQ_NEXT(installed_dr, dr_entry)) 683 selected_dr = TAILQ_FIRST(&nd_defrouter); 684 else 685 selected_dr = TAILQ_NEXT(installed_dr, dr_entry); 686 } else if (installed_dr && 687 (rt = nd6_lookup(&installed_dr->rtaddr, 0, installed_dr->ifp)) && 688 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 689 ND6_IS_LLINFO_PROBREACH(ln) && 690 rtpref(selected_dr) <= rtpref(installed_dr)) { 691 selected_dr = installed_dr; 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 splx(s); 706 return; 707 } 708 709 /* 710 * for default router selection 711 * regards router-preference field as a 2-bit signed integer 712 */ 713 int 714 rtpref(struct nd_defrouter *dr) 715 { 716 #ifdef RTPREF 717 switch (dr->flags & ND_RA_FLAG_RTPREF_MASK) { 718 case ND_RA_FLAG_RTPREF_HIGH: 719 return RTPREF_HIGH; 720 case ND_RA_FLAG_RTPREF_MEDIUM: 721 case ND_RA_FLAG_RTPREF_RSV: 722 return RTPREF_MEDIUM; 723 case ND_RA_FLAG_RTPREF_LOW: 724 return RTPREF_LOW; 725 default: 726 /* 727 * This case should never happen. If it did, it would mean a 728 * serious bug of kernel internal. We thus always bark here. 729 * Or, can we even panic? 730 */ 731 log(LOG_ERR, "rtpref: impossible RA flag %x", dr->flags); 732 return RTPREF_INVALID; 733 } 734 /* NOTREACHED */ 735 #else 736 return 0; 737 #endif 738 } 739 740 struct nd_defrouter * 741 defrtrlist_update(struct nd_defrouter *new) 742 { 743 struct nd_defrouter *dr, *n; 744 struct in6_ifextra *ext = new->ifp->if_afdata[AF_INET6]; 745 int s = splsoftnet(); 746 747 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) { 748 /* entry exists */ 749 if (new->rtlifetime == 0) { 750 defrtrlist_del(dr); 751 dr = NULL; 752 } else { 753 int oldpref = rtpref(dr); 754 755 /* override */ 756 dr->flags = new->flags; /* xxx flag check */ 757 dr->rtlifetime = new->rtlifetime; 758 dr->expire = new->expire; 759 760 if (!dr->installed) 761 defrouter_select(); 762 763 /* 764 * If the preference does not change, there's no need 765 * to sort the entries. 766 */ 767 if (rtpref(new) == oldpref) { 768 splx(s); 769 return (dr); 770 } 771 772 /* 773 * preferred router may be changed, so relocate 774 * this router. 775 * XXX: calling TAILQ_REMOVE directly is a bad manner. 776 * However, since defrtrlist_del() has many side 777 * effects, we intentionally do so here. 778 * defrouter_select() below will handle routing 779 * changes later. 780 */ 781 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 782 n = dr; 783 goto insert; 784 } 785 splx(s); 786 return (dr); 787 } 788 789 /* entry does not exist */ 790 if (new->rtlifetime == 0) { 791 /* flush all possible redirects */ 792 if (!ip6_forwarding && ip6_accept_rtadv) 793 rt6_flush(&new->rtaddr, new->ifp); 794 splx(s); 795 return (NULL); 796 } 797 798 if (ip6_maxifdefrouters >= 0 && 799 ext->ndefrouters >= ip6_maxifdefrouters) { 800 splx(s); 801 return (NULL); 802 } 803 804 n = malloc(sizeof(*n), M_IP6NDP, M_NOWAIT | M_ZERO); 805 if (n == NULL) { 806 splx(s); 807 return (NULL); 808 } 809 *n = *new; 810 811 insert: 812 /* 813 * Insert the new router in the Default Router List; 814 * The Default Router List should be in the descending order 815 * of router-preference. Routers with the same preference are 816 * sorted in the arriving time order. 817 */ 818 819 /* insert at the end of the group */ 820 TAILQ_FOREACH(dr, &nd_defrouter, dr_entry) 821 if (rtpref(n) > rtpref(dr)) 822 break; 823 if (dr) 824 TAILQ_INSERT_BEFORE(dr, n, dr_entry); 825 else 826 TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry); 827 828 defrouter_select(); 829 830 ext->ndefrouters++; 831 832 splx(s); 833 834 return (n); 835 } 836 837 struct nd_pfxrouter * 838 pfxrtr_lookup(struct nd_prefix *pr, struct nd_defrouter *dr) 839 { 840 struct nd_pfxrouter *search; 841 842 LIST_FOREACH(search, &pr->ndpr_advrtrs, pfr_entry) { 843 if (search->router == dr) 844 break; 845 } 846 847 return (search); 848 } 849 850 void 851 pfxrtr_add(struct nd_prefix *pr, struct nd_defrouter *dr) 852 { 853 struct nd_pfxrouter *new; 854 855 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO); 856 if (new == NULL) 857 return; 858 new->router = dr; 859 860 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry); 861 862 pfxlist_onlink_check(); 863 } 864 865 void 866 pfxrtr_del(struct nd_pfxrouter *pfr) 867 { 868 LIST_REMOVE(pfr, pfr_entry); 869 free(pfr, M_IP6NDP); 870 } 871 872 struct nd_prefix * 873 nd6_prefix_lookup(struct nd_prefix *pr) 874 { 875 struct nd_prefix *search; 876 877 LIST_FOREACH(search, &nd_prefix, ndpr_entry) { 878 if (pr->ndpr_ifp == search->ndpr_ifp && 879 pr->ndpr_plen == search->ndpr_plen && 880 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 881 &search->ndpr_prefix.sin6_addr, pr->ndpr_plen)) { 882 break; 883 } 884 } 885 886 return (search); 887 } 888 889 void 890 purge_detached(struct ifnet *ifp) 891 { 892 struct nd_prefix *pr, *pr_next; 893 struct in6_ifaddr *ia; 894 struct ifaddr *ifa, *ifa_next; 895 896 for (pr = nd_prefix.lh_first; pr; pr = pr_next) { 897 pr_next = pr->ndpr_next; 898 899 /* 900 * This function is called when we need to make more room for 901 * new prefixes rather than keeping old, possibly stale ones. 902 * Detached prefixes would be a good candidate; if all routers 903 * that advertised the prefix expired, the prefix is also 904 * probably stale. 905 */ 906 if (pr->ndpr_ifp != ifp || 907 IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr) || 908 ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 909 !LIST_EMPTY(&pr->ndpr_advrtrs))) 910 continue; 911 912 for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa_next) { 913 ifa_next = ifa->ifa_list.tqe_next; 914 if (ifa->ifa_addr->sa_family != AF_INET6) 915 continue; 916 ia = (struct in6_ifaddr *)ifa; 917 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) == 918 IN6_IFF_AUTOCONF && ia->ia6_ndpr == pr) { 919 in6_purgeaddr(ifa); 920 } 921 } 922 if (pr->ndpr_refcnt == 0) 923 prelist_remove(pr); 924 } 925 } 926 927 int 928 nd6_prelist_add(struct nd_prefix *pr, struct nd_defrouter *dr, 929 struct nd_prefix **newp) 930 { 931 struct nd_prefix *new = NULL; 932 int i, s; 933 struct in6_ifextra *ext = pr->ndpr_ifp->if_afdata[AF_INET6]; 934 935 if (ip6_maxifprefixes >= 0) { 936 if (ext->nprefixes >= ip6_maxifprefixes / 2) 937 purge_detached(pr->ndpr_ifp); 938 if (ext->nprefixes >= ip6_maxifprefixes) 939 return(ENOMEM); 940 } 941 942 new = malloc(sizeof(*new), M_IP6NDP, M_NOWAIT | M_ZERO); 943 if (new == NULL) 944 return ENOMEM; 945 *new = *pr; 946 if (newp != NULL) 947 *newp = new; 948 949 /* initialization */ 950 LIST_INIT(&new->ndpr_advrtrs); 951 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen); 952 /* make prefix in the canonical form */ 953 for (i = 0; i < 4; i++) 954 new->ndpr_prefix.sin6_addr.s6_addr32[i] &= 955 new->ndpr_mask.s6_addr32[i]; 956 957 s = splsoftnet(); 958 /* link ndpr_entry to nd_prefix list */ 959 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry); 960 splx(s); 961 962 /* ND_OPT_PI_FLAG_ONLINK processing */ 963 if (new->ndpr_raf_onlink) { 964 int e; 965 966 if ((e = nd6_prefix_onlink(new)) != 0) { 967 nd6log((LOG_ERR, "nd6_prelist_add: failed to make " 968 "the prefix %s/%d on-link on %s (errno=%d)\n", 969 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 970 pr->ndpr_plen, pr->ndpr_ifp->if_xname, e)); 971 /* proceed anyway. XXX: is it correct? */ 972 } 973 } 974 975 if (dr) 976 pfxrtr_add(new, dr); 977 978 ext->nprefixes++; 979 980 return 0; 981 } 982 983 void 984 prelist_remove(struct nd_prefix *pr) 985 { 986 struct nd_pfxrouter *pfr, *next; 987 int e, s; 988 struct in6_ifextra *ext = pr->ndpr_ifp->if_afdata[AF_INET6]; 989 990 /* make sure to invalidate the prefix until it is really freed. */ 991 pr->ndpr_vltime = 0; 992 pr->ndpr_pltime = 0; 993 #if 0 994 /* 995 * Though these flags are now meaningless, we'd rather keep the value 996 * not to confuse users when executing "ndp -p". 997 */ 998 pr->ndpr_raf_onlink = 0; 999 pr->ndpr_raf_auto = 0; 1000 #endif 1001 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0 && 1002 (e = nd6_prefix_offlink(pr)) != 0) { 1003 nd6log((LOG_ERR, "prelist_remove: failed to make %s/%d offlink " 1004 "on %s, errno=%d\n", 1005 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1006 pr->ndpr_plen, pr->ndpr_ifp->if_xname, e)); 1007 /* what should we do? */ 1008 } 1009 1010 if (pr->ndpr_refcnt > 0) 1011 return; /* notice here? */ 1012 1013 s = splsoftnet(); 1014 1015 /* unlink ndpr_entry from nd_prefix list */ 1016 LIST_REMOVE(pr, ndpr_entry); 1017 1018 /* free list of routers that adversed the prefix */ 1019 for (pfr = LIST_FIRST(&pr->ndpr_advrtrs); pfr != NULL; pfr = next) { 1020 next = LIST_NEXT(pfr, pfr_entry); 1021 1022 free(pfr, M_IP6NDP); 1023 } 1024 1025 ext->nprefixes--; 1026 if (ext->nprefixes < 0) { 1027 log(LOG_WARNING, "prelist_remove: negative count on %s\n", 1028 pr->ndpr_ifp->if_xname); 1029 } 1030 splx(s); 1031 1032 free(pr, M_IP6NDP); 1033 1034 pfxlist_onlink_check(); 1035 } 1036 1037 /* 1038 * dr - may be NULL 1039 */ 1040 1041 int 1042 prelist_update(struct nd_prefix *new, struct nd_defrouter *dr, struct mbuf *m) 1043 { 1044 struct in6_ifaddr *ia6_match = NULL; 1045 struct ifaddr *ifa; 1046 struct ifnet *ifp = new->ndpr_ifp; 1047 struct nd_prefix *pr; 1048 int s = splsoftnet(); 1049 int error = 0; 1050 int tempaddr_preferred = 0; 1051 int auth; 1052 struct in6_addrlifetime lt6_tmp; 1053 1054 auth = 0; 1055 if (m) { 1056 /* 1057 * Authenticity for NA consists authentication for 1058 * both IP header and IP datagrams, doesn't it ? 1059 */ 1060 auth = ((m->m_flags & M_AUTH_AH) && (m->m_flags & M_AUTH)); 1061 } 1062 1063 if ((pr = nd6_prefix_lookup(new)) != NULL) { 1064 /* 1065 * nd6_prefix_lookup() ensures that pr and new have the same 1066 * prefix on a same interface. 1067 */ 1068 1069 /* 1070 * Update prefix information. Note that the on-link (L) bit 1071 * and the autonomous (A) bit should NOT be changed from 1 1072 * to 0. 1073 */ 1074 if (new->ndpr_raf_onlink == 1) 1075 pr->ndpr_raf_onlink = 1; 1076 if (new->ndpr_raf_auto == 1) 1077 pr->ndpr_raf_auto = 1; 1078 if (new->ndpr_raf_onlink) { 1079 pr->ndpr_vltime = new->ndpr_vltime; 1080 pr->ndpr_pltime = new->ndpr_pltime; 1081 pr->ndpr_preferred = new->ndpr_preferred; 1082 pr->ndpr_expire = new->ndpr_expire; 1083 pr->ndpr_lastupdate = new->ndpr_lastupdate; 1084 } 1085 1086 if (new->ndpr_raf_onlink && 1087 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1088 int e; 1089 1090 if ((e = nd6_prefix_onlink(pr)) != 0) { 1091 nd6log((LOG_ERR, 1092 "prelist_update: failed to make " 1093 "the prefix %s/%d on-link on %s " 1094 "(errno=%d)\n", 1095 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1096 pr->ndpr_plen, pr->ndpr_ifp->if_xname, e)); 1097 /* proceed anyway. XXX: is it correct? */ 1098 } 1099 } 1100 1101 if (dr && pfxrtr_lookup(pr, dr) == NULL) 1102 pfxrtr_add(pr, dr); 1103 } else { 1104 struct nd_prefix *newpr = NULL; 1105 1106 if (new->ndpr_vltime == 0) 1107 goto end; 1108 if (new->ndpr_raf_onlink == 0 && new->ndpr_raf_auto == 0) 1109 goto end; 1110 1111 error = nd6_prelist_add(new, dr, &newpr); 1112 if (error != 0 || newpr == NULL) { 1113 nd6log((LOG_NOTICE, "prelist_update: " 1114 "nd6_prelist_add failed for %s/%d on %s " 1115 "errno=%d, returnpr=%p\n", 1116 ip6_sprintf(&new->ndpr_prefix.sin6_addr), 1117 new->ndpr_plen, new->ndpr_ifp->if_xname, 1118 error, newpr)); 1119 goto end; /* we should just give up in this case. */ 1120 } 1121 1122 /* 1123 * XXX: from the ND point of view, we can ignore a prefix 1124 * with the on-link bit being zero. However, we need a 1125 * prefix structure for references from autoconfigured 1126 * addresses. Thus, we explicitly make sure that the prefix 1127 * itself expires now. 1128 */ 1129 if (newpr->ndpr_raf_onlink == 0) { 1130 newpr->ndpr_vltime = 0; 1131 newpr->ndpr_pltime = 0; 1132 in6_init_prefix_ltimes(newpr); 1133 } 1134 1135 pr = newpr; 1136 } 1137 1138 /* 1139 * Address autoconfiguration based on Section 5.5.3 of RFC 2462. 1140 * Note that pr must be non NULL at this point. 1141 */ 1142 1143 /* 5.5.3 (a). Ignore the prefix without the A bit set. */ 1144 if (!new->ndpr_raf_auto) 1145 goto end; 1146 1147 /* 1148 * 5.5.3 (b). the link-local prefix should have been ignored in 1149 * nd6_ra_input. 1150 */ 1151 1152 /* 1153 * 5.5.3 (c). Consistency check on lifetimes: pltime <= vltime. 1154 * This should have been done in nd6_ra_input. 1155 */ 1156 1157 /* 1158 * 5.5.3 (d). If the prefix advertised does not match the prefix of an 1159 * address already in the list, and the Valid Lifetime is not 0, 1160 * form an address. Note that even a manually configured address 1161 * should reject autoconfiguration of a new address. 1162 */ 1163 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1164 struct in6_ifaddr *ifa6; 1165 int ifa_plen; 1166 u_int32_t storedlifetime; 1167 1168 if (ifa->ifa_addr->sa_family != AF_INET6) 1169 continue; 1170 1171 ifa6 = (struct in6_ifaddr *)ifa; 1172 1173 /* 1174 * Spec is not clear here, but I believe we should concentrate 1175 * on unicast (i.e. not anycast) addresses. 1176 * XXX: other ia6_flags? detached or duplicated? 1177 */ 1178 if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0) 1179 continue; 1180 1181 ifa_plen = in6_mask2len(&ifa6->ia_prefixmask.sin6_addr, NULL); 1182 if (ifa_plen != new->ndpr_plen || 1183 !in6_are_prefix_equal(&ifa6->ia_addr.sin6_addr, 1184 &new->ndpr_prefix.sin6_addr, ifa_plen)) 1185 continue; 1186 1187 if (ia6_match == NULL) /* remember the first one */ 1188 ia6_match = ifa6; 1189 1190 if ((ifa6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1191 continue; 1192 1193 /* 1194 * An already autoconfigured address matched. Now that we 1195 * are sure there is at least one matched address, we can 1196 * proceed to 5.5.3. (e): update the lifetimes according to the 1197 * "two hours" rule and the privacy extension. 1198 */ 1199 #define TWOHOUR (120*60) 1200 /* 1201 * RFC2462 introduces the notion of StoredLifetime to the 1202 * "two hours" rule as follows: 1203 * the Lifetime associated with the previously autoconfigured 1204 * address. 1205 * Our interpretation of this definition is "the remaining 1206 * lifetime to expiration at the evaluation time". One might 1207 * be wondering if this interpretation is really conform to the 1208 * RFC, because the text can read that "Lifetimes" are never 1209 * decreased, and our definition of the "storedlifetime" below 1210 * essentially reduces the "Valid Lifetime" advertised in the 1211 * previous RA. But, this is due to the wording of the text, 1212 * and our interpretation is the same as an author's intention. 1213 * See the discussion in the IETF ipngwg ML in August 2001, 1214 * with the Subject "StoredLifetime in RFC 2462". 1215 */ 1216 lt6_tmp = ifa6->ia6_lifetime; 1217 1218 /* RFC 4941 temporary addresses (privacy extension). */ 1219 if (ifa6->ia6_flags & IN6_IFF_PRIVACY) { 1220 /* Do we still have a non-deprecated address? */ 1221 if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0) 1222 tempaddr_preferred = 1; 1223 /* Don't extend lifetime for temporary addresses. */ 1224 if (new->ndpr_vltime >= lt6_tmp.ia6t_vltime) 1225 continue; 1226 if (new->ndpr_pltime >= lt6_tmp.ia6t_pltime) 1227 continue; 1228 } 1229 if (lt6_tmp.ia6t_vltime == ND6_INFINITE_LIFETIME) 1230 storedlifetime = ND6_INFINITE_LIFETIME; 1231 else if (time_second - ifa6->ia6_updatetime > 1232 lt6_tmp.ia6t_vltime) { 1233 /* 1234 * The case of "invalid" address. We should usually 1235 * not see this case. 1236 */ 1237 storedlifetime = 0; 1238 } else 1239 storedlifetime = lt6_tmp.ia6t_vltime - 1240 (time_second - ifa6->ia6_updatetime); 1241 if (TWOHOUR < new->ndpr_vltime || 1242 storedlifetime < new->ndpr_vltime) { 1243 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1244 } else if (storedlifetime <= TWOHOUR 1245 #if 0 1246 /* 1247 * This condition is logically redundant, so we just 1248 * omit it. 1249 * See IPng 6712, 6717, and 6721. 1250 */ 1251 && new->ndpr_vltime <= storedlifetime 1252 #endif 1253 ) { 1254 if (auth) { 1255 lt6_tmp.ia6t_vltime = new->ndpr_vltime; 1256 } 1257 } else { 1258 /* 1259 * new->ndpr_vltime <= TWOHOUR && 1260 * TWOHOUR < storedlifetime 1261 */ 1262 lt6_tmp.ia6t_vltime = TWOHOUR; 1263 } 1264 1265 /* The 2 hour rule is not imposed for preferred lifetime. */ 1266 lt6_tmp.ia6t_pltime = new->ndpr_pltime; 1267 1268 in6_init_address_ltimes(pr, <6_tmp); 1269 1270 ifa6->ia6_lifetime = lt6_tmp; 1271 ifa6->ia6_updatetime = time_second; 1272 } 1273 1274 if ((ia6_match == NULL || 1275 (((ia6_match->ia6_flags & IN6_IFF_PRIVACY) || 1276 (ifp->if_xflags & IFXF_INET6_PRIVACY)) && !tempaddr_preferred)) && 1277 new->ndpr_vltime) { 1278 /* 1279 * No address matched, or there is no preferred RFC 4941 1280 * temporary address. And the valid prefix lifetime is non-zero. 1281 * Create a new address in process context. 1282 */ 1283 pr->ndpr_refcnt++; 1284 if (workq_add_task(NULL, 0, nd6_addr_add, pr, NULL)) 1285 pr->ndpr_refcnt--; 1286 } 1287 1288 end: 1289 splx(s); 1290 return error; 1291 } 1292 1293 void 1294 nd6_addr_add(void *prptr, void *arg2) 1295 { 1296 struct nd_prefix *pr = (struct nd_prefix *)prptr; 1297 struct in6_ifaddr *ia6 = NULL; 1298 struct ifaddr *ifa; 1299 int ifa_plen; 1300 1301 /* Because prelist_update() runs in interrupt context it may run 1302 * again before this work queue task is run, causing multiple work 1303 * queue tasks to be scheduled all of which add addresses for the 1304 * same prefix. So check again if a non-deprecated address has already 1305 * been autoconfigured for this prefix. */ 1306 TAILQ_FOREACH(ifa, &pr->ndpr_ifp->if_addrlist, ifa_list) { 1307 if (ifa->ifa_addr->sa_family != AF_INET6) 1308 continue; 1309 1310 ia6 = (struct in6_ifaddr *)ifa; 1311 1312 /* 1313 * Spec is not clear here, but I believe we should concentrate 1314 * on unicast (i.e. not anycast) addresses. 1315 * XXX: other ia6_flags? detached or duplicated? 1316 */ 1317 if ((ia6->ia6_flags & IN6_IFF_ANYCAST) != 0) 1318 continue; 1319 1320 if ((ia6->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1321 continue; 1322 1323 if ((ia6->ia6_flags & IN6_IFF_DEPRECATED) != 0) 1324 continue; 1325 1326 ifa_plen = in6_mask2len(&ia6->ia_prefixmask.sin6_addr, NULL); 1327 if (ifa_plen == pr->ndpr_plen && 1328 in6_are_prefix_equal(&ia6->ia_addr.sin6_addr, 1329 &pr->ndpr_prefix.sin6_addr, ifa_plen)) { 1330 pr->ndpr_refcnt--; 1331 return; 1332 } 1333 } 1334 1335 if ((ia6 = in6_ifadd(pr)) != NULL) { 1336 ia6->ia6_ndpr = pr; 1337 1338 /* 1339 * A newly added address might affect the status 1340 * of other addresses, so we check and update it. 1341 * XXX: what if address duplication happens? 1342 */ 1343 pfxlist_onlink_check(); 1344 } else 1345 pr->ndpr_refcnt--; 1346 } 1347 1348 /* 1349 * A supplement function used in the on-link detection below; 1350 * detect if a given prefix has a (probably) reachable advertising router. 1351 * XXX: lengthy function name... 1352 */ 1353 struct nd_pfxrouter * 1354 find_pfxlist_reachable_router(struct nd_prefix *pr) 1355 { 1356 struct nd_pfxrouter *pfxrtr; 1357 struct rtentry *rt; 1358 struct llinfo_nd6 *ln; 1359 1360 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr; 1361 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) { 1362 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0, 1363 pfxrtr->router->ifp)) && 1364 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 1365 ND6_IS_LLINFO_PROBREACH(ln)) 1366 break; /* found */ 1367 } 1368 1369 return (pfxrtr); 1370 } 1371 1372 /* 1373 * Check if each prefix in the prefix list has at least one available router 1374 * that advertised the prefix (a router is "available" if its neighbor cache 1375 * entry is reachable or probably reachable). 1376 * If the check fails, the prefix may be off-link, because, for example, 1377 * we have moved from the network but the lifetime of the prefix has not 1378 * expired yet. So we should not use the prefix if there is another prefix 1379 * that has an available router. 1380 * But, if there is no prefix that has an available router, we still regards 1381 * all the prefixes as on-link. This is because we can't tell if all the 1382 * routers are simply dead or if we really moved from the network and there 1383 * is no router around us. 1384 */ 1385 void 1386 pfxlist_onlink_check(void) 1387 { 1388 struct nd_prefix *pr; 1389 struct in6_ifaddr *ifa; 1390 1391 /* 1392 * Check if there is a prefix that has a reachable advertising 1393 * router. 1394 */ 1395 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 1396 if (pr->ndpr_raf_onlink && find_pfxlist_reachable_router(pr)) 1397 break; 1398 } 1399 if (pr != NULL || !TAILQ_EMPTY(&nd_defrouter)) { 1400 /* 1401 * There is at least one prefix that has a reachable router, 1402 * or at least a router which probably does not advertise 1403 * any prefixes. The latter would be the case when we move 1404 * to a new link where we have a router that does not provide 1405 * prefixes and we configure an address by hand. 1406 * Detach prefixes which have no reachable advertising 1407 * router, and attach other prefixes. 1408 */ 1409 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 1410 /* XXX: a link-local prefix should never be detached */ 1411 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1412 continue; 1413 1414 /* 1415 * we aren't interested in prefixes without the L bit 1416 * set. 1417 */ 1418 if (pr->ndpr_raf_onlink == 0) 1419 continue; 1420 1421 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1422 find_pfxlist_reachable_router(pr) == NULL) 1423 pr->ndpr_stateflags |= NDPRF_DETACHED; 1424 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1425 find_pfxlist_reachable_router(pr) != 0) 1426 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1427 } 1428 } else { 1429 /* there is no prefix that has a reachable router */ 1430 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 1431 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1432 continue; 1433 1434 if (pr->ndpr_raf_onlink == 0) 1435 continue; 1436 1437 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1438 pr->ndpr_stateflags &= ~NDPRF_DETACHED; 1439 } 1440 } 1441 1442 /* 1443 * Remove each interface route associated with a (just) detached 1444 * prefix, and reinstall the interface route for a (just) attached 1445 * prefix. Note that all attempt of reinstallation does not 1446 * necessarily success, when a same prefix is shared among multiple 1447 * interfaces. Such cases will be handled in nd6_prefix_onlink, 1448 * so we don't have to care about them. 1449 */ 1450 LIST_FOREACH(pr, &nd_prefix, ndpr_entry) { 1451 int e; 1452 1453 if (IN6_IS_ADDR_LINKLOCAL(&pr->ndpr_prefix.sin6_addr)) 1454 continue; 1455 1456 if (pr->ndpr_raf_onlink == 0) 1457 continue; 1458 1459 if ((pr->ndpr_stateflags & NDPRF_DETACHED) != 0 && 1460 (pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1461 if ((e = nd6_prefix_offlink(pr)) != 0) { 1462 nd6log((LOG_ERR, 1463 "pfxlist_onlink_check: failed to " 1464 "make %s/%d offlink, errno=%d\n", 1465 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1466 pr->ndpr_plen, e)); 1467 } 1468 } 1469 if ((pr->ndpr_stateflags & NDPRF_DETACHED) == 0 && 1470 (pr->ndpr_stateflags & NDPRF_ONLINK) == 0 && 1471 pr->ndpr_raf_onlink) { 1472 if ((e = nd6_prefix_onlink(pr)) != 0) { 1473 nd6log((LOG_ERR, 1474 "pfxlist_onlink_check: failed to " 1475 "make %s/%d offlink, errno=%d\n", 1476 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1477 pr->ndpr_plen, e)); 1478 } 1479 } 1480 } 1481 1482 /* 1483 * Changes on the prefix status might affect address status as well. 1484 * Make sure that all addresses derived from an attached prefix are 1485 * attached, and that all addresses derived from a detached prefix are 1486 * detached. Note, however, that a manually configured address should 1487 * always be attached. 1488 * The precise detection logic is same as the one for prefixes. 1489 */ 1490 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 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 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 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 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1516 else 1517 ifa->ia6_flags |= IN6_IFF_DETACHED; 1518 } 1519 } 1520 else { 1521 for (ifa = in6_ifaddr; ifa; ifa = ifa->ia_next) { 1522 if ((ifa->ia6_flags & IN6_IFF_AUTOCONF) == 0) 1523 continue; 1524 1525 ifa->ia6_flags &= ~IN6_IFF_DETACHED; 1526 } 1527 } 1528 } 1529 1530 int 1531 nd6_prefix_onlink(struct nd_prefix *pr) 1532 { 1533 struct rt_addrinfo info; 1534 struct ifaddr *ifa; 1535 struct ifnet *ifp = pr->ndpr_ifp; 1536 struct sockaddr_in6 mask6; 1537 struct nd_prefix *opr; 1538 u_long rtflags; 1539 int error = 0; 1540 struct rtentry *rt = NULL; 1541 1542 /* sanity check */ 1543 if ((pr->ndpr_stateflags & NDPRF_ONLINK) != 0) { 1544 nd6log((LOG_ERR, 1545 "nd6_prefix_onlink: %s/%d is already on-link\n", 1546 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen)); 1547 return (EEXIST); 1548 } 1549 1550 /* 1551 * Add the interface route associated with the prefix. Before 1552 * installing the route, check if there's the same prefix on another 1553 * interface, and the prefix has already installed the interface route. 1554 * Although such a configuration is expected to be rare, we explicitly 1555 * allow it. 1556 */ 1557 LIST_FOREACH(opr, &nd_prefix, ndpr_entry) { 1558 if (opr == pr) 1559 continue; 1560 1561 if ((opr->ndpr_stateflags & NDPRF_ONLINK) == 0) 1562 continue; 1563 1564 if (opr->ndpr_plen == pr->ndpr_plen && 1565 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1566 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) 1567 return (0); 1568 } 1569 1570 /* 1571 * We prefer link-local addresses as the associated interface address. 1572 */ 1573 /* search for a link-local addr */ 1574 ifa = &in6ifa_ifpforlinklocal(ifp, 1575 IN6_IFF_NOTREADY | IN6_IFF_ANYCAST)->ia_ifa; 1576 if (ifa == NULL) { 1577 /* XXX: freebsd does not have ifa_ifwithaf */ 1578 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) { 1579 if (ifa->ifa_addr->sa_family == AF_INET6) 1580 break; 1581 } 1582 /* should we care about ia6_flags? */ 1583 } 1584 if (ifa == NULL) { 1585 /* 1586 * This can still happen, when, for example, we receive an RA 1587 * containing a prefix with the L bit set and the A bit clear, 1588 * after removing all IPv6 addresses on the receiving 1589 * interface. This should, of course, be rare though. 1590 */ 1591 nd6log((LOG_NOTICE, 1592 "nd6_prefix_onlink: failed to find any ifaddr" 1593 " to add route for a prefix(%s/%d) on %s\n", 1594 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1595 pr->ndpr_plen, ifp->if_xname)); 1596 return (0); 1597 } 1598 1599 /* 1600 * in6_ifinit() sets nd6_rtrequest to ifa_rtrequest for all ifaddrs. 1601 * ifa->ifa_rtrequest = nd6_rtrequest; 1602 */ 1603 bzero(&mask6, sizeof(mask6)); 1604 mask6.sin6_len = sizeof(mask6); 1605 mask6.sin6_addr = pr->ndpr_mask; 1606 /* rtrequest1() will probably set RTF_UP, but we're not sure. */ 1607 rtflags = ifa->ifa_flags | RTF_UP; 1608 if (nd6_need_cache(ifp)) { 1609 /* explicitly set in case ifa_flags does not set the flag. */ 1610 rtflags |= RTF_CLONING; 1611 } else { 1612 /* 1613 * explicitly clear the cloning bit in case ifa_flags sets it. 1614 */ 1615 rtflags &= ~RTF_CLONING; 1616 } 1617 1618 bzero(&info, sizeof(info)); 1619 info.rti_flags = rtflags; 1620 info.rti_info[RTAX_DST] = (struct sockaddr *)&pr->ndpr_prefix; 1621 info.rti_info[RTAX_GATEWAY] = ifa->ifa_addr; 1622 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask6; 1623 1624 error = rtrequest1(RTM_ADD, &info, RTP_CONNECTED, &rt, ifp->if_rdomain); 1625 if (error == 0) { 1626 if (rt != NULL) /* this should be non NULL, though */ 1627 nd6_rtmsg(RTM_ADD, rt); 1628 pr->ndpr_stateflags |= NDPRF_ONLINK; 1629 } else { 1630 nd6log((LOG_ERR, "nd6_prefix_onlink: failed to add route for a" 1631 " prefix (%s/%d) on %s, gw=%s, mask=%s, flags=%lx " 1632 "errno = %d\n", 1633 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), 1634 pr->ndpr_plen, ifp->if_xname, 1635 ip6_sprintf(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr), 1636 ip6_sprintf(&mask6.sin6_addr), rtflags, error)); 1637 } 1638 1639 if (rt != NULL) 1640 rt->rt_refcnt--; 1641 1642 return (error); 1643 } 1644 1645 int 1646 nd6_prefix_offlink(struct nd_prefix *pr) 1647 { 1648 struct rt_addrinfo info; 1649 int error = 0; 1650 struct ifnet *ifp = pr->ndpr_ifp; 1651 struct nd_prefix *opr; 1652 struct sockaddr_in6 sa6, mask6; 1653 struct rtentry *rt = NULL; 1654 1655 /* sanity check */ 1656 if ((pr->ndpr_stateflags & NDPRF_ONLINK) == 0) { 1657 nd6log((LOG_ERR, 1658 "nd6_prefix_offlink: %s/%d is already off-link\n", 1659 ip6_sprintf(&pr->ndpr_prefix.sin6_addr), pr->ndpr_plen)); 1660 return (EEXIST); 1661 } 1662 1663 bzero(&sa6, sizeof(sa6)); 1664 sa6.sin6_family = AF_INET6; 1665 sa6.sin6_len = sizeof(sa6); 1666 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr, 1667 sizeof(struct in6_addr)); 1668 bzero(&mask6, sizeof(mask6)); 1669 mask6.sin6_family = AF_INET6; 1670 mask6.sin6_len = sizeof(sa6); 1671 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr)); 1672 bzero(&info, sizeof(info)); 1673 info.rti_info[RTAX_DST] = (struct sockaddr *)&sa6; 1674 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&mask6; 1675 error = rtrequest1(RTM_DELETE, &info, RTP_CONNECTED, &rt, 1676 ifp->if_rdomain); 1677 if (error == 0) { 1678 pr->ndpr_stateflags &= ~NDPRF_ONLINK; 1679 1680 /* report the route deletion to the routing socket. */ 1681 if (rt != NULL) 1682 nd6_rtmsg(RTM_DELETE, rt); 1683 1684 /* 1685 * There might be the same prefix on another interface, 1686 * the prefix which could not be on-link just because we have 1687 * the interface route (see comments in nd6_prefix_onlink). 1688 * If there's one, try to make the prefix on-link on the 1689 * interface. 1690 */ 1691 LIST_FOREACH(opr, &nd_prefix, ndpr_entry) { 1692 if (opr == pr) 1693 continue; 1694 1695 if ((opr->ndpr_stateflags & NDPRF_ONLINK) != 0) 1696 continue; 1697 1698 /* 1699 * KAME specific: detached prefixes should not be 1700 * on-link. 1701 */ 1702 if ((opr->ndpr_stateflags & NDPRF_DETACHED) != 0) 1703 continue; 1704 1705 if (opr->ndpr_plen == pr->ndpr_plen && 1706 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 1707 &opr->ndpr_prefix.sin6_addr, pr->ndpr_plen)) { 1708 int e; 1709 1710 if ((e = nd6_prefix_onlink(opr)) != 0) { 1711 nd6log((LOG_ERR, 1712 "nd6_prefix_offlink: failed to " 1713 "recover a prefix %s/%d from %s " 1714 "to %s (errno = %d)\n", 1715 ip6_sprintf(&opr->ndpr_prefix.sin6_addr), 1716 opr->ndpr_plen, ifp->if_xname, 1717 opr->ndpr_ifp->if_xname, e)); 1718 } 1719 } 1720 } 1721 } else { 1722 /* XXX: can we still set the NDPRF_ONLINK flag? */ 1723 nd6log((LOG_ERR, 1724 "nd6_prefix_offlink: failed to delete route: " 1725 "%s/%d on %s (errno = %d)\n", 1726 ip6_sprintf(&sa6.sin6_addr), pr->ndpr_plen, ifp->if_xname, 1727 error)); 1728 } 1729 1730 if (rt != NULL) { 1731 if (rt->rt_refcnt <= 0) { 1732 /* XXX: we should free the entry ourselves. */ 1733 rt->rt_refcnt++; 1734 rtfree(rt); 1735 } 1736 } 1737 1738 return (error); 1739 } 1740 1741 struct in6_ifaddr * 1742 in6_ifadd(struct nd_prefix *pr) 1743 { 1744 struct ifnet *ifp = pr->ndpr_ifp; 1745 struct ifaddr *ifa; 1746 struct in6_aliasreq ifra; 1747 struct in6_ifaddr *ia, *ib; 1748 int error, s, plen0; 1749 struct in6_addr mask, rand_ifid; 1750 int prefixlen = pr->ndpr_plen; 1751 1752 in6_prefixlen2mask(&mask, prefixlen); 1753 1754 /* 1755 * find a link-local address (will be interface ID). 1756 * Is it really mandatory? Theoretically, a global or a site-local 1757 * address can be configured without a link-local address, if we 1758 * have a unique interface identifier... 1759 * 1760 * it is not mandatory to have a link-local address, we can generate 1761 * interface identifier on the fly. we do this because: 1762 * (1) it should be the easiest way to find interface identifier. 1763 * (2) RFC2462 5.4 suggesting the use of the same interface identifier 1764 * for multiple addresses on a single interface, and possible shortcut 1765 * of DAD. we omitted DAD for this reason in the past. 1766 * (3) a user can prevent autoconfiguration of global address 1767 * by removing link-local address by hand (this is partly because we 1768 * don't have other way to control the use of IPv6 on a interface. 1769 * this has been our design choice - cf. NRL's "ifconfig auto"). 1770 * (4) it is easier to manage when an interface has addresses 1771 * with the same interface identifier, than to have multiple addresses 1772 * with different interface identifiers. 1773 */ 1774 ifa = &in6ifa_ifpforlinklocal(ifp, 0)->ia_ifa; /* 0 is OK? */ 1775 if (ifa) 1776 ib = (struct in6_ifaddr *)ifa; 1777 else 1778 return NULL; 1779 1780 #if 0 /* don't care link local addr state, and always do DAD */ 1781 /* if link-local address is not eligible, do not autoconfigure. */ 1782 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) { 1783 printf("in6_ifadd: link-local address not ready\n"); 1784 return NULL; 1785 } 1786 #endif 1787 1788 /* prefixlen + ifidlen must be equal to 128 */ 1789 plen0 = in6_mask2len(&ib->ia_prefixmask.sin6_addr, NULL); 1790 if (prefixlen != plen0) { 1791 nd6log((LOG_INFO, "in6_ifadd: wrong prefixlen for %s " 1792 "(prefix=%d ifid=%d)\n", 1793 ifp->if_xname, prefixlen, 128 - plen0)); 1794 return NULL; 1795 } 1796 1797 /* make ifaddr */ 1798 1799 bzero(&ifra, sizeof(ifra)); 1800 /* 1801 * in6_update_ifa() does not use ifra_name, but we accurately set it 1802 * for safety. 1803 */ 1804 strncpy(ifra.ifra_name, ifp->if_xname, sizeof(ifra.ifra_name)); 1805 ifra.ifra_addr.sin6_family = AF_INET6; 1806 ifra.ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 1807 /* prefix */ 1808 bcopy(&pr->ndpr_prefix.sin6_addr, &ifra.ifra_addr.sin6_addr, 1809 sizeof(ifra.ifra_addr.sin6_addr)); 1810 ifra.ifra_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0]; 1811 ifra.ifra_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1]; 1812 ifra.ifra_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2]; 1813 ifra.ifra_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3]; 1814 1815 /* interface ID */ 1816 if (ifp->if_xflags & IFXF_INET6_PRIVACY) { 1817 ifra.ifra_flags |= IN6_IFF_PRIVACY; 1818 bcopy(&pr->ndpr_prefix.sin6_addr, &rand_ifid, 1819 sizeof(rand_ifid)); 1820 in6_get_rand_ifid(ifp, &rand_ifid); 1821 ifra.ifra_addr.sin6_addr.s6_addr32[0] |= 1822 (rand_ifid.s6_addr32[0] & ~mask.s6_addr32[0]); 1823 ifra.ifra_addr.sin6_addr.s6_addr32[1] |= 1824 (rand_ifid.s6_addr32[1] & ~mask.s6_addr32[1]); 1825 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 1826 (rand_ifid.s6_addr32[2] & ~mask.s6_addr32[2]); 1827 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 1828 (rand_ifid.s6_addr32[3] & ~mask.s6_addr32[3]); 1829 } else { 1830 ifra.ifra_addr.sin6_addr.s6_addr32[0] |= 1831 (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]); 1832 ifra.ifra_addr.sin6_addr.s6_addr32[1] |= 1833 (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]); 1834 ifra.ifra_addr.sin6_addr.s6_addr32[2] |= 1835 (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]); 1836 ifra.ifra_addr.sin6_addr.s6_addr32[3] |= 1837 (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]); 1838 } 1839 1840 /* new prefix mask. */ 1841 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1842 ifra.ifra_prefixmask.sin6_family = AF_INET6; 1843 bcopy(&mask, &ifra.ifra_prefixmask.sin6_addr, 1844 sizeof(ifra.ifra_prefixmask.sin6_addr)); 1845 1846 /* 1847 * lifetime. 1848 * XXX: in6_init_address_ltimes would override these values later. 1849 * We should reconsider this logic. 1850 */ 1851 ifra.ifra_lifetime.ia6t_vltime = pr->ndpr_vltime; 1852 ifra.ifra_lifetime.ia6t_pltime = pr->ndpr_pltime; 1853 1854 if (ifp->if_xflags & IFXF_INET6_PRIVACY) { 1855 if (ifra.ifra_lifetime.ia6t_vltime > ND6_PRIV_VALID_LIFETIME) 1856 ifra.ifra_lifetime.ia6t_vltime = ND6_PRIV_VALID_LIFETIME; 1857 if (ifra.ifra_lifetime.ia6t_pltime > ND6_PRIV_PREFERRED_LIFETIME) 1858 ifra.ifra_lifetime.ia6t_pltime = ND6_PRIV_PREFERRED_LIFETIME 1859 - (arc4random() % ND6_PRIV_MAX_DESYNC_FACTOR); 1860 } 1861 1862 /* XXX: scope zone ID? */ 1863 1864 ifra.ifra_flags |= IN6_IFF_AUTOCONF; /* obey autoconf */ 1865 1866 /* allocate ifaddr structure, link into chain, etc. */ 1867 s = splsoftnet(); 1868 error = in6_update_ifa(ifp, &ifra, NULL); 1869 splx(s); 1870 1871 if (error != 0) { 1872 nd6log((LOG_ERR, 1873 "in6_ifadd: failed to make ifaddr %s on %s (errno=%d)\n", 1874 ip6_sprintf(&ifra.ifra_addr.sin6_addr), ifp->if_xname, 1875 error)); 1876 return (NULL); /* ifaddr must not have been allocated. */ 1877 } 1878 1879 ia = in6ifa_ifpwithaddr(ifp, &ifra.ifra_addr.sin6_addr); 1880 1881 return (ia); /* this is always non-NULL */ 1882 } 1883 1884 int 1885 in6_init_prefix_ltimes(struct nd_prefix *ndpr) 1886 { 1887 1888 /* check if preferred lifetime > valid lifetime. RFC2462 5.5.3 (c) */ 1889 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) { 1890 nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime" 1891 "(%d) is greater than valid lifetime(%d)\n", 1892 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime)); 1893 return (EINVAL); 1894 } 1895 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) 1896 ndpr->ndpr_preferred = 0; 1897 else 1898 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime; 1899 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1900 ndpr->ndpr_expire = 0; 1901 else 1902 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime; 1903 1904 return 0; 1905 } 1906 1907 void 1908 in6_init_address_ltimes(struct nd_prefix *new, struct in6_addrlifetime *lt6) 1909 { 1910 1911 /* Valid lifetime must not be updated unless explicitly specified. */ 1912 /* init ia6t_expire */ 1913 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) 1914 lt6->ia6t_expire = 0; 1915 else { 1916 lt6->ia6t_expire = time_second; 1917 lt6->ia6t_expire += lt6->ia6t_vltime; 1918 } 1919 1920 /* init ia6t_preferred */ 1921 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) 1922 lt6->ia6t_preferred = 0; 1923 else { 1924 lt6->ia6t_preferred = time_second; 1925 lt6->ia6t_preferred += lt6->ia6t_pltime; 1926 } 1927 } 1928 1929 /* 1930 * Delete all the routing table entries that use the specified gateway. 1931 * XXX: this function causes search through all entries of routing table, so 1932 * it shouldn't be called when acting as a router. 1933 */ 1934 void 1935 rt6_flush(struct in6_addr *gateway, struct ifnet *ifp) 1936 { 1937 struct radix_node_head *rnh = rt_gettable(AF_INET6, 0); 1938 int s = splsoftnet(); 1939 1940 /* We'll care only link-local addresses */ 1941 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) { 1942 splx(s); 1943 return; 1944 } 1945 /* XXX: hack for KAME's link-local address kludge */ 1946 gateway->s6_addr16[1] = htons(ifp->if_index); 1947 1948 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway); 1949 splx(s); 1950 } 1951 1952 int 1953 rt6_deleteroute(struct radix_node *rn, void *arg, u_int id) 1954 { 1955 #define SIN6(s) ((struct sockaddr_in6 *)s) 1956 struct rt_addrinfo info; 1957 struct rtentry *rt = (struct rtentry *)rn; 1958 struct in6_addr *gate = (struct in6_addr *)arg; 1959 1960 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) 1961 return (0); 1962 1963 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) 1964 return (0); 1965 1966 /* 1967 * Do not delete a static route. 1968 * XXX: this seems to be a bit ad-hoc. Should we consider the 1969 * 'cloned' bit instead? 1970 */ 1971 if ((rt->rt_flags & RTF_STATIC) != 0) 1972 return (0); 1973 1974 /* 1975 * We delete only host route. This means, in particular, we don't 1976 * delete default route. 1977 */ 1978 if ((rt->rt_flags & RTF_HOST) == 0) 1979 return (0); 1980 1981 bzero(&info, sizeof(info)); 1982 info.rti_flags = rt->rt_flags; 1983 info.rti_info[RTAX_DST] = rt_key(rt); 1984 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; 1985 info.rti_info[RTAX_NETMASK] = rt_mask(rt); 1986 return (rtrequest1(RTM_DELETE, &info, RTP_ANY, NULL, id)); 1987 #undef SIN6 1988 } 1989 1990 int 1991 nd6_setdefaultiface(int ifindex) 1992 { 1993 int error = 0; 1994 1995 if (ifindex < 0 || if_indexlim <= ifindex) 1996 return (EINVAL); 1997 if (ifindex != 0 && !ifindex2ifnet[ifindex]) 1998 return (EINVAL); 1999 2000 if (nd6_defifindex != ifindex) { 2001 nd6_defifindex = ifindex; 2002 if (nd6_defifindex > 0) { 2003 nd6_defifp = ifindex2ifnet[nd6_defifindex]; 2004 } else 2005 nd6_defifp = NULL; 2006 } 2007 2008 return (error); 2009 } 2010