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