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/systm.h> 40 #include <sys/sockio.h> 41 #include <sys/malloc.h> 42 #include <sys/priv.h> 43 #include <sys/socket.h> 44 #include <sys/jail.h> 45 #include <sys/kernel.h> 46 #include <sys/proc.h> 47 #include <sys/sysctl.h> 48 #include <sys/syslog.h> 49 50 #include <net/if.h> 51 #include <net/if_var.h> 52 #include <net/if_arp.h> 53 #include <net/if_dl.h> 54 #include <net/if_llatbl.h> 55 #include <net/if_types.h> 56 #include <net/route.h> 57 #include <net/vnet.h> 58 59 #include <netinet/if_ether.h> 60 #include <netinet/in.h> 61 #include <netinet/in_var.h> 62 #include <netinet/in_pcb.h> 63 #include <netinet/ip_var.h> 64 #include <netinet/ip_carp.h> 65 #include <netinet/igmp_var.h> 66 #include <netinet/udp.h> 67 #include <netinet/udp_var.h> 68 69 static int in_mask2len(struct in_addr *); 70 static void in_len2mask(struct in_addr *, int); 71 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 72 struct ifnet *, struct thread *); 73 74 static void in_socktrim(struct sockaddr_in *); 75 static int in_ifinit(struct ifnet *, struct in_ifaddr *, 76 struct sockaddr_in *, int, int); 77 static void in_purgemaddrs(struct ifnet *); 78 79 static VNET_DEFINE(int, nosameprefix); 80 #define V_nosameprefix VNET(nosameprefix) 81 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, 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 /* 89 * Return 1 if an internet address is for a ``local'' host 90 * (one to which we have a connection). 91 */ 92 int 93 in_localaddr(struct in_addr in) 94 { 95 register u_long i = ntohl(in.s_addr); 96 register struct in_ifaddr *ia; 97 98 IN_IFADDR_RLOCK(); 99 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 100 if ((i & ia->ia_subnetmask) == ia->ia_subnet) { 101 IN_IFADDR_RUNLOCK(); 102 return (1); 103 } 104 } 105 IN_IFADDR_RUNLOCK(); 106 return (0); 107 } 108 109 /* 110 * Return 1 if an internet address is for the local host and configured 111 * on one of its interfaces. 112 */ 113 int 114 in_localip(struct in_addr in) 115 { 116 struct in_ifaddr *ia; 117 118 IN_IFADDR_RLOCK(); 119 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 120 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) { 121 IN_IFADDR_RUNLOCK(); 122 return (1); 123 } 124 } 125 IN_IFADDR_RUNLOCK(); 126 return (0); 127 } 128 129 /* 130 * Determine whether an IP address is in a reserved set of addresses 131 * that may not be forwarded, or whether datagrams to that destination 132 * may be forwarded. 133 */ 134 int 135 in_canforward(struct in_addr in) 136 { 137 register u_long i = ntohl(in.s_addr); 138 register u_long net; 139 140 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 141 return (0); 142 if (IN_CLASSA(i)) { 143 net = i & IN_CLASSA_NET; 144 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 145 return (0); 146 } 147 return (1); 148 } 149 150 /* 151 * Trim a mask in a sockaddr 152 */ 153 static void 154 in_socktrim(struct sockaddr_in *ap) 155 { 156 register char *cplim = (char *) &ap->sin_addr; 157 register char *cp = (char *) (&ap->sin_addr + 1); 158 159 ap->sin_len = 0; 160 while (--cp >= cplim) 161 if (*cp) { 162 (ap)->sin_len = cp - (char *) (ap) + 1; 163 break; 164 } 165 } 166 167 static int 168 in_mask2len(mask) 169 struct in_addr *mask; 170 { 171 int x, y; 172 u_char *p; 173 174 p = (u_char *)mask; 175 for (x = 0; x < sizeof(*mask); x++) { 176 if (p[x] != 0xff) 177 break; 178 } 179 y = 0; 180 if (x < sizeof(*mask)) { 181 for (y = 0; y < 8; y++) { 182 if ((p[x] & (0x80 >> y)) == 0) 183 break; 184 } 185 } 186 return (x * 8 + y); 187 } 188 189 static void 190 in_len2mask(struct in_addr *mask, int len) 191 { 192 int i; 193 u_char *p; 194 195 p = (u_char *)mask; 196 bzero(mask, sizeof(*mask)); 197 for (i = 0; i < len / 8; i++) 198 p[i] = 0xff; 199 if (len % 8) 200 p[i] = (0xff00 >> (len % 8)) & 0xff; 201 } 202 203 /* 204 * Generic internet control operations (ioctl's). 205 * 206 * ifp is NULL if not an interface-specific ioctl. 207 */ 208 /* ARGSUSED */ 209 int 210 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp, 211 struct thread *td) 212 { 213 register struct ifreq *ifr = (struct ifreq *)data; 214 register struct in_ifaddr *ia, *iap; 215 register struct ifaddr *ifa; 216 struct in_addr allhosts_addr; 217 struct in_addr dst; 218 struct in_ifinfo *ii; 219 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 220 int error, hostIsNew, iaIsNew, maskIsNew; 221 int iaIsFirst; 222 u_long ocmd = cmd; 223 224 /* 225 * Pre-10.x compat: OSIOCAIFADDR passes a shorter 226 * struct in_aliasreq, without ifra_vhid. 227 */ 228 if (cmd == OSIOCAIFADDR) 229 cmd = SIOCAIFADDR; 230 231 ia = NULL; 232 iaIsFirst = 0; 233 iaIsNew = 0; 234 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 235 236 /* 237 * Filter out ioctls we implement directly; forward the rest on to 238 * in_lifaddr_ioctl() and ifp->if_ioctl(). 239 */ 240 switch (cmd) { 241 case SIOCGIFADDR: 242 case SIOCGIFBRDADDR: 243 case SIOCGIFDSTADDR: 244 case SIOCGIFNETMASK: 245 case SIOCDIFADDR: 246 break; 247 case SIOCAIFADDR: 248 /* 249 * ifra_addr must be present and be of INET family. 250 * ifra_broadaddr and ifra_mask are optional. 251 */ 252 if (ifra->ifra_addr.sin_len != sizeof(struct sockaddr_in) || 253 ifra->ifra_addr.sin_family != AF_INET) 254 return (EINVAL); 255 if (ifra->ifra_broadaddr.sin_len != 0 && 256 (ifra->ifra_broadaddr.sin_len != 257 sizeof(struct sockaddr_in) || 258 ifra->ifra_broadaddr.sin_family != AF_INET)) 259 return (EINVAL); 260 #if 0 261 /* 262 * ifconfig(8) in pre-10.x doesn't set sin_family for the 263 * mask. The code is disabled for the 10.x timeline, to 264 * make SIOCAIFADDR compatible with 9.x ifconfig(8). 265 * The code should be enabled in 11.x 266 */ 267 if (ifra->ifra_mask.sin_len != 0 && 268 (ifra->ifra_mask.sin_len != sizeof(struct sockaddr_in) || 269 ifra->ifra_mask.sin_family != AF_INET)) 270 return (EINVAL); 271 #endif 272 break; 273 case SIOCSIFADDR: 274 case SIOCSIFBRDADDR: 275 case SIOCSIFDSTADDR: 276 case SIOCSIFNETMASK: 277 /* We no longer support that old commands. */ 278 return (EINVAL); 279 280 case SIOCALIFADDR: 281 if (td != NULL) { 282 error = priv_check(td, PRIV_NET_ADDIFADDR); 283 if (error) 284 return (error); 285 } 286 if (ifp == NULL) 287 return (EINVAL); 288 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 289 290 case SIOCDLIFADDR: 291 if (td != NULL) { 292 error = priv_check(td, PRIV_NET_DELIFADDR); 293 if (error) 294 return (error); 295 } 296 if (ifp == NULL) 297 return (EINVAL); 298 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 299 300 case SIOCGLIFADDR: 301 if (ifp == NULL) 302 return (EINVAL); 303 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 304 305 default: 306 if (ifp == NULL || ifp->if_ioctl == NULL) 307 return (EOPNOTSUPP); 308 return ((*ifp->if_ioctl)(ifp, cmd, data)); 309 } 310 311 if (ifp == NULL) 312 return (EADDRNOTAVAIL); 313 314 /* 315 * Security checks before we get involved in any work. 316 */ 317 switch (cmd) { 318 case SIOCAIFADDR: 319 if (td != NULL) { 320 error = priv_check(td, PRIV_NET_ADDIFADDR); 321 if (error) 322 return (error); 323 } 324 break; 325 326 case SIOCDIFADDR: 327 if (td != NULL) { 328 error = priv_check(td, PRIV_NET_DELIFADDR); 329 if (error) 330 return (error); 331 } 332 break; 333 } 334 335 /* 336 * Find address for this interface, if it exists. 337 * 338 * If an alias address was specified, find that one instead of the 339 * first one on the interface, if possible. 340 */ 341 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 342 IN_IFADDR_RLOCK(); 343 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) { 344 if (iap->ia_ifp == ifp && 345 iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 346 if (td == NULL || prison_check_ip4(td->td_ucred, 347 &dst) == 0) 348 ia = iap; 349 break; 350 } 351 } 352 if (ia != NULL) 353 ifa_ref(&ia->ia_ifa); 354 IN_IFADDR_RUNLOCK(); 355 if (ia == NULL) { 356 IF_ADDR_RLOCK(ifp); 357 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 358 iap = ifatoia(ifa); 359 if (iap->ia_addr.sin_family == AF_INET) { 360 if (td != NULL && 361 prison_check_ip4(td->td_ucred, 362 &iap->ia_addr.sin_addr) != 0) 363 continue; 364 ia = iap; 365 break; 366 } 367 } 368 if (ia != NULL) 369 ifa_ref(&ia->ia_ifa); 370 IF_ADDR_RUNLOCK(ifp); 371 } 372 if (ia == NULL) 373 iaIsFirst = 1; 374 375 error = 0; 376 switch (cmd) { 377 case SIOCAIFADDR: 378 case SIOCDIFADDR: 379 if (ifra->ifra_addr.sin_family == AF_INET) { 380 struct in_ifaddr *oia; 381 382 IN_IFADDR_RLOCK(); 383 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 384 if (ia->ia_ifp == ifp && 385 ia->ia_addr.sin_addr.s_addr == 386 ifra->ifra_addr.sin_addr.s_addr) 387 break; 388 } 389 if (ia != NULL && ia != oia) 390 ifa_ref(&ia->ia_ifa); 391 if (oia != NULL && ia != oia) 392 ifa_free(&oia->ia_ifa); 393 IN_IFADDR_RUNLOCK(); 394 if ((ifp->if_flags & IFF_POINTOPOINT) 395 && (cmd == SIOCAIFADDR) 396 && (ifra->ifra_dstaddr.sin_addr.s_addr 397 == INADDR_ANY)) { 398 error = EDESTADDRREQ; 399 goto out; 400 } 401 } 402 if (cmd == SIOCDIFADDR && ia == NULL) { 403 error = EADDRNOTAVAIL; 404 goto out; 405 } 406 if (ia == NULL) { 407 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK); 408 ia = (struct in_ifaddr *)ifa; 409 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 410 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 411 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 412 413 ia->ia_sockmask.sin_len = 8; 414 ia->ia_sockmask.sin_family = AF_INET; 415 if (ifp->if_flags & IFF_BROADCAST) { 416 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 417 ia->ia_broadaddr.sin_family = AF_INET; 418 } 419 ia->ia_ifp = ifp; 420 421 ifa_ref(ifa); /* if_addrhead */ 422 IF_ADDR_WLOCK(ifp); 423 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 424 IF_ADDR_WUNLOCK(ifp); 425 ifa_ref(ifa); /* in_ifaddrhead */ 426 IN_IFADDR_WLOCK(); 427 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link); 428 IN_IFADDR_WUNLOCK(); 429 iaIsNew = 1; 430 } 431 break; 432 433 case SIOCGIFADDR: 434 case SIOCGIFNETMASK: 435 case SIOCGIFDSTADDR: 436 case SIOCGIFBRDADDR: 437 if (ia == NULL) { 438 error = EADDRNOTAVAIL; 439 goto out; 440 } 441 break; 442 } 443 444 /* 445 * Most paths in this switch return directly or via out. Only paths 446 * that remove the address break in order to hit common removal code. 447 */ 448 switch (cmd) { 449 case SIOCGIFADDR: 450 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 451 goto out; 452 453 case SIOCGIFBRDADDR: 454 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 455 error = EINVAL; 456 goto out; 457 } 458 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 459 goto out; 460 461 case SIOCGIFDSTADDR: 462 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) { 463 error = EINVAL; 464 goto out; 465 } 466 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 467 goto out; 468 469 case SIOCGIFNETMASK: 470 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 471 goto out; 472 473 case SIOCAIFADDR: 474 maskIsNew = 0; 475 hostIsNew = 1; 476 error = 0; 477 if (ifra->ifra_addr.sin_addr.s_addr == 478 ia->ia_addr.sin_addr.s_addr) 479 hostIsNew = 0; 480 if (ifra->ifra_mask.sin_len) { 481 /* 482 * QL: XXX 483 * Need to scrub the prefix here in case 484 * the issued command is SIOCAIFADDR with 485 * the same address, but with a different 486 * prefix length. And if the prefix length 487 * is the same as before, then the call is 488 * un-necessarily executed here. 489 */ 490 in_ifscrub(ifp, ia, LLE_STATIC); 491 ia->ia_sockmask = ifra->ifra_mask; 492 ia->ia_sockmask.sin_family = AF_INET; 493 ia->ia_subnetmask = 494 ntohl(ia->ia_sockmask.sin_addr.s_addr); 495 maskIsNew = 1; 496 } 497 if ((ifp->if_flags & IFF_POINTOPOINT) && 498 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 499 in_ifscrub(ifp, ia, LLE_STATIC); 500 ia->ia_dstaddr = ifra->ifra_dstaddr; 501 maskIsNew = 1; /* We lie; but the effect's the same */ 502 } 503 if (hostIsNew || maskIsNew) 504 error = in_ifinit(ifp, ia, &ifra->ifra_addr, maskIsNew, 505 (ocmd == cmd ? ifra->ifra_vhid : 0)); 506 if (error != 0 && iaIsNew) 507 break; 508 509 if ((ifp->if_flags & IFF_BROADCAST) && 510 ifra->ifra_broadaddr.sin_len) 511 ia->ia_broadaddr = ifra->ifra_broadaddr; 512 if (error == 0) { 513 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 514 if (iaIsFirst && 515 (ifp->if_flags & IFF_MULTICAST) != 0) { 516 error = in_joingroup(ifp, &allhosts_addr, 517 NULL, &ii->ii_allhosts); 518 } 519 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 520 } 521 goto out; 522 523 case SIOCDIFADDR: 524 /* 525 * in_ifscrub kills the interface route. 526 */ 527 in_ifscrub(ifp, ia, LLE_STATIC); 528 529 /* 530 * in_ifadown gets rid of all the rest of 531 * the routes. This is not quite the right 532 * thing to do, but at least if we are running 533 * a routing process they will come back. 534 */ 535 in_ifadown(&ia->ia_ifa, 1); 536 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 537 error = 0; 538 break; 539 540 default: 541 panic("in_control: unsupported ioctl"); 542 } 543 544 if (ia->ia_ifa.ifa_carp) 545 (*carp_detach_p)(&ia->ia_ifa); 546 547 IF_ADDR_WLOCK(ifp); 548 /* Re-check that ia is still part of the list. */ 549 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 550 if (ifa == &ia->ia_ifa) 551 break; 552 } 553 if (ifa == NULL) { 554 /* 555 * If we lost the race with another thread, there is no need to 556 * try it again for the next loop as there is no other exit 557 * path between here and out. 558 */ 559 IF_ADDR_WUNLOCK(ifp); 560 error = EADDRNOTAVAIL; 561 goto out; 562 } 563 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 564 IF_ADDR_WUNLOCK(ifp); 565 ifa_free(&ia->ia_ifa); /* if_addrhead */ 566 567 IN_IFADDR_WLOCK(); 568 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link); 569 570 LIST_REMOVE(ia, ia_hash); 571 IN_IFADDR_WUNLOCK(); 572 /* 573 * If this is the last IPv4 address configured on this 574 * interface, leave the all-hosts group. 575 * No state-change report need be transmitted. 576 */ 577 IFP_TO_IA(ifp, iap); 578 if (iap == NULL) { 579 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]); 580 IN_MULTI_LOCK(); 581 if (ii->ii_allhosts) { 582 (void)in_leavegroup_locked(ii->ii_allhosts, NULL); 583 ii->ii_allhosts = NULL; 584 } 585 IN_MULTI_UNLOCK(); 586 } else 587 ifa_free(&iap->ia_ifa); 588 589 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */ 590 out: 591 if (ia != NULL) 592 ifa_free(&ia->ia_ifa); 593 return (error); 594 } 595 596 /* 597 * SIOC[GAD]LIFADDR. 598 * SIOCGLIFADDR: get first address. (?!?) 599 * SIOCGLIFADDR with IFLR_PREFIX: 600 * get first address that matches the specified prefix. 601 * SIOCALIFADDR: add the specified address. 602 * SIOCALIFADDR with IFLR_PREFIX: 603 * EINVAL since we can't deduce hostid part of the address. 604 * SIOCDLIFADDR: delete the specified address. 605 * SIOCDLIFADDR with IFLR_PREFIX: 606 * delete the first address that matches the specified prefix. 607 * return values: 608 * EINVAL on invalid parameters 609 * EADDRNOTAVAIL on prefix match failed/specified address not found 610 * other values may be returned from in_ioctl() 611 */ 612 static int 613 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, 614 struct ifnet *ifp, struct thread *td) 615 { 616 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 617 struct ifaddr *ifa; 618 619 /* sanity checks */ 620 if (data == NULL || ifp == NULL) { 621 panic("invalid argument to in_lifaddr_ioctl"); 622 /*NOTRECHED*/ 623 } 624 625 switch (cmd) { 626 case SIOCGLIFADDR: 627 /* address must be specified on GET with IFLR_PREFIX */ 628 if ((iflr->flags & IFLR_PREFIX) == 0) 629 break; 630 /*FALLTHROUGH*/ 631 case SIOCALIFADDR: 632 case SIOCDLIFADDR: 633 /* address must be specified on ADD and DELETE */ 634 if (iflr->addr.ss_family != AF_INET) 635 return (EINVAL); 636 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 637 return (EINVAL); 638 /* XXX need improvement */ 639 if (iflr->dstaddr.ss_family 640 && iflr->dstaddr.ss_family != AF_INET) 641 return (EINVAL); 642 if (iflr->dstaddr.ss_family 643 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 644 return (EINVAL); 645 break; 646 default: /*shouldn't happen*/ 647 return (EOPNOTSUPP); 648 } 649 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 650 return (EINVAL); 651 652 switch (cmd) { 653 case SIOCALIFADDR: 654 { 655 struct in_aliasreq ifra; 656 657 if (iflr->flags & IFLR_PREFIX) 658 return (EINVAL); 659 660 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */ 661 bzero(&ifra, sizeof(ifra)); 662 bcopy(iflr->iflr_name, ifra.ifra_name, 663 sizeof(ifra.ifra_name)); 664 665 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 666 667 if (iflr->dstaddr.ss_family) { /*XXX*/ 668 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 669 iflr->dstaddr.ss_len); 670 } 671 672 ifra.ifra_mask.sin_family = AF_INET; 673 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 674 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 675 676 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td)); 677 } 678 case SIOCGLIFADDR: 679 case SIOCDLIFADDR: 680 { 681 struct in_ifaddr *ia; 682 struct in_addr mask, candidate, match; 683 struct sockaddr_in *sin; 684 685 bzero(&mask, sizeof(mask)); 686 bzero(&match, sizeof(match)); 687 if (iflr->flags & IFLR_PREFIX) { 688 /* lookup a prefix rather than address. */ 689 in_len2mask(&mask, iflr->prefixlen); 690 691 sin = (struct sockaddr_in *)&iflr->addr; 692 match.s_addr = sin->sin_addr.s_addr; 693 match.s_addr &= mask.s_addr; 694 695 /* if you set extra bits, that's wrong */ 696 if (match.s_addr != sin->sin_addr.s_addr) 697 return (EINVAL); 698 699 } else { 700 /* on getting an address, take the 1st match */ 701 /* on deleting an address, do exact match */ 702 if (cmd != SIOCGLIFADDR) { 703 in_len2mask(&mask, 32); 704 sin = (struct sockaddr_in *)&iflr->addr; 705 match.s_addr = sin->sin_addr.s_addr; 706 } 707 } 708 709 IF_ADDR_RLOCK(ifp); 710 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 711 if (ifa->ifa_addr->sa_family != AF_INET) 712 continue; 713 if (match.s_addr == 0) 714 break; 715 sin = (struct sockaddr_in *)&ifa->ifa_addr; 716 candidate.s_addr = sin->sin_addr.s_addr; 717 candidate.s_addr &= mask.s_addr; 718 if (candidate.s_addr == match.s_addr) 719 break; 720 } 721 if (ifa != NULL) 722 ifa_ref(ifa); 723 IF_ADDR_RUNLOCK(ifp); 724 if (ifa == NULL) 725 return (EADDRNOTAVAIL); 726 ia = (struct in_ifaddr *)ifa; 727 728 if (cmd == SIOCGLIFADDR) { 729 /* fill in the if_laddrreq structure */ 730 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 731 732 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 733 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 734 ia->ia_dstaddr.sin_len); 735 } else 736 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 737 738 iflr->prefixlen = 739 in_mask2len(&ia->ia_sockmask.sin_addr); 740 741 iflr->flags = 0; /*XXX*/ 742 ifa_free(ifa); 743 744 return (0); 745 } else { 746 struct in_aliasreq ifra; 747 748 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */ 749 bzero(&ifra, sizeof(ifra)); 750 bcopy(iflr->iflr_name, ifra.ifra_name, 751 sizeof(ifra.ifra_name)); 752 753 bcopy(&ia->ia_addr, &ifra.ifra_addr, 754 ia->ia_addr.sin_len); 755 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 756 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 757 ia->ia_dstaddr.sin_len); 758 } 759 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 760 ia->ia_sockmask.sin_len); 761 ifa_free(ifa); 762 763 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 764 ifp, td)); 765 } 766 } 767 } 768 769 return (EOPNOTSUPP); /*just for safety*/ 770 } 771 772 /* 773 * Delete any existing route for an interface. 774 */ 775 void 776 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, u_int flags) 777 { 778 779 in_scrubprefix(ia, flags); 780 } 781 782 /* 783 * Initialize an interface's internet address 784 * and routing table entry. 785 */ 786 static int 787 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, 788 int masksupplied, int vhid) 789 { 790 register u_long i = ntohl(sin->sin_addr.s_addr); 791 int flags, error = 0; 792 793 IN_IFADDR_WLOCK(); 794 if (ia->ia_addr.sin_family == AF_INET) 795 LIST_REMOVE(ia, ia_hash); 796 ia->ia_addr = *sin; 797 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 798 ia, ia_hash); 799 IN_IFADDR_WUNLOCK(); 800 801 if (vhid > 0) { 802 if (carp_attach_p != NULL) 803 error = (*carp_attach_p)(&ia->ia_ifa, vhid); 804 else 805 error = EPROTONOSUPPORT; 806 } 807 if (error) 808 return (error); 809 810 /* 811 * Give the interface a chance to initialize 812 * if this is its first address, 813 * and to validate the address if necessary. 814 */ 815 if (ifp->if_ioctl != NULL && 816 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) != 0) 817 /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 818 return (error); 819 820 /* 821 * Be compatible with network classes, if netmask isn't supplied, 822 * guess it based on classes. 823 */ 824 if (!masksupplied) { 825 if (IN_CLASSA(i)) 826 ia->ia_subnetmask = IN_CLASSA_NET; 827 else if (IN_CLASSB(i)) 828 ia->ia_subnetmask = IN_CLASSB_NET; 829 else 830 ia->ia_subnetmask = IN_CLASSC_NET; 831 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 832 } 833 ia->ia_subnet = i & ia->ia_subnetmask; 834 in_socktrim(&ia->ia_sockmask); 835 836 /* 837 * Add route for the network. 838 */ 839 flags = RTF_UP; 840 ia->ia_ifa.ifa_metric = ifp->if_metric; 841 if (ifp->if_flags & IFF_BROADCAST) { 842 if (ia->ia_subnetmask == IN_RFC3021_MASK) 843 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST; 844 else 845 ia->ia_broadaddr.sin_addr.s_addr = 846 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 847 } else if (ifp->if_flags & IFF_LOOPBACK) { 848 ia->ia_dstaddr = ia->ia_addr; 849 flags |= RTF_HOST; 850 } else if (ifp->if_flags & IFF_POINTOPOINT) { 851 if (ia->ia_dstaddr.sin_family != AF_INET) 852 return (0); 853 flags |= RTF_HOST; 854 } 855 if (!vhid && (error = in_addprefix(ia, flags)) != 0) 856 return (error); 857 858 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY) 859 return (0); 860 861 if (ifp->if_flags & IFF_POINTOPOINT && 862 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr) 863 return (0); 864 865 /* 866 * add a loopback route to self 867 */ 868 if (V_useloopback && !vhid && !(ifp->if_flags & IFF_LOOPBACK)) { 869 struct route ia_ro; 870 871 bzero(&ia_ro, sizeof(ia_ro)); 872 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr; 873 rtalloc_ign_fib(&ia_ro, 0, RT_DEFAULT_FIB); 874 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) && 875 (ia_ro.ro_rt->rt_ifp == V_loif)) { 876 RT_LOCK(ia_ro.ro_rt); 877 RT_ADDREF(ia_ro.ro_rt); 878 RTFREE_LOCKED(ia_ro.ro_rt); 879 } else 880 error = ifa_add_loopback_route((struct ifaddr *)ia, 881 (struct sockaddr *)&ia->ia_addr); 882 if (error == 0) 883 ia->ia_flags |= IFA_RTSELF; 884 if (ia_ro.ro_rt != NULL) 885 RTFREE(ia_ro.ro_rt); 886 } 887 888 return (error); 889 } 890 891 #define rtinitflags(x) \ 892 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 893 ? RTF_HOST : 0) 894 895 /* 896 * Generate a routing message when inserting or deleting 897 * an interface address alias. 898 */ 899 static void in_addralias_rtmsg(int cmd, struct in_addr *prefix, 900 struct in_ifaddr *target) 901 { 902 struct route pfx_ro; 903 struct sockaddr_in *pfx_addr; 904 struct rtentry msg_rt; 905 906 /* QL: XXX 907 * This is a bit questionable because there is no 908 * additional route entry added/deleted for an address 909 * alias. Therefore this route report is inaccurate. 910 */ 911 bzero(&pfx_ro, sizeof(pfx_ro)); 912 pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst); 913 pfx_addr->sin_len = sizeof(*pfx_addr); 914 pfx_addr->sin_family = AF_INET; 915 pfx_addr->sin_addr = *prefix; 916 rtalloc_ign_fib(&pfx_ro, 0, 0); 917 if (pfx_ro.ro_rt != NULL) { 918 msg_rt = *pfx_ro.ro_rt; 919 920 /* QL: XXX 921 * Point the gateway to the new interface 922 * address as if a new prefix route entry has 923 * been added through the new address alias. 924 * All other parts of the rtentry is accurate, 925 * e.g., rt_key, rt_mask, rt_ifp etc. 926 */ 927 msg_rt.rt_gateway = (struct sockaddr *)&target->ia_addr; 928 rt_newaddrmsg(cmd, (struct ifaddr *)target, 0, &msg_rt); 929 RTFREE(pfx_ro.ro_rt); 930 } 931 return; 932 } 933 934 /* 935 * Check if we have a route for the given prefix already or add one accordingly. 936 */ 937 int 938 in_addprefix(struct in_ifaddr *target, int flags) 939 { 940 struct in_ifaddr *ia; 941 struct in_addr prefix, mask, p, m; 942 int error; 943 944 if ((flags & RTF_HOST) != 0) { 945 prefix = target->ia_dstaddr.sin_addr; 946 mask.s_addr = 0; 947 } else { 948 prefix = target->ia_addr.sin_addr; 949 mask = target->ia_sockmask.sin_addr; 950 prefix.s_addr &= mask.s_addr; 951 } 952 953 IN_IFADDR_RLOCK(); 954 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 955 if (rtinitflags(ia)) { 956 p = ia->ia_dstaddr.sin_addr; 957 958 if (prefix.s_addr != p.s_addr) 959 continue; 960 } else { 961 p = ia->ia_addr.sin_addr; 962 m = ia->ia_sockmask.sin_addr; 963 p.s_addr &= m.s_addr; 964 965 if (prefix.s_addr != p.s_addr || 966 mask.s_addr != m.s_addr) 967 continue; 968 } 969 970 /* 971 * If we got a matching prefix route inserted by other 972 * interface address, we are done here. 973 */ 974 if (ia->ia_flags & IFA_ROUTE) { 975 #ifdef RADIX_MPATH 976 if (ia->ia_addr.sin_addr.s_addr == 977 target->ia_addr.sin_addr.s_addr) { 978 IN_IFADDR_RUNLOCK(); 979 return (EEXIST); 980 } else 981 break; 982 #endif 983 if (V_nosameprefix) { 984 IN_IFADDR_RUNLOCK(); 985 return (EEXIST); 986 } else { 987 in_addralias_rtmsg(RTM_ADD, &prefix, target); 988 IN_IFADDR_RUNLOCK(); 989 return (0); 990 } 991 } 992 } 993 IN_IFADDR_RUNLOCK(); 994 995 /* 996 * No-one seem to have this prefix route, so we try to insert it. 997 */ 998 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 999 if (!error) 1000 target->ia_flags |= IFA_ROUTE; 1001 return (error); 1002 } 1003 1004 /* 1005 * If there is no other address in the system that can serve a route to the 1006 * same prefix, remove the route. Hand over the route to the new address 1007 * otherwise. 1008 */ 1009 int 1010 in_scrubprefix(struct in_ifaddr *target, u_int flags) 1011 { 1012 struct in_ifaddr *ia; 1013 struct in_addr prefix, mask, p, m; 1014 int error = 0; 1015 struct sockaddr_in prefix0, mask0; 1016 1017 /* 1018 * Remove the loopback route to the interface address. 1019 * The "useloopback" setting is not consulted because if the 1020 * user configures an interface address, turns off this 1021 * setting, and then tries to delete that interface address, 1022 * checking the current setting of "useloopback" would leave 1023 * that interface address loopback route untouched, which 1024 * would be wrong. Therefore the interface address loopback route 1025 * deletion is unconditional. 1026 */ 1027 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) && 1028 !(target->ia_ifp->if_flags & IFF_LOOPBACK) && 1029 (target->ia_flags & IFA_RTSELF)) { 1030 struct route ia_ro; 1031 int freeit = 0; 1032 1033 bzero(&ia_ro, sizeof(ia_ro)); 1034 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr; 1035 rtalloc_ign_fib(&ia_ro, 0, 0); 1036 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) && 1037 (ia_ro.ro_rt->rt_ifp == V_loif)) { 1038 RT_LOCK(ia_ro.ro_rt); 1039 if (ia_ro.ro_rt->rt_refcnt <= 1) 1040 freeit = 1; 1041 else if (flags & LLE_STATIC) { 1042 RT_REMREF(ia_ro.ro_rt); 1043 target->ia_flags &= ~IFA_RTSELF; 1044 } 1045 RTFREE_LOCKED(ia_ro.ro_rt); 1046 } 1047 if (freeit && (flags & LLE_STATIC)) { 1048 error = ifa_del_loopback_route((struct ifaddr *)target, 1049 (struct sockaddr *)&target->ia_addr); 1050 if (error == 0) 1051 target->ia_flags &= ~IFA_RTSELF; 1052 } 1053 if ((flags & LLE_STATIC) && 1054 !(target->ia_ifp->if_flags & IFF_NOARP)) 1055 /* remove arp cache */ 1056 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr); 1057 } 1058 1059 if (rtinitflags(target)) { 1060 prefix = target->ia_dstaddr.sin_addr; 1061 mask.s_addr = 0; 1062 } else { 1063 prefix = target->ia_addr.sin_addr; 1064 mask = target->ia_sockmask.sin_addr; 1065 prefix.s_addr &= mask.s_addr; 1066 } 1067 1068 if ((target->ia_flags & IFA_ROUTE) == 0) { 1069 in_addralias_rtmsg(RTM_DELETE, &prefix, target); 1070 return (0); 1071 } 1072 1073 IN_IFADDR_RLOCK(); 1074 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1075 if (rtinitflags(ia)) { 1076 p = ia->ia_dstaddr.sin_addr; 1077 1078 if (prefix.s_addr != p.s_addr) 1079 continue; 1080 } else { 1081 p = ia->ia_addr.sin_addr; 1082 m = ia->ia_sockmask.sin_addr; 1083 p.s_addr &= m.s_addr; 1084 1085 if (prefix.s_addr != p.s_addr || 1086 mask.s_addr != m.s_addr) 1087 continue; 1088 } 1089 1090 if ((ia->ia_ifp->if_flags & IFF_UP) == 0) 1091 continue; 1092 1093 /* 1094 * If we got a matching prefix address, move IFA_ROUTE and 1095 * the route itself to it. Make sure that routing daemons 1096 * get a heads-up. 1097 */ 1098 if ((ia->ia_flags & IFA_ROUTE) == 0) { 1099 ifa_ref(&ia->ia_ifa); 1100 IN_IFADDR_RUNLOCK(); 1101 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, 1102 rtinitflags(target)); 1103 if (error == 0) 1104 target->ia_flags &= ~IFA_ROUTE; 1105 else 1106 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n", 1107 error); 1108 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 1109 rtinitflags(ia) | RTF_UP); 1110 if (error == 0) 1111 ia->ia_flags |= IFA_ROUTE; 1112 else 1113 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n", 1114 error); 1115 ifa_free(&ia->ia_ifa); 1116 return (error); 1117 } 1118 } 1119 IN_IFADDR_RUNLOCK(); 1120 1121 /* 1122 * remove all L2 entries on the given prefix 1123 */ 1124 bzero(&prefix0, sizeof(prefix0)); 1125 prefix0.sin_len = sizeof(prefix0); 1126 prefix0.sin_family = AF_INET; 1127 prefix0.sin_addr.s_addr = target->ia_subnet; 1128 bzero(&mask0, sizeof(mask0)); 1129 mask0.sin_len = sizeof(mask0); 1130 mask0.sin_family = AF_INET; 1131 mask0.sin_addr.s_addr = target->ia_subnetmask; 1132 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0, 1133 (struct sockaddr *)&mask0, flags); 1134 1135 /* 1136 * As no-one seem to have this prefix, we can remove the route. 1137 */ 1138 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 1139 if (error == 0) 1140 target->ia_flags &= ~IFA_ROUTE; 1141 else 1142 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error); 1143 return (error); 1144 } 1145 1146 #undef rtinitflags 1147 1148 /* 1149 * Return 1 if the address might be a local broadcast address. 1150 */ 1151 int 1152 in_broadcast(struct in_addr in, struct ifnet *ifp) 1153 { 1154 register struct ifaddr *ifa; 1155 u_long t; 1156 1157 if (in.s_addr == INADDR_BROADCAST || 1158 in.s_addr == INADDR_ANY) 1159 return (1); 1160 if ((ifp->if_flags & IFF_BROADCAST) == 0) 1161 return (0); 1162 t = ntohl(in.s_addr); 1163 /* 1164 * Look through the list of addresses for a match 1165 * with a broadcast address. 1166 */ 1167 #define ia ((struct in_ifaddr *)ifa) 1168 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 1169 if (ifa->ifa_addr->sa_family == AF_INET && 1170 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 1171 /* 1172 * Check for old-style (host 0) broadcast, but 1173 * taking into account that RFC 3021 obsoletes it. 1174 */ 1175 (ia->ia_subnetmask != IN_RFC3021_MASK && 1176 t == ia->ia_subnet)) && 1177 /* 1178 * Check for an all one subnetmask. These 1179 * only exist when an interface gets a secondary 1180 * address. 1181 */ 1182 ia->ia_subnetmask != (u_long)0xffffffff) 1183 return (1); 1184 return (0); 1185 #undef ia 1186 } 1187 1188 /* 1189 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1190 */ 1191 void 1192 in_ifdetach(struct ifnet *ifp) 1193 { 1194 1195 in_pcbpurgeif0(&V_ripcbinfo, ifp); 1196 in_pcbpurgeif0(&V_udbinfo, ifp); 1197 in_purgemaddrs(ifp); 1198 } 1199 1200 /* 1201 * Delete all IPv4 multicast address records, and associated link-layer 1202 * multicast address records, associated with ifp. 1203 * XXX It looks like domifdetach runs AFTER the link layer cleanup. 1204 * XXX This should not race with ifma_protospec being set during 1205 * a new allocation, if it does, we have bigger problems. 1206 */ 1207 static void 1208 in_purgemaddrs(struct ifnet *ifp) 1209 { 1210 LIST_HEAD(,in_multi) purgeinms; 1211 struct in_multi *inm, *tinm; 1212 struct ifmultiaddr *ifma; 1213 1214 LIST_INIT(&purgeinms); 1215 IN_MULTI_LOCK(); 1216 1217 /* 1218 * Extract list of in_multi associated with the detaching ifp 1219 * which the PF_INET layer is about to release. 1220 * We need to do this as IF_ADDR_LOCK() may be re-acquired 1221 * by code further down. 1222 */ 1223 IF_ADDR_RLOCK(ifp); 1224 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 1225 if (ifma->ifma_addr->sa_family != AF_INET || 1226 ifma->ifma_protospec == NULL) 1227 continue; 1228 #if 0 1229 KASSERT(ifma->ifma_protospec != NULL, 1230 ("%s: ifma_protospec is NULL", __func__)); 1231 #endif 1232 inm = (struct in_multi *)ifma->ifma_protospec; 1233 LIST_INSERT_HEAD(&purgeinms, inm, inm_link); 1234 } 1235 IF_ADDR_RUNLOCK(ifp); 1236 1237 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) { 1238 LIST_REMOVE(inm, inm_link); 1239 inm_release_locked(inm); 1240 } 1241 igmp_ifdetach(ifp); 1242 1243 IN_MULTI_UNLOCK(); 1244 } 1245 1246 struct in_llentry { 1247 struct llentry base; 1248 struct sockaddr_in l3_addr4; 1249 }; 1250 1251 /* 1252 * Deletes an address from the address table. 1253 * This function is called by the timer functions 1254 * such as arptimer() and nd6_llinfo_timer(), and 1255 * the caller does the locking. 1256 */ 1257 static void 1258 in_lltable_free(struct lltable *llt, struct llentry *lle) 1259 { 1260 LLE_WUNLOCK(lle); 1261 LLE_LOCK_DESTROY(lle); 1262 free(lle, M_LLTABLE); 1263 } 1264 1265 static struct llentry * 1266 in_lltable_new(const struct sockaddr *l3addr, u_int flags) 1267 { 1268 struct in_llentry *lle; 1269 1270 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO); 1271 if (lle == NULL) /* NB: caller generates msg */ 1272 return NULL; 1273 1274 /* 1275 * For IPv4 this will trigger "arpresolve" to generate 1276 * an ARP request. 1277 */ 1278 lle->base.la_expire = time_uptime; /* mark expired */ 1279 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr; 1280 lle->base.lle_refcnt = 1; 1281 lle->base.lle_free = in_lltable_free; 1282 LLE_LOCK_INIT(&lle->base); 1283 callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock, 1284 CALLOUT_RETURNUNLOCKED); 1285 1286 return (&lle->base); 1287 } 1288 1289 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ 1290 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 ) 1291 1292 static void 1293 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix, 1294 const struct sockaddr *mask, u_int flags) 1295 { 1296 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix; 1297 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask; 1298 struct llentry *lle, *next; 1299 int i; 1300 size_t pkts_dropped; 1301 1302 IF_AFDATA_WLOCK(llt->llt_ifp); 1303 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1304 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) { 1305 /* 1306 * (flags & LLE_STATIC) means deleting all entries 1307 * including static ARP entries. 1308 */ 1309 if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)), 1310 pfx, msk) && ((flags & LLE_STATIC) || 1311 !(lle->la_flags & LLE_STATIC))) { 1312 LLE_WLOCK(lle); 1313 if (callout_stop(&lle->la_timer)) 1314 LLE_REMREF(lle); 1315 pkts_dropped = llentry_free(lle); 1316 ARPSTAT_ADD(dropped, pkts_dropped); 1317 } 1318 } 1319 } 1320 IF_AFDATA_WUNLOCK(llt->llt_ifp); 1321 } 1322 1323 1324 static int 1325 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr) 1326 { 1327 struct rtentry *rt; 1328 1329 KASSERT(l3addr->sa_family == AF_INET, 1330 ("sin_family %d", l3addr->sa_family)); 1331 1332 /* XXX rtalloc1 should take a const param */ 1333 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0); 1334 1335 if (rt == NULL) 1336 return (EINVAL); 1337 1338 /* 1339 * If the gateway for an existing host route matches the target L3 1340 * address, which is a special route inserted by some implementation 1341 * such as MANET, and the interface is of the correct type, then 1342 * allow for ARP to proceed. 1343 */ 1344 if (rt->rt_flags & RTF_GATEWAY) { 1345 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp || 1346 rt->rt_ifp->if_type != IFT_ETHER || 1347 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 || 1348 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data, 1349 sizeof(in_addr_t)) != 0) { 1350 RTFREE_LOCKED(rt); 1351 return (EINVAL); 1352 } 1353 } 1354 1355 /* 1356 * Make sure that at least the destination address is covered 1357 * by the route. This is for handling the case where 2 or more 1358 * interfaces have the same prefix. An incoming packet arrives 1359 * on one interface and the corresponding outgoing packet leaves 1360 * another interface. 1361 */ 1362 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) { 1363 const char *sa, *mask, *addr, *lim; 1364 int len; 1365 1366 mask = (const char *)rt_mask(rt); 1367 /* 1368 * Just being extra cautious to avoid some custom 1369 * code getting into trouble. 1370 */ 1371 if (mask == NULL) { 1372 RTFREE_LOCKED(rt); 1373 return (EINVAL); 1374 } 1375 1376 sa = (const char *)rt_key(rt); 1377 addr = (const char *)l3addr; 1378 len = ((const struct sockaddr_in *)l3addr)->sin_len; 1379 lim = addr + len; 1380 1381 for ( ; addr < lim; sa++, mask++, addr++) { 1382 if ((*sa ^ *addr) & *mask) { 1383 #ifdef DIAGNOSTIC 1384 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n", 1385 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr)); 1386 #endif 1387 RTFREE_LOCKED(rt); 1388 return (EINVAL); 1389 } 1390 } 1391 } 1392 1393 RTFREE_LOCKED(rt); 1394 return (0); 1395 } 1396 1397 /* 1398 * Return NULL if not found or marked for deletion. 1399 * If found return lle read locked. 1400 */ 1401 static struct llentry * 1402 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr) 1403 { 1404 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr; 1405 struct ifnet *ifp = llt->llt_ifp; 1406 struct llentry *lle; 1407 struct llentries *lleh; 1408 u_int hashkey; 1409 1410 IF_AFDATA_LOCK_ASSERT(ifp); 1411 KASSERT(l3addr->sa_family == AF_INET, 1412 ("sin_family %d", l3addr->sa_family)); 1413 1414 hashkey = sin->sin_addr.s_addr; 1415 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)]; 1416 LIST_FOREACH(lle, lleh, lle_next) { 1417 struct sockaddr_in *sa2 = satosin(L3_ADDR(lle)); 1418 if (lle->la_flags & LLE_DELETED) 1419 continue; 1420 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr) 1421 break; 1422 } 1423 if (lle == NULL) { 1424 #ifdef DIAGNOSTIC 1425 if (flags & LLE_DELETE) 1426 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle); 1427 #endif 1428 if (!(flags & LLE_CREATE)) 1429 return (NULL); 1430 /* 1431 * A route that covers the given address must have 1432 * been installed 1st because we are doing a resolution, 1433 * verify this. 1434 */ 1435 if (!(flags & LLE_IFADDR) && 1436 in_lltable_rtcheck(ifp, flags, l3addr) != 0) 1437 goto done; 1438 1439 lle = in_lltable_new(l3addr, flags); 1440 if (lle == NULL) { 1441 log(LOG_INFO, "lla_lookup: new lle malloc failed\n"); 1442 goto done; 1443 } 1444 lle->la_flags = flags & ~LLE_CREATE; 1445 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) { 1446 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen); 1447 lle->la_flags |= (LLE_VALID | LLE_STATIC); 1448 } 1449 1450 lle->lle_tbl = llt; 1451 lle->lle_head = lleh; 1452 lle->la_flags |= LLE_LINKED; 1453 LIST_INSERT_HEAD(lleh, lle, lle_next); 1454 } else if (flags & LLE_DELETE) { 1455 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) { 1456 LLE_WLOCK(lle); 1457 lle->la_flags |= LLE_DELETED; 1458 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED); 1459 #ifdef DIAGNOSTIC 1460 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle); 1461 #endif 1462 if ((lle->la_flags & 1463 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC) 1464 llentry_free(lle); 1465 else 1466 LLE_WUNLOCK(lle); 1467 } 1468 lle = (void *)-1; 1469 1470 } 1471 if (LLE_IS_VALID(lle)) { 1472 if (flags & LLE_EXCLUSIVE) 1473 LLE_WLOCK(lle); 1474 else 1475 LLE_RLOCK(lle); 1476 } 1477 done: 1478 return (lle); 1479 } 1480 1481 static int 1482 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr) 1483 { 1484 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle)) 1485 struct ifnet *ifp = llt->llt_ifp; 1486 struct llentry *lle; 1487 /* XXX stack use */ 1488 struct { 1489 struct rt_msghdr rtm; 1490 struct sockaddr_in sin; 1491 struct sockaddr_dl sdl; 1492 } arpc; 1493 int error, i; 1494 1495 LLTABLE_LOCK_ASSERT(); 1496 1497 error = 0; 1498 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) { 1499 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) { 1500 struct sockaddr_dl *sdl; 1501 1502 /* skip deleted entries */ 1503 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED) 1504 continue; 1505 /* Skip if jailed and not a valid IP of the prison. */ 1506 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0) 1507 continue; 1508 /* 1509 * produce a msg made of: 1510 * struct rt_msghdr; 1511 * struct sockaddr_in; (IPv4) 1512 * struct sockaddr_dl; 1513 */ 1514 bzero(&arpc, sizeof(arpc)); 1515 arpc.rtm.rtm_msglen = sizeof(arpc); 1516 arpc.rtm.rtm_version = RTM_VERSION; 1517 arpc.rtm.rtm_type = RTM_GET; 1518 arpc.rtm.rtm_flags = RTF_UP; 1519 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY; 1520 arpc.sin.sin_family = AF_INET; 1521 arpc.sin.sin_len = sizeof(arpc.sin); 1522 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr; 1523 1524 /* publish */ 1525 if (lle->la_flags & LLE_PUB) 1526 arpc.rtm.rtm_flags |= RTF_ANNOUNCE; 1527 1528 sdl = &arpc.sdl; 1529 sdl->sdl_family = AF_LINK; 1530 sdl->sdl_len = sizeof(*sdl); 1531 sdl->sdl_index = ifp->if_index; 1532 sdl->sdl_type = ifp->if_type; 1533 if ((lle->la_flags & LLE_VALID) == LLE_VALID) { 1534 sdl->sdl_alen = ifp->if_addrlen; 1535 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen); 1536 } else { 1537 sdl->sdl_alen = 0; 1538 bzero(LLADDR(sdl), ifp->if_addrlen); 1539 } 1540 1541 arpc.rtm.rtm_rmx.rmx_expire = 1542 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire; 1543 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA); 1544 if (lle->la_flags & LLE_STATIC) 1545 arpc.rtm.rtm_flags |= RTF_STATIC; 1546 arpc.rtm.rtm_index = ifp->if_index; 1547 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc)); 1548 if (error) 1549 break; 1550 } 1551 } 1552 return error; 1553 #undef SIN 1554 } 1555 1556 void * 1557 in_domifattach(struct ifnet *ifp) 1558 { 1559 struct in_ifinfo *ii; 1560 struct lltable *llt; 1561 1562 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO); 1563 1564 llt = lltable_init(ifp, AF_INET); 1565 if (llt != NULL) { 1566 llt->llt_prefix_free = in_lltable_prefix_free; 1567 llt->llt_lookup = in_lltable_lookup; 1568 llt->llt_dump = in_lltable_dump; 1569 } 1570 ii->ii_llt = llt; 1571 1572 ii->ii_igmp = igmp_domifattach(ifp); 1573 1574 return ii; 1575 } 1576 1577 void 1578 in_domifdetach(struct ifnet *ifp, void *aux) 1579 { 1580 struct in_ifinfo *ii = (struct in_ifinfo *)aux; 1581 1582 igmp_domifdetach(ifp); 1583 lltable_free(ii->ii_llt); 1584 free(ii, M_IFADDR); 1585 } 1586