2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
33 * @(#)in.c 8.4 (Berkeley) 1/9/95
34 * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
35 * $DragonFly: src/sys/netinet/in.c,v 1.4 2003/06/25 03:56:04 dillon Exp $
38 #include "opt_bootp.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/in_pcb.h>
56 #include <netinet/igmp_var.h>
58 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
60 static int in_mask2len __P((struct in_addr *));
61 static void in_len2mask __P((struct in_addr *, int));
62 static int in_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
63 struct ifnet *, struct thread *));
65 static void in_socktrim __P((struct sockaddr_in *));
66 static int in_ifinit __P((struct ifnet *,
67 struct in_ifaddr *, struct sockaddr_in *, int));
69 static int subnetsarelocal = 0;
70 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
71 &subnetsarelocal, 0, "");
73 struct in_multihead in_multihead; /* XXX BSS initialization */
75 extern struct inpcbinfo ripcbinfo;
76 extern struct inpcbinfo udbinfo;
79 * Return 1 if an internet address is for a ``local'' host
80 * (one to which we have a connection). If subnetsarelocal
81 * is true, this includes other subnets of the local net.
82 * Otherwise, it includes only the directly-connected (sub)nets.
88 register u_long i = ntohl(in.s_addr);
89 register struct in_ifaddr *ia;
91 if (subnetsarelocal) {
92 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
93 if ((i & ia->ia_netmask) == ia->ia_net)
96 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
97 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
104 * Determine whether an IP address is in a reserved set of addresses
105 * that may not be forwarded, or whether datagrams to that destination
112 register u_long i = ntohl(in.s_addr);
115 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
118 net = i & IN_CLASSA_NET;
119 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
126 * Trim a mask in a sockaddr
130 struct sockaddr_in *ap;
132 register char *cplim = (char *) &ap->sin_addr;
133 register char *cp = (char *) (&ap->sin_addr + 1);
136 while (--cp >= cplim)
138 (ap)->sin_len = cp - (char *) (ap) + 1;
145 struct in_addr *mask;
151 for (x = 0; x < sizeof(*mask); x++) {
156 if (x < sizeof(*mask)) {
157 for (y = 0; y < 8; y++) {
158 if ((p[x] & (0x80 >> y)) == 0)
166 in_len2mask(mask, len)
167 struct in_addr *mask;
174 bzero(mask, sizeof(*mask));
175 for (i = 0; i < len / 8; i++)
178 p[i] = (0xff00 >> (len % 8)) & 0xff;
181 static int in_interfaces; /* number of external internet interfaces */
184 * Generic internet control operations (ioctl's).
185 * Ifp is 0 if not an interface-specific ioctl.
189 in_control(so, cmd, data, ifp, td)
196 struct ifreq *ifr = (struct ifreq *)data;
197 struct in_ifaddr *ia = 0, *iap;
200 struct in_ifaddr *oia;
201 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
202 struct sockaddr_in oldaddr;
203 int error, hostIsNew, iaIsNew, maskIsNew, s;
210 if ((error = suser(td)) != 0)
216 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
220 * Find address for this interface, if it exists.
222 * If an alias address was specified, find that one instead of
223 * the first one on the interface, if possible
226 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
227 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
228 if (iap->ia_ifp == ifp &&
229 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
234 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
236 if (iap->ia_addr.sin_family == AF_INET) {
248 return (EADDRNOTAVAIL);
249 if (ifra->ifra_addr.sin_family == AF_INET) {
250 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
251 if (ia->ia_ifp == ifp &&
252 ia->ia_addr.sin_addr.s_addr ==
253 ifra->ifra_addr.sin_addr.s_addr)
256 if ((ifp->if_flags & IFF_POINTOPOINT)
257 && (cmd == SIOCAIFADDR)
258 && (ifra->ifra_dstaddr.sin_addr.s_addr
263 if (cmd == SIOCDIFADDR && ia == 0)
264 return (EADDRNOTAVAIL);
269 if ((error = suser(td)) != 0)
273 return (EADDRNOTAVAIL);
274 if (ia == (struct in_ifaddr *)0) {
275 ia = (struct in_ifaddr *)
276 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
277 if (ia == (struct in_ifaddr *)NULL)
279 bzero((caddr_t)ia, sizeof *ia);
281 * Protect from ipintr() traversing address list
282 * while we're modifying it.
286 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
288 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
290 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
291 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
292 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
293 ia->ia_sockmask.sin_len = 8;
294 ia->ia_sockmask.sin_family = AF_INET;
295 if (ifp->if_flags & IFF_BROADCAST) {
296 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
297 ia->ia_broadaddr.sin_family = AF_INET;
300 if (!(ifp->if_flags & IFF_LOOPBACK))
308 if ((error = suser(td)) != 0)
316 if (ia == (struct in_ifaddr *)0)
317 return (EADDRNOTAVAIL);
323 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
327 if ((ifp->if_flags & IFF_BROADCAST) == 0)
329 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
333 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
335 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
339 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
343 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
345 oldaddr = ia->ia_dstaddr;
346 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
347 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
348 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
349 ia->ia_dstaddr = oldaddr;
352 if (ia->ia_flags & IFA_ROUTE) {
353 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
354 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
355 ia->ia_ifa.ifa_dstaddr =
356 (struct sockaddr *)&ia->ia_dstaddr;
357 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
362 if ((ifp->if_flags & IFF_BROADCAST) == 0)
364 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
368 error = in_ifinit(ifp, ia,
369 (struct sockaddr_in *) &ifr->ifr_addr, 1);
370 if (error != 0 && iaIsNew)
375 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
376 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
383 if (ia->ia_addr.sin_family == AF_INET) {
384 if (ifra->ifra_addr.sin_len == 0) {
385 ifra->ifra_addr = ia->ia_addr;
387 } else if (ifra->ifra_addr.sin_addr.s_addr ==
388 ia->ia_addr.sin_addr.s_addr)
391 if (ifra->ifra_mask.sin_len) {
393 ia->ia_sockmask = ifra->ifra_mask;
394 ia->ia_sockmask.sin_family = AF_INET;
396 ntohl(ia->ia_sockmask.sin_addr.s_addr);
399 if ((ifp->if_flags & IFF_POINTOPOINT) &&
400 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
402 ia->ia_dstaddr = ifra->ifra_dstaddr;
403 maskIsNew = 1; /* We lie; but the effect's the same */
405 if (ifra->ifra_addr.sin_family == AF_INET &&
406 (hostIsNew || maskIsNew))
407 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
409 if (error != 0 && iaIsNew)
412 if ((ifp->if_flags & IFF_BROADCAST) &&
413 (ifra->ifra_broadaddr.sin_family == AF_INET))
414 ia->ia_broadaddr = ifra->ifra_broadaddr;
419 * in_ifscrub kills the interface route.
423 * in_ifadown gets rid of all the rest of
424 * the routes. This is not quite the right
425 * thing to do, but at least if we are running
426 * a routing process they will come back.
428 in_ifadown(&ia->ia_ifa, 1);
430 * XXX horrible hack to detect that we are being called
433 if (!ifnet_addrs[ifp->if_index - 1]) {
434 in_pcbpurgeif0(LIST_FIRST(ripcbinfo.listhead), ifp);
435 in_pcbpurgeif0(LIST_FIRST(udbinfo.listhead), ifp);
441 if (ifp == 0 || ifp->if_ioctl == 0)
443 return ((*ifp->if_ioctl)(ifp, cmd, data));
447 * Protect from ipintr() traversing address list while we're modifying
451 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
452 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
453 LIST_REMOVE(ia, ia_hash);
454 IFAFREE(&ia->ia_ifa);
462 * SIOCGLIFADDR: get first address. (?!?)
463 * SIOCGLIFADDR with IFLR_PREFIX:
464 * get first address that matches the specified prefix.
465 * SIOCALIFADDR: add the specified address.
466 * SIOCALIFADDR with IFLR_PREFIX:
467 * EINVAL since we can't deduce hostid part of the address.
468 * SIOCDLIFADDR: delete the specified address.
469 * SIOCDLIFADDR with IFLR_PREFIX:
470 * delete the first address that matches the specified prefix.
472 * EINVAL on invalid parameters
473 * EADDRNOTAVAIL on prefix match failed/specified address not found
474 * other values may be returned from in_ioctl()
477 in_lifaddr_ioctl(so, cmd, data, ifp, td)
484 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
489 panic("invalid argument to in_lifaddr_ioctl");
495 /* address must be specified on GET with IFLR_PREFIX */
496 if ((iflr->flags & IFLR_PREFIX) == 0)
501 /* address must be specified on ADD and DELETE */
502 if (iflr->addr.ss_family != AF_INET)
504 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
506 /* XXX need improvement */
507 if (iflr->dstaddr.ss_family
508 && iflr->dstaddr.ss_family != AF_INET)
510 if (iflr->dstaddr.ss_family
511 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
514 default: /*shouldn't happen*/
517 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
523 struct in_aliasreq ifra;
525 if (iflr->flags & IFLR_PREFIX)
528 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
529 bzero(&ifra, sizeof(ifra));
530 bcopy(iflr->iflr_name, ifra.ifra_name,
531 sizeof(ifra.ifra_name));
533 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
535 if (iflr->dstaddr.ss_family) { /*XXX*/
536 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
537 iflr->dstaddr.ss_len);
540 ifra.ifra_mask.sin_family = AF_INET;
541 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
542 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
544 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
549 struct in_ifaddr *ia;
550 struct in_addr mask, candidate, match;
551 struct sockaddr_in *sin;
554 bzero(&mask, sizeof(mask));
555 if (iflr->flags & IFLR_PREFIX) {
556 /* lookup a prefix rather than address. */
557 in_len2mask(&mask, iflr->prefixlen);
559 sin = (struct sockaddr_in *)&iflr->addr;
560 match.s_addr = sin->sin_addr.s_addr;
561 match.s_addr &= mask.s_addr;
563 /* if you set extra bits, that's wrong */
564 if (match.s_addr != sin->sin_addr.s_addr)
569 if (cmd == SIOCGLIFADDR) {
570 /* on getting an address, take the 1st match */
573 /* on deleting an address, do exact match */
574 in_len2mask(&mask, 32);
575 sin = (struct sockaddr_in *)&iflr->addr;
576 match.s_addr = sin->sin_addr.s_addr;
582 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
583 if (ifa->ifa_addr->sa_family != AF_INET6)
587 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
588 candidate.s_addr &= mask.s_addr;
589 if (candidate.s_addr == match.s_addr)
593 return EADDRNOTAVAIL;
594 ia = (struct in_ifaddr *)ifa;
596 if (cmd == SIOCGLIFADDR) {
597 /* fill in the if_laddrreq structure */
598 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
600 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
601 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
602 ia->ia_dstaddr.sin_len);
604 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
607 in_mask2len(&ia->ia_sockmask.sin_addr);
609 iflr->flags = 0; /*XXX*/
613 struct in_aliasreq ifra;
615 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
616 bzero(&ifra, sizeof(ifra));
617 bcopy(iflr->iflr_name, ifra.ifra_name,
618 sizeof(ifra.ifra_name));
620 bcopy(&ia->ia_addr, &ifra.ifra_addr,
621 ia->ia_addr.sin_len);
622 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
623 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
624 ia->ia_dstaddr.sin_len);
626 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
627 ia->ia_sockmask.sin_len);
629 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
635 return EOPNOTSUPP; /*just for safety*/
639 * Delete any existing route for an interface.
643 register struct ifnet *ifp;
644 register struct in_ifaddr *ia;
647 if ((ia->ia_flags & IFA_ROUTE) == 0)
649 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
650 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
652 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
653 ia->ia_flags &= ~IFA_ROUTE;
657 * Initialize an interface's internet address
658 * and routing table entry.
661 in_ifinit(ifp, ia, sin, scrub)
662 register struct ifnet *ifp;
663 register struct in_ifaddr *ia;
664 struct sockaddr_in *sin;
667 register u_long i = ntohl(sin->sin_addr.s_addr);
668 struct sockaddr_in oldaddr;
669 int s = splimp(), flags = RTF_UP, error = 0;
671 oldaddr = ia->ia_addr;
672 if (oldaddr.sin_family == AF_INET)
673 LIST_REMOVE(ia, ia_hash);
675 if (ia->ia_addr.sin_family == AF_INET)
676 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
679 * Give the interface a chance to initialize
680 * if this is its first address,
681 * and to validate the address if necessary.
684 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
686 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
687 ia->ia_addr = oldaddr;
688 if (ia->ia_addr.sin_family == AF_INET)
689 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
695 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
697 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
700 ia->ia_netmask = IN_CLASSA_NET;
701 else if (IN_CLASSB(i))
702 ia->ia_netmask = IN_CLASSB_NET;
704 ia->ia_netmask = IN_CLASSC_NET;
706 * The subnet mask usually includes at least the standard network part,
707 * but may may be smaller in the case of supernetting.
708 * If it is set, we believe it.
710 if (ia->ia_subnetmask == 0) {
711 ia->ia_subnetmask = ia->ia_netmask;
712 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
714 ia->ia_netmask &= ia->ia_subnetmask;
715 ia->ia_net = i & ia->ia_netmask;
716 ia->ia_subnet = i & ia->ia_subnetmask;
717 in_socktrim(&ia->ia_sockmask);
719 * Add route for the network.
721 ia->ia_ifa.ifa_metric = ifp->if_metric;
722 if (ifp->if_flags & IFF_BROADCAST) {
723 ia->ia_broadaddr.sin_addr.s_addr =
724 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
725 ia->ia_netbroadcast.s_addr =
726 htonl(ia->ia_net | ~ ia->ia_netmask);
727 } else if (ifp->if_flags & IFF_LOOPBACK) {
728 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
730 } else if (ifp->if_flags & IFF_POINTOPOINT) {
731 if (ia->ia_dstaddr.sin_family != AF_INET)
737 * Don't add host routes for interface addresses of
738 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
739 * possible to assign several such address pairs with consistent
740 * results (no host route) and is required by BOOTP.
742 * XXX: This is ugly ! There should be a way for the caller to
743 * say that they don't want a host route.
745 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
746 ia->ia_netmask != IN_CLASSA_NET ||
747 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
748 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
749 ia->ia_addr = oldaddr;
752 ia->ia_flags |= IFA_ROUTE;
756 * If the interface supports multicast, join the "all hosts"
757 * multicast group on that interface.
759 if (ifp->if_flags & IFF_MULTICAST) {
762 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
763 in_addmulti(&addr, ifp);
770 * Return 1 if the address might be a local broadcast address.
773 in_broadcast(in, ifp)
777 register struct ifaddr *ifa;
780 if (in.s_addr == INADDR_BROADCAST ||
781 in.s_addr == INADDR_ANY)
783 if ((ifp->if_flags & IFF_BROADCAST) == 0)
785 t = ntohl(in.s_addr);
787 * Look through the list of addresses for a match
788 * with a broadcast address.
790 #define ia ((struct in_ifaddr *)ifa)
791 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
792 if (ifa->ifa_addr->sa_family == AF_INET &&
793 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
794 in.s_addr == ia->ia_netbroadcast.s_addr ||
796 * Check for old-style (host 0) broadcast.
798 t == ia->ia_subnet || t == ia->ia_net) &&
800 * Check for an all one subnetmask. These
801 * only exist when an interface gets a secondary
804 ia->ia_subnetmask != (u_long)0xffffffff)
810 * Add an address to the list of IP multicast addresses for a given interface.
814 register struct in_addr *ap;
815 register struct ifnet *ifp;
817 register struct in_multi *inm;
819 struct sockaddr_in sin;
820 struct ifmultiaddr *ifma;
824 * Call generic routine to add membership or increment
825 * refcount. It wants addresses in the form of a sockaddr,
826 * so we build one here (being careful to zero the unused bytes).
828 bzero(&sin, sizeof sin);
829 sin.sin_family = AF_INET;
830 sin.sin_len = sizeof sin;
832 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
839 * If ifma->ifma_protospec is null, then if_addmulti() created
840 * a new record. Otherwise, we are done.
842 if (ifma->ifma_protospec != 0) {
844 return ifma->ifma_protospec;
847 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
848 at interrupt time? If so, need to fix if_addmulti. XXX */
849 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT);
855 bzero(inm, sizeof *inm);
858 inm->inm_ifma = ifma;
859 ifma->ifma_protospec = inm;
860 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
863 * Let IGMP know that we have joined a new IP multicast group.
871 * Delete a multicast address record.
875 register struct in_multi *inm;
877 struct ifmultiaddr *ifma = inm->inm_ifma;
878 struct in_multi my_inm;
881 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
882 if (ifma->ifma_refcount == 1) {
884 * No remaining claims to this record; let IGMP know that
885 * we are leaving the multicast group.
886 * But do it after the if_delmulti() which might reset
887 * the interface and nuke the packet.
890 ifma->ifma_protospec = 0;
891 LIST_REMOVE(inm, inm_link);
892 free(inm, M_IPMADDR);
894 /* XXX - should be separate API for when we have an ifma? */
895 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
896 if (my_inm.inm_ifp != NULL)
897 igmp_leavegroup(&my_inm);