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