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