in.c revision 7280
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.2 (Berkeley) 11/15/93 34 * $Id: in.c,v 1.10 1995/03/16 18:14:50 bde Exp $ 35 */ 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/ioctl.h> 40#include <sys/errno.h> 41#include <sys/malloc.h> 42#include <sys/socket.h> 43#include <sys/socketvar.h> 44#include <sys/queue.h> 45 46#include <net/if.h> 47#include <net/route.h> 48 49#include <netinet/in_systm.h> 50#include <netinet/in.h> 51#include <netinet/in_var.h> 52#include <netinet/if_ether.h> 53 54#include <netinet/igmp_var.h> 55 56/* 57 * This structure is used to keep track of in_multi chains which belong to 58 * deleted interface addresses. 59 */ 60static LIST_HEAD(, multi_kludge) in_mk; /* XXX BSS initialization */ 61 62struct multi_kludge { 63 LIST_ENTRY(multi_kludge) mk_entry; 64 struct ifnet *mk_ifp; 65 struct in_multihead mk_head; 66}; 67 68/* 69 * Return the network number from an internet address. 70 */ 71u_long 72in_netof(in) 73 struct in_addr in; 74{ 75 register u_long i = ntohl(in.s_addr); 76 register u_long net; 77 register struct in_ifaddr *ia; 78 79 if (IN_CLASSA(i)) 80 net = i & IN_CLASSA_NET; 81 else if (IN_CLASSB(i)) 82 net = i & IN_CLASSB_NET; 83 else if (IN_CLASSC(i)) 84 net = i & IN_CLASSC_NET; 85 else if (IN_CLASSD(i)) 86 net = i & IN_CLASSD_NET; 87 else 88 return (0); 89 90 /* 91 * Check whether network is a subnet; 92 * if so, return subnet number. 93 */ 94 for (ia = in_ifaddr; ia; ia = ia->ia_next) 95 if (net == ia->ia_net) 96 return (i & ia->ia_subnetmask); 97 return (net); 98} 99 100#ifndef SUBNETSARELOCAL 101#define SUBNETSARELOCAL 1 102#endif 103int subnetsarelocal = SUBNETSARELOCAL; 104/* 105 * Return 1 if an internet address is for a ``local'' host 106 * (one to which we have a connection). If subnetsarelocal 107 * is true, this includes other subnets of the local net. 108 * Otherwise, it includes only the directly-connected (sub)nets. 109 */ 110int 111in_localaddr(in) 112 struct in_addr in; 113{ 114 register u_long i = ntohl(in.s_addr); 115 register struct in_ifaddr *ia; 116 117 if (subnetsarelocal) { 118 for (ia = in_ifaddr; ia; ia = ia->ia_next) 119 if ((i & ia->ia_netmask) == ia->ia_net) 120 return (1); 121 } else { 122 for (ia = in_ifaddr; ia; ia = ia->ia_next) 123 if ((i & ia->ia_subnetmask) == ia->ia_subnet) 124 return (1); 125 } 126 return (0); 127} 128 129/* 130 * Determine whether an IP address is in a reserved set of addresses 131 * that may not be forwarded, or whether datagrams to that destination 132 * may be forwarded. 133 */ 134int 135in_canforward(in) 136 struct in_addr in; 137{ 138 register u_long i = ntohl(in.s_addr); 139 register u_long net; 140 141 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i)) 142 return (0); 143 if (IN_CLASSA(i)) { 144 net = i & IN_CLASSA_NET; 145 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT)) 146 return (0); 147 } 148 return (1); 149} 150 151/* 152 * Trim a mask in a sockaddr 153 */ 154void 155in_socktrim(ap) 156struct sockaddr_in *ap; 157{ 158 register char *cplim = (char *) &ap->sin_addr; 159 register char *cp = (char *) (&ap->sin_addr + 1); 160 161 ap->sin_len = 0; 162 while (--cp >= cplim) 163 if (*cp) { 164 (ap)->sin_len = cp - (char *) (ap) + 1; 165 break; 166 } 167} 168 169int in_interfaces; /* number of external internet interfaces */ 170 171/* 172 * Generic internet control operations (ioctl's). 173 * Ifp is 0 if not an interface-specific ioctl. 174 */ 175/* ARGSUSED */ 176int 177in_control(so, cmd, data, ifp) 178 struct socket *so; 179 int cmd; 180 caddr_t data; 181 register struct ifnet *ifp; 182{ 183 register struct ifreq *ifr = (struct ifreq *)data; 184 register struct in_ifaddr *ia = 0; 185 register struct ifaddr *ifa; 186 struct in_ifaddr *oia; 187 struct in_aliasreq *ifra = (struct in_aliasreq *)data; 188 struct sockaddr_in oldaddr; 189 int error, hostIsNew, maskIsNew; 190 u_long i; 191 struct multi_kludge *mk; 192 193 /* 194 * Find address for this interface, if it exists. 195 */ 196 if (ifp) 197 for (ia = in_ifaddr; ia; ia = ia->ia_next) 198 if (ia->ia_ifp == ifp) 199 break; 200 201 switch (cmd) { 202 203 case SIOCAIFADDR: 204 case SIOCDIFADDR: 205 if (ifra->ifra_addr.sin_family == AF_INET) 206 for (oia = ia; ia; ia = ia->ia_next) { 207 if (ia->ia_ifp == ifp && 208 ia->ia_addr.sin_addr.s_addr == 209 ifra->ifra_addr.sin_addr.s_addr) 210 break; 211 } 212 if (cmd == SIOCDIFADDR && ia == 0) 213 return (EADDRNOTAVAIL); 214 /* FALLTHROUGH */ 215 case SIOCSIFADDR: 216 case SIOCSIFNETMASK: 217 case SIOCSIFDSTADDR: 218 if ((so->so_state & SS_PRIV) == 0) 219 return (EPERM); 220 221 if (ifp == 0) 222 panic("in_control"); 223 if (ia == (struct in_ifaddr *)0) { 224 oia = (struct in_ifaddr *) 225 malloc(sizeof *oia, M_IFADDR, M_WAITOK); 226 if (oia == (struct in_ifaddr *)NULL) 227 return (ENOBUFS); 228 bzero((caddr_t)oia, sizeof *oia); 229 ia = in_ifaddr; 230 if (ia) { 231 for ( ; ia->ia_next; ia = ia->ia_next) 232 continue; 233 ia->ia_next = oia; 234 } else 235 in_ifaddr = oia; 236 ia = oia; 237 ifa = ifp->if_addrlist; 238 if (ifa) { 239 for ( ; ifa->ifa_next; ifa = ifa->ifa_next) 240 continue; 241 ifa->ifa_next = (struct ifaddr *) ia; 242 } else 243 ifp->if_addrlist = (struct ifaddr *) ia; 244 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 245 ia->ia_ifa.ifa_dstaddr 246 = (struct sockaddr *)&ia->ia_dstaddr; 247 ia->ia_ifa.ifa_netmask 248 = (struct sockaddr *)&ia->ia_sockmask; 249 ia->ia_sockmask.sin_len = 8; 250 if (ifp->if_flags & IFF_BROADCAST) { 251 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); 252 ia->ia_broadaddr.sin_family = AF_INET; 253 } 254 ia->ia_ifp = ifp; 255 if (ifp != &loif) 256 in_interfaces++; 257 } 258 break; 259 260 case SIOCSIFBRDADDR: 261 if ((so->so_state & SS_PRIV) == 0) 262 return (EPERM); 263 /* FALLTHROUGH */ 264 265 case SIOCGIFADDR: 266 case SIOCGIFNETMASK: 267 case SIOCGIFDSTADDR: 268 case SIOCGIFBRDADDR: 269 if (ia == (struct in_ifaddr *)0) 270 return (EADDRNOTAVAIL); 271 break; 272 } 273 switch (cmd) { 274 275 case SIOCGIFADDR: 276 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; 277 break; 278 279 case SIOCGIFBRDADDR: 280 if ((ifp->if_flags & IFF_BROADCAST) == 0) 281 return (EINVAL); 282 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; 283 break; 284 285 case SIOCGIFDSTADDR: 286 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 287 return (EINVAL); 288 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; 289 break; 290 291 case SIOCGIFNETMASK: 292 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; 293 break; 294 295 case SIOCSIFDSTADDR: 296 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) 297 return (EINVAL); 298 oldaddr = ia->ia_dstaddr; 299 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; 300 if (ifp->if_ioctl && (error = (*ifp->if_ioctl) 301 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) { 302 ia->ia_dstaddr = oldaddr; 303 return (error); 304 } 305 if (ia->ia_flags & IFA_ROUTE) { 306 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; 307 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 308 ia->ia_ifa.ifa_dstaddr = 309 (struct sockaddr *)&ia->ia_dstaddr; 310 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); 311 } 312 break; 313 314 case SIOCSIFBRDADDR: 315 if ((ifp->if_flags & IFF_BROADCAST) == 0) 316 return (EINVAL); 317 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; 318 break; 319 320 case SIOCSIFADDR: 321 return (in_ifinit(ifp, ia, 322 (struct sockaddr_in *) &ifr->ifr_addr, 1)); 323 324 case SIOCSIFNETMASK: 325 i = ifra->ifra_addr.sin_addr.s_addr; 326 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i); 327 break; 328 329 case SIOCAIFADDR: 330 maskIsNew = 0; 331 hostIsNew = 1; 332 error = 0; 333 if (ia->ia_addr.sin_family == AF_INET) { 334 if (ifra->ifra_addr.sin_len == 0) { 335 ifra->ifra_addr = ia->ia_addr; 336 hostIsNew = 0; 337 } else if (ifra->ifra_addr.sin_addr.s_addr == 338 ia->ia_addr.sin_addr.s_addr) 339 hostIsNew = 0; 340 } 341 if (ifra->ifra_mask.sin_len) { 342 in_ifscrub(ifp, ia); 343 ia->ia_sockmask = ifra->ifra_mask; 344 ia->ia_subnetmask = 345 ntohl(ia->ia_sockmask.sin_addr.s_addr); 346 maskIsNew = 1; 347 } 348 if ((ifp->if_flags & IFF_POINTOPOINT) && 349 (ifra->ifra_dstaddr.sin_family == AF_INET)) { 350 in_ifscrub(ifp, ia); 351 ia->ia_dstaddr = ifra->ifra_dstaddr; 352 maskIsNew = 1; /* We lie; but the effect's the same */ 353 } 354 if (ifra->ifra_addr.sin_family == AF_INET && 355 (hostIsNew || maskIsNew)) 356 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); 357 if ((ifp->if_flags & IFF_BROADCAST) && 358 (ifra->ifra_broadaddr.sin_family == AF_INET)) 359 ia->ia_broadaddr = ifra->ifra_broadaddr; 360 return (error); 361 362 case SIOCDIFADDR: 363 mk = malloc(sizeof *mk, M_IPMADDR, M_WAITOK); 364 if (!mk) 365 return ENOBUFS; 366 367 in_ifscrub(ifp, ia); 368 if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia) 369 ifp->if_addrlist = ifa->ifa_next; 370 else { 371 while (ifa->ifa_next && 372 (ifa->ifa_next != (struct ifaddr *)ia)) 373 ifa = ifa->ifa_next; 374 if (ifa->ifa_next) 375 ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next; 376 else 377 printf("Couldn't unlink inifaddr from ifp\n"); 378 } 379 oia = ia; 380 if (oia == (ia = in_ifaddr)) 381 in_ifaddr = ia->ia_next; 382 else { 383 while (ia->ia_next && (ia->ia_next != oia)) 384 ia = ia->ia_next; 385 if (ia->ia_next) 386 ia->ia_next = oia->ia_next; 387 else 388 printf("Didn't unlink inifadr from list\n"); 389 } 390 391 if (!oia->ia_multiaddrs.lh_first) { 392 IFAFREE(&oia->ia_ifa); 393 FREE(mk, M_IPMADDR); 394 break; 395 } 396 397 /* 398 * Multicast address kludge: 399 * If there were any multicast addresses attached to this 400 * interface address, either move them to another address 401 * on this interface, or save them until such time as this 402 * interface is reconfigured for IP. 403 */ 404 IFP_TO_IA(oia->ia_ifp, ia); 405 if (ia) { /* there is another address */ 406 struct in_multi *inm; 407 for(inm = oia->ia_multiaddrs.lh_first; inm; 408 inm = inm->inm_entry.le_next) { 409 IFAFREE(&inm->inm_ia->ia_ifa); 410 ia->ia_ifa.ifa_refcnt++; 411 inm->inm_ia = ia; 412 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, 413 inm_entry); 414 } 415 FREE(mk, M_IPMADDR); 416 } else { /* last address on this if deleted, save */ 417 struct in_multi *inm; 418 419 LIST_INIT(&mk->mk_head); 420 mk->mk_ifp = ifp; 421 422 for(inm = oia->ia_multiaddrs.lh_first; inm; 423 inm = inm->inm_entry.le_next) { 424 LIST_INSERT_HEAD(&mk->mk_head, inm, inm_entry); 425 } 426 427 if (mk->mk_head.lh_first) { 428 LIST_INSERT_HEAD(&in_mk, mk, mk_entry); 429 } else { 430 FREE(mk, M_IPMADDR); 431 } 432 } 433 434 IFAFREE((&oia->ia_ifa)); 435 break; 436 437 default: 438 if (ifp == 0 || ifp->if_ioctl == 0) 439 return (EOPNOTSUPP); 440 return ((*ifp->if_ioctl)(ifp, cmd, data)); 441 } 442 return (0); 443} 444 445/* 446 * Delete any existing route for an interface. 447 */ 448void 449in_ifscrub(ifp, ia) 450 register struct ifnet *ifp; 451 register struct in_ifaddr *ia; 452{ 453 454 if ((ia->ia_flags & IFA_ROUTE) == 0) 455 return; 456 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) 457 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); 458 else 459 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); 460 ia->ia_flags &= ~IFA_ROUTE; 461} 462 463/* 464 * Initialize an interface's internet address 465 * and routing table entry. 466 */ 467int 468in_ifinit(ifp, ia, sin, scrub) 469 register struct ifnet *ifp; 470 register struct in_ifaddr *ia; 471 struct sockaddr_in *sin; 472 int scrub; 473{ 474 register u_long i = ntohl(sin->sin_addr.s_addr); 475 struct sockaddr_in oldaddr; 476 int s = splimp(), flags = RTF_UP, error; 477 struct multi_kludge *mk; 478 479 oldaddr = ia->ia_addr; 480 ia->ia_addr = *sin; 481 /* 482 * Give the interface a chance to initialize 483 * if this is its first address, 484 * and to validate the address if necessary. 485 */ 486 if (ifp->if_ioctl && 487 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) { 488 splx(s); 489 ia->ia_addr = oldaddr; 490 return (error); 491 } 492 splx(s); 493 if (scrub) { 494 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; 495 in_ifscrub(ifp, ia); 496 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; 497 } 498 if (IN_CLASSA(i)) 499 ia->ia_netmask = IN_CLASSA_NET; 500 else if (IN_CLASSB(i)) 501 ia->ia_netmask = IN_CLASSB_NET; 502 else 503 ia->ia_netmask = IN_CLASSC_NET; 504 /* 505 * The subnet mask usually includes at least the standard network part, 506 * but may may be smaller in the case of supernetting. 507 * If it is set, we believe it. 508 */ 509 if (ia->ia_subnetmask == 0) { 510 ia->ia_subnetmask = ia->ia_netmask; 511 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); 512 } else 513 ia->ia_netmask &= ia->ia_subnetmask; 514 ia->ia_net = i & ia->ia_netmask; 515 ia->ia_subnet = i & ia->ia_subnetmask; 516 in_socktrim(&ia->ia_sockmask); 517 /* 518 * Add route for the network. 519 */ 520 ia->ia_ifa.ifa_metric = ifp->if_metric; 521 if (ifp->if_flags & IFF_BROADCAST) { 522 ia->ia_broadaddr.sin_addr.s_addr = 523 htonl(ia->ia_subnet | ~ia->ia_subnetmask); 524 ia->ia_netbroadcast.s_addr = 525 htonl(ia->ia_net | ~ ia->ia_netmask); 526 } else if (ifp->if_flags & IFF_LOOPBACK) { 527 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; 528 flags |= RTF_HOST; 529 } else if (ifp->if_flags & IFF_POINTOPOINT) { 530 if (ia->ia_dstaddr.sin_family != AF_INET) 531 return (0); 532 flags |= RTF_HOST; 533 } 534 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) 535 ia->ia_flags |= IFA_ROUTE; 536 537 LIST_INIT(&ia->ia_multiaddrs); 538 /* 539 * If the interface supports multicast, join the "all hosts" 540 * multicast group on that interface. 541 */ 542 if (ifp->if_flags & IFF_MULTICAST) { 543 struct in_addr addr; 544 545 /* 546 * Continuation of multicast address hack: 547 * If there was a multicast group list previously saved 548 * for this interface, then we re-attach it to the first 549 * address configured on the i/f. 550 */ 551 for(mk = in_mk.lh_first; mk; mk = mk->mk_entry.le_next) { 552 if(mk->mk_ifp == ifp) { 553 struct in_multi *inm; 554 555 for(inm = mk->mk_head.lh_first; inm; 556 inm = inm->inm_entry.le_next) { 557 IFAFREE(&inm->inm_ia->ia_ifa); 558 ia->ia_ifa.ifa_refcnt++; 559 inm->inm_ia = ia; 560 LIST_INSERT_HEAD(&ia->ia_multiaddrs, 561 inm, inm_entry); 562 } 563 LIST_REMOVE(mk, mk_entry); 564 free(mk, M_IPMADDR); 565 break; 566 } 567 } 568 569 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 570 in_addmulti(&addr, ifp); 571 } 572 return (error); 573} 574 575 576/* 577 * Return 1 if the address might be a local broadcast address. 578 */ 579int 580in_broadcast(in, ifp) 581 struct in_addr in; 582 struct ifnet *ifp; 583{ 584 register struct ifaddr *ifa; 585 u_long t; 586 587 if (in.s_addr == INADDR_BROADCAST || 588 in.s_addr == INADDR_ANY) 589 return 1; 590 if ((ifp->if_flags & IFF_BROADCAST) == 0) 591 return 0; 592 t = ntohl(in.s_addr); 593 /* 594 * Look through the list of addresses for a match 595 * with a broadcast address. 596 */ 597#define ia ((struct in_ifaddr *)ifa) 598 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 599 if (ifa->ifa_addr->sa_family == AF_INET && 600 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr || 601 in.s_addr == ia->ia_netbroadcast.s_addr || 602 /* 603 * Check for old-style (host 0) broadcast. 604 */ 605 t == ia->ia_subnet || t == ia->ia_net)) 606 return 1; 607 return (0); 608#undef ia 609} 610/* 611 * Add an address to the list of IP multicast addresses for a given interface. 612 */ 613struct in_multi * 614in_addmulti(ap, ifp) 615 register struct in_addr *ap; 616 register struct ifnet *ifp; 617{ 618 register struct in_multi *inm; 619 struct ifreq ifr; 620 struct in_ifaddr *ia; 621 int s = splnet(); 622 623 /* 624 * See if address already in list. 625 */ 626 IN_LOOKUP_MULTI(*ap, ifp, inm); 627 if (inm != NULL) { 628 /* 629 * Found it; just increment the reference count. 630 */ 631 ++inm->inm_refcount; 632 } 633 else { 634 /* 635 * New address; allocate a new multicast record 636 * and link it into the interface's multicast list. 637 */ 638 inm = (struct in_multi *)malloc(sizeof(*inm), 639 M_IPMADDR, M_NOWAIT); 640 if (inm == NULL) { 641 splx(s); 642 return (NULL); 643 } 644 inm->inm_addr = *ap; 645 inm->inm_ifp = ifp; 646 inm->inm_refcount = 1; 647 IFP_TO_IA(ifp, ia); 648 if (ia == NULL) { 649 free(inm, M_IPMADDR); 650 splx(s); 651 return (NULL); 652 } 653 inm->inm_ia = ia; 654 ia->ia_ifa.ifa_refcnt++; /* gain a reference */ 655 LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_entry); 656 657 /* 658 * Ask the network driver to update its multicast reception 659 * filter appropriately for the new address. 660 */ 661 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_family = AF_INET; 662 ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr = *ap; 663 if ((ifp->if_ioctl == NULL) || 664 (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) { 665 LIST_REMOVE(inm, inm_entry); 666 IFAFREE(&ia->ia_ifa); /* release reference */ 667 free(inm, M_IPMADDR); 668 splx(s); 669 return (NULL); 670 } 671 /* 672 * Let IGMP know that we have joined a new IP multicast group. 673 */ 674 igmp_joingroup(inm); 675 } 676 splx(s); 677 return (inm); 678} 679 680/* 681 * Delete a multicast address record. 682 */ 683void 684in_delmulti(inm) 685 register struct in_multi *inm; 686{ 687 register struct in_multi **p; 688 struct ifreq ifr; 689 int s = splnet(); 690 691 if (--inm->inm_refcount == 0) { 692 /* 693 * No remaining claims to this record; let IGMP know that 694 * we are leaving the multicast group. 695 */ 696 igmp_leavegroup(inm); 697 /* 698 * Unlink from list. 699 */ 700 LIST_REMOVE(inm, inm_entry); 701 IFAFREE(&inm->inm_ia->ia_ifa); /* release reference */ 702 703 /* 704 * Notify the network driver to update its multicast reception 705 * filter. 706 */ 707 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET; 708 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr = 709 inm->inm_addr; 710 (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, 711 (caddr_t)&ifr); 712 free(inm, M_IPMADDR); 713 } 714 splx(s); 715} 716