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 if (p && 211 (error = suser(p)) != 0) 212 return(error); 213 case SIOCGIFPSRCADDR: 214 case SIOCGIFPDSTADDR: 215 return gif_ioctl(ifp, cmd, data); 216 } 217 } 218 #endif 219 220 switch (cmd) { 221 case SIOCALIFADDR: 222 case SIOCDLIFADDR: 223 if (p && (error = suser(p)) != 0) 224 return error; 225 /*fall through*/ 226 case SIOCGLIFADDR: 227 if (!ifp) 228 return EINVAL; 229 return in_lifaddr_ioctl(so, cmd, data, ifp, p); 230 } 231 232 /* 233 * Find address for this interface, if it exists. 234 * 235 * If an alias address was specified, find that one instead of 236 * the first one on the interface. 237 */ 238 if (ifp) 239 for (iap = in_ifaddrhead.tqh_first; iap; 240 iap = iap->ia_link.tqe_next) 241 if (iap->ia_ifp == ifp) { 242 if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr == 243 iap->ia_addr.sin_addr.s_addr) { 244 ia = iap; 245 break; 246 } else if (ia == NULL) { 247 ia = iap; 248 if (ifr->ifr_addr.sa_family != AF_INET) 249 break; 250 } 251 } 252 253 switch (cmd) { 254 255 case SIOCAIFADDR: 256 case SIOCDIFADDR: 257 if (ifp == 0) 258 return (EADDRNOTAVAIL); 259 if (ifra->ifra_addr.sin_family == AF_INET) { 260 for (oia = ia; ia; ia = ia->ia_link.tqe_next) { 261 if (ia->ia_ifp == ifp && 262 ia->ia_addr.sin_addr.s_addr == 263 ifra->ifra_addr.sin_addr.s_addr) 264 break; 265 } 266 if ((ifp->if_flags & IFF_POINTOPOINT) 267 && (cmd == SIOCAIFADDR) 268 && (ifra->ifra_dstaddr.sin_addr.s_addr 269 == INADDR_ANY)) { 270 return EDESTADDRREQ; 271 } 272 } 273 if (cmd == SIOCDIFADDR && ia == 0) 274 return (EADDRNOTAVAIL); 275 /* FALLTHROUGH */ 276 case SIOCSIFADDR: 277 case SIOCSIFNETMASK: 278 case SIOCSIFDSTADDR: 279 if (p && (error = suser(p)) != 0) 280 return error; 281 282 if (ifp == 0) 283 return (EADDRNOTAVAIL); 284 if (ia == (struct in_ifaddr *)0) { 285 ia = (struct in_ifaddr *) 286 malloc(sizeof *ia, M_IFADDR, M_WAITOK); 287 if (ia == (struct in_ifaddr *)NULL) 288 return (ENOBUFS); 289 bzero((caddr_t)ia, sizeof *ia); 290 /* 291 * Protect from ipintr() traversing address list 292 * while we're modifying it. 293 */ 294 s = splnet(); 295 296 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link); 297 ifa = &ia->ia_ifa; 298 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link); 299 300 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr; 301 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr; 302 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask; 303 ia->ia_sockmask.sin_len = 8; 304 if (ifp->if_flags & IFF_BROADCAST) { 305 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 306 ia->ia_broadaddr.sin_family = AF_INET; 307 } 308 ia->ia_ifp = ifp; 309 if (!(ifp->if_flags & IFF_LOOPBACK)) 310 in_interfaces++; 311 splx(s); 312 } 313 break; 314 315 case SIOCSIFBRDADDR: 316 if (p && (error = suser(p)) != 0) 317 return error; 318 /* FALLTHROUGH */ 319 320 case SIOCGIFADDR: 321 case SIOCGIFNETMASK: 322 case SIOCGIFDSTADDR: 323 case SIOCGIFBRDADDR: 324 if (ia == (struct in_ifaddr *)0) 325 return (EADDRNOTAVAIL); 326 break; 327 } 328 switch (cmd) { 329 330 case SIOCGIFADDR: 331 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 332 break; 333 334 case SIOCGIFBRDADDR: 335 if ((ifp->if_flags & IFF_BROADCAST) == 0) 336 return (EINVAL); 337 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 338 break; 339 340 case SIOCGIFDSTADDR: 341 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 342 return (EINVAL); 343 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 344 break; 345 346 case SIOCGIFNETMASK: 347 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 348 break; 349 350 case SIOCSIFDSTADDR: 351 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 352 return (EINVAL); 353 oldaddr = ia->ia_dstaddr; 354 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 355 if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 356 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 357 ia->ia_dstaddr = oldaddr; 358 return (error); 359 } 360 if (ia->ia_flags & IFA_ROUTE) { 361 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 362 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 363 ia->ia_ifa.ifa_dstaddr = 364 (struct sockaddr *)&ia->ia_dstaddr; 365 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 366 } 367 break; 368 369 case SIOCSIFBRDADDR: 370 if ((ifp->if_flags & IFF_BROADCAST) == 0) 371 return (EINVAL); 372 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 373 break; 374 375 case SIOCSIFADDR: 376 return (in_ifinit(ifp, ia, 377 (struct sockaddr_in *) &ifr->ifr_addr, 1)); 378 379 case SIOCSIFNETMASK: 380 i = ifra->ifra_addr.sin_addr.s_addr; 381 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i); 382 break; 383 384 case SIOCAIFADDR: 385 maskIsNew = 0; 386 hostIsNew = 1; 387 error = 0; 388 if (ia->ia_addr.sin_family == AF_INET) { 389 if (ifra->ifra_addr.sin_len == 0) { 390 ifra->ifra_addr = ia->ia_addr; 391 hostIsNew = 0; 392 } else if (ifra->ifra_addr.sin_addr.s_addr == 393 ia->ia_addr.sin_addr.s_addr) 394 hostIsNew = 0; 395 } 396 if (ifra->ifra_mask.sin_len) { 397 in_ifscrub(ifp, ia); 398 ia->ia_sockmask = ifra->ifra_mask; 399 ia->ia_subnetmask = 400 ntohl(ia->ia_sockmask.sin_addr.s_addr); 401 maskIsNew = 1; 402 } 403 if ((ifp->if_flags & IFF_POINTOPOINT) && 404 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 405 in_ifscrub(ifp, ia); 406 ia->ia_dstaddr = ifra->ifra_dstaddr; 407 maskIsNew = 1; /* We lie; but the effect's the same */ 408 } 409 if (ifra->ifra_addr.sin_family == AF_INET && 410 (hostIsNew || maskIsNew)) 411 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 412 if ((ifp->if_flags & IFF_BROADCAST) && 413 (ifra->ifra_broadaddr.sin_family == AF_INET)) 414 ia->ia_broadaddr = ifra->ifra_broadaddr; 415 return (error); 416 417 case SIOCDIFADDR: 418 in_ifscrub(ifp, ia); 419 /* 420 * Protect from ipintr() traversing address list 421 * while we're modifying it. 422 */ 423 s = splnet(); 424 425 ifa = &ia->ia_ifa; 426 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link); 427 oia = ia; 428 TAILQ_REMOVE(&in_ifaddrhead, oia, ia_link); 429 IFAFREE(&oia->ia_ifa); 430 splx(s); 431 break; 432 433 default: 434 if (ifp == 0 || ifp->if_ioctl == 0) 435 return (EOPNOTSUPP); 436 return ((*ifp->if_ioctl)(ifp, cmd, data)); 437 } 438 return (0); 439 } 440 441 /* 442 * SIOC[GAD]LIFADDR. 443 * SIOCGLIFADDR: get first address. (?!?) 444 * SIOCGLIFADDR with IFLR_PREFIX: 445 * get first address that matches the specified prefix. 446 * SIOCALIFADDR: add the specified address. 447 * SIOCALIFADDR with IFLR_PREFIX: 448 * EINVAL since we can't deduce hostid part of the address. 449 * SIOCDLIFADDR: delete the specified address. 450 * SIOCDLIFADDR with IFLR_PREFIX: 451 * delete the first address that matches the specified prefix. 452 * return values: 453 * EINVAL on invalid parameters 454 * EADDRNOTAVAIL on prefix match failed/specified address not found 455 * other values may be returned from in_ioctl() 456 */ 457 static int 458 in_lifaddr_ioctl(so, cmd, data, ifp, p) 459 struct socket *so; 460 u_long cmd; 461 caddr_t data; 462 struct ifnet *ifp; 463 struct proc *p; 464 { 465 struct if_laddrreq *iflr = (struct if_laddrreq *)data; 466 struct ifaddr *ifa; 467 468 /* sanity checks */ 469 if (!data || !ifp) { 470 panic("invalid argument to in_lifaddr_ioctl"); 471 /*NOTRECHED*/ 472 } 473 474 switch (cmd) { 475 case SIOCGLIFADDR: 476 /* address must be specified on GET with IFLR_PREFIX */ 477 if ((iflr->flags & IFLR_PREFIX) == 0) 478 break; 479 /*FALLTHROUGH*/ 480 case SIOCALIFADDR: 481 case SIOCDLIFADDR: 482 /* address must be specified on ADD and DELETE */ 483 if (iflr->addr.ss_family != AF_INET) 484 return EINVAL; 485 if (iflr->addr.ss_len != sizeof(struct sockaddr_in)) 486 return EINVAL; 487 /* XXX need improvement */ 488 if (iflr->dstaddr.ss_family 489 && iflr->dstaddr.ss_family != AF_INET) 490 return EINVAL; 491 if (iflr->dstaddr.ss_family 492 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in)) 493 return EINVAL; 494 break; 495 default: /*shouldn't happen*/ 496 return EOPNOTSUPP; 497 } 498 if (sizeof(struct in_addr) * 8 < iflr->prefixlen) 499 return EINVAL; 500 501 switch (cmd) { 502 case SIOCALIFADDR: 503 { 504 struct in_aliasreq ifra; 505 506 if (iflr->flags & IFLR_PREFIX) 507 return EINVAL; 508 509 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */ 510 bzero(&ifra, sizeof(ifra)); 511 bcopy(iflr->iflr_name, ifra.ifra_name, 512 sizeof(ifra.ifra_name)); 513 514 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len); 515 516 if (iflr->dstaddr.ss_family) { /*XXX*/ 517 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr, 518 iflr->dstaddr.ss_len); 519 } 520 521 ifra.ifra_mask.sin_family = AF_INET; 522 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in); 523 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen); 524 525 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p); 526 } 527 case SIOCGLIFADDR: 528 case SIOCDLIFADDR: 529 { 530 struct in_ifaddr *ia; 531 struct in_addr mask, candidate, match; 532 struct sockaddr_in *sin; 533 int cmp; 534 535 bzero(&mask, sizeof(mask)); 536 if (iflr->flags & IFLR_PREFIX) { 537 /* lookup a prefix rather than address. */ 538 in_len2mask(&mask, iflr->prefixlen); 539 540 sin = (struct sockaddr_in *)&iflr->addr; 541 match.s_addr = sin->sin_addr.s_addr; 542 match.s_addr &= mask.s_addr; 543 544 /* if you set extra bits, that's wrong */ 545 if (match.s_addr != sin->sin_addr.s_addr) 546 return EINVAL; 547 548 cmp = 1; 549 } else { 550 if (cmd == SIOCGLIFADDR) { 551 /* on getting an address, take the 1st match */ 552 cmp = 0; /*XXX*/ 553 } else { 554 /* on deleting an address, do exact match */ 555 in_len2mask(&mask, 32); 556 sin = (struct sockaddr_in *)&iflr->addr; 557 match.s_addr = sin->sin_addr.s_addr; 558 559 cmp = 1; 560 } 561 } 562 563 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 564 if (ifa->ifa_addr->sa_family != AF_INET6) 565 continue; 566 if (!cmp) 567 break; 568 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr; 569 candidate.s_addr &= mask.s_addr; 570 if (candidate.s_addr == match.s_addr) 571 break; 572 } 573 if (!ifa) 574 return EADDRNOTAVAIL; 575 ia = (struct in_ifaddr *)ifa; 576 577 if (cmd == SIOCGLIFADDR) { 578 /* fill in the if_laddrreq structure */ 579 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len); 580 581 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 582 bcopy(&ia->ia_dstaddr, &iflr->dstaddr, 583 ia->ia_dstaddr.sin_len); 584 } else 585 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr)); 586 587 iflr->prefixlen = 588 in_mask2len(&ia->ia_sockmask.sin_addr); 589 590 iflr->flags = 0; /*XXX*/ 591 592 return 0; 593 } else { 594 struct in_aliasreq ifra; 595 596 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */ 597 bzero(&ifra, sizeof(ifra)); 598 bcopy(iflr->iflr_name, ifra.ifra_name, 599 sizeof(ifra.ifra_name)); 600 601 bcopy(&ia->ia_addr, &ifra.ifra_addr, 602 ia->ia_addr.sin_len); 603 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) { 604 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr, 605 ia->ia_dstaddr.sin_len); 606 } 607 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr, 608 ia->ia_sockmask.sin_len); 609 610 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra, 611 ifp, p); 612 } 613 } 614 } 615 616 return EOPNOTSUPP; /*just for safety*/ 617 } 618 619 /* 620 * Delete any existing route for an interface. 621 */ 622 void 623 in_ifscrub(ifp, ia) 624 register struct ifnet *ifp; 625 register struct in_ifaddr *ia; 626 { 627 628 if ((ia->ia_flags & IFA_ROUTE) == 0) 629 return; 630 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 631 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 632 else 633 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 634 ia->ia_flags &= ~IFA_ROUTE; 635 } 636 637 /* 638 * Initialize an interface's internet address 639 * and routing table entry. 640 */ 641 static int 642 in_ifinit(ifp, ia, sin, scrub) 643 register struct ifnet *ifp; 644 register struct in_ifaddr *ia; 645 struct sockaddr_in *sin; 646 int scrub; 647 { 648 register u_long i = ntohl(sin->sin_addr.s_addr); 649 struct sockaddr_in oldaddr; 650 int s = splimp(), flags = RTF_UP, error; 651 652 oldaddr = ia->ia_addr; 653 ia->ia_addr = *sin; 654 /* 655 * Give the interface a chance to initialize 656 * if this is its first address, 657 * and to validate the address if necessary. 658 */ 659 if (ifp->if_ioctl && 660 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 661 splx(s); 662 ia->ia_addr = oldaddr; 663 return (error); 664 } 665 splx(s); 666 if (scrub) { 667 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 668 in_ifscrub(ifp, ia); 669 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 670 } 671 if (IN_CLASSA(i)) 672 ia->ia_netmask = IN_CLASSA_NET; 673 else if (IN_CLASSB(i)) 674 ia->ia_netmask = IN_CLASSB_NET; 675 else 676 ia->ia_netmask = IN_CLASSC_NET; 677 /* 678 * The subnet mask usually includes at least the standard network part, 679 * but may may be smaller in the case of supernetting. 680 * If it is set, we believe it. 681 */ 682 if (ia->ia_subnetmask == 0) { 683 ia->ia_subnetmask = ia->ia_netmask; 684 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 685 } else 686 ia->ia_netmask &= ia->ia_subnetmask; 687 ia->ia_net = i & ia->ia_netmask; 688 ia->ia_subnet = i & ia->ia_subnetmask; 689 in_socktrim(&ia->ia_sockmask); 690 /* 691 * Add route for the network. 692 */ 693 ia->ia_ifa.ifa_metric = ifp->if_metric; 694 if (ifp->if_flags & IFF_BROADCAST) { 695 ia->ia_broadaddr.sin_addr.s_addr = 696 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 697 ia->ia_netbroadcast.s_addr = 698 htonl(ia->ia_net | ~ ia->ia_netmask); 699 } else if (ifp->if_flags & IFF_LOOPBACK) { 700 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 701 flags |= RTF_HOST; 702 } else if (ifp->if_flags & IFF_POINTOPOINT) { 703 if (ia->ia_dstaddr.sin_family != AF_INET) 704 return (0); 705 flags |= RTF_HOST; 706 } 707 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 708 ia->ia_flags |= IFA_ROUTE; 709 710 /* 711 * If the interface supports multicast, join the "all hosts" 712 * multicast group on that interface. 713 */ 714 if (ifp->if_flags & IFF_MULTICAST) { 715 struct in_addr addr; 716 717 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 718 in_addmulti(&addr, ifp); 719 } 720 return (error); 721 } 722 723 724 /* 725 * Return 1 if the address might be a local broadcast address. 726 */ 727 int 728 in_broadcast(in, ifp) 729 struct in_addr in; 730 struct ifnet *ifp; 731 { 732 register struct ifaddr *ifa; 733 u_long t; 734 735 if (in.s_addr == INADDR_BROADCAST || 736 in.s_addr == INADDR_ANY) 737 return 1; 738 if ((ifp->if_flags & IFF_BROADCAST) == 0) 739 return 0; 740 t = ntohl(in.s_addr); 741 /* 742 * Look through the list of addresses for a match 743 * with a broadcast address. 744 */ 745 #define ia ((struct in_ifaddr *)ifa) 746 for (ifa = ifp->if_addrhead.tqh_first; ifa; 747 ifa = ifa->ifa_link.tqe_next) 748 if (ifa->ifa_addr->sa_family == AF_INET && 749 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 750 in.s_addr == ia->ia_netbroadcast.s_addr || 751 /* 752 * Check for old-style (host 0) broadcast. 753 */ 754 t == ia->ia_subnet || t == ia->ia_net) && 755 /* 756 * Check for an all one subnetmask. These 757 * only exist when an interface gets a secondary 758 * address. 759 */ 760 ia->ia_subnetmask != (u_long)0xffffffff) 761 return 1; 762 return (0); 763 #undef ia 764 } 765 /* 766 * Add an address to the list of IP multicast addresses for a given interface. 767 */ 768 struct in_multi * 769 in_addmulti(ap, ifp) 770 register struct in_addr *ap; 771 register struct ifnet *ifp; 772 { 773 register struct in_multi *inm; 774 int error; 775 struct sockaddr_in sin; 776 struct ifmultiaddr *ifma; 777 int s = splnet(); 778 779 /* 780 * Call generic routine to add membership or increment 781 * refcount. It wants addresses in the form of a sockaddr, 782 * so we build one here (being careful to zero the unused bytes). 783 */ 784 bzero(&sin, sizeof sin); 785 sin.sin_family = AF_INET; 786 sin.sin_len = sizeof sin; 787 sin.sin_addr = *ap; 788 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma); 789 if (error) { 790 splx(s); 791 return 0; 792 } 793 794 /* 795 * If ifma->ifma_protospec is null, then if_addmulti() created 796 * a new record. Otherwise, we are done. 797 */ 798 if (ifma->ifma_protospec != 0) 799 return ifma->ifma_protospec; 800 801 /* XXX - if_addmulti uses M_WAITOK. Can this really be called 802 at interrupt time? If so, need to fix if_addmulti. XXX */ 803 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT); 804 if (inm == NULL) { 805 splx(s); 806 return (NULL); 807 } 808 809 bzero(inm, sizeof *inm); 810 inm->inm_addr = *ap; 811 inm->inm_ifp = ifp; 812 inm->inm_ifma = ifma; 813 ifma->ifma_protospec = inm; 814 LIST_INSERT_HEAD(&in_multihead, inm, inm_link); 815 816 /* 817 * Let IGMP know that we have joined a new IP multicast group. 818 */ 819 igmp_joingroup(inm); 820 splx(s); 821 return (inm); 822 } 823 824 /* 825 * Delete a multicast address record. 826 */ 827 void 828 in_delmulti(inm) 829 register struct in_multi *inm; 830 { 831 struct ifmultiaddr *ifma = inm->inm_ifma; 832 struct in_multi my_inm; 833 int s = splnet(); 834 835 my_inm.inm_ifp = NULL ; /* don't send the leave msg */ 836 if (ifma->ifma_refcount == 1) { 837 /* 838 * No remaining claims to this record; let IGMP know that 839 * we are leaving the multicast group. 840 * But do it after the if_delmulti() which might reset 841 * the interface and nuke the packet. 842 */ 843 my_inm = *inm ; 844 ifma->ifma_protospec = 0; 845 LIST_REMOVE(inm, inm_link); 846 free(inm, M_IPMADDR); 847 } 848 /* XXX - should be separate API for when we have an ifma? */ 849 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr); 850 if (my_inm.inm_ifp != NULL) 851 igmp_leavegroup(&my_inm); 852 splx(s); 853 } 854