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