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