2 * Copyright (c) 2007 Bruce M. Simpson.
3 * Copyright (c) 2005 Robert N. M. Watson.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote
15 * products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * IPv4 multicast socket, group, and socket option processing module.
33 * Until further notice, this file requires INET to compile.
34 * TODO: Make this infrastructure independent of address family.
35 * TODO: Teach netinet6 to use this code.
36 * TODO: Hook up SSM logic to IGMPv3/MLDv2.
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
52 #include <net/if_dl.h>
53 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/igmp_var.h>
62 #ifndef __SOCKUNION_DECLARED
64 struct sockaddr_storage ss;
66 struct sockaddr_dl sdl;
67 struct sockaddr_in sin;
69 struct sockaddr_in6 sin6;
72 typedef union sockunion sockunion_t;
73 #define __SOCKUNION_DECLARED
74 #endif /* __SOCKUNION_DECLARED */
76 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
77 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
78 static MALLOC_DEFINE(M_IPMSOURCE, "in_msource", "IPv4 multicast source filter");
81 * The IPv4 multicast list (in_multihead and associated structures) are
82 * protected by the global in_multi_mtx. See in_var.h for more details. For
83 * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
84 * ip_output() to send IGMP packets while holding the lock; this probably is
85 * not quite desirable.
87 struct in_multihead in_multihead; /* XXX BSS initialization */
88 struct mtx in_multi_mtx;
89 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
92 * Functions with non-static linkage defined in this file should be
93 * declared in in_var.h:
98 * in_delmulti_locked()
104 static int imo_grow(struct ip_moptions *);
105 static int imo_join_source(struct ip_moptions *, size_t, sockunion_t *);
106 static int imo_leave_source(struct ip_moptions *, size_t, sockunion_t *);
107 static int inp_change_source_filter(struct inpcb *, struct sockopt *);
108 static struct ip_moptions *
109 inp_findmoptions(struct inpcb *);
110 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
111 static int inp_join_group(struct inpcb *, struct sockopt *);
112 static int inp_leave_group(struct inpcb *, struct sockopt *);
113 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
114 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
117 * Resize the ip_moptions vector to the next power-of-two minus 1.
118 * May be called with locks held; do not sleep.
121 imo_grow(struct ip_moptions *imo)
123 struct in_multi **nmships;
124 struct in_multi **omships;
125 struct in_mfilter *nmfilters;
126 struct in_mfilter *omfilters;
133 omships = imo->imo_membership;
134 omfilters = imo->imo_mfilters;
135 oldmax = imo->imo_max_memberships;
136 newmax = ((oldmax + 1) * 2) - 1;
138 if (newmax <= IP_MAX_MEMBERSHIPS) {
139 nmships = (struct in_multi **)realloc(omships,
140 sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
141 nmfilters = (struct in_mfilter *)realloc(omfilters,
142 sizeof(struct in_mfilter) * newmax, M_IPMSOURCE, M_NOWAIT);
143 if (nmships != NULL && nmfilters != NULL) {
144 /* Initialize newly allocated source filter heads. */
145 for (idx = oldmax; idx < newmax; idx++) {
146 nmfilters[idx].imf_fmode = MCAST_EXCLUDE;
147 nmfilters[idx].imf_nsources = 0;
148 TAILQ_INIT(&nmfilters[idx].imf_sources);
150 imo->imo_max_memberships = newmax;
151 imo->imo_membership = nmships;
152 imo->imo_mfilters = nmfilters;
156 if (nmships == NULL || nmfilters == NULL) {
158 free(nmships, M_IPMOPTS);
159 if (nmfilters != NULL)
160 free(nmfilters, M_IPMSOURCE);
161 return (ETOOMANYREFS);
168 * Add a source to a multicast filter list.
169 * Assumes the associated inpcb is locked.
172 imo_join_source(struct ip_moptions *imo, size_t gidx, sockunion_t *src)
174 struct in_msource *ims, *nims;
175 struct in_mfilter *imf;
177 KASSERT(src->ss.ss_family == AF_INET, ("%s: !AF_INET", __func__));
178 KASSERT(imo->imo_mfilters != NULL,
179 ("%s: imo_mfilters vector not allocated", __func__));
181 imf = &imo->imo_mfilters[gidx];
182 if (imf->imf_nsources == IP_MAX_SOURCE_FILTER)
185 ims = imo_match_source(imo, gidx, &src->sa);
187 return (EADDRNOTAVAIL);
189 /* Do not sleep with inp lock held. */
190 MALLOC(nims, struct in_msource *, sizeof(struct in_msource),
191 M_IPMSOURCE, M_NOWAIT | M_ZERO);
195 nims->ims_addr = src->ss;
196 TAILQ_INSERT_TAIL(&imf->imf_sources, nims, ims_next);
203 imo_leave_source(struct ip_moptions *imo, size_t gidx, sockunion_t *src)
205 struct in_msource *ims;
206 struct in_mfilter *imf;
208 KASSERT(src->ss.ss_family == AF_INET, ("%s: !AF_INET", __func__));
209 KASSERT(imo->imo_mfilters != NULL,
210 ("%s: imo_mfilters vector not allocated", __func__));
212 imf = &imo->imo_mfilters[gidx];
213 if (imf->imf_nsources == IP_MAX_SOURCE_FILTER)
216 ims = imo_match_source(imo, gidx, &src->sa);
218 return (EADDRNOTAVAIL);
220 TAILQ_REMOVE(&imf->imf_sources, ims, ims_next);
221 FREE(ims, M_IPMSOURCE);
228 * Find an IPv4 multicast group entry for this ip_moptions instance
229 * which matches the specified group, and optionally an interface.
230 * Return its index into the array, or -1 if not found.
233 imo_match_group(struct ip_moptions *imo, struct ifnet *ifp,
234 struct sockaddr *group)
237 struct in_multi **pinm;
241 gsa = (sockunion_t *)group;
243 /* The imo_membership array may be lazy allocated. */
244 if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
247 nmships = imo->imo_num_memberships;
248 pinm = &imo->imo_membership[0];
249 for (idx = 0; idx < nmships; idx++, pinm++) {
253 printf("%s: trying ifp = %p, inaddr = %s ", __func__,
254 ifp, inet_ntoa(gsa->sin.sin_addr));
255 printf("against %p, %s\n",
256 (*pinm)->inm_ifp, inet_ntoa((*pinm)->inm_addr));
258 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
259 (*pinm)->inm_addr.s_addr == gsa->sin.sin_addr.s_addr) {
270 * Find a multicast source entry for this imo which matches
271 * the given group index for this socket, and source address.
274 imo_match_source(struct ip_moptions *imo, size_t gidx, struct sockaddr *src)
276 struct in_mfilter *imf;
277 struct in_msource *ims, *pims;
279 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
280 KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
281 ("%s: invalid index %d\n", __func__, (int)gidx));
283 /* The imo_mfilters array may be lazy allocated. */
284 if (imo->imo_mfilters == NULL)
288 imf = &imo->imo_mfilters[gidx];
289 TAILQ_FOREACH(ims, &imf->imf_sources, ims_next) {
291 * Perform bitwise comparison of two IPv4 addresses.
292 * TODO: Do the same for IPv6.
293 * Do not use sa_equal() for this as it is not aware of
294 * deeper structure in sockaddr_in or sockaddr_in6.
296 if (((struct sockaddr_in *)&ims->ims_addr)->sin_addr.s_addr ==
297 ((struct sockaddr_in *)src)->sin_addr.s_addr) {
307 * Join an IPv4 multicast group.
310 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
312 struct in_multi *inm;
319 IN_LOOKUP_MULTI(*ap, ifp, inm);
322 * If we already joined this group, just bump the
323 * refcount and return it.
325 KASSERT(inm->inm_refcount >= 1,
326 ("%s: bad refcount %d", __func__, inm->inm_refcount));
330 struct ifmultiaddr *ifma;
331 struct in_multi *ninm;
334 memset(&gsa, 0, sizeof(gsa));
335 gsa.sin.sin_family = AF_INET;
336 gsa.sin.sin_len = sizeof(struct sockaddr_in);
337 gsa.sin.sin_addr = *ap;
340 * Check if a link-layer group is already associated
341 * with this network-layer group on the given ifnet.
342 * If so, bump the refcount on the existing network-layer
343 * group association and return it.
345 error = if_addmulti(ifp, &gsa.sa, &ifma);
348 if (ifma->ifma_protospec != NULL) {
349 inm = (struct in_multi *)ifma->ifma_protospec;
351 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
352 inm->inm_addr.s_addr != ap->s_addr)
353 panic("%s: ifma is inconsistent", __func__);
360 * A new membership is needed; construct it and
361 * perform the IGMP join.
363 ninm = malloc(sizeof(*ninm), M_IPMADDR, M_NOWAIT | M_ZERO);
365 if_delmulti_ifma(ifma);
368 ninm->inm_addr = *ap;
370 ninm->inm_ifma = ifma;
371 ninm->inm_refcount = 1;
372 ifma->ifma_protospec = ninm;
373 LIST_INSERT_HEAD(&in_multihead, ninm, inm_link);
375 igmp_joingroup(ninm);
381 IFF_UNLOCKGIANT(ifp);
387 * Leave an IPv4 multicast group.
388 * It is OK to call this routine if the underlying ifnet went away.
390 * XXX: To deal with the ifp going away, we cheat; the link-layer code in net
391 * will set ifma_ifp to NULL when the associated ifnet instance is detached
394 * The only reason we need to violate layers and check ifma_ifp here at all
395 * is because certain hardware drivers still require Giant to be held,
396 * and it must always be taken before other locks.
399 in_delmulti(struct in_multi *inm)
403 KASSERT(inm != NULL, ("%s: inm is NULL", __func__));
404 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
405 ifp = inm->inm_ifma->ifma_ifp;
409 * Sanity check that netinet's notion of ifp is the
412 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
417 in_delmulti_locked(inm);
421 IFF_UNLOCKGIANT(ifp);
425 * Delete a multicast address record, with locks held.
427 * It is OK to call this routine if the ifp went away.
428 * Assumes that caller holds the IN_MULTI lock, and that
429 * Giant was taken before other locks if required by the hardware.
432 in_delmulti_locked(struct in_multi *inm)
434 struct ifmultiaddr *ifma;
436 IN_MULTI_LOCK_ASSERT();
437 KASSERT(inm->inm_refcount >= 1, ("%s: freeing freed inm", __func__));
439 if (--inm->inm_refcount == 0) {
440 igmp_leavegroup(inm);
442 ifma = inm->inm_ifma;
445 printf("%s: purging ifma %p\n", __func__, ifma);
447 KASSERT(ifma->ifma_protospec == inm,
448 ("%s: ifma_protospec != inm", __func__));
449 ifma->ifma_protospec = NULL;
451 LIST_REMOVE(inm, inm_link);
452 free(inm, M_IPMADDR);
454 if_delmulti_ifma(ifma);
459 * Block or unblock an ASM/SSM multicast source on an inpcb.
462 inp_change_source_filter(struct inpcb *inp, struct sockopt *sopt)
464 struct group_source_req gsr;
465 sockunion_t *gsa, *ssa;
467 struct in_mfilter *imf;
468 struct ip_moptions *imo;
469 struct in_msource *ims;
478 memset(&gsr, 0, sizeof(struct group_source_req));
479 gsa = (sockunion_t *)&gsr.gsr_group;
480 ssa = (sockunion_t *)&gsr.gsr_source;
482 switch (sopt->sopt_name) {
483 case IP_BLOCK_SOURCE:
484 case IP_UNBLOCK_SOURCE: {
485 struct ip_mreq_source mreqs;
487 error = sooptcopyin(sopt, &mreqs,
488 sizeof(struct ip_mreq_source),
489 sizeof(struct ip_mreq_source));
493 gsa->sin.sin_family = AF_INET;
494 gsa->sin.sin_len = sizeof(struct sockaddr_in);
495 gsa->sin.sin_addr = mreqs.imr_multiaddr;
497 ssa->sin.sin_family = AF_INET;
498 ssa->sin.sin_len = sizeof(struct sockaddr_in);
499 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
501 if (mreqs.imr_interface.s_addr != INADDR_ANY)
502 INADDR_TO_IFP(mreqs.imr_interface, ifp);
504 if (sopt->sopt_name == IP_BLOCK_SOURCE)
509 printf("%s: imr_interface = %s, ifp = %p\n",
510 __func__, inet_ntoa(mreqs.imr_interface), ifp);
516 case MCAST_BLOCK_SOURCE:
517 case MCAST_UNBLOCK_SOURCE:
518 error = sooptcopyin(sopt, &gsr,
519 sizeof(struct group_source_req),
520 sizeof(struct group_source_req));
524 if (gsa->sin.sin_family != AF_INET ||
525 gsa->sin.sin_len != sizeof(struct sockaddr_in))
528 if (ssa->sin.sin_family != AF_INET ||
529 ssa->sin.sin_len != sizeof(struct sockaddr_in))
532 if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface)
533 return (EADDRNOTAVAIL);
535 ifp = ifnet_byindex(gsr.gsr_interface);
537 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
544 printf("%s: unknown sopt_name %d\n", __func__,
553 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
557 * Check if we are actually a member of this group.
559 imo = inp_findmoptions(inp);
560 idx = imo_match_group(imo, ifp, &gsa->sa);
561 if (idx == -1 || imo->imo_mfilters == NULL) {
562 error = EADDRNOTAVAIL;
566 KASSERT(imo->imo_mfilters != NULL,
567 ("%s: imo_mfilters not allocated", __func__));
568 imf = &imo->imo_mfilters[idx];
571 * SSM multicast truth table for block/unblock operations.
573 * Operation Filter Mode Entry exists? Action
575 * block exclude no add source to filter
576 * unblock include no add source to filter
577 * block include no EINVAL
578 * unblock exclude no EINVAL
579 * block exclude yes EADDRNOTAVAIL
580 * unblock include yes EADDRNOTAVAIL
581 * block include yes remove source from filter
582 * unblock exclude yes remove source from filter
584 * FreeBSD does not explicitly distinguish between ASM and SSM
585 * mode sockets; all sockets are assumed to have a filter list.
589 printf("%s: imf_fmode is %s\n", __func__,
590 imf->imf_fmode == MCAST_INCLUDE ? "include" : "exclude");
593 ims = imo_match_source(imo, idx, &ssa->sa);
595 if ((block == 1 && imf->imf_fmode == MCAST_EXCLUDE) ||
596 (block == 0 && imf->imf_fmode == MCAST_INCLUDE)) {
599 printf("%s: adding %s to filter list\n",
600 __func__, inet_ntoa(ssa->sin.sin_addr));
603 error = imo_join_source(imo, idx, ssa);
605 if ((block == 1 && imf->imf_fmode == MCAST_INCLUDE) ||
606 (block == 0 && imf->imf_fmode == MCAST_EXCLUDE)) {
608 * If the socket is in inclusive mode:
609 * the source is already blocked as it has no entry.
610 * If the socket is in exclusive mode:
611 * the source is already unblocked as it has no entry.
615 printf("%s: ims %p; %s already [un]blocked\n",
617 inet_ntoa(ssa->sin.sin_addr));
623 if ((block == 1 && imf->imf_fmode == MCAST_EXCLUDE) ||
624 (block == 0 && imf->imf_fmode == MCAST_INCLUDE)) {
626 * If the socket is in exclusive mode:
627 * the source is already blocked as it has an entry.
628 * If the socket is in inclusive mode:
629 * the source is already unblocked as it has an entry.
633 printf("%s: ims %p; %s already [un]blocked\n",
635 inet_ntoa(ssa->sin.sin_addr));
638 error = EADDRNOTAVAIL;
640 if ((block == 1 && imf->imf_fmode == MCAST_INCLUDE) ||
641 (block == 0 && imf->imf_fmode == MCAST_EXCLUDE)) {
644 printf("%s: removing %s from filter list\n",
645 __func__, inet_ntoa(ssa->sin.sin_addr));
648 error = imo_leave_source(imo, idx, ssa);
658 * Given an inpcb, return its multicast options structure pointer. Accepts
659 * an unlocked inpcb pointer, but will return it locked. May sleep.
661 static struct ip_moptions *
662 inp_findmoptions(struct inpcb *inp)
664 struct ip_moptions *imo;
665 struct in_multi **immp;
666 struct in_mfilter *imfp;
670 if (inp->inp_moptions != NULL)
671 return (inp->inp_moptions);
675 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
677 immp = (struct in_multi **)malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS,
678 M_IPMOPTS, M_WAITOK | M_ZERO);
679 imfp = (struct in_mfilter *)malloc(
680 sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
681 M_IPMSOURCE, M_WAITOK);
683 imo->imo_multicast_ifp = NULL;
684 imo->imo_multicast_addr.s_addr = INADDR_ANY;
685 imo->imo_multicast_vif = -1;
686 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
687 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
688 imo->imo_num_memberships = 0;
689 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
690 imo->imo_membership = immp;
692 /* Initialize per-group source filters. */
693 for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++) {
694 imfp[idx].imf_fmode = MCAST_EXCLUDE;
695 imfp[idx].imf_nsources = 0;
696 TAILQ_INIT(&imfp[idx].imf_sources);
698 imo->imo_mfilters = imfp;
701 if (inp->inp_moptions != NULL) {
702 free(imfp, M_IPMSOURCE);
703 free(immp, M_IPMOPTS);
704 free(imo, M_IPMOPTS);
705 return (inp->inp_moptions);
707 inp->inp_moptions = imo;
712 * Discard the IP multicast options (and source filters).
715 inp_freemoptions(struct ip_moptions *imo)
717 struct in_mfilter *imf;
718 struct in_msource *ims, *tims;
721 KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__));
723 nmships = imo->imo_num_memberships;
724 for (idx = 0; idx < nmships; ++idx) {
725 in_delmulti(imo->imo_membership[idx]);
727 if (imo->imo_mfilters != NULL) {
728 imf = &imo->imo_mfilters[idx];
729 TAILQ_FOREACH_SAFE(ims, &imf->imf_sources,
731 TAILQ_REMOVE(&imf->imf_sources, ims, ims_next);
732 FREE(ims, M_IPMSOURCE);
735 KASSERT(imf->imf_nsources == 0,
736 ("%s: did not free all imf_nsources", __func__));
740 if (imo->imo_mfilters != NULL)
741 free(imo->imo_mfilters, M_IPMSOURCE);
742 free(imo->imo_membership, M_IPMOPTS);
743 free(imo, M_IPMOPTS);
747 * Atomically get source filters on a socket for an IPv4 multicast group.
748 * Called with INP lock held; returns with lock released.
751 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
753 struct __msfilterreq msfr;
756 struct ip_moptions *imo;
757 struct in_mfilter *imf;
758 struct in_msource *ims;
759 struct sockaddr_storage *ptss;
760 struct sockaddr_storage *tss;
764 INP_LOCK_ASSERT(inp);
766 imo = inp->inp_moptions;
767 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
771 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
772 sizeof(struct __msfilterreq));
776 if (msfr.msfr_ifindex == 0 || if_index < msfr.msfr_ifindex)
779 ifp = ifnet_byindex(msfr.msfr_ifindex);
786 * Lookup group on the socket.
788 gsa = (sockunion_t *)&msfr.msfr_group;
789 idx = imo_match_group(imo, ifp, &gsa->sa);
790 if (idx == -1 || imo->imo_mfilters == NULL) {
792 return (EADDRNOTAVAIL);
795 imf = &imo->imo_mfilters[idx];
796 msfr.msfr_fmode = imf->imf_fmode;
797 msfr.msfr_nsrcs = imf->imf_nsources;
800 * If the user specified a buffer, copy out the source filter
801 * entries to userland gracefully.
802 * msfr.msfr_nsrcs is always set to the total number of filter
803 * entries which the kernel currently has for this group.
806 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
808 * Make a copy of the source vector so that we do not
809 * thrash the inpcb lock whilst copying it out.
810 * We only copy out the number of entries which userland
811 * has asked for, but we always tell userland how big the
812 * buffer really needs to be.
814 MALLOC(tss, struct sockaddr_storage *,
815 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
821 TAILQ_FOREACH(ims, &imf->imf_sources, ims_next) {
822 memcpy(ptss++, &ims->ims_addr,
823 sizeof(struct sockaddr_storage));
831 error = copyout(tss, msfr.msfr_srcs,
832 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
839 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
845 * Return the IP multicast options in response to user getsockopt().
848 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
850 struct ip_mreqn mreqn;
851 struct ip_moptions *imo;
853 struct in_ifaddr *ia;
858 imo = inp->inp_moptions;
861 switch (sopt->sopt_name) {
862 case IP_MULTICAST_VIF:
864 optval = imo->imo_multicast_vif;
868 error = sooptcopyout(sopt, &optval, sizeof(int));
871 case IP_MULTICAST_IF:
872 memset(&mreqn, 0, sizeof(struct ip_mreqn));
874 ifp = imo->imo_multicast_ifp;
875 if (imo->imo_multicast_addr.s_addr != INADDR_ANY) {
876 mreqn.imr_address = imo->imo_multicast_addr;
877 } else if (ifp != NULL) {
878 mreqn.imr_ifindex = ifp->if_index;
882 IA_SIN(ia)->sin_addr;
887 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
888 error = sooptcopyout(sopt, &mreqn,
889 sizeof(struct ip_mreqn));
891 error = sooptcopyout(sopt, &mreqn.imr_address,
892 sizeof(struct in_addr));
896 case IP_MULTICAST_TTL:
898 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
900 optval = coptval = imo->imo_multicast_ttl;
902 if (sopt->sopt_valsize == sizeof(u_char))
903 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
905 error = sooptcopyout(sopt, &optval, sizeof(int));
908 case IP_MULTICAST_LOOP:
910 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
912 optval = coptval = imo->imo_multicast_loop;
914 if (sopt->sopt_valsize == sizeof(u_char))
915 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
917 error = sooptcopyout(sopt, &optval, sizeof(int));
922 error = EADDRNOTAVAIL;
925 error = inp_get_source_filters(inp, sopt);
935 INP_UNLOCK_ASSERT(inp);
941 * Join an IPv4 multicast group, possibly with a source.
944 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
946 struct group_source_req gsr;
947 sockunion_t *gsa, *ssa;
949 struct in_mfilter *imf;
950 struct ip_moptions *imo;
951 struct in_multi *inm;
958 memset(&gsr, 0, sizeof(struct group_source_req));
959 gsa = (sockunion_t *)&gsr.gsr_group;
960 gsa->ss.ss_family = AF_UNSPEC;
961 ssa = (sockunion_t *)&gsr.gsr_source;
962 ssa->ss.ss_family = AF_UNSPEC;
964 switch (sopt->sopt_name) {
965 case IP_ADD_MEMBERSHIP:
966 case IP_ADD_SOURCE_MEMBERSHIP: {
967 struct ip_mreq_source mreqs;
969 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
970 error = sooptcopyin(sopt, &mreqs,
971 sizeof(struct ip_mreq),
972 sizeof(struct ip_mreq));
974 * Do argument switcharoo from ip_mreq into
975 * ip_mreq_source to avoid using two instances.
977 mreqs.imr_interface = mreqs.imr_sourceaddr;
978 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
979 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
980 error = sooptcopyin(sopt, &mreqs,
981 sizeof(struct ip_mreq_source),
982 sizeof(struct ip_mreq_source));
987 gsa->sin.sin_family = AF_INET;
988 gsa->sin.sin_len = sizeof(struct sockaddr_in);
989 gsa->sin.sin_addr = mreqs.imr_multiaddr;
991 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
992 ssa->sin.sin_family = AF_INET;
993 ssa->sin.sin_len = sizeof(struct sockaddr_in);
994 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
998 * Obtain ifp. If no interface address was provided,
999 * use the interface of the route to the given multicast
1000 * address (usually this is the default route).
1002 if (mreqs.imr_interface.s_addr != INADDR_ANY) {
1003 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1008 *(struct sockaddr_in *)&ro.ro_dst = gsa->sin;
1009 rtalloc_ign(&ro, RTF_CLONING);
1010 if (ro.ro_rt == NULL) {
1012 printf("%s: no route to %s\n", __func__,
1013 inet_ntoa(gsa->sin.sin_addr));
1015 return (EADDRNOTAVAIL);
1017 ifp = ro.ro_rt->rt_ifp;
1018 KASSERT(ifp != NULL, ("%s: null ifp", __func__));
1023 printf("%s: imr_interface = %s, ifp = %p\n",
1024 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1030 case MCAST_JOIN_GROUP:
1031 case MCAST_JOIN_SOURCE_GROUP:
1032 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1033 error = sooptcopyin(sopt, &gsr,
1034 sizeof(struct group_req),
1035 sizeof(struct group_req));
1036 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1037 error = sooptcopyin(sopt, &gsr,
1038 sizeof(struct group_source_req),
1039 sizeof(struct group_source_req));
1044 if (gsa->sin.sin_family != AF_INET ||
1045 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1049 * Overwrite the port field if present, as the sockaddr
1050 * being copied in may be matched with a binary comparison.
1053 gsa->sin.sin_port = 0;
1054 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1055 if (ssa->sin.sin_family != AF_INET ||
1056 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1058 ssa->sin.sin_port = 0;
1064 if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface)
1065 return (EADDRNOTAVAIL);
1066 ifp = ifnet_byindex(gsr.gsr_interface);
1073 printf("%s: unknown sopt_name %d\n", __func__,
1077 return (EOPNOTSUPP);
1081 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1084 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1085 return (EADDRNOTAVAIL);
1088 * Check if we already hold membership of this group for this inpcb.
1089 * If so, we do not need to perform the initial join.
1091 imo = inp_findmoptions(inp);
1092 idx = imo_match_group(imo, ifp, &gsa->sa);
1094 if (ssa->ss.ss_family != AF_UNSPEC) {
1096 * Attempting to join an ASM group (when already
1097 * an ASM or SSM member) is an error.
1099 error = EADDRNOTAVAIL;
1101 imf = &imo->imo_mfilters[idx];
1102 if (imf->imf_nsources == 0) {
1104 * Attempting to join an SSM group (when
1105 * already an ASM member) is an error.
1110 * Attempting to join an SSM group (when
1111 * already an SSM member) means "add this
1112 * source to the inclusive filter list".
1114 error = imo_join_source(imo, idx, ssa);
1121 * Call imo_grow() to reallocate the membership and source filter
1122 * vectors if they are full. If the size would exceed the hard limit,
1123 * then we know we've really run out of entries. We keep the INP
1124 * lock held to avoid introducing a race condition.
1126 if (imo->imo_num_memberships == imo->imo_max_memberships) {
1127 error = imo_grow(imo);
1133 * So far, so good: perform the layer 3 join, layer 2 join,
1134 * and make an IGMP announcement if needed.
1136 inm = in_addmulti(&gsa->sin.sin_addr, ifp);
1141 idx = imo->imo_num_memberships;
1142 imo->imo_membership[idx] = inm;
1143 imo->imo_num_memberships++;
1145 KASSERT(imo->imo_mfilters != NULL,
1146 ("%s: imf_mfilters vector was not allocated", __func__));
1147 imf = &imo->imo_mfilters[idx];
1148 KASSERT(TAILQ_EMPTY(&imf->imf_sources),
1149 ("%s: imf_sources not empty", __func__));
1152 * If this is a new SSM group join (i.e. a source was specified
1153 * with this group), add this source to the filter list.
1155 if (ssa->ss.ss_family != AF_UNSPEC) {
1157 * An initial SSM join implies that this socket's membership
1158 * of the multicast group is now in inclusive mode.
1160 imf->imf_fmode = MCAST_INCLUDE;
1162 error = imo_join_source(imo, idx, ssa);
1165 * Drop inp lock before calling in_delmulti(),
1166 * to prevent a lock order reversal.
1168 --imo->imo_num_memberships;
1181 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
1184 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
1186 struct group_source_req gsr;
1187 struct ip_mreq_source mreqs;
1188 sockunion_t *gsa, *ssa;
1190 struct in_mfilter *imf;
1191 struct ip_moptions *imo;
1192 struct in_msource *ims, *tims;
1193 struct in_multi *inm;
1200 memset(&gsr, 0, sizeof(struct group_source_req));
1201 gsa = (sockunion_t *)&gsr.gsr_group;
1202 gsa->ss.ss_family = AF_UNSPEC;
1203 ssa = (sockunion_t *)&gsr.gsr_source;
1204 ssa->ss.ss_family = AF_UNSPEC;
1206 switch (sopt->sopt_name) {
1207 case IP_DROP_MEMBERSHIP:
1208 case IP_DROP_SOURCE_MEMBERSHIP:
1209 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
1210 error = sooptcopyin(sopt, &mreqs,
1211 sizeof(struct ip_mreq),
1212 sizeof(struct ip_mreq));
1214 * Swap interface and sourceaddr arguments,
1215 * as ip_mreq and ip_mreq_source are laid
1218 mreqs.imr_interface = mreqs.imr_sourceaddr;
1219 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
1220 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
1221 error = sooptcopyin(sopt, &mreqs,
1222 sizeof(struct ip_mreq_source),
1223 sizeof(struct ip_mreq_source));
1228 gsa->sin.sin_family = AF_INET;
1229 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1230 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1232 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
1233 ssa->sin.sin_family = AF_INET;
1234 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1235 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1238 if (gsa->sin.sin_addr.s_addr != INADDR_ANY)
1239 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1243 printf("%s: imr_interface = %s, ifp = %p\n",
1244 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1249 case MCAST_LEAVE_GROUP:
1250 case MCAST_LEAVE_SOURCE_GROUP:
1251 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
1252 error = sooptcopyin(sopt, &gsr,
1253 sizeof(struct group_req),
1254 sizeof(struct group_req));
1255 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
1256 error = sooptcopyin(sopt, &gsr,
1257 sizeof(struct group_source_req),
1258 sizeof(struct group_source_req));
1263 if (gsa->sin.sin_family != AF_INET ||
1264 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1267 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
1268 if (ssa->sin.sin_family != AF_INET ||
1269 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1273 if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface)
1274 return (EADDRNOTAVAIL);
1276 ifp = ifnet_byindex(gsr.gsr_interface);
1282 printf("%s: unknown sopt_name %d\n", __func__,
1286 return (EOPNOTSUPP);
1290 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1294 * Find the membership in the membership array.
1296 imo = inp_findmoptions(inp);
1297 idx = imo_match_group(imo, ifp, &gsa->sa);
1299 error = EADDRNOTAVAIL;
1302 imf = &imo->imo_mfilters[idx];
1305 * If we were instructed only to leave a given source, do so.
1307 if (ssa->ss.ss_family != AF_UNSPEC) {
1308 if (imf->imf_nsources == 0 ||
1309 imf->imf_fmode == MCAST_EXCLUDE) {
1311 * Attempting to SSM leave an ASM group
1312 * is an error; should use *_BLOCK_SOURCE instead.
1313 * Attempting to SSM leave a source in a group when
1314 * the socket is in 'exclude mode' is also an error.
1318 error = imo_leave_source(imo, idx, ssa);
1321 * If an error occurred, or this source is not the last
1322 * source in the group, do not leave the whole group.
1324 if (error || imf->imf_nsources > 0)
1329 * Give up the multicast address record to which the membership points.
1331 inm = imo->imo_membership[idx];
1335 * Free any source filters for this group if they exist.
1336 * Revert inpcb to the default MCAST_EXCLUDE state.
1338 if (imo->imo_mfilters != NULL) {
1339 TAILQ_FOREACH_SAFE(ims, &imf->imf_sources, ims_next, tims) {
1340 TAILQ_REMOVE(&imf->imf_sources, ims, ims_next);
1341 FREE(ims, M_IPMSOURCE);
1342 imf->imf_nsources--;
1344 KASSERT(imf->imf_nsources == 0,
1345 ("%s: imf_nsources not 0", __func__));
1346 KASSERT(TAILQ_EMPTY(&imf->imf_sources),
1347 ("%s: imf_sources not empty", __func__));
1348 imf->imf_fmode = MCAST_EXCLUDE;
1352 * Remove the gap in the membership array.
1354 for (++idx; idx < imo->imo_num_memberships; ++idx)
1355 imo->imo_membership[idx-1] = imo->imo_membership[idx];
1356 imo->imo_num_memberships--;
1364 * Select the interface for transmitting IPv4 multicast datagrams.
1366 * Either an instance of struct in_addr or an instance of struct ip_mreqn
1367 * may be passed to this socket option. An address of INADDR_ANY or an
1368 * interface index of 0 is used to remove a previous selection.
1369 * When no interface is selected, one is chosen for every send.
1372 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
1374 struct in_addr addr;
1375 struct ip_mreqn mreqn;
1377 struct ip_moptions *imo;
1380 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1382 * An interface index was specified using the
1383 * Linux-derived ip_mreqn structure.
1385 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
1386 sizeof(struct ip_mreqn));
1390 if (mreqn.imr_ifindex < 0 || if_index < mreqn.imr_ifindex)
1393 if (mreqn.imr_ifindex == 0) {
1396 ifp = ifnet_byindex(mreqn.imr_ifindex);
1398 return (EADDRNOTAVAIL);
1402 * An interface was specified by IPv4 address.
1403 * This is the traditional BSD usage.
1405 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
1406 sizeof(struct in_addr));
1409 if (addr.s_addr == INADDR_ANY) {
1412 INADDR_TO_IFP(addr, ifp);
1414 return (EADDRNOTAVAIL);
1418 printf("%s: ifp = %p, addr = %s\n",
1419 __func__, ifp, inet_ntoa(addr)); /* XXX INET6 */
1424 /* Reject interfaces which do not support multicast. */
1425 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
1426 return (EOPNOTSUPP);
1428 imo = inp_findmoptions(inp);
1429 imo->imo_multicast_ifp = ifp;
1430 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1437 * Atomically set source filters on a socket for an IPv4 multicast group.
1440 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
1442 struct __msfilterreq msfr;
1445 struct in_mfilter *imf;
1446 struct ip_moptions *imo;
1447 struct in_msource *ims, *tims;
1451 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1452 sizeof(struct __msfilterreq));
1456 if (msfr.msfr_nsrcs > IP_MAX_SOURCE_FILTER ||
1457 (msfr.msfr_fmode != MCAST_EXCLUDE &&
1458 msfr.msfr_fmode != MCAST_INCLUDE))
1461 if (msfr.msfr_group.ss_family != AF_INET ||
1462 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
1465 gsa = (sockunion_t *)&msfr.msfr_group;
1466 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1469 gsa->sin.sin_port = 0; /* ignore port */
1471 if (msfr.msfr_ifindex == 0 || if_index < msfr.msfr_ifindex)
1472 return (EADDRNOTAVAIL);
1474 ifp = ifnet_byindex(msfr.msfr_ifindex);
1476 return (EADDRNOTAVAIL);
1479 * Take the INP lock.
1480 * Check if this socket is a member of this group.
1482 imo = inp_findmoptions(inp);
1483 idx = imo_match_group(imo, ifp, &gsa->sa);
1484 if (idx == -1 || imo->imo_mfilters == NULL) {
1485 error = EADDRNOTAVAIL;
1488 imf = &imo->imo_mfilters[idx];
1492 printf("%s: clearing source list\n", __func__);
1496 * Remove any existing source filters.
1498 TAILQ_FOREACH_SAFE(ims, &imf->imf_sources, ims_next, tims) {
1499 TAILQ_REMOVE(&imf->imf_sources, ims, ims_next);
1500 FREE(ims, M_IPMSOURCE);
1501 imf->imf_nsources--;
1503 KASSERT(imf->imf_nsources == 0,
1504 ("%s: source list not cleared", __func__));
1507 * Apply any new source filters, if present.
1509 if (msfr.msfr_nsrcs > 0) {
1510 struct in_msource **pnims;
1511 struct in_msource *nims;
1512 struct sockaddr_storage *kss;
1513 struct sockaddr_storage *pkss;
1518 * Drop the inp lock so we may sleep if we need to
1519 * in order to satisfy a malloc request.
1520 * We will re-take it before changing socket state.
1525 printf("%s: loading %lu source list entries\n",
1526 __func__, (unsigned long)msfr.msfr_nsrcs);
1530 * Make a copy of the user-space source vector so
1531 * that we may copy them with a single copyin. This
1532 * allows us to deal with page faults up-front.
1534 MALLOC(kss, struct sockaddr_storage *,
1535 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1537 error = copyin(msfr.msfr_srcs, kss,
1538 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1545 * Perform argument checking on every sockaddr_storage
1546 * structure in the vector provided to us. Overwrite
1547 * fields which should not apply to source entries.
1548 * TODO: Check for duplicate sources on this pass.
1550 psu = (sockunion_t *)kss;
1551 for (i = 0; i < msfr.msfr_nsrcs; i++, psu++) {
1552 switch (psu->ss.ss_family) {
1554 if (psu->sin.sin_len !=
1555 sizeof(struct sockaddr_in)) {
1558 psu->sin.sin_port = 0;
1563 if (psu->sin6.sin6_len !=
1564 sizeof(struct sockaddr_in6)) {
1567 psu->sin6.sin6_port = 0;
1568 psu->sin6.sin6_flowinfo = 0;
1573 error = EAFNOSUPPORT;
1585 * Allocate a block to track all the in_msource
1586 * entries we are about to allocate, in case we
1587 * abruptly need to free them.
1589 MALLOC(pnims, struct in_msource **,
1590 sizeof(struct in_msource *) * msfr.msfr_nsrcs,
1591 M_TEMP, M_WAITOK | M_ZERO);
1594 * Allocate up to nsrcs individual chunks.
1595 * If we encounter an error, backtrack out of
1596 * all allocations cleanly; updates must be atomic.
1600 for (i = 0; i < msfr.msfr_nsrcs; i++, pkss++) {
1601 MALLOC(nims, struct in_msource *,
1602 sizeof(struct in_msource) * msfr.msfr_nsrcs,
1603 M_IPMSOURCE, M_WAITOK | M_ZERO);
1606 if (i < msfr.msfr_nsrcs) {
1607 for (j = 0; j < i; j++) {
1608 if (pnims[j] != NULL)
1609 FREE(pnims[j], M_IPMSOURCE);
1611 FREE(pnims, M_TEMP);
1616 INP_UNLOCK_ASSERT(inp);
1619 * Finally, apply the filters to the socket.
1620 * Re-take the inp lock; we are changing socket state.
1624 for (i = 0; i < msfr.msfr_nsrcs; i++, pkss++) {
1625 memcpy(&(pnims[i]->ims_addr), pkss,
1626 sizeof(struct sockaddr_storage));
1627 TAILQ_INSERT_TAIL(&imf->imf_sources, pnims[i],
1629 imf->imf_nsources++;
1631 FREE(pnims, M_TEMP);
1636 * Update the filter mode on the socket before releasing the inpcb.
1638 INP_LOCK_ASSERT(inp);
1639 imf->imf_fmode = msfr.msfr_fmode;
1647 * Set the IP multicast options in response to user setsockopt().
1649 * Many of the socket options handled in this function duplicate the
1650 * functionality of socket options in the regular unicast API. However,
1651 * it is not possible to merge the duplicate code, because the idempotence
1652 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
1653 * the effects of these options must be treated as separate and distinct.
1656 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1658 struct ip_moptions *imo;
1663 switch (sopt->sopt_name) {
1664 case IP_MULTICAST_VIF: {
1667 * Select a multicast VIF for transmission.
1668 * Only useful if multicast forwarding is active.
1670 if (legal_vif_num == NULL) {
1674 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
1677 if (!legal_vif_num(vifi) && (vifi != -1)) {
1681 imo = inp_findmoptions(inp);
1682 imo->imo_multicast_vif = vifi;
1687 case IP_MULTICAST_IF:
1688 error = inp_set_multicast_if(inp, sopt);
1691 case IP_MULTICAST_TTL: {
1695 * Set the IP time-to-live for outgoing multicast packets.
1696 * The original multicast API required a char argument,
1697 * which is inconsistent with the rest of the socket API.
1698 * We allow either a char or an int.
1700 if (sopt->sopt_valsize == sizeof(u_char)) {
1701 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
1708 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
1718 imo = inp_findmoptions(inp);
1719 imo->imo_multicast_ttl = ttl;
1724 case IP_MULTICAST_LOOP: {
1728 * Set the loopback flag for outgoing multicast packets.
1729 * Must be zero or one. The original multicast API required a
1730 * char argument, which is inconsistent with the rest
1731 * of the socket API. We allow either a char or an int.
1733 if (sopt->sopt_valsize == sizeof(u_char)) {
1734 error = sooptcopyin(sopt, &loop, sizeof(u_char),
1741 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
1745 loop = (u_char)iloop;
1747 imo = inp_findmoptions(inp);
1748 imo->imo_multicast_loop = !!loop;
1753 case IP_ADD_MEMBERSHIP:
1754 case IP_ADD_SOURCE_MEMBERSHIP:
1755 case MCAST_JOIN_GROUP:
1756 case MCAST_JOIN_SOURCE_GROUP:
1757 error = inp_join_group(inp, sopt);
1760 case IP_DROP_MEMBERSHIP:
1761 case IP_DROP_SOURCE_MEMBERSHIP:
1762 case MCAST_LEAVE_GROUP:
1763 case MCAST_LEAVE_SOURCE_GROUP:
1764 error = inp_leave_group(inp, sopt);
1767 case IP_BLOCK_SOURCE:
1768 case IP_UNBLOCK_SOURCE:
1769 case MCAST_BLOCK_SOURCE:
1770 case MCAST_UNBLOCK_SOURCE:
1771 error = inp_change_source_filter(inp, sopt);
1775 error = inp_set_source_filters(inp, sopt);
1783 INP_UNLOCK_ASSERT(inp);