1 /*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (C) 2001 WIDE Project. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 4. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * @(#)in.c 8.4 (Berkeley) 1/9/95 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_mpath.h" 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/proc.h> 48 #include <sys/sysctl.h> 49 #include <sys/syslog.h> 50 #include <sys/sx.h> 51 52 #include <net/if.h> 53 #include <net/if_var.h> 54 #include <net/if_arp.h> 55 #include <net/if_dl.h> 56 #include <net/if_llatbl.h> 57 #include <net/if_types.h> 58 #include <net/route.h> 59 #include <net/vnet.h> 60 61 #include <netinet/if_ether.h> 62 #include <netinet/in.h> 63 #include <netinet/in_var.h> 64 #include <netinet/in_pcb.h> 65 #include <netinet/ip_var.h> 66 #include <netinet/ip_carp.h> 67 #include <netinet/igmp_var.h> 68 #include <netinet/udp.h> 69 #include <netinet/udp_var.h> 70 71 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *); 72 static int in_difaddr_ioctl(caddr_t, struct ifnet *, struct thread *); 73 74 static void in_socktrim(struct sockaddr_in *); 75 static void in_purgemaddrs(struct ifnet *); 76 77 static VNET_DEFINE(int, nosameprefix); 78 #define V_nosameprefix VNET(nosameprefix) 79 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_RW, 80 &VNET_NAME(nosameprefix), 0, 81 "Refuse to create same prefixes on different interfaces"); 82 83 VNET_DECLARE(struct inpcbinfo, ripcbinfo); 84 #define V_ripcbinfo VNET(ripcbinfo) 85 86 static struct sx in_control_sx; 87 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control"); 88 89 /* 90 * Return 1 if an internet address is for a ``local'' host 91 * (one to which we have a connection). 92 */ 93 int 94 in_localaddr(struct in_addr in) 95 { 96 register u_long i = ntohl(in.s_addr); 97 register struct in_ifaddr *ia; 98 99 IN_IFADDR_RLOCK(); 100 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 101 if ((i & ia->ia_subnetmask) == ia->ia_subnet) { 102 IN_IFADDR_RUNLOCK(); 103 return (1); 104 } 105 } 106 IN_IFADDR_RUNLOCK(); 107 return (0); 108 } 109 110 /* 111 * Return 1 if an internet address is for the local host and configured 112 * on one of its interfaces. 113 */ 114 int 115 in_localip(struct in_addr in) 116 { 117 struct in_ifaddr *ia; 118 119 IN_IFADDR_RLOCK(); 120 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 121 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) { 122 IN_IFADDR_RUNLOCK(); 123 return (1); 124 } 125 } 126 IN_IFADDR_RUNLOCK(); 127 return (0); 128 } 129 130 /* 131 * Return a reference to the interface address which is different to 132 * the supplied one but with same IP address value. 133 */ 134 static struct in_ifaddr * 135 in_localip_more(struct in_ifaddr *ia) 136 { 137 in_addr_t in = IA_SIN(ia)->sin_addr.s_addr; 138 struct in_ifaddr *it; 139 140 IN_IFADDR_RLOCK(); 141 LIST_FOREACH(it, INADDR_HASH(in), ia_hash) { 142 if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) { 143 ifa_ref(&it->ia_ifa); 144 IN_IFADDR_RUNLOCK(); 145 return (it); 146 } 147 } 148 IN_IFADDR_RUNLOCK(); 149 150 return (NULL); 151 } 152 153 /* 154 * Determine whether an IP address is in a reserved set of addresses 155 * that may not be forwarded, or whether datagrams to that destination 156 * may be forwarded. 157 */ 158 int 159 in_canforward(struct in_addr in) 160 { 161 register u_long i = ntohl(in.s_addr); 162 register u_long net; 163 164 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 165 return (0); 166 if (IN_CLASSA(i)) { 167 net = i & IN_CLASSA_NET; 168 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 169 return (0); 170 } 171 return (1); 172 } 173 174 /* 175 * Trim a mask in a sockaddr 176 */ 177 static void 178 in_socktrim(struct sockaddr_in *ap) 179 { 180 register char *cplim = (char *) &ap->sin_addr; 181 register char *cp = (char *) (&ap->sin_addr + 1); 182 183 ap->sin_len = 0; 184 while (--cp >= cplim) 185 if (*cp) { 186 (ap)->sin_len = cp - (char *) (ap) + 1; 187 break; 188 } 189 } 190 191 /* 192 * Generic internet control operations (ioctl's). 193 */ 194 int 195 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 196 struct thread *td) 197 { 198 struct ifreq *ifr = (struct ifreq *)data; 199 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr; 200 struct ifaddr *ifa; 201 struct in_ifaddr *ia; 202 int error; 203 204 if (ifp == NULL) 205 return (EADDRNOTAVAIL); 206 207 /* 208 * Filter out 4 ioctls we implement directly. Forward the rest 209 * to specific functions and ifp->if_ioctl(). 210 */ 211 switch (cmd) { 212 case SIOCGIFADDR: 213 case SIOCGIFBRDADDR: 214 case SIOCGIFDSTADDR: 215 case SIOCGIFNETMASK: 216 break; 217 case SIOCDIFADDR: 218 sx_xlock(&in_control_sx); 219 error = in_difaddr_ioctl(data, ifp, td); 220 sx_xunlock(&in_control_sx); 221 return (error); 222 case OSIOCAIFADDR: /* 9.x compat */ 223 case SIOCAIFADDR: 224 sx_xlock(&in_control_sx); 225 error = in_aifaddr_ioctl(cmd, data, ifp, td); 226 sx_xunlock(&in_control_sx); 227 return (error); 228 case SIOCSIFADDR: 229 case SIOCSIFBRDADDR: 230 case SIOCSIFDSTADDR: 231 case SIOCSIFNETMASK: 232 /* We no longer support that old commands. */ 233 return (EINVAL); 234 default: 235 if (ifp->if_ioctl == NULL) 236 return (EOPNOTSUPP); 237 return ((*ifp->if_ioctl)(ifp, cmd, data)); 238 } 239 240 if (addr->sin_addr.s_addr != INADDR_ANY && 241 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0) 242 return (EADDRNOTAVAIL); 243 244 /* 245 * For SIOCGIFADDR, pick the first address. For the rest of 246 * ioctls, try to find specified address. 247 */ 248 IF_ADDR_RLOCK(ifp); 249 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 250 if (ifa->ifa_addr->sa_family != AF_INET) 251 continue; 252 ia = (struct in_ifaddr *)ifa; 253 if (cmd == SIOCGIFADDR || addr->sin_addr.s_addr == INADDR_ANY) 254 break; 255 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr) 256 break; 257 } 258 259 if (ifa == NULL) { 260 IF_ADDR_RUNLOCK(ifp); 261 return (EADDRNOTAVAIL); 262 } 263 264 error = 0; 265 switch (cmd) { 266 case SIOCGIFADDR: 267 *addr = ia->ia_addr; 268 break; 269 270 case SIOCGIFBRDADDR: 271 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 272 error = EINVAL; 273 break; 274 } 275 *addr = ia->ia_broadaddr; 276 break; 277 278 case SIOCGIFDSTADDR: 279 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 280 error = EINVAL; 281 break; 282 } 283 *addr = ia->ia_dstaddr; 284 break; 285 286 case SIOCGIFNETMASK: 287 *addr = ia->ia_sockmask; 288 break; 289 } 290 291 IF_ADDR_RUNLOCK(ifp); 292 293 return (error); 294 } 295 296 static int 297 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td) 298 { 299 const struct in_aliasreq *ifra = (struct in_aliasreq *)data; 300 const struct sockaddr_in *addr = &ifra->ifra_addr; 301 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr; 302 const struct sockaddr_in *mask = &ifra->ifra_mask; 303 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr; 304 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0; 305 struct ifaddr *ifa; 306 struct in_ifaddr *ia; 307 bool iaIsFirst; 308 int error = 0; 309 310 error = priv_check(td, PRIV_NET_ADDIFADDR); 311 if (error) 312 return (error); 313 314 /* 315 * ifra_addr must be present and be of INET family. 316 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional. 317 */ 318 if (addr->sin_len != sizeof(struct sockaddr_in) || 319 addr->sin_family != AF_INET) 320 return (EINVAL); 321 if (broadaddr->sin_len != 0 && 322 (broadaddr->sin_len != sizeof(struct sockaddr_in) || 323 broadaddr->sin_family != AF_INET)) 324 return (EINVAL); 325 if (mask->sin_len != 0 && 326 (mask->sin_len != sizeof(struct sockaddr_in) || 327 mask->sin_family != AF_INET)) 328 return (EINVAL); 329 if ((ifp->if_flags & IFF_POINTOPOINT) && 330 (dstaddr->sin_len != sizeof(struct sockaddr_in) || 331 dstaddr->sin_addr.s_addr == INADDR_ANY)) 332 return (EDESTADDRREQ); 333 if (vhid > 0 && carp_attach_p == NULL) 334 return (EPROTONOSUPPORT); 335 336 /* 337 * See whether address already exist. 338 */ 339 iaIsFirst = true; 340 ia = NULL; 341 IF_ADDR_RLOCK(ifp); 342 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 343 struct in_ifaddr *it; 344 345 if (ifa->ifa_addr->sa_family != AF_INET) 346 continue; 347 348 it = (struct in_ifaddr *)ifa; 349 iaIsFirst = false; 350 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 351 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) 352 ia = it; 353 } 354 IF_ADDR_RUNLOCK(ifp); 355 356 if (ia != NULL) 357 (void )in_difaddr_ioctl(data, ifp, td); 358 359 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK); 360 ia = (struct in_ifaddr *)ifa; 361 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 362 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 363 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 364 365 ia->ia_ifp = ifp; 366 ia->ia_ifa.ifa_metric = ifp->if_metric; 367 ia->ia_addr = *addr; 368 if (mask->sin_len != 0) { 369 ia->ia_sockmask = *mask; 370 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 371 } else { 372 in_addr_t i = ntohl(addr->sin_addr.s_addr); 373 374 /* 375 * Be compatible with network classes, if netmask isn't 376 * supplied, guess it based on classes. 377 */ 378 if (IN_CLASSA(i)) 379 ia->ia_subnetmask = IN_CLASSA_NET; 380 else if (IN_CLASSB(i)) 381 ia->ia_subnetmask = IN_CLASSB_NET; 382 else 383 ia->ia_subnetmask = IN_CLASSC_NET; 384 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 385 } 386 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask; 387 in_socktrim(&ia->ia_sockmask); 388 389 if (ifp->if_flags & IFF_BROADCAST) { 390 if (broadaddr->sin_len != 0) { 391 ia->ia_broadaddr = *broadaddr; 392 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) { 393 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 394 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 395 ia->ia_broadaddr.sin_family = AF_INET; 396 } else { 397 ia->ia_broadaddr.sin_addr.s_addr = 398 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 399 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in); 400 ia->ia_broadaddr.sin_family = AF_INET; 401 } 402 } 403 404 if (ifp->if_flags & IFF_POINTOPOINT) 405 ia->ia_dstaddr = *dstaddr; 406 407 /* XXXGL: rtinit() needs this strange assignment. */ 408 if (ifp->if_flags & IFF_LOOPBACK) 409 ia->ia_dstaddr = ia->ia_addr; 410 411 /* if_addrhead is already referenced by ifa_alloc() */ 412 IF_ADDR_WLOCK(ifp); 413 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 414 IF_ADDR_WUNLOCK(ifp); 415 416 ifa_ref(ifa); /* in_ifaddrhead */ 417 IN_IFADDR_WLOCK(); 418 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 419 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 420 IN_IFADDR_WUNLOCK(); 421 422 if (vhid != 0) 423 error = (*carp_attach_p)(&ia->ia_ifa, vhid); 424 if (error) 425 goto fail1; 426 427 /* 428 * Give the interface a chance to initialize 429 * if this is its first address, 430 * and to validate the address if necessary. 431 */ 432 if (ifp->if_ioctl != NULL) 433 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 434 if (error) 435 goto fail2; 436 437 /* 438 * Add route for the network. 439 */ 440 if (vhid == 0) { 441 int flags = RTF_UP; 442 443 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 444 flags |= RTF_HOST; 445 446 error = in_addprefix(ia, flags); 447 if (error) 448 goto fail2; 449 } 450 451 /* 452 * Add a loopback route to self. 453 */ 454 if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 && 455 ia->ia_addr.sin_addr.s_addr != INADDR_ANY && 456 !((ifp->if_flags & IFF_POINTOPOINT) && 457 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) { 458 struct in_ifaddr *eia; 459 460 eia = in_localip_more(ia); 461 462 if (eia == NULL) { 463 error = ifa_add_loopback_route((struct ifaddr *)ia, 464 (struct sockaddr *)&ia->ia_addr); 465 if (error) 466 goto fail3; 467 } else 468 ifa_free(&eia->ia_ifa); 469 } 470 471 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) { 472 struct in_addr allhosts_addr; 473 struct in_ifinfo *ii; 474 475 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 476 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 477 478 error = in_joingroup(ifp, &allhosts_addr, NULL, 479 &ii->ii_allhosts); 480 } 481 482 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 483 484 return (error); 485 486 fail3: 487 if (vhid == 0) 488 (void )in_scrubprefix(ia, LLE_STATIC); 489 490 fail2: 491 if (ia->ia_ifa.ifa_carp) 492 (*carp_detach_p)(&ia->ia_ifa); 493 494 fail1: 495 IF_ADDR_WLOCK(ifp); 496 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 497 IF_ADDR_WUNLOCK(ifp); 498 ifa_free(&ia->ia_ifa); /* if_addrhead */ 499 500 IN_IFADDR_WLOCK(); 501 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 502 LIST_REMOVE(ia, ia_hash); 503 IN_IFADDR_WUNLOCK(); 504 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 505 506 return (error); 507 } 508 509 static int 510 in_difaddr_ioctl(caddr_t data, struct ifnet *ifp, struct thread *td) 511 { 512 const struct ifreq *ifr = (struct ifreq *)data; 513 const struct sockaddr_in *addr = (const struct sockaddr_in *) 514 &ifr->ifr_addr; 515 struct ifaddr *ifa; 516 struct in_ifaddr *ia; 517 bool deleteAny, iaIsLast; 518 int error; 519 520 if (td != NULL) { 521 error = priv_check(td, PRIV_NET_DELIFADDR); 522 if (error) 523 return (error); 524 } 525 526 if (addr->sin_len != sizeof(struct sockaddr_in) || 527 addr->sin_family != AF_INET) 528 deleteAny = true; 529 else 530 deleteAny = false; 531 532 iaIsLast = true; 533 ia = NULL; 534 IF_ADDR_WLOCK(ifp); 535 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 536 struct in_ifaddr *it; 537 538 if (ifa->ifa_addr->sa_family != AF_INET) 539 continue; 540 541 it = (struct in_ifaddr *)ifa; 542 if (deleteAny && ia == NULL && (td == NULL || 543 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0)) 544 ia = it; 545 546 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr && 547 (td == NULL || prison_check_ip4(td->td_ucred, 548 &addr->sin_addr) == 0)) 549 ia = it; 550 551 if (it != ia) 552 iaIsLast = false; 553 } 554 555 if (ia == NULL) { 556 IF_ADDR_WUNLOCK(ifp); 557 return (EADDRNOTAVAIL); 558 } 559 560 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 561 IF_ADDR_WUNLOCK(ifp); 562 ifa_free(&ia->ia_ifa); /* if_addrhead */ 563 564 IN_IFADDR_WLOCK(); 565 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 566 LIST_REMOVE(ia, ia_hash); 567 IN_IFADDR_WUNLOCK(); 568 569 /* 570 * in_scrubprefix() kills the interface route. 571 */ 572 in_scrubprefix(ia, LLE_STATIC); 573 574 /* 575 * in_ifadown gets rid of all the rest of 576 * the routes. This is not quite the right 577 * thing to do, but at least if we are running 578 * a routing process they will come back. 579 */ 580 in_ifadown(&ia->ia_ifa, 1); 581 582 if (ia->ia_ifa.ifa_carp) 583 (*carp_detach_p)(&ia->ia_ifa); 584 585 /* 586 * If this is the last IPv4 address configured on this 587 * interface, leave the all-hosts group. 588 * No state-change report need be transmitted. 589 */ 590 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) { 591 struct in_ifinfo *ii; 592 593 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 594 IN_MULTI_LOCK(); 595 if (ii->ii_allhosts) { 596 (void)in_leavegroup_locked(ii->ii_allhosts, NULL); 597 ii->ii_allhosts = NULL; 598 } 599 IN_MULTI_UNLOCK(); 600 } 601 602 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 603 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 604 605 return (0); 606 } 607 608 #define rtinitflags(x) \ 609 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 610 ? RTF_HOST : 0) 611 612 /* 613 * Check if we have a route for the given prefix already or add one accordingly. 614 */ 615 int 616 in_addprefix(struct in_ifaddr *target, int flags) 617 { 618 struct in_ifaddr *ia; 619 struct in_addr prefix, mask, p, m; 620 int error, fibnum; 621 622 if ((flags & RTF_HOST) != 0) { 623 prefix = target->ia_dstaddr.sin_addr; 624 mask.s_addr = 0; 625 } else { 626 prefix = target->ia_addr.sin_addr; 627 mask = target->ia_sockmask.sin_addr; 628 prefix.s_addr &= mask.s_addr; 629 } 630 631 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS : target->ia_ifp->if_fib; 632 633 IN_IFADDR_RLOCK(); 634 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 635 if (rtinitflags(ia)) { 636 p = ia->ia_dstaddr.sin_addr; 637 638 if (prefix.s_addr != p.s_addr) 639 continue; 640 } else { 641 p = ia->ia_addr.sin_addr; 642 m = ia->ia_sockmask.sin_addr; 643 p.s_addr &= m.s_addr; 644 645 if (prefix.s_addr != p.s_addr || 646 mask.s_addr != m.s_addr) 647 continue; 648 } 649 650 /* 651 * If we got a matching prefix route inserted by other 652 * interface address, we are done here. 653 */ 654 if (ia->ia_flags & IFA_ROUTE) { 655 #ifdef RADIX_MPATH 656 if (ia->ia_addr.sin_addr.s_addr == 657 target->ia_addr.sin_addr.s_addr) { 658 IN_IFADDR_RUNLOCK(); 659 return (EEXIST); 660 } else 661 break; 662 #endif 663 if (V_nosameprefix) { 664 IN_IFADDR_RUNLOCK(); 665 return (EEXIST); 666 } else { 667 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum); 668 IN_IFADDR_RUNLOCK(); 669 return (0); 670 } 671 } 672 } 673 IN_IFADDR_RUNLOCK(); 674 675 /* 676 * No-one seem to have this prefix route, so we try to insert it. 677 */ 678 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 679 if (!error) 680 target->ia_flags |= IFA_ROUTE; 681 return (error); 682 } 683 684 /* 685 * If there is no other address in the system that can serve a route to the 686 * same prefix, remove the route. Hand over the route to the new address 687 * otherwise. 688 */ 689 int 690 in_scrubprefix(struct in_ifaddr *target, u_int flags) 691 { 692 struct in_ifaddr *ia; 693 struct in_addr prefix, mask, p, m; 694 int error = 0, fibnum; 695 struct sockaddr_in prefix0, mask0; 696 697 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS : target->ia_ifp->if_fib; 698 699 /* 700 * Remove the loopback route to the interface address. 701 */ 702 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) && 703 !(target->ia_ifp->if_flags & IFF_LOOPBACK) && 704 (flags & LLE_STATIC)) { 705 struct in_ifaddr *eia; 706 707 eia = in_localip_more(target); 708 709 if (eia != NULL) { 710 error = ifa_switch_loopback_route((struct ifaddr *)eia, 711 (struct sockaddr *)&target->ia_addr); 712 ifa_free(&eia->ia_ifa); 713 } else { 714 error = ifa_del_loopback_route((struct ifaddr *)target, 715 (struct sockaddr *)&target->ia_addr); 716 } 717 718 if (!(target->ia_ifp->if_flags & IFF_NOARP)) 719 /* remove arp cache */ 720 arp_ifscrub(target->ia_ifp, 721 IA_SIN(target)->sin_addr.s_addr); 722 } 723 724 if (rtinitflags(target)) { 725 prefix = target->ia_dstaddr.sin_addr; 726 mask.s_addr = 0; 727 } else { 728 prefix = target->ia_addr.sin_addr; 729 mask = target->ia_sockmask.sin_addr; 730 prefix.s_addr &= mask.s_addr; 731 } 732 733 if ((target->ia_flags & IFA_ROUTE) == 0) { 734 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum); 735 return (0); 736 } 737 738 IN_IFADDR_RLOCK(); 739 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 740 if (rtinitflags(ia)) { 741 p = ia->ia_dstaddr.sin_addr; 742 743 if (prefix.s_addr != p.s_addr) 744 continue; 745 } else { 746 p = ia->ia_addr.sin_addr; 747 m = ia->ia_sockmask.sin_addr; 748 p.s_addr &= m.s_addr; 749 750 if (prefix.s_addr != p.s_addr || 751 mask.s_addr != m.s_addr) 752 continue; 753 } 754 755 if ((ia->ia_ifp->if_flags & IFF_UP) == 0) 756 continue; 757 758 /* 759 * If we got a matching prefix address, move IFA_ROUTE and 760 * the route itself to it. Make sure that routing daemons 761 * get a heads-up. 762 */ 763 if ((ia->ia_flags & IFA_ROUTE) == 0) { 764 ifa_ref(&ia->ia_ifa); 765 IN_IFADDR_RUNLOCK(); 766 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, 767 rtinitflags(target)); 768 if (error == 0) 769 target->ia_flags &= ~IFA_ROUTE; 770 else 771 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n", 772 error); 773 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 774 rtinitflags(ia) | RTF_UP); 775 if (error == 0) 776 ia->ia_flags |= IFA_ROUTE; 777 else 778 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n", 779 error); 780 ifa_free(&ia->ia_ifa); 781 return (error); 782 } 783 } 784 IN_IFADDR_RUNLOCK(); 785 786 /* 787 * remove all L2 entries on the given prefix 788 */ 789 bzero(&prefix0, sizeof(prefix0)); 790 prefix0.sin_len = sizeof(prefix0); 791 prefix0.sin_family = AF_INET; 792 prefix0.sin_addr.s_addr = target->ia_subnet; 793 bzero(&mask0, sizeof(mask0)); 794 mask0.sin_len = sizeof(mask0); 795 mask0.sin_family = AF_INET; 796 mask0.sin_addr.s_addr = target->ia_subnetmask; 797 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0, 798 (struct sockaddr *)&mask0, flags); 799 800 /* 801 * As no-one seem to have this prefix, we can remove the route. 802 */ 803 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 804 if (error == 0) 805 target->ia_flags &= ~IFA_ROUTE; 806 else 807 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error); 808 return (error); 809 } 810 811 #undef rtinitflags 812 813 /* 814 * Return 1 if the address might be a local broadcast address. 815 */ 816 int 817 in_broadcast(struct in_addr in, struct ifnet *ifp) 818 { 819 register struct ifaddr *ifa; 820 u_long t; 821 822 if (in.s_addr == INADDR_BROADCAST || 823 in.s_addr == INADDR_ANY) 824 return (1); 825 if ((ifp->if_flags & IFF_BROADCAST) == 0) 826 return (0); 827 t = ntohl(in.s_addr); 828 /* 829 * Look through the list of addresses for a match 830 * with a broadcast address. 831 */ 832 #define ia ((struct in_ifaddr *)ifa) 833 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 834 if (ifa->ifa_addr->sa_family == AF_INET && 835 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 836 /* 837 * Check for old-style (host 0) broadcast, but 838 * taking into account that RFC 3021 obsoletes it. 839 */ 840 (ia->ia_subnetmask != IN_RFC3021_MASK && 841 t == ia->ia_subnet)) && 842 /* 843 * Check for an all one subnetmask. These 844 * only exist when an interface gets a secondary 845 * address. 846 */ 847 ia->ia_subnetmask != (u_long)0xffffffff) 848 return (1); 849 return (0); 850 #undef ia 851 } 852 853 /* 854 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 855 */ 856 void 857 in_ifdetach(struct ifnet *ifp) 858 { 859 860 in_pcbpurgeif0(&V_ripcbinfo, ifp); 861 in_pcbpurgeif0(&V_udbinfo, ifp); 862 in_pcbpurgeif0(&V_ulitecbinfo, ifp); 863 in_purgemaddrs(ifp); 864 } 865 866 /* 867 * Delete all IPv4 multicast address records, and associated link-layer 868 * multicast address records, associated with ifp. 869 * XXX It looks like domifdetach runs AFTER the link layer cleanup. 870 * XXX This should not race with ifma_protospec being set during 871 * a new allocation, if it does, we have bigger problems. 872 */ 873 static void 874 in_purgemaddrs(struct ifnet *ifp) 875 { 876 LIST_HEAD(,in_multi) purgeinms; 877 struct in_multi *inm, *tinm; 878 struct ifmultiaddr *ifma; 879 880 LIST_INIT(&purgeinms); 881 IN_MULTI_LOCK(); 882 883 /* 884 * Extract list of in_multi associated with the detaching ifp 885 * which the PF_INET layer is about to release. 886 * We need to do this as IF_ADDR_LOCK() may be re-acquired 887 * by code further down. 888 */ 889 IF_ADDR_RLOCK(ifp); 890 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 891 if (ifma->ifma_addr->sa_family != AF_INET || 892 ifma->ifma_protospec == NULL) 893 continue; 894 #if 0 895 KASSERT(ifma->ifma_protospec != NULL, 896 ("%s: ifma_protospec is NULL", __func__)); 897 #endif 898 inm = (struct in_multi *)ifma->ifma_protospec; 899 LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 900 } 901 IF_ADDR_RUNLOCK(ifp); 902 903 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 904 LIST_REMOVE(inm, inm_link); 905 inm_release_locked(inm); 906 } 907 igmp_ifdetach(ifp); 908 909 IN_MULTI_UNLOCK(); 910 } 911 912 struct in_llentry { 913 struct llentry base; 914 struct sockaddr_in l3_addr4; 915 }; 916 917 /* 918 * Deletes an address from the address table. 919 * This function is called by the timer functions 920 * such as arptimer() and nd6_llinfo_timer(), and 921 * the caller does the locking. 922 */ 923 static void 924 in_lltable_free(struct lltable *llt, struct llentry *lle) 925 { 926 LLE_WUNLOCK(lle); 927 LLE_LOCK_DESTROY(lle); 928 free(lle, M_LLTABLE); 929 } 930 931 static struct llentry * 932 in_lltable_new(const struct sockaddr *l3addr, u_int flags) 933 { 934 struct in_llentry *lle; 935 936 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 937 if (lle == NULL) /* NB: caller generates msg */ 938 return NULL; 939 940 /* 941 * For IPv4 this will trigger "arpresolve" to generate 942 * an ARP request. 943 */ 944 lle->base.la_expire = time_uptime; /* mark expired */ 945 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 946 lle->base.lle_refcnt = 1; 947 lle->base.lle_free = in_lltable_free; 948 LLE_LOCK_INIT(&lle->base); 949 callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock, 950 CALLOUT_RETURNUNLOCKED); 951 952 return (&lle->base); 953 } 954 955 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 956 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 957 958 static void 959 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix, 960 const struct sockaddr *mask, u_int flags) 961 { 962 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 963 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 964 struct llentry *lle, *next; 965 int i; 966 size_t pkts_dropped; 967 968 IF_AFDATA_WLOCK(llt->llt_ifp); 969 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 970 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 971 /* 972 * (flags & LLE_STATIC) means deleting all entries 973 * including static ARP entries. 974 */ 975 if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)), 976 pfx, msk) && ((flags & LLE_STATIC) || 977 !(lle->la_flags & LLE_STATIC))) { 978 LLE_WLOCK(lle); 979 if (callout_stop(&lle->la_timer)) 980 LLE_REMREF(lle); 981 pkts_dropped = llentry_free(lle); 982 ARPSTAT_ADD(dropped, pkts_dropped); 983 } 984 } 985 } 986 IF_AFDATA_WUNLOCK(llt->llt_ifp); 987 } 988 989 990 static int 991 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 992 { 993 struct rtentry *rt; 994 995 KASSERT(l3addr->sa_family == AF_INET, 996 ("sin_family %d", l3addr->sa_family)); 997 998 /* XXX rtalloc1_fib should take a const param */ 999 rt = rtalloc1_fib(__DECONST(struct sockaddr *, l3addr), 0, 0, 1000 ifp->if_fib); 1001 1002 if (rt == NULL) 1003 return (EINVAL); 1004 1005 /* 1006 * If the gateway for an existing host route matches the target L3 1007 * address, which is a special route inserted by some implementation 1008 * such as MANET, and the interface is of the correct type, then 1009 * allow for ARP to proceed. 1010 */ 1011 if (rt->rt_flags & RTF_GATEWAY) { 1012 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp || 1013 rt->rt_ifp->if_type != IFT_ETHER || 1014 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 || 1015 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 1016 sizeof(in_addr_t)) != 0) { 1017 RTFREE_LOCKED(rt); 1018 return (EINVAL); 1019 } 1020 } 1021 1022 /* 1023 * Make sure that at least the destination address is covered 1024 * by the route. This is for handling the case where 2 or more 1025 * interfaces have the same prefix. An incoming packet arrives 1026 * on one interface and the corresponding outgoing packet leaves 1027 * another interface. 1028 */ 1029 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) { 1030 const char *sa, *mask, *addr, *lim; 1031 int len; 1032 1033 mask = (const char *)rt_mask(rt); 1034 /* 1035 * Just being extra cautious to avoid some custom 1036 * code getting into trouble. 1037 */ 1038 if (mask == NULL) { 1039 RTFREE_LOCKED(rt); 1040 return (EINVAL); 1041 } 1042 1043 sa = (const char *)rt_key(rt); 1044 addr = (const char *)l3addr; 1045 len = ((const struct sockaddr_in *)l3addr)->sin_len; 1046 lim = addr + len; 1047 1048 for ( ; addr < lim; sa++, mask++, addr++) { 1049 if ((*sa ^ *addr) & *mask) { 1050 #ifdef DIAGNOSTIC 1051 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 1052 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1053 #endif 1054 RTFREE_LOCKED(rt); 1055 return (EINVAL); 1056 } 1057 } 1058 } 1059 1060 RTFREE_LOCKED(rt); 1061 return (0); 1062 } 1063 1064 /* 1065 * Return NULL if not found or marked for deletion. 1066 * If found return lle read locked. 1067 */ 1068 static struct llentry * 1069 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1070 { 1071 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1072 struct ifnet *ifp = llt->llt_ifp; 1073 struct llentry *lle; 1074 struct llentries *lleh; 1075 u_int hashkey; 1076 1077 IF_AFDATA_LOCK_ASSERT(ifp); 1078 KASSERT(l3addr->sa_family == AF_INET, 1079 ("sin_family %d", l3addr->sa_family)); 1080 1081 hashkey = sin->sin_addr.s_addr; 1082 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 1083 LIST_FOREACH(lle, lleh, lle_next) { 1084 struct sockaddr_in *sa2 = satosin(L3_ADDR(lle)); 1085 if (lle->la_flags & LLE_DELETED) 1086 continue; 1087 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 1088 break; 1089 } 1090 if (lle == NULL) { 1091 #ifdef DIAGNOSTIC 1092 if (flags & LLE_DELETE) 1093 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 1094 #endif 1095 if (!(flags & LLE_CREATE)) 1096 return (NULL); 1097 IF_AFDATA_WLOCK_ASSERT(ifp); 1098 /* 1099 * A route that covers the given address must have 1100 * been installed 1st because we are doing a resolution, 1101 * verify this. 1102 */ 1103 if (!(flags & LLE_IFADDR) && 1104 in_lltable_rtcheck(ifp, flags, l3addr) != 0) 1105 goto done; 1106 1107 lle = in_lltable_new(l3addr, flags); 1108 if (lle == NULL) { 1109 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1110 goto done; 1111 } 1112 lle->la_flags = flags & ~LLE_CREATE; 1113 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 1114 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 1115 lle->la_flags |= (LLE_VALID | LLE_STATIC); 1116 } 1117 1118 lle->lle_tbl = llt; 1119 lle->lle_head = lleh; 1120 lle->la_flags |= LLE_LINKED; 1121 LIST_INSERT_HEAD(lleh, lle, lle_next); 1122 } else if (flags & LLE_DELETE) { 1123 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 1124 LLE_WLOCK(lle); 1125 lle->la_flags |= LLE_DELETED; 1126 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 1127 #ifdef DIAGNOSTIC 1128 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1129 #endif 1130 if ((lle->la_flags & 1131 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) 1132 llentry_free(lle); 1133 else 1134 LLE_WUNLOCK(lle); 1135 } 1136 lle = (void *)-1; 1137 1138 } 1139 if (LLE_IS_VALID(lle)) { 1140 if (flags & LLE_EXCLUSIVE) 1141 LLE_WLOCK(lle); 1142 else 1143 LLE_RLOCK(lle); 1144 } 1145 done: 1146 return (lle); 1147 } 1148 1149 static int 1150 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 1151 { 1152 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 1153 struct ifnet *ifp = llt->llt_ifp; 1154 struct llentry *lle; 1155 /* XXX stack use */ 1156 struct { 1157 struct rt_msghdr rtm; 1158 struct sockaddr_in sin; 1159 struct sockaddr_dl sdl; 1160 } arpc; 1161 int error, i; 1162 1163 LLTABLE_LOCK_ASSERT(); 1164 1165 error = 0; 1166 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1167 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 1168 struct sockaddr_dl *sdl; 1169 1170 /* skip deleted entries */ 1171 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 1172 continue; 1173 /* Skip if jailed and not a valid IP of the prison. */ 1174 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1175 continue; 1176 /* 1177 * produce a msg made of: 1178 * struct rt_msghdr; 1179 * struct sockaddr_in; (IPv4) 1180 * struct sockaddr_dl; 1181 */ 1182 bzero(&arpc, sizeof(arpc)); 1183 arpc.rtm.rtm_msglen = sizeof(arpc); 1184 arpc.rtm.rtm_version = RTM_VERSION; 1185 arpc.rtm.rtm_type = RTM_GET; 1186 arpc.rtm.rtm_flags = RTF_UP; 1187 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 1188 arpc.sin.sin_family = AF_INET; 1189 arpc.sin.sin_len = sizeof(arpc.sin); 1190 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 1191 1192 /* publish */ 1193 if (lle->la_flags & LLE_PUB) 1194 arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1195 1196 sdl = &arpc.sdl; 1197 sdl->sdl_family = AF_LINK; 1198 sdl->sdl_len = sizeof(*sdl); 1199 sdl->sdl_index = ifp->if_index; 1200 sdl->sdl_type = ifp->if_type; 1201 if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 1202 sdl->sdl_alen = ifp->if_addrlen; 1203 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1204 } else { 1205 sdl->sdl_alen = 0; 1206 bzero(LLADDR(sdl), ifp->if_addrlen); 1207 } 1208 1209 arpc.rtm.rtm_rmx.rmx_expire = 1210 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1211 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1212 if (lle->la_flags & LLE_STATIC) 1213 arpc.rtm.rtm_flags |= RTF_STATIC; 1214 arpc.rtm.rtm_index = ifp->if_index; 1215 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1216 if (error) 1217 break; 1218 } 1219 } 1220 return error; 1221 #undef SIN 1222 } 1223 1224 void * 1225 in_domifattach(struct ifnet *ifp) 1226 { 1227 struct in_ifinfo *ii; 1228 struct lltable *llt; 1229 1230 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1231 1232 llt = lltable_init(ifp, AF_INET); 1233 if (llt != NULL) { 1234 llt->llt_prefix_free = in_lltable_prefix_free; 1235 llt->llt_lookup = in_lltable_lookup; 1236 llt->llt_dump = in_lltable_dump; 1237 } 1238 ii->ii_llt = llt; 1239 1240 ii->ii_igmp = igmp_domifattach(ifp); 1241 1242 return ii; 1243 } 1244 1245 void 1246 in_domifdetach(struct ifnet *ifp, void *aux) 1247 { 1248 struct in_ifinfo *ii = (struct in_ifinfo *)aux; 1249 1250 igmp_domifdetach(ifp); 1251 lltable_free(ii->ii_llt); 1252 free(ii, M_IFADDR); 1253 } 1254