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