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