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