2 * Copyright (c) 2007-2009 Bruce Simpson.
3 * Copyright (c) 1988 Stephen Deering.
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Stephen Deering of Stanford University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
38 * Internet Group Management Protocol (IGMP) routines.
39 * [RFC1112, RFC2236, RFC3376]
41 * Written by Steve Deering, Stanford, May 1988.
42 * Modified by Rosen Sharma, Stanford, Aug 1994.
43 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
44 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
45 * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson.
47 * MULTICAST Revision: 3.5.1.4
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/module.h>
56 #include <sys/malloc.h>
58 #include <sys/socket.h>
59 #include <sys/protosw.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
63 #include <sys/condvar.h>
66 #include <net/netisr.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/ip_options.h>
75 #include <netinet/igmp.h>
76 #include <netinet/igmp_var.h>
78 #include <machine/in_cksum.h>
80 #include <security/mac/mac_framework.h>
83 #define KTR_IGMPV3 KTR_INET
86 static struct igmp_ifinfo *
87 igi_alloc_locked(struct ifnet *);
88 static void igi_delete_locked(const struct ifnet *);
89 static void igmp_dispatch_queue(struct ifqueue *, int, const int);
90 static void igmp_fasttimo_vnet(void);
91 static void igmp_final_leave(struct in_multi *, struct igmp_ifinfo *);
92 static int igmp_handle_state_change(struct in_multi *,
93 struct igmp_ifinfo *);
94 static int igmp_initial_join(struct in_multi *, struct igmp_ifinfo *);
95 static int igmp_input_v1_query(struct ifnet *, const struct ip *,
97 static int igmp_input_v2_query(struct ifnet *, const struct ip *,
99 static int igmp_input_v3_query(struct ifnet *, const struct ip *,
100 /*const*/ struct igmpv3 *);
101 static int igmp_input_v3_group_query(struct in_multi *,
102 struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *);
103 static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
104 /*const*/ struct igmp *);
105 static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
106 /*const*/ struct igmp *);
107 static void igmp_intr(struct mbuf *);
108 static int igmp_isgroupreported(const struct in_addr);
112 static char * igmp_rec_type_to_str(const int);
114 static void igmp_set_version(struct igmp_ifinfo *, const int);
115 static void igmp_slowtimo_vnet(void);
116 static int igmp_v1v2_queue_report(struct in_multi *, const int);
117 static void igmp_v1v2_process_group_timer(struct in_multi *, const int);
118 static void igmp_v1v2_process_querier_timers(struct igmp_ifinfo *);
119 static void igmp_v2_update_group(struct in_multi *, const int);
120 static void igmp_v3_cancel_link_timers(struct igmp_ifinfo *);
121 static void igmp_v3_dispatch_general_query(struct igmp_ifinfo *);
123 igmp_v3_encap_report(struct ifnet *, struct mbuf *);
124 static int igmp_v3_enqueue_group_record(struct ifqueue *,
125 struct in_multi *, const int, const int, const int);
126 static int igmp_v3_enqueue_filter_change(struct ifqueue *,
128 static void igmp_v3_process_group_timers(struct igmp_ifinfo *,
129 struct ifqueue *, struct ifqueue *, struct in_multi *,
131 static int igmp_v3_merge_state_changes(struct in_multi *,
133 static void igmp_v3_suppress_group_record(struct in_multi *);
134 static int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
135 static int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
136 static int sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
138 static const struct netisr_handler igmp_nh = {
140 .nh_handler = igmp_intr,
141 .nh_proto = NETISR_IGMP,
142 .nh_policy = NETISR_POLICY_SOURCE,
146 * System-wide globals.
148 * Unlocked access to these is OK, except for the global IGMP output
149 * queue. The IGMP subsystem lock ends up being system-wide for the moment,
150 * because all VIMAGEs have to share a global output queue, as netisrs
151 * themselves are not virtualized.
154 * * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
155 * Any may be taken independently; if any are held at the same
156 * time, the above lock order must be followed.
157 * * All output is delegated to the netisr.
158 * Now that Giant has been eliminated, the netisr may be inlined.
159 * * IN_MULTI_LOCK covers in_multi.
160 * * IGMP_LOCK covers igmp_ifinfo and any global variables in this file,
161 * including the output queue.
162 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
163 * per-link state iterators.
164 * * igmp_ifinfo is valid as long as PF_INET is attached to the interface,
165 * therefore it is not refcounted.
166 * We allow unlocked reads of igmp_ifinfo when accessed via in_multi.
169 * * IGMP acquires its own reference every time an in_multi is passed to
170 * it and the group is being joined for the first time.
171 * * IGMP releases its reference(s) on in_multi in a deferred way,
172 * because the operations which process the release run as part of
173 * a loop whose control variables are directly affected by the release
174 * (that, and not recursing on the IF_ADDR_LOCK).
176 * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
177 * to a vnet in ifp->if_vnet.
179 * SMPng: XXX We may potentially race operations on ifma_protospec.
180 * The problem is that we currently lack a clean way of taking the
181 * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
182 * as anything which modifies ifma needs to be covered by that lock.
183 * So check for ifma_protospec being NULL before proceeding.
187 struct mbuf *m_raopt; /* Router Alert option */
188 MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
191 * VIMAGE-wide globals.
193 * The IGMPv3 timers themselves need to run per-image, however,
194 * protosw timers run globally (see tcp).
195 * An ifnet can only be in one vimage at a time, and the loopback
196 * ifnet, loif, is itself virtualized.
197 * It would otherwise be possible to seriously hose IGMP state,
198 * and create inconsistencies in upstream multicast routing, if you have
199 * multiple VIMAGEs running on the same link joining different multicast
200 * groups, UNLESS the "primary IP address" is different. This is because
201 * IGMP for IPv4 does not force link-local addresses to be used for each
202 * node, unlike MLD for IPv6.
203 * Obviously the IGMPv3 per-interface state has per-vimage granularity
206 * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
207 * policy to control the address used by IGMP on the link.
209 static VNET_DEFINE(int, interface_timers_running); /* IGMPv3 general
211 static VNET_DEFINE(int, state_change_timers_running); /* IGMPv3 state-change
213 static VNET_DEFINE(int, current_state_timers_running); /* IGMPv1/v2 host
214 * report; IGMPv3 g/sg
217 #define V_interface_timers_running VNET(interface_timers_running)
218 #define V_state_change_timers_running VNET(state_change_timers_running)
219 #define V_current_state_timers_running VNET(current_state_timers_running)
221 static VNET_DEFINE(LIST_HEAD(, igmp_ifinfo), igi_head);
222 static VNET_DEFINE(struct igmpstat, igmpstat) = {
223 .igps_version = IGPS_VERSION_3,
224 .igps_len = sizeof(struct igmpstat),
226 static VNET_DEFINE(struct timeval, igmp_gsrdelay) = {10, 0};
228 #define V_igi_head VNET(igi_head)
229 #define V_igmpstat VNET(igmpstat)
230 #define V_igmp_gsrdelay VNET(igmp_gsrdelay)
232 static VNET_DEFINE(int, igmp_recvifkludge) = 1;
233 static VNET_DEFINE(int, igmp_sendra) = 1;
234 static VNET_DEFINE(int, igmp_sendlocal) = 1;
235 static VNET_DEFINE(int, igmp_v1enable) = 1;
236 static VNET_DEFINE(int, igmp_v2enable) = 1;
237 static VNET_DEFINE(int, igmp_legacysupp);
238 static VNET_DEFINE(int, igmp_default_version) = IGMP_VERSION_3;
240 #define V_igmp_recvifkludge VNET(igmp_recvifkludge)
241 #define V_igmp_sendra VNET(igmp_sendra)
242 #define V_igmp_sendlocal VNET(igmp_sendlocal)
243 #define V_igmp_v1enable VNET(igmp_v1enable)
244 #define V_igmp_v2enable VNET(igmp_v2enable)
245 #define V_igmp_legacysupp VNET(igmp_legacysupp)
246 #define V_igmp_default_version VNET(igmp_default_version)
249 * Virtualized sysctls.
251 SYSCTL_VNET_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
252 &VNET_NAME(igmpstat), igmpstat, "");
253 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_RW,
254 &VNET_NAME(igmp_recvifkludge), 0,
255 "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
256 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_RW,
257 &VNET_NAME(igmp_sendra), 0,
258 "Send IP Router Alert option in IGMPv2/v3 messages");
259 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_RW,
260 &VNET_NAME(igmp_sendlocal), 0,
261 "Send IGMP membership reports for 224.0.0.0/24 groups");
262 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_RW,
263 &VNET_NAME(igmp_v1enable), 0,
264 "Enable backwards compatibility with IGMPv1");
265 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_RW,
266 &VNET_NAME(igmp_v2enable), 0,
267 "Enable backwards compatibility with IGMPv2");
268 SYSCTL_VNET_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_RW,
269 &VNET_NAME(igmp_legacysupp), 0,
270 "Allow v1/v2 reports to suppress v3 group responses");
271 SYSCTL_VNET_PROC(_net_inet_igmp, OID_AUTO, default_version,
272 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
273 &VNET_NAME(igmp_default_version), 0, sysctl_igmp_default_version, "I",
274 "Default version of IGMP to run on each interface");
275 SYSCTL_VNET_PROC(_net_inet_igmp, OID_AUTO, gsrdelay,
276 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
277 &VNET_NAME(igmp_gsrdelay.tv_sec), 0, sysctl_igmp_gsr, "I",
278 "Rate limit for IGMPv3 Group-and-Source queries in seconds");
281 * Non-virtualized sysctls.
283 SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo, CTLFLAG_RD | CTLFLAG_MPSAFE,
284 sysctl_igmp_ifinfo, "Per-interface IGMPv3 state");
287 igmp_save_context(struct mbuf *m, struct ifnet *ifp)
291 m->m_pkthdr.header = ifp->if_vnet;
293 m->m_pkthdr.flowid = ifp->if_index;
297 igmp_scrub_context(struct mbuf *m)
300 m->m_pkthdr.header = NULL;
301 m->m_pkthdr.flowid = 0;
305 static __inline char *
306 inet_ntoa_haddr(in_addr_t haddr)
310 ia.s_addr = htonl(haddr);
311 return (inet_ntoa(ia));
316 * Restore context from a queued IGMP output chain.
317 * Return saved ifindex.
319 * VIMAGE: The assertion is there to make sure that we
320 * actually called CURVNET_SET() with what's in the mbuf chain.
322 static __inline uint32_t
323 igmp_restore_context(struct mbuf *m)
327 #if defined(VIMAGE) && defined(INVARIANTS)
328 KASSERT(curvnet == (m->m_pkthdr.header),
329 ("%s: called when curvnet was not restored", __func__));
332 return (m->m_pkthdr.flowid);
336 * Retrieve or set default IGMP version.
338 * VIMAGE: Assume curvnet set by caller.
339 * SMPng: NOTE: Serialized by IGMP lock.
342 sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
347 error = sysctl_wire_old_buffer(req, sizeof(int));
353 new = V_igmp_default_version;
355 error = sysctl_handle_int(oidp, &new, 0, req);
356 if (error || !req->newptr)
359 if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
364 CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
365 V_igmp_default_version, new);
367 V_igmp_default_version = new;
375 * Retrieve or set threshold between group-source queries in seconds.
377 * VIMAGE: Assume curvnet set by caller.
378 * SMPng: NOTE: Serialized by IGMP lock.
381 sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
386 error = sysctl_wire_old_buffer(req, sizeof(int));
392 i = V_igmp_gsrdelay.tv_sec;
394 error = sysctl_handle_int(oidp, &i, 0, req);
395 if (error || !req->newptr)
398 if (i < -1 || i >= 60) {
403 CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
404 V_igmp_gsrdelay.tv_sec, i);
405 V_igmp_gsrdelay.tv_sec = i;
413 * Expose struct igmp_ifinfo to userland, keyed by ifindex.
414 * For use by ifmcstat(8).
416 * SMPng: NOTE: Does an unlocked ifindex space read.
417 * VIMAGE: Assume curvnet set by caller. The node handler itself
418 * is not directly virtualized.
421 sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
427 struct igmp_ifinfo *igi;
432 if (req->newptr != NULL)
438 error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
445 if (name[0] <= 0 || name[0] > V_if_index) {
452 ifp = ifnet_byindex(name[0]);
456 LIST_FOREACH(igi, &V_igi_head, igi_link) {
457 if (ifp == igi->igi_ifp) {
458 error = SYSCTL_OUT(req, igi,
459 sizeof(struct igmp_ifinfo));
471 * Dispatch an entire queue of pending packet chains
473 * VIMAGE: Assumes the vnet pointer has been set.
476 igmp_dispatch_queue(struct ifqueue *ifq, int limit, const int loop)
484 CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, ifq, m);
486 m->m_flags |= M_IGMP_LOOP;
487 netisr_dispatch(NETISR_IGMP, m);
494 * Filter outgoing IGMP report state by group.
496 * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
497 * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
498 * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
499 * this may break certain IGMP snooping switches which rely on the old
502 * Return zero if the given group is one for which IGMP reports
503 * should be suppressed, or non-zero if reports should be issued.
506 igmp_isgroupreported(const struct in_addr addr)
509 if (in_allhosts(addr) ||
510 ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
517 * Construct a Router Alert option to use in outgoing packets.
525 MGET(m, M_DONTWAIT, MT_DATA);
526 p = mtod(m, struct ipoption *);
527 p->ipopt_dst.s_addr = INADDR_ANY;
528 p->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
529 p->ipopt_list[1] = 0x04; /* 4 bytes long */
530 p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */
531 p->ipopt_list[3] = 0x00; /* pad byte */
532 m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
538 * Attach IGMP when PF_INET is attached to an interface.
541 igmp_domifattach(struct ifnet *ifp)
543 struct igmp_ifinfo *igi;
545 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
546 __func__, ifp, ifp->if_xname);
550 igi = igi_alloc_locked(ifp);
551 if (!(ifp->if_flags & IFF_MULTICAST))
552 igi->igi_flags |= IGIF_SILENT;
560 * VIMAGE: assume curvnet set by caller.
562 static struct igmp_ifinfo *
563 igi_alloc_locked(/*const*/ struct ifnet *ifp)
565 struct igmp_ifinfo *igi;
569 igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO);
574 igi->igi_version = V_igmp_default_version;
576 igi->igi_rv = IGMP_RV_INIT;
577 igi->igi_qi = IGMP_QI_INIT;
578 igi->igi_qri = IGMP_QRI_INIT;
579 igi->igi_uri = IGMP_URI_INIT;
581 SLIST_INIT(&igi->igi_relinmhead);
584 * Responses to general queries are subject to bounds.
586 IFQ_SET_MAXLEN(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
588 LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
590 CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)",
600 * NOTE: Some finalization tasks need to run before the protocol domain
601 * is detached, but also before the link layer does its cleanup.
603 * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
604 * XXX This is also bitten by unlocked ifma_protospec access.
607 igmp_ifdetach(struct ifnet *ifp)
609 struct igmp_ifinfo *igi;
610 struct ifmultiaddr *ifma;
611 struct in_multi *inm, *tinm;
613 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
618 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
619 if (igi->igi_version == IGMP_VERSION_3) {
621 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
622 if (ifma->ifma_addr->sa_family != AF_INET ||
623 ifma->ifma_protospec == NULL)
626 KASSERT(ifma->ifma_protospec != NULL,
627 ("%s: ifma_protospec is NULL", __func__));
629 inm = (struct in_multi *)ifma->ifma_protospec;
630 if (inm->inm_state == IGMP_LEAVING_MEMBER) {
631 SLIST_INSERT_HEAD(&igi->igi_relinmhead,
634 inm_clear_recorded(inm);
636 IF_ADDR_RUNLOCK(ifp);
638 * Free the in_multi reference(s) for this IGMP lifecycle.
640 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele,
642 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
643 inm_release_locked(inm);
651 * Hook for domifdetach.
654 igmp_domifdetach(struct ifnet *ifp)
656 struct igmp_ifinfo *igi;
658 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
659 __func__, ifp, ifp->if_xname);
663 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
664 igi_delete_locked(ifp);
670 igi_delete_locked(const struct ifnet *ifp)
672 struct igmp_ifinfo *igi, *tigi;
674 CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)",
675 __func__, ifp, ifp->if_xname);
679 LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
680 if (igi->igi_ifp == ifp) {
682 * Free deferred General Query responses.
684 _IF_DRAIN(&igi->igi_gq);
686 LIST_REMOVE(igi, igi_link);
688 KASSERT(SLIST_EMPTY(&igi->igi_relinmhead),
689 ("%s: there are dangling in_multi references",
698 panic("%s: igmp_ifinfo not found for ifp %p\n", __func__, ifp);
703 * Process a received IGMPv1 query.
704 * Return non-zero if the message should be dropped.
706 * VIMAGE: The curvnet pointer is derived from the input ifp.
709 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
710 const struct igmp *igmp)
712 struct ifmultiaddr *ifma;
713 struct igmp_ifinfo *igi;
714 struct in_multi *inm;
717 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
718 * 224.0.0.1. They are always treated as General Queries.
719 * igmp_group is always ignored. Do not drop it as a userland
720 * daemon may wish to see it.
721 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
723 if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
724 IGMPSTAT_INC(igps_rcv_badqueries);
727 IGMPSTAT_INC(igps_rcv_gen_queries);
732 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
733 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
735 if (igi->igi_flags & IGIF_LOOPBACK) {
736 CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
742 * Switch to IGMPv1 host compatibility mode.
744 igmp_set_version(igi, IGMP_VERSION_1);
746 CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
749 * Start the timers in all of our group records
750 * for the interface on which the query arrived,
751 * except those which are already running.
754 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
755 if (ifma->ifma_addr->sa_family != AF_INET ||
756 ifma->ifma_protospec == NULL)
758 inm = (struct in_multi *)ifma->ifma_protospec;
759 if (inm->inm_timer != 0)
761 switch (inm->inm_state) {
762 case IGMP_NOT_MEMBER:
763 case IGMP_SILENT_MEMBER:
765 case IGMP_G_QUERY_PENDING_MEMBER:
766 case IGMP_SG_QUERY_PENDING_MEMBER:
767 case IGMP_REPORTING_MEMBER:
768 case IGMP_IDLE_MEMBER:
769 case IGMP_LAZY_MEMBER:
770 case IGMP_SLEEPING_MEMBER:
771 case IGMP_AWAKENING_MEMBER:
772 inm->inm_state = IGMP_REPORTING_MEMBER;
773 inm->inm_timer = IGMP_RANDOM_DELAY(
774 IGMP_V1V2_MAX_RI * PR_FASTHZ);
775 V_current_state_timers_running = 1;
777 case IGMP_LEAVING_MEMBER:
781 IF_ADDR_RUNLOCK(ifp);
791 * Process a received IGMPv2 general or group-specific query.
794 igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
795 const struct igmp *igmp)
797 struct ifmultiaddr *ifma;
798 struct igmp_ifinfo *igi;
799 struct in_multi *inm;
800 int is_general_query;
803 is_general_query = 0;
806 * Validate address fields upfront.
807 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
809 if (in_nullhost(igmp->igmp_group)) {
811 * IGMPv2 General Query.
812 * If this was not sent to the all-hosts group, ignore it.
814 if (!in_allhosts(ip->ip_dst))
816 IGMPSTAT_INC(igps_rcv_gen_queries);
817 is_general_query = 1;
819 /* IGMPv2 Group-Specific Query. */
820 IGMPSTAT_INC(igps_rcv_group_queries);
826 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
827 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
829 if (igi->igi_flags & IGIF_LOOPBACK) {
830 CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
836 * Ignore v2 query if in v1 Compatibility Mode.
838 if (igi->igi_version == IGMP_VERSION_1)
841 igmp_set_version(igi, IGMP_VERSION_2);
843 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
847 if (is_general_query) {
849 * For each reporting group joined on this
850 * interface, kick the report timer.
852 CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
855 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
856 if (ifma->ifma_addr->sa_family != AF_INET ||
857 ifma->ifma_protospec == NULL)
859 inm = (struct in_multi *)ifma->ifma_protospec;
860 igmp_v2_update_group(inm, timer);
862 IF_ADDR_RUNLOCK(ifp);
865 * Group-specific IGMPv2 query, we need only
866 * look up the single group to process it.
868 inm = inm_lookup(ifp, igmp->igmp_group);
870 CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)",
871 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
872 igmp_v2_update_group(inm, timer);
884 * Update the report timer on a group in response to an IGMPv2 query.
886 * If we are becoming the reporting member for this group, start the timer.
887 * If we already are the reporting member for this group, and timer is
888 * below the threshold, reset it.
890 * We may be updating the group for the first time since we switched
891 * to IGMPv3. If we are, then we must clear any recorded source lists,
892 * and transition to REPORTING state; the group timer is overloaded
893 * for group and group-source query responses.
895 * Unlike IGMPv3, the delay per group should be jittered
896 * to avoid bursts of IGMPv2 reports.
899 igmp_v2_update_group(struct in_multi *inm, const int timer)
902 CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__,
903 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer);
905 IN_MULTI_LOCK_ASSERT();
907 switch (inm->inm_state) {
908 case IGMP_NOT_MEMBER:
909 case IGMP_SILENT_MEMBER:
911 case IGMP_REPORTING_MEMBER:
912 if (inm->inm_timer != 0 &&
913 inm->inm_timer <= timer) {
914 CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
915 "skipping.", __func__);
919 case IGMP_SG_QUERY_PENDING_MEMBER:
920 case IGMP_G_QUERY_PENDING_MEMBER:
921 case IGMP_IDLE_MEMBER:
922 case IGMP_LAZY_MEMBER:
923 case IGMP_AWAKENING_MEMBER:
924 CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
925 inm->inm_state = IGMP_REPORTING_MEMBER;
926 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
927 V_current_state_timers_running = 1;
929 case IGMP_SLEEPING_MEMBER:
930 CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
931 inm->inm_state = IGMP_AWAKENING_MEMBER;
933 case IGMP_LEAVING_MEMBER:
939 * Process a received IGMPv3 general, group-specific or
940 * group-and-source-specific query.
941 * Assumes m has already been pulled up to the full IGMP message length.
942 * Return 0 if successful, otherwise an appropriate error code is returned.
945 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
946 /*const*/ struct igmpv3 *igmpv3)
948 struct igmp_ifinfo *igi;
949 struct in_multi *inm;
950 int is_general_query;
951 uint32_t maxresp, nsrc, qqi;
955 is_general_query = 0;
957 CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
959 maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */
960 if (maxresp >= 128) {
961 maxresp = IGMP_MANT(igmpv3->igmp_code) <<
962 (IGMP_EXP(igmpv3->igmp_code) + 3);
966 * Robustness must never be less than 2 for on-wire IGMPv3.
967 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
968 * an exception for interfaces whose IGMPv3 state changes
969 * are redirected to loopback (e.g. MANET).
971 qrv = IGMP_QRV(igmpv3->igmp_misc);
973 CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
978 qqi = igmpv3->igmp_qqi;
980 qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
981 (IGMP_EXP(igmpv3->igmp_qqi) + 3);
984 timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
988 nsrc = ntohs(igmpv3->igmp_numsrc);
991 * Validate address fields and versions upfront before
992 * accepting v3 query.
993 * XXX SMPng: Unlocked access to igmpstat counters here.
995 if (in_nullhost(igmpv3->igmp_group)) {
997 * IGMPv3 General Query.
999 * General Queries SHOULD be directed to 224.0.0.1.
1000 * A general query with a source list has undefined
1001 * behaviour; discard it.
1003 IGMPSTAT_INC(igps_rcv_gen_queries);
1004 if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
1005 IGMPSTAT_INC(igps_rcv_badqueries);
1008 is_general_query = 1;
1010 /* Group or group-source specific query. */
1012 IGMPSTAT_INC(igps_rcv_group_queries);
1014 IGMPSTAT_INC(igps_rcv_gsr_queries);
1020 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1021 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
1023 if (igi->igi_flags & IGIF_LOOPBACK) {
1024 CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1025 ifp, ifp->if_xname);
1030 * Discard the v3 query if we're in Compatibility Mode.
1031 * The RFC is not obviously worded that hosts need to stay in
1032 * compatibility mode until the Old Version Querier Present
1035 if (igi->igi_version != IGMP_VERSION_3) {
1036 CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1037 igi->igi_version, ifp, ifp->if_xname);
1041 igmp_set_version(igi, IGMP_VERSION_3);
1044 igi->igi_qri = maxresp;
1046 CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1049 if (is_general_query) {
1051 * Schedule a current-state report on this ifp for
1052 * all groups, possibly containing source lists.
1053 * If there is a pending General Query response
1054 * scheduled earlier than the selected delay, do
1055 * not schedule any other reports.
1056 * Otherwise, reset the interface timer.
1058 CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1059 ifp, ifp->if_xname);
1060 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1061 igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1062 V_interface_timers_running = 1;
1066 * Group-source-specific queries are throttled on
1067 * a per-group basis to defeat denial-of-service attempts.
1068 * Queries for groups we are not a member of on this
1069 * link are simply ignored.
1071 inm = inm_lookup(ifp, igmpv3->igmp_group);
1075 if (!ratecheck(&inm->inm_lastgsrtv,
1076 &V_igmp_gsrdelay)) {
1077 CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1079 IGMPSTAT_INC(igps_drop_gsr_queries);
1083 CTR3(KTR_IGMPV3, "process v3 %s query on ifp %p(%s)",
1084 inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_xname);
1086 * If there is a pending General Query response
1087 * scheduled sooner than the selected delay, no
1088 * further report need be scheduled.
1089 * Otherwise, prepare to respond to the
1090 * group-specific or group-and-source query.
1092 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1093 igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1104 * Process a recieved IGMPv3 group-specific or group-and-source-specific
1106 * Return <0 if any error occured. Currently this is ignored.
1109 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi,
1110 int timer, /*const*/ struct igmpv3 *igmpv3)
1115 IN_MULTI_LOCK_ASSERT();
1120 switch (inm->inm_state) {
1121 case IGMP_NOT_MEMBER:
1122 case IGMP_SILENT_MEMBER:
1123 case IGMP_SLEEPING_MEMBER:
1124 case IGMP_LAZY_MEMBER:
1125 case IGMP_AWAKENING_MEMBER:
1126 case IGMP_IDLE_MEMBER:
1127 case IGMP_LEAVING_MEMBER:
1130 case IGMP_REPORTING_MEMBER:
1131 case IGMP_G_QUERY_PENDING_MEMBER:
1132 case IGMP_SG_QUERY_PENDING_MEMBER:
1136 nsrc = ntohs(igmpv3->igmp_numsrc);
1139 * Deal with group-specific queries upfront.
1140 * If any group query is already pending, purge any recorded
1141 * source-list state if it exists, and schedule a query response
1142 * for this group-specific query.
1145 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1146 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1147 inm_clear_recorded(inm);
1148 timer = min(inm->inm_timer, timer);
1150 inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1151 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1152 V_current_state_timers_running = 1;
1157 * Deal with the case where a group-and-source-specific query has
1158 * been received but a group-specific query is already pending.
1160 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1161 timer = min(inm->inm_timer, timer);
1162 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1163 V_current_state_timers_running = 1;
1168 * Finally, deal with the case where a group-and-source-specific
1169 * query has been received, where a response to a previous g-s-r
1170 * query exists, or none exists.
1171 * In this case, we need to parse the source-list which the Querier
1172 * has provided us with and check if we have any source list filter
1173 * entries at T1 for these sources. If we do not, there is no need
1174 * schedule a report and the query may be dropped.
1175 * If we do, we must record them and schedule a current-state
1176 * report for those sources.
1177 * FIXME: Handling source lists larger than 1 mbuf requires that
1178 * we pass the mbuf chain pointer down to this function, and use
1179 * m_getptr() to walk the chain.
1181 if (inm->inm_nsrc > 0) {
1182 const struct in_addr *ap;
1185 ap = (const struct in_addr *)(igmpv3 + 1);
1187 for (i = 0; i < nsrc; i++, ap++) {
1188 retval = inm_record_source(inm, ap->s_addr);
1191 nrecorded += retval;
1193 if (nrecorded > 0) {
1195 "%s: schedule response to SG query", __func__);
1196 inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1197 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1198 V_current_state_timers_running = 1;
1206 * Process a received IGMPv1 host membership report.
1208 * NOTE: 0.0.0.0 workaround breaks const correctness.
1211 igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1212 /*const*/ struct igmp *igmp)
1214 struct in_ifaddr *ia;
1215 struct in_multi *inm;
1217 IGMPSTAT_INC(igps_rcv_reports);
1219 if (ifp->if_flags & IFF_LOOPBACK)
1222 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1223 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1224 IGMPSTAT_INC(igps_rcv_badreports);
1229 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1230 * Booting clients may use the source address 0.0.0.0. Some
1231 * IGMP daemons may not know how to use IP_RECVIF to determine
1232 * the interface upon which this message was received.
1233 * Replace 0.0.0.0 with the subnet address if told to do so.
1235 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1238 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1239 ifa_free(&ia->ia_ifa);
1243 CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)",
1244 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1247 * IGMPv1 report suppression.
1248 * If we are a member of this group, and our membership should be
1249 * reported, stop our group timer and transition to the 'lazy' state.
1252 inm = inm_lookup(ifp, igmp->igmp_group);
1254 struct igmp_ifinfo *igi;
1258 KASSERT(igi != NULL,
1259 ("%s: no igi for ifp %p", __func__, ifp));
1263 IGMPSTAT_INC(igps_rcv_ourreports);
1266 * If we are in IGMPv3 host mode, do not allow the
1267 * other host's IGMPv1 report to suppress our reports
1268 * unless explicitly configured to do so.
1270 if (igi->igi_version == IGMP_VERSION_3) {
1271 if (V_igmp_legacysupp)
1272 igmp_v3_suppress_group_record(inm);
1278 switch (inm->inm_state) {
1279 case IGMP_NOT_MEMBER:
1280 case IGMP_SILENT_MEMBER:
1282 case IGMP_IDLE_MEMBER:
1283 case IGMP_LAZY_MEMBER:
1284 case IGMP_AWAKENING_MEMBER:
1286 "report suppressed for %s on ifp %p(%s)",
1287 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1288 case IGMP_SLEEPING_MEMBER:
1289 inm->inm_state = IGMP_SLEEPING_MEMBER;
1291 case IGMP_REPORTING_MEMBER:
1293 "report suppressed for %s on ifp %p(%s)",
1294 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1295 if (igi->igi_version == IGMP_VERSION_1)
1296 inm->inm_state = IGMP_LAZY_MEMBER;
1297 else if (igi->igi_version == IGMP_VERSION_2)
1298 inm->inm_state = IGMP_SLEEPING_MEMBER;
1300 case IGMP_G_QUERY_PENDING_MEMBER:
1301 case IGMP_SG_QUERY_PENDING_MEMBER:
1302 case IGMP_LEAVING_MEMBER:
1314 * Process a received IGMPv2 host membership report.
1316 * NOTE: 0.0.0.0 workaround breaks const correctness.
1319 igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1320 /*const*/ struct igmp *igmp)
1322 struct in_ifaddr *ia;
1323 struct in_multi *inm;
1326 * Make sure we don't hear our own membership report. Fast
1327 * leave requires knowing that we are the only member of a
1331 if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1332 ifa_free(&ia->ia_ifa);
1336 IGMPSTAT_INC(igps_rcv_reports);
1338 if (ifp->if_flags & IFF_LOOPBACK) {
1340 ifa_free(&ia->ia_ifa);
1344 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1345 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1347 ifa_free(&ia->ia_ifa);
1348 IGMPSTAT_INC(igps_rcv_badreports);
1353 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1354 * Booting clients may use the source address 0.0.0.0. Some
1355 * IGMP daemons may not know how to use IP_RECVIF to determine
1356 * the interface upon which this message was received.
1357 * Replace 0.0.0.0 with the subnet address if told to do so.
1359 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1361 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1364 ifa_free(&ia->ia_ifa);
1366 CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)",
1367 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1370 * IGMPv2 report suppression.
1371 * If we are a member of this group, and our membership should be
1372 * reported, and our group timer is pending or about to be reset,
1373 * stop our group timer by transitioning to the 'lazy' state.
1376 inm = inm_lookup(ifp, igmp->igmp_group);
1378 struct igmp_ifinfo *igi;
1381 KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1383 IGMPSTAT_INC(igps_rcv_ourreports);
1386 * If we are in IGMPv3 host mode, do not allow the
1387 * other host's IGMPv1 report to suppress our reports
1388 * unless explicitly configured to do so.
1390 if (igi->igi_version == IGMP_VERSION_3) {
1391 if (V_igmp_legacysupp)
1392 igmp_v3_suppress_group_record(inm);
1398 switch (inm->inm_state) {
1399 case IGMP_NOT_MEMBER:
1400 case IGMP_SILENT_MEMBER:
1401 case IGMP_SLEEPING_MEMBER:
1403 case IGMP_REPORTING_MEMBER:
1404 case IGMP_IDLE_MEMBER:
1405 case IGMP_AWAKENING_MEMBER:
1407 "report suppressed for %s on ifp %p(%s)",
1408 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1409 case IGMP_LAZY_MEMBER:
1410 inm->inm_state = IGMP_LAZY_MEMBER;
1412 case IGMP_G_QUERY_PENDING_MEMBER:
1413 case IGMP_SG_QUERY_PENDING_MEMBER:
1414 case IGMP_LEAVING_MEMBER:
1426 igmp_input(struct mbuf *m, int off)
1436 CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1438 ifp = m->m_pkthdr.rcvif;
1440 IGMPSTAT_INC(igps_rcv_total);
1442 ip = mtod(m, struct ip *);
1444 igmplen = ip->ip_len;
1449 if (igmplen < IGMP_MINLEN) {
1450 IGMPSTAT_INC(igps_rcv_tooshort);
1456 * Always pullup to the minimum size for v1/v2 or v3
1457 * to amortize calls to m_pullup().
1460 if (igmplen >= IGMP_V3_QUERY_MINLEN)
1461 minlen += IGMP_V3_QUERY_MINLEN;
1463 minlen += IGMP_MINLEN;
1464 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
1465 (m = m_pullup(m, minlen)) == 0) {
1466 IGMPSTAT_INC(igps_rcv_tooshort);
1469 ip = mtod(m, struct ip *);
1472 * Validate checksum.
1474 m->m_data += iphlen;
1476 igmp = mtod(m, struct igmp *);
1477 if (in_cksum(m, igmplen)) {
1478 IGMPSTAT_INC(igps_rcv_badsum);
1482 m->m_data -= iphlen;
1486 * IGMP control traffic is link-scope, and must have a TTL of 1.
1487 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1488 * probe packets may come from beyond the LAN.
1490 if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) {
1491 IGMPSTAT_INC(igps_rcv_badttl);
1496 switch (igmp->igmp_type) {
1497 case IGMP_HOST_MEMBERSHIP_QUERY:
1498 if (igmplen == IGMP_MINLEN) {
1499 if (igmp->igmp_code == 0)
1500 queryver = IGMP_VERSION_1;
1502 queryver = IGMP_VERSION_2;
1503 } else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1504 queryver = IGMP_VERSION_3;
1506 IGMPSTAT_INC(igps_rcv_tooshort);
1512 case IGMP_VERSION_1:
1513 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1514 if (!V_igmp_v1enable)
1516 if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1522 case IGMP_VERSION_2:
1523 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1524 if (!V_igmp_v2enable)
1526 if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
1532 case IGMP_VERSION_3: {
1533 struct igmpv3 *igmpv3;
1537 IGMPSTAT_INC(igps_rcv_v3_queries);
1538 igmpv3 = (struct igmpv3 *)igmp;
1540 * Validate length based on source count.
1542 nsrc = ntohs(igmpv3->igmp_numsrc);
1543 if (nsrc * sizeof(in_addr_t) >
1544 UINT16_MAX - iphlen - IGMP_V3_QUERY_MINLEN) {
1545 IGMPSTAT_INC(igps_rcv_tooshort);
1549 * m_pullup() may modify m, so pullup in
1552 igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1553 sizeof(struct in_addr) * nsrc;
1554 if ((m->m_flags & M_EXT ||
1555 m->m_len < igmpv3len) &&
1556 (m = m_pullup(m, igmpv3len)) == NULL) {
1557 IGMPSTAT_INC(igps_rcv_tooshort);
1560 igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1562 if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1571 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1572 if (!V_igmp_v1enable)
1574 if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1580 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1581 if (!V_igmp_v2enable)
1583 if (!ip_checkrouteralert(m))
1584 IGMPSTAT_INC(igps_rcv_nora);
1585 if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
1591 case IGMP_v3_HOST_MEMBERSHIP_REPORT:
1593 * Hosts do not need to process IGMPv3 membership reports,
1594 * as report suppression is no longer required.
1596 if (!ip_checkrouteralert(m))
1597 IGMPSTAT_INC(igps_rcv_nora);
1605 * Pass all valid IGMP packets up to any process(es) listening on a
1613 * Fast timeout handler (global).
1614 * VIMAGE: Timeout handlers are expected to service all vimages.
1619 VNET_ITERATOR_DECL(vnet_iter);
1621 VNET_LIST_RLOCK_NOSLEEP();
1622 VNET_FOREACH(vnet_iter) {
1623 CURVNET_SET(vnet_iter);
1624 igmp_fasttimo_vnet();
1627 VNET_LIST_RUNLOCK_NOSLEEP();
1631 * Fast timeout handler (per-vnet).
1632 * Sends are shuffled off to a netisr to deal with Giant.
1634 * VIMAGE: Assume caller has set up our curvnet.
1637 igmp_fasttimo_vnet(void)
1639 struct ifqueue scq; /* State-change packets */
1640 struct ifqueue qrq; /* Query response packets */
1642 struct igmp_ifinfo *igi;
1643 struct ifmultiaddr *ifma;
1644 struct in_multi *inm;
1645 int loop, uri_fasthz;
1651 * Quick check to see if any work needs to be done, in order to
1652 * minimize the overhead of fasttimo processing.
1653 * SMPng: XXX Unlocked reads.
1655 if (!V_current_state_timers_running &&
1656 !V_interface_timers_running &&
1657 !V_state_change_timers_running)
1664 * IGMPv3 General Query response timer processing.
1666 if (V_interface_timers_running) {
1667 CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1669 V_interface_timers_running = 0;
1670 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1671 if (igi->igi_v3_timer == 0) {
1673 } else if (--igi->igi_v3_timer == 0) {
1674 igmp_v3_dispatch_general_query(igi);
1676 V_interface_timers_running = 1;
1681 if (!V_current_state_timers_running &&
1682 !V_state_change_timers_running)
1685 V_current_state_timers_running = 0;
1686 V_state_change_timers_running = 0;
1688 CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1691 * IGMPv1/v2/v3 host report and state-change timer processing.
1692 * Note: Processing a v3 group timer may remove a node.
1694 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1697 if (igi->igi_version == IGMP_VERSION_3) {
1698 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1699 uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1702 memset(&qrq, 0, sizeof(struct ifqueue));
1703 IFQ_SET_MAXLEN(&qrq, IGMP_MAX_G_GS_PACKETS);
1705 memset(&scq, 0, sizeof(struct ifqueue));
1706 IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1710 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1711 if (ifma->ifma_addr->sa_family != AF_INET ||
1712 ifma->ifma_protospec == NULL)
1714 inm = (struct in_multi *)ifma->ifma_protospec;
1715 switch (igi->igi_version) {
1716 case IGMP_VERSION_1:
1717 case IGMP_VERSION_2:
1718 igmp_v1v2_process_group_timer(inm,
1721 case IGMP_VERSION_3:
1722 igmp_v3_process_group_timers(igi, &qrq,
1723 &scq, inm, uri_fasthz);
1727 IF_ADDR_RUNLOCK(ifp);
1729 if (igi->igi_version == IGMP_VERSION_3) {
1730 struct in_multi *tinm;
1732 igmp_dispatch_queue(&qrq, 0, loop);
1733 igmp_dispatch_queue(&scq, 0, loop);
1736 * Free the in_multi reference(s) for this
1739 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead,
1741 SLIST_REMOVE_HEAD(&igi->igi_relinmhead,
1743 inm_release_locked(inm);
1754 * Update host report group timer for IGMPv1/v2.
1755 * Will update the global pending timer flags.
1758 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1760 int report_timer_expired;
1762 IN_MULTI_LOCK_ASSERT();
1765 if (inm->inm_timer == 0) {
1766 report_timer_expired = 0;
1767 } else if (--inm->inm_timer == 0) {
1768 report_timer_expired = 1;
1770 V_current_state_timers_running = 1;
1774 switch (inm->inm_state) {
1775 case IGMP_NOT_MEMBER:
1776 case IGMP_SILENT_MEMBER:
1777 case IGMP_IDLE_MEMBER:
1778 case IGMP_LAZY_MEMBER:
1779 case IGMP_SLEEPING_MEMBER:
1780 case IGMP_AWAKENING_MEMBER:
1782 case IGMP_REPORTING_MEMBER:
1783 if (report_timer_expired) {
1784 inm->inm_state = IGMP_IDLE_MEMBER;
1785 (void)igmp_v1v2_queue_report(inm,
1786 (version == IGMP_VERSION_2) ?
1787 IGMP_v2_HOST_MEMBERSHIP_REPORT :
1788 IGMP_v1_HOST_MEMBERSHIP_REPORT);
1791 case IGMP_G_QUERY_PENDING_MEMBER:
1792 case IGMP_SG_QUERY_PENDING_MEMBER:
1793 case IGMP_LEAVING_MEMBER:
1799 * Update a group's timers for IGMPv3.
1800 * Will update the global pending timer flags.
1801 * Note: Unlocked read from igi.
1804 igmp_v3_process_group_timers(struct igmp_ifinfo *igi,
1805 struct ifqueue *qrq, struct ifqueue *scq,
1806 struct in_multi *inm, const int uri_fasthz)
1808 int query_response_timer_expired;
1809 int state_change_retransmit_timer_expired;
1811 IN_MULTI_LOCK_ASSERT();
1814 query_response_timer_expired = 0;
1815 state_change_retransmit_timer_expired = 0;
1818 * During a transition from v1/v2 compatibility mode back to v3,
1819 * a group record in REPORTING state may still have its group
1820 * timer active. This is a no-op in this function; it is easier
1821 * to deal with it here than to complicate the slow-timeout path.
1823 if (inm->inm_timer == 0) {
1824 query_response_timer_expired = 0;
1825 } else if (--inm->inm_timer == 0) {
1826 query_response_timer_expired = 1;
1828 V_current_state_timers_running = 1;
1831 if (inm->inm_sctimer == 0) {
1832 state_change_retransmit_timer_expired = 0;
1833 } else if (--inm->inm_sctimer == 0) {
1834 state_change_retransmit_timer_expired = 1;
1836 V_state_change_timers_running = 1;
1839 /* We are in fasttimo, so be quick about it. */
1840 if (!state_change_retransmit_timer_expired &&
1841 !query_response_timer_expired)
1844 switch (inm->inm_state) {
1845 case IGMP_NOT_MEMBER:
1846 case IGMP_SILENT_MEMBER:
1847 case IGMP_SLEEPING_MEMBER:
1848 case IGMP_LAZY_MEMBER:
1849 case IGMP_AWAKENING_MEMBER:
1850 case IGMP_IDLE_MEMBER:
1852 case IGMP_G_QUERY_PENDING_MEMBER:
1853 case IGMP_SG_QUERY_PENDING_MEMBER:
1855 * Respond to a previously pending Group-Specific
1856 * or Group-and-Source-Specific query by enqueueing
1857 * the appropriate Current-State report for
1858 * immediate transmission.
1860 if (query_response_timer_expired) {
1863 retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1864 (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1865 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1867 inm->inm_state = IGMP_REPORTING_MEMBER;
1868 /* XXX Clear recorded sources for next time. */
1869 inm_clear_recorded(inm);
1872 case IGMP_REPORTING_MEMBER:
1873 case IGMP_LEAVING_MEMBER:
1874 if (state_change_retransmit_timer_expired) {
1876 * State-change retransmission timer fired.
1877 * If there are any further pending retransmissions,
1878 * set the global pending state-change flag, and
1881 if (--inm->inm_scrv > 0) {
1882 inm->inm_sctimer = uri_fasthz;
1883 V_state_change_timers_running = 1;
1886 * Retransmit the previously computed state-change
1887 * report. If there are no further pending
1888 * retransmissions, the mbuf queue will be consumed.
1889 * Update T0 state to T1 as we have now sent
1892 (void)igmp_v3_merge_state_changes(inm, scq);
1895 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
1896 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
1899 * If we are leaving the group for good, make sure
1900 * we release IGMP's reference to it.
1901 * This release must be deferred using a SLIST,
1902 * as we are called from a loop which traverses
1903 * the in_ifmultiaddr TAILQ.
1905 if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1906 inm->inm_scrv == 0) {
1907 inm->inm_state = IGMP_NOT_MEMBER;
1908 SLIST_INSERT_HEAD(&igi->igi_relinmhead,
1918 * Suppress a group's pending response to a group or source/group query.
1920 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1921 * Do NOT update ST1/ST0 as this operation merely suppresses
1922 * the currently pending group record.
1923 * Do NOT suppress the response to a general query. It is possible but
1924 * it would require adding another state or flag.
1927 igmp_v3_suppress_group_record(struct in_multi *inm)
1930 IN_MULTI_LOCK_ASSERT();
1932 KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1933 ("%s: not IGMPv3 mode on link", __func__));
1935 if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1936 inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1939 if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1940 inm_clear_recorded(inm);
1943 inm->inm_state = IGMP_REPORTING_MEMBER;
1947 * Switch to a different IGMP version on the given interface,
1948 * as per Section 7.2.1.
1951 igmp_set_version(struct igmp_ifinfo *igi, const int version)
1953 int old_version_timer;
1957 CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1958 version, igi->igi_ifp, igi->igi_ifp->if_xname);
1960 if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1962 * Compute the "Older Version Querier Present" timer as per
1965 old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1966 old_version_timer *= PR_SLOWHZ;
1968 if (version == IGMP_VERSION_1) {
1969 igi->igi_v1_timer = old_version_timer;
1970 igi->igi_v2_timer = 0;
1971 } else if (version == IGMP_VERSION_2) {
1972 igi->igi_v1_timer = 0;
1973 igi->igi_v2_timer = old_version_timer;
1977 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
1978 if (igi->igi_version != IGMP_VERSION_2) {
1979 igi->igi_version = IGMP_VERSION_2;
1980 igmp_v3_cancel_link_timers(igi);
1982 } else if (igi->igi_v1_timer > 0) {
1983 if (igi->igi_version != IGMP_VERSION_1) {
1984 igi->igi_version = IGMP_VERSION_1;
1985 igmp_v3_cancel_link_timers(igi);
1991 * Cancel pending IGMPv3 timers for the given link and all groups
1992 * joined on it; state-change, general-query, and group-query timers.
1994 * Only ever called on a transition from v3 to Compatibility mode. Kill
1995 * the timers stone dead (this may be expensive for large N groups), they
1996 * will be restarted if Compatibility Mode deems that they must be due to
2000 igmp_v3_cancel_link_timers(struct igmp_ifinfo *igi)
2002 struct ifmultiaddr *ifma;
2004 struct in_multi *inm, *tinm;
2006 CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2007 igi->igi_ifp, igi->igi_ifp->if_xname);
2009 IN_MULTI_LOCK_ASSERT();
2013 * Stop the v3 General Query Response on this link stone dead.
2014 * If fasttimo is woken up due to V_interface_timers_running,
2015 * the flag will be cleared if there are no pending link timers.
2017 igi->igi_v3_timer = 0;
2020 * Now clear the current-state and state-change report timers
2021 * for all memberships scoped to this link.
2025 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2026 if (ifma->ifma_addr->sa_family != AF_INET ||
2027 ifma->ifma_protospec == NULL)
2029 inm = (struct in_multi *)ifma->ifma_protospec;
2030 switch (inm->inm_state) {
2031 case IGMP_NOT_MEMBER:
2032 case IGMP_SILENT_MEMBER:
2033 case IGMP_IDLE_MEMBER:
2034 case IGMP_LAZY_MEMBER:
2035 case IGMP_SLEEPING_MEMBER:
2036 case IGMP_AWAKENING_MEMBER:
2038 * These states are either not relevant in v3 mode,
2039 * or are unreported. Do nothing.
2042 case IGMP_LEAVING_MEMBER:
2044 * If we are leaving the group and switching to
2045 * compatibility mode, we need to release the final
2046 * reference held for issuing the INCLUDE {}, and
2047 * transition to REPORTING to ensure the host leave
2048 * message is sent upstream to the old querier --
2049 * transition to NOT would lose the leave and race.
2051 SLIST_INSERT_HEAD(&igi->igi_relinmhead, inm, inm_nrele);
2053 case IGMP_G_QUERY_PENDING_MEMBER:
2054 case IGMP_SG_QUERY_PENDING_MEMBER:
2055 inm_clear_recorded(inm);
2057 case IGMP_REPORTING_MEMBER:
2058 inm->inm_state = IGMP_REPORTING_MEMBER;
2062 * Always clear state-change and group report timers.
2063 * Free any pending IGMPv3 state-change records.
2065 inm->inm_sctimer = 0;
2067 _IF_DRAIN(&inm->inm_scq);
2069 IF_ADDR_RUNLOCK(ifp);
2070 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, tinm) {
2071 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
2072 inm_release_locked(inm);
2077 * Update the Older Version Querier Present timers for a link.
2078 * See Section 7.2.1 of RFC 3376.
2081 igmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi)
2086 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2088 * IGMPv1 and IGMPv2 Querier Present timers expired.
2092 if (igi->igi_version != IGMP_VERSION_3) {
2094 "%s: transition from v%d -> v%d on %p(%s)",
2095 __func__, igi->igi_version, IGMP_VERSION_3,
2096 igi->igi_ifp, igi->igi_ifp->if_xname);
2097 igi->igi_version = IGMP_VERSION_3;
2099 } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2101 * IGMPv1 Querier Present timer expired,
2102 * IGMPv2 Querier Present timer running.
2103 * If IGMPv2 was disabled since last timeout,
2105 * If IGMPv2 is enabled, revert to IGMPv2.
2107 if (!V_igmp_v2enable) {
2109 "%s: transition from v%d -> v%d on %p(%s)",
2110 __func__, igi->igi_version, IGMP_VERSION_3,
2111 igi->igi_ifp, igi->igi_ifp->if_xname);
2112 igi->igi_v2_timer = 0;
2113 igi->igi_version = IGMP_VERSION_3;
2115 --igi->igi_v2_timer;
2116 if (igi->igi_version != IGMP_VERSION_2) {
2118 "%s: transition from v%d -> v%d on %p(%s)",
2119 __func__, igi->igi_version, IGMP_VERSION_2,
2120 igi->igi_ifp, igi->igi_ifp->if_xname);
2121 igi->igi_version = IGMP_VERSION_2;
2124 } else if (igi->igi_v1_timer > 0) {
2126 * IGMPv1 Querier Present timer running.
2127 * Stop IGMPv2 timer if running.
2129 * If IGMPv1 was disabled since last timeout,
2131 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2133 if (!V_igmp_v1enable) {
2135 "%s: transition from v%d -> v%d on %p(%s)",
2136 __func__, igi->igi_version, IGMP_VERSION_3,
2137 igi->igi_ifp, igi->igi_ifp->if_xname);
2138 igi->igi_v1_timer = 0;
2139 igi->igi_version = IGMP_VERSION_3;
2141 --igi->igi_v1_timer;
2143 if (igi->igi_v2_timer > 0) {
2145 "%s: cancel v2 timer on %p(%s)",
2146 __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2147 igi->igi_v2_timer = 0;
2153 * Global slowtimo handler.
2154 * VIMAGE: Timeout handlers are expected to service all vimages.
2159 VNET_ITERATOR_DECL(vnet_iter);
2161 VNET_LIST_RLOCK_NOSLEEP();
2162 VNET_FOREACH(vnet_iter) {
2163 CURVNET_SET(vnet_iter);
2164 igmp_slowtimo_vnet();
2167 VNET_LIST_RUNLOCK_NOSLEEP();
2171 * Per-vnet slowtimo handler.
2174 igmp_slowtimo_vnet(void)
2176 struct igmp_ifinfo *igi;
2180 LIST_FOREACH(igi, &V_igi_head, igi_link) {
2181 igmp_v1v2_process_querier_timers(igi);
2188 * Dispatch an IGMPv1/v2 host report or leave message.
2189 * These are always small enough to fit inside a single mbuf.
2192 igmp_v1v2_queue_report(struct in_multi *inm, const int type)
2199 IN_MULTI_LOCK_ASSERT();
2204 MGETHDR(m, M_DONTWAIT, MT_DATA);
2207 MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
2209 m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2211 m->m_data += sizeof(struct ip);
2212 m->m_len = sizeof(struct igmp);
2214 igmp = mtod(m, struct igmp *);
2215 igmp->igmp_type = type;
2216 igmp->igmp_code = 0;
2217 igmp->igmp_group = inm->inm_addr;
2218 igmp->igmp_cksum = 0;
2219 igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
2221 m->m_data -= sizeof(struct ip);
2222 m->m_len += sizeof(struct ip);
2224 ip = mtod(m, struct ip *);
2226 ip->ip_len = sizeof(struct ip) + sizeof(struct igmp);
2228 ip->ip_p = IPPROTO_IGMP;
2229 ip->ip_src.s_addr = INADDR_ANY;
2231 if (type == IGMP_HOST_LEAVE_MESSAGE)
2232 ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2234 ip->ip_dst = inm->inm_addr;
2236 igmp_save_context(m, ifp);
2238 m->m_flags |= M_IGMPV2;
2239 if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2240 m->m_flags |= M_IGMP_LOOP;
2242 CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2243 netisr_dispatch(NETISR_IGMP, m);
2249 * Process a state change from the upper layer for the given IPv4 group.
2251 * Each socket holds a reference on the in_multi in its own ip_moptions.
2252 * The socket layer will have made the necessary updates to.the group
2253 * state, it is now up to IGMP to issue a state change report if there
2254 * has been any change between T0 (when the last state-change was issued)
2257 * We use the IGMPv3 state machine at group level. The IGMP module
2258 * however makes the decision as to which IGMP protocol version to speak.
2259 * A state change *from* INCLUDE {} always means an initial join.
2260 * A state change *to* INCLUDE {} always means a final leave.
2262 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2263 * save ourselves a bunch of work; any exclusive mode groups need not
2264 * compute source filter lists.
2266 * VIMAGE: curvnet should have been set by caller, as this routine
2267 * is called from the socket option handlers.
2270 igmp_change_state(struct in_multi *inm)
2272 struct igmp_ifinfo *igi;
2276 IN_MULTI_LOCK_ASSERT();
2281 * Try to detect if the upper layer just asked us to change state
2282 * for an interface which has now gone away.
2284 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2285 ifp = inm->inm_ifma->ifma_ifp;
2287 * Sanity check that netinet's notion of ifp is the
2290 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2294 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2295 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
2298 * If we detect a state transition to or from MCAST_UNDEFINED
2299 * for this group, then we are starting or finishing an IGMP
2300 * life cycle for this group.
2302 if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2303 CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2304 inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2305 if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2306 CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2307 error = igmp_initial_join(inm, igi);
2309 } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2310 CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2311 igmp_final_leave(inm, igi);
2315 CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2318 error = igmp_handle_state_change(inm, igi);
2326 * Perform the initial join for an IGMP group.
2328 * When joining a group:
2329 * If the group should have its IGMP traffic suppressed, do nothing.
2330 * IGMPv1 starts sending IGMPv1 host membership reports.
2331 * IGMPv2 starts sending IGMPv2 host membership reports.
2332 * IGMPv3 will schedule an IGMPv3 state-change report containing the
2333 * initial state of the membership.
2336 igmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi)
2339 struct ifqueue *ifq;
2340 int error, retval, syncstates;
2342 CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)",
2343 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2344 inm->inm_ifp->if_xname);
2351 IN_MULTI_LOCK_ASSERT();
2354 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2357 * Groups joined on loopback or marked as 'not reported',
2358 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2359 * are never reported in any IGMP protocol exchanges.
2360 * All other groups enter the appropriate IGMP state machine
2361 * for the version in use on this link.
2362 * A link marked as IGIF_SILENT causes IGMP to be completely
2363 * disabled for the link.
2365 if ((ifp->if_flags & IFF_LOOPBACK) ||
2366 (igi->igi_flags & IGIF_SILENT) ||
2367 !igmp_isgroupreported(inm->inm_addr)) {
2369 "%s: not kicking state machine for silent group", __func__);
2370 inm->inm_state = IGMP_SILENT_MEMBER;
2374 * Deal with overlapping in_multi lifecycle.
2375 * If this group was LEAVING, then make sure
2376 * we drop the reference we picked up to keep the
2377 * group around for the final INCLUDE {} enqueue.
2379 if (igi->igi_version == IGMP_VERSION_3 &&
2380 inm->inm_state == IGMP_LEAVING_MEMBER)
2381 inm_release_locked(inm);
2383 inm->inm_state = IGMP_REPORTING_MEMBER;
2385 switch (igi->igi_version) {
2386 case IGMP_VERSION_1:
2387 case IGMP_VERSION_2:
2388 inm->inm_state = IGMP_IDLE_MEMBER;
2389 error = igmp_v1v2_queue_report(inm,
2390 (igi->igi_version == IGMP_VERSION_2) ?
2391 IGMP_v2_HOST_MEMBERSHIP_REPORT :
2392 IGMP_v1_HOST_MEMBERSHIP_REPORT);
2394 inm->inm_timer = IGMP_RANDOM_DELAY(
2395 IGMP_V1V2_MAX_RI * PR_FASTHZ);
2396 V_current_state_timers_running = 1;
2400 case IGMP_VERSION_3:
2402 * Defer update of T0 to T1, until the first copy
2403 * of the state change has been transmitted.
2408 * Immediately enqueue a State-Change Report for
2409 * this interface, freeing any previous reports.
2410 * Don't kick the timers if there is nothing to do,
2411 * or if an error occurred.
2413 ifq = &inm->inm_scq;
2415 retval = igmp_v3_enqueue_group_record(ifq, inm, 1,
2417 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2420 error = retval * -1;
2425 * Schedule transmission of pending state-change
2426 * report up to RV times for this link. The timer
2427 * will fire at the next igmp_fasttimo (~200ms),
2428 * giving us an opportunity to merge the reports.
2430 if (igi->igi_flags & IGIF_LOOPBACK) {
2433 KASSERT(igi->igi_rv > 1,
2434 ("%s: invalid robustness %d", __func__,
2436 inm->inm_scrv = igi->igi_rv;
2438 inm->inm_sctimer = 1;
2439 V_state_change_timers_running = 1;
2447 * Only update the T0 state if state change is atomic,
2448 * i.e. we don't need to wait for a timer to fire before we
2449 * can consider the state change to have been communicated.
2453 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2454 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2461 * Issue an intermediate state change during the IGMP life-cycle.
2464 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi)
2469 CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)",
2470 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2471 inm->inm_ifp->if_xname);
2475 IN_MULTI_LOCK_ASSERT();
2478 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2480 if ((ifp->if_flags & IFF_LOOPBACK) ||
2481 (igi->igi_flags & IGIF_SILENT) ||
2482 !igmp_isgroupreported(inm->inm_addr) ||
2483 (igi->igi_version != IGMP_VERSION_3)) {
2484 if (!igmp_isgroupreported(inm->inm_addr)) {
2486 "%s: not kicking state machine for silent group", __func__);
2488 CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2490 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2491 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2495 _IF_DRAIN(&inm->inm_scq);
2497 retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2498 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2503 * If record(s) were enqueued, start the state-change
2504 * report timer for this group.
2506 inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2507 inm->inm_sctimer = 1;
2508 V_state_change_timers_running = 1;
2514 * Perform the final leave for an IGMP group.
2516 * When leaving a group:
2517 * IGMPv1 does nothing.
2518 * IGMPv2 sends a host leave message, if and only if we are the reporter.
2519 * IGMPv3 enqueues a state-change report containing a transition
2520 * to INCLUDE {} for immediate transmission.
2523 igmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi)
2529 CTR4(KTR_IGMPV3, "%s: final leave %s on ifp %p(%s)",
2530 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2531 inm->inm_ifp->if_xname);
2533 IN_MULTI_LOCK_ASSERT();
2536 switch (inm->inm_state) {
2537 case IGMP_NOT_MEMBER:
2538 case IGMP_SILENT_MEMBER:
2539 case IGMP_LEAVING_MEMBER:
2540 /* Already leaving or left; do nothing. */
2542 "%s: not kicking state machine for silent group", __func__);
2544 case IGMP_REPORTING_MEMBER:
2545 case IGMP_IDLE_MEMBER:
2546 case IGMP_G_QUERY_PENDING_MEMBER:
2547 case IGMP_SG_QUERY_PENDING_MEMBER:
2548 if (igi->igi_version == IGMP_VERSION_2) {
2550 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2551 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2552 panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2555 igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2556 inm->inm_state = IGMP_NOT_MEMBER;
2557 } else if (igi->igi_version == IGMP_VERSION_3) {
2559 * Stop group timer and all pending reports.
2560 * Immediately enqueue a state-change report
2561 * TO_IN {} to be sent on the next fast timeout,
2562 * giving us an opportunity to merge reports.
2564 _IF_DRAIN(&inm->inm_scq);
2566 if (igi->igi_flags & IGIF_LOOPBACK) {
2569 inm->inm_scrv = igi->igi_rv;
2571 CTR4(KTR_IGMPV3, "%s: Leaving %s/%s with %d "
2572 "pending retransmissions.", __func__,
2573 inet_ntoa(inm->inm_addr),
2574 inm->inm_ifp->if_xname, inm->inm_scrv);
2575 if (inm->inm_scrv == 0) {
2576 inm->inm_state = IGMP_NOT_MEMBER;
2577 inm->inm_sctimer = 0;
2581 inm_acquire_locked(inm);
2583 retval = igmp_v3_enqueue_group_record(
2584 &inm->inm_scq, inm, 1, 0, 0);
2585 KASSERT(retval != 0,
2586 ("%s: enqueue record = %d", __func__,
2589 inm->inm_state = IGMP_LEAVING_MEMBER;
2590 inm->inm_sctimer = 1;
2591 V_state_change_timers_running = 1;
2597 case IGMP_LAZY_MEMBER:
2598 case IGMP_SLEEPING_MEMBER:
2599 case IGMP_AWAKENING_MEMBER:
2600 /* Our reports are suppressed; do nothing. */
2606 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2607 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2608 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2609 CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for %s/%s",
2610 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2615 * Enqueue an IGMPv3 group record to the given output queue.
2617 * XXX This function could do with having the allocation code
2618 * split out, and the multiple-tree-walks coalesced into a single
2619 * routine as has been done in igmp_v3_enqueue_filter_change().
2621 * If is_state_change is zero, a current-state record is appended.
2622 * If is_state_change is non-zero, a state-change report is appended.
2624 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2625 * If is_group_query is zero, and if there is a packet with free space
2626 * at the tail of the queue, it will be appended to providing there
2627 * is enough free space.
2628 * Otherwise a new mbuf packet chain is allocated.
2630 * If is_source_query is non-zero, each source is checked to see if
2631 * it was recorded for a Group-Source query, and will be omitted if
2632 * it is not both in-mode and recorded.
2634 * The function will attempt to allocate leading space in the packet
2635 * for the IP/IGMP header to be prepended without fragmenting the chain.
2637 * If successful the size of all data appended to the queue is returned,
2638 * otherwise an error code less than zero is returned, or zero if
2639 * no record(s) were appended.
2642 igmp_v3_enqueue_group_record(struct ifqueue *ifq, struct in_multi *inm,
2643 const int is_state_change, const int is_group_query,
2644 const int is_source_query)
2646 struct igmp_grouprec ig;
2647 struct igmp_grouprec *pig;
2649 struct ip_msource *ims, *nims;
2650 struct mbuf *m0, *m, *md;
2651 int error, is_filter_list_change;
2652 int minrec0len, m0srcs, msrcs, nbytes, off;
2653 int record_has_sources;
2659 IN_MULTI_LOCK_ASSERT();
2663 is_filter_list_change = 0;
2670 record_has_sources = 1;
2672 type = IGMP_DO_NOTHING;
2673 mode = inm->inm_st[1].iss_fmode;
2676 * If we did not transition out of ASM mode during t0->t1,
2677 * and there are no source nodes to process, we can skip
2678 * the generation of source records.
2680 if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2682 record_has_sources = 0;
2684 if (is_state_change) {
2686 * Queue a state change record.
2687 * If the mode did not change, and there are non-ASM
2688 * listeners or source filters present,
2689 * we potentially need to issue two records for the group.
2690 * If we are transitioning to MCAST_UNDEFINED, we need
2691 * not send any sources.
2692 * If there are ASM listeners, and there was no filter
2693 * mode transition of any kind, do nothing.
2695 if (mode != inm->inm_st[0].iss_fmode) {
2696 if (mode == MCAST_EXCLUDE) {
2697 CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2699 type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2701 CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2703 type = IGMP_CHANGE_TO_INCLUDE_MODE;
2704 if (mode == MCAST_UNDEFINED)
2705 record_has_sources = 0;
2708 if (record_has_sources) {
2709 is_filter_list_change = 1;
2711 type = IGMP_DO_NOTHING;
2716 * Queue a current state record.
2718 if (mode == MCAST_EXCLUDE) {
2719 type = IGMP_MODE_IS_EXCLUDE;
2720 } else if (mode == MCAST_INCLUDE) {
2721 type = IGMP_MODE_IS_INCLUDE;
2722 KASSERT(inm->inm_st[1].iss_asm == 0,
2723 ("%s: inm %p is INCLUDE but ASM count is %d",
2724 __func__, inm, inm->inm_st[1].iss_asm));
2729 * Generate the filter list changes using a separate function.
2731 if (is_filter_list_change)
2732 return (igmp_v3_enqueue_filter_change(ifq, inm));
2734 if (type == IGMP_DO_NOTHING) {
2735 CTR3(KTR_IGMPV3, "%s: nothing to do for %s/%s",
2736 __func__, inet_ntoa(inm->inm_addr),
2737 inm->inm_ifp->if_xname);
2742 * If any sources are present, we must be able to fit at least
2743 * one in the trailing space of the tail packet's mbuf,
2746 minrec0len = sizeof(struct igmp_grouprec);
2747 if (record_has_sources)
2748 minrec0len += sizeof(in_addr_t);
2750 CTR4(KTR_IGMPV3, "%s: queueing %s for %s/%s", __func__,
2751 igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr),
2752 inm->inm_ifp->if_xname);
2755 * Check if we have a packet in the tail of the queue for this
2756 * group into which the first group record for this group will fit.
2757 * Otherwise allocate a new packet.
2758 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2759 * Note: Group records for G/GSR query responses MUST be sent
2760 * in their own packet.
2763 if (!is_group_query &&
2765 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2766 (m0->m_pkthdr.len + minrec0len) <
2767 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2768 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2769 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2771 CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2773 if (_IF_QFULL(ifq)) {
2774 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2778 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2779 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2780 if (!is_state_change && !is_group_query) {
2781 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2783 m->m_data += IGMP_LEADINGSPACE;
2786 m = m_gethdr(M_DONTWAIT, MT_DATA);
2788 MH_ALIGN(m, IGMP_LEADINGSPACE);
2793 igmp_save_context(m, ifp);
2795 CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2799 * Append group record.
2800 * If we have sources, we don't know how many yet.
2805 ig.ig_group = inm->inm_addr;
2806 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2809 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2812 nbytes += sizeof(struct igmp_grouprec);
2815 * Append as many sources as will fit in the first packet.
2816 * If we are appending to a new packet, the chain allocation
2817 * may potentially use clusters; use m_getptr() in this case.
2818 * If we are appending to an existing packet, we need to obtain
2819 * a pointer to the group record after m_append(), in case a new
2820 * mbuf was allocated.
2821 * Only append sources which are in-mode at t1. If we are
2822 * transitioning to MCAST_UNDEFINED state on the group, do not
2823 * include source entries.
2824 * Only report recorded sources in our filter set when responding
2825 * to a group-source query.
2827 if (record_has_sources) {
2830 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2831 md->m_len - nbytes);
2833 md = m_getptr(m, 0, &off);
2834 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2838 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2839 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2840 inet_ntoa_haddr(ims->ims_haddr));
2841 now = ims_get_mode(inm, ims, 1);
2842 CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2843 if ((now != mode) ||
2844 (now == mode && mode == MCAST_UNDEFINED)) {
2845 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2848 if (is_source_query && ims->ims_stp == 0) {
2849 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2853 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2854 naddr = htonl(ims->ims_haddr);
2855 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2858 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2862 nbytes += sizeof(in_addr_t);
2864 if (msrcs == m0srcs)
2867 CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2869 pig->ig_numsrc = htons(msrcs);
2870 nbytes += (msrcs * sizeof(in_addr_t));
2873 if (is_source_query && msrcs == 0) {
2874 CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2881 * We are good to go with first packet.
2884 CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2885 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2886 _IF_ENQUEUE(ifq, m);
2888 m->m_pkthdr.PH_vt.vt_nrecs++;
2891 * No further work needed if no source list in packet(s).
2893 if (!record_has_sources)
2897 * Whilst sources remain to be announced, we need to allocate
2898 * a new packet and fill out as many sources as will fit.
2899 * Always try for a cluster first.
2901 while (nims != NULL) {
2902 if (_IF_QFULL(ifq)) {
2903 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2906 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2908 m->m_data += IGMP_LEADINGSPACE;
2910 m = m_gethdr(M_DONTWAIT, MT_DATA);
2912 MH_ALIGN(m, IGMP_LEADINGSPACE);
2916 igmp_save_context(m, ifp);
2917 md = m_getptr(m, 0, &off);
2918 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2919 CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2921 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2924 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2927 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2928 nbytes += sizeof(struct igmp_grouprec);
2930 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2931 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2934 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2935 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2936 inet_ntoa_haddr(ims->ims_haddr));
2937 now = ims_get_mode(inm, ims, 1);
2938 if ((now != mode) ||
2939 (now == mode && mode == MCAST_UNDEFINED)) {
2940 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2943 if (is_source_query && ims->ims_stp == 0) {
2944 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2948 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2949 naddr = htonl(ims->ims_haddr);
2950 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2953 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2958 if (msrcs == m0srcs)
2961 pig->ig_numsrc = htons(msrcs);
2962 nbytes += (msrcs * sizeof(in_addr_t));
2964 CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
2965 _IF_ENQUEUE(ifq, m);
2972 * Type used to mark record pass completion.
2973 * We exploit the fact we can cast to this easily from the
2974 * current filter modes on each ip_msource node.
2977 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2978 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2979 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2980 REC_FULL = REC_ALLOW | REC_BLOCK
2984 * Enqueue an IGMPv3 filter list change to the given output queue.
2986 * Source list filter state is held in an RB-tree. When the filter list
2987 * for a group is changed without changing its mode, we need to compute
2988 * the deltas between T0 and T1 for each source in the filter set,
2989 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2991 * As we may potentially queue two record types, and the entire R-B tree
2992 * needs to be walked at once, we break this out into its own function
2993 * so we can generate a tightly packed queue of packets.
2995 * XXX This could be written to only use one tree walk, although that makes
2996 * serializing into the mbuf chains a bit harder. For now we do two walks
2997 * which makes things easier on us, and it may or may not be harder on
3000 * If successful the size of all data appended to the queue is returned,
3001 * otherwise an error code less than zero is returned, or zero if
3002 * no record(s) were appended.
3005 igmp_v3_enqueue_filter_change(struct ifqueue *ifq, struct in_multi *inm)
3007 static const int MINRECLEN =
3008 sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3010 struct igmp_grouprec ig;
3011 struct igmp_grouprec *pig;
3012 struct ip_msource *ims, *nims;
3013 struct mbuf *m, *m0, *md;
3015 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3017 uint8_t mode, now, then;
3018 rectype_t crt, drt, nrt;
3020 IN_MULTI_LOCK_ASSERT();
3022 if (inm->inm_nsrc == 0 ||
3023 (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3026 ifp = inm->inm_ifp; /* interface */
3027 mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */
3028 crt = REC_NONE; /* current group record type */
3029 drt = REC_NONE; /* mask of completed group record types */
3030 nrt = REC_NONE; /* record type for current node */
3031 m0srcs = 0; /* # source which will fit in current mbuf chain */
3032 nbytes = 0; /* # of bytes appended to group's state-change queue */
3033 npbytes = 0; /* # of bytes appended this packet */
3034 rsrcs = 0; /* # sources encoded in current record */
3035 schanged = 0; /* # nodes encoded in overall filter change */
3036 nallow = 0; /* # of source entries in ALLOW_NEW */
3037 nblock = 0; /* # of source entries in BLOCK_OLD */
3038 nims = NULL; /* next tree node pointer */
3041 * For each possible filter record mode.
3042 * The first kind of source we encounter tells us which
3043 * is the first kind of record we start appending.
3044 * If a node transitioned to UNDEFINED at t1, its mode is treated
3045 * as the inverse of the group's filter mode.
3047 while (drt != REC_FULL) {
3051 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
3052 IGMP_V3_REPORT_MAXRECS) &&
3053 (m0->m_pkthdr.len + MINRECLEN) <
3054 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3056 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3057 sizeof(struct igmp_grouprec)) /
3060 "%s: use previous packet", __func__);
3062 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
3064 m->m_data += IGMP_LEADINGSPACE;
3066 m = m_gethdr(M_DONTWAIT, MT_DATA);
3068 MH_ALIGN(m, IGMP_LEADINGSPACE);
3072 "%s: m_get*() failed", __func__);
3075 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3076 igmp_save_context(m, ifp);
3077 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3078 sizeof(struct igmp_grouprec)) /
3082 "%s: allocated new packet", __func__);
3085 * Append the IGMP group record header to the
3086 * current packet's data area.
3087 * Recalculate pointer to free space for next
3088 * group record, in case m_append() allocated
3089 * a new mbuf or cluster.
3091 memset(&ig, 0, sizeof(ig));
3092 ig.ig_group = inm->inm_addr;
3093 if (!m_append(m, sizeof(ig), (void *)&ig)) {
3097 "%s: m_append() failed", __func__);
3100 npbytes += sizeof(struct igmp_grouprec);
3102 /* new packet; offset in c hain */
3103 md = m_getptr(m, npbytes -
3104 sizeof(struct igmp_grouprec), &off);
3105 pig = (struct igmp_grouprec *)(mtod(md,
3108 /* current packet; offset from last append */
3110 pig = (struct igmp_grouprec *)(mtod(md,
3111 uint8_t *) + md->m_len -
3112 sizeof(struct igmp_grouprec));
3115 * Begin walking the tree for this record type
3116 * pass, or continue from where we left off
3117 * previously if we had to allocate a new packet.
3118 * Only report deltas in-mode at t1.
3119 * We need not report included sources as allowed
3120 * if we are in inclusive mode on the group,
3121 * however the converse is not true.
3125 nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3126 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3127 CTR2(KTR_IGMPV3, "%s: visit node %s",
3128 __func__, inet_ntoa_haddr(ims->ims_haddr));
3129 now = ims_get_mode(inm, ims, 1);
3130 then = ims_get_mode(inm, ims, 0);
3131 CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3132 __func__, then, now);
3135 "%s: skip unchanged", __func__);
3138 if (mode == MCAST_EXCLUDE &&
3139 now == MCAST_INCLUDE) {
3141 "%s: skip IN src on EX group",
3145 nrt = (rectype_t)now;
3146 if (nrt == REC_NONE)
3147 nrt = (rectype_t)(~mode & REC_FULL);
3148 if (schanged++ == 0) {
3150 } else if (crt != nrt)
3152 naddr = htonl(ims->ims_haddr);
3153 if (!m_append(m, sizeof(in_addr_t),
3158 "%s: m_append() failed", __func__);
3161 nallow += !!(crt == REC_ALLOW);
3162 nblock += !!(crt == REC_BLOCK);
3163 if (++rsrcs == m0srcs)
3167 * If we did not append any tree nodes on this
3168 * pass, back out of allocations.
3171 npbytes -= sizeof(struct igmp_grouprec);
3174 "%s: m_free(m)", __func__);
3178 "%s: m_adj(m, -ig)", __func__);
3179 m_adj(m, -((int)sizeof(
3180 struct igmp_grouprec)));
3184 npbytes += (rsrcs * sizeof(in_addr_t));
3185 if (crt == REC_ALLOW)
3186 pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3187 else if (crt == REC_BLOCK)
3188 pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3189 pig->ig_numsrc = htons(rsrcs);
3191 * Count the new group record, and enqueue this
3192 * packet if it wasn't already queued.
3194 m->m_pkthdr.PH_vt.vt_nrecs++;
3196 _IF_ENQUEUE(ifq, m);
3198 } while (nims != NULL);
3200 crt = (~crt & REC_FULL);
3203 CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3210 igmp_v3_merge_state_changes(struct in_multi *inm, struct ifqueue *ifscq)
3213 struct mbuf *m; /* pending state-change */
3214 struct mbuf *m0; /* copy of pending state-change */
3215 struct mbuf *mt; /* last state-change in packet */
3216 int docopy, domerge;
3223 IN_MULTI_LOCK_ASSERT();
3227 * If there are further pending retransmissions, make a writable
3228 * copy of each queued state-change message before merging.
3230 if (inm->inm_scrv > 0)
3235 if (gq->ifq_head == NULL) {
3236 CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3244 * Only merge the report into the current packet if
3245 * there is sufficient space to do so; an IGMPv3 report
3246 * packet may only contain 65,535 group records.
3247 * Always use a simple mbuf chain concatentation to do this,
3248 * as large state changes for single groups may have
3249 * allocated clusters.
3252 mt = ifscq->ifq_tail;
3254 recslen = m_length(m, NULL);
3256 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
3257 m->m_pkthdr.PH_vt.vt_nrecs <=
3258 IGMP_V3_REPORT_MAXRECS) &&
3259 (mt->m_pkthdr.len + recslen <=
3260 (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3264 if (!domerge && _IF_QFULL(gq)) {
3266 "%s: outbound queue full, skipping whole packet %p",
3276 CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3277 _IF_DEQUEUE(gq, m0);
3280 CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3281 m0 = m_dup(m, M_NOWAIT);
3284 m0->m_nextpkt = NULL;
3289 CTR3(KTR_IGMPV3, "%s: queueing %p to ifscq %p)",
3290 __func__, m0, ifscq);
3291 _IF_ENQUEUE(ifscq, m0);
3293 struct mbuf *mtl; /* last mbuf of packet mt */
3295 CTR3(KTR_IGMPV3, "%s: merging %p with ifscq tail %p)",
3299 m0->m_flags &= ~M_PKTHDR;
3300 mt->m_pkthdr.len += recslen;
3301 mt->m_pkthdr.PH_vt.vt_nrecs +=
3302 m0->m_pkthdr.PH_vt.vt_nrecs;
3312 * Respond to a pending IGMPv3 General Query.
3315 igmp_v3_dispatch_general_query(struct igmp_ifinfo *igi)
3317 struct ifmultiaddr *ifma;
3319 struct in_multi *inm;
3322 IN_MULTI_LOCK_ASSERT();
3325 KASSERT(igi->igi_version == IGMP_VERSION_3,
3326 ("%s: called when version %d", __func__, igi->igi_version));
3331 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3332 if (ifma->ifma_addr->sa_family != AF_INET ||
3333 ifma->ifma_protospec == NULL)
3336 inm = (struct in_multi *)ifma->ifma_protospec;
3337 KASSERT(ifp == inm->inm_ifp,
3338 ("%s: inconsistent ifp", __func__));
3340 switch (inm->inm_state) {
3341 case IGMP_NOT_MEMBER:
3342 case IGMP_SILENT_MEMBER:
3344 case IGMP_REPORTING_MEMBER:
3345 case IGMP_IDLE_MEMBER:
3346 case IGMP_LAZY_MEMBER:
3347 case IGMP_SLEEPING_MEMBER:
3348 case IGMP_AWAKENING_MEMBER:
3349 inm->inm_state = IGMP_REPORTING_MEMBER;
3350 retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3352 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3355 case IGMP_G_QUERY_PENDING_MEMBER:
3356 case IGMP_SG_QUERY_PENDING_MEMBER:
3357 case IGMP_LEAVING_MEMBER:
3361 IF_ADDR_RUNLOCK(ifp);
3363 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3364 igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3367 * Slew transmission of bursts over 500ms intervals.
3369 if (igi->igi_gq.ifq_head != NULL) {
3370 igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3371 IGMP_RESPONSE_BURST_INTERVAL);
3372 V_interface_timers_running = 1;
3377 * Transmit the next pending IGMP message in the output queue.
3379 * We get called from netisr_processqueue(). A mutex private to igmpoq
3380 * will be acquired and released around this routine.
3382 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3383 * MRT: Nothing needs to be done, as IGMP traffic is always local to
3384 * a link and uses a link-scope multicast address.
3387 igmp_intr(struct mbuf *m)
3389 struct ip_moptions imo;
3391 struct mbuf *ipopts, *m0;
3395 CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3398 * Set VNET image pointer from enqueued mbuf chain
3399 * before doing anything else. Whilst we use interface
3400 * indexes to guard against interface detach, they are
3401 * unique to each VIMAGE and must be retrieved.
3403 CURVNET_SET((struct vnet *)(m->m_pkthdr.header));
3404 ifindex = igmp_restore_context(m);
3407 * Check if the ifnet still exists. This limits the scope of
3408 * any race in the absence of a global ifp lock for low cost
3409 * (an array lookup).
3411 ifp = ifnet_byindex(ifindex);
3413 CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3414 __func__, m, ifindex);
3416 IPSTAT_INC(ips_noroute);
3420 ipopts = V_igmp_sendra ? m_raopt : NULL;
3422 imo.imo_multicast_ttl = 1;
3423 imo.imo_multicast_vif = -1;
3424 imo.imo_multicast_loop = (V_ip_mrouter != NULL);
3427 * If the user requested that IGMP traffic be explicitly
3428 * redirected to the loopback interface (e.g. they are running a
3429 * MANET interface and the routing protocol needs to see the
3430 * updates), handle this now.
3432 if (m->m_flags & M_IGMP_LOOP)
3433 imo.imo_multicast_ifp = V_loif;
3435 imo.imo_multicast_ifp = ifp;
3437 if (m->m_flags & M_IGMPV2) {
3440 m0 = igmp_v3_encap_report(ifp, m);
3442 CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3444 IPSTAT_INC(ips_odropped);
3449 igmp_scrub_context(m0);
3450 m->m_flags &= ~(M_PROTOFLAGS);
3451 m0->m_pkthdr.rcvif = V_loif;
3453 mac_netinet_igmp_send(ifp, m0);
3455 error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3457 CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3461 IGMPSTAT_INC(igps_snd_reports);
3465 * We must restore the existing vnet pointer before
3466 * continuing as we are run from netisr context.
3472 * Encapsulate an IGMPv3 report.
3474 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3475 * chain has already had its IP/IGMPv3 header prepended. In this case
3476 * the function will not attempt to prepend; the lengths and checksums
3477 * will however be re-computed.
3479 * Returns a pointer to the new mbuf chain head, or NULL if the
3480 * allocation failed.
3482 static struct mbuf *
3483 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3485 struct igmp_report *igmp;
3487 int hdrlen, igmpreclen;
3489 KASSERT((m->m_flags & M_PKTHDR),
3490 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3492 igmpreclen = m_length(m, NULL);
3493 hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3495 if (m->m_flags & M_IGMPV3_HDR) {
3496 igmpreclen -= hdrlen;
3498 M_PREPEND(m, hdrlen, M_DONTWAIT);
3501 m->m_flags |= M_IGMPV3_HDR;
3504 CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3506 m->m_data += sizeof(struct ip);
3507 m->m_len -= sizeof(struct ip);
3509 igmp = mtod(m, struct igmp_report *);
3510 igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3513 igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3515 igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3516 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3518 m->m_data -= sizeof(struct ip);
3519 m->m_len += sizeof(struct ip);
3521 ip = mtod(m, struct ip *);
3522 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3523 ip->ip_len = hdrlen + igmpreclen;
3525 ip->ip_p = IPPROTO_IGMP;
3528 ip->ip_src.s_addr = INADDR_ANY;
3530 if (m->m_flags & M_IGMP_LOOP) {
3531 struct in_ifaddr *ia;
3535 ip->ip_src = ia->ia_addr.sin_addr;
3536 ifa_free(&ia->ia_ifa);
3540 ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3547 igmp_rec_type_to_str(const int type)
3551 case IGMP_CHANGE_TO_EXCLUDE_MODE:
3554 case IGMP_CHANGE_TO_INCLUDE_MODE:
3557 case IGMP_MODE_IS_EXCLUDE:
3560 case IGMP_MODE_IS_INCLUDE:
3563 case IGMP_ALLOW_NEW_SOURCES:
3566 case IGMP_BLOCK_OLD_SOURCES:
3577 igmp_init(void *unused __unused)
3580 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3584 m_raopt = igmp_ra_alloc();
3586 netisr_register(&igmp_nh);
3588 SYSINIT(igmp_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_init, NULL);
3591 igmp_uninit(void *unused __unused)
3594 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3596 netisr_unregister(&igmp_nh);
3601 IGMP_LOCK_DESTROY();
3603 SYSUNINIT(igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_uninit, NULL);
3606 vnet_igmp_init(const void *unused __unused)
3609 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3611 LIST_INIT(&V_igi_head);
3613 VNET_SYSINIT(vnet_igmp_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_igmp_init,
3617 vnet_igmp_uninit(const void *unused __unused)
3620 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3622 KASSERT(LIST_EMPTY(&V_igi_head),
3623 ("%s: igi list not empty; ifnets not detached?", __func__));
3625 VNET_SYSUNINIT(vnet_igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
3626 vnet_igmp_uninit, NULL);
3629 igmp_modevent(module_t mod, int type, void *unused __unused)
3637 return (EOPNOTSUPP);
3642 static moduledata_t igmp_mod = {
3647 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);