1 /* $OpenBSD: nd6_rtr.c,v 1.11 2001/06/09 06:43:38 angelos Exp $ */ 2 /* $KAME: nd6_rtr.c,v 1.97 2001/02/07 11:09:13 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/socket.h> 38 #include <sys/sockio.h> 39 #include <sys/time.h> 40 #include <sys/kernel.h> 41 #include <sys/errno.h> 42 #include <sys/ioctl.h> 43 #include <sys/syslog.h> 44 #include <dev/rndvar.h> 45 46 #include <net/if.h> 47 #include <net/if_types.h> 48 #include <net/if_dl.h> 49 #include <net/route.h> 50 #include <net/radix.h> 51 52 #include <netinet/in.h> 53 #include <netinet6/in6_var.h> 54 #include <netinet/ip6.h> 55 #include <netinet6/ip6_var.h> 56 #include <netinet6/nd6.h> 57 #include <netinet/icmp6.h> 58 59 #define SDL(s) ((struct sockaddr_dl *)s) 60 61 static struct nd_defrouter *defrtrlist_update __P((struct nd_defrouter *)); 62 static int prelist_add __P((struct nd_prefix *, struct nd_defrouter *)); 63 static struct nd_prefix *prefix_lookup __P((struct nd_prefix *)); 64 static struct in6_ifaddr *in6_ifadd __P((struct ifnet *, struct in6_addr *, 65 struct in6_addr *, int)); 66 static struct nd_pfxrouter *pfxrtr_lookup __P((struct nd_prefix *, 67 struct nd_defrouter *)); 68 static void pfxrtr_add __P((struct nd_prefix *, struct nd_defrouter *)); 69 static void pfxrtr_del __P((struct nd_pfxrouter *)); 70 static struct nd_pfxrouter *find_pfxlist_reachable_router __P((struct nd_prefix *)); 71 static void nd6_detach_prefix __P((struct nd_prefix *)); 72 static void nd6_attach_prefix __P((struct nd_prefix *)); 73 static void defrouter_addifreq __P((struct ifnet *)); 74 75 static void in6_init_address_ltimes __P((struct nd_prefix *ndpr, 76 struct in6_addrlifetime *lt6, 77 int update_vltime)); 78 79 static int rt6_deleteroute __P((struct radix_node *, void *)); 80 81 extern int nd6_recalc_reachtm_interval; 82 83 struct ifnet *nd6_defifp; 84 int nd6_defifindex; 85 86 /* 87 * Receive Router Solicitation Message - just for routers. 88 * Router solicitation/advertisement is mostly managed by userland program 89 * (rtadvd) so here we have no function like nd6_ra_output(). 90 * 91 * Based on RFC 2461 92 */ 93 void 94 nd6_rs_input(m, off, icmp6len) 95 struct mbuf *m; 96 int off, icmp6len; 97 { 98 struct ifnet *ifp = m->m_pkthdr.rcvif; 99 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 100 struct nd_router_solicit *nd_rs; 101 struct in6_addr saddr6 = ip6->ip6_src; 102 #if 0 103 struct in6_addr daddr6 = ip6->ip6_dst; 104 #endif 105 char *lladdr = NULL; 106 int lladdrlen = 0; 107 #if 0 108 struct sockaddr_dl *sdl = (struct sockaddr_dl *)NULL; 109 struct llinfo_nd6 *ln = (struct llinfo_nd6 *)NULL; 110 struct rtentry *rt = NULL; 111 int is_newentry; 112 #endif 113 union nd_opts ndopts; 114 115 /* If I'm not a router, ignore it. */ 116 if (ip6_accept_rtadv != 0 || ip6_forwarding != 1) 117 goto freeit; 118 119 /* Sanity checks */ 120 if (ip6->ip6_hlim != 255) { 121 nd6log((LOG_ERR, 122 "nd6_rs_input: invalid hlim (%d) from %s to %s on %s\n", 123 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 124 ip6_sprintf(&ip6->ip6_dst), ifp->if_xname)); 125 goto bad; 126 } 127 128 /* 129 * Don't update the neighbor cache, if src = ::. 130 * This indicates that the src has no IP address assigned yet. 131 */ 132 if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 133 goto freeit; 134 135 #ifndef PULLDOWN_TEST 136 IP6_EXTHDR_CHECK(m, off, icmp6len,); 137 nd_rs = (struct nd_router_solicit *)((caddr_t)ip6 + off); 138 #else 139 IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, icmp6len); 140 if (nd_rs == NULL) { 141 icmp6stat.icp6s_tooshort++; 142 return; 143 } 144 #endif 145 146 icmp6len -= sizeof(*nd_rs); 147 nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 148 if (nd6_options(&ndopts) < 0) { 149 nd6log((LOG_INFO, 150 "nd6_rs_input: invalid ND option, ignored\n")); 151 /* nd6_options have incremented stats */ 152 goto freeit; 153 } 154 155 if (ndopts.nd_opts_src_lladdr) { 156 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 157 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 158 } 159 160 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 161 nd6log((LOG_INFO, 162 "nd6_rs_input: lladdrlen mismatch for %s " 163 "(if %d, RS packet %d)\n", 164 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2)); 165 goto bad; 166 } 167 168 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_SOLICIT, 0); 169 170 freeit: 171 m_freem(m); 172 return; 173 174 bad: 175 icmp6stat.icp6s_badrs++; 176 m_freem(m); 177 } 178 179 /* 180 * Receive Router Advertisement Message. 181 * 182 * Based on RFC 2461 183 * TODO: on-link bit on prefix information 184 * TODO: ND_RA_FLAG_{OTHER,MANAGED} processing 185 */ 186 void 187 nd6_ra_input(m, off, icmp6len) 188 struct mbuf *m; 189 int off, icmp6len; 190 { 191 struct ifnet *ifp = m->m_pkthdr.rcvif; 192 struct nd_ifinfo *ndi = &nd_ifinfo[ifp->if_index]; 193 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 194 struct nd_router_advert *nd_ra; 195 struct in6_addr saddr6 = ip6->ip6_src; 196 #if 0 197 struct in6_addr daddr6 = ip6->ip6_dst; 198 int flags; /* = nd_ra->nd_ra_flags_reserved; */ 199 int is_managed = ((flags & ND_RA_FLAG_MANAGED) != 0); 200 int is_other = ((flags & ND_RA_FLAG_OTHER) != 0); 201 #endif 202 union nd_opts ndopts; 203 struct nd_defrouter *dr; 204 205 if (ip6_accept_rtadv == 0) 206 goto freeit; 207 208 if (ip6->ip6_hlim != 255) { 209 nd6log((LOG_ERR, 210 "nd6_ra_input: invalid hlim (%d) from %s to %s on %s\n", 211 ip6->ip6_hlim, ip6_sprintf(&ip6->ip6_src), 212 ip6_sprintf(&ip6->ip6_dst), ifp->if_xname)); 213 goto bad; 214 } 215 216 if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 217 nd6log((LOG_ERR, 218 "nd6_ra_input: src %s is not link-local\n", 219 ip6_sprintf(&saddr6))); 220 goto bad; 221 } 222 223 #ifndef PULLDOWN_TEST 224 IP6_EXTHDR_CHECK(m, off, icmp6len,); 225 nd_ra = (struct nd_router_advert *)((caddr_t)ip6 + off); 226 #else 227 IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, icmp6len); 228 if (nd_ra == NULL) { 229 icmp6stat.icp6s_tooshort++; 230 return; 231 } 232 #endif 233 234 icmp6len -= sizeof(*nd_ra); 235 nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 236 if (nd6_options(&ndopts) < 0) { 237 nd6log((LOG_INFO, 238 "nd6_ra_input: invalid ND option, ignored\n")); 239 /* nd6_options have incremented stats */ 240 goto freeit; 241 } 242 243 { 244 struct nd_defrouter dr0; 245 u_int32_t advreachable = nd_ra->nd_ra_reachable; 246 long time_second = time.tv_sec; 247 248 dr0.rtaddr = saddr6; 249 dr0.flags = nd_ra->nd_ra_flags_reserved; 250 dr0.rtlifetime = ntohs(nd_ra->nd_ra_router_lifetime); 251 dr0.expire = time_second + dr0.rtlifetime; 252 dr0.ifp = ifp; 253 /* unspecified or not? (RFC 2461 6.3.4) */ 254 if (advreachable) { 255 NTOHL(advreachable); 256 if (advreachable <= MAX_REACHABLE_TIME && 257 ndi->basereachable != advreachable) { 258 ndi->basereachable = advreachable; 259 ndi->reachable = ND_COMPUTE_RTIME(ndi->basereachable); 260 ndi->recalctm = nd6_recalc_reachtm_interval; /* reset */ 261 } 262 } 263 if (nd_ra->nd_ra_retransmit) 264 ndi->retrans = ntohl(nd_ra->nd_ra_retransmit); 265 if (nd_ra->nd_ra_curhoplimit) 266 ndi->chlim = nd_ra->nd_ra_curhoplimit; 267 dr = defrtrlist_update(&dr0); 268 } 269 270 /* 271 * prefix 272 */ 273 if (ndopts.nd_opts_pi) { 274 struct nd_opt_hdr *pt; 275 struct nd_opt_prefix_info *pi; 276 struct nd_prefix pr; 277 278 for (pt = (struct nd_opt_hdr *)ndopts.nd_opts_pi; 279 pt <= (struct nd_opt_hdr *)ndopts.nd_opts_pi_end; 280 pt = (struct nd_opt_hdr *)((caddr_t)pt + 281 (pt->nd_opt_len << 3))) { 282 if (pt->nd_opt_type != ND_OPT_PREFIX_INFORMATION) 283 continue; 284 pi = (struct nd_opt_prefix_info *)pt; 285 286 if (pi->nd_opt_pi_len != 4) { 287 nd6log((LOG_INFO, 288 "nd6_ra_input: invalid option " 289 "len %d for prefix information option, " 290 "ignored\n", pi->nd_opt_pi_len)); 291 continue; 292 } 293 294 if (128 < pi->nd_opt_pi_prefix_len) { 295 nd6log((LOG_INFO, 296 "nd6_ra_input: invalid prefix " 297 "len %d for prefix information option, " 298 "ignored\n", pi->nd_opt_pi_prefix_len)); 299 continue; 300 } 301 302 if (IN6_IS_ADDR_MULTICAST(&pi->nd_opt_pi_prefix) 303 || IN6_IS_ADDR_LINKLOCAL(&pi->nd_opt_pi_prefix)) { 304 nd6log((LOG_INFO, 305 "nd6_ra_input: invalid prefix " 306 "%s, ignored\n", 307 ip6_sprintf(&pi->nd_opt_pi_prefix))); 308 continue; 309 } 310 311 /* aggregatable unicast address, rfc2374 */ 312 if ((pi->nd_opt_pi_prefix.s6_addr8[0] & 0xe0) == 0x20 313 && pi->nd_opt_pi_prefix_len != 64) { 314 nd6log((LOG_INFO, 315 "nd6_ra_input: invalid prefixlen " 316 "%d for rfc2374 prefix %s, ignored\n", 317 pi->nd_opt_pi_prefix_len, 318 ip6_sprintf(&pi->nd_opt_pi_prefix))); 319 continue; 320 } 321 322 bzero(&pr, sizeof(pr)); 323 pr.ndpr_prefix.sin6_family = AF_INET6; 324 pr.ndpr_prefix.sin6_len = sizeof(pr.ndpr_prefix); 325 pr.ndpr_prefix.sin6_addr = pi->nd_opt_pi_prefix; 326 pr.ndpr_ifp = (struct ifnet *)m->m_pkthdr.rcvif; 327 328 pr.ndpr_raf_onlink = (pi->nd_opt_pi_flags_reserved & 329 ND_OPT_PI_FLAG_ONLINK) ? 1 : 0; 330 pr.ndpr_raf_auto = (pi->nd_opt_pi_flags_reserved & 331 ND_OPT_PI_FLAG_AUTO) ? 1 : 0; 332 pr.ndpr_plen = pi->nd_opt_pi_prefix_len; 333 pr.ndpr_vltime = ntohl(pi->nd_opt_pi_valid_time); 334 pr.ndpr_pltime = 335 ntohl(pi->nd_opt_pi_preferred_time); 336 337 if (in6_init_prefix_ltimes(&pr)) 338 continue; /* prefix lifetime init failed */ 339 340 (void)prelist_update(&pr, dr, m); 341 } 342 } 343 344 /* 345 * MTU 346 */ 347 if (ndopts.nd_opts_mtu && ndopts.nd_opts_mtu->nd_opt_mtu_len == 1) { 348 u_int32_t mtu = ntohl(ndopts.nd_opts_mtu->nd_opt_mtu_mtu); 349 350 /* lower bound */ 351 if (mtu < IPV6_MMTU) { 352 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu option " 353 "mtu=%d sent from %s, ignoring\n", 354 mtu, ip6_sprintf(&ip6->ip6_src))); 355 goto skip; 356 } 357 358 /* upper bound */ 359 if (ndi->maxmtu) { 360 if (mtu <= ndi->maxmtu) { 361 int change = (ndi->linkmtu != mtu); 362 363 ndi->linkmtu = mtu; 364 if (change) /* in6_maxmtu may change */ 365 in6_setmaxmtu(); 366 } else { 367 nd6log((LOG_INFO, "nd6_ra_input: bogus mtu " 368 "mtu=%d sent from %s; " 369 "exceeds maxmtu %d, ignoring\n", 370 mtu, ip6_sprintf(&ip6->ip6_src), 371 ndi->maxmtu)); 372 } 373 } else { 374 nd6log((LOG_INFO, "nd6_ra_input: mtu option " 375 "mtu=%d sent from %s; maxmtu unknown, " 376 "ignoring\n", 377 mtu, ip6_sprintf(&ip6->ip6_src))); 378 } 379 } 380 381 skip: 382 383 /* 384 * Src linkaddress 385 */ 386 { 387 char *lladdr = NULL; 388 int lladdrlen = 0; 389 390 if (ndopts.nd_opts_src_lladdr) { 391 lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 392 lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 393 } 394 395 if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 396 nd6log((LOG_INFO, 397 "nd6_ra_input: lladdrlen mismatch for %s " 398 "(if %d, RA packet %d)\n", 399 ip6_sprintf(&saddr6), ifp->if_addrlen, lladdrlen - 2)); 400 goto bad; 401 } 402 403 nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, ND_ROUTER_ADVERT, 0); 404 405 /* 406 * Installing a link-layer address might change the state of the 407 * router's neighbor cache, which might also affect our on-link 408 * detection of adveritsed prefixes. 409 */ 410 pfxlist_onlink_check(); 411 } 412 413 freeit: 414 m_freem(m); 415 return; 416 417 bad: 418 icmp6stat.icp6s_badra++; 419 m_freem(m); 420 } 421 422 /* 423 * default router list proccessing sub routines 424 */ 425 void 426 defrouter_addreq(new) 427 struct nd_defrouter *new; 428 { 429 struct sockaddr_in6 def, mask, gate; 430 int s; 431 432 Bzero(&def, sizeof(def)); 433 Bzero(&mask, sizeof(mask)); 434 Bzero(&gate, sizeof(gate)); 435 436 def.sin6_len = mask.sin6_len = gate.sin6_len 437 = sizeof(struct sockaddr_in6); 438 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 439 gate.sin6_addr = new->rtaddr; 440 441 s = splnet(); 442 (void)rtrequest(RTM_ADD, (struct sockaddr *)&def, 443 (struct sockaddr *)&gate, (struct sockaddr *)&mask, 444 RTF_GATEWAY, NULL); 445 splx(s); 446 return; 447 } 448 449 /* Add a route to a given interface as default */ 450 static void 451 defrouter_addifreq(ifp) 452 struct ifnet *ifp; 453 { 454 struct sockaddr_in6 def, mask; 455 struct ifaddr *ifa; 456 int error, flags; 457 458 bzero(&def, sizeof(def)); 459 bzero(&mask, sizeof(mask)); 460 461 def.sin6_len = mask.sin6_len = sizeof(struct sockaddr_in6); 462 def.sin6_family = mask.sin6_family = AF_INET6; 463 464 /* 465 * Search for an ifaddr beloging to the specified interface. 466 * XXX: An IPv6 address are required to be assigned on the interface. 467 */ 468 if ((ifa = ifaof_ifpforaddr((struct sockaddr *)&def, ifp)) == NULL) { 469 nd6log((LOG_ERR, /* better error? */ 470 "defrouter_addifreq: failed to find an ifaddr " 471 "to install a route to interface %s\n", ifp->if_xname)); 472 return; 473 } 474 475 flags = ifa->ifa_flags; 476 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) 477 flags &= ~RTF_CLONING; 478 if ((error = rtrequest(RTM_ADD, (struct sockaddr *)&def, 479 ifa->ifa_addr, (struct sockaddr *)&mask, 480 flags, NULL)) != 0) { 481 nd6log((LOG_ERR, 482 "defrouter_addifreq: failed to install a route to " 483 "interface %s (errno = %d)\n", 484 ifp->if_xname, error)); 485 } 486 } 487 488 struct nd_defrouter * 489 defrouter_lookup(addr, ifp) 490 struct in6_addr *addr; 491 struct ifnet *ifp; 492 { 493 struct nd_defrouter *dr; 494 495 for (dr = TAILQ_FIRST(&nd_defrouter); dr; 496 dr = TAILQ_NEXT(dr, dr_entry)) { 497 if (dr->ifp == ifp && IN6_ARE_ADDR_EQUAL(addr, &dr->rtaddr)) 498 return(dr); 499 } 500 501 return(NULL); /* search failed */ 502 } 503 504 void 505 defrouter_delreq(dr, dofree) 506 struct nd_defrouter *dr; 507 int dofree; 508 { 509 struct sockaddr_in6 def, mask, gate; 510 511 Bzero(&def, sizeof(def)); 512 Bzero(&mask, sizeof(mask)); 513 Bzero(&gate, sizeof(gate)); 514 515 def.sin6_len = mask.sin6_len = gate.sin6_len 516 = sizeof(struct sockaddr_in6); 517 def.sin6_family = mask.sin6_family = gate.sin6_family = AF_INET6; 518 gate.sin6_addr = dr->rtaddr; 519 520 rtrequest(RTM_DELETE, (struct sockaddr *)&def, 521 (struct sockaddr *)&gate, 522 (struct sockaddr *)&mask, 523 RTF_GATEWAY, (struct rtentry **)0); 524 525 if (dofree) /* XXX: necessary? */ 526 free(dr, M_IP6NDP); 527 } 528 529 void 530 defrtrlist_del(dr) 531 struct nd_defrouter *dr; 532 { 533 struct nd_defrouter *deldr = NULL; 534 struct nd_prefix *pr; 535 536 /* 537 * Flush all the routing table entries that use the router 538 * as a next hop. 539 */ 540 if (!ip6_forwarding && ip6_accept_rtadv) { 541 /* above is a good condition? */ 542 rt6_flush(&dr->rtaddr, dr->ifp); 543 } 544 545 if (dr == TAILQ_FIRST(&nd_defrouter)) 546 deldr = dr; /* The router is primary. */ 547 548 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 549 550 /* 551 * Also delete all the pointers to the router in each prefix lists. 552 */ 553 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 554 struct nd_pfxrouter *pfxrtr; 555 if ((pfxrtr = pfxrtr_lookup(pr, dr)) != NULL) 556 pfxrtr_del(pfxrtr); 557 } 558 pfxlist_onlink_check(); 559 560 /* 561 * If the router is the primary one, choose a new one. 562 * Note that defrouter_select() will remove the current gateway 563 * from the routing table. 564 */ 565 if (deldr) 566 defrouter_select(); 567 568 free(dr, M_IP6NDP); 569 } 570 571 /* 572 * Default Router Selection according to Section 6.3.6 of RFC 2461: 573 * 1) Routers that are reachable or probably reachable should be 574 * preferred. 575 * 2) When no routers on the list are known to be reachable or 576 * probably reachable, routers SHOULD be selected in a round-robin 577 * fashion. 578 * 3) If the Default Router List is empty, assume that all 579 * destinations are on-link. 580 */ 581 void 582 defrouter_select() 583 { 584 int s = splnet(); 585 struct nd_defrouter *dr, anydr; 586 struct rtentry *rt = NULL; 587 struct llinfo_nd6 *ln = NULL; 588 589 /* 590 * Search for a (probably) reachable router from the list. 591 */ 592 for (dr = TAILQ_FIRST(&nd_defrouter); dr; 593 dr = TAILQ_NEXT(dr, dr_entry)) { 594 if ((rt = nd6_lookup(&dr->rtaddr, 0, dr->ifp)) && 595 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 596 ND6_IS_LLINFO_PROBREACH(ln)) { 597 /* Got it, and move it to the head */ 598 TAILQ_REMOVE(&nd_defrouter, dr, dr_entry); 599 TAILQ_INSERT_HEAD(&nd_defrouter, dr, dr_entry); 600 break; 601 } 602 } 603 604 if ((dr = TAILQ_FIRST(&nd_defrouter))) { 605 /* 606 * De-install the previous default gateway and install 607 * a new one. 608 * Note that if there is no reachable router in the list, 609 * the head entry will be used anyway. 610 * XXX: do we have to check the current routing table entry? 611 */ 612 bzero(&anydr, sizeof(anydr)); 613 defrouter_delreq(&anydr, 0); 614 defrouter_addreq(dr); 615 } 616 else { 617 /* 618 * The Default Router List is empty, so install the default 619 * route to an inteface. 620 * XXX: The specification does not say this mechanism should 621 * be restricted to hosts, but this would be not useful 622 * (even harmful) for routers. 623 */ 624 if (!ip6_forwarding) { 625 /* 626 * De-install the current default route 627 * in advance. 628 */ 629 bzero(&anydr, sizeof(anydr)); 630 defrouter_delreq(&anydr, 0); 631 if (nd6_defifp) { 632 /* 633 * Install a route to the default interface 634 * as default route. 635 * XXX: we enable this for host only, because 636 * this may override a default route installed 637 * a user process (e.g. routing daemon) in a 638 * router case. 639 */ 640 defrouter_addifreq(nd6_defifp); 641 } else { 642 nd6log((LOG_INFO, "defrouter_select: " 643 "there's no default router and no default" 644 " interface\n")); 645 } 646 } 647 } 648 649 splx(s); 650 return; 651 } 652 653 static struct nd_defrouter * 654 defrtrlist_update(new) 655 struct nd_defrouter *new; 656 { 657 struct nd_defrouter *dr, *n; 658 int s = splnet(); 659 660 if ((dr = defrouter_lookup(&new->rtaddr, new->ifp)) != NULL) { 661 /* entry exists */ 662 if (new->rtlifetime == 0) { 663 defrtrlist_del(dr); 664 dr = NULL; 665 } else { 666 /* override */ 667 dr->flags = new->flags; /* xxx flag check */ 668 dr->rtlifetime = new->rtlifetime; 669 dr->expire = new->expire; 670 } 671 splx(s); 672 return(dr); 673 } 674 675 /* entry does not exist */ 676 if (new->rtlifetime == 0) { 677 splx(s); 678 return(NULL); 679 } 680 681 n = (struct nd_defrouter *)malloc(sizeof(*n), M_IP6NDP, M_NOWAIT); 682 if (n == NULL) { 683 splx(s); 684 return(NULL); 685 } 686 bzero(n, sizeof(*n)); 687 *n = *new; 688 689 /* 690 * Insert the new router at the end of the Default Router List. 691 * If there is no other router, install it anyway. Otherwise, 692 * just continue to use the current default router. 693 */ 694 TAILQ_INSERT_TAIL(&nd_defrouter, n, dr_entry); 695 if (TAILQ_FIRST(&nd_defrouter) == n) 696 defrouter_select(); 697 splx(s); 698 699 return(n); 700 } 701 702 static struct nd_pfxrouter * 703 pfxrtr_lookup(pr, dr) 704 struct nd_prefix *pr; 705 struct nd_defrouter *dr; 706 { 707 struct nd_pfxrouter *search; 708 709 for (search = pr->ndpr_advrtrs.lh_first; search; search = search->pfr_next) { 710 if (search->router == dr) 711 break; 712 } 713 714 return(search); 715 } 716 717 static void 718 pfxrtr_add(pr, dr) 719 struct nd_prefix *pr; 720 struct nd_defrouter *dr; 721 { 722 struct nd_pfxrouter *new; 723 724 new = (struct nd_pfxrouter *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 725 if (new == NULL) 726 return; 727 bzero(new, sizeof(*new)); 728 new->router = dr; 729 730 LIST_INSERT_HEAD(&pr->ndpr_advrtrs, new, pfr_entry); 731 732 pfxlist_onlink_check(); 733 } 734 735 static void 736 pfxrtr_del(pfr) 737 struct nd_pfxrouter *pfr; 738 { 739 LIST_REMOVE(pfr, pfr_entry); 740 free(pfr, M_IP6NDP); 741 } 742 743 static struct nd_prefix * 744 prefix_lookup(pr) 745 struct nd_prefix *pr; 746 { 747 struct nd_prefix *search; 748 749 for (search = nd_prefix.lh_first; search; search = search->ndpr_next) { 750 if (pr->ndpr_ifp == search->ndpr_ifp && 751 pr->ndpr_plen == search->ndpr_plen && 752 in6_are_prefix_equal(&pr->ndpr_prefix.sin6_addr, 753 &search->ndpr_prefix.sin6_addr, 754 pr->ndpr_plen) 755 ) { 756 break; 757 } 758 } 759 760 return(search); 761 } 762 763 static int 764 prelist_add(pr, dr) 765 struct nd_prefix *pr; 766 struct nd_defrouter *dr; 767 { 768 struct nd_prefix *new; 769 int i, s; 770 771 new = (struct nd_prefix *)malloc(sizeof(*new), M_IP6NDP, M_NOWAIT); 772 if (new == NULL) 773 return ENOMEM; 774 bzero(new, sizeof(*new)); 775 *new = *pr; 776 777 /* initilization */ 778 new->ndpr_statef_onlink = pr->ndpr_statef_onlink; 779 LIST_INIT(&new->ndpr_advrtrs); 780 in6_prefixlen2mask(&new->ndpr_mask, new->ndpr_plen); 781 /* make prefix in the canonical form */ 782 for (i = 0; i < 4; i++) 783 new->ndpr_prefix.sin6_addr.s6_addr32[i] &= 784 new->ndpr_mask.s6_addr32[i]; 785 786 /* xxx ND_OPT_PI_FLAG_ONLINK processing */ 787 788 s = splnet(); 789 790 /* link ndpr_entry to nd_prefix list */ 791 LIST_INSERT_HEAD(&nd_prefix, new, ndpr_entry); 792 splx(s); 793 794 if (dr) 795 pfxrtr_add(new, dr); 796 797 return 0; 798 } 799 800 void 801 prelist_remove(pr) 802 struct nd_prefix *pr; 803 { 804 struct nd_pfxrouter *pfr, *next; 805 int s; 806 807 s = splnet(); 808 809 /* unlink ndpr_entry from nd_prefix list */ 810 LIST_REMOVE(pr, ndpr_entry); 811 812 /* free list of routers that adversed the prefix */ 813 for (pfr = pr->ndpr_advrtrs.lh_first; pfr; pfr = next) { 814 next = pfr->pfr_next; 815 816 free(pfr, M_IP6NDP); 817 } 818 splx(s); 819 820 free(pr, M_IP6NDP); 821 822 pfxlist_onlink_check(); 823 } 824 825 /* 826 * NOTE: We set address lifetime to keep 827 * address lifetime <= prefix lifetime 828 * invariant. This is to simplify on-link determination code. 829 * If onlink determination is udated, this routine may have to be updated too. 830 */ 831 int 832 prelist_update(new, dr, m) 833 struct nd_prefix *new; 834 struct nd_defrouter *dr; /* may be NULL */ 835 struct mbuf *m; 836 { 837 struct in6_ifaddr *ia6 = NULL; 838 struct nd_prefix *pr; 839 int s = splnet(); 840 841 int error = 0; 842 int auth; 843 struct in6_addrlifetime *lt6; 844 845 auth = 0; 846 if (m) { 847 /* 848 * Authenticity for NA consists authentication for 849 * both IP header and IP datagrams, doesn't it ? 850 */ 851 #if defined(M_AUTHIPHDR) && defined(M_AUTHIPDGM) 852 auth = (m->m_flags & M_AUTHIPHDR 853 && m->m_flags & M_AUTHIPDGM) ? 1 : 0; 854 #endif 855 } 856 857 if ((pr = prefix_lookup(new)) != NULL) { 858 if (pr->ndpr_ifp != new->ndpr_ifp) { 859 error = EADDRNOTAVAIL; 860 goto end; 861 } 862 /* update prefix information */ 863 pr->ndpr_flags = new->ndpr_flags; 864 pr->ndpr_vltime = new->ndpr_vltime; 865 pr->ndpr_pltime = new->ndpr_pltime; 866 pr->ndpr_preferred = new->ndpr_preferred; 867 pr->ndpr_expire = new->ndpr_expire; 868 869 /* 870 * RFC 2462 5.5.3 (d) or (e) 871 * We got a prefix which we have seen in the past. 872 */ 873 if (!new->ndpr_raf_auto) 874 goto noautoconf1; 875 876 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr)) 877 ia6 = NULL; 878 else 879 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr); 880 881 if (ia6 == NULL) { 882 /* 883 * Special case: 884 * (1) We have seen the prefix advertised before, but 885 * we have never performed autoconfig for this prefix. 886 * This is because Autonomous bit was 0 previously, or 887 * autoconfig failed due to some other reasons. 888 * (2) We have seen the prefix advertised before and 889 * we have performed autoconfig in the past, but 890 * we seem to have no interface address right now. 891 * This is because the interface address have expired. 892 * 893 * This prefix is fresh, with respect to autoconfig 894 * process. 895 * 896 * Add an address based on RFC 2462 5.5.3 (d). 897 */ 898 ia6 = in6_ifadd(pr->ndpr_ifp, 899 &pr->ndpr_prefix.sin6_addr, &pr->ndpr_addr, 900 new->ndpr_plen); 901 if (!ia6) { 902 error = EADDRNOTAVAIL; 903 nd6log((LOG_ERR, 904 "prelist_update: failed to add a " 905 "new address\n")); 906 goto noautoconf1; 907 } 908 909 lt6 = &ia6->ia6_lifetime; 910 911 /* address lifetime <= prefix lifetime */ 912 lt6->ia6t_vltime = new->ndpr_vltime; 913 lt6->ia6t_pltime = new->ndpr_pltime; 914 in6_init_address_ltimes(new, lt6, 1); 915 } else { 916 #define TWOHOUR (120*60) 917 /* 918 * We have seen the prefix before, and we have added 919 * interface address in the past. We still have 920 * the interface address assigned. 921 * 922 * update address lifetime based on RFC 2462 923 * 5.5.3 (e). 924 */ 925 int update = 0; 926 927 lt6 = &ia6->ia6_lifetime; 928 929 #if 0 /* RFC 2462 5.5.3 (e) */ 930 lt6->ia6t_pltime = new->ndpr_pltime; 931 if (TWOHOUR < new->ndpr_vltime 932 || lt6pr->nd < new->ndpr_vltime) { 933 lt6->ia6t_vltime = new->ndpr_vltime; 934 update++; 935 } else if (auth 936 && lt6->ia6t_vltime <= TWOHOUR0 937 && new->ndpr_vltime <= lt6->ia6t_vltime) { 938 lt6->ia6t_vltime = new->ndpr_vltime; 939 update++; 940 } else { 941 lt6->ia6t_vltime = TWOHOUR; 942 update++; 943 } 944 945 /* 2 hour rule is not imposed for pref lifetime */ 946 new->ndpr_apltime = new->ndpr_pltime; 947 lt6->ia6t_pltime = new->ndpr_pltime; 948 #else /* update from Jim Bound, (ipng 6712) */ 949 if (TWOHOUR < new->ndpr_vltime 950 || lt6->ia6t_vltime < new->ndpr_vltime) { 951 lt6->ia6t_vltime = new->ndpr_vltime; 952 update++; 953 } else if (auth) { 954 lt6->ia6t_vltime = new->ndpr_vltime; 955 update++; 956 } 957 958 /* jim bound rule is not imposed for pref lifetime */ 959 lt6->ia6t_pltime = new->ndpr_pltime; 960 #endif 961 in6_init_address_ltimes(new, lt6, update); 962 } 963 964 noautoconf1: 965 966 #if 0 967 /* address lifetime expire processing, RFC 2462 5.5.4. */ 968 if (pr->ndpr_preferred && pr->ndpr_preferred < time_second) { 969 struct in6_ifaddr *ia6; 970 971 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr); 972 if (ia6) 973 ia6->ia6_flags &= ~IN6_IFF_DEPRECATED; 974 } 975 #endif 976 977 if (dr && pfxrtr_lookup(pr, dr) == NULL) 978 pfxrtr_add(pr, dr); 979 } else { 980 int error_tmp; 981 982 if (new->ndpr_vltime == 0) goto end; 983 984 bzero(&new->ndpr_addr, sizeof(struct in6_addr)); 985 986 /* 987 * RFC 2462 5.5.3 (d) 988 * We got a fresh prefix. Perform some sanity checks 989 * and add an interface address by appending interface ID 990 * to the advertised prefix. 991 */ 992 if (!new->ndpr_raf_auto) 993 goto noautoconf2; 994 995 ia6 = in6_ifadd(new->ndpr_ifp, &new->ndpr_prefix.sin6_addr, 996 &new->ndpr_addr, new->ndpr_plen); 997 if (!ia6) { 998 error = EADDRNOTAVAIL; 999 nd6log((LOG_ERR, "prelist_update: " 1000 "failed to add a new address\n")); 1001 goto noautoconf2; 1002 } 1003 /* set onlink bit if an interface route is configured */ 1004 new->ndpr_statef_onlink = (ia6->ia_flags & IFA_ROUTE) ? 1 : 0; 1005 1006 lt6 = &ia6->ia6_lifetime; 1007 1008 /* address lifetime <= prefix lifetime */ 1009 lt6->ia6t_vltime = new->ndpr_vltime; 1010 lt6->ia6t_pltime = new->ndpr_pltime; 1011 in6_init_address_ltimes(new, lt6, 1); 1012 1013 noautoconf2: 1014 error_tmp = prelist_add(new, dr); 1015 error = error_tmp ? error_tmp : error; 1016 } 1017 1018 end: 1019 splx(s); 1020 return error; 1021 } 1022 1023 /* 1024 * A supplement function used in the on-link detection below; 1025 * detect if a given prefix has a (probably) reachable advertising router. 1026 * XXX: lengthy function name... 1027 */ 1028 static struct nd_pfxrouter * 1029 find_pfxlist_reachable_router(pr) 1030 struct nd_prefix *pr; 1031 { 1032 struct nd_pfxrouter *pfxrtr; 1033 struct rtentry *rt; 1034 struct llinfo_nd6 *ln; 1035 1036 for (pfxrtr = LIST_FIRST(&pr->ndpr_advrtrs); pfxrtr; 1037 pfxrtr = LIST_NEXT(pfxrtr, pfr_entry)) { 1038 if ((rt = nd6_lookup(&pfxrtr->router->rtaddr, 0, 1039 pfxrtr->router->ifp)) && 1040 (ln = (struct llinfo_nd6 *)rt->rt_llinfo) && 1041 ND6_IS_LLINFO_PROBREACH(ln)) 1042 break; /* found */ 1043 } 1044 1045 return(pfxrtr); 1046 1047 } 1048 1049 /* 1050 * Check if each prefix in the prefix list has at least one available router 1051 * that advertised the prefix (A router is "available" if its neighbor cache 1052 * entry has reachable or probably reachable). 1053 * If the check fails, the prefix may be off-link, because, for example, 1054 * we have moved from the network but the lifetime of the prefix has not 1055 * been expired yet. So we should not use the prefix if there is another 1056 * prefix that has an available router. 1057 * But if there is no prefix that has an available router, we still regards 1058 * all the prefixes as on-link. This is because we can't tell if all the 1059 * routers are simply dead or if we really moved from the network and there 1060 * is no router around us. 1061 */ 1062 void 1063 pfxlist_onlink_check() 1064 { 1065 struct nd_prefix *pr; 1066 1067 /* 1068 * Check if there is a prefix that has a reachable advertising 1069 * router. 1070 */ 1071 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1072 if (find_pfxlist_reachable_router(pr)) 1073 break; 1074 } 1075 1076 if (pr) { 1077 /* 1078 * There is at least one prefix that has a reachable router. 1079 * First, detach prefixes which has no reachable advertising 1080 * router and then attach other prefixes. 1081 * The order is important since an attached prefix and a 1082 * detached prefix may have a same interface route. 1083 */ 1084 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1085 if (find_pfxlist_reachable_router(pr) == NULL && 1086 pr->ndpr_statef_onlink) { 1087 pr->ndpr_statef_onlink = 0; 1088 nd6_detach_prefix(pr); 1089 } 1090 } 1091 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) { 1092 if (find_pfxlist_reachable_router(pr) && 1093 pr->ndpr_statef_onlink == 0) 1094 nd6_attach_prefix(pr); 1095 } 1096 } 1097 else { 1098 /* there is no prefix that has a reachable router */ 1099 for (pr = nd_prefix.lh_first; pr; pr = pr->ndpr_next) 1100 if (pr->ndpr_statef_onlink == 0) 1101 nd6_attach_prefix(pr); 1102 } 1103 } 1104 1105 static void 1106 nd6_detach_prefix(pr) 1107 struct nd_prefix *pr; 1108 { 1109 struct in6_ifaddr *ia6; 1110 struct sockaddr_in6 sa6, mask6; 1111 1112 /* 1113 * Delete the interface route associated with the prefix. 1114 */ 1115 bzero(&sa6, sizeof(sa6)); 1116 sa6.sin6_family = AF_INET6; 1117 sa6.sin6_len = sizeof(sa6); 1118 bcopy(&pr->ndpr_prefix.sin6_addr, &sa6.sin6_addr, 1119 sizeof(struct in6_addr)); 1120 bzero(&mask6, sizeof(mask6)); 1121 mask6.sin6_family = AF_INET6; 1122 mask6.sin6_len = sizeof(sa6); 1123 bcopy(&pr->ndpr_mask, &mask6.sin6_addr, sizeof(struct in6_addr)); 1124 { 1125 int e; 1126 1127 e = rtrequest(RTM_DELETE, (struct sockaddr *)&sa6, NULL, 1128 (struct sockaddr *)&mask6, 0, NULL); 1129 if (e) { 1130 nd6log((LOG_ERR, 1131 "nd6_detach_prefix: failed to delete route: " 1132 "%s/%d (errno = %d)\n", 1133 ip6_sprintf(&sa6.sin6_addr), 1134 pr->ndpr_plen, 1135 e)); 1136 } 1137 } 1138 1139 /* 1140 * Mark the address derived from the prefix detached so that 1141 * it won't be used as a source address for a new connection. 1142 */ 1143 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr)) 1144 ia6 = NULL; 1145 else 1146 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr); 1147 if (ia6) 1148 ia6->ia6_flags |= IN6_IFF_DETACHED; 1149 } 1150 1151 static void 1152 nd6_attach_prefix(pr) 1153 struct nd_prefix *pr; 1154 { 1155 struct ifaddr *ifa; 1156 struct in6_ifaddr *ia6; 1157 1158 /* 1159 * Add the interface route associated with the prefix(if necessary) 1160 * Should we consider if the L bit is set in pr->ndpr_flags? 1161 */ 1162 ifa = ifaof_ifpforaddr((struct sockaddr *)&pr->ndpr_prefix, 1163 pr->ndpr_ifp); 1164 if (ifa == NULL) { 1165 nd6log((LOG_ERR, 1166 "nd6_attach_prefix: failed to find any ifaddr" 1167 " to add route for a prefix(%s/%d)\n", 1168 ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen)); 1169 } 1170 else { 1171 int e; 1172 struct sockaddr_in6 mask6; 1173 1174 bzero(&mask6, sizeof(mask6)); 1175 mask6.sin6_family = AF_INET6; 1176 mask6.sin6_len = sizeof(mask6); 1177 mask6.sin6_addr = pr->ndpr_mask; 1178 e = rtrequest(RTM_ADD, (struct sockaddr *)&pr->ndpr_prefix, 1179 ifa->ifa_addr, (struct sockaddr *)&mask6, 1180 ifa->ifa_flags, NULL); 1181 if (e == 0) 1182 pr->ndpr_statef_onlink = 1; 1183 else { 1184 nd6log((LOG_ERR, 1185 "nd6_attach_prefix: failed to add route for" 1186 " a prefix(%s/%d), errno = %d\n", 1187 ip6_sprintf(&pr->ndpr_addr), pr->ndpr_plen, e)); 1188 } 1189 } 1190 1191 /* 1192 * Now the address derived from the prefix can be used as a source 1193 * for a new connection, so clear the detached flag. 1194 */ 1195 if (IN6_IS_ADDR_UNSPECIFIED(&pr->ndpr_addr)) 1196 ia6 = NULL; 1197 else 1198 ia6 = in6ifa_ifpwithaddr(pr->ndpr_ifp, &pr->ndpr_addr); 1199 if (ia6) { 1200 ia6->ia6_flags &= ~IN6_IFF_DETACHED; 1201 if (pr->ndpr_statef_onlink) 1202 ia6->ia_flags |= IFA_ROUTE; 1203 } 1204 } 1205 1206 static struct in6_ifaddr * 1207 in6_ifadd(ifp, in6, addr, prefixlen) 1208 struct ifnet *ifp; 1209 struct in6_addr *in6; 1210 struct in6_addr *addr; 1211 int prefixlen; /* prefix len of the new prefix in "in6" */ 1212 { 1213 struct ifaddr *ifa; 1214 struct in6_ifaddr *ia, *ib, *oia; 1215 int s, error; 1216 struct in6_addr mask; 1217 1218 in6_len2mask(&mask, prefixlen); 1219 1220 /* find link-local address (will be interface ID) */ 1221 ifa = (struct ifaddr *)in6ifa_ifpforlinklocal(ifp, 0);/* 0 is OK? */ 1222 if (ifa) 1223 ib = (struct in6_ifaddr *)ifa; 1224 else 1225 return NULL; 1226 1227 #if 0 /* don't care link local addr state, and always do DAD */ 1228 /* if link-local address is not eligible, do not autoconfigure. */ 1229 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) { 1230 printf("in6_ifadd: link-local address not ready\n"); 1231 return NULL; 1232 } 1233 #endif 1234 1235 /* prefixlen + ifidlen must be equal to 128 */ 1236 if (prefixlen != in6_mask2len(&ib->ia_prefixmask.sin6_addr)) { 1237 nd6log((LOG_ERR, "in6_ifadd: wrong prefixlen for %s" 1238 "(prefix=%d ifid=%d)\n", ifp->if_xname, 1239 prefixlen, 1240 128 - in6_mask2len(&ib->ia_prefixmask.sin6_addr))); 1241 return NULL; 1242 } 1243 1244 /* make ifaddr */ 1245 ia = (struct in6_ifaddr *)malloc(sizeof(*ia), M_IFADDR, M_DONTWAIT); 1246 if (ia == NULL) { 1247 printf("ENOBUFS in in6_ifadd %d\n", __LINE__); 1248 return NULL; 1249 } 1250 1251 bzero((caddr_t)ia, sizeof(*ia)); 1252 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 1253 if (ifp->if_flags & IFF_POINTOPOINT) 1254 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 1255 else 1256 ia->ia_ifa.ifa_dstaddr = NULL; 1257 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 1258 ia->ia_ifp = ifp; 1259 1260 /* link to in6_ifaddr */ 1261 if ((oia = in6_ifaddr) != NULL) { 1262 for( ; oia->ia_next; oia = oia->ia_next) 1263 continue; 1264 oia->ia_next = ia; 1265 } else { 1266 /* 1267 * This should be impossible, since we have at least one 1268 * link-local address (see the beginning of this function). 1269 * XXX: should we rather panic here? 1270 */ 1271 printf("in6_ifadd: in6_ifaddr is NULL (impossible!)\n"); 1272 in6_ifaddr = ia; 1273 } 1274 /* gain a refcnt for the link from in6_ifaddr */ 1275 ia->ia_ifa.ifa_refcnt++; 1276 1277 /* link to if_addrlist */ 1278 TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list); 1279 /* gain another refcnt for the link from if_addrlist */ 1280 ia->ia_ifa.ifa_refcnt++; 1281 1282 /* new address */ 1283 ia->ia_addr.sin6_len = sizeof(struct sockaddr_in6); 1284 ia->ia_addr.sin6_family = AF_INET6; 1285 /* prefix */ 1286 bcopy(in6, &ia->ia_addr.sin6_addr, sizeof(ia->ia_addr.sin6_addr)); 1287 ia->ia_addr.sin6_addr.s6_addr32[0] &= mask.s6_addr32[0]; 1288 ia->ia_addr.sin6_addr.s6_addr32[1] &= mask.s6_addr32[1]; 1289 ia->ia_addr.sin6_addr.s6_addr32[2] &= mask.s6_addr32[2]; 1290 ia->ia_addr.sin6_addr.s6_addr32[3] &= mask.s6_addr32[3]; 1291 /* interface ID */ 1292 ia->ia_addr.sin6_addr.s6_addr32[0] 1293 |= (ib->ia_addr.sin6_addr.s6_addr32[0] & ~mask.s6_addr32[0]); 1294 ia->ia_addr.sin6_addr.s6_addr32[1] 1295 |= (ib->ia_addr.sin6_addr.s6_addr32[1] & ~mask.s6_addr32[1]); 1296 ia->ia_addr.sin6_addr.s6_addr32[2] 1297 |= (ib->ia_addr.sin6_addr.s6_addr32[2] & ~mask.s6_addr32[2]); 1298 ia->ia_addr.sin6_addr.s6_addr32[3] 1299 |= (ib->ia_addr.sin6_addr.s6_addr32[3] & ~mask.s6_addr32[3]); 1300 1301 /* new prefix */ 1302 ia->ia_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1303 ia->ia_prefixmask.sin6_family = AF_INET6; 1304 bcopy(&mask, &ia->ia_prefixmask.sin6_addr, 1305 sizeof(ia->ia_prefixmask.sin6_addr)); 1306 1307 /* same routine */ 1308 ia->ia_ifa.ifa_rtrequest = 1309 (ifp->if_type == IFT_PPP) ? nd6_p2p_rtrequest : nd6_rtrequest; 1310 ia->ia_ifa.ifa_flags |= RTF_CLONING; 1311 ia->ia_ifa.ifa_metric = ifp->if_metric; 1312 1313 /* add interface route */ 1314 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_UP|RTF_CLONING))) { 1315 nd6log((LOG_NOTICE, 1316 "in6_ifadd: failed to add an interface route " 1317 "for %s/%d on %s, errno = %d\n", 1318 ip6_sprintf(&ia->ia_addr.sin6_addr), prefixlen, 1319 ifp->if_xname, error)); 1320 } 1321 else 1322 ia->ia_flags |= IFA_ROUTE; 1323 1324 *addr = ia->ia_addr.sin6_addr; 1325 1326 if (ifp->if_flags & IFF_MULTICAST) { 1327 int error; /* not used */ 1328 struct in6_addr sol6; 1329 1330 /* Restore saved multicast addresses(if any). */ 1331 in6_restoremkludge(ia, ifp); 1332 1333 /* join solicited node multicast address */ 1334 bzero(&sol6, sizeof(sol6)); 1335 sol6.s6_addr16[0] = htons(0xff02); 1336 sol6.s6_addr16[1] = htons(ifp->if_index); 1337 sol6.s6_addr32[1] = 0; 1338 sol6.s6_addr32[2] = htonl(1); 1339 sol6.s6_addr32[3] = ia->ia_addr.sin6_addr.s6_addr32[3]; 1340 sol6.s6_addr8[12] = 0xff; 1341 (void)in6_addmulti(&sol6, ifp, &error); 1342 } 1343 1344 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1345 1346 /* 1347 * To make the interface up. Only AF_INET6 in ia is used... 1348 */ 1349 s = splimp(); 1350 if (ifp->if_ioctl && (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) { 1351 splx(s); 1352 return NULL; 1353 } 1354 splx(s); 1355 1356 /* Perform DAD, if needed. */ 1357 nd6_dad_start((struct ifaddr *)ia, NULL); 1358 1359 return ia; 1360 } 1361 1362 int 1363 in6_ifdel(ifp, in6) 1364 struct ifnet *ifp; 1365 struct in6_addr *in6; 1366 { 1367 struct in6_ifaddr *ia = (struct in6_ifaddr *)NULL; 1368 struct in6_ifaddr *oia = (struct in6_ifaddr *)NULL; 1369 1370 if (!ifp) 1371 return -1; 1372 1373 ia = in6ifa_ifpwithaddr(ifp, in6); 1374 if (!ia) 1375 return -1; 1376 1377 if (ifp->if_flags & IFF_MULTICAST) { 1378 /* 1379 * delete solicited multicast addr for deleting host id 1380 */ 1381 struct in6_multi *in6m; 1382 struct in6_addr llsol; 1383 bzero(&llsol, sizeof(struct in6_addr)); 1384 llsol.s6_addr16[0] = htons(0xff02); 1385 llsol.s6_addr16[1] = htons(ifp->if_index); 1386 llsol.s6_addr32[1] = 0; 1387 llsol.s6_addr32[2] = htonl(1); 1388 llsol.s6_addr32[3] = 1389 ia->ia_addr.sin6_addr.s6_addr32[3]; 1390 llsol.s6_addr8[12] = 0xff; 1391 1392 IN6_LOOKUP_MULTI(llsol, ifp, in6m); 1393 if (in6m) 1394 in6_delmulti(in6m); 1395 } 1396 1397 if (ia->ia_flags & IFA_ROUTE) { 1398 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 1399 ia->ia_flags &= ~IFA_ROUTE; 1400 } 1401 1402 TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list); 1403 IFAFREE(&ia->ia_ifa); 1404 1405 /* lladdr is never deleted */ 1406 oia = ia; 1407 if (oia == (ia = in6_ifaddr)) 1408 in6_ifaddr = ia->ia_next; 1409 else { 1410 while (ia->ia_next && (ia->ia_next != oia)) 1411 ia = ia->ia_next; 1412 if (ia->ia_next) 1413 ia->ia_next = oia->ia_next; 1414 else 1415 return -1; 1416 } 1417 1418 in6_savemkludge(oia); 1419 IFAFREE((&oia->ia_ifa)); 1420 /* xxx 1421 rtrequest(RTM_DELETE, 1422 (struct sockaddr *)&ia->ia_addr, 1423 (struct sockaddr *)0 1424 (struct sockaddr *)&ia->ia_prefixmask, 1425 RTF_UP|RTF_CLONING, 1426 (struct rtentry **)0); 1427 */ 1428 return 0; 1429 } 1430 1431 int 1432 in6_init_prefix_ltimes(struct nd_prefix *ndpr) 1433 { 1434 long time_second = time.tv_sec; 1435 1436 /* check if preferred lifetime > valid lifetime */ 1437 if (ndpr->ndpr_pltime > ndpr->ndpr_vltime) { 1438 nd6log((LOG_INFO, "in6_init_prefix_ltimes: preferred lifetime" 1439 "(%d) is greater than valid lifetime(%d)\n", 1440 (u_int)ndpr->ndpr_pltime, (u_int)ndpr->ndpr_vltime)); 1441 return (EINVAL); 1442 } 1443 if (ndpr->ndpr_pltime == ND6_INFINITE_LIFETIME) 1444 ndpr->ndpr_preferred = 0; 1445 else 1446 ndpr->ndpr_preferred = time_second + ndpr->ndpr_pltime; 1447 if (ndpr->ndpr_vltime == ND6_INFINITE_LIFETIME) 1448 ndpr->ndpr_expire = 0; 1449 else 1450 ndpr->ndpr_expire = time_second + ndpr->ndpr_vltime; 1451 1452 return 0; 1453 } 1454 1455 static void 1456 in6_init_address_ltimes(struct nd_prefix *new, 1457 struct in6_addrlifetime *lt6, 1458 int update_vltime) 1459 { 1460 long time_second = time.tv_sec; 1461 1462 /* Valid lifetime must not be updated unless explicitly specified. */ 1463 if (update_vltime) { 1464 /* init ia6t_expire */ 1465 if (lt6->ia6t_vltime == ND6_INFINITE_LIFETIME) 1466 lt6->ia6t_expire = 0; 1467 else { 1468 lt6->ia6t_expire = time_second; 1469 lt6->ia6t_expire += lt6->ia6t_vltime; 1470 } 1471 /* Ensure addr lifetime <= prefix lifetime. */ 1472 if (new->ndpr_expire && lt6->ia6t_expire && 1473 new->ndpr_expire < lt6->ia6t_expire) 1474 lt6->ia6t_expire = new->ndpr_expire; 1475 } 1476 1477 /* init ia6t_preferred */ 1478 if (lt6->ia6t_pltime == ND6_INFINITE_LIFETIME) 1479 lt6->ia6t_preferred = 0; 1480 else { 1481 lt6->ia6t_preferred = time_second; 1482 lt6->ia6t_preferred += lt6->ia6t_pltime; 1483 } 1484 /* Ensure addr lifetime <= prefix lifetime. */ 1485 if (new->ndpr_preferred && lt6->ia6t_preferred 1486 && new->ndpr_preferred < lt6->ia6t_preferred) 1487 lt6->ia6t_preferred = new->ndpr_preferred; 1488 } 1489 1490 /* 1491 * Delete all the routing table entries that use the specified gateway. 1492 * XXX: this function causes search through all entries of routing table, so 1493 * it shouldn't be called when acting as a router. 1494 */ 1495 void 1496 rt6_flush(gateway, ifp) 1497 struct in6_addr *gateway; 1498 struct ifnet *ifp; 1499 { 1500 struct radix_node_head *rnh = rt_tables[AF_INET6]; 1501 int s = splnet(); 1502 1503 /* We'll care only link-local addresses */ 1504 if (!IN6_IS_ADDR_LINKLOCAL(gateway)) { 1505 splx(s); 1506 return; 1507 } 1508 /* XXX: hack for KAME's link-local address kludge */ 1509 gateway->s6_addr16[1] = htons(ifp->if_index); 1510 1511 rnh->rnh_walktree(rnh, rt6_deleteroute, (void *)gateway); 1512 splx(s); 1513 } 1514 1515 static int 1516 rt6_deleteroute(rn, arg) 1517 struct radix_node *rn; 1518 void *arg; 1519 { 1520 #define SIN6(s) ((struct sockaddr_in6 *)s) 1521 struct rtentry *rt = (struct rtentry *)rn; 1522 struct in6_addr *gate = (struct in6_addr *)arg; 1523 1524 if (rt->rt_gateway == NULL || rt->rt_gateway->sa_family != AF_INET6) 1525 return(0); 1526 1527 if (!IN6_ARE_ADDR_EQUAL(gate, &SIN6(rt->rt_gateway)->sin6_addr)) 1528 return(0); 1529 1530 /* 1531 * We delete only host route. This means, in particular, we don't 1532 * delete default route. 1533 */ 1534 if ((rt->rt_flags & RTF_HOST) == 0) 1535 return(0); 1536 1537 return(rtrequest(RTM_DELETE, rt_key(rt), 1538 rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0)); 1539 #undef SIN6 1540 } 1541 1542 int 1543 nd6_setdefaultiface(ifindex) 1544 int ifindex; 1545 { 1546 int error = 0; 1547 1548 if (ifindex < 0 || if_index < ifindex) 1549 return(EINVAL); 1550 1551 if (nd6_defifindex != ifindex) { 1552 nd6_defifindex = ifindex; 1553 if (nd6_defifindex > 0) 1554 nd6_defifp = ifindex2ifnet[nd6_defifindex]; 1555 else 1556 nd6_defifp = NULL; 1557 1558 /* 1559 * If the Default Router List is empty, install a route 1560 * to the specified interface as default or remove the default 1561 * route when the default interface becomes canceled. 1562 * The check for the queue is actually redundant, but 1563 * we do this here to avoid re-install the default route 1564 * if the list is NOT empty. 1565 */ 1566 if (TAILQ_FIRST(&nd_defrouter) == NULL) 1567 defrouter_select(); 1568 } 1569 1570 return(error); 1571 } 1572