in_mcast.c revision 171746
1170613Sbms/*- 2170613Sbms * Copyright (c) 2007 Bruce M. Simpson. 3170613Sbms * Copyright (c) 2005 Robert N. M. Watson. 4170613Sbms * All rights reserved. 5170613Sbms * 6170613Sbms * Redistribution and use in source and binary forms, with or without 7170613Sbms * modification, are permitted provided that the following conditions 8170613Sbms * are met: 9170613Sbms * 1. Redistributions of source code must retain the above copyright 10170613Sbms * notice, this list of conditions and the following disclaimer. 11170613Sbms * 2. Redistributions in binary form must reproduce the above copyright 12170613Sbms * notice, this list of conditions and the following disclaimer in the 13170613Sbms * documentation and/or other materials provided with the distribution. 14170613Sbms * 3. The name of the author may not be used to endorse or promote 15170613Sbms * products derived from this software without specific prior written 16170613Sbms * permission. 17170613Sbms * 18170613Sbms * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19170613Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20170613Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21170613Sbms * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22170613Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23170613Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24170613Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25170613Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26170613Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27170613Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28170613Sbms * SUCH DAMAGE. 29170613Sbms */ 30170613Sbms 31170613Sbms/* 32170613Sbms * IPv4 multicast socket, group, and socket option processing module. 33170613Sbms * Until further notice, this file requires INET to compile. 34170613Sbms * TODO: Make this infrastructure independent of address family. 35170613Sbms * TODO: Teach netinet6 to use this code. 36170613Sbms * TODO: Hook up SSM logic to IGMPv3/MLDv2. 37170613Sbms */ 38170613Sbms 39170613Sbms#include <sys/cdefs.h> 40170613Sbms__FBSDID("$FreeBSD: head/sys/netinet/in_mcast.c 171746 2007-08-06 22:06:36Z csjp $"); 41170613Sbms 42170613Sbms#include <sys/param.h> 43170613Sbms#include <sys/systm.h> 44170613Sbms#include <sys/kernel.h> 45170613Sbms#include <sys/malloc.h> 46170613Sbms#include <sys/mbuf.h> 47171746Scsjp#include <sys/protosw.h> 48170613Sbms#include <sys/socket.h> 49170613Sbms#include <sys/socketvar.h> 50170613Sbms#include <sys/sysctl.h> 51170613Sbms 52170613Sbms#include <net/if.h> 53170613Sbms#include <net/if_dl.h> 54170613Sbms#include <net/route.h> 55170613Sbms 56170613Sbms#include <netinet/in.h> 57170613Sbms#include <netinet/in_systm.h> 58170613Sbms#include <netinet/in_pcb.h> 59170613Sbms#include <netinet/in_var.h> 60170613Sbms#include <netinet/ip_var.h> 61170613Sbms#include <netinet/igmp_var.h> 62170613Sbms 63170613Sbms#ifndef __SOCKUNION_DECLARED 64170613Sbmsunion sockunion { 65170613Sbms struct sockaddr_storage ss; 66170613Sbms struct sockaddr sa; 67170613Sbms struct sockaddr_dl sdl; 68170613Sbms struct sockaddr_in sin; 69170613Sbms#ifdef INET6 70170613Sbms struct sockaddr_in6 sin6; 71170613Sbms#endif 72170613Sbms}; 73170613Sbmstypedef union sockunion sockunion_t; 74170613Sbms#define __SOCKUNION_DECLARED 75170613Sbms#endif /* __SOCKUNION_DECLARED */ 76170613Sbms 77170613Sbmsstatic MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group"); 78170613Sbmsstatic MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options"); 79170613Sbmsstatic MALLOC_DEFINE(M_IPMSOURCE, "in_msource", "IPv4 multicast source filter"); 80170613Sbms 81170613Sbms/* 82170613Sbms * The IPv4 multicast list (in_multihead and associated structures) are 83170613Sbms * protected by the global in_multi_mtx. See in_var.h for more details. For 84170613Sbms * now, in_multi_mtx is marked as recursible due to IGMP's calling back into 85170613Sbms * ip_output() to send IGMP packets while holding the lock; this probably is 86170613Sbms * not quite desirable. 87170613Sbms */ 88170613Sbmsstruct in_multihead in_multihead; /* XXX BSS initialization */ 89170613Sbmsstruct mtx in_multi_mtx; 90170613SbmsMTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE); 91170613Sbms 92170613Sbms/* 93170613Sbms * Functions with non-static linkage defined in this file should be 94170613Sbms * declared in in_var.h: 95170613Sbms * imo_match_group() 96170613Sbms * imo_match_source() 97170613Sbms * in_addmulti() 98170613Sbms * in_delmulti() 99170613Sbms * in_delmulti_locked() 100170613Sbms * and ip_var.h: 101170613Sbms * inp_freemoptions() 102170613Sbms * inp_getmoptions() 103170613Sbms * inp_setmoptions() 104170613Sbms */ 105170613Sbmsstatic int imo_grow(struct ip_moptions *); 106170613Sbmsstatic int imo_join_source(struct ip_moptions *, size_t, sockunion_t *); 107170613Sbmsstatic int imo_leave_source(struct ip_moptions *, size_t, sockunion_t *); 108170613Sbmsstatic int inp_change_source_filter(struct inpcb *, struct sockopt *); 109170613Sbmsstatic struct ip_moptions * 110170613Sbms inp_findmoptions(struct inpcb *); 111170613Sbmsstatic int inp_get_source_filters(struct inpcb *, struct sockopt *); 112170613Sbmsstatic int inp_join_group(struct inpcb *, struct sockopt *); 113170613Sbmsstatic int inp_leave_group(struct inpcb *, struct sockopt *); 114170613Sbmsstatic int inp_set_multicast_if(struct inpcb *, struct sockopt *); 115170613Sbmsstatic int inp_set_source_filters(struct inpcb *, struct sockopt *); 116170613Sbms 117170613Sbms/* 118170613Sbms * Resize the ip_moptions vector to the next power-of-two minus 1. 119170613Sbms * May be called with locks held; do not sleep. 120170613Sbms */ 121170613Sbmsstatic int 122170613Sbmsimo_grow(struct ip_moptions *imo) 123170613Sbms{ 124170613Sbms struct in_multi **nmships; 125170613Sbms struct in_multi **omships; 126170613Sbms struct in_mfilter *nmfilters; 127170613Sbms struct in_mfilter *omfilters; 128170613Sbms size_t idx; 129170613Sbms size_t newmax; 130170613Sbms size_t oldmax; 131170613Sbms 132170613Sbms nmships = NULL; 133170613Sbms nmfilters = NULL; 134170613Sbms omships = imo->imo_membership; 135170613Sbms omfilters = imo->imo_mfilters; 136170613Sbms oldmax = imo->imo_max_memberships; 137170613Sbms newmax = ((oldmax + 1) * 2) - 1; 138170613Sbms 139170613Sbms if (newmax <= IP_MAX_MEMBERSHIPS) { 140170613Sbms nmships = (struct in_multi **)realloc(omships, 141170613Sbms sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT); 142170613Sbms nmfilters = (struct in_mfilter *)realloc(omfilters, 143170613Sbms sizeof(struct in_mfilter) * newmax, M_IPMSOURCE, M_NOWAIT); 144170613Sbms if (nmships != NULL && nmfilters != NULL) { 145170613Sbms /* Initialize newly allocated source filter heads. */ 146170613Sbms for (idx = oldmax; idx < newmax; idx++) { 147170613Sbms nmfilters[idx].imf_fmode = MCAST_EXCLUDE; 148170613Sbms nmfilters[idx].imf_nsources = 0; 149170613Sbms TAILQ_INIT(&nmfilters[idx].imf_sources); 150170613Sbms } 151170613Sbms imo->imo_max_memberships = newmax; 152170613Sbms imo->imo_membership = nmships; 153170613Sbms imo->imo_mfilters = nmfilters; 154170613Sbms } 155170613Sbms } 156170613Sbms 157170613Sbms if (nmships == NULL || nmfilters == NULL) { 158170613Sbms if (nmships != NULL) 159170613Sbms free(nmships, M_IPMOPTS); 160170613Sbms if (nmfilters != NULL) 161170613Sbms free(nmfilters, M_IPMSOURCE); 162170613Sbms return (ETOOMANYREFS); 163170613Sbms } 164170613Sbms 165170613Sbms return (0); 166170613Sbms} 167170613Sbms 168170613Sbms/* 169170613Sbms * Add a source to a multicast filter list. 170170613Sbms * Assumes the associated inpcb is locked. 171170613Sbms */ 172170613Sbmsstatic int 173170613Sbmsimo_join_source(struct ip_moptions *imo, size_t gidx, sockunion_t *src) 174170613Sbms{ 175170613Sbms struct in_msource *ims, *nims; 176170613Sbms struct in_mfilter *imf; 177170613Sbms 178170613Sbms KASSERT(src->ss.ss_family == AF_INET, ("%s: !AF_INET", __func__)); 179170613Sbms KASSERT(imo->imo_mfilters != NULL, 180170613Sbms ("%s: imo_mfilters vector not allocated", __func__)); 181170613Sbms 182170613Sbms imf = &imo->imo_mfilters[gidx]; 183170613Sbms if (imf->imf_nsources == IP_MAX_SOURCE_FILTER) 184170613Sbms return (ENOBUFS); 185170613Sbms 186170613Sbms ims = imo_match_source(imo, gidx, &src->sa); 187170613Sbms if (ims != NULL) 188170613Sbms return (EADDRNOTAVAIL); 189170613Sbms 190170613Sbms /* Do not sleep with inp lock held. */ 191170613Sbms MALLOC(nims, struct in_msource *, sizeof(struct in_msource), 192170613Sbms M_IPMSOURCE, M_NOWAIT | M_ZERO); 193170613Sbms if (nims == NULL) 194170613Sbms return (ENOBUFS); 195170613Sbms 196170613Sbms nims->ims_addr = src->ss; 197170613Sbms TAILQ_INSERT_TAIL(&imf->imf_sources, nims, ims_next); 198170613Sbms imf->imf_nsources++; 199170613Sbms 200170613Sbms return (0); 201170613Sbms} 202170613Sbms 203170613Sbmsstatic int 204170613Sbmsimo_leave_source(struct ip_moptions *imo, size_t gidx, sockunion_t *src) 205170613Sbms{ 206170613Sbms struct in_msource *ims; 207170613Sbms struct in_mfilter *imf; 208170613Sbms 209170613Sbms KASSERT(src->ss.ss_family == AF_INET, ("%s: !AF_INET", __func__)); 210170613Sbms KASSERT(imo->imo_mfilters != NULL, 211170613Sbms ("%s: imo_mfilters vector not allocated", __func__)); 212170613Sbms 213170613Sbms imf = &imo->imo_mfilters[gidx]; 214170613Sbms if (imf->imf_nsources == IP_MAX_SOURCE_FILTER) 215170613Sbms return (ENOBUFS); 216170613Sbms 217170613Sbms ims = imo_match_source(imo, gidx, &src->sa); 218170613Sbms if (ims == NULL) 219170613Sbms return (EADDRNOTAVAIL); 220170613Sbms 221170613Sbms TAILQ_REMOVE(&imf->imf_sources, ims, ims_next); 222170613Sbms FREE(ims, M_IPMSOURCE); 223170613Sbms imf->imf_nsources--; 224170613Sbms 225170613Sbms return (0); 226170613Sbms} 227170613Sbms 228170613Sbms/* 229170613Sbms * Find an IPv4 multicast group entry for this ip_moptions instance 230170613Sbms * which matches the specified group, and optionally an interface. 231170613Sbms * Return its index into the array, or -1 if not found. 232170613Sbms */ 233170613Sbmssize_t 234170613Sbmsimo_match_group(struct ip_moptions *imo, struct ifnet *ifp, 235170613Sbms struct sockaddr *group) 236170613Sbms{ 237170613Sbms sockunion_t *gsa; 238170613Sbms struct in_multi **pinm; 239170613Sbms int idx; 240170613Sbms int nmships; 241170613Sbms 242170613Sbms gsa = (sockunion_t *)group; 243170613Sbms 244170613Sbms /* The imo_membership array may be lazy allocated. */ 245170613Sbms if (imo->imo_membership == NULL || imo->imo_num_memberships == 0) 246170613Sbms return (-1); 247170613Sbms 248170613Sbms nmships = imo->imo_num_memberships; 249170613Sbms pinm = &imo->imo_membership[0]; 250170613Sbms for (idx = 0; idx < nmships; idx++, pinm++) { 251170613Sbms if (*pinm == NULL) 252170613Sbms continue; 253170613Sbms#if 0 254170613Sbms printf("%s: trying ifp = %p, inaddr = %s ", __func__, 255170613Sbms ifp, inet_ntoa(gsa->sin.sin_addr)); 256170613Sbms printf("against %p, %s\n", 257170613Sbms (*pinm)->inm_ifp, inet_ntoa((*pinm)->inm_addr)); 258170613Sbms#endif 259170613Sbms if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) && 260170613Sbms (*pinm)->inm_addr.s_addr == gsa->sin.sin_addr.s_addr) { 261170613Sbms break; 262170613Sbms } 263170613Sbms } 264170613Sbms if (idx >= nmships) 265170613Sbms idx = -1; 266170613Sbms 267170613Sbms return (idx); 268170613Sbms} 269170613Sbms 270170613Sbms/* 271170613Sbms * Find a multicast source entry for this imo which matches 272170613Sbms * the given group index for this socket, and source address. 273170613Sbms */ 274170613Sbmsstruct in_msource * 275170613Sbmsimo_match_source(struct ip_moptions *imo, size_t gidx, struct sockaddr *src) 276170613Sbms{ 277170613Sbms struct in_mfilter *imf; 278170613Sbms struct in_msource *ims, *pims; 279170613Sbms 280170613Sbms KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__)); 281170613Sbms KASSERT(gidx != -1 && gidx < imo->imo_num_memberships, 282170613Sbms ("%s: invalid index %d\n", __func__, (int)gidx)); 283170613Sbms 284170613Sbms /* The imo_mfilters array may be lazy allocated. */ 285170613Sbms if (imo->imo_mfilters == NULL) 286170613Sbms return (NULL); 287170613Sbms 288170613Sbms pims = NULL; 289170613Sbms imf = &imo->imo_mfilters[gidx]; 290170613Sbms TAILQ_FOREACH(ims, &imf->imf_sources, ims_next) { 291170613Sbms /* 292170613Sbms * Perform bitwise comparison of two IPv4 addresses. 293170613Sbms * TODO: Do the same for IPv6. 294170613Sbms * Do not use sa_equal() for this as it is not aware of 295170613Sbms * deeper structure in sockaddr_in or sockaddr_in6. 296170613Sbms */ 297170613Sbms if (((struct sockaddr_in *)&ims->ims_addr)->sin_addr.s_addr == 298170613Sbms ((struct sockaddr_in *)src)->sin_addr.s_addr) { 299170613Sbms pims = ims; 300170613Sbms break; 301170613Sbms } 302170613Sbms } 303170613Sbms 304170613Sbms return (pims); 305170613Sbms} 306170613Sbms 307170613Sbms/* 308170613Sbms * Join an IPv4 multicast group. 309170613Sbms */ 310170613Sbmsstruct in_multi * 311170613Sbmsin_addmulti(struct in_addr *ap, struct ifnet *ifp) 312170613Sbms{ 313170613Sbms struct in_multi *inm; 314170613Sbms 315170613Sbms inm = NULL; 316170613Sbms 317170613Sbms IFF_LOCKGIANT(ifp); 318170613Sbms IN_MULTI_LOCK(); 319170613Sbms 320170613Sbms IN_LOOKUP_MULTI(*ap, ifp, inm); 321170613Sbms if (inm != NULL) { 322170613Sbms /* 323170613Sbms * If we already joined this group, just bump the 324170613Sbms * refcount and return it. 325170613Sbms */ 326170613Sbms KASSERT(inm->inm_refcount >= 1, 327170613Sbms ("%s: bad refcount %d", __func__, inm->inm_refcount)); 328170613Sbms ++inm->inm_refcount; 329170613Sbms } else do { 330170613Sbms sockunion_t gsa; 331170613Sbms struct ifmultiaddr *ifma; 332170613Sbms struct in_multi *ninm; 333170613Sbms int error; 334170613Sbms 335170613Sbms memset(&gsa, 0, sizeof(gsa)); 336170613Sbms gsa.sin.sin_family = AF_INET; 337170613Sbms gsa.sin.sin_len = sizeof(struct sockaddr_in); 338170613Sbms gsa.sin.sin_addr = *ap; 339170613Sbms 340170613Sbms /* 341170613Sbms * Check if a link-layer group is already associated 342170613Sbms * with this network-layer group on the given ifnet. 343170613Sbms * If so, bump the refcount on the existing network-layer 344170613Sbms * group association and return it. 345170613Sbms */ 346170613Sbms error = if_addmulti(ifp, &gsa.sa, &ifma); 347170613Sbms if (error) 348170613Sbms break; 349170613Sbms if (ifma->ifma_protospec != NULL) { 350170613Sbms inm = (struct in_multi *)ifma->ifma_protospec; 351170613Sbms#ifdef INVARIANTS 352170613Sbms if (inm->inm_ifma != ifma || inm->inm_ifp != ifp || 353170613Sbms inm->inm_addr.s_addr != ap->s_addr) 354170613Sbms panic("%s: ifma is inconsistent", __func__); 355170613Sbms#endif 356170613Sbms ++inm->inm_refcount; 357170613Sbms break; 358170613Sbms } 359170613Sbms 360170613Sbms /* 361170613Sbms * A new membership is needed; construct it and 362170613Sbms * perform the IGMP join. 363170613Sbms */ 364170613Sbms ninm = malloc(sizeof(*ninm), M_IPMADDR, M_NOWAIT | M_ZERO); 365170613Sbms if (ninm == NULL) { 366170613Sbms if_delmulti_ifma(ifma); 367170613Sbms break; 368170613Sbms } 369170613Sbms ninm->inm_addr = *ap; 370170613Sbms ninm->inm_ifp = ifp; 371170613Sbms ninm->inm_ifma = ifma; 372170613Sbms ninm->inm_refcount = 1; 373170613Sbms ifma->ifma_protospec = ninm; 374170613Sbms LIST_INSERT_HEAD(&in_multihead, ninm, inm_link); 375170613Sbms 376170613Sbms igmp_joingroup(ninm); 377170613Sbms 378170613Sbms inm = ninm; 379170613Sbms } while (0); 380170613Sbms 381170613Sbms IN_MULTI_UNLOCK(); 382170613Sbms IFF_UNLOCKGIANT(ifp); 383170613Sbms 384170613Sbms return (inm); 385170613Sbms} 386170613Sbms 387170613Sbms/* 388170613Sbms * Leave an IPv4 multicast group. 389170613Sbms * It is OK to call this routine if the underlying ifnet went away. 390170613Sbms * 391170613Sbms * XXX: To deal with the ifp going away, we cheat; the link-layer code in net 392170613Sbms * will set ifma_ifp to NULL when the associated ifnet instance is detached 393170613Sbms * from the system. 394170613Sbms * 395170613Sbms * The only reason we need to violate layers and check ifma_ifp here at all 396170613Sbms * is because certain hardware drivers still require Giant to be held, 397170613Sbms * and it must always be taken before other locks. 398170613Sbms */ 399170613Sbmsvoid 400170613Sbmsin_delmulti(struct in_multi *inm) 401170613Sbms{ 402170613Sbms struct ifnet *ifp; 403170613Sbms 404170613Sbms KASSERT(inm != NULL, ("%s: inm is NULL", __func__)); 405170613Sbms KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__)); 406170613Sbms ifp = inm->inm_ifma->ifma_ifp; 407170613Sbms 408170613Sbms if (ifp != NULL) { 409170613Sbms /* 410170613Sbms * Sanity check that netinet's notion of ifp is the 411170613Sbms * same as net's. 412170613Sbms */ 413170613Sbms KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__)); 414170613Sbms IFF_LOCKGIANT(ifp); 415170613Sbms } 416170613Sbms 417170613Sbms IN_MULTI_LOCK(); 418170613Sbms in_delmulti_locked(inm); 419170613Sbms IN_MULTI_UNLOCK(); 420170613Sbms 421170613Sbms if (ifp != NULL) 422170613Sbms IFF_UNLOCKGIANT(ifp); 423170613Sbms} 424170613Sbms 425170613Sbms/* 426170613Sbms * Delete a multicast address record, with locks held. 427170613Sbms * 428170613Sbms * It is OK to call this routine if the ifp went away. 429170613Sbms * Assumes that caller holds the IN_MULTI lock, and that 430170613Sbms * Giant was taken before other locks if required by the hardware. 431170613Sbms */ 432170613Sbmsvoid 433170613Sbmsin_delmulti_locked(struct in_multi *inm) 434170613Sbms{ 435170613Sbms struct ifmultiaddr *ifma; 436170613Sbms 437170613Sbms IN_MULTI_LOCK_ASSERT(); 438170613Sbms KASSERT(inm->inm_refcount >= 1, ("%s: freeing freed inm", __func__)); 439170613Sbms 440170613Sbms if (--inm->inm_refcount == 0) { 441170613Sbms igmp_leavegroup(inm); 442170613Sbms 443170613Sbms ifma = inm->inm_ifma; 444170613Sbms#ifdef DIAGNOSTIC 445170613Sbms if (bootverbose) 446170613Sbms printf("%s: purging ifma %p\n", __func__, ifma); 447170613Sbms#endif 448170613Sbms KASSERT(ifma->ifma_protospec == inm, 449170613Sbms ("%s: ifma_protospec != inm", __func__)); 450170613Sbms ifma->ifma_protospec = NULL; 451170613Sbms 452170613Sbms LIST_REMOVE(inm, inm_link); 453170613Sbms free(inm, M_IPMADDR); 454170613Sbms 455170613Sbms if_delmulti_ifma(ifma); 456170613Sbms } 457170613Sbms} 458170613Sbms 459170613Sbms/* 460170613Sbms * Block or unblock an ASM/SSM multicast source on an inpcb. 461170613Sbms */ 462170613Sbmsstatic int 463170613Sbmsinp_change_source_filter(struct inpcb *inp, struct sockopt *sopt) 464170613Sbms{ 465170613Sbms struct group_source_req gsr; 466170613Sbms sockunion_t *gsa, *ssa; 467170613Sbms struct ifnet *ifp; 468170613Sbms struct in_mfilter *imf; 469170613Sbms struct ip_moptions *imo; 470170613Sbms struct in_msource *ims; 471170613Sbms size_t idx; 472170613Sbms int error; 473170613Sbms int block; 474170613Sbms 475170613Sbms ifp = NULL; 476170613Sbms error = 0; 477170613Sbms block = 0; 478170613Sbms 479170613Sbms memset(&gsr, 0, sizeof(struct group_source_req)); 480170613Sbms gsa = (sockunion_t *)&gsr.gsr_group; 481170613Sbms ssa = (sockunion_t *)&gsr.gsr_source; 482170613Sbms 483170613Sbms switch (sopt->sopt_name) { 484170613Sbms case IP_BLOCK_SOURCE: 485170613Sbms case IP_UNBLOCK_SOURCE: { 486170613Sbms struct ip_mreq_source mreqs; 487170613Sbms 488170613Sbms error = sooptcopyin(sopt, &mreqs, 489170613Sbms sizeof(struct ip_mreq_source), 490170613Sbms sizeof(struct ip_mreq_source)); 491170613Sbms if (error) 492170613Sbms return (error); 493170613Sbms 494170613Sbms gsa->sin.sin_family = AF_INET; 495170613Sbms gsa->sin.sin_len = sizeof(struct sockaddr_in); 496170613Sbms gsa->sin.sin_addr = mreqs.imr_multiaddr; 497170613Sbms 498170613Sbms ssa->sin.sin_family = AF_INET; 499170613Sbms ssa->sin.sin_len = sizeof(struct sockaddr_in); 500170613Sbms ssa->sin.sin_addr = mreqs.imr_sourceaddr; 501170613Sbms 502170613Sbms if (mreqs.imr_interface.s_addr != INADDR_ANY) 503170613Sbms INADDR_TO_IFP(mreqs.imr_interface, ifp); 504170613Sbms 505170613Sbms if (sopt->sopt_name == IP_BLOCK_SOURCE) 506170613Sbms block = 1; 507170613Sbms 508170613Sbms#ifdef DIAGNOSTIC 509170613Sbms if (bootverbose) { 510170613Sbms printf("%s: imr_interface = %s, ifp = %p\n", 511170613Sbms __func__, inet_ntoa(mreqs.imr_interface), ifp); 512170613Sbms } 513170613Sbms#endif 514170613Sbms break; 515170613Sbms } 516170613Sbms 517170613Sbms case MCAST_BLOCK_SOURCE: 518170613Sbms case MCAST_UNBLOCK_SOURCE: 519170613Sbms error = sooptcopyin(sopt, &gsr, 520170613Sbms sizeof(struct group_source_req), 521170613Sbms sizeof(struct group_source_req)); 522170613Sbms if (error) 523170613Sbms return (error); 524170613Sbms 525170613Sbms if (gsa->sin.sin_family != AF_INET || 526170613Sbms gsa->sin.sin_len != sizeof(struct sockaddr_in)) 527170613Sbms return (EINVAL); 528170613Sbms 529170613Sbms if (ssa->sin.sin_family != AF_INET || 530170613Sbms ssa->sin.sin_len != sizeof(struct sockaddr_in)) 531170613Sbms return (EINVAL); 532170613Sbms 533170613Sbms if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface) 534170613Sbms return (EADDRNOTAVAIL); 535170613Sbms 536170613Sbms ifp = ifnet_byindex(gsr.gsr_interface); 537170613Sbms 538170613Sbms if (sopt->sopt_name == MCAST_BLOCK_SOURCE) 539170613Sbms block = 1; 540170613Sbms break; 541170613Sbms 542170613Sbms default: 543170613Sbms#ifdef DIAGNOSTIC 544170613Sbms if (bootverbose) { 545170613Sbms printf("%s: unknown sopt_name %d\n", __func__, 546170613Sbms sopt->sopt_name); 547170613Sbms } 548170613Sbms#endif 549170613Sbms return (EOPNOTSUPP); 550170613Sbms break; 551170613Sbms } 552170613Sbms 553170613Sbms /* XXX INET6 */ 554170613Sbms if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 555170613Sbms return (EINVAL); 556170613Sbms 557170613Sbms /* 558170613Sbms * Check if we are actually a member of this group. 559170613Sbms */ 560170613Sbms imo = inp_findmoptions(inp); 561170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 562170613Sbms if (idx == -1 || imo->imo_mfilters == NULL) { 563170613Sbms error = EADDRNOTAVAIL; 564170613Sbms goto out_locked; 565170613Sbms } 566170613Sbms 567170613Sbms KASSERT(imo->imo_mfilters != NULL, 568170613Sbms ("%s: imo_mfilters not allocated", __func__)); 569170613Sbms imf = &imo->imo_mfilters[idx]; 570170613Sbms 571170613Sbms /* 572170613Sbms * SSM multicast truth table for block/unblock operations. 573170613Sbms * 574170613Sbms * Operation Filter Mode Entry exists? Action 575170613Sbms * 576170613Sbms * block exclude no add source to filter 577170613Sbms * unblock include no add source to filter 578170613Sbms * block include no EINVAL 579170613Sbms * unblock exclude no EINVAL 580170613Sbms * block exclude yes EADDRNOTAVAIL 581170613Sbms * unblock include yes EADDRNOTAVAIL 582170613Sbms * block include yes remove source from filter 583170613Sbms * unblock exclude yes remove source from filter 584170613Sbms * 585170613Sbms * FreeBSD does not explicitly distinguish between ASM and SSM 586170613Sbms * mode sockets; all sockets are assumed to have a filter list. 587170613Sbms */ 588170613Sbms#ifdef DIAGNOSTIC 589170613Sbms if (bootverbose) { 590170613Sbms printf("%s: imf_fmode is %s\n", __func__, 591170613Sbms imf->imf_fmode == MCAST_INCLUDE ? "include" : "exclude"); 592170613Sbms } 593170613Sbms#endif 594170613Sbms ims = imo_match_source(imo, idx, &ssa->sa); 595170613Sbms if (ims == NULL) { 596170613Sbms if ((block == 1 && imf->imf_fmode == MCAST_EXCLUDE) || 597170613Sbms (block == 0 && imf->imf_fmode == MCAST_INCLUDE)) { 598170613Sbms#ifdef DIAGNOSTIC 599170613Sbms if (bootverbose) { 600170613Sbms printf("%s: adding %s to filter list\n", 601170613Sbms __func__, inet_ntoa(ssa->sin.sin_addr)); 602170613Sbms } 603170613Sbms#endif 604170613Sbms error = imo_join_source(imo, idx, ssa); 605170613Sbms } 606170613Sbms if ((block == 1 && imf->imf_fmode == MCAST_INCLUDE) || 607170613Sbms (block == 0 && imf->imf_fmode == MCAST_EXCLUDE)) { 608170613Sbms /* 609170613Sbms * If the socket is in inclusive mode: 610170613Sbms * the source is already blocked as it has no entry. 611170613Sbms * If the socket is in exclusive mode: 612170613Sbms * the source is already unblocked as it has no entry. 613170613Sbms */ 614170613Sbms#ifdef DIAGNOSTIC 615170613Sbms if (bootverbose) { 616170613Sbms printf("%s: ims %p; %s already [un]blocked\n", 617170613Sbms __func__, ims, 618170613Sbms inet_ntoa(ssa->sin.sin_addr)); 619170613Sbms } 620170613Sbms#endif 621170613Sbms error = EINVAL; 622170613Sbms } 623170613Sbms } else { 624170613Sbms if ((block == 1 && imf->imf_fmode == MCAST_EXCLUDE) || 625170613Sbms (block == 0 && imf->imf_fmode == MCAST_INCLUDE)) { 626170613Sbms /* 627170613Sbms * If the socket is in exclusive mode: 628170613Sbms * the source is already blocked as it has an entry. 629170613Sbms * If the socket is in inclusive mode: 630170613Sbms * the source is already unblocked as it has an entry. 631170613Sbms */ 632170613Sbms#ifdef DIAGNOSTIC 633170613Sbms if (bootverbose) { 634170613Sbms printf("%s: ims %p; %s already [un]blocked\n", 635170613Sbms __func__, ims, 636170613Sbms inet_ntoa(ssa->sin.sin_addr)); 637170613Sbms } 638170613Sbms#endif 639170613Sbms error = EADDRNOTAVAIL; 640170613Sbms } 641170613Sbms if ((block == 1 && imf->imf_fmode == MCAST_INCLUDE) || 642170613Sbms (block == 0 && imf->imf_fmode == MCAST_EXCLUDE)) { 643170613Sbms#ifdef DIAGNOSTIC 644170613Sbms if (bootverbose) { 645170613Sbms printf("%s: removing %s from filter list\n", 646170613Sbms __func__, inet_ntoa(ssa->sin.sin_addr)); 647170613Sbms } 648170613Sbms#endif 649170613Sbms error = imo_leave_source(imo, idx, ssa); 650170613Sbms } 651170613Sbms } 652170613Sbms 653170613Sbmsout_locked: 654170613Sbms INP_UNLOCK(inp); 655170613Sbms return (error); 656170613Sbms} 657170613Sbms 658170613Sbms/* 659170613Sbms * Given an inpcb, return its multicast options structure pointer. Accepts 660170613Sbms * an unlocked inpcb pointer, but will return it locked. May sleep. 661170613Sbms */ 662170613Sbmsstatic struct ip_moptions * 663170613Sbmsinp_findmoptions(struct inpcb *inp) 664170613Sbms{ 665170613Sbms struct ip_moptions *imo; 666170613Sbms struct in_multi **immp; 667170613Sbms struct in_mfilter *imfp; 668170613Sbms size_t idx; 669170613Sbms 670170613Sbms INP_LOCK(inp); 671170613Sbms if (inp->inp_moptions != NULL) 672170613Sbms return (inp->inp_moptions); 673170613Sbms 674170613Sbms INP_UNLOCK(inp); 675170613Sbms 676170613Sbms imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS, 677170613Sbms M_WAITOK); 678170613Sbms immp = (struct in_multi **)malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, 679170613Sbms M_IPMOPTS, M_WAITOK | M_ZERO); 680170613Sbms imfp = (struct in_mfilter *)malloc( 681170613Sbms sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS, 682170613Sbms M_IPMSOURCE, M_WAITOK); 683170613Sbms 684170613Sbms imo->imo_multicast_ifp = NULL; 685170613Sbms imo->imo_multicast_addr.s_addr = INADDR_ANY; 686170613Sbms imo->imo_multicast_vif = -1; 687170613Sbms imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 688170613Sbms imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 689170613Sbms imo->imo_num_memberships = 0; 690170613Sbms imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; 691170613Sbms imo->imo_membership = immp; 692170613Sbms 693170613Sbms /* Initialize per-group source filters. */ 694170613Sbms for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++) { 695170613Sbms imfp[idx].imf_fmode = MCAST_EXCLUDE; 696170613Sbms imfp[idx].imf_nsources = 0; 697170613Sbms TAILQ_INIT(&imfp[idx].imf_sources); 698170613Sbms } 699170613Sbms imo->imo_mfilters = imfp; 700170613Sbms 701170613Sbms INP_LOCK(inp); 702170613Sbms if (inp->inp_moptions != NULL) { 703170613Sbms free(imfp, M_IPMSOURCE); 704170613Sbms free(immp, M_IPMOPTS); 705170613Sbms free(imo, M_IPMOPTS); 706170613Sbms return (inp->inp_moptions); 707170613Sbms } 708170613Sbms inp->inp_moptions = imo; 709170613Sbms return (imo); 710170613Sbms} 711170613Sbms 712170613Sbms/* 713170613Sbms * Discard the IP multicast options (and source filters). 714170613Sbms */ 715170613Sbmsvoid 716170613Sbmsinp_freemoptions(struct ip_moptions *imo) 717170613Sbms{ 718170613Sbms struct in_mfilter *imf; 719170613Sbms struct in_msource *ims, *tims; 720170613Sbms size_t idx, nmships; 721170613Sbms 722170613Sbms KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__)); 723170613Sbms 724170613Sbms nmships = imo->imo_num_memberships; 725170613Sbms for (idx = 0; idx < nmships; ++idx) { 726170613Sbms in_delmulti(imo->imo_membership[idx]); 727170613Sbms 728170613Sbms if (imo->imo_mfilters != NULL) { 729170613Sbms imf = &imo->imo_mfilters[idx]; 730170613Sbms TAILQ_FOREACH_SAFE(ims, &imf->imf_sources, 731170613Sbms ims_next, tims) { 732170613Sbms TAILQ_REMOVE(&imf->imf_sources, ims, ims_next); 733170613Sbms FREE(ims, M_IPMSOURCE); 734170613Sbms imf->imf_nsources--; 735170613Sbms } 736170613Sbms KASSERT(imf->imf_nsources == 0, 737170613Sbms ("%s: did not free all imf_nsources", __func__)); 738170613Sbms } 739170613Sbms } 740170613Sbms 741170613Sbms if (imo->imo_mfilters != NULL) 742170613Sbms free(imo->imo_mfilters, M_IPMSOURCE); 743170613Sbms free(imo->imo_membership, M_IPMOPTS); 744170613Sbms free(imo, M_IPMOPTS); 745170613Sbms} 746170613Sbms 747170613Sbms/* 748170613Sbms * Atomically get source filters on a socket for an IPv4 multicast group. 749170613Sbms * Called with INP lock held; returns with lock released. 750170613Sbms */ 751170613Sbmsstatic int 752170613Sbmsinp_get_source_filters(struct inpcb *inp, struct sockopt *sopt) 753170613Sbms{ 754170613Sbms struct __msfilterreq msfr; 755170613Sbms sockunion_t *gsa; 756170613Sbms struct ifnet *ifp; 757170613Sbms struct ip_moptions *imo; 758170613Sbms struct in_mfilter *imf; 759170613Sbms struct in_msource *ims; 760170613Sbms struct sockaddr_storage *ptss; 761170613Sbms struct sockaddr_storage *tss; 762170613Sbms int error; 763170613Sbms size_t idx; 764170613Sbms 765170613Sbms INP_LOCK_ASSERT(inp); 766170613Sbms 767170613Sbms imo = inp->inp_moptions; 768170613Sbms KASSERT(imo != NULL, ("%s: null ip_moptions", __func__)); 769170613Sbms 770170613Sbms INP_UNLOCK(inp); 771170613Sbms 772170613Sbms error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq), 773170613Sbms sizeof(struct __msfilterreq)); 774170613Sbms if (error) 775170613Sbms return (error); 776170613Sbms 777170613Sbms if (msfr.msfr_ifindex == 0 || if_index < msfr.msfr_ifindex) 778170613Sbms return (EINVAL); 779170613Sbms 780170613Sbms ifp = ifnet_byindex(msfr.msfr_ifindex); 781170613Sbms if (ifp == NULL) 782170613Sbms return (EINVAL); 783170613Sbms 784170613Sbms INP_LOCK(inp); 785170613Sbms 786170613Sbms /* 787170613Sbms * Lookup group on the socket. 788170613Sbms */ 789170613Sbms gsa = (sockunion_t *)&msfr.msfr_group; 790170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 791170613Sbms if (idx == -1 || imo->imo_mfilters == NULL) { 792170613Sbms INP_UNLOCK(inp); 793170613Sbms return (EADDRNOTAVAIL); 794170613Sbms } 795170613Sbms 796170613Sbms imf = &imo->imo_mfilters[idx]; 797170613Sbms msfr.msfr_fmode = imf->imf_fmode; 798170613Sbms msfr.msfr_nsrcs = imf->imf_nsources; 799170613Sbms 800170613Sbms /* 801170613Sbms * If the user specified a buffer, copy out the source filter 802170613Sbms * entries to userland gracefully. 803170613Sbms * msfr.msfr_nsrcs is always set to the total number of filter 804170613Sbms * entries which the kernel currently has for this group. 805170613Sbms */ 806170613Sbms tss = NULL; 807170613Sbms if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) { 808170613Sbms /* 809170613Sbms * Make a copy of the source vector so that we do not 810170613Sbms * thrash the inpcb lock whilst copying it out. 811170613Sbms * We only copy out the number of entries which userland 812170613Sbms * has asked for, but we always tell userland how big the 813170613Sbms * buffer really needs to be. 814170613Sbms */ 815170613Sbms MALLOC(tss, struct sockaddr_storage *, 816170613Sbms sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs, 817170613Sbms M_TEMP, M_NOWAIT); 818170613Sbms if (tss == NULL) { 819170613Sbms error = ENOBUFS; 820170613Sbms } else { 821170613Sbms ptss = tss; 822170613Sbms TAILQ_FOREACH(ims, &imf->imf_sources, ims_next) { 823170613Sbms memcpy(ptss++, &ims->ims_addr, 824170613Sbms sizeof(struct sockaddr_storage)); 825170613Sbms } 826170613Sbms } 827170613Sbms } 828170613Sbms 829170613Sbms INP_UNLOCK(inp); 830170613Sbms 831170613Sbms if (tss != NULL) { 832170613Sbms error = copyout(tss, msfr.msfr_srcs, 833170613Sbms sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs); 834170613Sbms FREE(tss, M_TEMP); 835170613Sbms } 836170613Sbms 837170613Sbms if (error) 838170613Sbms return (error); 839170613Sbms 840170613Sbms error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq)); 841170613Sbms 842170613Sbms return (error); 843170613Sbms} 844170613Sbms 845170613Sbms/* 846170613Sbms * Return the IP multicast options in response to user getsockopt(). 847170613Sbms */ 848170613Sbmsint 849170613Sbmsinp_getmoptions(struct inpcb *inp, struct sockopt *sopt) 850170613Sbms{ 851170613Sbms struct ip_mreqn mreqn; 852170613Sbms struct ip_moptions *imo; 853170613Sbms struct ifnet *ifp; 854170613Sbms struct in_ifaddr *ia; 855170613Sbms int error, optval; 856170613Sbms u_char coptval; 857170613Sbms 858170613Sbms INP_LOCK(inp); 859170613Sbms imo = inp->inp_moptions; 860171746Scsjp /* 861171746Scsjp * If socket is neither of type SOCK_RAW or SOCK_DGRAM, 862171746Scsjp * or is a divert socket, reject it. 863171746Scsjp */ 864171746Scsjp if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT || 865171746Scsjp (inp->inp_socket->so_proto->pr_type != SOCK_RAW && 866171746Scsjp inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) { 867171746Scsjp INP_UNLOCK(inp); 868171746Scsjp return (EOPNOTSUPP); 869171746Scsjp } 870170613Sbms 871170613Sbms error = 0; 872170613Sbms switch (sopt->sopt_name) { 873170613Sbms case IP_MULTICAST_VIF: 874170613Sbms if (imo != NULL) 875170613Sbms optval = imo->imo_multicast_vif; 876170613Sbms else 877170613Sbms optval = -1; 878170613Sbms INP_UNLOCK(inp); 879170613Sbms error = sooptcopyout(sopt, &optval, sizeof(int)); 880170613Sbms break; 881170613Sbms 882170613Sbms case IP_MULTICAST_IF: 883170613Sbms memset(&mreqn, 0, sizeof(struct ip_mreqn)); 884170613Sbms if (imo != NULL) { 885170613Sbms ifp = imo->imo_multicast_ifp; 886170613Sbms if (imo->imo_multicast_addr.s_addr != INADDR_ANY) { 887170613Sbms mreqn.imr_address = imo->imo_multicast_addr; 888170613Sbms } else if (ifp != NULL) { 889170613Sbms mreqn.imr_ifindex = ifp->if_index; 890170613Sbms IFP_TO_IA(ifp, ia); 891170613Sbms if (ia != NULL) { 892170613Sbms mreqn.imr_address = 893170613Sbms IA_SIN(ia)->sin_addr; 894170613Sbms } 895170613Sbms } 896170613Sbms } 897170613Sbms INP_UNLOCK(inp); 898170613Sbms if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { 899170613Sbms error = sooptcopyout(sopt, &mreqn, 900170613Sbms sizeof(struct ip_mreqn)); 901170613Sbms } else { 902170613Sbms error = sooptcopyout(sopt, &mreqn.imr_address, 903170613Sbms sizeof(struct in_addr)); 904170613Sbms } 905170613Sbms break; 906170613Sbms 907170613Sbms case IP_MULTICAST_TTL: 908170613Sbms if (imo == 0) 909170613Sbms optval = coptval = IP_DEFAULT_MULTICAST_TTL; 910170613Sbms else 911170613Sbms optval = coptval = imo->imo_multicast_ttl; 912170613Sbms INP_UNLOCK(inp); 913170613Sbms if (sopt->sopt_valsize == sizeof(u_char)) 914170613Sbms error = sooptcopyout(sopt, &coptval, sizeof(u_char)); 915170613Sbms else 916170613Sbms error = sooptcopyout(sopt, &optval, sizeof(int)); 917170613Sbms break; 918170613Sbms 919170613Sbms case IP_MULTICAST_LOOP: 920170613Sbms if (imo == 0) 921170613Sbms optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 922170613Sbms else 923170613Sbms optval = coptval = imo->imo_multicast_loop; 924170613Sbms INP_UNLOCK(inp); 925170613Sbms if (sopt->sopt_valsize == sizeof(u_char)) 926170613Sbms error = sooptcopyout(sopt, &coptval, sizeof(u_char)); 927170613Sbms else 928170613Sbms error = sooptcopyout(sopt, &optval, sizeof(int)); 929170613Sbms break; 930170613Sbms 931170613Sbms case IP_MSFILTER: 932170613Sbms if (imo == NULL) { 933170613Sbms error = EADDRNOTAVAIL; 934170613Sbms INP_UNLOCK(inp); 935170613Sbms } else { 936170613Sbms error = inp_get_source_filters(inp, sopt); 937170613Sbms } 938170613Sbms break; 939170613Sbms 940170613Sbms default: 941170613Sbms INP_UNLOCK(inp); 942170613Sbms error = ENOPROTOOPT; 943170613Sbms break; 944170613Sbms } 945170613Sbms 946170613Sbms INP_UNLOCK_ASSERT(inp); 947170613Sbms 948170613Sbms return (error); 949170613Sbms} 950170613Sbms 951170613Sbms/* 952170613Sbms * Join an IPv4 multicast group, possibly with a source. 953170613Sbms */ 954170613Sbmsstatic int 955170613Sbmsinp_join_group(struct inpcb *inp, struct sockopt *sopt) 956170613Sbms{ 957170613Sbms struct group_source_req gsr; 958170613Sbms sockunion_t *gsa, *ssa; 959170613Sbms struct ifnet *ifp; 960170613Sbms struct in_mfilter *imf; 961170613Sbms struct ip_moptions *imo; 962170613Sbms struct in_multi *inm; 963170613Sbms size_t idx; 964170613Sbms int error; 965170613Sbms 966170613Sbms ifp = NULL; 967170613Sbms error = 0; 968170613Sbms 969170613Sbms memset(&gsr, 0, sizeof(struct group_source_req)); 970170613Sbms gsa = (sockunion_t *)&gsr.gsr_group; 971170613Sbms gsa->ss.ss_family = AF_UNSPEC; 972170613Sbms ssa = (sockunion_t *)&gsr.gsr_source; 973170613Sbms ssa->ss.ss_family = AF_UNSPEC; 974170613Sbms 975170613Sbms switch (sopt->sopt_name) { 976170613Sbms case IP_ADD_MEMBERSHIP: 977170613Sbms case IP_ADD_SOURCE_MEMBERSHIP: { 978170613Sbms struct ip_mreq_source mreqs; 979170613Sbms 980170613Sbms if (sopt->sopt_name == IP_ADD_MEMBERSHIP) { 981170613Sbms error = sooptcopyin(sopt, &mreqs, 982170613Sbms sizeof(struct ip_mreq), 983170613Sbms sizeof(struct ip_mreq)); 984170613Sbms /* 985170613Sbms * Do argument switcharoo from ip_mreq into 986170613Sbms * ip_mreq_source to avoid using two instances. 987170613Sbms */ 988170613Sbms mreqs.imr_interface = mreqs.imr_sourceaddr; 989170613Sbms mreqs.imr_sourceaddr.s_addr = INADDR_ANY; 990170613Sbms } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { 991170613Sbms error = sooptcopyin(sopt, &mreqs, 992170613Sbms sizeof(struct ip_mreq_source), 993170613Sbms sizeof(struct ip_mreq_source)); 994170613Sbms } 995170613Sbms if (error) 996170613Sbms return (error); 997170613Sbms 998170613Sbms gsa->sin.sin_family = AF_INET; 999170613Sbms gsa->sin.sin_len = sizeof(struct sockaddr_in); 1000170613Sbms gsa->sin.sin_addr = mreqs.imr_multiaddr; 1001170613Sbms 1002170613Sbms if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { 1003170613Sbms ssa->sin.sin_family = AF_INET; 1004170613Sbms ssa->sin.sin_len = sizeof(struct sockaddr_in); 1005170613Sbms ssa->sin.sin_addr = mreqs.imr_sourceaddr; 1006170613Sbms } 1007170613Sbms 1008170613Sbms /* 1009170613Sbms * Obtain ifp. If no interface address was provided, 1010171317Sbms * use the interface of the route in the unicast FIB for 1011171317Sbms * the given multicast destination; usually, this is the 1012171317Sbms * default route. 1013171317Sbms * If this lookup fails, attempt to use the first non-loopback 1014171317Sbms * interface with multicast capability in the system as a 1015171317Sbms * last resort. The legacy IPv4 ASM API requires that we do 1016171317Sbms * this in order to allow groups to be joined when the routing 1017171317Sbms * table has not yet been populated during boot. 1018171317Sbms * If all of these conditions fail, return EADDRNOTAVAIL, and 1019171317Sbms * reject the IPv4 multicast join. 1020170613Sbms */ 1021170613Sbms if (mreqs.imr_interface.s_addr != INADDR_ANY) { 1022170613Sbms INADDR_TO_IFP(mreqs.imr_interface, ifp); 1023170613Sbms } else { 1024170613Sbms struct route ro; 1025170613Sbms 1026170613Sbms ro.ro_rt = NULL; 1027170613Sbms *(struct sockaddr_in *)&ro.ro_dst = gsa->sin; 1028170613Sbms rtalloc_ign(&ro, RTF_CLONING); 1029171317Sbms if (ro.ro_rt != NULL) { 1030171317Sbms ifp = ro.ro_rt->rt_ifp; 1031171317Sbms KASSERT(ifp != NULL, ("%s: null ifp", 1032171317Sbms __func__)); 1033171317Sbms RTFREE(ro.ro_rt); 1034171317Sbms } else { 1035171317Sbms struct in_ifaddr *ia; 1036171317Sbms struct ifnet *mfp = NULL; 1037171317Sbms TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 1038171317Sbms mfp = ia->ia_ifp; 1039171317Sbms if (!(mfp->if_flags & IFF_LOOPBACK) && 1040171317Sbms (mfp->if_flags & IFF_MULTICAST)) { 1041171317Sbms ifp = mfp; 1042171317Sbms break; 1043171317Sbms } 1044171317Sbms } 1045170613Sbms } 1046170613Sbms } 1047170613Sbms#ifdef DIAGNOSTIC 1048170613Sbms if (bootverbose) { 1049170613Sbms printf("%s: imr_interface = %s, ifp = %p\n", 1050170613Sbms __func__, inet_ntoa(mreqs.imr_interface), ifp); 1051170613Sbms } 1052170613Sbms#endif 1053170613Sbms break; 1054170613Sbms } 1055170613Sbms 1056170613Sbms case MCAST_JOIN_GROUP: 1057170613Sbms case MCAST_JOIN_SOURCE_GROUP: 1058170613Sbms if (sopt->sopt_name == MCAST_JOIN_GROUP) { 1059170613Sbms error = sooptcopyin(sopt, &gsr, 1060170613Sbms sizeof(struct group_req), 1061170613Sbms sizeof(struct group_req)); 1062170613Sbms } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { 1063170613Sbms error = sooptcopyin(sopt, &gsr, 1064170613Sbms sizeof(struct group_source_req), 1065170613Sbms sizeof(struct group_source_req)); 1066170613Sbms } 1067170613Sbms if (error) 1068170613Sbms return (error); 1069170613Sbms 1070170613Sbms if (gsa->sin.sin_family != AF_INET || 1071170613Sbms gsa->sin.sin_len != sizeof(struct sockaddr_in)) 1072170613Sbms return (EINVAL); 1073170613Sbms 1074170613Sbms /* 1075170613Sbms * Overwrite the port field if present, as the sockaddr 1076170613Sbms * being copied in may be matched with a binary comparison. 1077170613Sbms * XXX INET6 1078170613Sbms */ 1079170613Sbms gsa->sin.sin_port = 0; 1080170613Sbms if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { 1081170613Sbms if (ssa->sin.sin_family != AF_INET || 1082170613Sbms ssa->sin.sin_len != sizeof(struct sockaddr_in)) 1083170613Sbms return (EINVAL); 1084170613Sbms ssa->sin.sin_port = 0; 1085170613Sbms } 1086170613Sbms 1087170613Sbms /* 1088170613Sbms * Obtain the ifp. 1089170613Sbms */ 1090170613Sbms if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface) 1091170613Sbms return (EADDRNOTAVAIL); 1092170613Sbms ifp = ifnet_byindex(gsr.gsr_interface); 1093170613Sbms 1094170613Sbms break; 1095170613Sbms 1096170613Sbms default: 1097170613Sbms#ifdef DIAGNOSTIC 1098170613Sbms if (bootverbose) { 1099170613Sbms printf("%s: unknown sopt_name %d\n", __func__, 1100170613Sbms sopt->sopt_name); 1101170613Sbms } 1102170613Sbms#endif 1103170613Sbms return (EOPNOTSUPP); 1104170613Sbms break; 1105170613Sbms } 1106170613Sbms 1107170613Sbms if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 1108170613Sbms return (EINVAL); 1109170613Sbms 1110170613Sbms if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) 1111170613Sbms return (EADDRNOTAVAIL); 1112170613Sbms 1113170613Sbms /* 1114170613Sbms * Check if we already hold membership of this group for this inpcb. 1115170613Sbms * If so, we do not need to perform the initial join. 1116170613Sbms */ 1117170613Sbms imo = inp_findmoptions(inp); 1118170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 1119170613Sbms if (idx != -1) { 1120170613Sbms if (ssa->ss.ss_family != AF_UNSPEC) { 1121170613Sbms /* 1122170613Sbms * Attempting to join an ASM group (when already 1123170613Sbms * an ASM or SSM member) is an error. 1124170613Sbms */ 1125170613Sbms error = EADDRNOTAVAIL; 1126170613Sbms } else { 1127170613Sbms imf = &imo->imo_mfilters[idx]; 1128170613Sbms if (imf->imf_nsources == 0) { 1129170613Sbms /* 1130170613Sbms * Attempting to join an SSM group (when 1131170613Sbms * already an ASM member) is an error. 1132170613Sbms */ 1133170613Sbms error = EINVAL; 1134170613Sbms } else { 1135170613Sbms /* 1136170613Sbms * Attempting to join an SSM group (when 1137170613Sbms * already an SSM member) means "add this 1138170613Sbms * source to the inclusive filter list". 1139170613Sbms */ 1140170613Sbms error = imo_join_source(imo, idx, ssa); 1141170613Sbms } 1142170613Sbms } 1143170613Sbms goto out_locked; 1144170613Sbms } 1145170613Sbms 1146170613Sbms /* 1147170613Sbms * Call imo_grow() to reallocate the membership and source filter 1148170613Sbms * vectors if they are full. If the size would exceed the hard limit, 1149170613Sbms * then we know we've really run out of entries. We keep the INP 1150170613Sbms * lock held to avoid introducing a race condition. 1151170613Sbms */ 1152170613Sbms if (imo->imo_num_memberships == imo->imo_max_memberships) { 1153170613Sbms error = imo_grow(imo); 1154170613Sbms if (error) 1155170613Sbms goto out_locked; 1156170613Sbms } 1157170613Sbms 1158170613Sbms /* 1159170613Sbms * So far, so good: perform the layer 3 join, layer 2 join, 1160170613Sbms * and make an IGMP announcement if needed. 1161170613Sbms */ 1162170613Sbms inm = in_addmulti(&gsa->sin.sin_addr, ifp); 1163170613Sbms if (inm == NULL) { 1164170613Sbms error = ENOBUFS; 1165170613Sbms goto out_locked; 1166170613Sbms } 1167170613Sbms idx = imo->imo_num_memberships; 1168170613Sbms imo->imo_membership[idx] = inm; 1169170613Sbms imo->imo_num_memberships++; 1170170613Sbms 1171170613Sbms KASSERT(imo->imo_mfilters != NULL, 1172170613Sbms ("%s: imf_mfilters vector was not allocated", __func__)); 1173170613Sbms imf = &imo->imo_mfilters[idx]; 1174170613Sbms KASSERT(TAILQ_EMPTY(&imf->imf_sources), 1175170613Sbms ("%s: imf_sources not empty", __func__)); 1176170613Sbms 1177170613Sbms /* 1178170613Sbms * If this is a new SSM group join (i.e. a source was specified 1179170613Sbms * with this group), add this source to the filter list. 1180170613Sbms */ 1181170613Sbms if (ssa->ss.ss_family != AF_UNSPEC) { 1182170613Sbms /* 1183170613Sbms * An initial SSM join implies that this socket's membership 1184170613Sbms * of the multicast group is now in inclusive mode. 1185170613Sbms */ 1186170613Sbms imf->imf_fmode = MCAST_INCLUDE; 1187170613Sbms 1188170613Sbms error = imo_join_source(imo, idx, ssa); 1189170613Sbms if (error) { 1190170613Sbms /* 1191170613Sbms * Drop inp lock before calling in_delmulti(), 1192170613Sbms * to prevent a lock order reversal. 1193170613Sbms */ 1194170613Sbms --imo->imo_num_memberships; 1195170613Sbms INP_UNLOCK(inp); 1196170613Sbms in_delmulti(inm); 1197170613Sbms return (error); 1198170613Sbms } 1199170613Sbms } 1200170613Sbms 1201170613Sbmsout_locked: 1202170613Sbms INP_UNLOCK(inp); 1203170613Sbms return (error); 1204170613Sbms} 1205170613Sbms 1206170613Sbms/* 1207170613Sbms * Leave an IPv4 multicast group on an inpcb, possibly with a source. 1208170613Sbms */ 1209170613Sbmsstatic int 1210170613Sbmsinp_leave_group(struct inpcb *inp, struct sockopt *sopt) 1211170613Sbms{ 1212170613Sbms struct group_source_req gsr; 1213170613Sbms struct ip_mreq_source mreqs; 1214170613Sbms sockunion_t *gsa, *ssa; 1215170613Sbms struct ifnet *ifp; 1216170613Sbms struct in_mfilter *imf; 1217170613Sbms struct ip_moptions *imo; 1218170613Sbms struct in_msource *ims, *tims; 1219170613Sbms struct in_multi *inm; 1220170613Sbms size_t idx; 1221170613Sbms int error; 1222170613Sbms 1223170613Sbms ifp = NULL; 1224170613Sbms error = 0; 1225170613Sbms 1226170613Sbms memset(&gsr, 0, sizeof(struct group_source_req)); 1227170613Sbms gsa = (sockunion_t *)&gsr.gsr_group; 1228170613Sbms gsa->ss.ss_family = AF_UNSPEC; 1229170613Sbms ssa = (sockunion_t *)&gsr.gsr_source; 1230170613Sbms ssa->ss.ss_family = AF_UNSPEC; 1231170613Sbms 1232170613Sbms switch (sopt->sopt_name) { 1233170613Sbms case IP_DROP_MEMBERSHIP: 1234170613Sbms case IP_DROP_SOURCE_MEMBERSHIP: 1235170613Sbms if (sopt->sopt_name == IP_DROP_MEMBERSHIP) { 1236170613Sbms error = sooptcopyin(sopt, &mreqs, 1237170613Sbms sizeof(struct ip_mreq), 1238170613Sbms sizeof(struct ip_mreq)); 1239170613Sbms /* 1240170613Sbms * Swap interface and sourceaddr arguments, 1241170613Sbms * as ip_mreq and ip_mreq_source are laid 1242170613Sbms * out differently. 1243170613Sbms */ 1244170613Sbms mreqs.imr_interface = mreqs.imr_sourceaddr; 1245170613Sbms mreqs.imr_sourceaddr.s_addr = INADDR_ANY; 1246170613Sbms } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { 1247170613Sbms error = sooptcopyin(sopt, &mreqs, 1248170613Sbms sizeof(struct ip_mreq_source), 1249170613Sbms sizeof(struct ip_mreq_source)); 1250170613Sbms } 1251170613Sbms if (error) 1252170613Sbms return (error); 1253170613Sbms 1254170613Sbms gsa->sin.sin_family = AF_INET; 1255170613Sbms gsa->sin.sin_len = sizeof(struct sockaddr_in); 1256170613Sbms gsa->sin.sin_addr = mreqs.imr_multiaddr; 1257170613Sbms 1258170613Sbms if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { 1259170613Sbms ssa->sin.sin_family = AF_INET; 1260170613Sbms ssa->sin.sin_len = sizeof(struct sockaddr_in); 1261170613Sbms ssa->sin.sin_addr = mreqs.imr_sourceaddr; 1262170613Sbms } 1263170613Sbms 1264170613Sbms if (gsa->sin.sin_addr.s_addr != INADDR_ANY) 1265170613Sbms INADDR_TO_IFP(mreqs.imr_interface, ifp); 1266170613Sbms 1267170613Sbms#ifdef DIAGNOSTIC 1268170613Sbms if (bootverbose) { 1269170613Sbms printf("%s: imr_interface = %s, ifp = %p\n", 1270170613Sbms __func__, inet_ntoa(mreqs.imr_interface), ifp); 1271170613Sbms } 1272170613Sbms#endif 1273170613Sbms break; 1274170613Sbms 1275170613Sbms case MCAST_LEAVE_GROUP: 1276170613Sbms case MCAST_LEAVE_SOURCE_GROUP: 1277170613Sbms if (sopt->sopt_name == MCAST_LEAVE_GROUP) { 1278170613Sbms error = sooptcopyin(sopt, &gsr, 1279170613Sbms sizeof(struct group_req), 1280170613Sbms sizeof(struct group_req)); 1281170613Sbms } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { 1282170613Sbms error = sooptcopyin(sopt, &gsr, 1283170613Sbms sizeof(struct group_source_req), 1284170613Sbms sizeof(struct group_source_req)); 1285170613Sbms } 1286170613Sbms if (error) 1287170613Sbms return (error); 1288170613Sbms 1289170613Sbms if (gsa->sin.sin_family != AF_INET || 1290170613Sbms gsa->sin.sin_len != sizeof(struct sockaddr_in)) 1291170613Sbms return (EINVAL); 1292170613Sbms 1293170613Sbms if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { 1294170613Sbms if (ssa->sin.sin_family != AF_INET || 1295170613Sbms ssa->sin.sin_len != sizeof(struct sockaddr_in)) 1296170613Sbms return (EINVAL); 1297170613Sbms } 1298170613Sbms 1299170613Sbms if (gsr.gsr_interface == 0 || if_index < gsr.gsr_interface) 1300170613Sbms return (EADDRNOTAVAIL); 1301170613Sbms 1302170613Sbms ifp = ifnet_byindex(gsr.gsr_interface); 1303170613Sbms break; 1304170613Sbms 1305170613Sbms default: 1306170613Sbms#ifdef DIAGNOSTIC 1307170613Sbms if (bootverbose) { 1308170613Sbms printf("%s: unknown sopt_name %d\n", __func__, 1309170613Sbms sopt->sopt_name); 1310170613Sbms } 1311170613Sbms#endif 1312170613Sbms return (EOPNOTSUPP); 1313170613Sbms break; 1314170613Sbms } 1315170613Sbms 1316170613Sbms if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 1317170613Sbms return (EINVAL); 1318170613Sbms 1319170613Sbms /* 1320170613Sbms * Find the membership in the membership array. 1321170613Sbms */ 1322170613Sbms imo = inp_findmoptions(inp); 1323170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 1324170613Sbms if (idx == -1) { 1325170613Sbms error = EADDRNOTAVAIL; 1326170613Sbms goto out_locked; 1327170613Sbms } 1328170613Sbms imf = &imo->imo_mfilters[idx]; 1329170613Sbms 1330170613Sbms /* 1331170613Sbms * If we were instructed only to leave a given source, do so. 1332170613Sbms */ 1333170613Sbms if (ssa->ss.ss_family != AF_UNSPEC) { 1334170613Sbms if (imf->imf_nsources == 0 || 1335170613Sbms imf->imf_fmode == MCAST_EXCLUDE) { 1336170613Sbms /* 1337170613Sbms * Attempting to SSM leave an ASM group 1338170613Sbms * is an error; should use *_BLOCK_SOURCE instead. 1339170613Sbms * Attempting to SSM leave a source in a group when 1340170613Sbms * the socket is in 'exclude mode' is also an error. 1341170613Sbms */ 1342170613Sbms error = EINVAL; 1343170613Sbms } else { 1344170613Sbms error = imo_leave_source(imo, idx, ssa); 1345170613Sbms } 1346170613Sbms /* 1347170613Sbms * If an error occurred, or this source is not the last 1348170613Sbms * source in the group, do not leave the whole group. 1349170613Sbms */ 1350170613Sbms if (error || imf->imf_nsources > 0) 1351170613Sbms goto out_locked; 1352170613Sbms } 1353170613Sbms 1354170613Sbms /* 1355170613Sbms * Give up the multicast address record to which the membership points. 1356170613Sbms */ 1357170613Sbms inm = imo->imo_membership[idx]; 1358170613Sbms in_delmulti(inm); 1359170613Sbms 1360170613Sbms /* 1361170613Sbms * Free any source filters for this group if they exist. 1362170613Sbms * Revert inpcb to the default MCAST_EXCLUDE state. 1363170613Sbms */ 1364170613Sbms if (imo->imo_mfilters != NULL) { 1365170613Sbms TAILQ_FOREACH_SAFE(ims, &imf->imf_sources, ims_next, tims) { 1366170613Sbms TAILQ_REMOVE(&imf->imf_sources, ims, ims_next); 1367170613Sbms FREE(ims, M_IPMSOURCE); 1368170613Sbms imf->imf_nsources--; 1369170613Sbms } 1370170613Sbms KASSERT(imf->imf_nsources == 0, 1371170613Sbms ("%s: imf_nsources not 0", __func__)); 1372170613Sbms KASSERT(TAILQ_EMPTY(&imf->imf_sources), 1373170613Sbms ("%s: imf_sources not empty", __func__)); 1374170613Sbms imf->imf_fmode = MCAST_EXCLUDE; 1375170613Sbms } 1376170613Sbms 1377170613Sbms /* 1378170613Sbms * Remove the gap in the membership array. 1379170613Sbms */ 1380170613Sbms for (++idx; idx < imo->imo_num_memberships; ++idx) 1381170613Sbms imo->imo_membership[idx-1] = imo->imo_membership[idx]; 1382170613Sbms imo->imo_num_memberships--; 1383170613Sbms 1384170613Sbmsout_locked: 1385170613Sbms INP_UNLOCK(inp); 1386170613Sbms return (error); 1387170613Sbms} 1388170613Sbms 1389170613Sbms/* 1390170613Sbms * Select the interface for transmitting IPv4 multicast datagrams. 1391170613Sbms * 1392170613Sbms * Either an instance of struct in_addr or an instance of struct ip_mreqn 1393170613Sbms * may be passed to this socket option. An address of INADDR_ANY or an 1394170613Sbms * interface index of 0 is used to remove a previous selection. 1395170613Sbms * When no interface is selected, one is chosen for every send. 1396170613Sbms */ 1397170613Sbmsstatic int 1398170613Sbmsinp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt) 1399170613Sbms{ 1400170613Sbms struct in_addr addr; 1401170613Sbms struct ip_mreqn mreqn; 1402170613Sbms struct ifnet *ifp; 1403170613Sbms struct ip_moptions *imo; 1404170613Sbms int error; 1405170613Sbms 1406170613Sbms if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { 1407170613Sbms /* 1408170613Sbms * An interface index was specified using the 1409170613Sbms * Linux-derived ip_mreqn structure. 1410170613Sbms */ 1411170613Sbms error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn), 1412170613Sbms sizeof(struct ip_mreqn)); 1413170613Sbms if (error) 1414170613Sbms return (error); 1415170613Sbms 1416170613Sbms if (mreqn.imr_ifindex < 0 || if_index < mreqn.imr_ifindex) 1417170613Sbms return (EINVAL); 1418170613Sbms 1419170613Sbms if (mreqn.imr_ifindex == 0) { 1420170613Sbms ifp = NULL; 1421170613Sbms } else { 1422170613Sbms ifp = ifnet_byindex(mreqn.imr_ifindex); 1423170613Sbms if (ifp == NULL) 1424170613Sbms return (EADDRNOTAVAIL); 1425170613Sbms } 1426170613Sbms } else { 1427170613Sbms /* 1428170613Sbms * An interface was specified by IPv4 address. 1429170613Sbms * This is the traditional BSD usage. 1430170613Sbms */ 1431170613Sbms error = sooptcopyin(sopt, &addr, sizeof(struct in_addr), 1432170613Sbms sizeof(struct in_addr)); 1433170613Sbms if (error) 1434170613Sbms return (error); 1435170613Sbms if (addr.s_addr == INADDR_ANY) { 1436170613Sbms ifp = NULL; 1437170613Sbms } else { 1438170613Sbms INADDR_TO_IFP(addr, ifp); 1439170613Sbms if (ifp == NULL) 1440170613Sbms return (EADDRNOTAVAIL); 1441170613Sbms } 1442170613Sbms#ifdef DIAGNOSTIC 1443170613Sbms if (bootverbose) { 1444170613Sbms printf("%s: ifp = %p, addr = %s\n", 1445170613Sbms __func__, ifp, inet_ntoa(addr)); /* XXX INET6 */ 1446170613Sbms } 1447170613Sbms#endif 1448170613Sbms } 1449170613Sbms 1450170613Sbms /* Reject interfaces which do not support multicast. */ 1451170613Sbms if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0) 1452170613Sbms return (EOPNOTSUPP); 1453170613Sbms 1454170613Sbms imo = inp_findmoptions(inp); 1455170613Sbms imo->imo_multicast_ifp = ifp; 1456170613Sbms imo->imo_multicast_addr.s_addr = INADDR_ANY; 1457170613Sbms INP_UNLOCK(inp); 1458170613Sbms 1459170613Sbms return (0); 1460170613Sbms} 1461170613Sbms 1462170613Sbms/* 1463170613Sbms * Atomically set source filters on a socket for an IPv4 multicast group. 1464170613Sbms */ 1465170613Sbmsstatic int 1466170613Sbmsinp_set_source_filters(struct inpcb *inp, struct sockopt *sopt) 1467170613Sbms{ 1468170613Sbms struct __msfilterreq msfr; 1469170613Sbms sockunion_t *gsa; 1470170613Sbms struct ifnet *ifp; 1471170613Sbms struct in_mfilter *imf; 1472170613Sbms struct ip_moptions *imo; 1473170613Sbms struct in_msource *ims, *tims; 1474170613Sbms size_t idx; 1475170613Sbms int error; 1476170613Sbms 1477170613Sbms error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq), 1478170613Sbms sizeof(struct __msfilterreq)); 1479170613Sbms if (error) 1480170613Sbms return (error); 1481170613Sbms 1482170613Sbms if (msfr.msfr_nsrcs > IP_MAX_SOURCE_FILTER || 1483170613Sbms (msfr.msfr_fmode != MCAST_EXCLUDE && 1484170613Sbms msfr.msfr_fmode != MCAST_INCLUDE)) 1485170613Sbms return (EINVAL); 1486170613Sbms 1487170613Sbms if (msfr.msfr_group.ss_family != AF_INET || 1488170613Sbms msfr.msfr_group.ss_len != sizeof(struct sockaddr_in)) 1489170613Sbms return (EINVAL); 1490170613Sbms 1491170613Sbms gsa = (sockunion_t *)&msfr.msfr_group; 1492170613Sbms if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 1493170613Sbms return (EINVAL); 1494170613Sbms 1495170613Sbms gsa->sin.sin_port = 0; /* ignore port */ 1496170613Sbms 1497170613Sbms if (msfr.msfr_ifindex == 0 || if_index < msfr.msfr_ifindex) 1498170613Sbms return (EADDRNOTAVAIL); 1499170613Sbms 1500170613Sbms ifp = ifnet_byindex(msfr.msfr_ifindex); 1501170613Sbms if (ifp == NULL) 1502170613Sbms return (EADDRNOTAVAIL); 1503170613Sbms 1504170613Sbms /* 1505170613Sbms * Take the INP lock. 1506170613Sbms * Check if this socket is a member of this group. 1507170613Sbms */ 1508170613Sbms imo = inp_findmoptions(inp); 1509170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 1510170613Sbms if (idx == -1 || imo->imo_mfilters == NULL) { 1511170613Sbms error = EADDRNOTAVAIL; 1512170613Sbms goto out_locked; 1513170613Sbms } 1514170613Sbms imf = &imo->imo_mfilters[idx]; 1515170613Sbms 1516170613Sbms#ifdef DIAGNOSTIC 1517170613Sbms if (bootverbose) 1518170613Sbms printf("%s: clearing source list\n", __func__); 1519170613Sbms#endif 1520170613Sbms 1521170613Sbms /* 1522170613Sbms * Remove any existing source filters. 1523170613Sbms */ 1524170613Sbms TAILQ_FOREACH_SAFE(ims, &imf->imf_sources, ims_next, tims) { 1525170613Sbms TAILQ_REMOVE(&imf->imf_sources, ims, ims_next); 1526170613Sbms FREE(ims, M_IPMSOURCE); 1527170613Sbms imf->imf_nsources--; 1528170613Sbms } 1529170613Sbms KASSERT(imf->imf_nsources == 0, 1530170613Sbms ("%s: source list not cleared", __func__)); 1531170613Sbms 1532170613Sbms /* 1533170613Sbms * Apply any new source filters, if present. 1534170613Sbms */ 1535170613Sbms if (msfr.msfr_nsrcs > 0) { 1536170613Sbms struct in_msource **pnims; 1537170613Sbms struct in_msource *nims; 1538170613Sbms struct sockaddr_storage *kss; 1539170613Sbms struct sockaddr_storage *pkss; 1540170613Sbms sockunion_t *psu; 1541170613Sbms int i, j; 1542170613Sbms 1543170613Sbms /* 1544170613Sbms * Drop the inp lock so we may sleep if we need to 1545170613Sbms * in order to satisfy a malloc request. 1546170613Sbms * We will re-take it before changing socket state. 1547170613Sbms */ 1548170613Sbms INP_UNLOCK(inp); 1549170613Sbms#ifdef DIAGNOSTIC 1550170613Sbms if (bootverbose) { 1551170613Sbms printf("%s: loading %lu source list entries\n", 1552170613Sbms __func__, (unsigned long)msfr.msfr_nsrcs); 1553170613Sbms } 1554170613Sbms#endif 1555170613Sbms /* 1556170613Sbms * Make a copy of the user-space source vector so 1557170613Sbms * that we may copy them with a single copyin. This 1558170613Sbms * allows us to deal with page faults up-front. 1559170613Sbms */ 1560170613Sbms MALLOC(kss, struct sockaddr_storage *, 1561170613Sbms sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs, 1562170613Sbms M_TEMP, M_WAITOK); 1563170613Sbms error = copyin(msfr.msfr_srcs, kss, 1564170613Sbms sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs); 1565170613Sbms if (error) { 1566170613Sbms FREE(kss, M_TEMP); 1567170613Sbms return (error); 1568170613Sbms } 1569170613Sbms 1570170613Sbms /* 1571170613Sbms * Perform argument checking on every sockaddr_storage 1572170613Sbms * structure in the vector provided to us. Overwrite 1573170613Sbms * fields which should not apply to source entries. 1574170613Sbms * TODO: Check for duplicate sources on this pass. 1575170613Sbms */ 1576170613Sbms psu = (sockunion_t *)kss; 1577170613Sbms for (i = 0; i < msfr.msfr_nsrcs; i++, psu++) { 1578170613Sbms switch (psu->ss.ss_family) { 1579170613Sbms case AF_INET: 1580170613Sbms if (psu->sin.sin_len != 1581170613Sbms sizeof(struct sockaddr_in)) { 1582170613Sbms error = EINVAL; 1583170613Sbms } else { 1584170613Sbms psu->sin.sin_port = 0; 1585170613Sbms } 1586170613Sbms break; 1587170613Sbms#ifdef notyet 1588170613Sbms case AF_INET6; 1589170613Sbms if (psu->sin6.sin6_len != 1590170613Sbms sizeof(struct sockaddr_in6)) { 1591170613Sbms error = EINVAL; 1592170613Sbms } else { 1593170613Sbms psu->sin6.sin6_port = 0; 1594170613Sbms psu->sin6.sin6_flowinfo = 0; 1595170613Sbms } 1596170613Sbms break; 1597170613Sbms#endif 1598170613Sbms default: 1599170613Sbms error = EAFNOSUPPORT; 1600170613Sbms break; 1601170613Sbms } 1602170613Sbms if (error) 1603170613Sbms break; 1604170613Sbms } 1605170613Sbms if (error) { 1606170613Sbms FREE(kss, M_TEMP); 1607170613Sbms return (error); 1608170613Sbms } 1609170613Sbms 1610170613Sbms /* 1611170613Sbms * Allocate a block to track all the in_msource 1612170613Sbms * entries we are about to allocate, in case we 1613170613Sbms * abruptly need to free them. 1614170613Sbms */ 1615170613Sbms MALLOC(pnims, struct in_msource **, 1616170613Sbms sizeof(struct in_msource *) * msfr.msfr_nsrcs, 1617170613Sbms M_TEMP, M_WAITOK | M_ZERO); 1618170613Sbms 1619170613Sbms /* 1620170613Sbms * Allocate up to nsrcs individual chunks. 1621170613Sbms * If we encounter an error, backtrack out of 1622170613Sbms * all allocations cleanly; updates must be atomic. 1623170613Sbms */ 1624170613Sbms pkss = kss; 1625170613Sbms nims = NULL; 1626170613Sbms for (i = 0; i < msfr.msfr_nsrcs; i++, pkss++) { 1627170613Sbms MALLOC(nims, struct in_msource *, 1628170613Sbms sizeof(struct in_msource) * msfr.msfr_nsrcs, 1629170613Sbms M_IPMSOURCE, M_WAITOK | M_ZERO); 1630170613Sbms pnims[i] = nims; 1631170613Sbms } 1632170613Sbms if (i < msfr.msfr_nsrcs) { 1633170613Sbms for (j = 0; j < i; j++) { 1634170613Sbms if (pnims[j] != NULL) 1635170613Sbms FREE(pnims[j], M_IPMSOURCE); 1636170613Sbms } 1637170613Sbms FREE(pnims, M_TEMP); 1638170613Sbms FREE(kss, M_TEMP); 1639170613Sbms return (ENOBUFS); 1640170613Sbms } 1641170613Sbms 1642170613Sbms INP_UNLOCK_ASSERT(inp); 1643170613Sbms 1644170613Sbms /* 1645170613Sbms * Finally, apply the filters to the socket. 1646170613Sbms * Re-take the inp lock; we are changing socket state. 1647170613Sbms */ 1648170613Sbms pkss = kss; 1649170613Sbms INP_LOCK(inp); 1650170613Sbms for (i = 0; i < msfr.msfr_nsrcs; i++, pkss++) { 1651170613Sbms memcpy(&(pnims[i]->ims_addr), pkss, 1652170613Sbms sizeof(struct sockaddr_storage)); 1653170613Sbms TAILQ_INSERT_TAIL(&imf->imf_sources, pnims[i], 1654170613Sbms ims_next); 1655170613Sbms imf->imf_nsources++; 1656170613Sbms } 1657170613Sbms FREE(pnims, M_TEMP); 1658170613Sbms FREE(kss, M_TEMP); 1659170613Sbms } 1660170613Sbms 1661170613Sbms /* 1662170613Sbms * Update the filter mode on the socket before releasing the inpcb. 1663170613Sbms */ 1664170613Sbms INP_LOCK_ASSERT(inp); 1665170613Sbms imf->imf_fmode = msfr.msfr_fmode; 1666170613Sbms 1667170613Sbmsout_locked: 1668170613Sbms INP_UNLOCK(inp); 1669170613Sbms return (error); 1670170613Sbms} 1671170613Sbms 1672170613Sbms/* 1673170613Sbms * Set the IP multicast options in response to user setsockopt(). 1674170613Sbms * 1675170613Sbms * Many of the socket options handled in this function duplicate the 1676170613Sbms * functionality of socket options in the regular unicast API. However, 1677170613Sbms * it is not possible to merge the duplicate code, because the idempotence 1678170613Sbms * of the IPv4 multicast part of the BSD Sockets API must be preserved; 1679170613Sbms * the effects of these options must be treated as separate and distinct. 1680170613Sbms */ 1681170613Sbmsint 1682170613Sbmsinp_setmoptions(struct inpcb *inp, struct sockopt *sopt) 1683170613Sbms{ 1684170613Sbms struct ip_moptions *imo; 1685170613Sbms int error; 1686170613Sbms 1687170613Sbms error = 0; 1688170613Sbms 1689171746Scsjp /* 1690171746Scsjp * If socket is neither of type SOCK_RAW or SOCK_DGRAM, 1691171746Scsjp * or is a divert socket, reject it. 1692171746Scsjp * XXX Unlocked read of inp_socket believed OK. 1693171746Scsjp */ 1694171746Scsjp if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT || 1695171746Scsjp (inp->inp_socket->so_proto->pr_type != SOCK_RAW && 1696171746Scsjp inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) 1697171746Scsjp return (EOPNOTSUPP); 1698171746Scsjp 1699170613Sbms switch (sopt->sopt_name) { 1700170613Sbms case IP_MULTICAST_VIF: { 1701170613Sbms int vifi; 1702170613Sbms /* 1703170613Sbms * Select a multicast VIF for transmission. 1704170613Sbms * Only useful if multicast forwarding is active. 1705170613Sbms */ 1706170613Sbms if (legal_vif_num == NULL) { 1707170613Sbms error = EOPNOTSUPP; 1708170613Sbms break; 1709170613Sbms } 1710170613Sbms error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int)); 1711170613Sbms if (error) 1712170613Sbms break; 1713170613Sbms if (!legal_vif_num(vifi) && (vifi != -1)) { 1714170613Sbms error = EINVAL; 1715170613Sbms break; 1716170613Sbms } 1717170613Sbms imo = inp_findmoptions(inp); 1718170613Sbms imo->imo_multicast_vif = vifi; 1719170613Sbms INP_UNLOCK(inp); 1720170613Sbms break; 1721170613Sbms } 1722170613Sbms 1723170613Sbms case IP_MULTICAST_IF: 1724170613Sbms error = inp_set_multicast_if(inp, sopt); 1725170613Sbms break; 1726170613Sbms 1727170613Sbms case IP_MULTICAST_TTL: { 1728170613Sbms u_char ttl; 1729170613Sbms 1730170613Sbms /* 1731170613Sbms * Set the IP time-to-live for outgoing multicast packets. 1732170613Sbms * The original multicast API required a char argument, 1733170613Sbms * which is inconsistent with the rest of the socket API. 1734170613Sbms * We allow either a char or an int. 1735170613Sbms */ 1736170613Sbms if (sopt->sopt_valsize == sizeof(u_char)) { 1737170613Sbms error = sooptcopyin(sopt, &ttl, sizeof(u_char), 1738170613Sbms sizeof(u_char)); 1739170613Sbms if (error) 1740170613Sbms break; 1741170613Sbms } else { 1742170613Sbms u_int ittl; 1743170613Sbms 1744170613Sbms error = sooptcopyin(sopt, &ittl, sizeof(u_int), 1745170613Sbms sizeof(u_int)); 1746170613Sbms if (error) 1747170613Sbms break; 1748170613Sbms if (ittl > 255) { 1749170613Sbms error = EINVAL; 1750170613Sbms break; 1751170613Sbms } 1752170613Sbms ttl = (u_char)ittl; 1753170613Sbms } 1754170613Sbms imo = inp_findmoptions(inp); 1755170613Sbms imo->imo_multicast_ttl = ttl; 1756170613Sbms INP_UNLOCK(inp); 1757170613Sbms break; 1758170613Sbms } 1759170613Sbms 1760170613Sbms case IP_MULTICAST_LOOP: { 1761170613Sbms u_char loop; 1762170613Sbms 1763170613Sbms /* 1764170613Sbms * Set the loopback flag for outgoing multicast packets. 1765170613Sbms * Must be zero or one. The original multicast API required a 1766170613Sbms * char argument, which is inconsistent with the rest 1767170613Sbms * of the socket API. We allow either a char or an int. 1768170613Sbms */ 1769170613Sbms if (sopt->sopt_valsize == sizeof(u_char)) { 1770170613Sbms error = sooptcopyin(sopt, &loop, sizeof(u_char), 1771170613Sbms sizeof(u_char)); 1772170613Sbms if (error) 1773170613Sbms break; 1774170613Sbms } else { 1775170613Sbms u_int iloop; 1776170613Sbms 1777170613Sbms error = sooptcopyin(sopt, &iloop, sizeof(u_int), 1778170613Sbms sizeof(u_int)); 1779170613Sbms if (error) 1780170613Sbms break; 1781170613Sbms loop = (u_char)iloop; 1782170613Sbms } 1783170613Sbms imo = inp_findmoptions(inp); 1784170613Sbms imo->imo_multicast_loop = !!loop; 1785170613Sbms INP_UNLOCK(inp); 1786170613Sbms break; 1787170613Sbms } 1788170613Sbms 1789170613Sbms case IP_ADD_MEMBERSHIP: 1790170613Sbms case IP_ADD_SOURCE_MEMBERSHIP: 1791170613Sbms case MCAST_JOIN_GROUP: 1792170613Sbms case MCAST_JOIN_SOURCE_GROUP: 1793170613Sbms error = inp_join_group(inp, sopt); 1794170613Sbms break; 1795170613Sbms 1796170613Sbms case IP_DROP_MEMBERSHIP: 1797170613Sbms case IP_DROP_SOURCE_MEMBERSHIP: 1798170613Sbms case MCAST_LEAVE_GROUP: 1799170613Sbms case MCAST_LEAVE_SOURCE_GROUP: 1800170613Sbms error = inp_leave_group(inp, sopt); 1801170613Sbms break; 1802170613Sbms 1803170613Sbms case IP_BLOCK_SOURCE: 1804170613Sbms case IP_UNBLOCK_SOURCE: 1805170613Sbms case MCAST_BLOCK_SOURCE: 1806170613Sbms case MCAST_UNBLOCK_SOURCE: 1807170613Sbms error = inp_change_source_filter(inp, sopt); 1808170613Sbms break; 1809170613Sbms 1810170613Sbms case IP_MSFILTER: 1811170613Sbms error = inp_set_source_filters(inp, sopt); 1812170613Sbms break; 1813170613Sbms 1814170613Sbms default: 1815170613Sbms error = EOPNOTSUPP; 1816170613Sbms break; 1817170613Sbms } 1818170613Sbms 1819170613Sbms INP_UNLOCK_ASSERT(inp); 1820170613Sbms 1821170613Sbms return (error); 1822170613Sbms} 1823