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.11 2004/06/07 02:36:22 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 (struct in_addr *);
61 static void in_len2mask (struct in_addr *, int);
62 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
63 struct ifnet *, struct thread *);
65 static void in_socktrim (struct sockaddr_in *);
66 static int in_ifinit (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 u_long i = ntohl(in.s_addr);
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 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 char *cplim = (char *) &ap->sin_addr;
133 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.
187 * NOTE! td might be NULL.
191 in_control(so, cmd, data, ifp, td)
198 struct ifreq *ifr = (struct ifreq *)data;
199 struct in_ifaddr *ia = 0, *iap;
202 struct in_ifaddr *oia;
203 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
204 struct sockaddr_in oldaddr;
205 int hostIsNew, iaIsNew, maskIsNew, s;
213 if (td && (error = suser(td)) != 0)
219 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
223 * Find address for this interface, if it exists.
225 * If an alias address was specified, find that one instead of
226 * the first one on the interface, if possible
229 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
230 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
231 if (iap->ia_ifp == ifp &&
232 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
237 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
239 if (iap->ia_addr.sin_family == AF_INET) {
251 return (EADDRNOTAVAIL);
252 if (ifra->ifra_addr.sin_family == AF_INET) {
253 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
254 if (ia->ia_ifp == ifp &&
255 ia->ia_addr.sin_addr.s_addr ==
256 ifra->ifra_addr.sin_addr.s_addr)
259 if ((ifp->if_flags & IFF_POINTOPOINT)
260 && (cmd == SIOCAIFADDR)
261 && (ifra->ifra_dstaddr.sin_addr.s_addr
266 if (cmd == SIOCDIFADDR && ia == 0)
267 return (EADDRNOTAVAIL);
272 if (td && (error = suser(td)) != 0)
276 return (EADDRNOTAVAIL);
277 if (ia == (struct in_ifaddr *)0) {
278 ia = (struct in_ifaddr *)
279 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
280 if (ia == (struct in_ifaddr *)NULL)
282 bzero((caddr_t)ia, sizeof *ia);
284 * Protect from ipintr() traversing address list
285 * while we're modifying it.
289 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
291 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
293 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
294 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
295 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
296 ia->ia_sockmask.sin_len = 8;
297 ia->ia_sockmask.sin_family = AF_INET;
298 if (ifp->if_flags & IFF_BROADCAST) {
299 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
300 ia->ia_broadaddr.sin_family = AF_INET;
303 if (!(ifp->if_flags & IFF_LOOPBACK))
311 if (td && (error = suser(td)) != 0)
319 if (ia == (struct in_ifaddr *)0)
320 return (EADDRNOTAVAIL);
326 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
330 if ((ifp->if_flags & IFF_BROADCAST) == 0)
332 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
336 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
338 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
342 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
346 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
348 oldaddr = ia->ia_dstaddr;
349 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
351 (error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
352 td->td_proc->p_ucred))) {
353 ia->ia_dstaddr = oldaddr;
356 if (ia->ia_flags & IFA_ROUTE) {
357 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
358 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
359 ia->ia_ifa.ifa_dstaddr =
360 (struct sockaddr *)&ia->ia_dstaddr;
361 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
366 if ((ifp->if_flags & IFF_BROADCAST) == 0)
368 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
372 error = in_ifinit(ifp, ia,
373 (struct sockaddr_in *) &ifr->ifr_addr, 1);
374 if (error != 0 && iaIsNew)
379 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
380 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
387 if (ia->ia_addr.sin_family == AF_INET) {
388 if (ifra->ifra_addr.sin_len == 0) {
389 ifra->ifra_addr = ia->ia_addr;
391 } else if (ifra->ifra_addr.sin_addr.s_addr ==
392 ia->ia_addr.sin_addr.s_addr)
395 if (ifra->ifra_mask.sin_len) {
397 ia->ia_sockmask = ifra->ifra_mask;
398 ia->ia_sockmask.sin_family = AF_INET;
400 ntohl(ia->ia_sockmask.sin_addr.s_addr);
403 if ((ifp->if_flags & IFF_POINTOPOINT) &&
404 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
406 ia->ia_dstaddr = ifra->ifra_dstaddr;
407 maskIsNew = 1; /* We lie; but the effect's the same */
409 if (ifra->ifra_addr.sin_family == AF_INET &&
410 (hostIsNew || maskIsNew))
411 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
413 if (error != 0 && iaIsNew)
416 if ((ifp->if_flags & IFF_BROADCAST) &&
417 (ifra->ifra_broadaddr.sin_family == AF_INET))
418 ia->ia_broadaddr = ifra->ifra_broadaddr;
423 * in_ifscrub kills the interface route.
427 * in_ifadown gets rid of all the rest of
428 * the routes. This is not quite the right
429 * thing to do, but at least if we are running
430 * a routing process they will come back.
432 in_ifadown(&ia->ia_ifa, 1);
434 * XXX horrible hack to detect that we are being called
437 if (!ifnet_addrs[ifp->if_index - 1]) {
438 in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
439 in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
445 if (ifp == NULL || ifp->if_ioctl == NULL)
447 return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
451 * Protect from ipintr() traversing address list while we're modifying
455 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
456 TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
457 LIST_REMOVE(ia, ia_hash);
458 IFAFREE(&ia->ia_ifa);
466 * SIOCGLIFADDR: get first address. (?!?)
467 * SIOCGLIFADDR with IFLR_PREFIX:
468 * get first address that matches the specified prefix.
469 * SIOCALIFADDR: add the specified address.
470 * SIOCALIFADDR with IFLR_PREFIX:
471 * EINVAL since we can't deduce hostid part of the address.
472 * SIOCDLIFADDR: delete the specified address.
473 * SIOCDLIFADDR with IFLR_PREFIX:
474 * delete the first address that matches the specified prefix.
476 * EINVAL on invalid parameters
477 * EADDRNOTAVAIL on prefix match failed/specified address not found
478 * other values may be returned from in_ioctl()
480 * NOTE! td might be NULL.
483 in_lifaddr_ioctl(so, cmd, data, ifp, td)
490 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
495 panic("invalid argument to in_lifaddr_ioctl");
501 /* address must be specified on GET with IFLR_PREFIX */
502 if ((iflr->flags & IFLR_PREFIX) == 0)
507 /* address must be specified on ADD and DELETE */
508 if (iflr->addr.ss_family != AF_INET)
510 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
512 /* XXX need improvement */
513 if (iflr->dstaddr.ss_family
514 && iflr->dstaddr.ss_family != AF_INET)
516 if (iflr->dstaddr.ss_family
517 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
520 default: /*shouldn't happen*/
523 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
529 struct in_aliasreq ifra;
531 if (iflr->flags & IFLR_PREFIX)
534 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
535 bzero(&ifra, sizeof(ifra));
536 bcopy(iflr->iflr_name, ifra.ifra_name,
537 sizeof(ifra.ifra_name));
539 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
541 if (iflr->dstaddr.ss_family) { /*XXX*/
542 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
543 iflr->dstaddr.ss_len);
546 ifra.ifra_mask.sin_family = AF_INET;
547 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
548 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
550 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
555 struct in_ifaddr *ia;
556 struct in_addr mask, candidate, match;
557 struct sockaddr_in *sin;
560 bzero(&mask, sizeof(mask));
561 if (iflr->flags & IFLR_PREFIX) {
562 /* lookup a prefix rather than address. */
563 in_len2mask(&mask, iflr->prefixlen);
565 sin = (struct sockaddr_in *)&iflr->addr;
566 match.s_addr = sin->sin_addr.s_addr;
567 match.s_addr &= mask.s_addr;
569 /* if you set extra bits, that's wrong */
570 if (match.s_addr != sin->sin_addr.s_addr)
575 if (cmd == SIOCGLIFADDR) {
576 /* on getting an address, take the 1st match */
579 /* on deleting an address, do exact match */
580 in_len2mask(&mask, 32);
581 sin = (struct sockaddr_in *)&iflr->addr;
582 match.s_addr = sin->sin_addr.s_addr;
588 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
589 if (ifa->ifa_addr->sa_family != AF_INET6)
593 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
594 candidate.s_addr &= mask.s_addr;
595 if (candidate.s_addr == match.s_addr)
599 return EADDRNOTAVAIL;
600 ia = (struct in_ifaddr *)ifa;
602 if (cmd == SIOCGLIFADDR) {
603 /* fill in the if_laddrreq structure */
604 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
606 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
607 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
608 ia->ia_dstaddr.sin_len);
610 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
613 in_mask2len(&ia->ia_sockmask.sin_addr);
615 iflr->flags = 0; /*XXX*/
619 struct in_aliasreq ifra;
621 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
622 bzero(&ifra, sizeof(ifra));
623 bcopy(iflr->iflr_name, ifra.ifra_name,
624 sizeof(ifra.ifra_name));
626 bcopy(&ia->ia_addr, &ifra.ifra_addr,
627 ia->ia_addr.sin_len);
628 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
629 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
630 ia->ia_dstaddr.sin_len);
632 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
633 ia->ia_sockmask.sin_len);
635 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
641 return EOPNOTSUPP; /*just for safety*/
645 * Delete any existing route for an interface.
650 struct in_ifaddr *ia;
653 if ((ia->ia_flags & IFA_ROUTE) == 0)
655 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
656 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
658 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
659 ia->ia_flags &= ~IFA_ROUTE;
663 * Initialize an interface's internet address
664 * and routing table entry.
667 in_ifinit(ifp, ia, sin, scrub)
669 struct in_ifaddr *ia;
670 struct sockaddr_in *sin;
673 u_long i = ntohl(sin->sin_addr.s_addr);
674 struct sockaddr_in oldaddr;
675 int s = splimp(), flags = RTF_UP, error = 0;
677 oldaddr = ia->ia_addr;
678 if (oldaddr.sin_family == AF_INET)
679 LIST_REMOVE(ia, ia_hash);
681 if (ia->ia_addr.sin_family == AF_INET)
682 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
685 * Give the interface a chance to initialize
686 * if this is its first address,
687 * and to validate the address if necessary.
690 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia,
691 (struct ucred *)NULL))) {
693 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
694 ia->ia_addr = oldaddr;
695 if (ia->ia_addr.sin_family == AF_INET)
696 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
702 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
704 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
707 ia->ia_netmask = IN_CLASSA_NET;
708 else if (IN_CLASSB(i))
709 ia->ia_netmask = IN_CLASSB_NET;
711 ia->ia_netmask = IN_CLASSC_NET;
713 * The subnet mask usually includes at least the standard network part,
714 * but may may be smaller in the case of supernetting.
715 * If it is set, we believe it.
717 if (ia->ia_subnetmask == 0) {
718 ia->ia_subnetmask = ia->ia_netmask;
719 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
721 ia->ia_netmask &= ia->ia_subnetmask;
722 ia->ia_net = i & ia->ia_netmask;
723 ia->ia_subnet = i & ia->ia_subnetmask;
724 in_socktrim(&ia->ia_sockmask);
726 * Add route for the network.
728 ia->ia_ifa.ifa_metric = ifp->if_metric;
729 if (ifp->if_flags & IFF_BROADCAST) {
730 ia->ia_broadaddr.sin_addr.s_addr =
731 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
732 ia->ia_netbroadcast.s_addr =
733 htonl(ia->ia_net | ~ ia->ia_netmask);
734 } else if (ifp->if_flags & IFF_LOOPBACK) {
735 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
737 } else if (ifp->if_flags & IFF_POINTOPOINT) {
738 if (ia->ia_dstaddr.sin_family != AF_INET)
744 * Don't add host routes for interface addresses of
745 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0. This makes it
746 * possible to assign several such address pairs with consistent
747 * results (no host route) and is required by BOOTP.
749 * XXX: This is ugly ! There should be a way for the caller to
750 * say that they don't want a host route.
752 if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
753 ia->ia_netmask != IN_CLASSA_NET ||
754 ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
755 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
756 ia->ia_addr = oldaddr;
759 ia->ia_flags |= IFA_ROUTE;
763 * If the interface supports multicast, join the "all hosts"
764 * multicast group on that interface.
766 if (ifp->if_flags & IFF_MULTICAST) {
769 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
770 in_addmulti(&addr, ifp);
777 * Return 1 if the address might be a local broadcast address.
780 in_broadcast(in, ifp)
787 if (in.s_addr == INADDR_BROADCAST ||
788 in.s_addr == INADDR_ANY)
790 if ((ifp->if_flags & IFF_BROADCAST) == 0)
792 t = ntohl(in.s_addr);
794 * Look through the list of addresses for a match
795 * with a broadcast address.
797 #define ia ((struct in_ifaddr *)ifa)
798 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
799 if (ifa->ifa_addr->sa_family == AF_INET &&
800 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
801 in.s_addr == ia->ia_netbroadcast.s_addr ||
803 * Check for old-style (host 0) broadcast.
805 t == ia->ia_subnet || t == ia->ia_net) &&
807 * Check for an all one subnetmask. These
808 * only exist when an interface gets a secondary
811 ia->ia_subnetmask != (u_long)0xffffffff)
817 * Add an address to the list of IP multicast addresses for a given interface.
824 struct in_multi *inm;
826 struct sockaddr_in sin;
827 struct ifmultiaddr *ifma;
831 * Call generic routine to add membership or increment
832 * refcount. It wants addresses in the form of a sockaddr,
833 * so we build one here (being careful to zero the unused bytes).
835 bzero(&sin, sizeof sin);
836 sin.sin_family = AF_INET;
837 sin.sin_len = sizeof sin;
839 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
846 * If ifma->ifma_protospec is null, then if_addmulti() created
847 * a new record. Otherwise, we are done.
849 if (ifma->ifma_protospec != 0) {
851 return ifma->ifma_protospec;
854 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
855 at interrupt time? If so, need to fix if_addmulti. XXX */
856 inm = malloc(sizeof(*inm), M_IPMADDR, M_WAITOK | M_ZERO);
859 inm->inm_ifma = ifma;
860 ifma->ifma_protospec = inm;
861 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
864 * Let IGMP know that we have joined a new IP multicast group.
872 * Delete a multicast address record.
876 struct in_multi *inm;
878 struct ifmultiaddr *ifma = inm->inm_ifma;
879 struct in_multi my_inm;
882 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
883 if (ifma->ifma_refcount == 1) {
885 * No remaining claims to this record; let IGMP know that
886 * we are leaving the multicast group.
887 * But do it after the if_delmulti() which might reset
888 * the interface and nuke the packet.
891 ifma->ifma_protospec = 0;
892 LIST_REMOVE(inm, inm_link);
893 free(inm, M_IPMADDR);
895 /* XXX - should be separate API for when we have an ifma? */
896 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
897 if (my_inm.inm_ifp != NULL)
898 igmp_leavegroup(&my_inm);