in_mcast.c revision 189931
1170613Sbms/*- 2189592Sbms * Copyright (c) 2007-2009 Bruce 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 */ 34170613Sbms 35170613Sbms#include <sys/cdefs.h> 36170613Sbms__FBSDID("$FreeBSD: head/sys/netinet/in_mcast.c 189931 2009-03-17 14:41:54Z bms $"); 37170613Sbms 38189106Sbz#include "opt_route.h" 39189106Sbz 40170613Sbms#include <sys/param.h> 41170613Sbms#include <sys/systm.h> 42170613Sbms#include <sys/kernel.h> 43170613Sbms#include <sys/malloc.h> 44170613Sbms#include <sys/mbuf.h> 45171746Scsjp#include <sys/protosw.h> 46170613Sbms#include <sys/socket.h> 47170613Sbms#include <sys/socketvar.h> 48189592Sbms#include <sys/protosw.h> 49170613Sbms#include <sys/sysctl.h> 50181803Sbz#include <sys/vimage.h> 51189592Sbms#include <sys/ktr.h> 52189592Sbms#include <sys/tree.h> 53170613Sbms 54170613Sbms#include <net/if.h> 55170613Sbms#include <net/if_dl.h> 56170613Sbms#include <net/route.h> 57185571Sbz#include <net/vnet.h> 58170613Sbms 59170613Sbms#include <netinet/in.h> 60170613Sbms#include <netinet/in_systm.h> 61170613Sbms#include <netinet/in_pcb.h> 62170613Sbms#include <netinet/in_var.h> 63170613Sbms#include <netinet/ip_var.h> 64170613Sbms#include <netinet/igmp_var.h> 65185571Sbz#include <netinet/vinet.h> 66170613Sbms 67189592Sbms#ifndef KTR_IGMPV3 68189592Sbms#define KTR_IGMPV3 KTR_SUBSYS 69189592Sbms#endif 70189592Sbms 71170613Sbms#ifndef __SOCKUNION_DECLARED 72170613Sbmsunion sockunion { 73170613Sbms struct sockaddr_storage ss; 74170613Sbms struct sockaddr sa; 75170613Sbms struct sockaddr_dl sdl; 76170613Sbms struct sockaddr_in sin; 77170613Sbms}; 78170613Sbmstypedef union sockunion sockunion_t; 79170613Sbms#define __SOCKUNION_DECLARED 80170613Sbms#endif /* __SOCKUNION_DECLARED */ 81170613Sbms 82189592Sbmsstatic MALLOC_DEFINE(M_INMFILTER, "in_mfilter", 83189592Sbms "IPv4 multicast PCB-layer source filter"); 84170613Sbmsstatic MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group"); 85170613Sbmsstatic MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options"); 86189592Sbmsstatic MALLOC_DEFINE(M_IPMSOURCE, "ip_msource", 87189592Sbms "IPv4 multicast IGMP-layer source filter"); 88170613Sbms 89189592Sbms#ifdef VIMAGE_GLOBALS 90189592Sbmsstruct in_multihead in_multihead; /* XXX now unused; retain for ABI */ 91189592Sbms#endif 92189592Sbms 93170613Sbms/* 94189592Sbms * Locking: 95189592Sbms * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK. 96189592Sbms * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however 97189592Sbms * it can be taken by code in net/if.c also. 98189592Sbms * - ip_moptions and in_mfilter are covered by the INP_WLOCK. 99189592Sbms * 100189592Sbms * struct in_multi is covered by IN_MULTI_LOCK. There isn't strictly 101189592Sbms * any need for in_multi itself to be virtualized -- it is bound to an ifp 102189592Sbms * anyway no matter what happens. 103170613Sbms */ 104170613Sbmsstruct mtx in_multi_mtx; 105189592SbmsMTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF); 106170613Sbms 107170613Sbms/* 108170613Sbms * Functions with non-static linkage defined in this file should be 109170613Sbms * declared in in_var.h: 110189592Sbms * imo_multi_filter() 111170613Sbms * in_addmulti() 112170613Sbms * in_delmulti() 113189592Sbms * in_joingroup() 114189592Sbms * in_joingroup_locked() 115189592Sbms * in_leavegroup() 116189592Sbms * in_leavegroup_locked() 117170613Sbms * and ip_var.h: 118170613Sbms * inp_freemoptions() 119170613Sbms * inp_getmoptions() 120170613Sbms * inp_setmoptions() 121189592Sbms * 122189592Sbms * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti() 123189592Sbms * and in_delmulti(). 124170613Sbms */ 125189592Sbmsstatic void imf_commit(struct in_mfilter *); 126189592Sbmsstatic int imf_get_source(struct in_mfilter *imf, 127189592Sbms const struct sockaddr_in *psin, 128189592Sbms struct in_msource **); 129189592Sbmsstatic struct in_msource * 130189592Sbms imf_graft(struct in_mfilter *, const uint8_t, 131189592Sbms const struct sockaddr_in *); 132189592Sbmsstatic void imf_leave(struct in_mfilter *); 133189592Sbmsstatic int imf_prune(struct in_mfilter *, const struct sockaddr_in *); 134189592Sbmsstatic void imf_purge(struct in_mfilter *); 135189592Sbmsstatic void imf_rollback(struct in_mfilter *); 136189592Sbmsstatic void imf_reap(struct in_mfilter *); 137170613Sbmsstatic int imo_grow(struct ip_moptions *); 138189592Sbmsstatic size_t imo_match_group(const struct ip_moptions *, 139189592Sbms const struct ifnet *, const struct sockaddr *); 140189592Sbmsstatic struct in_msource * 141189592Sbms imo_match_source(const struct ip_moptions *, const size_t, 142189592Sbms const struct sockaddr *); 143189592Sbmsstatic void ims_merge(struct ip_msource *ims, 144189592Sbms const struct in_msource *lims, const int rollback); 145189592Sbmsstatic int in_getmulti(struct ifnet *, const struct in_addr *, 146189592Sbms struct in_multi **); 147189592Sbmsstatic int inm_get_source(struct in_multi *inm, const in_addr_t haddr, 148189592Sbms const int noalloc, struct ip_msource **pims); 149189592Sbmsstatic int inm_is_ifp_detached(const struct in_multi *); 150189592Sbmsstatic int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *); 151189592Sbmsstatic void inm_purge(struct in_multi *); 152189592Sbmsstatic void inm_reap(struct in_multi *); 153170613Sbmsstatic struct ip_moptions * 154170613Sbms inp_findmoptions(struct inpcb *); 155170613Sbmsstatic int inp_get_source_filters(struct inpcb *, struct sockopt *); 156170613Sbmsstatic int inp_join_group(struct inpcb *, struct sockopt *); 157170613Sbmsstatic int inp_leave_group(struct inpcb *, struct sockopt *); 158189592Sbmsstatic struct ifnet * 159189592Sbms inp_lookup_mcast_ifp(const struct inpcb *, 160189592Sbms const struct sockaddr_in *, const struct in_addr); 161189592Sbmsstatic int inp_block_unblock_source(struct inpcb *, struct sockopt *); 162170613Sbmsstatic int inp_set_multicast_if(struct inpcb *, struct sockopt *); 163170613Sbmsstatic int inp_set_source_filters(struct inpcb *, struct sockopt *); 164189592Sbmsstatic int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS); 165170613Sbms 166189357SbmsSYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0, "IPv4 multicast"); 167189357Sbms 168189592Sbmsstatic u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER; 169189592SbmsSYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc, 170189592Sbms CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxgrpsrc, 0, 171189592Sbms "Max source filters per group"); 172189592SbmsTUNABLE_ULONG("net.inet.ip.mcast.maxgrpsrc", &in_mcast_maxgrpsrc); 173189592Sbms 174189592Sbmsstatic u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER; 175189592SbmsSYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc, 176189592Sbms CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxsocksrc, 0, 177189592Sbms "Max source filters per socket"); 178189592SbmsTUNABLE_ULONG("net.inet.ip.mcast.maxsocksrc", &in_mcast_maxsocksrc); 179189592Sbms 180189357Sbmsint in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP; 181189357SbmsSYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN, 182189357Sbms &in_mcast_loop, 0, "Loopback multicast datagrams by default"); 183189357SbmsTUNABLE_INT("net.inet.ip.mcast.loop", &in_mcast_loop); 184189357Sbms 185189592SbmsSYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters, 186189592Sbms CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters, 187189592Sbms "Per-interface stack-wide source filters"); 188189592Sbms 189170613Sbms/* 190189592Sbms * Inline function which wraps assertions for a valid ifp. 191189592Sbms * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp 192189592Sbms * is detached. 193189592Sbms */ 194189592Sbmsstatic int __inline 195189592Sbmsinm_is_ifp_detached(const struct in_multi *inm) 196189592Sbms{ 197189592Sbms struct ifnet *ifp; 198189592Sbms 199189592Sbms KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__)); 200189592Sbms ifp = inm->inm_ifma->ifma_ifp; 201189592Sbms if (ifp != NULL) { 202189592Sbms /* 203189592Sbms * Sanity check that netinet's notion of ifp is the 204189592Sbms * same as net's. 205189592Sbms */ 206189592Sbms KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__)); 207189592Sbms } 208189592Sbms 209189592Sbms return (ifp == NULL); 210189592Sbms} 211189592Sbms 212189592Sbms/* 213189592Sbms * Initialize an in_mfilter structure to a known state at t0, t1 214189592Sbms * with an empty source filter list. 215189592Sbms */ 216189592Sbmsstatic __inline void 217189592Sbmsimf_init(struct in_mfilter *imf, const int st0, const int st1) 218189592Sbms{ 219189592Sbms memset(imf, 0, sizeof(struct in_mfilter)); 220189592Sbms RB_INIT(&imf->imf_sources); 221189592Sbms imf->imf_st[0] = st0; 222189592Sbms imf->imf_st[1] = st1; 223189592Sbms} 224189592Sbms 225189592Sbms/* 226170613Sbms * Resize the ip_moptions vector to the next power-of-two minus 1. 227170613Sbms * May be called with locks held; do not sleep. 228170613Sbms */ 229170613Sbmsstatic int 230170613Sbmsimo_grow(struct ip_moptions *imo) 231170613Sbms{ 232170613Sbms struct in_multi **nmships; 233170613Sbms struct in_multi **omships; 234170613Sbms struct in_mfilter *nmfilters; 235170613Sbms struct in_mfilter *omfilters; 236170613Sbms size_t idx; 237170613Sbms size_t newmax; 238170613Sbms size_t oldmax; 239170613Sbms 240170613Sbms nmships = NULL; 241170613Sbms nmfilters = NULL; 242170613Sbms omships = imo->imo_membership; 243170613Sbms omfilters = imo->imo_mfilters; 244170613Sbms oldmax = imo->imo_max_memberships; 245170613Sbms newmax = ((oldmax + 1) * 2) - 1; 246170613Sbms 247170613Sbms if (newmax <= IP_MAX_MEMBERSHIPS) { 248170613Sbms nmships = (struct in_multi **)realloc(omships, 249170613Sbms sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT); 250170613Sbms nmfilters = (struct in_mfilter *)realloc(omfilters, 251189592Sbms sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT); 252170613Sbms if (nmships != NULL && nmfilters != NULL) { 253170613Sbms /* Initialize newly allocated source filter heads. */ 254170613Sbms for (idx = oldmax; idx < newmax; idx++) { 255189592Sbms imf_init(&nmfilters[idx], MCAST_UNDEFINED, 256189592Sbms MCAST_EXCLUDE); 257170613Sbms } 258170613Sbms imo->imo_max_memberships = newmax; 259170613Sbms imo->imo_membership = nmships; 260170613Sbms imo->imo_mfilters = nmfilters; 261170613Sbms } 262170613Sbms } 263170613Sbms 264170613Sbms if (nmships == NULL || nmfilters == NULL) { 265170613Sbms if (nmships != NULL) 266170613Sbms free(nmships, M_IPMOPTS); 267170613Sbms if (nmfilters != NULL) 268189592Sbms free(nmfilters, M_INMFILTER); 269170613Sbms return (ETOOMANYREFS); 270170613Sbms } 271170613Sbms 272170613Sbms return (0); 273170613Sbms} 274170613Sbms 275170613Sbms/* 276170613Sbms * Find an IPv4 multicast group entry for this ip_moptions instance 277170613Sbms * which matches the specified group, and optionally an interface. 278170613Sbms * Return its index into the array, or -1 if not found. 279170613Sbms */ 280189592Sbmsstatic size_t 281189592Sbmsimo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp, 282189592Sbms const struct sockaddr *group) 283170613Sbms{ 284189592Sbms const struct sockaddr_in *gsin; 285170613Sbms struct in_multi **pinm; 286170613Sbms int idx; 287170613Sbms int nmships; 288170613Sbms 289189592Sbms gsin = (const struct sockaddr_in *)group; 290170613Sbms 291170613Sbms /* The imo_membership array may be lazy allocated. */ 292170613Sbms if (imo->imo_membership == NULL || imo->imo_num_memberships == 0) 293170613Sbms return (-1); 294170613Sbms 295170613Sbms nmships = imo->imo_num_memberships; 296170613Sbms pinm = &imo->imo_membership[0]; 297170613Sbms for (idx = 0; idx < nmships; idx++, pinm++) { 298170613Sbms if (*pinm == NULL) 299170613Sbms continue; 300170613Sbms if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) && 301189592Sbms in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) { 302170613Sbms break; 303170613Sbms } 304170613Sbms } 305170613Sbms if (idx >= nmships) 306170613Sbms idx = -1; 307170613Sbms 308170613Sbms return (idx); 309170613Sbms} 310170613Sbms 311170613Sbms/* 312189592Sbms * Find an IPv4 multicast source entry for this imo which matches 313170613Sbms * the given group index for this socket, and source address. 314189592Sbms * 315189592Sbms * NOTE: This does not check if the entry is in-mode, merely if 316189592Sbms * it exists, which may not be the desired behaviour. 317170613Sbms */ 318189592Sbmsstatic struct in_msource * 319189592Sbmsimo_match_source(const struct ip_moptions *imo, const size_t gidx, 320189592Sbms const struct sockaddr *src) 321170613Sbms{ 322189592Sbms struct ip_msource find; 323170613Sbms struct in_mfilter *imf; 324189592Sbms struct ip_msource *ims; 325189592Sbms const sockunion_t *psa; 326170613Sbms 327170613Sbms KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__)); 328170613Sbms KASSERT(gidx != -1 && gidx < imo->imo_num_memberships, 329170613Sbms ("%s: invalid index %d\n", __func__, (int)gidx)); 330170613Sbms 331170613Sbms /* The imo_mfilters array may be lazy allocated. */ 332170613Sbms if (imo->imo_mfilters == NULL) 333170613Sbms return (NULL); 334170613Sbms imf = &imo->imo_mfilters[gidx]; 335170613Sbms 336189592Sbms /* Source trees are keyed in host byte order. */ 337189592Sbms psa = (const sockunion_t *)src; 338189592Sbms find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr); 339189592Sbms ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); 340189592Sbms 341189592Sbms return ((struct in_msource *)ims); 342170613Sbms} 343170613Sbms 344170613Sbms/* 345189592Sbms * Perform filtering for multicast datagrams on a socket by group and source. 346189592Sbms * 347189592Sbms * Returns 0 if a datagram should be allowed through, or various error codes 348189592Sbms * if the socket was not a member of the group, or the source was muted, etc. 349170613Sbms */ 350189592Sbmsint 351189592Sbmsimo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp, 352189592Sbms const struct sockaddr *group, const struct sockaddr *src) 353170613Sbms{ 354189592Sbms size_t gidx; 355189592Sbms struct in_msource *ims; 356189592Sbms int mode; 357189592Sbms 358189592Sbms KASSERT(ifp != NULL, ("%s: null ifp", __func__)); 359189592Sbms 360189592Sbms gidx = imo_match_group(imo, ifp, group); 361189592Sbms if (gidx == -1) 362189592Sbms return (MCAST_NOTGMEMBER); 363189592Sbms 364189592Sbms /* 365189592Sbms * Check if the source was included in an (S,G) join. 366189592Sbms * Allow reception on exclusive memberships by default, 367189592Sbms * reject reception on inclusive memberships by default. 368189592Sbms * Exclude source only if an in-mode exclude filter exists. 369189592Sbms * Include source only if an in-mode include filter exists. 370189592Sbms * NOTE: We are comparing group state here at IGMP t1 (now) 371189592Sbms * with socket-layer t0 (since last downcall). 372189592Sbms */ 373189592Sbms mode = imo->imo_mfilters[gidx].imf_st[1]; 374189592Sbms ims = imo_match_source(imo, gidx, src); 375189592Sbms 376189592Sbms if ((ims == NULL && mode == MCAST_INCLUDE) || 377189592Sbms (ims != NULL && ims->imsl_st[0] != mode)) 378189592Sbms return (MCAST_NOTSMEMBER); 379189592Sbms 380189592Sbms return (MCAST_PASS); 381189592Sbms} 382189592Sbms 383189592Sbms/* 384189592Sbms * Find and return a reference to an in_multi record for (ifp, group), 385189592Sbms * and bump its reference count. 386189592Sbms * If one does not exist, try to allocate it, and update link-layer multicast 387189592Sbms * filters on ifp to listen for group. 388189592Sbms * Assumes the IN_MULTI lock is held across the call. 389189592Sbms * Return 0 if successful, otherwise return an appropriate error code. 390189592Sbms */ 391189592Sbmsstatic int 392189592Sbmsin_getmulti(struct ifnet *ifp, const struct in_addr *group, 393189592Sbms struct in_multi **pinm) 394189592Sbms{ 395183550Szec INIT_VNET_INET(ifp->if_vnet); 396189592Sbms struct sockaddr_in gsin; 397189592Sbms struct ifmultiaddr *ifma; 398189592Sbms struct in_ifinfo *ii; 399189592Sbms struct in_multi *inm; 400189592Sbms int error; 401170613Sbms 402189592Sbms#if defined(INVARIANTS) && defined(IFF_ASSERTGIANT) 403189592Sbms IFF_ASSERTGIANT(ifp); 404189592Sbms#endif 405189592Sbms IN_MULTI_LOCK_ASSERT(); 406170613Sbms 407189592Sbms ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET]; 408170613Sbms 409189592Sbms inm = inm_lookup(ifp, *group); 410170613Sbms if (inm != NULL) { 411170613Sbms /* 412170613Sbms * If we already joined this group, just bump the 413170613Sbms * refcount and return it. 414170613Sbms */ 415170613Sbms KASSERT(inm->inm_refcount >= 1, 416170613Sbms ("%s: bad refcount %d", __func__, inm->inm_refcount)); 417170613Sbms ++inm->inm_refcount; 418189592Sbms *pinm = inm; 419189592Sbms return (0); 420189592Sbms } 421170613Sbms 422189592Sbms memset(&gsin, 0, sizeof(gsin)); 423189592Sbms gsin.sin_family = AF_INET; 424189592Sbms gsin.sin_len = sizeof(struct sockaddr_in); 425189592Sbms gsin.sin_addr = *group; 426170613Sbms 427189592Sbms /* 428189592Sbms * Check if a link-layer group is already associated 429189592Sbms * with this network-layer group on the given ifnet. 430189592Sbms */ 431189592Sbms error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma); 432189592Sbms if (error != 0) 433189592Sbms return (error); 434189592Sbms 435189931Sbms /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */ 436189931Sbms IF_ADDR_LOCK(ifp); 437189931Sbms 438189592Sbms /* 439189592Sbms * If something other than netinet is occupying the link-layer 440189592Sbms * group, print a meaningful error message and back out of 441189592Sbms * the allocation. 442189592Sbms * Otherwise, bump the refcount on the existing network-layer 443189592Sbms * group association and return it. 444189592Sbms */ 445189592Sbms if (ifma->ifma_protospec != NULL) { 446189592Sbms inm = (struct in_multi *)ifma->ifma_protospec; 447170613Sbms#ifdef INVARIANTS 448189592Sbms KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr", 449189592Sbms __func__)); 450189592Sbms KASSERT(ifma->ifma_addr->sa_family == AF_INET, 451189592Sbms ("%s: ifma not AF_INET", __func__)); 452189592Sbms KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__)); 453189592Sbms if (inm->inm_ifma != ifma || inm->inm_ifp != ifp || 454189592Sbms !in_hosteq(inm->inm_addr, *group)) 455189592Sbms panic("%s: ifma %p is inconsistent with %p (%s)", 456189592Sbms __func__, ifma, inm, inet_ntoa(*group)); 457170613Sbms#endif 458189592Sbms ++inm->inm_refcount; 459189592Sbms *pinm = inm; 460189931Sbms IF_ADDR_UNLOCK(ifp); 461189592Sbms return (0); 462189592Sbms } 463189592Sbms 464189931Sbms IF_ADDR_LOCK_ASSERT(ifp); 465189931Sbms 466189592Sbms /* 467189592Sbms * A new in_multi record is needed; allocate and initialize it. 468189592Sbms * We DO NOT perform an IGMP join as the in_ layer may need to 469189592Sbms * push an initial source list down to IGMP to support SSM. 470189592Sbms * 471189592Sbms * The initial source filter state is INCLUDE, {} as per the RFC. 472189592Sbms */ 473189592Sbms inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO); 474189592Sbms if (inm == NULL) { 475189592Sbms if_delmulti_ifma(ifma); 476189931Sbms IF_ADDR_UNLOCK(ifp); 477189592Sbms return (ENOMEM); 478189592Sbms } 479189592Sbms inm->inm_addr = *group; 480189592Sbms inm->inm_ifp = ifp; 481189592Sbms inm->inm_igi = ii->ii_igmp; 482189592Sbms inm->inm_ifma = ifma; 483189592Sbms inm->inm_refcount = 1; 484189592Sbms inm->inm_state = IGMP_NOT_MEMBER; 485189592Sbms 486189592Sbms /* 487189592Sbms * Pending state-changes per group are subject to a bounds check. 488189592Sbms */ 489189592Sbms IFQ_SET_MAXLEN(&inm->inm_scq, IGMP_MAX_STATE_CHANGES); 490189592Sbms 491189592Sbms inm->inm_st[0].iss_fmode = MCAST_UNDEFINED; 492189592Sbms inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; 493189592Sbms RB_INIT(&inm->inm_srcs); 494189592Sbms 495189592Sbms ifma->ifma_protospec = inm; 496189592Sbms 497189592Sbms *pinm = inm; 498189592Sbms 499189931Sbms IF_ADDR_UNLOCK(ifp); 500189592Sbms return (0); 501189592Sbms} 502189592Sbms 503189592Sbms/* 504189592Sbms * Drop a reference to an in_multi record. 505189592Sbms * 506189592Sbms * If the refcount drops to 0, free the in_multi record and 507189592Sbms * delete the underlying link-layer membership. 508189592Sbms */ 509189592Sbmsvoid 510189592Sbmsinm_release_locked(struct in_multi *inm) 511189592Sbms{ 512189592Sbms struct ifmultiaddr *ifma; 513189592Sbms 514189592Sbms#if defined(INVARIANTS) && defined(IFF_ASSERTGIANT) 515189592Sbms if (!inm_is_ifp_detached(inm)) 516189592Sbms IFF_ASSERTGIANT(ifp); 517189592Sbms#endif 518189592Sbms 519189592Sbms IN_MULTI_LOCK_ASSERT(); 520189592Sbms 521189592Sbms CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount); 522189592Sbms 523189592Sbms if (--inm->inm_refcount > 0) { 524189592Sbms CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__, 525189592Sbms inm->inm_refcount); 526189592Sbms return; 527189592Sbms } 528189592Sbms 529189592Sbms CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm); 530189592Sbms 531189592Sbms ifma = inm->inm_ifma; 532189592Sbms 533189931Sbms /* XXX this access is not covered by IF_ADDR_LOCK */ 534189592Sbms CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma); 535189592Sbms KASSERT(ifma->ifma_protospec == inm, 536189592Sbms ("%s: ifma_protospec != inm", __func__)); 537189592Sbms ifma->ifma_protospec = NULL; 538189592Sbms 539189592Sbms inm_purge(inm); 540189592Sbms 541189592Sbms free(inm, M_IPMADDR); 542189592Sbms 543189592Sbms if_delmulti_ifma(ifma); 544189592Sbms} 545189592Sbms 546189592Sbms/* 547189592Sbms * Clear recorded source entries for a group. 548189592Sbms * Used by the IGMP code. Caller must hold the IN_MULTI lock. 549189592Sbms * FIXME: Should reap. 550189592Sbms */ 551189592Sbmsvoid 552189592Sbmsinm_clear_recorded(struct in_multi *inm) 553189592Sbms{ 554189592Sbms struct ip_msource *ims; 555189592Sbms 556189592Sbms IN_MULTI_LOCK_ASSERT(); 557189592Sbms 558189592Sbms RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { 559189592Sbms if (ims->ims_stp) { 560189592Sbms ims->ims_stp = 0; 561189592Sbms --inm->inm_st[1].iss_rec; 562170613Sbms } 563189592Sbms } 564189592Sbms KASSERT(inm->inm_st[1].iss_rec == 0, 565189592Sbms ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec)); 566189592Sbms} 567170613Sbms 568189592Sbms/* 569189592Sbms * Record a source as pending for a Source-Group IGMPv3 query. 570189592Sbms * This lives here as it modifies the shared tree. 571189592Sbms * 572189592Sbms * inm is the group descriptor. 573189592Sbms * naddr is the address of the source to record in network-byte order. 574189592Sbms * 575189592Sbms * If the net.inet.igmp.sgalloc sysctl is non-zero, we will 576189592Sbms * lazy-allocate a source node in response to an SG query. 577189592Sbms * Otherwise, no allocation is performed. This saves some memory 578189592Sbms * with the trade-off that the source will not be reported to the 579189592Sbms * router if joined in the window between the query response and 580189592Sbms * the group actually being joined on the local host. 581189592Sbms * 582189592Sbms * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed. 583189592Sbms * This turns off the allocation of a recorded source entry if 584189592Sbms * the group has not been joined. 585189592Sbms * 586189592Sbms * Return 0 if the source didn't exist or was already marked as recorded. 587189592Sbms * Return 1 if the source was marked as recorded by this function. 588189592Sbms * Return <0 if any error occured (negated errno code). 589189592Sbms */ 590189592Sbmsint 591189592Sbmsinm_record_source(struct in_multi *inm, const in_addr_t naddr) 592189592Sbms{ 593189592Sbms struct ip_msource find; 594189592Sbms struct ip_msource *ims, *nims; 595189592Sbms 596189592Sbms IN_MULTI_LOCK_ASSERT(); 597189592Sbms 598189592Sbms find.ims_haddr = ntohl(naddr); 599189592Sbms ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find); 600189592Sbms if (ims && ims->ims_stp) 601189592Sbms return (0); 602189592Sbms if (ims == NULL) { 603189592Sbms if (inm->inm_nsrc == in_mcast_maxgrpsrc) 604189592Sbms return (-ENOSPC); 605189592Sbms nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE, 606189592Sbms M_NOWAIT | M_ZERO); 607189592Sbms if (nims == NULL) 608189592Sbms return (-ENOMEM); 609189592Sbms nims->ims_haddr = find.ims_haddr; 610189592Sbms RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims); 611189592Sbms ++inm->inm_nsrc; 612189592Sbms ims = nims; 613189592Sbms } 614189592Sbms 615189592Sbms /* 616189592Sbms * Mark the source as recorded and update the recorded 617189592Sbms * source count. 618189592Sbms */ 619189592Sbms ++ims->ims_stp; 620189592Sbms ++inm->inm_st[1].iss_rec; 621189592Sbms 622189592Sbms return (1); 623189592Sbms} 624189592Sbms 625189592Sbms/* 626189592Sbms * Return a pointer to an in_msource owned by an in_mfilter, 627189592Sbms * given its source address. 628189592Sbms * Lazy-allocate if needed. If this is a new entry its filter state is 629189592Sbms * undefined at t0. 630189592Sbms * 631189592Sbms * imf is the filter set being modified. 632189592Sbms * haddr is the source address in *host* byte-order. 633189592Sbms * 634189592Sbms * SMPng: May be called with locks held; malloc must not block. 635189592Sbms */ 636189592Sbmsstatic int 637189592Sbmsimf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin, 638189592Sbms struct in_msource **plims) 639189592Sbms{ 640189592Sbms struct ip_msource find; 641189592Sbms struct ip_msource *ims, *nims; 642189592Sbms struct in_msource *lims; 643189592Sbms int error; 644189592Sbms 645189592Sbms error = 0; 646189592Sbms ims = NULL; 647189592Sbms lims = NULL; 648189592Sbms 649189592Sbms /* key is host byte order */ 650189592Sbms find.ims_haddr = ntohl(psin->sin_addr.s_addr); 651189592Sbms ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); 652189592Sbms lims = (struct in_msource *)ims; 653189592Sbms if (lims == NULL) { 654189592Sbms if (imf->imf_nsrc == in_mcast_maxsocksrc) 655189592Sbms return (ENOSPC); 656189592Sbms nims = malloc(sizeof(struct in_msource), M_INMFILTER, 657189592Sbms M_NOWAIT | M_ZERO); 658189592Sbms if (nims == NULL) 659189592Sbms return (ENOMEM); 660189592Sbms lims = (struct in_msource *)nims; 661189592Sbms lims->ims_haddr = find.ims_haddr; 662189592Sbms lims->imsl_st[0] = MCAST_UNDEFINED; 663189592Sbms RB_INSERT(ip_msource_tree, &imf->imf_sources, nims); 664189592Sbms ++imf->imf_nsrc; 665189592Sbms } 666189592Sbms 667189592Sbms *plims = lims; 668189592Sbms 669189592Sbms return (error); 670189592Sbms} 671189592Sbms 672189592Sbms/* 673189592Sbms * Graft a source entry into an existing socket-layer filter set, 674189592Sbms * maintaining any required invariants and checking allocations. 675189592Sbms * 676189592Sbms * The source is marked as being in the new filter mode at t1. 677189592Sbms * 678189592Sbms * Return the pointer to the new node, otherwise return NULL. 679189592Sbms */ 680189592Sbmsstatic struct in_msource * 681189592Sbmsimf_graft(struct in_mfilter *imf, const uint8_t st1, 682189592Sbms const struct sockaddr_in *psin) 683189592Sbms{ 684189592Sbms struct ip_msource *nims; 685189592Sbms struct in_msource *lims; 686189592Sbms 687189592Sbms nims = malloc(sizeof(struct in_msource), M_INMFILTER, 688189592Sbms M_NOWAIT | M_ZERO); 689189592Sbms if (nims == NULL) 690189592Sbms return (NULL); 691189592Sbms lims = (struct in_msource *)nims; 692189592Sbms lims->ims_haddr = ntohl(psin->sin_addr.s_addr); 693189592Sbms lims->imsl_st[0] = MCAST_UNDEFINED; 694189592Sbms lims->imsl_st[1] = st1; 695189592Sbms RB_INSERT(ip_msource_tree, &imf->imf_sources, nims); 696189592Sbms ++imf->imf_nsrc; 697189592Sbms 698189592Sbms return (lims); 699189592Sbms} 700189592Sbms 701189592Sbms/* 702189592Sbms * Prune a source entry from an existing socket-layer filter set, 703189592Sbms * maintaining any required invariants and checking allocations. 704189592Sbms * 705189592Sbms * The source is marked as being left at t1, it is not freed. 706189592Sbms * 707189592Sbms * Return 0 if no error occurred, otherwise return an errno value. 708189592Sbms */ 709189592Sbmsstatic int 710189592Sbmsimf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin) 711189592Sbms{ 712189592Sbms struct ip_msource find; 713189592Sbms struct ip_msource *ims; 714189592Sbms struct in_msource *lims; 715189592Sbms 716189592Sbms /* key is host byte order */ 717189592Sbms find.ims_haddr = ntohl(psin->sin_addr.s_addr); 718189592Sbms ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); 719189592Sbms if (ims == NULL) 720189592Sbms return (ENOENT); 721189592Sbms lims = (struct in_msource *)ims; 722189592Sbms lims->imsl_st[1] = MCAST_UNDEFINED; 723189592Sbms return (0); 724189592Sbms} 725189592Sbms 726189592Sbms/* 727189592Sbms * Revert socket-layer filter set deltas at t1 to t0 state. 728189592Sbms */ 729189592Sbmsstatic void 730189592Sbmsimf_rollback(struct in_mfilter *imf) 731189592Sbms{ 732189592Sbms struct ip_msource *ims, *tims; 733189592Sbms struct in_msource *lims; 734189592Sbms 735189592Sbms RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { 736189592Sbms lims = (struct in_msource *)ims; 737189592Sbms if (lims->imsl_st[0] == lims->imsl_st[1]) { 738189592Sbms /* no change at t1 */ 739189592Sbms continue; 740189592Sbms } else if (lims->imsl_st[0] != MCAST_UNDEFINED) { 741189592Sbms /* revert change to existing source at t1 */ 742189592Sbms lims->imsl_st[1] = lims->imsl_st[0]; 743189592Sbms } else { 744189592Sbms /* revert source added t1 */ 745189592Sbms CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims); 746189592Sbms RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); 747189592Sbms free(ims, M_INMFILTER); 748189592Sbms imf->imf_nsrc--; 749189592Sbms } 750189592Sbms } 751189592Sbms imf->imf_st[1] = imf->imf_st[0]; 752189592Sbms} 753189592Sbms 754189592Sbms/* 755189592Sbms * Mark socket-layer filter set as INCLUDE {} at t1. 756189592Sbms */ 757189592Sbmsstatic void 758189592Sbmsimf_leave(struct in_mfilter *imf) 759189592Sbms{ 760189592Sbms struct ip_msource *ims; 761189592Sbms struct in_msource *lims; 762189592Sbms 763189592Sbms RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { 764189592Sbms lims = (struct in_msource *)ims; 765189592Sbms lims->imsl_st[1] = MCAST_UNDEFINED; 766189592Sbms } 767189592Sbms imf->imf_st[1] = MCAST_INCLUDE; 768189592Sbms} 769189592Sbms 770189592Sbms/* 771189592Sbms * Mark socket-layer filter set deltas as committed. 772189592Sbms */ 773189592Sbmsstatic void 774189592Sbmsimf_commit(struct in_mfilter *imf) 775189592Sbms{ 776189592Sbms struct ip_msource *ims; 777189592Sbms struct in_msource *lims; 778189592Sbms 779189592Sbms RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { 780189592Sbms lims = (struct in_msource *)ims; 781189592Sbms lims->imsl_st[0] = lims->imsl_st[1]; 782189592Sbms } 783189592Sbms imf->imf_st[0] = imf->imf_st[1]; 784189592Sbms} 785189592Sbms 786189592Sbms/* 787189592Sbms * Reap unreferenced sources from socket-layer filter set. 788189592Sbms */ 789189592Sbmsstatic void 790189592Sbmsimf_reap(struct in_mfilter *imf) 791189592Sbms{ 792189592Sbms struct ip_msource *ims, *tims; 793189592Sbms struct in_msource *lims; 794189592Sbms 795189592Sbms RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { 796189592Sbms lims = (struct in_msource *)ims; 797189592Sbms if ((lims->imsl_st[0] == MCAST_UNDEFINED) && 798189592Sbms (lims->imsl_st[1] == MCAST_UNDEFINED)) { 799189592Sbms CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims); 800189592Sbms RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); 801189592Sbms free(ims, M_INMFILTER); 802189592Sbms imf->imf_nsrc--; 803189592Sbms } 804189592Sbms } 805189592Sbms} 806189592Sbms 807189592Sbms/* 808189592Sbms * Purge socket-layer filter set. 809189592Sbms */ 810189592Sbmsstatic void 811189592Sbmsimf_purge(struct in_mfilter *imf) 812189592Sbms{ 813189592Sbms struct ip_msource *ims, *tims; 814189592Sbms 815189592Sbms RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { 816189592Sbms CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims); 817189592Sbms RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); 818189592Sbms free(ims, M_INMFILTER); 819189592Sbms imf->imf_nsrc--; 820189592Sbms } 821189592Sbms imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED; 822189592Sbms KASSERT(RB_EMPTY(&imf->imf_sources), 823189592Sbms ("%s: imf_sources not empty", __func__)); 824189592Sbms} 825189592Sbms 826189592Sbms/* 827189592Sbms * Look up a source filter entry for a multicast group. 828189592Sbms * 829189592Sbms * inm is the group descriptor to work with. 830189592Sbms * haddr is the host-byte-order IPv4 address to look up. 831189592Sbms * noalloc may be non-zero to suppress allocation of sources. 832189592Sbms * *pims will be set to the address of the retrieved or allocated source. 833189592Sbms * 834189592Sbms * SMPng: NOTE: may be called with locks held. 835189592Sbms * Return 0 if successful, otherwise return a non-zero error code. 836189592Sbms */ 837189592Sbmsstatic int 838189592Sbmsinm_get_source(struct in_multi *inm, const in_addr_t haddr, 839189592Sbms const int noalloc, struct ip_msource **pims) 840189592Sbms{ 841189592Sbms struct ip_msource find; 842189592Sbms struct ip_msource *ims, *nims; 843189592Sbms#ifdef KTR 844189592Sbms struct in_addr ia; 845189592Sbms#endif 846189592Sbms 847189592Sbms find.ims_haddr = haddr; 848189592Sbms ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find); 849189592Sbms if (ims == NULL && !noalloc) { 850189592Sbms if (inm->inm_nsrc == in_mcast_maxgrpsrc) 851189592Sbms return (ENOSPC); 852189592Sbms nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE, 853189592Sbms M_NOWAIT | M_ZERO); 854189592Sbms if (nims == NULL) 855189592Sbms return (ENOMEM); 856189592Sbms nims->ims_haddr = haddr; 857189592Sbms RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims); 858189592Sbms ++inm->inm_nsrc; 859189592Sbms ims = nims; 860189592Sbms#ifdef KTR 861189592Sbms ia.s_addr = htonl(haddr); 862189592Sbms CTR3(KTR_IGMPV3, "%s: allocated %s as %p", __func__, 863189592Sbms inet_ntoa(ia), ims); 864189592Sbms#endif 865189592Sbms } 866189592Sbms 867189592Sbms *pims = ims; 868189592Sbms return (0); 869189592Sbms} 870189592Sbms 871189592Sbms/* 872189592Sbms * Merge socket-layer source into IGMP-layer source. 873189592Sbms * If rollback is non-zero, perform the inverse of the merge. 874189592Sbms */ 875189592Sbmsstatic void 876189592Sbmsims_merge(struct ip_msource *ims, const struct in_msource *lims, 877189592Sbms const int rollback) 878189592Sbms{ 879189592Sbms int n = rollback ? -1 : 1; 880189592Sbms#ifdef KTR 881189592Sbms struct in_addr ia; 882189592Sbms 883189592Sbms ia.s_addr = htonl(ims->ims_haddr); 884189592Sbms#endif 885189592Sbms 886189592Sbms if (lims->imsl_st[0] == MCAST_EXCLUDE) { 887189592Sbms CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on %s", 888189592Sbms __func__, n, inet_ntoa(ia)); 889189592Sbms ims->ims_st[1].ex -= n; 890189592Sbms } else if (lims->imsl_st[0] == MCAST_INCLUDE) { 891189592Sbms CTR3(KTR_IGMPV3, "%s: t1 in -= %d on %s", 892189592Sbms __func__, n, inet_ntoa(ia)); 893189592Sbms ims->ims_st[1].in -= n; 894189592Sbms } 895189592Sbms 896189592Sbms if (lims->imsl_st[1] == MCAST_EXCLUDE) { 897189592Sbms CTR3(KTR_IGMPV3, "%s: t1 ex += %d on %s", 898189592Sbms __func__, n, inet_ntoa(ia)); 899189592Sbms ims->ims_st[1].ex += n; 900189592Sbms } else if (lims->imsl_st[1] == MCAST_INCLUDE) { 901189592Sbms CTR3(KTR_IGMPV3, "%s: t1 in += %d on %s", 902189592Sbms __func__, n, inet_ntoa(ia)); 903189592Sbms ims->ims_st[1].in += n; 904189592Sbms } 905189592Sbms} 906189592Sbms 907189592Sbms/* 908189592Sbms * Atomically update the global in_multi state, when a membership's 909189592Sbms * filter list is being updated in any way. 910189592Sbms * 911189592Sbms * imf is the per-inpcb-membership group filter pointer. 912189592Sbms * A fake imf may be passed for in-kernel consumers. 913189592Sbms * 914189592Sbms * XXX This is a candidate for a set-symmetric-difference style loop 915189592Sbms * which would eliminate the repeated lookup from root of ims nodes, 916189592Sbms * as they share the same key space. 917189592Sbms * 918189592Sbms * If any error occurred this function will back out of refcounts 919189592Sbms * and return a non-zero value. 920189592Sbms */ 921189592Sbmsstatic int 922189592Sbmsinm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf) 923189592Sbms{ 924189592Sbms struct ip_msource *ims, *nims; 925189592Sbms struct in_msource *lims; 926189592Sbms int schanged, error; 927189592Sbms int nsrc0, nsrc1; 928189592Sbms 929189592Sbms schanged = 0; 930189592Sbms error = 0; 931189592Sbms nsrc1 = nsrc0 = 0; 932189592Sbms 933189592Sbms /* 934189592Sbms * Update the source filters first, as this may fail. 935189592Sbms * Maintain count of in-mode filters at t0, t1. These are 936189592Sbms * used to work out if we transition into ASM mode or not. 937189592Sbms * Maintain a count of source filters whose state was 938189592Sbms * actually modified by this operation. 939189592Sbms */ 940189592Sbms RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { 941189592Sbms lims = (struct in_msource *)ims; 942189592Sbms if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++; 943189592Sbms if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++; 944189592Sbms if (lims->imsl_st[0] == lims->imsl_st[1]) continue; 945189592Sbms error = inm_get_source(inm, lims->ims_haddr, 0, &nims); 946189592Sbms ++schanged; 947189592Sbms if (error) 948170613Sbms break; 949189592Sbms ims_merge(nims, lims, 0); 950189592Sbms } 951189592Sbms if (error) { 952189592Sbms struct ip_msource *bims; 953189592Sbms 954189592Sbms RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) { 955189592Sbms lims = (struct in_msource *)ims; 956189592Sbms if (lims->imsl_st[0] == lims->imsl_st[1]) 957189592Sbms continue; 958189592Sbms (void)inm_get_source(inm, lims->ims_haddr, 1, &bims); 959189592Sbms if (bims == NULL) 960189592Sbms continue; 961189592Sbms ims_merge(bims, lims, 1); 962170613Sbms } 963189592Sbms goto out_reap; 964189592Sbms } 965170613Sbms 966189592Sbms CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1", 967189592Sbms __func__, nsrc0, nsrc1); 968170613Sbms 969189592Sbms /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */ 970189592Sbms if (imf->imf_st[0] == imf->imf_st[1] && 971189592Sbms imf->imf_st[1] == MCAST_INCLUDE) { 972189592Sbms if (nsrc1 == 0) { 973189592Sbms CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__); 974189592Sbms --inm->inm_st[1].iss_in; 975189592Sbms } 976189592Sbms } 977170613Sbms 978189592Sbms /* Handle filter mode transition on socket. */ 979189592Sbms if (imf->imf_st[0] != imf->imf_st[1]) { 980189592Sbms CTR3(KTR_IGMPV3, "%s: imf transition %d to %d", 981189592Sbms __func__, imf->imf_st[0], imf->imf_st[1]); 982189592Sbms 983189592Sbms if (imf->imf_st[0] == MCAST_EXCLUDE) { 984189592Sbms CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__); 985189592Sbms --inm->inm_st[1].iss_ex; 986189592Sbms } else if (imf->imf_st[0] == MCAST_INCLUDE) { 987189592Sbms CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__); 988189592Sbms --inm->inm_st[1].iss_in; 989189592Sbms } 990189592Sbms 991189592Sbms if (imf->imf_st[1] == MCAST_EXCLUDE) { 992189592Sbms CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__); 993189592Sbms inm->inm_st[1].iss_ex++; 994189592Sbms } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) { 995189592Sbms CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__); 996189592Sbms inm->inm_st[1].iss_in++; 997189592Sbms } 998189592Sbms } 999189592Sbms 1000189592Sbms /* 1001189592Sbms * Track inm filter state in terms of listener counts. 1002189592Sbms * If there are any exclusive listeners, stack-wide 1003189592Sbms * membership is exclusive. 1004189592Sbms * Otherwise, if only inclusive listeners, stack-wide is inclusive. 1005189592Sbms * If no listeners remain, state is undefined at t1, 1006189592Sbms * and the IGMP lifecycle for this group should finish. 1007189592Sbms */ 1008189592Sbms if (inm->inm_st[1].iss_ex > 0) { 1009189592Sbms CTR1(KTR_IGMPV3, "%s: transition to EX", __func__); 1010189592Sbms inm->inm_st[1].iss_fmode = MCAST_EXCLUDE; 1011189592Sbms } else if (inm->inm_st[1].iss_in > 0) { 1012189592Sbms CTR1(KTR_IGMPV3, "%s: transition to IN", __func__); 1013189592Sbms inm->inm_st[1].iss_fmode = MCAST_INCLUDE; 1014189592Sbms } else { 1015189592Sbms CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__); 1016189592Sbms inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; 1017189592Sbms } 1018189592Sbms 1019189592Sbms /* Decrement ASM listener count on transition out of ASM mode. */ 1020189592Sbms if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) { 1021189592Sbms if ((imf->imf_st[1] != MCAST_EXCLUDE) || 1022189592Sbms (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) 1023189592Sbms CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__); 1024189592Sbms --inm->inm_st[1].iss_asm; 1025189592Sbms } 1026189592Sbms 1027189592Sbms /* Increment ASM listener count on transition to ASM mode. */ 1028189592Sbms if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) { 1029189592Sbms CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__); 1030189592Sbms inm->inm_st[1].iss_asm++; 1031189592Sbms } 1032189592Sbms 1033189592Sbms CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm); 1034189592Sbms inm_print(inm); 1035189592Sbms 1036189592Sbmsout_reap: 1037189592Sbms if (schanged > 0) { 1038189592Sbms CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__); 1039189592Sbms inm_reap(inm); 1040189592Sbms } 1041189592Sbms return (error); 1042189592Sbms} 1043189592Sbms 1044189592Sbms/* 1045189592Sbms * Mark an in_multi's filter set deltas as committed. 1046189592Sbms * Called by IGMP after a state change has been enqueued. 1047189592Sbms */ 1048189592Sbmsvoid 1049189592Sbmsinm_commit(struct in_multi *inm) 1050189592Sbms{ 1051189592Sbms struct ip_msource *ims; 1052189592Sbms 1053189592Sbms CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm); 1054189592Sbms CTR1(KTR_IGMPV3, "%s: pre commit:", __func__); 1055189592Sbms inm_print(inm); 1056189592Sbms 1057189592Sbms RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { 1058189592Sbms ims->ims_st[0] = ims->ims_st[1]; 1059189592Sbms } 1060189592Sbms inm->inm_st[0] = inm->inm_st[1]; 1061189592Sbms} 1062189592Sbms 1063189592Sbms/* 1064189592Sbms * Reap unreferenced nodes from an in_multi's filter set. 1065189592Sbms */ 1066189592Sbmsstatic void 1067189592Sbmsinm_reap(struct in_multi *inm) 1068189592Sbms{ 1069189592Sbms struct ip_msource *ims, *tims; 1070189592Sbms 1071189592Sbms RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) { 1072189592Sbms if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 || 1073189592Sbms ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 || 1074189592Sbms ims->ims_stp != 0) 1075189592Sbms continue; 1076189592Sbms CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims); 1077189592Sbms RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims); 1078189592Sbms free(ims, M_IPMSOURCE); 1079189592Sbms inm->inm_nsrc--; 1080189592Sbms } 1081189592Sbms} 1082189592Sbms 1083189592Sbms/* 1084189592Sbms * Purge all source nodes from an in_multi's filter set. 1085189592Sbms */ 1086189592Sbmsstatic void 1087189592Sbmsinm_purge(struct in_multi *inm) 1088189592Sbms{ 1089189592Sbms struct ip_msource *ims, *tims; 1090189592Sbms 1091189592Sbms RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) { 1092189592Sbms CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims); 1093189592Sbms RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims); 1094189592Sbms free(ims, M_IPMSOURCE); 1095189592Sbms inm->inm_nsrc--; 1096189592Sbms } 1097189592Sbms} 1098189592Sbms 1099189592Sbms/* 1100189592Sbms * Join a multicast group; unlocked entry point. 1101189592Sbms * 1102189592Sbms * SMPng: XXX: in_joingroup() is called from in_control() when Giant 1103189592Sbms * is not held. Fortunately, ifp is unlikely to have been detached 1104189592Sbms * at this point, so we assume it's OK to recurse. 1105189592Sbms */ 1106189592Sbmsint 1107189592Sbmsin_joingroup(struct ifnet *ifp, const struct in_addr *gina, 1108189592Sbms /*const*/ struct in_mfilter *imf, struct in_multi **pinm) 1109189592Sbms{ 1110189592Sbms int error; 1111189592Sbms 1112189592Sbms IN_MULTI_LOCK(); 1113189592Sbms error = in_joingroup_locked(ifp, gina, imf, pinm); 1114170613Sbms IN_MULTI_UNLOCK(); 1115170613Sbms 1116189592Sbms return (error); 1117170613Sbms} 1118170613Sbms 1119170613Sbms/* 1120189592Sbms * Join a multicast group; real entry point. 1121170613Sbms * 1122189592Sbms * Only preserves atomicity at inm level. 1123189592Sbms * NOTE: imf argument cannot be const due to sys/tree.h limitations. 1124170613Sbms * 1125189592Sbms * If the IGMP downcall fails, the group is not joined, and an error 1126189592Sbms * code is returned. 1127170613Sbms */ 1128189592Sbmsint 1129189592Sbmsin_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina, 1130189592Sbms /*const*/ struct in_mfilter *imf, struct in_multi **pinm) 1131170613Sbms{ 1132189592Sbms struct in_mfilter timf; 1133189592Sbms struct in_multi *inm; 1134189592Sbms int error; 1135170613Sbms 1136189592Sbms IN_MULTI_LOCK_ASSERT(); 1137170613Sbms 1138189592Sbms CTR4(KTR_IGMPV3, "%s: join %s on %p(%s))", __func__, 1139189592Sbms inet_ntoa(*gina), ifp, ifp->if_xname); 1140189592Sbms 1141189592Sbms error = 0; 1142189592Sbms inm = NULL; 1143189592Sbms 1144189592Sbms /* 1145189592Sbms * If no imf was specified (i.e. kernel consumer), 1146189592Sbms * fake one up and assume it is an ASM join. 1147189592Sbms */ 1148189592Sbms if (imf == NULL) { 1149189592Sbms imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE); 1150189592Sbms imf = &timf; 1151170613Sbms } 1152170613Sbms 1153189592Sbms error = in_getmulti(ifp, gina, &inm); 1154189592Sbms if (error) { 1155189592Sbms CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__); 1156189592Sbms return (error); 1157189592Sbms } 1158189592Sbms 1159189592Sbms CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 1160189592Sbms error = inm_merge(inm, imf); 1161189592Sbms if (error) { 1162189592Sbms CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__); 1163189592Sbms goto out_inm_release; 1164189592Sbms } 1165189592Sbms 1166189592Sbms CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 1167189592Sbms error = igmp_change_state(inm); 1168189592Sbms if (error) { 1169189592Sbms CTR1(KTR_IGMPV3, "%s: failed to update source", __func__); 1170189592Sbms goto out_inm_release; 1171189592Sbms } 1172189592Sbms 1173189592Sbmsout_inm_release: 1174189592Sbms if (error) { 1175189592Sbms CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm); 1176189592Sbms inm_release_locked(inm); 1177189592Sbms } else { 1178189592Sbms *pinm = inm; 1179189592Sbms } 1180189592Sbms 1181189592Sbms return (error); 1182189592Sbms} 1183189592Sbms 1184189592Sbms/* 1185189592Sbms * Leave a multicast group; unlocked entry point. 1186189592Sbms */ 1187189592Sbmsint 1188189592Sbmsin_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf) 1189189592Sbms{ 1190189592Sbms struct ifnet *ifp; 1191189851Srwatson int error; 1192189592Sbms 1193189592Sbms ifp = inm->inm_ifp; 1194189592Sbms 1195170613Sbms IN_MULTI_LOCK(); 1196189592Sbms error = in_leavegroup_locked(inm, imf); 1197170613Sbms IN_MULTI_UNLOCK(); 1198170613Sbms 1199189592Sbms return (error); 1200170613Sbms} 1201170613Sbms 1202170613Sbms/* 1203189592Sbms * Leave a multicast group; real entry point. 1204189592Sbms * All source filters will be expunged. 1205170613Sbms * 1206189592Sbms * Only preserves atomicity at inm level. 1207189592Sbms * 1208189592Sbms * Holding the write lock for the INP which contains imf 1209189592Sbms * is highly advisable. We can't assert for it as imf does not 1210189592Sbms * contain a back-pointer to the owning inp. 1211189592Sbms * 1212189592Sbms * Note: This is not the same as inm_release(*) as this function also 1213189592Sbms * makes a state change downcall into IGMP. 1214170613Sbms */ 1215189592Sbmsint 1216189592Sbmsin_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf) 1217170613Sbms{ 1218189592Sbms struct in_mfilter timf; 1219189592Sbms int error; 1220170613Sbms 1221189592Sbms error = 0; 1222189592Sbms 1223189592Sbms#if defined(INVARIANTS) && defined(IFF_ASSERTGIANT) 1224189592Sbms if (!inm_is_ifp_detached(inm)) 1225189592Sbms IFF_ASSERTGIANT(inm->inm_ifp); 1226189592Sbms#endif 1227189592Sbms 1228170613Sbms IN_MULTI_LOCK_ASSERT(); 1229170613Sbms 1230189592Sbms CTR5(KTR_IGMPV3, "%s: leave inm %p, %s/%s, imf %p", __func__, 1231189592Sbms inm, inet_ntoa(inm->inm_addr), 1232189592Sbms (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname), 1233189592Sbms imf); 1234170613Sbms 1235189592Sbms /* 1236189592Sbms * If no imf was specified (i.e. kernel consumer), 1237189592Sbms * fake one up and assume it is an ASM join. 1238189592Sbms */ 1239189592Sbms if (imf == NULL) { 1240189592Sbms imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED); 1241189592Sbms imf = &timf; 1242189592Sbms } 1243170613Sbms 1244189592Sbms /* 1245189592Sbms * Begin state merge transaction at IGMP layer. 1246189592Sbms * 1247189592Sbms * As this particular invocation should not cause any memory 1248189592Sbms * to be allocated, and there is no opportunity to roll back 1249189592Sbms * the transaction, it MUST NOT fail. 1250189592Sbms */ 1251189592Sbms CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 1252189592Sbms error = inm_merge(inm, imf); 1253189592Sbms KASSERT(error == 0, ("%s: failed to merge inm state", __func__)); 1254170613Sbms 1255189592Sbms CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 1256189592Sbms error = igmp_change_state(inm); 1257189592Sbms if (error) 1258189592Sbms CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__); 1259189592Sbms 1260189592Sbms CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm); 1261189592Sbms inm_release_locked(inm); 1262189592Sbms 1263189592Sbms return (error); 1264170613Sbms} 1265170613Sbms 1266189592Sbms/*#ifndef BURN_BRIDGES*/ 1267170613Sbms/* 1268189592Sbms * Join an IPv4 multicast group in (*,G) exclusive mode. 1269189592Sbms * The group must be a 224.0.0.0/24 link-scope group. 1270189592Sbms * This KPI is for legacy kernel consumers only. 1271170613Sbms */ 1272189592Sbmsstruct in_multi * 1273189592Sbmsin_addmulti(struct in_addr *ap, struct ifnet *ifp) 1274189592Sbms{ 1275189592Sbms struct in_multi *pinm; 1276189592Sbms int error; 1277189592Sbms 1278189592Sbms KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)), 1279189592Sbms ("%s: %s not in 224.0.0.0/24", __func__, inet_ntoa(*ap))); 1280189592Sbms 1281189592Sbms error = in_joingroup(ifp, ap, NULL, &pinm); 1282189592Sbms if (error != 0) 1283189592Sbms pinm = NULL; 1284189592Sbms 1285189592Sbms return (pinm); 1286189592Sbms} 1287189592Sbms 1288189592Sbms/* 1289189592Sbms * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode. 1290189592Sbms * This KPI is for legacy kernel consumers only. 1291189592Sbms */ 1292189592Sbmsvoid 1293189592Sbmsin_delmulti(struct in_multi *inm) 1294189592Sbms{ 1295189592Sbms 1296189592Sbms (void)in_leavegroup(inm, NULL); 1297189592Sbms} 1298189592Sbms/*#endif*/ 1299189592Sbms 1300189592Sbms/* 1301189592Sbms * Block or unblock an ASM multicast source on an inpcb. 1302189592Sbms * This implements the delta-based API described in RFC 3678. 1303189592Sbms * 1304189592Sbms * The delta-based API applies only to exclusive-mode memberships. 1305189592Sbms * An IGMP downcall will be performed. 1306189592Sbms * 1307189592Sbms * SMPng: NOTE: Must take Giant as a join may create a new ifma. 1308189592Sbms * 1309189592Sbms * Return 0 if successful, otherwise return an appropriate error code. 1310189592Sbms */ 1311170613Sbmsstatic int 1312189592Sbmsinp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) 1313170613Sbms{ 1314183550Szec INIT_VNET_NET(curvnet); 1315183550Szec INIT_VNET_INET(curvnet); 1316170613Sbms struct group_source_req gsr; 1317170613Sbms sockunion_t *gsa, *ssa; 1318170613Sbms struct ifnet *ifp; 1319170613Sbms struct in_mfilter *imf; 1320170613Sbms struct ip_moptions *imo; 1321170613Sbms struct in_msource *ims; 1322189592Sbms struct in_multi *inm; 1323170613Sbms size_t idx; 1324189592Sbms uint16_t fmode; 1325189592Sbms int error, doblock; 1326170613Sbms 1327170613Sbms ifp = NULL; 1328170613Sbms error = 0; 1329189592Sbms doblock = 0; 1330170613Sbms 1331170613Sbms memset(&gsr, 0, sizeof(struct group_source_req)); 1332170613Sbms gsa = (sockunion_t *)&gsr.gsr_group; 1333170613Sbms ssa = (sockunion_t *)&gsr.gsr_source; 1334170613Sbms 1335170613Sbms switch (sopt->sopt_name) { 1336170613Sbms case IP_BLOCK_SOURCE: 1337170613Sbms case IP_UNBLOCK_SOURCE: { 1338170613Sbms struct ip_mreq_source mreqs; 1339170613Sbms 1340170613Sbms error = sooptcopyin(sopt, &mreqs, 1341170613Sbms sizeof(struct ip_mreq_source), 1342170613Sbms sizeof(struct ip_mreq_source)); 1343170613Sbms if (error) 1344170613Sbms return (error); 1345170613Sbms 1346170613Sbms gsa->sin.sin_family = AF_INET; 1347170613Sbms gsa->sin.sin_len = sizeof(struct sockaddr_in); 1348170613Sbms gsa->sin.sin_addr = mreqs.imr_multiaddr; 1349170613Sbms 1350170613Sbms ssa->sin.sin_family = AF_INET; 1351170613Sbms ssa->sin.sin_len = sizeof(struct sockaddr_in); 1352170613Sbms ssa->sin.sin_addr = mreqs.imr_sourceaddr; 1353170613Sbms 1354189592Sbms if (!in_nullhost(mreqs.imr_interface)) 1355170613Sbms INADDR_TO_IFP(mreqs.imr_interface, ifp); 1356170613Sbms 1357170613Sbms if (sopt->sopt_name == IP_BLOCK_SOURCE) 1358189592Sbms doblock = 1; 1359170613Sbms 1360189592Sbms CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p", 1361189592Sbms __func__, inet_ntoa(mreqs.imr_interface), ifp); 1362170613Sbms break; 1363170613Sbms } 1364170613Sbms 1365170613Sbms case MCAST_BLOCK_SOURCE: 1366170613Sbms case MCAST_UNBLOCK_SOURCE: 1367170613Sbms error = sooptcopyin(sopt, &gsr, 1368170613Sbms sizeof(struct group_source_req), 1369170613Sbms sizeof(struct group_source_req)); 1370170613Sbms if (error) 1371170613Sbms return (error); 1372170613Sbms 1373170613Sbms if (gsa->sin.sin_family != AF_INET || 1374170613Sbms gsa->sin.sin_len != sizeof(struct sockaddr_in)) 1375170613Sbms return (EINVAL); 1376170613Sbms 1377170613Sbms if (ssa->sin.sin_family != AF_INET || 1378170613Sbms ssa->sin.sin_len != sizeof(struct sockaddr_in)) 1379170613Sbms return (EINVAL); 1380170613Sbms 1381181803Sbz if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface) 1382170613Sbms return (EADDRNOTAVAIL); 1383170613Sbms 1384170613Sbms ifp = ifnet_byindex(gsr.gsr_interface); 1385170613Sbms 1386170613Sbms if (sopt->sopt_name == MCAST_BLOCK_SOURCE) 1387189592Sbms doblock = 1; 1388170613Sbms break; 1389170613Sbms 1390170613Sbms default: 1391189592Sbms CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d", 1392189592Sbms __func__, sopt->sopt_name); 1393170613Sbms return (EOPNOTSUPP); 1394170613Sbms break; 1395170613Sbms } 1396170613Sbms 1397170613Sbms if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 1398170613Sbms return (EINVAL); 1399170613Sbms 1400170613Sbms /* 1401170613Sbms * Check if we are actually a member of this group. 1402170613Sbms */ 1403170613Sbms imo = inp_findmoptions(inp); 1404170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 1405170613Sbms if (idx == -1 || imo->imo_mfilters == NULL) { 1406170613Sbms error = EADDRNOTAVAIL; 1407189592Sbms goto out_inp_locked; 1408170613Sbms } 1409170613Sbms 1410170613Sbms KASSERT(imo->imo_mfilters != NULL, 1411170613Sbms ("%s: imo_mfilters not allocated", __func__)); 1412170613Sbms imf = &imo->imo_mfilters[idx]; 1413189592Sbms inm = imo->imo_membership[idx]; 1414170613Sbms 1415170613Sbms /* 1416189592Sbms * Attempting to use the delta-based API on an 1417189592Sbms * non exclusive-mode membership is an error. 1418170613Sbms */ 1419189592Sbms fmode = imf->imf_st[0]; 1420189592Sbms if (fmode != MCAST_EXCLUDE) { 1421189592Sbms error = EINVAL; 1422189592Sbms goto out_inp_locked; 1423170613Sbms } 1424189592Sbms 1425189592Sbms /* 1426189592Sbms * Deal with error cases up-front: 1427189592Sbms * Asked to block, but already blocked; or 1428189592Sbms * Asked to unblock, but nothing to unblock. 1429189592Sbms * If adding a new block entry, allocate it. 1430189592Sbms */ 1431170613Sbms ims = imo_match_source(imo, idx, &ssa->sa); 1432189592Sbms if ((ims != NULL && doblock) || (ims == NULL && !doblock)) { 1433189592Sbms CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__, 1434189592Sbms inet_ntoa(ssa->sin.sin_addr), doblock ? "" : "not "); 1435189592Sbms error = EADDRNOTAVAIL; 1436189592Sbms goto out_inp_locked; 1437189592Sbms } 1438189592Sbms 1439189592Sbms INP_WLOCK_ASSERT(inp); 1440189592Sbms 1441189592Sbms /* 1442189592Sbms * Begin state merge transaction at socket layer. 1443189592Sbms */ 1444189592Sbms if (doblock) { 1445189592Sbms CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block"); 1446189592Sbms ims = imf_graft(imf, fmode, &ssa->sin); 1447189592Sbms if (ims == NULL) 1448189592Sbms error = ENOMEM; 1449170613Sbms } else { 1450189592Sbms CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow"); 1451189592Sbms error = imf_prune(imf, &ssa->sin); 1452170613Sbms } 1453170613Sbms 1454189592Sbms if (error) { 1455189592Sbms CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__); 1456189592Sbms goto out_imf_rollback; 1457189592Sbms } 1458189592Sbms 1459189592Sbms /* 1460189592Sbms * Begin state merge transaction at IGMP layer. 1461189592Sbms */ 1462189592Sbms IN_MULTI_LOCK(); 1463189592Sbms 1464189592Sbms CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 1465189592Sbms error = inm_merge(inm, imf); 1466189592Sbms if (error) { 1467189592Sbms CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__); 1468189592Sbms goto out_imf_rollback; 1469189592Sbms } 1470189592Sbms 1471189592Sbms CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 1472189592Sbms error = igmp_change_state(inm); 1473189592Sbms if (error) 1474189592Sbms CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__); 1475189592Sbms 1476189592Sbms IN_MULTI_UNLOCK(); 1477189592Sbms 1478189592Sbmsout_imf_rollback: 1479189592Sbms if (error) 1480189592Sbms imf_rollback(imf); 1481189592Sbms else 1482189592Sbms imf_commit(imf); 1483189592Sbms 1484189592Sbms imf_reap(imf); 1485189592Sbms 1486189592Sbmsout_inp_locked: 1487178285Srwatson INP_WUNLOCK(inp); 1488170613Sbms return (error); 1489170613Sbms} 1490170613Sbms 1491170613Sbms/* 1492170613Sbms * Given an inpcb, return its multicast options structure pointer. Accepts 1493170613Sbms * an unlocked inpcb pointer, but will return it locked. May sleep. 1494189592Sbms * 1495189592Sbms * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held. 1496189592Sbms * SMPng: NOTE: Returns with the INP write lock held. 1497170613Sbms */ 1498170613Sbmsstatic struct ip_moptions * 1499170613Sbmsinp_findmoptions(struct inpcb *inp) 1500170613Sbms{ 1501170613Sbms struct ip_moptions *imo; 1502170613Sbms struct in_multi **immp; 1503170613Sbms struct in_mfilter *imfp; 1504170613Sbms size_t idx; 1505170613Sbms 1506178285Srwatson INP_WLOCK(inp); 1507170613Sbms if (inp->inp_moptions != NULL) 1508170613Sbms return (inp->inp_moptions); 1509170613Sbms 1510178285Srwatson INP_WUNLOCK(inp); 1511170613Sbms 1512189592Sbms imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK); 1513189592Sbms immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS, 1514189592Sbms M_WAITOK | M_ZERO); 1515189592Sbms imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS, 1516189592Sbms M_INMFILTER, M_WAITOK); 1517170613Sbms 1518170613Sbms imo->imo_multicast_ifp = NULL; 1519170613Sbms imo->imo_multicast_addr.s_addr = INADDR_ANY; 1520170613Sbms imo->imo_multicast_vif = -1; 1521170613Sbms imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1522189357Sbms imo->imo_multicast_loop = in_mcast_loop; 1523170613Sbms imo->imo_num_memberships = 0; 1524170613Sbms imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; 1525170613Sbms imo->imo_membership = immp; 1526170613Sbms 1527170613Sbms /* Initialize per-group source filters. */ 1528189592Sbms for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++) 1529189592Sbms imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); 1530170613Sbms imo->imo_mfilters = imfp; 1531170613Sbms 1532178285Srwatson INP_WLOCK(inp); 1533170613Sbms if (inp->inp_moptions != NULL) { 1534189592Sbms free(imfp, M_INMFILTER); 1535170613Sbms free(immp, M_IPMOPTS); 1536170613Sbms free(imo, M_IPMOPTS); 1537170613Sbms return (inp->inp_moptions); 1538170613Sbms } 1539170613Sbms inp->inp_moptions = imo; 1540170613Sbms return (imo); 1541170613Sbms} 1542170613Sbms 1543170613Sbms/* 1544170613Sbms * Discard the IP multicast options (and source filters). 1545189592Sbms * 1546189592Sbms * SMPng: NOTE: assumes INP write lock is held. 1547170613Sbms */ 1548170613Sbmsvoid 1549170613Sbmsinp_freemoptions(struct ip_moptions *imo) 1550170613Sbms{ 1551170613Sbms struct in_mfilter *imf; 1552170613Sbms size_t idx, nmships; 1553170613Sbms 1554170613Sbms KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__)); 1555170613Sbms 1556170613Sbms nmships = imo->imo_num_memberships; 1557170613Sbms for (idx = 0; idx < nmships; ++idx) { 1558189592Sbms imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL; 1559189592Sbms if (imf) 1560189592Sbms imf_leave(imf); 1561189592Sbms (void)in_leavegroup(imo->imo_membership[idx], imf); 1562189592Sbms if (imf) 1563189592Sbms imf_purge(imf); 1564170613Sbms } 1565170613Sbms 1566189592Sbms if (imo->imo_mfilters) 1567189592Sbms free(imo->imo_mfilters, M_INMFILTER); 1568170613Sbms free(imo->imo_membership, M_IPMOPTS); 1569170613Sbms free(imo, M_IPMOPTS); 1570170613Sbms} 1571170613Sbms 1572170613Sbms/* 1573170613Sbms * Atomically get source filters on a socket for an IPv4 multicast group. 1574170613Sbms * Called with INP lock held; returns with lock released. 1575170613Sbms */ 1576170613Sbmsstatic int 1577170613Sbmsinp_get_source_filters(struct inpcb *inp, struct sockopt *sopt) 1578170613Sbms{ 1579183550Szec INIT_VNET_NET(curvnet); 1580170613Sbms struct __msfilterreq msfr; 1581170613Sbms sockunion_t *gsa; 1582170613Sbms struct ifnet *ifp; 1583170613Sbms struct ip_moptions *imo; 1584170613Sbms struct in_mfilter *imf; 1585189592Sbms struct ip_msource *ims; 1586189592Sbms struct in_msource *lims; 1587189592Sbms struct sockaddr_in *psin; 1588170613Sbms struct sockaddr_storage *ptss; 1589170613Sbms struct sockaddr_storage *tss; 1590170613Sbms int error; 1591189592Sbms size_t idx, nsrcs, ncsrcs; 1592170613Sbms 1593178285Srwatson INP_WLOCK_ASSERT(inp); 1594170613Sbms 1595170613Sbms imo = inp->inp_moptions; 1596170613Sbms KASSERT(imo != NULL, ("%s: null ip_moptions", __func__)); 1597170613Sbms 1598178285Srwatson INP_WUNLOCK(inp); 1599170613Sbms 1600170613Sbms error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq), 1601170613Sbms sizeof(struct __msfilterreq)); 1602170613Sbms if (error) 1603170613Sbms return (error); 1604170613Sbms 1605181803Sbz if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex) 1606170613Sbms return (EINVAL); 1607170613Sbms 1608170613Sbms ifp = ifnet_byindex(msfr.msfr_ifindex); 1609170613Sbms if (ifp == NULL) 1610170613Sbms return (EINVAL); 1611170613Sbms 1612178285Srwatson INP_WLOCK(inp); 1613170613Sbms 1614170613Sbms /* 1615170613Sbms * Lookup group on the socket. 1616170613Sbms */ 1617170613Sbms gsa = (sockunion_t *)&msfr.msfr_group; 1618170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 1619170613Sbms if (idx == -1 || imo->imo_mfilters == NULL) { 1620178285Srwatson INP_WUNLOCK(inp); 1621170613Sbms return (EADDRNOTAVAIL); 1622170613Sbms } 1623170613Sbms imf = &imo->imo_mfilters[idx]; 1624170613Sbms 1625170613Sbms /* 1626189592Sbms * Ignore memberships which are in limbo. 1627189592Sbms */ 1628189592Sbms if (imf->imf_st[1] == MCAST_UNDEFINED) { 1629189592Sbms INP_WUNLOCK(inp); 1630189592Sbms return (EAGAIN); 1631189592Sbms } 1632189592Sbms msfr.msfr_fmode = imf->imf_st[1]; 1633189592Sbms 1634189592Sbms /* 1635170613Sbms * If the user specified a buffer, copy out the source filter 1636170613Sbms * entries to userland gracefully. 1637189592Sbms * We only copy out the number of entries which userland 1638189592Sbms * has asked for, but we always tell userland how big the 1639189592Sbms * buffer really needs to be. 1640170613Sbms */ 1641170613Sbms tss = NULL; 1642170613Sbms if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) { 1643184214Sdes tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs, 1644189592Sbms M_TEMP, M_NOWAIT | M_ZERO); 1645170613Sbms if (tss == NULL) { 1646189592Sbms INP_WUNLOCK(inp); 1647189592Sbms return (ENOBUFS); 1648170613Sbms } 1649170613Sbms } 1650170613Sbms 1651189592Sbms /* 1652189592Sbms * Count number of sources in-mode at t0. 1653189592Sbms * If buffer space exists and remains, copy out source entries. 1654189592Sbms */ 1655189592Sbms nsrcs = msfr.msfr_nsrcs; 1656189592Sbms ncsrcs = 0; 1657189592Sbms ptss = tss; 1658189592Sbms RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { 1659189592Sbms lims = (struct in_msource *)ims; 1660189592Sbms if (lims->imsl_st[0] == MCAST_UNDEFINED || 1661189592Sbms lims->imsl_st[0] != imf->imf_st[0]) 1662189592Sbms continue; 1663189592Sbms ++ncsrcs; 1664189592Sbms if (tss != NULL && nsrcs-- > 0) { 1665189592Sbms psin = (struct sockaddr_in *)ptss++; 1666189592Sbms psin->sin_family = AF_INET; 1667189592Sbms psin->sin_len = sizeof(struct sockaddr_in); 1668189592Sbms psin->sin_addr.s_addr = htonl(lims->ims_haddr); 1669189592Sbms } 1670189592Sbms } 1671189592Sbms 1672178285Srwatson INP_WUNLOCK(inp); 1673170613Sbms 1674170613Sbms if (tss != NULL) { 1675170613Sbms error = copyout(tss, msfr.msfr_srcs, 1676170613Sbms sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs); 1677184205Sdes free(tss, M_TEMP); 1678189592Sbms if (error) 1679189592Sbms return (error); 1680170613Sbms } 1681170613Sbms 1682189592Sbms msfr.msfr_nsrcs = ncsrcs; 1683170613Sbms error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq)); 1684170613Sbms 1685170613Sbms return (error); 1686170613Sbms} 1687170613Sbms 1688170613Sbms/* 1689170613Sbms * Return the IP multicast options in response to user getsockopt(). 1690170613Sbms */ 1691170613Sbmsint 1692170613Sbmsinp_getmoptions(struct inpcb *inp, struct sockopt *sopt) 1693170613Sbms{ 1694183550Szec INIT_VNET_INET(curvnet); 1695170613Sbms struct ip_mreqn mreqn; 1696170613Sbms struct ip_moptions *imo; 1697170613Sbms struct ifnet *ifp; 1698170613Sbms struct in_ifaddr *ia; 1699170613Sbms int error, optval; 1700170613Sbms u_char coptval; 1701170613Sbms 1702178285Srwatson INP_WLOCK(inp); 1703170613Sbms imo = inp->inp_moptions; 1704171746Scsjp /* 1705171746Scsjp * If socket is neither of type SOCK_RAW or SOCK_DGRAM, 1706171746Scsjp * or is a divert socket, reject it. 1707171746Scsjp */ 1708171746Scsjp if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT || 1709171746Scsjp (inp->inp_socket->so_proto->pr_type != SOCK_RAW && 1710171746Scsjp inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) { 1711178285Srwatson INP_WUNLOCK(inp); 1712171746Scsjp return (EOPNOTSUPP); 1713171746Scsjp } 1714170613Sbms 1715170613Sbms error = 0; 1716170613Sbms switch (sopt->sopt_name) { 1717170613Sbms case IP_MULTICAST_VIF: 1718170613Sbms if (imo != NULL) 1719170613Sbms optval = imo->imo_multicast_vif; 1720170613Sbms else 1721170613Sbms optval = -1; 1722178285Srwatson INP_WUNLOCK(inp); 1723170613Sbms error = sooptcopyout(sopt, &optval, sizeof(int)); 1724170613Sbms break; 1725170613Sbms 1726170613Sbms case IP_MULTICAST_IF: 1727170613Sbms memset(&mreqn, 0, sizeof(struct ip_mreqn)); 1728170613Sbms if (imo != NULL) { 1729170613Sbms ifp = imo->imo_multicast_ifp; 1730189592Sbms if (!in_nullhost(imo->imo_multicast_addr)) { 1731170613Sbms mreqn.imr_address = imo->imo_multicast_addr; 1732170613Sbms } else if (ifp != NULL) { 1733170613Sbms mreqn.imr_ifindex = ifp->if_index; 1734170613Sbms IFP_TO_IA(ifp, ia); 1735170613Sbms if (ia != NULL) { 1736170613Sbms mreqn.imr_address = 1737170613Sbms IA_SIN(ia)->sin_addr; 1738170613Sbms } 1739170613Sbms } 1740170613Sbms } 1741178285Srwatson INP_WUNLOCK(inp); 1742170613Sbms if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { 1743170613Sbms error = sooptcopyout(sopt, &mreqn, 1744170613Sbms sizeof(struct ip_mreqn)); 1745170613Sbms } else { 1746170613Sbms error = sooptcopyout(sopt, &mreqn.imr_address, 1747170613Sbms sizeof(struct in_addr)); 1748170613Sbms } 1749170613Sbms break; 1750170613Sbms 1751170613Sbms case IP_MULTICAST_TTL: 1752170613Sbms if (imo == 0) 1753170613Sbms optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1754170613Sbms else 1755170613Sbms optval = coptval = imo->imo_multicast_ttl; 1756178285Srwatson INP_WUNLOCK(inp); 1757170613Sbms if (sopt->sopt_valsize == sizeof(u_char)) 1758170613Sbms error = sooptcopyout(sopt, &coptval, sizeof(u_char)); 1759170613Sbms else 1760170613Sbms error = sooptcopyout(sopt, &optval, sizeof(int)); 1761170613Sbms break; 1762170613Sbms 1763170613Sbms case IP_MULTICAST_LOOP: 1764170613Sbms if (imo == 0) 1765170613Sbms optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1766170613Sbms else 1767170613Sbms optval = coptval = imo->imo_multicast_loop; 1768178285Srwatson INP_WUNLOCK(inp); 1769170613Sbms if (sopt->sopt_valsize == sizeof(u_char)) 1770170613Sbms error = sooptcopyout(sopt, &coptval, sizeof(u_char)); 1771170613Sbms else 1772170613Sbms error = sooptcopyout(sopt, &optval, sizeof(int)); 1773170613Sbms break; 1774170613Sbms 1775170613Sbms case IP_MSFILTER: 1776170613Sbms if (imo == NULL) { 1777170613Sbms error = EADDRNOTAVAIL; 1778178285Srwatson INP_WUNLOCK(inp); 1779170613Sbms } else { 1780170613Sbms error = inp_get_source_filters(inp, sopt); 1781170613Sbms } 1782170613Sbms break; 1783170613Sbms 1784170613Sbms default: 1785178285Srwatson INP_WUNLOCK(inp); 1786170613Sbms error = ENOPROTOOPT; 1787170613Sbms break; 1788170613Sbms } 1789170613Sbms 1790170613Sbms INP_UNLOCK_ASSERT(inp); 1791170613Sbms 1792170613Sbms return (error); 1793170613Sbms} 1794170613Sbms 1795170613Sbms/* 1796189592Sbms * Look up the ifnet to use for a multicast group membership, 1797189592Sbms * given the IPv4 address of an interface, and the IPv4 group address. 1798189592Sbms * 1799189592Sbms * This routine exists to support legacy multicast applications 1800189592Sbms * which do not understand that multicast memberships are scoped to 1801189592Sbms * specific physical links in the networking stack, or which need 1802189592Sbms * to join link-scope groups before IPv4 addresses are configured. 1803189592Sbms * 1804189592Sbms * If inp is non-NULL, use this socket's current FIB number for any 1805189592Sbms * required FIB lookup. 1806189592Sbms * If ina is INADDR_ANY, look up the group address in the unicast FIB, 1807189592Sbms * and use its ifp; usually, this points to the default next-hop. 1808189592Sbms * 1809189592Sbms * If the FIB lookup fails, attempt to use the first non-loopback 1810189592Sbms * interface with multicast capability in the system as a 1811189592Sbms * last resort. The legacy IPv4 ASM API requires that we do 1812189592Sbms * this in order to allow groups to be joined when the routing 1813189592Sbms * table has not yet been populated during boot. 1814189592Sbms * 1815189592Sbms * Returns NULL if no ifp could be found. 1816189592Sbms * 1817189592Sbms * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP. 1818189592Sbms * FUTURE: Implement IPv4 source-address selection. 1819189592Sbms */ 1820189592Sbmsstatic struct ifnet * 1821189592Sbmsinp_lookup_mcast_ifp(const struct inpcb *inp, 1822189592Sbms const struct sockaddr_in *gsin, const struct in_addr ina) 1823189592Sbms{ 1824189592Sbms struct ifnet *ifp; 1825189592Sbms 1826189592Sbms KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__)); 1827189592Sbms KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)), 1828189592Sbms ("%s: not multicast", __func__)); 1829189592Sbms 1830189592Sbms ifp = NULL; 1831189592Sbms if (!in_nullhost(ina)) { 1832189592Sbms INADDR_TO_IFP(ina, ifp); 1833189592Sbms } else { 1834189592Sbms struct route ro; 1835189592Sbms 1836189592Sbms ro.ro_rt = NULL; 1837189592Sbms memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in)); 1838189592Sbms in_rtalloc_ign(&ro, 0, inp ? inp->inp_inc.inc_fibnum : 0); 1839189592Sbms if (ro.ro_rt != NULL) { 1840189592Sbms ifp = ro.ro_rt->rt_ifp; 1841189592Sbms KASSERT(ifp != NULL, ("%s: null ifp", __func__)); 1842189592Sbms RTFREE(ro.ro_rt); 1843189592Sbms } else { 1844189592Sbms struct in_ifaddr *ia; 1845189592Sbms struct ifnet *mifp; 1846189592Sbms 1847189592Sbms mifp = NULL; 1848189592Sbms TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1849189592Sbms mifp = ia->ia_ifp; 1850189592Sbms if (!(mifp->if_flags & IFF_LOOPBACK) && 1851189592Sbms (mifp->if_flags & IFF_MULTICAST)) { 1852189592Sbms ifp = mifp; 1853189592Sbms break; 1854189592Sbms } 1855189592Sbms } 1856189592Sbms } 1857189592Sbms } 1858189592Sbms 1859189592Sbms return (ifp); 1860189592Sbms} 1861189592Sbms 1862189592Sbms/* 1863170613Sbms * Join an IPv4 multicast group, possibly with a source. 1864170613Sbms */ 1865170613Sbmsstatic int 1866170613Sbmsinp_join_group(struct inpcb *inp, struct sockopt *sopt) 1867170613Sbms{ 1868183550Szec INIT_VNET_NET(curvnet); 1869183550Szec INIT_VNET_INET(curvnet); 1870170613Sbms struct group_source_req gsr; 1871170613Sbms sockunion_t *gsa, *ssa; 1872170613Sbms struct ifnet *ifp; 1873170613Sbms struct in_mfilter *imf; 1874170613Sbms struct ip_moptions *imo; 1875170613Sbms struct in_multi *inm; 1876189592Sbms struct in_msource *lims; 1877170613Sbms size_t idx; 1878189592Sbms int error, is_new; 1879170613Sbms 1880170613Sbms ifp = NULL; 1881189592Sbms imf = NULL; 1882170613Sbms error = 0; 1883189592Sbms is_new = 0; 1884170613Sbms 1885170613Sbms memset(&gsr, 0, sizeof(struct group_source_req)); 1886170613Sbms gsa = (sockunion_t *)&gsr.gsr_group; 1887170613Sbms gsa->ss.ss_family = AF_UNSPEC; 1888170613Sbms ssa = (sockunion_t *)&gsr.gsr_source; 1889170613Sbms ssa->ss.ss_family = AF_UNSPEC; 1890170613Sbms 1891170613Sbms switch (sopt->sopt_name) { 1892170613Sbms case IP_ADD_MEMBERSHIP: 1893170613Sbms case IP_ADD_SOURCE_MEMBERSHIP: { 1894170613Sbms struct ip_mreq_source mreqs; 1895170613Sbms 1896170613Sbms if (sopt->sopt_name == IP_ADD_MEMBERSHIP) { 1897170613Sbms error = sooptcopyin(sopt, &mreqs, 1898170613Sbms sizeof(struct ip_mreq), 1899170613Sbms sizeof(struct ip_mreq)); 1900170613Sbms /* 1901170613Sbms * Do argument switcharoo from ip_mreq into 1902170613Sbms * ip_mreq_source to avoid using two instances. 1903170613Sbms */ 1904170613Sbms mreqs.imr_interface = mreqs.imr_sourceaddr; 1905170613Sbms mreqs.imr_sourceaddr.s_addr = INADDR_ANY; 1906170613Sbms } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { 1907170613Sbms error = sooptcopyin(sopt, &mreqs, 1908170613Sbms sizeof(struct ip_mreq_source), 1909170613Sbms sizeof(struct ip_mreq_source)); 1910170613Sbms } 1911170613Sbms if (error) 1912170613Sbms return (error); 1913170613Sbms 1914170613Sbms gsa->sin.sin_family = AF_INET; 1915170613Sbms gsa->sin.sin_len = sizeof(struct sockaddr_in); 1916170613Sbms gsa->sin.sin_addr = mreqs.imr_multiaddr; 1917170613Sbms 1918170613Sbms if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { 1919170613Sbms ssa->sin.sin_family = AF_INET; 1920170613Sbms ssa->sin.sin_len = sizeof(struct sockaddr_in); 1921170613Sbms ssa->sin.sin_addr = mreqs.imr_sourceaddr; 1922170613Sbms } 1923170613Sbms 1924189592Sbms ifp = inp_lookup_mcast_ifp(inp, &gsa->sin, 1925189592Sbms mreqs.imr_interface); 1926189592Sbms CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p", 1927189592Sbms __func__, inet_ntoa(mreqs.imr_interface), ifp); 1928170613Sbms break; 1929170613Sbms } 1930170613Sbms 1931170613Sbms case MCAST_JOIN_GROUP: 1932170613Sbms case MCAST_JOIN_SOURCE_GROUP: 1933170613Sbms if (sopt->sopt_name == MCAST_JOIN_GROUP) { 1934170613Sbms error = sooptcopyin(sopt, &gsr, 1935170613Sbms sizeof(struct group_req), 1936170613Sbms sizeof(struct group_req)); 1937170613Sbms } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { 1938170613Sbms error = sooptcopyin(sopt, &gsr, 1939170613Sbms sizeof(struct group_source_req), 1940170613Sbms sizeof(struct group_source_req)); 1941170613Sbms } 1942170613Sbms if (error) 1943170613Sbms return (error); 1944170613Sbms 1945170613Sbms if (gsa->sin.sin_family != AF_INET || 1946170613Sbms gsa->sin.sin_len != sizeof(struct sockaddr_in)) 1947170613Sbms return (EINVAL); 1948170613Sbms 1949170613Sbms /* 1950170613Sbms * Overwrite the port field if present, as the sockaddr 1951170613Sbms * being copied in may be matched with a binary comparison. 1952170613Sbms */ 1953170613Sbms gsa->sin.sin_port = 0; 1954170613Sbms if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { 1955170613Sbms if (ssa->sin.sin_family != AF_INET || 1956170613Sbms ssa->sin.sin_len != sizeof(struct sockaddr_in)) 1957170613Sbms return (EINVAL); 1958170613Sbms ssa->sin.sin_port = 0; 1959170613Sbms } 1960170613Sbms 1961181803Sbz if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface) 1962170613Sbms return (EADDRNOTAVAIL); 1963170613Sbms ifp = ifnet_byindex(gsr.gsr_interface); 1964170613Sbms break; 1965170613Sbms 1966170613Sbms default: 1967189592Sbms CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d", 1968189592Sbms __func__, sopt->sopt_name); 1969170613Sbms return (EOPNOTSUPP); 1970170613Sbms break; 1971170613Sbms } 1972170613Sbms 1973170613Sbms if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 1974170613Sbms return (EINVAL); 1975170613Sbms 1976170613Sbms if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) 1977170613Sbms return (EADDRNOTAVAIL); 1978170613Sbms 1979170613Sbms /* 1980189592Sbms * MCAST_JOIN_SOURCE on an exclusive membership is an error. 1981189592Sbms * On an existing inclusive membership, it just adds the 1982189592Sbms * source to the filter list. 1983170613Sbms */ 1984170613Sbms imo = inp_findmoptions(inp); 1985170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 1986189592Sbms if (idx == -1) { 1987189592Sbms is_new = 1; 1988189592Sbms } else { 1989189592Sbms inm = imo->imo_membership[idx]; 1990189592Sbms imf = &imo->imo_mfilters[idx]; 1991189592Sbms if (ssa->ss.ss_family != AF_UNSPEC && 1992189592Sbms imf->imf_st[1] != MCAST_INCLUDE) { 1993189592Sbms error = EINVAL; 1994189592Sbms goto out_inp_locked; 1995189592Sbms } 1996189592Sbms lims = imo_match_source(imo, idx, &ssa->sa); 1997189592Sbms if (lims != NULL) { 1998170613Sbms error = EADDRNOTAVAIL; 1999189592Sbms goto out_inp_locked; 2000170613Sbms } 2001170613Sbms } 2002170613Sbms 2003170613Sbms /* 2004189592Sbms * Begin state merge transaction at socket layer. 2005170613Sbms */ 2006189592Sbms INP_WLOCK_ASSERT(inp); 2007189592Sbms 2008189592Sbms if (is_new) { 2009189592Sbms if (imo->imo_num_memberships == imo->imo_max_memberships) { 2010189592Sbms error = imo_grow(imo); 2011189592Sbms if (error) 2012189592Sbms goto out_inp_locked; 2013189592Sbms } 2014189592Sbms /* 2015189592Sbms * Allocate the new slot upfront so we can deal with 2016189592Sbms * grafting the new source filter in same code path 2017189592Sbms * as for join-source on existing membership. 2018189592Sbms */ 2019189592Sbms idx = imo->imo_num_memberships; 2020189592Sbms imo->imo_membership[idx] = NULL; 2021189592Sbms imo->imo_num_memberships++; 2022189592Sbms KASSERT(imo->imo_mfilters != NULL, 2023189592Sbms ("%s: imf_mfilters vector was not allocated", __func__)); 2024189592Sbms imf = &imo->imo_mfilters[idx]; 2025189592Sbms KASSERT(RB_EMPTY(&imf->imf_sources), 2026189592Sbms ("%s: imf_sources not empty", __func__)); 2027170613Sbms } 2028170613Sbms 2029170613Sbms /* 2030189592Sbms * Graft new source into filter list for this inpcb's 2031189592Sbms * membership of the group. The in_multi may not have 2032189592Sbms * been allocated yet if this is a new membership. 2033170613Sbms */ 2034189592Sbms if (ssa->ss.ss_family != AF_UNSPEC) { 2035189592Sbms /* Membership starts in IN mode */ 2036189592Sbms if (is_new) { 2037189592Sbms CTR1(KTR_IGMPV3, "%s: new join w/source", __func__); 2038189592Sbms imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE); 2039189592Sbms } else { 2040189592Sbms CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow"); 2041189592Sbms } 2042189592Sbms lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin); 2043189592Sbms if (lims == NULL) { 2044189592Sbms CTR1(KTR_IGMPV3, "%s: merge imf state failed", 2045189592Sbms __func__); 2046189592Sbms error = ENOMEM; 2047189592Sbms goto out_imo_free; 2048189592Sbms } 2049170613Sbms } 2050170613Sbms 2051170613Sbms /* 2052189592Sbms * Begin state merge transaction at IGMP layer. 2053170613Sbms */ 2054189592Sbms IN_MULTI_LOCK(); 2055170613Sbms 2056189592Sbms if (is_new) { 2057189592Sbms error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf, 2058189592Sbms &inm); 2059189592Sbms if (error) 2060189592Sbms goto out_imo_free; 2061189592Sbms imo->imo_membership[idx] = inm; 2062189592Sbms } else { 2063189592Sbms CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 2064189592Sbms error = inm_merge(inm, imf); 2065170613Sbms if (error) { 2066189592Sbms CTR1(KTR_IGMPV3, "%s: failed to merge inm state", 2067189592Sbms __func__); 2068189592Sbms goto out_imf_rollback; 2069170613Sbms } 2070189592Sbms CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 2071189592Sbms error = igmp_change_state(inm); 2072189592Sbms if (error) { 2073189592Sbms CTR1(KTR_IGMPV3, "%s: failed igmp downcall", 2074189592Sbms __func__); 2075189592Sbms goto out_imf_rollback; 2076189592Sbms } 2077170613Sbms } 2078170613Sbms 2079189592Sbms IN_MULTI_UNLOCK(); 2080189592Sbms 2081189592Sbmsout_imf_rollback: 2082189592Sbms INP_WLOCK_ASSERT(inp); 2083189592Sbms if (error) { 2084189592Sbms imf_rollback(imf); 2085189592Sbms if (is_new) 2086189592Sbms imf_purge(imf); 2087189592Sbms else 2088189592Sbms imf_reap(imf); 2089189592Sbms } else { 2090189592Sbms imf_commit(imf); 2091189592Sbms } 2092189592Sbms 2093189592Sbmsout_imo_free: 2094189592Sbms if (error && is_new) { 2095189592Sbms imo->imo_membership[idx] = NULL; 2096189592Sbms --imo->imo_num_memberships; 2097189592Sbms } 2098189592Sbms 2099189592Sbmsout_inp_locked: 2100178285Srwatson INP_WUNLOCK(inp); 2101170613Sbms return (error); 2102170613Sbms} 2103170613Sbms 2104170613Sbms/* 2105170613Sbms * Leave an IPv4 multicast group on an inpcb, possibly with a source. 2106170613Sbms */ 2107170613Sbmsstatic int 2108170613Sbmsinp_leave_group(struct inpcb *inp, struct sockopt *sopt) 2109170613Sbms{ 2110183550Szec INIT_VNET_NET(curvnet); 2111183550Szec INIT_VNET_INET(curvnet); 2112170613Sbms struct group_source_req gsr; 2113170613Sbms struct ip_mreq_source mreqs; 2114170613Sbms sockunion_t *gsa, *ssa; 2115170613Sbms struct ifnet *ifp; 2116170613Sbms struct in_mfilter *imf; 2117170613Sbms struct ip_moptions *imo; 2118189592Sbms struct in_msource *ims; 2119170613Sbms struct in_multi *inm; 2120170613Sbms size_t idx; 2121189592Sbms int error, is_final; 2122170613Sbms 2123170613Sbms ifp = NULL; 2124170613Sbms error = 0; 2125189592Sbms is_final = 1; 2126170613Sbms 2127170613Sbms memset(&gsr, 0, sizeof(struct group_source_req)); 2128170613Sbms gsa = (sockunion_t *)&gsr.gsr_group; 2129170613Sbms gsa->ss.ss_family = AF_UNSPEC; 2130170613Sbms ssa = (sockunion_t *)&gsr.gsr_source; 2131170613Sbms ssa->ss.ss_family = AF_UNSPEC; 2132170613Sbms 2133170613Sbms switch (sopt->sopt_name) { 2134170613Sbms case IP_DROP_MEMBERSHIP: 2135170613Sbms case IP_DROP_SOURCE_MEMBERSHIP: 2136170613Sbms if (sopt->sopt_name == IP_DROP_MEMBERSHIP) { 2137170613Sbms error = sooptcopyin(sopt, &mreqs, 2138170613Sbms sizeof(struct ip_mreq), 2139170613Sbms sizeof(struct ip_mreq)); 2140170613Sbms /* 2141170613Sbms * Swap interface and sourceaddr arguments, 2142170613Sbms * as ip_mreq and ip_mreq_source are laid 2143170613Sbms * out differently. 2144170613Sbms */ 2145170613Sbms mreqs.imr_interface = mreqs.imr_sourceaddr; 2146170613Sbms mreqs.imr_sourceaddr.s_addr = INADDR_ANY; 2147170613Sbms } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { 2148170613Sbms error = sooptcopyin(sopt, &mreqs, 2149170613Sbms sizeof(struct ip_mreq_source), 2150170613Sbms sizeof(struct ip_mreq_source)); 2151170613Sbms } 2152170613Sbms if (error) 2153170613Sbms return (error); 2154170613Sbms 2155170613Sbms gsa->sin.sin_family = AF_INET; 2156170613Sbms gsa->sin.sin_len = sizeof(struct sockaddr_in); 2157170613Sbms gsa->sin.sin_addr = mreqs.imr_multiaddr; 2158170613Sbms 2159170613Sbms if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { 2160170613Sbms ssa->sin.sin_family = AF_INET; 2161170613Sbms ssa->sin.sin_len = sizeof(struct sockaddr_in); 2162170613Sbms ssa->sin.sin_addr = mreqs.imr_sourceaddr; 2163170613Sbms } 2164170613Sbms 2165189592Sbms if (!in_nullhost(gsa->sin.sin_addr)) 2166170613Sbms INADDR_TO_IFP(mreqs.imr_interface, ifp); 2167170613Sbms 2168189592Sbms CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p", 2169189592Sbms __func__, inet_ntoa(mreqs.imr_interface), ifp); 2170189592Sbms 2171170613Sbms break; 2172170613Sbms 2173170613Sbms case MCAST_LEAVE_GROUP: 2174170613Sbms case MCAST_LEAVE_SOURCE_GROUP: 2175170613Sbms if (sopt->sopt_name == MCAST_LEAVE_GROUP) { 2176170613Sbms error = sooptcopyin(sopt, &gsr, 2177170613Sbms sizeof(struct group_req), 2178170613Sbms sizeof(struct group_req)); 2179170613Sbms } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { 2180170613Sbms error = sooptcopyin(sopt, &gsr, 2181170613Sbms sizeof(struct group_source_req), 2182170613Sbms sizeof(struct group_source_req)); 2183170613Sbms } 2184170613Sbms if (error) 2185170613Sbms return (error); 2186170613Sbms 2187170613Sbms if (gsa->sin.sin_family != AF_INET || 2188170613Sbms gsa->sin.sin_len != sizeof(struct sockaddr_in)) 2189170613Sbms return (EINVAL); 2190170613Sbms 2191170613Sbms if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { 2192170613Sbms if (ssa->sin.sin_family != AF_INET || 2193170613Sbms ssa->sin.sin_len != sizeof(struct sockaddr_in)) 2194170613Sbms return (EINVAL); 2195170613Sbms } 2196170613Sbms 2197181803Sbz if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface) 2198170613Sbms return (EADDRNOTAVAIL); 2199170613Sbms 2200170613Sbms ifp = ifnet_byindex(gsr.gsr_interface); 2201170613Sbms break; 2202170613Sbms 2203170613Sbms default: 2204189592Sbms CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d", 2205189592Sbms __func__, sopt->sopt_name); 2206170613Sbms return (EOPNOTSUPP); 2207170613Sbms break; 2208170613Sbms } 2209170613Sbms 2210170613Sbms if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 2211170613Sbms return (EINVAL); 2212170613Sbms 2213170613Sbms /* 2214170613Sbms * Find the membership in the membership array. 2215170613Sbms */ 2216170613Sbms imo = inp_findmoptions(inp); 2217170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 2218170613Sbms if (idx == -1) { 2219170613Sbms error = EADDRNOTAVAIL; 2220189592Sbms goto out_inp_locked; 2221170613Sbms } 2222189592Sbms inm = imo->imo_membership[idx]; 2223170613Sbms imf = &imo->imo_mfilters[idx]; 2224170613Sbms 2225189592Sbms if (ssa->ss.ss_family != AF_UNSPEC) 2226189592Sbms is_final = 0; 2227189592Sbms 2228170613Sbms /* 2229189592Sbms * Begin state merge transaction at socket layer. 2230189592Sbms */ 2231189592Sbms INP_WLOCK_ASSERT(inp); 2232189592Sbms 2233189592Sbms /* 2234170613Sbms * If we were instructed only to leave a given source, do so. 2235189592Sbms * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships. 2236170613Sbms */ 2237189592Sbms if (is_final) { 2238189592Sbms imf_leave(imf); 2239189592Sbms } else { 2240189592Sbms if (imf->imf_st[0] == MCAST_EXCLUDE) { 2241189592Sbms error = EADDRNOTAVAIL; 2242189592Sbms goto out_inp_locked; 2243170613Sbms } 2244189592Sbms ims = imo_match_source(imo, idx, &ssa->sa); 2245189592Sbms if (ims == NULL) { 2246189592Sbms CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__, 2247189592Sbms inet_ntoa(ssa->sin.sin_addr), "not "); 2248189592Sbms error = EADDRNOTAVAIL; 2249189592Sbms goto out_inp_locked; 2250189592Sbms } 2251189592Sbms CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block"); 2252189592Sbms error = imf_prune(imf, &ssa->sin); 2253189592Sbms if (error) { 2254189592Sbms CTR1(KTR_IGMPV3, "%s: merge imf state failed", 2255189592Sbms __func__); 2256189592Sbms goto out_inp_locked; 2257189592Sbms } 2258170613Sbms } 2259170613Sbms 2260170613Sbms /* 2261189592Sbms * Begin state merge transaction at IGMP layer. 2262170613Sbms */ 2263189592Sbms IN_MULTI_LOCK(); 2264170613Sbms 2265189592Sbms if (is_final) { 2266189592Sbms /* 2267189592Sbms * Give up the multicast address record to which 2268189592Sbms * the membership points. 2269189592Sbms */ 2270189592Sbms (void)in_leavegroup_locked(inm, imf); 2271189592Sbms } else { 2272189592Sbms CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 2273189592Sbms error = inm_merge(inm, imf); 2274189592Sbms if (error) { 2275189592Sbms CTR1(KTR_IGMPV3, "%s: failed to merge inm state", 2276189592Sbms __func__); 2277189592Sbms goto out_imf_rollback; 2278170613Sbms } 2279189592Sbms 2280189592Sbms CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 2281189592Sbms error = igmp_change_state(inm); 2282189592Sbms if (error) { 2283189592Sbms CTR1(KTR_IGMPV3, "%s: failed igmp downcall", 2284189592Sbms __func__); 2285189592Sbms } 2286170613Sbms } 2287170613Sbms 2288189592Sbms IN_MULTI_UNLOCK(); 2289170613Sbms 2290189592Sbmsout_imf_rollback: 2291189592Sbms if (error) 2292189592Sbms imf_rollback(imf); 2293189592Sbms else 2294189592Sbms imf_commit(imf); 2295189592Sbms 2296189592Sbms imf_reap(imf); 2297189592Sbms 2298189592Sbms if (is_final) { 2299189592Sbms /* Remove the gap in the membership array. */ 2300189592Sbms for (++idx; idx < imo->imo_num_memberships; ++idx) 2301189592Sbms imo->imo_membership[idx-1] = imo->imo_membership[idx]; 2302189592Sbms imo->imo_num_memberships--; 2303189592Sbms } 2304189592Sbms 2305189592Sbmsout_inp_locked: 2306178285Srwatson INP_WUNLOCK(inp); 2307170613Sbms return (error); 2308170613Sbms} 2309170613Sbms 2310170613Sbms/* 2311170613Sbms * Select the interface for transmitting IPv4 multicast datagrams. 2312170613Sbms * 2313170613Sbms * Either an instance of struct in_addr or an instance of struct ip_mreqn 2314170613Sbms * may be passed to this socket option. An address of INADDR_ANY or an 2315170613Sbms * interface index of 0 is used to remove a previous selection. 2316170613Sbms * When no interface is selected, one is chosen for every send. 2317170613Sbms */ 2318170613Sbmsstatic int 2319170613Sbmsinp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt) 2320170613Sbms{ 2321183550Szec INIT_VNET_NET(curvnet); 2322170613Sbms struct in_addr addr; 2323170613Sbms struct ip_mreqn mreqn; 2324170613Sbms struct ifnet *ifp; 2325170613Sbms struct ip_moptions *imo; 2326170613Sbms int error; 2327170613Sbms 2328170613Sbms if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { 2329170613Sbms /* 2330170613Sbms * An interface index was specified using the 2331170613Sbms * Linux-derived ip_mreqn structure. 2332170613Sbms */ 2333170613Sbms error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn), 2334170613Sbms sizeof(struct ip_mreqn)); 2335170613Sbms if (error) 2336170613Sbms return (error); 2337170613Sbms 2338181803Sbz if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex) 2339170613Sbms return (EINVAL); 2340170613Sbms 2341170613Sbms if (mreqn.imr_ifindex == 0) { 2342170613Sbms ifp = NULL; 2343170613Sbms } else { 2344170613Sbms ifp = ifnet_byindex(mreqn.imr_ifindex); 2345170613Sbms if (ifp == NULL) 2346170613Sbms return (EADDRNOTAVAIL); 2347170613Sbms } 2348170613Sbms } else { 2349170613Sbms /* 2350170613Sbms * An interface was specified by IPv4 address. 2351170613Sbms * This is the traditional BSD usage. 2352170613Sbms */ 2353170613Sbms error = sooptcopyin(sopt, &addr, sizeof(struct in_addr), 2354170613Sbms sizeof(struct in_addr)); 2355170613Sbms if (error) 2356170613Sbms return (error); 2357189592Sbms if (in_nullhost(addr)) { 2358170613Sbms ifp = NULL; 2359170613Sbms } else { 2360170613Sbms INADDR_TO_IFP(addr, ifp); 2361170613Sbms if (ifp == NULL) 2362170613Sbms return (EADDRNOTAVAIL); 2363170613Sbms } 2364189592Sbms CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = %s", __func__, ifp, 2365189592Sbms inet_ntoa(addr)); 2366170613Sbms } 2367170613Sbms 2368170613Sbms /* Reject interfaces which do not support multicast. */ 2369170613Sbms if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0) 2370170613Sbms return (EOPNOTSUPP); 2371170613Sbms 2372170613Sbms imo = inp_findmoptions(inp); 2373170613Sbms imo->imo_multicast_ifp = ifp; 2374170613Sbms imo->imo_multicast_addr.s_addr = INADDR_ANY; 2375178285Srwatson INP_WUNLOCK(inp); 2376170613Sbms 2377170613Sbms return (0); 2378170613Sbms} 2379170613Sbms 2380170613Sbms/* 2381170613Sbms * Atomically set source filters on a socket for an IPv4 multicast group. 2382189592Sbms * 2383189592Sbms * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held. 2384170613Sbms */ 2385170613Sbmsstatic int 2386170613Sbmsinp_set_source_filters(struct inpcb *inp, struct sockopt *sopt) 2387170613Sbms{ 2388183550Szec INIT_VNET_NET(curvnet); 2389170613Sbms struct __msfilterreq msfr; 2390170613Sbms sockunion_t *gsa; 2391170613Sbms struct ifnet *ifp; 2392170613Sbms struct in_mfilter *imf; 2393170613Sbms struct ip_moptions *imo; 2394189592Sbms struct in_multi *inm; 2395170613Sbms size_t idx; 2396170613Sbms int error; 2397170613Sbms 2398170613Sbms error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq), 2399170613Sbms sizeof(struct __msfilterreq)); 2400170613Sbms if (error) 2401170613Sbms return (error); 2402170613Sbms 2403189592Sbms if (msfr.msfr_nsrcs > in_mcast_maxsocksrc || 2404170613Sbms (msfr.msfr_fmode != MCAST_EXCLUDE && 2405170613Sbms msfr.msfr_fmode != MCAST_INCLUDE)) 2406170613Sbms return (EINVAL); 2407170613Sbms 2408170613Sbms if (msfr.msfr_group.ss_family != AF_INET || 2409170613Sbms msfr.msfr_group.ss_len != sizeof(struct sockaddr_in)) 2410170613Sbms return (EINVAL); 2411170613Sbms 2412170613Sbms gsa = (sockunion_t *)&msfr.msfr_group; 2413170613Sbms if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 2414170613Sbms return (EINVAL); 2415170613Sbms 2416170613Sbms gsa->sin.sin_port = 0; /* ignore port */ 2417170613Sbms 2418181803Sbz if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex) 2419170613Sbms return (EADDRNOTAVAIL); 2420170613Sbms 2421170613Sbms ifp = ifnet_byindex(msfr.msfr_ifindex); 2422170613Sbms if (ifp == NULL) 2423170613Sbms return (EADDRNOTAVAIL); 2424170613Sbms 2425170613Sbms /* 2426189592Sbms * Take the INP write lock. 2427170613Sbms * Check if this socket is a member of this group. 2428170613Sbms */ 2429170613Sbms imo = inp_findmoptions(inp); 2430170613Sbms idx = imo_match_group(imo, ifp, &gsa->sa); 2431170613Sbms if (idx == -1 || imo->imo_mfilters == NULL) { 2432170613Sbms error = EADDRNOTAVAIL; 2433189592Sbms goto out_inp_locked; 2434170613Sbms } 2435189592Sbms inm = imo->imo_membership[idx]; 2436170613Sbms imf = &imo->imo_mfilters[idx]; 2437170613Sbms 2438170613Sbms /* 2439189592Sbms * Begin state merge transaction at socket layer. 2440170613Sbms */ 2441189592Sbms INP_WLOCK_ASSERT(inp); 2442170613Sbms 2443189592Sbms imf->imf_st[1] = msfr.msfr_fmode; 2444189592Sbms 2445170613Sbms /* 2446170613Sbms * Apply any new source filters, if present. 2447189592Sbms * Make a copy of the user-space source vector so 2448189592Sbms * that we may copy them with a single copyin. This 2449189592Sbms * allows us to deal with page faults up-front. 2450170613Sbms */ 2451170613Sbms if (msfr.msfr_nsrcs > 0) { 2452189592Sbms struct in_msource *lims; 2453189592Sbms struct sockaddr_in *psin; 2454189592Sbms struct sockaddr_storage *kss, *pkss; 2455189592Sbms int i; 2456170613Sbms 2457178285Srwatson INP_WUNLOCK(inp); 2458189592Sbms 2459189592Sbms CTR2(KTR_IGMPV3, "%s: loading %lu source list entries", 2460189592Sbms __func__, (unsigned long)msfr.msfr_nsrcs); 2461184214Sdes kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs, 2462170613Sbms M_TEMP, M_WAITOK); 2463170613Sbms error = copyin(msfr.msfr_srcs, kss, 2464170613Sbms sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs); 2465170613Sbms if (error) { 2466184205Sdes free(kss, M_TEMP); 2467170613Sbms return (error); 2468170613Sbms } 2469170613Sbms 2470189592Sbms INP_WLOCK(inp); 2471189592Sbms 2472170613Sbms /* 2473189592Sbms * Mark all source filters as UNDEFINED at t1. 2474189592Sbms * Restore new group filter mode, as imf_leave() 2475189592Sbms * will set it to INCLUDE. 2476170613Sbms */ 2477189592Sbms imf_leave(imf); 2478189592Sbms imf->imf_st[1] = msfr.msfr_fmode; 2479189592Sbms 2480189592Sbms /* 2481189592Sbms * Update socket layer filters at t1, lazy-allocating 2482189592Sbms * new entries. This saves a bunch of memory at the 2483189592Sbms * cost of one RB_FIND() per source entry; duplicate 2484189592Sbms * entries in the msfr_nsrcs vector are ignored. 2485189592Sbms * If we encounter an error, rollback transaction. 2486189592Sbms * 2487189592Sbms * XXX This too could be replaced with a set-symmetric 2488189592Sbms * difference like loop to avoid walking from root 2489189592Sbms * every time, as the key space is common. 2490189592Sbms */ 2491189592Sbms for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) { 2492189592Sbms psin = (struct sockaddr_in *)pkss; 2493189592Sbms if (psin->sin_family != AF_INET) { 2494170613Sbms error = EAFNOSUPPORT; 2495170613Sbms break; 2496170613Sbms } 2497189592Sbms if (psin->sin_len != sizeof(struct sockaddr_in)) { 2498189592Sbms error = EINVAL; 2499189592Sbms break; 2500189592Sbms } 2501189592Sbms error = imf_get_source(imf, psin, &lims); 2502170613Sbms if (error) 2503170613Sbms break; 2504189592Sbms lims->imsl_st[1] = imf->imf_st[1]; 2505170613Sbms } 2506189592Sbms free(kss, M_TEMP); 2507189592Sbms } 2508170613Sbms 2509189592Sbms if (error) 2510189592Sbms goto out_imf_rollback; 2511170613Sbms 2512189592Sbms INP_WLOCK_ASSERT(inp); 2513189592Sbms IN_MULTI_LOCK(); 2514170613Sbms 2515170613Sbms /* 2516189592Sbms * Begin state merge transaction at IGMP layer. 2517170613Sbms */ 2518189592Sbms CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 2519189592Sbms error = inm_merge(inm, imf); 2520189592Sbms if (error) { 2521189592Sbms CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__); 2522189592Sbms goto out_imf_rollback; 2523189592Sbms } 2524170613Sbms 2525189592Sbms CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 2526189592Sbms error = igmp_change_state(inm); 2527189592Sbms if (error) 2528189592Sbms CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__); 2529189592Sbms 2530189592Sbms IN_MULTI_UNLOCK(); 2531189592Sbms 2532189592Sbmsout_imf_rollback: 2533189592Sbms if (error) 2534189592Sbms imf_rollback(imf); 2535189592Sbms else 2536189592Sbms imf_commit(imf); 2537189592Sbms 2538189592Sbms imf_reap(imf); 2539189592Sbms 2540189592Sbmsout_inp_locked: 2541178285Srwatson INP_WUNLOCK(inp); 2542170613Sbms return (error); 2543170613Sbms} 2544170613Sbms 2545170613Sbms/* 2546170613Sbms * Set the IP multicast options in response to user setsockopt(). 2547170613Sbms * 2548170613Sbms * Many of the socket options handled in this function duplicate the 2549170613Sbms * functionality of socket options in the regular unicast API. However, 2550170613Sbms * it is not possible to merge the duplicate code, because the idempotence 2551170613Sbms * of the IPv4 multicast part of the BSD Sockets API must be preserved; 2552170613Sbms * the effects of these options must be treated as separate and distinct. 2553189592Sbms * 2554189592Sbms * SMPng: XXX: Unlocked read of inp_socket believed OK. 2555189592Sbms * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING 2556189592Sbms * is refactored to no longer use vifs. 2557170613Sbms */ 2558170613Sbmsint 2559170613Sbmsinp_setmoptions(struct inpcb *inp, struct sockopt *sopt) 2560170613Sbms{ 2561170613Sbms struct ip_moptions *imo; 2562170613Sbms int error; 2563170613Sbms 2564170613Sbms error = 0; 2565170613Sbms 2566171746Scsjp /* 2567171746Scsjp * If socket is neither of type SOCK_RAW or SOCK_DGRAM, 2568171746Scsjp * or is a divert socket, reject it. 2569171746Scsjp */ 2570171746Scsjp if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT || 2571171746Scsjp (inp->inp_socket->so_proto->pr_type != SOCK_RAW && 2572189592Sbms inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) 2573171746Scsjp return (EOPNOTSUPP); 2574171746Scsjp 2575170613Sbms switch (sopt->sopt_name) { 2576170613Sbms case IP_MULTICAST_VIF: { 2577170613Sbms int vifi; 2578170613Sbms /* 2579170613Sbms * Select a multicast VIF for transmission. 2580170613Sbms * Only useful if multicast forwarding is active. 2581170613Sbms */ 2582170613Sbms if (legal_vif_num == NULL) { 2583170613Sbms error = EOPNOTSUPP; 2584170613Sbms break; 2585170613Sbms } 2586170613Sbms error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int)); 2587170613Sbms if (error) 2588170613Sbms break; 2589170613Sbms if (!legal_vif_num(vifi) && (vifi != -1)) { 2590170613Sbms error = EINVAL; 2591170613Sbms break; 2592170613Sbms } 2593170613Sbms imo = inp_findmoptions(inp); 2594170613Sbms imo->imo_multicast_vif = vifi; 2595178285Srwatson INP_WUNLOCK(inp); 2596170613Sbms break; 2597170613Sbms } 2598170613Sbms 2599170613Sbms case IP_MULTICAST_IF: 2600170613Sbms error = inp_set_multicast_if(inp, sopt); 2601170613Sbms break; 2602170613Sbms 2603170613Sbms case IP_MULTICAST_TTL: { 2604170613Sbms u_char ttl; 2605170613Sbms 2606170613Sbms /* 2607170613Sbms * Set the IP time-to-live for outgoing multicast packets. 2608170613Sbms * The original multicast API required a char argument, 2609170613Sbms * which is inconsistent with the rest of the socket API. 2610170613Sbms * We allow either a char or an int. 2611170613Sbms */ 2612170613Sbms if (sopt->sopt_valsize == sizeof(u_char)) { 2613170613Sbms error = sooptcopyin(sopt, &ttl, sizeof(u_char), 2614170613Sbms sizeof(u_char)); 2615170613Sbms if (error) 2616170613Sbms break; 2617170613Sbms } else { 2618170613Sbms u_int ittl; 2619170613Sbms 2620170613Sbms error = sooptcopyin(sopt, &ittl, sizeof(u_int), 2621170613Sbms sizeof(u_int)); 2622170613Sbms if (error) 2623170613Sbms break; 2624170613Sbms if (ittl > 255) { 2625170613Sbms error = EINVAL; 2626170613Sbms break; 2627170613Sbms } 2628170613Sbms ttl = (u_char)ittl; 2629170613Sbms } 2630170613Sbms imo = inp_findmoptions(inp); 2631170613Sbms imo->imo_multicast_ttl = ttl; 2632178285Srwatson INP_WUNLOCK(inp); 2633170613Sbms break; 2634170613Sbms } 2635170613Sbms 2636170613Sbms case IP_MULTICAST_LOOP: { 2637170613Sbms u_char loop; 2638170613Sbms 2639170613Sbms /* 2640170613Sbms * Set the loopback flag for outgoing multicast packets. 2641170613Sbms * Must be zero or one. The original multicast API required a 2642170613Sbms * char argument, which is inconsistent with the rest 2643170613Sbms * of the socket API. We allow either a char or an int. 2644170613Sbms */ 2645170613Sbms if (sopt->sopt_valsize == sizeof(u_char)) { 2646170613Sbms error = sooptcopyin(sopt, &loop, sizeof(u_char), 2647170613Sbms sizeof(u_char)); 2648170613Sbms if (error) 2649170613Sbms break; 2650170613Sbms } else { 2651170613Sbms u_int iloop; 2652170613Sbms 2653170613Sbms error = sooptcopyin(sopt, &iloop, sizeof(u_int), 2654170613Sbms sizeof(u_int)); 2655170613Sbms if (error) 2656170613Sbms break; 2657170613Sbms loop = (u_char)iloop; 2658170613Sbms } 2659170613Sbms imo = inp_findmoptions(inp); 2660170613Sbms imo->imo_multicast_loop = !!loop; 2661178285Srwatson INP_WUNLOCK(inp); 2662170613Sbms break; 2663170613Sbms } 2664170613Sbms 2665170613Sbms case IP_ADD_MEMBERSHIP: 2666170613Sbms case IP_ADD_SOURCE_MEMBERSHIP: 2667170613Sbms case MCAST_JOIN_GROUP: 2668170613Sbms case MCAST_JOIN_SOURCE_GROUP: 2669170613Sbms error = inp_join_group(inp, sopt); 2670170613Sbms break; 2671170613Sbms 2672170613Sbms case IP_DROP_MEMBERSHIP: 2673170613Sbms case IP_DROP_SOURCE_MEMBERSHIP: 2674170613Sbms case MCAST_LEAVE_GROUP: 2675170613Sbms case MCAST_LEAVE_SOURCE_GROUP: 2676170613Sbms error = inp_leave_group(inp, sopt); 2677170613Sbms break; 2678170613Sbms 2679170613Sbms case IP_BLOCK_SOURCE: 2680170613Sbms case IP_UNBLOCK_SOURCE: 2681170613Sbms case MCAST_BLOCK_SOURCE: 2682170613Sbms case MCAST_UNBLOCK_SOURCE: 2683189592Sbms error = inp_block_unblock_source(inp, sopt); 2684170613Sbms break; 2685170613Sbms 2686170613Sbms case IP_MSFILTER: 2687170613Sbms error = inp_set_source_filters(inp, sopt); 2688170613Sbms break; 2689170613Sbms 2690170613Sbms default: 2691170613Sbms error = EOPNOTSUPP; 2692170613Sbms break; 2693170613Sbms } 2694170613Sbms 2695170613Sbms INP_UNLOCK_ASSERT(inp); 2696170613Sbms 2697170613Sbms return (error); 2698170613Sbms} 2699189592Sbms 2700189592Sbms/* 2701189592Sbms * Expose IGMP's multicast filter mode and source list(s) to userland, 2702189592Sbms * keyed by (ifindex, group). 2703189592Sbms * The filter mode is written out as a uint32_t, followed by 2704189592Sbms * 0..n of struct in_addr. 2705189592Sbms * For use by ifmcstat(8). 2706189592Sbms * SMPng: NOTE: unlocked read of ifindex space. 2707189592Sbms */ 2708189592Sbmsstatic int 2709189592Sbmssysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS) 2710189592Sbms{ 2711189592Sbms INIT_VNET_NET(curvnet); 2712189592Sbms struct in_addr src, group; 2713189592Sbms struct ifnet *ifp; 2714189592Sbms struct ifmultiaddr *ifma; 2715189592Sbms struct in_multi *inm; 2716189592Sbms struct ip_msource *ims; 2717189592Sbms int *name; 2718189592Sbms int retval; 2719189592Sbms u_int namelen; 2720189592Sbms uint32_t fmode, ifindex; 2721189592Sbms 2722189592Sbms name = (int *)arg1; 2723189592Sbms namelen = arg2; 2724189592Sbms 2725189592Sbms if (req->newptr != NULL) 2726189592Sbms return (EPERM); 2727189592Sbms 2728189592Sbms if (namelen != 2) 2729189592Sbms return (EINVAL); 2730189592Sbms 2731189592Sbms ifindex = name[0]; 2732189592Sbms if (ifindex <= 0 || ifindex > V_if_index) { 2733189592Sbms CTR2(KTR_IGMPV3, "%s: ifindex %u out of range", 2734189592Sbms __func__, ifindex); 2735189592Sbms return (ENOENT); 2736189592Sbms } 2737189592Sbms 2738189592Sbms group.s_addr = name[1]; 2739189592Sbms if (!IN_MULTICAST(ntohl(group.s_addr))) { 2740189592Sbms CTR2(KTR_IGMPV3, "%s: group %s is not multicast", 2741189592Sbms __func__, inet_ntoa(group)); 2742189592Sbms return (EINVAL); 2743189592Sbms } 2744189592Sbms 2745189592Sbms ifp = ifnet_byindex(ifindex); 2746189592Sbms if (ifp == NULL) { 2747189592Sbms CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u", 2748189592Sbms __func__, ifindex); 2749189592Sbms return (ENOENT); 2750189592Sbms } 2751189592Sbms 2752189592Sbms retval = sysctl_wire_old_buffer(req, 2753189592Sbms sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr))); 2754189592Sbms if (retval) 2755189592Sbms return (retval); 2756189592Sbms 2757189592Sbms IN_MULTI_LOCK(); 2758189592Sbms 2759189592Sbms IF_ADDR_LOCK(ifp); 2760189592Sbms TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2761189592Sbms if (ifma->ifma_addr->sa_family != AF_INET || 2762189592Sbms ifma->ifma_protospec == NULL) 2763189592Sbms continue; 2764189592Sbms inm = (struct in_multi *)ifma->ifma_protospec; 2765189592Sbms if (!in_hosteq(inm->inm_addr, group)) 2766189592Sbms continue; 2767189592Sbms fmode = inm->inm_st[1].iss_fmode; 2768189592Sbms retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t)); 2769189592Sbms if (retval != 0) 2770189592Sbms break; 2771189592Sbms RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { 2772189592Sbms#ifdef KTR 2773189592Sbms struct in_addr ina; 2774189592Sbms ina.s_addr = htonl(ims->ims_haddr); 2775189592Sbms CTR2(KTR_IGMPV3, "%s: visit node %s", __func__, 2776189592Sbms inet_ntoa(ina)); 2777189592Sbms#endif 2778189592Sbms /* 2779189592Sbms * Only copy-out sources which are in-mode. 2780189592Sbms */ 2781189592Sbms if (fmode != ims_get_mode(inm, ims, 1)) { 2782189592Sbms CTR1(KTR_IGMPV3, "%s: skip non-in-mode", 2783189592Sbms __func__); 2784189592Sbms continue; 2785189592Sbms } 2786189592Sbms src.s_addr = htonl(ims->ims_haddr); 2787189592Sbms retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr)); 2788189592Sbms if (retval != 0) 2789189592Sbms break; 2790189592Sbms } 2791189592Sbms } 2792189592Sbms IF_ADDR_UNLOCK(ifp); 2793189592Sbms 2794189592Sbms IN_MULTI_UNLOCK(); 2795189592Sbms 2796189592Sbms return (retval); 2797189592Sbms} 2798189592Sbms 2799189592Sbms#ifdef KTR 2800189592Sbms 2801189592Sbmsstatic const char *inm_modestrs[] = { "un", "in", "ex" }; 2802189592Sbms 2803189592Sbmsstatic const char * 2804189592Sbmsinm_mode_str(const int mode) 2805189592Sbms{ 2806189592Sbms 2807189592Sbms if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE) 2808189592Sbms return (inm_modestrs[mode]); 2809189592Sbms return ("??"); 2810189592Sbms} 2811189592Sbms 2812189592Sbmsstatic const char *inm_statestrs[] = { 2813189592Sbms "not-member", 2814189592Sbms "silent", 2815189592Sbms "idle", 2816189592Sbms "lazy", 2817189592Sbms "sleeping", 2818189592Sbms "awakening", 2819189592Sbms "query-pending", 2820189592Sbms "sg-query-pending", 2821189592Sbms "leaving" 2822189592Sbms}; 2823189592Sbms 2824189592Sbmsstatic const char * 2825189592Sbmsinm_state_str(const int state) 2826189592Sbms{ 2827189592Sbms 2828189592Sbms if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER) 2829189592Sbms return (inm_statestrs[state]); 2830189592Sbms return ("??"); 2831189592Sbms} 2832189592Sbms 2833189592Sbms/* 2834189592Sbms * Dump an in_multi structure to the console. 2835189592Sbms */ 2836189592Sbmsvoid 2837189592Sbmsinm_print(const struct in_multi *inm) 2838189592Sbms{ 2839189592Sbms int t; 2840189592Sbms 2841189635Sbms if ((KTR_COMPILE & KTR_IGMPV3) == 0) 2842189635Sbms return; 2843189635Sbms 2844189592Sbms printf("%s: --- begin inm %p ---\n", __func__, inm); 2845189592Sbms printf("addr %s ifp %p(%s) ifma %p\n", 2846189592Sbms inet_ntoa(inm->inm_addr), 2847189592Sbms inm->inm_ifp, 2848189592Sbms inm->inm_ifp->if_xname, 2849189592Sbms inm->inm_ifma); 2850189592Sbms printf("timer %u state %s refcount %u scq.len %u\n", 2851189592Sbms inm->inm_timer, 2852189592Sbms inm_state_str(inm->inm_state), 2853189592Sbms inm->inm_refcount, 2854189592Sbms inm->inm_scq.ifq_len); 2855189592Sbms printf("igi %p nsrc %lu sctimer %u scrv %u\n", 2856189592Sbms inm->inm_igi, 2857189592Sbms inm->inm_nsrc, 2858189592Sbms inm->inm_sctimer, 2859189592Sbms inm->inm_scrv); 2860189592Sbms for (t = 0; t < 2; t++) { 2861189592Sbms printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t, 2862189592Sbms inm_mode_str(inm->inm_st[t].iss_fmode), 2863189592Sbms inm->inm_st[t].iss_asm, 2864189592Sbms inm->inm_st[t].iss_ex, 2865189592Sbms inm->inm_st[t].iss_in, 2866189592Sbms inm->inm_st[t].iss_rec); 2867189592Sbms } 2868189592Sbms printf("%s: --- end inm %p ---\n", __func__, inm); 2869189592Sbms} 2870189592Sbms 2871189592Sbms#else /* !KTR */ 2872189592Sbms 2873189592Sbmsvoid 2874189592Sbmsinm_print(const struct in_multi *inm) 2875189592Sbms{ 2876189592Sbms 2877189592Sbms} 2878189592Sbms 2879189592Sbms#endif /* KTR */ 2880189592Sbms 2881189592SbmsRB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp); 2882