1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1982, 1986, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (C) 2001 WIDE Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 #include "opt_inet.h" 35 36 #define IN_HISTORICAL_NETS /* include class masks */ 37 38 #include <sys/param.h> 39 #include <sys/eventhandler.h> 40 #include <sys/systm.h> 41 #include <sys/sockio.h> 42 #include <sys/malloc.h> 43 #include <sys/priv.h> 44 #include <sys/socket.h> 45 #include <sys/jail.h> 46 #include <sys/kernel.h> 47 #include <sys/lock.h> 48 #include <sys/proc.h> 49 #include <sys/sysctl.h> 50 #include <sys/syslog.h> 51 #include <sys/sx.h> 52 53 #include <net/if.h> 54 #include <net/if_var.h> 55 #include <net/if_arp.h> 56 #include <net/if_dl.h> 57 #include <net/if_llatbl.h> 58 #include <net/if_private.h> 59 #include <net/if_types.h> 60 #include <net/route.h> 61 #include <net/route/nhop.h> 62 #include <net/route/route_ctl.h> 63 #include <net/vnet.h> 64 65 #include <netinet/if_ether.h> 66 #include <netinet/in.h> 67 #include <netinet/in_fib.h> 68 #include <netinet/in_var.h> 69 #include <netinet/in_pcb.h> 70 #include <netinet/ip_var.h> 71 #include <netinet/ip_carp.h> 72 #include <netinet/igmp_var.h> 73 #include <netinet/udp.h> 74 #include <netinet/udp_var.h> 75 76 #ifdef MAC 77 #include <security/mac/mac_framework.h> 78 #endif 79 80 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *); 81 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *); 82 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *); 83 84 static void in_socktrim(struct sockaddr_in *); 85 static void in_purgemaddrs(struct ifnet *); 86 87 static bool ia_need_loopback_route(const struct in_ifaddr *); 88 89 VNET_DEFINE_STATIC(int, nosameprefix); 90 #define V_nosameprefix VNET(nosameprefix) 91 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW, 92 &VNET_NAME(nosameprefix), 0, 93 "Refuse to create same prefixes on different interfaces"); 94 95 VNET_DEFINE_STATIC(bool, broadcast_lowest); 96 #define V_broadcast_lowest VNET(broadcast_lowest) 97 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW, 98 &VNET_NAME(broadcast_lowest), 0, 99 "Treat lowest address on a subnet (host 0) as broadcast"); 100 101 VNET_DEFINE(bool, ip_allow_net240) = false; 102 #define V_ip_allow_net240 VNET(ip_allow_net240) 103 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net240, 104 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net240), 0, 105 "Allow forwarding of and ICMP response to Experimental addresses, aka Class E (240/4)"); 106 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-240 */ 107 108 VNET_DEFINE(bool, ip_allow_net0) = false; 109 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net0, 110 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net0), 0, 111 "Allow forwarding of and ICMP response to addresses in network 0/8"); 112 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-0 */ 113 114 VNET_DEFINE(uint32_t, in_loopback_mask) = IN_LOOPBACK_MASK_DFLT; 115 #define V_in_loopback_mask VNET(in_loopback_mask) 116 static int sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS); 117 SYSCTL_PROC(_net_inet_ip, OID_AUTO, loopback_prefixlen, 118 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, 119 NULL, 0, sysctl_loopback_prefixlen, "I", 120 "Prefix length of address space reserved for loopback"); 121 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-127 */ 122 123 VNET_DECLARE(struct inpcbinfo, ripcbinfo); 124 #define V_ripcbinfo VNET(ripcbinfo) 125 126 static struct sx in_control_sx; 127 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control"); 128 129 /* 130 * Return true if an internet address is for a ``local'' host 131 * (one to which we have a connection). 132 */ 133 bool 134 in_localaddr(struct in_addr in) 135 { 136 u_long i = ntohl(in.s_addr); 137 struct in_ifaddr *ia; 138 139 NET_EPOCH_ASSERT(); 140 141 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 142 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 143 return (true); 144 } 145 146 return (false); 147 } 148 149 /* 150 * Return true if an internet address is for the local host and configured 151 * on one of its interfaces. 152 */ 153 bool 154 in_localip(struct in_addr in) 155 { 156 struct in_ifaddr *ia; 157 158 NET_EPOCH_ASSERT(); 159 160 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) 161 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 162 return (true); 163 164 return (false); 165 } 166 167 /* 168 * Like in_localip(), but FIB-aware and carp(4)-aware. 169 */ 170 bool 171 in_localip_fib(struct in_addr in, uint16_t fib) 172 { 173 struct in_ifaddr *ia; 174 175 NET_EPOCH_ASSERT(); 176 177 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) 178 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr && 179 (ia->ia_ifa.ifa_carp == NULL || 180 carp_master_p(&ia->ia_ifa)) && 181 ia->ia_ifa.ifa_ifp->if_fib == fib) 182 return (true); 183 184 return (false); 185 } 186 187 /* 188 * Return true if an internet address is configured on an interface. 189 */ 190 bool 191 in_ifhasaddr(struct ifnet *ifp, struct in_addr in) 192 { 193 struct ifaddr *ifa; 194 struct in_ifaddr *ia; 195 196 NET_EPOCH_ASSERT(); 197 198 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 199 if (ifa->ifa_addr->sa_family != AF_INET) 200 continue; 201 ia = (struct in_ifaddr *)ifa; 202 if (ia->ia_addr.sin_addr.s_addr == in.s_addr) 203 return (true); 204 } 205 206 return (false); 207 } 208 209 /* 210 * Return a reference to the interface address which is different to 211 * the supplied one but with same IP address value. 212 */ 213 static struct in_ifaddr * 214 in_localip_more(struct in_ifaddr *original_ia) 215 { 216 struct epoch_tracker et; 217 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr; 218 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib; 219 struct in_ifaddr *ia; 220 221 NET_EPOCH_ENTER(et); 222 CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) { 223 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr; 224 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib; 225 if (!V_rt_add_addr_allfibs && (original_fib != fib)) 226 continue; 227 if ((original_ia != ia) && (original_addr == addr)) { 228 ifa_ref(&ia->ia_ifa); 229 NET_EPOCH_EXIT(et); 230 return (ia); 231 } 232 } 233 NET_EPOCH_EXIT(et); 234 235 return (NULL); 236 } 237 238 /* 239 * Tries to find first IPv4 address in the provided fib. 240 * Prefers non-loopback addresses and return loopback IFF 241 * @loopback_ok is set. 242 * 243 * Returns ifa or NULL. 244 */ 245 struct in_ifaddr * 246 in_findlocal(uint32_t fibnum, bool loopback_ok) 247 { 248 struct in_ifaddr *ia = NULL, *ia_lo = NULL; 249 250 NET_EPOCH_ASSERT(); 251 252 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 253 uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib; 254 if (!V_rt_add_addr_allfibs && (fibnum != ia_fib)) 255 continue; 256 257 if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr))) 258 break; 259 if (loopback_ok) 260 ia_lo = ia; 261 } 262 263 if (ia == NULL) 264 ia = ia_lo; 265 266 return (ia); 267 } 268 269 /* 270 * Determine whether an IP address is in a reserved set of addresses 271 * that may not be forwarded, or whether datagrams to that destination 272 * may be forwarded. 273 */ 274 bool 275 in_canforward(struct in_addr in) 276 { 277 u_long i = ntohl(in.s_addr); 278 279 if (IN_MULTICAST(i) || IN_LINKLOCAL(i) || IN_LOOPBACK(i) || 280 in_nullhost(in)) 281 return (false); 282 if (IN_EXPERIMENTAL(i) && !V_ip_allow_net240) 283 return (false); 284 if (IN_ZERONET(i) && !V_ip_allow_net0) 285 return (false); 286 return (true); 287 } 288 289 /* 290 * Sysctl to manage prefix of reserved loopback network; translate 291 * to/from mask. The mask is always contiguous high-order 1 bits 292 * followed by all 0 bits. 293 */ 294 static int 295 sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS) 296 { 297 int error, preflen; 298 299 /* ffs is 1-based; compensate. */ 300 preflen = 33 - ffs(V_in_loopback_mask); 301 error = sysctl_handle_int(oidp, &preflen, 0, req); 302 if (error || !req->newptr) 303 return (error); 304 if (preflen < 8 || preflen > 31) 305 return (EINVAL); 306 V_in_loopback_mask = 0xffffffff << (32 - preflen); 307 return (0); 308 } 309 310 /* 311 * Trim a mask in a sockaddr 312 */ 313 static void 314 in_socktrim(struct sockaddr_in *ap) 315 { 316 char *cplim = (char *) &ap->sin_addr; 317 char *cp = (char *) (&ap->sin_addr + 1); 318 319 ap->sin_len = 0; 320 while (--cp >= cplim) 321 if (*cp) { 322 (ap)->sin_len = cp - (char *) (ap) + 1; 323 break; 324 } 325 } 326 327 /* 328 * Generic internet control operations (ioctl's). 329 */ 330 int 331 in_control_ioctl(u_long cmd, void *data, struct ifnet *ifp, 332 struct ucred *cred) 333 { 334 struct ifreq *ifr = (struct ifreq *)data; 335 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr; 336 struct epoch_tracker et; 337 struct ifaddr *ifa; 338 struct in_ifaddr *ia; 339 int error; 340 341 if (ifp == NULL) 342 return (EADDRNOTAVAIL); 343 344 /* 345 * Filter out 4 ioctls we implement directly. Forward the rest 346 * to specific functions and ifp->if_ioctl(). 347 */ 348 switch (cmd) { 349 case SIOCGIFADDR: 350 case SIOCGIFBRDADDR: 351 case SIOCGIFDSTADDR: 352 case SIOCGIFNETMASK: 353 break; 354 case SIOCGIFALIAS: 355 sx_xlock(&in_control_sx); 356 error = in_gifaddr_ioctl(cmd, data, ifp, cred); 357 sx_xunlock(&in_control_sx); 358 return (error); 359 case SIOCDIFADDR: 360 sx_xlock(&in_control_sx); 361 error = in_difaddr_ioctl(cmd, data, ifp, cred); 362 sx_xunlock(&in_control_sx); 363 return (error); 364 case OSIOCAIFADDR: /* 9.x compat */ 365 case SIOCAIFADDR: 366 sx_xlock(&in_control_sx); 367 error = in_aifaddr_ioctl(cmd, data, ifp, cred); 368 sx_xunlock(&in_control_sx); 369 return (error); 370 case SIOCSIFADDR: 371 case SIOCSIFBRDADDR: 372 case SIOCSIFDSTADDR: 373 case SIOCSIFNETMASK: 374 /* We no longer support that old commands. */ 375 return (EINVAL); 376 default: 377 if (ifp->if_ioctl == NULL) 378 return (EOPNOTSUPP); 379 return ((*ifp->if_ioctl)(ifp, cmd, data)); 380 } 381 382 if (addr->sin_addr.s_addr != INADDR_ANY && 383 prison_check_ip4(cred, &addr->sin_addr) != 0) 384 return (EADDRNOTAVAIL); 385 386 /* 387 * Find address for this interface, if it exists. If an 388 * address was specified, find that one instead of the 389 * first one on the interface, if possible. 390 */ 391 NET_EPOCH_ENTER(et); 392 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 393 if (ifa->ifa_addr->sa_family != AF_INET) 394 continue; 395 ia = (struct in_ifaddr *)ifa; 396 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr) 397 break; 398 } 399 if (ifa == NULL) 400 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 401 if (ifa->ifa_addr->sa_family == AF_INET) { 402 ia = (struct in_ifaddr *)ifa; 403 if (prison_check_ip4(cred, 404 &ia->ia_addr.sin_addr) == 0) 405 break; 406 } 407 408 if (ifa == NULL) { 409 NET_EPOCH_EXIT(et); 410 return (EADDRNOTAVAIL); 411 } 412 413 error = 0; 414 switch (cmd) { 415 case SIOCGIFADDR: 416 *addr = ia->ia_addr; 417 break; 418 419 case SIOCGIFBRDADDR: 420 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 421 error = EINVAL; 422 break; 423 } 424 *addr = ia->ia_broadaddr; 425 break; 426 427 case SIOCGIFDSTADDR: 428 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 429 error = EINVAL; 430 break; 431 } 432 *addr = ia->ia_dstaddr; 433 break; 434 435 case SIOCGIFNETMASK: 436 *addr = ia->ia_sockmask; 437 break; 438 } 439 440 NET_EPOCH_EXIT(et); 441 442 return (error); 443 } 444 445 int 446 in_mask2len(struct in_addr *mask) 447 { 448 int x, y; 449 u_char *p; 450 451 p = (u_char *)mask; 452 for (x = 0; x < sizeof(*mask); x++) { 453 if (p[x] != 0xff) 454 break; 455 } 456 y = 0; 457 if (x < sizeof(*mask)) { 458 for (y = 0; y < 8; y++) { 459 if ((p[x] & (0x80 >> y)) == 0) 460 break; 461 } 462 } 463 return (x * 8 + y); 464 } 465 466 int 467 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp, 468 struct thread *td) 469 { 470 return (in_control_ioctl(cmd, data, ifp, td ? td->td_ucred : NULL)); 471 } 472 473 static int 474 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred) 475 { 476 const struct in_aliasreq *ifra = (struct in_aliasreq *)data; 477 const struct sockaddr_in *addr = &ifra->ifra_addr; 478 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr; 479 const struct sockaddr_in *mask = &ifra->ifra_mask; 480 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr; 481 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0; 482 struct epoch_tracker et; 483 struct ifaddr *ifa; 484 struct in_ifaddr *ia; 485 bool iaIsFirst; 486 int error = 0; 487 488 error = priv_check_cred(cred, PRIV_NET_ADDIFADDR); 489 if (error) 490 return (error); 491 492 /* 493 * ifra_addr must be present and be of INET family. 494 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional. 495 */ 496 if (addr->sin_len != sizeof(struct sockaddr_in) || 497 addr->sin_family != AF_INET) 498 return (EINVAL); 499 if (broadaddr->sin_len != 0 && 500 (broadaddr->sin_len != sizeof(struct sockaddr_in) || 501 broadaddr->sin_family != AF_INET)) 502 return (EINVAL); 503 if (mask->sin_len != 0 && 504 (mask->sin_len != sizeof(struct sockaddr_in) || 505 mask->sin_family != AF_INET)) 506 return (EINVAL); 507 if ((ifp->if_flags & IFF_POINTOPOINT) && 508 (dstaddr->sin_len != sizeof(struct sockaddr_in) || 509 dstaddr->sin_addr.s_addr == INADDR_ANY)) 510 return (EDESTADDRREQ); 511 if (vhid != 0 && carp_attach_p == NULL) 512 return (EPROTONOSUPPORT); 513 514 #ifdef MAC 515 /* Check if a MAC policy disallows setting the IPv4 address. */ 516 error = mac_inet_check_add_addr(cred, &addr->sin_addr, ifp); 517 if (error != 0) 518 return (error); 519 #endif 520 521 /* 522 * See whether address already exist. 523 */ 524 iaIsFirst = true; 525 ia = NULL; 526 NET_EPOCH_ENTER(et); 527 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 528 struct in_ifaddr *it; 529 530 if (ifa->ifa_addr->sa_family != AF_INET) 531 continue; 532 533 it = (struct in_ifaddr *)ifa; 534 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 535 prison_check_ip4(cred, &addr->sin_addr) == 0) 536 ia = it; 537 else 538 iaIsFirst = false; 539 } 540 NET_EPOCH_EXIT(et); 541 542 if (ia != NULL) 543 (void )in_difaddr_ioctl(cmd, data, ifp, cred); 544 545 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK); 546 ia = (struct in_ifaddr *)ifa; 547 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 548 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 549 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 550 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock, 551 CALLOUT_RETURNUNLOCKED); 552 553 ia->ia_ifp = ifp; 554 ia->ia_addr = *addr; 555 if (mask->sin_len != 0) { 556 ia->ia_sockmask = *mask; 557 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 558 } else { 559 in_addr_t i = ntohl(addr->sin_addr.s_addr); 560 561 /* 562 * If netmask isn't supplied, use historical default. 563 * This is deprecated for interfaces other than loopback 564 * or point-to-point; warn in other cases. In the future 565 * we should return an error rather than warning. 566 */ 567 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0) 568 printf("%s: set address: WARNING: network mask " 569 "should be specified; using historical default\n", 570 ifp->if_xname); 571 if (IN_CLASSA(i)) 572 ia->ia_subnetmask = IN_CLASSA_NET; 573 else if (IN_CLASSB(i)) 574 ia->ia_subnetmask = IN_CLASSB_NET; 575 else 576 ia->ia_subnetmask = IN_CLASSC_NET; 577 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 578 } 579 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask; 580 in_socktrim(&ia->ia_sockmask); 581 582 if (ifp->if_flags & IFF_BROADCAST) { 583 if (broadaddr->sin_len != 0) { 584 ia->ia_broadaddr = *broadaddr; 585 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) { 586 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 587 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 588 ia->ia_broadaddr.sin_family = AF_INET; 589 } else { 590 ia->ia_broadaddr.sin_addr.s_addr = 591 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 592 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 593 ia->ia_broadaddr.sin_family = AF_INET; 594 } 595 } 596 597 if (ifp->if_flags & IFF_POINTOPOINT) 598 ia->ia_dstaddr = *dstaddr; 599 600 if (vhid != 0) { 601 error = (*carp_attach_p)(&ia->ia_ifa, vhid); 602 if (error) 603 return (error); 604 } 605 606 /* if_addrhead is already referenced by ifa_alloc() */ 607 IF_ADDR_WLOCK(ifp); 608 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 609 IF_ADDR_WUNLOCK(ifp); 610 611 ifa_ref(ifa); /* in_ifaddrhead */ 612 sx_assert(&in_control_sx, SA_XLOCKED); 613 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 614 CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, 615 ia_hash); 616 617 /* 618 * Give the interface a chance to initialize 619 * if this is its first address, 620 * and to validate the address if necessary. 621 */ 622 if (ifp->if_ioctl != NULL) { 623 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 624 if (error) 625 goto fail1; 626 } 627 628 /* 629 * Add route for the network. 630 */ 631 if (vhid == 0) { 632 error = in_addprefix(ia); 633 if (error) 634 goto fail1; 635 } 636 637 /* 638 * Add a loopback route to self. 639 */ 640 if (vhid == 0 && ia_need_loopback_route(ia)) { 641 struct in_ifaddr *eia; 642 643 eia = in_localip_more(ia); 644 645 if (eia == NULL) { 646 error = ifa_add_loopback_route((struct ifaddr *)ia, 647 (struct sockaddr *)&ia->ia_addr); 648 if (error) 649 goto fail2; 650 } else 651 ifa_free(&eia->ia_ifa); 652 } 653 654 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) { 655 struct in_addr allhosts_addr; 656 struct in_ifinfo *ii; 657 658 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 659 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 660 661 error = in_joingroup(ifp, &allhosts_addr, NULL, 662 &ii->ii_allhosts); 663 } 664 665 /* 666 * Note: we don't need extra reference for ifa, since we called 667 * with sx lock held, and ifaddr can not be deleted in concurrent 668 * thread. 669 */ 670 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD); 671 672 return (error); 673 674 fail2: 675 if (vhid == 0) 676 (void )in_scrubprefix(ia, LLE_STATIC); 677 678 fail1: 679 if (ia->ia_ifa.ifa_carp) 680 (*carp_detach_p)(&ia->ia_ifa, false); 681 682 IF_ADDR_WLOCK(ifp); 683 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link); 684 IF_ADDR_WUNLOCK(ifp); 685 ifa_free(&ia->ia_ifa); /* if_addrhead */ 686 687 sx_assert(&in_control_sx, SA_XLOCKED); 688 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link); 689 CK_LIST_REMOVE(ia, ia_hash); 690 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 691 692 return (error); 693 } 694 695 static int 696 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred) 697 { 698 const struct ifreq *ifr = (struct ifreq *)data; 699 const struct sockaddr_in *addr = (const struct sockaddr_in *) 700 &ifr->ifr_addr; 701 struct ifaddr *ifa; 702 struct in_ifaddr *ia; 703 bool deleteAny, iaIsLast; 704 int error; 705 706 if (cred != NULL) { 707 error = priv_check_cred(cred, PRIV_NET_DELIFADDR); 708 if (error) 709 return (error); 710 } 711 712 if (addr->sin_len != sizeof(struct sockaddr_in) || 713 addr->sin_family != AF_INET) 714 deleteAny = true; 715 else 716 deleteAny = false; 717 718 iaIsLast = true; 719 ia = NULL; 720 IF_ADDR_WLOCK(ifp); 721 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 722 struct in_ifaddr *it; 723 724 if (ifa->ifa_addr->sa_family != AF_INET) 725 continue; 726 727 it = (struct in_ifaddr *)ifa; 728 if (deleteAny && ia == NULL && (cred == NULL || 729 prison_check_ip4(cred, &it->ia_addr.sin_addr) == 0)) 730 ia = it; 731 732 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 733 (cred == NULL || prison_check_ip4(cred, 734 &addr->sin_addr) == 0)) 735 ia = it; 736 737 if (it != ia) 738 iaIsLast = false; 739 } 740 741 if (ia == NULL) { 742 IF_ADDR_WUNLOCK(ifp); 743 return (EADDRNOTAVAIL); 744 } 745 746 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link); 747 IF_ADDR_WUNLOCK(ifp); 748 ifa_free(&ia->ia_ifa); /* if_addrhead */ 749 750 sx_assert(&in_control_sx, SA_XLOCKED); 751 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link); 752 CK_LIST_REMOVE(ia, ia_hash); 753 754 /* 755 * in_scrubprefix() kills the interface route. 756 */ 757 in_scrubprefix(ia, LLE_STATIC); 758 759 /* 760 * in_ifadown gets rid of all the rest of 761 * the routes. This is not quite the right 762 * thing to do, but at least if we are running 763 * a routing process they will come back. 764 */ 765 in_ifadown(&ia->ia_ifa, 1); 766 767 if (ia->ia_ifa.ifa_carp) 768 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR); 769 770 /* 771 * If this is the last IPv4 address configured on this 772 * interface, leave the all-hosts group. 773 * No state-change report need be transmitted. 774 */ 775 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) { 776 struct in_ifinfo *ii; 777 778 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 779 if (ii->ii_allhosts) { 780 (void)in_leavegroup(ii->ii_allhosts, NULL); 781 ii->ii_allhosts = NULL; 782 } 783 } 784 785 IF_ADDR_WLOCK(ifp); 786 if (callout_stop(&ia->ia_garp_timer) == 1) { 787 ifa_free(&ia->ia_ifa); 788 } 789 IF_ADDR_WUNLOCK(ifp); 790 791 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa, 792 IFADDR_EVENT_DEL); 793 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 794 795 return (0); 796 } 797 798 static int 799 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred) 800 { 801 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 802 const struct sockaddr_in *addr = &ifra->ifra_addr; 803 struct epoch_tracker et; 804 struct ifaddr *ifa; 805 struct in_ifaddr *ia; 806 807 /* 808 * ifra_addr must be present and be of INET family. 809 */ 810 if (addr->sin_len != sizeof(struct sockaddr_in) || 811 addr->sin_family != AF_INET) 812 return (EINVAL); 813 814 /* 815 * See whether address exist. 816 */ 817 ia = NULL; 818 NET_EPOCH_ENTER(et); 819 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 820 struct in_ifaddr *it; 821 822 if (ifa->ifa_addr->sa_family != AF_INET) 823 continue; 824 825 it = (struct in_ifaddr *)ifa; 826 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 827 prison_check_ip4(cred, &addr->sin_addr) == 0) { 828 ia = it; 829 break; 830 } 831 } 832 if (ia == NULL) { 833 NET_EPOCH_EXIT(et); 834 return (EADDRNOTAVAIL); 835 } 836 837 ifra->ifra_mask = ia->ia_sockmask; 838 if ((ifp->if_flags & IFF_POINTOPOINT) && 839 ia->ia_dstaddr.sin_family == AF_INET) 840 ifra->ifra_dstaddr = ia->ia_dstaddr; 841 else if ((ifp->if_flags & IFF_BROADCAST) && 842 ia->ia_broadaddr.sin_family == AF_INET) 843 ifra->ifra_broadaddr = ia->ia_broadaddr; 844 else 845 memset(&ifra->ifra_broadaddr, 0, 846 sizeof(ifra->ifra_broadaddr)); 847 848 NET_EPOCH_EXIT(et); 849 return (0); 850 } 851 852 static int 853 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg) 854 { 855 856 if (nh->nh_ifa == (struct ifaddr *)arg) 857 return (1); 858 859 return (0); 860 } 861 862 static int 863 in_handle_prefix_route(uint32_t fibnum, int cmd, 864 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa, 865 struct ifnet *ifp) 866 { 867 868 NET_EPOCH_ASSERT(); 869 870 /* Prepare gateway */ 871 struct sockaddr_dl_short sdl = { 872 .sdl_family = AF_LINK, 873 .sdl_len = sizeof(struct sockaddr_dl_short), 874 .sdl_type = ifa->ifa_ifp->if_type, 875 .sdl_index = ifa->ifa_ifp->if_index, 876 }; 877 878 struct rt_addrinfo info = { 879 .rti_ifa = ifa, 880 .rti_ifp = ifp, 881 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST), 882 .rti_info = { 883 [RTAX_DST] = (struct sockaddr *)dst, 884 [RTAX_NETMASK] = (struct sockaddr *)netmask, 885 [RTAX_GATEWAY] = (struct sockaddr *)&sdl, 886 }, 887 /* Ensure we delete the prefix IFF prefix ifa matches */ 888 .rti_filter = in_match_ifaddr, 889 .rti_filterdata = ifa, 890 }; 891 892 return (rib_handle_ifaddr_info(fibnum, cmd, &info)); 893 } 894 895 /* 896 * Routing table interaction with interface addresses. 897 * 898 * In general, two types of routes needs to be installed: 899 * a) "interface" or "prefix" route, telling user that the addresses 900 * behind the ifa prefix are reached directly. 901 * b) "loopback" route installed for the ifa address, telling user that 902 * the address belongs to local system. 903 * 904 * Handling for (a) and (b) differs in multi-fib aspects, hence they 905 * are implemented in different functions below. 906 * 907 * The cases above may intersect - /32 interface aliases results in 908 * the same prefix produced by (a) and (b). This blurs the definition 909 * of the "loopback" route and complicate interactions. The interaction 910 * table is defined below. The case numbers are used in the multiple 911 * functions below to refer to the particular test case. 912 * 913 * There can be multiple options: 914 * 1) Adding address with prefix on non-p2p/non-loopback interface. 915 * Example: 192.0.2.1/24. Action: 916 * * add "prefix" route towards 192.0.2.0/24 via @ia interface, 917 * using @ia as an address source. 918 * * add "loopback" route towards 192.0.2.1 via V_loif, saving 919 * @ia ifp in the gateway and using @ia as an address source. 920 * 921 * 2) Adding address with /32 mask to non-p2p/non-loopback interface. 922 * Example: 192.0.2.2/32. Action: 923 * * add "prefix" host route via V_loif, using @ia as an address source. 924 * 925 * 3) Adding address with or without prefix to p2p interface. 926 * Example: 10.0.0.1/24->10.0.0.2. Action: 927 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia 928 * as an address source. Note: no sense in installing full /24 as the interface 929 * is point-to-point. 930 * * add "loopback" route towards 10.0.9.1 via V_loif, saving 931 * @ia ifp in the gateway and using @ia as an address source. 932 * 933 * 4) Adding address with or without prefix to loopback interface. 934 * Example: 192.0.2.1/24. Action: 935 * * add "prefix" host route via @ia interface, using @ia as an address source. 936 * Note: Skip installing /24 prefix as it would introduce TTL loop 937 * for the traffic destined to these addresses. 938 */ 939 940 /* 941 * Checks if @ia needs to install loopback route to @ia address via 942 * ifa_maintain_loopback_route(). 943 * 944 * Return true on success. 945 */ 946 static bool 947 ia_need_loopback_route(const struct in_ifaddr *ia) 948 { 949 struct ifnet *ifp = ia->ia_ifp; 950 951 /* Case 4: Skip loopback interfaces */ 952 if ((ifp->if_flags & IFF_LOOPBACK) || 953 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)) 954 return (false); 955 956 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */ 957 if ((ifp->if_flags & IFF_POINTOPOINT) && 958 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr) 959 return (false); 960 961 /* Case 2: skip /32 prefixes */ 962 if (!(ifp->if_flags & IFF_POINTOPOINT) && 963 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST)) 964 return (false); 965 966 return (true); 967 } 968 969 /* 970 * Calculate "prefix" route corresponding to @ia. 971 */ 972 static void 973 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask) 974 { 975 976 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) { 977 /* Case 3: return host route for dstaddr */ 978 *prefix = ia->ia_dstaddr.sin_addr; 979 mask->s_addr = INADDR_BROADCAST; 980 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) { 981 /* Case 4: return host route for ifaddr */ 982 *prefix = ia->ia_addr.sin_addr; 983 mask->s_addr = INADDR_BROADCAST; 984 } else { 985 /* Cases 1,2: return actual ia prefix */ 986 *prefix = ia->ia_addr.sin_addr; 987 *mask = ia->ia_sockmask.sin_addr; 988 prefix->s_addr &= mask->s_addr; 989 } 990 } 991 992 /* 993 * Adds or delete interface "prefix" route corresponding to @ifa. 994 * Returns 0 on success or errno. 995 */ 996 static int 997 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia) 998 { 999 struct ifaddr *ifa = &ia->ia_ifa; 1000 struct in_addr daddr, maddr; 1001 struct sockaddr_in *pmask; 1002 struct epoch_tracker et; 1003 int error; 1004 1005 ia_getrtprefix(ia, &daddr, &maddr); 1006 1007 struct sockaddr_in mask = { 1008 .sin_family = AF_INET, 1009 .sin_len = sizeof(struct sockaddr_in), 1010 .sin_addr = maddr, 1011 }; 1012 1013 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL; 1014 1015 struct sockaddr_in dst = { 1016 .sin_family = AF_INET, 1017 .sin_len = sizeof(struct sockaddr_in), 1018 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr, 1019 }; 1020 1021 struct ifnet *ifp = ia->ia_ifp; 1022 1023 if ((maddr.s_addr == INADDR_BROADCAST) && 1024 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) { 1025 /* Case 2: host route on broadcast interface */ 1026 ifp = V_loif; 1027 } 1028 1029 uint32_t fibnum = ifa->ifa_ifp->if_fib; 1030 NET_EPOCH_ENTER(et); 1031 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp); 1032 NET_EPOCH_EXIT(et); 1033 1034 return (error); 1035 } 1036 1037 /* 1038 * Check if we have a route for the given prefix already. 1039 */ 1040 static bool 1041 in_hasrtprefix(struct in_ifaddr *target) 1042 { 1043 struct epoch_tracker et; 1044 struct in_ifaddr *ia; 1045 struct in_addr prefix, mask, p, m; 1046 bool result = false; 1047 1048 ia_getrtprefix(target, &prefix, &mask); 1049 1050 /* Look for an existing address with the same prefix, mask, and fib */ 1051 NET_EPOCH_ENTER(et); 1052 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1053 ia_getrtprefix(ia, &p, &m); 1054 1055 if (prefix.s_addr != p.s_addr || 1056 mask.s_addr != m.s_addr) 1057 continue; 1058 1059 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib) 1060 continue; 1061 1062 /* 1063 * If we got a matching prefix route inserted by other 1064 * interface address, we are done here. 1065 */ 1066 if (ia->ia_flags & IFA_ROUTE) { 1067 result = true; 1068 break; 1069 } 1070 } 1071 NET_EPOCH_EXIT(et); 1072 1073 return (result); 1074 } 1075 1076 int 1077 in_addprefix(struct in_ifaddr *target) 1078 { 1079 int error; 1080 1081 if (in_hasrtprefix(target)) { 1082 if (V_nosameprefix) 1083 return (EEXIST); 1084 else { 1085 rt_addrmsg(RTM_ADD, &target->ia_ifa, 1086 target->ia_ifp->if_fib); 1087 return (0); 1088 } 1089 } 1090 1091 /* 1092 * No-one seem to have this prefix route, so we try to insert it. 1093 */ 1094 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib); 1095 error = in_handle_ifaddr_route(RTM_ADD, target); 1096 if (!error) 1097 target->ia_flags |= IFA_ROUTE; 1098 return (error); 1099 } 1100 1101 /* 1102 * Removes either all lle entries for given @ia, or lle 1103 * corresponding to @ia address. 1104 */ 1105 static void 1106 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags) 1107 { 1108 struct sockaddr_in addr, mask; 1109 struct sockaddr *saddr, *smask; 1110 struct ifnet *ifp; 1111 1112 saddr = (struct sockaddr *)&addr; 1113 bzero(&addr, sizeof(addr)); 1114 addr.sin_len = sizeof(addr); 1115 addr.sin_family = AF_INET; 1116 smask = (struct sockaddr *)&mask; 1117 bzero(&mask, sizeof(mask)); 1118 mask.sin_len = sizeof(mask); 1119 mask.sin_family = AF_INET; 1120 mask.sin_addr.s_addr = ia->ia_subnetmask; 1121 ifp = ia->ia_ifp; 1122 1123 if (all) { 1124 /* 1125 * Remove all L2 entries matching given prefix. 1126 * Convert address to host representation to avoid 1127 * doing this on every callback. ia_subnetmask is already 1128 * stored in host representation. 1129 */ 1130 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr); 1131 lltable_prefix_free(AF_INET, saddr, smask, flags); 1132 } else { 1133 /* Remove interface address only */ 1134 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr; 1135 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr); 1136 } 1137 } 1138 1139 /* 1140 * If there is no other address in the system that can serve a route to the 1141 * same prefix, remove the route. Hand over the route to the new address 1142 * otherwise. 1143 */ 1144 int 1145 in_scrubprefix(struct in_ifaddr *target, u_int flags) 1146 { 1147 struct epoch_tracker et; 1148 struct in_ifaddr *ia; 1149 struct in_addr prefix, mask, p, m; 1150 int error = 0; 1151 1152 /* 1153 * Remove the loopback route to the interface address. 1154 */ 1155 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) { 1156 struct in_ifaddr *eia; 1157 1158 eia = in_localip_more(target); 1159 1160 if (eia != NULL) { 1161 error = ifa_switch_loopback_route((struct ifaddr *)eia, 1162 (struct sockaddr *)&target->ia_addr); 1163 ifa_free(&eia->ia_ifa); 1164 } else { 1165 error = ifa_del_loopback_route((struct ifaddr *)target, 1166 (struct sockaddr *)&target->ia_addr); 1167 } 1168 } 1169 1170 ia_getrtprefix(target, &prefix, &mask); 1171 1172 if ((target->ia_flags & IFA_ROUTE) == 0) { 1173 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib); 1174 1175 /* 1176 * Removing address from !IFF_UP interface or 1177 * prefix which exists on other interface (along with route). 1178 * No entries should exist here except target addr. 1179 * Given that, delete this entry only. 1180 */ 1181 in_scrubprefixlle(target, 0, flags); 1182 return (0); 1183 } 1184 1185 NET_EPOCH_ENTER(et); 1186 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1187 ia_getrtprefix(ia, &p, &m); 1188 1189 if (prefix.s_addr != p.s_addr || 1190 mask.s_addr != m.s_addr) 1191 continue; 1192 1193 if ((ia->ia_ifp->if_flags & IFF_UP) == 0) 1194 continue; 1195 1196 /* 1197 * If we got a matching prefix address, move IFA_ROUTE and 1198 * the route itself to it. Make sure that routing daemons 1199 * get a heads-up. 1200 */ 1201 if ((ia->ia_flags & IFA_ROUTE) == 0) { 1202 ifa_ref(&ia->ia_ifa); 1203 NET_EPOCH_EXIT(et); 1204 error = in_handle_ifaddr_route(RTM_DELETE, target); 1205 if (error == 0) 1206 target->ia_flags &= ~IFA_ROUTE; 1207 else 1208 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n", 1209 error); 1210 /* Scrub all entries IFF interface is different */ 1211 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp, 1212 flags); 1213 error = in_handle_ifaddr_route(RTM_ADD, ia); 1214 if (error == 0) 1215 ia->ia_flags |= IFA_ROUTE; 1216 else 1217 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n", 1218 error); 1219 ifa_free(&ia->ia_ifa); 1220 return (error); 1221 } 1222 } 1223 NET_EPOCH_EXIT(et); 1224 1225 /* 1226 * remove all L2 entries on the given prefix 1227 */ 1228 in_scrubprefixlle(target, 1, flags); 1229 1230 /* 1231 * As no-one seem to have this prefix, we can remove the route. 1232 */ 1233 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib); 1234 error = in_handle_ifaddr_route(RTM_DELETE, target); 1235 if (error == 0) 1236 target->ia_flags &= ~IFA_ROUTE; 1237 else 1238 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error); 1239 return (error); 1240 } 1241 1242 void 1243 in_ifscrub_all(void) 1244 { 1245 struct ifnet *ifp; 1246 struct ifaddr *ifa, *nifa; 1247 struct ifreq ifr; 1248 1249 IFNET_RLOCK(); 1250 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) { 1251 /* Cannot lock here - lock recursion. */ 1252 /* NET_EPOCH_ENTER(et); */ 1253 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) { 1254 if (ifa->ifa_addr->sa_family != AF_INET) 1255 continue; 1256 1257 /* 1258 * This is ugly but the only way for legacy IP to 1259 * cleanly remove addresses and everything attached. 1260 */ 1261 bzero(&ifr, sizeof(ifr)); 1262 ifr.ifr_addr = *ifa->ifa_addr; 1263 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, 1264 ifp, NULL); 1265 } 1266 /* NET_EPOCH_EXIT(et); */ 1267 in_purgemaddrs(ifp); 1268 igmp_domifdetach(ifp); 1269 } 1270 IFNET_RUNLOCK(); 1271 } 1272 1273 bool 1274 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia) 1275 { 1276 1277 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1278 /* 1279 * Optionally check for old-style (host 0) broadcast, but 1280 * taking into account that RFC 3021 obsoletes it. 1281 */ 1282 __predict_false(V_broadcast_lowest && 1283 ia->ia_subnetmask != IN_RFC3021_MASK && 1284 ntohl(in.s_addr) == ia->ia_subnet)) && 1285 /* 1286 * Check for an all one subnetmask. These 1287 * only exist when an interface gets a secondary 1288 * address. 1289 */ 1290 ia->ia_subnetmask != (u_long)0xffffffff); 1291 } 1292 1293 /* 1294 * Return true if the address might be a local broadcast address. 1295 */ 1296 bool 1297 in_ifnet_broadcast(struct in_addr in, struct ifnet *ifp) 1298 { 1299 struct ifaddr *ifa; 1300 1301 NET_EPOCH_ASSERT(); 1302 1303 if (in_broadcast(in)) 1304 return (true); 1305 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1306 return (false); 1307 /* 1308 * Look through the list of addresses for a match 1309 * with a broadcast address. 1310 */ 1311 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1312 if (ifa->ifa_addr->sa_family == AF_INET && 1313 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) 1314 return (true); 1315 return (false); 1316 } 1317 1318 /* 1319 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1320 */ 1321 void 1322 in_ifdetach(struct ifnet *ifp) 1323 { 1324 IN_MULTI_LOCK(); 1325 in_pcbpurgeif0(&V_ripcbinfo, ifp); 1326 in_pcbpurgeif0(&V_udbinfo, ifp); 1327 in_pcbpurgeif0(&V_ulitecbinfo, ifp); 1328 in_purgemaddrs(ifp); 1329 IN_MULTI_UNLOCK(); 1330 1331 /* 1332 * Make sure all multicast deletions invoking if_ioctl() are 1333 * completed before returning. Else we risk accessing a freed 1334 * ifnet structure pointer. 1335 */ 1336 inm_release_wait(NULL); 1337 } 1338 1339 static void 1340 in_ifnet_event(void *arg __unused, struct ifnet *ifp, int event) 1341 { 1342 struct epoch_tracker et; 1343 struct ifaddr *ifa; 1344 struct in_ifaddr *ia; 1345 int error; 1346 1347 NET_EPOCH_ENTER(et); 1348 switch (event) { 1349 case IFNET_EVENT_DOWN: 1350 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1351 if (ifa->ifa_addr->sa_family != AF_INET) 1352 continue; 1353 ia = (struct in_ifaddr *)ifa; 1354 if ((ia->ia_flags & IFA_ROUTE) == 0) 1355 continue; 1356 ifa_ref(ifa); 1357 /* 1358 * in_scrubprefix() kills the interface route. 1359 */ 1360 in_scrubprefix(ia, 0); 1361 /* 1362 * in_ifadown gets rid of all the rest of the 1363 * routes. This is not quite the right thing 1364 * to do, but at least if we are running a 1365 * routing process they will come back. 1366 */ 1367 in_ifadown(ifa, 0); 1368 ifa_free(ifa); 1369 } 1370 break; 1371 1372 case IFNET_EVENT_UP: 1373 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 1374 if (ifa->ifa_addr->sa_family != AF_INET) 1375 continue; 1376 ia = (struct in_ifaddr *)ifa; 1377 if (ia->ia_flags & IFA_ROUTE) 1378 continue; 1379 ifa_ref(ifa); 1380 error = ifa_del_loopback_route(ifa, ifa->ifa_addr); 1381 rt_addrmsg(RTM_ADD, ifa, ifa->ifa_ifp->if_fib); 1382 error = in_handle_ifaddr_route(RTM_ADD, ia); 1383 if (error == 0) 1384 ia->ia_flags |= IFA_ROUTE; 1385 error = ifa_add_loopback_route(ifa, ifa->ifa_addr); 1386 ifa_free(ifa); 1387 } 1388 break; 1389 } 1390 NET_EPOCH_EXIT(et); 1391 } 1392 EVENTHANDLER_DEFINE(ifnet_event, in_ifnet_event, NULL, EVENTHANDLER_PRI_ANY); 1393 1394 /* 1395 * Delete all IPv4 multicast address records, and associated link-layer 1396 * multicast address records, associated with ifp. 1397 * XXX It looks like domifdetach runs AFTER the link layer cleanup. 1398 * XXX This should not race with ifma_protospec being set during 1399 * a new allocation, if it does, we have bigger problems. 1400 */ 1401 static void 1402 in_purgemaddrs(struct ifnet *ifp) 1403 { 1404 struct epoch_tracker et; 1405 struct in_multi_head purgeinms; 1406 struct in_multi *inm; 1407 struct ifmultiaddr *ifma; 1408 1409 SLIST_INIT(&purgeinms); 1410 IN_MULTI_LIST_LOCK(); 1411 1412 /* 1413 * Extract list of in_multi associated with the detaching ifp 1414 * which the PF_INET layer is about to release. 1415 * We need to do this as IF_ADDR_LOCK() may be re-acquired 1416 * by code further down. 1417 */ 1418 IF_ADDR_WLOCK(ifp); 1419 NET_EPOCH_ENTER(et); 1420 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1421 inm = inm_ifmultiaddr_get_inm(ifma); 1422 if (inm == NULL) 1423 continue; 1424 inm_rele_locked(&purgeinms, inm); 1425 } 1426 NET_EPOCH_EXIT(et); 1427 IF_ADDR_WUNLOCK(ifp); 1428 1429 inm_release_list_deferred(&purgeinms); 1430 igmp_ifdetach(ifp); 1431 IN_MULTI_LIST_UNLOCK(); 1432 } 1433 1434 struct in_llentry { 1435 struct llentry base; 1436 }; 1437 1438 #define IN_LLTBL_DEFAULT_HSIZE 32 1439 #define IN_LLTBL_HASH(k, h) \ 1440 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1)) 1441 1442 /* 1443 * Do actual deallocation of @lle. 1444 */ 1445 static void 1446 in_lltable_destroy_lle_unlocked(epoch_context_t ctx) 1447 { 1448 struct llentry *lle; 1449 1450 lle = __containerof(ctx, struct llentry, lle_epoch_ctx); 1451 LLE_LOCK_DESTROY(lle); 1452 LLE_REQ_DESTROY(lle); 1453 free(lle, M_LLTABLE); 1454 } 1455 1456 /* 1457 * Called by LLE_FREE_LOCKED when number of references 1458 * drops to zero. 1459 */ 1460 static void 1461 in_lltable_destroy_lle(struct llentry *lle) 1462 { 1463 1464 LLE_WUNLOCK(lle); 1465 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx); 1466 } 1467 1468 static struct llentry * 1469 in_lltable_new(struct in_addr addr4, u_int flags) 1470 { 1471 struct in_llentry *lle; 1472 1473 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 1474 if (lle == NULL) /* NB: caller generates msg */ 1475 return NULL; 1476 1477 /* 1478 * For IPv4 this will trigger "arpresolve" to generate 1479 * an ARP request. 1480 */ 1481 lle->base.la_expire = time_uptime; /* mark expired */ 1482 lle->base.r_l3addr.addr4 = addr4; 1483 lle->base.lle_refcnt = 1; 1484 lle->base.lle_free = in_lltable_destroy_lle; 1485 LLE_LOCK_INIT(&lle->base); 1486 LLE_REQ_INIT(&lle->base); 1487 callout_init(&lle->base.lle_timer, 1); 1488 1489 return (&lle->base); 1490 } 1491 1492 static int 1493 in_lltable_match_prefix(const struct sockaddr *saddr, 1494 const struct sockaddr *smask, u_int flags, struct llentry *lle) 1495 { 1496 struct in_addr addr, mask, lle_addr; 1497 1498 addr = ((const struct sockaddr_in *)saddr)->sin_addr; 1499 mask = ((const struct sockaddr_in *)smask)->sin_addr; 1500 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr); 1501 1502 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0) 1503 return (0); 1504 1505 if (lle->la_flags & LLE_IFADDR) { 1506 /* 1507 * Delete LLE_IFADDR records IFF address & flag matches. 1508 * Note that addr is the interface address within prefix 1509 * being matched. 1510 * Note also we should handle 'ifdown' cases without removing 1511 * ifaddr macs. 1512 */ 1513 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0) 1514 return (1); 1515 return (0); 1516 } 1517 1518 /* flags & LLE_STATIC means deleting both dynamic and static entries */ 1519 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)) 1520 return (1); 1521 1522 return (0); 1523 } 1524 1525 static void 1526 in_lltable_free_entry(struct lltable *llt, struct llentry *lle) 1527 { 1528 size_t pkts_dropped; 1529 1530 LLE_WLOCK_ASSERT(lle); 1531 KASSERT(llt != NULL, ("lltable is NULL")); 1532 1533 /* Unlink entry from table if not already */ 1534 if ((lle->la_flags & LLE_LINKED) != 0) { 1535 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp); 1536 lltable_unlink_entry(llt, lle); 1537 } 1538 1539 /* Drop hold queue */ 1540 pkts_dropped = llentry_free(lle); 1541 ARPSTAT_ADD(dropped, pkts_dropped); 1542 } 1543 1544 static int 1545 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 1546 { 1547 struct nhop_object *nh; 1548 struct in_addr addr; 1549 1550 KASSERT(l3addr->sa_family == AF_INET, 1551 ("sin_family %d", l3addr->sa_family)); 1552 1553 addr = ((const struct sockaddr_in *)l3addr)->sin_addr; 1554 1555 nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0); 1556 if (nh == NULL) 1557 return (EINVAL); 1558 1559 /* 1560 * If the gateway for an existing host route matches the target L3 1561 * address, which is a special route inserted by some implementation 1562 * such as MANET, and the interface is of the correct type, then 1563 * allow for ARP to proceed. 1564 */ 1565 if (nh->nh_flags & NHF_GATEWAY) { 1566 if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER || 1567 (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 || 1568 memcmp(nh->gw_sa.sa_data, l3addr->sa_data, 1569 sizeof(in_addr_t)) != 0) { 1570 return (EINVAL); 1571 } 1572 } 1573 1574 /* 1575 * Make sure that at least the destination address is covered 1576 * by the route. This is for handling the case where 2 or more 1577 * interfaces have the same prefix. An incoming packet arrives 1578 * on one interface and the corresponding outgoing packet leaves 1579 * another interface. 1580 */ 1581 if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) { 1582 struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp); 1583 struct in_addr dst_addr, mask_addr; 1584 1585 if (ia == NULL) 1586 return (EINVAL); 1587 1588 /* 1589 * ifaof_ifpforaddr() returns _best matching_ IFA. 1590 * It is possible that ifa prefix does not cover our address. 1591 * Explicitly verify and fail if that's the case. 1592 */ 1593 dst_addr = IA_SIN(ia)->sin_addr; 1594 mask_addr.s_addr = htonl(ia->ia_subnetmask); 1595 1596 if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr)) 1597 return (EINVAL); 1598 } 1599 1600 return (0); 1601 } 1602 1603 static inline uint32_t 1604 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize) 1605 { 1606 1607 return (IN_LLTBL_HASH(dst.s_addr, hsize)); 1608 } 1609 1610 static uint32_t 1611 in_lltable_hash(const struct llentry *lle, uint32_t hsize) 1612 { 1613 1614 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize)); 1615 } 1616 1617 static void 1618 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa) 1619 { 1620 struct sockaddr_in *sin; 1621 1622 sin = (struct sockaddr_in *)sa; 1623 bzero(sin, sizeof(*sin)); 1624 sin->sin_family = AF_INET; 1625 sin->sin_len = sizeof(*sin); 1626 sin->sin_addr = lle->r_l3addr.addr4; 1627 } 1628 1629 static inline struct llentry * 1630 in_lltable_find_dst(struct lltable *llt, struct in_addr dst) 1631 { 1632 struct llentry *lle; 1633 struct llentries *lleh; 1634 u_int hashidx; 1635 1636 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize); 1637 lleh = &llt->lle_head[hashidx]; 1638 CK_LIST_FOREACH(lle, lleh, lle_next) { 1639 if (lle->la_flags & LLE_DELETED) 1640 continue; 1641 if (lle->r_l3addr.addr4.s_addr == dst.s_addr) 1642 break; 1643 } 1644 1645 return (lle); 1646 } 1647 1648 static void 1649 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle) 1650 { 1651 1652 lle->la_flags |= LLE_DELETED; 1653 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 1654 #ifdef DIAGNOSTIC 1655 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1656 #endif 1657 llentry_free(lle); 1658 } 1659 1660 static struct llentry * 1661 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1662 { 1663 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1664 struct ifnet *ifp = llt->llt_ifp; 1665 struct llentry *lle; 1666 char linkhdr[LLE_MAX_LINKHDR]; 1667 size_t linkhdrsize; 1668 int lladdr_off; 1669 1670 KASSERT(l3addr->sa_family == AF_INET, 1671 ("sin_family %d", l3addr->sa_family)); 1672 1673 /* 1674 * A route that covers the given address must have 1675 * been installed 1st because we are doing a resolution, 1676 * verify this. 1677 */ 1678 if (!(flags & LLE_IFADDR) && 1679 in_lltable_rtcheck(ifp, flags, l3addr) != 0) 1680 return (NULL); 1681 1682 lle = in_lltable_new(sin->sin_addr, flags); 1683 if (lle == NULL) { 1684 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1685 return (NULL); 1686 } 1687 lle->la_flags = flags; 1688 if (flags & LLE_STATIC) 1689 lle->r_flags |= RLLE_VALID; 1690 if ((flags & LLE_IFADDR) == LLE_IFADDR) { 1691 linkhdrsize = LLE_MAX_LINKHDR; 1692 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp), 1693 linkhdr, &linkhdrsize, &lladdr_off) != 0) { 1694 in_lltable_free_entry(llt, lle); 1695 return (NULL); 1696 } 1697 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize, 1698 lladdr_off); 1699 lle->la_flags |= LLE_STATIC; 1700 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR); 1701 lle->la_expire = 0; 1702 } 1703 1704 return (lle); 1705 } 1706 1707 /* 1708 * Return NULL if not found or marked for deletion. 1709 * If found return lle read locked. 1710 */ 1711 static struct llentry * 1712 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1713 { 1714 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1715 struct llentry *lle; 1716 1717 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp); 1718 KASSERT(l3addr->sa_family == AF_INET, 1719 ("sin_family %d", l3addr->sa_family)); 1720 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) != 1721 (LLE_UNLOCKED | LLE_EXCLUSIVE), 1722 ("wrong lle request flags: %#x", flags)); 1723 1724 lle = in_lltable_find_dst(llt, sin->sin_addr); 1725 if (lle == NULL) 1726 return (NULL); 1727 if (flags & LLE_UNLOCKED) 1728 return (lle); 1729 1730 if (flags & LLE_EXCLUSIVE) 1731 LLE_WLOCK(lle); 1732 else 1733 LLE_RLOCK(lle); 1734 1735 /* 1736 * If the afdata lock is not held, the LLE may have been unlinked while 1737 * we were blocked on the LLE lock. Check for this case. 1738 */ 1739 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) { 1740 if (flags & LLE_EXCLUSIVE) 1741 LLE_WUNLOCK(lle); 1742 else 1743 LLE_RUNLOCK(lle); 1744 return (NULL); 1745 } 1746 return (lle); 1747 } 1748 1749 static int 1750 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle, 1751 struct sysctl_req *wr) 1752 { 1753 struct ifnet *ifp = llt->llt_ifp; 1754 /* XXX stack use */ 1755 struct { 1756 struct rt_msghdr rtm; 1757 struct sockaddr_in sin; 1758 struct sockaddr_dl sdl; 1759 } arpc; 1760 struct sockaddr_dl *sdl; 1761 int error; 1762 1763 bzero(&arpc, sizeof(arpc)); 1764 /* skip deleted entries */ 1765 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 1766 return (0); 1767 /* Skip if jailed and not a valid IP of the prison. */ 1768 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin); 1769 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0) 1770 return (0); 1771 /* 1772 * produce a msg made of: 1773 * struct rt_msghdr; 1774 * struct sockaddr_in; (IPv4) 1775 * struct sockaddr_dl; 1776 */ 1777 arpc.rtm.rtm_msglen = sizeof(arpc); 1778 arpc.rtm.rtm_version = RTM_VERSION; 1779 arpc.rtm.rtm_type = RTM_GET; 1780 arpc.rtm.rtm_flags = RTF_UP; 1781 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 1782 1783 /* publish */ 1784 if (lle->la_flags & LLE_PUB) 1785 arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1786 1787 sdl = &arpc.sdl; 1788 sdl->sdl_family = AF_LINK; 1789 sdl->sdl_len = sizeof(*sdl); 1790 sdl->sdl_index = ifp->if_index; 1791 sdl->sdl_type = ifp->if_type; 1792 if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 1793 sdl->sdl_alen = ifp->if_addrlen; 1794 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1795 } else { 1796 sdl->sdl_alen = 0; 1797 bzero(LLADDR(sdl), ifp->if_addrlen); 1798 } 1799 1800 arpc.rtm.rtm_rmx.rmx_expire = 1801 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1802 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1803 if (lle->la_flags & LLE_STATIC) 1804 arpc.rtm.rtm_flags |= RTF_STATIC; 1805 if (lle->la_flags & LLE_IFADDR) 1806 arpc.rtm.rtm_flags |= RTF_PINNED; 1807 arpc.rtm.rtm_index = ifp->if_index; 1808 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1809 1810 return (error); 1811 } 1812 1813 static void 1814 in_lltable_post_resolved(struct lltable *llt, struct llentry *lle) 1815 { 1816 struct ifnet *ifp = llt->llt_ifp; 1817 1818 /* gratuitous ARP */ 1819 if ((lle->la_flags & LLE_PUB) != 0) 1820 arprequest(ifp, &lle->r_l3addr.addr4, &lle->r_l3addr.addr4, 1821 lle->ll_addr); 1822 } 1823 1824 static struct lltable * 1825 in_lltattach(struct ifnet *ifp) 1826 { 1827 struct lltable *llt; 1828 1829 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE); 1830 llt->llt_af = AF_INET; 1831 llt->llt_ifp = ifp; 1832 1833 llt->llt_lookup = in_lltable_lookup; 1834 llt->llt_alloc_entry = in_lltable_alloc; 1835 llt->llt_delete_entry = in_lltable_delete_entry; 1836 llt->llt_dump_entry = in_lltable_dump_entry; 1837 llt->llt_hash = in_lltable_hash; 1838 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry; 1839 llt->llt_free_entry = in_lltable_free_entry; 1840 llt->llt_match_prefix = in_lltable_match_prefix; 1841 llt->llt_mark_used = llentry_mark_used; 1842 llt->llt_post_resolved = in_lltable_post_resolved; 1843 lltable_link(llt); 1844 1845 return (llt); 1846 } 1847 1848 struct lltable * 1849 in_lltable_get(struct ifnet *ifp) 1850 { 1851 struct lltable *llt = NULL; 1852 1853 void *afdata_ptr = ifp->if_afdata[AF_INET]; 1854 if (afdata_ptr != NULL) 1855 llt = ((struct in_ifinfo *)afdata_ptr)->ii_llt; 1856 return (llt); 1857 } 1858 1859 void * 1860 in_domifattach(struct ifnet *ifp) 1861 { 1862 struct in_ifinfo *ii; 1863 1864 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1865 1866 ii->ii_llt = in_lltattach(ifp); 1867 ii->ii_igmp = igmp_domifattach(ifp); 1868 1869 return (ii); 1870 } 1871 1872 void 1873 in_domifdetach(struct ifnet *ifp, void *aux) 1874 { 1875 struct in_ifinfo *ii = (struct in_ifinfo *)aux; 1876 1877 igmp_domifdetach(ifp); 1878 lltable_free(ii->ii_llt); 1879 free(ii, M_IFADDR); 1880 } 1881