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