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