2 * Copyright (c) 2007-2009 Bruce Simpson.
3 * Copyright (c) 1988 Stephen Deering.
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Stephen Deering of Stanford University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
38 * Internet Group Management Protocol (IGMP) routines.
39 * [RFC1112, RFC2236, RFC3376]
41 * Written by Steve Deering, Stanford, May 1988.
42 * Modified by Rosen Sharma, Stanford, Aug 1994.
43 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
44 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
45 * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson.
47 * MULTICAST Revision: 3.5.1.4
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/module.h>
56 #include <sys/malloc.h>
58 #include <sys/socket.h>
59 #include <sys/protosw.h>
60 #include <sys/kernel.h>
61 #include <sys/sysctl.h>
63 #include <sys/condvar.h>
66 #include <net/if_var.h>
67 #include <net/netisr.h>
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_options.h>
76 #include <netinet/igmp.h>
77 #include <netinet/igmp_var.h>
79 #include <machine/in_cksum.h>
81 #include <security/mac/mac_framework.h>
84 #define KTR_IGMPV3 KTR_INET
87 static struct igmp_ifinfo *
88 igi_alloc_locked(struct ifnet *);
89 static void igi_delete_locked(const struct ifnet *);
90 static void igmp_dispatch_queue(struct mbufq *, int, const int);
91 static void igmp_fasttimo_vnet(void);
92 static void igmp_final_leave(struct in_multi *, struct igmp_ifinfo *);
93 static int igmp_handle_state_change(struct in_multi *,
94 struct igmp_ifinfo *);
95 static int igmp_initial_join(struct in_multi *, struct igmp_ifinfo *);
96 static int igmp_input_v1_query(struct ifnet *, const struct ip *,
98 static int igmp_input_v2_query(struct ifnet *, const struct ip *,
100 static int igmp_input_v3_query(struct ifnet *, const struct ip *,
101 /*const*/ struct igmpv3 *);
102 static int igmp_input_v3_group_query(struct in_multi *,
103 struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *);
104 static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
105 /*const*/ struct igmp *);
106 static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
107 /*const*/ struct igmp *);
108 static void igmp_intr(struct mbuf *);
109 static int igmp_isgroupreported(const struct in_addr);
113 static char * igmp_rec_type_to_str(const int);
115 static void igmp_set_version(struct igmp_ifinfo *, const int);
116 static void igmp_slowtimo_vnet(void);
117 static int igmp_v1v2_queue_report(struct in_multi *, const int);
118 static void igmp_v1v2_process_group_timer(struct in_multi *, const int);
119 static void igmp_v1v2_process_querier_timers(struct igmp_ifinfo *);
120 static void igmp_v2_update_group(struct in_multi *, const int);
121 static void igmp_v3_cancel_link_timers(struct igmp_ifinfo *);
122 static void igmp_v3_dispatch_general_query(struct igmp_ifinfo *);
124 igmp_v3_encap_report(struct ifnet *, struct mbuf *);
125 static int igmp_v3_enqueue_group_record(struct mbufq *,
126 struct in_multi *, const int, const int, const int);
127 static int igmp_v3_enqueue_filter_change(struct mbufq *,
129 static void igmp_v3_process_group_timers(struct igmp_ifinfo *,
130 struct mbufq *, struct mbufq *, struct in_multi *,
132 static int igmp_v3_merge_state_changes(struct in_multi *,
134 static void igmp_v3_suppress_group_record(struct in_multi *);
135 static int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
136 static int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
137 static int sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
139 static const struct netisr_handler igmp_nh = {
141 .nh_handler = igmp_intr,
142 .nh_proto = NETISR_IGMP,
143 .nh_policy = NETISR_POLICY_SOURCE,
147 * System-wide globals.
149 * Unlocked access to these is OK, except for the global IGMP output
150 * queue. The IGMP subsystem lock ends up being system-wide for the moment,
151 * because all VIMAGEs have to share a global output queue, as netisrs
152 * themselves are not virtualized.
155 * * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
156 * Any may be taken independently; if any are held at the same
157 * time, the above lock order must be followed.
158 * * All output is delegated to the netisr.
159 * Now that Giant has been eliminated, the netisr may be inlined.
160 * * IN_MULTI_LOCK covers in_multi.
161 * * IGMP_LOCK covers igmp_ifinfo and any global variables in this file,
162 * including the output queue.
163 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
164 * per-link state iterators.
165 * * igmp_ifinfo is valid as long as PF_INET is attached to the interface,
166 * therefore it is not refcounted.
167 * We allow unlocked reads of igmp_ifinfo when accessed via in_multi.
170 * * IGMP acquires its own reference every time an in_multi is passed to
171 * it and the group is being joined for the first time.
172 * * IGMP releases its reference(s) on in_multi in a deferred way,
173 * because the operations which process the release run as part of
174 * a loop whose control variables are directly affected by the release
175 * (that, and not recursing on the IF_ADDR_LOCK).
177 * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
178 * to a vnet in ifp->if_vnet.
180 * SMPng: XXX We may potentially race operations on ifma_protospec.
181 * The problem is that we currently lack a clean way of taking the
182 * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
183 * as anything which modifies ifma needs to be covered by that lock.
184 * So check for ifma_protospec being NULL before proceeding.
188 struct mbuf *m_raopt; /* Router Alert option */
189 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
192 * VIMAGE-wide globals.
194 * The IGMPv3 timers themselves need to run per-image, however,
195 * protosw timers run globally (see tcp).
196 * An ifnet can only be in one vimage at a time, and the loopback
197 * ifnet, loif, is itself virtualized.
198 * It would otherwise be possible to seriously hose IGMP state,
199 * and create inconsistencies in upstream multicast routing, if you have
200 * multiple VIMAGEs running on the same link joining different multicast
201 * groups, UNLESS the "primary IP address" is different. This is because
202 * IGMP for IPv4 does not force link-local addresses to be used for each
203 * node, unlike MLD for IPv6.
204 * Obviously the IGMPv3 per-interface state has per-vimage granularity
207 * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
208 * policy to control the address used by IGMP on the link.
210 static VNET_DEFINE(int, interface_timers_running); /* IGMPv3 general
212 static VNET_DEFINE(int, state_change_timers_running); /* IGMPv3 state-change
214 static VNET_DEFINE(int, current_state_timers_running); /* IGMPv1/v2 host
215 * report; IGMPv3 g/sg
218 #define V_interface_timers_running VNET(interface_timers_running)
219 #define V_state_change_timers_running VNET(state_change_timers_running)
220 #define V_current_state_timers_running VNET(current_state_timers_running)
222 static VNET_DEFINE(LIST_HEAD(, igmp_ifinfo), igi_head);
223 static VNET_DEFINE(struct igmpstat, igmpstat) = {
224 .igps_version = IGPS_VERSION_3,
225 .igps_len = sizeof(struct igmpstat),
227 static VNET_DEFINE(struct timeval, igmp_gsrdelay) = {10, 0};
229 #define V_igi_head VNET(igi_head)
230 #define V_igmpstat VNET(igmpstat)
231 #define V_igmp_gsrdelay VNET(igmp_gsrdelay)
233 static VNET_DEFINE(int, igmp_recvifkludge) = 1;
234 static VNET_DEFINE(int, igmp_sendra) = 1;
235 static VNET_DEFINE(int, igmp_sendlocal) = 1;
236 static VNET_DEFINE(int, igmp_v1enable) = 1;
237 static VNET_DEFINE(int, igmp_v2enable) = 1;
238 static VNET_DEFINE(int, igmp_legacysupp);
239 static VNET_DEFINE(int, igmp_default_version) = IGMP_VERSION_3;
241 #define V_igmp_recvifkludge VNET(igmp_recvifkludge)
242 #define V_igmp_sendra VNET(igmp_sendra)
243 #define V_igmp_sendlocal VNET(igmp_sendlocal)
244 #define V_igmp_v1enable VNET(igmp_v1enable)
245 #define V_igmp_v2enable VNET(igmp_v2enable)
246 #define V_igmp_legacysupp VNET(igmp_legacysupp)
247 #define V_igmp_default_version VNET(igmp_default_version)
250 * Virtualized sysctls.
252 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_VNET | CTLFLAG_RW,
253 &VNET_NAME(igmpstat), igmpstat, "");
254 SYSCTL_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_VNET | CTLFLAG_RW,
255 &VNET_NAME(igmp_recvifkludge), 0,
256 "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
257 SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_VNET | CTLFLAG_RW,
258 &VNET_NAME(igmp_sendra), 0,
259 "Send IP Router Alert option in IGMPv2/v3 messages");
260 SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_VNET | CTLFLAG_RW,
261 &VNET_NAME(igmp_sendlocal), 0,
262 "Send IGMP membership reports for 224.0.0.0/24 groups");
263 SYSCTL_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_VNET | CTLFLAG_RW,
264 &VNET_NAME(igmp_v1enable), 0,
265 "Enable backwards compatibility with IGMPv1");
266 SYSCTL_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_VNET | CTLFLAG_RW,
267 &VNET_NAME(igmp_v2enable), 0,
268 "Enable backwards compatibility with IGMPv2");
269 SYSCTL_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_VNET | CTLFLAG_RW,
270 &VNET_NAME(igmp_legacysupp), 0,
271 "Allow v1/v2 reports to suppress v3 group responses");
272 SYSCTL_PROC(_net_inet_igmp, OID_AUTO, default_version,
273 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
274 &VNET_NAME(igmp_default_version), 0, sysctl_igmp_default_version, "I",
275 "Default version of IGMP to run on each interface");
276 SYSCTL_PROC(_net_inet_igmp, OID_AUTO, gsrdelay,
277 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
278 &VNET_NAME(igmp_gsrdelay.tv_sec), 0, sysctl_igmp_gsr, "I",
279 "Rate limit for IGMPv3 Group-and-Source queries in seconds");
282 * Non-virtualized sysctls.
284 static SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo,
285 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_igmp_ifinfo,
286 "Per-interface IGMPv3 state");
289 igmp_save_context(struct mbuf *m, struct ifnet *ifp)
293 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
295 m->m_pkthdr.flowid = ifp->if_index;
299 igmp_scrub_context(struct mbuf *m)
302 m->m_pkthdr.PH_loc.ptr = NULL;
303 m->m_pkthdr.flowid = 0;
307 static __inline char *
308 inet_ntoa_haddr(in_addr_t haddr)
312 ia.s_addr = htonl(haddr);
313 return (inet_ntoa(ia));
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_ifinfo 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_ifinfo *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 error = SYSCTL_OUT(req, igi,
461 sizeof(struct igmp_ifinfo));
473 * Dispatch an entire queue of pending packet chains
475 * VIMAGE: Assumes the vnet pointer has been set.
478 igmp_dispatch_queue(struct mbufq *mq, int limit, const int loop)
482 while ((m = mbufq_dequeue(mq)) != NULL) {
483 CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, mq, m);
485 m->m_flags |= M_IGMP_LOOP;
486 netisr_dispatch(NETISR_IGMP, m);
493 * Filter outgoing IGMP report state by group.
495 * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
496 * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
497 * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
498 * this may break certain IGMP snooping switches which rely on the old
501 * Return zero if the given group is one for which IGMP reports
502 * should be suppressed, or non-zero if reports should be issued.
505 igmp_isgroupreported(const struct in_addr addr)
508 if (in_allhosts(addr) ||
509 ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
516 * Construct a Router Alert option to use in outgoing packets.
524 m = m_get(M_WAITOK, MT_DATA);
525 p = mtod(m, struct ipoption *);
526 p->ipopt_dst.s_addr = INADDR_ANY;
527 p->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
528 p->ipopt_list[1] = 0x04; /* 4 bytes long */
529 p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */
530 p->ipopt_list[3] = 0x00; /* pad byte */
531 m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
537 * Attach IGMP when PF_INET is attached to an interface.
540 igmp_domifattach(struct ifnet *ifp)
542 struct igmp_ifinfo *igi;
544 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
545 __func__, ifp, ifp->if_xname);
549 igi = igi_alloc_locked(ifp);
550 if (!(ifp->if_flags & IFF_MULTICAST))
551 igi->igi_flags |= IGIF_SILENT;
559 * VIMAGE: assume curvnet set by caller.
561 static struct igmp_ifinfo *
562 igi_alloc_locked(/*const*/ struct ifnet *ifp)
564 struct igmp_ifinfo *igi;
568 igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO);
573 igi->igi_version = V_igmp_default_version;
575 igi->igi_rv = IGMP_RV_INIT;
576 igi->igi_qi = IGMP_QI_INIT;
577 igi->igi_qri = IGMP_QRI_INIT;
578 igi->igi_uri = IGMP_URI_INIT;
579 SLIST_INIT(&igi->igi_relinmhead);
580 mbufq_init(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
582 LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
584 CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)",
594 * NOTE: Some finalization tasks need to run before the protocol domain
595 * is detached, but also before the link layer does its cleanup.
597 * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
598 * XXX This is also bitten by unlocked ifma_protospec access.
601 igmp_ifdetach(struct ifnet *ifp)
603 struct igmp_ifinfo *igi;
604 struct ifmultiaddr *ifma;
605 struct in_multi *inm, *tinm;
607 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
612 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
613 if (igi->igi_version == IGMP_VERSION_3) {
615 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
616 if (ifma->ifma_addr->sa_family != AF_INET ||
617 ifma->ifma_protospec == NULL)
620 KASSERT(ifma->ifma_protospec != NULL,
621 ("%s: ifma_protospec is NULL", __func__));
623 inm = (struct in_multi *)ifma->ifma_protospec;
624 if (inm->inm_state == IGMP_LEAVING_MEMBER) {
625 SLIST_INSERT_HEAD(&igi->igi_relinmhead,
628 inm_clear_recorded(inm);
630 IF_ADDR_RUNLOCK(ifp);
632 * Free the in_multi reference(s) for this IGMP lifecycle.
634 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele,
636 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
637 inm_release_locked(inm);
645 * Hook for domifdetach.
648 igmp_domifdetach(struct ifnet *ifp)
650 struct igmp_ifinfo *igi;
652 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
653 __func__, ifp, ifp->if_xname);
657 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
658 igi_delete_locked(ifp);
664 igi_delete_locked(const struct ifnet *ifp)
666 struct igmp_ifinfo *igi, *tigi;
668 CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)",
669 __func__, ifp, ifp->if_xname);
673 LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
674 if (igi->igi_ifp == ifp) {
676 * Free deferred General Query responses.
678 mbufq_drain(&igi->igi_gq);
680 LIST_REMOVE(igi, igi_link);
682 KASSERT(SLIST_EMPTY(&igi->igi_relinmhead),
683 ("%s: there are dangling in_multi references",
692 panic("%s: igmp_ifinfo not found for ifp %p\n", __func__, ifp);
697 * Process a received IGMPv1 query.
698 * Return non-zero if the message should be dropped.
700 * VIMAGE: The curvnet pointer is derived from the input ifp.
703 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
704 const struct igmp *igmp)
706 struct ifmultiaddr *ifma;
707 struct igmp_ifinfo *igi;
708 struct in_multi *inm;
711 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
712 * 224.0.0.1. They are always treated as General Queries.
713 * igmp_group is always ignored. Do not drop it as a userland
714 * daemon may wish to see it.
715 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
717 if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
718 IGMPSTAT_INC(igps_rcv_badqueries);
721 IGMPSTAT_INC(igps_rcv_gen_queries);
726 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
727 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
729 if (igi->igi_flags & IGIF_LOOPBACK) {
730 CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
736 * Switch to IGMPv1 host compatibility mode.
738 igmp_set_version(igi, IGMP_VERSION_1);
740 CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
743 * Start the timers in all of our group records
744 * for the interface on which the query arrived,
745 * except those which are already running.
748 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
749 if (ifma->ifma_addr->sa_family != AF_INET ||
750 ifma->ifma_protospec == NULL)
752 inm = (struct in_multi *)ifma->ifma_protospec;
753 if (inm->inm_timer != 0)
755 switch (inm->inm_state) {
756 case IGMP_NOT_MEMBER:
757 case IGMP_SILENT_MEMBER:
759 case IGMP_G_QUERY_PENDING_MEMBER:
760 case IGMP_SG_QUERY_PENDING_MEMBER:
761 case IGMP_REPORTING_MEMBER:
762 case IGMP_IDLE_MEMBER:
763 case IGMP_LAZY_MEMBER:
764 case IGMP_SLEEPING_MEMBER:
765 case IGMP_AWAKENING_MEMBER:
766 inm->inm_state = IGMP_REPORTING_MEMBER;
767 inm->inm_timer = IGMP_RANDOM_DELAY(
768 IGMP_V1V2_MAX_RI * PR_FASTHZ);
769 V_current_state_timers_running = 1;
771 case IGMP_LEAVING_MEMBER:
775 IF_ADDR_RUNLOCK(ifp);
785 * Process a received IGMPv2 general or group-specific query.
788 igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
789 const struct igmp *igmp)
791 struct ifmultiaddr *ifma;
792 struct igmp_ifinfo *igi;
793 struct in_multi *inm;
794 int is_general_query;
797 is_general_query = 0;
800 * Validate address fields upfront.
801 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
803 if (in_nullhost(igmp->igmp_group)) {
805 * IGMPv2 General Query.
806 * If this was not sent to the all-hosts group, ignore it.
808 if (!in_allhosts(ip->ip_dst))
810 IGMPSTAT_INC(igps_rcv_gen_queries);
811 is_general_query = 1;
813 /* IGMPv2 Group-Specific Query. */
814 IGMPSTAT_INC(igps_rcv_group_queries);
820 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
821 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
823 if (igi->igi_flags & IGIF_LOOPBACK) {
824 CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
830 * Ignore v2 query if in v1 Compatibility Mode.
832 if (igi->igi_version == IGMP_VERSION_1)
835 igmp_set_version(igi, IGMP_VERSION_2);
837 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
841 if (is_general_query) {
843 * For each reporting group joined on this
844 * interface, kick the report timer.
846 CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
849 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
850 if (ifma->ifma_addr->sa_family != AF_INET ||
851 ifma->ifma_protospec == NULL)
853 inm = (struct in_multi *)ifma->ifma_protospec;
854 igmp_v2_update_group(inm, timer);
856 IF_ADDR_RUNLOCK(ifp);
859 * Group-specific IGMPv2 query, we need only
860 * look up the single group to process it.
862 inm = inm_lookup(ifp, igmp->igmp_group);
864 CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)",
865 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
866 igmp_v2_update_group(inm, timer);
878 * Update the report timer on a group in response to an IGMPv2 query.
880 * If we are becoming the reporting member for this group, start the timer.
881 * If we already are the reporting member for this group, and timer is
882 * below the threshold, reset it.
884 * We may be updating the group for the first time since we switched
885 * to IGMPv3. If we are, then we must clear any recorded source lists,
886 * and transition to REPORTING state; the group timer is overloaded
887 * for group and group-source query responses.
889 * Unlike IGMPv3, the delay per group should be jittered
890 * to avoid bursts of IGMPv2 reports.
893 igmp_v2_update_group(struct in_multi *inm, const int timer)
896 CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__,
897 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer);
899 IN_MULTI_LOCK_ASSERT();
901 switch (inm->inm_state) {
902 case IGMP_NOT_MEMBER:
903 case IGMP_SILENT_MEMBER:
905 case IGMP_REPORTING_MEMBER:
906 if (inm->inm_timer != 0 &&
907 inm->inm_timer <= timer) {
908 CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
909 "skipping.", __func__);
913 case IGMP_SG_QUERY_PENDING_MEMBER:
914 case IGMP_G_QUERY_PENDING_MEMBER:
915 case IGMP_IDLE_MEMBER:
916 case IGMP_LAZY_MEMBER:
917 case IGMP_AWAKENING_MEMBER:
918 CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
919 inm->inm_state = IGMP_REPORTING_MEMBER;
920 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
921 V_current_state_timers_running = 1;
923 case IGMP_SLEEPING_MEMBER:
924 CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
925 inm->inm_state = IGMP_AWAKENING_MEMBER;
927 case IGMP_LEAVING_MEMBER:
933 * Process a received IGMPv3 general, group-specific or
934 * group-and-source-specific query.
935 * Assumes m has already been pulled up to the full IGMP message length.
936 * Return 0 if successful, otherwise an appropriate error code is returned.
939 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
940 /*const*/ struct igmpv3 *igmpv3)
942 struct igmp_ifinfo *igi;
943 struct in_multi *inm;
944 int is_general_query;
945 uint32_t maxresp, nsrc, qqi;
949 is_general_query = 0;
951 CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
953 maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */
954 if (maxresp >= 128) {
955 maxresp = IGMP_MANT(igmpv3->igmp_code) <<
956 (IGMP_EXP(igmpv3->igmp_code) + 3);
960 * Robustness must never be less than 2 for on-wire IGMPv3.
961 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
962 * an exception for interfaces whose IGMPv3 state changes
963 * are redirected to loopback (e.g. MANET).
965 qrv = IGMP_QRV(igmpv3->igmp_misc);
967 CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
972 qqi = igmpv3->igmp_qqi;
974 qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
975 (IGMP_EXP(igmpv3->igmp_qqi) + 3);
978 timer = maxresp * PR_FASTHZ / IGMP_TIMER_SCALE;
982 nsrc = ntohs(igmpv3->igmp_numsrc);
985 * Validate address fields and versions upfront before
986 * accepting v3 query.
987 * XXX SMPng: Unlocked access to igmpstat counters here.
989 if (in_nullhost(igmpv3->igmp_group)) {
991 * IGMPv3 General Query.
993 * General Queries SHOULD be directed to 224.0.0.1.
994 * A general query with a source list has undefined
995 * behaviour; discard it.
997 IGMPSTAT_INC(igps_rcv_gen_queries);
998 if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
999 IGMPSTAT_INC(igps_rcv_badqueries);
1002 is_general_query = 1;
1004 /* Group or group-source specific query. */
1006 IGMPSTAT_INC(igps_rcv_group_queries);
1008 IGMPSTAT_INC(igps_rcv_gsr_queries);
1014 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1015 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
1017 if (igi->igi_flags & IGIF_LOOPBACK) {
1018 CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1019 ifp, ifp->if_xname);
1024 * Discard the v3 query if we're in Compatibility Mode.
1025 * The RFC is not obviously worded that hosts need to stay in
1026 * compatibility mode until the Old Version Querier Present
1029 if (igi->igi_version != IGMP_VERSION_3) {
1030 CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1031 igi->igi_version, ifp, ifp->if_xname);
1035 igmp_set_version(igi, IGMP_VERSION_3);
1038 igi->igi_qri = maxresp;
1040 CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1043 if (is_general_query) {
1045 * Schedule a current-state report on this ifp for
1046 * all groups, possibly containing source lists.
1047 * If there is a pending General Query response
1048 * scheduled earlier than the selected delay, do
1049 * not schedule any other reports.
1050 * Otherwise, reset the interface timer.
1052 CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1053 ifp, ifp->if_xname);
1054 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1055 igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1056 V_interface_timers_running = 1;
1060 * Group-source-specific queries are throttled on
1061 * a per-group basis to defeat denial-of-service attempts.
1062 * Queries for groups we are not a member of on this
1063 * link are simply ignored.
1065 inm = inm_lookup(ifp, igmpv3->igmp_group);
1069 if (!ratecheck(&inm->inm_lastgsrtv,
1070 &V_igmp_gsrdelay)) {
1071 CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1073 IGMPSTAT_INC(igps_drop_gsr_queries);
1077 CTR3(KTR_IGMPV3, "process v3 %s query on ifp %p(%s)",
1078 inet_ntoa(igmpv3->igmp_group), ifp, ifp->if_xname);
1080 * If there is a pending General Query response
1081 * scheduled sooner than the selected delay, no
1082 * further report need be scheduled.
1083 * Otherwise, prepare to respond to the
1084 * group-specific or group-and-source query.
1086 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1087 igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1098 * Process a recieved IGMPv3 group-specific or group-and-source-specific
1100 * Return <0 if any error occured. Currently this is ignored.
1103 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi,
1104 int timer, /*const*/ struct igmpv3 *igmpv3)
1109 IN_MULTI_LOCK_ASSERT();
1114 switch (inm->inm_state) {
1115 case IGMP_NOT_MEMBER:
1116 case IGMP_SILENT_MEMBER:
1117 case IGMP_SLEEPING_MEMBER:
1118 case IGMP_LAZY_MEMBER:
1119 case IGMP_AWAKENING_MEMBER:
1120 case IGMP_IDLE_MEMBER:
1121 case IGMP_LEAVING_MEMBER:
1124 case IGMP_REPORTING_MEMBER:
1125 case IGMP_G_QUERY_PENDING_MEMBER:
1126 case IGMP_SG_QUERY_PENDING_MEMBER:
1130 nsrc = ntohs(igmpv3->igmp_numsrc);
1133 * Deal with group-specific queries upfront.
1134 * If any group query is already pending, purge any recorded
1135 * source-list state if it exists, and schedule a query response
1136 * for this group-specific query.
1139 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1140 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1141 inm_clear_recorded(inm);
1142 timer = min(inm->inm_timer, timer);
1144 inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1145 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1146 V_current_state_timers_running = 1;
1151 * Deal with the case where a group-and-source-specific query has
1152 * been received but a group-specific query is already pending.
1154 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1155 timer = min(inm->inm_timer, timer);
1156 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1157 V_current_state_timers_running = 1;
1162 * Finally, deal with the case where a group-and-source-specific
1163 * query has been received, where a response to a previous g-s-r
1164 * query exists, or none exists.
1165 * In this case, we need to parse the source-list which the Querier
1166 * has provided us with and check if we have any source list filter
1167 * entries at T1 for these sources. If we do not, there is no need
1168 * schedule a report and the query may be dropped.
1169 * If we do, we must record them and schedule a current-state
1170 * report for those sources.
1171 * FIXME: Handling source lists larger than 1 mbuf requires that
1172 * we pass the mbuf chain pointer down to this function, and use
1173 * m_getptr() to walk the chain.
1175 if (inm->inm_nsrc > 0) {
1176 const struct in_addr *ap;
1179 ap = (const struct in_addr *)(igmpv3 + 1);
1181 for (i = 0; i < nsrc; i++, ap++) {
1182 retval = inm_record_source(inm, ap->s_addr);
1185 nrecorded += retval;
1187 if (nrecorded > 0) {
1189 "%s: schedule response to SG query", __func__);
1190 inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1191 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1192 V_current_state_timers_running = 1;
1200 * Process a received IGMPv1 host membership report.
1202 * NOTE: 0.0.0.0 workaround breaks const correctness.
1205 igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1206 /*const*/ struct igmp *igmp)
1208 struct in_ifaddr *ia;
1209 struct in_multi *inm;
1211 IGMPSTAT_INC(igps_rcv_reports);
1213 if (ifp->if_flags & IFF_LOOPBACK)
1216 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1217 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1218 IGMPSTAT_INC(igps_rcv_badreports);
1223 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1224 * Booting clients may use the source address 0.0.0.0. Some
1225 * IGMP daemons may not know how to use IP_RECVIF to determine
1226 * the interface upon which this message was received.
1227 * Replace 0.0.0.0 with the subnet address if told to do so.
1229 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1232 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1233 ifa_free(&ia->ia_ifa);
1237 CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)",
1238 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1241 * IGMPv1 report suppression.
1242 * If we are a member of this group, and our membership should be
1243 * reported, stop our group timer and transition to the 'lazy' state.
1246 inm = inm_lookup(ifp, igmp->igmp_group);
1248 struct igmp_ifinfo *igi;
1252 KASSERT(igi != NULL,
1253 ("%s: no igi for ifp %p", __func__, ifp));
1257 IGMPSTAT_INC(igps_rcv_ourreports);
1260 * If we are in IGMPv3 host mode, do not allow the
1261 * other host's IGMPv1 report to suppress our reports
1262 * unless explicitly configured to do so.
1264 if (igi->igi_version == IGMP_VERSION_3) {
1265 if (V_igmp_legacysupp)
1266 igmp_v3_suppress_group_record(inm);
1272 switch (inm->inm_state) {
1273 case IGMP_NOT_MEMBER:
1274 case IGMP_SILENT_MEMBER:
1276 case IGMP_IDLE_MEMBER:
1277 case IGMP_LAZY_MEMBER:
1278 case IGMP_AWAKENING_MEMBER:
1280 "report suppressed for %s on ifp %p(%s)",
1281 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1282 case IGMP_SLEEPING_MEMBER:
1283 inm->inm_state = IGMP_SLEEPING_MEMBER;
1285 case IGMP_REPORTING_MEMBER:
1287 "report suppressed for %s on ifp %p(%s)",
1288 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1289 if (igi->igi_version == IGMP_VERSION_1)
1290 inm->inm_state = IGMP_LAZY_MEMBER;
1291 else if (igi->igi_version == IGMP_VERSION_2)
1292 inm->inm_state = IGMP_SLEEPING_MEMBER;
1294 case IGMP_G_QUERY_PENDING_MEMBER:
1295 case IGMP_SG_QUERY_PENDING_MEMBER:
1296 case IGMP_LEAVING_MEMBER:
1308 * Process a received IGMPv2 host membership report.
1310 * NOTE: 0.0.0.0 workaround breaks const correctness.
1313 igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1314 /*const*/ struct igmp *igmp)
1316 struct in_ifaddr *ia;
1317 struct in_multi *inm;
1320 * Make sure we don't hear our own membership report. Fast
1321 * leave requires knowing that we are the only member of a
1325 if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1326 ifa_free(&ia->ia_ifa);
1330 IGMPSTAT_INC(igps_rcv_reports);
1332 if (ifp->if_flags & IFF_LOOPBACK) {
1334 ifa_free(&ia->ia_ifa);
1338 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1339 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1341 ifa_free(&ia->ia_ifa);
1342 IGMPSTAT_INC(igps_rcv_badreports);
1347 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1348 * Booting clients may use the source address 0.0.0.0. Some
1349 * IGMP daemons may not know how to use IP_RECVIF to determine
1350 * the interface upon which this message was received.
1351 * Replace 0.0.0.0 with the subnet address if told to do so.
1353 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1355 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1358 ifa_free(&ia->ia_ifa);
1360 CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)",
1361 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1364 * IGMPv2 report suppression.
1365 * If we are a member of this group, and our membership should be
1366 * reported, and our group timer is pending or about to be reset,
1367 * stop our group timer by transitioning to the 'lazy' state.
1370 inm = inm_lookup(ifp, igmp->igmp_group);
1372 struct igmp_ifinfo *igi;
1375 KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1377 IGMPSTAT_INC(igps_rcv_ourreports);
1380 * If we are in IGMPv3 host mode, do not allow the
1381 * other host's IGMPv1 report to suppress our reports
1382 * unless explicitly configured to do so.
1384 if (igi->igi_version == IGMP_VERSION_3) {
1385 if (V_igmp_legacysupp)
1386 igmp_v3_suppress_group_record(inm);
1392 switch (inm->inm_state) {
1393 case IGMP_NOT_MEMBER:
1394 case IGMP_SILENT_MEMBER:
1395 case IGMP_SLEEPING_MEMBER:
1397 case IGMP_REPORTING_MEMBER:
1398 case IGMP_IDLE_MEMBER:
1399 case IGMP_AWAKENING_MEMBER:
1401 "report suppressed for %s on ifp %p(%s)",
1402 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1403 case IGMP_LAZY_MEMBER:
1404 inm->inm_state = IGMP_LAZY_MEMBER;
1406 case IGMP_G_QUERY_PENDING_MEMBER:
1407 case IGMP_SG_QUERY_PENDING_MEMBER:
1408 case IGMP_LEAVING_MEMBER:
1420 igmp_input(struct mbuf **mp, int *offp, int proto)
1431 CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, *mp, *offp);
1434 ifp = m->m_pkthdr.rcvif;
1437 IGMPSTAT_INC(igps_rcv_total);
1439 ip = mtod(m, struct ip *);
1441 igmplen = ntohs(ip->ip_len) - iphlen;
1446 if (igmplen < IGMP_MINLEN) {
1447 IGMPSTAT_INC(igps_rcv_tooshort);
1449 return (IPPROTO_DONE);
1453 * Always pullup to the minimum size for v1/v2 or v3
1454 * to amortize calls to m_pullup().
1457 if (igmplen >= IGMP_V3_QUERY_MINLEN)
1458 minlen += IGMP_V3_QUERY_MINLEN;
1460 minlen += IGMP_MINLEN;
1461 if ((!M_WRITABLE(m) || m->m_len < minlen) &&
1462 (m = m_pullup(m, minlen)) == 0) {
1463 IGMPSTAT_INC(igps_rcv_tooshort);
1464 return (IPPROTO_DONE);
1466 ip = mtod(m, struct ip *);
1469 * Validate checksum.
1471 m->m_data += iphlen;
1473 igmp = mtod(m, struct igmp *);
1474 if (in_cksum(m, igmplen)) {
1475 IGMPSTAT_INC(igps_rcv_badsum);
1477 return (IPPROTO_DONE);
1479 m->m_data -= iphlen;
1483 * IGMP control traffic is link-scope, and must have a TTL of 1.
1484 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1485 * probe packets may come from beyond the LAN.
1487 if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) {
1488 IGMPSTAT_INC(igps_rcv_badttl);
1490 return (IPPROTO_DONE);
1493 switch (igmp->igmp_type) {
1494 case IGMP_HOST_MEMBERSHIP_QUERY:
1495 if (igmplen == IGMP_MINLEN) {
1496 if (igmp->igmp_code == 0)
1497 queryver = IGMP_VERSION_1;
1499 queryver = IGMP_VERSION_2;
1500 } else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1501 queryver = IGMP_VERSION_3;
1503 IGMPSTAT_INC(igps_rcv_tooshort);
1505 return (IPPROTO_DONE);
1509 case IGMP_VERSION_1:
1510 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1511 if (!V_igmp_v1enable)
1513 if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1515 return (IPPROTO_DONE);
1519 case IGMP_VERSION_2:
1520 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1521 if (!V_igmp_v2enable)
1523 if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
1525 return (IPPROTO_DONE);
1529 case IGMP_VERSION_3: {
1530 struct igmpv3 *igmpv3;
1535 IGMPSTAT_INC(igps_rcv_v3_queries);
1536 igmpv3 = (struct igmpv3 *)igmp;
1538 * Validate length based on source count.
1540 nsrc = ntohs(igmpv3->igmp_numsrc);
1541 srclen = sizeof(struct in_addr) * nsrc;
1542 if (nsrc * sizeof(in_addr_t) > srclen) {
1543 IGMPSTAT_INC(igps_rcv_tooshort);
1544 return (IPPROTO_DONE);
1547 * m_pullup() may modify m, so pullup in
1550 igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1552 if ((!M_WRITABLE(m) ||
1553 m->m_len < igmpv3len) &&
1554 (m = m_pullup(m, igmpv3len)) == NULL) {
1555 IGMPSTAT_INC(igps_rcv_tooshort);
1556 return (IPPROTO_DONE);
1558 igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1560 if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1562 return (IPPROTO_DONE);
1569 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1570 if (!V_igmp_v1enable)
1572 if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1574 return (IPPROTO_DONE);
1578 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1579 if (!V_igmp_v2enable)
1581 if (!ip_checkrouteralert(m))
1582 IGMPSTAT_INC(igps_rcv_nora);
1583 if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
1585 return (IPPROTO_DONE);
1589 case IGMP_v3_HOST_MEMBERSHIP_REPORT:
1591 * Hosts do not need to process IGMPv3 membership reports,
1592 * as report suppression is no longer required.
1594 if (!ip_checkrouteralert(m))
1595 IGMPSTAT_INC(igps_rcv_nora);
1603 * Pass all valid IGMP packets up to any process(es) listening on a
1607 return (rip_input(mp, offp, proto));
1612 * Fast timeout handler (global).
1613 * VIMAGE: Timeout handlers are expected to service all vimages.
1618 VNET_ITERATOR_DECL(vnet_iter);
1620 VNET_LIST_RLOCK_NOSLEEP();
1621 VNET_FOREACH(vnet_iter) {
1622 CURVNET_SET(vnet_iter);
1623 igmp_fasttimo_vnet();
1626 VNET_LIST_RUNLOCK_NOSLEEP();
1630 * Fast timeout handler (per-vnet).
1631 * Sends are shuffled off to a netisr to deal with Giant.
1633 * VIMAGE: Assume caller has set up our curvnet.
1636 igmp_fasttimo_vnet(void)
1638 struct mbufq scq; /* State-change packets */
1639 struct mbufq qrq; /* Query response packets */
1641 struct igmp_ifinfo *igi;
1642 struct ifmultiaddr *ifma;
1643 struct in_multi *inm;
1644 int loop, uri_fasthz;
1650 * Quick check to see if any work needs to be done, in order to
1651 * minimize the overhead of fasttimo processing.
1652 * SMPng: XXX Unlocked reads.
1654 if (!V_current_state_timers_running &&
1655 !V_interface_timers_running &&
1656 !V_state_change_timers_running)
1663 * IGMPv3 General Query response timer processing.
1665 if (V_interface_timers_running) {
1666 CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1668 V_interface_timers_running = 0;
1669 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1670 if (igi->igi_v3_timer == 0) {
1672 } else if (--igi->igi_v3_timer == 0) {
1673 igmp_v3_dispatch_general_query(igi);
1675 V_interface_timers_running = 1;
1680 if (!V_current_state_timers_running &&
1681 !V_state_change_timers_running)
1684 V_current_state_timers_running = 0;
1685 V_state_change_timers_running = 0;
1687 CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1690 * IGMPv1/v2/v3 host report and state-change timer processing.
1691 * Note: Processing a v3 group timer may remove a node.
1693 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1696 if (igi->igi_version == IGMP_VERSION_3) {
1697 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1698 uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1700 mbufq_init(&qrq, IGMP_MAX_G_GS_PACKETS);
1701 mbufq_init(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1705 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1706 if (ifma->ifma_addr->sa_family != AF_INET ||
1707 ifma->ifma_protospec == NULL)
1709 inm = (struct in_multi *)ifma->ifma_protospec;
1710 switch (igi->igi_version) {
1711 case IGMP_VERSION_1:
1712 case IGMP_VERSION_2:
1713 igmp_v1v2_process_group_timer(inm,
1716 case IGMP_VERSION_3:
1717 igmp_v3_process_group_timers(igi, &qrq,
1718 &scq, inm, uri_fasthz);
1722 IF_ADDR_RUNLOCK(ifp);
1724 if (igi->igi_version == IGMP_VERSION_3) {
1725 struct in_multi *tinm;
1727 igmp_dispatch_queue(&qrq, 0, loop);
1728 igmp_dispatch_queue(&scq, 0, loop);
1731 * Free the in_multi reference(s) for this
1734 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead,
1736 SLIST_REMOVE_HEAD(&igi->igi_relinmhead,
1738 inm_release_locked(inm);
1749 * Update host report group timer for IGMPv1/v2.
1750 * Will update the global pending timer flags.
1753 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1755 int report_timer_expired;
1757 IN_MULTI_LOCK_ASSERT();
1760 if (inm->inm_timer == 0) {
1761 report_timer_expired = 0;
1762 } else if (--inm->inm_timer == 0) {
1763 report_timer_expired = 1;
1765 V_current_state_timers_running = 1;
1769 switch (inm->inm_state) {
1770 case IGMP_NOT_MEMBER:
1771 case IGMP_SILENT_MEMBER:
1772 case IGMP_IDLE_MEMBER:
1773 case IGMP_LAZY_MEMBER:
1774 case IGMP_SLEEPING_MEMBER:
1775 case IGMP_AWAKENING_MEMBER:
1777 case IGMP_REPORTING_MEMBER:
1778 if (report_timer_expired) {
1779 inm->inm_state = IGMP_IDLE_MEMBER;
1780 (void)igmp_v1v2_queue_report(inm,
1781 (version == IGMP_VERSION_2) ?
1782 IGMP_v2_HOST_MEMBERSHIP_REPORT :
1783 IGMP_v1_HOST_MEMBERSHIP_REPORT);
1786 case IGMP_G_QUERY_PENDING_MEMBER:
1787 case IGMP_SG_QUERY_PENDING_MEMBER:
1788 case IGMP_LEAVING_MEMBER:
1794 * Update a group's timers for IGMPv3.
1795 * Will update the global pending timer flags.
1796 * Note: Unlocked read from igi.
1799 igmp_v3_process_group_timers(struct igmp_ifinfo *igi,
1800 struct mbufq *qrq, struct mbufq *scq,
1801 struct in_multi *inm, const int uri_fasthz)
1803 int query_response_timer_expired;
1804 int state_change_retransmit_timer_expired;
1806 IN_MULTI_LOCK_ASSERT();
1809 query_response_timer_expired = 0;
1810 state_change_retransmit_timer_expired = 0;
1813 * During a transition from v1/v2 compatibility mode back to v3,
1814 * a group record in REPORTING state may still have its group
1815 * timer active. This is a no-op in this function; it is easier
1816 * to deal with it here than to complicate the slow-timeout path.
1818 if (inm->inm_timer == 0) {
1819 query_response_timer_expired = 0;
1820 } else if (--inm->inm_timer == 0) {
1821 query_response_timer_expired = 1;
1823 V_current_state_timers_running = 1;
1826 if (inm->inm_sctimer == 0) {
1827 state_change_retransmit_timer_expired = 0;
1828 } else if (--inm->inm_sctimer == 0) {
1829 state_change_retransmit_timer_expired = 1;
1831 V_state_change_timers_running = 1;
1834 /* We are in fasttimo, so be quick about it. */
1835 if (!state_change_retransmit_timer_expired &&
1836 !query_response_timer_expired)
1839 switch (inm->inm_state) {
1840 case IGMP_NOT_MEMBER:
1841 case IGMP_SILENT_MEMBER:
1842 case IGMP_SLEEPING_MEMBER:
1843 case IGMP_LAZY_MEMBER:
1844 case IGMP_AWAKENING_MEMBER:
1845 case IGMP_IDLE_MEMBER:
1847 case IGMP_G_QUERY_PENDING_MEMBER:
1848 case IGMP_SG_QUERY_PENDING_MEMBER:
1850 * Respond to a previously pending Group-Specific
1851 * or Group-and-Source-Specific query by enqueueing
1852 * the appropriate Current-State report for
1853 * immediate transmission.
1855 if (query_response_timer_expired) {
1858 retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1859 (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1860 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1862 inm->inm_state = IGMP_REPORTING_MEMBER;
1863 /* XXX Clear recorded sources for next time. */
1864 inm_clear_recorded(inm);
1867 case IGMP_REPORTING_MEMBER:
1868 case IGMP_LEAVING_MEMBER:
1869 if (state_change_retransmit_timer_expired) {
1871 * State-change retransmission timer fired.
1872 * If there are any further pending retransmissions,
1873 * set the global pending state-change flag, and
1876 if (--inm->inm_scrv > 0) {
1877 inm->inm_sctimer = uri_fasthz;
1878 V_state_change_timers_running = 1;
1881 * Retransmit the previously computed state-change
1882 * report. If there are no further pending
1883 * retransmissions, the mbuf queue will be consumed.
1884 * Update T0 state to T1 as we have now sent
1887 (void)igmp_v3_merge_state_changes(inm, scq);
1890 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
1891 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
1894 * If we are leaving the group for good, make sure
1895 * we release IGMP's reference to it.
1896 * This release must be deferred using a SLIST,
1897 * as we are called from a loop which traverses
1898 * the in_ifmultiaddr TAILQ.
1900 if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1901 inm->inm_scrv == 0) {
1902 inm->inm_state = IGMP_NOT_MEMBER;
1903 SLIST_INSERT_HEAD(&igi->igi_relinmhead,
1913 * Suppress a group's pending response to a group or source/group query.
1915 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1916 * Do NOT update ST1/ST0 as this operation merely suppresses
1917 * the currently pending group record.
1918 * Do NOT suppress the response to a general query. It is possible but
1919 * it would require adding another state or flag.
1922 igmp_v3_suppress_group_record(struct in_multi *inm)
1925 IN_MULTI_LOCK_ASSERT();
1927 KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1928 ("%s: not IGMPv3 mode on link", __func__));
1930 if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1931 inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1934 if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1935 inm_clear_recorded(inm);
1938 inm->inm_state = IGMP_REPORTING_MEMBER;
1942 * Switch to a different IGMP version on the given interface,
1943 * as per Section 7.2.1.
1946 igmp_set_version(struct igmp_ifinfo *igi, const int version)
1948 int old_version_timer;
1952 CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1953 version, igi->igi_ifp, igi->igi_ifp->if_xname);
1955 if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1957 * Compute the "Older Version Querier Present" timer as per
1960 old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1961 old_version_timer *= PR_SLOWHZ;
1963 if (version == IGMP_VERSION_1) {
1964 igi->igi_v1_timer = old_version_timer;
1965 igi->igi_v2_timer = 0;
1966 } else if (version == IGMP_VERSION_2) {
1967 igi->igi_v1_timer = 0;
1968 igi->igi_v2_timer = old_version_timer;
1972 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
1973 if (igi->igi_version != IGMP_VERSION_2) {
1974 igi->igi_version = IGMP_VERSION_2;
1975 igmp_v3_cancel_link_timers(igi);
1977 } else if (igi->igi_v1_timer > 0) {
1978 if (igi->igi_version != IGMP_VERSION_1) {
1979 igi->igi_version = IGMP_VERSION_1;
1980 igmp_v3_cancel_link_timers(igi);
1986 * Cancel pending IGMPv3 timers for the given link and all groups
1987 * joined on it; state-change, general-query, and group-query timers.
1989 * Only ever called on a transition from v3 to Compatibility mode. Kill
1990 * the timers stone dead (this may be expensive for large N groups), they
1991 * will be restarted if Compatibility Mode deems that they must be due to
1995 igmp_v3_cancel_link_timers(struct igmp_ifinfo *igi)
1997 struct ifmultiaddr *ifma;
1999 struct in_multi *inm, *tinm;
2001 CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2002 igi->igi_ifp, igi->igi_ifp->if_xname);
2004 IN_MULTI_LOCK_ASSERT();
2008 * Stop the v3 General Query Response on this link stone dead.
2009 * If fasttimo is woken up due to V_interface_timers_running,
2010 * the flag will be cleared if there are no pending link timers.
2012 igi->igi_v3_timer = 0;
2015 * Now clear the current-state and state-change report timers
2016 * for all memberships scoped to this link.
2020 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2021 if (ifma->ifma_addr->sa_family != AF_INET ||
2022 ifma->ifma_protospec == NULL)
2024 inm = (struct in_multi *)ifma->ifma_protospec;
2025 switch (inm->inm_state) {
2026 case IGMP_NOT_MEMBER:
2027 case IGMP_SILENT_MEMBER:
2028 case IGMP_IDLE_MEMBER:
2029 case IGMP_LAZY_MEMBER:
2030 case IGMP_SLEEPING_MEMBER:
2031 case IGMP_AWAKENING_MEMBER:
2033 * These states are either not relevant in v3 mode,
2034 * or are unreported. Do nothing.
2037 case IGMP_LEAVING_MEMBER:
2039 * If we are leaving the group and switching to
2040 * compatibility mode, we need to release the final
2041 * reference held for issuing the INCLUDE {}, and
2042 * transition to REPORTING to ensure the host leave
2043 * message is sent upstream to the old querier --
2044 * transition to NOT would lose the leave and race.
2046 SLIST_INSERT_HEAD(&igi->igi_relinmhead, inm, inm_nrele);
2048 case IGMP_G_QUERY_PENDING_MEMBER:
2049 case IGMP_SG_QUERY_PENDING_MEMBER:
2050 inm_clear_recorded(inm);
2052 case IGMP_REPORTING_MEMBER:
2053 inm->inm_state = IGMP_REPORTING_MEMBER;
2057 * Always clear state-change and group report timers.
2058 * Free any pending IGMPv3 state-change records.
2060 inm->inm_sctimer = 0;
2062 mbufq_drain(&inm->inm_scq);
2064 IF_ADDR_RUNLOCK(ifp);
2065 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele, tinm) {
2066 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
2067 inm_release_locked(inm);
2072 * Update the Older Version Querier Present timers for a link.
2073 * See Section 7.2.1 of RFC 3376.
2076 igmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi)
2081 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2083 * IGMPv1 and IGMPv2 Querier Present timers expired.
2087 if (igi->igi_version != IGMP_VERSION_3) {
2089 "%s: transition from v%d -> v%d on %p(%s)",
2090 __func__, igi->igi_version, IGMP_VERSION_3,
2091 igi->igi_ifp, igi->igi_ifp->if_xname);
2092 igi->igi_version = IGMP_VERSION_3;
2094 } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2096 * IGMPv1 Querier Present timer expired,
2097 * IGMPv2 Querier Present timer running.
2098 * If IGMPv2 was disabled since last timeout,
2100 * If IGMPv2 is enabled, revert to IGMPv2.
2102 if (!V_igmp_v2enable) {
2104 "%s: transition from v%d -> v%d on %p(%s)",
2105 __func__, igi->igi_version, IGMP_VERSION_3,
2106 igi->igi_ifp, igi->igi_ifp->if_xname);
2107 igi->igi_v2_timer = 0;
2108 igi->igi_version = IGMP_VERSION_3;
2110 --igi->igi_v2_timer;
2111 if (igi->igi_version != IGMP_VERSION_2) {
2113 "%s: transition from v%d -> v%d on %p(%s)",
2114 __func__, igi->igi_version, IGMP_VERSION_2,
2115 igi->igi_ifp, igi->igi_ifp->if_xname);
2116 igi->igi_version = IGMP_VERSION_2;
2117 igmp_v3_cancel_link_timers(igi);
2120 } else if (igi->igi_v1_timer > 0) {
2122 * IGMPv1 Querier Present timer running.
2123 * Stop IGMPv2 timer if running.
2125 * If IGMPv1 was disabled since last timeout,
2127 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2129 if (!V_igmp_v1enable) {
2131 "%s: transition from v%d -> v%d on %p(%s)",
2132 __func__, igi->igi_version, IGMP_VERSION_3,
2133 igi->igi_ifp, igi->igi_ifp->if_xname);
2134 igi->igi_v1_timer = 0;
2135 igi->igi_version = IGMP_VERSION_3;
2137 --igi->igi_v1_timer;
2139 if (igi->igi_v2_timer > 0) {
2141 "%s: cancel v2 timer on %p(%s)",
2142 __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2143 igi->igi_v2_timer = 0;
2149 * Global slowtimo handler.
2150 * VIMAGE: Timeout handlers are expected to service all vimages.
2155 VNET_ITERATOR_DECL(vnet_iter);
2157 VNET_LIST_RLOCK_NOSLEEP();
2158 VNET_FOREACH(vnet_iter) {
2159 CURVNET_SET(vnet_iter);
2160 igmp_slowtimo_vnet();
2163 VNET_LIST_RUNLOCK_NOSLEEP();
2167 * Per-vnet slowtimo handler.
2170 igmp_slowtimo_vnet(void)
2172 struct igmp_ifinfo *igi;
2176 LIST_FOREACH(igi, &V_igi_head, igi_link) {
2177 igmp_v1v2_process_querier_timers(igi);
2184 * Dispatch an IGMPv1/v2 host report or leave message.
2185 * These are always small enough to fit inside a single mbuf.
2188 igmp_v1v2_queue_report(struct in_multi *inm, const int type)
2195 IN_MULTI_LOCK_ASSERT();
2200 m = m_gethdr(M_NOWAIT, MT_DATA);
2203 M_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
2205 m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2207 m->m_data += sizeof(struct ip);
2208 m->m_len = sizeof(struct igmp);
2210 igmp = mtod(m, struct igmp *);
2211 igmp->igmp_type = type;
2212 igmp->igmp_code = 0;
2213 igmp->igmp_group = inm->inm_addr;
2214 igmp->igmp_cksum = 0;
2215 igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
2217 m->m_data -= sizeof(struct ip);
2218 m->m_len += sizeof(struct ip);
2220 ip = mtod(m, struct ip *);
2222 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct igmp));
2224 ip->ip_p = IPPROTO_IGMP;
2225 ip->ip_src.s_addr = INADDR_ANY;
2227 if (type == IGMP_HOST_LEAVE_MESSAGE)
2228 ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2230 ip->ip_dst = inm->inm_addr;
2232 igmp_save_context(m, ifp);
2234 m->m_flags |= M_IGMPV2;
2235 if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2236 m->m_flags |= M_IGMP_LOOP;
2238 CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2239 netisr_dispatch(NETISR_IGMP, m);
2245 * Process a state change from the upper layer for the given IPv4 group.
2247 * Each socket holds a reference on the in_multi in its own ip_moptions.
2248 * The socket layer will have made the necessary updates to.the group
2249 * state, it is now up to IGMP to issue a state change report if there
2250 * has been any change between T0 (when the last state-change was issued)
2253 * We use the IGMPv3 state machine at group level. The IGMP module
2254 * however makes the decision as to which IGMP protocol version to speak.
2255 * A state change *from* INCLUDE {} always means an initial join.
2256 * A state change *to* INCLUDE {} always means a final leave.
2258 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2259 * save ourselves a bunch of work; any exclusive mode groups need not
2260 * compute source filter lists.
2262 * VIMAGE: curvnet should have been set by caller, as this routine
2263 * is called from the socket option handlers.
2266 igmp_change_state(struct in_multi *inm)
2268 struct igmp_ifinfo *igi;
2272 IN_MULTI_LOCK_ASSERT();
2277 * Try to detect if the upper layer just asked us to change state
2278 * for an interface which has now gone away.
2280 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2281 ifp = inm->inm_ifma->ifma_ifp;
2283 * Sanity check that netinet's notion of ifp is the
2286 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2290 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2291 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
2294 * If we detect a state transition to or from MCAST_UNDEFINED
2295 * for this group, then we are starting or finishing an IGMP
2296 * life cycle for this group.
2298 if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2299 CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2300 inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2301 if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2302 CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2303 error = igmp_initial_join(inm, igi);
2305 } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2306 CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2307 igmp_final_leave(inm, igi);
2311 CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2314 error = igmp_handle_state_change(inm, igi);
2322 * Perform the initial join for an IGMP group.
2324 * When joining a group:
2325 * If the group should have its IGMP traffic suppressed, do nothing.
2326 * IGMPv1 starts sending IGMPv1 host membership reports.
2327 * IGMPv2 starts sending IGMPv2 host membership reports.
2328 * IGMPv3 will schedule an IGMPv3 state-change report containing the
2329 * initial state of the membership.
2332 igmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi)
2336 int error, retval, syncstates;
2338 CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)",
2339 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2340 inm->inm_ifp->if_xname);
2347 IN_MULTI_LOCK_ASSERT();
2350 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2353 * Groups joined on loopback or marked as 'not reported',
2354 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2355 * are never reported in any IGMP protocol exchanges.
2356 * All other groups enter the appropriate IGMP state machine
2357 * for the version in use on this link.
2358 * A link marked as IGIF_SILENT causes IGMP to be completely
2359 * disabled for the link.
2361 if ((ifp->if_flags & IFF_LOOPBACK) ||
2362 (igi->igi_flags & IGIF_SILENT) ||
2363 !igmp_isgroupreported(inm->inm_addr)) {
2365 "%s: not kicking state machine for silent group", __func__);
2366 inm->inm_state = IGMP_SILENT_MEMBER;
2370 * Deal with overlapping in_multi lifecycle.
2371 * If this group was LEAVING, then make sure
2372 * we drop the reference we picked up to keep the
2373 * group around for the final INCLUDE {} enqueue.
2375 if (igi->igi_version == IGMP_VERSION_3 &&
2376 inm->inm_state == IGMP_LEAVING_MEMBER)
2377 inm_release_locked(inm);
2379 inm->inm_state = IGMP_REPORTING_MEMBER;
2381 switch (igi->igi_version) {
2382 case IGMP_VERSION_1:
2383 case IGMP_VERSION_2:
2384 inm->inm_state = IGMP_IDLE_MEMBER;
2385 error = igmp_v1v2_queue_report(inm,
2386 (igi->igi_version == IGMP_VERSION_2) ?
2387 IGMP_v2_HOST_MEMBERSHIP_REPORT :
2388 IGMP_v1_HOST_MEMBERSHIP_REPORT);
2390 inm->inm_timer = IGMP_RANDOM_DELAY(
2391 IGMP_V1V2_MAX_RI * PR_FASTHZ);
2392 V_current_state_timers_running = 1;
2396 case IGMP_VERSION_3:
2398 * Defer update of T0 to T1, until the first copy
2399 * of the state change has been transmitted.
2404 * Immediately enqueue a State-Change Report for
2405 * this interface, freeing any previous reports.
2406 * Don't kick the timers if there is nothing to do,
2407 * or if an error occurred.
2411 retval = igmp_v3_enqueue_group_record(mq, inm, 1,
2413 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2416 error = retval * -1;
2421 * Schedule transmission of pending state-change
2422 * report up to RV times for this link. The timer
2423 * will fire at the next igmp_fasttimo (~200ms),
2424 * giving us an opportunity to merge the reports.
2426 if (igi->igi_flags & IGIF_LOOPBACK) {
2429 KASSERT(igi->igi_rv > 1,
2430 ("%s: invalid robustness %d", __func__,
2432 inm->inm_scrv = igi->igi_rv;
2434 inm->inm_sctimer = 1;
2435 V_state_change_timers_running = 1;
2443 * Only update the T0 state if state change is atomic,
2444 * i.e. we don't need to wait for a timer to fire before we
2445 * can consider the state change to have been communicated.
2449 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2450 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2457 * Issue an intermediate state change during the IGMP life-cycle.
2460 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi)
2465 CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)",
2466 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2467 inm->inm_ifp->if_xname);
2471 IN_MULTI_LOCK_ASSERT();
2474 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2476 if ((ifp->if_flags & IFF_LOOPBACK) ||
2477 (igi->igi_flags & IGIF_SILENT) ||
2478 !igmp_isgroupreported(inm->inm_addr) ||
2479 (igi->igi_version != IGMP_VERSION_3)) {
2480 if (!igmp_isgroupreported(inm->inm_addr)) {
2482 "%s: not kicking state machine for silent group", __func__);
2484 CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2486 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2487 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2491 mbufq_drain(&inm->inm_scq);
2493 retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2494 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2499 * If record(s) were enqueued, start the state-change
2500 * report timer for this group.
2502 inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2503 inm->inm_sctimer = 1;
2504 V_state_change_timers_running = 1;
2510 * Perform the final leave for an IGMP group.
2512 * When leaving a group:
2513 * IGMPv1 does nothing.
2514 * IGMPv2 sends a host leave message, if and only if we are the reporter.
2515 * IGMPv3 enqueues a state-change report containing a transition
2516 * to INCLUDE {} for immediate transmission.
2519 igmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi)
2525 CTR4(KTR_IGMPV3, "%s: final leave %s on ifp %p(%s)",
2526 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2527 inm->inm_ifp->if_xname);
2529 IN_MULTI_LOCK_ASSERT();
2532 switch (inm->inm_state) {
2533 case IGMP_NOT_MEMBER:
2534 case IGMP_SILENT_MEMBER:
2535 case IGMP_LEAVING_MEMBER:
2536 /* Already leaving or left; do nothing. */
2538 "%s: not kicking state machine for silent group", __func__);
2540 case IGMP_REPORTING_MEMBER:
2541 case IGMP_IDLE_MEMBER:
2542 case IGMP_G_QUERY_PENDING_MEMBER:
2543 case IGMP_SG_QUERY_PENDING_MEMBER:
2544 if (igi->igi_version == IGMP_VERSION_2) {
2546 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2547 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2548 panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2551 igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2552 inm->inm_state = IGMP_NOT_MEMBER;
2553 } else if (igi->igi_version == IGMP_VERSION_3) {
2555 * Stop group timer and all pending reports.
2556 * Immediately enqueue a state-change report
2557 * TO_IN {} to be sent on the next fast timeout,
2558 * giving us an opportunity to merge reports.
2560 mbufq_drain(&inm->inm_scq);
2562 if (igi->igi_flags & IGIF_LOOPBACK) {
2565 inm->inm_scrv = igi->igi_rv;
2567 CTR4(KTR_IGMPV3, "%s: Leaving %s/%s with %d "
2568 "pending retransmissions.", __func__,
2569 inet_ntoa(inm->inm_addr),
2570 inm->inm_ifp->if_xname, inm->inm_scrv);
2571 if (inm->inm_scrv == 0) {
2572 inm->inm_state = IGMP_NOT_MEMBER;
2573 inm->inm_sctimer = 0;
2577 inm_acquire_locked(inm);
2579 retval = igmp_v3_enqueue_group_record(
2580 &inm->inm_scq, inm, 1, 0, 0);
2581 KASSERT(retval != 0,
2582 ("%s: enqueue record = %d", __func__,
2585 inm->inm_state = IGMP_LEAVING_MEMBER;
2586 inm->inm_sctimer = 1;
2587 V_state_change_timers_running = 1;
2593 case IGMP_LAZY_MEMBER:
2594 case IGMP_SLEEPING_MEMBER:
2595 case IGMP_AWAKENING_MEMBER:
2596 /* Our reports are suppressed; do nothing. */
2602 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2603 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2604 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2605 CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for %s/%s",
2606 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2611 * Enqueue an IGMPv3 group record to the given output queue.
2613 * XXX This function could do with having the allocation code
2614 * split out, and the multiple-tree-walks coalesced into a single
2615 * routine as has been done in igmp_v3_enqueue_filter_change().
2617 * If is_state_change is zero, a current-state record is appended.
2618 * If is_state_change is non-zero, a state-change report is appended.
2620 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2621 * If is_group_query is zero, and if there is a packet with free space
2622 * at the tail of the queue, it will be appended to providing there
2623 * is enough free space.
2624 * Otherwise a new mbuf packet chain is allocated.
2626 * If is_source_query is non-zero, each source is checked to see if
2627 * it was recorded for a Group-Source query, and will be omitted if
2628 * it is not both in-mode and recorded.
2630 * The function will attempt to allocate leading space in the packet
2631 * for the IP/IGMP header to be prepended without fragmenting the chain.
2633 * If successful the size of all data appended to the queue is returned,
2634 * otherwise an error code less than zero is returned, or zero if
2635 * no record(s) were appended.
2638 igmp_v3_enqueue_group_record(struct mbufq *mq, struct in_multi *inm,
2639 const int is_state_change, const int is_group_query,
2640 const int is_source_query)
2642 struct igmp_grouprec ig;
2643 struct igmp_grouprec *pig;
2645 struct ip_msource *ims, *nims;
2646 struct mbuf *m0, *m, *md;
2647 int error, is_filter_list_change;
2648 int minrec0len, m0srcs, msrcs, nbytes, off;
2649 int record_has_sources;
2655 IN_MULTI_LOCK_ASSERT();
2659 is_filter_list_change = 0;
2666 record_has_sources = 1;
2668 type = IGMP_DO_NOTHING;
2669 mode = inm->inm_st[1].iss_fmode;
2672 * If we did not transition out of ASM mode during t0->t1,
2673 * and there are no source nodes to process, we can skip
2674 * the generation of source records.
2676 if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2678 record_has_sources = 0;
2680 if (is_state_change) {
2682 * Queue a state change record.
2683 * If the mode did not change, and there are non-ASM
2684 * listeners or source filters present,
2685 * we potentially need to issue two records for the group.
2686 * If we are transitioning to MCAST_UNDEFINED, we need
2687 * not send any sources.
2688 * If there are ASM listeners, and there was no filter
2689 * mode transition of any kind, do nothing.
2691 if (mode != inm->inm_st[0].iss_fmode) {
2692 if (mode == MCAST_EXCLUDE) {
2693 CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2695 type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2697 CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2699 type = IGMP_CHANGE_TO_INCLUDE_MODE;
2700 if (mode == MCAST_UNDEFINED)
2701 record_has_sources = 0;
2704 if (record_has_sources) {
2705 is_filter_list_change = 1;
2707 type = IGMP_DO_NOTHING;
2712 * Queue a current state record.
2714 if (mode == MCAST_EXCLUDE) {
2715 type = IGMP_MODE_IS_EXCLUDE;
2716 } else if (mode == MCAST_INCLUDE) {
2717 type = IGMP_MODE_IS_INCLUDE;
2718 KASSERT(inm->inm_st[1].iss_asm == 0,
2719 ("%s: inm %p is INCLUDE but ASM count is %d",
2720 __func__, inm, inm->inm_st[1].iss_asm));
2725 * Generate the filter list changes using a separate function.
2727 if (is_filter_list_change)
2728 return (igmp_v3_enqueue_filter_change(mq, inm));
2730 if (type == IGMP_DO_NOTHING) {
2731 CTR3(KTR_IGMPV3, "%s: nothing to do for %s/%s",
2732 __func__, inet_ntoa(inm->inm_addr),
2733 inm->inm_ifp->if_xname);
2738 * If any sources are present, we must be able to fit at least
2739 * one in the trailing space of the tail packet's mbuf,
2742 minrec0len = sizeof(struct igmp_grouprec);
2743 if (record_has_sources)
2744 minrec0len += sizeof(in_addr_t);
2746 CTR4(KTR_IGMPV3, "%s: queueing %s for %s/%s", __func__,
2747 igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr),
2748 inm->inm_ifp->if_xname);
2751 * Check if we have a packet in the tail of the queue for this
2752 * group into which the first group record for this group will fit.
2753 * Otherwise allocate a new packet.
2754 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2755 * Note: Group records for G/GSR query responses MUST be sent
2756 * in their own packet.
2758 m0 = mbufq_last(mq);
2759 if (!is_group_query &&
2761 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2762 (m0->m_pkthdr.len + minrec0len) <
2763 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2764 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2765 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2767 CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2769 if (mbufq_full(mq)) {
2770 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2774 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2775 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2776 if (!is_state_change && !is_group_query) {
2777 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2779 m->m_data += IGMP_LEADINGSPACE;
2782 m = m_gethdr(M_NOWAIT, MT_DATA);
2784 M_ALIGN(m, IGMP_LEADINGSPACE);
2789 igmp_save_context(m, ifp);
2791 CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2795 * Append group record.
2796 * If we have sources, we don't know how many yet.
2801 ig.ig_group = inm->inm_addr;
2802 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2805 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2808 nbytes += sizeof(struct igmp_grouprec);
2811 * Append as many sources as will fit in the first packet.
2812 * If we are appending to a new packet, the chain allocation
2813 * may potentially use clusters; use m_getptr() in this case.
2814 * If we are appending to an existing packet, we need to obtain
2815 * a pointer to the group record after m_append(), in case a new
2816 * mbuf was allocated.
2817 * Only append sources which are in-mode at t1. If we are
2818 * transitioning to MCAST_UNDEFINED state on the group, do not
2819 * include source entries.
2820 * Only report recorded sources in our filter set when responding
2821 * to a group-source query.
2823 if (record_has_sources) {
2826 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2827 md->m_len - nbytes);
2829 md = m_getptr(m, 0, &off);
2830 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2834 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2835 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2836 inet_ntoa_haddr(ims->ims_haddr));
2837 now = ims_get_mode(inm, ims, 1);
2838 CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2839 if ((now != mode) ||
2840 (now == mode && mode == MCAST_UNDEFINED)) {
2841 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2844 if (is_source_query && ims->ims_stp == 0) {
2845 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2849 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2850 naddr = htonl(ims->ims_haddr);
2851 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2854 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2858 nbytes += sizeof(in_addr_t);
2860 if (msrcs == m0srcs)
2863 CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2865 pig->ig_numsrc = htons(msrcs);
2866 nbytes += (msrcs * sizeof(in_addr_t));
2869 if (is_source_query && msrcs == 0) {
2870 CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2877 * We are good to go with first packet.
2880 CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2881 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2882 mbufq_enqueue(mq, m);
2884 m->m_pkthdr.PH_vt.vt_nrecs++;
2887 * No further work needed if no source list in packet(s).
2889 if (!record_has_sources)
2893 * Whilst sources remain to be announced, we need to allocate
2894 * a new packet and fill out as many sources as will fit.
2895 * Always try for a cluster first.
2897 while (nims != NULL) {
2898 if (mbufq_full(mq)) {
2899 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2902 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2904 m->m_data += IGMP_LEADINGSPACE;
2906 m = m_gethdr(M_NOWAIT, MT_DATA);
2908 M_ALIGN(m, IGMP_LEADINGSPACE);
2912 igmp_save_context(m, ifp);
2913 md = m_getptr(m, 0, &off);
2914 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2915 CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2917 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2920 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2923 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2924 nbytes += sizeof(struct igmp_grouprec);
2926 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2927 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2930 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2931 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2932 inet_ntoa_haddr(ims->ims_haddr));
2933 now = ims_get_mode(inm, ims, 1);
2934 if ((now != mode) ||
2935 (now == mode && mode == MCAST_UNDEFINED)) {
2936 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2939 if (is_source_query && ims->ims_stp == 0) {
2940 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2944 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2945 naddr = htonl(ims->ims_haddr);
2946 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2949 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2954 if (msrcs == m0srcs)
2957 pig->ig_numsrc = htons(msrcs);
2958 nbytes += (msrcs * sizeof(in_addr_t));
2960 CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
2961 mbufq_enqueue(mq, m);
2968 * Type used to mark record pass completion.
2969 * We exploit the fact we can cast to this easily from the
2970 * current filter modes on each ip_msource node.
2973 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2974 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2975 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2976 REC_FULL = REC_ALLOW | REC_BLOCK
2980 * Enqueue an IGMPv3 filter list change to the given output queue.
2982 * Source list filter state is held in an RB-tree. When the filter list
2983 * for a group is changed without changing its mode, we need to compute
2984 * the deltas between T0 and T1 for each source in the filter set,
2985 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
2987 * As we may potentially queue two record types, and the entire R-B tree
2988 * needs to be walked at once, we break this out into its own function
2989 * so we can generate a tightly packed queue of packets.
2991 * XXX This could be written to only use one tree walk, although that makes
2992 * serializing into the mbuf chains a bit harder. For now we do two walks
2993 * which makes things easier on us, and it may or may not be harder on
2996 * If successful the size of all data appended to the queue is returned,
2997 * otherwise an error code less than zero is returned, or zero if
2998 * no record(s) were appended.
3001 igmp_v3_enqueue_filter_change(struct mbufq *mq, struct in_multi *inm)
3003 static const int MINRECLEN =
3004 sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3006 struct igmp_grouprec ig;
3007 struct igmp_grouprec *pig;
3008 struct ip_msource *ims, *nims;
3009 struct mbuf *m, *m0, *md;
3011 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3013 uint8_t mode, now, then;
3014 rectype_t crt, drt, nrt;
3016 IN_MULTI_LOCK_ASSERT();
3018 if (inm->inm_nsrc == 0 ||
3019 (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3022 ifp = inm->inm_ifp; /* interface */
3023 mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */
3024 crt = REC_NONE; /* current group record type */
3025 drt = REC_NONE; /* mask of completed group record types */
3026 nrt = REC_NONE; /* record type for current node */
3027 m0srcs = 0; /* # source which will fit in current mbuf chain */
3028 nbytes = 0; /* # of bytes appended to group's state-change queue */
3029 npbytes = 0; /* # of bytes appended this packet */
3030 rsrcs = 0; /* # sources encoded in current record */
3031 schanged = 0; /* # nodes encoded in overall filter change */
3032 nallow = 0; /* # of source entries in ALLOW_NEW */
3033 nblock = 0; /* # of source entries in BLOCK_OLD */
3034 nims = NULL; /* next tree node pointer */
3037 * For each possible filter record mode.
3038 * The first kind of source we encounter tells us which
3039 * is the first kind of record we start appending.
3040 * If a node transitioned to UNDEFINED at t1, its mode is treated
3041 * as the inverse of the group's filter mode.
3043 while (drt != REC_FULL) {
3045 m0 = mbufq_last(mq);
3047 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
3048 IGMP_V3_REPORT_MAXRECS) &&
3049 (m0->m_pkthdr.len + MINRECLEN) <
3050 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3052 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3053 sizeof(struct igmp_grouprec)) /
3056 "%s: use previous packet", __func__);
3058 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3060 m->m_data += IGMP_LEADINGSPACE;
3062 m = m_gethdr(M_NOWAIT, MT_DATA);
3064 M_ALIGN(m, IGMP_LEADINGSPACE);
3068 "%s: m_get*() failed", __func__);
3071 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3072 igmp_save_context(m, ifp);
3073 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3074 sizeof(struct igmp_grouprec)) /
3078 "%s: allocated new packet", __func__);
3081 * Append the IGMP group record header to the
3082 * current packet's data area.
3083 * Recalculate pointer to free space for next
3084 * group record, in case m_append() allocated
3085 * a new mbuf or cluster.
3087 memset(&ig, 0, sizeof(ig));
3088 ig.ig_group = inm->inm_addr;
3089 if (!m_append(m, sizeof(ig), (void *)&ig)) {
3093 "%s: m_append() failed", __func__);
3096 npbytes += sizeof(struct igmp_grouprec);
3098 /* new packet; offset in c hain */
3099 md = m_getptr(m, npbytes -
3100 sizeof(struct igmp_grouprec), &off);
3101 pig = (struct igmp_grouprec *)(mtod(md,
3104 /* current packet; offset from last append */
3106 pig = (struct igmp_grouprec *)(mtod(md,
3107 uint8_t *) + md->m_len -
3108 sizeof(struct igmp_grouprec));
3111 * Begin walking the tree for this record type
3112 * pass, or continue from where we left off
3113 * previously if we had to allocate a new packet.
3114 * Only report deltas in-mode at t1.
3115 * We need not report included sources as allowed
3116 * if we are in inclusive mode on the group,
3117 * however the converse is not true.
3121 nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3122 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3123 CTR2(KTR_IGMPV3, "%s: visit node %s",
3124 __func__, inet_ntoa_haddr(ims->ims_haddr));
3125 now = ims_get_mode(inm, ims, 1);
3126 then = ims_get_mode(inm, ims, 0);
3127 CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3128 __func__, then, now);
3131 "%s: skip unchanged", __func__);
3134 if (mode == MCAST_EXCLUDE &&
3135 now == MCAST_INCLUDE) {
3137 "%s: skip IN src on EX group",
3141 nrt = (rectype_t)now;
3142 if (nrt == REC_NONE)
3143 nrt = (rectype_t)(~mode & REC_FULL);
3144 if (schanged++ == 0) {
3146 } else if (crt != nrt)
3148 naddr = htonl(ims->ims_haddr);
3149 if (!m_append(m, sizeof(in_addr_t),
3154 "%s: m_append() failed", __func__);
3157 nallow += !!(crt == REC_ALLOW);
3158 nblock += !!(crt == REC_BLOCK);
3159 if (++rsrcs == m0srcs)
3163 * If we did not append any tree nodes on this
3164 * pass, back out of allocations.
3167 npbytes -= sizeof(struct igmp_grouprec);
3170 "%s: m_free(m)", __func__);
3174 "%s: m_adj(m, -ig)", __func__);
3175 m_adj(m, -((int)sizeof(
3176 struct igmp_grouprec)));
3180 npbytes += (rsrcs * sizeof(in_addr_t));
3181 if (crt == REC_ALLOW)
3182 pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3183 else if (crt == REC_BLOCK)
3184 pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3185 pig->ig_numsrc = htons(rsrcs);
3187 * Count the new group record, and enqueue this
3188 * packet if it wasn't already queued.
3190 m->m_pkthdr.PH_vt.vt_nrecs++;
3192 mbufq_enqueue(mq, m);
3194 } while (nims != NULL);
3196 crt = (~crt & REC_FULL);
3199 CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3206 igmp_v3_merge_state_changes(struct in_multi *inm, struct mbufq *scq)
3209 struct mbuf *m; /* pending state-change */
3210 struct mbuf *m0; /* copy of pending state-change */
3211 struct mbuf *mt; /* last state-change in packet */
3212 int docopy, domerge;
3219 IN_MULTI_LOCK_ASSERT();
3223 * If there are further pending retransmissions, make a writable
3224 * copy of each queued state-change message before merging.
3226 if (inm->inm_scrv > 0)
3231 if (mbufq_first(gq) == NULL) {
3232 CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3237 m = mbufq_first(gq);
3240 * Only merge the report into the current packet if
3241 * there is sufficient space to do so; an IGMPv3 report
3242 * packet may only contain 65,535 group records.
3243 * Always use a simple mbuf chain concatentation to do this,
3244 * as large state changes for single groups may have
3245 * allocated clusters.
3248 mt = mbufq_last(scq);
3250 recslen = m_length(m, NULL);
3252 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
3253 m->m_pkthdr.PH_vt.vt_nrecs <=
3254 IGMP_V3_REPORT_MAXRECS) &&
3255 (mt->m_pkthdr.len + recslen <=
3256 (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3260 if (!domerge && mbufq_full(gq)) {
3262 "%s: outbound queue full, skipping whole packet %p",
3272 CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3273 m0 = mbufq_dequeue(gq);
3276 CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3277 m0 = m_dup(m, M_NOWAIT);
3280 m0->m_nextpkt = NULL;
3285 CTR3(KTR_IGMPV3, "%s: queueing %p to scq %p)",
3287 mbufq_enqueue(scq, m0);
3289 struct mbuf *mtl; /* last mbuf of packet mt */
3291 CTR3(KTR_IGMPV3, "%s: merging %p with scq tail %p)",
3295 m0->m_flags &= ~M_PKTHDR;
3296 mt->m_pkthdr.len += recslen;
3297 mt->m_pkthdr.PH_vt.vt_nrecs +=
3298 m0->m_pkthdr.PH_vt.vt_nrecs;
3308 * Respond to a pending IGMPv3 General Query.
3311 igmp_v3_dispatch_general_query(struct igmp_ifinfo *igi)
3313 struct ifmultiaddr *ifma;
3315 struct in_multi *inm;
3318 IN_MULTI_LOCK_ASSERT();
3321 KASSERT(igi->igi_version == IGMP_VERSION_3,
3322 ("%s: called when version %d", __func__, igi->igi_version));
3327 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3328 if (ifma->ifma_addr->sa_family != AF_INET ||
3329 ifma->ifma_protospec == NULL)
3332 inm = (struct in_multi *)ifma->ifma_protospec;
3333 KASSERT(ifp == inm->inm_ifp,
3334 ("%s: inconsistent ifp", __func__));
3336 switch (inm->inm_state) {
3337 case IGMP_NOT_MEMBER:
3338 case IGMP_SILENT_MEMBER:
3340 case IGMP_REPORTING_MEMBER:
3341 case IGMP_IDLE_MEMBER:
3342 case IGMP_LAZY_MEMBER:
3343 case IGMP_SLEEPING_MEMBER:
3344 case IGMP_AWAKENING_MEMBER:
3345 inm->inm_state = IGMP_REPORTING_MEMBER;
3346 retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3348 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3351 case IGMP_G_QUERY_PENDING_MEMBER:
3352 case IGMP_SG_QUERY_PENDING_MEMBER:
3353 case IGMP_LEAVING_MEMBER:
3357 IF_ADDR_RUNLOCK(ifp);
3359 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3360 igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3363 * Slew transmission of bursts over 500ms intervals.
3365 if (mbufq_first(&igi->igi_gq) != NULL) {
3366 igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3367 IGMP_RESPONSE_BURST_INTERVAL);
3368 V_interface_timers_running = 1;
3373 * Transmit the next pending IGMP message in the output queue.
3375 * We get called from netisr_processqueue(). A mutex private to igmpoq
3376 * will be acquired and released around this routine.
3378 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3379 * MRT: Nothing needs to be done, as IGMP traffic is always local to
3380 * a link and uses a link-scope multicast address.
3383 igmp_intr(struct mbuf *m)
3385 struct ip_moptions imo;
3387 struct mbuf *ipopts, *m0;
3391 CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3394 * Set VNET image pointer from enqueued mbuf chain
3395 * before doing anything else. Whilst we use interface
3396 * indexes to guard against interface detach, they are
3397 * unique to each VIMAGE and must be retrieved.
3399 CURVNET_SET((struct vnet *)(m->m_pkthdr.PH_loc.ptr));
3400 ifindex = igmp_restore_context(m);
3403 * Check if the ifnet still exists. This limits the scope of
3404 * any race in the absence of a global ifp lock for low cost
3405 * (an array lookup).
3407 ifp = ifnet_byindex(ifindex);
3409 CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3410 __func__, m, ifindex);
3412 IPSTAT_INC(ips_noroute);
3416 ipopts = V_igmp_sendra ? m_raopt : NULL;
3418 imo.imo_multicast_ttl = 1;
3419 imo.imo_multicast_vif = -1;
3420 imo.imo_multicast_loop = (V_ip_mrouter != NULL);
3423 * If the user requested that IGMP traffic be explicitly
3424 * redirected to the loopback interface (e.g. they are running a
3425 * MANET interface and the routing protocol needs to see the
3426 * updates), handle this now.
3428 if (m->m_flags & M_IGMP_LOOP)
3429 imo.imo_multicast_ifp = V_loif;
3431 imo.imo_multicast_ifp = ifp;
3433 if (m->m_flags & M_IGMPV2) {
3436 m0 = igmp_v3_encap_report(ifp, m);
3438 CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3440 IPSTAT_INC(ips_odropped);
3445 igmp_scrub_context(m0);
3447 m0->m_pkthdr.rcvif = V_loif;
3449 mac_netinet_igmp_send(ifp, m0);
3451 error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3453 CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3457 IGMPSTAT_INC(igps_snd_reports);
3461 * We must restore the existing vnet pointer before
3462 * continuing as we are run from netisr context.
3468 * Encapsulate an IGMPv3 report.
3470 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3471 * chain has already had its IP/IGMPv3 header prepended. In this case
3472 * the function will not attempt to prepend; the lengths and checksums
3473 * will however be re-computed.
3475 * Returns a pointer to the new mbuf chain head, or NULL if the
3476 * allocation failed.
3478 static struct mbuf *
3479 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3481 struct igmp_report *igmp;
3483 int hdrlen, igmpreclen;
3485 KASSERT((m->m_flags & M_PKTHDR),
3486 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3488 igmpreclen = m_length(m, NULL);
3489 hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3491 if (m->m_flags & M_IGMPV3_HDR) {
3492 igmpreclen -= hdrlen;
3494 M_PREPEND(m, hdrlen, M_NOWAIT);
3497 m->m_flags |= M_IGMPV3_HDR;
3500 CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3502 m->m_data += sizeof(struct ip);
3503 m->m_len -= sizeof(struct ip);
3505 igmp = mtod(m, struct igmp_report *);
3506 igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3509 igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3511 igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3512 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3514 m->m_data -= sizeof(struct ip);
3515 m->m_len += sizeof(struct ip);
3517 ip = mtod(m, struct ip *);
3518 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3519 ip->ip_len = htons(hdrlen + igmpreclen);
3520 ip->ip_off = htons(IP_DF);
3521 ip->ip_p = IPPROTO_IGMP;
3524 ip->ip_src.s_addr = INADDR_ANY;
3526 if (m->m_flags & M_IGMP_LOOP) {
3527 struct in_ifaddr *ia;
3531 ip->ip_src = ia->ia_addr.sin_addr;
3532 ifa_free(&ia->ia_ifa);
3536 ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3543 igmp_rec_type_to_str(const int type)
3547 case IGMP_CHANGE_TO_EXCLUDE_MODE:
3550 case IGMP_CHANGE_TO_INCLUDE_MODE:
3553 case IGMP_MODE_IS_EXCLUDE:
3556 case IGMP_MODE_IS_INCLUDE:
3559 case IGMP_ALLOW_NEW_SOURCES:
3562 case IGMP_BLOCK_OLD_SOURCES:
3573 igmp_init(void *unused __unused)
3576 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3580 m_raopt = igmp_ra_alloc();
3582 netisr_register(&igmp_nh);
3584 SYSINIT(igmp_init, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_init, NULL);
3587 igmp_uninit(void *unused __unused)
3590 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3592 netisr_unregister(&igmp_nh);
3597 IGMP_LOCK_DESTROY();
3599 SYSUNINIT(igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_MIDDLE, igmp_uninit, NULL);
3602 vnet_igmp_init(const void *unused __unused)
3605 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3607 LIST_INIT(&V_igi_head);
3609 VNET_SYSINIT(vnet_igmp_init, SI_SUB_PSEUDO, SI_ORDER_ANY, vnet_igmp_init,
3613 vnet_igmp_uninit(const void *unused __unused)
3616 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3618 KASSERT(LIST_EMPTY(&V_igi_head),
3619 ("%s: igi list not empty; ifnets not detached?", __func__));
3621 VNET_SYSUNINIT(vnet_igmp_uninit, SI_SUB_PSEUDO, SI_ORDER_ANY,
3622 vnet_igmp_uninit, NULL);
3625 igmp_modevent(module_t mod, int type, void *unused __unused)
3633 return (EOPNOTSUPP);
3638 static moduledata_t igmp_mod = {
3643 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);