1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $KAME: in6.c,v 1.259 2002/01/21 11:37:50 keiichi Exp $ 32 */ 33 34 /*- 35 * Copyright (c) 1982, 1986, 1991, 1993 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 */ 62 63 #include "opt_inet.h" 64 #include "opt_inet6.h" 65 66 #include <sys/param.h> 67 #include <sys/eventhandler.h> 68 #include <sys/errno.h> 69 #include <sys/jail.h> 70 #include <sys/malloc.h> 71 #include <sys/socket.h> 72 #include <sys/socketvar.h> 73 #include <sys/sockio.h> 74 #include <sys/systm.h> 75 #include <sys/priv.h> 76 #include <sys/proc.h> 77 #include <sys/time.h> 78 #include <sys/kernel.h> 79 #include <sys/lock.h> 80 #include <sys/rmlock.h> 81 #include <sys/sysctl.h> 82 #include <sys/syslog.h> 83 84 #include <net/if.h> 85 #include <net/if_var.h> 86 #include <net/if_private.h> 87 #include <net/if_types.h> 88 #include <net/if_bridgevar.h> 89 #include <net/route.h> 90 #include <net/route/route_ctl.h> 91 #include <net/route/nhop.h> 92 #include <net/if_dl.h> 93 #include <net/vnet.h> 94 95 #include <netinet/in.h> 96 #include <netinet/in_var.h> 97 #include <net/if_llatbl.h> 98 #include <netinet/if_ether.h> 99 #include <netinet/in_systm.h> 100 #include <netinet/ip.h> 101 #include <netinet/in_pcb.h> 102 #include <netinet/ip_carp.h> 103 #include <netinet/icmp6.h> 104 105 #include <netinet/ip6.h> 106 #include <netinet6/ip6_var.h> 107 #include <netinet6/nd6.h> 108 #include <netinet6/mld6_var.h> 109 #include <netinet6/ip6_mroute.h> 110 #include <netinet6/in6_ifattach.h> 111 #include <netinet6/scope6_var.h> 112 #include <netinet6/in6_fib.h> 113 #include <netinet6/in6_pcb.h> 114 115 #ifdef MAC 116 #include <security/mac/mac_framework.h> 117 #endif 118 119 /* 120 * struct in6_ifreq and struct ifreq must be type punnable for common members 121 * of ifr_ifru to allow accessors to be shared. 122 */ 123 _Static_assert(offsetof(struct in6_ifreq, ifr_ifru) == 124 offsetof(struct ifreq, ifr_ifru), 125 "struct in6_ifreq and struct ifreq are not type punnable"); 126 127 VNET_DEFINE_STATIC(int, icmp6_nodeinfo_oldmcprefix) = 1; 128 #define V_icmp6_nodeinfo_oldmcprefix VNET(icmp6_nodeinfo_oldmcprefix) 129 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_NODEINFO_OLDMCPREFIX, 130 nodeinfo_oldmcprefix, CTLFLAG_VNET | CTLFLAG_RW, 131 &VNET_NAME(icmp6_nodeinfo_oldmcprefix), 0, 132 "Join old IPv6 NI group address in draft-ietf-ipngwg-icmp-name-lookup " 133 "for compatibility with KAME implementation"); 134 135 VNET_DEFINE_STATIC(int, nd6_useloopback) = 1; 136 #define V_nd6_useloopback VNET(nd6_useloopback) 137 SYSCTL_INT(_net_inet6_icmp6, ICMPV6CTL_ND6_USELOOPBACK, nd6_useloopback, 138 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(nd6_useloopback), 0, 139 "Create a loopback route when configuring an IPv6 address"); 140 141 /* 142 * Definitions of some costant IP6 addresses. 143 */ 144 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; 145 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; 146 const struct in6_addr in6addr_nodelocal_allnodes = 147 IN6ADDR_NODELOCAL_ALLNODES_INIT; 148 const struct in6_addr in6addr_linklocal_allnodes = 149 IN6ADDR_LINKLOCAL_ALLNODES_INIT; 150 const struct in6_addr in6addr_linklocal_allrouters = 151 IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; 152 const struct in6_addr in6addr_linklocal_allv2routers = 153 IN6ADDR_LINKLOCAL_ALLV2ROUTERS_INIT; 154 155 const struct in6_addr in6mask0 = IN6MASK0; 156 const struct in6_addr in6mask32 = IN6MASK32; 157 const struct in6_addr in6mask64 = IN6MASK64; 158 const struct in6_addr in6mask96 = IN6MASK96; 159 const struct in6_addr in6mask128 = IN6MASK128; 160 161 const struct sockaddr_in6 sa6_any = 162 { sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0 }; 163 164 static int in6_notify_ifa(struct ifnet *, struct in6_ifaddr *, 165 struct in6_aliasreq *, int); 166 static void in6_unlink_ifa(struct in6_ifaddr *, struct ifnet *); 167 168 static int in6_validate_ifra(struct ifnet *, struct in6_aliasreq *, 169 struct in6_ifaddr *, int); 170 static struct in6_ifaddr *in6_alloc_ifa(struct ifnet *, 171 struct in6_aliasreq *, int flags); 172 static int in6_update_ifa_internal(struct ifnet *, struct in6_aliasreq *, 173 struct in6_ifaddr *, int, int); 174 static int in6_broadcast_ifa(struct ifnet *, struct in6_aliasreq *, 175 struct in6_ifaddr *, int); 176 177 static void in6_join_proxy_ndp_mc(struct ifnet *, const struct in6_addr *); 178 static void in6_leave_proxy_ndp_mc(struct ifnet *, const struct in6_addr *); 179 180 #define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa)) 181 #define ia62ifa(ia6) (&((ia6)->ia_ifa)) 182 183 static struct sx in6_control_sx; 184 SX_SYSINIT(in6_control_sx, &in6_control_sx, "in6_control"); 185 186 void 187 in6_newaddrmsg(struct in6_ifaddr *ia, int cmd) 188 { 189 struct rt_addrinfo info; 190 struct ifaddr *ifa; 191 struct sockaddr_dl gateway; 192 int fibnum; 193 194 ifa = &ia->ia_ifa; 195 196 /* 197 * Prepare info data for the host route. 198 * This code mimics one from ifa_maintain_loopback_route(). 199 */ 200 bzero(&info, sizeof(struct rt_addrinfo)); 201 info.rti_flags = ifa->ifa_flags | RTF_HOST | RTF_STATIC | RTF_PINNED; 202 info.rti_info[RTAX_DST] = ifa->ifa_addr; 203 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&gateway; 204 link_init_sdl(ifa->ifa_ifp, (struct sockaddr *)&gateway, ifa->ifa_ifp->if_type); 205 if (cmd != RTM_DELETE) 206 info.rti_ifp = V_loif; 207 208 fibnum = ia62ifa(ia)->ifa_ifp->if_fib; 209 210 if (cmd == RTM_ADD) { 211 rt_addrmsg(cmd, &ia->ia_ifa, fibnum); 212 rt_routemsg_info(cmd, &info, fibnum); 213 } else if (cmd == RTM_DELETE) { 214 rt_routemsg_info(cmd, &info, fibnum); 215 rt_addrmsg(cmd, &ia->ia_ifa, fibnum); 216 } 217 } 218 219 int 220 in6_mask2len(struct in6_addr *mask, u_char *lim0) 221 { 222 int x = 0, y; 223 u_char *lim = lim0, *p; 224 225 /* ignore the scope_id part */ 226 if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) 227 lim = (u_char *)mask + sizeof(*mask); 228 for (p = (u_char *)mask; p < lim; x++, p++) { 229 if (*p != 0xff) 230 break; 231 } 232 y = 0; 233 if (p < lim) { 234 for (y = 0; y < 8; y++) { 235 if ((*p & (0x80 >> y)) == 0) 236 break; 237 } 238 } 239 240 /* 241 * when the limit pointer is given, do a stricter check on the 242 * remaining bits. 243 */ 244 if (p < lim) { 245 if (y != 0 && (*p & (0x00ff >> y)) != 0) 246 return (-1); 247 for (p = p + 1; p < lim; p++) 248 if (*p != 0) 249 return (-1); 250 } 251 252 return x * 8 + y; 253 } 254 255 #ifdef COMPAT_FREEBSD32 256 struct in6_ndifreq32 { 257 char ifname[IFNAMSIZ]; 258 uint32_t ifindex; 259 }; 260 #define SIOCGDEFIFACE32_IN6 _IOWR('i', 86, struct in6_ndifreq32) 261 #endif 262 263 int 264 in6_control_ioctl(u_long cmd, void *data, 265 struct ifnet *ifp, struct ucred *cred) 266 { 267 struct in6_ifreq *ifr = (struct in6_ifreq *)data; 268 struct in6_ifaddr *ia = NULL; 269 struct in6_aliasreq tmpifra, *ifra = (struct in6_aliasreq *)data; 270 struct sockaddr_in6 *sa6; 271 int error; 272 bool control_locked = false; 273 274 /* 275 * Compat to make pre-10.x ifconfig(8) operable. 276 */ 277 if (cmd == OSIOCAIFADDR_IN6) { 278 gone_in(17, "%s (pid %d) uses OSIOCAIFADDR_IN6", 279 curthread->td_proc->p_comm, curthread->td_proc->p_pid); 280 cmd = SIOCAIFADDR_IN6; 281 ifra = &tmpifra; 282 memset(ifra, 0, sizeof(*ifra)); 283 memcpy(ifra, data, sizeof(struct oin6_aliasreq)); 284 } 285 286 switch (cmd) { 287 case SIOCAADDRCTL_POLICY: 288 case SIOCDADDRCTL_POLICY: 289 if (cred != NULL) { 290 error = priv_check_cred(cred, PRIV_NETINET_ADDRCTRL6); 291 if (error) 292 return (error); 293 } 294 return (in6_src_ioctl(cmd, data)); 295 } 296 297 if (ifp == NULL) 298 return (EOPNOTSUPP); 299 300 switch (cmd) { 301 case SIOCSNDFLUSH_IN6: 302 case SIOCSPFXFLUSH_IN6: 303 case SIOCSRTRFLUSH_IN6: 304 case SIOCSDEFIFACE_IN6: 305 case SIOCSIFINFO_FLAGS: 306 case SIOCSIFINFO_IN6: 307 if (cred != NULL) { 308 error = priv_check_cred(cred, PRIV_NETINET_ND6); 309 if (error) 310 return (error); 311 } 312 /* FALLTHROUGH */ 313 case SIOCGIFINFO_IN6: 314 case SIOCGNBRINFO_IN6: 315 case SIOCGDEFIFACE_IN6: 316 return (nd6_ioctl(cmd, data, ifp)); 317 318 #ifdef COMPAT_FREEBSD32 319 case SIOCGDEFIFACE32_IN6: 320 { 321 struct in6_ndifreq ndif; 322 struct in6_ndifreq32 *ndif32; 323 324 error = nd6_ioctl(SIOCGDEFIFACE_IN6, (caddr_t)&ndif, 325 ifp); 326 if (error) 327 return (error); 328 ndif32 = (struct in6_ndifreq32 *)data; 329 ndif32->ifindex = ndif.ifindex; 330 return (0); 331 } 332 #endif 333 } 334 335 switch (cmd) { 336 case SIOCSIFPREFIX_IN6: 337 case SIOCDIFPREFIX_IN6: 338 case SIOCAIFPREFIX_IN6: 339 case SIOCCIFPREFIX_IN6: 340 case SIOCSGIFPREFIX_IN6: 341 case SIOCGIFPREFIX_IN6: 342 log(LOG_NOTICE, 343 "prefix ioctls are now invalidated. " 344 "please use ifconfig.\n"); 345 return (EOPNOTSUPP); 346 } 347 348 switch (cmd) { 349 case SIOCSSCOPE6: 350 if (cred != NULL) { 351 error = priv_check_cred(cred, PRIV_NETINET_SCOPE6); 352 if (error) 353 return (error); 354 } 355 /* FALLTHROUGH */ 356 case SIOCGSCOPE6: 357 case SIOCGSCOPE6DEF: 358 return (scope6_ioctl(cmd, data, ifp)); 359 } 360 361 /* 362 * Find address for this interface, if it exists. 363 * 364 * In netinet code, we have checked ifra_addr in SIOCSIF*ADDR operation 365 * only, and used the first interface address as the target of other 366 * operations (without checking ifra_addr). This was because netinet 367 * code/API assumed at most 1 interface address per interface. 368 * Since IPv6 allows a node to assign multiple addresses 369 * on a single interface, we almost always look and check the 370 * presence of ifra_addr, and reject invalid ones here. 371 * It also decreases duplicated code among SIOC*_IN6 operations. 372 */ 373 switch (cmd) { 374 case SIOCAIFADDR_IN6: 375 case SIOCSIFPHYADDR_IN6: 376 sa6 = &ifra->ifra_addr; 377 break; 378 case SIOCSIFADDR_IN6: 379 case SIOCGIFADDR_IN6: 380 case SIOCSIFDSTADDR_IN6: 381 case SIOCSIFNETMASK_IN6: 382 case SIOCGIFDSTADDR_IN6: 383 case SIOCGIFNETMASK_IN6: 384 case SIOCDIFADDR_IN6: 385 case SIOCGIFPSRCADDR_IN6: 386 case SIOCGIFPDSTADDR_IN6: 387 case SIOCGIFAFLAG_IN6: 388 case SIOCSNDFLUSH_IN6: 389 case SIOCSPFXFLUSH_IN6: 390 case SIOCSRTRFLUSH_IN6: 391 case SIOCGIFALIFETIME_IN6: 392 case SIOCGIFSTAT_IN6: 393 case SIOCGIFSTAT_ICMP6: 394 sa6 = &ifr->ifr_addr; 395 break; 396 case SIOCSIFADDR: 397 case SIOCSIFBRDADDR: 398 case SIOCSIFDSTADDR: 399 case SIOCSIFNETMASK: 400 /* 401 * Although we should pass any non-INET6 ioctl requests 402 * down to driver, we filter some legacy INET requests. 403 * Drivers trust SIOCSIFADDR et al to come from an already 404 * privileged layer, and do not perform any credentials 405 * checks or input validation. 406 */ 407 return (EINVAL); 408 default: 409 sa6 = NULL; 410 break; 411 } 412 if (sa6 && sa6->sin6_family == AF_INET6) { 413 if (sa6->sin6_scope_id != 0) 414 error = sa6_embedscope(sa6, 0); 415 else 416 error = in6_setscope(&sa6->sin6_addr, ifp, NULL); 417 if (error != 0) 418 return (error); 419 if (cred != NULL && (error = prison_check_ip6(cred, 420 &sa6->sin6_addr)) != 0) 421 return (error); 422 sx_xlock(&in6_control_sx); 423 control_locked = true; 424 ia = in6ifa_ifpwithaddr(ifp, &sa6->sin6_addr); 425 } else 426 ia = NULL; 427 428 switch (cmd) { 429 case SIOCSIFADDR_IN6: 430 case SIOCSIFDSTADDR_IN6: 431 case SIOCSIFNETMASK_IN6: 432 /* 433 * Since IPv6 allows a node to assign multiple addresses 434 * on a single interface, SIOCSIFxxx ioctls are deprecated. 435 */ 436 /* we decided to obsolete this command (20000704) */ 437 error = EINVAL; 438 goto out; 439 440 case SIOCDIFADDR_IN6: 441 /* 442 * for IPv4, we look for existing in_ifaddr here to allow 443 * "ifconfig if0 delete" to remove the first IPv4 address on 444 * the interface. For IPv6, as the spec allows multiple 445 * interface address from the day one, we consider "remove the 446 * first one" semantics to be not preferable. 447 */ 448 if (ia == NULL) { 449 error = EADDRNOTAVAIL; 450 goto out; 451 } 452 /* FALLTHROUGH */ 453 case SIOCAIFADDR_IN6: 454 /* 455 * We always require users to specify a valid IPv6 address for 456 * the corresponding operation. 457 */ 458 if (ifra->ifra_addr.sin6_family != AF_INET6 || 459 ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6)) { 460 error = EAFNOSUPPORT; 461 goto out; 462 } 463 464 if (cred != NULL) { 465 error = priv_check_cred(cred, (cmd == SIOCDIFADDR_IN6) ? 466 PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR); 467 if (error) 468 goto out; 469 } 470 /* FALLTHROUGH */ 471 case SIOCGIFSTAT_IN6: 472 case SIOCGIFSTAT_ICMP6: 473 if (ifp->if_inet6 == NULL) { 474 error = EPFNOSUPPORT; 475 goto out; 476 } 477 break; 478 479 case SIOCGIFADDR_IN6: 480 /* This interface is basically deprecated. use SIOCGIFCONF. */ 481 /* FALLTHROUGH */ 482 case SIOCGIFAFLAG_IN6: 483 case SIOCGIFNETMASK_IN6: 484 case SIOCGIFDSTADDR_IN6: 485 case SIOCGIFALIFETIME_IN6: 486 /* must think again about its semantics */ 487 if (ia == NULL) { 488 error = EADDRNOTAVAIL; 489 goto out; 490 } 491 break; 492 } 493 494 switch (cmd) { 495 case SIOCGIFADDR_IN6: 496 ifr->ifr_addr = ia->ia_addr; 497 if ((error = sa6_recoverscope(&ifr->ifr_addr)) != 0) 498 goto out; 499 break; 500 501 case SIOCGIFDSTADDR_IN6: 502 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 503 error = EINVAL; 504 goto out; 505 } 506 ifr->ifr_dstaddr = ia->ia_dstaddr; 507 if ((error = sa6_recoverscope(&ifr->ifr_dstaddr)) != 0) 508 goto out; 509 break; 510 511 case SIOCGIFNETMASK_IN6: 512 ifr->ifr_addr = ia->ia_prefixmask; 513 break; 514 515 case SIOCGIFAFLAG_IN6: 516 ifr->ifr_ifru.ifru_flags6 = ia->ia6_flags; 517 break; 518 519 case SIOCGIFSTAT_IN6: 520 COUNTER_ARRAY_COPY(ifp->if_inet6->in6_ifstat, 521 &ifr->ifr_ifru.ifru_stat, 522 sizeof(struct in6_ifstat) / sizeof(uint64_t)); 523 break; 524 525 case SIOCGIFSTAT_ICMP6: 526 COUNTER_ARRAY_COPY(ifp->if_inet6->icmp6_ifstat, 527 &ifr->ifr_ifru.ifru_icmp6stat, 528 sizeof(struct icmp6_ifstat) / sizeof(uint64_t)); 529 break; 530 531 case SIOCGIFALIFETIME_IN6: 532 ifr->ifr_ifru.ifru_lifetime = ia->ia6_lifetime; 533 if (ia->ia6_lifetime.ia6t_vltime != ND6_INFINITE_LIFETIME) { 534 time_t maxexpire; 535 struct in6_addrlifetime *retlt = 536 &ifr->ifr_ifru.ifru_lifetime; 537 538 /* 539 * XXX: adjust expiration time assuming time_t is 540 * signed. 541 */ 542 maxexpire = (-1) & 543 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 544 if (ia->ia6_lifetime.ia6t_vltime < 545 maxexpire - ia->ia6_updatetime) { 546 retlt->ia6t_expire = ia->ia6_updatetime + 547 ia->ia6_lifetime.ia6t_vltime; 548 } else 549 retlt->ia6t_expire = maxexpire; 550 } 551 if (ia->ia6_lifetime.ia6t_pltime != ND6_INFINITE_LIFETIME) { 552 time_t maxexpire; 553 struct in6_addrlifetime *retlt = 554 &ifr->ifr_ifru.ifru_lifetime; 555 556 /* 557 * XXX: adjust expiration time assuming time_t is 558 * signed. 559 */ 560 maxexpire = (-1) & 561 ~((time_t)1 << ((sizeof(maxexpire) * 8) - 1)); 562 if (ia->ia6_lifetime.ia6t_pltime < 563 maxexpire - ia->ia6_updatetime) { 564 retlt->ia6t_preferred = ia->ia6_updatetime + 565 ia->ia6_lifetime.ia6t_pltime; 566 } else 567 retlt->ia6t_preferred = maxexpire; 568 } 569 break; 570 571 case SIOCAIFADDR_IN6: 572 #ifdef MAC 573 /* Check if a MAC policy disallows setting the IPv6 address. */ 574 error = mac_inet6_check_add_addr(cred, &sa6->sin6_addr, ifp); 575 if (error != 0) 576 goto out; 577 #endif 578 error = in6_addifaddr(ifp, ifra, ia); 579 ia = NULL; 580 goto out; 581 582 case SIOCDIFADDR_IN6: 583 in6_purgeifaddr(ia); 584 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa, 585 IFADDR_EVENT_DEL); 586 break; 587 588 default: 589 if (ifp->if_ioctl == NULL) { 590 error = EOPNOTSUPP; 591 goto out; 592 } 593 error = (*ifp->if_ioctl)(ifp, cmd, data); 594 goto out; 595 } 596 597 error = 0; 598 out: 599 if (control_locked) 600 sx_xunlock(&in6_control_sx); 601 602 if (ia != NULL) 603 ifa_free(&ia->ia_ifa); 604 return (error); 605 } 606 607 int 608 in6_control(struct socket *so, u_long cmd, void *data, 609 struct ifnet *ifp, struct thread *td) 610 { 611 switch (cmd) { 612 case SIOCGETSGCNT_IN6: 613 case SIOCGETMIFCNT_IN6: 614 return (mrt6_ioctl ? 615 mrt6_ioctl(cmd, data, so->so_fibnum) : EOPNOTSUPP); 616 } 617 return (in6_control_ioctl(cmd, data, ifp, td ? td->td_ucred : NULL)); 618 } 619 620 static struct in6_multi_mship * 621 in6_joingroup_legacy(struct ifnet *ifp, const struct in6_addr *mcaddr, 622 int *errorp, int delay) 623 { 624 struct in6_multi_mship *imm; 625 int error; 626 627 imm = malloc(sizeof(*imm), M_IP6MADDR, M_NOWAIT); 628 if (imm == NULL) { 629 *errorp = ENOBUFS; 630 return (NULL); 631 } 632 633 delay = (delay * MLD_FASTHZ) / hz; 634 635 error = in6_joingroup(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay); 636 if (error) { 637 *errorp = error; 638 free(imm, M_IP6MADDR); 639 return (NULL); 640 } 641 642 return (imm); 643 } 644 645 static int 646 in6_solicited_node_maddr(struct in6_addr *maddr, 647 struct ifnet *ifp, const struct in6_addr *base) 648 { 649 int error; 650 651 bzero(maddr, sizeof(struct in6_addr)); 652 maddr->s6_addr32[0] = IPV6_ADDR_INT32_MLL; 653 maddr->s6_addr32[2] = htonl(1); 654 maddr->s6_addr32[3] = base->s6_addr32[3]; 655 maddr->s6_addr8[12] = 0xff; 656 if ((error = in6_setscope(maddr, ifp, NULL)) != 0) { 657 /* XXX: should not happen */ 658 log(LOG_ERR, "%s: in6_setscope failed\n", __func__); 659 } 660 661 return error; 662 } 663 664 /* 665 * Join necessary multicast groups. Factored out from in6_update_ifa(). 666 * This entire work should only be done once, for the default FIB. 667 */ 668 static int 669 in6_update_ifa_join_mc(struct ifnet *ifp, struct in6_aliasreq *ifra, 670 struct in6_ifaddr *ia, int flags, struct in6_multi **in6m_sol) 671 { 672 char ip6buf[INET6_ADDRSTRLEN]; 673 struct in6_addr mltaddr; 674 struct in6_multi_mship *imm; 675 int delay, error; 676 677 KASSERT(in6m_sol != NULL, ("%s: in6m_sol is NULL", __func__)); 678 679 /* Join solicited multicast addr for new host id. */ 680 if ((error = in6_solicited_node_maddr(&mltaddr, ifp, 681 &ifra->ifra_addr.sin6_addr)) != 0) 682 goto cleanup; 683 delay = error = 0; 684 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 685 /* 686 * We need a random delay for DAD on the address being 687 * configured. It also means delaying transmission of the 688 * corresponding MLD report to avoid report collision. 689 * [RFC 4861, Section 6.3.7] 690 */ 691 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz); 692 } 693 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay); 694 if (imm == NULL) { 695 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s " 696 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr), 697 if_name(ifp), error)); 698 goto cleanup; 699 } 700 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 701 *in6m_sol = imm->i6mm_maddr; 702 703 /* 704 * Join link-local all-nodes address. 705 */ 706 mltaddr = in6addr_linklocal_allnodes; 707 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0) 708 goto cleanup; /* XXX: should not fail */ 709 710 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, 0); 711 if (imm == NULL) { 712 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s " 713 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, &mltaddr), 714 if_name(ifp), error)); 715 goto cleanup; 716 } 717 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 718 719 /* 720 * Join node information group address. 721 */ 722 delay = 0; 723 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 724 /* 725 * The spec does not say anything about delay for this group, 726 * but the same logic should apply. 727 */ 728 delay = arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz); 729 } 730 if (in6_nigroup(ifp, NULL, -1, &mltaddr) == 0) { 731 /* XXX jinmei */ 732 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay); 733 if (imm == NULL) 734 nd6log((LOG_WARNING, 735 "%s: in6_joingroup failed for %s on %s " 736 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, 737 &mltaddr), if_name(ifp), error)); 738 /* XXX not very fatal, go on... */ 739 else 740 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 741 } 742 if (V_icmp6_nodeinfo_oldmcprefix && 743 in6_nigroup_oldmcprefix(ifp, NULL, -1, &mltaddr) == 0) { 744 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, delay); 745 if (imm == NULL) 746 nd6log((LOG_WARNING, 747 "%s: in6_joingroup failed for %s on %s " 748 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, 749 &mltaddr), if_name(ifp), error)); 750 /* XXX not very fatal, go on... */ 751 else 752 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 753 } 754 755 /* 756 * Join interface-local all-nodes address. 757 * (ff01::1%ifN, and ff01::%ifN/32) 758 */ 759 mltaddr = in6addr_nodelocal_allnodes; 760 if ((error = in6_setscope(&mltaddr, ifp, NULL)) != 0) 761 goto cleanup; /* XXX: should not fail */ 762 763 imm = in6_joingroup_legacy(ifp, &mltaddr, &error, 0); 764 if (imm == NULL) { 765 nd6log((LOG_WARNING, "%s: in6_joingroup failed for %s on %s " 766 "(errno=%d)\n", __func__, ip6_sprintf(ip6buf, 767 &mltaddr), if_name(ifp), error)); 768 goto cleanup; 769 } 770 LIST_INSERT_HEAD(&ia->ia6_memberships, imm, i6mm_chain); 771 772 cleanup: 773 return (error); 774 } 775 776 /* 777 * Update parameters of an IPv6 interface address. 778 * If necessary, a new entry is created and linked into address chains. 779 * This function is separated from in6_control(). 780 */ 781 int 782 in6_update_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 783 struct in6_ifaddr *ia, int flags) 784 { 785 int error, hostIsNew = 0; 786 787 if ((error = in6_validate_ifra(ifp, ifra, ia, flags)) != 0) 788 return (error); 789 790 if (ia == NULL) { 791 hostIsNew = 1; 792 if ((ia = in6_alloc_ifa(ifp, ifra, flags)) == NULL) 793 return (ENOBUFS); 794 } 795 796 error = in6_update_ifa_internal(ifp, ifra, ia, hostIsNew, flags); 797 if (error != 0) { 798 if (hostIsNew != 0) { 799 in6_unlink_ifa(ia, ifp); 800 ifa_free(&ia->ia_ifa); 801 } 802 return (error); 803 } 804 805 if (hostIsNew) 806 error = in6_broadcast_ifa(ifp, ifra, ia, flags); 807 808 return (error); 809 } 810 811 /* 812 * Fill in basic IPv6 address request info. 813 */ 814 void 815 in6_prepare_ifra(struct in6_aliasreq *ifra, const struct in6_addr *addr, 816 const struct in6_addr *mask) 817 { 818 819 memset(ifra, 0, sizeof(struct in6_aliasreq)); 820 821 ifra->ifra_addr.sin6_family = AF_INET6; 822 ifra->ifra_addr.sin6_len = sizeof(struct sockaddr_in6); 823 if (addr != NULL) 824 ifra->ifra_addr.sin6_addr = *addr; 825 826 ifra->ifra_prefixmask.sin6_family = AF_INET6; 827 ifra->ifra_prefixmask.sin6_len = sizeof(struct sockaddr_in6); 828 if (mask != NULL) 829 ifra->ifra_prefixmask.sin6_addr = *mask; 830 } 831 832 static int 833 in6_validate_ifra(struct ifnet *ifp, struct in6_aliasreq *ifra, 834 struct in6_ifaddr *ia, int flags) 835 { 836 int plen = -1; 837 struct sockaddr_in6 dst6; 838 struct in6_addrlifetime *lt; 839 char ip6buf[INET6_ADDRSTRLEN]; 840 841 /* Validate parameters */ 842 if (ifp == NULL || ifra == NULL) /* this maybe redundant */ 843 return (EINVAL); 844 845 /* 846 * The destination address for a p2p link must have a family 847 * of AF_UNSPEC or AF_INET6. 848 */ 849 if ((ifp->if_flags & IFF_POINTOPOINT) != 0 && 850 ifra->ifra_dstaddr.sin6_family != AF_INET6 && 851 ifra->ifra_dstaddr.sin6_family != AF_UNSPEC) 852 return (EAFNOSUPPORT); 853 854 /* 855 * Validate address 856 */ 857 if (ifra->ifra_addr.sin6_len != sizeof(struct sockaddr_in6) || 858 ifra->ifra_addr.sin6_family != AF_INET6) 859 return (EINVAL); 860 861 /* 862 * validate ifra_prefixmask. don't check sin6_family, netmask 863 * does not carry fields other than sin6_len. 864 */ 865 if (ifra->ifra_prefixmask.sin6_len > sizeof(struct sockaddr_in6)) 866 return (EINVAL); 867 /* 868 * Because the IPv6 address architecture is classless, we require 869 * users to specify a (non 0) prefix length (mask) for a new address. 870 * We also require the prefix (when specified) mask is valid, and thus 871 * reject a non-consecutive mask. 872 */ 873 if (ia == NULL && ifra->ifra_prefixmask.sin6_len == 0) 874 return (EINVAL); 875 if (ifra->ifra_prefixmask.sin6_len != 0) { 876 plen = in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, 877 (u_char *)&ifra->ifra_prefixmask + 878 ifra->ifra_prefixmask.sin6_len); 879 if (plen <= 0) 880 return (EINVAL); 881 } else { 882 /* 883 * In this case, ia must not be NULL. We just use its prefix 884 * length. 885 */ 886 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); 887 } 888 /* 889 * If the destination address on a p2p interface is specified, 890 * and the address is a scoped one, validate/set the scope 891 * zone identifier. 892 */ 893 dst6 = ifra->ifra_dstaddr; 894 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) != 0 && 895 (dst6.sin6_family == AF_INET6)) { 896 struct in6_addr in6_tmp; 897 u_int32_t zoneid; 898 899 in6_tmp = dst6.sin6_addr; 900 if (in6_setscope(&in6_tmp, ifp, &zoneid)) 901 return (EINVAL); /* XXX: should be impossible */ 902 903 if (dst6.sin6_scope_id != 0) { 904 if (dst6.sin6_scope_id != zoneid) 905 return (EINVAL); 906 } else /* user omit to specify the ID. */ 907 dst6.sin6_scope_id = zoneid; 908 909 /* convert into the internal form */ 910 if (sa6_embedscope(&dst6, 0)) 911 return (EINVAL); /* XXX: should be impossible */ 912 } 913 /* Modify original ifra_dstaddr to reflect changes */ 914 ifra->ifra_dstaddr = dst6; 915 916 /* 917 * The destination address can be specified only for a p2p or a 918 * loopback interface. If specified, the corresponding prefix length 919 * must be 128. 920 */ 921 if (ifra->ifra_dstaddr.sin6_family == AF_INET6) { 922 if ((ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)) == 0) { 923 /* XXX: noisy message */ 924 nd6log((LOG_INFO, "in6_update_ifa: a destination can " 925 "be specified for a p2p or a loopback IF only\n")); 926 return (EINVAL); 927 } 928 if (plen != 128) { 929 nd6log((LOG_INFO, "in6_update_ifa: prefixlen should " 930 "be 128 when dstaddr is specified\n")); 931 return (EINVAL); 932 } 933 } 934 /* lifetime consistency check */ 935 lt = &ifra->ifra_lifetime; 936 if (lt->ia6t_pltime > lt->ia6t_vltime) 937 return (EINVAL); 938 if (lt->ia6t_vltime == 0) { 939 /* 940 * the following log might be noisy, but this is a typical 941 * configuration mistake or a tool's bug. 942 */ 943 nd6log((LOG_INFO, 944 "in6_update_ifa: valid lifetime is 0 for %s\n", 945 ip6_sprintf(ip6buf, &ifra->ifra_addr.sin6_addr))); 946 947 if (ia == NULL) 948 return (0); /* there's nothing to do */ 949 } 950 951 /* Check prefix mask */ 952 if (ia != NULL && ifra->ifra_prefixmask.sin6_len != 0) { 953 /* 954 * We prohibit changing the prefix length of an existing 955 * address, because 956 * + such an operation should be rare in IPv6, and 957 * + the operation would confuse prefix management. 958 */ 959 if (ia->ia_prefixmask.sin6_len != 0 && 960 in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL) != plen) { 961 nd6log((LOG_INFO, "in6_validate_ifa: the prefix length " 962 "of an existing %s address should not be changed\n", 963 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 964 965 return (EINVAL); 966 } 967 } 968 969 return (0); 970 } 971 972 /* 973 * Allocate a new ifaddr and link it into chains. 974 */ 975 static struct in6_ifaddr * 976 in6_alloc_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, int flags) 977 { 978 struct in6_ifaddr *ia; 979 980 /* 981 * When in6_alloc_ifa() is called in a process of a received 982 * RA, it is called under an interrupt context. So, we should 983 * call malloc with M_NOWAIT. 984 */ 985 ia = (struct in6_ifaddr *)ifa_alloc(sizeof(*ia), M_NOWAIT); 986 if (ia == NULL) 987 return (NULL); 988 LIST_INIT(&ia->ia6_memberships); 989 /* Initialize the address and masks, and put time stamp */ 990 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 991 ia->ia_addr.sin6_family = AF_INET6; 992 ia->ia_addr.sin6_len = sizeof(ia->ia_addr); 993 /* XXX: Can we assign ,sin6_addr and skip the rest? */ 994 ia->ia_addr = ifra->ifra_addr; 995 ia->ia6_createtime = time_uptime; 996 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) != 0) { 997 /* 998 * Some functions expect that ifa_dstaddr is not 999 * NULL for p2p interfaces. 1000 */ 1001 ia->ia_ifa.ifa_dstaddr = 1002 (struct sockaddr *)&ia->ia_dstaddr; 1003 } else { 1004 ia->ia_ifa.ifa_dstaddr = NULL; 1005 } 1006 1007 /* set prefix mask if any */ 1008 ia->ia_ifa.ifa_netmask = (struct sockaddr *)&ia->ia_prefixmask; 1009 if (ifra->ifra_prefixmask.sin6_len != 0) { 1010 ia->ia_prefixmask.sin6_family = AF_INET6; 1011 ia->ia_prefixmask.sin6_len = ifra->ifra_prefixmask.sin6_len; 1012 ia->ia_prefixmask.sin6_addr = ifra->ifra_prefixmask.sin6_addr; 1013 } 1014 1015 ia->ia_ifp = ifp; 1016 ifa_ref(&ia->ia_ifa); /* if_addrhead */ 1017 IF_ADDR_WLOCK(ifp); 1018 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 1019 IF_ADDR_WUNLOCK(ifp); 1020 1021 ifa_ref(&ia->ia_ifa); /* in6_ifaddrhead */ 1022 IN6_IFADDR_WLOCK(); 1023 CK_STAILQ_INSERT_TAIL(&V_in6_ifaddrhead, ia, ia_link); 1024 CK_LIST_INSERT_HEAD(IN6ADDR_HASH(&ia->ia_addr.sin6_addr), ia, ia6_hash); 1025 IN6_IFADDR_WUNLOCK(); 1026 1027 return (ia); 1028 } 1029 1030 time_t 1031 in6_expire_time(uint32_t ltime) 1032 { 1033 if (ltime == ND6_INFINITE_LIFETIME) 1034 return (0); 1035 else 1036 return (time_uptime + ltime); 1037 } 1038 1039 /* 1040 * Update/configure interface address parameters: 1041 * 1042 * 1) Update lifetime 1043 * 2) Update interface metric ad flags 1044 * 3) Notify other subsystems 1045 */ 1046 static int 1047 in6_update_ifa_internal(struct ifnet *ifp, struct in6_aliasreq *ifra, 1048 struct in6_ifaddr *ia, int hostIsNew, int flags) 1049 { 1050 int error; 1051 1052 /* update timestamp */ 1053 ia->ia6_updatetime = time_uptime; 1054 1055 /* 1056 * Set lifetimes. We do not refer to ia6t_expire and ia6t_preferred 1057 * to see if the address is deprecated or invalidated, but initialize 1058 * these members for applications. 1059 */ 1060 ia->ia6_lifetime = ifra->ifra_lifetime; 1061 ia->ia6_lifetime.ia6t_expire = 1062 in6_expire_time(ifra->ifra_lifetime.ia6t_vltime); 1063 ia->ia6_lifetime.ia6t_preferred = 1064 in6_expire_time(ifra->ifra_lifetime.ia6t_pltime); 1065 1066 /* 1067 * backward compatibility - if IN6_IFF_DEPRECATED is set from the 1068 * userland, make it deprecated. 1069 */ 1070 if ((ifra->ifra_flags & IN6_IFF_DEPRECATED) != 0) { 1071 ia->ia6_lifetime.ia6t_pltime = 0; 1072 ia->ia6_lifetime.ia6t_preferred = time_uptime; 1073 } 1074 1075 /* 1076 * configure address flags. 1077 */ 1078 ia->ia6_flags = ifra->ifra_flags; 1079 1080 /* 1081 * Make the address tentative before joining multicast addresses, 1082 * so that corresponding MLD responses would not have a tentative 1083 * source address. 1084 */ 1085 ia->ia6_flags &= ~IN6_IFF_DUPLICATED; /* safety */ 1086 1087 /* 1088 * DAD should be performed for an new address or addresses on 1089 * an interface with ND6_IFF_IFDISABLED. 1090 */ 1091 if (in6if_do_dad(ifp) && 1092 (hostIsNew || (ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED))) 1093 ia->ia6_flags |= IN6_IFF_TENTATIVE; 1094 1095 /* notify other subsystems */ 1096 error = in6_notify_ifa(ifp, ia, ifra, hostIsNew); 1097 1098 return (error); 1099 } 1100 1101 /* 1102 * Do link-level ifa job: 1103 * 1) Add lle entry for added address 1104 * 2) Notifies routing socket users about new address 1105 * 3) join appropriate multicast group 1106 * 4) start DAD if enabled 1107 */ 1108 static int 1109 in6_broadcast_ifa(struct ifnet *ifp, struct in6_aliasreq *ifra, 1110 struct in6_ifaddr *ia, int flags) 1111 { 1112 struct in6_multi *in6m_sol; 1113 int error = 0; 1114 1115 /* Add local address to lltable, if necessary (ex. on p2p link). */ 1116 if ((error = nd6_add_ifa_lle(ia)) != 0) { 1117 in6_purgeaddr(&ia->ia_ifa); 1118 ifa_free(&ia->ia_ifa); 1119 return (error); 1120 } 1121 1122 /* Join necessary multicast groups. */ 1123 in6m_sol = NULL; 1124 if ((ifp->if_flags & IFF_MULTICAST) != 0) { 1125 error = in6_update_ifa_join_mc(ifp, ifra, ia, flags, &in6m_sol); 1126 if (error != 0) { 1127 in6_purgeaddr(&ia->ia_ifa); 1128 ifa_free(&ia->ia_ifa); 1129 return (error); 1130 } 1131 } 1132 1133 /* Perform DAD, if the address is TENTATIVE. */ 1134 if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) { 1135 int delay, mindelay, maxdelay; 1136 1137 delay = 0; 1138 if ((flags & IN6_IFAUPDATE_DADDELAY)) { 1139 /* 1140 * We need to impose a delay before sending an NS 1141 * for DAD. Check if we also needed a delay for the 1142 * corresponding MLD message. If we did, the delay 1143 * should be larger than the MLD delay (this could be 1144 * relaxed a bit, but this simple logic is at least 1145 * safe). 1146 * XXX: Break data hiding guidelines and look at 1147 * state for the solicited multicast group. 1148 */ 1149 mindelay = 0; 1150 if (in6m_sol != NULL && 1151 in6m_sol->in6m_state == MLD_REPORTING_MEMBER) { 1152 mindelay = in6m_sol->in6m_timer; 1153 } 1154 maxdelay = MAX_RTR_SOLICITATION_DELAY * hz; 1155 if (maxdelay - mindelay == 0) 1156 delay = 0; 1157 else { 1158 delay = 1159 (arc4random() % (maxdelay - mindelay)) + 1160 mindelay; 1161 } 1162 } 1163 nd6_dad_start((struct ifaddr *)ia, delay); 1164 } 1165 1166 in6_newaddrmsg(ia, RTM_ADD); 1167 ifa_free(&ia->ia_ifa); 1168 return (error); 1169 } 1170 1171 /* 1172 * Adds or deletes interface route for p2p ifa. 1173 * Returns 0 on success or errno. 1174 */ 1175 static int 1176 in6_handle_dstaddr_rtrequest(int cmd, struct in6_ifaddr *ia) 1177 { 1178 struct epoch_tracker et; 1179 struct ifaddr *ifa = &ia->ia_ifa; 1180 int error; 1181 1182 /* Prepare gateway */ 1183 struct sockaddr_dl_short sdl = { 1184 .sdl_family = AF_LINK, 1185 .sdl_len = sizeof(struct sockaddr_dl_short), 1186 .sdl_type = ifa->ifa_ifp->if_type, 1187 .sdl_index = ifa->ifa_ifp->if_index, 1188 }; 1189 1190 struct sockaddr_in6 dst = { 1191 .sin6_family = AF_INET6, 1192 .sin6_len = sizeof(struct sockaddr_in6), 1193 .sin6_addr = ia->ia_dstaddr.sin6_addr, 1194 }; 1195 1196 struct rt_addrinfo info = { 1197 .rti_ifa = ifa, 1198 .rti_ifp = ifa->ifa_ifp, 1199 .rti_flags = RTF_PINNED | RTF_HOST, 1200 .rti_info = { 1201 [RTAX_DST] = (struct sockaddr *)&dst, 1202 [RTAX_GATEWAY] = (struct sockaddr *)&sdl, 1203 }, 1204 }; 1205 /* Don't set additional per-gw filters on removal */ 1206 1207 NET_EPOCH_ENTER(et); 1208 error = rib_handle_ifaddr_info(ifa->ifa_ifp->if_fib, cmd, &info); 1209 NET_EPOCH_EXIT(et); 1210 1211 return (error); 1212 } 1213 1214 static bool 1215 ifa_is_p2p(struct in6_ifaddr *ia) 1216 { 1217 int plen; 1218 1219 plen = in6_mask2len(&ia->ia_prefixmask.sin6_addr, NULL); /* XXX */ 1220 1221 if ((plen == 128) && (ia->ia_dstaddr.sin6_family == AF_INET6) && 1222 !IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &ia->ia_dstaddr.sin6_addr)) 1223 return (true); 1224 1225 return (false); 1226 } 1227 1228 int 1229 in6_addifaddr(struct ifnet *ifp, struct in6_aliasreq *ifra, struct in6_ifaddr *ia) 1230 { 1231 struct nd_prefixctl pr0; 1232 struct nd_prefix *pr; 1233 int carp_attached = 0; 1234 int error; 1235 1236 /* 1237 * Check if bridge wants to allow adding addrs to member interfaces. 1238 */ 1239 if (ifp->if_bridge != NULL && ifp->if_type != IFT_GIF && 1240 bridge_member_ifaddrs_p != NULL) { 1241 if (bridge_member_ifaddrs_p()) { 1242 if_printf(ifp, "WARNING: Assigning an IP address to " 1243 "an interface which is also a bridge member is " 1244 "deprecated and will be unsupported in a future " 1245 "release.\n"); 1246 } else { 1247 error = EINVAL; 1248 goto out; 1249 } 1250 } 1251 1252 /* 1253 * first, make or update the interface address structure, 1254 * and link it to the list. 1255 */ 1256 if ((error = in6_update_ifa(ifp, ifra, ia, 0)) != 0) 1257 goto out; 1258 if (ia != NULL) { 1259 if (ia->ia_ifa.ifa_carp) 1260 (*carp_detach_p)(&ia->ia_ifa, true); 1261 ifa_free(&ia->ia_ifa); 1262 } 1263 if ((ia = in6ifa_ifpwithaddr(ifp, &ifra->ifra_addr.sin6_addr)) == NULL) { 1264 /* 1265 * this can happen when the user specify the 0 valid 1266 * lifetime. 1267 */ 1268 return (0); 1269 } 1270 1271 if (ifra->ifra_vhid > 0) { 1272 if (carp_attach_p != NULL) 1273 error = (*carp_attach_p)(&ia->ia_ifa, 1274 ifra->ifra_vhid); 1275 else 1276 error = EPROTONOSUPPORT; 1277 if (error) 1278 goto out; 1279 else 1280 carp_attached = 1; 1281 } 1282 1283 /* 1284 * then, make the prefix on-link on the interface. 1285 * XXX: we'd rather create the prefix before the address, but 1286 * we need at least one address to install the corresponding 1287 * interface route, so we configure the address first. 1288 */ 1289 1290 /* 1291 * convert mask to prefix length (prefixmask has already 1292 * been validated in in6_update_ifa(). 1293 */ 1294 bzero(&pr0, sizeof(pr0)); 1295 pr0.ndpr_ifp = ifp; 1296 pr0.ndpr_plen = ia->ia_plen = 1297 in6_mask2len(&ifra->ifra_prefixmask.sin6_addr, NULL); 1298 if (pr0.ndpr_plen == 128) { 1299 /* we don't need to install a host route. */ 1300 goto aifaddr_out; 1301 } 1302 pr0.ndpr_prefix = ifra->ifra_addr; 1303 /* apply the mask for safety. */ 1304 IN6_MASK_ADDR(&pr0.ndpr_prefix.sin6_addr, 1305 &ifra->ifra_prefixmask.sin6_addr); 1306 1307 /* 1308 * XXX: since we don't have an API to set prefix (not address) 1309 * lifetimes, we just use the same lifetimes as addresses. 1310 * The (temporarily) installed lifetimes can be overridden by 1311 * later advertised RAs (when accept_rtadv is non 0), which is 1312 * an intended behavior. 1313 */ 1314 pr0.ndpr_raf_onlink = 1; /* should be configurable? */ 1315 pr0.ndpr_raf_auto = 1316 ((ifra->ifra_flags & IN6_IFF_AUTOCONF) != 0); 1317 pr0.ndpr_vltime = ifra->ifra_lifetime.ia6t_vltime; 1318 pr0.ndpr_pltime = ifra->ifra_lifetime.ia6t_pltime; 1319 1320 /* add the prefix if not yet. */ 1321 if ((pr = nd6_prefix_lookup(&pr0)) == NULL) { 1322 /* 1323 * nd6_prelist_add will install the corresponding 1324 * interface route. 1325 */ 1326 if ((error = nd6_prelist_add(&pr0, &pr)) != 0) { 1327 if (carp_attached) 1328 (*carp_detach_p)(&ia->ia_ifa, false); 1329 goto out; 1330 } 1331 } else if (pr->ndpr_raf_onlink) { 1332 time_t expiry; 1333 1334 /* 1335 * If the prefix already exists, update lifetimes, but avoid 1336 * shortening them. 1337 */ 1338 ND6_WLOCK(); 1339 expiry = in6_expire_time(pr0.ndpr_pltime); 1340 if (pr->ndpr_preferred != 0 && 1341 (pr->ndpr_preferred < expiry || expiry == 0)) { 1342 pr->ndpr_pltime = pr0.ndpr_pltime; 1343 pr->ndpr_preferred = expiry; 1344 } 1345 expiry = in6_expire_time(pr0.ndpr_vltime); 1346 if (pr->ndpr_expire != 0 && 1347 (pr->ndpr_expire < expiry || expiry == 0)) { 1348 pr->ndpr_vltime = pr0.ndpr_vltime; 1349 pr->ndpr_expire = expiry; 1350 } 1351 pr->ndpr_lastupdate = time_uptime; 1352 ND6_WUNLOCK(); 1353 } 1354 1355 /* relate the address to the prefix */ 1356 if (ia->ia6_ndpr == NULL) { 1357 ia->ia6_ndpr = pr; 1358 pr->ndpr_addrcnt++; 1359 1360 /* 1361 * If this is the first autoconf address from the 1362 * prefix, create a temporary address as well 1363 * (when required). 1364 */ 1365 if ((ia->ia6_flags & IN6_IFF_AUTOCONF) && 1366 V_ip6_use_tempaddr && pr->ndpr_addrcnt == 1) { 1367 int e; 1368 if ((e = in6_tmpifadd(ia, 1, 0)) != 0) { 1369 log(LOG_NOTICE, "in6_control: failed " 1370 "to create a temporary address, " 1371 "errno=%d\n", e); 1372 } 1373 } 1374 } 1375 nd6_prefix_rele(pr); 1376 1377 /* 1378 * this might affect the status of autoconfigured addresses, 1379 * that is, this address might make other addresses detached. 1380 */ 1381 pfxlist_onlink_check(); 1382 1383 aifaddr_out: 1384 /* 1385 * Try to clear the flag when a new IPv6 address is added 1386 * onto an IFDISABLED interface and it succeeds. 1387 */ 1388 if (ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED) { 1389 struct in6_ndireq nd; 1390 1391 memset(&nd, 0, sizeof(nd)); 1392 nd.ndi.flags = ifp->if_inet6->nd_flags; 1393 nd.ndi.flags &= ~ND6_IFF_IFDISABLED; 1394 if (nd6_ioctl(SIOCSIFINFO_FLAGS, (caddr_t)&nd, ifp) < 0) 1395 log(LOG_NOTICE, "SIOCAIFADDR_IN6: " 1396 "SIOCSIFINFO_FLAGS for -ifdisabled " 1397 "failed."); 1398 /* 1399 * Ignore failure of clearing the flag intentionally. 1400 * The failure means address duplication was detected. 1401 */ 1402 } 1403 error = 0; 1404 1405 out: 1406 if (ia != NULL) 1407 ifa_free(&ia->ia_ifa); 1408 return (error); 1409 } 1410 1411 void 1412 in6_purgeaddr(struct ifaddr *ifa) 1413 { 1414 struct ifnet *ifp = ifa->ifa_ifp; 1415 struct in6_ifaddr *ia = (struct in6_ifaddr *) ifa; 1416 struct in6_multi_mship *imm; 1417 int error; 1418 1419 if (ifa->ifa_carp) 1420 (*carp_detach_p)(ifa, false); 1421 1422 /* 1423 * Remove the loopback route to the interface address. 1424 * The check for the current setting of "nd6_useloopback" 1425 * is not needed. 1426 */ 1427 if (ia->ia_flags & IFA_RTSELF) { 1428 error = ifa_del_loopback_route((struct ifaddr *)ia, 1429 (struct sockaddr *)&ia->ia_addr); 1430 if (error == 0) 1431 ia->ia_flags &= ~IFA_RTSELF; 1432 } 1433 1434 /* make sure there are no queued ND6 */ 1435 nd6_queue_stop(ifa); 1436 1437 /* stop DAD processing */ 1438 nd6_dad_stop(ifa); 1439 1440 /* Leave multicast groups. */ 1441 while ((imm = LIST_FIRST(&ia->ia6_memberships)) != NULL) { 1442 LIST_REMOVE(imm, i6mm_chain); 1443 if (imm->i6mm_maddr != NULL) 1444 in6_leavegroup(imm->i6mm_maddr, NULL); 1445 free(imm, M_IP6MADDR); 1446 } 1447 /* Check if we need to remove p2p route */ 1448 if ((ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) { 1449 error = in6_handle_dstaddr_rtrequest(RTM_DELETE, ia); 1450 if (error != 0) 1451 log(LOG_INFO, "%s: err=%d, destination address delete " 1452 "failed\n", __func__, error); 1453 ia->ia_flags &= ~IFA_ROUTE; 1454 } 1455 1456 in6_newaddrmsg(ia, RTM_DELETE); 1457 in6_unlink_ifa(ia, ifp); 1458 } 1459 1460 /* 1461 * Removes @ia from the corresponding interfaces and unlinks corresponding 1462 * prefix if no addresses are using it anymore. 1463 */ 1464 void 1465 in6_purgeifaddr(struct in6_ifaddr *ia) 1466 { 1467 struct nd_prefix *pr; 1468 1469 /* 1470 * If the address being deleted is the only one that owns 1471 * the corresponding prefix, expire the prefix as well. 1472 * XXX: theoretically, we don't have to worry about such 1473 * relationship, since we separate the address management 1474 * and the prefix management. We do this, however, to provide 1475 * as much backward compatibility as possible in terms of 1476 * the ioctl operation. 1477 * Note that in6_purgeaddr() will decrement ndpr_addrcnt. 1478 */ 1479 pr = ia->ia6_ndpr; 1480 in6_purgeaddr(&ia->ia_ifa); 1481 if (pr != NULL && pr->ndpr_addrcnt == 0) { 1482 ND6_WLOCK(); 1483 nd6_prefix_unlink(pr, NULL); 1484 ND6_WUNLOCK(); 1485 nd6_prefix_del(pr); 1486 } 1487 } 1488 1489 1490 static void 1491 in6_unlink_ifa(struct in6_ifaddr *ia, struct ifnet *ifp) 1492 { 1493 char ip6buf[INET6_ADDRSTRLEN]; 1494 int remove_lle; 1495 1496 IF_ADDR_WLOCK(ifp); 1497 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link); 1498 IF_ADDR_WUNLOCK(ifp); 1499 ifa_free(&ia->ia_ifa); /* if_addrhead */ 1500 1501 /* 1502 * Defer the release of what might be the last reference to the 1503 * in6_ifaddr so that it can't be freed before the remainder of the 1504 * cleanup. 1505 */ 1506 IN6_IFADDR_WLOCK(); 1507 CK_STAILQ_REMOVE(&V_in6_ifaddrhead, ia, in6_ifaddr, ia_link); 1508 CK_LIST_REMOVE(ia, ia6_hash); 1509 IN6_IFADDR_WUNLOCK(); 1510 1511 /* 1512 * Release the reference to the base prefix. There should be a 1513 * positive reference. 1514 */ 1515 remove_lle = 0; 1516 if (ia->ia6_ndpr != NULL) { 1517 ia->ia6_ndpr->ndpr_addrcnt--; 1518 /* Do not delete lles within prefix if refcont != 0 */ 1519 if (ia->ia6_ndpr->ndpr_addrcnt == 0) 1520 remove_lle = 1; 1521 ia->ia6_ndpr = NULL; 1522 } else if (ia->ia_plen < 128) { 1523 nd6log((LOG_NOTICE, 1524 "in6_unlink_ifa: autoconf'ed address " 1525 "%s has no prefix\n", ip6_sprintf(ip6buf, IA6_IN6(ia)))); 1526 } 1527 1528 nd6_rem_ifa_lle(ia, remove_lle); 1529 1530 /* 1531 * Also, if the address being removed is autoconf'ed, call 1532 * pfxlist_onlink_check() since the release might affect the status of 1533 * other (detached) addresses. 1534 */ 1535 if ((ia->ia6_flags & IN6_IFF_AUTOCONF)) { 1536 pfxlist_onlink_check(); 1537 } 1538 ifa_free(&ia->ia_ifa); /* in6_ifaddrhead */ 1539 } 1540 1541 /* 1542 * Notifies other subsystems about address change/arrival: 1543 * 1) Notifies device handler on the first IPv6 address assignment 1544 * 2) Handle routing table changes for P2P links and route 1545 * 3) Handle routing table changes for address host route 1546 */ 1547 static int 1548 in6_notify_ifa(struct ifnet *ifp, struct in6_ifaddr *ia, 1549 struct in6_aliasreq *ifra, int hostIsNew) 1550 { 1551 int error = 0, ifacount = 0; 1552 struct ifaddr *ifa; 1553 struct sockaddr_in6 *pdst; 1554 char ip6buf[INET6_ADDRSTRLEN]; 1555 1556 /* 1557 * Give the interface a chance to initialize 1558 * if this is its first address, 1559 */ 1560 if (hostIsNew != 0) { 1561 struct epoch_tracker et; 1562 1563 NET_EPOCH_ENTER(et); 1564 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1565 if (ifa->ifa_addr->sa_family != AF_INET6) 1566 continue; 1567 ifacount++; 1568 } 1569 NET_EPOCH_EXIT(et); 1570 } 1571 1572 if (ifacount <= 1 && ifp->if_ioctl) { 1573 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 1574 if (error) 1575 goto done; 1576 } 1577 1578 /* 1579 * If a new destination address is specified, scrub the old one and 1580 * install the new destination. Note that the interface must be 1581 * p2p or loopback. 1582 */ 1583 pdst = &ifra->ifra_dstaddr; 1584 if (pdst->sin6_family == AF_INET6 && 1585 !IN6_ARE_ADDR_EQUAL(&pdst->sin6_addr, &ia->ia_dstaddr.sin6_addr)) { 1586 if ((ia->ia_flags & IFA_ROUTE) != 0 && 1587 (in6_handle_dstaddr_rtrequest(RTM_DELETE, ia) != 0)) { 1588 nd6log((LOG_ERR, "in6_update_ifa_internal: failed to " 1589 "remove a route to the old destination: %s\n", 1590 ip6_sprintf(ip6buf, &ia->ia_addr.sin6_addr))); 1591 /* proceed anyway... */ 1592 } else 1593 ia->ia_flags &= ~IFA_ROUTE; 1594 ia->ia_dstaddr = *pdst; 1595 } 1596 1597 /* 1598 * If a new destination address is specified for a point-to-point 1599 * interface, install a route to the destination as an interface 1600 * direct route. 1601 * XXX: the logic below rejects assigning multiple addresses on a p2p 1602 * interface that share the same destination. 1603 */ 1604 if (!(ia->ia_flags & IFA_ROUTE) && ifa_is_p2p(ia)) { 1605 error = in6_handle_dstaddr_rtrequest(RTM_ADD, ia); 1606 if (error) 1607 goto done; 1608 ia->ia_flags |= IFA_ROUTE; 1609 } 1610 1611 /* 1612 * add a loopback route to self if not exists 1613 */ 1614 if (!(ia->ia_flags & IFA_RTSELF) && V_nd6_useloopback) { 1615 error = ifa_add_loopback_route((struct ifaddr *)ia, 1616 (struct sockaddr *)&ia->ia_addr); 1617 if (error == 0) 1618 ia->ia_flags |= IFA_RTSELF; 1619 } 1620 done: 1621 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 1622 "Invoking IPv6 network device address event may sleep"); 1623 1624 ifa_ref(&ia->ia_ifa); 1625 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa, 1626 IFADDR_EVENT_ADD); 1627 ifa_free(&ia->ia_ifa); 1628 1629 return (error); 1630 } 1631 1632 /* 1633 * Find an IPv6 interface link-local address specific to an interface. 1634 * ifaddr is returned referenced. 1635 */ 1636 struct in6_ifaddr * 1637 in6ifa_ifpforlinklocal(struct ifnet *ifp, int ignoreflags) 1638 { 1639 struct ifaddr *ifa; 1640 1641 NET_EPOCH_ASSERT(); 1642 1643 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1644 if (ifa->ifa_addr->sa_family != AF_INET6) 1645 continue; 1646 if (IN6_IS_ADDR_LINKLOCAL(IFA_IN6(ifa))) { 1647 if ((((struct in6_ifaddr *)ifa)->ia6_flags & 1648 ignoreflags) != 0) 1649 continue; 1650 ifa_ref(ifa); 1651 break; 1652 } 1653 } 1654 1655 return ((struct in6_ifaddr *)ifa); 1656 } 1657 1658 /* 1659 * find the interface address corresponding to a given IPv6 address. 1660 * ifaddr is returned referenced if @referenced flag is set. 1661 */ 1662 struct in6_ifaddr * 1663 in6ifa_ifwithaddr(const struct in6_addr *addr, uint32_t zoneid, bool referenced) 1664 { 1665 struct rm_priotracker in6_ifa_tracker; 1666 struct in6_ifaddr *ia; 1667 1668 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 1669 CK_LIST_FOREACH(ia, IN6ADDR_HASH(addr), ia6_hash) { 1670 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), addr)) { 1671 if (zoneid != 0 && 1672 zoneid != ia->ia_addr.sin6_scope_id) 1673 continue; 1674 if (referenced) 1675 ifa_ref(&ia->ia_ifa); 1676 break; 1677 } 1678 } 1679 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1680 return (ia); 1681 } 1682 1683 /* 1684 * find the internet address corresponding to a given interface and address. 1685 * ifaddr is returned referenced. 1686 */ 1687 struct in6_ifaddr * 1688 in6ifa_ifpwithaddr(struct ifnet *ifp, const struct in6_addr *addr) 1689 { 1690 struct epoch_tracker et; 1691 struct ifaddr *ifa; 1692 1693 NET_EPOCH_ENTER(et); 1694 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1695 if (ifa->ifa_addr->sa_family != AF_INET6) 1696 continue; 1697 if (IN6_ARE_ADDR_EQUAL(addr, IFA_IN6(ifa))) { 1698 ifa_ref(ifa); 1699 break; 1700 } 1701 } 1702 NET_EPOCH_EXIT(et); 1703 1704 return ((struct in6_ifaddr *)ifa); 1705 } 1706 1707 /* 1708 * Find a link-local scoped address on ifp and return it if any. 1709 */ 1710 struct in6_ifaddr * 1711 in6ifa_llaonifp(struct ifnet *ifp) 1712 { 1713 struct epoch_tracker et; 1714 struct sockaddr_in6 *sin6; 1715 struct ifaddr *ifa; 1716 1717 if (ifp->if_inet6->nd_flags & ND6_IFF_IFDISABLED) 1718 return (NULL); 1719 NET_EPOCH_ENTER(et); 1720 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1721 if (ifa->ifa_addr->sa_family != AF_INET6) 1722 continue; 1723 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 1724 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr) || 1725 IN6_IS_ADDR_MC_INTFACELOCAL(&sin6->sin6_addr) || 1726 IN6_IS_ADDR_MC_NODELOCAL(&sin6->sin6_addr)) 1727 break; 1728 } 1729 NET_EPOCH_EXIT(et); 1730 1731 return ((struct in6_ifaddr *)ifa); 1732 } 1733 1734 /* 1735 * Convert IP6 address to printable (loggable) representation. Caller 1736 * has to make sure that ip6buf is at least INET6_ADDRSTRLEN long. 1737 */ 1738 static char digits[] = "0123456789abcdef"; 1739 char * 1740 ip6_sprintf(char *ip6buf, const struct in6_addr *addr) 1741 { 1742 int i, cnt = 0, maxcnt = 0, idx = 0, index = 0; 1743 char *cp; 1744 const u_int16_t *a = (const u_int16_t *)addr; 1745 const u_int8_t *d; 1746 int dcolon = 0, zero = 0; 1747 1748 cp = ip6buf; 1749 1750 for (i = 0; i < 8; i++) { 1751 if (*(a + i) == 0) { 1752 cnt++; 1753 if (cnt == 1) 1754 idx = i; 1755 } 1756 else if (maxcnt < cnt) { 1757 maxcnt = cnt; 1758 index = idx; 1759 cnt = 0; 1760 } 1761 } 1762 if (maxcnt < cnt) { 1763 maxcnt = cnt; 1764 index = idx; 1765 } 1766 1767 for (i = 0; i < 8; i++) { 1768 if (dcolon == 1) { 1769 if (*a == 0) { 1770 if (i == 7) 1771 *cp++ = ':'; 1772 a++; 1773 continue; 1774 } else 1775 dcolon = 2; 1776 } 1777 if (*a == 0) { 1778 if (dcolon == 0 && *(a + 1) == 0 && i == index) { 1779 if (i == 0) 1780 *cp++ = ':'; 1781 *cp++ = ':'; 1782 dcolon = 1; 1783 } else { 1784 *cp++ = '0'; 1785 *cp++ = ':'; 1786 } 1787 a++; 1788 continue; 1789 } 1790 d = (const u_char *)a; 1791 /* Try to eliminate leading zeros in printout like in :0001. */ 1792 zero = 1; 1793 *cp = digits[*d >> 4]; 1794 if (*cp != '0') { 1795 zero = 0; 1796 cp++; 1797 } 1798 *cp = digits[*d++ & 0xf]; 1799 if (zero == 0 || (*cp != '0')) { 1800 zero = 0; 1801 cp++; 1802 } 1803 *cp = digits[*d >> 4]; 1804 if (zero == 0 || (*cp != '0')) { 1805 zero = 0; 1806 cp++; 1807 } 1808 *cp++ = digits[*d & 0xf]; 1809 *cp++ = ':'; 1810 a++; 1811 } 1812 *--cp = '\0'; 1813 return (ip6buf); 1814 } 1815 1816 int 1817 in6_localaddr(struct in6_addr *in6) 1818 { 1819 struct rm_priotracker in6_ifa_tracker; 1820 struct in6_ifaddr *ia; 1821 1822 if (IN6_IS_ADDR_LOOPBACK(in6) || IN6_IS_ADDR_LINKLOCAL(in6)) 1823 return 1; 1824 1825 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 1826 CK_STAILQ_FOREACH(ia, &V_in6_ifaddrhead, ia_link) { 1827 if (IN6_ARE_MASKED_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr, 1828 &ia->ia_prefixmask.sin6_addr)) { 1829 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1830 return 1; 1831 } 1832 } 1833 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1834 1835 return (0); 1836 } 1837 1838 /* 1839 * Return 1 if an internet address is for the local host and configured 1840 * on one of its interfaces. 1841 */ 1842 int 1843 in6_localip(struct in6_addr *in6) 1844 { 1845 struct rm_priotracker in6_ifa_tracker; 1846 struct in6_ifaddr *ia; 1847 1848 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 1849 CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) { 1850 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr)) { 1851 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1852 return (1); 1853 } 1854 } 1855 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1856 return (0); 1857 } 1858 1859 /* 1860 * Like in6_localip(), but FIB-aware and carp(4)-aware. 1861 */ 1862 bool 1863 in6_localip_fib(struct in6_addr *in6, uint16_t fib) 1864 { 1865 struct rm_priotracker in6_ifa_tracker; 1866 struct in6_ifaddr *ia; 1867 1868 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 1869 CK_LIST_FOREACH(ia, IN6ADDR_HASH(in6), ia6_hash) { 1870 if (IN6_ARE_ADDR_EQUAL(in6, &ia->ia_addr.sin6_addr) && 1871 (ia->ia_ifa.ifa_carp == NULL || 1872 carp_master_p(&ia->ia_ifa)) && 1873 ia->ia_ifa.ifa_ifp->if_fib == fib) { 1874 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1875 return (true); 1876 } 1877 } 1878 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1879 return (false); 1880 } 1881 1882 /* 1883 * Return 1 if an internet address is configured on an interface. 1884 */ 1885 int 1886 in6_ifhasaddr(struct ifnet *ifp, struct in6_addr *addr) 1887 { 1888 struct in6_addr in6; 1889 struct ifaddr *ifa; 1890 struct in6_ifaddr *ia6; 1891 1892 NET_EPOCH_ASSERT(); 1893 1894 in6 = *addr; 1895 if (in6_clearscope(&in6)) 1896 return (0); 1897 in6_setscope(&in6, ifp, NULL); 1898 1899 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1900 if (ifa->ifa_addr->sa_family != AF_INET6) 1901 continue; 1902 ia6 = (struct in6_ifaddr *)ifa; 1903 if (IN6_ARE_ADDR_EQUAL(&ia6->ia_addr.sin6_addr, &in6)) 1904 return (1); 1905 } 1906 1907 return (0); 1908 } 1909 1910 int 1911 in6_is_addr_deprecated(struct sockaddr_in6 *sa6) 1912 { 1913 struct rm_priotracker in6_ifa_tracker; 1914 struct in6_ifaddr *ia; 1915 1916 IN6_IFADDR_RLOCK(&in6_ifa_tracker); 1917 CK_LIST_FOREACH(ia, IN6ADDR_HASH(&sa6->sin6_addr), ia6_hash) { 1918 if (IN6_ARE_ADDR_EQUAL(IA6_IN6(ia), &sa6->sin6_addr)) { 1919 if (ia->ia6_flags & IN6_IFF_DEPRECATED) { 1920 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1921 return (1); /* true */ 1922 } 1923 break; 1924 } 1925 } 1926 IN6_IFADDR_RUNLOCK(&in6_ifa_tracker); 1927 1928 return (0); /* false */ 1929 } 1930 1931 /* 1932 * return length of part which dst and src are equal 1933 * hard coding... 1934 */ 1935 int 1936 in6_matchlen(struct in6_addr *src, struct in6_addr *dst) 1937 { 1938 int match = 0; 1939 u_char *s = (u_char *)src, *d = (u_char *)dst; 1940 u_char *lim = s + 16, r; 1941 1942 while (s < lim) 1943 if ((r = (*d++ ^ *s++)) != 0) { 1944 while (r < 128) { 1945 match++; 1946 r <<= 1; 1947 } 1948 break; 1949 } else 1950 match += 8; 1951 return match; 1952 } 1953 1954 /* XXX: to be scope conscious */ 1955 int 1956 in6_are_prefix_equal(struct in6_addr *p1, struct in6_addr *p2, int len) 1957 { 1958 int bytelen, bitlen; 1959 1960 /* sanity check */ 1961 if (0 > len || len > 128) { 1962 log(LOG_ERR, "in6_are_prefix_equal: invalid prefix length(%d)\n", 1963 len); 1964 return (0); 1965 } 1966 1967 bytelen = len / 8; 1968 bitlen = len % 8; 1969 1970 if (bcmp(&p1->s6_addr, &p2->s6_addr, bytelen)) 1971 return (0); 1972 if (bitlen != 0 && 1973 p1->s6_addr[bytelen] >> (8 - bitlen) != 1974 p2->s6_addr[bytelen] >> (8 - bitlen)) 1975 return (0); 1976 1977 return (1); 1978 } 1979 1980 void 1981 in6_prefixlen2mask(struct in6_addr *maskp, int len) 1982 { 1983 u_char maskarray[8] = {0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 1984 int bytelen, bitlen, i; 1985 1986 /* sanity check */ 1987 if (0 > len || len > 128) { 1988 log(LOG_ERR, "in6_prefixlen2mask: invalid prefix length(%d)\n", 1989 len); 1990 return; 1991 } 1992 1993 bzero(maskp, sizeof(*maskp)); 1994 bytelen = len / 8; 1995 bitlen = len % 8; 1996 for (i = 0; i < bytelen; i++) 1997 maskp->s6_addr[i] = 0xff; 1998 if (bitlen) 1999 maskp->s6_addr[bytelen] = maskarray[bitlen - 1]; 2000 } 2001 2002 /* 2003 * return the best address out of the same scope. if no address was 2004 * found, return the first valid address from designated IF. 2005 */ 2006 struct in6_ifaddr * 2007 in6_ifawithifp(struct ifnet *ifp, struct in6_addr *dst) 2008 { 2009 int dst_scope = in6_addrscope(dst), blen = -1, tlen; 2010 struct ifaddr *ifa; 2011 struct in6_ifaddr *besta = NULL; 2012 struct in6_ifaddr *dep[2]; /* last-resort: deprecated */ 2013 2014 NET_EPOCH_ASSERT(); 2015 2016 dep[0] = dep[1] = NULL; 2017 2018 /* 2019 * We first look for addresses in the same scope. 2020 * If there is one, return it. 2021 * If two or more, return one which matches the dst longest. 2022 * If none, return one of global addresses assigned other ifs. 2023 */ 2024 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2025 if (ifa->ifa_addr->sa_family != AF_INET6) 2026 continue; 2027 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2028 continue; /* XXX: is there any case to allow anycast? */ 2029 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2030 continue; /* don't use this interface */ 2031 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2032 continue; 2033 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2034 if (V_ip6_use_deprecated) 2035 dep[0] = (struct in6_ifaddr *)ifa; 2036 continue; 2037 } 2038 2039 if (dst_scope == in6_addrscope(IFA_IN6(ifa))) { 2040 /* 2041 * call in6_matchlen() as few as possible 2042 */ 2043 if (besta) { 2044 if (blen == -1) 2045 blen = in6_matchlen(&besta->ia_addr.sin6_addr, dst); 2046 tlen = in6_matchlen(IFA_IN6(ifa), dst); 2047 if (tlen > blen) { 2048 blen = tlen; 2049 besta = (struct in6_ifaddr *)ifa; 2050 } 2051 } else 2052 besta = (struct in6_ifaddr *)ifa; 2053 } 2054 } 2055 if (besta) 2056 return (besta); 2057 2058 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2059 if (ifa->ifa_addr->sa_family != AF_INET6) 2060 continue; 2061 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_ANYCAST) 2062 continue; /* XXX: is there any case to allow anycast? */ 2063 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_NOTREADY) 2064 continue; /* don't use this interface */ 2065 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DETACHED) 2066 continue; 2067 if (((struct in6_ifaddr *)ifa)->ia6_flags & IN6_IFF_DEPRECATED) { 2068 if (V_ip6_use_deprecated) 2069 dep[1] = (struct in6_ifaddr *)ifa; 2070 continue; 2071 } 2072 2073 return (struct in6_ifaddr *)ifa; 2074 } 2075 2076 /* use the last-resort values, that are, deprecated addresses */ 2077 if (dep[0]) 2078 return dep[0]; 2079 if (dep[1]) 2080 return dep[1]; 2081 2082 return NULL; 2083 } 2084 2085 /* 2086 * perform DAD when interface becomes IFF_UP. 2087 */ 2088 void 2089 in6_if_up(struct ifnet *ifp) 2090 { 2091 struct epoch_tracker et; 2092 struct ifaddr *ifa; 2093 struct in6_ifaddr *ia; 2094 2095 NET_EPOCH_ENTER(et); 2096 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 2097 if (ifa->ifa_addr->sa_family != AF_INET6) 2098 continue; 2099 ia = (struct in6_ifaddr *)ifa; 2100 if (ia->ia6_flags & IN6_IFF_TENTATIVE) { 2101 /* 2102 * The TENTATIVE flag was likely set by hand 2103 * beforehand, implicitly indicating the need for DAD. 2104 * We may be able to skip the random delay in this 2105 * case, but we impose delays just in case. 2106 */ 2107 nd6_dad_start(ifa, 2108 arc4random() % (MAX_RTR_SOLICITATION_DELAY * hz)); 2109 } 2110 } 2111 NET_EPOCH_EXIT(et); 2112 2113 /* 2114 * special cases, like 6to4, are handled in in6_ifattach 2115 */ 2116 in6_ifattach(ifp, NULL); 2117 } 2118 2119 static void 2120 in6_ifevent(void *arg __unused, struct ifnet *ifp, int event) 2121 { 2122 if (event == IFNET_EVENT_UP) 2123 in6_if_up(ifp); 2124 } 2125 2126 static void 2127 in6_init(void *arg __unused) 2128 { 2129 EVENTHANDLER_REGISTER(ifnet_event, in6_ifevent, NULL, EVENTHANDLER_PRI_ANY); 2130 } 2131 SYSINIT(in6_init, SI_SUB_PROTO_DOMAIN, SI_ORDER_THIRD, in6_init, NULL); 2132 2133 int 2134 in6if_do_dad(struct ifnet *ifp) 2135 { 2136 2137 if ((ifp->if_flags & IFF_LOOPBACK) != 0) 2138 return (0); 2139 if ((ifp->if_flags & IFF_MULTICAST) == 0) 2140 return (0); 2141 if ((ifp->if_inet6->nd_flags & 2142 (ND6_IFF_IFDISABLED | ND6_IFF_NO_DAD)) != 0) 2143 return (0); 2144 return (1); 2145 } 2146 2147 /* 2148 * Provide the length of interface identifiers to be used for the link attached 2149 * to the given interface. The length should be defined in "IPv6 over 2150 * xxx-link" document. Note that address architecture might also define 2151 * the length for a particular set of address prefixes, regardless of the 2152 * link type. As clarified in rfc2462bis, those two definitions should be 2153 * consistent, and those really are as of August 2004. 2154 */ 2155 int 2156 in6_if2idlen(struct ifnet *ifp) 2157 { 2158 switch (ifp->if_type) { 2159 case IFT_ETHER: /* RFC2464 */ 2160 case IFT_PROPVIRTUAL: /* XXX: no RFC. treat it as ether */ 2161 case IFT_L2VLAN: /* ditto */ 2162 case IFT_BRIDGE: /* bridge(4) only does Ethernet-like links */ 2163 case IFT_INFINIBAND: 2164 return (64); 2165 case IFT_PPP: /* RFC2472 */ 2166 return (64); 2167 case IFT_FRELAY: /* RFC2590 */ 2168 return (64); 2169 case IFT_IEEE1394: /* RFC3146 */ 2170 return (64); 2171 case IFT_GIF: 2172 return (64); /* draft-ietf-v6ops-mech-v2-07 */ 2173 case IFT_LOOP: 2174 return (64); /* XXX: is this really correct? */ 2175 default: 2176 /* 2177 * Unknown link type: 2178 * It might be controversial to use the today's common constant 2179 * of 64 for these cases unconditionally. For full compliance, 2180 * we should return an error in this case. On the other hand, 2181 * if we simply miss the standard for the link type or a new 2182 * standard is defined for a new link type, the IFID length 2183 * is very likely to be the common constant. As a compromise, 2184 * we always use the constant, but make an explicit notice 2185 * indicating the "unknown" case. 2186 */ 2187 printf("in6_if2idlen: unknown link type (%d)\n", ifp->if_type); 2188 return (64); 2189 } 2190 } 2191 2192 struct in6_llentry { 2193 struct llentry base; 2194 }; 2195 2196 #define IN6_LLTBL_DEFAULT_HSIZE 32 2197 #define IN6_LLTBL_HASH(k, h) \ 2198 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1)) 2199 2200 /* 2201 * Do actual deallocation of @lle. 2202 */ 2203 static void 2204 in6_lltable_destroy_lle_unlocked(epoch_context_t ctx) 2205 { 2206 struct llentry *lle; 2207 2208 lle = __containerof(ctx, struct llentry, lle_epoch_ctx); 2209 LLE_LOCK_DESTROY(lle); 2210 LLE_REQ_DESTROY(lle); 2211 free(lle, M_LLTABLE); 2212 } 2213 2214 /* 2215 * Called by LLE_FREE_LOCKED when number of references 2216 * drops to zero. 2217 */ 2218 static void 2219 in6_lltable_destroy_lle(struct llentry *lle) 2220 { 2221 2222 LLE_WUNLOCK(lle); 2223 NET_EPOCH_CALL(in6_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx); 2224 } 2225 2226 static struct llentry * 2227 in6_lltable_new(const struct in6_addr *addr6, u_int flags) 2228 { 2229 struct in6_llentry *lle; 2230 2231 lle = malloc(sizeof(struct in6_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 2232 if (lle == NULL) /* NB: caller generates msg */ 2233 return NULL; 2234 2235 lle->base.r_l3addr.addr6 = *addr6; 2236 lle->base.lle_refcnt = 1; 2237 lle->base.lle_free = in6_lltable_destroy_lle; 2238 LLE_LOCK_INIT(&lle->base); 2239 LLE_REQ_INIT(&lle->base); 2240 callout_init(&lle->base.lle_timer, 1); 2241 2242 return (&lle->base); 2243 } 2244 2245 static int 2246 in6_lltable_match_prefix(const struct sockaddr *saddr, 2247 const struct sockaddr *smask, u_int flags, struct llentry *lle) 2248 { 2249 const struct in6_addr *addr, *mask, *lle_addr; 2250 2251 addr = &((const struct sockaddr_in6 *)saddr)->sin6_addr; 2252 mask = &((const struct sockaddr_in6 *)smask)->sin6_addr; 2253 lle_addr = &lle->r_l3addr.addr6; 2254 2255 if (IN6_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0) 2256 return (0); 2257 2258 if (lle->la_flags & LLE_IFADDR) { 2259 /* 2260 * Delete LLE_IFADDR records IFF address & flag matches. 2261 * Note that addr is the interface address within prefix 2262 * being matched. 2263 */ 2264 if (IN6_ARE_ADDR_EQUAL(addr, lle_addr) && 2265 (flags & LLE_STATIC) != 0) 2266 return (1); 2267 return (0); 2268 } 2269 2270 /* flags & LLE_STATIC means deleting both dynamic and static entries */ 2271 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)) 2272 return (1); 2273 2274 return (0); 2275 } 2276 2277 static void 2278 in6_lltable_free_entry(struct lltable *llt, struct llentry *lle) 2279 { 2280 2281 LLE_WLOCK_ASSERT(lle); 2282 KASSERT(llt != NULL, ("lltable is NULL")); 2283 2284 /* Unlink entry from table */ 2285 if ((lle->la_flags & LLE_LINKED) != 0) { 2286 LLTABLE_LOCK_ASSERT(llt); 2287 lltable_unlink_entry(llt, lle); 2288 } 2289 2290 llentry_free(lle); 2291 } 2292 2293 static int 2294 in6_lltable_rtcheck(struct ifnet *ifp, 2295 u_int flags, 2296 const struct sockaddr *l3addr) 2297 { 2298 const struct sockaddr_in6 *sin6; 2299 struct nhop_object *nh; 2300 struct in6_addr dst; 2301 uint32_t scopeid; 2302 char ip6buf[INET6_ADDRSTRLEN]; 2303 int fibnum; 2304 2305 NET_EPOCH_ASSERT(); 2306 KASSERT(l3addr->sa_family == AF_INET6, 2307 ("sin_family %d", l3addr->sa_family)); 2308 2309 sin6 = (const struct sockaddr_in6 *)l3addr; 2310 in6_splitscope(&sin6->sin6_addr, &dst, &scopeid); 2311 fibnum = V_rt_add_addr_allfibs ? RT_DEFAULT_FIB : ifp->if_fib; 2312 nh = fib6_lookup(fibnum, &dst, scopeid, NHR_NONE, 0); 2313 if (nh && ((nh->nh_flags & NHF_GATEWAY) || nh->nh_ifp != ifp)) { 2314 struct ifaddr *ifa; 2315 /* 2316 * Create an ND6 cache for an IPv6 neighbor 2317 * that is not covered by our own prefix. 2318 */ 2319 ifa = ifaof_ifpforaddr(l3addr, ifp); 2320 if (ifa != NULL) { 2321 return 0; 2322 } 2323 log(LOG_INFO, "IPv6 address: \"%s\" is not on the network\n", 2324 ip6_sprintf(ip6buf, &sin6->sin6_addr)); 2325 return EINVAL; 2326 } 2327 return 0; 2328 } 2329 2330 static inline uint32_t 2331 in6_lltable_hash_dst(const struct in6_addr *dst, uint32_t hsize) 2332 { 2333 2334 return (IN6_LLTBL_HASH(dst->s6_addr32[3], hsize)); 2335 } 2336 2337 static uint32_t 2338 in6_lltable_hash(const struct llentry *lle, uint32_t hsize) 2339 { 2340 2341 return (in6_lltable_hash_dst(&lle->r_l3addr.addr6, hsize)); 2342 } 2343 2344 static void 2345 in6_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) 2346 { 2347 struct sockaddr_in6 *sin6; 2348 2349 sin6 = (struct sockaddr_in6 *)sa; 2350 bzero(sin6, sizeof(*sin6)); 2351 sin6->sin6_family = AF_INET6; 2352 sin6->sin6_len = sizeof(*sin6); 2353 sin6->sin6_addr = lle->r_l3addr.addr6; 2354 } 2355 2356 static inline struct llentry * 2357 in6_lltable_find_dst(struct lltable *llt, const struct in6_addr *dst) 2358 { 2359 struct llentry *lle; 2360 struct llentries *lleh; 2361 u_int hashidx; 2362 2363 hashidx = in6_lltable_hash_dst(dst, llt->llt_hsize); 2364 lleh = &llt->lle_head[hashidx]; 2365 CK_LIST_FOREACH(lle, lleh, lle_next) { 2366 if (lle->la_flags & LLE_DELETED) 2367 continue; 2368 if (IN6_ARE_ADDR_EQUAL(&lle->r_l3addr.addr6, dst)) 2369 break; 2370 } 2371 2372 return (lle); 2373 } 2374 2375 static void 2376 in6_lltable_delete_entry(struct lltable *llt, struct llentry *lle) 2377 { 2378 2379 lle->la_flags |= LLE_DELETED; 2380 2381 /* Leave the solicited multicast group. */ 2382 if ((lle->la_flags & LLE_PUB) != 0) 2383 in6_leave_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6); 2384 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 2385 #ifdef DIAGNOSTIC 2386 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 2387 #endif 2388 llentry_free(lle); 2389 } 2390 2391 static struct llentry * 2392 in6_lltable_alloc(struct lltable *llt, u_int flags, 2393 const struct sockaddr *l3addr) 2394 { 2395 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2396 struct ifnet *ifp = llt->llt_ifp; 2397 struct llentry *lle; 2398 char linkhdr[LLE_MAX_LINKHDR]; 2399 size_t linkhdrsize; 2400 int lladdr_off; 2401 2402 KASSERT(l3addr->sa_family == AF_INET6, 2403 ("sin_family %d", l3addr->sa_family)); 2404 2405 /* 2406 * A route that covers the given address must have 2407 * been installed 1st because we are doing a resolution, 2408 * verify this. 2409 */ 2410 if (!(flags & LLE_IFADDR) && 2411 in6_lltable_rtcheck(ifp, flags, l3addr) != 0) 2412 return (NULL); 2413 2414 lle = in6_lltable_new(&sin6->sin6_addr, flags); 2415 if (lle == NULL) { 2416 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 2417 return (NULL); 2418 } 2419 lle->la_flags = flags; 2420 if ((flags & LLE_IFADDR) == LLE_IFADDR) { 2421 linkhdrsize = LLE_MAX_LINKHDR; 2422 if (lltable_calc_llheader(ifp, AF_INET6, IF_LLADDR(ifp), 2423 linkhdr, &linkhdrsize, &lladdr_off) != 0) { 2424 in6_lltable_free_entry(llt, lle); 2425 return (NULL); 2426 } 2427 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, 2428 lladdr_off); 2429 lle->la_flags |= LLE_STATIC; 2430 } 2431 2432 if ((lle->la_flags & LLE_STATIC) != 0) 2433 lle->ln_state = ND6_LLINFO_REACHABLE; 2434 2435 return (lle); 2436 } 2437 2438 static struct llentry * 2439 in6_lltable_lookup(struct lltable *llt, u_int flags, 2440 const struct sockaddr *l3addr) 2441 { 2442 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)l3addr; 2443 int family = flags >> 16; 2444 struct llentry *lle; 2445 2446 LLTABLE_RLOCK_ASSERT(llt); 2447 KASSERT(l3addr->sa_family == AF_INET6, 2448 ("sin_family %d", l3addr->sa_family)); 2449 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) != 2450 (LLE_UNLOCKED | LLE_EXCLUSIVE), 2451 ("wrong lle request flags: %#x", flags)); 2452 2453 lle = in6_lltable_find_dst(llt, &sin6->sin6_addr); 2454 2455 if (__predict_false(family != AF_INET6)) 2456 lle = llentry_lookup_family(lle, family); 2457 2458 if (lle == NULL) 2459 return (NULL); 2460 2461 if (flags & LLE_UNLOCKED) 2462 return (lle); 2463 2464 if (flags & LLE_EXCLUSIVE) 2465 LLE_WLOCK(lle); 2466 else 2467 LLE_RLOCK(lle); 2468 2469 /* 2470 * If the lltable lock is not held, the LLE may have been unlinked while 2471 * we were blocked on the LLE lock. Check for this case. 2472 */ 2473 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) { 2474 if (flags & LLE_EXCLUSIVE) 2475 LLE_WUNLOCK(lle); 2476 else 2477 LLE_RUNLOCK(lle); 2478 return (NULL); 2479 } 2480 return (lle); 2481 } 2482 2483 static int 2484 in6_lltable_dump_entry(struct lltable *llt, struct llentry *lle, 2485 struct sysctl_req *wr) 2486 { 2487 struct ifnet *ifp = llt->llt_ifp; 2488 /* XXX stack use */ 2489 struct { 2490 struct rt_msghdr rtm; 2491 struct sockaddr_in6 sin6; 2492 /* 2493 * ndp.c assumes that sdl is word aligned 2494 */ 2495 #ifdef __LP64__ 2496 uint32_t pad; 2497 #endif 2498 struct sockaddr_dl sdl; 2499 } ndpc; 2500 struct sockaddr_dl *sdl; 2501 int error; 2502 2503 bzero(&ndpc, sizeof(ndpc)); 2504 /* skip deleted entries */ 2505 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 2506 return (0); 2507 /* Skip if jailed and not a valid IP of the prison. */ 2508 lltable_fill_sa_entry(lle, (struct sockaddr *)&ndpc.sin6); 2509 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&ndpc.sin6) != 0) 2510 return (0); 2511 /* 2512 * produce a msg made of: 2513 * struct rt_msghdr; 2514 * struct sockaddr_in6 (IPv6) 2515 * struct sockaddr_dl; 2516 */ 2517 ndpc.rtm.rtm_msglen = sizeof(ndpc); 2518 ndpc.rtm.rtm_version = RTM_VERSION; 2519 ndpc.rtm.rtm_type = RTM_GET; 2520 ndpc.rtm.rtm_flags = RTF_UP; 2521 ndpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 2522 sa6_recoverscope(&ndpc.sin6); 2523 2524 /* publish */ 2525 if (lle->la_flags & LLE_PUB) 2526 ndpc.rtm.rtm_flags |= RTF_ANNOUNCE; 2527 2528 sdl = &ndpc.sdl; 2529 sdl->sdl_family = AF_LINK; 2530 sdl->sdl_len = sizeof(*sdl); 2531 sdl->sdl_index = ifp->if_index; 2532 sdl->sdl_type = ifp->if_type; 2533 if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 2534 sdl->sdl_alen = ifp->if_addrlen; 2535 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 2536 } else { 2537 sdl->sdl_alen = 0; 2538 bzero(LLADDR(sdl), ifp->if_addrlen); 2539 } 2540 if (lle->la_expire != 0) 2541 ndpc.rtm.rtm_rmx.rmx_expire = lle->la_expire + 2542 lle->lle_remtime / hz + time_second - time_uptime; 2543 ndpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 2544 if (lle->la_flags & LLE_STATIC) 2545 ndpc.rtm.rtm_flags |= RTF_STATIC; 2546 if (lle->la_flags & LLE_IFADDR) 2547 ndpc.rtm.rtm_flags |= RTF_PINNED; 2548 if (lle->ln_router != 0) 2549 ndpc.rtm.rtm_flags |= RTF_GATEWAY; 2550 ndpc.rtm.rtm_rmx.rmx_pksent = lle->la_asked; 2551 /* Store state in rmx_weight value */ 2552 ndpc.rtm.rtm_rmx.rmx_state = lle->ln_state; 2553 ndpc.rtm.rtm_index = ifp->if_index; 2554 error = SYSCTL_OUT(wr, &ndpc, sizeof(ndpc)); 2555 2556 return (error); 2557 } 2558 2559 static void 2560 in6_lltable_post_resolved(struct lltable *llt, struct llentry *lle) 2561 { 2562 /* Join the solicited multicast group for dst. */ 2563 if ((lle->la_flags & LLE_PUB) == LLE_PUB) 2564 in6_join_proxy_ndp_mc(llt->llt_ifp, &lle->r_l3addr.addr6); 2565 } 2566 2567 static struct lltable * 2568 in6_lltattach(struct ifnet *ifp) 2569 { 2570 struct lltable *llt; 2571 2572 llt = lltable_allocate_htbl(IN6_LLTBL_DEFAULT_HSIZE); 2573 llt->llt_af = AF_INET6; 2574 llt->llt_ifp = ifp; 2575 2576 llt->llt_lookup = in6_lltable_lookup; 2577 llt->llt_alloc_entry = in6_lltable_alloc; 2578 llt->llt_delete_entry = in6_lltable_delete_entry; 2579 llt->llt_dump_entry = in6_lltable_dump_entry; 2580 llt->llt_hash = in6_lltable_hash; 2581 llt->llt_fill_sa_entry = in6_lltable_fill_sa_entry; 2582 llt->llt_free_entry = in6_lltable_free_entry; 2583 llt->llt_match_prefix = in6_lltable_match_prefix; 2584 llt->llt_mark_used = llentry_mark_used; 2585 llt->llt_post_resolved = in6_lltable_post_resolved; 2586 lltable_link(llt); 2587 2588 return (llt); 2589 } 2590 2591 struct lltable * 2592 in6_lltable_get(struct ifnet *ifp) 2593 { 2594 if (ifp->if_inet6 == NULL) 2595 return (NULL); 2596 2597 return (ifp->if_inet6->lltable); 2598 } 2599 2600 void 2601 in6_ifarrival(void *arg __unused, struct ifnet *ifp) 2602 { 2603 struct in6_ifextra *ext; 2604 2605 /* There are not IPv6-capable interfaces. */ 2606 switch (ifp->if_type) { 2607 case IFT_PFSYNC: 2608 ifp->if_inet6 = NULL; 2609 return; 2610 } 2611 ext = ifp->if_inet6 = malloc(sizeof(*ext), M_IFADDR, M_WAITOK | M_ZERO); 2612 COUNTER_ARRAY_ALLOC(ext->in6_ifstat, 2613 sizeof(struct in6_ifstat) / sizeof(uint64_t), M_WAITOK); 2614 COUNTER_ARRAY_ALLOC(ext->icmp6_ifstat, 2615 sizeof(struct icmp6_ifstat) / sizeof(uint64_t), M_WAITOK); 2616 nd6_ifattach(ifp); 2617 mld_domifattach(ifp); 2618 scope6_ifattach(ifp); 2619 2620 ext->lltable = in6_lltattach(ifp); 2621 } 2622 EVENTHANDLER_DEFINE(ifnet_arrival_event, in6_ifarrival, NULL, 2623 EVENTHANDLER_PRI_ANY); 2624 2625 uint32_t 2626 in6_ifmtu(const struct ifnet *ifp) 2627 { 2628 const uint32_t 2629 linkmtu = ifp->if_inet6->nd_linkmtu, 2630 maxmtu = ifp->if_inet6->nd_maxmtu, 2631 ifmtu = ifp->if_mtu; 2632 2633 if (linkmtu > 0 && linkmtu < ifmtu) 2634 return (linkmtu); 2635 if (maxmtu > 0 && maxmtu < ifmtu) 2636 return (maxmtu); 2637 return (ifmtu); 2638 } 2639 2640 /* 2641 * Convert sockaddr_in6 to sockaddr_in. Original sockaddr_in6 must be 2642 * v4 mapped addr or v4 compat addr 2643 */ 2644 void 2645 in6_sin6_2_sin(struct sockaddr_in *sin, const struct sockaddr_in6 *sin6) 2646 { 2647 2648 bzero(sin, sizeof(*sin)); 2649 sin->sin_len = sizeof(struct sockaddr_in); 2650 sin->sin_family = AF_INET; 2651 sin->sin_port = sin6->sin6_port; 2652 sin->sin_addr.s_addr = sin6->sin6_addr.s6_addr32[3]; 2653 } 2654 2655 /* Convert sockaddr_in to sockaddr_in6 in v4 mapped addr format. */ 2656 void 2657 in6_sin_2_v4mapsin6(const struct sockaddr_in *sin, struct sockaddr_in6 *sin6) 2658 { 2659 bzero(sin6, sizeof(*sin6)); 2660 sin6->sin6_len = sizeof(struct sockaddr_in6); 2661 sin6->sin6_family = AF_INET6; 2662 sin6->sin6_port = sin->sin_port; 2663 sin6->sin6_addr.s6_addr32[0] = 0; 2664 sin6->sin6_addr.s6_addr32[1] = 0; 2665 sin6->sin6_addr.s6_addr32[2] = IPV6_ADDR_INT32_SMP; 2666 sin6->sin6_addr.s6_addr32[3] = sin->sin_addr.s_addr; 2667 } 2668 2669 /* Convert sockaddr_in6 into sockaddr_in. */ 2670 void 2671 in6_sin6_2_sin_in_sock(struct sockaddr *nam) 2672 { 2673 struct sockaddr_in *sin_p; 2674 struct sockaddr_in6 sin6; 2675 2676 /* 2677 * Save original sockaddr_in6 addr and convert it 2678 * to sockaddr_in. 2679 */ 2680 sin6 = *(struct sockaddr_in6 *)nam; 2681 sin_p = (struct sockaddr_in *)nam; 2682 in6_sin6_2_sin(sin_p, &sin6); 2683 } 2684 2685 /* 2686 * Join/leave the solicited multicast groups for proxy NDP entries. 2687 */ 2688 static void 2689 in6_join_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst) 2690 { 2691 struct in6_multi *inm; 2692 struct in6_addr mltaddr; 2693 char ip6buf[INET6_ADDRSTRLEN]; 2694 int error; 2695 2696 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0) 2697 return; /* error logged in in6_solicited_node_maddr. */ 2698 2699 error = in6_joingroup(ifp, &mltaddr, NULL, &inm, 0); 2700 if (error != 0) { 2701 nd6log((LOG_WARNING, 2702 "%s: in6_joingroup failed for %s on %s (errno=%d)\n", 2703 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp), 2704 error)); 2705 } 2706 } 2707 2708 static void 2709 in6_leave_proxy_ndp_mc(struct ifnet *ifp, const struct in6_addr *dst) 2710 { 2711 struct epoch_tracker et; 2712 struct in6_multi *inm; 2713 struct in6_addr mltaddr; 2714 char ip6buf[INET6_ADDRSTRLEN]; 2715 2716 if (in6_solicited_node_maddr(&mltaddr, ifp, dst) != 0) 2717 return; /* error logged in in6_solicited_node_maddr. */ 2718 2719 NET_EPOCH_ENTER(et); 2720 inm = in6m_lookup(ifp, &mltaddr); 2721 NET_EPOCH_EXIT(et); 2722 if (inm != NULL) 2723 in6_leavegroup(inm, NULL); 2724 else 2725 nd6log((LOG_WARNING, "%s: in6m_lookup failed for %s on %s\n", 2726 __func__, ip6_sprintf(ip6buf, &mltaddr), if_name(ifp))); 2727 } 2728 2729 static bool 2730 in6_lle_match_pub(struct lltable *llt, struct llentry *lle, void *farg) 2731 { 2732 return ((lle->la_flags & LLE_PUB) != 0); 2733 } 2734 2735 void 2736 in6_purge_proxy_ndp(struct ifnet *ifp) 2737 { 2738 struct lltable *llt; 2739 bool need_purge; 2740 2741 if (ifp->if_inet6 == NULL) 2742 return; 2743 2744 llt = LLTABLE6(ifp); 2745 LLTABLE_LOCK(llt); 2746 need_purge = ((llt->llt_flags & LLT_ADDEDPROXY) != 0); 2747 LLTABLE_UNLOCK(llt); 2748 2749 /* 2750 * Ever added proxy ndp entries, leave solicited node multicast 2751 * before deleting the llentry. 2752 */ 2753 if (need_purge) 2754 lltable_delete_conditional(llt, in6_lle_match_pub, NULL); 2755 } 2756