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