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