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 * $FreeBSD$ 32 */ 33 34 #include "opt_carp.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/sockio.h> 39 #include <sys/malloc.h> 40 #include <sys/priv.h> 41 #include <sys/socket.h> 42 #include <sys/kernel.h> 43 #include <sys/sysctl.h> 44 45 #include <net/if.h> 46 #include <net/if_types.h> 47 #include <net/route.h> 48 49 #include <netinet/in.h> 50 #include <netinet/in_var.h> 51 #include <netinet/in_pcb.h> 52 53 #include <netinet/igmp_var.h> 54 55 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address"); 56 57 static int in_mask2len(struct in_addr *); 58 static void in_len2mask(struct in_addr *, int); 59 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 60 struct ifnet *, struct thread *); 61 62 static int in_addprefix(struct in_ifaddr *, int); 63 static int in_scrubprefix(struct in_ifaddr *); 64 static void in_socktrim(struct sockaddr_in *); 65 static int in_ifinit(struct ifnet *, 66 struct in_ifaddr *, struct sockaddr_in *, int); 67 static void in_purgemaddrs(struct ifnet *); 68 69 static int subnetsarelocal = 0; 70 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, 71 &subnetsarelocal, 0, "Treat all subnets as directly connected"); 72 static int sameprefixcarponly = 0; 73 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW, 74 &sameprefixcarponly, 0, 75 "Refuse to create same prefixes on different interfaces"); 76 77 /* 78 * The IPv4 multicast list (in_multihead and associated structures) are 79 * protected by the global in_multi_mtx. See in_var.h for more details. For 80 * now, in_multi_mtx is marked as recursible due to IGMP's calling back into 81 * ip_output() to send IGMP packets while holding the lock; this probably is 82 * not quite desirable. 83 */ 84 struct in_multihead in_multihead; /* XXX BSS initialization */ 85 struct mtx in_multi_mtx; 86 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE); 87 88 extern struct inpcbinfo ripcbinfo; 89 extern struct inpcbinfo udbinfo; 90 91 /* 92 * Return 1 if an internet address is for a ``local'' host 93 * (one to which we have a connection). If subnetsarelocal 94 * is true, this includes other subnets of the local net. 95 * Otherwise, it includes only the directly-connected (sub)nets. 96 */ 97 int 98 in_localaddr(in) 99 struct in_addr in; 100 { 101 register u_long i = ntohl(in.s_addr); 102 register struct in_ifaddr *ia; 103 104 if (subnetsarelocal) { 105 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 106 if ((i & ia->ia_netmask) == ia->ia_net) 107 return (1); 108 } else { 109 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 110 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 111 return (1); 112 } 113 return (0); 114 } 115 116 /* 117 * Return 1 if an internet address is for the local host and configured 118 * on one of its interfaces. 119 */ 120 int 121 in_localip(in) 122 struct in_addr in; 123 { 124 struct in_ifaddr *ia; 125 126 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) { 127 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) 128 return 1; 129 } 130 return 0; 131 } 132 133 /* 134 * Determine whether an IP address is in a reserved set of addresses 135 * that may not be forwarded, or whether datagrams to that destination 136 * may be forwarded. 137 */ 138 int 139 in_canforward(in) 140 struct in_addr in; 141 { 142 register u_long i = ntohl(in.s_addr); 143 register u_long net; 144 145 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i)) 146 return (0); 147 if (IN_CLASSA(i)) { 148 net = i & IN_CLASSA_NET; 149 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 150 return (0); 151 } 152 return (1); 153 } 154 155 /* 156 * Trim a mask in a sockaddr 157 */ 158 static void 159 in_socktrim(ap) 160 struct sockaddr_in *ap; 161 { 162 register char *cplim = (char *) &ap->sin_addr; 163 register char *cp = (char *) (&ap->sin_addr + 1); 164 165 ap->sin_len = 0; 166 while (--cp >= cplim) 167 if (*cp) { 168 (ap)->sin_len = cp - (char *) (ap) + 1; 169 break; 170 } 171 } 172 173 static int 174 in_mask2len(mask) 175 struct in_addr *mask; 176 { 177 int x, y; 178 u_char *p; 179 180 p = (u_char *)mask; 181 for (x = 0; x < sizeof(*mask); x++) { 182 if (p[x] != 0xff) 183 break; 184 } 185 y = 0; 186 if (x < sizeof(*mask)) { 187 for (y = 0; y < 8; y++) { 188 if ((p[x] & (0x80 >> y)) == 0) 189 break; 190 } 191 } 192 return x * 8 + y; 193 } 194 195 static void 196 in_len2mask(mask, len) 197 struct in_addr *mask; 198 int len; 199 { 200 int i; 201 u_char *p; 202 203 p = (u_char *)mask; 204 bzero(mask, sizeof(*mask)); 205 for (i = 0; i < len / 8; i++) 206 p[i] = 0xff; 207 if (len % 8) 208 p[i] = (0xff00 >> (len % 8)) & 0xff; 209 } 210 211 /* 212 * Generic internet control operations (ioctl's). 213 * Ifp is 0 if not an interface-specific ioctl. 214 */ 215 /* ARGSUSED */ 216 int 217 in_control(so, cmd, data, ifp, td) 218 struct socket *so; 219 u_long cmd; 220 caddr_t data; 221 register struct ifnet *ifp; 222 struct thread *td; 223 { 224 register struct ifreq *ifr = (struct ifreq *)data; 225 register struct in_ifaddr *ia = 0, *iap; 226 register struct ifaddr *ifa; 227 struct in_addr dst; 228 struct in_ifaddr *oia; 229 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 230 struct sockaddr_in oldaddr; 231 int error, hostIsNew, iaIsNew, maskIsNew, s; 232 233 iaIsNew = 0; 234 235 switch (cmd) { 236 case SIOCALIFADDR: 237 if (td != NULL) { 238 error = priv_check(td, PRIV_NET_ADDIFADDR); 239 if (error) 240 return (error); 241 } 242 if (!ifp) 243 return EINVAL; 244 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 245 246 case SIOCDLIFADDR: 247 if (td != NULL) { 248 error = priv_check(td, PRIV_NET_DELIFADDR); 249 if (error) 250 return (error); 251 } 252 if (!ifp) 253 return EINVAL; 254 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 255 256 case SIOCGLIFADDR: 257 if (!ifp) 258 return EINVAL; 259 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 260 } 261 262 /* 263 * Find address for this interface, if it exists. 264 * 265 * If an alias address was specified, find that one instead of 266 * the first one on the interface, if possible. 267 */ 268 if (ifp) { 269 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 270 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) 271 if (iap->ia_ifp == ifp && 272 iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 273 ia = iap; 274 break; 275 } 276 if (ia == NULL) 277 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 278 iap = ifatoia(ifa); 279 if (iap->ia_addr.sin_family == AF_INET) { 280 ia = iap; 281 break; 282 } 283 } 284 } 285 286 switch (cmd) { 287 288 case SIOCAIFADDR: 289 case SIOCDIFADDR: 290 if (ifp == 0) 291 return (EADDRNOTAVAIL); 292 if (ifra->ifra_addr.sin_family == AF_INET) { 293 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 294 if (ia->ia_ifp == ifp && 295 ia->ia_addr.sin_addr.s_addr == 296 ifra->ifra_addr.sin_addr.s_addr) 297 break; 298 } 299 if ((ifp->if_flags & IFF_POINTOPOINT) 300 && (cmd == SIOCAIFADDR) 301 && (ifra->ifra_dstaddr.sin_addr.s_addr 302 == INADDR_ANY)) { 303 return EDESTADDRREQ; 304 } 305 } 306 if (cmd == SIOCDIFADDR && ia == 0) 307 return (EADDRNOTAVAIL); 308 /* FALLTHROUGH */ 309 case SIOCSIFADDR: 310 case SIOCSIFNETMASK: 311 case SIOCSIFDSTADDR: 312 if (td != NULL) { 313 error = priv_check(td, PRIV_NET_ADDIFADDR); 314 if (error) 315 return (error); 316 } 317 318 if (ifp == 0) 319 return (EADDRNOTAVAIL); 320 if (ia == (struct in_ifaddr *)0) { 321 ia = (struct in_ifaddr *) 322 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 323 if (ia == (struct in_ifaddr *)NULL) 324 return (ENOBUFS); 325 /* 326 * Protect from ipintr() traversing address list 327 * while we're modifying it. 328 */ 329 s = splnet(); 330 ifa = &ia->ia_ifa; 331 IFA_LOCK_INIT(ifa); 332 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 333 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 334 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 335 ifa->ifa_refcnt = 1; 336 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 337 338 ia->ia_sockmask.sin_len = 8; 339 ia->ia_sockmask.sin_family = AF_INET; 340 if (ifp->if_flags & IFF_BROADCAST) { 341 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 342 ia->ia_broadaddr.sin_family = AF_INET; 343 } 344 ia->ia_ifp = ifp; 345 346 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link); 347 splx(s); 348 iaIsNew = 1; 349 } 350 break; 351 352 case SIOCSIFBRDADDR: 353 if (td != NULL) { 354 error = priv_check(td, PRIV_NET_ADDIFADDR); 355 if (error) 356 return (error); 357 } 358 /* FALLTHROUGH */ 359 360 case SIOCGIFADDR: 361 case SIOCGIFNETMASK: 362 case SIOCGIFDSTADDR: 363 case SIOCGIFBRDADDR: 364 if (ia == (struct in_ifaddr *)0) 365 return (EADDRNOTAVAIL); 366 break; 367 } 368 switch (cmd) { 369 370 case SIOCGIFADDR: 371 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 372 return (0); 373 374 case SIOCGIFBRDADDR: 375 if ((ifp->if_flags & IFF_BROADCAST) == 0) 376 return (EINVAL); 377 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 378 return (0); 379 380 case SIOCGIFDSTADDR: 381 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 382 return (EINVAL); 383 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 384 return (0); 385 386 case SIOCGIFNETMASK: 387 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 388 return (0); 389 390 case SIOCSIFDSTADDR: 391 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 392 return (EINVAL); 393 oldaddr = ia->ia_dstaddr; 394 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 395 if (ifp->if_ioctl) { 396 IFF_LOCKGIANT(ifp); 397 error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, 398 (caddr_t)ia); 399 IFF_UNLOCKGIANT(ifp); 400 if (error) { 401 ia->ia_dstaddr = oldaddr; 402 return (error); 403 } 404 } 405 if (ia->ia_flags & IFA_ROUTE) { 406 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 407 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 408 ia->ia_ifa.ifa_dstaddr = 409 (struct sockaddr *)&ia->ia_dstaddr; 410 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 411 } 412 return (0); 413 414 case SIOCSIFBRDADDR: 415 if ((ifp->if_flags & IFF_BROADCAST) == 0) 416 return (EINVAL); 417 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 418 return (0); 419 420 case SIOCSIFADDR: 421 error = in_ifinit(ifp, ia, 422 (struct sockaddr_in *) &ifr->ifr_addr, 1); 423 if (error != 0 && iaIsNew) 424 break; 425 if (error == 0) 426 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 427 return (0); 428 429 case SIOCSIFNETMASK: 430 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 431 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 432 return (0); 433 434 case SIOCAIFADDR: 435 maskIsNew = 0; 436 hostIsNew = 1; 437 error = 0; 438 if (ia->ia_addr.sin_family == AF_INET) { 439 if (ifra->ifra_addr.sin_len == 0) { 440 ifra->ifra_addr = ia->ia_addr; 441 hostIsNew = 0; 442 } else if (ifra->ifra_addr.sin_addr.s_addr == 443 ia->ia_addr.sin_addr.s_addr) 444 hostIsNew = 0; 445 } 446 if (ifra->ifra_mask.sin_len) { 447 in_ifscrub(ifp, ia); 448 ia->ia_sockmask = ifra->ifra_mask; 449 ia->ia_sockmask.sin_family = AF_INET; 450 ia->ia_subnetmask = 451 ntohl(ia->ia_sockmask.sin_addr.s_addr); 452 maskIsNew = 1; 453 } 454 if ((ifp->if_flags & IFF_POINTOPOINT) && 455 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 456 in_ifscrub(ifp, ia); 457 ia->ia_dstaddr = ifra->ifra_dstaddr; 458 maskIsNew = 1; /* We lie; but the effect's the same */ 459 } 460 if (ifra->ifra_addr.sin_family == AF_INET && 461 (hostIsNew || maskIsNew)) 462 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 463 if (error != 0 && iaIsNew) 464 break; 465 466 if ((ifp->if_flags & IFF_BROADCAST) && 467 (ifra->ifra_broadaddr.sin_family == AF_INET)) 468 ia->ia_broadaddr = ifra->ifra_broadaddr; 469 if (error == 0) 470 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 471 return (error); 472 473 case SIOCDIFADDR: 474 /* 475 * in_ifscrub kills the interface route. 476 */ 477 in_ifscrub(ifp, ia); 478 /* 479 * in_ifadown gets rid of all the rest of 480 * the routes. This is not quite the right 481 * thing to do, but at least if we are running 482 * a routing process they will come back. 483 */ 484 in_ifadown(&ia->ia_ifa, 1); 485 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 486 error = 0; 487 break; 488 489 default: 490 if (ifp == 0 || ifp->if_ioctl == 0) 491 return (EOPNOTSUPP); 492 IFF_LOCKGIANT(ifp); 493 error = (*ifp->if_ioctl)(ifp, cmd, data); 494 IFF_UNLOCKGIANT(ifp); 495 return (error); 496 } 497 498 /* 499 * Protect from ipintr() traversing address list while we're modifying 500 * it. 501 */ 502 s = splnet(); 503 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 504 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link); 505 if (ia->ia_addr.sin_family == AF_INET) 506 LIST_REMOVE(ia, ia_hash); 507 IFAFREE(&ia->ia_ifa); 508 splx(s); 509 510 return (error); 511 } 512 513 /* 514 * SIOC[GAD]LIFADDR. 515 * SIOCGLIFADDR: get first address. (?!?) 516 * SIOCGLIFADDR with IFLR_PREFIX: 517 * get first address that matches the specified prefix. 518 * SIOCALIFADDR: add the specified address. 519 * SIOCALIFADDR with IFLR_PREFIX: 520 * EINVAL since we can't deduce hostid part of the address. 521 * SIOCDLIFADDR: delete the specified address. 522 * SIOCDLIFADDR with IFLR_PREFIX: 523 * delete the first address that matches the specified prefix. 524 * return values: 525 * EINVAL on invalid parameters 526 * EADDRNOTAVAIL on prefix match failed/specified address not found 527 * other values may be returned from in_ioctl() 528 */ 529 static int 530 in_lifaddr_ioctl(so, cmd, data, ifp, td) 531 struct socket *so; 532 u_long cmd; 533 caddr_t data; 534 struct ifnet *ifp; 535 struct thread *td; 536 { 537 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 538 struct ifaddr *ifa; 539 540 /* sanity checks */ 541 if (!data || !ifp) { 542 panic("invalid argument to in_lifaddr_ioctl"); 543 /*NOTRECHED*/ 544 } 545 546 switch (cmd) { 547 case SIOCGLIFADDR: 548 /* address must be specified on GET with IFLR_PREFIX */ 549 if ((iflr->flags & IFLR_PREFIX) == 0) 550 break; 551 /*FALLTHROUGH*/ 552 case SIOCALIFADDR: 553 case SIOCDLIFADDR: 554 /* address must be specified on ADD and DELETE */ 555 if (iflr->addr.ss_family != AF_INET) 556 return EINVAL; 557 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 558 return EINVAL; 559 /* XXX need improvement */ 560 if (iflr->dstaddr.ss_family 561 && iflr->dstaddr.ss_family != AF_INET) 562 return EINVAL; 563 if (iflr->dstaddr.ss_family 564 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 565 return EINVAL; 566 break; 567 default: /*shouldn't happen*/ 568 return EOPNOTSUPP; 569 } 570 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 571 return EINVAL; 572 573 switch (cmd) { 574 case SIOCALIFADDR: 575 { 576 struct in_aliasreq ifra; 577 578 if (iflr->flags & IFLR_PREFIX) 579 return EINVAL; 580 581 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 582 bzero(&ifra, sizeof(ifra)); 583 bcopy(iflr->iflr_name, ifra.ifra_name, 584 sizeof(ifra.ifra_name)); 585 586 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 587 588 if (iflr->dstaddr.ss_family) { /*XXX*/ 589 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 590 iflr->dstaddr.ss_len); 591 } 592 593 ifra.ifra_mask.sin_family = AF_INET; 594 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 595 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 596 597 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td); 598 } 599 case SIOCGLIFADDR: 600 case SIOCDLIFADDR: 601 { 602 struct in_ifaddr *ia; 603 struct in_addr mask, candidate, match; 604 struct sockaddr_in *sin; 605 int cmp; 606 607 bzero(&mask, sizeof(mask)); 608 if (iflr->flags & IFLR_PREFIX) { 609 /* lookup a prefix rather than address. */ 610 in_len2mask(&mask, iflr->prefixlen); 611 612 sin = (struct sockaddr_in *)&iflr->addr; 613 match.s_addr = sin->sin_addr.s_addr; 614 match.s_addr &= mask.s_addr; 615 616 /* if you set extra bits, that's wrong */ 617 if (match.s_addr != sin->sin_addr.s_addr) 618 return EINVAL; 619 620 cmp = 1; 621 } else { 622 if (cmd == SIOCGLIFADDR) { 623 /* on getting an address, take the 1st match */ 624 cmp = 0; /*XXX*/ 625 } else { 626 /* on deleting an address, do exact match */ 627 in_len2mask(&mask, 32); 628 sin = (struct sockaddr_in *)&iflr->addr; 629 match.s_addr = sin->sin_addr.s_addr; 630 631 cmp = 1; 632 } 633 } 634 635 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 636 if (ifa->ifa_addr->sa_family != AF_INET6) 637 continue; 638 if (!cmp) 639 break; 640 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 641 candidate.s_addr &= mask.s_addr; 642 if (candidate.s_addr == match.s_addr) 643 break; 644 } 645 if (!ifa) 646 return EADDRNOTAVAIL; 647 ia = (struct in_ifaddr *)ifa; 648 649 if (cmd == SIOCGLIFADDR) { 650 /* fill in the if_laddrreq structure */ 651 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 652 653 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 654 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 655 ia->ia_dstaddr.sin_len); 656 } else 657 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 658 659 iflr->prefixlen = 660 in_mask2len(&ia->ia_sockmask.sin_addr); 661 662 iflr->flags = 0; /*XXX*/ 663 664 return 0; 665 } else { 666 struct in_aliasreq ifra; 667 668 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 669 bzero(&ifra, sizeof(ifra)); 670 bcopy(iflr->iflr_name, ifra.ifra_name, 671 sizeof(ifra.ifra_name)); 672 673 bcopy(&ia->ia_addr, &ifra.ifra_addr, 674 ia->ia_addr.sin_len); 675 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 676 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 677 ia->ia_dstaddr.sin_len); 678 } 679 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 680 ia->ia_sockmask.sin_len); 681 682 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 683 ifp, td); 684 } 685 } 686 } 687 688 return EOPNOTSUPP; /*just for safety*/ 689 } 690 691 /* 692 * Delete any existing route for an interface. 693 */ 694 void 695 in_ifscrub(ifp, ia) 696 register struct ifnet *ifp; 697 register struct in_ifaddr *ia; 698 { 699 in_scrubprefix(ia); 700 } 701 702 /* 703 * Initialize an interface's internet address 704 * and routing table entry. 705 */ 706 static int 707 in_ifinit(ifp, ia, sin, scrub) 708 register struct ifnet *ifp; 709 register struct in_ifaddr *ia; 710 struct sockaddr_in *sin; 711 int scrub; 712 { 713 register u_long i = ntohl(sin->sin_addr.s_addr); 714 struct sockaddr_in oldaddr; 715 int s = splimp(), flags = RTF_UP, error = 0; 716 717 oldaddr = ia->ia_addr; 718 if (oldaddr.sin_family == AF_INET) 719 LIST_REMOVE(ia, ia_hash); 720 ia->ia_addr = *sin; 721 if (ia->ia_addr.sin_family == AF_INET) 722 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 723 ia, ia_hash); 724 /* 725 * Give the interface a chance to initialize 726 * if this is its first address, 727 * and to validate the address if necessary. 728 */ 729 if (ifp->if_ioctl) { 730 IFF_LOCKGIANT(ifp); 731 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia); 732 IFF_UNLOCKGIANT(ifp); 733 if (error) { 734 splx(s); 735 /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 736 ia->ia_addr = oldaddr; 737 if (ia->ia_addr.sin_family == AF_INET) 738 LIST_INSERT_HEAD(INADDR_HASH( 739 ia->ia_addr.sin_addr.s_addr), ia, ia_hash); 740 return (error); 741 } 742 } 743 splx(s); 744 if (scrub) { 745 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 746 in_ifscrub(ifp, ia); 747 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 748 } 749 if (IN_CLASSA(i)) 750 ia->ia_netmask = IN_CLASSA_NET; 751 else if (IN_CLASSB(i)) 752 ia->ia_netmask = IN_CLASSB_NET; 753 else 754 ia->ia_netmask = IN_CLASSC_NET; 755 /* 756 * The subnet mask usually includes at least the standard network part, 757 * but may may be smaller in the case of supernetting. 758 * If it is set, we believe it. 759 */ 760 if (ia->ia_subnetmask == 0) { 761 ia->ia_subnetmask = ia->ia_netmask; 762 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 763 } else 764 ia->ia_netmask &= ia->ia_subnetmask; 765 ia->ia_net = i & ia->ia_netmask; 766 ia->ia_subnet = i & ia->ia_subnetmask; 767 in_socktrim(&ia->ia_sockmask); 768 #ifdef DEV_CARP 769 /* 770 * XXX: carp(4) does not have interface route 771 */ 772 if (ifp->if_type == IFT_CARP) 773 return (0); 774 #endif 775 /* 776 * Add route for the network. 777 */ 778 ia->ia_ifa.ifa_metric = ifp->if_metric; 779 if (ifp->if_flags & IFF_BROADCAST) { 780 ia->ia_broadaddr.sin_addr.s_addr = 781 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 782 ia->ia_netbroadcast.s_addr = 783 htonl(ia->ia_net | ~ ia->ia_netmask); 784 } else if (ifp->if_flags & IFF_LOOPBACK) { 785 ia->ia_dstaddr = ia->ia_addr; 786 flags |= RTF_HOST; 787 } else if (ifp->if_flags & IFF_POINTOPOINT) { 788 if (ia->ia_dstaddr.sin_family != AF_INET) 789 return (0); 790 flags |= RTF_HOST; 791 } 792 if ((error = in_addprefix(ia, flags)) != 0) 793 return (error); 794 795 /* 796 * If the interface supports multicast, join the "all hosts" 797 * multicast group on that interface. 798 */ 799 if (ifp->if_flags & IFF_MULTICAST) { 800 struct in_addr addr; 801 802 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 803 in_addmulti(&addr, ifp); 804 } 805 return (error); 806 } 807 808 #define rtinitflags(x) \ 809 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \ 810 ? RTF_HOST : 0) 811 /* 812 * Check if we have a route for the given prefix already or add a one 813 * accordingly. 814 */ 815 static int 816 in_addprefix(target, flags) 817 struct in_ifaddr *target; 818 int flags; 819 { 820 struct in_ifaddr *ia; 821 struct in_addr prefix, mask, p, m; 822 int error; 823 824 if ((flags & RTF_HOST) != 0) 825 prefix = target->ia_dstaddr.sin_addr; 826 else { 827 prefix = target->ia_addr.sin_addr; 828 mask = target->ia_sockmask.sin_addr; 829 prefix.s_addr &= mask.s_addr; 830 } 831 832 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 833 if (rtinitflags(ia)) { 834 p = ia->ia_addr.sin_addr; 835 836 if (prefix.s_addr != p.s_addr) 837 continue; 838 } else { 839 p = ia->ia_addr.sin_addr; 840 m = ia->ia_sockmask.sin_addr; 841 p.s_addr &= m.s_addr; 842 843 if (prefix.s_addr != p.s_addr || 844 mask.s_addr != m.s_addr) 845 continue; 846 } 847 848 /* 849 * If we got a matching prefix route inserted by other 850 * interface address, we are done here. 851 */ 852 if (ia->ia_flags & IFA_ROUTE) { 853 if (sameprefixcarponly && 854 target->ia_ifp->if_type != IFT_CARP && 855 ia->ia_ifp->if_type != IFT_CARP) 856 return (EEXIST); 857 else 858 return (0); 859 } 860 } 861 862 /* 863 * No-one seem to have this prefix route, so we try to insert it. 864 */ 865 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags); 866 if (!error) 867 target->ia_flags |= IFA_ROUTE; 868 return error; 869 } 870 871 /* 872 * If there is no other address in the system that can serve a route to the 873 * same prefix, remove the route. Hand over the route to the new address 874 * otherwise. 875 */ 876 static int 877 in_scrubprefix(target) 878 struct in_ifaddr *target; 879 { 880 struct in_ifaddr *ia; 881 struct in_addr prefix, mask, p; 882 int error; 883 884 if ((target->ia_flags & IFA_ROUTE) == 0) 885 return 0; 886 887 if (rtinitflags(target)) 888 prefix = target->ia_dstaddr.sin_addr; 889 else { 890 prefix = target->ia_addr.sin_addr; 891 mask = target->ia_sockmask.sin_addr; 892 prefix.s_addr &= mask.s_addr; 893 } 894 895 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 896 if (rtinitflags(ia)) 897 p = ia->ia_dstaddr.sin_addr; 898 else { 899 p = ia->ia_addr.sin_addr; 900 p.s_addr &= ia->ia_sockmask.sin_addr.s_addr; 901 } 902 903 if (prefix.s_addr != p.s_addr) 904 continue; 905 906 /* 907 * If we got a matching prefix address, move IFA_ROUTE and 908 * the route itself to it. Make sure that routing daemons 909 * get a heads-up. 910 * 911 * XXX: a special case for carp(4) interface 912 */ 913 if ((ia->ia_flags & IFA_ROUTE) == 0 914 #ifdef DEV_CARP 915 && (ia->ia_ifp->if_type != IFT_CARP) 916 #endif 917 ) { 918 rtinit(&(target->ia_ifa), (int)RTM_DELETE, 919 rtinitflags(target)); 920 target->ia_flags &= ~IFA_ROUTE; 921 922 error = rtinit(&ia->ia_ifa, (int)RTM_ADD, 923 rtinitflags(ia) | RTF_UP); 924 if (error == 0) 925 ia->ia_flags |= IFA_ROUTE; 926 return error; 927 } 928 } 929 930 /* 931 * As no-one seem to have this prefix, we can remove the route. 932 */ 933 rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target)); 934 target->ia_flags &= ~IFA_ROUTE; 935 return 0; 936 } 937 938 #undef rtinitflags 939 940 /* 941 * Return 1 if the address might be a local broadcast address. 942 */ 943 int 944 in_broadcast(in, ifp) 945 struct in_addr in; 946 struct ifnet *ifp; 947 { 948 register struct ifaddr *ifa; 949 u_long t; 950 951 if (in.s_addr == INADDR_BROADCAST || 952 in.s_addr == INADDR_ANY) 953 return 1; 954 if ((ifp->if_flags & IFF_BROADCAST) == 0) 955 return 0; 956 t = ntohl(in.s_addr); 957 /* 958 * Look through the list of addresses for a match 959 * with a broadcast address. 960 */ 961 #define ia ((struct in_ifaddr *)ifa) 962 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 963 if (ifa->ifa_addr->sa_family == AF_INET && 964 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 965 in.s_addr == ia->ia_netbroadcast.s_addr || 966 /* 967 * Check for old-style (host 0) broadcast. 968 */ 969 t == ia->ia_subnet || t == ia->ia_net) && 970 /* 971 * Check for an all one subnetmask. These 972 * only exist when an interface gets a secondary 973 * address. 974 */ 975 ia->ia_subnetmask != (u_long)0xffffffff) 976 return 1; 977 return (0); 978 #undef ia 979 } 980 981 /* 982 * Add an address to the list of IP multicast addresses for a given interface. 983 */ 984 struct in_multi * 985 in_addmulti(struct in_addr *ap, struct ifnet *ifp) 986 { 987 struct in_multi *inm; 988 989 inm = NULL; 990 991 IFF_LOCKGIANT(ifp); 992 IN_MULTI_LOCK(); 993 994 IN_LOOKUP_MULTI(*ap, ifp, inm); 995 if (inm != NULL) { 996 /* 997 * If we already joined this group, just bump the 998 * refcount and return it. 999 */ 1000 KASSERT(inm->inm_refcount >= 1, 1001 ("%s: bad refcount %d", __func__, inm->inm_refcount)); 1002 ++inm->inm_refcount; 1003 } else do { 1004 struct sockaddr_in sin; 1005 struct ifmultiaddr *ifma; 1006 struct in_multi *ninm; 1007 int error; 1008 1009 bzero(&sin, sizeof sin); 1010 sin.sin_family = AF_INET; 1011 sin.sin_len = sizeof(struct sockaddr_in); 1012 sin.sin_addr = *ap; 1013 1014 /* 1015 * Check if a link-layer group is already associated 1016 * with this network-layer group on the given ifnet. 1017 * If so, bump the refcount on the existing network-layer 1018 * group association and return it. 1019 */ 1020 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma); 1021 if (error) 1022 break; 1023 if (ifma->ifma_protospec != NULL) { 1024 inm = (struct in_multi *)ifma->ifma_protospec; 1025 #ifdef INVARIANTS 1026 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp || 1027 inm->inm_addr.s_addr != ap->s_addr) 1028 panic("%s: ifma is inconsistent", __func__); 1029 #endif 1030 ++inm->inm_refcount; 1031 break; 1032 } 1033 1034 /* 1035 * A new membership is needed; construct it and 1036 * perform the IGMP join. 1037 */ 1038 ninm = malloc(sizeof(*ninm), M_IPMADDR, M_NOWAIT | M_ZERO); 1039 if (ninm == NULL) { 1040 if_delmulti_ifma(ifma); 1041 break; 1042 } 1043 ninm->inm_addr = *ap; 1044 ninm->inm_ifp = ifp; 1045 ninm->inm_ifma = ifma; 1046 ninm->inm_refcount = 1; 1047 ifma->ifma_protospec = ninm; 1048 LIST_INSERT_HEAD(&in_multihead, ninm, inm_link); 1049 1050 igmp_joingroup(ninm); 1051 1052 inm = ninm; 1053 } while (0); 1054 1055 IN_MULTI_UNLOCK(); 1056 IFF_UNLOCKGIANT(ifp); 1057 1058 return (inm); 1059 } 1060 1061 /* 1062 * Delete a multicast address record. 1063 * It is OK to call this routine if the underlying ifnet went away. 1064 * 1065 * XXX: To deal with the ifp going away, we cheat; the link-layer code in net 1066 * will set ifma_ifp to NULL when the associated ifnet instance is detached 1067 * from the system. 1068 * The only reason we need to violate layers and check ifma_ifp here at all 1069 * is because certain hardware drivers still require Giant to be held, 1070 * and it must always be taken before other locks. 1071 */ 1072 void 1073 in_delmulti(struct in_multi *inm) 1074 { 1075 struct ifnet *ifp; 1076 1077 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__)); 1078 ifp = inm->inm_ifma->ifma_ifp; 1079 1080 if (ifp != NULL) { 1081 /* 1082 * Sanity check that netinet's notion of ifp is the 1083 * same as net's. 1084 */ 1085 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__)); 1086 IFF_LOCKGIANT(ifp); 1087 } 1088 1089 IN_MULTI_LOCK(); 1090 in_delmulti_locked(inm); 1091 IN_MULTI_UNLOCK(); 1092 1093 if (ifp != NULL) 1094 IFF_UNLOCKGIANT(ifp); 1095 } 1096 1097 /* 1098 * Delete a multicast address record, with locks held. 1099 * 1100 * It is OK to call this routine if the ifp went away. 1101 * Assumes that caller holds the IN_MULTI lock, and that 1102 * Giant was taken before other locks if required by the hardware. 1103 */ 1104 void 1105 in_delmulti_locked(struct in_multi *inm) 1106 { 1107 struct ifmultiaddr *ifma; 1108 1109 IN_MULTI_LOCK_ASSERT(); 1110 KASSERT(inm->inm_refcount >= 1, ("%s: freeing freed inm", __func__)); 1111 1112 if (--inm->inm_refcount == 0) { 1113 igmp_leavegroup(inm); 1114 1115 ifma = inm->inm_ifma; 1116 KASSERT(ifma->ifma_protospec == inm, 1117 ("%s: ifma_protospec != inm", __func__)); 1118 ifma->ifma_protospec = NULL; 1119 1120 LIST_REMOVE(inm, inm_link); 1121 free(inm, M_IPMADDR); 1122 1123 if_delmulti_ifma(ifma); 1124 } 1125 } 1126 1127 /* 1128 * Delete all IPv4 multicast address records, and associated link-layer 1129 * multicast address records, associated with ifp. 1130 */ 1131 static void 1132 in_purgemaddrs(struct ifnet *ifp) 1133 { 1134 struct in_multi *inm; 1135 struct in_multi *oinm; 1136 1137 IFF_LOCKGIANT(ifp); 1138 IN_MULTI_LOCK(); 1139 LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) { 1140 if (inm->inm_ifp == ifp) 1141 in_delmulti_locked(inm); 1142 } 1143 IN_MULTI_UNLOCK(); 1144 IFF_UNLOCKGIANT(ifp); 1145 } 1146 1147 /* 1148 * On interface removal, clean up IPv4 data structures hung off of the ifnet. 1149 */ 1150 void 1151 in_ifdetach(ifp) 1152 struct ifnet *ifp; 1153 { 1154 1155 in_pcbpurgeif0(&ripcbinfo, ifp); 1156 in_pcbpurgeif0(&udbinfo, ifp); 1157 in_purgemaddrs(ifp); 1158 } 1159