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