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