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/rmlock.h>
67 #include <sys/sysctl.h>
69 #include <sys/condvar.h>
76 #include <net/if_var.h>
77 #include <net/netisr.h>
80 #include <netinet/in.h>
81 #include <netinet/in_var.h>
82 #include <netinet/in_systm.h>
83 #include <netinet/ip.h>
84 #include <netinet/ip_var.h>
85 #include <netinet/ip_options.h>
86 #include <netinet/igmp.h>
87 #include <netinet/igmp_var.h>
89 #include <machine/in_cksum.h>
91 #include <security/mac/mac_framework.h>
94 #define KTR_IGMPV3 KTR_INET
97 static struct igmp_ifsoftc *
98 igi_alloc_locked(struct ifnet *);
99 static void igi_delete_locked(const struct ifnet *);
100 static void igmp_dispatch_queue(struct mbufq *, int, const int);
101 static void igmp_fasttimo_vnet(void);
102 static void igmp_final_leave(struct in_multi *, struct igmp_ifsoftc *);
103 static int igmp_handle_state_change(struct in_multi *,
104 struct igmp_ifsoftc *);
105 static int igmp_initial_join(struct in_multi *, struct igmp_ifsoftc *);
106 static int igmp_input_v1_query(struct ifnet *, const struct ip *,
107 const struct igmp *);
108 static int igmp_input_v2_query(struct ifnet *, const struct ip *,
109 const struct igmp *);
110 static int igmp_input_v3_query(struct ifnet *, const struct ip *,
111 /*const*/ struct igmpv3 *);
112 static int igmp_input_v3_group_query(struct in_multi *,
113 struct igmp_ifsoftc *, int, /*const*/ struct igmpv3 *);
114 static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
115 /*const*/ struct igmp *);
116 static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
117 /*const*/ struct igmp *);
118 static void igmp_intr(struct mbuf *);
119 static int igmp_isgroupreported(const struct in_addr);
123 static char * igmp_rec_type_to_str(const int);
125 static void igmp_set_version(struct igmp_ifsoftc *, const int);
126 static void igmp_slowtimo_vnet(void);
127 static int igmp_v1v2_queue_report(struct in_multi *, const int);
128 static void igmp_v1v2_process_group_timer(struct in_multi *, const int);
129 static void igmp_v1v2_process_querier_timers(struct igmp_ifsoftc *);
130 static void igmp_v2_update_group(struct in_multi *, const int);
131 static void igmp_v3_cancel_link_timers(struct igmp_ifsoftc *);
132 static void igmp_v3_dispatch_general_query(struct igmp_ifsoftc *);
134 igmp_v3_encap_report(struct ifnet *, struct mbuf *);
135 static int igmp_v3_enqueue_group_record(struct mbufq *,
136 struct in_multi *, const int, const int, const int);
137 static int igmp_v3_enqueue_filter_change(struct mbufq *,
139 static void igmp_v3_process_group_timers(struct igmp_ifsoftc *,
140 struct mbufq *, struct mbufq *, struct in_multi *,
142 static int igmp_v3_merge_state_changes(struct in_multi *,
144 static void igmp_v3_suppress_group_record(struct in_multi *);
145 static int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
146 static int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
147 static int sysctl_igmp_ifinfo(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_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_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 static VNET_DEFINE(int, interface_timers_running); /* IGMPv3 general
222 static VNET_DEFINE(int, state_change_timers_running); /* IGMPv3 state-change
224 static VNET_DEFINE(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 static VNET_DEFINE(LIST_HEAD(, igmp_ifsoftc), igi_head) =
233 LIST_HEAD_INITIALIZER(igi_head);
234 static VNET_DEFINE(struct igmpstat, igmpstat) = {
235 .igps_version = IGPS_VERSION_3,
236 .igps_len = sizeof(struct igmpstat),
238 static VNET_DEFINE(struct timeval, igmp_gsrdelay) = {10, 0};
240 #define V_igi_head VNET(igi_head)
241 #define V_igmpstat VNET(igmpstat)
242 #define V_igmp_gsrdelay VNET(igmp_gsrdelay)
244 static VNET_DEFINE(int, igmp_recvifkludge) = 1;
245 static VNET_DEFINE(int, igmp_sendra) = 1;
246 static VNET_DEFINE(int, igmp_sendlocal) = 1;
247 static VNET_DEFINE(int, igmp_v1enable) = 1;
248 static VNET_DEFINE(int, igmp_v2enable) = 1;
249 static VNET_DEFINE(int, igmp_legacysupp);
250 static VNET_DEFINE(int, igmp_default_version) = IGMP_VERSION_3;
252 #define V_igmp_recvifkludge VNET(igmp_recvifkludge)
253 #define V_igmp_sendra VNET(igmp_sendra)
254 #define V_igmp_sendlocal VNET(igmp_sendlocal)
255 #define V_igmp_v1enable VNET(igmp_v1enable)
256 #define V_igmp_v2enable VNET(igmp_v2enable)
257 #define V_igmp_legacysupp VNET(igmp_legacysupp)
258 #define V_igmp_default_version VNET(igmp_default_version)
261 * Virtualized sysctls.
263 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_VNET | CTLFLAG_RW,
264 &VNET_NAME(igmpstat), igmpstat, "");
265 SYSCTL_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_VNET | CTLFLAG_RW,
266 &VNET_NAME(igmp_recvifkludge), 0,
267 "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
268 SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_VNET | CTLFLAG_RW,
269 &VNET_NAME(igmp_sendra), 0,
270 "Send IP Router Alert option in IGMPv2/v3 messages");
271 SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_VNET | CTLFLAG_RW,
272 &VNET_NAME(igmp_sendlocal), 0,
273 "Send IGMP membership reports for 224.0.0.0/24 groups");
274 SYSCTL_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_VNET | CTLFLAG_RW,
275 &VNET_NAME(igmp_v1enable), 0,
276 "Enable backwards compatibility with IGMPv1");
277 SYSCTL_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_VNET | CTLFLAG_RW,
278 &VNET_NAME(igmp_v2enable), 0,
279 "Enable backwards compatibility with IGMPv2");
280 SYSCTL_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_VNET | CTLFLAG_RW,
281 &VNET_NAME(igmp_legacysupp), 0,
282 "Allow v1/v2 reports to suppress v3 group responses");
283 SYSCTL_PROC(_net_inet_igmp, OID_AUTO, default_version,
284 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
285 &VNET_NAME(igmp_default_version), 0, sysctl_igmp_default_version, "I",
286 "Default version of IGMP to run on each interface");
287 SYSCTL_PROC(_net_inet_igmp, OID_AUTO, gsrdelay,
288 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
289 &VNET_NAME(igmp_gsrdelay.tv_sec), 0, sysctl_igmp_gsr, "I",
290 "Rate limit for IGMPv3 Group-and-Source queries in seconds");
293 * Non-virtualized sysctls.
295 static SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo,
296 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_igmp_ifinfo,
297 "Per-interface IGMPv3 state");
300 igmp_save_context(struct mbuf *m, struct ifnet *ifp)
304 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
306 m->m_pkthdr.flowid = ifp->if_index;
310 igmp_scrub_context(struct mbuf *m)
313 m->m_pkthdr.PH_loc.ptr = NULL;
314 m->m_pkthdr.flowid = 0;
318 * Restore context from a queued IGMP output chain.
319 * Return saved ifindex.
321 * VIMAGE: The assertion is there to make sure that we
322 * actually called CURVNET_SET() with what's in the mbuf chain.
324 static __inline uint32_t
325 igmp_restore_context(struct mbuf *m)
329 #if defined(VIMAGE) && defined(INVARIANTS)
330 KASSERT(curvnet == (m->m_pkthdr.PH_loc.ptr),
331 ("%s: called when curvnet was not restored", __func__));
334 return (m->m_pkthdr.flowid);
338 * Retrieve or set default IGMP version.
340 * VIMAGE: Assume curvnet set by caller.
341 * SMPng: NOTE: Serialized by IGMP lock.
344 sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
349 error = sysctl_wire_old_buffer(req, sizeof(int));
355 new = V_igmp_default_version;
357 error = sysctl_handle_int(oidp, &new, 0, req);
358 if (error || !req->newptr)
361 if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
366 CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
367 V_igmp_default_version, new);
369 V_igmp_default_version = new;
377 * Retrieve or set threshold between group-source queries in seconds.
379 * VIMAGE: Assume curvnet set by caller.
380 * SMPng: NOTE: Serialized by IGMP lock.
383 sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
388 error = sysctl_wire_old_buffer(req, sizeof(int));
394 i = V_igmp_gsrdelay.tv_sec;
396 error = sysctl_handle_int(oidp, &i, 0, req);
397 if (error || !req->newptr)
400 if (i < -1 || i >= 60) {
405 CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
406 V_igmp_gsrdelay.tv_sec, i);
407 V_igmp_gsrdelay.tv_sec = i;
415 * Expose struct igmp_ifsoftc to userland, keyed by ifindex.
416 * For use by ifmcstat(8).
418 * SMPng: NOTE: Does an unlocked ifindex space read.
419 * VIMAGE: Assume curvnet set by caller. The node handler itself
420 * is not directly virtualized.
423 sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
429 struct igmp_ifsoftc *igi;
434 if (req->newptr != NULL)
440 error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
447 if (name[0] <= 0 || name[0] > V_if_index) {
454 ifp = ifnet_byindex(name[0]);
458 LIST_FOREACH(igi, &V_igi_head, igi_link) {
459 if (ifp == igi->igi_ifp) {
460 struct igmp_ifinfo info;
462 info.igi_version = igi->igi_version;
463 info.igi_v1_timer = igi->igi_v1_timer;
464 info.igi_v2_timer = igi->igi_v2_timer;
465 info.igi_v3_timer = igi->igi_v3_timer;
466 info.igi_flags = igi->igi_flags;
467 info.igi_rv = igi->igi_rv;
468 info.igi_qi = igi->igi_qi;
469 info.igi_qri = igi->igi_qri;
470 info.igi_uri = igi->igi_uri;
471 error = SYSCTL_OUT(req, &info, sizeof(info));
483 * Dispatch an entire queue of pending packet chains
485 * VIMAGE: Assumes the vnet pointer has been set.
488 igmp_dispatch_queue(struct mbufq *mq, int limit, const int loop)
492 while ((m = mbufq_dequeue(mq)) != NULL) {
493 CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, mq, m);
495 m->m_flags |= M_IGMP_LOOP;
496 netisr_dispatch(NETISR_IGMP, m);
503 * Filter outgoing IGMP report state by group.
505 * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
506 * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
507 * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
508 * this may break certain IGMP snooping switches which rely on the old
511 * Return zero if the given group is one for which IGMP reports
512 * should be suppressed, or non-zero if reports should be issued.
515 igmp_isgroupreported(const struct in_addr addr)
518 if (in_allhosts(addr) ||
519 ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
526 * Construct a Router Alert option to use in outgoing packets.
534 m = m_get(M_WAITOK, MT_DATA);
535 p = mtod(m, struct ipoption *);
536 p->ipopt_dst.s_addr = INADDR_ANY;
537 p->ipopt_list[0] = (char)IPOPT_RA; /* Router Alert Option */
538 p->ipopt_list[1] = 0x04; /* 4 bytes long */
539 p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */
540 p->ipopt_list[3] = 0x00; /* pad byte */
541 m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
547 * Attach IGMP when PF_INET is attached to an interface.
549 struct igmp_ifsoftc *
550 igmp_domifattach(struct ifnet *ifp)
552 struct igmp_ifsoftc *igi;
554 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
555 __func__, ifp, ifp->if_xname);
559 igi = igi_alloc_locked(ifp);
560 if (!(ifp->if_flags & IFF_MULTICAST))
561 igi->igi_flags |= IGIF_SILENT;
569 * VIMAGE: assume curvnet set by caller.
571 static struct igmp_ifsoftc *
572 igi_alloc_locked(/*const*/ struct ifnet *ifp)
574 struct igmp_ifsoftc *igi;
578 igi = malloc(sizeof(struct igmp_ifsoftc), M_IGMP, M_NOWAIT|M_ZERO);
583 igi->igi_version = V_igmp_default_version;
585 igi->igi_rv = IGMP_RV_INIT;
586 igi->igi_qi = IGMP_QI_INIT;
587 igi->igi_qri = IGMP_QRI_INIT;
588 igi->igi_uri = IGMP_URI_INIT;
589 SLIST_INIT(&igi->igi_relinmhead);
590 mbufq_init(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
592 LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
594 CTR2(KTR_IGMPV3, "allocate igmp_ifsoftc for ifp %p(%s)",
604 * NOTE: Some finalization tasks need to run before the protocol domain
605 * is detached, but also before the link layer does its cleanup.
607 * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
608 * XXX This is also bitten by unlocked ifma_protospec access.
611 igmp_ifdetach(struct ifnet *ifp)
613 struct igmp_ifsoftc *igi;
614 struct ifmultiaddr *ifma;
615 struct in_multi *inm, *tinm;
617 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
622 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
623 if (igi->igi_version == IGMP_VERSION_3) {
625 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
626 if (ifma->ifma_addr->sa_family != AF_INET ||
627 ifma->ifma_protospec == NULL)
630 KASSERT(ifma->ifma_protospec != NULL,
631 ("%s: ifma_protospec is NULL", __func__));
633 inm = (struct in_multi *)ifma->ifma_protospec;
634 if (inm->inm_state == IGMP_LEAVING_MEMBER) {
635 SLIST_INSERT_HEAD(&igi->igi_relinmhead,
638 inm_clear_recorded(inm);
640 IF_ADDR_RUNLOCK(ifp);
642 * Free the in_multi reference(s) for this IGMP lifecycle.
644 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele,
646 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
647 inm_release_locked(inm);
655 * Hook for domifdetach.
658 igmp_domifdetach(struct ifnet *ifp)
661 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
662 __func__, ifp, ifp->if_xname);
665 igi_delete_locked(ifp);
670 igi_delete_locked(const struct ifnet *ifp)
672 struct igmp_ifsoftc *igi, *tigi;
674 CTR3(KTR_IGMPV3, "%s: freeing igmp_ifsoftc 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 mbufq_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",
699 * Process a received IGMPv1 query.
700 * Return non-zero if the message should be dropped.
702 * VIMAGE: The curvnet pointer is derived from the input ifp.
705 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
706 const struct igmp *igmp)
708 struct ifmultiaddr *ifma;
709 struct igmp_ifsoftc *igi;
710 struct in_multi *inm;
713 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
714 * 224.0.0.1. They are always treated as General Queries.
715 * igmp_group is always ignored. Do not drop it as a userland
716 * daemon may wish to see it.
717 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
719 if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
720 IGMPSTAT_INC(igps_rcv_badqueries);
723 IGMPSTAT_INC(igps_rcv_gen_queries);
728 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
729 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
731 if (igi->igi_flags & IGIF_LOOPBACK) {
732 CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
738 * Switch to IGMPv1 host compatibility mode.
740 igmp_set_version(igi, IGMP_VERSION_1);
742 CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
745 * Start the timers in all of our group records
746 * for the interface on which the query arrived,
747 * except those which are already running.
750 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
751 if (ifma->ifma_addr->sa_family != AF_INET ||
752 ifma->ifma_protospec == NULL)
754 inm = (struct in_multi *)ifma->ifma_protospec;
755 if (inm->inm_timer != 0)
757 switch (inm->inm_state) {
758 case IGMP_NOT_MEMBER:
759 case IGMP_SILENT_MEMBER:
761 case IGMP_G_QUERY_PENDING_MEMBER:
762 case IGMP_SG_QUERY_PENDING_MEMBER:
763 case IGMP_REPORTING_MEMBER:
764 case IGMP_IDLE_MEMBER:
765 case IGMP_LAZY_MEMBER:
766 case IGMP_SLEEPING_MEMBER:
767 case IGMP_AWAKENING_MEMBER:
768 inm->inm_state = IGMP_REPORTING_MEMBER;
769 inm->inm_timer = IGMP_RANDOM_DELAY(
770 IGMP_V1V2_MAX_RI * PR_FASTHZ);
771 V_current_state_timers_running = 1;
773 case IGMP_LEAVING_MEMBER:
777 IF_ADDR_RUNLOCK(ifp);
787 * Process a received IGMPv2 general or group-specific query.
790 igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
791 const struct igmp *igmp)
793 struct ifmultiaddr *ifma;
794 struct igmp_ifsoftc *igi;
795 struct in_multi *inm;
796 int is_general_query;
799 is_general_query = 0;
802 * Validate address fields upfront.
803 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
805 if (in_nullhost(igmp->igmp_group)) {
807 * IGMPv2 General Query.
808 * If this was not sent to the all-hosts group, ignore it.
810 if (!in_allhosts(ip->ip_dst))
812 IGMPSTAT_INC(igps_rcv_gen_queries);
813 is_general_query = 1;
815 /* IGMPv2 Group-Specific Query. */
816 IGMPSTAT_INC(igps_rcv_group_queries);
822 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
823 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
825 if (igi->igi_flags & IGIF_LOOPBACK) {
826 CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
832 * Ignore v2 query if in v1 Compatibility Mode.
834 if (igi->igi_version == IGMP_VERSION_1)
837 igmp_set_version(igi, IGMP_VERSION_2);
839 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
843 if (is_general_query) {
845 * For each reporting group joined on this
846 * interface, kick the report timer.
848 CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
851 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
852 if (ifma->ifma_addr->sa_family != AF_INET ||
853 ifma->ifma_protospec == NULL)
855 inm = (struct in_multi *)ifma->ifma_protospec;
856 igmp_v2_update_group(inm, timer);
858 IF_ADDR_RUNLOCK(ifp);
861 * Group-specific IGMPv2 query, we need only
862 * look up the single group to process it.
864 inm = inm_lookup(ifp, igmp->igmp_group);
867 "process v2 query 0x%08x on ifp %p(%s)",
868 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
869 igmp_v2_update_group(inm, timer);
881 * Update the report timer on a group in response to an IGMPv2 query.
883 * If we are becoming the reporting member for this group, start the timer.
884 * If we already are the reporting member for this group, and timer is
885 * below the threshold, reset it.
887 * We may be updating the group for the first time since we switched
888 * to IGMPv3. If we are, then we must clear any recorded source lists,
889 * and transition to REPORTING state; the group timer is overloaded
890 * for group and group-source query responses.
892 * Unlike IGMPv3, the delay per group should be jittered
893 * to avoid bursts of IGMPv2 reports.
896 igmp_v2_update_group(struct in_multi *inm, const int timer)
899 CTR4(KTR_IGMPV3, "0x%08x: %s/%s timer=%d", __func__,
900 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname, timer);
902 IN_MULTI_LOCK_ASSERT();
904 switch (inm->inm_state) {
905 case IGMP_NOT_MEMBER:
906 case IGMP_SILENT_MEMBER:
908 case IGMP_REPORTING_MEMBER:
909 if (inm->inm_timer != 0 &&
910 inm->inm_timer <= timer) {
911 CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
912 "skipping.", __func__);
916 case IGMP_SG_QUERY_PENDING_MEMBER:
917 case IGMP_G_QUERY_PENDING_MEMBER:
918 case IGMP_IDLE_MEMBER:
919 case IGMP_LAZY_MEMBER:
920 case IGMP_AWAKENING_MEMBER:
921 CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
922 inm->inm_state = IGMP_REPORTING_MEMBER;
923 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
924 V_current_state_timers_running = 1;
926 case IGMP_SLEEPING_MEMBER:
927 CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
928 inm->inm_state = IGMP_AWAKENING_MEMBER;
930 case IGMP_LEAVING_MEMBER:
936 * Process a received IGMPv3 general, group-specific or
937 * group-and-source-specific query.
938 * Assumes m has already been pulled up to the full IGMP message length.
939 * Return 0 if successful, otherwise an appropriate error code is returned.
942 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
943 /*const*/ struct igmpv3 *igmpv3)
945 struct igmp_ifsoftc *igi;
946 struct in_multi *inm;
947 int is_general_query;
948 uint32_t maxresp, nsrc, qqi;
952 is_general_query = 0;
954 CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
956 maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */
957 if (maxresp >= 128) {
958 maxresp = IGMP_MANT(igmpv3->igmp_code) <<
959 (IGMP_EXP(igmpv3->igmp_code) + 3);
963 * Robustness must never be less than 2 for on-wire IGMPv3.
964 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
965 * an exception for interfaces whose IGMPv3 state changes
966 * are redirected to loopback (e.g. MANET).
968 qrv = IGMP_QRV(igmpv3->igmp_misc);
970 CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
975 qqi = igmpv3->igmp_qqi;
977 qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
978 (IGMP_EXP(igmpv3->igmp_qqi) + 3);
981 timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
985 nsrc = ntohs(igmpv3->igmp_numsrc);
988 * Validate address fields and versions upfront before
989 * accepting v3 query.
990 * XXX SMPng: Unlocked access to igmpstat counters here.
992 if (in_nullhost(igmpv3->igmp_group)) {
994 * IGMPv3 General Query.
996 * General Queries SHOULD be directed to 224.0.0.1.
997 * A general query with a source list has undefined
998 * behaviour; discard it.
1000 IGMPSTAT_INC(igps_rcv_gen_queries);
1001 if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
1002 IGMPSTAT_INC(igps_rcv_badqueries);
1005 is_general_query = 1;
1007 /* Group or group-source specific query. */
1009 IGMPSTAT_INC(igps_rcv_group_queries);
1011 IGMPSTAT_INC(igps_rcv_gsr_queries);
1017 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1018 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
1020 if (igi->igi_flags & IGIF_LOOPBACK) {
1021 CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1022 ifp, ifp->if_xname);
1027 * Discard the v3 query if we're in Compatibility Mode.
1028 * The RFC is not obviously worded that hosts need to stay in
1029 * compatibility mode until the Old Version Querier Present
1032 if (igi->igi_version != IGMP_VERSION_3) {
1033 CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1034 igi->igi_version, ifp, ifp->if_xname);
1038 igmp_set_version(igi, IGMP_VERSION_3);
1041 igi->igi_qri = maxresp;
1043 CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1046 if (is_general_query) {
1048 * Schedule a current-state report on this ifp for
1049 * all groups, possibly containing source lists.
1050 * If there is a pending General Query response
1051 * scheduled earlier than the selected delay, do
1052 * not schedule any other reports.
1053 * Otherwise, reset the interface timer.
1055 CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1056 ifp, ifp->if_xname);
1057 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1058 igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1059 V_interface_timers_running = 1;
1063 * Group-source-specific queries are throttled on
1064 * a per-group basis to defeat denial-of-service attempts.
1065 * Queries for groups we are not a member of on this
1066 * link are simply ignored.
1068 inm = inm_lookup(ifp, igmpv3->igmp_group);
1072 if (!ratecheck(&inm->inm_lastgsrtv,
1073 &V_igmp_gsrdelay)) {
1074 CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1076 IGMPSTAT_INC(igps_drop_gsr_queries);
1080 CTR3(KTR_IGMPV3, "process v3 0x%08x query on ifp %p(%s)",
1081 ntohl(igmpv3->igmp_group.s_addr), ifp, ifp->if_xname);
1083 * If there is a pending General Query response
1084 * scheduled sooner than the selected delay, no
1085 * further report need be scheduled.
1086 * Otherwise, prepare to respond to the
1087 * group-specific or group-and-source query.
1089 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1090 igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1101 * Process a received IGMPv3 group-specific or group-and-source-specific
1103 * Return <0 if any error occurred. Currently this is ignored.
1106 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifsoftc *igi,
1107 int timer, /*const*/ struct igmpv3 *igmpv3)
1112 IN_MULTI_LOCK_ASSERT();
1117 switch (inm->inm_state) {
1118 case IGMP_NOT_MEMBER:
1119 case IGMP_SILENT_MEMBER:
1120 case IGMP_SLEEPING_MEMBER:
1121 case IGMP_LAZY_MEMBER:
1122 case IGMP_AWAKENING_MEMBER:
1123 case IGMP_IDLE_MEMBER:
1124 case IGMP_LEAVING_MEMBER:
1127 case IGMP_REPORTING_MEMBER:
1128 case IGMP_G_QUERY_PENDING_MEMBER:
1129 case IGMP_SG_QUERY_PENDING_MEMBER:
1133 nsrc = ntohs(igmpv3->igmp_numsrc);
1136 * Deal with group-specific queries upfront.
1137 * If any group query is already pending, purge any recorded
1138 * source-list state if it exists, and schedule a query response
1139 * for this group-specific query.
1142 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1143 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1144 inm_clear_recorded(inm);
1145 timer = min(inm->inm_timer, timer);
1147 inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1148 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1149 V_current_state_timers_running = 1;
1154 * Deal with the case where a group-and-source-specific query has
1155 * been received but a group-specific query is already pending.
1157 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1158 timer = min(inm->inm_timer, timer);
1159 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1160 V_current_state_timers_running = 1;
1165 * Finally, deal with the case where a group-and-source-specific
1166 * query has been received, where a response to a previous g-s-r
1167 * query exists, or none exists.
1168 * In this case, we need to parse the source-list which the Querier
1169 * has provided us with and check if we have any source list filter
1170 * entries at T1 for these sources. If we do not, there is no need
1171 * schedule a report and the query may be dropped.
1172 * If we do, we must record them and schedule a current-state
1173 * report for those sources.
1174 * FIXME: Handling source lists larger than 1 mbuf requires that
1175 * we pass the mbuf chain pointer down to this function, and use
1176 * m_getptr() to walk the chain.
1178 if (inm->inm_nsrc > 0) {
1179 const struct in_addr *ap;
1182 ap = (const struct in_addr *)(igmpv3 + 1);
1184 for (i = 0; i < nsrc; i++, ap++) {
1185 retval = inm_record_source(inm, ap->s_addr);
1188 nrecorded += retval;
1190 if (nrecorded > 0) {
1192 "%s: schedule response to SG query", __func__);
1193 inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1194 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1195 V_current_state_timers_running = 1;
1203 * Process a received IGMPv1 host membership report.
1205 * NOTE: 0.0.0.0 workaround breaks const correctness.
1208 igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1209 /*const*/ struct igmp *igmp)
1211 struct rm_priotracker in_ifa_tracker;
1212 struct in_ifaddr *ia;
1213 struct in_multi *inm;
1215 IGMPSTAT_INC(igps_rcv_reports);
1217 if (ifp->if_flags & IFF_LOOPBACK)
1220 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1221 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1222 IGMPSTAT_INC(igps_rcv_badreports);
1227 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1228 * Booting clients may use the source address 0.0.0.0. Some
1229 * IGMP daemons may not know how to use IP_RECVIF to determine
1230 * the interface upon which this message was received.
1231 * Replace 0.0.0.0 with the subnet address if told to do so.
1233 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1234 IFP_TO_IA(ifp, ia, &in_ifa_tracker);
1236 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1237 ifa_free(&ia->ia_ifa);
1241 CTR3(KTR_IGMPV3, "process v1 report 0x%08x on ifp %p(%s)",
1242 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1245 * IGMPv1 report suppression.
1246 * If we are a member of this group, and our membership should be
1247 * reported, stop our group timer and transition to the 'lazy' state.
1250 inm = inm_lookup(ifp, igmp->igmp_group);
1252 struct igmp_ifsoftc *igi;
1256 KASSERT(igi != NULL,
1257 ("%s: no igi for ifp %p", __func__, ifp));
1261 IGMPSTAT_INC(igps_rcv_ourreports);
1264 * If we are in IGMPv3 host mode, do not allow the
1265 * other host's IGMPv1 report to suppress our reports
1266 * unless explicitly configured to do so.
1268 if (igi->igi_version == IGMP_VERSION_3) {
1269 if (V_igmp_legacysupp)
1270 igmp_v3_suppress_group_record(inm);
1276 switch (inm->inm_state) {
1277 case IGMP_NOT_MEMBER:
1278 case IGMP_SILENT_MEMBER:
1280 case IGMP_IDLE_MEMBER:
1281 case IGMP_LAZY_MEMBER:
1282 case IGMP_AWAKENING_MEMBER:
1284 "report suppressed for 0x%08x on ifp %p(%s)",
1285 ntohl(igmp->igmp_group.s_addr), ifp,
1287 case IGMP_SLEEPING_MEMBER:
1288 inm->inm_state = IGMP_SLEEPING_MEMBER;
1290 case IGMP_REPORTING_MEMBER:
1292 "report suppressed for 0x%08x on ifp %p(%s)",
1293 ntohl(igmp->igmp_group.s_addr), ifp,
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 rm_priotracker in_ifa_tracker;
1323 struct in_ifaddr *ia;
1324 struct in_multi *inm;
1327 * Make sure we don't hear our own membership report. Fast
1328 * leave requires knowing that we are the only member of a
1331 IFP_TO_IA(ifp, ia, &in_ifa_tracker);
1332 if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1333 ifa_free(&ia->ia_ifa);
1337 IGMPSTAT_INC(igps_rcv_reports);
1339 if (ifp->if_flags & IFF_LOOPBACK) {
1341 ifa_free(&ia->ia_ifa);
1345 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1346 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1348 ifa_free(&ia->ia_ifa);
1349 IGMPSTAT_INC(igps_rcv_badreports);
1354 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1355 * Booting clients may use the source address 0.0.0.0. Some
1356 * IGMP daemons may not know how to use IP_RECVIF to determine
1357 * the interface upon which this message was received.
1358 * Replace 0.0.0.0 with the subnet address if told to do so.
1360 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1362 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1365 ifa_free(&ia->ia_ifa);
1367 CTR3(KTR_IGMPV3, "process v2 report 0x%08x on ifp %p(%s)",
1368 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1371 * IGMPv2 report suppression.
1372 * If we are a member of this group, and our membership should be
1373 * reported, and our group timer is pending or about to be reset,
1374 * stop our group timer by transitioning to the 'lazy' state.
1377 inm = inm_lookup(ifp, igmp->igmp_group);
1379 struct igmp_ifsoftc *igi;
1382 KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1384 IGMPSTAT_INC(igps_rcv_ourreports);
1387 * If we are in IGMPv3 host mode, do not allow the
1388 * other host's IGMPv1 report to suppress our reports
1389 * unless explicitly configured to do so.
1391 if (igi->igi_version == IGMP_VERSION_3) {
1392 if (V_igmp_legacysupp)
1393 igmp_v3_suppress_group_record(inm);
1399 switch (inm->inm_state) {
1400 case IGMP_NOT_MEMBER:
1401 case IGMP_SILENT_MEMBER:
1402 case IGMP_SLEEPING_MEMBER:
1404 case IGMP_REPORTING_MEMBER:
1405 case IGMP_IDLE_MEMBER:
1406 case IGMP_AWAKENING_MEMBER:
1408 "report suppressed for 0x%08x on ifp %p(%s)",
1409 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1410 case IGMP_LAZY_MEMBER:
1411 inm->inm_state = IGMP_LAZY_MEMBER;
1413 case IGMP_G_QUERY_PENDING_MEMBER:
1414 case IGMP_SG_QUERY_PENDING_MEMBER:
1415 case IGMP_LEAVING_MEMBER:
1427 igmp_input(struct mbuf **mp, int *offp, int proto)
1438 CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, *mp, *offp);
1441 ifp = m->m_pkthdr.rcvif;
1444 IGMPSTAT_INC(igps_rcv_total);
1446 ip = mtod(m, struct ip *);
1448 igmplen = ntohs(ip->ip_len) - iphlen;
1453 if (igmplen < IGMP_MINLEN) {
1454 IGMPSTAT_INC(igps_rcv_tooshort);
1456 return (IPPROTO_DONE);
1460 * Always pullup to the minimum size for v1/v2 or v3
1461 * to amortize calls to m_pullup().
1464 if (igmplen >= IGMP_V3_QUERY_MINLEN)
1465 minlen += IGMP_V3_QUERY_MINLEN;
1467 minlen += IGMP_MINLEN;
1468 if ((!M_WRITABLE(m) || m->m_len < minlen) &&
1469 (m = m_pullup(m, minlen)) == NULL) {
1470 IGMPSTAT_INC(igps_rcv_tooshort);
1471 return (IPPROTO_DONE);
1473 ip = mtod(m, struct ip *);
1476 * Validate checksum.
1478 m->m_data += iphlen;
1480 igmp = mtod(m, struct igmp *);
1481 if (in_cksum(m, igmplen)) {
1482 IGMPSTAT_INC(igps_rcv_badsum);
1484 return (IPPROTO_DONE);
1486 m->m_data -= iphlen;
1490 * IGMP control traffic is link-scope, and must have a TTL of 1.
1491 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1492 * probe packets may come from beyond the LAN.
1494 if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) {
1495 IGMPSTAT_INC(igps_rcv_badttl);
1497 return (IPPROTO_DONE);
1500 switch (igmp->igmp_type) {
1501 case IGMP_HOST_MEMBERSHIP_QUERY:
1502 if (igmplen == IGMP_MINLEN) {
1503 if (igmp->igmp_code == 0)
1504 queryver = IGMP_VERSION_1;
1506 queryver = IGMP_VERSION_2;
1507 } else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1508 queryver = IGMP_VERSION_3;
1510 IGMPSTAT_INC(igps_rcv_tooshort);
1512 return (IPPROTO_DONE);
1516 case IGMP_VERSION_1:
1517 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1518 if (!V_igmp_v1enable)
1520 if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1522 return (IPPROTO_DONE);
1526 case IGMP_VERSION_2:
1527 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1528 if (!V_igmp_v2enable)
1530 if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
1532 return (IPPROTO_DONE);
1536 case IGMP_VERSION_3: {
1537 struct igmpv3 *igmpv3;
1541 IGMPSTAT_INC(igps_rcv_v3_queries);
1542 igmpv3 = (struct igmpv3 *)igmp;
1544 * Validate length based on source count.
1546 nsrc = ntohs(igmpv3->igmp_numsrc);
1547 if (nsrc * sizeof(in_addr_t) >
1548 UINT16_MAX - iphlen - IGMP_V3_QUERY_MINLEN) {
1549 IGMPSTAT_INC(igps_rcv_tooshort);
1550 return (IPPROTO_DONE);
1553 * m_pullup() may modify m, so pullup in
1556 igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1557 sizeof(struct in_addr) * nsrc;
1558 if ((!M_WRITABLE(m) ||
1559 m->m_len < igmpv3len) &&
1560 (m = m_pullup(m, igmpv3len)) == NULL) {
1561 IGMPSTAT_INC(igps_rcv_tooshort);
1562 return (IPPROTO_DONE);
1564 igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1566 if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1568 return (IPPROTO_DONE);
1575 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1576 if (!V_igmp_v1enable)
1578 if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1580 return (IPPROTO_DONE);
1584 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1585 if (!V_igmp_v2enable)
1587 if (!ip_checkrouteralert(m))
1588 IGMPSTAT_INC(igps_rcv_nora);
1589 if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
1591 return (IPPROTO_DONE);
1595 case IGMP_v3_HOST_MEMBERSHIP_REPORT:
1597 * Hosts do not need to process IGMPv3 membership reports,
1598 * as report suppression is no longer required.
1600 if (!ip_checkrouteralert(m))
1601 IGMPSTAT_INC(igps_rcv_nora);
1609 * Pass all valid IGMP packets up to any process(es) listening on a
1613 return (rip_input(mp, offp, proto));
1618 * Fast timeout handler (global).
1619 * VIMAGE: Timeout handlers are expected to service all vimages.
1624 VNET_ITERATOR_DECL(vnet_iter);
1626 VNET_LIST_RLOCK_NOSLEEP();
1627 VNET_FOREACH(vnet_iter) {
1628 CURVNET_SET(vnet_iter);
1629 igmp_fasttimo_vnet();
1632 VNET_LIST_RUNLOCK_NOSLEEP();
1636 * Fast timeout handler (per-vnet).
1637 * Sends are shuffled off to a netisr to deal with Giant.
1639 * VIMAGE: Assume caller has set up our curvnet.
1642 igmp_fasttimo_vnet(void)
1644 struct mbufq scq; /* State-change packets */
1645 struct mbufq qrq; /* Query response packets */
1647 struct igmp_ifsoftc *igi;
1648 struct ifmultiaddr *ifma;
1649 struct in_multi *inm;
1650 int loop, uri_fasthz;
1656 * Quick check to see if any work needs to be done, in order to
1657 * minimize the overhead of fasttimo processing.
1658 * SMPng: XXX Unlocked reads.
1660 if (!V_current_state_timers_running &&
1661 !V_interface_timers_running &&
1662 !V_state_change_timers_running)
1669 * IGMPv3 General Query response timer processing.
1671 if (V_interface_timers_running) {
1672 CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1674 V_interface_timers_running = 0;
1675 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1676 if (igi->igi_v3_timer == 0) {
1678 } else if (--igi->igi_v3_timer == 0) {
1679 igmp_v3_dispatch_general_query(igi);
1681 V_interface_timers_running = 1;
1686 if (!V_current_state_timers_running &&
1687 !V_state_change_timers_running)
1690 V_current_state_timers_running = 0;
1691 V_state_change_timers_running = 0;
1693 CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1696 * IGMPv1/v2/v3 host report and state-change timer processing.
1697 * Note: Processing a v3 group timer may remove a node.
1699 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1702 if (igi->igi_version == IGMP_VERSION_3) {
1703 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1704 uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1706 mbufq_init(&qrq, IGMP_MAX_G_GS_PACKETS);
1707 mbufq_init(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1711 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1712 if (ifma->ifma_addr->sa_family != AF_INET ||
1713 ifma->ifma_protospec == NULL)
1715 inm = (struct in_multi *)ifma->ifma_protospec;
1716 switch (igi->igi_version) {
1717 case IGMP_VERSION_1:
1718 case IGMP_VERSION_2:
1719 igmp_v1v2_process_group_timer(inm,
1722 case IGMP_VERSION_3:
1723 igmp_v3_process_group_timers(igi, &qrq,
1724 &scq, inm, uri_fasthz);
1728 IF_ADDR_RUNLOCK(ifp);
1730 if (igi->igi_version == IGMP_VERSION_3) {
1731 struct in_multi *tinm;
1733 igmp_dispatch_queue(&qrq, 0, loop);
1734 igmp_dispatch_queue(&scq, 0, loop);
1737 * Free the in_multi reference(s) for this
1740 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead,
1742 SLIST_REMOVE_HEAD(&igi->igi_relinmhead,
1744 inm_release_locked(inm);
1755 * Update host report group timer for IGMPv1/v2.
1756 * Will update the global pending timer flags.
1759 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1761 int report_timer_expired;
1763 IN_MULTI_LOCK_ASSERT();
1766 if (inm->inm_timer == 0) {
1767 report_timer_expired = 0;
1768 } else if (--inm->inm_timer == 0) {
1769 report_timer_expired = 1;
1771 V_current_state_timers_running = 1;
1775 switch (inm->inm_state) {
1776 case IGMP_NOT_MEMBER:
1777 case IGMP_SILENT_MEMBER:
1778 case IGMP_IDLE_MEMBER:
1779 case IGMP_LAZY_MEMBER:
1780 case IGMP_SLEEPING_MEMBER:
1781 case IGMP_AWAKENING_MEMBER:
1783 case IGMP_REPORTING_MEMBER:
1784 if (report_timer_expired) {
1785 inm->inm_state = IGMP_IDLE_MEMBER;
1786 (void)igmp_v1v2_queue_report(inm,
1787 (version == IGMP_VERSION_2) ?
1788 IGMP_v2_HOST_MEMBERSHIP_REPORT :
1789 IGMP_v1_HOST_MEMBERSHIP_REPORT);
1792 case IGMP_G_QUERY_PENDING_MEMBER:
1793 case IGMP_SG_QUERY_PENDING_MEMBER:
1794 case IGMP_LEAVING_MEMBER:
1800 * Update a group's timers for IGMPv3.
1801 * Will update the global pending timer flags.
1802 * Note: Unlocked read from igi.
1805 igmp_v3_process_group_timers(struct igmp_ifsoftc *igi,
1806 struct mbufq *qrq, struct mbufq *scq,
1807 struct in_multi *inm, const int uri_fasthz)
1809 int query_response_timer_expired;
1810 int state_change_retransmit_timer_expired;
1812 IN_MULTI_LOCK_ASSERT();
1815 query_response_timer_expired = 0;
1816 state_change_retransmit_timer_expired = 0;
1819 * During a transition from v1/v2 compatibility mode back to v3,
1820 * a group record in REPORTING state may still have its group
1821 * timer active. This is a no-op in this function; it is easier
1822 * to deal with it here than to complicate the slow-timeout path.
1824 if (inm->inm_timer == 0) {
1825 query_response_timer_expired = 0;
1826 } else if (--inm->inm_timer == 0) {
1827 query_response_timer_expired = 1;
1829 V_current_state_timers_running = 1;
1832 if (inm->inm_sctimer == 0) {
1833 state_change_retransmit_timer_expired = 0;
1834 } else if (--inm->inm_sctimer == 0) {
1835 state_change_retransmit_timer_expired = 1;
1837 V_state_change_timers_running = 1;
1840 /* We are in fasttimo, so be quick about it. */
1841 if (!state_change_retransmit_timer_expired &&
1842 !query_response_timer_expired)
1845 switch (inm->inm_state) {
1846 case IGMP_NOT_MEMBER:
1847 case IGMP_SILENT_MEMBER:
1848 case IGMP_SLEEPING_MEMBER:
1849 case IGMP_LAZY_MEMBER:
1850 case IGMP_AWAKENING_MEMBER:
1851 case IGMP_IDLE_MEMBER:
1853 case IGMP_G_QUERY_PENDING_MEMBER:
1854 case IGMP_SG_QUERY_PENDING_MEMBER:
1856 * Respond to a previously pending Group-Specific
1857 * or Group-and-Source-Specific query by enqueueing
1858 * the appropriate Current-State report for
1859 * immediate transmission.
1861 if (query_response_timer_expired) {
1864 retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1865 (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1866 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1868 inm->inm_state = IGMP_REPORTING_MEMBER;
1869 /* XXX Clear recorded sources for next time. */
1870 inm_clear_recorded(inm);
1873 case IGMP_REPORTING_MEMBER:
1874 case IGMP_LEAVING_MEMBER:
1875 if (state_change_retransmit_timer_expired) {
1877 * State-change retransmission timer fired.
1878 * If there are any further pending retransmissions,
1879 * set the global pending state-change flag, and
1882 if (--inm->inm_scrv > 0) {
1883 inm->inm_sctimer = uri_fasthz;
1884 V_state_change_timers_running = 1;
1887 * Retransmit the previously computed state-change
1888 * report. If there are no further pending
1889 * retransmissions, the mbuf queue will be consumed.
1890 * Update T0 state to T1 as we have now sent
1893 (void)igmp_v3_merge_state_changes(inm, scq);
1896 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
1897 ntohl(inm->inm_addr.s_addr),
1898 inm->inm_ifp->if_xname);
1901 * If we are leaving the group for good, make sure
1902 * we release IGMP's reference to it.
1903 * This release must be deferred using a SLIST,
1904 * as we are called from a loop which traverses
1905 * the in_ifmultiaddr TAILQ.
1907 if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1908 inm->inm_scrv == 0) {
1909 inm->inm_state = IGMP_NOT_MEMBER;
1910 SLIST_INSERT_HEAD(&igi->igi_relinmhead,
1920 * Suppress a group's pending response to a group or source/group query.
1922 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1923 * Do NOT update ST1/ST0 as this operation merely suppresses
1924 * the currently pending group record.
1925 * Do NOT suppress the response to a general query. It is possible but
1926 * it would require adding another state or flag.
1929 igmp_v3_suppress_group_record(struct in_multi *inm)
1932 IN_MULTI_LOCK_ASSERT();
1934 KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1935 ("%s: not IGMPv3 mode on link", __func__));
1937 if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1938 inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1941 if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1942 inm_clear_recorded(inm);
1945 inm->inm_state = IGMP_REPORTING_MEMBER;
1949 * Switch to a different IGMP version on the given interface,
1950 * as per Section 7.2.1.
1953 igmp_set_version(struct igmp_ifsoftc *igi, const int version)
1955 int old_version_timer;
1959 CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1960 version, igi->igi_ifp, igi->igi_ifp->if_xname);
1962 if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1964 * Compute the "Older Version Querier Present" timer as per
1967 old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1968 old_version_timer *= PR_SLOWHZ;
1970 if (version == IGMP_VERSION_1) {
1971 igi->igi_v1_timer = old_version_timer;
1972 igi->igi_v2_timer = 0;
1973 } else if (version == IGMP_VERSION_2) {
1974 igi->igi_v1_timer = 0;
1975 igi->igi_v2_timer = old_version_timer;
1979 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
1980 if (igi->igi_version != IGMP_VERSION_2) {
1981 igi->igi_version = IGMP_VERSION_2;
1982 igmp_v3_cancel_link_timers(igi);
1984 } else if (igi->igi_v1_timer > 0) {
1985 if (igi->igi_version != IGMP_VERSION_1) {
1986 igi->igi_version = IGMP_VERSION_1;
1987 igmp_v3_cancel_link_timers(igi);
1993 * Cancel pending IGMPv3 timers for the given link and all groups
1994 * joined on it; state-change, general-query, and group-query timers.
1996 * Only ever called on a transition from v3 to Compatibility mode. Kill
1997 * the timers stone dead (this may be expensive for large N groups), they
1998 * will be restarted if Compatibility Mode deems that they must be due to
2002 igmp_v3_cancel_link_timers(struct igmp_ifsoftc *igi)
2004 struct ifmultiaddr *ifma;
2006 struct in_multi *inm, *tinm;
2008 CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2009 igi->igi_ifp, igi->igi_ifp->if_xname);
2011 IN_MULTI_LOCK_ASSERT();
2015 * Stop the v3 General Query Response on this link stone dead.
2016 * If fasttimo is woken up due to V_interface_timers_running,
2017 * the flag will be cleared if there are no pending link timers.
2019 igi->igi_v3_timer = 0;
2022 * Now clear the current-state and state-change report timers
2023 * for all memberships scoped to this link.
2027 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2028 if (ifma->ifma_addr->sa_family != AF_INET ||
2029 ifma->ifma_protospec == NULL)
2031 inm = (struct in_multi *)ifma->ifma_protospec;
2032 switch (inm->inm_state) {
2033 case IGMP_NOT_MEMBER:
2034 case IGMP_SILENT_MEMBER:
2035 case IGMP_IDLE_MEMBER:
2036 case IGMP_LAZY_MEMBER:
2037 case IGMP_SLEEPING_MEMBER:
2038 case IGMP_AWAKENING_MEMBER:
2040 * These states are either not relevant in v3 mode,
2041 * or are unreported. Do nothing.
2044 case IGMP_LEAVING_MEMBER:
2046 * If we are leaving the group and switching to
2047 * compatibility mode, we need to release the final
2048 * reference held for issuing the INCLUDE {}, and
2049 * transition to REPORTING to ensure the host leave
2050 * message is sent upstream to the old querier --
2051 * transition to NOT would lose the leave and race.
2053 SLIST_INSERT_HEAD(&igi->igi_relinmhead, inm, inm_nrele);
2055 case IGMP_G_QUERY_PENDING_MEMBER:
2056 case IGMP_SG_QUERY_PENDING_MEMBER:
2057 inm_clear_recorded(inm);
2059 case IGMP_REPORTING_MEMBER:
2060 inm->inm_state = IGMP_REPORTING_MEMBER;
2064 * Always clear state-change and group report timers.
2065 * Free any pending IGMPv3 state-change records.
2067 inm->inm_sctimer = 0;
2069 mbufq_drain(&inm->inm_scq);
2071 IF_ADDR_RUNLOCK(ifp);
2072 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, tinm) {
2073 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
2074 inm_release_locked(inm);
2079 * Update the Older Version Querier Present timers for a link.
2080 * See Section 7.2.1 of RFC 3376.
2083 igmp_v1v2_process_querier_timers(struct igmp_ifsoftc *igi)
2088 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2090 * IGMPv1 and IGMPv2 Querier Present timers expired.
2094 if (igi->igi_version != IGMP_VERSION_3) {
2096 "%s: transition from v%d -> v%d on %p(%s)",
2097 __func__, igi->igi_version, IGMP_VERSION_3,
2098 igi->igi_ifp, igi->igi_ifp->if_xname);
2099 igi->igi_version = IGMP_VERSION_3;
2101 } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2103 * IGMPv1 Querier Present timer expired,
2104 * IGMPv2 Querier Present timer running.
2105 * If IGMPv2 was disabled since last timeout,
2107 * If IGMPv2 is enabled, revert to IGMPv2.
2109 if (!V_igmp_v2enable) {
2111 "%s: transition from v%d -> v%d on %p(%s)",
2112 __func__, igi->igi_version, IGMP_VERSION_3,
2113 igi->igi_ifp, igi->igi_ifp->if_xname);
2114 igi->igi_v2_timer = 0;
2115 igi->igi_version = IGMP_VERSION_3;
2117 --igi->igi_v2_timer;
2118 if (igi->igi_version != IGMP_VERSION_2) {
2120 "%s: transition from v%d -> v%d on %p(%s)",
2121 __func__, igi->igi_version, IGMP_VERSION_2,
2122 igi->igi_ifp, igi->igi_ifp->if_xname);
2123 igi->igi_version = IGMP_VERSION_2;
2124 igmp_v3_cancel_link_timers(igi);
2127 } else if (igi->igi_v1_timer > 0) {
2129 * IGMPv1 Querier Present timer running.
2130 * Stop IGMPv2 timer if running.
2132 * If IGMPv1 was disabled since last timeout,
2134 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2136 if (!V_igmp_v1enable) {
2138 "%s: transition from v%d -> v%d on %p(%s)",
2139 __func__, igi->igi_version, IGMP_VERSION_3,
2140 igi->igi_ifp, igi->igi_ifp->if_xname);
2141 igi->igi_v1_timer = 0;
2142 igi->igi_version = IGMP_VERSION_3;
2144 --igi->igi_v1_timer;
2146 if (igi->igi_v2_timer > 0) {
2148 "%s: cancel v2 timer on %p(%s)",
2149 __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2150 igi->igi_v2_timer = 0;
2156 * Global slowtimo handler.
2157 * VIMAGE: Timeout handlers are expected to service all vimages.
2162 VNET_ITERATOR_DECL(vnet_iter);
2164 VNET_LIST_RLOCK_NOSLEEP();
2165 VNET_FOREACH(vnet_iter) {
2166 CURVNET_SET(vnet_iter);
2167 igmp_slowtimo_vnet();
2170 VNET_LIST_RUNLOCK_NOSLEEP();
2174 * Per-vnet slowtimo handler.
2177 igmp_slowtimo_vnet(void)
2179 struct igmp_ifsoftc *igi;
2183 LIST_FOREACH(igi, &V_igi_head, igi_link) {
2184 igmp_v1v2_process_querier_timers(igi);
2191 * Dispatch an IGMPv1/v2 host report or leave message.
2192 * These are always small enough to fit inside a single mbuf.
2195 igmp_v1v2_queue_report(struct in_multi *inm, const int type)
2202 IN_MULTI_LOCK_ASSERT();
2207 m = m_gethdr(M_NOWAIT, MT_DATA);
2210 M_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
2212 m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2214 m->m_data += sizeof(struct ip);
2215 m->m_len = sizeof(struct igmp);
2217 igmp = mtod(m, struct igmp *);
2218 igmp->igmp_type = type;
2219 igmp->igmp_code = 0;
2220 igmp->igmp_group = inm->inm_addr;
2221 igmp->igmp_cksum = 0;
2222 igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
2224 m->m_data -= sizeof(struct ip);
2225 m->m_len += sizeof(struct ip);
2227 ip = mtod(m, struct ip *);
2229 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct igmp));
2231 ip->ip_p = IPPROTO_IGMP;
2232 ip->ip_src.s_addr = INADDR_ANY;
2234 if (type == IGMP_HOST_LEAVE_MESSAGE)
2235 ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2237 ip->ip_dst = inm->inm_addr;
2239 igmp_save_context(m, ifp);
2241 m->m_flags |= M_IGMPV2;
2242 if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2243 m->m_flags |= M_IGMP_LOOP;
2245 CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2246 netisr_dispatch(NETISR_IGMP, m);
2252 * Process a state change from the upper layer for the given IPv4 group.
2254 * Each socket holds a reference on the in_multi in its own ip_moptions.
2255 * The socket layer will have made the necessary updates to.the group
2256 * state, it is now up to IGMP to issue a state change report if there
2257 * has been any change between T0 (when the last state-change was issued)
2260 * We use the IGMPv3 state machine at group level. The IGMP module
2261 * however makes the decision as to which IGMP protocol version to speak.
2262 * A state change *from* INCLUDE {} always means an initial join.
2263 * A state change *to* INCLUDE {} always means a final leave.
2265 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2266 * save ourselves a bunch of work; any exclusive mode groups need not
2267 * compute source filter lists.
2269 * VIMAGE: curvnet should have been set by caller, as this routine
2270 * is called from the socket option handlers.
2273 igmp_change_state(struct in_multi *inm)
2275 struct igmp_ifsoftc *igi;
2279 IN_MULTI_LOCK_ASSERT();
2284 * Try to detect if the upper layer just asked us to change state
2285 * for an interface which has now gone away.
2287 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2288 ifp = inm->inm_ifma->ifma_ifp;
2290 * Sanity check that netinet's notion of ifp is the
2293 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2297 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2298 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
2301 * If we detect a state transition to or from MCAST_UNDEFINED
2302 * for this group, then we are starting or finishing an IGMP
2303 * life cycle for this group.
2305 if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2306 CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2307 inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2308 if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2309 CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2310 error = igmp_initial_join(inm, igi);
2312 } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2313 CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2314 igmp_final_leave(inm, igi);
2318 CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2321 error = igmp_handle_state_change(inm, igi);
2329 * Perform the initial join for an IGMP group.
2331 * When joining a group:
2332 * If the group should have its IGMP traffic suppressed, do nothing.
2333 * IGMPv1 starts sending IGMPv1 host membership reports.
2334 * IGMPv2 starts sending IGMPv2 host membership reports.
2335 * IGMPv3 will schedule an IGMPv3 state-change report containing the
2336 * initial state of the membership.
2339 igmp_initial_join(struct in_multi *inm, struct igmp_ifsoftc *igi)
2343 int error, retval, syncstates;
2345 CTR4(KTR_IGMPV3, "%s: initial join 0x%08x on ifp %p(%s)", __func__,
2346 ntohl(inm->inm_addr.s_addr), inm->inm_ifp, inm->inm_ifp->if_xname);
2353 IN_MULTI_LOCK_ASSERT();
2356 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2359 * Groups joined on loopback or marked as 'not reported',
2360 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2361 * are never reported in any IGMP protocol exchanges.
2362 * All other groups enter the appropriate IGMP state machine
2363 * for the version in use on this link.
2364 * A link marked as IGIF_SILENT causes IGMP to be completely
2365 * disabled for the link.
2367 if ((ifp->if_flags & IFF_LOOPBACK) ||
2368 (igi->igi_flags & IGIF_SILENT) ||
2369 !igmp_isgroupreported(inm->inm_addr)) {
2371 "%s: not kicking state machine for silent group", __func__);
2372 inm->inm_state = IGMP_SILENT_MEMBER;
2376 * Deal with overlapping in_multi lifecycle.
2377 * If this group was LEAVING, then make sure
2378 * we drop the reference we picked up to keep the
2379 * group around for the final INCLUDE {} enqueue.
2381 if (igi->igi_version == IGMP_VERSION_3 &&
2382 inm->inm_state == IGMP_LEAVING_MEMBER)
2383 inm_release_locked(inm);
2385 inm->inm_state = IGMP_REPORTING_MEMBER;
2387 switch (igi->igi_version) {
2388 case IGMP_VERSION_1:
2389 case IGMP_VERSION_2:
2390 inm->inm_state = IGMP_IDLE_MEMBER;
2391 error = igmp_v1v2_queue_report(inm,
2392 (igi->igi_version == IGMP_VERSION_2) ?
2393 IGMP_v2_HOST_MEMBERSHIP_REPORT :
2394 IGMP_v1_HOST_MEMBERSHIP_REPORT);
2396 inm->inm_timer = IGMP_RANDOM_DELAY(
2397 IGMP_V1V2_MAX_RI * PR_FASTHZ);
2398 V_current_state_timers_running = 1;
2402 case IGMP_VERSION_3:
2404 * Defer update of T0 to T1, until the first copy
2405 * of the state change has been transmitted.
2410 * Immediately enqueue a State-Change Report for
2411 * this interface, freeing any previous reports.
2412 * Don't kick the timers if there is nothing to do,
2413 * or if an error occurred.
2417 retval = igmp_v3_enqueue_group_record(mq, inm, 1,
2419 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2422 error = retval * -1;
2427 * Schedule transmission of pending state-change
2428 * report up to RV times for this link. The timer
2429 * will fire at the next igmp_fasttimo (~200ms),
2430 * giving us an opportunity to merge the reports.
2432 if (igi->igi_flags & IGIF_LOOPBACK) {
2435 KASSERT(igi->igi_rv > 1,
2436 ("%s: invalid robustness %d", __func__,
2438 inm->inm_scrv = igi->igi_rv;
2440 inm->inm_sctimer = 1;
2441 V_state_change_timers_running = 1;
2449 * Only update the T0 state if state change is atomic,
2450 * i.e. we don't need to wait for a timer to fire before we
2451 * can consider the state change to have been communicated.
2455 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2456 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2463 * Issue an intermediate state change during the IGMP life-cycle.
2466 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifsoftc *igi)
2471 CTR4(KTR_IGMPV3, "%s: state change for 0x%08x on ifp %p(%s)", __func__,
2472 ntohl(inm->inm_addr.s_addr), inm->inm_ifp, inm->inm_ifp->if_xname);
2476 IN_MULTI_LOCK_ASSERT();
2479 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2481 if ((ifp->if_flags & IFF_LOOPBACK) ||
2482 (igi->igi_flags & IGIF_SILENT) ||
2483 !igmp_isgroupreported(inm->inm_addr) ||
2484 (igi->igi_version != IGMP_VERSION_3)) {
2485 if (!igmp_isgroupreported(inm->inm_addr)) {
2487 "%s: not kicking state machine for silent group", __func__);
2489 CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2491 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2492 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2496 mbufq_drain(&inm->inm_scq);
2498 retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2499 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2504 * If record(s) were enqueued, start the state-change
2505 * report timer for this group.
2507 inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2508 inm->inm_sctimer = 1;
2509 V_state_change_timers_running = 1;
2515 * Perform the final leave for an IGMP group.
2517 * When leaving a group:
2518 * IGMPv1 does nothing.
2519 * IGMPv2 sends a host leave message, if and only if we are the reporter.
2520 * IGMPv3 enqueues a state-change report containing a transition
2521 * to INCLUDE {} for immediate transmission.
2524 igmp_final_leave(struct in_multi *inm, struct igmp_ifsoftc *igi)
2530 CTR4(KTR_IGMPV3, "%s: final leave 0x%08x on ifp %p(%s)",
2531 __func__, ntohl(inm->inm_addr.s_addr), inm->inm_ifp,
2532 inm->inm_ifp->if_xname);
2534 IN_MULTI_LOCK_ASSERT();
2537 switch (inm->inm_state) {
2538 case IGMP_NOT_MEMBER:
2539 case IGMP_SILENT_MEMBER:
2540 case IGMP_LEAVING_MEMBER:
2541 /* Already leaving or left; do nothing. */
2543 "%s: not kicking state machine for silent group", __func__);
2545 case IGMP_REPORTING_MEMBER:
2546 case IGMP_IDLE_MEMBER:
2547 case IGMP_G_QUERY_PENDING_MEMBER:
2548 case IGMP_SG_QUERY_PENDING_MEMBER:
2549 if (igi->igi_version == IGMP_VERSION_2) {
2551 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2552 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2553 panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2556 igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2557 inm->inm_state = IGMP_NOT_MEMBER;
2558 } else if (igi->igi_version == IGMP_VERSION_3) {
2560 * Stop group timer and all pending reports.
2561 * Immediately enqueue a state-change report
2562 * TO_IN {} to be sent on the next fast timeout,
2563 * giving us an opportunity to merge reports.
2565 mbufq_drain(&inm->inm_scq);
2567 if (igi->igi_flags & IGIF_LOOPBACK) {
2570 inm->inm_scrv = igi->igi_rv;
2572 CTR4(KTR_IGMPV3, "%s: Leaving 0x%08x/%s with %d "
2573 "pending retransmissions.", __func__,
2574 ntohl(inm->inm_addr.s_addr),
2575 inm->inm_ifp->if_xname, inm->inm_scrv);
2576 if (inm->inm_scrv == 0) {
2577 inm->inm_state = IGMP_NOT_MEMBER;
2578 inm->inm_sctimer = 0;
2582 inm_acquire_locked(inm);
2584 retval = igmp_v3_enqueue_group_record(
2585 &inm->inm_scq, inm, 1, 0, 0);
2586 KASSERT(retval != 0,
2587 ("%s: enqueue record = %d", __func__,
2590 inm->inm_state = IGMP_LEAVING_MEMBER;
2591 inm->inm_sctimer = 1;
2592 V_state_change_timers_running = 1;
2598 case IGMP_LAZY_MEMBER:
2599 case IGMP_SLEEPING_MEMBER:
2600 case IGMP_AWAKENING_MEMBER:
2601 /* Our reports are suppressed; do nothing. */
2607 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2608 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2609 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2610 CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for 0x%08x/%s",
2611 __func__, ntohl(inm->inm_addr.s_addr),
2612 inm->inm_ifp->if_xname);
2617 * Enqueue an IGMPv3 group record to the given output queue.
2619 * XXX This function could do with having the allocation code
2620 * split out, and the multiple-tree-walks coalesced into a single
2621 * routine as has been done in igmp_v3_enqueue_filter_change().
2623 * If is_state_change is zero, a current-state record is appended.
2624 * If is_state_change is non-zero, a state-change report is appended.
2626 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2627 * If is_group_query is zero, and if there is a packet with free space
2628 * at the tail of the queue, it will be appended to providing there
2629 * is enough free space.
2630 * Otherwise a new mbuf packet chain is allocated.
2632 * If is_source_query is non-zero, each source is checked to see if
2633 * it was recorded for a Group-Source query, and will be omitted if
2634 * it is not both in-mode and recorded.
2636 * The function will attempt to allocate leading space in the packet
2637 * for the IP/IGMP header to be prepended without fragmenting the chain.
2639 * If successful the size of all data appended to the queue is returned,
2640 * otherwise an error code less than zero is returned, or zero if
2641 * no record(s) were appended.
2644 igmp_v3_enqueue_group_record(struct mbufq *mq, struct in_multi *inm,
2645 const int is_state_change, const int is_group_query,
2646 const int is_source_query)
2648 struct igmp_grouprec ig;
2649 struct igmp_grouprec *pig;
2651 struct ip_msource *ims, *nims;
2652 struct mbuf *m0, *m, *md;
2653 int error, is_filter_list_change;
2654 int minrec0len, m0srcs, msrcs, nbytes, off;
2655 int record_has_sources;
2661 IN_MULTI_LOCK_ASSERT();
2665 is_filter_list_change = 0;
2672 record_has_sources = 1;
2674 type = IGMP_DO_NOTHING;
2675 mode = inm->inm_st[1].iss_fmode;
2678 * If we did not transition out of ASM mode during t0->t1,
2679 * and there are no source nodes to process, we can skip
2680 * the generation of source records.
2682 if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2684 record_has_sources = 0;
2686 if (is_state_change) {
2688 * Queue a state change record.
2689 * If the mode did not change, and there are non-ASM
2690 * listeners or source filters present,
2691 * we potentially need to issue two records for the group.
2692 * If we are transitioning to MCAST_UNDEFINED, we need
2693 * not send any sources.
2694 * If there are ASM listeners, and there was no filter
2695 * mode transition of any kind, do nothing.
2697 if (mode != inm->inm_st[0].iss_fmode) {
2698 if (mode == MCAST_EXCLUDE) {
2699 CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2701 type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2703 CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2705 type = IGMP_CHANGE_TO_INCLUDE_MODE;
2706 if (mode == MCAST_UNDEFINED)
2707 record_has_sources = 0;
2710 if (record_has_sources) {
2711 is_filter_list_change = 1;
2713 type = IGMP_DO_NOTHING;
2718 * Queue a current state record.
2720 if (mode == MCAST_EXCLUDE) {
2721 type = IGMP_MODE_IS_EXCLUDE;
2722 } else if (mode == MCAST_INCLUDE) {
2723 type = IGMP_MODE_IS_INCLUDE;
2724 KASSERT(inm->inm_st[1].iss_asm == 0,
2725 ("%s: inm %p is INCLUDE but ASM count is %d",
2726 __func__, inm, inm->inm_st[1].iss_asm));
2731 * Generate the filter list changes using a separate function.
2733 if (is_filter_list_change)
2734 return (igmp_v3_enqueue_filter_change(mq, inm));
2736 if (type == IGMP_DO_NOTHING) {
2737 CTR3(KTR_IGMPV3, "%s: nothing to do for 0x%08x/%s", __func__,
2738 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2743 * If any sources are present, we must be able to fit at least
2744 * one in the trailing space of the tail packet's mbuf,
2747 minrec0len = sizeof(struct igmp_grouprec);
2748 if (record_has_sources)
2749 minrec0len += sizeof(in_addr_t);
2751 CTR4(KTR_IGMPV3, "%s: queueing %s for 0x%08x/%s", __func__,
2752 igmp_rec_type_to_str(type), ntohl(inm->inm_addr.s_addr),
2753 inm->inm_ifp->if_xname);
2756 * Check if we have a packet in the tail of the queue for this
2757 * group into which the first group record for this group will fit.
2758 * Otherwise allocate a new packet.
2759 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2760 * Note: Group records for G/GSR query responses MUST be sent
2761 * in their own packet.
2763 m0 = mbufq_last(mq);
2764 if (!is_group_query &&
2766 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2767 (m0->m_pkthdr.len + minrec0len) <
2768 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2769 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2770 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2772 CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2774 if (mbufq_full(mq)) {
2775 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2779 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2780 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2781 if (!is_state_change && !is_group_query) {
2782 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2784 m->m_data += IGMP_LEADINGSPACE;
2787 m = m_gethdr(M_NOWAIT, MT_DATA);
2789 M_ALIGN(m, IGMP_LEADINGSPACE);
2794 igmp_save_context(m, ifp);
2796 CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2800 * Append group record.
2801 * If we have sources, we don't know how many yet.
2806 ig.ig_group = inm->inm_addr;
2807 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2810 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2813 nbytes += sizeof(struct igmp_grouprec);
2816 * Append as many sources as will fit in the first packet.
2817 * If we are appending to a new packet, the chain allocation
2818 * may potentially use clusters; use m_getptr() in this case.
2819 * If we are appending to an existing packet, we need to obtain
2820 * a pointer to the group record after m_append(), in case a new
2821 * mbuf was allocated.
2822 * Only append sources which are in-mode at t1. If we are
2823 * transitioning to MCAST_UNDEFINED state on the group, do not
2824 * include source entries.
2825 * Only report recorded sources in our filter set when responding
2826 * to a group-source query.
2828 if (record_has_sources) {
2831 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2832 md->m_len - nbytes);
2834 md = m_getptr(m, 0, &off);
2835 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2839 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2840 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2842 now = ims_get_mode(inm, ims, 1);
2843 CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2844 if ((now != mode) ||
2845 (now == mode && mode == MCAST_UNDEFINED)) {
2846 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2849 if (is_source_query && ims->ims_stp == 0) {
2850 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2854 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2855 naddr = htonl(ims->ims_haddr);
2856 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2859 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2863 nbytes += sizeof(in_addr_t);
2865 if (msrcs == m0srcs)
2868 CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2870 pig->ig_numsrc = htons(msrcs);
2871 nbytes += (msrcs * sizeof(in_addr_t));
2874 if (is_source_query && msrcs == 0) {
2875 CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2882 * We are good to go with first packet.
2885 CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2886 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2887 mbufq_enqueue(mq, m);
2889 m->m_pkthdr.PH_vt.vt_nrecs++;
2892 * No further work needed if no source list in packet(s).
2894 if (!record_has_sources)
2898 * Whilst sources remain to be announced, we need to allocate
2899 * a new packet and fill out as many sources as will fit.
2900 * Always try for a cluster first.
2902 while (nims != NULL) {
2903 if (mbufq_full(mq)) {
2904 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2907 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2909 m->m_data += IGMP_LEADINGSPACE;
2911 m = m_gethdr(M_NOWAIT, MT_DATA);
2913 M_ALIGN(m, IGMP_LEADINGSPACE);
2917 igmp_save_context(m, ifp);
2918 md = m_getptr(m, 0, &off);
2919 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2920 CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2922 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2925 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2928 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2929 nbytes += sizeof(struct igmp_grouprec);
2931 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2932 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2935 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2936 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2938 now = ims_get_mode(inm, ims, 1);
2939 if ((now != mode) ||
2940 (now == mode && mode == MCAST_UNDEFINED)) {
2941 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2944 if (is_source_query && ims->ims_stp == 0) {
2945 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2949 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2950 naddr = htonl(ims->ims_haddr);
2951 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2954 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2959 if (msrcs == m0srcs)
2962 pig->ig_numsrc = htons(msrcs);
2963 nbytes += (msrcs * sizeof(in_addr_t));
2965 CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
2966 mbufq_enqueue(mq, m);
2973 * Type used to mark record pass completion.
2974 * We exploit the fact we can cast to this easily from the
2975 * current filter modes on each ip_msource node.
2978 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2979 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2980 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2981 REC_FULL = REC_ALLOW | REC_BLOCK
2985 * Enqueue an IGMPv3 filter list change to the given output queue.
2987 * Source list filter state is held in an RB-tree. When the filter list
2988 * for a group is changed without changing its mode, we need to compute
2989 * the deltas between T0 and T1 for each source in the filter set,
2990 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2992 * As we may potentially queue two record types, and the entire R-B tree
2993 * needs to be walked at once, we break this out into its own function
2994 * so we can generate a tightly packed queue of packets.
2996 * XXX This could be written to only use one tree walk, although that makes
2997 * serializing into the mbuf chains a bit harder. For now we do two walks
2998 * which makes things easier on us, and it may or may not be harder on
3001 * If successful the size of all data appended to the queue is returned,
3002 * otherwise an error code less than zero is returned, or zero if
3003 * no record(s) were appended.
3006 igmp_v3_enqueue_filter_change(struct mbufq *mq, struct in_multi *inm)
3008 static const int MINRECLEN =
3009 sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3011 struct igmp_grouprec ig;
3012 struct igmp_grouprec *pig;
3013 struct ip_msource *ims, *nims;
3014 struct mbuf *m, *m0, *md;
3016 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3018 uint8_t mode, now, then;
3019 rectype_t crt, drt, nrt;
3021 IN_MULTI_LOCK_ASSERT();
3023 if (inm->inm_nsrc == 0 ||
3024 (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3027 ifp = inm->inm_ifp; /* interface */
3028 mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */
3029 crt = REC_NONE; /* current group record type */
3030 drt = REC_NONE; /* mask of completed group record types */
3031 nrt = REC_NONE; /* record type for current node */
3032 m0srcs = 0; /* # source which will fit in current mbuf chain */
3033 nbytes = 0; /* # of bytes appended to group's state-change queue */
3034 npbytes = 0; /* # of bytes appended this packet */
3035 rsrcs = 0; /* # sources encoded in current record */
3036 schanged = 0; /* # nodes encoded in overall filter change */
3037 nallow = 0; /* # of source entries in ALLOW_NEW */
3038 nblock = 0; /* # of source entries in BLOCK_OLD */
3039 nims = NULL; /* next tree node pointer */
3042 * For each possible filter record mode.
3043 * The first kind of source we encounter tells us which
3044 * is the first kind of record we start appending.
3045 * If a node transitioned to UNDEFINED at t1, its mode is treated
3046 * as the inverse of the group's filter mode.
3048 while (drt != REC_FULL) {
3050 m0 = mbufq_last(mq);
3052 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
3053 IGMP_V3_REPORT_MAXRECS) &&
3054 (m0->m_pkthdr.len + MINRECLEN) <
3055 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3057 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3058 sizeof(struct igmp_grouprec)) /
3061 "%s: use previous packet", __func__);
3063 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3065 m->m_data += IGMP_LEADINGSPACE;
3067 m = m_gethdr(M_NOWAIT, MT_DATA);
3069 M_ALIGN(m, IGMP_LEADINGSPACE);
3073 "%s: m_get*() failed", __func__);
3076 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3077 igmp_save_context(m, ifp);
3078 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3079 sizeof(struct igmp_grouprec)) /
3083 "%s: allocated new packet", __func__);
3086 * Append the IGMP group record header to the
3087 * current packet's data area.
3088 * Recalculate pointer to free space for next
3089 * group record, in case m_append() allocated
3090 * a new mbuf or cluster.
3092 memset(&ig, 0, sizeof(ig));
3093 ig.ig_group = inm->inm_addr;
3094 if (!m_append(m, sizeof(ig), (void *)&ig)) {
3098 "%s: m_append() failed", __func__);
3101 npbytes += sizeof(struct igmp_grouprec);
3103 /* new packet; offset in c hain */
3104 md = m_getptr(m, npbytes -
3105 sizeof(struct igmp_grouprec), &off);
3106 pig = (struct igmp_grouprec *)(mtod(md,
3109 /* current packet; offset from last append */
3111 pig = (struct igmp_grouprec *)(mtod(md,
3112 uint8_t *) + md->m_len -
3113 sizeof(struct igmp_grouprec));
3116 * Begin walking the tree for this record type
3117 * pass, or continue from where we left off
3118 * previously if we had to allocate a new packet.
3119 * Only report deltas in-mode at t1.
3120 * We need not report included sources as allowed
3121 * if we are in inclusive mode on the group,
3122 * however the converse is not true.
3126 nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3127 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3128 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x",
3129 __func__, ims->ims_haddr);
3130 now = ims_get_mode(inm, ims, 1);
3131 then = ims_get_mode(inm, ims, 0);
3132 CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3133 __func__, then, now);
3136 "%s: skip unchanged", __func__);
3139 if (mode == MCAST_EXCLUDE &&
3140 now == MCAST_INCLUDE) {
3142 "%s: skip IN src on EX group",
3146 nrt = (rectype_t)now;
3147 if (nrt == REC_NONE)
3148 nrt = (rectype_t)(~mode & REC_FULL);
3149 if (schanged++ == 0) {
3151 } else if (crt != nrt)
3153 naddr = htonl(ims->ims_haddr);
3154 if (!m_append(m, sizeof(in_addr_t),
3159 "%s: m_append() failed", __func__);
3162 nallow += !!(crt == REC_ALLOW);
3163 nblock += !!(crt == REC_BLOCK);
3164 if (++rsrcs == m0srcs)
3168 * If we did not append any tree nodes on this
3169 * pass, back out of allocations.
3172 npbytes -= sizeof(struct igmp_grouprec);
3175 "%s: m_free(m)", __func__);
3179 "%s: m_adj(m, -ig)", __func__);
3180 m_adj(m, -((int)sizeof(
3181 struct igmp_grouprec)));
3185 npbytes += (rsrcs * sizeof(in_addr_t));
3186 if (crt == REC_ALLOW)
3187 pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3188 else if (crt == REC_BLOCK)
3189 pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3190 pig->ig_numsrc = htons(rsrcs);
3192 * Count the new group record, and enqueue this
3193 * packet if it wasn't already queued.
3195 m->m_pkthdr.PH_vt.vt_nrecs++;
3197 mbufq_enqueue(mq, m);
3199 } while (nims != NULL);
3201 crt = (~crt & REC_FULL);
3204 CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3211 igmp_v3_merge_state_changes(struct in_multi *inm, struct mbufq *scq)
3214 struct mbuf *m; /* pending state-change */
3215 struct mbuf *m0; /* copy of pending state-change */
3216 struct mbuf *mt; /* last state-change in packet */
3217 int docopy, domerge;
3224 IN_MULTI_LOCK_ASSERT();
3228 * If there are further pending retransmissions, make a writable
3229 * copy of each queued state-change message before merging.
3231 if (inm->inm_scrv > 0)
3236 if (mbufq_first(gq) == NULL) {
3237 CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3242 m = mbufq_first(gq);
3245 * Only merge the report into the current packet if
3246 * there is sufficient space to do so; an IGMPv3 report
3247 * packet may only contain 65,535 group records.
3248 * Always use a simple mbuf chain concatentation to do this,
3249 * as large state changes for single groups may have
3250 * allocated clusters.
3253 mt = mbufq_last(scq);
3255 recslen = m_length(m, NULL);
3257 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
3258 m->m_pkthdr.PH_vt.vt_nrecs <=
3259 IGMP_V3_REPORT_MAXRECS) &&
3260 (mt->m_pkthdr.len + recslen <=
3261 (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3265 if (!domerge && mbufq_full(gq)) {
3267 "%s: outbound queue full, skipping whole packet %p",
3277 CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3278 m0 = mbufq_dequeue(gq);
3281 CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3282 m0 = m_dup(m, M_NOWAIT);
3285 m0->m_nextpkt = NULL;
3290 CTR3(KTR_IGMPV3, "%s: queueing %p to scq %p)",
3292 mbufq_enqueue(scq, m0);
3294 struct mbuf *mtl; /* last mbuf of packet mt */
3296 CTR3(KTR_IGMPV3, "%s: merging %p with scq tail %p)",
3300 m0->m_flags &= ~M_PKTHDR;
3301 mt->m_pkthdr.len += recslen;
3302 mt->m_pkthdr.PH_vt.vt_nrecs +=
3303 m0->m_pkthdr.PH_vt.vt_nrecs;
3313 * Respond to a pending IGMPv3 General Query.
3316 igmp_v3_dispatch_general_query(struct igmp_ifsoftc *igi)
3318 struct ifmultiaddr *ifma;
3320 struct in_multi *inm;
3323 IN_MULTI_LOCK_ASSERT();
3326 KASSERT(igi->igi_version == IGMP_VERSION_3,
3327 ("%s: called when version %d", __func__, igi->igi_version));
3330 * Check that there are some packets queued. If so, send them first.
3331 * For large number of groups the reply to general query can take
3332 * many packets, we should finish sending them before starting of
3333 * queuing the new reply.
3335 if (mbufq_len(&igi->igi_gq) != 0)
3341 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3342 if (ifma->ifma_addr->sa_family != AF_INET ||
3343 ifma->ifma_protospec == NULL)
3346 inm = (struct in_multi *)ifma->ifma_protospec;
3347 KASSERT(ifp == inm->inm_ifp,
3348 ("%s: inconsistent ifp", __func__));
3350 switch (inm->inm_state) {
3351 case IGMP_NOT_MEMBER:
3352 case IGMP_SILENT_MEMBER:
3354 case IGMP_REPORTING_MEMBER:
3355 case IGMP_IDLE_MEMBER:
3356 case IGMP_LAZY_MEMBER:
3357 case IGMP_SLEEPING_MEMBER:
3358 case IGMP_AWAKENING_MEMBER:
3359 inm->inm_state = IGMP_REPORTING_MEMBER;
3360 retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3362 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3365 case IGMP_G_QUERY_PENDING_MEMBER:
3366 case IGMP_SG_QUERY_PENDING_MEMBER:
3367 case IGMP_LEAVING_MEMBER:
3371 IF_ADDR_RUNLOCK(ifp);
3374 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3375 igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3378 * Slew transmission of bursts over 500ms intervals.
3380 if (mbufq_first(&igi->igi_gq) != NULL) {
3381 igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3382 IGMP_RESPONSE_BURST_INTERVAL);
3383 V_interface_timers_running = 1;
3388 * Transmit the next pending IGMP message in the output queue.
3390 * We get called from netisr_processqueue(). A mutex private to igmpoq
3391 * will be acquired and released around this routine.
3393 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3394 * MRT: Nothing needs to be done, as IGMP traffic is always local to
3395 * a link and uses a link-scope multicast address.
3398 igmp_intr(struct mbuf *m)
3400 struct ip_moptions imo;
3402 struct mbuf *ipopts, *m0;
3406 CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3409 * Set VNET image pointer from enqueued mbuf chain
3410 * before doing anything else. Whilst we use interface
3411 * indexes to guard against interface detach, they are
3412 * unique to each VIMAGE and must be retrieved.
3414 CURVNET_SET((struct vnet *)(m->m_pkthdr.PH_loc.ptr));
3415 ifindex = igmp_restore_context(m);
3418 * Check if the ifnet still exists. This limits the scope of
3419 * any race in the absence of a global ifp lock for low cost
3420 * (an array lookup).
3422 ifp = ifnet_byindex(ifindex);
3424 CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3425 __func__, m, ifindex);
3427 IPSTAT_INC(ips_noroute);
3431 ipopts = V_igmp_sendra ? m_raopt : NULL;
3433 imo.imo_multicast_ttl = 1;
3434 imo.imo_multicast_vif = -1;
3435 imo.imo_multicast_loop = (V_ip_mrouter != NULL);
3438 * If the user requested that IGMP traffic be explicitly
3439 * redirected to the loopback interface (e.g. they are running a
3440 * MANET interface and the routing protocol needs to see the
3441 * updates), handle this now.
3443 if (m->m_flags & M_IGMP_LOOP)
3444 imo.imo_multicast_ifp = V_loif;
3446 imo.imo_multicast_ifp = ifp;
3448 if (m->m_flags & M_IGMPV2) {
3451 m0 = igmp_v3_encap_report(ifp, m);
3453 CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3455 IPSTAT_INC(ips_odropped);
3460 igmp_scrub_context(m0);
3462 m0->m_pkthdr.rcvif = V_loif;
3464 mac_netinet_igmp_send(ifp, m0);
3466 error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3468 CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3472 IGMPSTAT_INC(igps_snd_reports);
3476 * We must restore the existing vnet pointer before
3477 * continuing as we are run from netisr context.
3483 * Encapsulate an IGMPv3 report.
3485 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3486 * chain has already had its IP/IGMPv3 header prepended. In this case
3487 * the function will not attempt to prepend; the lengths and checksums
3488 * will however be re-computed.
3490 * Returns a pointer to the new mbuf chain head, or NULL if the
3491 * allocation failed.
3493 static struct mbuf *
3494 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3496 struct rm_priotracker in_ifa_tracker;
3497 struct igmp_report *igmp;
3499 int hdrlen, igmpreclen;
3501 KASSERT((m->m_flags & M_PKTHDR),
3502 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3504 igmpreclen = m_length(m, NULL);
3505 hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3507 if (m->m_flags & M_IGMPV3_HDR) {
3508 igmpreclen -= hdrlen;
3510 M_PREPEND(m, hdrlen, M_NOWAIT);
3513 m->m_flags |= M_IGMPV3_HDR;
3516 CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3518 m->m_data += sizeof(struct ip);
3519 m->m_len -= sizeof(struct ip);
3521 igmp = mtod(m, struct igmp_report *);
3522 igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3525 igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3527 igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3528 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3530 m->m_data -= sizeof(struct ip);
3531 m->m_len += sizeof(struct ip);
3533 ip = mtod(m, struct ip *);
3534 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3535 ip->ip_len = htons(hdrlen + igmpreclen);
3536 ip->ip_off = htons(IP_DF);
3537 ip->ip_p = IPPROTO_IGMP;
3540 ip->ip_src.s_addr = INADDR_ANY;
3542 if (m->m_flags & M_IGMP_LOOP) {
3543 struct in_ifaddr *ia;
3545 IFP_TO_IA(ifp, ia, &in_ifa_tracker);
3547 ip->ip_src = ia->ia_addr.sin_addr;
3548 ifa_free(&ia->ia_ifa);
3552 ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3559 igmp_rec_type_to_str(const int type)
3563 case IGMP_CHANGE_TO_EXCLUDE_MODE:
3566 case IGMP_CHANGE_TO_INCLUDE_MODE:
3569 case IGMP_MODE_IS_EXCLUDE:
3572 case IGMP_MODE_IS_INCLUDE:
3575 case IGMP_ALLOW_NEW_SOURCES:
3578 case IGMP_BLOCK_OLD_SOURCES:
3590 vnet_igmp_init(const void *unused __unused)
3593 netisr_register_vnet(&igmp_nh);
3595 VNET_SYSINIT(vnet_igmp_init, SI_SUB_PROTO_MC, SI_ORDER_ANY,
3596 vnet_igmp_init, NULL);
3599 vnet_igmp_uninit(const void *unused __unused)
3602 /* This can happen when we shutdown the entire network stack. */
3603 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3605 netisr_unregister_vnet(&igmp_nh);
3607 VNET_SYSUNINIT(vnet_igmp_uninit, SI_SUB_PROTO_MC, SI_ORDER_ANY,
3608 vnet_igmp_uninit, NULL);
3612 DB_SHOW_COMMAND(igi_list, db_show_igi_list)
3614 struct igmp_ifsoftc *igi, *tigi;
3615 LIST_HEAD(_igi_list, igmp_ifsoftc) *igi_head;
3618 db_printf("usage: show igi_list <addr>\n");
3621 igi_head = (struct _igi_list *)addr;
3623 LIST_FOREACH_SAFE(igi, igi_head, igi_link, tigi) {
3624 db_printf("igmp_ifsoftc %p:\n", igi);
3625 db_printf(" ifp %p\n", igi->igi_ifp);
3626 db_printf(" version %u\n", igi->igi_version);
3627 db_printf(" v1_timer %u\n", igi->igi_v1_timer);
3628 db_printf(" v2_timer %u\n", igi->igi_v2_timer);
3629 db_printf(" v3_timer %u\n", igi->igi_v3_timer);
3630 db_printf(" flags %#x\n", igi->igi_flags);
3631 db_printf(" rv %u\n", igi->igi_rv);
3632 db_printf(" qi %u\n", igi->igi_qi);
3633 db_printf(" qri %u\n", igi->igi_qri);
3634 db_printf(" uri %u\n", igi->igi_uri);
3635 /* SLIST_HEAD(,in_multi) igi_relinmhead */
3636 /* struct mbufq igi_gq; */
3643 igmp_modevent(module_t mod, int type, void *unused __unused)
3648 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3650 m_raopt = igmp_ra_alloc();
3651 netisr_register(&igmp_nh);
3654 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3655 netisr_unregister(&igmp_nh);
3658 IGMP_LOCK_DESTROY();
3661 return (EOPNOTSUPP);
3666 static moduledata_t igmp_mod = {
3671 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE);