1 /* $NetBSD: in6.c,v 1.257 2018/01/10 10:56:30 knakahara Exp $ */ 2 /* $KAME: in6.c,v 1.198 2001/07/18 09:12:38 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 /* 34 * Copyright (c) 1982, 1986, 1991, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)in.c 8.2 (Berkeley) 11/15/93 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: in6.c,v 1.257 2018/01/10 10:56:30 knakahara Exp $"); 66 67 #ifdef _KERNEL_OPT 68 #include "opt_inet.h" 69 #include "opt_compat_netbsd.h" 70 #include "opt_net_mpsafe.h" 71 #endif 72 73 #include <sys/param.h> 74 #include <sys/ioctl.h> 75 #include <sys/errno.h> 76 #include <sys/malloc.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/sockio.h> 80 #include <sys/systm.h> 81 #include <sys/proc.h> 82 #include <sys/time.h> 83 #include <sys/kernel.h> 84 #include <sys/syslog.h> 85 #include <sys/kauth.h> 86 #include <sys/cprng.h> 87 #include <sys/kmem.h> 88 89 #include <net/if.h> 90 #include <net/if_types.h> 91 #include <net/if_llatbl.h> 92 #include <net/if_ether.h> 93 #include <net/if_dl.h> 94 #include <net/pfil.h> 95 #include <net/route.h> 96 97 #include <netinet/in.h> 98 #include <netinet/in_var.h> 99 100 #include <netinet/ip6.h> 101 #include <netinet6/ip6_var.h> 102 #include <netinet6/nd6.h> 103 #include <netinet6/mld6_var.h> 104 #include <netinet6/ip6_mroute.h> 105 #include <netinet6/in6_ifattach.h> 106 #include <netinet6/scope6_var.h> 107 108 #include <net/net_osdep.h> 109 110 #ifdef COMPAT_50 111 #include <compat/netinet6/in6_var.h> 112 #endif 113 114 MALLOC_DEFINE(M_IP6OPT, "ip6_options", "IPv6 options"); 115 116 /* enable backward compatibility code for obsoleted ioctls */ 117 #define COMPAT_IN6IFIOCTL 118 119 #ifdef IN6_DEBUG 120 #define IN6_DPRINTF(__fmt, ...) printf(__fmt, __VA_ARGS__) 121 #else 122 #define IN6_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0) 123 #endif /* IN6_DEBUG */ 124 125 /* 126 * Definitions of some constant IP6 addresses. 127 */ 128 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 129 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 130 const struct in6_addr in6addr_nodelocal_allnodes = 131 IN6ADDR_NODELOCAL_ALLNODES_INIT; 132 const struct in6_addr in6addr_linklocal_allnodes = 133 IN6ADDR_LINKLOCAL_ALLNODES_INIT; 134 const struct in6_addr in6addr_linklocal_allrouters = 135 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 136 137 const struct in6_addr in6mask0 = IN6MASK0; 138 const struct in6_addr in6mask32 = IN6MASK32; 139 const struct in6_addr in6mask64 = IN6MASK64; 140 const struct in6_addr in6mask96 = IN6MASK96; 141 const struct in6_addr in6mask128 = IN6MASK128; 142 143 const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6, 144 0, 0, IN6ADDR_ANY_INIT, 0}; 145 146 struct pslist_head in6_ifaddr_list; 147 kmutex_t in6_ifaddr_lock; 148 149 static int in6_lifaddr_ioctl(struct socket *, u_long, void *, 150 struct ifnet *); 151 static int in6_ifaddprefix(struct in6_ifaddr *); 152 static int in6_ifremprefix(struct in6_ifaddr *); 153 static int in6_ifinit(struct ifnet *, struct in6_ifaddr *, 154 const struct sockaddr_in6 *, int); 155 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 156 static int in6_update_ifa1(struct ifnet *, struct in6_aliasreq *, 157 struct in6_ifaddr **, struct psref *, int); 158 159 void 160 in6_init(void) 161 { 162 163 PSLIST_INIT(&in6_ifaddr_list); 164 mutex_init(&in6_ifaddr_lock, MUTEX_DEFAULT, IPL_NONE); 165 166 in6_sysctl_multicast_setup(NULL); 167 } 168 169 /* 170 * Add ownaddr as loopback rtentry. We previously add the route only if 171 * necessary (ex. on a p2p link). However, since we now manage addresses 172 * separately from prefixes, we should always add the route. We can't 173 * rely on the cloning mechanism from the corresponding interface route 174 * any more. 175 */ 176 void 177 in6_ifaddlocal(struct ifaddr *ifa) 178 { 179 180 if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &in6addr_any) || 181 (ifa->ifa_ifp->if_flags & IFF_POINTOPOINT && 182 IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), IFA_DSTIN6(ifa)))) 183 { 184 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 185 return; 186 } 187 188 rt_ifa_addlocal(ifa); 189 } 190 191 /* 192 * Remove loopback rtentry of ownaddr generated by in6_ifaddlocal(), 193 * if it exists. 194 */ 195 void 196 in6_ifremlocal(struct ifaddr *ifa) 197 { 198 struct in6_ifaddr *ia; 199 struct ifaddr *alt_ifa = NULL; 200 int ia_count = 0; 201 struct psref psref; 202 int s; 203 204 /* 205 * Some of BSD variants do not remove cloned routes 206 * from an interface direct route, when removing the direct route 207 * (see comments in net/net_osdep.h). Even for variants that do remove 208 * cloned routes, they could fail to remove the cloned routes when 209 * we handle multple addresses that share a common prefix. 210 * So, we should remove the route corresponding to the deleted address. 211 */ 212 213 /* 214 * Delete the entry only if exactly one ifaddr matches the 215 * address, ifa->ifa_addr. 216 * 217 * If more than one ifaddr matches, replace the ifaddr in 218 * the routing table, rt_ifa, with a different ifaddr than 219 * the one we are purging, ifa. It is important to do 220 * this, or else the routing table can accumulate dangling 221 * pointers rt->rt_ifa->ifa_ifp to destroyed interfaces, 222 * which will lead to crashes, later. (More than one ifaddr 223 * can match if we assign the same address to multiple---probably 224 * p2p---interfaces.) 225 * 226 * XXX An old comment at this place said, "we should avoid 227 * XXX such a configuration [i.e., interfaces with the same 228 * XXX addressed assigned --ed.] in IPv6...". I do not 229 * XXX agree, especially now that I have fixed the dangling 230 * XXX ifp-pointers bug. 231 */ 232 s = pserialize_read_enter(); 233 IN6_ADDRLIST_READER_FOREACH(ia) { 234 if (!IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) 235 continue; 236 if (ia->ia_ifp != ifa->ifa_ifp) 237 alt_ifa = &ia->ia_ifa; 238 if (++ia_count > 1 && alt_ifa != NULL) 239 break; 240 } 241 if (ia_count > 1 && alt_ifa != NULL) 242 ifa_acquire(alt_ifa, &psref); 243 pserialize_read_exit(s); 244 245 if (ia_count == 0) 246 return; 247 248 rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa); 249 250 if (ia_count > 1 && alt_ifa != NULL) 251 ifa_release(alt_ifa, &psref); 252 } 253 254 /* Add prefix route for the network. */ 255 static int 256 in6_ifaddprefix(struct in6_ifaddr *ia) 257 { 258 int error, flags = 0; 259 260 if (in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) == 128) { 261 if (ia->ia_dstaddr.sin6_family != AF_INET6) 262 /* We don't need to install a host route. */ 263 return 0; 264 flags |= RTF_HOST; 265 } 266 267 /* Is this a connected route for neighbour discovery? */ 268 if (nd6_need_cache(ia->ia_ifp)) 269 flags |= RTF_CONNECTED; 270 271 if ((error = rtinit(&ia->ia_ifa, RTM_ADD, RTF_UP | flags)) == 0) 272 ia->ia_flags |= IFA_ROUTE; 273 else if (error == EEXIST) 274 /* Existance of the route is not an error. */ 275 error = 0; 276 277 return error; 278 } 279 280 /* Delete network prefix route if present. 281 * Re-add it to another address if the prefix matches. */ 282 static int 283 in6_ifremprefix(struct in6_ifaddr *target) 284 { 285 int error, s; 286 struct in6_ifaddr *ia; 287 288 if ((target->ia_flags & IFA_ROUTE) == 0) 289 return 0; 290 291 s = pserialize_read_enter(); 292 IN6_ADDRLIST_READER_FOREACH(ia) { 293 if (target->ia_dstaddr.sin6_len) { 294 if (ia->ia_dstaddr.sin6_len == 0 || 295 !IN6_ARE_ADDR_EQUAL(&ia->ia_dstaddr.sin6_addr, 296 &target->ia_dstaddr.sin6_addr)) 297 continue; 298 } else { 299 if (!IN6_ARE_MASKED_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 300 &target->ia_addr.sin6_addr, 301 &target->ia_prefixmask.sin6_addr)) 302 continue; 303 } 304 305 /* 306 * if we got a matching prefix route, move IFA_ROUTE to him 307 */ 308 if ((ia->ia_flags & IFA_ROUTE) == 0) { 309 struct psref psref; 310 int bound = curlwp_bind(); 311 312 ia6_acquire(ia, &psref); 313 pserialize_read_exit(s); 314 315 rtinit(&target->ia_ifa, RTM_DELETE, 0); 316 target->ia_flags &= ~IFA_ROUTE; 317 318 error = in6_ifaddprefix(ia); 319 320 ia6_release(ia, &psref); 321 curlwp_bindx(bound); 322 323 return error; 324 } 325 } 326 pserialize_read_exit(s); 327 328 /* 329 * noone seem to have prefix route. remove it. 330 */ 331 rtinit(&target->ia_ifa, RTM_DELETE, 0); 332 target->ia_flags &= ~IFA_ROUTE; 333 return 0; 334 } 335 336 int 337 in6_mask2len(struct in6_addr *mask, u_char *lim0) 338 { 339 int x = 0, y; 340 u_char *lim = lim0, *p; 341 342 /* ignore the scope_id part */ 343 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) 344 lim = (u_char *)mask + sizeof(*mask); 345 for (p = (u_char *)mask; p < lim; x++, p++) { 346 if (*p != 0xff) 347 break; 348 } 349 y = 0; 350 if (p < lim) { 351 for (y = 0; y < NBBY; y++) { 352 if ((*p & (0x80 >> y)) == 0) 353 break; 354 } 355 } 356 357 /* 358 * when the limit pointer is given, do a stricter check on the 359 * remaining bits. 360 */ 361 if (p < lim) { 362 if (y != 0 && (*p & (0x00ff >> y)) != 0) 363 return -1; 364 for (p = p + 1; p < lim; p++) 365 if (*p != 0) 366 return -1; 367 } 368 369 return x * NBBY + y; 370 } 371 372 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 373 #define ia62ifa(ia6) (&((ia6)->ia_ifa)) 374 375 static int 376 in6_control1(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 377 { 378 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 379 struct in6_ifaddr *ia = NULL; 380 struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; 381 struct sockaddr_in6 *sa6; 382 int error, bound; 383 struct psref psref; 384 385 switch (cmd) { 386 case SIOCAADDRCTL_POLICY: 387 case SIOCDADDRCTL_POLICY: 388 /* Privileged. */ 389 return in6_src_ioctl(cmd, data); 390 /* 391 * XXX: Fix me, once we fix SIOCSIFADDR, SIOCIFDSTADDR, etc. 392 */ 393 case SIOCSIFADDR: 394 case SIOCSIFDSTADDR: 395 case SIOCSIFBRDADDR: 396 case SIOCSIFNETMASK: 397 return EOPNOTSUPP; 398 case SIOCGETSGCNT_IN6: 399 case SIOCGETMIFCNT_IN6: 400 return mrt6_ioctl(cmd, data); 401 case SIOCGIFADDRPREF: 402 case SIOCSIFADDRPREF: 403 if (ifp == NULL) 404 return EINVAL; 405 return ifaddrpref_ioctl(so, cmd, data, ifp); 406 } 407 408 if (ifp == NULL) 409 return EOPNOTSUPP; 410 411 switch (cmd) { 412 case SIOCSNDFLUSH_IN6: 413 case SIOCSPFXFLUSH_IN6: 414 case SIOCSRTRFLUSH_IN6: 415 case SIOCSDEFIFACE_IN6: 416 case SIOCSIFINFO_FLAGS: 417 case SIOCSIFINFO_IN6: 418 /* Privileged. */ 419 /* FALLTHROUGH */ 420 case OSIOCGIFINFO_IN6: 421 case SIOCGIFINFO_IN6: 422 case SIOCGDRLST_IN6: 423 case SIOCGPRLST_IN6: 424 case SIOCGNBRINFO_IN6: 425 case SIOCGDEFIFACE_IN6: 426 return nd6_ioctl(cmd, data, ifp); 427 } 428 429 switch (cmd) { 430 case SIOCSIFPREFIX_IN6: 431 case SIOCDIFPREFIX_IN6: 432 case SIOCAIFPREFIX_IN6: 433 case SIOCCIFPREFIX_IN6: 434 case SIOCSGIFPREFIX_IN6: 435 case SIOCGIFPREFIX_IN6: 436 log(LOG_NOTICE, 437 "prefix ioctls are now invalidated. " 438 "please use ifconfig.\n"); 439 return EOPNOTSUPP; 440 } 441 442 switch (cmd) { 443 case SIOCALIFADDR: 444 case SIOCDLIFADDR: 445 /* Privileged. */ 446 /* FALLTHROUGH */ 447 case SIOCGLIFADDR: 448 return in6_lifaddr_ioctl(so, cmd, data, ifp); 449 } 450 451 /* 452 * Find address for this interface, if it exists. 453 * 454 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 455 * only, and used the first interface address as the target of other 456 * operations (without checking ifra_addr). This was because netinet 457 * code/API assumed at most 1 interface address per interface. 458 * Since IPv6 allows a node to assign multiple addresses 459 * on a single interface, we almost always look and check the 460 * presence of ifra_addr, and reject invalid ones here. 461 * It also decreases duplicated code among SIOC*_IN6 operations. 462 */ 463 switch (cmd) { 464 case SIOCAIFADDR_IN6: 465 #ifdef OSIOCAIFADDR_IN6 466 case OSIOCAIFADDR_IN6: 467 #endif 468 #ifdef OSIOCSIFPHYADDR_IN6 469 case OSIOCSIFPHYADDR_IN6: 470 #endif 471 case SIOCSIFPHYADDR_IN6: 472 sa6 = &ifra->ifra_addr; 473 break; 474 case SIOCSIFADDR_IN6: 475 case SIOCGIFADDR_IN6: 476 case SIOCSIFDSTADDR_IN6: 477 case SIOCSIFNETMASK_IN6: 478 case SIOCGIFDSTADDR_IN6: 479 case SIOCGIFNETMASK_IN6: 480 case SIOCDIFADDR_IN6: 481 case SIOCGIFPSRCADDR_IN6: 482 case SIOCGIFPDSTADDR_IN6: 483 case SIOCGIFAFLAG_IN6: 484 case SIOCSNDFLUSH_IN6: 485 case SIOCSPFXFLUSH_IN6: 486 case SIOCSRTRFLUSH_IN6: 487 case SIOCGIFALIFETIME_IN6: 488 #ifdef OSIOCGIFALIFETIME_IN6 489 case OSIOCGIFALIFETIME_IN6: 490 #endif 491 case SIOCGIFSTAT_IN6: 492 case SIOCGIFSTAT_ICMP6: 493 sa6 = &ifr->ifr_addr; 494 break; 495 default: 496 sa6 = NULL; 497 break; 498 } 499 500 error = 0; 501 bound = curlwp_bind(); 502 if (sa6 && sa6->sin6_family == AF_INET6) { 503 if (sa6->sin6_scope_id != 0) 504 error = sa6_embedscope(sa6, 0); 505 else 506 error = in6_setscope(&sa6->sin6_addr, ifp, NULL); 507 if (error != 0) 508 goto out; 509 ia = in6ifa_ifpwithaddr_psref(ifp, &sa6->sin6_addr, &psref); 510 } else 511 ia = NULL; 512 513 switch (cmd) { 514 case SIOCSIFADDR_IN6: 515 case SIOCSIFDSTADDR_IN6: 516 case SIOCSIFNETMASK_IN6: 517 /* 518 * Since IPv6 allows a node to assign multiple addresses 519 * on a single interface, SIOCSIFxxx ioctls are deprecated. 520 */ 521 error = EINVAL; 522 goto release; 523 524 case SIOCDIFADDR_IN6: 525 /* 526 * for IPv4, we look for existing in_ifaddr here to allow 527 * "ifconfig if0 delete" to remove the first IPv4 address on 528 * the interface. For IPv6, as the spec allows multiple 529 * interface address from the day one, we consider "remove the 530 * first one" semantics to be not preferable. 531 */ 532 if (ia == NULL) { 533 error = EADDRNOTAVAIL; 534 goto out; 535 } 536 /* FALLTHROUGH */ 537 #ifdef OSIOCAIFADDR_IN6 538 case OSIOCAIFADDR_IN6: 539 #endif 540 case SIOCAIFADDR_IN6: 541 /* 542 * We always require users to specify a valid IPv6 address for 543 * the corresponding operation. 544 */ 545 if (ifra->ifra_addr.sin6_family != AF_INET6 || 546 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) { 547 error = EAFNOSUPPORT; 548 goto release; 549 } 550 /* Privileged. */ 551 552 break; 553 554 case SIOCGIFADDR_IN6: 555 /* This interface is basically deprecated. use SIOCGIFCONF. */ 556 /* FALLTHROUGH */ 557 case SIOCGIFAFLAG_IN6: 558 case SIOCGIFNETMASK_IN6: 559 case SIOCGIFDSTADDR_IN6: 560 case SIOCGIFALIFETIME_IN6: 561 #ifdef OSIOCGIFALIFETIME_IN6 562 case OSIOCGIFALIFETIME_IN6: 563 #endif 564 /* must think again about its semantics */ 565 if (ia == NULL) { 566 error = EADDRNOTAVAIL; 567 goto out; 568 } 569 break; 570 } 571 572 switch (cmd) { 573 574 case SIOCGIFADDR_IN6: 575 ifr->ifr_addr = ia->ia_addr; 576 error = sa6_recoverscope(&ifr->ifr_addr); 577 break; 578 579 case SIOCGIFDSTADDR_IN6: 580 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 581 error = EINVAL; 582 break; 583 } 584 /* 585 * XXX: should we check if ifa_dstaddr is NULL and return 586 * an error? 587 */ 588 ifr->ifr_dstaddr = ia->ia_dstaddr; 589 error = sa6_recoverscope(&ifr->ifr_dstaddr); 590 break; 591 592 case SIOCGIFNETMASK_IN6: 593 ifr->ifr_addr = ia->ia_prefixmask; 594 break; 595 596 case SIOCGIFAFLAG_IN6: 597 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 598 break; 599 600 case SIOCGIFSTAT_IN6: 601 if (ifp == NULL) { 602 error = EINVAL; 603 break; 604 } 605 memset(&ifr->ifr_ifru.ifru_stat, 0, 606 sizeof(ifr->ifr_ifru.ifru_stat)); 607 ifr->ifr_ifru.ifru_stat = 608 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->in6_ifstat; 609 break; 610 611 case SIOCGIFSTAT_ICMP6: 612 if (ifp == NULL) { 613 error = EINVAL; 614 break; 615 } 616 memset(&ifr->ifr_ifru.ifru_icmp6stat, 0, 617 sizeof(ifr->ifr_ifru.ifru_icmp6stat)); 618 ifr->ifr_ifru.ifru_icmp6stat = 619 *((struct in6_ifextra *)ifp->if_afdata[AF_INET6])->icmp6_ifstat; 620 break; 621 622 #ifdef OSIOCGIFALIFETIME_IN6 623 case OSIOCGIFALIFETIME_IN6: 624 #endif 625 case SIOCGIFALIFETIME_IN6: 626 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 627 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 628 time_t maxexpire; 629 struct in6_addrlifetime *retlt = 630 &ifr->ifr_ifru.ifru_lifetime; 631 632 /* 633 * XXX: adjust expiration time assuming time_t is 634 * signed. 635 */ 636 maxexpire = ((time_t)~0) & 637 ~((time_t)1 << ((sizeof(maxexpire) * NBBY) - 1)); 638 if (ia->ia6_lifetime.ia6t_vltime < 639 maxexpire - ia->ia6_updatetime) { 640 retlt->ia6t_expire = ia->ia6_updatetime + 641 ia->ia6_lifetime.ia6t_vltime; 642 retlt->ia6t_expire = retlt->ia6t_expire ? 643 time_mono_to_wall(retlt->ia6t_expire) : 644 0; 645 } else 646 retlt->ia6t_expire = maxexpire; 647 } 648 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 649 time_t maxexpire; 650 struct in6_addrlifetime *retlt = 651 &ifr->ifr_ifru.ifru_lifetime; 652 653 /* 654 * XXX: adjust expiration time assuming time_t is 655 * signed. 656 */ 657 maxexpire = ((time_t)~0) & 658 ~((time_t)1 << ((sizeof(maxexpire) * NBBY) - 1)); 659 if (ia->ia6_lifetime.ia6t_pltime < 660 maxexpire - ia->ia6_updatetime) { 661 retlt->ia6t_preferred = ia->ia6_updatetime + 662 ia->ia6_lifetime.ia6t_pltime; 663 retlt->ia6t_preferred = retlt->ia6t_preferred ? 664 time_mono_to_wall(retlt->ia6t_preferred) : 665 0; 666 } else 667 retlt->ia6t_preferred = maxexpire; 668 } 669 #ifdef OSIOCFIFALIFETIME_IN6 670 if (cmd == OSIOCFIFALIFETIME_IN6) 671 in6_addrlifetime_to_in6_addrlifetime50( 672 &ifr->ifru.ifru_lifetime); 673 #endif 674 break; 675 676 #ifdef OSIOCAIFADDR_IN6 677 case OSIOCAIFADDR_IN6: 678 in6_aliasreq50_to_in6_aliasreq(ifra); 679 /*FALLTHROUGH*/ 680 #endif 681 case SIOCAIFADDR_IN6: 682 { 683 struct in6_addrlifetime *lt; 684 685 /* reject read-only flags */ 686 if ((ifra->ifra_flags & IN6_IFF_DUPLICATED) != 0 || 687 (ifra->ifra_flags & IN6_IFF_DETACHED) != 0 || 688 (ifra->ifra_flags & IN6_IFF_TENTATIVE) != 0 || 689 (ifra->ifra_flags & IN6_IFF_NODAD) != 0 || 690 (ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0) { 691 error = EINVAL; 692 break; 693 } 694 /* 695 * ia6t_expire and ia6t_preferred won't be used for now, 696 * so just in case. 697 */ 698 lt = &ifra->ifra_lifetime; 699 if (lt->ia6t_expire != 0) 700 lt->ia6t_expire = time_wall_to_mono(lt->ia6t_expire); 701 if (lt->ia6t_preferred != 0) 702 lt->ia6t_preferred = 703 time_wall_to_mono(lt->ia6t_preferred); 704 /* 705 * make (ia == NULL) or update (ia != NULL) the interface 706 * address structure, and link it to the list. 707 */ 708 int s = splsoftnet(); 709 error = in6_update_ifa1(ifp, ifra, &ia, &psref, 0); 710 splx(s); 711 if (error) 712 break; 713 pfil_run_addrhooks(if_pfil, cmd, &ia->ia_ifa); 714 break; 715 } 716 717 case SIOCDIFADDR_IN6: 718 ia6_release(ia, &psref); 719 ifaref(&ia->ia_ifa); 720 in6_purgeaddr(&ia->ia_ifa); 721 pfil_run_addrhooks(if_pfil, cmd, &ia->ia_ifa); 722 ifafree(&ia->ia_ifa); 723 ia = NULL; 724 break; 725 726 default: 727 error = ENOTTY; 728 } 729 release: 730 ia6_release(ia, &psref); 731 out: 732 curlwp_bindx(bound); 733 return error; 734 } 735 736 int 737 in6_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp) 738 { 739 int error, s; 740 741 switch (cmd) { 742 case SIOCSNDFLUSH_IN6: 743 case SIOCSPFXFLUSH_IN6: 744 case SIOCSRTRFLUSH_IN6: 745 case SIOCSDEFIFACE_IN6: 746 case SIOCSIFINFO_FLAGS: 747 case SIOCSIFINFO_IN6: 748 749 case SIOCALIFADDR: 750 case SIOCDLIFADDR: 751 752 case SIOCDIFADDR_IN6: 753 #ifdef OSIOCAIFADDR_IN6 754 case OSIOCAIFADDR_IN6: 755 #endif 756 case SIOCAIFADDR_IN6: 757 758 case SIOCAADDRCTL_POLICY: 759 case SIOCDADDRCTL_POLICY: 760 761 if (kauth_authorize_network(curlwp->l_cred, 762 KAUTH_NETWORK_SOCKET, 763 KAUTH_REQ_NETWORK_SOCKET_SETPRIV, 764 so, NULL, NULL)) 765 return EPERM; 766 break; 767 } 768 769 s = splsoftnet(); 770 SOFTNET_LOCK_UNLESS_NET_MPSAFE(); 771 error = in6_control1(so , cmd, data, ifp); 772 SOFTNET_UNLOCK_UNLESS_NET_MPSAFE(); 773 splx(s); 774 return error; 775 } 776 777 static int 778 in6_get_llsol_addr(struct in6_addr *llsol, struct ifnet *ifp, 779 struct in6_addr *ip6) 780 { 781 int error; 782 783 memset(llsol, 0, sizeof(struct in6_addr)); 784 llsol->s6_addr16[0] = htons(0xff02); 785 llsol->s6_addr32[1] = 0; 786 llsol->s6_addr32[2] = htonl(1); 787 llsol->s6_addr32[3] = ip6->s6_addr32[3]; 788 llsol->s6_addr8[12] = 0xff; 789 790 error = in6_setscope(llsol, ifp, NULL); 791 if (error != 0) { 792 /* XXX: should not happen */ 793 log(LOG_ERR, "%s: in6_setscope failed\n", __func__); 794 } 795 796 return error; 797 } 798 799 static int 800 in6_join_mcastgroups(struct in6_aliasreq *ifra, struct in6_ifaddr *ia, 801 struct ifnet *ifp, int flags) 802 { 803 int error; 804 struct sockaddr_in6 mltaddr, mltmask; 805 struct in6_multi_mship *imm; 806 struct in6_addr llsol; 807 struct rtentry *rt; 808 int dad_delay; 809 char ip6buf[INET6_ADDRSTRLEN]; 810 811 /* join solicited multicast addr for new host id */ 812 error = in6_get_llsol_addr(&llsol, ifp, &ifra->ifra_addr.sin6_addr); 813 if (error != 0) 814 goto out; 815 dad_delay = 0; 816 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 817 /* 818 * We need a random delay for DAD on the address 819 * being configured. It also means delaying 820 * transmission of the corresponding MLD report to 821 * avoid report collision. 822 * [draft-ietf-ipv6-rfc2462bis-02.txt] 823 */ 824 dad_delay = cprng_fast32() % (MAX_RTR_SOLICITATION_DELAY * hz); 825 } 826 827 #define MLTMASK_LEN 4 /* mltmask's masklen (=32bit=4octet) */ 828 /* join solicited multicast addr for new host id */ 829 imm = in6_joingroup(ifp, &llsol, &error, dad_delay); 830 if (!imm) { 831 nd6log(LOG_ERR, 832 "addmulti failed for %s on %s (errno=%d)\n", 833 IN6_PRINT(ip6buf, &llsol), if_name(ifp), error); 834 goto out; 835 } 836 mutex_enter(&in6_ifaddr_lock); 837 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 838 mutex_exit(&in6_ifaddr_lock); 839 840 sockaddr_in6_init(&mltmask, &in6mask32, 0, 0, 0); 841 842 /* 843 * join link-local all-nodes address 844 */ 845 sockaddr_in6_init(&mltaddr, &in6addr_linklocal_allnodes, 846 0, 0, 0); 847 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0) 848 goto out; /* XXX: should not fail */ 849 850 /* 851 * XXX: do we really need this automatic routes? 852 * We should probably reconsider this stuff. Most applications 853 * actually do not need the routes, since they usually specify 854 * the outgoing interface. 855 */ 856 rt = rtalloc1(sin6tosa(&mltaddr), 0); 857 if (rt) { 858 if (memcmp(&mltaddr.sin6_addr, 859 &satocsin6(rt_getkey(rt))->sin6_addr, 860 MLTMASK_LEN)) { 861 rt_unref(rt); 862 rt = NULL; 863 } else if (rt->rt_ifp != ifp) { 864 IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) " 865 "network %04x:%04x::/32 = %04x:%04x::/32\n", 866 __func__, rt->rt_ifp, ifp, ifp->if_xname, 867 ntohs(mltaddr.sin6_addr.s6_addr16[0]), 868 ntohs(mltaddr.sin6_addr.s6_addr16[1]), 869 satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[0], 870 satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[1]); 871 rt_replace_ifa(rt, &ia->ia_ifa); 872 rt->rt_ifp = ifp; 873 } 874 } 875 if (!rt) { 876 struct rt_addrinfo info; 877 878 memset(&info, 0, sizeof(info)); 879 info.rti_info[RTAX_DST] = sin6tosa(&mltaddr); 880 info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia->ia_addr); 881 info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask); 882 info.rti_info[RTAX_IFA] = sin6tosa(&ia->ia_addr); 883 /* XXX: we need RTF_CONNECTED to fake nd6_rtrequest */ 884 info.rti_flags = RTF_UP | RTF_CONNECTED; 885 error = rtrequest1(RTM_ADD, &info, NULL); 886 if (error) 887 goto out; 888 } else { 889 rt_unref(rt); 890 } 891 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 892 if (!imm) { 893 nd6log(LOG_WARNING, 894 "addmulti failed for %s on %s (errno=%d)\n", 895 IN6_PRINT(ip6buf, &mltaddr.sin6_addr), 896 if_name(ifp), error); 897 goto out; 898 } 899 mutex_enter(&in6_ifaddr_lock); 900 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 901 mutex_exit(&in6_ifaddr_lock); 902 903 /* 904 * join node information group address 905 */ 906 dad_delay = 0; 907 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 908 /* 909 * The spec doesn't say anything about delay for this 910 * group, but the same logic should apply. 911 */ 912 dad_delay = cprng_fast32() % (MAX_RTR_SOLICITATION_DELAY * hz); 913 } 914 if (in6_nigroup(ifp, hostname, hostnamelen, &mltaddr) != 0) 915 ; 916 else if ((imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 917 dad_delay)) == NULL) { /* XXX jinmei */ 918 nd6log(LOG_WARNING, 919 "addmulti failed for %s on %s (errno=%d)\n", 920 IN6_PRINT(ip6buf, &mltaddr.sin6_addr), 921 if_name(ifp), error); 922 /* XXX not very fatal, go on... */ 923 } else { 924 mutex_enter(&in6_ifaddr_lock); 925 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 926 mutex_exit(&in6_ifaddr_lock); 927 } 928 929 930 /* 931 * join interface-local all-nodes address. 932 * (ff01::1%ifN, and ff01::%ifN/32) 933 */ 934 mltaddr.sin6_addr = in6addr_nodelocal_allnodes; 935 if ((error = in6_setscope(&mltaddr.sin6_addr, ifp, NULL)) != 0) 936 goto out; /* XXX: should not fail */ 937 938 /* XXX: again, do we really need the route? */ 939 rt = rtalloc1(sin6tosa(&mltaddr), 0); 940 if (rt) { 941 /* 32bit came from "mltmask" */ 942 if (memcmp(&mltaddr.sin6_addr, 943 &satocsin6(rt_getkey(rt))->sin6_addr, 944 32 / NBBY)) { 945 rt_unref(rt); 946 rt = NULL; 947 } else if (rt->rt_ifp != ifp) { 948 IN6_DPRINTF("%s: rt_ifp %p -> %p (%s) " 949 "network %04x:%04x::/32 = %04x:%04x::/32\n", 950 __func__, rt->rt_ifp, ifp, ifp->if_xname, 951 ntohs(mltaddr.sin6_addr.s6_addr16[0]), 952 ntohs(mltaddr.sin6_addr.s6_addr16[1]), 953 satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[0], 954 satocsin6(rt_getkey(rt))->sin6_addr.s6_addr16[1]); 955 rt_replace_ifa(rt, &ia->ia_ifa); 956 rt->rt_ifp = ifp; 957 } 958 } 959 if (!rt) { 960 struct rt_addrinfo info; 961 962 memset(&info, 0, sizeof(info)); 963 info.rti_info[RTAX_DST] = sin6tosa(&mltaddr); 964 info.rti_info[RTAX_GATEWAY] = sin6tosa(&ia->ia_addr); 965 info.rti_info[RTAX_NETMASK] = sin6tosa(&mltmask); 966 info.rti_info[RTAX_IFA] = sin6tosa(&ia->ia_addr); 967 info.rti_flags = RTF_UP | RTF_CONNECTED; 968 error = rtrequest1(RTM_ADD, &info, NULL); 969 if (error) 970 goto out; 971 #undef MLTMASK_LEN 972 } else { 973 rt_unref(rt); 974 } 975 imm = in6_joingroup(ifp, &mltaddr.sin6_addr, &error, 0); 976 if (!imm) { 977 nd6log(LOG_WARNING, 978 "addmulti failed for %s on %s (errno=%d)\n", 979 IN6_PRINT(ip6buf, &mltaddr.sin6_addr), 980 if_name(ifp), error); 981 goto out; 982 } else { 983 mutex_enter(&in6_ifaddr_lock); 984 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 985 mutex_exit(&in6_ifaddr_lock); 986 } 987 return 0; 988 989 out: 990 KASSERT(error != 0); 991 return error; 992 } 993 994 /* 995 * Update parameters of an IPv6 interface address. 996 * If necessary, a new entry is created and linked into address chains. 997 * This function is separated from in6_control(). 998 * XXX: should this be performed under splsoftnet()? 999 */ 1000 static int 1001 in6_update_ifa1(struct ifnet *ifp, struct in6_aliasreq *ifra, 1002 struct in6_ifaddr **iap, struct psref *psref, int flags) 1003 { 1004 int error = 0, hostIsNew = 0, plen = -1; 1005 struct sockaddr_in6 dst6; 1006 struct in6_addrlifetime *lt; 1007 int dad_delay, was_tentative; 1008 struct in6_ifaddr *ia = iap ? *iap : NULL; 1009 char ip6buf[INET6_ADDRSTRLEN]; 1010 1011 KASSERT((iap == NULL && psref == NULL) || 1012 (iap != NULL && psref != NULL)); 1013 1014 /* Validate parameters */ 1015 if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 1016 return EINVAL; 1017 1018 /* 1019 * The destination address for a p2p link must have a family 1020 * of AF_UNSPEC or AF_INET6. 1021 */ 1022 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 1023 ifra->ifra_dstaddr.sin6_family != AF_INET6 && 1024 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 1025 return EAFNOSUPPORT; 1026 /* 1027 * validate ifra_prefixmask. don't check sin6_family, netmask 1028 * does not carry fields other than sin6_len. 1029 */ 1030 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 1031 return EINVAL; 1032 /* 1033 * Because the IPv6 address architecture is classless, we require 1034 * users to specify a (non 0) prefix length (mask) for a new address. 1035 * We also require the prefix (when specified) mask is valid, and thus 1036 * reject a non-consecutive mask. 1037 */ 1038 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 1039 return EINVAL; 1040 if (ifra->ifra_prefixmask.sin6_len != 0) { 1041 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 1042 (u_char *)&ifra->ifra_prefixmask + 1043 ifra->ifra_prefixmask.sin6_len); 1044 if (plen <= 0) 1045 return EINVAL; 1046 } else { 1047 /* 1048 * In this case, ia must not be NULL. We just use its prefix 1049 * length. 1050 */ 1051 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 1052 } 1053 /* 1054 * If the destination address on a p2p interface is specified, 1055 * and the address is a scoped one, validate/set the scope 1056 * zone identifier. 1057 */ 1058 dst6 = ifra->ifra_dstaddr; 1059 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 1060 (dst6.sin6_family == AF_INET6)) { 1061 struct in6_addr in6_tmp; 1062 u_int32_t zoneid; 1063 1064 in6_tmp = dst6.sin6_addr; 1065 if (in6_setscope(&in6_tmp, ifp, &zoneid)) 1066 return EINVAL; /* XXX: should be impossible */ 1067 1068 if (dst6.sin6_scope_id != 0) { 1069 if (dst6.sin6_scope_id != zoneid) 1070 return EINVAL; 1071 } else /* user omit to specify the ID. */ 1072 dst6.sin6_scope_id = zoneid; 1073 1074 /* convert into the internal form */ 1075 if (sa6_embedscope(&dst6, 0)) 1076 return EINVAL; /* XXX: should be impossible */ 1077 } 1078 /* 1079 * The destination address can be specified only for a p2p or a 1080 * loopback interface. If specified, the corresponding prefix length 1081 * must be 128. 1082 */ 1083 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 1084 #ifdef FORCE_P2PPLEN 1085 int i; 1086 #endif 1087 1088 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 1089 /* XXX: noisy message */ 1090 nd6log(LOG_INFO, "a destination can " 1091 "be specified for a p2p or a loopback IF only\n"); 1092 return EINVAL; 1093 } 1094 if (plen != 128) { 1095 nd6log(LOG_INFO, "prefixlen should " 1096 "be 128 when dstaddr is specified\n"); 1097 #ifdef FORCE_P2PPLEN 1098 /* 1099 * To be compatible with old configurations, 1100 * such as ifconfig gif0 inet6 2001::1 2001::2 1101 * prefixlen 126, we override the specified 1102 * prefixmask as if the prefix length was 128. 1103 */ 1104 ifra->ifra_prefixmask.sin6_len = 1105 sizeof(struct sockaddr_in6); 1106 for (i = 0; i < 4; i++) 1107 ifra->ifra_prefixmask.sin6_addr.s6_addr32[i] = 1108 0xffffffff; 1109 plen = 128; 1110 #else 1111 return EINVAL; 1112 #endif 1113 } 1114 } 1115 /* lifetime consistency check */ 1116 lt = &ifra->ifra_lifetime; 1117 if (lt->ia6t_pltime > lt->ia6t_vltime) 1118 return EINVAL; 1119 if (lt->ia6t_vltime == 0) { 1120 /* 1121 * the following log might be noisy, but this is a typical 1122 * configuration mistake or a tool's bug. 1123 */ 1124 nd6log(LOG_INFO, "valid lifetime is 0 for %s\n", 1125 IN6_PRINT(ip6buf, &ifra->ifra_addr.sin6_addr)); 1126 1127 if (ia == NULL) 1128 return 0; /* there's nothing to do */ 1129 } 1130 1131 /* 1132 * If this is a new address, allocate a new ifaddr and link it 1133 * into chains. 1134 */ 1135 if (ia == NULL) { 1136 hostIsNew = 1; 1137 /* 1138 * When in6_update_ifa() is called in a process of a received 1139 * RA, it is called under an interrupt context. So, we should 1140 * call malloc with M_NOWAIT. 1141 */ 1142 ia = malloc(sizeof(*ia), M_IFADDR, M_NOWAIT|M_ZERO); 1143 if (ia == NULL) 1144 return ENOBUFS; 1145 LIST_INIT(&ia->ia6_memberships); 1146 /* Initialize the address and masks, and put time stamp */ 1147 ia->ia_ifa.ifa_addr = sin6tosa(&ia->ia_addr); 1148 ia->ia_addr.sin6_family = AF_INET6; 1149 ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 1150 ia->ia6_createtime = time_uptime; 1151 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 1152 /* 1153 * XXX: some functions expect that ifa_dstaddr is not 1154 * NULL for p2p interfaces. 1155 */ 1156 ia->ia_ifa.ifa_dstaddr = sin6tosa(&ia->ia_dstaddr); 1157 } else { 1158 ia->ia_ifa.ifa_dstaddr = NULL; 1159 } 1160 ia->ia_ifa.ifa_netmask = sin6tosa(&ia->ia_prefixmask); 1161 1162 ia->ia_ifp = ifp; 1163 IN6_ADDRLIST_ENTRY_INIT(ia); 1164 ifa_psref_init(&ia->ia_ifa); 1165 } 1166 1167 /* update timestamp */ 1168 ia->ia6_updatetime = time_uptime; 1169 1170 /* set prefix mask */ 1171 if (ifra->ifra_prefixmask.sin6_len) { 1172 if (ia->ia_prefixmask.sin6_len) { 1173 /* 1174 * We prohibit changing the prefix length of an 1175 * existing autoconf address, because the operation 1176 * would confuse prefix management. 1177 */ 1178 if (ia->ia6_ndpr != NULL && 1179 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != 1180 plen) 1181 { 1182 nd6log(LOG_INFO, "the prefix length of an" 1183 " existing (%s) autoconf address should" 1184 " not be changed\n", 1185 IN6_PRINT(ip6buf, 1186 &ia->ia_addr.sin6_addr)); 1187 error = EINVAL; 1188 if (hostIsNew) 1189 free(ia, M_IFADDR); 1190 return error; 1191 } 1192 1193 if (!IN6_ARE_ADDR_EQUAL(&ia->ia_prefixmask.sin6_addr, 1194 &ifra->ifra_prefixmask.sin6_addr)) 1195 in6_ifremprefix(ia); 1196 } 1197 ia->ia_prefixmask = ifra->ifra_prefixmask; 1198 } 1199 1200 /* Set destination address. */ 1201 if (dst6.sin6_family == AF_INET6) { 1202 if (!IN6_ARE_ADDR_EQUAL(&dst6.sin6_addr, 1203 &ia->ia_dstaddr.sin6_addr)) 1204 in6_ifremprefix(ia); 1205 ia->ia_dstaddr = dst6; 1206 } 1207 1208 /* 1209 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 1210 * to see if the address is deprecated or invalidated, but initialize 1211 * these members for applications. 1212 */ 1213 ia->ia6_lifetime = ifra->ifra_lifetime; 1214 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 1215 ia->ia6_lifetime.ia6t_expire = 1216 time_uptime + ia->ia6_lifetime.ia6t_vltime; 1217 } else 1218 ia->ia6_lifetime.ia6t_expire = 0; 1219 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 1220 ia->ia6_lifetime.ia6t_preferred = 1221 time_uptime + ia->ia6_lifetime.ia6t_pltime; 1222 } else 1223 ia->ia6_lifetime.ia6t_preferred = 0; 1224 1225 /* 1226 * configure address flags. 1227 * We need to preserve tentative state so DAD works if 1228 * something adds the same address before DAD finishes. 1229 */ 1230 was_tentative = ia->ia6_flags & (IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED); 1231 ia->ia6_flags = ifra->ifra_flags; 1232 1233 /* 1234 * Make the address tentative before joining multicast addresses, 1235 * so that corresponding MLD responses would not have a tentative 1236 * source address. 1237 */ 1238 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 1239 if (ifp->if_link_state == LINK_STATE_DOWN) { 1240 ia->ia6_flags |= IN6_IFF_DETACHED; 1241 ia->ia6_flags &= ~IN6_IFF_TENTATIVE; 1242 } else if ((hostIsNew || was_tentative) && if_do_dad(ifp)) 1243 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1244 1245 /* 1246 * backward compatibility - if IN6_IFF_DEPRECATED is set from the 1247 * userland, make it deprecated. 1248 */ 1249 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 1250 ia->ia6_lifetime.ia6t_pltime = 0; 1251 ia->ia6_lifetime.ia6t_preferred = time_uptime; 1252 } 1253 1254 if (hostIsNew) { 1255 /* 1256 * We need a reference to ia before calling in6_ifinit. 1257 * Otherwise ia can be freed in in6_ifinit accidentally. 1258 */ 1259 ifaref(&ia->ia_ifa); 1260 } 1261 1262 /* Must execute in6_ifinit and ifa_insert atomically */ 1263 mutex_enter(&in6_ifaddr_lock); 1264 1265 /* reset the interface and routing table appropriately. */ 1266 error = in6_ifinit(ifp, ia, &ifra->ifra_addr, hostIsNew); 1267 if (error != 0) { 1268 if (hostIsNew) 1269 free(ia, M_IFADDR); 1270 mutex_exit(&in6_ifaddr_lock); 1271 return error; 1272 } 1273 1274 /* 1275 * We are done if we have simply modified an existing address. 1276 */ 1277 if (!hostIsNew) { 1278 mutex_exit(&in6_ifaddr_lock); 1279 return error; 1280 } 1281 1282 /* 1283 * Insert ia to the global list and ifa to the interface's list. 1284 * A reference to it is already gained above. 1285 */ 1286 IN6_ADDRLIST_WRITER_INSERT_TAIL(ia); 1287 ifa_insert(ifp, &ia->ia_ifa); 1288 1289 mutex_exit(&in6_ifaddr_lock); 1290 1291 /* 1292 * Beyond this point, we should call in6_purgeaddr upon an error, 1293 * not just go to unlink. 1294 */ 1295 1296 /* join necessary multicast groups */ 1297 if ((ifp->if_flags & IFF_MULTICAST) != 0) { 1298 error = in6_join_mcastgroups(ifra, ia, ifp, flags); 1299 if (error != 0) 1300 goto cleanup; 1301 } 1302 1303 if (nd6_need_cache(ifp)) { 1304 /* XXX maybe unnecessary */ 1305 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 1306 ia->ia_ifa.ifa_flags |= RTF_CONNECTED; 1307 } 1308 1309 /* 1310 * Perform DAD, if needed. 1311 * XXX It may be of use, if we can administratively 1312 * disable DAD. 1313 */ 1314 if (hostIsNew && if_do_dad(ifp) && 1315 ((ifra->ifra_flags & IN6_IFF_NODAD) == 0) && 1316 (ia->ia6_flags & IN6_IFF_TENTATIVE)) 1317 { 1318 int mindelay, maxdelay; 1319 1320 dad_delay = 0; 1321 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1322 struct in6_addr llsol; 1323 struct in6_multi *in6m_sol = NULL; 1324 /* 1325 * We need to impose a delay before sending an NS 1326 * for DAD. Check if we also needed a delay for the 1327 * corresponding MLD message. If we did, the delay 1328 * should be larger than the MLD delay (this could be 1329 * relaxed a bit, but this simple logic is at least 1330 * safe). 1331 */ 1332 mindelay = 0; 1333 error = in6_get_llsol_addr(&llsol, ifp, 1334 &ifra->ifra_addr.sin6_addr); 1335 in6_multi_lock(RW_READER); 1336 if (error == 0) 1337 in6m_sol = in6_lookup_multi(&llsol, ifp); 1338 if (in6m_sol != NULL && 1339 in6m_sol->in6m_state == MLD_REPORTPENDING) { 1340 mindelay = in6m_sol->in6m_timer; 1341 } 1342 in6_multi_unlock(); 1343 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1344 if (maxdelay - mindelay == 0) 1345 dad_delay = 0; 1346 else { 1347 dad_delay = 1348 (cprng_fast32() % (maxdelay - mindelay)) + 1349 mindelay; 1350 } 1351 } 1352 /* +1 ensures callout is always used */ 1353 nd6_dad_start(&ia->ia_ifa, dad_delay + 1); 1354 } 1355 1356 if (iap != NULL) { 1357 *iap = ia; 1358 if (hostIsNew) 1359 ia6_acquire(ia, psref); 1360 } 1361 1362 return 0; 1363 1364 cleanup: 1365 in6_purgeaddr(&ia->ia_ifa); 1366 return error; 1367 } 1368 1369 int 1370 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags) 1371 { 1372 int rc, s; 1373 1374 s = splsoftnet(); 1375 rc = in6_update_ifa1(ifp, ifra, NULL, NULL, flags); 1376 splx(s); 1377 return rc; 1378 } 1379 1380 void 1381 in6_purgeaddr(struct ifaddr *ifa) 1382 { 1383 struct ifnet *ifp = ifa->ifa_ifp; 1384 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1385 struct in6_multi_mship *imm; 1386 1387 /* KASSERT(!ifa_held(ifa)); XXX need ifa_not_held (psref_not_held) */ 1388 KASSERT(IFNET_LOCKED(ifp)); 1389 1390 ifa->ifa_flags |= IFA_DESTROYING; 1391 1392 /* stop DAD processing */ 1393 nd6_dad_stop(ifa); 1394 1395 /* Delete any network route. */ 1396 in6_ifremprefix(ia); 1397 1398 /* Remove ownaddr's loopback rtentry, if it exists. */ 1399 in6_ifremlocal(&(ia->ia_ifa)); 1400 1401 /* 1402 * leave from multicast groups we have joined for the interface 1403 */ 1404 again: 1405 mutex_enter(&in6_ifaddr_lock); 1406 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) { 1407 LIST_REMOVE(imm, i6mm_chain); 1408 mutex_exit(&in6_ifaddr_lock); 1409 KASSERT(imm->i6mm_maddr->in6m_ifp == ifp); 1410 in6_leavegroup(imm); 1411 goto again; 1412 } 1413 mutex_exit(&in6_ifaddr_lock); 1414 1415 in6_unlink_ifa(ia, ifp); 1416 } 1417 1418 static void 1419 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 1420 { 1421 int s = splsoftnet(); 1422 1423 mutex_enter(&in6_ifaddr_lock); 1424 IN6_ADDRLIST_WRITER_REMOVE(ia); 1425 ifa_remove(ifp, &ia->ia_ifa); 1426 mutex_exit(&in6_ifaddr_lock); 1427 1428 /* 1429 * Release the reference to the ND prefix. 1430 */ 1431 if (ia->ia6_ndpr != NULL) { 1432 nd6_prefix_unref(ia->ia6_ndpr); 1433 ia->ia6_ndpr = NULL; 1434 } 1435 1436 /* 1437 * Also, if the address being removed is autoconf'ed, call 1438 * nd6_pfxlist_onlink_check() since the release might affect the status of 1439 * other (detached) addresses. 1440 */ 1441 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) != 0) { 1442 ND6_WLOCK(); 1443 nd6_pfxlist_onlink_check(); 1444 ND6_UNLOCK(); 1445 } 1446 1447 IN6_ADDRLIST_ENTRY_DESTROY(ia); 1448 1449 /* 1450 * release another refcnt for the link from in6_ifaddr. 1451 * Note that we should decrement the refcnt at least once for all *BSD. 1452 */ 1453 ifafree(&ia->ia_ifa); 1454 1455 splx(s); 1456 } 1457 1458 void 1459 in6_purgeif(struct ifnet *ifp) 1460 { 1461 1462 IFNET_LOCK(ifp); 1463 in6_ifdetach(ifp); 1464 IFNET_UNLOCK(ifp); 1465 } 1466 1467 void 1468 in6_purge_mcast_references(struct in6_multi *in6m) 1469 { 1470 struct in6_ifaddr *ia; 1471 1472 KASSERT(in6_multi_locked(RW_WRITER)); 1473 1474 mutex_enter(&in6_ifaddr_lock); 1475 IN6_ADDRLIST_WRITER_FOREACH(ia) { 1476 struct in6_multi_mship *imm; 1477 LIST_FOREACH(imm, &ia->ia6_memberships, i6mm_chain) { 1478 if (imm->i6mm_maddr == in6m) 1479 imm->i6mm_maddr = NULL; 1480 } 1481 } 1482 mutex_exit(&in6_ifaddr_lock); 1483 } 1484 1485 /* 1486 * SIOC[GAD]LIFADDR. 1487 * SIOCGLIFADDR: get first address. (?) 1488 * SIOCGLIFADDR with IFLR_PREFIX: 1489 * get first address that matches the specified prefix. 1490 * SIOCALIFADDR: add the specified address. 1491 * SIOCALIFADDR with IFLR_PREFIX: 1492 * add the specified prefix, filling hostid part from 1493 * the first link-local address. prefixlen must be <= 64. 1494 * SIOCDLIFADDR: delete the specified address. 1495 * SIOCDLIFADDR with IFLR_PREFIX: 1496 * delete the first address that matches the specified prefix. 1497 * return values: 1498 * EINVAL on invalid parameters 1499 * EADDRNOTAVAIL on prefix match failed/specified address not found 1500 * other values may be returned from in6_ioctl() 1501 * 1502 * NOTE: SIOCALIFADDR(with IFLR_PREFIX set) allows prefixlen less than 64. 1503 * this is to accommodate address naming scheme other than RFC2374, 1504 * in the future. 1505 * RFC2373 defines interface id to be 64bit, but it allows non-RFC2374 1506 * address encoding scheme. (see figure on page 8) 1507 */ 1508 static int 1509 in6_lifaddr_ioctl(struct socket *so, u_long cmd, void *data, 1510 struct ifnet *ifp) 1511 { 1512 struct in6_ifaddr *ia = NULL; /* XXX gcc 4.8 maybe-uninitialized */ 1513 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 1514 struct ifaddr *ifa; 1515 struct sockaddr *sa; 1516 1517 /* sanity checks */ 1518 if (!data || !ifp) { 1519 panic("invalid argument to in6_lifaddr_ioctl"); 1520 /* NOTREACHED */ 1521 } 1522 1523 switch (cmd) { 1524 case SIOCGLIFADDR: 1525 /* address must be specified on GET with IFLR_PREFIX */ 1526 if ((iflr->flags & IFLR_PREFIX) == 0) 1527 break; 1528 /* FALLTHROUGH */ 1529 case SIOCALIFADDR: 1530 case SIOCDLIFADDR: 1531 /* address must be specified on ADD and DELETE */ 1532 sa = (struct sockaddr *)&iflr->addr; 1533 if (sa->sa_family != AF_INET6) 1534 return EINVAL; 1535 if (sa->sa_len != sizeof(struct sockaddr_in6)) 1536 return EINVAL; 1537 /* XXX need improvement */ 1538 sa = (struct sockaddr *)&iflr->dstaddr; 1539 if (sa->sa_family && sa->sa_family != AF_INET6) 1540 return EINVAL; 1541 if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in6)) 1542 return EINVAL; 1543 break; 1544 default: /* shouldn't happen */ 1545 #if 0 1546 panic("invalid cmd to in6_lifaddr_ioctl"); 1547 /* NOTREACHED */ 1548 #else 1549 return EOPNOTSUPP; 1550 #endif 1551 } 1552 if (sizeof(struct in6_addr) * NBBY < iflr->prefixlen) 1553 return EINVAL; 1554 1555 switch (cmd) { 1556 case SIOCALIFADDR: 1557 { 1558 struct in6_aliasreq ifra; 1559 struct in6_addr *xhostid = NULL; 1560 int prefixlen; 1561 int bound = curlwp_bind(); 1562 struct psref psref; 1563 1564 if ((iflr->flags & IFLR_PREFIX) != 0) { 1565 struct sockaddr_in6 *sin6; 1566 1567 /* 1568 * xhostid is to fill in the hostid part of the 1569 * address. xhostid points to the first link-local 1570 * address attached to the interface. 1571 */ 1572 ia = in6ifa_ifpforlinklocal_psref(ifp, 0, &psref); 1573 if (ia == NULL) { 1574 curlwp_bindx(bound); 1575 return EADDRNOTAVAIL; 1576 } 1577 xhostid = IFA_IN6(&ia->ia_ifa); 1578 1579 /* prefixlen must be <= 64. */ 1580 if (64 < iflr->prefixlen) { 1581 ia6_release(ia, &psref); 1582 curlwp_bindx(bound); 1583 return EINVAL; 1584 } 1585 prefixlen = iflr->prefixlen; 1586 1587 /* hostid part must be zero. */ 1588 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1589 if (sin6->sin6_addr.s6_addr32[2] != 0 1590 || sin6->sin6_addr.s6_addr32[3] != 0) { 1591 ia6_release(ia, &psref); 1592 curlwp_bindx(bound); 1593 return EINVAL; 1594 } 1595 } else 1596 prefixlen = iflr->prefixlen; 1597 1598 /* copy args to in6_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 1599 memset(&ifra, 0, sizeof(ifra)); 1600 memcpy(ifra.ifra_name, iflr->iflr_name, sizeof(ifra.ifra_name)); 1601 1602 memcpy(&ifra.ifra_addr, &iflr->addr, 1603 ((struct sockaddr *)&iflr->addr)->sa_len); 1604 if (xhostid) { 1605 /* fill in hostid part */ 1606 ifra.ifra_addr.sin6_addr.s6_addr32[2] = 1607 xhostid->s6_addr32[2]; 1608 ifra.ifra_addr.sin6_addr.s6_addr32[3] = 1609 xhostid->s6_addr32[3]; 1610 } 1611 1612 if (((struct sockaddr *)&iflr->dstaddr)->sa_family) { /* XXX */ 1613 memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr, 1614 ((struct sockaddr *)&iflr->dstaddr)->sa_len); 1615 if (xhostid) { 1616 ifra.ifra_dstaddr.sin6_addr.s6_addr32[2] = 1617 xhostid->s6_addr32[2]; 1618 ifra.ifra_dstaddr.sin6_addr.s6_addr32[3] = 1619 xhostid->s6_addr32[3]; 1620 } 1621 } 1622 if (xhostid) { 1623 ia6_release(ia, &psref); 1624 ia = NULL; 1625 } 1626 curlwp_bindx(bound); 1627 1628 ifra.ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 1629 in6_prefixlen2mask(&ifra.ifra_prefixmask.sin6_addr, prefixlen); 1630 1631 ifra.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; 1632 ifra.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; 1633 ifra.ifra_flags = iflr->flags & ~IFLR_PREFIX; 1634 return in6_control(so, SIOCAIFADDR_IN6, &ifra, ifp); 1635 } 1636 case SIOCGLIFADDR: 1637 case SIOCDLIFADDR: 1638 { 1639 struct in6_addr mask, candidate, match; 1640 struct sockaddr_in6 *sin6; 1641 int cmp; 1642 int error, s; 1643 1644 memset(&mask, 0, sizeof(mask)); 1645 if (iflr->flags & IFLR_PREFIX) { 1646 /* lookup a prefix rather than address. */ 1647 in6_prefixlen2mask(&mask, iflr->prefixlen); 1648 1649 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1650 memcpy(&match, &sin6->sin6_addr, sizeof(match)); 1651 match.s6_addr32[0] &= mask.s6_addr32[0]; 1652 match.s6_addr32[1] &= mask.s6_addr32[1]; 1653 match.s6_addr32[2] &= mask.s6_addr32[2]; 1654 match.s6_addr32[3] &= mask.s6_addr32[3]; 1655 1656 /* if you set extra bits, that's wrong */ 1657 if (memcmp(&match, &sin6->sin6_addr, sizeof(match))) 1658 return EINVAL; 1659 1660 cmp = 1; 1661 } else { 1662 if (cmd == SIOCGLIFADDR) { 1663 /* on getting an address, take the 1st match */ 1664 cmp = 0; /* XXX */ 1665 } else { 1666 /* on deleting an address, do exact match */ 1667 in6_prefixlen2mask(&mask, 128); 1668 sin6 = (struct sockaddr_in6 *)&iflr->addr; 1669 memcpy(&match, &sin6->sin6_addr, sizeof(match)); 1670 1671 cmp = 1; 1672 } 1673 } 1674 1675 s = pserialize_read_enter(); 1676 IFADDR_READER_FOREACH(ifa, ifp) { 1677 if (ifa->ifa_addr->sa_family != AF_INET6) 1678 continue; 1679 if (!cmp) 1680 break; 1681 1682 /* 1683 * XXX: this is adhoc, but is necessary to allow 1684 * a user to specify fe80::/64 (not /10) for a 1685 * link-local address. 1686 */ 1687 memcpy(&candidate, IFA_IN6(ifa), sizeof(candidate)); 1688 in6_clearscope(&candidate); 1689 candidate.s6_addr32[0] &= mask.s6_addr32[0]; 1690 candidate.s6_addr32[1] &= mask.s6_addr32[1]; 1691 candidate.s6_addr32[2] &= mask.s6_addr32[2]; 1692 candidate.s6_addr32[3] &= mask.s6_addr32[3]; 1693 if (IN6_ARE_ADDR_EQUAL(&candidate, &match)) 1694 break; 1695 } 1696 if (!ifa) { 1697 error = EADDRNOTAVAIL; 1698 goto error; 1699 } 1700 ia = ifa2ia6(ifa); 1701 1702 if (cmd == SIOCGLIFADDR) { 1703 /* fill in the if_laddrreq structure */ 1704 memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin6_len); 1705 error = sa6_recoverscope( 1706 (struct sockaddr_in6 *)&iflr->addr); 1707 if (error != 0) 1708 goto error; 1709 1710 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1711 memcpy(&iflr->dstaddr, &ia->ia_dstaddr, 1712 ia->ia_dstaddr.sin6_len); 1713 error = sa6_recoverscope( 1714 (struct sockaddr_in6 *)&iflr->dstaddr); 1715 if (error != 0) 1716 goto error; 1717 } else 1718 memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr)); 1719 1720 iflr->prefixlen = 1721 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 1722 1723 iflr->flags = ia->ia6_flags; /* XXX */ 1724 1725 error = 0; 1726 } else { 1727 struct in6_aliasreq ifra; 1728 1729 /* fill in6_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 1730 memset(&ifra, 0, sizeof(ifra)); 1731 memcpy(ifra.ifra_name, iflr->iflr_name, 1732 sizeof(ifra.ifra_name)); 1733 1734 memcpy(&ifra.ifra_addr, &ia->ia_addr, 1735 ia->ia_addr.sin6_len); 1736 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 1737 memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr, 1738 ia->ia_dstaddr.sin6_len); 1739 } else { 1740 memset(&ifra.ifra_dstaddr, 0, 1741 sizeof(ifra.ifra_dstaddr)); 1742 } 1743 memcpy(&ifra.ifra_dstaddr, &ia->ia_prefixmask, 1744 ia->ia_prefixmask.sin6_len); 1745 1746 ifra.ifra_flags = ia->ia6_flags; 1747 pserialize_read_exit(s); 1748 1749 return in6_control(so, SIOCDIFADDR_IN6, &ifra, ifp); 1750 } 1751 error: 1752 pserialize_read_exit(s); 1753 return error; 1754 } 1755 } 1756 1757 return EOPNOTSUPP; /* just for safety */ 1758 } 1759 1760 /* 1761 * Initialize an interface's internet6 address 1762 * and routing table entry. 1763 */ 1764 static int 1765 in6_ifinit(struct ifnet *ifp, struct in6_ifaddr *ia, 1766 const struct sockaddr_in6 *sin6, int newhost) 1767 { 1768 int error = 0, ifacount = 0; 1769 int s; 1770 struct ifaddr *ifa; 1771 1772 KASSERT(mutex_owned(&in6_ifaddr_lock)); 1773 1774 /* 1775 * Give the interface a chance to initialize 1776 * if this is its first address, 1777 * and to validate the address if necessary. 1778 */ 1779 s = pserialize_read_enter(); 1780 IFADDR_READER_FOREACH(ifa, ifp) { 1781 if (ifa->ifa_addr->sa_family != AF_INET6) 1782 continue; 1783 ifacount++; 1784 } 1785 pserialize_read_exit(s); 1786 1787 ia->ia_addr = *sin6; 1788 1789 if (ifacount == 0 && 1790 (error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0) { 1791 return error; 1792 } 1793 1794 ia->ia_ifa.ifa_metric = ifp->if_metric; 1795 1796 /* we could do in(6)_socktrim here, but just omit it at this moment. */ 1797 1798 /* Add ownaddr as loopback rtentry, if necessary (ex. on p2p link). */ 1799 if (newhost) { 1800 /* set the rtrequest function to create llinfo */ 1801 if (ifp->if_flags & IFF_POINTOPOINT) 1802 ia->ia_ifa.ifa_rtrequest = p2p_rtrequest; 1803 else if ((ifp->if_flags & IFF_LOOPBACK) == 0) 1804 ia->ia_ifa.ifa_rtrequest = nd6_rtrequest; 1805 in6_ifaddlocal(&ia->ia_ifa); 1806 } else { 1807 /* Inform the routing socket of new flags/timings */ 1808 rt_newaddrmsg(RTM_NEWADDR, &ia->ia_ifa, 0, NULL); 1809 } 1810 1811 /* Add the network prefix route. */ 1812 if ((error = in6_ifaddprefix(ia)) != 0) { 1813 if (newhost) 1814 in6_ifremlocal(&ia->ia_ifa); 1815 return error; 1816 } 1817 1818 return error; 1819 } 1820 1821 static struct ifaddr * 1822 bestifa(struct ifaddr *best_ifa, struct ifaddr *ifa) 1823 { 1824 if (best_ifa == NULL || best_ifa->ifa_preference < ifa->ifa_preference) 1825 return ifa; 1826 return best_ifa; 1827 } 1828 1829 /* 1830 * Find an IPv6 interface link-local address specific to an interface. 1831 */ 1832 struct in6_ifaddr * 1833 in6ifa_ifpforlinklocal(const struct ifnet *ifp, const int ignoreflags) 1834 { 1835 struct ifaddr *best_ifa = NULL, *ifa; 1836 1837 IFADDR_READER_FOREACH(ifa, ifp) { 1838 if (ifa->ifa_addr->sa_family != AF_INET6) 1839 continue; 1840 if (!IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) 1841 continue; 1842 if ((((struct in6_ifaddr *)ifa)->ia6_flags & ignoreflags) != 0) 1843 continue; 1844 best_ifa = bestifa(best_ifa, ifa); 1845 } 1846 1847 return (struct in6_ifaddr *)best_ifa; 1848 } 1849 1850 struct in6_ifaddr * 1851 in6ifa_ifpforlinklocal_psref(const struct ifnet *ifp, const int ignoreflags, 1852 struct psref *psref) 1853 { 1854 struct in6_ifaddr *ia; 1855 int s = pserialize_read_enter(); 1856 1857 ia = in6ifa_ifpforlinklocal(ifp, ignoreflags); 1858 if (ia != NULL) 1859 ia6_acquire(ia, psref); 1860 pserialize_read_exit(s); 1861 1862 return ia; 1863 } 1864 1865 /* 1866 * find the internet address corresponding to a given address. 1867 * ifaddr is returned referenced. 1868 */ 1869 struct in6_ifaddr * 1870 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid) 1871 { 1872 struct in6_ifaddr *ia; 1873 int s; 1874 1875 s = pserialize_read_enter(); 1876 IN6_ADDRLIST_READER_FOREACH(ia) { 1877 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) { 1878 if (zoneid != 0 && 1879 zoneid != ia->ia_addr.sin6_scope_id) 1880 continue; 1881 ifaref(&ia->ia_ifa); 1882 break; 1883 } 1884 } 1885 pserialize_read_exit(s); 1886 1887 return ia; 1888 } 1889 1890 /* 1891 * find the internet address corresponding to a given interface and address. 1892 */ 1893 struct in6_ifaddr * 1894 in6ifa_ifpwithaddr(const struct ifnet *ifp, const struct in6_addr *addr) 1895 { 1896 struct ifaddr *best_ifa = NULL, *ifa; 1897 1898 IFADDR_READER_FOREACH(ifa, ifp) { 1899 if (ifa->ifa_addr->sa_family != AF_INET6) 1900 continue; 1901 if (!IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) 1902 continue; 1903 best_ifa = bestifa(best_ifa, ifa); 1904 } 1905 1906 return (struct in6_ifaddr *)best_ifa; 1907 } 1908 1909 struct in6_ifaddr * 1910 in6ifa_ifpwithaddr_psref(const struct ifnet *ifp, const struct in6_addr *addr, 1911 struct psref *psref) 1912 { 1913 struct in6_ifaddr *ia; 1914 int s = pserialize_read_enter(); 1915 1916 ia = in6ifa_ifpwithaddr(ifp, addr); 1917 if (ia != NULL) 1918 ia6_acquire(ia, psref); 1919 pserialize_read_exit(s); 1920 1921 return ia; 1922 } 1923 1924 static struct in6_ifaddr * 1925 bestia(struct in6_ifaddr *best_ia, struct in6_ifaddr *ia) 1926 { 1927 if (best_ia == NULL || 1928 best_ia->ia_ifa.ifa_preference < ia->ia_ifa.ifa_preference) 1929 return ia; 1930 return best_ia; 1931 } 1932 1933 /* 1934 * Determine if an address is on a local network. 1935 */ 1936 int 1937 in6_localaddr(const struct in6_addr *in6) 1938 { 1939 struct in6_ifaddr *ia; 1940 int s; 1941 1942 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1943 return 1; 1944 1945 s = pserialize_read_enter(); 1946 IN6_ADDRLIST_READER_FOREACH(ia) { 1947 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1948 &ia->ia_prefixmask.sin6_addr)) { 1949 pserialize_read_exit(s); 1950 return 1; 1951 } 1952 } 1953 pserialize_read_exit(s); 1954 1955 return 0; 1956 } 1957 1958 int 1959 in6_is_addr_deprecated(struct sockaddr_in6 *sa6) 1960 { 1961 struct in6_ifaddr *ia; 1962 int s; 1963 1964 s = pserialize_read_enter(); 1965 IN6_ADDRLIST_READER_FOREACH(ia) { 1966 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, 1967 &sa6->sin6_addr) && 1968 #ifdef SCOPEDROUTING 1969 ia->ia_addr.sin6_scope_id == sa6->sin6_scope_id && 1970 #endif 1971 (ia->ia6_flags & IN6_IFF_DEPRECATED) != 0) { 1972 pserialize_read_exit(s); 1973 return 1; /* true */ 1974 } 1975 1976 /* XXX: do we still have to go thru the rest of the list? */ 1977 } 1978 pserialize_read_exit(s); 1979 1980 return 0; /* false */ 1981 } 1982 1983 /* 1984 * return length of part which dst and src are equal 1985 * hard coding... 1986 */ 1987 int 1988 in6_matchlen(struct in6_addr *src, struct in6_addr *dst) 1989 { 1990 int match = 0; 1991 u_char *s = (u_char *)src, *d = (u_char *)dst; 1992 u_char *lim = s + 16, r; 1993 1994 while (s < lim) 1995 if ((r = (*d++ ^ *s++)) != 0) { 1996 while (r < 128) { 1997 match++; 1998 r <<= 1; 1999 } 2000 break; 2001 } else 2002 match += NBBY; 2003 return match; 2004 } 2005 2006 /* XXX: to be scope conscious */ 2007 int 2008 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 2009 { 2010 int bytelen, bitlen; 2011 2012 /* sanity check */ 2013 if (len < 0 || len > 128) { 2014 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 2015 len); 2016 return 0; 2017 } 2018 2019 bytelen = len / NBBY; 2020 bitlen = len % NBBY; 2021 2022 if (memcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 2023 return 0; 2024 if (bitlen != 0 && 2025 p1->s6_addr[bytelen] >> (NBBY - bitlen) != 2026 p2->s6_addr[bytelen] >> (NBBY - bitlen)) 2027 return 0; 2028 2029 return 1; 2030 } 2031 2032 void 2033 in6_prefixlen2mask(struct in6_addr *maskp, int len) 2034 { 2035 static const u_char maskarray[NBBY] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 2036 int bytelen, bitlen, i; 2037 2038 /* sanity check */ 2039 if (len < 0 || len > 128) { 2040 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 2041 len); 2042 return; 2043 } 2044 2045 memset(maskp, 0, sizeof(*maskp)); 2046 bytelen = len / NBBY; 2047 bitlen = len % NBBY; 2048 for (i = 0; i < bytelen; i++) 2049 maskp->s6_addr[i] = 0xff; 2050 if (bitlen) 2051 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 2052 } 2053 2054 /* 2055 * return the best address out of the same scope. if no address was 2056 * found, return the first valid address from designated IF. 2057 */ 2058 struct in6_ifaddr * 2059 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 2060 { 2061 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 2062 struct ifaddr *ifa; 2063 struct in6_ifaddr *best_ia = NULL, *ia; 2064 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 2065 2066 dep[0] = dep[1] = NULL; 2067 2068 /* 2069 * We first look for addresses in the same scope. 2070 * If there is one, return it. 2071 * If two or more, return one which matches the dst longest. 2072 * If none, return one of global addresses assigned other ifs. 2073 */ 2074 IFADDR_READER_FOREACH(ifa, ifp) { 2075 if (ifa->ifa_addr->sa_family != AF_INET6) 2076 continue; 2077 ia = (struct in6_ifaddr *)ifa; 2078 if (ia->ia6_flags & IN6_IFF_ANYCAST) 2079 continue; /* XXX: is there any case to allow anycast? */ 2080 if (ia->ia6_flags & IN6_IFF_NOTREADY) 2081 continue; /* don't use this interface */ 2082 if (ia->ia6_flags & IN6_IFF_DETACHED) 2083 continue; 2084 if (ia->ia6_flags & IN6_IFF_DEPRECATED) { 2085 if (ip6_use_deprecated) 2086 dep[0] = ia; 2087 continue; 2088 } 2089 2090 if (dst_scope != in6_addrscope(IFA_IN6(ifa))) 2091 continue; 2092 /* 2093 * call in6_matchlen() as few as possible 2094 */ 2095 if (best_ia == NULL) { 2096 best_ia = ia; 2097 continue; 2098 } 2099 if (blen == -1) 2100 blen = in6_matchlen(&best_ia->ia_addr.sin6_addr, dst); 2101 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2102 if (tlen > blen) { 2103 blen = tlen; 2104 best_ia = ia; 2105 } else if (tlen == blen) 2106 best_ia = bestia(best_ia, ia); 2107 } 2108 if (best_ia != NULL) 2109 return best_ia; 2110 2111 IFADDR_READER_FOREACH(ifa, ifp) { 2112 if (ifa->ifa_addr->sa_family != AF_INET6) 2113 continue; 2114 ia = (struct in6_ifaddr *)ifa; 2115 if (ia->ia6_flags & IN6_IFF_ANYCAST) 2116 continue; /* XXX: is there any case to allow anycast? */ 2117 if (ia->ia6_flags & IN6_IFF_NOTREADY) 2118 continue; /* don't use this interface */ 2119 if (ia->ia6_flags & IN6_IFF_DETACHED) 2120 continue; 2121 if (ia->ia6_flags & IN6_IFF_DEPRECATED) { 2122 if (ip6_use_deprecated) 2123 dep[1] = (struct in6_ifaddr *)ifa; 2124 continue; 2125 } 2126 2127 best_ia = bestia(best_ia, ia); 2128 } 2129 if (best_ia != NULL) 2130 return best_ia; 2131 2132 /* use the last-resort values, that are, deprecated addresses */ 2133 if (dep[0]) 2134 return dep[0]; 2135 if (dep[1]) 2136 return dep[1]; 2137 2138 return NULL; 2139 } 2140 2141 /* 2142 * perform DAD when interface becomes IFF_UP. 2143 */ 2144 void 2145 in6_if_link_up(struct ifnet *ifp) 2146 { 2147 struct ifaddr *ifa; 2148 struct in6_ifaddr *ia; 2149 int s, bound; 2150 char ip6buf[INET6_ADDRSTRLEN]; 2151 2152 /* Ensure it's sane to run DAD */ 2153 if (ifp->if_link_state == LINK_STATE_DOWN) 2154 return; 2155 if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) 2156 return; 2157 2158 bound = curlwp_bind(); 2159 s = pserialize_read_enter(); 2160 IFADDR_READER_FOREACH(ifa, ifp) { 2161 struct psref psref; 2162 2163 if (ifa->ifa_addr->sa_family != AF_INET6) 2164 continue; 2165 2166 ifa_acquire(ifa, &psref); 2167 pserialize_read_exit(s); 2168 ia = (struct in6_ifaddr *)ifa; 2169 2170 /* If detached then mark as tentative */ 2171 if (ia->ia6_flags & IN6_IFF_DETACHED) { 2172 ia->ia6_flags &= ~IN6_IFF_DETACHED; 2173 if (if_do_dad(ifp)) { 2174 ia->ia6_flags |= IN6_IFF_TENTATIVE; 2175 nd6log(LOG_ERR, "%s marked tentative\n", 2176 IN6_PRINT(ip6buf, 2177 &ia->ia_addr.sin6_addr)); 2178 } else if ((ia->ia6_flags & IN6_IFF_TENTATIVE) == 0) 2179 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 2180 } 2181 2182 if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 2183 int rand_delay; 2184 2185 /* Clear the duplicated flag as we're starting DAD. */ 2186 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; 2187 2188 /* 2189 * The TENTATIVE flag was likely set by hand 2190 * beforehand, implicitly indicating the need for DAD. 2191 * We may be able to skip the random delay in this 2192 * case, but we impose delays just in case. 2193 */ 2194 rand_delay = cprng_fast32() % 2195 (MAX_RTR_SOLICITATION_DELAY * hz); 2196 /* +1 ensures callout is always used */ 2197 nd6_dad_start(ifa, rand_delay + 1); 2198 } 2199 2200 s = pserialize_read_enter(); 2201 ifa_release(ifa, &psref); 2202 } 2203 pserialize_read_exit(s); 2204 curlwp_bindx(bound); 2205 2206 /* Restore any detached prefixes */ 2207 ND6_WLOCK(); 2208 nd6_pfxlist_onlink_check(); 2209 ND6_UNLOCK(); 2210 } 2211 2212 void 2213 in6_if_up(struct ifnet *ifp) 2214 { 2215 2216 /* 2217 * special cases, like 6to4, are handled in in6_ifattach 2218 */ 2219 in6_ifattach(ifp, NULL); 2220 2221 /* interface may not support link state, so bring it up also */ 2222 in6_if_link_up(ifp); 2223 } 2224 2225 /* 2226 * Mark all addresses as detached. 2227 */ 2228 void 2229 in6_if_link_down(struct ifnet *ifp) 2230 { 2231 struct ifaddr *ifa; 2232 struct in6_ifaddr *ia; 2233 int s, bound; 2234 char ip6buf[INET6_ADDRSTRLEN]; 2235 2236 /* Any prefixes on this interface should be detached as well */ 2237 ND6_WLOCK(); 2238 nd6_pfxlist_onlink_check(); 2239 ND6_UNLOCK(); 2240 2241 bound = curlwp_bind(); 2242 s = pserialize_read_enter(); 2243 IFADDR_READER_FOREACH(ifa, ifp) { 2244 struct psref psref; 2245 2246 if (ifa->ifa_addr->sa_family != AF_INET6) 2247 continue; 2248 2249 ifa_acquire(ifa, &psref); 2250 pserialize_read_exit(s); 2251 ia = (struct in6_ifaddr *)ifa; 2252 2253 /* Stop DAD processing */ 2254 nd6_dad_stop(ifa); 2255 2256 /* 2257 * Mark the address as detached. 2258 * This satisfies RFC4862 Section 5.3, but we should apply 2259 * this logic to all addresses to be a good citizen and 2260 * avoid potential duplicated addresses. 2261 * When the interface comes up again, detached addresses 2262 * are marked tentative and DAD commences. 2263 */ 2264 if (!(ia->ia6_flags & IN6_IFF_DETACHED)) { 2265 nd6log(LOG_DEBUG, "%s marked detached\n", 2266 IN6_PRINT(ip6buf, &ia->ia_addr.sin6_addr)); 2267 ia->ia6_flags |= IN6_IFF_DETACHED; 2268 ia->ia6_flags &= 2269 ~(IN6_IFF_TENTATIVE | IN6_IFF_DUPLICATED); 2270 rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL); 2271 } 2272 2273 s = pserialize_read_enter(); 2274 ifa_release(ifa, &psref); 2275 } 2276 pserialize_read_exit(s); 2277 curlwp_bindx(bound); 2278 } 2279 2280 void 2281 in6_if_down(struct ifnet *ifp) 2282 { 2283 2284 in6_if_link_down(ifp); 2285 lltable_purge_entries(LLTABLE6(ifp)); 2286 } 2287 2288 void 2289 in6_if_link_state_change(struct ifnet *ifp, int link_state) 2290 { 2291 2292 switch (link_state) { 2293 case LINK_STATE_DOWN: 2294 in6_if_link_down(ifp); 2295 break; 2296 case LINK_STATE_UP: 2297 in6_if_link_up(ifp); 2298 break; 2299 } 2300 } 2301 2302 /* 2303 * Calculate max IPv6 MTU through all the interfaces and store it 2304 * to in6_maxmtu. 2305 */ 2306 void 2307 in6_setmaxmtu(void) 2308 { 2309 unsigned long maxmtu = 0; 2310 struct ifnet *ifp; 2311 int s; 2312 2313 s = pserialize_read_enter(); 2314 IFNET_READER_FOREACH(ifp) { 2315 /* this function can be called during ifnet initialization */ 2316 if (!ifp->if_afdata[AF_INET6]) 2317 continue; 2318 if ((ifp->if_flags & IFF_LOOPBACK) == 0 && 2319 IN6_LINKMTU(ifp) > maxmtu) 2320 maxmtu = IN6_LINKMTU(ifp); 2321 } 2322 pserialize_read_exit(s); 2323 if (maxmtu) /* update only when maxmtu is positive */ 2324 in6_maxmtu = maxmtu; 2325 } 2326 2327 int 2328 in6_tunnel_validate(const struct ip6_hdr *ip6, const struct in6_addr *src, 2329 const struct in6_addr *dst) 2330 { 2331 2332 /* check for address match */ 2333 if (!IN6_ARE_ADDR_EQUAL(src, &ip6->ip6_dst) || 2334 !IN6_ARE_ADDR_EQUAL(dst, &ip6->ip6_src)) 2335 return 0; 2336 2337 /* martian filters on outer source - done in ip6_input */ 2338 2339 /* NOTE: the pakcet may be dropped by uRPF. */ 2340 2341 /* return valid bytes length */ 2342 return sizeof(*src) + sizeof(*dst); 2343 } 2344 2345 /* 2346 * Provide the length of interface identifiers to be used for the link attached 2347 * to the given interface. The length should be defined in "IPv6 over 2348 * xxx-link" document. Note that address architecture might also define 2349 * the length for a particular set of address prefixes, regardless of the 2350 * link type. As clarified in rfc2462bis, those two definitions should be 2351 * consistent, and those really are as of August 2004. 2352 */ 2353 int 2354 in6_if2idlen(struct ifnet *ifp) 2355 { 2356 switch (ifp->if_type) { 2357 case IFT_ETHER: /* RFC2464 */ 2358 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2359 case IFT_L2VLAN: /* ditto */ 2360 case IFT_IEEE80211: /* ditto */ 2361 case IFT_FDDI: /* RFC2467 */ 2362 case IFT_ISO88025: /* RFC2470 (IPv6 over Token Ring) */ 2363 case IFT_PPP: /* RFC2472 */ 2364 case IFT_ARCNET: /* RFC2497 */ 2365 case IFT_FRELAY: /* RFC2590 */ 2366 case IFT_IEEE1394: /* RFC3146 */ 2367 case IFT_GIF: /* draft-ietf-v6ops-mech-v2-07 */ 2368 case IFT_LOOP: /* XXX: is this really correct? */ 2369 return 64; 2370 default: 2371 /* 2372 * Unknown link type: 2373 * It might be controversial to use the today's common constant 2374 * of 64 for these cases unconditionally. For full compliance, 2375 * we should return an error in this case. On the other hand, 2376 * if we simply miss the standard for the link type or a new 2377 * standard is defined for a new link type, the IFID length 2378 * is very likely to be the common constant. As a compromise, 2379 * we always use the constant, but make an explicit notice 2380 * indicating the "unknown" case. 2381 */ 2382 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2383 return 64; 2384 } 2385 } 2386 2387 struct in6_llentry { 2388 struct llentry base; 2389 }; 2390 2391 #define IN6_LLTBL_DEFAULT_HSIZE 32 2392 #define IN6_LLTBL_HASH(k, h) \ 2393 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1)) 2394 2395 /* 2396 * Do actual deallocation of @lle. 2397 * Called by LLE_FREE_LOCKED when number of references 2398 * drops to zero. 2399 */ 2400 static void 2401 in6_lltable_destroy_lle(struct llentry *lle) 2402 { 2403 2404 LLE_WUNLOCK(lle); 2405 LLE_LOCK_DESTROY(lle); 2406 kmem_intr_free(lle, sizeof(struct in6_llentry)); 2407 } 2408 2409 static struct llentry * 2410 in6_lltable_new(const struct in6_addr *addr6, u_int flags) 2411 { 2412 struct in6_llentry *lle; 2413 2414 lle = kmem_intr_zalloc(sizeof(struct in6_llentry), KM_NOSLEEP); 2415 if (lle == NULL) /* NB: caller generates msg */ 2416 return NULL; 2417 2418 lle->base.r_l3addr.addr6 = *addr6; 2419 lle->base.lle_refcnt = 1; 2420 lle->base.lle_free = in6_lltable_destroy_lle; 2421 LLE_LOCK_INIT(&lle->base); 2422 callout_init(&lle->base.lle_timer, CALLOUT_MPSAFE); 2423 2424 return &lle->base; 2425 } 2426 2427 static int 2428 in6_lltable_match_prefix(const struct sockaddr *prefix, 2429 const struct sockaddr *mask, u_int flags, struct llentry *lle) 2430 { 2431 const struct sockaddr_in6 *pfx = (const struct sockaddr_in6 *)prefix; 2432 const struct sockaddr_in6 *msk = (const struct sockaddr_in6 *)mask; 2433 2434 if (IN6_ARE_MASKED_ADDR_EQUAL(&lle->r_l3addr.addr6, 2435 &pfx->sin6_addr, &msk->sin6_addr) && 2436 ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))) 2437 return 1; 2438 2439 return 0; 2440 } 2441 2442 static void 2443 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle) 2444 { 2445 struct ifnet *ifp = llt->llt_ifp; 2446 bool locked = false; 2447 2448 LLE_WLOCK_ASSERT(lle); 2449 2450 /* Unlink entry from table */ 2451 if ((lle->la_flags & LLE_LINKED) != 0) { 2452 IF_AFDATA_WLOCK_ASSERT(ifp); 2453 lltable_unlink_entry(llt, lle); 2454 KASSERT((lle->la_flags & LLE_LINKED) == 0); 2455 locked = true; 2456 } 2457 /* 2458 * We need to release the lock here to lle_timer proceeds; 2459 * lle_timer should stop immediately if LLE_LINKED isn't set. 2460 * Note that we cannot pass lle->lle_lock to callout_halt 2461 * because it's a rwlock. 2462 */ 2463 LLE_ADDREF(lle); 2464 LLE_WUNLOCK(lle); 2465 if (locked) 2466 IF_AFDATA_WUNLOCK(ifp); 2467 2468 #ifdef NET_MPSAFE 2469 callout_halt(&lle->lle_timer, NULL); 2470 #else 2471 if (mutex_owned(softnet_lock)) 2472 callout_halt(&lle->lle_timer, softnet_lock); 2473 else 2474 callout_halt(&lle->lle_timer, NULL); 2475 #endif 2476 LLE_WLOCK(lle); 2477 LLE_REMREF(lle); 2478 2479 lltable_drop_entry_queue(lle); 2480 LLE_FREE_LOCKED(lle); 2481 2482 if (locked) 2483 IF_AFDATA_WLOCK(ifp); 2484 } 2485 2486 static int 2487 in6_lltable_rtcheck(struct ifnet *ifp, u_int flags, 2488 const struct sockaddr *l3addr, const struct rtentry *rt) 2489 { 2490 char ip6buf[INET6_ADDRSTRLEN]; 2491 2492 if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) { 2493 int s; 2494 struct ifaddr *ifa; 2495 /* 2496 * Create an ND6 cache for an IPv6 neighbor 2497 * that is not covered by our own prefix. 2498 */ 2499 /* XXX ifaof_ifpforaddr should take a const param */ 2500 s = pserialize_read_enter(); 2501 ifa = ifaof_ifpforaddr(l3addr, ifp); 2502 if (ifa != NULL) { 2503 pserialize_read_exit(s); 2504 return 0; 2505 } 2506 pserialize_read_exit(s); 2507 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2508 IN6_PRINT(ip6buf, 2509 &((const struct sockaddr_in6 *)l3addr)->sin6_addr)); 2510 return EINVAL; 2511 } 2512 return 0; 2513 } 2514 2515 static inline uint32_t 2516 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize) 2517 { 2518 2519 return IN6_LLTBL_HASH(dst->s6_addr32[3], hsize); 2520 } 2521 2522 static uint32_t 2523 in6_lltable_hash(const struct llentry *lle, uint32_t hsize) 2524 { 2525 2526 return in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize); 2527 } 2528 2529 static void 2530 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) 2531 { 2532 struct sockaddr_in6 *sin6; 2533 2534 sin6 = (struct sockaddr_in6 *)sa; 2535 bzero(sin6, sizeof(*sin6)); 2536 sin6->sin6_family = AF_INET6; 2537 sin6->sin6_len = sizeof(*sin6); 2538 sin6->sin6_addr = lle->r_l3addr.addr6; 2539 } 2540 2541 static inline struct llentry * 2542 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst) 2543 { 2544 struct llentry *lle; 2545 struct llentries *lleh; 2546 u_int hashidx; 2547 2548 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize); 2549 lleh = &llt->lle_head[hashidx]; 2550 LIST_FOREACH(lle, lleh, lle_next) { 2551 if (lle->la_flags & LLE_DELETED) 2552 continue; 2553 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst)) 2554 break; 2555 } 2556 2557 return lle; 2558 } 2559 2560 static int 2561 in6_lltable_delete(struct lltable *llt, u_int flags, 2562 const struct sockaddr *l3addr) 2563 { 2564 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2565 struct llentry *lle; 2566 2567 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp); 2568 KASSERTMSG(l3addr->sa_family == AF_INET6, 2569 "sin_family %d", l3addr->sa_family); 2570 2571 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr); 2572 2573 if (lle == NULL) { 2574 #ifdef DEBUG 2575 char buf[64]; 2576 sockaddr_format(l3addr, buf, sizeof(buf)); 2577 log(LOG_INFO, "%s: cache for %s is not found\n", 2578 __func__, buf); 2579 #endif 2580 return ENOENT; 2581 } 2582 2583 LLE_WLOCK(lle); 2584 lle->la_flags |= LLE_DELETED; 2585 #ifdef DEBUG 2586 { 2587 char buf[64]; 2588 sockaddr_format(l3addr, buf, sizeof(buf)); 2589 log(LOG_INFO, "%s: cache for %s (%p) is deleted\n", 2590 __func__, buf, lle); 2591 } 2592 #endif 2593 if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) 2594 llentry_free(lle); 2595 else 2596 LLE_WUNLOCK(lle); 2597 2598 return 0; 2599 } 2600 2601 static struct llentry * 2602 in6_lltable_create(struct lltable *llt, u_int flags, 2603 const struct sockaddr *l3addr, const struct rtentry *rt) 2604 { 2605 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2606 struct ifnet *ifp = llt->llt_ifp; 2607 struct llentry *lle; 2608 2609 IF_AFDATA_WLOCK_ASSERT(ifp); 2610 KASSERTMSG(l3addr->sa_family == AF_INET6, 2611 "sin_family %d", l3addr->sa_family); 2612 2613 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr); 2614 2615 if (lle != NULL) { 2616 LLE_WLOCK(lle); 2617 return lle; 2618 } 2619 2620 /* 2621 * A route that covers the given address must have 2622 * been installed 1st because we are doing a resolution, 2623 * verify this. 2624 */ 2625 if (!(flags & LLE_IFADDR) && 2626 in6_lltable_rtcheck(ifp, flags, l3addr, rt) != 0) 2627 return NULL; 2628 2629 lle = in6_lltable_new(&sin6->sin6_addr, flags); 2630 if (lle == NULL) { 2631 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2632 return NULL; 2633 } 2634 lle->la_flags = flags; 2635 if ((flags & LLE_IFADDR) == LLE_IFADDR) { 2636 memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen); 2637 lle->la_flags |= LLE_VALID; 2638 } 2639 2640 lltable_link_entry(llt, lle); 2641 LLE_WLOCK(lle); 2642 2643 return lle; 2644 } 2645 2646 static struct llentry * 2647 in6_lltable_lookup(struct lltable *llt, u_int flags, 2648 const struct sockaddr *l3addr) 2649 { 2650 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2651 struct llentry *lle; 2652 2653 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp); 2654 KASSERTMSG(l3addr->sa_family == AF_INET6, 2655 "sin_family %d", l3addr->sa_family); 2656 2657 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr); 2658 2659 if (lle == NULL) 2660 return NULL; 2661 2662 if (flags & LLE_EXCLUSIVE) 2663 LLE_WLOCK(lle); 2664 else 2665 LLE_RLOCK(lle); 2666 return lle; 2667 } 2668 2669 static int 2670 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle, 2671 struct rt_walkarg *w) 2672 { 2673 struct sockaddr_in6 sin6; 2674 2675 LLTABLE_LOCK_ASSERT(); 2676 2677 /* skip deleted entries */ 2678 if (lle->la_flags & LLE_DELETED) 2679 return 0; 2680 2681 sockaddr_in6_init(&sin6, &lle->r_l3addr.addr6, 0, 0, 0); 2682 2683 return lltable_dump_entry(llt, lle, w, sin6tosa(&sin6)); 2684 } 2685 2686 static struct lltable * 2687 in6_lltattach(struct ifnet *ifp) 2688 { 2689 struct lltable *llt; 2690 2691 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE); 2692 llt->llt_af = AF_INET6; 2693 llt->llt_ifp = ifp; 2694 2695 llt->llt_lookup = in6_lltable_lookup; 2696 llt->llt_create = in6_lltable_create; 2697 llt->llt_delete = in6_lltable_delete; 2698 llt->llt_dump_entry = in6_lltable_dump_entry; 2699 llt->llt_hash = in6_lltable_hash; 2700 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry; 2701 llt->llt_free_entry = in6_lltable_free_entry; 2702 llt->llt_match_prefix = in6_lltable_match_prefix; 2703 lltable_link(llt); 2704 2705 return llt; 2706 } 2707 2708 void * 2709 in6_domifattach(struct ifnet *ifp) 2710 { 2711 struct in6_ifextra *ext; 2712 2713 ext = malloc(sizeof(*ext), M_IFADDR, M_WAITOK|M_ZERO); 2714 2715 ext->in6_ifstat = malloc(sizeof(struct in6_ifstat), 2716 M_IFADDR, M_WAITOK|M_ZERO); 2717 2718 ext->icmp6_ifstat = malloc(sizeof(struct icmp6_ifstat), 2719 M_IFADDR, M_WAITOK|M_ZERO); 2720 2721 ext->nd_ifinfo = nd6_ifattach(ifp); 2722 ext->scope6_id = scope6_ifattach(ifp); 2723 ext->nprefixes = 0; 2724 ext->ndefrouters = 0; 2725 2726 ext->lltable = in6_lltattach(ifp); 2727 2728 return ext; 2729 } 2730 2731 void 2732 in6_domifdetach(struct ifnet *ifp, void *aux) 2733 { 2734 struct in6_ifextra *ext = (struct in6_ifextra *)aux; 2735 2736 lltable_free(ext->lltable); 2737 ext->lltable = NULL; 2738 SOFTNET_LOCK_UNLESS_NET_MPSAFE(); 2739 nd6_ifdetach(ifp, ext); 2740 SOFTNET_UNLOCK_UNLESS_NET_MPSAFE(); 2741 free(ext->in6_ifstat, M_IFADDR); 2742 free(ext->icmp6_ifstat, M_IFADDR); 2743 scope6_ifdetach(ext->scope6_id); 2744 free(ext, M_IFADDR); 2745 } 2746 2747 /* 2748 * Convert IPv4 address stored in struct in_addr to IPv4-Mapped IPv6 address 2749 * stored in struct in6_addr as defined in RFC 4921 section 2.5.5.2. 2750 */ 2751 void 2752 in6_in_2_v4mapin6(const struct in_addr *in, struct in6_addr *in6) 2753 { 2754 in6->s6_addr32[0] = 0; 2755 in6->s6_addr32[1] = 0; 2756 in6->s6_addr32[2] = IPV6_ADDR_INT32_SMP; 2757 in6->s6_addr32[3] = in->s_addr; 2758 } 2759 2760 /* 2761 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 2762 * v4 mapped addr or v4 compat addr 2763 */ 2764 void 2765 in6_sin6_2_sin(struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2766 { 2767 memset(sin, 0, sizeof(*sin)); 2768 sin->sin_len = sizeof(struct sockaddr_in); 2769 sin->sin_family = AF_INET; 2770 sin->sin_port = sin6->sin6_port; 2771 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 2772 } 2773 2774 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 2775 void 2776 in6_sin_2_v4mapsin6(const struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2777 { 2778 memset(sin6, 0, sizeof(*sin6)); 2779 sin6->sin6_len = sizeof(struct sockaddr_in6); 2780 sin6->sin6_family = AF_INET6; 2781 sin6->sin6_port = sin->sin_port; 2782 in6_in_2_v4mapin6(&sin->sin_addr, &sin6->sin6_addr); 2783 } 2784 2785 /* Convert sockaddr_in6 into sockaddr_in. */ 2786 void 2787 in6_sin6_2_sin_in_sock(struct sockaddr *nam) 2788 { 2789 struct sockaddr_in *sin_p; 2790 struct sockaddr_in6 sin6; 2791 2792 /* 2793 * Save original sockaddr_in6 addr and convert it 2794 * to sockaddr_in. 2795 */ 2796 sin6 = *(struct sockaddr_in6 *)nam; 2797 sin_p = (struct sockaddr_in *)nam; 2798 in6_sin6_2_sin(sin_p, &sin6); 2799 } 2800 2801 /* Convert sockaddr_in into sockaddr_in6 in v4 mapped addr format. */ 2802 void 2803 in6_sin_2_v4mapsin6_in_sock(struct sockaddr **nam) 2804 { 2805 struct sockaddr_in *sin_p; 2806 struct sockaddr_in6 *sin6_p; 2807 2808 sin6_p = malloc(sizeof(*sin6_p), M_SONAME, M_WAITOK); 2809 sin_p = (struct sockaddr_in *)*nam; 2810 in6_sin_2_v4mapsin6(sin_p, sin6_p); 2811 free(*nam, M_SONAME); 2812 *nam = sin6tosa(sin6_p); 2813 } 2814