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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)in.c 8.4 (Berkeley) 1/9/95
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/sockio.h>
36 #include <sys/malloc.h>
37 #include <sys/socket.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
42 #include <net/if_types.h>
43 #include <net/route.h>
45 #include <netinet/in.h>
46 #include <netinet/in_var.h>
47 #include <netinet/in_pcb.h>
49 #include <netinet/igmp_var.h>
51 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
53 static int in_mask2len(struct in_addr *);
54 static void in_len2mask(struct in_addr *, int);
55 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
56 struct ifnet *, struct thread *);
58 static void in_socktrim(struct sockaddr_in *);
59 static int in_ifinit(struct ifnet *,
60 struct in_ifaddr *, struct sockaddr_in *, int);
62 static int subnetsarelocal = 0;
63 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
64 &subnetsarelocal, 0, "Treat all subnets as directly connected");
66 struct in_multihead in_multihead; /* XXX BSS initialization */
68 extern struct inpcbinfo ripcbinfo;
69 extern struct inpcbinfo udbinfo;
72 * Return 1 if an internet address is for a ``local'' host
73 * (one to which we have a connection). If subnetsarelocal
74 * is true, this includes other subnets of the local net.
75 * Otherwise, it includes only the directly-connected (sub)nets.
81 register u_long i = ntohl(in.s_addr);
82 register struct in_ifaddr *ia;
84 if (subnetsarelocal) {
85 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
86 if ((i & ia->ia_netmask) == ia->ia_net)
89 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
90 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
97 * Return 1 if an internet address is for the local host and configured
98 * on one of its interfaces.
104 struct in_ifaddr *ia;
106 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
107 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
114 * Determine whether an IP address is in a reserved set of addresses
115 * that may not be forwarded, or whether datagrams to that destination
122 register u_long i = ntohl(in.s_addr);
125 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
128 net = i & IN_CLASSA_NET;
129 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
136 * Trim a mask in a sockaddr
140 struct sockaddr_in *ap;
142 register char *cplim = (char *) &ap->sin_addr;
143 register char *cp = (char *) (&ap->sin_addr + 1);
146 while (--cp >= cplim)
148 (ap)->sin_len = cp - (char *) (ap) + 1;
155 struct in_addr *mask;
161 for (x = 0; x < sizeof(*mask); x++) {
166 if (x < sizeof(*mask)) {
167 for (y = 0; y < 8; y++) {
168 if ((p[x] & (0x80 >> y)) == 0)
176 in_len2mask(mask, len)
177 struct in_addr *mask;
184 bzero(mask, sizeof(*mask));
185 for (i = 0; i < len / 8; i++)
188 p[i] = (0xff00 >> (len % 8)) & 0xff;
192 * Generic internet control operations (ioctl's).
193 * Ifp is 0 if not an interface-specific ioctl.
197 in_control(so, cmd, data, ifp, td)
201 register struct ifnet *ifp;
204 register struct ifreq *ifr = (struct ifreq *)data;
205 register struct in_ifaddr *ia = 0, *iap;
206 register struct ifaddr *ifa;
208 struct in_ifaddr *oia;
209 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
210 struct sockaddr_in oldaddr;
211 int error, hostIsNew, iaIsNew, maskIsNew, s;
218 if (td && (error = suser(td)) != 0)
224 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
228 * Find address for this interface, if it exists.
230 * If an alias address was specified, find that one instead of
231 * the first one on the interface, if possible.
234 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
235 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
236 if (iap->ia_ifp == ifp &&
237 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
242 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
244 if (iap->ia_addr.sin_family == AF_INET) {
256 return (EADDRNOTAVAIL);
257 if (ifra->ifra_addr.sin_family == AF_INET) {
258 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
259 if (ia->ia_ifp == ifp &&
260 ia->ia_addr.sin_addr.s_addr ==
261 ifra->ifra_addr.sin_addr.s_addr)
264 if ((ifp->if_flags & IFF_POINTOPOINT)
265 && (cmd == SIOCAIFADDR)
266 && (ifra->ifra_dstaddr.sin_addr.s_addr
271 if (cmd == SIOCDIFADDR && ia == 0)
272 return (EADDRNOTAVAIL);
277 if (td && (error = suser(td)) != 0)
281 return (EADDRNOTAVAIL);
282 if (ia == (struct in_ifaddr *)0) {
283 ia = (struct in_ifaddr *)
284 malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
285 if (ia == (struct in_ifaddr *)NULL)
288 * Protect from ipintr() traversing address list
289 * while we're modifying it.
292 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
296 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
297 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
298 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
300 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
302 ia->ia_sockmask.sin_len = 8;
303 ia->ia_sockmask.sin_family = AF_INET;
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;
315 if (td && (error = suser(td)) != 0)
323 if (ia == (struct in_ifaddr *)0)
324 return (EADDRNOTAVAIL);
330 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
334 if ((ifp->if_flags & IFF_BROADCAST) == 0)
336 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
340 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
342 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
346 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
350 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
352 oldaddr = ia->ia_dstaddr;
353 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
354 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
355 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
356 ia->ia_dstaddr = oldaddr;
359 if (ia->ia_flags & IFA_ROUTE) {
360 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
361 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
362 ia->ia_ifa.ifa_dstaddr =
363 (struct sockaddr *)&ia->ia_dstaddr;
364 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
369 if ((ifp->if_flags & IFF_BROADCAST) == 0)
371 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
375 error = in_ifinit(ifp, ia,
376 (struct sockaddr_in *) &ifr->ifr_addr, 1);
377 if (error != 0 && iaIsNew)
380 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
384 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
385 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
392 if (ia->ia_addr.sin_family == AF_INET) {
393 if (ifra->ifra_addr.sin_len == 0) {
394 ifra->ifra_addr = ia->ia_addr;
396 } else if (ifra->ifra_addr.sin_addr.s_addr ==
397 ia->ia_addr.sin_addr.s_addr)
400 if (ifra->ifra_mask.sin_len) {
402 ia->ia_sockmask = ifra->ifra_mask;
403 ia->ia_sockmask.sin_family = AF_INET;
405 ntohl(ia->ia_sockmask.sin_addr.s_addr);
408 if ((ifp->if_flags & IFF_POINTOPOINT) &&
409 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
411 ia->ia_dstaddr = ifra->ifra_dstaddr;
412 maskIsNew = 1; /* We lie; but the effect's the same */
414 if (ifra->ifra_addr.sin_family == AF_INET &&
415 (hostIsNew || maskIsNew))
416 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
417 if (error != 0 && iaIsNew)
420 if ((ifp->if_flags & IFF_BROADCAST) &&
421 (ifra->ifra_broadaddr.sin_family == AF_INET))
422 ia->ia_broadaddr = ifra->ifra_broadaddr;
424 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
429 * in_ifscrub kills the interface route.
433 * in_ifadown gets rid of all the rest of
434 * the routes. This is not quite the right
435 * thing to do, but at least if we are running
436 * a routing process they will come back.
438 in_ifadown(&ia->ia_ifa, 1);
440 * XXX horrible hack to detect that we are being called
443 if (ifaddr_byindex(ifp->if_index) == NULL) {
444 in_pcbpurgeif0(&ripcbinfo, ifp);
445 in_pcbpurgeif0(&udbinfo, ifp);
447 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
452 if (ifp == 0 || ifp->if_ioctl == 0)
454 return ((*ifp->if_ioctl)(ifp, cmd, data));
458 * Protect from ipintr() traversing address list while we're modifying
462 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
463 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
464 LIST_REMOVE(ia, ia_hash);
465 IFAFREE(&ia->ia_ifa);
473 * SIOCGLIFADDR: get first address. (?!?)
474 * SIOCGLIFADDR with IFLR_PREFIX:
475 * get first address that matches the specified prefix.
476 * SIOCALIFADDR: add the specified address.
477 * SIOCALIFADDR with IFLR_PREFIX:
478 * EINVAL since we can't deduce hostid part of the address.
479 * SIOCDLIFADDR: delete the specified address.
480 * SIOCDLIFADDR with IFLR_PREFIX:
481 * delete the first address that matches the specified prefix.
483 * EINVAL on invalid parameters
484 * EADDRNOTAVAIL on prefix match failed/specified address not found
485 * other values may be returned from in_ioctl()
488 in_lifaddr_ioctl(so, cmd, data, ifp, td)
495 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
500 panic("invalid argument to in_lifaddr_ioctl");
506 /* address must be specified on GET with IFLR_PREFIX */
507 if ((iflr->flags & IFLR_PREFIX) == 0)
512 /* address must be specified on ADD and DELETE */
513 if (iflr->addr.ss_family != AF_INET)
515 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
517 /* XXX need improvement */
518 if (iflr->dstaddr.ss_family
519 && iflr->dstaddr.ss_family != AF_INET)
521 if (iflr->dstaddr.ss_family
522 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
525 default: /*shouldn't happen*/
528 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
534 struct in_aliasreq ifra;
536 if (iflr->flags & IFLR_PREFIX)
539 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
540 bzero(&ifra, sizeof(ifra));
541 bcopy(iflr->iflr_name, ifra.ifra_name,
542 sizeof(ifra.ifra_name));
544 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
546 if (iflr->dstaddr.ss_family) { /*XXX*/
547 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
548 iflr->dstaddr.ss_len);
551 ifra.ifra_mask.sin_family = AF_INET;
552 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
553 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
555 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
560 struct in_ifaddr *ia;
561 struct in_addr mask, candidate, match;
562 struct sockaddr_in *sin;
565 bzero(&mask, sizeof(mask));
566 if (iflr->flags & IFLR_PREFIX) {
567 /* lookup a prefix rather than address. */
568 in_len2mask(&mask, iflr->prefixlen);
570 sin = (struct sockaddr_in *)&iflr->addr;
571 match.s_addr = sin->sin_addr.s_addr;
572 match.s_addr &= mask.s_addr;
574 /* if you set extra bits, that's wrong */
575 if (match.s_addr != sin->sin_addr.s_addr)
580 if (cmd == SIOCGLIFADDR) {
581 /* on getting an address, take the 1st match */
584 /* on deleting an address, do exact match */
585 in_len2mask(&mask, 32);
586 sin = (struct sockaddr_in *)&iflr->addr;
587 match.s_addr = sin->sin_addr.s_addr;
593 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
594 if (ifa->ifa_addr->sa_family != AF_INET6)
598 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
599 candidate.s_addr &= mask.s_addr;
600 if (candidate.s_addr == match.s_addr)
604 return EADDRNOTAVAIL;
605 ia = (struct in_ifaddr *)ifa;
607 if (cmd == SIOCGLIFADDR) {
608 /* fill in the if_laddrreq structure */
609 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
611 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
612 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
613 ia->ia_dstaddr.sin_len);
615 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
618 in_mask2len(&ia->ia_sockmask.sin_addr);
620 iflr->flags = 0; /*XXX*/
624 struct in_aliasreq ifra;
626 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
627 bzero(&ifra, sizeof(ifra));
628 bcopy(iflr->iflr_name, ifra.ifra_name,
629 sizeof(ifra.ifra_name));
631 bcopy(&ia->ia_addr, &ifra.ifra_addr,
632 ia->ia_addr.sin_len);
633 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
634 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
635 ia->ia_dstaddr.sin_len);
637 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
638 ia->ia_sockmask.sin_len);
640 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
646 return EOPNOTSUPP; /*just for safety*/
650 * Delete any existing route for an interface.
654 register struct ifnet *ifp;
655 register struct in_ifaddr *ia;
658 if ((ia->ia_flags & IFA_ROUTE) == 0)
660 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
661 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
663 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
664 ia->ia_flags &= ~IFA_ROUTE;
668 * Initialize an interface's internet address
669 * and routing table entry.
672 in_ifinit(ifp, ia, sin, scrub)
673 register struct ifnet *ifp;
674 register struct in_ifaddr *ia;
675 struct sockaddr_in *sin;
678 register u_long i = ntohl(sin->sin_addr.s_addr);
679 struct sockaddr_in oldaddr;
680 int s = splimp(), flags = RTF_UP, error = 0;
682 oldaddr = ia->ia_addr;
683 if (oldaddr.sin_family == AF_INET)
684 LIST_REMOVE(ia, ia_hash);
686 if (ia->ia_addr.sin_family == AF_INET)
687 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
690 * Give the interface a chance to initialize
691 * if this is its first address,
692 * and to validate the address if necessary.
695 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
697 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
698 ia->ia_addr = oldaddr;
699 if (ia->ia_addr.sin_family == AF_INET)
700 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
706 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
708 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
711 ia->ia_netmask = IN_CLASSA_NET;
712 else if (IN_CLASSB(i))
713 ia->ia_netmask = IN_CLASSB_NET;
715 ia->ia_netmask = IN_CLASSC_NET;
717 * The subnet mask usually includes at least the standard network part,
718 * but may may be smaller in the case of supernetting.
719 * If it is set, we believe it.
721 if (ia->ia_subnetmask == 0) {
722 ia->ia_subnetmask = ia->ia_netmask;
723 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
725 ia->ia_netmask &= ia->ia_subnetmask;
726 ia->ia_net = i & ia->ia_netmask;
727 ia->ia_subnet = i & ia->ia_subnetmask;
728 in_socktrim(&ia->ia_sockmask);
730 * Add route for the network.
732 ia->ia_ifa.ifa_metric = ifp->if_metric;
733 if (ifp->if_flags & IFF_BROADCAST) {
734 ia->ia_broadaddr.sin_addr.s_addr =
735 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
736 ia->ia_netbroadcast.s_addr =
737 htonl(ia->ia_net | ~ ia->ia_netmask);
738 } else if (ifp->if_flags & IFF_LOOPBACK) {
739 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
741 } else if (ifp->if_flags & IFF_POINTOPOINT) {
742 if (ia->ia_dstaddr.sin_family != AF_INET)
748 * Don't add host routes for interface addresses of
749 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
750 * possible to assign several such address pairs with consistent
751 * results (no host route) and is required by BOOTP.
753 * XXX: This is ugly ! There should be a way for the caller to
754 * say that they don't want a host route.
756 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
757 ia->ia_netmask != IN_CLASSA_NET ||
758 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
759 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
760 ia->ia_addr = oldaddr;
763 ia->ia_flags |= IFA_ROUTE;
767 * If the interface supports multicast, join the "all hosts"
768 * multicast group on that interface.
770 if (ifp->if_flags & IFF_MULTICAST) {
773 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
774 in_addmulti(&addr, ifp);
781 * Return 1 if the address might be a local broadcast address.
784 in_broadcast(in, ifp)
788 register struct ifaddr *ifa;
791 if (in.s_addr == INADDR_BROADCAST ||
792 in.s_addr == INADDR_ANY)
794 if ((ifp->if_flags & IFF_BROADCAST) == 0)
796 t = ntohl(in.s_addr);
798 * Look through the list of addresses for a match
799 * with a broadcast address.
801 #define ia ((struct in_ifaddr *)ifa)
802 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
803 if (ifa->ifa_addr->sa_family == AF_INET &&
804 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
805 in.s_addr == ia->ia_netbroadcast.s_addr ||
807 * Check for old-style (host 0) broadcast.
809 t == ia->ia_subnet || t == ia->ia_net) &&
811 * Check for an all one subnetmask. These
812 * only exist when an interface gets a secondary
815 ia->ia_subnetmask != (u_long)0xffffffff)
821 * Add an address to the list of IP multicast addresses for a given interface.
825 register struct in_addr *ap;
826 register struct ifnet *ifp;
828 register struct in_multi *inm;
830 struct sockaddr_in sin;
831 struct ifmultiaddr *ifma;
835 * Call generic routine to add membership or increment
836 * refcount. It wants addresses in the form of a sockaddr,
837 * so we build one here (being careful to zero the unused bytes).
839 bzero(&sin, sizeof sin);
840 sin.sin_family = AF_INET;
841 sin.sin_len = sizeof sin;
843 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
850 * If ifma->ifma_protospec is null, then if_addmulti() created
851 * a new record. Otherwise, we are done.
853 if (ifma->ifma_protospec != 0) {
855 return ifma->ifma_protospec;
858 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
859 at interrupt time? If so, need to fix if_addmulti. XXX */
860 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
869 inm->inm_ifma = ifma;
870 ifma->ifma_protospec = inm;
871 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
874 * Let IGMP know that we have joined a new IP multicast group.
882 * Delete a multicast address record.
886 register struct in_multi *inm;
888 struct ifmultiaddr *ifma = inm->inm_ifma;
889 struct in_multi my_inm;
892 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
893 if (ifma->ifma_refcount == 1) {
895 * No remaining claims to this record; let IGMP know that
896 * we are leaving the multicast group.
897 * But do it after the if_delmulti() which might reset
898 * the interface and nuke the packet.
901 ifma->ifma_protospec = 0;
902 LIST_REMOVE(inm, inm_link);
903 free(inm, M_IPMADDR);
905 /* XXX - should be separate API for when we have an ifma? */
906 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
907 if (my_inm.inm_ifp != NULL)
908 igmp_leavegroup(&my_inm);