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