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