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