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