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/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/sysctl.h>
53 #include <net/if_dl.h>
54 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_pcb.h>
59 #include <netinet/in_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/igmp_var.h>
63 #ifndef __SOCKUNION_DECLARED
65 struct sockaddr_storage ss;
67 struct sockaddr_dl sdl;
68 struct sockaddr_in sin;
70 struct sockaddr_in6 sin6;
73 typedef union sockunion sockunion_t;
74 #define __SOCKUNION_DECLARED
75 #endif /* __SOCKUNION_DECLARED */
77 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
78 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
79 static MALLOC_DEFINE(M_IPMSOURCE, "in_msource", "IPv4 multicast source filter");
82 * The IPv4 multicast list (in_multihead and associated structures) are
83 * protected by the global in_multi_mtx. See in_var.h for more details. For
84 * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
85 * ip_output() to send IGMP packets while holding the lock; this probably is
86 * not quite desirable.
88 struct in_multihead in_multihead; /* XXX BSS initialization */
89 struct mtx in_multi_mtx;
90 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
93 * Functions with non-static linkage defined in this file should be
94 * declared in in_var.h:
99 * in_delmulti_locked()
105 static int imo_grow(struct ip_moptions *);
106 static int imo_join_source(struct ip_moptions *, size_t, sockunion_t *);
107 static int imo_leave_source(struct ip_moptions *, size_t, sockunion_t *);
108 static int inp_change_source_filter(struct inpcb *, struct sockopt *);
109 static struct ip_moptions *
110 inp_findmoptions(struct inpcb *);
111 static int inp_get_source_filters(struct inpcb *, struct sockopt *);
112 static int inp_join_group(struct inpcb *, struct sockopt *);
113 static int inp_leave_group(struct inpcb *, struct sockopt *);
114 static int inp_set_multicast_if(struct inpcb *, struct sockopt *);
115 static int inp_set_source_filters(struct inpcb *, struct sockopt *);
118 * Resize the ip_moptions vector to the next power-of-two minus 1.
119 * May be called with locks held; do not sleep.
122 imo_grow(struct ip_moptions *imo)
124 struct in_multi **nmships;
125 struct in_multi **omships;
126 struct in_mfilter *nmfilters;
127 struct in_mfilter *omfilters;
134 omships = imo->imo_membership;
135 omfilters = imo->imo_mfilters;
136 oldmax = imo->imo_max_memberships;
137 newmax = ((oldmax + 1) * 2) - 1;
139 if (newmax <= IP_MAX_MEMBERSHIPS) {
140 nmships = (struct in_multi **)realloc(omships,
141 sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
142 nmfilters = (struct in_mfilter *)realloc(omfilters,
143 sizeof(struct in_mfilter) * newmax, M_IPMSOURCE, M_NOWAIT);
144 if (nmships != NULL && nmfilters != NULL) {
145 /* Initialize newly allocated source filter heads. */
146 for (idx = oldmax; idx < newmax; idx++) {
147 nmfilters[idx].imf_fmode = MCAST_EXCLUDE;
148 nmfilters[idx].imf_nsources = 0;
149 TAILQ_INIT(&nmfilters[idx].imf_sources);
151 imo->imo_max_memberships = newmax;
152 imo->imo_membership = nmships;
153 imo->imo_mfilters = nmfilters;
157 if (nmships == NULL || nmfilters == NULL) {
159 free(nmships, M_IPMOPTS);
160 if (nmfilters != NULL)
161 free(nmfilters, M_IPMSOURCE);
162 return (ETOOMANYREFS);
169 * Add a source to a multicast filter list.
170 * Assumes the associated inpcb is locked.
173 imo_join_source(struct ip_moptions *imo, size_t gidx, sockunion_t *src)
175 struct in_msource *ims, *nims;
176 struct in_mfilter *imf;
178 KASSERT(src->ss.ss_family == AF_INET, ("%s: !AF_INET", __func__));
179 KASSERT(imo->imo_mfilters != NULL,
180 ("%s: imo_mfilters vector not allocated", __func__));
182 imf = &imo->imo_mfilters[gidx];
183 if (imf->imf_nsources == IP_MAX_SOURCE_FILTER)
186 ims = imo_match_source(imo, gidx, &src->sa);
188 return (EADDRNOTAVAIL);
190 /* Do not sleep with inp lock held. */
191 MALLOC(nims, struct in_msource *, sizeof(struct in_msource),
192 M_IPMSOURCE, M_NOWAIT | M_ZERO);
196 nims->ims_addr = src->ss;
197 TAILQ_INSERT_TAIL(&imf->imf_sources, nims, ims_next);
204 imo_leave_source(struct ip_moptions *imo, size_t gidx, sockunion_t *src)
206 struct in_msource *ims;
207 struct in_mfilter *imf;
209 KASSERT(src->ss.ss_family == AF_INET, ("%s: !AF_INET", __func__));
210 KASSERT(imo->imo_mfilters != NULL,
211 ("%s: imo_mfilters vector not allocated", __func__));
213 imf = &imo->imo_mfilters[gidx];
214 if (imf->imf_nsources == IP_MAX_SOURCE_FILTER)
217 ims = imo_match_source(imo, gidx, &src->sa);
219 return (EADDRNOTAVAIL);
221 TAILQ_REMOVE(&imf->imf_sources, ims, ims_next);
222 FREE(ims, M_IPMSOURCE);
229 * Find an IPv4 multicast group entry for this ip_moptions instance
230 * which matches the specified group, and optionally an interface.
231 * Return its index into the array, or -1 if not found.
234 imo_match_group(struct ip_moptions *imo, struct ifnet *ifp,
235 struct sockaddr *group)
238 struct in_multi **pinm;
242 gsa = (sockunion_t *)group;
244 /* The imo_membership array may be lazy allocated. */
245 if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
248 nmships = imo->imo_num_memberships;
249 pinm = &imo->imo_membership[0];
250 for (idx = 0; idx < nmships; idx++, pinm++) {
254 printf("%s: trying ifp = %p, inaddr = %s ", __func__,
255 ifp, inet_ntoa(gsa->sin.sin_addr));
256 printf("against %p, %s\n",
257 (*pinm)->inm_ifp, inet_ntoa((*pinm)->inm_addr));
259 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
260 (*pinm)->inm_addr.s_addr == gsa->sin.sin_addr.s_addr) {
271 * Find a multicast source entry for this imo which matches
272 * the given group index for this socket, and source address.
275 imo_match_source(struct ip_moptions *imo, size_t gidx, struct sockaddr *src)
277 struct in_mfilter *imf;
278 struct in_msource *ims, *pims;
280 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
281 KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
282 ("%s: invalid index %d\n", __func__, (int)gidx));
284 /* The imo_mfilters array may be lazy allocated. */
285 if (imo->imo_mfilters == NULL)
289 imf = &imo->imo_mfilters[gidx];
290 TAILQ_FOREACH(ims, &imf->imf_sources, ims_next) {
292 * Perform bitwise comparison of two IPv4 addresses.
293 * TODO: Do the same for IPv6.
294 * Do not use sa_equal() for this as it is not aware of
295 * deeper structure in sockaddr_in or sockaddr_in6.
297 if (((struct sockaddr_in *)&ims->ims_addr)->sin_addr.s_addr ==
298 ((struct sockaddr_in *)src)->sin_addr.s_addr) {
308 * Join an IPv4 multicast group.
311 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
313 struct in_multi *inm;
320 IN_LOOKUP_MULTI(*ap, ifp, inm);
323 * If we already joined this group, just bump the
324 * refcount and return it.
326 KASSERT(inm->inm_refcount >= 1,
327 ("%s: bad refcount %d", __func__, inm->inm_refcount));
331 struct ifmultiaddr *ifma;
332 struct in_multi *ninm;
335 memset(&gsa, 0, sizeof(gsa));
336 gsa.sin.sin_family = AF_INET;
337 gsa.sin.sin_len = sizeof(struct sockaddr_in);
338 gsa.sin.sin_addr = *ap;
341 * Check if a link-layer group is already associated
342 * with this network-layer group on the given ifnet.
343 * If so, bump the refcount on the existing network-layer
344 * group association and return it.
346 error = if_addmulti(ifp, &gsa.sa, &ifma);
349 if (ifma->ifma_protospec != NULL) {
350 inm = (struct in_multi *)ifma->ifma_protospec;
352 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
353 inm->inm_addr.s_addr != ap->s_addr)
354 panic("%s: ifma is inconsistent", __func__);
361 * A new membership is needed; construct it and
362 * perform the IGMP join.
364 ninm = malloc(sizeof(*ninm), M_IPMADDR, M_NOWAIT | M_ZERO);
366 if_delmulti_ifma(ifma);
369 ninm->inm_addr = *ap;
371 ninm->inm_ifma = ifma;
372 ninm->inm_refcount = 1;
373 ifma->ifma_protospec = ninm;
374 LIST_INSERT_HEAD(&in_multihead, ninm, inm_link);
376 igmp_joingroup(ninm);
382 IFF_UNLOCKGIANT(ifp);
388 * Leave an IPv4 multicast group.
389 * It is OK to call this routine if the underlying ifnet went away.
391 * XXX: To deal with the ifp going away, we cheat; the link-layer code in net
392 * will set ifma_ifp to NULL when the associated ifnet instance is detached
395 * The only reason we need to violate layers and check ifma_ifp here at all
396 * is because certain hardware drivers still require Giant to be held,
397 * and it must always be taken before other locks.
400 in_delmulti(struct in_multi *inm)
404 KASSERT(inm != NULL, ("%s: inm is NULL", __func__));
405 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
406 ifp = inm->inm_ifma->ifma_ifp;
410 * Sanity check that netinet's notion of ifp is the
413 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
418 in_delmulti_locked(inm);
422 IFF_UNLOCKGIANT(ifp);
426 * Delete a multicast address record, with locks held.
428 * It is OK to call this routine if the ifp went away.
429 * Assumes that caller holds the IN_MULTI lock, and that
430 * Giant was taken before other locks if required by the hardware.
433 in_delmulti_locked(struct in_multi *inm)
435 struct ifmultiaddr *ifma;
437 IN_MULTI_LOCK_ASSERT();
438 KASSERT(inm->inm_refcount >= 1, ("%s: freeing freed inm", __func__));
440 if (--inm->inm_refcount == 0) {
441 igmp_leavegroup(inm);
443 ifma = inm->inm_ifma;
446 printf("%s: purging ifma %p\n", __func__, ifma);
448 KASSERT(ifma->ifma_protospec == inm,
449 ("%s: ifma_protospec != inm", __func__));
450 ifma->ifma_protospec = NULL;
452 LIST_REMOVE(inm, inm_link);
453 free(inm, M_IPMADDR);
455 if_delmulti_ifma(ifma);
460 * Block or unblock an ASM/SSM multicast source on an inpcb.
463 inp_change_source_filter(struct inpcb *inp, struct sockopt *sopt)
465 struct group_source_req gsr;
466 sockunion_t *gsa, *ssa;
468 struct in_mfilter *imf;
469 struct ip_moptions *imo;
470 struct in_msource *ims;
479 memset(&gsr, 0, sizeof(struct group_source_req));
480 gsa = (sockunion_t *)&gsr.gsr_group;
481 ssa = (sockunion_t *)&gsr.gsr_source;
483 switch (sopt->sopt_name) {
484 case IP_BLOCK_SOURCE:
485 case IP_UNBLOCK_SOURCE: {
486 struct ip_mreq_source mreqs;
488 error = sooptcopyin(sopt, &mreqs,
489 sizeof(struct ip_mreq_source),
490 sizeof(struct ip_mreq_source));
494 gsa->sin.sin_family = AF_INET;
495 gsa->sin.sin_len = sizeof(struct sockaddr_in);
496 gsa->sin.sin_addr = mreqs.imr_multiaddr;
498 ssa->sin.sin_family = AF_INET;
499 ssa->sin.sin_len = sizeof(struct sockaddr_in);
500 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
502 if (mreqs.imr_interface.s_addr != INADDR_ANY)
503 INADDR_TO_IFP(mreqs.imr_interface, ifp);
505 if (sopt->sopt_name == IP_BLOCK_SOURCE)
510 printf("%s: imr_interface = %s, ifp = %p\n",
511 __func__, inet_ntoa(mreqs.imr_interface), ifp);
517 case MCAST_BLOCK_SOURCE:
518 case MCAST_UNBLOCK_SOURCE:
519 error = sooptcopyin(sopt, &gsr,
520 sizeof(struct group_source_req),
521 sizeof(struct group_source_req));
525 if (gsa->sin.sin_family != AF_INET ||
526 gsa->sin.sin_len != sizeof(struct sockaddr_in))
529 if (ssa->sin.sin_family != AF_INET ||
530 ssa->sin.sin_len != sizeof(struct sockaddr_in))
533 if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface)
534 return (EADDRNOTAVAIL);
536 ifp = ifnet_byindex(gsr.gsr_interface);
538 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
545 printf("%s: unknown sopt_name %d\n", __func__,
554 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
558 * Check if we are actually a member of this group.
560 imo = inp_findmoptions(inp);
561 idx = imo_match_group(imo, ifp, &gsa->sa);
562 if (idx == -1 || imo->imo_mfilters == NULL) {
563 error = EADDRNOTAVAIL;
567 KASSERT(imo->imo_mfilters != NULL,
568 ("%s: imo_mfilters not allocated", __func__));
569 imf = &imo->imo_mfilters[idx];
572 * SSM multicast truth table for block/unblock operations.
574 * Operation Filter Mode Entry exists? Action
576 * block exclude no add source to filter
577 * unblock include no add source to filter
578 * block include no EINVAL
579 * unblock exclude no EINVAL
580 * block exclude yes EADDRNOTAVAIL
581 * unblock include yes EADDRNOTAVAIL
582 * block include yes remove source from filter
583 * unblock exclude yes remove source from filter
585 * FreeBSD does not explicitly distinguish between ASM and SSM
586 * mode sockets; all sockets are assumed to have a filter list.
590 printf("%s: imf_fmode is %s\n", __func__,
591 imf->imf_fmode == MCAST_INCLUDE ? "include" : "exclude");
594 ims = imo_match_source(imo, idx, &ssa->sa);
596 if ((block == 1 && imf->imf_fmode == MCAST_EXCLUDE) ||
597 (block == 0 && imf->imf_fmode == MCAST_INCLUDE)) {
600 printf("%s: adding %s to filter list\n",
601 __func__, inet_ntoa(ssa->sin.sin_addr));
604 error = imo_join_source(imo, idx, ssa);
606 if ((block == 1 && imf->imf_fmode == MCAST_INCLUDE) ||
607 (block == 0 && imf->imf_fmode == MCAST_EXCLUDE)) {
609 * If the socket is in inclusive mode:
610 * the source is already blocked as it has no entry.
611 * If the socket is in exclusive mode:
612 * the source is already unblocked as it has no entry.
616 printf("%s: ims %p; %s already [un]blocked\n",
618 inet_ntoa(ssa->sin.sin_addr));
624 if ((block == 1 && imf->imf_fmode == MCAST_EXCLUDE) ||
625 (block == 0 && imf->imf_fmode == MCAST_INCLUDE)) {
627 * If the socket is in exclusive mode:
628 * the source is already blocked as it has an entry.
629 * If the socket is in inclusive mode:
630 * the source is already unblocked as it has an entry.
634 printf("%s: ims %p; %s already [un]blocked\n",
636 inet_ntoa(ssa->sin.sin_addr));
639 error = EADDRNOTAVAIL;
641 if ((block == 1 && imf->imf_fmode == MCAST_INCLUDE) ||
642 (block == 0 && imf->imf_fmode == MCAST_EXCLUDE)) {
645 printf("%s: removing %s from filter list\n",
646 __func__, inet_ntoa(ssa->sin.sin_addr));
649 error = imo_leave_source(imo, idx, ssa);
659 * Given an inpcb, return its multicast options structure pointer. Accepts
660 * an unlocked inpcb pointer, but will return it locked. May sleep.
662 static struct ip_moptions *
663 inp_findmoptions(struct inpcb *inp)
665 struct ip_moptions *imo;
666 struct in_multi **immp;
667 struct in_mfilter *imfp;
671 if (inp->inp_moptions != NULL)
672 return (inp->inp_moptions);
676 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS,
678 immp = (struct in_multi **)malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS,
679 M_IPMOPTS, M_WAITOK | M_ZERO);
680 imfp = (struct in_mfilter *)malloc(
681 sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
682 M_IPMSOURCE, M_WAITOK);
684 imo->imo_multicast_ifp = NULL;
685 imo->imo_multicast_addr.s_addr = INADDR_ANY;
686 imo->imo_multicast_vif = -1;
687 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
688 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP;
689 imo->imo_num_memberships = 0;
690 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
691 imo->imo_membership = immp;
693 /* Initialize per-group source filters. */
694 for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++) {
695 imfp[idx].imf_fmode = MCAST_EXCLUDE;
696 imfp[idx].imf_nsources = 0;
697 TAILQ_INIT(&imfp[idx].imf_sources);
699 imo->imo_mfilters = imfp;
702 if (inp->inp_moptions != NULL) {
703 free(imfp, M_IPMSOURCE);
704 free(immp, M_IPMOPTS);
705 free(imo, M_IPMOPTS);
706 return (inp->inp_moptions);
708 inp->inp_moptions = imo;
713 * Discard the IP multicast options (and source filters).
716 inp_freemoptions(struct ip_moptions *imo)
718 struct in_mfilter *imf;
719 struct in_msource *ims, *tims;
722 KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__));
724 nmships = imo->imo_num_memberships;
725 for (idx = 0; idx < nmships; ++idx) {
726 in_delmulti(imo->imo_membership[idx]);
728 if (imo->imo_mfilters != NULL) {
729 imf = &imo->imo_mfilters[idx];
730 TAILQ_FOREACH_SAFE(ims, &imf->imf_sources,
732 TAILQ_REMOVE(&imf->imf_sources, ims, ims_next);
733 FREE(ims, M_IPMSOURCE);
736 KASSERT(imf->imf_nsources == 0,
737 ("%s: did not free all imf_nsources", __func__));
741 if (imo->imo_mfilters != NULL)
742 free(imo->imo_mfilters, M_IPMSOURCE);
743 free(imo->imo_membership, M_IPMOPTS);
744 free(imo, M_IPMOPTS);
748 * Atomically get source filters on a socket for an IPv4 multicast group.
749 * Called with INP lock held; returns with lock released.
752 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
754 struct __msfilterreq msfr;
757 struct ip_moptions *imo;
758 struct in_mfilter *imf;
759 struct in_msource *ims;
760 struct sockaddr_storage *ptss;
761 struct sockaddr_storage *tss;
765 INP_WLOCK_ASSERT(inp);
767 imo = inp->inp_moptions;
768 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
772 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
773 sizeof(struct __msfilterreq));
777 if (msfr.msfr_ifindex == 0 || if_index < msfr.msfr_ifindex)
780 ifp = ifnet_byindex(msfr.msfr_ifindex);
787 * Lookup group on the socket.
789 gsa = (sockunion_t *)&msfr.msfr_group;
790 idx = imo_match_group(imo, ifp, &gsa->sa);
791 if (idx == -1 || imo->imo_mfilters == NULL) {
793 return (EADDRNOTAVAIL);
796 imf = &imo->imo_mfilters[idx];
797 msfr.msfr_fmode = imf->imf_fmode;
798 msfr.msfr_nsrcs = imf->imf_nsources;
801 * If the user specified a buffer, copy out the source filter
802 * entries to userland gracefully.
803 * msfr.msfr_nsrcs is always set to the total number of filter
804 * entries which the kernel currently has for this group.
807 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
809 * Make a copy of the source vector so that we do not
810 * thrash the inpcb lock whilst copying it out.
811 * We only copy out the number of entries which userland
812 * has asked for, but we always tell userland how big the
813 * buffer really needs to be.
815 MALLOC(tss, struct sockaddr_storage *,
816 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
822 TAILQ_FOREACH(ims, &imf->imf_sources, ims_next) {
823 memcpy(ptss++, &ims->ims_addr,
824 sizeof(struct sockaddr_storage));
832 error = copyout(tss, msfr.msfr_srcs,
833 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
840 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
846 * Return the IP multicast options in response to user getsockopt().
849 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
851 struct ip_mreqn mreqn;
852 struct ip_moptions *imo;
854 struct in_ifaddr *ia;
859 imo = inp->inp_moptions;
861 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
862 * or is a divert socket, reject it.
864 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
865 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
866 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
872 switch (sopt->sopt_name) {
873 case IP_MULTICAST_VIF:
875 optval = imo->imo_multicast_vif;
879 error = sooptcopyout(sopt, &optval, sizeof(int));
882 case IP_MULTICAST_IF:
883 memset(&mreqn, 0, sizeof(struct ip_mreqn));
885 ifp = imo->imo_multicast_ifp;
886 if (imo->imo_multicast_addr.s_addr != INADDR_ANY) {
887 mreqn.imr_address = imo->imo_multicast_addr;
888 } else if (ifp != NULL) {
889 mreqn.imr_ifindex = ifp->if_index;
893 IA_SIN(ia)->sin_addr;
898 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
899 error = sooptcopyout(sopt, &mreqn,
900 sizeof(struct ip_mreqn));
902 error = sooptcopyout(sopt, &mreqn.imr_address,
903 sizeof(struct in_addr));
907 case IP_MULTICAST_TTL:
909 optval = coptval = IP_DEFAULT_MULTICAST_TTL;
911 optval = coptval = imo->imo_multicast_ttl;
913 if (sopt->sopt_valsize == sizeof(u_char))
914 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
916 error = sooptcopyout(sopt, &optval, sizeof(int));
919 case IP_MULTICAST_LOOP:
921 optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
923 optval = coptval = imo->imo_multicast_loop;
925 if (sopt->sopt_valsize == sizeof(u_char))
926 error = sooptcopyout(sopt, &coptval, sizeof(u_char));
928 error = sooptcopyout(sopt, &optval, sizeof(int));
933 error = EADDRNOTAVAIL;
936 error = inp_get_source_filters(inp, sopt);
946 INP_UNLOCK_ASSERT(inp);
952 * Join an IPv4 multicast group, possibly with a source.
955 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
957 struct group_source_req gsr;
958 sockunion_t *gsa, *ssa;
960 struct in_mfilter *imf;
961 struct ip_moptions *imo;
962 struct in_multi *inm;
969 memset(&gsr, 0, sizeof(struct group_source_req));
970 gsa = (sockunion_t *)&gsr.gsr_group;
971 gsa->ss.ss_family = AF_UNSPEC;
972 ssa = (sockunion_t *)&gsr.gsr_source;
973 ssa->ss.ss_family = AF_UNSPEC;
975 switch (sopt->sopt_name) {
976 case IP_ADD_MEMBERSHIP:
977 case IP_ADD_SOURCE_MEMBERSHIP: {
978 struct ip_mreq_source mreqs;
980 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
981 error = sooptcopyin(sopt, &mreqs,
982 sizeof(struct ip_mreq),
983 sizeof(struct ip_mreq));
985 * Do argument switcharoo from ip_mreq into
986 * ip_mreq_source to avoid using two instances.
988 mreqs.imr_interface = mreqs.imr_sourceaddr;
989 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
990 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
991 error = sooptcopyin(sopt, &mreqs,
992 sizeof(struct ip_mreq_source),
993 sizeof(struct ip_mreq_source));
998 gsa->sin.sin_family = AF_INET;
999 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1000 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1002 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
1003 ssa->sin.sin_family = AF_INET;
1004 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1005 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1009 * Obtain ifp. If no interface address was provided,
1010 * use the interface of the route in the unicast FIB for
1011 * the given multicast destination; usually, this is the
1013 * If this lookup fails, attempt to use the first non-loopback
1014 * interface with multicast capability in the system as a
1015 * last resort. The legacy IPv4 ASM API requires that we do
1016 * this in order to allow groups to be joined when the routing
1017 * table has not yet been populated during boot.
1018 * If all of these conditions fail, return EADDRNOTAVAIL, and
1019 * reject the IPv4 multicast join.
1021 if (mreqs.imr_interface.s_addr != INADDR_ANY) {
1022 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1027 *(struct sockaddr_in *)&ro.ro_dst = gsa->sin;
1028 rtalloc_ign(&ro, RTF_CLONING);
1029 if (ro.ro_rt != NULL) {
1030 ifp = ro.ro_rt->rt_ifp;
1031 KASSERT(ifp != NULL, ("%s: null ifp",
1035 struct in_ifaddr *ia;
1036 struct ifnet *mfp = NULL;
1037 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
1039 if (!(mfp->if_flags & IFF_LOOPBACK) &&
1040 (mfp->if_flags & IFF_MULTICAST)) {
1049 printf("%s: imr_interface = %s, ifp = %p\n",
1050 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1056 case MCAST_JOIN_GROUP:
1057 case MCAST_JOIN_SOURCE_GROUP:
1058 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
1059 error = sooptcopyin(sopt, &gsr,
1060 sizeof(struct group_req),
1061 sizeof(struct group_req));
1062 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1063 error = sooptcopyin(sopt, &gsr,
1064 sizeof(struct group_source_req),
1065 sizeof(struct group_source_req));
1070 if (gsa->sin.sin_family != AF_INET ||
1071 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1075 * Overwrite the port field if present, as the sockaddr
1076 * being copied in may be matched with a binary comparison.
1079 gsa->sin.sin_port = 0;
1080 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
1081 if (ssa->sin.sin_family != AF_INET ||
1082 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1084 ssa->sin.sin_port = 0;
1090 if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface)
1091 return (EADDRNOTAVAIL);
1092 ifp = ifnet_byindex(gsr.gsr_interface);
1099 printf("%s: unknown sopt_name %d\n", __func__,
1103 return (EOPNOTSUPP);
1107 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1110 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
1111 return (EADDRNOTAVAIL);
1114 * Check if we already hold membership of this group for this inpcb.
1115 * If so, we do not need to perform the initial join.
1117 imo = inp_findmoptions(inp);
1118 idx = imo_match_group(imo, ifp, &gsa->sa);
1120 if (ssa->ss.ss_family != AF_UNSPEC) {
1122 * Attempting to join an ASM group (when already
1123 * an ASM or SSM member) is an error.
1125 error = EADDRNOTAVAIL;
1127 imf = &imo->imo_mfilters[idx];
1128 if (imf->imf_nsources == 0) {
1130 * Attempting to join an SSM group (when
1131 * already an ASM member) is an error.
1136 * Attempting to join an SSM group (when
1137 * already an SSM member) means "add this
1138 * source to the inclusive filter list".
1140 error = imo_join_source(imo, idx, ssa);
1147 * Call imo_grow() to reallocate the membership and source filter
1148 * vectors if they are full. If the size would exceed the hard limit,
1149 * then we know we've really run out of entries. We keep the INP
1150 * lock held to avoid introducing a race condition.
1152 if (imo->imo_num_memberships == imo->imo_max_memberships) {
1153 error = imo_grow(imo);
1159 * So far, so good: perform the layer 3 join, layer 2 join,
1160 * and make an IGMP announcement if needed.
1162 inm = in_addmulti(&gsa->sin.sin_addr, ifp);
1167 idx = imo->imo_num_memberships;
1168 imo->imo_membership[idx] = inm;
1169 imo->imo_num_memberships++;
1171 KASSERT(imo->imo_mfilters != NULL,
1172 ("%s: imf_mfilters vector was not allocated", __func__));
1173 imf = &imo->imo_mfilters[idx];
1174 KASSERT(TAILQ_EMPTY(&imf->imf_sources),
1175 ("%s: imf_sources not empty", __func__));
1178 * If this is a new SSM group join (i.e. a source was specified
1179 * with this group), add this source to the filter list.
1181 if (ssa->ss.ss_family != AF_UNSPEC) {
1183 * An initial SSM join implies that this socket's membership
1184 * of the multicast group is now in inclusive mode.
1186 imf->imf_fmode = MCAST_INCLUDE;
1188 error = imo_join_source(imo, idx, ssa);
1191 * Drop inp lock before calling in_delmulti(),
1192 * to prevent a lock order reversal.
1194 --imo->imo_num_memberships;
1207 * Leave an IPv4 multicast group on an inpcb, possibly with a source.
1210 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
1212 struct group_source_req gsr;
1213 struct ip_mreq_source mreqs;
1214 sockunion_t *gsa, *ssa;
1216 struct in_mfilter *imf;
1217 struct ip_moptions *imo;
1218 struct in_msource *ims, *tims;
1219 struct in_multi *inm;
1226 memset(&gsr, 0, sizeof(struct group_source_req));
1227 gsa = (sockunion_t *)&gsr.gsr_group;
1228 gsa->ss.ss_family = AF_UNSPEC;
1229 ssa = (sockunion_t *)&gsr.gsr_source;
1230 ssa->ss.ss_family = AF_UNSPEC;
1232 switch (sopt->sopt_name) {
1233 case IP_DROP_MEMBERSHIP:
1234 case IP_DROP_SOURCE_MEMBERSHIP:
1235 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
1236 error = sooptcopyin(sopt, &mreqs,
1237 sizeof(struct ip_mreq),
1238 sizeof(struct ip_mreq));
1240 * Swap interface and sourceaddr arguments,
1241 * as ip_mreq and ip_mreq_source are laid
1244 mreqs.imr_interface = mreqs.imr_sourceaddr;
1245 mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
1246 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
1247 error = sooptcopyin(sopt, &mreqs,
1248 sizeof(struct ip_mreq_source),
1249 sizeof(struct ip_mreq_source));
1254 gsa->sin.sin_family = AF_INET;
1255 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1256 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1258 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
1259 ssa->sin.sin_family = AF_INET;
1260 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1261 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1264 if (gsa->sin.sin_addr.s_addr != INADDR_ANY)
1265 INADDR_TO_IFP(mreqs.imr_interface, ifp);
1269 printf("%s: imr_interface = %s, ifp = %p\n",
1270 __func__, inet_ntoa(mreqs.imr_interface), ifp);
1275 case MCAST_LEAVE_GROUP:
1276 case MCAST_LEAVE_SOURCE_GROUP:
1277 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
1278 error = sooptcopyin(sopt, &gsr,
1279 sizeof(struct group_req),
1280 sizeof(struct group_req));
1281 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
1282 error = sooptcopyin(sopt, &gsr,
1283 sizeof(struct group_source_req),
1284 sizeof(struct group_source_req));
1289 if (gsa->sin.sin_family != AF_INET ||
1290 gsa->sin.sin_len != sizeof(struct sockaddr_in))
1293 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
1294 if (ssa->sin.sin_family != AF_INET ||
1295 ssa->sin.sin_len != sizeof(struct sockaddr_in))
1299 if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface)
1300 return (EADDRNOTAVAIL);
1302 ifp = ifnet_byindex(gsr.gsr_interface);
1308 printf("%s: unknown sopt_name %d\n", __func__,
1312 return (EOPNOTSUPP);
1316 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1320 * Find the membership in the membership array.
1322 imo = inp_findmoptions(inp);
1323 idx = imo_match_group(imo, ifp, &gsa->sa);
1325 error = EADDRNOTAVAIL;
1328 imf = &imo->imo_mfilters[idx];
1331 * If we were instructed only to leave a given source, do so.
1333 if (ssa->ss.ss_family != AF_UNSPEC) {
1334 if (imf->imf_nsources == 0 ||
1335 imf->imf_fmode == MCAST_EXCLUDE) {
1337 * Attempting to SSM leave an ASM group
1338 * is an error; should use *_BLOCK_SOURCE instead.
1339 * Attempting to SSM leave a source in a group when
1340 * the socket is in 'exclude mode' is also an error.
1344 error = imo_leave_source(imo, idx, ssa);
1347 * If an error occurred, or this source is not the last
1348 * source in the group, do not leave the whole group.
1350 if (error || imf->imf_nsources > 0)
1355 * Give up the multicast address record to which the membership points.
1357 inm = imo->imo_membership[idx];
1361 * Free any source filters for this group if they exist.
1362 * Revert inpcb to the default MCAST_EXCLUDE state.
1364 if (imo->imo_mfilters != NULL) {
1365 TAILQ_FOREACH_SAFE(ims, &imf->imf_sources, ims_next, tims) {
1366 TAILQ_REMOVE(&imf->imf_sources, ims, ims_next);
1367 FREE(ims, M_IPMSOURCE);
1368 imf->imf_nsources--;
1370 KASSERT(imf->imf_nsources == 0,
1371 ("%s: imf_nsources not 0", __func__));
1372 KASSERT(TAILQ_EMPTY(&imf->imf_sources),
1373 ("%s: imf_sources not empty", __func__));
1374 imf->imf_fmode = MCAST_EXCLUDE;
1378 * Remove the gap in the membership array.
1380 for (++idx; idx < imo->imo_num_memberships; ++idx)
1381 imo->imo_membership[idx-1] = imo->imo_membership[idx];
1382 imo->imo_num_memberships--;
1390 * Select the interface for transmitting IPv4 multicast datagrams.
1392 * Either an instance of struct in_addr or an instance of struct ip_mreqn
1393 * may be passed to this socket option. An address of INADDR_ANY or an
1394 * interface index of 0 is used to remove a previous selection.
1395 * When no interface is selected, one is chosen for every send.
1398 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
1400 struct in_addr addr;
1401 struct ip_mreqn mreqn;
1403 struct ip_moptions *imo;
1406 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1408 * An interface index was specified using the
1409 * Linux-derived ip_mreqn structure.
1411 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
1412 sizeof(struct ip_mreqn));
1416 if (mreqn.imr_ifindex < 0 || if_index < mreqn.imr_ifindex)
1419 if (mreqn.imr_ifindex == 0) {
1422 ifp = ifnet_byindex(mreqn.imr_ifindex);
1424 return (EADDRNOTAVAIL);
1428 * An interface was specified by IPv4 address.
1429 * This is the traditional BSD usage.
1431 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
1432 sizeof(struct in_addr));
1435 if (addr.s_addr == INADDR_ANY) {
1438 INADDR_TO_IFP(addr, ifp);
1440 return (EADDRNOTAVAIL);
1444 printf("%s: ifp = %p, addr = %s\n",
1445 __func__, ifp, inet_ntoa(addr)); /* XXX INET6 */
1450 /* Reject interfaces which do not support multicast. */
1451 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
1452 return (EOPNOTSUPP);
1454 imo = inp_findmoptions(inp);
1455 imo->imo_multicast_ifp = ifp;
1456 imo->imo_multicast_addr.s_addr = INADDR_ANY;
1463 * Atomically set source filters on a socket for an IPv4 multicast group.
1466 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
1468 struct __msfilterreq msfr;
1471 struct in_mfilter *imf;
1472 struct ip_moptions *imo;
1473 struct in_msource *ims, *tims;
1477 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1478 sizeof(struct __msfilterreq));
1482 if (msfr.msfr_nsrcs > IP_MAX_SOURCE_FILTER ||
1483 (msfr.msfr_fmode != MCAST_EXCLUDE &&
1484 msfr.msfr_fmode != MCAST_INCLUDE))
1487 if (msfr.msfr_group.ss_family != AF_INET ||
1488 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
1491 gsa = (sockunion_t *)&msfr.msfr_group;
1492 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1495 gsa->sin.sin_port = 0; /* ignore port */
1497 if (msfr.msfr_ifindex == 0 || if_index < msfr.msfr_ifindex)
1498 return (EADDRNOTAVAIL);
1500 ifp = ifnet_byindex(msfr.msfr_ifindex);
1502 return (EADDRNOTAVAIL);
1505 * Take the INP lock.
1506 * Check if this socket is a member of this group.
1508 imo = inp_findmoptions(inp);
1509 idx = imo_match_group(imo, ifp, &gsa->sa);
1510 if (idx == -1 || imo->imo_mfilters == NULL) {
1511 error = EADDRNOTAVAIL;
1514 imf = &imo->imo_mfilters[idx];
1518 printf("%s: clearing source list\n", __func__);
1522 * Remove any existing source filters.
1524 TAILQ_FOREACH_SAFE(ims, &imf->imf_sources, ims_next, tims) {
1525 TAILQ_REMOVE(&imf->imf_sources, ims, ims_next);
1526 FREE(ims, M_IPMSOURCE);
1527 imf->imf_nsources--;
1529 KASSERT(imf->imf_nsources == 0,
1530 ("%s: source list not cleared", __func__));
1533 * Apply any new source filters, if present.
1535 if (msfr.msfr_nsrcs > 0) {
1536 struct in_msource **pnims;
1537 struct in_msource *nims;
1538 struct sockaddr_storage *kss;
1539 struct sockaddr_storage *pkss;
1544 * Drop the inp lock so we may sleep if we need to
1545 * in order to satisfy a malloc request.
1546 * We will re-take it before changing socket state.
1551 printf("%s: loading %lu source list entries\n",
1552 __func__, (unsigned long)msfr.msfr_nsrcs);
1556 * Make a copy of the user-space source vector so
1557 * that we may copy them with a single copyin. This
1558 * allows us to deal with page faults up-front.
1560 MALLOC(kss, struct sockaddr_storage *,
1561 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1563 error = copyin(msfr.msfr_srcs, kss,
1564 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1571 * Perform argument checking on every sockaddr_storage
1572 * structure in the vector provided to us. Overwrite
1573 * fields which should not apply to source entries.
1574 * TODO: Check for duplicate sources on this pass.
1576 psu = (sockunion_t *)kss;
1577 for (i = 0; i < msfr.msfr_nsrcs; i++, psu++) {
1578 switch (psu->ss.ss_family) {
1580 if (psu->sin.sin_len !=
1581 sizeof(struct sockaddr_in)) {
1584 psu->sin.sin_port = 0;
1589 if (psu->sin6.sin6_len !=
1590 sizeof(struct sockaddr_in6)) {
1593 psu->sin6.sin6_port = 0;
1594 psu->sin6.sin6_flowinfo = 0;
1599 error = EAFNOSUPPORT;
1611 * Allocate a block to track all the in_msource
1612 * entries we are about to allocate, in case we
1613 * abruptly need to free them.
1615 MALLOC(pnims, struct in_msource **,
1616 sizeof(struct in_msource *) * msfr.msfr_nsrcs,
1617 M_TEMP, M_WAITOK | M_ZERO);
1620 * Allocate up to nsrcs individual chunks.
1621 * If we encounter an error, backtrack out of
1622 * all allocations cleanly; updates must be atomic.
1626 for (i = 0; i < msfr.msfr_nsrcs; i++, pkss++) {
1627 MALLOC(nims, struct in_msource *,
1628 sizeof(struct in_msource) * msfr.msfr_nsrcs,
1629 M_IPMSOURCE, M_WAITOK | M_ZERO);
1632 if (i < msfr.msfr_nsrcs) {
1633 for (j = 0; j < i; j++) {
1634 if (pnims[j] != NULL)
1635 FREE(pnims[j], M_IPMSOURCE);
1637 FREE(pnims, M_TEMP);
1642 INP_UNLOCK_ASSERT(inp);
1645 * Finally, apply the filters to the socket.
1646 * Re-take the inp lock; we are changing socket state.
1650 for (i = 0; i < msfr.msfr_nsrcs; i++, pkss++) {
1651 memcpy(&(pnims[i]->ims_addr), pkss,
1652 sizeof(struct sockaddr_storage));
1653 TAILQ_INSERT_TAIL(&imf->imf_sources, pnims[i],
1655 imf->imf_nsources++;
1657 FREE(pnims, M_TEMP);
1662 * Update the filter mode on the socket before releasing the inpcb.
1664 INP_WLOCK_ASSERT(inp);
1665 imf->imf_fmode = msfr.msfr_fmode;
1673 * Set the IP multicast options in response to user setsockopt().
1675 * Many of the socket options handled in this function duplicate the
1676 * functionality of socket options in the regular unicast API. However,
1677 * it is not possible to merge the duplicate code, because the idempotence
1678 * of the IPv4 multicast part of the BSD Sockets API must be preserved;
1679 * the effects of these options must be treated as separate and distinct.
1682 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
1684 struct ip_moptions *imo;
1690 * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1691 * or is a divert socket, reject it.
1692 * XXX Unlocked read of inp_socket believed OK.
1694 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1695 (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1696 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
1697 return (EOPNOTSUPP);
1699 switch (sopt->sopt_name) {
1700 case IP_MULTICAST_VIF: {
1703 * Select a multicast VIF for transmission.
1704 * Only useful if multicast forwarding is active.
1706 if (legal_vif_num == NULL) {
1710 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
1713 if (!legal_vif_num(vifi) && (vifi != -1)) {
1717 imo = inp_findmoptions(inp);
1718 imo->imo_multicast_vif = vifi;
1723 case IP_MULTICAST_IF:
1724 error = inp_set_multicast_if(inp, sopt);
1727 case IP_MULTICAST_TTL: {
1731 * Set the IP time-to-live for outgoing multicast packets.
1732 * The original multicast API required a char argument,
1733 * which is inconsistent with the rest of the socket API.
1734 * We allow either a char or an int.
1736 if (sopt->sopt_valsize == sizeof(u_char)) {
1737 error = sooptcopyin(sopt, &ttl, sizeof(u_char),
1744 error = sooptcopyin(sopt, &ittl, sizeof(u_int),
1754 imo = inp_findmoptions(inp);
1755 imo->imo_multicast_ttl = ttl;
1760 case IP_MULTICAST_LOOP: {
1764 * Set the loopback flag for outgoing multicast packets.
1765 * Must be zero or one. The original multicast API required a
1766 * char argument, which is inconsistent with the rest
1767 * of the socket API. We allow either a char or an int.
1769 if (sopt->sopt_valsize == sizeof(u_char)) {
1770 error = sooptcopyin(sopt, &loop, sizeof(u_char),
1777 error = sooptcopyin(sopt, &iloop, sizeof(u_int),
1781 loop = (u_char)iloop;
1783 imo = inp_findmoptions(inp);
1784 imo->imo_multicast_loop = !!loop;
1789 case IP_ADD_MEMBERSHIP:
1790 case IP_ADD_SOURCE_MEMBERSHIP:
1791 case MCAST_JOIN_GROUP:
1792 case MCAST_JOIN_SOURCE_GROUP:
1793 error = inp_join_group(inp, sopt);
1796 case IP_DROP_MEMBERSHIP:
1797 case IP_DROP_SOURCE_MEMBERSHIP:
1798 case MCAST_LEAVE_GROUP:
1799 case MCAST_LEAVE_SOURCE_GROUP:
1800 error = inp_leave_group(inp, sopt);
1803 case IP_BLOCK_SOURCE:
1804 case IP_UNBLOCK_SOURCE:
1805 case MCAST_BLOCK_SOURCE:
1806 case MCAST_UNBLOCK_SOURCE:
1807 error = inp_change_source_filter(inp, sopt);
1811 error = inp_set_source_filters(inp, sopt);
1819 INP_UNLOCK_ASSERT(inp);