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