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