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