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