1 /* 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)in.c 8.4 (Berkeley) 1/9/95 30 * $FreeBSD$ 31 */ 32 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/sockio.h> 36 #include <sys/malloc.h> 37 #include <sys/socket.h> 38 #include <sys/kernel.h> 39 #include <sys/sysctl.h> 40 41 #include <net/if.h> 42 #include <net/if_types.h> 43 #include <net/route.h> 44 45 #include <netinet/in.h> 46 #include <netinet/in_var.h> 47 #include <netinet/in_pcb.h> 48 49 #include <netinet/igmp_var.h> 50 51 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address"); 52 53 static int in_mask2len(struct in_addr *); 54 static void in_len2mask(struct in_addr *, int); 55 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t, 56 struct ifnet *, struct thread *); 57 58 static void in_socktrim(struct sockaddr_in *); 59 static int in_ifinit(struct ifnet *, 60 struct in_ifaddr *, struct sockaddr_in *, int); 61 62 static int subnetsarelocal = 0; 63 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW, 64 &subnetsarelocal, 0, "Treat all subnets as directly connected"); 65 66 struct in_multihead in_multihead; /* XXX BSS initialization */ 67 68 extern struct inpcbinfo ripcbinfo; 69 extern struct inpcbinfo udbinfo; 70 71 /* 72 * Return 1 if an internet address is for a ``local'' host 73 * (one to which we have a connection). If subnetsarelocal 74 * is true, this includes other subnets of the local net. 75 * Otherwise, it includes only the directly-connected (sub)nets. 76 */ 77 int 78 in_localaddr(in) 79 struct in_addr in; 80 { 81 register u_long i = ntohl(in.s_addr); 82 register struct in_ifaddr *ia; 83 84 if (subnetsarelocal) { 85 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 86 if ((i & ia->ia_netmask) == ia->ia_net) 87 return (1); 88 } else { 89 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) 90 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 91 return (1); 92 } 93 return (0); 94 } 95 96 /* 97 * Determine whether an IP address is in a reserved set of addresses 98 * that may not be forwarded, or whether datagrams to that destination 99 * may be forwarded. 100 */ 101 int 102 in_canforward(in) 103 struct in_addr in; 104 { 105 register u_long i = ntohl(in.s_addr); 106 register u_long net; 107 108 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) 109 return (0); 110 if (IN_CLASSA(i)) { 111 net = i & IN_CLASSA_NET; 112 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 113 return (0); 114 } 115 return (1); 116 } 117 118 /* 119 * Trim a mask in a sockaddr 120 */ 121 static void 122 in_socktrim(ap) 123 struct sockaddr_in *ap; 124 { 125 register char *cplim = (char *) &ap->sin_addr; 126 register char *cp = (char *) (&ap->sin_addr + 1); 127 128 ap->sin_len = 0; 129 while (--cp >= cplim) 130 if (*cp) { 131 (ap)->sin_len = cp - (char *) (ap) + 1; 132 break; 133 } 134 } 135 136 static int 137 in_mask2len(mask) 138 struct in_addr *mask; 139 { 140 int x, y; 141 u_char *p; 142 143 p = (u_char *)mask; 144 for (x = 0; x < sizeof(*mask); x++) { 145 if (p[x] != 0xff) 146 break; 147 } 148 y = 0; 149 if (x < sizeof(*mask)) { 150 for (y = 0; y < 8; y++) { 151 if ((p[x] & (0x80 >> y)) == 0) 152 break; 153 } 154 } 155 return x * 8 + y; 156 } 157 158 static void 159 in_len2mask(mask, len) 160 struct in_addr *mask; 161 int len; 162 { 163 int i; 164 u_char *p; 165 166 p = (u_char *)mask; 167 bzero(mask, sizeof(*mask)); 168 for (i = 0; i < len / 8; i++) 169 p[i] = 0xff; 170 if (len % 8) 171 p[i] = (0xff00 >> (len % 8)) & 0xff; 172 } 173 174 /* 175 * Generic internet control operations (ioctl's). 176 * Ifp is 0 if not an interface-specific ioctl. 177 */ 178 /* ARGSUSED */ 179 int 180 in_control(so, cmd, data, ifp, td) 181 struct socket *so; 182 u_long cmd; 183 caddr_t data; 184 register struct ifnet *ifp; 185 struct thread *td; 186 { 187 register struct ifreq *ifr = (struct ifreq *)data; 188 register struct in_ifaddr *ia = 0, *iap; 189 register struct ifaddr *ifa; 190 struct in_addr dst; 191 struct in_ifaddr *oia; 192 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 193 struct sockaddr_in oldaddr; 194 int error, hostIsNew, iaIsNew, maskIsNew, s; 195 196 iaIsNew = 0; 197 198 switch (cmd) { 199 case SIOCALIFADDR: 200 case SIOCDLIFADDR: 201 if (td && (error = suser(td)) != 0) 202 return error; 203 /*fall through*/ 204 case SIOCGLIFADDR: 205 if (!ifp) 206 return EINVAL; 207 return in_lifaddr_ioctl(so, cmd, data, ifp, td); 208 } 209 210 /* 211 * Find address for this interface, if it exists. 212 * 213 * If an alias address was specified, find that one instead of 214 * the first one on the interface, if possible. 215 */ 216 if (ifp) { 217 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr; 218 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) 219 if (iap->ia_ifp == ifp && 220 iap->ia_addr.sin_addr.s_addr == dst.s_addr) { 221 ia = iap; 222 break; 223 } 224 if (ia == NULL) 225 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 226 iap = ifatoia(ifa); 227 if (iap->ia_addr.sin_family == AF_INET) { 228 ia = iap; 229 break; 230 } 231 } 232 } 233 234 switch (cmd) { 235 236 case SIOCAIFADDR: 237 case SIOCDIFADDR: 238 if (ifp == 0) 239 return (EADDRNOTAVAIL); 240 if (ifra->ifra_addr.sin_family == AF_INET) { 241 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) { 242 if (ia->ia_ifp == ifp && 243 ia->ia_addr.sin_addr.s_addr == 244 ifra->ifra_addr.sin_addr.s_addr) 245 break; 246 } 247 if ((ifp->if_flags & IFF_POINTOPOINT) 248 && (cmd == SIOCAIFADDR) 249 && (ifra->ifra_dstaddr.sin_addr.s_addr 250 == INADDR_ANY)) { 251 return EDESTADDRREQ; 252 } 253 } 254 if (cmd == SIOCDIFADDR && ia == 0) 255 return (EADDRNOTAVAIL); 256 /* FALLTHROUGH */ 257 case SIOCSIFADDR: 258 case SIOCSIFNETMASK: 259 case SIOCSIFDSTADDR: 260 if (td && (error = suser(td)) != 0) 261 return error; 262 263 if (ifp == 0) 264 return (EADDRNOTAVAIL); 265 if (ia == (struct in_ifaddr *)0) { 266 ia = (struct in_ifaddr *) 267 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO); 268 if (ia == (struct in_ifaddr *)NULL) 269 return (ENOBUFS); 270 /* 271 * Protect from ipintr() traversing address list 272 * while we're modifying it. 273 */ 274 s = splnet(); 275 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link); 276 277 ifa = &ia->ia_ifa; 278 IFA_LOCK_INIT(ifa); 279 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 280 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 281 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 282 ifa->ifa_refcnt = 1; 283 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 284 285 ia->ia_sockmask.sin_len = 8; 286 ia->ia_sockmask.sin_family = AF_INET; 287 if (ifp->if_flags & IFF_BROADCAST) { 288 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 289 ia->ia_broadaddr.sin_family = AF_INET; 290 } 291 ia->ia_ifp = ifp; 292 splx(s); 293 iaIsNew = 1; 294 } 295 break; 296 297 case SIOCSIFBRDADDR: 298 if (td && (error = suser(td)) != 0) 299 return error; 300 /* FALLTHROUGH */ 301 302 case SIOCGIFADDR: 303 case SIOCGIFNETMASK: 304 case SIOCGIFDSTADDR: 305 case SIOCGIFBRDADDR: 306 if (ia == (struct in_ifaddr *)0) 307 return (EADDRNOTAVAIL); 308 break; 309 } 310 switch (cmd) { 311 312 case SIOCGIFADDR: 313 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 314 return (0); 315 316 case SIOCGIFBRDADDR: 317 if ((ifp->if_flags & IFF_BROADCAST) == 0) 318 return (EINVAL); 319 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 320 return (0); 321 322 case SIOCGIFDSTADDR: 323 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 324 return (EINVAL); 325 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 326 return (0); 327 328 case SIOCGIFNETMASK: 329 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 330 return (0); 331 332 case SIOCSIFDSTADDR: 333 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 334 return (EINVAL); 335 oldaddr = ia->ia_dstaddr; 336 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 337 if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 338 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 339 ia->ia_dstaddr = oldaddr; 340 return (error); 341 } 342 if (ia->ia_flags & IFA_ROUTE) { 343 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 344 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 345 ia->ia_ifa.ifa_dstaddr = 346 (struct sockaddr *)&ia->ia_dstaddr; 347 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 348 } 349 return (0); 350 351 case SIOCSIFBRDADDR: 352 if ((ifp->if_flags & IFF_BROADCAST) == 0) 353 return (EINVAL); 354 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 355 return (0); 356 357 case SIOCSIFADDR: 358 error = in_ifinit(ifp, ia, 359 (struct sockaddr_in *) &ifr->ifr_addr, 1); 360 if (error != 0 && iaIsNew) 361 break; 362 if (error == 0) 363 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 364 return (0); 365 366 case SIOCSIFNETMASK: 367 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr; 368 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr); 369 return (0); 370 371 case SIOCAIFADDR: 372 maskIsNew = 0; 373 hostIsNew = 1; 374 error = 0; 375 if (ia->ia_addr.sin_family == AF_INET) { 376 if (ifra->ifra_addr.sin_len == 0) { 377 ifra->ifra_addr = ia->ia_addr; 378 hostIsNew = 0; 379 } else if (ifra->ifra_addr.sin_addr.s_addr == 380 ia->ia_addr.sin_addr.s_addr) 381 hostIsNew = 0; 382 } 383 if (ifra->ifra_mask.sin_len) { 384 in_ifscrub(ifp, ia); 385 ia->ia_sockmask = ifra->ifra_mask; 386 ia->ia_sockmask.sin_family = AF_INET; 387 ia->ia_subnetmask = 388 ntohl(ia->ia_sockmask.sin_addr.s_addr); 389 maskIsNew = 1; 390 } 391 if ((ifp->if_flags & IFF_POINTOPOINT) && 392 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 393 in_ifscrub(ifp, ia); 394 ia->ia_dstaddr = ifra->ifra_dstaddr; 395 maskIsNew = 1; /* We lie; but the effect's the same */ 396 } 397 if (ifra->ifra_addr.sin_family == AF_INET && 398 (hostIsNew || maskIsNew)) 399 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 400 if (error != 0 && iaIsNew) 401 break; 402 403 if ((ifp->if_flags & IFF_BROADCAST) && 404 (ifra->ifra_broadaddr.sin_family == AF_INET)) 405 ia->ia_broadaddr = ifra->ifra_broadaddr; 406 if (error == 0) 407 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 408 return (error); 409 410 case SIOCDIFADDR: 411 /* 412 * in_ifscrub kills the interface route. 413 */ 414 in_ifscrub(ifp, ia); 415 /* 416 * in_ifadown gets rid of all the rest of 417 * the routes. This is not quite the right 418 * thing to do, but at least if we are running 419 * a routing process they will come back. 420 */ 421 in_ifadown(&ia->ia_ifa, 1); 422 /* 423 * XXX horrible hack to detect that we are being called 424 * from if_detach() 425 */ 426 if (ifaddr_byindex(ifp->if_index) == NULL) { 427 in_pcbpurgeif0(&ripcbinfo, ifp); 428 in_pcbpurgeif0(&udbinfo, ifp); 429 } 430 EVENTHANDLER_INVOKE(ifaddr_event, ifp); 431 error = 0; 432 break; 433 434 default: 435 if (ifp == 0 || ifp->if_ioctl == 0) 436 return (EOPNOTSUPP); 437 return ((*ifp->if_ioctl)(ifp, cmd, data)); 438 } 439 440 /* 441 * Protect from ipintr() traversing address list while we're modifying 442 * it. 443 */ 444 s = splnet(); 445 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link); 446 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link); 447 LIST_REMOVE(ia, ia_hash); 448 IFAFREE(&ia->ia_ifa); 449 splx(s); 450 451 return (error); 452 } 453 454 /* 455 * SIOC[GAD]LIFADDR. 456 * SIOCGLIFADDR: get first address. (?!?) 457 * SIOCGLIFADDR with IFLR_PREFIX: 458 * get first address that matches the specified prefix. 459 * SIOCALIFADDR: add the specified address. 460 * SIOCALIFADDR with IFLR_PREFIX: 461 * EINVAL since we can't deduce hostid part of the address. 462 * SIOCDLIFADDR: delete the specified address. 463 * SIOCDLIFADDR with IFLR_PREFIX: 464 * delete the first address that matches the specified prefix. 465 * return values: 466 * EINVAL on invalid parameters 467 * EADDRNOTAVAIL on prefix match failed/specified address not found 468 * other values may be returned from in_ioctl() 469 */ 470 static int 471 in_lifaddr_ioctl(so, cmd, data, ifp, td) 472 struct socket *so; 473 u_long cmd; 474 caddr_t data; 475 struct ifnet *ifp; 476 struct thread *td; 477 { 478 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 479 struct ifaddr *ifa; 480 481 /* sanity checks */ 482 if (!data || !ifp) { 483 panic("invalid argument to in_lifaddr_ioctl"); 484 /*NOTRECHED*/ 485 } 486 487 switch (cmd) { 488 case SIOCGLIFADDR: 489 /* address must be specified on GET with IFLR_PREFIX */ 490 if ((iflr->flags & IFLR_PREFIX) == 0) 491 break; 492 /*FALLTHROUGH*/ 493 case SIOCALIFADDR: 494 case SIOCDLIFADDR: 495 /* address must be specified on ADD and DELETE */ 496 if (iflr->addr.ss_family != AF_INET) 497 return EINVAL; 498 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 499 return EINVAL; 500 /* XXX need improvement */ 501 if (iflr->dstaddr.ss_family 502 && iflr->dstaddr.ss_family != AF_INET) 503 return EINVAL; 504 if (iflr->dstaddr.ss_family 505 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 506 return EINVAL; 507 break; 508 default: /*shouldn't happen*/ 509 return EOPNOTSUPP; 510 } 511 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 512 return EINVAL; 513 514 switch (cmd) { 515 case SIOCALIFADDR: 516 { 517 struct in_aliasreq ifra; 518 519 if (iflr->flags & IFLR_PREFIX) 520 return EINVAL; 521 522 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 523 bzero(&ifra, sizeof(ifra)); 524 bcopy(iflr->iflr_name, ifra.ifra_name, 525 sizeof(ifra.ifra_name)); 526 527 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 528 529 if (iflr->dstaddr.ss_family) { /*XXX*/ 530 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 531 iflr->dstaddr.ss_len); 532 } 533 534 ifra.ifra_mask.sin_family = AF_INET; 535 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 536 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 537 538 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td); 539 } 540 case SIOCGLIFADDR: 541 case SIOCDLIFADDR: 542 { 543 struct in_ifaddr *ia; 544 struct in_addr mask, candidate, match; 545 struct sockaddr_in *sin; 546 int cmp; 547 548 bzero(&mask, sizeof(mask)); 549 if (iflr->flags & IFLR_PREFIX) { 550 /* lookup a prefix rather than address. */ 551 in_len2mask(&mask, iflr->prefixlen); 552 553 sin = (struct sockaddr_in *)&iflr->addr; 554 match.s_addr = sin->sin_addr.s_addr; 555 match.s_addr &= mask.s_addr; 556 557 /* if you set extra bits, that's wrong */ 558 if (match.s_addr != sin->sin_addr.s_addr) 559 return EINVAL; 560 561 cmp = 1; 562 } else { 563 if (cmd == SIOCGLIFADDR) { 564 /* on getting an address, take the 1st match */ 565 cmp = 0; /*XXX*/ 566 } else { 567 /* on deleting an address, do exact match */ 568 in_len2mask(&mask, 32); 569 sin = (struct sockaddr_in *)&iflr->addr; 570 match.s_addr = sin->sin_addr.s_addr; 571 572 cmp = 1; 573 } 574 } 575 576 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 577 if (ifa->ifa_addr->sa_family != AF_INET6) 578 continue; 579 if (!cmp) 580 break; 581 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 582 candidate.s_addr &= mask.s_addr; 583 if (candidate.s_addr == match.s_addr) 584 break; 585 } 586 if (!ifa) 587 return EADDRNOTAVAIL; 588 ia = (struct in_ifaddr *)ifa; 589 590 if (cmd == SIOCGLIFADDR) { 591 /* fill in the if_laddrreq structure */ 592 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 593 594 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 595 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 596 ia->ia_dstaddr.sin_len); 597 } else 598 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 599 600 iflr->prefixlen = 601 in_mask2len(&ia->ia_sockmask.sin_addr); 602 603 iflr->flags = 0; /*XXX*/ 604 605 return 0; 606 } else { 607 struct in_aliasreq ifra; 608 609 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 610 bzero(&ifra, sizeof(ifra)); 611 bcopy(iflr->iflr_name, ifra.ifra_name, 612 sizeof(ifra.ifra_name)); 613 614 bcopy(&ia->ia_addr, &ifra.ifra_addr, 615 ia->ia_addr.sin_len); 616 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 617 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 618 ia->ia_dstaddr.sin_len); 619 } 620 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 621 ia->ia_sockmask.sin_len); 622 623 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 624 ifp, td); 625 } 626 } 627 } 628 629 return EOPNOTSUPP; /*just for safety*/ 630 } 631 632 /* 633 * Delete any existing route for an interface. 634 */ 635 void 636 in_ifscrub(ifp, ia) 637 register struct ifnet *ifp; 638 register struct in_ifaddr *ia; 639 { 640 641 if ((ia->ia_flags & IFA_ROUTE) == 0) 642 return; 643 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 644 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 645 else 646 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 647 ia->ia_flags &= ~IFA_ROUTE; 648 } 649 650 /* 651 * Initialize an interface's internet address 652 * and routing table entry. 653 */ 654 static int 655 in_ifinit(ifp, ia, sin, scrub) 656 register struct ifnet *ifp; 657 register struct in_ifaddr *ia; 658 struct sockaddr_in *sin; 659 int scrub; 660 { 661 register u_long i = ntohl(sin->sin_addr.s_addr); 662 struct sockaddr_in oldaddr; 663 int s = splimp(), flags = RTF_UP, error = 0; 664 665 oldaddr = ia->ia_addr; 666 if (oldaddr.sin_family == AF_INET) 667 LIST_REMOVE(ia, ia_hash); 668 ia->ia_addr = *sin; 669 if (ia->ia_addr.sin_family == AF_INET) 670 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 671 ia, ia_hash); 672 /* 673 * Give the interface a chance to initialize 674 * if this is its first address, 675 * and to validate the address if necessary. 676 */ 677 if (ifp->if_ioctl && 678 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 679 splx(s); 680 /* LIST_REMOVE(ia, ia_hash) is done in in_control */ 681 ia->ia_addr = oldaddr; 682 if (ia->ia_addr.sin_family == AF_INET) 683 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), 684 ia, ia_hash); 685 return (error); 686 } 687 splx(s); 688 if (scrub) { 689 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 690 in_ifscrub(ifp, ia); 691 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 692 } 693 if (IN_CLASSA(i)) 694 ia->ia_netmask = IN_CLASSA_NET; 695 else if (IN_CLASSB(i)) 696 ia->ia_netmask = IN_CLASSB_NET; 697 else 698 ia->ia_netmask = IN_CLASSC_NET; 699 /* 700 * The subnet mask usually includes at least the standard network part, 701 * but may may be smaller in the case of supernetting. 702 * If it is set, we believe it. 703 */ 704 if (ia->ia_subnetmask == 0) { 705 ia->ia_subnetmask = ia->ia_netmask; 706 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 707 } else 708 ia->ia_netmask &= ia->ia_subnetmask; 709 ia->ia_net = i & ia->ia_netmask; 710 ia->ia_subnet = i & ia->ia_subnetmask; 711 in_socktrim(&ia->ia_sockmask); 712 /* 713 * Add route for the network. 714 */ 715 ia->ia_ifa.ifa_metric = ifp->if_metric; 716 if (ifp->if_flags & IFF_BROADCAST) { 717 ia->ia_broadaddr.sin_addr.s_addr = 718 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 719 ia->ia_netbroadcast.s_addr = 720 htonl(ia->ia_net | ~ ia->ia_netmask); 721 } else if (ifp->if_flags & IFF_LOOPBACK) { 722 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 723 flags |= RTF_HOST; 724 } else if (ifp->if_flags & IFF_POINTOPOINT) { 725 if (ia->ia_dstaddr.sin_family != AF_INET) 726 return (0); 727 flags |= RTF_HOST; 728 } 729 730 /*- 731 * Don't add host routes for interface addresses of 732 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it 733 * possible to assign several such address pairs with consistent 734 * results (no host route) and is required by BOOTP. 735 * 736 * XXX: This is ugly ! There should be a way for the caller to 737 * say that they don't want a host route. 738 */ 739 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY || 740 ia->ia_netmask != IN_CLASSA_NET || 741 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) { 742 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) { 743 ia->ia_addr = oldaddr; 744 return (error); 745 } 746 ia->ia_flags |= IFA_ROUTE; 747 } 748 749 /* 750 * If the interface supports multicast, join the "all hosts" 751 * multicast group on that interface. 752 */ 753 if (ifp->if_flags & IFF_MULTICAST) { 754 struct in_addr addr; 755 756 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 757 in_addmulti(&addr, ifp); 758 } 759 return (error); 760 } 761 762 763 /* 764 * Return 1 if the address might be a local broadcast address. 765 */ 766 int 767 in_broadcast(in, ifp) 768 struct in_addr in; 769 struct ifnet *ifp; 770 { 771 register struct ifaddr *ifa; 772 u_long t; 773 774 if (in.s_addr == INADDR_BROADCAST || 775 in.s_addr == INADDR_ANY) 776 return 1; 777 if ((ifp->if_flags & IFF_BROADCAST) == 0) 778 return 0; 779 t = ntohl(in.s_addr); 780 /* 781 * Look through the list of addresses for a match 782 * with a broadcast address. 783 */ 784 #define ia ((struct in_ifaddr *)ifa) 785 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 786 if (ifa->ifa_addr->sa_family == AF_INET && 787 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 788 in.s_addr == ia->ia_netbroadcast.s_addr || 789 /* 790 * Check for old-style (host 0) broadcast. 791 */ 792 t == ia->ia_subnet || t == ia->ia_net) && 793 /* 794 * Check for an all one subnetmask. These 795 * only exist when an interface gets a secondary 796 * address. 797 */ 798 ia->ia_subnetmask != (u_long)0xffffffff) 799 return 1; 800 return (0); 801 #undef ia 802 } 803 /* 804 * Add an address to the list of IP multicast addresses for a given interface. 805 */ 806 struct in_multi * 807 in_addmulti(ap, ifp) 808 register struct in_addr *ap; 809 register struct ifnet *ifp; 810 { 811 register struct in_multi *inm; 812 int error; 813 struct sockaddr_in sin; 814 struct ifmultiaddr *ifma; 815 int s = splnet(); 816 817 /* 818 * Call generic routine to add membership or increment 819 * refcount. It wants addresses in the form of a sockaddr, 820 * so we build one here (being careful to zero the unused bytes). 821 */ 822 bzero(&sin, sizeof sin); 823 sin.sin_family = AF_INET; 824 sin.sin_len = sizeof sin; 825 sin.sin_addr = *ap; 826 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma); 827 if (error) { 828 splx(s); 829 return 0; 830 } 831 832 /* 833 * If ifma->ifma_protospec is null, then if_addmulti() created 834 * a new record. Otherwise, we are done. 835 */ 836 if (ifma->ifma_protospec != 0) { 837 splx(s); 838 return ifma->ifma_protospec; 839 } 840 841 /* XXX - if_addmulti uses M_WAITOK. Can this really be called 842 at interrupt time? If so, need to fix if_addmulti. XXX */ 843 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, 844 M_NOWAIT | M_ZERO); 845 if (inm == NULL) { 846 splx(s); 847 return (NULL); 848 } 849 850 inm->inm_addr = *ap; 851 inm->inm_ifp = ifp; 852 inm->inm_ifma = ifma; 853 ifma->ifma_protospec = inm; 854 LIST_INSERT_HEAD(&in_multihead, inm, inm_link); 855 856 /* 857 * Let IGMP know that we have joined a new IP multicast group. 858 */ 859 igmp_joingroup(inm); 860 splx(s); 861 return (inm); 862 } 863 864 /* 865 * Delete a multicast address record. 866 */ 867 void 868 in_delmulti(inm) 869 register struct in_multi *inm; 870 { 871 struct ifmultiaddr *ifma = inm->inm_ifma; 872 struct in_multi my_inm; 873 int s = splnet(); 874 875 my_inm.inm_ifp = NULL ; /* don't send the leave msg */ 876 if (ifma->ifma_refcount == 1) { 877 /* 878 * No remaining claims to this record; let IGMP know that 879 * we are leaving the multicast group. 880 * But do it after the if_delmulti() which might reset 881 * the interface and nuke the packet. 882 */ 883 my_inm = *inm ; 884 ifma->ifma_protospec = 0; 885 LIST_REMOVE(inm, inm_link); 886 free(inm, M_IPMADDR); 887 } 888 /* XXX - should be separate API for when we have an ifma? */ 889 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 890 if (my_inm.inm_ifp != NULL) 891 igmp_leavegroup(&my_inm); 892 splx(s); 893 } 894