2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007-2009 Bruce Simpson.
5 * Copyright (c) 1988 Stephen Deering.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Stephen Deering of Stanford University.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
40 * Internet Group Management Protocol (IGMP) routines.
41 * [RFC1112, RFC2236, RFC3376]
43 * Written by Steve Deering, Stanford, May 1988.
44 * Modified by Rosen Sharma, Stanford, Aug 1994.
45 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
46 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
47 * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson.
49 * MULTICAST Revision: 3.5.1.4
52 #include <sys/cdefs.h>
53 __FBSDID("$FreeBSD$");
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/module.h>
60 #include <sys/malloc.h>
62 #include <sys/socket.h>
63 #include <sys/protosw.h>
64 #include <sys/kernel.h>
66 #include <sys/sysctl.h>
68 #include <sys/condvar.h>
75 #include <net/if_var.h>
76 #include <net/netisr.h>
79 #include <netinet/in.h>
80 #include <netinet/in_var.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_var.h>
84 #include <netinet/ip_options.h>
85 #include <netinet/igmp.h>
86 #include <netinet/igmp_var.h>
88 #include <machine/in_cksum.h>
90 #include <security/mac/mac_framework.h>
93 #define KTR_IGMPV3 KTR_INET
96 static struct igmp_ifsoftc *
97 igi_alloc_locked(struct ifnet *);
98 static void igi_delete_locked(const struct ifnet *);
99 static void igmp_dispatch_queue(struct mbufq *, int, const int);
100 static void igmp_fasttimo_vnet(void);
101 static void igmp_final_leave(struct in_multi *, struct igmp_ifsoftc *);
102 static int igmp_handle_state_change(struct in_multi *,
103 struct igmp_ifsoftc *);
104 static int igmp_initial_join(struct in_multi *, struct igmp_ifsoftc *);
105 static int igmp_input_v1_query(struct ifnet *, const struct ip *,
106 const struct igmp *);
107 static int igmp_input_v2_query(struct ifnet *, const struct ip *,
108 const struct igmp *);
109 static int igmp_input_v3_query(struct ifnet *, const struct ip *,
110 /*const*/ struct igmpv3 *);
111 static int igmp_input_v3_group_query(struct in_multi *,
112 struct igmp_ifsoftc *, int, /*const*/ struct igmpv3 *);
113 static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
114 /*const*/ struct igmp *);
115 static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
116 /*const*/ struct igmp *);
117 static void igmp_intr(struct mbuf *);
118 static int igmp_isgroupreported(const struct in_addr);
122 static char * igmp_rec_type_to_str(const int);
124 static void igmp_set_version(struct igmp_ifsoftc *, const int);
125 static void igmp_slowtimo_vnet(void);
126 static int igmp_v1v2_queue_report(struct in_multi *, const int);
127 static void igmp_v1v2_process_group_timer(struct in_multi *, const int);
128 static void igmp_v1v2_process_querier_timers(struct igmp_ifsoftc *);
129 static void igmp_v2_update_group(struct in_multi *, const int);
130 static void igmp_v3_cancel_link_timers(struct igmp_ifsoftc *);
131 static void igmp_v3_dispatch_general_query(struct igmp_ifsoftc *);
133 igmp_v3_encap_report(struct ifnet *, struct mbuf *);
134 static int igmp_v3_enqueue_group_record(struct mbufq *,
135 struct in_multi *, const int, const int, const int);
136 static int igmp_v3_enqueue_filter_change(struct mbufq *,
138 static void igmp_v3_process_group_timers(struct in_multi_head *,
139 struct mbufq *, struct mbufq *, struct in_multi *,
141 static int igmp_v3_merge_state_changes(struct in_multi *,
143 static void igmp_v3_suppress_group_record(struct in_multi *);
144 static int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
145 static int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
146 static int sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
147 static int sysctl_igmp_stat(SYSCTL_HANDLER_ARGS);
149 static const struct netisr_handler igmp_nh = {
151 .nh_handler = igmp_intr,
152 .nh_proto = NETISR_IGMP,
153 .nh_policy = NETISR_POLICY_SOURCE,
157 * System-wide globals.
159 * Unlocked access to these is OK, except for the global IGMP output
160 * queue. The IGMP subsystem lock ends up being system-wide for the moment,
161 * because all VIMAGEs have to share a global output queue, as netisrs
162 * themselves are not virtualized.
165 * * The permitted lock order is: IN_MULTI_LIST_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
166 * Any may be taken independently; if any are held at the same
167 * time, the above lock order must be followed.
168 * * All output is delegated to the netisr.
169 * Now that Giant has been eliminated, the netisr may be inlined.
170 * * IN_MULTI_LIST_LOCK covers in_multi.
171 * * IGMP_LOCK covers igmp_ifsoftc and any global variables in this file,
172 * including the output queue.
173 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
174 * per-link state iterators.
175 * * igmp_ifsoftc is valid as long as PF_INET is attached to the interface,
176 * therefore it is not refcounted.
177 * We allow unlocked reads of igmp_ifsoftc when accessed via in_multi.
180 * * IGMP acquires its own reference every time an in_multi is passed to
181 * it and the group is being joined for the first time.
182 * * IGMP releases its reference(s) on in_multi in a deferred way,
183 * because the operations which process the release run as part of
184 * a loop whose control variables are directly affected by the release
185 * (that, and not recursing on the IF_ADDR_LOCK).
187 * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
188 * to a vnet in ifp->if_vnet.
190 * SMPng: XXX We may potentially race operations on ifma_protospec.
191 * The problem is that we currently lack a clean way of taking the
192 * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
193 * as anything which modifies ifma needs to be covered by that lock.
194 * So check for ifma_protospec being NULL before proceeding.
198 struct mbuf *m_raopt; /* Router Alert option */
199 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
202 * VIMAGE-wide globals.
204 * The IGMPv3 timers themselves need to run per-image, however,
205 * protosw timers run globally (see tcp).
206 * An ifnet can only be in one vimage at a time, and the loopback
207 * ifnet, loif, is itself virtualized.
208 * It would otherwise be possible to seriously hose IGMP state,
209 * and create inconsistencies in upstream multicast routing, if you have
210 * multiple VIMAGEs running on the same link joining different multicast
211 * groups, UNLESS the "primary IP address" is different. This is because
212 * IGMP for IPv4 does not force link-local addresses to be used for each
213 * node, unlike MLD for IPv6.
214 * Obviously the IGMPv3 per-interface state has per-vimage granularity
217 * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
218 * policy to control the address used by IGMP on the link.
220 VNET_DEFINE_STATIC(int, interface_timers_running); /* IGMPv3 general
222 VNET_DEFINE_STATIC(int, state_change_timers_running); /* IGMPv3 state-change
224 VNET_DEFINE_STATIC(int, current_state_timers_running); /* IGMPv1/v2 host
225 * report; IGMPv3 g/sg
228 #define V_interface_timers_running VNET(interface_timers_running)
229 #define V_state_change_timers_running VNET(state_change_timers_running)
230 #define V_current_state_timers_running VNET(current_state_timers_running)
232 VNET_PCPUSTAT_DEFINE(struct igmpstat, igmpstat);
233 VNET_PCPUSTAT_SYSINIT(igmpstat);
234 VNET_PCPUSTAT_SYSUNINIT(igmpstat);
236 VNET_DEFINE_STATIC(LIST_HEAD(, igmp_ifsoftc), igi_head) =
237 LIST_HEAD_INITIALIZER(igi_head);
238 VNET_DEFINE_STATIC(struct timeval, igmp_gsrdelay) = {10, 0};
240 #define V_igi_head VNET(igi_head)
241 #define V_igmp_gsrdelay VNET(igmp_gsrdelay)
243 VNET_DEFINE_STATIC(int, igmp_recvifkludge) = 1;
244 VNET_DEFINE_STATIC(int, igmp_sendra) = 1;
245 VNET_DEFINE_STATIC(int, igmp_sendlocal) = 1;
246 VNET_DEFINE_STATIC(int, igmp_v1enable) = 1;
247 VNET_DEFINE_STATIC(int, igmp_v2enable) = 1;
248 VNET_DEFINE_STATIC(int, igmp_legacysupp);
249 VNET_DEFINE_STATIC(int, igmp_default_version) = IGMP_VERSION_3;
251 #define V_igmp_recvifkludge VNET(igmp_recvifkludge)
252 #define V_igmp_sendra VNET(igmp_sendra)
253 #define V_igmp_sendlocal VNET(igmp_sendlocal)
254 #define V_igmp_v1enable VNET(igmp_v1enable)
255 #define V_igmp_v2enable VNET(igmp_v2enable)
256 #define V_igmp_legacysupp VNET(igmp_legacysupp)
257 #define V_igmp_default_version VNET(igmp_default_version)
260 * Virtualized sysctls.
262 SYSCTL_PROC(_net_inet_igmp, IGMPCTL_STATS, stats,
263 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_MPSAFE,
264 &VNET_NAME(igmpstat), 0, sysctl_igmp_stat, "S,igmpstat",
265 "IGMP statistics (struct igmpstat, netinet/igmp_var.h)");
266 SYSCTL_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_VNET | CTLFLAG_RW,
267 &VNET_NAME(igmp_recvifkludge), 0,
268 "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
269 SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_VNET | CTLFLAG_RW,
270 &VNET_NAME(igmp_sendra), 0,
271 "Send IP Router Alert option in IGMPv2/v3 messages");
272 SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_VNET | CTLFLAG_RW,
273 &VNET_NAME(igmp_sendlocal), 0,
274 "Send IGMP membership reports for 224.0.0.0/24 groups");
275 SYSCTL_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_VNET | CTLFLAG_RW,
276 &VNET_NAME(igmp_v1enable), 0,
277 "Enable backwards compatibility with IGMPv1");
278 SYSCTL_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_VNET | CTLFLAG_RW,
279 &VNET_NAME(igmp_v2enable), 0,
280 "Enable backwards compatibility with IGMPv2");
281 SYSCTL_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_VNET | CTLFLAG_RW,
282 &VNET_NAME(igmp_legacysupp), 0,
283 "Allow v1/v2 reports to suppress v3 group responses");
284 SYSCTL_PROC(_net_inet_igmp, OID_AUTO, default_version,
285 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
286 &VNET_NAME(igmp_default_version), 0, sysctl_igmp_default_version, "I",
287 "Default version of IGMP to run on each interface");
288 SYSCTL_PROC(_net_inet_igmp, OID_AUTO, gsrdelay,
289 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
290 &VNET_NAME(igmp_gsrdelay.tv_sec), 0, sysctl_igmp_gsr, "I",
291 "Rate limit for IGMPv3 Group-and-Source queries in seconds");
294 * Non-virtualized sysctls.
296 static SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo,
297 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_igmp_ifinfo,
298 "Per-interface IGMPv3 state");
301 igmp_save_context(struct mbuf *m, struct ifnet *ifp)
305 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
307 m->m_pkthdr.rcvif = ifp;
308 m->m_pkthdr.flowid = ifp->if_index;
312 igmp_scrub_context(struct mbuf *m)
315 m->m_pkthdr.PH_loc.ptr = NULL;
316 m->m_pkthdr.flowid = 0;
320 * Restore context from a queued IGMP output chain.
321 * Return saved ifindex.
323 * VIMAGE: The assertion is there to make sure that we
324 * actually called CURVNET_SET() with what's in the mbuf chain.
326 static __inline uint32_t
327 igmp_restore_context(struct mbuf *m)
331 #if defined(VIMAGE) && defined(INVARIANTS)
332 KASSERT(curvnet == (m->m_pkthdr.PH_loc.ptr),
333 ("%s: called when curvnet was not restored", __func__));
336 return (m->m_pkthdr.flowid);
343 sysctl_igmp_stat(SYSCTL_HANDLER_ARGS)
345 struct igmpstat igps0;
349 error = sysctl_wire_old_buffer(req, sizeof(struct igmpstat));
353 if (req->oldptr != NULL) {
354 if (req->oldlen < sizeof(struct igmpstat))
358 * Copy the counters, and explicitly set the struct's
359 * version and length fields.
361 COUNTER_ARRAY_COPY(VNET(igmpstat), &igps0,
362 sizeof(struct igmpstat) / sizeof(uint64_t));
363 igps0.igps_version = IGPS_VERSION_3;
364 igps0.igps_len = IGPS_VERSION3_LEN;
365 error = SYSCTL_OUT(req, &igps0,
366 sizeof(struct igmpstat));
369 req->validlen = sizeof(struct igmpstat);
372 if (req->newptr != NULL) {
373 if (req->newlen < sizeof(struct igmpstat))
376 error = SYSCTL_IN(req, &igps0,
381 * igps0 must be "all zero".
384 while (p < (char *)&igps0 + sizeof(igps0) && *p == '\0')
386 if (p != (char *)&igps0 + sizeof(igps0)) {
390 COUNTER_ARRAY_ZERO(VNET(igmpstat),
391 sizeof(struct igmpstat) / sizeof(uint64_t));
398 * Retrieve or set default IGMP version.
400 * VIMAGE: Assume curvnet set by caller.
401 * SMPng: NOTE: Serialized by IGMP lock.
404 sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
409 error = sysctl_wire_old_buffer(req, sizeof(int));
415 new = V_igmp_default_version;
417 error = sysctl_handle_int(oidp, &new, 0, req);
418 if (error || !req->newptr)
421 if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
426 CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
427 V_igmp_default_version, new);
429 V_igmp_default_version = new;
437 * Retrieve or set threshold between group-source queries in seconds.
439 * VIMAGE: Assume curvnet set by caller.
440 * SMPng: NOTE: Serialized by IGMP lock.
443 sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
448 error = sysctl_wire_old_buffer(req, sizeof(int));
454 i = V_igmp_gsrdelay.tv_sec;
456 error = sysctl_handle_int(oidp, &i, 0, req);
457 if (error || !req->newptr)
460 if (i < -1 || i >= 60) {
465 CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
466 V_igmp_gsrdelay.tv_sec, i);
467 V_igmp_gsrdelay.tv_sec = i;
475 * Expose struct igmp_ifsoftc to userland, keyed by ifindex.
476 * For use by ifmcstat(8).
478 * SMPng: NOTE: Does an unlocked ifindex space read.
479 * VIMAGE: Assume curvnet set by caller. The node handler itself
480 * is not directly virtualized.
483 sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
485 struct epoch_tracker et;
490 struct igmp_ifsoftc *igi;
495 if (req->newptr != NULL)
501 error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
505 IN_MULTI_LIST_LOCK();
511 ifp = ifnet_byindex(name[0]);
516 LIST_FOREACH(igi, &V_igi_head, igi_link) {
517 if (ifp == igi->igi_ifp) {
518 struct igmp_ifinfo info;
520 info.igi_version = igi->igi_version;
521 info.igi_v1_timer = igi->igi_v1_timer;
522 info.igi_v2_timer = igi->igi_v2_timer;
523 info.igi_v3_timer = igi->igi_v3_timer;
524 info.igi_flags = igi->igi_flags;
525 info.igi_rv = igi->igi_rv;
526 info.igi_qi = igi->igi_qi;
527 info.igi_qri = igi->igi_qri;
528 info.igi_uri = igi->igi_uri;
529 error = SYSCTL_OUT(req, &info, sizeof(info));
536 IN_MULTI_LIST_UNLOCK();
541 * Dispatch an entire queue of pending packet chains
543 * VIMAGE: Assumes the vnet pointer has been set.
546 igmp_dispatch_queue(struct mbufq *mq, int limit, const int loop)
548 struct epoch_tracker et;
552 while ((m = mbufq_dequeue(mq)) != NULL) {
553 CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, mq, m);
555 m->m_flags |= M_IGMP_LOOP;
556 netisr_dispatch(NETISR_IGMP, m);
564 * Filter outgoing IGMP report state by group.
566 * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
567 * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
568 * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
569 * this may break certain IGMP snooping switches which rely on the old
572 * Return zero if the given group is one for which IGMP reports
573 * should be suppressed, or non-zero if reports should be issued.
576 igmp_isgroupreported(const struct in_addr addr)
579 if (in_allhosts(addr) ||
580 ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
587 * Construct a Router Alert option to use in outgoing packets.
595 m = m_get(M_WAITOK, MT_DATA);
596 p = mtod(m, struct ipoption *);
597 p->ipopt_dst.s_addr = INADDR_ANY;
598 p->ipopt_list[0] = (char)IPOPT_RA; /* Router Alert Option */
599 p->ipopt_list[1] = 0x04; /* 4 bytes long */
600 p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */
601 p->ipopt_list[3] = 0x00; /* pad byte */
602 m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
608 * Attach IGMP when PF_INET is attached to an interface.
610 struct igmp_ifsoftc *
611 igmp_domifattach(struct ifnet *ifp)
613 struct igmp_ifsoftc *igi;
615 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
616 __func__, ifp, ifp->if_xname);
620 igi = igi_alloc_locked(ifp);
621 if (!(ifp->if_flags & IFF_MULTICAST))
622 igi->igi_flags |= IGIF_SILENT;
630 * VIMAGE: assume curvnet set by caller.
632 static struct igmp_ifsoftc *
633 igi_alloc_locked(/*const*/ struct ifnet *ifp)
635 struct igmp_ifsoftc *igi;
639 igi = malloc(sizeof(struct igmp_ifsoftc), M_IGMP, M_NOWAIT|M_ZERO);
644 igi->igi_version = V_igmp_default_version;
646 igi->igi_rv = IGMP_RV_INIT;
647 igi->igi_qi = IGMP_QI_INIT;
648 igi->igi_qri = IGMP_QRI_INIT;
649 igi->igi_uri = IGMP_URI_INIT;
650 mbufq_init(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
652 LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
654 CTR2(KTR_IGMPV3, "allocate igmp_ifsoftc for ifp %p(%s)",
664 * NOTE: Some finalization tasks need to run before the protocol domain
665 * is detached, but also before the link layer does its cleanup.
667 * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
668 * XXX This is also bitten by unlocked ifma_protospec access.
671 igmp_ifdetach(struct ifnet *ifp)
673 struct igmp_ifsoftc *igi;
674 struct ifmultiaddr *ifma, *next;
675 struct in_multi *inm;
676 struct in_multi_head inm_free_tmp;
677 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
680 SLIST_INIT(&inm_free_tmp);
683 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
684 if (igi->igi_version == IGMP_VERSION_3) {
687 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
688 if (ifma->ifma_addr->sa_family != AF_INET ||
689 ifma->ifma_protospec == NULL)
691 inm = (struct in_multi *)ifma->ifma_protospec;
692 if (inm->inm_state == IGMP_LEAVING_MEMBER)
693 inm_rele_locked(&inm_free_tmp, inm);
694 inm_clear_recorded(inm);
695 if (__predict_false(ifma_restart)) {
696 ifma_restart = false;
700 IF_ADDR_WUNLOCK(ifp);
701 inm_release_list_deferred(&inm_free_tmp);
708 * Hook for domifdetach.
711 igmp_domifdetach(struct ifnet *ifp)
714 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
715 __func__, ifp, ifp->if_xname);
718 igi_delete_locked(ifp);
723 igi_delete_locked(const struct ifnet *ifp)
725 struct igmp_ifsoftc *igi, *tigi;
727 CTR3(KTR_IGMPV3, "%s: freeing igmp_ifsoftc for ifp %p(%s)",
728 __func__, ifp, ifp->if_xname);
732 LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
733 if (igi->igi_ifp == ifp) {
735 * Free deferred General Query responses.
737 mbufq_drain(&igi->igi_gq);
739 LIST_REMOVE(igi, igi_link);
747 * Process a received IGMPv1 query.
748 * Return non-zero if the message should be dropped.
750 * VIMAGE: The curvnet pointer is derived from the input ifp.
753 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
754 const struct igmp *igmp)
756 struct ifmultiaddr *ifma;
757 struct igmp_ifsoftc *igi;
758 struct in_multi *inm;
763 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
764 * 224.0.0.1. They are always treated as General Queries.
765 * igmp_group is always ignored. Do not drop it as a userland
766 * daemon may wish to see it.
767 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
769 if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
770 IGMPSTAT_INC(igps_rcv_badqueries);
773 IGMPSTAT_INC(igps_rcv_gen_queries);
775 IN_MULTI_LIST_LOCK();
778 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
779 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
781 if (igi->igi_flags & IGIF_LOOPBACK) {
782 CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
788 * Switch to IGMPv1 host compatibility mode.
790 igmp_set_version(igi, IGMP_VERSION_1);
792 CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
795 * Start the timers in all of our group records
796 * for the interface on which the query arrived,
797 * except those which are already running.
799 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
800 if (ifma->ifma_addr->sa_family != AF_INET ||
801 ifma->ifma_protospec == NULL)
803 inm = (struct in_multi *)ifma->ifma_protospec;
804 if (inm->inm_timer != 0)
806 switch (inm->inm_state) {
807 case IGMP_NOT_MEMBER:
808 case IGMP_SILENT_MEMBER:
810 case IGMP_G_QUERY_PENDING_MEMBER:
811 case IGMP_SG_QUERY_PENDING_MEMBER:
812 case IGMP_REPORTING_MEMBER:
813 case IGMP_IDLE_MEMBER:
814 case IGMP_LAZY_MEMBER:
815 case IGMP_SLEEPING_MEMBER:
816 case IGMP_AWAKENING_MEMBER:
817 inm->inm_state = IGMP_REPORTING_MEMBER;
818 inm->inm_timer = IGMP_RANDOM_DELAY(
819 IGMP_V1V2_MAX_RI * PR_FASTHZ);
820 V_current_state_timers_running = 1;
822 case IGMP_LEAVING_MEMBER:
829 IN_MULTI_LIST_UNLOCK();
835 * Process a received IGMPv2 general or group-specific query.
838 igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
839 const struct igmp *igmp)
841 struct ifmultiaddr *ifma;
842 struct igmp_ifsoftc *igi;
843 struct in_multi *inm;
844 int is_general_query;
849 is_general_query = 0;
852 * Validate address fields upfront.
853 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
855 if (in_nullhost(igmp->igmp_group)) {
857 * IGMPv2 General Query.
858 * If this was not sent to the all-hosts group, ignore it.
860 if (!in_allhosts(ip->ip_dst))
862 IGMPSTAT_INC(igps_rcv_gen_queries);
863 is_general_query = 1;
865 /* IGMPv2 Group-Specific Query. */
866 IGMPSTAT_INC(igps_rcv_group_queries);
869 IN_MULTI_LIST_LOCK();
872 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
873 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
875 if (igi->igi_flags & IGIF_LOOPBACK) {
876 CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
882 * Ignore v2 query if in v1 Compatibility Mode.
884 if (igi->igi_version == IGMP_VERSION_1)
887 igmp_set_version(igi, IGMP_VERSION_2);
889 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
893 if (is_general_query) {
895 * For each reporting group joined on this
896 * interface, kick the report timer.
898 CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
900 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
901 if (ifma->ifma_addr->sa_family != AF_INET ||
902 ifma->ifma_protospec == NULL)
904 inm = (struct in_multi *)ifma->ifma_protospec;
905 igmp_v2_update_group(inm, timer);
909 * Group-specific IGMPv2 query, we need only
910 * look up the single group to process it.
912 inm = inm_lookup(ifp, igmp->igmp_group);
915 "process v2 query 0x%08x on ifp %p(%s)",
916 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
917 igmp_v2_update_group(inm, timer);
923 IN_MULTI_LIST_UNLOCK();
929 * Update the report timer on a group in response to an IGMPv2 query.
931 * If we are becoming the reporting member for this group, start the timer.
932 * If we already are the reporting member for this group, and timer is
933 * below the threshold, reset it.
935 * We may be updating the group for the first time since we switched
936 * to IGMPv3. If we are, then we must clear any recorded source lists,
937 * and transition to REPORTING state; the group timer is overloaded
938 * for group and group-source query responses.
940 * Unlike IGMPv3, the delay per group should be jittered
941 * to avoid bursts of IGMPv2 reports.
944 igmp_v2_update_group(struct in_multi *inm, const int timer)
947 CTR4(KTR_IGMPV3, "0x%08x: %s/%s timer=%d", __func__,
948 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname, timer);
950 IN_MULTI_LIST_LOCK_ASSERT();
952 switch (inm->inm_state) {
953 case IGMP_NOT_MEMBER:
954 case IGMP_SILENT_MEMBER:
956 case IGMP_REPORTING_MEMBER:
957 if (inm->inm_timer != 0 &&
958 inm->inm_timer <= timer) {
959 CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
960 "skipping.", __func__);
964 case IGMP_SG_QUERY_PENDING_MEMBER:
965 case IGMP_G_QUERY_PENDING_MEMBER:
966 case IGMP_IDLE_MEMBER:
967 case IGMP_LAZY_MEMBER:
968 case IGMP_AWAKENING_MEMBER:
969 CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
970 inm->inm_state = IGMP_REPORTING_MEMBER;
971 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
972 V_current_state_timers_running = 1;
974 case IGMP_SLEEPING_MEMBER:
975 CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
976 inm->inm_state = IGMP_AWAKENING_MEMBER;
978 case IGMP_LEAVING_MEMBER:
984 * Process a received IGMPv3 general, group-specific or
985 * group-and-source-specific query.
986 * Assumes m has already been pulled up to the full IGMP message length.
987 * Return 0 if successful, otherwise an appropriate error code is returned.
990 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
991 /*const*/ struct igmpv3 *igmpv3)
993 struct igmp_ifsoftc *igi;
994 struct in_multi *inm;
995 int is_general_query;
996 uint32_t maxresp, nsrc, qqi;
1000 is_general_query = 0;
1002 CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
1004 maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */
1005 if (maxresp >= 128) {
1006 maxresp = IGMP_MANT(igmpv3->igmp_code) <<
1007 (IGMP_EXP(igmpv3->igmp_code) + 3);
1011 * Robustness must never be less than 2 for on-wire IGMPv3.
1012 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
1013 * an exception for interfaces whose IGMPv3 state changes
1014 * are redirected to loopback (e.g. MANET).
1016 qrv = IGMP_QRV(igmpv3->igmp_misc);
1018 CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
1023 qqi = igmpv3->igmp_qqi;
1025 qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
1026 (IGMP_EXP(igmpv3->igmp_qqi) + 3);
1029 timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
1033 nsrc = ntohs(igmpv3->igmp_numsrc);
1036 * Validate address fields and versions upfront before
1037 * accepting v3 query.
1038 * XXX SMPng: Unlocked access to igmpstat counters here.
1040 if (in_nullhost(igmpv3->igmp_group)) {
1042 * IGMPv3 General Query.
1044 * General Queries SHOULD be directed to 224.0.0.1.
1045 * A general query with a source list has undefined
1046 * behaviour; discard it.
1048 IGMPSTAT_INC(igps_rcv_gen_queries);
1049 if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
1050 IGMPSTAT_INC(igps_rcv_badqueries);
1053 is_general_query = 1;
1055 /* Group or group-source specific query. */
1057 IGMPSTAT_INC(igps_rcv_group_queries);
1059 IGMPSTAT_INC(igps_rcv_gsr_queries);
1062 IN_MULTI_LIST_LOCK();
1065 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1066 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
1068 if (igi->igi_flags & IGIF_LOOPBACK) {
1069 CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1070 ifp, ifp->if_xname);
1075 * Discard the v3 query if we're in Compatibility Mode.
1076 * The RFC is not obviously worded that hosts need to stay in
1077 * compatibility mode until the Old Version Querier Present
1080 if (igi->igi_version != IGMP_VERSION_3) {
1081 CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1082 igi->igi_version, ifp, ifp->if_xname);
1086 igmp_set_version(igi, IGMP_VERSION_3);
1089 igi->igi_qri = maxresp;
1091 CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1094 if (is_general_query) {
1096 * Schedule a current-state report on this ifp for
1097 * all groups, possibly containing source lists.
1098 * If there is a pending General Query response
1099 * scheduled earlier than the selected delay, do
1100 * not schedule any other reports.
1101 * Otherwise, reset the interface timer.
1103 CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1104 ifp, ifp->if_xname);
1105 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1106 igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1107 V_interface_timers_running = 1;
1111 * Group-source-specific queries are throttled on
1112 * a per-group basis to defeat denial-of-service attempts.
1113 * Queries for groups we are not a member of on this
1114 * link are simply ignored.
1116 inm = inm_lookup(ifp, igmpv3->igmp_group);
1120 if (!ratecheck(&inm->inm_lastgsrtv,
1121 &V_igmp_gsrdelay)) {
1122 CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1124 IGMPSTAT_INC(igps_drop_gsr_queries);
1128 CTR3(KTR_IGMPV3, "process v3 0x%08x query on ifp %p(%s)",
1129 ntohl(igmpv3->igmp_group.s_addr), ifp, ifp->if_xname);
1131 * If there is a pending General Query response
1132 * scheduled sooner than the selected delay, no
1133 * further report need be scheduled.
1134 * Otherwise, prepare to respond to the
1135 * group-specific or group-and-source query.
1137 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1138 igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1143 IN_MULTI_LIST_UNLOCK();
1149 * Process a received IGMPv3 group-specific or group-and-source-specific
1151 * Return <0 if any error occurred. Currently this is ignored.
1154 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifsoftc *igi,
1155 int timer, /*const*/ struct igmpv3 *igmpv3)
1160 IN_MULTI_LIST_LOCK_ASSERT();
1165 switch (inm->inm_state) {
1166 case IGMP_NOT_MEMBER:
1167 case IGMP_SILENT_MEMBER:
1168 case IGMP_SLEEPING_MEMBER:
1169 case IGMP_LAZY_MEMBER:
1170 case IGMP_AWAKENING_MEMBER:
1171 case IGMP_IDLE_MEMBER:
1172 case IGMP_LEAVING_MEMBER:
1175 case IGMP_REPORTING_MEMBER:
1176 case IGMP_G_QUERY_PENDING_MEMBER:
1177 case IGMP_SG_QUERY_PENDING_MEMBER:
1181 nsrc = ntohs(igmpv3->igmp_numsrc);
1184 * Deal with group-specific queries upfront.
1185 * If any group query is already pending, purge any recorded
1186 * source-list state if it exists, and schedule a query response
1187 * for this group-specific query.
1190 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1191 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1192 inm_clear_recorded(inm);
1193 timer = min(inm->inm_timer, timer);
1195 inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1196 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1197 V_current_state_timers_running = 1;
1202 * Deal with the case where a group-and-source-specific query has
1203 * been received but a group-specific query is already pending.
1205 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1206 timer = min(inm->inm_timer, timer);
1207 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1208 V_current_state_timers_running = 1;
1213 * Finally, deal with the case where a group-and-source-specific
1214 * query has been received, where a response to a previous g-s-r
1215 * query exists, or none exists.
1216 * In this case, we need to parse the source-list which the Querier
1217 * has provided us with and check if we have any source list filter
1218 * entries at T1 for these sources. If we do not, there is no need
1219 * schedule a report and the query may be dropped.
1220 * If we do, we must record them and schedule a current-state
1221 * report for those sources.
1222 * FIXME: Handling source lists larger than 1 mbuf requires that
1223 * we pass the mbuf chain pointer down to this function, and use
1224 * m_getptr() to walk the chain.
1226 if (inm->inm_nsrc > 0) {
1227 const struct in_addr *ap;
1230 ap = (const struct in_addr *)(igmpv3 + 1);
1232 for (i = 0; i < nsrc; i++, ap++) {
1233 retval = inm_record_source(inm, ap->s_addr);
1236 nrecorded += retval;
1238 if (nrecorded > 0) {
1240 "%s: schedule response to SG query", __func__);
1241 inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1242 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1243 V_current_state_timers_running = 1;
1251 * Process a received IGMPv1 host membership report.
1253 * NOTE: 0.0.0.0 workaround breaks const correctness.
1256 igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1257 /*const*/ struct igmp *igmp)
1259 struct in_ifaddr *ia;
1260 struct in_multi *inm;
1262 IGMPSTAT_INC(igps_rcv_reports);
1264 if (ifp->if_flags & IFF_LOOPBACK)
1267 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1268 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1269 IGMPSTAT_INC(igps_rcv_badreports);
1274 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1275 * Booting clients may use the source address 0.0.0.0. Some
1276 * IGMP daemons may not know how to use IP_RECVIF to determine
1277 * the interface upon which this message was received.
1278 * Replace 0.0.0.0 with the subnet address if told to do so.
1280 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1283 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1286 CTR3(KTR_IGMPV3, "process v1 report 0x%08x on ifp %p(%s)",
1287 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1290 * IGMPv1 report suppression.
1291 * If we are a member of this group, and our membership should be
1292 * reported, stop our group timer and transition to the 'lazy' state.
1294 IN_MULTI_LIST_LOCK();
1295 inm = inm_lookup(ifp, igmp->igmp_group);
1297 struct igmp_ifsoftc *igi;
1301 KASSERT(igi != NULL,
1302 ("%s: no igi for ifp %p", __func__, ifp));
1306 IGMPSTAT_INC(igps_rcv_ourreports);
1309 * If we are in IGMPv3 host mode, do not allow the
1310 * other host's IGMPv1 report to suppress our reports
1311 * unless explicitly configured to do so.
1313 if (igi->igi_version == IGMP_VERSION_3) {
1314 if (V_igmp_legacysupp)
1315 igmp_v3_suppress_group_record(inm);
1321 switch (inm->inm_state) {
1322 case IGMP_NOT_MEMBER:
1323 case IGMP_SILENT_MEMBER:
1325 case IGMP_IDLE_MEMBER:
1326 case IGMP_LAZY_MEMBER:
1327 case IGMP_AWAKENING_MEMBER:
1329 "report suppressed for 0x%08x on ifp %p(%s)",
1330 ntohl(igmp->igmp_group.s_addr), ifp,
1332 case IGMP_SLEEPING_MEMBER:
1333 inm->inm_state = IGMP_SLEEPING_MEMBER;
1335 case IGMP_REPORTING_MEMBER:
1337 "report suppressed for 0x%08x on ifp %p(%s)",
1338 ntohl(igmp->igmp_group.s_addr), ifp,
1340 if (igi->igi_version == IGMP_VERSION_1)
1341 inm->inm_state = IGMP_LAZY_MEMBER;
1342 else if (igi->igi_version == IGMP_VERSION_2)
1343 inm->inm_state = IGMP_SLEEPING_MEMBER;
1345 case IGMP_G_QUERY_PENDING_MEMBER:
1346 case IGMP_SG_QUERY_PENDING_MEMBER:
1347 case IGMP_LEAVING_MEMBER:
1353 IN_MULTI_LIST_UNLOCK();
1359 * Process a received IGMPv2 host membership report.
1361 * NOTE: 0.0.0.0 workaround breaks const correctness.
1364 igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1365 /*const*/ struct igmp *igmp)
1367 struct in_ifaddr *ia;
1368 struct in_multi *inm;
1371 * Make sure we don't hear our own membership report. Fast
1372 * leave requires knowing that we are the only member of a
1376 if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1380 IGMPSTAT_INC(igps_rcv_reports);
1382 if (ifp->if_flags & IFF_LOOPBACK) {
1386 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1387 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1388 IGMPSTAT_INC(igps_rcv_badreports);
1393 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1394 * Booting clients may use the source address 0.0.0.0. Some
1395 * IGMP daemons may not know how to use IP_RECVIF to determine
1396 * the interface upon which this message was received.
1397 * Replace 0.0.0.0 with the subnet address if told to do so.
1399 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1401 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1404 CTR3(KTR_IGMPV3, "process v2 report 0x%08x on ifp %p(%s)",
1405 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1408 * IGMPv2 report suppression.
1409 * If we are a member of this group, and our membership should be
1410 * reported, and our group timer is pending or about to be reset,
1411 * stop our group timer by transitioning to the 'lazy' state.
1413 IN_MULTI_LIST_LOCK();
1414 inm = inm_lookup(ifp, igmp->igmp_group);
1416 struct igmp_ifsoftc *igi;
1419 KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1421 IGMPSTAT_INC(igps_rcv_ourreports);
1424 * If we are in IGMPv3 host mode, do not allow the
1425 * other host's IGMPv1 report to suppress our reports
1426 * unless explicitly configured to do so.
1428 if (igi->igi_version == IGMP_VERSION_3) {
1429 if (V_igmp_legacysupp)
1430 igmp_v3_suppress_group_record(inm);
1436 switch (inm->inm_state) {
1437 case IGMP_NOT_MEMBER:
1438 case IGMP_SILENT_MEMBER:
1439 case IGMP_SLEEPING_MEMBER:
1441 case IGMP_REPORTING_MEMBER:
1442 case IGMP_IDLE_MEMBER:
1443 case IGMP_AWAKENING_MEMBER:
1445 "report suppressed for 0x%08x on ifp %p(%s)",
1446 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1447 case IGMP_LAZY_MEMBER:
1448 inm->inm_state = IGMP_LAZY_MEMBER;
1450 case IGMP_G_QUERY_PENDING_MEMBER:
1451 case IGMP_SG_QUERY_PENDING_MEMBER:
1452 case IGMP_LEAVING_MEMBER:
1458 IN_MULTI_LIST_UNLOCK();
1464 igmp_input(struct mbuf **mp, int *offp, int proto)
1475 CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, *mp, *offp);
1478 ifp = m->m_pkthdr.rcvif;
1481 IGMPSTAT_INC(igps_rcv_total);
1483 ip = mtod(m, struct ip *);
1485 igmplen = ntohs(ip->ip_len) - iphlen;
1490 if (igmplen < IGMP_MINLEN) {
1491 IGMPSTAT_INC(igps_rcv_tooshort);
1493 return (IPPROTO_DONE);
1497 * Always pullup to the minimum size for v1/v2 or v3
1498 * to amortize calls to m_pullup().
1501 if (igmplen >= IGMP_V3_QUERY_MINLEN)
1502 minlen += IGMP_V3_QUERY_MINLEN;
1504 minlen += IGMP_MINLEN;
1505 if ((!M_WRITABLE(m) || m->m_len < minlen) &&
1506 (m = m_pullup(m, minlen)) == NULL) {
1507 IGMPSTAT_INC(igps_rcv_tooshort);
1508 return (IPPROTO_DONE);
1510 ip = mtod(m, struct ip *);
1513 * Validate checksum.
1515 m->m_data += iphlen;
1517 igmp = mtod(m, struct igmp *);
1518 if (in_cksum(m, igmplen)) {
1519 IGMPSTAT_INC(igps_rcv_badsum);
1521 return (IPPROTO_DONE);
1523 m->m_data -= iphlen;
1527 * IGMP control traffic is link-scope, and must have a TTL of 1.
1528 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1529 * probe packets may come from beyond the LAN.
1531 if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) {
1532 IGMPSTAT_INC(igps_rcv_badttl);
1534 return (IPPROTO_DONE);
1537 switch (igmp->igmp_type) {
1538 case IGMP_HOST_MEMBERSHIP_QUERY:
1539 if (igmplen == IGMP_MINLEN) {
1540 if (igmp->igmp_code == 0)
1541 queryver = IGMP_VERSION_1;
1543 queryver = IGMP_VERSION_2;
1544 } else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1545 queryver = IGMP_VERSION_3;
1547 IGMPSTAT_INC(igps_rcv_tooshort);
1549 return (IPPROTO_DONE);
1553 case IGMP_VERSION_1:
1554 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1555 if (!V_igmp_v1enable)
1557 if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1559 return (IPPROTO_DONE);
1563 case IGMP_VERSION_2:
1564 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1565 if (!V_igmp_v2enable)
1567 if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
1569 return (IPPROTO_DONE);
1573 case IGMP_VERSION_3: {
1574 struct igmpv3 *igmpv3;
1578 IGMPSTAT_INC(igps_rcv_v3_queries);
1579 igmpv3 = (struct igmpv3 *)igmp;
1581 * Validate length based on source count.
1583 nsrc = ntohs(igmpv3->igmp_numsrc);
1584 if (nsrc * sizeof(in_addr_t) >
1585 UINT16_MAX - iphlen - IGMP_V3_QUERY_MINLEN) {
1586 IGMPSTAT_INC(igps_rcv_tooshort);
1588 return (IPPROTO_DONE);
1591 * m_pullup() may modify m, so pullup in
1594 igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1595 sizeof(struct in_addr) * nsrc;
1596 if ((!M_WRITABLE(m) ||
1597 m->m_len < igmpv3len) &&
1598 (m = m_pullup(m, igmpv3len)) == NULL) {
1599 IGMPSTAT_INC(igps_rcv_tooshort);
1600 return (IPPROTO_DONE);
1602 igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1604 if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1606 return (IPPROTO_DONE);
1613 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1614 if (!V_igmp_v1enable)
1616 if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1618 return (IPPROTO_DONE);
1622 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1623 if (!V_igmp_v2enable)
1625 if (!ip_checkrouteralert(m))
1626 IGMPSTAT_INC(igps_rcv_nora);
1627 if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
1629 return (IPPROTO_DONE);
1633 case IGMP_v3_HOST_MEMBERSHIP_REPORT:
1635 * Hosts do not need to process IGMPv3 membership reports,
1636 * as report suppression is no longer required.
1638 if (!ip_checkrouteralert(m))
1639 IGMPSTAT_INC(igps_rcv_nora);
1647 * Pass all valid IGMP packets up to any process(es) listening on a
1651 return (rip_input(mp, offp, proto));
1655 * Fast timeout handler (global).
1656 * VIMAGE: Timeout handlers are expected to service all vimages.
1661 VNET_ITERATOR_DECL(vnet_iter);
1663 VNET_LIST_RLOCK_NOSLEEP();
1664 VNET_FOREACH(vnet_iter) {
1665 CURVNET_SET(vnet_iter);
1666 igmp_fasttimo_vnet();
1669 VNET_LIST_RUNLOCK_NOSLEEP();
1673 * Fast timeout handler (per-vnet).
1674 * Sends are shuffled off to a netisr to deal with Giant.
1676 * VIMAGE: Assume caller has set up our curvnet.
1679 igmp_fasttimo_vnet(void)
1681 struct mbufq scq; /* State-change packets */
1682 struct mbufq qrq; /* Query response packets */
1684 struct igmp_ifsoftc *igi;
1685 struct ifmultiaddr *ifma, *next;
1686 struct in_multi *inm;
1687 struct in_multi_head inm_free_tmp;
1688 int loop, uri_fasthz;
1694 * Quick check to see if any work needs to be done, in order to
1695 * minimize the overhead of fasttimo processing.
1696 * SMPng: XXX Unlocked reads.
1698 if (!V_current_state_timers_running &&
1699 !V_interface_timers_running &&
1700 !V_state_change_timers_running)
1703 SLIST_INIT(&inm_free_tmp);
1704 IN_MULTI_LIST_LOCK();
1708 * IGMPv3 General Query response timer processing.
1710 if (V_interface_timers_running) {
1711 CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1713 V_interface_timers_running = 0;
1714 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1715 if (igi->igi_v3_timer == 0) {
1717 } else if (--igi->igi_v3_timer == 0) {
1718 igmp_v3_dispatch_general_query(igi);
1720 V_interface_timers_running = 1;
1725 if (!V_current_state_timers_running &&
1726 !V_state_change_timers_running)
1729 V_current_state_timers_running = 0;
1730 V_state_change_timers_running = 0;
1732 CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1735 * IGMPv1/v2/v3 host report and state-change timer processing.
1736 * Note: Processing a v3 group timer may remove a node.
1738 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1741 if (igi->igi_version == IGMP_VERSION_3) {
1742 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1743 uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1745 mbufq_init(&qrq, IGMP_MAX_G_GS_PACKETS);
1746 mbufq_init(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1751 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1752 if (ifma->ifma_addr->sa_family != AF_INET ||
1753 ifma->ifma_protospec == NULL)
1755 inm = (struct in_multi *)ifma->ifma_protospec;
1756 switch (igi->igi_version) {
1757 case IGMP_VERSION_1:
1758 case IGMP_VERSION_2:
1759 igmp_v1v2_process_group_timer(inm,
1762 case IGMP_VERSION_3:
1763 igmp_v3_process_group_timers(&inm_free_tmp, &qrq,
1764 &scq, inm, uri_fasthz);
1767 if (__predict_false(ifma_restart)) {
1768 ifma_restart = false;
1772 IF_ADDR_WUNLOCK(ifp);
1774 if (igi->igi_version == IGMP_VERSION_3) {
1775 igmp_dispatch_queue(&qrq, 0, loop);
1776 igmp_dispatch_queue(&scq, 0, loop);
1779 * Free the in_multi reference(s) for this
1782 inm_release_list_deferred(&inm_free_tmp);
1788 IN_MULTI_LIST_UNLOCK();
1792 * Update host report group timer for IGMPv1/v2.
1793 * Will update the global pending timer flags.
1796 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1798 int report_timer_expired;
1800 IN_MULTI_LIST_LOCK_ASSERT();
1803 if (inm->inm_timer == 0) {
1804 report_timer_expired = 0;
1805 } else if (--inm->inm_timer == 0) {
1806 report_timer_expired = 1;
1808 V_current_state_timers_running = 1;
1812 switch (inm->inm_state) {
1813 case IGMP_NOT_MEMBER:
1814 case IGMP_SILENT_MEMBER:
1815 case IGMP_IDLE_MEMBER:
1816 case IGMP_LAZY_MEMBER:
1817 case IGMP_SLEEPING_MEMBER:
1818 case IGMP_AWAKENING_MEMBER:
1820 case IGMP_REPORTING_MEMBER:
1821 if (report_timer_expired) {
1822 inm->inm_state = IGMP_IDLE_MEMBER;
1823 (void)igmp_v1v2_queue_report(inm,
1824 (version == IGMP_VERSION_2) ?
1825 IGMP_v2_HOST_MEMBERSHIP_REPORT :
1826 IGMP_v1_HOST_MEMBERSHIP_REPORT);
1829 case IGMP_G_QUERY_PENDING_MEMBER:
1830 case IGMP_SG_QUERY_PENDING_MEMBER:
1831 case IGMP_LEAVING_MEMBER:
1837 * Update a group's timers for IGMPv3.
1838 * Will update the global pending timer flags.
1839 * Note: Unlocked read from igi.
1842 igmp_v3_process_group_timers(struct in_multi_head *inmh,
1843 struct mbufq *qrq, struct mbufq *scq,
1844 struct in_multi *inm, const int uri_fasthz)
1846 int query_response_timer_expired;
1847 int state_change_retransmit_timer_expired;
1849 IN_MULTI_LIST_LOCK_ASSERT();
1852 query_response_timer_expired = 0;
1853 state_change_retransmit_timer_expired = 0;
1856 * During a transition from v1/v2 compatibility mode back to v3,
1857 * a group record in REPORTING state may still have its group
1858 * timer active. This is a no-op in this function; it is easier
1859 * to deal with it here than to complicate the slow-timeout path.
1861 if (inm->inm_timer == 0) {
1862 query_response_timer_expired = 0;
1863 } else if (--inm->inm_timer == 0) {
1864 query_response_timer_expired = 1;
1866 V_current_state_timers_running = 1;
1869 if (inm->inm_sctimer == 0) {
1870 state_change_retransmit_timer_expired = 0;
1871 } else if (--inm->inm_sctimer == 0) {
1872 state_change_retransmit_timer_expired = 1;
1874 V_state_change_timers_running = 1;
1877 /* We are in fasttimo, so be quick about it. */
1878 if (!state_change_retransmit_timer_expired &&
1879 !query_response_timer_expired)
1882 switch (inm->inm_state) {
1883 case IGMP_NOT_MEMBER:
1884 case IGMP_SILENT_MEMBER:
1885 case IGMP_SLEEPING_MEMBER:
1886 case IGMP_LAZY_MEMBER:
1887 case IGMP_AWAKENING_MEMBER:
1888 case IGMP_IDLE_MEMBER:
1890 case IGMP_G_QUERY_PENDING_MEMBER:
1891 case IGMP_SG_QUERY_PENDING_MEMBER:
1893 * Respond to a previously pending Group-Specific
1894 * or Group-and-Source-Specific query by enqueueing
1895 * the appropriate Current-State report for
1896 * immediate transmission.
1898 if (query_response_timer_expired) {
1899 int retval __unused;
1901 retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1902 (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1903 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1905 inm->inm_state = IGMP_REPORTING_MEMBER;
1906 /* XXX Clear recorded sources for next time. */
1907 inm_clear_recorded(inm);
1910 case IGMP_REPORTING_MEMBER:
1911 case IGMP_LEAVING_MEMBER:
1912 if (state_change_retransmit_timer_expired) {
1914 * State-change retransmission timer fired.
1915 * If there are any further pending retransmissions,
1916 * set the global pending state-change flag, and
1919 if (--inm->inm_scrv > 0) {
1920 inm->inm_sctimer = uri_fasthz;
1921 V_state_change_timers_running = 1;
1924 * Retransmit the previously computed state-change
1925 * report. If there are no further pending
1926 * retransmissions, the mbuf queue will be consumed.
1927 * Update T0 state to T1 as we have now sent
1930 (void)igmp_v3_merge_state_changes(inm, scq);
1933 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
1934 ntohl(inm->inm_addr.s_addr),
1935 inm->inm_ifp->if_xname);
1938 * If we are leaving the group for good, make sure
1939 * we release IGMP's reference to it.
1940 * This release must be deferred using a SLIST,
1941 * as we are called from a loop which traverses
1942 * the in_ifmultiaddr TAILQ.
1944 if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1945 inm->inm_scrv == 0) {
1946 inm->inm_state = IGMP_NOT_MEMBER;
1947 inm_rele_locked(inmh, inm);
1955 * Suppress a group's pending response to a group or source/group query.
1957 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1958 * Do NOT update ST1/ST0 as this operation merely suppresses
1959 * the currently pending group record.
1960 * Do NOT suppress the response to a general query. It is possible but
1961 * it would require adding another state or flag.
1964 igmp_v3_suppress_group_record(struct in_multi *inm)
1967 IN_MULTI_LIST_LOCK_ASSERT();
1969 KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1970 ("%s: not IGMPv3 mode on link", __func__));
1972 if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1973 inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1976 if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1977 inm_clear_recorded(inm);
1980 inm->inm_state = IGMP_REPORTING_MEMBER;
1984 * Switch to a different IGMP version on the given interface,
1985 * as per Section 7.2.1.
1988 igmp_set_version(struct igmp_ifsoftc *igi, const int version)
1990 int old_version_timer;
1994 CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1995 version, igi->igi_ifp, igi->igi_ifp->if_xname);
1997 if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1999 * Compute the "Older Version Querier Present" timer as per
2002 old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
2003 old_version_timer *= PR_SLOWHZ;
2005 if (version == IGMP_VERSION_1) {
2006 igi->igi_v1_timer = old_version_timer;
2007 igi->igi_v2_timer = 0;
2008 } else if (version == IGMP_VERSION_2) {
2009 igi->igi_v1_timer = 0;
2010 igi->igi_v2_timer = old_version_timer;
2014 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2015 if (igi->igi_version != IGMP_VERSION_2) {
2016 igi->igi_version = IGMP_VERSION_2;
2017 igmp_v3_cancel_link_timers(igi);
2019 } else if (igi->igi_v1_timer > 0) {
2020 if (igi->igi_version != IGMP_VERSION_1) {
2021 igi->igi_version = IGMP_VERSION_1;
2022 igmp_v3_cancel_link_timers(igi);
2028 * Cancel pending IGMPv3 timers for the given link and all groups
2029 * joined on it; state-change, general-query, and group-query timers.
2031 * Only ever called on a transition from v3 to Compatibility mode. Kill
2032 * the timers stone dead (this may be expensive for large N groups), they
2033 * will be restarted if Compatibility Mode deems that they must be due to
2037 igmp_v3_cancel_link_timers(struct igmp_ifsoftc *igi)
2039 struct ifmultiaddr *ifma, *ifmatmp;
2041 struct in_multi *inm;
2042 struct in_multi_head inm_free_tmp;
2044 CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2045 igi->igi_ifp, igi->igi_ifp->if_xname);
2047 IN_MULTI_LIST_LOCK_ASSERT();
2051 SLIST_INIT(&inm_free_tmp);
2054 * Stop the v3 General Query Response on this link stone dead.
2055 * If fasttimo is woken up due to V_interface_timers_running,
2056 * the flag will be cleared if there are no pending link timers.
2058 igi->igi_v3_timer = 0;
2061 * Now clear the current-state and state-change report timers
2062 * for all memberships scoped to this link.
2066 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, ifmatmp) {
2067 if (ifma->ifma_addr->sa_family != AF_INET ||
2068 ifma->ifma_protospec == NULL)
2070 inm = (struct in_multi *)ifma->ifma_protospec;
2071 switch (inm->inm_state) {
2072 case IGMP_NOT_MEMBER:
2073 case IGMP_SILENT_MEMBER:
2074 case IGMP_IDLE_MEMBER:
2075 case IGMP_LAZY_MEMBER:
2076 case IGMP_SLEEPING_MEMBER:
2077 case IGMP_AWAKENING_MEMBER:
2079 * These states are either not relevant in v3 mode,
2080 * or are unreported. Do nothing.
2083 case IGMP_LEAVING_MEMBER:
2085 * If we are leaving the group and switching to
2086 * compatibility mode, we need to release the final
2087 * reference held for issuing the INCLUDE {}, and
2088 * transition to REPORTING to ensure the host leave
2089 * message is sent upstream to the old querier --
2090 * transition to NOT would lose the leave and race.
2092 inm_rele_locked(&inm_free_tmp, inm);
2094 case IGMP_G_QUERY_PENDING_MEMBER:
2095 case IGMP_SG_QUERY_PENDING_MEMBER:
2096 inm_clear_recorded(inm);
2098 case IGMP_REPORTING_MEMBER:
2099 inm->inm_state = IGMP_REPORTING_MEMBER;
2103 * Always clear state-change and group report timers.
2104 * Free any pending IGMPv3 state-change records.
2106 inm->inm_sctimer = 0;
2108 mbufq_drain(&inm->inm_scq);
2110 IF_ADDR_WUNLOCK(ifp);
2112 inm_release_list_deferred(&inm_free_tmp);
2116 * Update the Older Version Querier Present timers for a link.
2117 * See Section 7.2.1 of RFC 3376.
2120 igmp_v1v2_process_querier_timers(struct igmp_ifsoftc *igi)
2125 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2127 * IGMPv1 and IGMPv2 Querier Present timers expired.
2131 if (igi->igi_version != IGMP_VERSION_3) {
2133 "%s: transition from v%d -> v%d on %p(%s)",
2134 __func__, igi->igi_version, IGMP_VERSION_3,
2135 igi->igi_ifp, igi->igi_ifp->if_xname);
2136 igi->igi_version = IGMP_VERSION_3;
2138 } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2140 * IGMPv1 Querier Present timer expired,
2141 * IGMPv2 Querier Present timer running.
2142 * If IGMPv2 was disabled since last timeout,
2144 * If IGMPv2 is enabled, revert to IGMPv2.
2146 if (!V_igmp_v2enable) {
2148 "%s: transition from v%d -> v%d on %p(%s)",
2149 __func__, igi->igi_version, IGMP_VERSION_3,
2150 igi->igi_ifp, igi->igi_ifp->if_xname);
2151 igi->igi_v2_timer = 0;
2152 igi->igi_version = IGMP_VERSION_3;
2154 --igi->igi_v2_timer;
2155 if (igi->igi_version != IGMP_VERSION_2) {
2157 "%s: transition from v%d -> v%d on %p(%s)",
2158 __func__, igi->igi_version, IGMP_VERSION_2,
2159 igi->igi_ifp, igi->igi_ifp->if_xname);
2160 igi->igi_version = IGMP_VERSION_2;
2161 igmp_v3_cancel_link_timers(igi);
2164 } else if (igi->igi_v1_timer > 0) {
2166 * IGMPv1 Querier Present timer running.
2167 * Stop IGMPv2 timer if running.
2169 * If IGMPv1 was disabled since last timeout,
2171 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2173 if (!V_igmp_v1enable) {
2175 "%s: transition from v%d -> v%d on %p(%s)",
2176 __func__, igi->igi_version, IGMP_VERSION_3,
2177 igi->igi_ifp, igi->igi_ifp->if_xname);
2178 igi->igi_v1_timer = 0;
2179 igi->igi_version = IGMP_VERSION_3;
2181 --igi->igi_v1_timer;
2183 if (igi->igi_v2_timer > 0) {
2185 "%s: cancel v2 timer on %p(%s)",
2186 __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2187 igi->igi_v2_timer = 0;
2193 * Global slowtimo handler.
2194 * VIMAGE: Timeout handlers are expected to service all vimages.
2199 VNET_ITERATOR_DECL(vnet_iter);
2201 VNET_LIST_RLOCK_NOSLEEP();
2202 VNET_FOREACH(vnet_iter) {
2203 CURVNET_SET(vnet_iter);
2204 igmp_slowtimo_vnet();
2207 VNET_LIST_RUNLOCK_NOSLEEP();
2211 * Per-vnet slowtimo handler.
2214 igmp_slowtimo_vnet(void)
2216 struct igmp_ifsoftc *igi;
2220 LIST_FOREACH(igi, &V_igi_head, igi_link) {
2221 igmp_v1v2_process_querier_timers(igi);
2228 * Dispatch an IGMPv1/v2 host report or leave message.
2229 * These are always small enough to fit inside a single mbuf.
2232 igmp_v1v2_queue_report(struct in_multi *inm, const int type)
2234 struct epoch_tracker et;
2240 IN_MULTI_LIST_LOCK_ASSERT();
2245 m = m_gethdr(M_NOWAIT, MT_DATA);
2248 M_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
2250 m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2252 m->m_data += sizeof(struct ip);
2253 m->m_len = sizeof(struct igmp);
2255 igmp = mtod(m, struct igmp *);
2256 igmp->igmp_type = type;
2257 igmp->igmp_code = 0;
2258 igmp->igmp_group = inm->inm_addr;
2259 igmp->igmp_cksum = 0;
2260 igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
2262 m->m_data -= sizeof(struct ip);
2263 m->m_len += sizeof(struct ip);
2265 ip = mtod(m, struct ip *);
2267 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct igmp));
2269 ip->ip_p = IPPROTO_IGMP;
2270 ip->ip_src.s_addr = INADDR_ANY;
2272 if (type == IGMP_HOST_LEAVE_MESSAGE)
2273 ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2275 ip->ip_dst = inm->inm_addr;
2277 igmp_save_context(m, ifp);
2279 m->m_flags |= M_IGMPV2;
2280 if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2281 m->m_flags |= M_IGMP_LOOP;
2283 CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2284 NET_EPOCH_ENTER(et);
2285 netisr_dispatch(NETISR_IGMP, m);
2292 * Process a state change from the upper layer for the given IPv4 group.
2294 * Each socket holds a reference on the in_multi in its own ip_moptions.
2295 * The socket layer will have made the necessary updates to.the group
2296 * state, it is now up to IGMP to issue a state change report if there
2297 * has been any change between T0 (when the last state-change was issued)
2300 * We use the IGMPv3 state machine at group level. The IGMP module
2301 * however makes the decision as to which IGMP protocol version to speak.
2302 * A state change *from* INCLUDE {} always means an initial join.
2303 * A state change *to* INCLUDE {} always means a final leave.
2305 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2306 * save ourselves a bunch of work; any exclusive mode groups need not
2307 * compute source filter lists.
2309 * VIMAGE: curvnet should have been set by caller, as this routine
2310 * is called from the socket option handlers.
2313 igmp_change_state(struct in_multi *inm)
2315 struct igmp_ifsoftc *igi;
2320 IN_MULTI_LOCK_ASSERT();
2322 * Try to detect if the upper layer just asked us to change state
2323 * for an interface which has now gone away.
2325 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2326 ifp = inm->inm_ifma->ifma_ifp;
2328 * Sanity check that netinet's notion of ifp is the
2331 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2335 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2336 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
2339 * If we detect a state transition to or from MCAST_UNDEFINED
2340 * for this group, then we are starting or finishing an IGMP
2341 * life cycle for this group.
2343 if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2344 CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2345 inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2346 if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2347 CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2348 error = igmp_initial_join(inm, igi);
2350 } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2351 CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2352 igmp_final_leave(inm, igi);
2356 CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2359 error = igmp_handle_state_change(inm, igi);
2367 * Perform the initial join for an IGMP group.
2369 * When joining a group:
2370 * If the group should have its IGMP traffic suppressed, do nothing.
2371 * IGMPv1 starts sending IGMPv1 host membership reports.
2372 * IGMPv2 starts sending IGMPv2 host membership reports.
2373 * IGMPv3 will schedule an IGMPv3 state-change report containing the
2374 * initial state of the membership.
2377 igmp_initial_join(struct in_multi *inm, struct igmp_ifsoftc *igi)
2381 int error, retval, syncstates;
2383 CTR4(KTR_IGMPV3, "%s: initial join 0x%08x on ifp %p(%s)", __func__,
2384 ntohl(inm->inm_addr.s_addr), inm->inm_ifp, inm->inm_ifp->if_xname);
2391 IN_MULTI_LOCK_ASSERT();
2394 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2397 * Groups joined on loopback or marked as 'not reported',
2398 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2399 * are never reported in any IGMP protocol exchanges.
2400 * All other groups enter the appropriate IGMP state machine
2401 * for the version in use on this link.
2402 * A link marked as IGIF_SILENT causes IGMP to be completely
2403 * disabled for the link.
2405 if ((ifp->if_flags & IFF_LOOPBACK) ||
2406 (igi->igi_flags & IGIF_SILENT) ||
2407 !igmp_isgroupreported(inm->inm_addr)) {
2409 "%s: not kicking state machine for silent group", __func__);
2410 inm->inm_state = IGMP_SILENT_MEMBER;
2414 * Deal with overlapping in_multi lifecycle.
2415 * If this group was LEAVING, then make sure
2416 * we drop the reference we picked up to keep the
2417 * group around for the final INCLUDE {} enqueue.
2419 if (igi->igi_version == IGMP_VERSION_3 &&
2420 inm->inm_state == IGMP_LEAVING_MEMBER) {
2421 MPASS(inm->inm_refcount > 1);
2422 inm_rele_locked(NULL, inm);
2424 inm->inm_state = IGMP_REPORTING_MEMBER;
2426 switch (igi->igi_version) {
2427 case IGMP_VERSION_1:
2428 case IGMP_VERSION_2:
2429 inm->inm_state = IGMP_IDLE_MEMBER;
2430 error = igmp_v1v2_queue_report(inm,
2431 (igi->igi_version == IGMP_VERSION_2) ?
2432 IGMP_v2_HOST_MEMBERSHIP_REPORT :
2433 IGMP_v1_HOST_MEMBERSHIP_REPORT);
2435 inm->inm_timer = IGMP_RANDOM_DELAY(
2436 IGMP_V1V2_MAX_RI * PR_FASTHZ);
2437 V_current_state_timers_running = 1;
2441 case IGMP_VERSION_3:
2443 * Defer update of T0 to T1, until the first copy
2444 * of the state change has been transmitted.
2449 * Immediately enqueue a State-Change Report for
2450 * this interface, freeing any previous reports.
2451 * Don't kick the timers if there is nothing to do,
2452 * or if an error occurred.
2456 retval = igmp_v3_enqueue_group_record(mq, inm, 1,
2458 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2461 error = retval * -1;
2466 * Schedule transmission of pending state-change
2467 * report up to RV times for this link. The timer
2468 * will fire at the next igmp_fasttimo (~200ms),
2469 * giving us an opportunity to merge the reports.
2471 if (igi->igi_flags & IGIF_LOOPBACK) {
2474 KASSERT(igi->igi_rv > 1,
2475 ("%s: invalid robustness %d", __func__,
2477 inm->inm_scrv = igi->igi_rv;
2479 inm->inm_sctimer = 1;
2480 V_state_change_timers_running = 1;
2488 * Only update the T0 state if state change is atomic,
2489 * i.e. we don't need to wait for a timer to fire before we
2490 * can consider the state change to have been communicated.
2494 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2495 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2502 * Issue an intermediate state change during the IGMP life-cycle.
2505 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifsoftc *igi)
2510 CTR4(KTR_IGMPV3, "%s: state change for 0x%08x on ifp %p(%s)", __func__,
2511 ntohl(inm->inm_addr.s_addr), inm->inm_ifp, inm->inm_ifp->if_xname);
2515 IN_MULTI_LIST_LOCK_ASSERT();
2518 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2520 if ((ifp->if_flags & IFF_LOOPBACK) ||
2521 (igi->igi_flags & IGIF_SILENT) ||
2522 !igmp_isgroupreported(inm->inm_addr) ||
2523 (igi->igi_version != IGMP_VERSION_3)) {
2524 if (!igmp_isgroupreported(inm->inm_addr)) {
2526 "%s: not kicking state machine for silent group", __func__);
2528 CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2530 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2531 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2535 mbufq_drain(&inm->inm_scq);
2537 retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2538 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2543 * If record(s) were enqueued, start the state-change
2544 * report timer for this group.
2546 inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2547 inm->inm_sctimer = 1;
2548 V_state_change_timers_running = 1;
2554 * Perform the final leave for an IGMP group.
2556 * When leaving a group:
2557 * IGMPv1 does nothing.
2558 * IGMPv2 sends a host leave message, if and only if we are the reporter.
2559 * IGMPv3 enqueues a state-change report containing a transition
2560 * to INCLUDE {} for immediate transmission.
2563 igmp_final_leave(struct in_multi *inm, struct igmp_ifsoftc *igi)
2569 CTR4(KTR_IGMPV3, "%s: final leave 0x%08x on ifp %p(%s)",
2570 __func__, ntohl(inm->inm_addr.s_addr), inm->inm_ifp,
2571 inm->inm_ifp->if_xname);
2573 IN_MULTI_LIST_LOCK_ASSERT();
2576 switch (inm->inm_state) {
2577 case IGMP_NOT_MEMBER:
2578 case IGMP_SILENT_MEMBER:
2579 case IGMP_LEAVING_MEMBER:
2580 /* Already leaving or left; do nothing. */
2582 "%s: not kicking state machine for silent group", __func__);
2584 case IGMP_REPORTING_MEMBER:
2585 case IGMP_IDLE_MEMBER:
2586 case IGMP_G_QUERY_PENDING_MEMBER:
2587 case IGMP_SG_QUERY_PENDING_MEMBER:
2588 if (igi->igi_version == IGMP_VERSION_2) {
2590 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2591 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2592 panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2595 igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2596 inm->inm_state = IGMP_NOT_MEMBER;
2597 } else if (igi->igi_version == IGMP_VERSION_3) {
2599 * Stop group timer and all pending reports.
2600 * Immediately enqueue a state-change report
2601 * TO_IN {} to be sent on the next fast timeout,
2602 * giving us an opportunity to merge reports.
2604 mbufq_drain(&inm->inm_scq);
2606 if (igi->igi_flags & IGIF_LOOPBACK) {
2609 inm->inm_scrv = igi->igi_rv;
2611 CTR4(KTR_IGMPV3, "%s: Leaving 0x%08x/%s with %d "
2612 "pending retransmissions.", __func__,
2613 ntohl(inm->inm_addr.s_addr),
2614 inm->inm_ifp->if_xname, inm->inm_scrv);
2615 if (inm->inm_scrv == 0) {
2616 inm->inm_state = IGMP_NOT_MEMBER;
2617 inm->inm_sctimer = 0;
2619 int retval __unused;
2621 inm_acquire_locked(inm);
2623 retval = igmp_v3_enqueue_group_record(
2624 &inm->inm_scq, inm, 1, 0, 0);
2625 KASSERT(retval != 0,
2626 ("%s: enqueue record = %d", __func__,
2629 inm->inm_state = IGMP_LEAVING_MEMBER;
2630 inm->inm_sctimer = 1;
2631 V_state_change_timers_running = 1;
2637 case IGMP_LAZY_MEMBER:
2638 case IGMP_SLEEPING_MEMBER:
2639 case IGMP_AWAKENING_MEMBER:
2640 /* Our reports are suppressed; do nothing. */
2646 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2647 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2648 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2649 CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for 0x%08x/%s",
2650 __func__, ntohl(inm->inm_addr.s_addr),
2651 inm->inm_ifp->if_xname);
2656 * Enqueue an IGMPv3 group record to the given output queue.
2658 * XXX This function could do with having the allocation code
2659 * split out, and the multiple-tree-walks coalesced into a single
2660 * routine as has been done in igmp_v3_enqueue_filter_change().
2662 * If is_state_change is zero, a current-state record is appended.
2663 * If is_state_change is non-zero, a state-change report is appended.
2665 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2666 * If is_group_query is zero, and if there is a packet with free space
2667 * at the tail of the queue, it will be appended to providing there
2668 * is enough free space.
2669 * Otherwise a new mbuf packet chain is allocated.
2671 * If is_source_query is non-zero, each source is checked to see if
2672 * it was recorded for a Group-Source query, and will be omitted if
2673 * it is not both in-mode and recorded.
2675 * The function will attempt to allocate leading space in the packet
2676 * for the IP/IGMP header to be prepended without fragmenting the chain.
2678 * If successful the size of all data appended to the queue is returned,
2679 * otherwise an error code less than zero is returned, or zero if
2680 * no record(s) were appended.
2683 igmp_v3_enqueue_group_record(struct mbufq *mq, struct in_multi *inm,
2684 const int is_state_change, const int is_group_query,
2685 const int is_source_query)
2687 struct igmp_grouprec ig;
2688 struct igmp_grouprec *pig;
2690 struct ip_msource *ims, *nims;
2691 struct mbuf *m0, *m, *md;
2692 int is_filter_list_change;
2693 int minrec0len, m0srcs, msrcs, nbytes, off;
2694 int record_has_sources;
2700 IN_MULTI_LIST_LOCK_ASSERT();
2703 is_filter_list_change = 0;
2710 record_has_sources = 1;
2712 type = IGMP_DO_NOTHING;
2713 mode = inm->inm_st[1].iss_fmode;
2716 * If we did not transition out of ASM mode during t0->t1,
2717 * and there are no source nodes to process, we can skip
2718 * the generation of source records.
2720 if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2722 record_has_sources = 0;
2724 if (is_state_change) {
2726 * Queue a state change record.
2727 * If the mode did not change, and there are non-ASM
2728 * listeners or source filters present,
2729 * we potentially need to issue two records for the group.
2730 * If we are transitioning to MCAST_UNDEFINED, we need
2731 * not send any sources.
2732 * If there are ASM listeners, and there was no filter
2733 * mode transition of any kind, do nothing.
2735 if (mode != inm->inm_st[0].iss_fmode) {
2736 if (mode == MCAST_EXCLUDE) {
2737 CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2739 type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2741 CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2743 type = IGMP_CHANGE_TO_INCLUDE_MODE;
2744 if (mode == MCAST_UNDEFINED)
2745 record_has_sources = 0;
2748 if (record_has_sources) {
2749 is_filter_list_change = 1;
2751 type = IGMP_DO_NOTHING;
2756 * Queue a current state record.
2758 if (mode == MCAST_EXCLUDE) {
2759 type = IGMP_MODE_IS_EXCLUDE;
2760 } else if (mode == MCAST_INCLUDE) {
2761 type = IGMP_MODE_IS_INCLUDE;
2762 KASSERT(inm->inm_st[1].iss_asm == 0,
2763 ("%s: inm %p is INCLUDE but ASM count is %d",
2764 __func__, inm, inm->inm_st[1].iss_asm));
2769 * Generate the filter list changes using a separate function.
2771 if (is_filter_list_change)
2772 return (igmp_v3_enqueue_filter_change(mq, inm));
2774 if (type == IGMP_DO_NOTHING) {
2775 CTR3(KTR_IGMPV3, "%s: nothing to do for 0x%08x/%s", __func__,
2776 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2781 * If any sources are present, we must be able to fit at least
2782 * one in the trailing space of the tail packet's mbuf,
2785 minrec0len = sizeof(struct igmp_grouprec);
2786 if (record_has_sources)
2787 minrec0len += sizeof(in_addr_t);
2789 CTR4(KTR_IGMPV3, "%s: queueing %s for 0x%08x/%s", __func__,
2790 igmp_rec_type_to_str(type), ntohl(inm->inm_addr.s_addr),
2791 inm->inm_ifp->if_xname);
2794 * Check if we have a packet in the tail of the queue for this
2795 * group into which the first group record for this group will fit.
2796 * Otherwise allocate a new packet.
2797 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2798 * Note: Group records for G/GSR query responses MUST be sent
2799 * in their own packet.
2801 m0 = mbufq_last(mq);
2802 if (!is_group_query &&
2804 (m0->m_pkthdr.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2805 (m0->m_pkthdr.len + minrec0len) <
2806 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2807 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2808 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2810 CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2812 if (mbufq_full(mq)) {
2813 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2817 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2818 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2819 if (!is_state_change && !is_group_query) {
2820 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2822 m->m_data += IGMP_LEADINGSPACE;
2825 m = m_gethdr(M_NOWAIT, MT_DATA);
2827 M_ALIGN(m, IGMP_LEADINGSPACE);
2832 igmp_save_context(m, ifp);
2834 CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2838 * Append group record.
2839 * If we have sources, we don't know how many yet.
2844 ig.ig_group = inm->inm_addr;
2845 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2848 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2851 nbytes += sizeof(struct igmp_grouprec);
2854 * Append as many sources as will fit in the first packet.
2855 * If we are appending to a new packet, the chain allocation
2856 * may potentially use clusters; use m_getptr() in this case.
2857 * If we are appending to an existing packet, we need to obtain
2858 * a pointer to the group record after m_append(), in case a new
2859 * mbuf was allocated.
2860 * Only append sources which are in-mode at t1. If we are
2861 * transitioning to MCAST_UNDEFINED state on the group, do not
2862 * include source entries.
2863 * Only report recorded sources in our filter set when responding
2864 * to a group-source query.
2866 if (record_has_sources) {
2869 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2870 md->m_len - nbytes);
2872 md = m_getptr(m, 0, &off);
2873 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2877 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2878 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2880 now = ims_get_mode(inm, ims, 1);
2881 CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2882 if ((now != mode) ||
2883 (now == mode && mode == MCAST_UNDEFINED)) {
2884 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2887 if (is_source_query && ims->ims_stp == 0) {
2888 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2892 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2893 naddr = htonl(ims->ims_haddr);
2894 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2897 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2901 nbytes += sizeof(in_addr_t);
2903 if (msrcs == m0srcs)
2906 CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2908 pig->ig_numsrc = htons(msrcs);
2909 nbytes += (msrcs * sizeof(in_addr_t));
2912 if (is_source_query && msrcs == 0) {
2913 CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2920 * We are good to go with first packet.
2923 CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2924 m->m_pkthdr.vt_nrecs = 1;
2925 mbufq_enqueue(mq, m);
2927 m->m_pkthdr.vt_nrecs++;
2930 * No further work needed if no source list in packet(s).
2932 if (!record_has_sources)
2936 * Whilst sources remain to be announced, we need to allocate
2937 * a new packet and fill out as many sources as will fit.
2938 * Always try for a cluster first.
2940 while (nims != NULL) {
2941 if (mbufq_full(mq)) {
2942 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2945 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2947 m->m_data += IGMP_LEADINGSPACE;
2949 m = m_gethdr(M_NOWAIT, MT_DATA);
2951 M_ALIGN(m, IGMP_LEADINGSPACE);
2955 igmp_save_context(m, ifp);
2956 md = m_getptr(m, 0, &off);
2957 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2958 CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2960 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2963 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2966 m->m_pkthdr.vt_nrecs = 1;
2967 nbytes += sizeof(struct igmp_grouprec);
2969 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2970 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2973 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2974 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2976 now = ims_get_mode(inm, ims, 1);
2977 if ((now != mode) ||
2978 (now == mode && mode == MCAST_UNDEFINED)) {
2979 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2982 if (is_source_query && ims->ims_stp == 0) {
2983 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2987 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2988 naddr = htonl(ims->ims_haddr);
2989 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2992 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2997 if (msrcs == m0srcs)
3000 pig->ig_numsrc = htons(msrcs);
3001 nbytes += (msrcs * sizeof(in_addr_t));
3003 CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
3004 mbufq_enqueue(mq, m);
3011 * Type used to mark record pass completion.
3012 * We exploit the fact we can cast to this easily from the
3013 * current filter modes on each ip_msource node.
3016 REC_NONE = 0x00, /* MCAST_UNDEFINED */
3017 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
3018 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
3019 REC_FULL = REC_ALLOW | REC_BLOCK
3023 * Enqueue an IGMPv3 filter list change to the given output queue.
3025 * Source list filter state is held in an RB-tree. When the filter list
3026 * for a group is changed without changing its mode, we need to compute
3027 * the deltas between T0 and T1 for each source in the filter set,
3028 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
3030 * As we may potentially queue two record types, and the entire R-B tree
3031 * needs to be walked at once, we break this out into its own function
3032 * so we can generate a tightly packed queue of packets.
3034 * XXX This could be written to only use one tree walk, although that makes
3035 * serializing into the mbuf chains a bit harder. For now we do two walks
3036 * which makes things easier on us, and it may or may not be harder on
3039 * If successful the size of all data appended to the queue is returned,
3040 * otherwise an error code less than zero is returned, or zero if
3041 * no record(s) were appended.
3044 igmp_v3_enqueue_filter_change(struct mbufq *mq, struct in_multi *inm)
3046 static const int MINRECLEN =
3047 sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3049 struct igmp_grouprec ig;
3050 struct igmp_grouprec *pig;
3051 struct ip_msource *ims, *nims;
3052 struct mbuf *m, *m0, *md;
3054 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3058 uint8_t mode, now, then;
3059 rectype_t crt, drt, nrt;
3061 IN_MULTI_LIST_LOCK_ASSERT();
3063 if (inm->inm_nsrc == 0 ||
3064 (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3067 ifp = inm->inm_ifp; /* interface */
3068 mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */
3069 crt = REC_NONE; /* current group record type */
3070 drt = REC_NONE; /* mask of completed group record types */
3071 nrt = REC_NONE; /* record type for current node */
3072 m0srcs = 0; /* # source which will fit in current mbuf chain */
3073 nbytes = 0; /* # of bytes appended to group's state-change queue */
3074 npbytes = 0; /* # of bytes appended this packet */
3075 rsrcs = 0; /* # sources encoded in current record */
3076 schanged = 0; /* # nodes encoded in overall filter change */
3078 nallow = 0; /* # of source entries in ALLOW_NEW */
3079 nblock = 0; /* # of source entries in BLOCK_OLD */
3081 nims = NULL; /* next tree node pointer */
3084 * For each possible filter record mode.
3085 * The first kind of source we encounter tells us which
3086 * is the first kind of record we start appending.
3087 * If a node transitioned to UNDEFINED at t1, its mode is treated
3088 * as the inverse of the group's filter mode.
3090 while (drt != REC_FULL) {
3092 m0 = mbufq_last(mq);
3094 (m0->m_pkthdr.vt_nrecs + 1 <=
3095 IGMP_V3_REPORT_MAXRECS) &&
3096 (m0->m_pkthdr.len + MINRECLEN) <
3097 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3099 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3100 sizeof(struct igmp_grouprec)) /
3103 "%s: use previous packet", __func__);
3105 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3107 m->m_data += IGMP_LEADINGSPACE;
3109 m = m_gethdr(M_NOWAIT, MT_DATA);
3111 M_ALIGN(m, IGMP_LEADINGSPACE);
3115 "%s: m_get*() failed", __func__);
3118 m->m_pkthdr.vt_nrecs = 0;
3119 igmp_save_context(m, ifp);
3120 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3121 sizeof(struct igmp_grouprec)) /
3125 "%s: allocated new packet", __func__);
3128 * Append the IGMP group record header to the
3129 * current packet's data area.
3130 * Recalculate pointer to free space for next
3131 * group record, in case m_append() allocated
3132 * a new mbuf or cluster.
3134 memset(&ig, 0, sizeof(ig));
3135 ig.ig_group = inm->inm_addr;
3136 if (!m_append(m, sizeof(ig), (void *)&ig)) {
3140 "%s: m_append() failed", __func__);
3143 npbytes += sizeof(struct igmp_grouprec);
3145 /* new packet; offset in c hain */
3146 md = m_getptr(m, npbytes -
3147 sizeof(struct igmp_grouprec), &off);
3148 pig = (struct igmp_grouprec *)(mtod(md,
3151 /* current packet; offset from last append */
3153 pig = (struct igmp_grouprec *)(mtod(md,
3154 uint8_t *) + md->m_len -
3155 sizeof(struct igmp_grouprec));
3158 * Begin walking the tree for this record type
3159 * pass, or continue from where we left off
3160 * previously if we had to allocate a new packet.
3161 * Only report deltas in-mode at t1.
3162 * We need not report included sources as allowed
3163 * if we are in inclusive mode on the group,
3164 * however the converse is not true.
3168 nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3169 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3170 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x",
3171 __func__, ims->ims_haddr);
3172 now = ims_get_mode(inm, ims, 1);
3173 then = ims_get_mode(inm, ims, 0);
3174 CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3175 __func__, then, now);
3178 "%s: skip unchanged", __func__);
3181 if (mode == MCAST_EXCLUDE &&
3182 now == MCAST_INCLUDE) {
3184 "%s: skip IN src on EX group",
3188 nrt = (rectype_t)now;
3189 if (nrt == REC_NONE)
3190 nrt = (rectype_t)(~mode & REC_FULL);
3191 if (schanged++ == 0) {
3193 } else if (crt != nrt)
3195 naddr = htonl(ims->ims_haddr);
3196 if (!m_append(m, sizeof(in_addr_t),
3201 "%s: m_append() failed", __func__);
3205 nallow += !!(crt == REC_ALLOW);
3206 nblock += !!(crt == REC_BLOCK);
3208 if (++rsrcs == m0srcs)
3212 * If we did not append any tree nodes on this
3213 * pass, back out of allocations.
3216 npbytes -= sizeof(struct igmp_grouprec);
3219 "%s: m_free(m)", __func__);
3223 "%s: m_adj(m, -ig)", __func__);
3224 m_adj(m, -((int)sizeof(
3225 struct igmp_grouprec)));
3229 npbytes += (rsrcs * sizeof(in_addr_t));
3230 if (crt == REC_ALLOW)
3231 pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3232 else if (crt == REC_BLOCK)
3233 pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3234 pig->ig_numsrc = htons(rsrcs);
3236 * Count the new group record, and enqueue this
3237 * packet if it wasn't already queued.
3239 m->m_pkthdr.vt_nrecs++;
3241 mbufq_enqueue(mq, m);
3243 } while (nims != NULL);
3245 crt = (~crt & REC_FULL);
3248 CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3255 igmp_v3_merge_state_changes(struct in_multi *inm, struct mbufq *scq)
3258 struct mbuf *m; /* pending state-change */
3259 struct mbuf *m0; /* copy of pending state-change */
3260 struct mbuf *mt; /* last state-change in packet */
3261 int docopy, domerge;
3268 IN_MULTI_LIST_LOCK_ASSERT();
3272 * If there are further pending retransmissions, make a writable
3273 * copy of each queued state-change message before merging.
3275 if (inm->inm_scrv > 0)
3280 if (mbufq_first(gq) == NULL) {
3281 CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3286 m = mbufq_first(gq);
3289 * Only merge the report into the current packet if
3290 * there is sufficient space to do so; an IGMPv3 report
3291 * packet may only contain 65,535 group records.
3292 * Always use a simple mbuf chain concatentation to do this,
3293 * as large state changes for single groups may have
3294 * allocated clusters.
3297 mt = mbufq_last(scq);
3299 recslen = m_length(m, NULL);
3301 if ((mt->m_pkthdr.vt_nrecs +
3302 m->m_pkthdr.vt_nrecs <=
3303 IGMP_V3_REPORT_MAXRECS) &&
3304 (mt->m_pkthdr.len + recslen <=
3305 (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3309 if (!domerge && mbufq_full(gq)) {
3311 "%s: outbound queue full, skipping whole packet %p",
3321 CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3322 m0 = mbufq_dequeue(gq);
3325 CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3326 m0 = m_dup(m, M_NOWAIT);
3329 m0->m_nextpkt = NULL;
3334 CTR3(KTR_IGMPV3, "%s: queueing %p to scq %p)",
3336 mbufq_enqueue(scq, m0);
3338 struct mbuf *mtl; /* last mbuf of packet mt */
3340 CTR3(KTR_IGMPV3, "%s: merging %p with scq tail %p)",
3344 m0->m_flags &= ~M_PKTHDR;
3345 mt->m_pkthdr.len += recslen;
3346 mt->m_pkthdr.vt_nrecs +=
3347 m0->m_pkthdr.vt_nrecs;
3357 * Respond to a pending IGMPv3 General Query.
3360 igmp_v3_dispatch_general_query(struct igmp_ifsoftc *igi)
3362 struct ifmultiaddr *ifma;
3364 struct in_multi *inm;
3365 int retval __unused, loop;
3367 IN_MULTI_LIST_LOCK_ASSERT();
3371 KASSERT(igi->igi_version == IGMP_VERSION_3,
3372 ("%s: called when version %d", __func__, igi->igi_version));
3375 * Check that there are some packets queued. If so, send them first.
3376 * For large number of groups the reply to general query can take
3377 * many packets, we should finish sending them before starting of
3378 * queuing the new reply.
3380 if (mbufq_len(&igi->igi_gq) != 0)
3385 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3386 if (ifma->ifma_addr->sa_family != AF_INET ||
3387 ifma->ifma_protospec == NULL)
3390 inm = (struct in_multi *)ifma->ifma_protospec;
3391 KASSERT(ifp == inm->inm_ifp,
3392 ("%s: inconsistent ifp", __func__));
3394 switch (inm->inm_state) {
3395 case IGMP_NOT_MEMBER:
3396 case IGMP_SILENT_MEMBER:
3398 case IGMP_REPORTING_MEMBER:
3399 case IGMP_IDLE_MEMBER:
3400 case IGMP_LAZY_MEMBER:
3401 case IGMP_SLEEPING_MEMBER:
3402 case IGMP_AWAKENING_MEMBER:
3403 inm->inm_state = IGMP_REPORTING_MEMBER;
3404 retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3406 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3409 case IGMP_G_QUERY_PENDING_MEMBER:
3410 case IGMP_SG_QUERY_PENDING_MEMBER:
3411 case IGMP_LEAVING_MEMBER:
3417 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3418 igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3421 * Slew transmission of bursts over 500ms intervals.
3423 if (mbufq_first(&igi->igi_gq) != NULL) {
3424 igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3425 IGMP_RESPONSE_BURST_INTERVAL);
3426 V_interface_timers_running = 1;
3431 * Transmit the next pending IGMP message in the output queue.
3433 * We get called from netisr_processqueue(). A mutex private to igmpoq
3434 * will be acquired and released around this routine.
3436 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3437 * MRT: Nothing needs to be done, as IGMP traffic is always local to
3438 * a link and uses a link-scope multicast address.
3441 igmp_intr(struct mbuf *m)
3443 struct ip_moptions imo;
3445 struct mbuf *ipopts, *m0;
3449 CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3452 * Set VNET image pointer from enqueued mbuf chain
3453 * before doing anything else. Whilst we use interface
3454 * indexes to guard against interface detach, they are
3455 * unique to each VIMAGE and must be retrieved.
3457 CURVNET_SET((struct vnet *)(m->m_pkthdr.PH_loc.ptr));
3458 ifindex = igmp_restore_context(m);
3461 * Check if the ifnet still exists. This limits the scope of
3462 * any race in the absence of a global ifp lock for low cost
3463 * (an array lookup).
3465 ifp = ifnet_byindex(ifindex);
3467 CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3468 __func__, m, ifindex);
3470 IPSTAT_INC(ips_noroute);
3474 ipopts = V_igmp_sendra ? m_raopt : NULL;
3476 imo.imo_multicast_ttl = 1;
3477 imo.imo_multicast_vif = -1;
3478 imo.imo_multicast_loop = (V_ip_mrouter != NULL);
3481 * If the user requested that IGMP traffic be explicitly
3482 * redirected to the loopback interface (e.g. they are running a
3483 * MANET interface and the routing protocol needs to see the
3484 * updates), handle this now.
3486 if (m->m_flags & M_IGMP_LOOP)
3487 imo.imo_multicast_ifp = V_loif;
3489 imo.imo_multicast_ifp = ifp;
3491 if (m->m_flags & M_IGMPV2) {
3494 m0 = igmp_v3_encap_report(ifp, m);
3496 CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3498 IPSTAT_INC(ips_odropped);
3503 igmp_scrub_context(m0);
3505 m0->m_pkthdr.rcvif = V_loif;
3507 mac_netinet_igmp_send(ifp, m0);
3509 error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3511 CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3515 IGMPSTAT_INC(igps_snd_reports);
3519 * We must restore the existing vnet pointer before
3520 * continuing as we are run from netisr context.
3526 * Encapsulate an IGMPv3 report.
3528 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3529 * chain has already had its IP/IGMPv3 header prepended. In this case
3530 * the function will not attempt to prepend; the lengths and checksums
3531 * will however be re-computed.
3533 * Returns a pointer to the new mbuf chain head, or NULL if the
3534 * allocation failed.
3536 static struct mbuf *
3537 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3539 struct igmp_report *igmp;
3541 int hdrlen, igmpreclen;
3543 KASSERT((m->m_flags & M_PKTHDR),
3544 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3546 igmpreclen = m_length(m, NULL);
3547 hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3549 if (m->m_flags & M_IGMPV3_HDR) {
3550 igmpreclen -= hdrlen;
3552 M_PREPEND(m, hdrlen, M_NOWAIT);
3555 m->m_flags |= M_IGMPV3_HDR;
3558 CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3560 m->m_data += sizeof(struct ip);
3561 m->m_len -= sizeof(struct ip);
3563 igmp = mtod(m, struct igmp_report *);
3564 igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3567 igmp->ir_numgrps = htons(m->m_pkthdr.vt_nrecs);
3569 igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3570 m->m_pkthdr.vt_nrecs = 0;
3572 m->m_data -= sizeof(struct ip);
3573 m->m_len += sizeof(struct ip);
3575 ip = mtod(m, struct ip *);
3576 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3577 ip->ip_len = htons(hdrlen + igmpreclen);
3578 ip->ip_off = htons(IP_DF);
3579 ip->ip_p = IPPROTO_IGMP;
3582 ip->ip_src.s_addr = INADDR_ANY;
3584 if (m->m_flags & M_IGMP_LOOP) {
3585 struct in_ifaddr *ia;
3589 ip->ip_src = ia->ia_addr.sin_addr;
3592 ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3599 igmp_rec_type_to_str(const int type)
3603 case IGMP_CHANGE_TO_EXCLUDE_MODE:
3606 case IGMP_CHANGE_TO_INCLUDE_MODE:
3609 case IGMP_MODE_IS_EXCLUDE:
3612 case IGMP_MODE_IS_INCLUDE:
3615 case IGMP_ALLOW_NEW_SOURCES:
3618 case IGMP_BLOCK_OLD_SOURCES:
3630 vnet_igmp_init(const void *unused __unused)
3633 netisr_register_vnet(&igmp_nh);
3635 VNET_SYSINIT(vnet_igmp_init, SI_SUB_PROTO_MC, SI_ORDER_ANY,
3636 vnet_igmp_init, NULL);
3639 vnet_igmp_uninit(const void *unused __unused)
3642 /* This can happen when we shutdown the entire network stack. */
3643 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3645 netisr_unregister_vnet(&igmp_nh);
3647 VNET_SYSUNINIT(vnet_igmp_uninit, SI_SUB_PROTO_MC, SI_ORDER_ANY,
3648 vnet_igmp_uninit, NULL);
3652 DB_SHOW_COMMAND(igi_list, db_show_igi_list)
3654 struct igmp_ifsoftc *igi, *tigi;
3655 LIST_HEAD(_igi_list, igmp_ifsoftc) *igi_head;
3658 db_printf("usage: show igi_list <addr>\n");
3661 igi_head = (struct _igi_list *)addr;
3663 LIST_FOREACH_SAFE(igi, igi_head, igi_link, tigi) {
3664 db_printf("igmp_ifsoftc %p:\n", igi);
3665 db_printf(" ifp %p\n", igi->igi_ifp);
3666 db_printf(" version %u\n", igi->igi_version);
3667 db_printf(" v1_timer %u\n", igi->igi_v1_timer);
3668 db_printf(" v2_timer %u\n", igi->igi_v2_timer);
3669 db_printf(" v3_timer %u\n", igi->igi_v3_timer);
3670 db_printf(" flags %#x\n", igi->igi_flags);
3671 db_printf(" rv %u\n", igi->igi_rv);
3672 db_printf(" qi %u\n", igi->igi_qi);
3673 db_printf(" qri %u\n", igi->igi_qri);
3674 db_printf(" uri %u\n", igi->igi_uri);
3675 /* struct mbufq igi_gq; */
3682 igmp_modevent(module_t mod, int type, void *unused __unused)
3687 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3689 m_raopt = igmp_ra_alloc();
3690 netisr_register(&igmp_nh);
3693 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3694 netisr_unregister(&igmp_nh);
3697 IGMP_LOCK_DESTROY();
3700 return (EOPNOTSUPP);
3705 static moduledata_t igmp_mod = {
3710 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE);