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