in_mcast.c revision 191348
1/*- 2 * Copyright (c) 2007-2009 Bruce Simpson. 3 * Copyright (c) 2005 Robert N. M. Watson. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote 15 * products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31/* 32 * IPv4 multicast socket, group, and socket option processing module. 33 */ 34 35#include <sys/cdefs.h> 36__FBSDID("$FreeBSD: head/sys/netinet/in_mcast.c 191348 2009-04-21 09:43:51Z bms $"); 37 38#include "opt_route.h" 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/kernel.h> 43#include <sys/malloc.h> 44#include <sys/mbuf.h> 45#include <sys/protosw.h> 46#include <sys/socket.h> 47#include <sys/socketvar.h> 48#include <sys/protosw.h> 49#include <sys/sysctl.h> 50#include <sys/vimage.h> 51#include <sys/ktr.h> 52#include <sys/tree.h> 53 54#include <net/if.h> 55#include <net/if_dl.h> 56#include <net/route.h> 57#include <net/vnet.h> 58 59#include <netinet/in.h> 60#include <netinet/in_systm.h> 61#include <netinet/in_pcb.h> 62#include <netinet/in_var.h> 63#include <netinet/ip_var.h> 64#include <netinet/igmp_var.h> 65#include <netinet/vinet.h> 66 67#ifndef KTR_IGMPV3 68#define KTR_IGMPV3 KTR_SUBSYS 69#endif 70 71#ifndef __SOCKUNION_DECLARED 72union sockunion { 73 struct sockaddr_storage ss; 74 struct sockaddr sa; 75 struct sockaddr_dl sdl; 76 struct sockaddr_in sin; 77}; 78typedef union sockunion sockunion_t; 79#define __SOCKUNION_DECLARED 80#endif /* __SOCKUNION_DECLARED */ 81 82static MALLOC_DEFINE(M_INMFILTER, "in_mfilter", 83 "IPv4 multicast PCB-layer source filter"); 84static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group"); 85static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options"); 86static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource", 87 "IPv4 multicast IGMP-layer source filter"); 88 89#ifdef VIMAGE_GLOBALS 90struct in_multihead in_multihead; /* XXX now unused; retain for ABI */ 91#endif 92 93/* 94 * Locking: 95 * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK. 96 * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however 97 * it can be taken by code in net/if.c also. 98 * - ip_moptions and in_mfilter are covered by the INP_WLOCK. 99 * 100 * struct in_multi is covered by IN_MULTI_LOCK. There isn't strictly 101 * any need for in_multi itself to be virtualized -- it is bound to an ifp 102 * anyway no matter what happens. 103 */ 104struct mtx in_multi_mtx; 105MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF); 106 107/* 108 * Functions with non-static linkage defined in this file should be 109 * declared in in_var.h: 110 * imo_multi_filter() 111 * in_addmulti() 112 * in_delmulti() 113 * in_joingroup() 114 * in_joingroup_locked() 115 * in_leavegroup() 116 * in_leavegroup_locked() 117 * and ip_var.h: 118 * inp_freemoptions() 119 * inp_getmoptions() 120 * inp_setmoptions() 121 * 122 * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti() 123 * and in_delmulti(). 124 */ 125static void imf_commit(struct in_mfilter *); 126static int imf_get_source(struct in_mfilter *imf, 127 const struct sockaddr_in *psin, 128 struct in_msource **); 129static struct in_msource * 130 imf_graft(struct in_mfilter *, const uint8_t, 131 const struct sockaddr_in *); 132static void imf_leave(struct in_mfilter *); 133static int imf_prune(struct in_mfilter *, const struct sockaddr_in *); 134static void imf_purge(struct in_mfilter *); 135static void imf_rollback(struct in_mfilter *); 136static void imf_reap(struct in_mfilter *); 137static int imo_grow(struct ip_moptions *); 138static size_t imo_match_group(const struct ip_moptions *, 139 const struct ifnet *, const struct sockaddr *); 140static struct in_msource * 141 imo_match_source(const struct ip_moptions *, const size_t, 142 const struct sockaddr *); 143static void ims_merge(struct ip_msource *ims, 144 const struct in_msource *lims, const int rollback); 145static int in_getmulti(struct ifnet *, const struct in_addr *, 146 struct in_multi **); 147static int inm_get_source(struct in_multi *inm, const in_addr_t haddr, 148 const int noalloc, struct ip_msource **pims); 149static int inm_is_ifp_detached(const struct in_multi *); 150static int inm_merge(struct in_multi *, /*const*/ struct in_mfilter *); 151static void inm_purge(struct in_multi *); 152static void inm_reap(struct in_multi *); 153static struct ip_moptions * 154 inp_findmoptions(struct inpcb *); 155static int inp_get_source_filters(struct inpcb *, struct sockopt *); 156static int inp_join_group(struct inpcb *, struct sockopt *); 157static int inp_leave_group(struct inpcb *, struct sockopt *); 158static struct ifnet * 159 inp_lookup_mcast_ifp(const struct inpcb *, 160 const struct sockaddr_in *, const struct in_addr); 161static int inp_block_unblock_source(struct inpcb *, struct sockopt *); 162static int inp_set_multicast_if(struct inpcb *, struct sockopt *); 163static int inp_set_source_filters(struct inpcb *, struct sockopt *); 164static int sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS); 165 166SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0, "IPv4 multicast"); 167 168static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER; 169SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc, 170 CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxgrpsrc, 0, 171 "Max source filters per group"); 172TUNABLE_ULONG("net.inet.ip.mcast.maxgrpsrc", &in_mcast_maxgrpsrc); 173 174static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER; 175SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc, 176 CTLFLAG_RW | CTLFLAG_TUN, &in_mcast_maxsocksrc, 0, 177 "Max source filters per socket"); 178TUNABLE_ULONG("net.inet.ip.mcast.maxsocksrc", &in_mcast_maxsocksrc); 179 180int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP; 181SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RW | CTLFLAG_TUN, 182 &in_mcast_loop, 0, "Loopback multicast datagrams by default"); 183TUNABLE_INT("net.inet.ip.mcast.loop", &in_mcast_loop); 184 185SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters, 186 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters, 187 "Per-interface stack-wide source filters"); 188 189/* 190 * Inline function which wraps assertions for a valid ifp. 191 * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp 192 * is detached. 193 */ 194static int __inline 195inm_is_ifp_detached(const struct in_multi *inm) 196{ 197 struct ifnet *ifp; 198 199 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__)); 200 ifp = inm->inm_ifma->ifma_ifp; 201 if (ifp != NULL) { 202 /* 203 * Sanity check that netinet's notion of ifp is the 204 * same as net's. 205 */ 206 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__)); 207 } 208 209 return (ifp == NULL); 210} 211 212/* 213 * Initialize an in_mfilter structure to a known state at t0, t1 214 * with an empty source filter list. 215 */ 216static __inline void 217imf_init(struct in_mfilter *imf, const int st0, const int st1) 218{ 219 memset(imf, 0, sizeof(struct in_mfilter)); 220 RB_INIT(&imf->imf_sources); 221 imf->imf_st[0] = st0; 222 imf->imf_st[1] = st1; 223} 224 225/* 226 * Resize the ip_moptions vector to the next power-of-two minus 1. 227 * May be called with locks held; do not sleep. 228 */ 229static int 230imo_grow(struct ip_moptions *imo) 231{ 232 struct in_multi **nmships; 233 struct in_multi **omships; 234 struct in_mfilter *nmfilters; 235 struct in_mfilter *omfilters; 236 size_t idx; 237 size_t newmax; 238 size_t oldmax; 239 240 nmships = NULL; 241 nmfilters = NULL; 242 omships = imo->imo_membership; 243 omfilters = imo->imo_mfilters; 244 oldmax = imo->imo_max_memberships; 245 newmax = ((oldmax + 1) * 2) - 1; 246 247 if (newmax <= IP_MAX_MEMBERSHIPS) { 248 nmships = (struct in_multi **)realloc(omships, 249 sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT); 250 nmfilters = (struct in_mfilter *)realloc(omfilters, 251 sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT); 252 if (nmships != NULL && nmfilters != NULL) { 253 /* Initialize newly allocated source filter heads. */ 254 for (idx = oldmax; idx < newmax; idx++) { 255 imf_init(&nmfilters[idx], MCAST_UNDEFINED, 256 MCAST_EXCLUDE); 257 } 258 imo->imo_max_memberships = newmax; 259 imo->imo_membership = nmships; 260 imo->imo_mfilters = nmfilters; 261 } 262 } 263 264 if (nmships == NULL || nmfilters == NULL) { 265 if (nmships != NULL) 266 free(nmships, M_IPMOPTS); 267 if (nmfilters != NULL) 268 free(nmfilters, M_INMFILTER); 269 return (ETOOMANYREFS); 270 } 271 272 return (0); 273} 274 275/* 276 * Find an IPv4 multicast group entry for this ip_moptions instance 277 * which matches the specified group, and optionally an interface. 278 * Return its index into the array, or -1 if not found. 279 */ 280static size_t 281imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp, 282 const struct sockaddr *group) 283{ 284 const struct sockaddr_in *gsin; 285 struct in_multi **pinm; 286 int idx; 287 int nmships; 288 289 gsin = (const struct sockaddr_in *)group; 290 291 /* The imo_membership array may be lazy allocated. */ 292 if (imo->imo_membership == NULL || imo->imo_num_memberships == 0) 293 return (-1); 294 295 nmships = imo->imo_num_memberships; 296 pinm = &imo->imo_membership[0]; 297 for (idx = 0; idx < nmships; idx++, pinm++) { 298 if (*pinm == NULL) 299 continue; 300 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) && 301 in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) { 302 break; 303 } 304 } 305 if (idx >= nmships) 306 idx = -1; 307 308 return (idx); 309} 310 311/* 312 * Find an IPv4 multicast source entry for this imo which matches 313 * the given group index for this socket, and source address. 314 * 315 * NOTE: This does not check if the entry is in-mode, merely if 316 * it exists, which may not be the desired behaviour. 317 */ 318static struct in_msource * 319imo_match_source(const struct ip_moptions *imo, const size_t gidx, 320 const struct sockaddr *src) 321{ 322 struct ip_msource find; 323 struct in_mfilter *imf; 324 struct ip_msource *ims; 325 const sockunion_t *psa; 326 327 KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__)); 328 KASSERT(gidx != -1 && gidx < imo->imo_num_memberships, 329 ("%s: invalid index %d\n", __func__, (int)gidx)); 330 331 /* The imo_mfilters array may be lazy allocated. */ 332 if (imo->imo_mfilters == NULL) 333 return (NULL); 334 imf = &imo->imo_mfilters[gidx]; 335 336 /* Source trees are keyed in host byte order. */ 337 psa = (const sockunion_t *)src; 338 find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr); 339 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); 340 341 return ((struct in_msource *)ims); 342} 343 344/* 345 * Perform filtering for multicast datagrams on a socket by group and source. 346 * 347 * Returns 0 if a datagram should be allowed through, or various error codes 348 * if the socket was not a member of the group, or the source was muted, etc. 349 */ 350int 351imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp, 352 const struct sockaddr *group, const struct sockaddr *src) 353{ 354 size_t gidx; 355 struct in_msource *ims; 356 int mode; 357 358 KASSERT(ifp != NULL, ("%s: null ifp", __func__)); 359 360 gidx = imo_match_group(imo, ifp, group); 361 if (gidx == -1) 362 return (MCAST_NOTGMEMBER); 363 364 /* 365 * Check if the source was included in an (S,G) join. 366 * Allow reception on exclusive memberships by default, 367 * reject reception on inclusive memberships by default. 368 * Exclude source only if an in-mode exclude filter exists. 369 * Include source only if an in-mode include filter exists. 370 * NOTE: We are comparing group state here at IGMP t1 (now) 371 * with socket-layer t0 (since last downcall). 372 */ 373 mode = imo->imo_mfilters[gidx].imf_st[1]; 374 ims = imo_match_source(imo, gidx, src); 375 376 if ((ims == NULL && mode == MCAST_INCLUDE) || 377 (ims != NULL && ims->imsl_st[0] != mode)) 378 return (MCAST_NOTSMEMBER); 379 380 return (MCAST_PASS); 381} 382 383/* 384 * Find and return a reference to an in_multi record for (ifp, group), 385 * and bump its reference count. 386 * If one does not exist, try to allocate it, and update link-layer multicast 387 * filters on ifp to listen for group. 388 * Assumes the IN_MULTI lock is held across the call. 389 * Return 0 if successful, otherwise return an appropriate error code. 390 */ 391static int 392in_getmulti(struct ifnet *ifp, const struct in_addr *group, 393 struct in_multi **pinm) 394{ 395 INIT_VNET_INET(ifp->if_vnet); 396 struct sockaddr_in gsin; 397 struct ifmultiaddr *ifma; 398 struct in_ifinfo *ii; 399 struct in_multi *inm; 400 int error; 401 402 IN_MULTI_LOCK_ASSERT(); 403 404 ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET]; 405 406 inm = inm_lookup(ifp, *group); 407 if (inm != NULL) { 408 /* 409 * If we already joined this group, just bump the 410 * refcount and return it. 411 */ 412 KASSERT(inm->inm_refcount >= 1, 413 ("%s: bad refcount %d", __func__, inm->inm_refcount)); 414 ++inm->inm_refcount; 415 *pinm = inm; 416 return (0); 417 } 418 419 memset(&gsin, 0, sizeof(gsin)); 420 gsin.sin_family = AF_INET; 421 gsin.sin_len = sizeof(struct sockaddr_in); 422 gsin.sin_addr = *group; 423 424 /* 425 * Check if a link-layer group is already associated 426 * with this network-layer group on the given ifnet. 427 */ 428 error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma); 429 if (error != 0) 430 return (error); 431 432 /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */ 433 IF_ADDR_LOCK(ifp); 434 435 /* 436 * If something other than netinet is occupying the link-layer 437 * group, print a meaningful error message and back out of 438 * the allocation. 439 * Otherwise, bump the refcount on the existing network-layer 440 * group association and return it. 441 */ 442 if (ifma->ifma_protospec != NULL) { 443 inm = (struct in_multi *)ifma->ifma_protospec; 444#ifdef INVARIANTS 445 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr", 446 __func__)); 447 KASSERT(ifma->ifma_addr->sa_family == AF_INET, 448 ("%s: ifma not AF_INET", __func__)); 449 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__)); 450 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp || 451 !in_hosteq(inm->inm_addr, *group)) 452 panic("%s: ifma %p is inconsistent with %p (%s)", 453 __func__, ifma, inm, inet_ntoa(*group)); 454#endif 455 ++inm->inm_refcount; 456 *pinm = inm; 457 IF_ADDR_UNLOCK(ifp); 458 return (0); 459 } 460 461 IF_ADDR_LOCK_ASSERT(ifp); 462 463 /* 464 * A new in_multi record is needed; allocate and initialize it. 465 * We DO NOT perform an IGMP join as the in_ layer may need to 466 * push an initial source list down to IGMP to support SSM. 467 * 468 * The initial source filter state is INCLUDE, {} as per the RFC. 469 */ 470 inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO); 471 if (inm == NULL) { 472 if_delmulti_ifma(ifma); 473 IF_ADDR_UNLOCK(ifp); 474 return (ENOMEM); 475 } 476 inm->inm_addr = *group; 477 inm->inm_ifp = ifp; 478 inm->inm_igi = ii->ii_igmp; 479 inm->inm_ifma = ifma; 480 inm->inm_refcount = 1; 481 inm->inm_state = IGMP_NOT_MEMBER; 482 483 /* 484 * Pending state-changes per group are subject to a bounds check. 485 */ 486 IFQ_SET_MAXLEN(&inm->inm_scq, IGMP_MAX_STATE_CHANGES); 487 488 inm->inm_st[0].iss_fmode = MCAST_UNDEFINED; 489 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; 490 RB_INIT(&inm->inm_srcs); 491 492 ifma->ifma_protospec = inm; 493 494 *pinm = inm; 495 496 IF_ADDR_UNLOCK(ifp); 497 return (0); 498} 499 500/* 501 * Drop a reference to an in_multi record. 502 * 503 * If the refcount drops to 0, free the in_multi record and 504 * delete the underlying link-layer membership. 505 */ 506void 507inm_release_locked(struct in_multi *inm) 508{ 509 struct ifmultiaddr *ifma; 510 511 IN_MULTI_LOCK_ASSERT(); 512 513 CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount); 514 515 if (--inm->inm_refcount > 0) { 516 CTR2(KTR_IGMPV3, "%s: refcount is now %d", __func__, 517 inm->inm_refcount); 518 return; 519 } 520 521 CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm); 522 523 ifma = inm->inm_ifma; 524 525 /* XXX this access is not covered by IF_ADDR_LOCK */ 526 CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma); 527 KASSERT(ifma->ifma_protospec == inm, 528 ("%s: ifma_protospec != inm", __func__)); 529 ifma->ifma_protospec = NULL; 530 531 inm_purge(inm); 532 533 free(inm, M_IPMADDR); 534 535 if_delmulti_ifma(ifma); 536} 537 538/* 539 * Clear recorded source entries for a group. 540 * Used by the IGMP code. Caller must hold the IN_MULTI lock. 541 * FIXME: Should reap. 542 */ 543void 544inm_clear_recorded(struct in_multi *inm) 545{ 546 struct ip_msource *ims; 547 548 IN_MULTI_LOCK_ASSERT(); 549 550 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { 551 if (ims->ims_stp) { 552 ims->ims_stp = 0; 553 --inm->inm_st[1].iss_rec; 554 } 555 } 556 KASSERT(inm->inm_st[1].iss_rec == 0, 557 ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec)); 558} 559 560/* 561 * Record a source as pending for a Source-Group IGMPv3 query. 562 * This lives here as it modifies the shared tree. 563 * 564 * inm is the group descriptor. 565 * naddr is the address of the source to record in network-byte order. 566 * 567 * If the net.inet.igmp.sgalloc sysctl is non-zero, we will 568 * lazy-allocate a source node in response to an SG query. 569 * Otherwise, no allocation is performed. This saves some memory 570 * with the trade-off that the source will not be reported to the 571 * router if joined in the window between the query response and 572 * the group actually being joined on the local host. 573 * 574 * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed. 575 * This turns off the allocation of a recorded source entry if 576 * the group has not been joined. 577 * 578 * Return 0 if the source didn't exist or was already marked as recorded. 579 * Return 1 if the source was marked as recorded by this function. 580 * Return <0 if any error occured (negated errno code). 581 */ 582int 583inm_record_source(struct in_multi *inm, const in_addr_t naddr) 584{ 585 struct ip_msource find; 586 struct ip_msource *ims, *nims; 587 588 IN_MULTI_LOCK_ASSERT(); 589 590 find.ims_haddr = ntohl(naddr); 591 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find); 592 if (ims && ims->ims_stp) 593 return (0); 594 if (ims == NULL) { 595 if (inm->inm_nsrc == in_mcast_maxgrpsrc) 596 return (-ENOSPC); 597 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE, 598 M_NOWAIT | M_ZERO); 599 if (nims == NULL) 600 return (-ENOMEM); 601 nims->ims_haddr = find.ims_haddr; 602 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims); 603 ++inm->inm_nsrc; 604 ims = nims; 605 } 606 607 /* 608 * Mark the source as recorded and update the recorded 609 * source count. 610 */ 611 ++ims->ims_stp; 612 ++inm->inm_st[1].iss_rec; 613 614 return (1); 615} 616 617/* 618 * Return a pointer to an in_msource owned by an in_mfilter, 619 * given its source address. 620 * Lazy-allocate if needed. If this is a new entry its filter state is 621 * undefined at t0. 622 * 623 * imf is the filter set being modified. 624 * haddr is the source address in *host* byte-order. 625 * 626 * SMPng: May be called with locks held; malloc must not block. 627 */ 628static int 629imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin, 630 struct in_msource **plims) 631{ 632 struct ip_msource find; 633 struct ip_msource *ims, *nims; 634 struct in_msource *lims; 635 int error; 636 637 error = 0; 638 ims = NULL; 639 lims = NULL; 640 641 /* key is host byte order */ 642 find.ims_haddr = ntohl(psin->sin_addr.s_addr); 643 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); 644 lims = (struct in_msource *)ims; 645 if (lims == NULL) { 646 if (imf->imf_nsrc == in_mcast_maxsocksrc) 647 return (ENOSPC); 648 nims = malloc(sizeof(struct in_msource), M_INMFILTER, 649 M_NOWAIT | M_ZERO); 650 if (nims == NULL) 651 return (ENOMEM); 652 lims = (struct in_msource *)nims; 653 lims->ims_haddr = find.ims_haddr; 654 lims->imsl_st[0] = MCAST_UNDEFINED; 655 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims); 656 ++imf->imf_nsrc; 657 } 658 659 *plims = lims; 660 661 return (error); 662} 663 664/* 665 * Graft a source entry into an existing socket-layer filter set, 666 * maintaining any required invariants and checking allocations. 667 * 668 * The source is marked as being in the new filter mode at t1. 669 * 670 * Return the pointer to the new node, otherwise return NULL. 671 */ 672static struct in_msource * 673imf_graft(struct in_mfilter *imf, const uint8_t st1, 674 const struct sockaddr_in *psin) 675{ 676 struct ip_msource *nims; 677 struct in_msource *lims; 678 679 nims = malloc(sizeof(struct in_msource), M_INMFILTER, 680 M_NOWAIT | M_ZERO); 681 if (nims == NULL) 682 return (NULL); 683 lims = (struct in_msource *)nims; 684 lims->ims_haddr = ntohl(psin->sin_addr.s_addr); 685 lims->imsl_st[0] = MCAST_UNDEFINED; 686 lims->imsl_st[1] = st1; 687 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims); 688 ++imf->imf_nsrc; 689 690 return (lims); 691} 692 693/* 694 * Prune a source entry from an existing socket-layer filter set, 695 * maintaining any required invariants and checking allocations. 696 * 697 * The source is marked as being left at t1, it is not freed. 698 * 699 * Return 0 if no error occurred, otherwise return an errno value. 700 */ 701static int 702imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin) 703{ 704 struct ip_msource find; 705 struct ip_msource *ims; 706 struct in_msource *lims; 707 708 /* key is host byte order */ 709 find.ims_haddr = ntohl(psin->sin_addr.s_addr); 710 ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find); 711 if (ims == NULL) 712 return (ENOENT); 713 lims = (struct in_msource *)ims; 714 lims->imsl_st[1] = MCAST_UNDEFINED; 715 return (0); 716} 717 718/* 719 * Revert socket-layer filter set deltas at t1 to t0 state. 720 */ 721static void 722imf_rollback(struct in_mfilter *imf) 723{ 724 struct ip_msource *ims, *tims; 725 struct in_msource *lims; 726 727 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { 728 lims = (struct in_msource *)ims; 729 if (lims->imsl_st[0] == lims->imsl_st[1]) { 730 /* no change at t1 */ 731 continue; 732 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) { 733 /* revert change to existing source at t1 */ 734 lims->imsl_st[1] = lims->imsl_st[0]; 735 } else { 736 /* revert source added t1 */ 737 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims); 738 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); 739 free(ims, M_INMFILTER); 740 imf->imf_nsrc--; 741 } 742 } 743 imf->imf_st[1] = imf->imf_st[0]; 744} 745 746/* 747 * Mark socket-layer filter set as INCLUDE {} at t1. 748 */ 749static void 750imf_leave(struct in_mfilter *imf) 751{ 752 struct ip_msource *ims; 753 struct in_msource *lims; 754 755 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { 756 lims = (struct in_msource *)ims; 757 lims->imsl_st[1] = MCAST_UNDEFINED; 758 } 759 imf->imf_st[1] = MCAST_INCLUDE; 760} 761 762/* 763 * Mark socket-layer filter set deltas as committed. 764 */ 765static void 766imf_commit(struct in_mfilter *imf) 767{ 768 struct ip_msource *ims; 769 struct in_msource *lims; 770 771 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { 772 lims = (struct in_msource *)ims; 773 lims->imsl_st[0] = lims->imsl_st[1]; 774 } 775 imf->imf_st[0] = imf->imf_st[1]; 776} 777 778/* 779 * Reap unreferenced sources from socket-layer filter set. 780 */ 781static void 782imf_reap(struct in_mfilter *imf) 783{ 784 struct ip_msource *ims, *tims; 785 struct in_msource *lims; 786 787 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { 788 lims = (struct in_msource *)ims; 789 if ((lims->imsl_st[0] == MCAST_UNDEFINED) && 790 (lims->imsl_st[1] == MCAST_UNDEFINED)) { 791 CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims); 792 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); 793 free(ims, M_INMFILTER); 794 imf->imf_nsrc--; 795 } 796 } 797} 798 799/* 800 * Purge socket-layer filter set. 801 */ 802static void 803imf_purge(struct in_mfilter *imf) 804{ 805 struct ip_msource *ims, *tims; 806 807 RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) { 808 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims); 809 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims); 810 free(ims, M_INMFILTER); 811 imf->imf_nsrc--; 812 } 813 imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED; 814 KASSERT(RB_EMPTY(&imf->imf_sources), 815 ("%s: imf_sources not empty", __func__)); 816} 817 818/* 819 * Look up a source filter entry for a multicast group. 820 * 821 * inm is the group descriptor to work with. 822 * haddr is the host-byte-order IPv4 address to look up. 823 * noalloc may be non-zero to suppress allocation of sources. 824 * *pims will be set to the address of the retrieved or allocated source. 825 * 826 * SMPng: NOTE: may be called with locks held. 827 * Return 0 if successful, otherwise return a non-zero error code. 828 */ 829static int 830inm_get_source(struct in_multi *inm, const in_addr_t haddr, 831 const int noalloc, struct ip_msource **pims) 832{ 833 struct ip_msource find; 834 struct ip_msource *ims, *nims; 835#ifdef KTR 836 struct in_addr ia; 837#endif 838 839 find.ims_haddr = haddr; 840 ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find); 841 if (ims == NULL && !noalloc) { 842 if (inm->inm_nsrc == in_mcast_maxgrpsrc) 843 return (ENOSPC); 844 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE, 845 M_NOWAIT | M_ZERO); 846 if (nims == NULL) 847 return (ENOMEM); 848 nims->ims_haddr = haddr; 849 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims); 850 ++inm->inm_nsrc; 851 ims = nims; 852#ifdef KTR 853 ia.s_addr = htonl(haddr); 854 CTR3(KTR_IGMPV3, "%s: allocated %s as %p", __func__, 855 inet_ntoa(ia), ims); 856#endif 857 } 858 859 *pims = ims; 860 return (0); 861} 862 863/* 864 * Merge socket-layer source into IGMP-layer source. 865 * If rollback is non-zero, perform the inverse of the merge. 866 */ 867static void 868ims_merge(struct ip_msource *ims, const struct in_msource *lims, 869 const int rollback) 870{ 871 int n = rollback ? -1 : 1; 872#ifdef KTR 873 struct in_addr ia; 874 875 ia.s_addr = htonl(ims->ims_haddr); 876#endif 877 878 if (lims->imsl_st[0] == MCAST_EXCLUDE) { 879 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on %s", 880 __func__, n, inet_ntoa(ia)); 881 ims->ims_st[1].ex -= n; 882 } else if (lims->imsl_st[0] == MCAST_INCLUDE) { 883 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on %s", 884 __func__, n, inet_ntoa(ia)); 885 ims->ims_st[1].in -= n; 886 } 887 888 if (lims->imsl_st[1] == MCAST_EXCLUDE) { 889 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on %s", 890 __func__, n, inet_ntoa(ia)); 891 ims->ims_st[1].ex += n; 892 } else if (lims->imsl_st[1] == MCAST_INCLUDE) { 893 CTR3(KTR_IGMPV3, "%s: t1 in += %d on %s", 894 __func__, n, inet_ntoa(ia)); 895 ims->ims_st[1].in += n; 896 } 897} 898 899/* 900 * Atomically update the global in_multi state, when a membership's 901 * filter list is being updated in any way. 902 * 903 * imf is the per-inpcb-membership group filter pointer. 904 * A fake imf may be passed for in-kernel consumers. 905 * 906 * XXX This is a candidate for a set-symmetric-difference style loop 907 * which would eliminate the repeated lookup from root of ims nodes, 908 * as they share the same key space. 909 * 910 * If any error occurred this function will back out of refcounts 911 * and return a non-zero value. 912 */ 913static int 914inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf) 915{ 916 struct ip_msource *ims, *nims; 917 struct in_msource *lims; 918 int schanged, error; 919 int nsrc0, nsrc1; 920 921 schanged = 0; 922 error = 0; 923 nsrc1 = nsrc0 = 0; 924 925 /* 926 * Update the source filters first, as this may fail. 927 * Maintain count of in-mode filters at t0, t1. These are 928 * used to work out if we transition into ASM mode or not. 929 * Maintain a count of source filters whose state was 930 * actually modified by this operation. 931 */ 932 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { 933 lims = (struct in_msource *)ims; 934 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++; 935 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++; 936 if (lims->imsl_st[0] == lims->imsl_st[1]) continue; 937 error = inm_get_source(inm, lims->ims_haddr, 0, &nims); 938 ++schanged; 939 if (error) 940 break; 941 ims_merge(nims, lims, 0); 942 } 943 if (error) { 944 struct ip_msource *bims; 945 946 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) { 947 lims = (struct in_msource *)ims; 948 if (lims->imsl_st[0] == lims->imsl_st[1]) 949 continue; 950 (void)inm_get_source(inm, lims->ims_haddr, 1, &bims); 951 if (bims == NULL) 952 continue; 953 ims_merge(bims, lims, 1); 954 } 955 goto out_reap; 956 } 957 958 CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1", 959 __func__, nsrc0, nsrc1); 960 961 /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */ 962 if (imf->imf_st[0] == imf->imf_st[1] && 963 imf->imf_st[1] == MCAST_INCLUDE) { 964 if (nsrc1 == 0) { 965 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__); 966 --inm->inm_st[1].iss_in; 967 } 968 } 969 970 /* Handle filter mode transition on socket. */ 971 if (imf->imf_st[0] != imf->imf_st[1]) { 972 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d", 973 __func__, imf->imf_st[0], imf->imf_st[1]); 974 975 if (imf->imf_st[0] == MCAST_EXCLUDE) { 976 CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__); 977 --inm->inm_st[1].iss_ex; 978 } else if (imf->imf_st[0] == MCAST_INCLUDE) { 979 CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__); 980 --inm->inm_st[1].iss_in; 981 } 982 983 if (imf->imf_st[1] == MCAST_EXCLUDE) { 984 CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__); 985 inm->inm_st[1].iss_ex++; 986 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) { 987 CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__); 988 inm->inm_st[1].iss_in++; 989 } 990 } 991 992 /* 993 * Track inm filter state in terms of listener counts. 994 * If there are any exclusive listeners, stack-wide 995 * membership is exclusive. 996 * Otherwise, if only inclusive listeners, stack-wide is inclusive. 997 * If no listeners remain, state is undefined at t1, 998 * and the IGMP lifecycle for this group should finish. 999 */ 1000 if (inm->inm_st[1].iss_ex > 0) { 1001 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__); 1002 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE; 1003 } else if (inm->inm_st[1].iss_in > 0) { 1004 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__); 1005 inm->inm_st[1].iss_fmode = MCAST_INCLUDE; 1006 } else { 1007 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__); 1008 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED; 1009 } 1010 1011 /* Decrement ASM listener count on transition out of ASM mode. */ 1012 if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) { 1013 if ((imf->imf_st[1] != MCAST_EXCLUDE) || 1014 (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) 1015 CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__); 1016 --inm->inm_st[1].iss_asm; 1017 } 1018 1019 /* Increment ASM listener count on transition to ASM mode. */ 1020 if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) { 1021 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__); 1022 inm->inm_st[1].iss_asm++; 1023 } 1024 1025 CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm); 1026 inm_print(inm); 1027 1028out_reap: 1029 if (schanged > 0) { 1030 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__); 1031 inm_reap(inm); 1032 } 1033 return (error); 1034} 1035 1036/* 1037 * Mark an in_multi's filter set deltas as committed. 1038 * Called by IGMP after a state change has been enqueued. 1039 */ 1040void 1041inm_commit(struct in_multi *inm) 1042{ 1043 struct ip_msource *ims; 1044 1045 CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm); 1046 CTR1(KTR_IGMPV3, "%s: pre commit:", __func__); 1047 inm_print(inm); 1048 1049 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { 1050 ims->ims_st[0] = ims->ims_st[1]; 1051 } 1052 inm->inm_st[0] = inm->inm_st[1]; 1053} 1054 1055/* 1056 * Reap unreferenced nodes from an in_multi's filter set. 1057 */ 1058static void 1059inm_reap(struct in_multi *inm) 1060{ 1061 struct ip_msource *ims, *tims; 1062 1063 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) { 1064 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 || 1065 ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 || 1066 ims->ims_stp != 0) 1067 continue; 1068 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims); 1069 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims); 1070 free(ims, M_IPMSOURCE); 1071 inm->inm_nsrc--; 1072 } 1073} 1074 1075/* 1076 * Purge all source nodes from an in_multi's filter set. 1077 */ 1078static void 1079inm_purge(struct in_multi *inm) 1080{ 1081 struct ip_msource *ims, *tims; 1082 1083 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) { 1084 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims); 1085 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims); 1086 free(ims, M_IPMSOURCE); 1087 inm->inm_nsrc--; 1088 } 1089} 1090 1091/* 1092 * Join a multicast group; unlocked entry point. 1093 * 1094 * SMPng: XXX: in_joingroup() is called from in_control() when Giant 1095 * is not held. Fortunately, ifp is unlikely to have been detached 1096 * at this point, so we assume it's OK to recurse. 1097 */ 1098int 1099in_joingroup(struct ifnet *ifp, const struct in_addr *gina, 1100 /*const*/ struct in_mfilter *imf, struct in_multi **pinm) 1101{ 1102 int error; 1103 1104 IN_MULTI_LOCK(); 1105 error = in_joingroup_locked(ifp, gina, imf, pinm); 1106 IN_MULTI_UNLOCK(); 1107 1108 return (error); 1109} 1110 1111/* 1112 * Join a multicast group; real entry point. 1113 * 1114 * Only preserves atomicity at inm level. 1115 * NOTE: imf argument cannot be const due to sys/tree.h limitations. 1116 * 1117 * If the IGMP downcall fails, the group is not joined, and an error 1118 * code is returned. 1119 */ 1120int 1121in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina, 1122 /*const*/ struct in_mfilter *imf, struct in_multi **pinm) 1123{ 1124 struct in_mfilter timf; 1125 struct in_multi *inm; 1126 int error; 1127 1128 IN_MULTI_LOCK_ASSERT(); 1129 1130 CTR4(KTR_IGMPV3, "%s: join %s on %p(%s))", __func__, 1131 inet_ntoa(*gina), ifp, ifp->if_xname); 1132 1133 error = 0; 1134 inm = NULL; 1135 1136 /* 1137 * If no imf was specified (i.e. kernel consumer), 1138 * fake one up and assume it is an ASM join. 1139 */ 1140 if (imf == NULL) { 1141 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE); 1142 imf = &timf; 1143 } 1144 1145 error = in_getmulti(ifp, gina, &inm); 1146 if (error) { 1147 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__); 1148 return (error); 1149 } 1150 1151 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 1152 error = inm_merge(inm, imf); 1153 if (error) { 1154 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__); 1155 goto out_inm_release; 1156 } 1157 1158 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 1159 error = igmp_change_state(inm); 1160 if (error) { 1161 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__); 1162 goto out_inm_release; 1163 } 1164 1165out_inm_release: 1166 if (error) { 1167 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm); 1168 inm_release_locked(inm); 1169 } else { 1170 *pinm = inm; 1171 } 1172 1173 return (error); 1174} 1175 1176/* 1177 * Leave a multicast group; unlocked entry point. 1178 */ 1179int 1180in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf) 1181{ 1182 struct ifnet *ifp; 1183 int error; 1184 1185 ifp = inm->inm_ifp; 1186 1187 IN_MULTI_LOCK(); 1188 error = in_leavegroup_locked(inm, imf); 1189 IN_MULTI_UNLOCK(); 1190 1191 return (error); 1192} 1193 1194/* 1195 * Leave a multicast group; real entry point. 1196 * All source filters will be expunged. 1197 * 1198 * Only preserves atomicity at inm level. 1199 * 1200 * Holding the write lock for the INP which contains imf 1201 * is highly advisable. We can't assert for it as imf does not 1202 * contain a back-pointer to the owning inp. 1203 * 1204 * Note: This is not the same as inm_release(*) as this function also 1205 * makes a state change downcall into IGMP. 1206 */ 1207int 1208in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf) 1209{ 1210 struct in_mfilter timf; 1211 int error; 1212 1213 error = 0; 1214 1215 IN_MULTI_LOCK_ASSERT(); 1216 1217 CTR5(KTR_IGMPV3, "%s: leave inm %p, %s/%s, imf %p", __func__, 1218 inm, inet_ntoa(inm->inm_addr), 1219 (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname), 1220 imf); 1221 1222 /* 1223 * If no imf was specified (i.e. kernel consumer), 1224 * fake one up and assume it is an ASM join. 1225 */ 1226 if (imf == NULL) { 1227 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED); 1228 imf = &timf; 1229 } 1230 1231 /* 1232 * Begin state merge transaction at IGMP layer. 1233 * 1234 * As this particular invocation should not cause any memory 1235 * to be allocated, and there is no opportunity to roll back 1236 * the transaction, it MUST NOT fail. 1237 */ 1238 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 1239 error = inm_merge(inm, imf); 1240 KASSERT(error == 0, ("%s: failed to merge inm state", __func__)); 1241 1242 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 1243 error = igmp_change_state(inm); 1244 if (error) 1245 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__); 1246 1247 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm); 1248 inm_release_locked(inm); 1249 1250 return (error); 1251} 1252 1253/*#ifndef BURN_BRIDGES*/ 1254/* 1255 * Join an IPv4 multicast group in (*,G) exclusive mode. 1256 * The group must be a 224.0.0.0/24 link-scope group. 1257 * This KPI is for legacy kernel consumers only. 1258 */ 1259struct in_multi * 1260in_addmulti(struct in_addr *ap, struct ifnet *ifp) 1261{ 1262 struct in_multi *pinm; 1263 int error; 1264 1265 KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)), 1266 ("%s: %s not in 224.0.0.0/24", __func__, inet_ntoa(*ap))); 1267 1268 error = in_joingroup(ifp, ap, NULL, &pinm); 1269 if (error != 0) 1270 pinm = NULL; 1271 1272 return (pinm); 1273} 1274 1275/* 1276 * Leave an IPv4 multicast group, assumed to be in exclusive (*,G) mode. 1277 * This KPI is for legacy kernel consumers only. 1278 */ 1279void 1280in_delmulti(struct in_multi *inm) 1281{ 1282 1283 (void)in_leavegroup(inm, NULL); 1284} 1285/*#endif*/ 1286 1287/* 1288 * Block or unblock an ASM multicast source on an inpcb. 1289 * This implements the delta-based API described in RFC 3678. 1290 * 1291 * The delta-based API applies only to exclusive-mode memberships. 1292 * An IGMP downcall will be performed. 1293 * 1294 * SMPng: NOTE: Must take Giant as a join may create a new ifma. 1295 * 1296 * Return 0 if successful, otherwise return an appropriate error code. 1297 */ 1298static int 1299inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) 1300{ 1301 INIT_VNET_NET(curvnet); 1302 INIT_VNET_INET(curvnet); 1303 struct group_source_req gsr; 1304 sockunion_t *gsa, *ssa; 1305 struct ifnet *ifp; 1306 struct in_mfilter *imf; 1307 struct ip_moptions *imo; 1308 struct in_msource *ims; 1309 struct in_multi *inm; 1310 size_t idx; 1311 uint16_t fmode; 1312 int error, doblock; 1313 1314 ifp = NULL; 1315 error = 0; 1316 doblock = 0; 1317 1318 memset(&gsr, 0, sizeof(struct group_source_req)); 1319 gsa = (sockunion_t *)&gsr.gsr_group; 1320 ssa = (sockunion_t *)&gsr.gsr_source; 1321 1322 switch (sopt->sopt_name) { 1323 case IP_BLOCK_SOURCE: 1324 case IP_UNBLOCK_SOURCE: { 1325 struct ip_mreq_source mreqs; 1326 1327 error = sooptcopyin(sopt, &mreqs, 1328 sizeof(struct ip_mreq_source), 1329 sizeof(struct ip_mreq_source)); 1330 if (error) 1331 return (error); 1332 1333 gsa->sin.sin_family = AF_INET; 1334 gsa->sin.sin_len = sizeof(struct sockaddr_in); 1335 gsa->sin.sin_addr = mreqs.imr_multiaddr; 1336 1337 ssa->sin.sin_family = AF_INET; 1338 ssa->sin.sin_len = sizeof(struct sockaddr_in); 1339 ssa->sin.sin_addr = mreqs.imr_sourceaddr; 1340 1341 if (!in_nullhost(mreqs.imr_interface)) 1342 INADDR_TO_IFP(mreqs.imr_interface, ifp); 1343 1344 if (sopt->sopt_name == IP_BLOCK_SOURCE) 1345 doblock = 1; 1346 1347 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p", 1348 __func__, inet_ntoa(mreqs.imr_interface), ifp); 1349 break; 1350 } 1351 1352 case MCAST_BLOCK_SOURCE: 1353 case MCAST_UNBLOCK_SOURCE: 1354 error = sooptcopyin(sopt, &gsr, 1355 sizeof(struct group_source_req), 1356 sizeof(struct group_source_req)); 1357 if (error) 1358 return (error); 1359 1360 if (gsa->sin.sin_family != AF_INET || 1361 gsa->sin.sin_len != sizeof(struct sockaddr_in)) 1362 return (EINVAL); 1363 1364 if (ssa->sin.sin_family != AF_INET || 1365 ssa->sin.sin_len != sizeof(struct sockaddr_in)) 1366 return (EINVAL); 1367 1368 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface) 1369 return (EADDRNOTAVAIL); 1370 1371 ifp = ifnet_byindex(gsr.gsr_interface); 1372 1373 if (sopt->sopt_name == MCAST_BLOCK_SOURCE) 1374 doblock = 1; 1375 break; 1376 1377 default: 1378 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d", 1379 __func__, sopt->sopt_name); 1380 return (EOPNOTSUPP); 1381 break; 1382 } 1383 1384 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 1385 return (EINVAL); 1386 1387 /* 1388 * Check if we are actually a member of this group. 1389 */ 1390 imo = inp_findmoptions(inp); 1391 idx = imo_match_group(imo, ifp, &gsa->sa); 1392 if (idx == -1 || imo->imo_mfilters == NULL) { 1393 error = EADDRNOTAVAIL; 1394 goto out_inp_locked; 1395 } 1396 1397 KASSERT(imo->imo_mfilters != NULL, 1398 ("%s: imo_mfilters not allocated", __func__)); 1399 imf = &imo->imo_mfilters[idx]; 1400 inm = imo->imo_membership[idx]; 1401 1402 /* 1403 * Attempting to use the delta-based API on an 1404 * non exclusive-mode membership is an error. 1405 */ 1406 fmode = imf->imf_st[0]; 1407 if (fmode != MCAST_EXCLUDE) { 1408 error = EINVAL; 1409 goto out_inp_locked; 1410 } 1411 1412 /* 1413 * Deal with error cases up-front: 1414 * Asked to block, but already blocked; or 1415 * Asked to unblock, but nothing to unblock. 1416 * If adding a new block entry, allocate it. 1417 */ 1418 ims = imo_match_source(imo, idx, &ssa->sa); 1419 if ((ims != NULL && doblock) || (ims == NULL && !doblock)) { 1420 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__, 1421 inet_ntoa(ssa->sin.sin_addr), doblock ? "" : "not "); 1422 error = EADDRNOTAVAIL; 1423 goto out_inp_locked; 1424 } 1425 1426 INP_WLOCK_ASSERT(inp); 1427 1428 /* 1429 * Begin state merge transaction at socket layer. 1430 */ 1431 if (doblock) { 1432 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block"); 1433 ims = imf_graft(imf, fmode, &ssa->sin); 1434 if (ims == NULL) 1435 error = ENOMEM; 1436 } else { 1437 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow"); 1438 error = imf_prune(imf, &ssa->sin); 1439 } 1440 1441 if (error) { 1442 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__); 1443 goto out_imf_rollback; 1444 } 1445 1446 /* 1447 * Begin state merge transaction at IGMP layer. 1448 */ 1449 IN_MULTI_LOCK(); 1450 1451 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 1452 error = inm_merge(inm, imf); 1453 if (error) { 1454 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__); 1455 goto out_imf_rollback; 1456 } 1457 1458 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 1459 error = igmp_change_state(inm); 1460 if (error) 1461 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__); 1462 1463 IN_MULTI_UNLOCK(); 1464 1465out_imf_rollback: 1466 if (error) 1467 imf_rollback(imf); 1468 else 1469 imf_commit(imf); 1470 1471 imf_reap(imf); 1472 1473out_inp_locked: 1474 INP_WUNLOCK(inp); 1475 return (error); 1476} 1477 1478/* 1479 * Given an inpcb, return its multicast options structure pointer. Accepts 1480 * an unlocked inpcb pointer, but will return it locked. May sleep. 1481 * 1482 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held. 1483 * SMPng: NOTE: Returns with the INP write lock held. 1484 */ 1485static struct ip_moptions * 1486inp_findmoptions(struct inpcb *inp) 1487{ 1488 struct ip_moptions *imo; 1489 struct in_multi **immp; 1490 struct in_mfilter *imfp; 1491 size_t idx; 1492 1493 INP_WLOCK(inp); 1494 if (inp->inp_moptions != NULL) 1495 return (inp->inp_moptions); 1496 1497 INP_WUNLOCK(inp); 1498 1499 imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK); 1500 immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS, 1501 M_WAITOK | M_ZERO); 1502 imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS, 1503 M_INMFILTER, M_WAITOK); 1504 1505 imo->imo_multicast_ifp = NULL; 1506 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1507 imo->imo_multicast_vif = -1; 1508 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1509 imo->imo_multicast_loop = in_mcast_loop; 1510 imo->imo_num_memberships = 0; 1511 imo->imo_max_memberships = IP_MIN_MEMBERSHIPS; 1512 imo->imo_membership = immp; 1513 1514 /* Initialize per-group source filters. */ 1515 for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++) 1516 imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); 1517 imo->imo_mfilters = imfp; 1518 1519 INP_WLOCK(inp); 1520 if (inp->inp_moptions != NULL) { 1521 free(imfp, M_INMFILTER); 1522 free(immp, M_IPMOPTS); 1523 free(imo, M_IPMOPTS); 1524 return (inp->inp_moptions); 1525 } 1526 inp->inp_moptions = imo; 1527 return (imo); 1528} 1529 1530/* 1531 * Discard the IP multicast options (and source filters). 1532 * 1533 * SMPng: NOTE: assumes INP write lock is held. 1534 */ 1535void 1536inp_freemoptions(struct ip_moptions *imo) 1537{ 1538 struct in_mfilter *imf; 1539 size_t idx, nmships; 1540 1541 KASSERT(imo != NULL, ("%s: ip_moptions is NULL", __func__)); 1542 1543 nmships = imo->imo_num_memberships; 1544 for (idx = 0; idx < nmships; ++idx) { 1545 imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL; 1546 if (imf) 1547 imf_leave(imf); 1548 (void)in_leavegroup(imo->imo_membership[idx], imf); 1549 if (imf) 1550 imf_purge(imf); 1551 } 1552 1553 if (imo->imo_mfilters) 1554 free(imo->imo_mfilters, M_INMFILTER); 1555 free(imo->imo_membership, M_IPMOPTS); 1556 free(imo, M_IPMOPTS); 1557} 1558 1559/* 1560 * Atomically get source filters on a socket for an IPv4 multicast group. 1561 * Called with INP lock held; returns with lock released. 1562 */ 1563static int 1564inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt) 1565{ 1566 INIT_VNET_NET(curvnet); 1567 struct __msfilterreq msfr; 1568 sockunion_t *gsa; 1569 struct ifnet *ifp; 1570 struct ip_moptions *imo; 1571 struct in_mfilter *imf; 1572 struct ip_msource *ims; 1573 struct in_msource *lims; 1574 struct sockaddr_in *psin; 1575 struct sockaddr_storage *ptss; 1576 struct sockaddr_storage *tss; 1577 int error; 1578 size_t idx, nsrcs, ncsrcs; 1579 1580 INP_WLOCK_ASSERT(inp); 1581 1582 imo = inp->inp_moptions; 1583 KASSERT(imo != NULL, ("%s: null ip_moptions", __func__)); 1584 1585 INP_WUNLOCK(inp); 1586 1587 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq), 1588 sizeof(struct __msfilterreq)); 1589 if (error) 1590 return (error); 1591 1592 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex) 1593 return (EINVAL); 1594 1595 ifp = ifnet_byindex(msfr.msfr_ifindex); 1596 if (ifp == NULL) 1597 return (EINVAL); 1598 1599 INP_WLOCK(inp); 1600 1601 /* 1602 * Lookup group on the socket. 1603 */ 1604 gsa = (sockunion_t *)&msfr.msfr_group; 1605 idx = imo_match_group(imo, ifp, &gsa->sa); 1606 if (idx == -1 || imo->imo_mfilters == NULL) { 1607 INP_WUNLOCK(inp); 1608 return (EADDRNOTAVAIL); 1609 } 1610 imf = &imo->imo_mfilters[idx]; 1611 1612 /* 1613 * Ignore memberships which are in limbo. 1614 */ 1615 if (imf->imf_st[1] == MCAST_UNDEFINED) { 1616 INP_WUNLOCK(inp); 1617 return (EAGAIN); 1618 } 1619 msfr.msfr_fmode = imf->imf_st[1]; 1620 1621 /* 1622 * If the user specified a buffer, copy out the source filter 1623 * entries to userland gracefully. 1624 * We only copy out the number of entries which userland 1625 * has asked for, but we always tell userland how big the 1626 * buffer really needs to be. 1627 */ 1628 tss = NULL; 1629 if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) { 1630 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs, 1631 M_TEMP, M_NOWAIT | M_ZERO); 1632 if (tss == NULL) { 1633 INP_WUNLOCK(inp); 1634 return (ENOBUFS); 1635 } 1636 } 1637 1638 /* 1639 * Count number of sources in-mode at t0. 1640 * If buffer space exists and remains, copy out source entries. 1641 */ 1642 nsrcs = msfr.msfr_nsrcs; 1643 ncsrcs = 0; 1644 ptss = tss; 1645 RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) { 1646 lims = (struct in_msource *)ims; 1647 if (lims->imsl_st[0] == MCAST_UNDEFINED || 1648 lims->imsl_st[0] != imf->imf_st[0]) 1649 continue; 1650 ++ncsrcs; 1651 if (tss != NULL && nsrcs-- > 0) { 1652 psin = (struct sockaddr_in *)ptss++; 1653 psin->sin_family = AF_INET; 1654 psin->sin_len = sizeof(struct sockaddr_in); 1655 psin->sin_addr.s_addr = htonl(lims->ims_haddr); 1656 } 1657 } 1658 1659 INP_WUNLOCK(inp); 1660 1661 if (tss != NULL) { 1662 error = copyout(tss, msfr.msfr_srcs, 1663 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs); 1664 free(tss, M_TEMP); 1665 if (error) 1666 return (error); 1667 } 1668 1669 msfr.msfr_nsrcs = ncsrcs; 1670 error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq)); 1671 1672 return (error); 1673} 1674 1675/* 1676 * Return the IP multicast options in response to user getsockopt(). 1677 */ 1678int 1679inp_getmoptions(struct inpcb *inp, struct sockopt *sopt) 1680{ 1681 INIT_VNET_INET(curvnet); 1682 struct ip_mreqn mreqn; 1683 struct ip_moptions *imo; 1684 struct ifnet *ifp; 1685 struct in_ifaddr *ia; 1686 int error, optval; 1687 u_char coptval; 1688 1689 INP_WLOCK(inp); 1690 imo = inp->inp_moptions; 1691 /* 1692 * If socket is neither of type SOCK_RAW or SOCK_DGRAM, 1693 * or is a divert socket, reject it. 1694 */ 1695 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT || 1696 (inp->inp_socket->so_proto->pr_type != SOCK_RAW && 1697 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) { 1698 INP_WUNLOCK(inp); 1699 return (EOPNOTSUPP); 1700 } 1701 1702 error = 0; 1703 switch (sopt->sopt_name) { 1704 case IP_MULTICAST_VIF: 1705 if (imo != NULL) 1706 optval = imo->imo_multicast_vif; 1707 else 1708 optval = -1; 1709 INP_WUNLOCK(inp); 1710 error = sooptcopyout(sopt, &optval, sizeof(int)); 1711 break; 1712 1713 case IP_MULTICAST_IF: 1714 memset(&mreqn, 0, sizeof(struct ip_mreqn)); 1715 if (imo != NULL) { 1716 ifp = imo->imo_multicast_ifp; 1717 if (!in_nullhost(imo->imo_multicast_addr)) { 1718 mreqn.imr_address = imo->imo_multicast_addr; 1719 } else if (ifp != NULL) { 1720 mreqn.imr_ifindex = ifp->if_index; 1721 IFP_TO_IA(ifp, ia); 1722 if (ia != NULL) { 1723 mreqn.imr_address = 1724 IA_SIN(ia)->sin_addr; 1725 } 1726 } 1727 } 1728 INP_WUNLOCK(inp); 1729 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { 1730 error = sooptcopyout(sopt, &mreqn, 1731 sizeof(struct ip_mreqn)); 1732 } else { 1733 error = sooptcopyout(sopt, &mreqn.imr_address, 1734 sizeof(struct in_addr)); 1735 } 1736 break; 1737 1738 case IP_MULTICAST_TTL: 1739 if (imo == 0) 1740 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1741 else 1742 optval = coptval = imo->imo_multicast_ttl; 1743 INP_WUNLOCK(inp); 1744 if (sopt->sopt_valsize == sizeof(u_char)) 1745 error = sooptcopyout(sopt, &coptval, sizeof(u_char)); 1746 else 1747 error = sooptcopyout(sopt, &optval, sizeof(int)); 1748 break; 1749 1750 case IP_MULTICAST_LOOP: 1751 if (imo == 0) 1752 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1753 else 1754 optval = coptval = imo->imo_multicast_loop; 1755 INP_WUNLOCK(inp); 1756 if (sopt->sopt_valsize == sizeof(u_char)) 1757 error = sooptcopyout(sopt, &coptval, sizeof(u_char)); 1758 else 1759 error = sooptcopyout(sopt, &optval, sizeof(int)); 1760 break; 1761 1762 case IP_MSFILTER: 1763 if (imo == NULL) { 1764 error = EADDRNOTAVAIL; 1765 INP_WUNLOCK(inp); 1766 } else { 1767 error = inp_get_source_filters(inp, sopt); 1768 } 1769 break; 1770 1771 default: 1772 INP_WUNLOCK(inp); 1773 error = ENOPROTOOPT; 1774 break; 1775 } 1776 1777 INP_UNLOCK_ASSERT(inp); 1778 1779 return (error); 1780} 1781 1782/* 1783 * Look up the ifnet to use for a multicast group membership, 1784 * given the IPv4 address of an interface, and the IPv4 group address. 1785 * 1786 * This routine exists to support legacy multicast applications 1787 * which do not understand that multicast memberships are scoped to 1788 * specific physical links in the networking stack, or which need 1789 * to join link-scope groups before IPv4 addresses are configured. 1790 * 1791 * If inp is non-NULL, use this socket's current FIB number for any 1792 * required FIB lookup. 1793 * If ina is INADDR_ANY, look up the group address in the unicast FIB, 1794 * and use its ifp; usually, this points to the default next-hop. 1795 * 1796 * If the FIB lookup fails, attempt to use the first non-loopback 1797 * interface with multicast capability in the system as a 1798 * last resort. The legacy IPv4 ASM API requires that we do 1799 * this in order to allow groups to be joined when the routing 1800 * table has not yet been populated during boot. 1801 * 1802 * Returns NULL if no ifp could be found. 1803 * 1804 * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP. 1805 * FUTURE: Implement IPv4 source-address selection. 1806 */ 1807static struct ifnet * 1808inp_lookup_mcast_ifp(const struct inpcb *inp, 1809 const struct sockaddr_in *gsin, const struct in_addr ina) 1810{ 1811 struct ifnet *ifp; 1812 1813 KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__)); 1814 KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)), 1815 ("%s: not multicast", __func__)); 1816 1817 ifp = NULL; 1818 if (!in_nullhost(ina)) { 1819 INADDR_TO_IFP(ina, ifp); 1820 } else { 1821 struct route ro; 1822 1823 ro.ro_rt = NULL; 1824 memcpy(&ro.ro_dst, gsin, sizeof(struct sockaddr_in)); 1825 in_rtalloc_ign(&ro, 0, inp ? inp->inp_inc.inc_fibnum : 0); 1826 if (ro.ro_rt != NULL) { 1827 ifp = ro.ro_rt->rt_ifp; 1828 KASSERT(ifp != NULL, ("%s: null ifp", __func__)); 1829 RTFREE(ro.ro_rt); 1830 } else { 1831 struct in_ifaddr *ia; 1832 struct ifnet *mifp; 1833 1834 mifp = NULL; 1835 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 1836 mifp = ia->ia_ifp; 1837 if (!(mifp->if_flags & IFF_LOOPBACK) && 1838 (mifp->if_flags & IFF_MULTICAST)) { 1839 ifp = mifp; 1840 break; 1841 } 1842 } 1843 } 1844 } 1845 1846 return (ifp); 1847} 1848 1849/* 1850 * Join an IPv4 multicast group, possibly with a source. 1851 */ 1852static int 1853inp_join_group(struct inpcb *inp, struct sockopt *sopt) 1854{ 1855 INIT_VNET_NET(curvnet); 1856 INIT_VNET_INET(curvnet); 1857 struct group_source_req gsr; 1858 sockunion_t *gsa, *ssa; 1859 struct ifnet *ifp; 1860 struct in_mfilter *imf; 1861 struct ip_moptions *imo; 1862 struct in_multi *inm; 1863 struct in_msource *lims; 1864 size_t idx; 1865 int error, is_new; 1866 1867 ifp = NULL; 1868 imf = NULL; 1869 error = 0; 1870 is_new = 0; 1871 1872 memset(&gsr, 0, sizeof(struct group_source_req)); 1873 gsa = (sockunion_t *)&gsr.gsr_group; 1874 gsa->ss.ss_family = AF_UNSPEC; 1875 ssa = (sockunion_t *)&gsr.gsr_source; 1876 ssa->ss.ss_family = AF_UNSPEC; 1877 1878 switch (sopt->sopt_name) { 1879 case IP_ADD_MEMBERSHIP: 1880 case IP_ADD_SOURCE_MEMBERSHIP: { 1881 struct ip_mreq_source mreqs; 1882 1883 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) { 1884 error = sooptcopyin(sopt, &mreqs, 1885 sizeof(struct ip_mreq), 1886 sizeof(struct ip_mreq)); 1887 /* 1888 * Do argument switcharoo from ip_mreq into 1889 * ip_mreq_source to avoid using two instances. 1890 */ 1891 mreqs.imr_interface = mreqs.imr_sourceaddr; 1892 mreqs.imr_sourceaddr.s_addr = INADDR_ANY; 1893 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { 1894 error = sooptcopyin(sopt, &mreqs, 1895 sizeof(struct ip_mreq_source), 1896 sizeof(struct ip_mreq_source)); 1897 } 1898 if (error) 1899 return (error); 1900 1901 gsa->sin.sin_family = AF_INET; 1902 gsa->sin.sin_len = sizeof(struct sockaddr_in); 1903 gsa->sin.sin_addr = mreqs.imr_multiaddr; 1904 1905 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) { 1906 ssa->sin.sin_family = AF_INET; 1907 ssa->sin.sin_len = sizeof(struct sockaddr_in); 1908 ssa->sin.sin_addr = mreqs.imr_sourceaddr; 1909 } 1910 1911 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin, 1912 mreqs.imr_interface); 1913 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p", 1914 __func__, inet_ntoa(mreqs.imr_interface), ifp); 1915 break; 1916 } 1917 1918 case MCAST_JOIN_GROUP: 1919 case MCAST_JOIN_SOURCE_GROUP: 1920 if (sopt->sopt_name == MCAST_JOIN_GROUP) { 1921 error = sooptcopyin(sopt, &gsr, 1922 sizeof(struct group_req), 1923 sizeof(struct group_req)); 1924 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { 1925 error = sooptcopyin(sopt, &gsr, 1926 sizeof(struct group_source_req), 1927 sizeof(struct group_source_req)); 1928 } 1929 if (error) 1930 return (error); 1931 1932 if (gsa->sin.sin_family != AF_INET || 1933 gsa->sin.sin_len != sizeof(struct sockaddr_in)) 1934 return (EINVAL); 1935 1936 /* 1937 * Overwrite the port field if present, as the sockaddr 1938 * being copied in may be matched with a binary comparison. 1939 */ 1940 gsa->sin.sin_port = 0; 1941 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { 1942 if (ssa->sin.sin_family != AF_INET || 1943 ssa->sin.sin_len != sizeof(struct sockaddr_in)) 1944 return (EINVAL); 1945 ssa->sin.sin_port = 0; 1946 } 1947 1948 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface) 1949 return (EADDRNOTAVAIL); 1950 ifp = ifnet_byindex(gsr.gsr_interface); 1951 break; 1952 1953 default: 1954 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d", 1955 __func__, sopt->sopt_name); 1956 return (EOPNOTSUPP); 1957 break; 1958 } 1959 1960 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 1961 return (EINVAL); 1962 1963 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) 1964 return (EADDRNOTAVAIL); 1965 1966 /* 1967 * MCAST_JOIN_SOURCE on an exclusive membership is an error. 1968 * On an existing inclusive membership, it just adds the 1969 * source to the filter list. 1970 */ 1971 imo = inp_findmoptions(inp); 1972 idx = imo_match_group(imo, ifp, &gsa->sa); 1973 if (idx == -1) { 1974 is_new = 1; 1975 } else { 1976 inm = imo->imo_membership[idx]; 1977 imf = &imo->imo_mfilters[idx]; 1978 if (ssa->ss.ss_family != AF_UNSPEC && 1979 imf->imf_st[1] != MCAST_INCLUDE) { 1980 error = EINVAL; 1981 goto out_inp_locked; 1982 } 1983 lims = imo_match_source(imo, idx, &ssa->sa); 1984 if (lims != NULL) { 1985 error = EADDRNOTAVAIL; 1986 goto out_inp_locked; 1987 } 1988 } 1989 1990 /* 1991 * Begin state merge transaction at socket layer. 1992 */ 1993 INP_WLOCK_ASSERT(inp); 1994 1995 if (is_new) { 1996 if (imo->imo_num_memberships == imo->imo_max_memberships) { 1997 error = imo_grow(imo); 1998 if (error) 1999 goto out_inp_locked; 2000 } 2001 /* 2002 * Allocate the new slot upfront so we can deal with 2003 * grafting the new source filter in same code path 2004 * as for join-source on existing membership. 2005 */ 2006 idx = imo->imo_num_memberships; 2007 imo->imo_membership[idx] = NULL; 2008 imo->imo_num_memberships++; 2009 KASSERT(imo->imo_mfilters != NULL, 2010 ("%s: imf_mfilters vector was not allocated", __func__)); 2011 imf = &imo->imo_mfilters[idx]; 2012 KASSERT(RB_EMPTY(&imf->imf_sources), 2013 ("%s: imf_sources not empty", __func__)); 2014 } 2015 2016 /* 2017 * Graft new source into filter list for this inpcb's 2018 * membership of the group. The in_multi may not have 2019 * been allocated yet if this is a new membership. 2020 */ 2021 if (ssa->ss.ss_family != AF_UNSPEC) { 2022 /* Membership starts in IN mode */ 2023 if (is_new) { 2024 CTR1(KTR_IGMPV3, "%s: new join w/source", __func__); 2025 imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE); 2026 } else { 2027 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow"); 2028 } 2029 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin); 2030 if (lims == NULL) { 2031 CTR1(KTR_IGMPV3, "%s: merge imf state failed", 2032 __func__); 2033 error = ENOMEM; 2034 goto out_imo_free; 2035 } 2036 } 2037 2038 /* 2039 * Begin state merge transaction at IGMP layer. 2040 */ 2041 IN_MULTI_LOCK(); 2042 2043 if (is_new) { 2044 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf, 2045 &inm); 2046 if (error) 2047 goto out_imo_free; 2048 imo->imo_membership[idx] = inm; 2049 } else { 2050 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 2051 error = inm_merge(inm, imf); 2052 if (error) { 2053 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", 2054 __func__); 2055 goto out_imf_rollback; 2056 } 2057 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 2058 error = igmp_change_state(inm); 2059 if (error) { 2060 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", 2061 __func__); 2062 goto out_imf_rollback; 2063 } 2064 } 2065 2066 IN_MULTI_UNLOCK(); 2067 2068out_imf_rollback: 2069 INP_WLOCK_ASSERT(inp); 2070 if (error) { 2071 imf_rollback(imf); 2072 if (is_new) 2073 imf_purge(imf); 2074 else 2075 imf_reap(imf); 2076 } else { 2077 imf_commit(imf); 2078 } 2079 2080out_imo_free: 2081 if (error && is_new) { 2082 imo->imo_membership[idx] = NULL; 2083 --imo->imo_num_memberships; 2084 } 2085 2086out_inp_locked: 2087 INP_WUNLOCK(inp); 2088 return (error); 2089} 2090 2091/* 2092 * Leave an IPv4 multicast group on an inpcb, possibly with a source. 2093 */ 2094static int 2095inp_leave_group(struct inpcb *inp, struct sockopt *sopt) 2096{ 2097 INIT_VNET_NET(curvnet); 2098 INIT_VNET_INET(curvnet); 2099 struct group_source_req gsr; 2100 struct ip_mreq_source mreqs; 2101 sockunion_t *gsa, *ssa; 2102 struct ifnet *ifp; 2103 struct in_mfilter *imf; 2104 struct ip_moptions *imo; 2105 struct in_msource *ims; 2106 struct in_multi *inm; 2107 size_t idx; 2108 int error, is_final; 2109 2110 ifp = NULL; 2111 error = 0; 2112 is_final = 1; 2113 2114 memset(&gsr, 0, sizeof(struct group_source_req)); 2115 gsa = (sockunion_t *)&gsr.gsr_group; 2116 gsa->ss.ss_family = AF_UNSPEC; 2117 ssa = (sockunion_t *)&gsr.gsr_source; 2118 ssa->ss.ss_family = AF_UNSPEC; 2119 2120 switch (sopt->sopt_name) { 2121 case IP_DROP_MEMBERSHIP: 2122 case IP_DROP_SOURCE_MEMBERSHIP: 2123 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) { 2124 error = sooptcopyin(sopt, &mreqs, 2125 sizeof(struct ip_mreq), 2126 sizeof(struct ip_mreq)); 2127 /* 2128 * Swap interface and sourceaddr arguments, 2129 * as ip_mreq and ip_mreq_source are laid 2130 * out differently. 2131 */ 2132 mreqs.imr_interface = mreqs.imr_sourceaddr; 2133 mreqs.imr_sourceaddr.s_addr = INADDR_ANY; 2134 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { 2135 error = sooptcopyin(sopt, &mreqs, 2136 sizeof(struct ip_mreq_source), 2137 sizeof(struct ip_mreq_source)); 2138 } 2139 if (error) 2140 return (error); 2141 2142 gsa->sin.sin_family = AF_INET; 2143 gsa->sin.sin_len = sizeof(struct sockaddr_in); 2144 gsa->sin.sin_addr = mreqs.imr_multiaddr; 2145 2146 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) { 2147 ssa->sin.sin_family = AF_INET; 2148 ssa->sin.sin_len = sizeof(struct sockaddr_in); 2149 ssa->sin.sin_addr = mreqs.imr_sourceaddr; 2150 } 2151 2152 if (!in_nullhost(gsa->sin.sin_addr)) 2153 INADDR_TO_IFP(mreqs.imr_interface, ifp); 2154 2155 CTR3(KTR_IGMPV3, "%s: imr_interface = %s, ifp = %p", 2156 __func__, inet_ntoa(mreqs.imr_interface), ifp); 2157 2158 break; 2159 2160 case MCAST_LEAVE_GROUP: 2161 case MCAST_LEAVE_SOURCE_GROUP: 2162 if (sopt->sopt_name == MCAST_LEAVE_GROUP) { 2163 error = sooptcopyin(sopt, &gsr, 2164 sizeof(struct group_req), 2165 sizeof(struct group_req)); 2166 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { 2167 error = sooptcopyin(sopt, &gsr, 2168 sizeof(struct group_source_req), 2169 sizeof(struct group_source_req)); 2170 } 2171 if (error) 2172 return (error); 2173 2174 if (gsa->sin.sin_family != AF_INET || 2175 gsa->sin.sin_len != sizeof(struct sockaddr_in)) 2176 return (EINVAL); 2177 2178 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { 2179 if (ssa->sin.sin_family != AF_INET || 2180 ssa->sin.sin_len != sizeof(struct sockaddr_in)) 2181 return (EINVAL); 2182 } 2183 2184 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface) 2185 return (EADDRNOTAVAIL); 2186 2187 ifp = ifnet_byindex(gsr.gsr_interface); 2188 break; 2189 2190 default: 2191 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d", 2192 __func__, sopt->sopt_name); 2193 return (EOPNOTSUPP); 2194 break; 2195 } 2196 2197 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 2198 return (EINVAL); 2199 2200 /* 2201 * Find the membership in the membership array. 2202 */ 2203 imo = inp_findmoptions(inp); 2204 idx = imo_match_group(imo, ifp, &gsa->sa); 2205 if (idx == -1) { 2206 error = EADDRNOTAVAIL; 2207 goto out_inp_locked; 2208 } 2209 inm = imo->imo_membership[idx]; 2210 imf = &imo->imo_mfilters[idx]; 2211 2212 if (ssa->ss.ss_family != AF_UNSPEC) 2213 is_final = 0; 2214 2215 /* 2216 * Begin state merge transaction at socket layer. 2217 */ 2218 INP_WLOCK_ASSERT(inp); 2219 2220 /* 2221 * If we were instructed only to leave a given source, do so. 2222 * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships. 2223 */ 2224 if (is_final) { 2225 imf_leave(imf); 2226 } else { 2227 if (imf->imf_st[0] == MCAST_EXCLUDE) { 2228 error = EADDRNOTAVAIL; 2229 goto out_inp_locked; 2230 } 2231 ims = imo_match_source(imo, idx, &ssa->sa); 2232 if (ims == NULL) { 2233 CTR3(KTR_IGMPV3, "%s: source %s %spresent", __func__, 2234 inet_ntoa(ssa->sin.sin_addr), "not "); 2235 error = EADDRNOTAVAIL; 2236 goto out_inp_locked; 2237 } 2238 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block"); 2239 error = imf_prune(imf, &ssa->sin); 2240 if (error) { 2241 CTR1(KTR_IGMPV3, "%s: merge imf state failed", 2242 __func__); 2243 goto out_inp_locked; 2244 } 2245 } 2246 2247 /* 2248 * Begin state merge transaction at IGMP layer. 2249 */ 2250 IN_MULTI_LOCK(); 2251 2252 if (is_final) { 2253 /* 2254 * Give up the multicast address record to which 2255 * the membership points. 2256 */ 2257 (void)in_leavegroup_locked(inm, imf); 2258 } else { 2259 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 2260 error = inm_merge(inm, imf); 2261 if (error) { 2262 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", 2263 __func__); 2264 goto out_imf_rollback; 2265 } 2266 2267 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 2268 error = igmp_change_state(inm); 2269 if (error) { 2270 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", 2271 __func__); 2272 } 2273 } 2274 2275 IN_MULTI_UNLOCK(); 2276 2277out_imf_rollback: 2278 if (error) 2279 imf_rollback(imf); 2280 else 2281 imf_commit(imf); 2282 2283 imf_reap(imf); 2284 2285 if (is_final) { 2286 /* Remove the gap in the membership array. */ 2287 for (++idx; idx < imo->imo_num_memberships; ++idx) 2288 imo->imo_membership[idx-1] = imo->imo_membership[idx]; 2289 imo->imo_num_memberships--; 2290 } 2291 2292out_inp_locked: 2293 INP_WUNLOCK(inp); 2294 return (error); 2295} 2296 2297/* 2298 * Select the interface for transmitting IPv4 multicast datagrams. 2299 * 2300 * Either an instance of struct in_addr or an instance of struct ip_mreqn 2301 * may be passed to this socket option. An address of INADDR_ANY or an 2302 * interface index of 0 is used to remove a previous selection. 2303 * When no interface is selected, one is chosen for every send. 2304 */ 2305static int 2306inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt) 2307{ 2308 INIT_VNET_NET(curvnet); 2309 struct in_addr addr; 2310 struct ip_mreqn mreqn; 2311 struct ifnet *ifp; 2312 struct ip_moptions *imo; 2313 int error; 2314 2315 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) { 2316 /* 2317 * An interface index was specified using the 2318 * Linux-derived ip_mreqn structure. 2319 */ 2320 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn), 2321 sizeof(struct ip_mreqn)); 2322 if (error) 2323 return (error); 2324 2325 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex) 2326 return (EINVAL); 2327 2328 if (mreqn.imr_ifindex == 0) { 2329 ifp = NULL; 2330 } else { 2331 ifp = ifnet_byindex(mreqn.imr_ifindex); 2332 if (ifp == NULL) 2333 return (EADDRNOTAVAIL); 2334 } 2335 } else { 2336 /* 2337 * An interface was specified by IPv4 address. 2338 * This is the traditional BSD usage. 2339 */ 2340 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr), 2341 sizeof(struct in_addr)); 2342 if (error) 2343 return (error); 2344 if (in_nullhost(addr)) { 2345 ifp = NULL; 2346 } else { 2347 INADDR_TO_IFP(addr, ifp); 2348 if (ifp == NULL) 2349 return (EADDRNOTAVAIL); 2350 } 2351 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = %s", __func__, ifp, 2352 inet_ntoa(addr)); 2353 } 2354 2355 /* Reject interfaces which do not support multicast. */ 2356 if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0) 2357 return (EOPNOTSUPP); 2358 2359 imo = inp_findmoptions(inp); 2360 imo->imo_multicast_ifp = ifp; 2361 imo->imo_multicast_addr.s_addr = INADDR_ANY; 2362 INP_WUNLOCK(inp); 2363 2364 return (0); 2365} 2366 2367/* 2368 * Atomically set source filters on a socket for an IPv4 multicast group. 2369 * 2370 * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held. 2371 */ 2372static int 2373inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt) 2374{ 2375 INIT_VNET_NET(curvnet); 2376 struct __msfilterreq msfr; 2377 sockunion_t *gsa; 2378 struct ifnet *ifp; 2379 struct in_mfilter *imf; 2380 struct ip_moptions *imo; 2381 struct in_multi *inm; 2382 size_t idx; 2383 int error; 2384 2385 error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq), 2386 sizeof(struct __msfilterreq)); 2387 if (error) 2388 return (error); 2389 2390 if (msfr.msfr_nsrcs > in_mcast_maxsocksrc || 2391 (msfr.msfr_fmode != MCAST_EXCLUDE && 2392 msfr.msfr_fmode != MCAST_INCLUDE)) 2393 return (EINVAL); 2394 2395 if (msfr.msfr_group.ss_family != AF_INET || 2396 msfr.msfr_group.ss_len != sizeof(struct sockaddr_in)) 2397 return (EINVAL); 2398 2399 gsa = (sockunion_t *)&msfr.msfr_group; 2400 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr))) 2401 return (EINVAL); 2402 2403 gsa->sin.sin_port = 0; /* ignore port */ 2404 2405 if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex) 2406 return (EADDRNOTAVAIL); 2407 2408 ifp = ifnet_byindex(msfr.msfr_ifindex); 2409 if (ifp == NULL) 2410 return (EADDRNOTAVAIL); 2411 2412 /* 2413 * Take the INP write lock. 2414 * Check if this socket is a member of this group. 2415 */ 2416 imo = inp_findmoptions(inp); 2417 idx = imo_match_group(imo, ifp, &gsa->sa); 2418 if (idx == -1 || imo->imo_mfilters == NULL) { 2419 error = EADDRNOTAVAIL; 2420 goto out_inp_locked; 2421 } 2422 inm = imo->imo_membership[idx]; 2423 imf = &imo->imo_mfilters[idx]; 2424 2425 /* 2426 * Begin state merge transaction at socket layer. 2427 */ 2428 INP_WLOCK_ASSERT(inp); 2429 2430 imf->imf_st[1] = msfr.msfr_fmode; 2431 2432 /* 2433 * Apply any new source filters, if present. 2434 * Make a copy of the user-space source vector so 2435 * that we may copy them with a single copyin. This 2436 * allows us to deal with page faults up-front. 2437 */ 2438 if (msfr.msfr_nsrcs > 0) { 2439 struct in_msource *lims; 2440 struct sockaddr_in *psin; 2441 struct sockaddr_storage *kss, *pkss; 2442 int i; 2443 2444 INP_WUNLOCK(inp); 2445 2446 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries", 2447 __func__, (unsigned long)msfr.msfr_nsrcs); 2448 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs, 2449 M_TEMP, M_WAITOK); 2450 error = copyin(msfr.msfr_srcs, kss, 2451 sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs); 2452 if (error) { 2453 free(kss, M_TEMP); 2454 return (error); 2455 } 2456 2457 INP_WLOCK(inp); 2458 2459 /* 2460 * Mark all source filters as UNDEFINED at t1. 2461 * Restore new group filter mode, as imf_leave() 2462 * will set it to INCLUDE. 2463 */ 2464 imf_leave(imf); 2465 imf->imf_st[1] = msfr.msfr_fmode; 2466 2467 /* 2468 * Update socket layer filters at t1, lazy-allocating 2469 * new entries. This saves a bunch of memory at the 2470 * cost of one RB_FIND() per source entry; duplicate 2471 * entries in the msfr_nsrcs vector are ignored. 2472 * If we encounter an error, rollback transaction. 2473 * 2474 * XXX This too could be replaced with a set-symmetric 2475 * difference like loop to avoid walking from root 2476 * every time, as the key space is common. 2477 */ 2478 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) { 2479 psin = (struct sockaddr_in *)pkss; 2480 if (psin->sin_family != AF_INET) { 2481 error = EAFNOSUPPORT; 2482 break; 2483 } 2484 if (psin->sin_len != sizeof(struct sockaddr_in)) { 2485 error = EINVAL; 2486 break; 2487 } 2488 error = imf_get_source(imf, psin, &lims); 2489 if (error) 2490 break; 2491 lims->imsl_st[1] = imf->imf_st[1]; 2492 } 2493 free(kss, M_TEMP); 2494 } 2495 2496 if (error) 2497 goto out_imf_rollback; 2498 2499 INP_WLOCK_ASSERT(inp); 2500 IN_MULTI_LOCK(); 2501 2502 /* 2503 * Begin state merge transaction at IGMP layer. 2504 */ 2505 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__); 2506 error = inm_merge(inm, imf); 2507 if (error) { 2508 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__); 2509 goto out_imf_rollback; 2510 } 2511 2512 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__); 2513 error = igmp_change_state(inm); 2514 if (error) 2515 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__); 2516 2517 IN_MULTI_UNLOCK(); 2518 2519out_imf_rollback: 2520 if (error) 2521 imf_rollback(imf); 2522 else 2523 imf_commit(imf); 2524 2525 imf_reap(imf); 2526 2527out_inp_locked: 2528 INP_WUNLOCK(inp); 2529 return (error); 2530} 2531 2532/* 2533 * Set the IP multicast options in response to user setsockopt(). 2534 * 2535 * Many of the socket options handled in this function duplicate the 2536 * functionality of socket options in the regular unicast API. However, 2537 * it is not possible to merge the duplicate code, because the idempotence 2538 * of the IPv4 multicast part of the BSD Sockets API must be preserved; 2539 * the effects of these options must be treated as separate and distinct. 2540 * 2541 * SMPng: XXX: Unlocked read of inp_socket believed OK. 2542 * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING 2543 * is refactored to no longer use vifs. 2544 */ 2545int 2546inp_setmoptions(struct inpcb *inp, struct sockopt *sopt) 2547{ 2548 struct ip_moptions *imo; 2549 int error; 2550 2551 error = 0; 2552 2553 /* 2554 * If socket is neither of type SOCK_RAW or SOCK_DGRAM, 2555 * or is a divert socket, reject it. 2556 */ 2557 if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT || 2558 (inp->inp_socket->so_proto->pr_type != SOCK_RAW && 2559 inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) 2560 return (EOPNOTSUPP); 2561 2562 switch (sopt->sopt_name) { 2563 case IP_MULTICAST_VIF: { 2564 int vifi; 2565 /* 2566 * Select a multicast VIF for transmission. 2567 * Only useful if multicast forwarding is active. 2568 */ 2569 if (legal_vif_num == NULL) { 2570 error = EOPNOTSUPP; 2571 break; 2572 } 2573 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int)); 2574 if (error) 2575 break; 2576 if (!legal_vif_num(vifi) && (vifi != -1)) { 2577 error = EINVAL; 2578 break; 2579 } 2580 imo = inp_findmoptions(inp); 2581 imo->imo_multicast_vif = vifi; 2582 INP_WUNLOCK(inp); 2583 break; 2584 } 2585 2586 case IP_MULTICAST_IF: 2587 error = inp_set_multicast_if(inp, sopt); 2588 break; 2589 2590 case IP_MULTICAST_TTL: { 2591 u_char ttl; 2592 2593 /* 2594 * Set the IP time-to-live for outgoing multicast packets. 2595 * The original multicast API required a char argument, 2596 * which is inconsistent with the rest of the socket API. 2597 * We allow either a char or an int. 2598 */ 2599 if (sopt->sopt_valsize == sizeof(u_char)) { 2600 error = sooptcopyin(sopt, &ttl, sizeof(u_char), 2601 sizeof(u_char)); 2602 if (error) 2603 break; 2604 } else { 2605 u_int ittl; 2606 2607 error = sooptcopyin(sopt, &ittl, sizeof(u_int), 2608 sizeof(u_int)); 2609 if (error) 2610 break; 2611 if (ittl > 255) { 2612 error = EINVAL; 2613 break; 2614 } 2615 ttl = (u_char)ittl; 2616 } 2617 imo = inp_findmoptions(inp); 2618 imo->imo_multicast_ttl = ttl; 2619 INP_WUNLOCK(inp); 2620 break; 2621 } 2622 2623 case IP_MULTICAST_LOOP: { 2624 u_char loop; 2625 2626 /* 2627 * Set the loopback flag for outgoing multicast packets. 2628 * Must be zero or one. The original multicast API required a 2629 * char argument, which is inconsistent with the rest 2630 * of the socket API. We allow either a char or an int. 2631 */ 2632 if (sopt->sopt_valsize == sizeof(u_char)) { 2633 error = sooptcopyin(sopt, &loop, sizeof(u_char), 2634 sizeof(u_char)); 2635 if (error) 2636 break; 2637 } else { 2638 u_int iloop; 2639 2640 error = sooptcopyin(sopt, &iloop, sizeof(u_int), 2641 sizeof(u_int)); 2642 if (error) 2643 break; 2644 loop = (u_char)iloop; 2645 } 2646 imo = inp_findmoptions(inp); 2647 imo->imo_multicast_loop = !!loop; 2648 INP_WUNLOCK(inp); 2649 break; 2650 } 2651 2652 case IP_ADD_MEMBERSHIP: 2653 case IP_ADD_SOURCE_MEMBERSHIP: 2654 case MCAST_JOIN_GROUP: 2655 case MCAST_JOIN_SOURCE_GROUP: 2656 error = inp_join_group(inp, sopt); 2657 break; 2658 2659 case IP_DROP_MEMBERSHIP: 2660 case IP_DROP_SOURCE_MEMBERSHIP: 2661 case MCAST_LEAVE_GROUP: 2662 case MCAST_LEAVE_SOURCE_GROUP: 2663 error = inp_leave_group(inp, sopt); 2664 break; 2665 2666 case IP_BLOCK_SOURCE: 2667 case IP_UNBLOCK_SOURCE: 2668 case MCAST_BLOCK_SOURCE: 2669 case MCAST_UNBLOCK_SOURCE: 2670 error = inp_block_unblock_source(inp, sopt); 2671 break; 2672 2673 case IP_MSFILTER: 2674 error = inp_set_source_filters(inp, sopt); 2675 break; 2676 2677 default: 2678 error = EOPNOTSUPP; 2679 break; 2680 } 2681 2682 INP_UNLOCK_ASSERT(inp); 2683 2684 return (error); 2685} 2686 2687/* 2688 * Expose IGMP's multicast filter mode and source list(s) to userland, 2689 * keyed by (ifindex, group). 2690 * The filter mode is written out as a uint32_t, followed by 2691 * 0..n of struct in_addr. 2692 * For use by ifmcstat(8). 2693 * SMPng: NOTE: unlocked read of ifindex space. 2694 */ 2695static int 2696sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS) 2697{ 2698 INIT_VNET_NET(curvnet); 2699 struct in_addr src, group; 2700 struct ifnet *ifp; 2701 struct ifmultiaddr *ifma; 2702 struct in_multi *inm; 2703 struct ip_msource *ims; 2704 int *name; 2705 int retval; 2706 u_int namelen; 2707 uint32_t fmode, ifindex; 2708 2709 name = (int *)arg1; 2710 namelen = arg2; 2711 2712 if (req->newptr != NULL) 2713 return (EPERM); 2714 2715 if (namelen != 2) 2716 return (EINVAL); 2717 2718 ifindex = name[0]; 2719 if (ifindex <= 0 || ifindex > V_if_index) { 2720 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range", 2721 __func__, ifindex); 2722 return (ENOENT); 2723 } 2724 2725 group.s_addr = name[1]; 2726 if (!IN_MULTICAST(ntohl(group.s_addr))) { 2727 CTR2(KTR_IGMPV3, "%s: group %s is not multicast", 2728 __func__, inet_ntoa(group)); 2729 return (EINVAL); 2730 } 2731 2732 ifp = ifnet_byindex(ifindex); 2733 if (ifp == NULL) { 2734 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u", 2735 __func__, ifindex); 2736 return (ENOENT); 2737 } 2738 2739 retval = sysctl_wire_old_buffer(req, 2740 sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr))); 2741 if (retval) 2742 return (retval); 2743 2744 IN_MULTI_LOCK(); 2745 2746 IF_ADDR_LOCK(ifp); 2747 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 2748 if (ifma->ifma_addr->sa_family != AF_INET || 2749 ifma->ifma_protospec == NULL) 2750 continue; 2751 inm = (struct in_multi *)ifma->ifma_protospec; 2752 if (!in_hosteq(inm->inm_addr, group)) 2753 continue; 2754 fmode = inm->inm_st[1].iss_fmode; 2755 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t)); 2756 if (retval != 0) 2757 break; 2758 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) { 2759#ifdef KTR 2760 struct in_addr ina; 2761 ina.s_addr = htonl(ims->ims_haddr); 2762 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__, 2763 inet_ntoa(ina)); 2764#endif 2765 /* 2766 * Only copy-out sources which are in-mode. 2767 */ 2768 if (fmode != ims_get_mode(inm, ims, 1)) { 2769 CTR1(KTR_IGMPV3, "%s: skip non-in-mode", 2770 __func__); 2771 continue; 2772 } 2773 src.s_addr = htonl(ims->ims_haddr); 2774 retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr)); 2775 if (retval != 0) 2776 break; 2777 } 2778 } 2779 IF_ADDR_UNLOCK(ifp); 2780 2781 IN_MULTI_UNLOCK(); 2782 2783 return (retval); 2784} 2785 2786#ifdef KTR 2787 2788static const char *inm_modestrs[] = { "un", "in", "ex" }; 2789 2790static const char * 2791inm_mode_str(const int mode) 2792{ 2793 2794 if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE) 2795 return (inm_modestrs[mode]); 2796 return ("??"); 2797} 2798 2799static const char *inm_statestrs[] = { 2800 "not-member", 2801 "silent", 2802 "idle", 2803 "lazy", 2804 "sleeping", 2805 "awakening", 2806 "query-pending", 2807 "sg-query-pending", 2808 "leaving" 2809}; 2810 2811static const char * 2812inm_state_str(const int state) 2813{ 2814 2815 if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER) 2816 return (inm_statestrs[state]); 2817 return ("??"); 2818} 2819 2820/* 2821 * Dump an in_multi structure to the console. 2822 */ 2823void 2824inm_print(const struct in_multi *inm) 2825{ 2826 int t; 2827 2828 if ((ktr_mask & KTR_IGMPV3) == 0) 2829 return; 2830 2831 printf("%s: --- begin inm %p ---\n", __func__, inm); 2832 printf("addr %s ifp %p(%s) ifma %p\n", 2833 inet_ntoa(inm->inm_addr), 2834 inm->inm_ifp, 2835 inm->inm_ifp->if_xname, 2836 inm->inm_ifma); 2837 printf("timer %u state %s refcount %u scq.len %u\n", 2838 inm->inm_timer, 2839 inm_state_str(inm->inm_state), 2840 inm->inm_refcount, 2841 inm->inm_scq.ifq_len); 2842 printf("igi %p nsrc %lu sctimer %u scrv %u\n", 2843 inm->inm_igi, 2844 inm->inm_nsrc, 2845 inm->inm_sctimer, 2846 inm->inm_scrv); 2847 for (t = 0; t < 2; t++) { 2848 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t, 2849 inm_mode_str(inm->inm_st[t].iss_fmode), 2850 inm->inm_st[t].iss_asm, 2851 inm->inm_st[t].iss_ex, 2852 inm->inm_st[t].iss_in, 2853 inm->inm_st[t].iss_rec); 2854 } 2855 printf("%s: --- end inm %p ---\n", __func__, inm); 2856} 2857 2858#else /* !KTR */ 2859 2860void 2861inm_print(const struct in_multi *inm) 2862{ 2863 2864} 2865 2866#endif /* KTR */ 2867 2868RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp); 2869