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