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