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