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 $
37 #include "opt_bootp.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sockio.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
48 #include <net/if_types.h>
49 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_var.h>
53 #include <netinet/in_pcb.h>
55 #include <netinet/igmp_var.h>
57 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
59 static int in_mask2len __P((struct in_addr *));
60 static void in_len2mask __P((struct in_addr *, int));
61 static int in_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
62 struct ifnet *, struct proc *));
64 static void in_socktrim __P((struct sockaddr_in *));
65 static int in_ifinit __P((struct ifnet *,
66 struct in_ifaddr *, struct sockaddr_in *, int));
68 static int subnetsarelocal = 0;
69 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
70 &subnetsarelocal, 0, "");
72 struct in_multihead in_multihead; /* XXX BSS initialization */
74 extern struct inpcbinfo ripcbinfo;
75 extern struct inpcbinfo udbinfo;
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.
87 register u_long i = ntohl(in.s_addr);
88 register struct in_ifaddr *ia;
90 if (subnetsarelocal) {
91 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
92 if ((i & ia->ia_netmask) == ia->ia_net)
95 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
96 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
103 * Determine whether an IP address is in a reserved set of addresses
104 * that may not be forwarded, or whether datagrams to that destination
111 register u_long i = ntohl(in.s_addr);
114 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
117 net = i & IN_CLASSA_NET;
118 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
125 * Trim a mask in a sockaddr
129 struct sockaddr_in *ap;
131 register char *cplim = (char *) &ap->sin_addr;
132 register char *cp = (char *) (&ap->sin_addr + 1);
135 while (--cp >= cplim)
137 (ap)->sin_len = cp - (char *) (ap) + 1;
144 struct in_addr *mask;
150 for (x = 0; x < sizeof(*mask); x++) {
155 if (x < sizeof(*mask)) {
156 for (y = 0; y < 8; y++) {
157 if ((p[x] & (0x80 >> y)) == 0)
165 in_len2mask(mask, len)
166 struct in_addr *mask;
173 bzero(mask, sizeof(*mask));
174 for (i = 0; i < len / 8; i++)
177 p[i] = (0xff00 >> (len % 8)) & 0xff;
180 static int in_interfaces; /* number of external internet interfaces */
183 * Generic internet control operations (ioctl's).
184 * Ifp is 0 if not an interface-specific ioctl.
188 in_control(so, cmd, data, ifp, p)
192 register struct ifnet *ifp;
195 register struct ifreq *ifr = (struct ifreq *)data;
196 register struct in_ifaddr *ia = 0, *iap;
197 register struct ifaddr *ifa;
199 struct in_ifaddr *oia;
200 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
201 struct sockaddr_in oldaddr;
202 int error, hostIsNew, iaIsNew, maskIsNew, s;
209 if (p && (error = suser(p)) != 0)
215 return in_lifaddr_ioctl(so, cmd, data, ifp, p);
219 * Find address for this interface, if it exists.
221 * If an alias address was specified, find that one instead of
222 * the first one on the interface, if possible
225 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
226 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
227 if (iap->ia_ifp == ifp &&
228 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
233 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
235 if (iap->ia_addr.sin_family == AF_INET) {
247 return (EADDRNOTAVAIL);
248 if (ifra->ifra_addr.sin_family == AF_INET) {
249 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
250 if (ia->ia_ifp == ifp &&
251 ia->ia_addr.sin_addr.s_addr ==
252 ifra->ifra_addr.sin_addr.s_addr)
255 if ((ifp->if_flags & IFF_POINTOPOINT)
256 && (cmd == SIOCAIFADDR)
257 && (ifra->ifra_dstaddr.sin_addr.s_addr
262 if (cmd == SIOCDIFADDR && ia == 0)
263 return (EADDRNOTAVAIL);
268 if (p && (error = suser(p)) != 0)
272 return (EADDRNOTAVAIL);
273 if (ia == (struct in_ifaddr *)0) {
274 ia = (struct in_ifaddr *)
275 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
276 if (ia == (struct in_ifaddr *)NULL)
278 bzero((caddr_t)ia, sizeof *ia);
280 * Protect from ipintr() traversing address list
281 * while we're modifying it.
285 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
287 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
289 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
290 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
291 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
292 ia->ia_sockmask.sin_len = 8;
293 ia->ia_sockmask.sin_family = AF_INET;
294 if (ifp->if_flags & IFF_BROADCAST) {
295 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
296 ia->ia_broadaddr.sin_family = AF_INET;
299 if (!(ifp->if_flags & IFF_LOOPBACK))
307 if (p && (error = suser(p)) != 0)
315 if (ia == (struct in_ifaddr *)0)
316 return (EADDRNOTAVAIL);
322 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
326 if ((ifp->if_flags & IFF_BROADCAST) == 0)
328 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
332 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
334 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
338 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
342 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
344 oldaddr = ia->ia_dstaddr;
345 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
346 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
347 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
348 ia->ia_dstaddr = oldaddr;
351 if (ia->ia_flags & IFA_ROUTE) {
352 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
353 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
354 ia->ia_ifa.ifa_dstaddr =
355 (struct sockaddr *)&ia->ia_dstaddr;
356 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
361 if ((ifp->if_flags & IFF_BROADCAST) == 0)
363 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
367 error = in_ifinit(ifp, ia,
368 (struct sockaddr_in *) &ifr->ifr_addr, 1);
369 if (error != 0 && iaIsNew)
374 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
375 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
382 if (ia->ia_addr.sin_family == AF_INET) {
383 if (ifra->ifra_addr.sin_len == 0) {
384 ifra->ifra_addr = ia->ia_addr;
386 } else if (ifra->ifra_addr.sin_addr.s_addr ==
387 ia->ia_addr.sin_addr.s_addr)
390 if (ifra->ifra_mask.sin_len) {
392 ia->ia_sockmask = ifra->ifra_mask;
393 ia->ia_sockmask.sin_family = AF_INET;
395 ntohl(ia->ia_sockmask.sin_addr.s_addr);
398 if ((ifp->if_flags & IFF_POINTOPOINT) &&
399 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
401 ia->ia_dstaddr = ifra->ifra_dstaddr;
402 maskIsNew = 1; /* We lie; but the effect's the same */
404 if (ifra->ifra_addr.sin_family == AF_INET &&
405 (hostIsNew || maskIsNew))
406 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
408 if (error != 0 && iaIsNew)
411 if ((ifp->if_flags & IFF_BROADCAST) &&
412 (ifra->ifra_broadaddr.sin_family == AF_INET))
413 ia->ia_broadaddr = ifra->ifra_broadaddr;
418 * in_ifscrub kills the interface route.
422 * in_ifadown gets rid of all the rest of
423 * the routes. This is not quite the right
424 * thing to do, but at least if we are running
425 * a routing process they will come back.
427 in_ifadown(&ia->ia_ifa, 1);
429 * XXX horrible hack to detect that we are being called
432 if (!ifnet_addrs[ifp->if_index - 1]) {
433 in_pcbpurgeif0(LIST_FIRST(ripcbinfo.listhead), ifp);
434 in_pcbpurgeif0(LIST_FIRST(udbinfo.listhead), ifp);
440 if (ifp == 0 || ifp->if_ioctl == 0)
442 return ((*ifp->if_ioctl)(ifp, cmd, data));
446 * Protect from ipintr() traversing address list while we're modifying
450 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
451 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
452 LIST_REMOVE(ia, ia_hash);
453 IFAFREE(&ia->ia_ifa);
461 * SIOCGLIFADDR: get first address. (?!?)
462 * SIOCGLIFADDR with IFLR_PREFIX:
463 * get first address that matches the specified prefix.
464 * SIOCALIFADDR: add the specified address.
465 * SIOCALIFADDR with IFLR_PREFIX:
466 * EINVAL since we can't deduce hostid part of the address.
467 * SIOCDLIFADDR: delete the specified address.
468 * SIOCDLIFADDR with IFLR_PREFIX:
469 * delete the first address that matches the specified prefix.
471 * EINVAL on invalid parameters
472 * EADDRNOTAVAIL on prefix match failed/specified address not found
473 * other values may be returned from in_ioctl()
476 in_lifaddr_ioctl(so, cmd, data, ifp, p)
483 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
488 panic("invalid argument to in_lifaddr_ioctl");
494 /* address must be specified on GET with IFLR_PREFIX */
495 if ((iflr->flags & IFLR_PREFIX) == 0)
500 /* address must be specified on ADD and DELETE */
501 if (iflr->addr.ss_family != AF_INET)
503 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
505 /* XXX need improvement */
506 if (iflr->dstaddr.ss_family
507 && iflr->dstaddr.ss_family != AF_INET)
509 if (iflr->dstaddr.ss_family
510 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
513 default: /*shouldn't happen*/
516 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
522 struct in_aliasreq ifra;
524 if (iflr->flags & IFLR_PREFIX)
527 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
528 bzero(&ifra, sizeof(ifra));
529 bcopy(iflr->iflr_name, ifra.ifra_name,
530 sizeof(ifra.ifra_name));
532 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
534 if (iflr->dstaddr.ss_family) { /*XXX*/
535 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
536 iflr->dstaddr.ss_len);
539 ifra.ifra_mask.sin_family = AF_INET;
540 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
541 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
543 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p);
548 struct in_ifaddr *ia;
549 struct in_addr mask, candidate, match;
550 struct sockaddr_in *sin;
553 bzero(&mask, sizeof(mask));
554 if (iflr->flags & IFLR_PREFIX) {
555 /* lookup a prefix rather than address. */
556 in_len2mask(&mask, iflr->prefixlen);
558 sin = (struct sockaddr_in *)&iflr->addr;
559 match.s_addr = sin->sin_addr.s_addr;
560 match.s_addr &= mask.s_addr;
562 /* if you set extra bits, that's wrong */
563 if (match.s_addr != sin->sin_addr.s_addr)
568 if (cmd == SIOCGLIFADDR) {
569 /* on getting an address, take the 1st match */
572 /* on deleting an address, do exact match */
573 in_len2mask(&mask, 32);
574 sin = (struct sockaddr_in *)&iflr->addr;
575 match.s_addr = sin->sin_addr.s_addr;
581 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
582 if (ifa->ifa_addr->sa_family != AF_INET6)
586 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
587 candidate.s_addr &= mask.s_addr;
588 if (candidate.s_addr == match.s_addr)
592 return EADDRNOTAVAIL;
593 ia = (struct in_ifaddr *)ifa;
595 if (cmd == SIOCGLIFADDR) {
596 /* fill in the if_laddrreq structure */
597 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
599 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
600 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
601 ia->ia_dstaddr.sin_len);
603 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
606 in_mask2len(&ia->ia_sockmask.sin_addr);
608 iflr->flags = 0; /*XXX*/
612 struct in_aliasreq ifra;
614 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
615 bzero(&ifra, sizeof(ifra));
616 bcopy(iflr->iflr_name, ifra.ifra_name,
617 sizeof(ifra.ifra_name));
619 bcopy(&ia->ia_addr, &ifra.ifra_addr,
620 ia->ia_addr.sin_len);
621 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
622 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
623 ia->ia_dstaddr.sin_len);
625 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
626 ia->ia_sockmask.sin_len);
628 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
634 return EOPNOTSUPP; /*just for safety*/
638 * Delete any existing route for an interface.
642 register struct ifnet *ifp;
643 register struct in_ifaddr *ia;
646 if ((ia->ia_flags & IFA_ROUTE) == 0)
648 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
649 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
651 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
652 ia->ia_flags &= ~IFA_ROUTE;
656 * Initialize an interface's internet address
657 * and routing table entry.
660 in_ifinit(ifp, ia, sin, scrub)
661 register struct ifnet *ifp;
662 register struct in_ifaddr *ia;
663 struct sockaddr_in *sin;
666 register u_long i = ntohl(sin->sin_addr.s_addr);
667 struct sockaddr_in oldaddr;
668 int s = splimp(), flags = RTF_UP, error = 0;
670 oldaddr = ia->ia_addr;
671 if (oldaddr.sin_family == AF_INET)
672 LIST_REMOVE(ia, ia_hash);
674 if (ia->ia_addr.sin_family == AF_INET)
675 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
678 * Give the interface a chance to initialize
679 * if this is its first address,
680 * and to validate the address if necessary.
683 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
685 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
686 ia->ia_addr = oldaddr;
687 if (ia->ia_addr.sin_family == AF_INET)
688 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
694 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
696 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
699 ia->ia_netmask = IN_CLASSA_NET;
700 else if (IN_CLASSB(i))
701 ia->ia_netmask = IN_CLASSB_NET;
703 ia->ia_netmask = IN_CLASSC_NET;
705 * The subnet mask usually includes at least the standard network part,
706 * but may may be smaller in the case of supernetting.
707 * If it is set, we believe it.
709 if (ia->ia_subnetmask == 0) {
710 ia->ia_subnetmask = ia->ia_netmask;
711 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
713 ia->ia_netmask &= ia->ia_subnetmask;
714 ia->ia_net = i & ia->ia_netmask;
715 ia->ia_subnet = i & ia->ia_subnetmask;
716 in_socktrim(&ia->ia_sockmask);
718 * Add route for the network.
720 ia->ia_ifa.ifa_metric = ifp->if_metric;
721 if (ifp->if_flags & IFF_BROADCAST) {
722 ia->ia_broadaddr.sin_addr.s_addr =
723 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
724 ia->ia_netbroadcast.s_addr =
725 htonl(ia->ia_net | ~ ia->ia_netmask);
726 } else if (ifp->if_flags & IFF_LOOPBACK) {
727 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
729 } else if (ifp->if_flags & IFF_POINTOPOINT) {
730 if (ia->ia_dstaddr.sin_family != AF_INET)
736 * Don't add host routes for interface addresses of
737 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
738 * possible to assign several such address pairs with consistent
739 * results (no host route) and is required by BOOTP.
741 * XXX: This is ugly ! There should be a way for the caller to
742 * say that they don't want a host route.
744 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
745 ia->ia_netmask != IN_CLASSA_NET ||
746 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
747 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
748 ia->ia_addr = oldaddr;
751 ia->ia_flags |= IFA_ROUTE;
755 * If the interface supports multicast, join the "all hosts"
756 * multicast group on that interface.
758 if (ifp->if_flags & IFF_MULTICAST) {
761 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
762 in_addmulti(&addr, ifp);
769 * Return 1 if the address might be a local broadcast address.
772 in_broadcast(in, ifp)
776 register struct ifaddr *ifa;
779 if (in.s_addr == INADDR_BROADCAST ||
780 in.s_addr == INADDR_ANY)
782 if ((ifp->if_flags & IFF_BROADCAST) == 0)
784 t = ntohl(in.s_addr);
786 * Look through the list of addresses for a match
787 * with a broadcast address.
789 #define ia ((struct in_ifaddr *)ifa)
790 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
791 if (ifa->ifa_addr->sa_family == AF_INET &&
792 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
793 in.s_addr == ia->ia_netbroadcast.s_addr ||
795 * Check for old-style (host 0) broadcast.
797 t == ia->ia_subnet || t == ia->ia_net) &&
799 * Check for an all one subnetmask. These
800 * only exist when an interface gets a secondary
803 ia->ia_subnetmask != (u_long)0xffffffff)
809 * Add an address to the list of IP multicast addresses for a given interface.
813 register struct in_addr *ap;
814 register struct ifnet *ifp;
816 register struct in_multi *inm;
818 struct sockaddr_in sin;
819 struct ifmultiaddr *ifma;
823 * Call generic routine to add membership or increment
824 * refcount. It wants addresses in the form of a sockaddr,
825 * so we build one here (being careful to zero the unused bytes).
827 bzero(&sin, sizeof sin);
828 sin.sin_family = AF_INET;
829 sin.sin_len = sizeof sin;
831 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
838 * If ifma->ifma_protospec is null, then if_addmulti() created
839 * a new record. Otherwise, we are done.
841 if (ifma->ifma_protospec != 0) {
843 return ifma->ifma_protospec;
846 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
847 at interrupt time? If so, need to fix if_addmulti. XXX */
848 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT);
854 bzero(inm, sizeof *inm);
857 inm->inm_ifma = ifma;
858 ifma->ifma_protospec = inm;
859 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
862 * Let IGMP know that we have joined a new IP multicast group.
870 * Delete a multicast address record.
874 register struct in_multi *inm;
876 struct ifmultiaddr *ifma = inm->inm_ifma;
877 struct in_multi my_inm;
880 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
881 if (ifma->ifma_refcount == 1) {
883 * No remaining claims to this record; let IGMP know that
884 * we are leaving the multicast group.
885 * But do it after the if_delmulti() which might reset
886 * the interface and nuke the packet.
889 ifma->ifma_protospec = 0;
890 LIST_REMOVE(inm, inm_link);
891 free(inm, M_IPMADDR);
893 /* XXX - should be separate API for when we have an ifma? */
894 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
895 if (my_inm.inm_ifp != NULL)
896 igmp_leavegroup(&my_inm);