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