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