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