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