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>
62 #include <sys/vimage.h>
64 #include <sys/condvar.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>
78 #include <netinet/vinet.h>
80 #include <machine/in_cksum.h>
82 #include <security/mac/mac_framework.h>
85 #define KTR_IGMPV3 KTR_INET
88 static struct igmp_ifinfo *
89 igi_alloc_locked(struct ifnet *);
90 static void igi_delete_locked(const struct ifnet *);
91 static void igmp_dispatch_queue(struct ifqueue *, int, const int);
92 static void igmp_fasttimo_vnet(void);
93 static void igmp_final_leave(struct in_multi *, struct igmp_ifinfo *);
94 static int igmp_handle_state_change(struct in_multi *,
95 struct igmp_ifinfo *);
96 static int igmp_initial_join(struct in_multi *, struct igmp_ifinfo *);
97 static int igmp_input_v1_query(struct ifnet *, const struct ip *,
99 static int igmp_input_v2_query(struct ifnet *, const struct ip *,
100 const struct igmp *);
101 static int igmp_input_v3_query(struct ifnet *, const struct ip *,
102 /*const*/ struct igmpv3 *);
103 static int igmp_input_v3_group_query(struct in_multi *,
104 struct igmp_ifinfo *, int, /*const*/ struct igmpv3 *);
105 static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
106 /*const*/ struct igmp *);
107 static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
108 /*const*/ struct igmp *);
109 static void igmp_intr(struct mbuf *);
110 static int igmp_isgroupreported(const struct in_addr);
114 static char * igmp_rec_type_to_str(const int);
116 static void igmp_set_version(struct igmp_ifinfo *, const int);
117 static void igmp_slowtimo_vnet(void);
118 static void igmp_sysinit(void);
119 static int igmp_v1v2_queue_report(struct in_multi *, const int);
120 static void igmp_v1v2_process_group_timer(struct in_multi *, const int);
121 static void igmp_v1v2_process_querier_timers(struct igmp_ifinfo *);
122 static void igmp_v2_update_group(struct in_multi *, const int);
123 static void igmp_v3_cancel_link_timers(struct igmp_ifinfo *);
124 static void igmp_v3_dispatch_general_query(struct igmp_ifinfo *);
126 igmp_v3_encap_report(struct ifnet *, struct mbuf *);
127 static int igmp_v3_enqueue_group_record(struct ifqueue *,
128 struct in_multi *, const int, const int, const int);
129 static int igmp_v3_enqueue_filter_change(struct ifqueue *,
131 static void igmp_v3_process_group_timers(struct igmp_ifinfo *,
132 struct ifqueue *, struct ifqueue *, struct in_multi *,
134 static int igmp_v3_merge_state_changes(struct in_multi *,
136 static void igmp_v3_suppress_group_record(struct in_multi *);
137 static int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
138 static int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
139 static int sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
141 static vnet_attach_fn vnet_igmp_iattach;
142 static vnet_detach_fn vnet_igmp_idetach;
144 static const struct netisr_handler igmp_nh = {
146 .nh_handler = igmp_intr,
147 .nh_proto = NETISR_IGMP,
148 .nh_policy = NETISR_POLICY_SOURCE,
152 * System-wide globals.
154 * Unlocked access to these is OK, except for the global IGMP output
155 * queue. The IGMP subsystem lock ends up being system-wide for the moment,
156 * because all VIMAGEs have to share a global output queue, as netisrs
157 * themselves are not virtualized.
160 * * The permitted lock order is: IN_MULTI_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
161 * Any may be taken independently; if any are held at the same
162 * time, the above lock order must be followed.
163 * * All output is delegated to the netisr.
164 * Now that Giant has been eliminated, the netisr may be inlined.
165 * * IN_MULTI_LOCK covers in_multi.
166 * * IGMP_LOCK covers igmp_ifinfo and any global variables in this file,
167 * including the output queue.
168 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
169 * per-link state iterators.
170 * * igmp_ifinfo is valid as long as PF_INET is attached to the interface,
171 * therefore it is not refcounted.
172 * We allow unlocked reads of igmp_ifinfo when accessed via in_multi.
175 * * IGMP acquires its own reference every time an in_multi is passed to
176 * it and the group is being joined for the first time.
177 * * IGMP releases its reference(s) on in_multi in a deferred way,
178 * because the operations which process the release run as part of
179 * a loop whose control variables are directly affected by the release
180 * (that, and not recursing on the IF_ADDR_LOCK).
182 * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
183 * to a vnet in ifp->if_vnet.
185 * SMPng: XXX We may potentially race operations on ifma_protospec.
186 * The problem is that we currently lack a clean way of taking the
187 * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
188 * as anything which modifies ifma needs to be covered by that lock.
189 * So check for ifma_protospec being NULL before proceeding.
193 struct mbuf *m_raopt; /* Router Alert option */
194 MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
197 * VIMAGE-wide globals.
199 * The IGMPv3 timers themselves need to run per-image, however,
200 * protosw timers run globally (see tcp).
201 * An ifnet can only be in one vimage at a time, and the loopback
202 * ifnet, loif, is itself virtualized.
203 * It would otherwise be possible to seriously hose IGMP state,
204 * and create inconsistencies in upstream multicast routing, if you have
205 * multiple VIMAGEs running on the same link joining different multicast
206 * groups, UNLESS the "primary IP address" is different. This is because
207 * IGMP for IPv4 does not force link-local addresses to be used for each
208 * node, unlike MLD for IPv6.
209 * Obviously the IGMPv3 per-interface state has per-vimage granularity
212 * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
213 * policy to control the address used by IGMP on the link.
215 #ifdef VIMAGE_GLOBALS
216 int interface_timers_running; /* IGMPv3 general query response */
217 int state_change_timers_running; /* IGMPv3 state-change retransmit */
218 int current_state_timers_running; /* IGMPv1/v2 host report;
219 * IGMPv3 g/sg query response */
221 LIST_HEAD(, igmp_ifinfo) igi_head;
222 struct igmpstat igmpstat;
223 struct timeval igmp_gsrdelay;
225 int igmp_recvifkludge;
231 int igmp_default_version;
232 #endif /* VIMAGE_GLOBALS */
235 * Virtualized sysctls.
237 SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_igmp, IGMPCTL_STATS, stats,
238 CTLFLAG_RW, igmpstat, igmpstat, "");
239 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, recvifkludge,
240 CTLFLAG_RW, igmp_recvifkludge, 0,
241 "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
242 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, sendra,
243 CTLFLAG_RW, igmp_sendra, 0,
244 "Send IP Router Alert option in IGMPv2/v3 messages");
245 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, sendlocal,
246 CTLFLAG_RW, igmp_sendlocal, 0,
247 "Send IGMP membership reports for 224.0.0.0/24 groups");
248 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, v1enable,
249 CTLFLAG_RW, igmp_v1enable, 0,
250 "Enable backwards compatibility with IGMPv1");
251 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, v2enable,
252 CTLFLAG_RW, igmp_v2enable, 0,
253 "Enable backwards compatibility with IGMPv2");
254 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, legacysupp,
255 CTLFLAG_RW, igmp_legacysupp, 0,
256 "Allow v1/v2 reports to suppress v3 group responses");
257 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, default_version,
258 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, igmp_default_version, 0,
259 sysctl_igmp_default_version, "I",
260 "Default version of IGMP to run on each interface");
261 SYSCTL_V_PROC(V_NET, vnet_inet, _net_inet_igmp, OID_AUTO, gsrdelay,
262 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, igmp_gsrdelay.tv_sec, 0,
263 sysctl_igmp_gsr, "I",
264 "Rate limit for IGMPv3 Group-and-Source queries in seconds");
267 * Non-virtualized sysctls.
269 SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo, CTLFLAG_RD | CTLFLAG_MPSAFE,
270 sysctl_igmp_ifinfo, "Per-interface IGMPv3 state");
273 igmp_save_context(struct mbuf *m, struct ifnet *ifp)
277 m->m_pkthdr.header = ifp->if_vnet;
279 m->m_pkthdr.flowid = ifp->if_index;
283 igmp_scrub_context(struct mbuf *m)
286 m->m_pkthdr.header = NULL;
287 m->m_pkthdr.flowid = 0;
291 static __inline char *
292 inet_ntoa_haddr(in_addr_t haddr)
296 ia.s_addr = htonl(haddr);
297 return (inet_ntoa(ia));
302 * Restore context from a queued IGMP output chain.
303 * Return saved ifindex.
305 * VIMAGE: The assertion is there to make sure that we
306 * actually called CURVNET_SET() with what's in the mbuf chain.
308 static __inline uint32_t
309 igmp_restore_context(struct mbuf *m)
313 #if defined(VIMAGE) && defined(INVARIANTS)
314 KASSERT(curvnet == (m->m_pkthdr.header),
315 ("%s: called when curvnet was not restored", __func__));
318 return (m->m_pkthdr.flowid);
322 * Retrieve or set default IGMP version.
324 * VIMAGE: Assume curvnet set by caller.
325 * SMPng: NOTE: Serialized by IGMP lock.
328 sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
330 INIT_VNET_INET(curvnet);
334 error = sysctl_wire_old_buffer(req, sizeof(int));
340 new = V_igmp_default_version;
342 error = sysctl_handle_int(oidp, &new, 0, req);
343 if (error || !req->newptr)
346 if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
351 CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
352 V_igmp_default_version, new);
354 V_igmp_default_version = new;
362 * Retrieve or set threshold between group-source queries in seconds.
364 * VIMAGE: Assume curvnet set by caller.
365 * SMPng: NOTE: Serialized by IGMP lock.
368 sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
370 INIT_VNET_INET(curvnet);
374 error = sysctl_wire_old_buffer(req, sizeof(int));
380 i = V_igmp_gsrdelay.tv_sec;
382 error = sysctl_handle_int(oidp, &i, 0, req);
383 if (error || !req->newptr)
386 if (i < -1 || i >= 60) {
391 CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
392 V_igmp_gsrdelay.tv_sec, i);
393 V_igmp_gsrdelay.tv_sec = i;
401 * Expose struct igmp_ifinfo to userland, keyed by ifindex.
402 * For use by ifmcstat(8).
404 * SMPng: NOTE: Does an unlocked ifindex space read.
405 * VIMAGE: Assume curvnet set by caller. The node handler itself
406 * is not directly virtualized.
409 sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
411 INIT_VNET_NET(curvnet);
412 INIT_VNET_INET(curvnet);
417 struct igmp_ifinfo *igi;
422 if (req->newptr != NULL)
428 error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
435 if (name[0] <= 0 || name[0] > V_if_index) {
442 ifp = ifnet_byindex(name[0]);
446 LIST_FOREACH(igi, &V_igi_head, igi_link) {
447 if (ifp == igi->igi_ifp) {
448 error = SYSCTL_OUT(req, igi,
449 sizeof(struct igmp_ifinfo));
461 * Dispatch an entire queue of pending packet chains
463 * VIMAGE: Assumes the vnet pointer has been set.
466 igmp_dispatch_queue(struct ifqueue *ifq, int limit, const int loop)
474 CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, ifq, m);
476 m->m_flags |= M_IGMP_LOOP;
477 netisr_dispatch(NETISR_IGMP, m);
484 * Filter outgoing IGMP report state by group.
486 * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
487 * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
488 * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
489 * this may break certain IGMP snooping switches which rely on the old
492 * Return zero if the given group is one for which IGMP reports
493 * should be suppressed, or non-zero if reports should be issued.
496 igmp_isgroupreported(const struct in_addr addr)
498 INIT_VNET_INET(curvnet);
500 if (in_allhosts(addr) ||
501 ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
508 * Construct a Router Alert option to use in outgoing packets.
516 MGET(m, M_DONTWAIT, MT_DATA);
517 p = mtod(m, struct ipoption *);
518 p->ipopt_dst.s_addr = INADDR_ANY;
519 p->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
520 p->ipopt_list[1] = 0x04; /* 4 bytes long */
521 p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */
522 p->ipopt_list[3] = 0x00; /* pad byte */
523 m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
529 * Attach IGMP when PF_INET is attached to an interface.
532 igmp_domifattach(struct ifnet *ifp)
534 struct igmp_ifinfo *igi;
536 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
537 __func__, ifp, ifp->if_xname);
541 igi = igi_alloc_locked(ifp);
542 if (!(ifp->if_flags & IFF_MULTICAST))
543 igi->igi_flags |= IGIF_SILENT;
551 * VIMAGE: assume curvnet set by caller.
553 static struct igmp_ifinfo *
554 igi_alloc_locked(/*const*/ struct ifnet *ifp)
556 INIT_VNET_INET(ifp->if_vnet);
557 struct igmp_ifinfo *igi;
561 igi = malloc(sizeof(struct igmp_ifinfo), M_IGMP, M_NOWAIT|M_ZERO);
566 igi->igi_version = V_igmp_default_version;
568 igi->igi_rv = IGMP_RV_INIT;
569 igi->igi_qi = IGMP_QI_INIT;
570 igi->igi_qri = IGMP_QRI_INIT;
571 igi->igi_uri = IGMP_URI_INIT;
573 SLIST_INIT(&igi->igi_relinmhead);
576 * Responses to general queries are subject to bounds.
578 IFQ_SET_MAXLEN(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
580 LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
582 CTR2(KTR_IGMPV3, "allocate igmp_ifinfo for ifp %p(%s)",
592 * NOTE: Some finalization tasks need to run before the protocol domain
593 * is detached, but also before the link layer does its cleanup.
595 * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
596 * XXX This is also bitten by unlocked ifma_protospec access.
599 igmp_ifdetach(struct ifnet *ifp)
601 struct igmp_ifinfo *igi;
602 struct ifmultiaddr *ifma;
603 struct in_multi *inm, *tinm;
605 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
610 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
611 if (igi->igi_version == IGMP_VERSION_3) {
613 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
614 if (ifma->ifma_addr->sa_family != AF_INET ||
615 ifma->ifma_protospec == NULL)
618 KASSERT(ifma->ifma_protospec != NULL,
619 ("%s: ifma_protospec is NULL", __func__));
621 inm = (struct in_multi *)ifma->ifma_protospec;
622 if (inm->inm_state == IGMP_LEAVING_MEMBER) {
623 SLIST_INSERT_HEAD(&igi->igi_relinmhead,
626 inm_clear_recorded(inm);
630 * Free the in_multi reference(s) for this IGMP lifecycle.
632 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead, inm_nrele,
634 SLIST_REMOVE_HEAD(&igi->igi_relinmhead, inm_nrele);
635 inm_release_locked(inm);
643 * Hook for domifdetach.
646 igmp_domifdetach(struct ifnet *ifp)
648 struct igmp_ifinfo *igi;
650 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
651 __func__, ifp, ifp->if_xname);
655 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
656 igi_delete_locked(ifp);
662 igi_delete_locked(const struct ifnet *ifp)
664 INIT_VNET_INET(ifp->if_vnet);
665 struct igmp_ifinfo *igi, *tigi;
667 CTR3(KTR_IGMPV3, "%s: freeing igmp_ifinfo for ifp %p(%s)",
668 __func__, ifp, ifp->if_xname);
672 LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
673 if (igi->igi_ifp == ifp) {
675 * Free deferred General Query responses.
677 _IF_DRAIN(&igi->igi_gq);
679 LIST_REMOVE(igi, igi_link);
681 KASSERT(SLIST_EMPTY(&igi->igi_relinmhead),
682 ("%s: there are dangling in_multi references",
691 panic("%s: igmp_ifinfo not found for ifp %p\n", __func__, ifp);
696 * Process a received IGMPv1 query.
697 * Return non-zero if the message should be dropped.
699 * VIMAGE: The curvnet pointer is derived from the input ifp.
702 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
703 const struct igmp *igmp)
705 INIT_VNET_INET(ifp->if_vnet);
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:
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 INIT_VNET_INET(ifp->if_vnet);
792 struct ifmultiaddr *ifma;
793 struct igmp_ifinfo *igi;
794 struct in_multi *inm;
795 int is_general_query;
798 is_general_query = 0;
801 * Validate address fields upfront.
802 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
804 if (in_nullhost(igmp->igmp_group)) {
806 * IGMPv2 General Query.
807 * If this was not sent to the all-hosts group, ignore it.
809 if (!in_allhosts(ip->ip_dst))
811 IGMPSTAT_INC(igps_rcv_gen_queries);
812 is_general_query = 1;
814 /* IGMPv2 Group-Specific Query. */
815 IGMPSTAT_INC(igps_rcv_group_queries);
821 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
822 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
824 if (igi->igi_flags & IGIF_LOOPBACK) {
825 CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
831 * Ignore v2 query if in v1 Compatibility Mode.
833 if (igi->igi_version == IGMP_VERSION_1)
836 igmp_set_version(igi, IGMP_VERSION_2);
838 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
842 if (is_general_query) {
844 * For each reporting group joined on this
845 * interface, kick the report timer.
847 CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
850 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
851 if (ifma->ifma_addr->sa_family != AF_INET ||
852 ifma->ifma_protospec == NULL)
854 inm = (struct in_multi *)ifma->ifma_protospec;
855 igmp_v2_update_group(inm, timer);
860 * Group-specific IGMPv2 query, we need only
861 * look up the single group to process it.
863 inm = inm_lookup(ifp, igmp->igmp_group);
865 CTR3(KTR_IGMPV3, "process v2 query %s on ifp %p(%s)",
866 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
867 igmp_v2_update_group(inm, timer);
879 * Update the report timer on a group in response to an IGMPv2 query.
881 * If we are becoming the reporting member for this group, start the timer.
882 * If we already are the reporting member for this group, and timer is
883 * below the threshold, reset it.
885 * We may be updating the group for the first time since we switched
886 * to IGMPv3. If we are, then we must clear any recorded source lists,
887 * and transition to REPORTING state; the group timer is overloaded
888 * for group and group-source query responses.
890 * Unlike IGMPv3, the delay per group should be jittered
891 * to avoid bursts of IGMPv2 reports.
894 igmp_v2_update_group(struct in_multi *inm, const int timer)
896 INIT_VNET_INET(curvnet);
898 CTR4(KTR_IGMPV3, "%s: %s/%s timer=%d", __func__,
899 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname, timer);
901 IN_MULTI_LOCK_ASSERT();
903 switch (inm->inm_state) {
904 case IGMP_NOT_MEMBER:
905 case IGMP_SILENT_MEMBER:
907 case IGMP_REPORTING_MEMBER:
908 if (inm->inm_timer != 0 &&
909 inm->inm_timer <= timer) {
910 CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
911 "skipping.", __func__);
915 case IGMP_SG_QUERY_PENDING_MEMBER:
916 case IGMP_G_QUERY_PENDING_MEMBER:
917 case IGMP_IDLE_MEMBER:
918 case IGMP_LAZY_MEMBER:
919 case IGMP_AWAKENING_MEMBER:
920 CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
921 inm->inm_state = IGMP_REPORTING_MEMBER;
922 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
923 V_current_state_timers_running = 1;
925 case IGMP_SLEEPING_MEMBER:
926 CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
927 inm->inm_state = IGMP_AWAKENING_MEMBER;
929 case IGMP_LEAVING_MEMBER:
935 * Process a received IGMPv3 general, group-specific or
936 * group-and-source-specific query.
937 * Assumes m has already been pulled up to the full IGMP message length.
938 * Return 0 if successful, otherwise an appropriate error code is returned.
941 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
942 /*const*/ struct igmpv3 *igmpv3)
944 INIT_VNET_INET(ifp->if_vnet);
945 struct igmp_ifinfo *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_ifinfo 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 %s query on ifp %p(%s)",
1081 inet_ntoa(igmpv3->igmp_group), 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 recieved IGMPv3 group-specific or group-and-source-specific
1103 * Return <0 if any error occured. Currently this is ignored.
1106 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifinfo *igi,
1107 int timer, /*const*/ struct igmpv3 *igmpv3)
1109 INIT_VNET_INET(curvnet);
1113 IN_MULTI_LOCK_ASSERT();
1118 switch (inm->inm_state) {
1119 case IGMP_NOT_MEMBER:
1120 case IGMP_SILENT_MEMBER:
1121 case IGMP_SLEEPING_MEMBER:
1122 case IGMP_LAZY_MEMBER:
1123 case IGMP_AWAKENING_MEMBER:
1124 case IGMP_IDLE_MEMBER:
1125 case IGMP_LEAVING_MEMBER:
1128 case IGMP_REPORTING_MEMBER:
1129 case IGMP_G_QUERY_PENDING_MEMBER:
1130 case IGMP_SG_QUERY_PENDING_MEMBER:
1134 nsrc = ntohs(igmpv3->igmp_numsrc);
1137 * Deal with group-specific queries upfront.
1138 * If any group query is already pending, purge any recorded
1139 * source-list state if it exists, and schedule a query response
1140 * for this group-specific query.
1143 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1144 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1145 inm_clear_recorded(inm);
1146 timer = min(inm->inm_timer, timer);
1148 inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1149 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1150 V_current_state_timers_running = 1;
1155 * Deal with the case where a group-and-source-specific query has
1156 * been received but a group-specific query is already pending.
1158 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1159 timer = min(inm->inm_timer, timer);
1160 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1161 V_current_state_timers_running = 1;
1166 * Finally, deal with the case where a group-and-source-specific
1167 * query has been received, where a response to a previous g-s-r
1168 * query exists, or none exists.
1169 * In this case, we need to parse the source-list which the Querier
1170 * has provided us with and check if we have any source list filter
1171 * entries at T1 for these sources. If we do not, there is no need
1172 * schedule a report and the query may be dropped.
1173 * If we do, we must record them and schedule a current-state
1174 * report for those sources.
1175 * FIXME: Handling source lists larger than 1 mbuf requires that
1176 * we pass the mbuf chain pointer down to this function, and use
1177 * m_getptr() to walk the chain.
1179 if (inm->inm_nsrc > 0) {
1180 const struct in_addr *ap;
1183 ap = (const struct in_addr *)(igmpv3 + 1);
1185 for (i = 0; i < nsrc; i++, ap++) {
1186 retval = inm_record_source(inm, ap->s_addr);
1189 nrecorded += retval;
1191 if (nrecorded > 0) {
1193 "%s: schedule response to SG query", __func__);
1194 inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1195 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1196 V_current_state_timers_running = 1;
1204 * Process a received IGMPv1 host membership report.
1206 * NOTE: 0.0.0.0 workaround breaks const correctness.
1209 igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1210 /*const*/ struct igmp *igmp)
1212 INIT_VNET_INET(ifp->if_vnet);
1213 struct in_ifaddr *ia;
1214 struct in_multi *inm;
1216 IGMPSTAT_INC(igps_rcv_reports);
1218 if (ifp->if_flags & IFF_LOOPBACK)
1221 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr) ||
1222 !in_hosteq(igmp->igmp_group, ip->ip_dst))) {
1223 IGMPSTAT_INC(igps_rcv_badreports);
1228 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1229 * Booting clients may use the source address 0.0.0.0. Some
1230 * IGMP daemons may not know how to use IP_RECVIF to determine
1231 * the interface upon which this message was received.
1232 * Replace 0.0.0.0 with the subnet address if told to do so.
1234 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1237 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1238 ifa_free(&ia->ia_ifa);
1242 CTR3(KTR_IGMPV3, "process v1 report %s on ifp %p(%s)",
1243 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1246 * IGMPv1 report suppression.
1247 * If we are a member of this group, and our membership should be
1248 * reported, stop our group timer and transition to the 'lazy' state.
1251 inm = inm_lookup(ifp, igmp->igmp_group);
1253 struct igmp_ifinfo *igi;
1257 KASSERT(igi != NULL,
1258 ("%s: no igi for ifp %p", __func__, ifp));
1262 IGMPSTAT_INC(igps_rcv_ourreports);
1265 * If we are in IGMPv3 host mode, do not allow the
1266 * other host's IGMPv1 report to suppress our reports
1267 * unless explicitly configured to do so.
1269 if (igi->igi_version == IGMP_VERSION_3) {
1270 if (V_igmp_legacysupp)
1271 igmp_v3_suppress_group_record(inm);
1277 switch (inm->inm_state) {
1278 case IGMP_NOT_MEMBER:
1279 case IGMP_SILENT_MEMBER:
1281 case IGMP_IDLE_MEMBER:
1282 case IGMP_LAZY_MEMBER:
1283 case IGMP_AWAKENING_MEMBER:
1285 "report suppressed for %s on ifp %p(%s)",
1286 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1287 case IGMP_SLEEPING_MEMBER:
1288 inm->inm_state = IGMP_SLEEPING_MEMBER;
1290 case IGMP_REPORTING_MEMBER:
1292 "report suppressed for %s on ifp %p(%s)",
1293 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1294 if (igi->igi_version == IGMP_VERSION_1)
1295 inm->inm_state = IGMP_LAZY_MEMBER;
1296 else if (igi->igi_version == IGMP_VERSION_2)
1297 inm->inm_state = IGMP_SLEEPING_MEMBER;
1299 case IGMP_G_QUERY_PENDING_MEMBER:
1300 case IGMP_SG_QUERY_PENDING_MEMBER:
1301 case IGMP_LEAVING_MEMBER:
1313 * Process a received IGMPv2 host membership report.
1315 * NOTE: 0.0.0.0 workaround breaks const correctness.
1318 igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1319 /*const*/ struct igmp *igmp)
1321 INIT_VNET_INET(ifp->if_vnet);
1322 struct in_ifaddr *ia;
1323 struct in_multi *inm;
1326 * Make sure we don't hear our own membership report. Fast
1327 * leave requires knowing that we are the only member of a
1331 if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1332 ifa_free(&ia->ia_ifa);
1336 IGMPSTAT_INC(igps_rcv_reports);
1338 if (ifp->if_flags & IFF_LOOPBACK) {
1340 ifa_free(&ia->ia_ifa);
1344 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1345 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1347 ifa_free(&ia->ia_ifa);
1348 IGMPSTAT_INC(igps_rcv_badreports);
1353 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1354 * Booting clients may use the source address 0.0.0.0. Some
1355 * IGMP daemons may not know how to use IP_RECVIF to determine
1356 * the interface upon which this message was received.
1357 * Replace 0.0.0.0 with the subnet address if told to do so.
1359 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1361 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1364 ifa_free(&ia->ia_ifa);
1366 CTR3(KTR_IGMPV3, "process v2 report %s on ifp %p(%s)",
1367 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1370 * IGMPv2 report suppression.
1371 * If we are a member of this group, and our membership should be
1372 * reported, and our group timer is pending or about to be reset,
1373 * stop our group timer by transitioning to the 'lazy' state.
1376 inm = inm_lookup(ifp, igmp->igmp_group);
1378 struct igmp_ifinfo *igi;
1381 KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1383 IGMPSTAT_INC(igps_rcv_ourreports);
1386 * If we are in IGMPv3 host mode, do not allow the
1387 * other host's IGMPv1 report to suppress our reports
1388 * unless explicitly configured to do so.
1390 if (igi->igi_version == IGMP_VERSION_3) {
1391 if (V_igmp_legacysupp)
1392 igmp_v3_suppress_group_record(inm);
1398 switch (inm->inm_state) {
1399 case IGMP_NOT_MEMBER:
1400 case IGMP_SILENT_MEMBER:
1401 case IGMP_SLEEPING_MEMBER:
1403 case IGMP_REPORTING_MEMBER:
1404 case IGMP_IDLE_MEMBER:
1405 case IGMP_AWAKENING_MEMBER:
1407 "report suppressed for %s on ifp %p(%s)",
1408 inet_ntoa(igmp->igmp_group), ifp, ifp->if_xname);
1409 case IGMP_LAZY_MEMBER:
1410 inm->inm_state = IGMP_LAZY_MEMBER;
1412 case IGMP_G_QUERY_PENDING_MEMBER:
1413 case IGMP_SG_QUERY_PENDING_MEMBER:
1414 case IGMP_LEAVING_MEMBER:
1426 igmp_input(struct mbuf *m, int off)
1436 CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, m, off);
1438 ifp = m->m_pkthdr.rcvif;
1439 INIT_VNET_INET(ifp->if_vnet);
1441 IGMPSTAT_INC(igps_rcv_total);
1443 ip = mtod(m, struct ip *);
1445 igmplen = ip->ip_len;
1450 if (igmplen < IGMP_MINLEN) {
1451 IGMPSTAT_INC(igps_rcv_tooshort);
1457 * Always pullup to the minimum size for v1/v2 or v3
1458 * to amortize calls to m_pullup().
1461 if (igmplen >= IGMP_V3_QUERY_MINLEN)
1462 minlen += IGMP_V3_QUERY_MINLEN;
1464 minlen += IGMP_MINLEN;
1465 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
1466 (m = m_pullup(m, minlen)) == 0) {
1467 IGMPSTAT_INC(igps_rcv_tooshort);
1470 ip = mtod(m, struct ip *);
1472 if (ip->ip_ttl != 1) {
1473 IGMPSTAT_INC(igps_rcv_badttl);
1479 * Validate checksum.
1481 m->m_data += iphlen;
1483 igmp = mtod(m, struct igmp *);
1484 if (in_cksum(m, igmplen)) {
1485 IGMPSTAT_INC(igps_rcv_badsum);
1489 m->m_data -= iphlen;
1492 switch (igmp->igmp_type) {
1493 case IGMP_HOST_MEMBERSHIP_QUERY:
1494 if (igmplen == IGMP_MINLEN) {
1495 if (igmp->igmp_code == 0)
1496 queryver = IGMP_VERSION_1;
1498 queryver = IGMP_VERSION_2;
1499 } else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1500 queryver = IGMP_VERSION_3;
1502 IGMPSTAT_INC(igps_rcv_tooshort);
1508 case IGMP_VERSION_1:
1509 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1510 if (!V_igmp_v1enable)
1512 if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1518 case IGMP_VERSION_2:
1519 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1520 if (!V_igmp_v2enable)
1522 if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
1528 case IGMP_VERSION_3: {
1529 struct igmpv3 *igmpv3;
1534 IGMPSTAT_INC(igps_rcv_v3_queries);
1535 igmpv3 = (struct igmpv3 *)igmp;
1537 * Validate length based on source count.
1539 nsrc = ntohs(igmpv3->igmp_numsrc);
1540 srclen = sizeof(struct in_addr) * nsrc;
1541 if (nsrc * sizeof(in_addr_t) > srclen) {
1542 IGMPSTAT_INC(igps_rcv_tooshort);
1546 * m_pullup() may modify m, so pullup in
1549 igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1551 if ((m->m_flags & M_EXT ||
1552 m->m_len < igmpv3len) &&
1553 (m = m_pullup(m, igmpv3len)) == NULL) {
1554 IGMPSTAT_INC(igps_rcv_tooshort);
1557 igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1559 if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1568 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1569 if (!V_igmp_v1enable)
1571 if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1577 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1578 if (!V_igmp_v2enable)
1580 if (!ip_checkrouteralert(m))
1581 IGMPSTAT_INC(igps_rcv_nora);
1582 if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
1588 case IGMP_v3_HOST_MEMBERSHIP_REPORT:
1590 * Hosts do not need to process IGMPv3 membership reports,
1591 * as report suppression is no longer required.
1593 if (!ip_checkrouteralert(m))
1594 IGMPSTAT_INC(igps_rcv_nora);
1602 * Pass all valid IGMP packets up to any process(es) listening on a
1610 * Fast timeout handler (global).
1611 * VIMAGE: Timeout handlers are expected to service all vimages.
1616 VNET_ITERATOR_DECL(vnet_iter);
1619 VNET_FOREACH(vnet_iter) {
1620 CURVNET_SET(vnet_iter);
1621 igmp_fasttimo_vnet();
1624 VNET_LIST_RUNLOCK();
1628 * Fast timeout handler (per-vnet).
1629 * Sends are shuffled off to a netisr to deal with Giant.
1631 * VIMAGE: Assume caller has set up our curvnet.
1634 igmp_fasttimo_vnet(void)
1636 INIT_VNET_INET(curvnet);
1637 struct ifqueue scq; /* State-change packets */
1638 struct ifqueue qrq; /* Query response packets */
1640 struct igmp_ifinfo *igi;
1641 struct ifmultiaddr *ifma, *tifma;
1642 struct in_multi *inm;
1643 int loop, uri_fasthz;
1649 * Quick check to see if any work needs to be done, in order to
1650 * minimize the overhead of fasttimo processing.
1651 * SMPng: XXX Unlocked reads.
1653 if (!V_current_state_timers_running &&
1654 !V_interface_timers_running &&
1655 !V_state_change_timers_running)
1662 * IGMPv3 General Query response timer processing.
1664 if (V_interface_timers_running) {
1665 CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1667 V_interface_timers_running = 0;
1668 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1669 if (igi->igi_v3_timer == 0) {
1671 } else if (--igi->igi_v3_timer == 0) {
1672 igmp_v3_dispatch_general_query(igi);
1674 V_interface_timers_running = 1;
1679 if (!V_current_state_timers_running &&
1680 !V_state_change_timers_running)
1683 V_current_state_timers_running = 0;
1684 V_state_change_timers_running = 0;
1686 CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1689 * IGMPv1/v2/v3 host report and state-change timer processing.
1690 * Note: Processing a v3 group timer may remove a node.
1692 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1695 if (igi->igi_version == IGMP_VERSION_3) {
1696 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1697 uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1700 memset(&qrq, 0, sizeof(struct ifqueue));
1701 IFQ_SET_MAXLEN(&qrq, IGMP_MAX_G_GS_PACKETS);
1703 memset(&scq, 0, sizeof(struct ifqueue));
1704 IFQ_SET_MAXLEN(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1708 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link,
1710 if (ifma->ifma_addr->sa_family != AF_INET ||
1711 ifma->ifma_protospec == NULL)
1713 inm = (struct in_multi *)ifma->ifma_protospec;
1714 switch (igi->igi_version) {
1715 case IGMP_VERSION_1:
1716 case IGMP_VERSION_2:
1717 igmp_v1v2_process_group_timer(inm,
1720 case IGMP_VERSION_3:
1721 igmp_v3_process_group_timers(igi, &qrq,
1722 &scq, inm, uri_fasthz);
1726 IF_ADDR_UNLOCK(ifp);
1728 if (igi->igi_version == IGMP_VERSION_3) {
1729 struct in_multi *tinm;
1731 igmp_dispatch_queue(&qrq, 0, loop);
1732 igmp_dispatch_queue(&scq, 0, loop);
1735 * Free the in_multi reference(s) for this
1738 SLIST_FOREACH_SAFE(inm, &igi->igi_relinmhead,
1740 SLIST_REMOVE_HEAD(&igi->igi_relinmhead,
1742 inm_release_locked(inm);
1753 * Update host report group timer for IGMPv1/v2.
1754 * Will update the global pending timer flags.
1757 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1759 INIT_VNET_INET(curvnet);
1760 int report_timer_expired;
1762 IN_MULTI_LOCK_ASSERT();
1765 if (inm->inm_timer == 0) {
1766 report_timer_expired = 0;
1767 } else if (--inm->inm_timer == 0) {
1768 report_timer_expired = 1;
1770 V_current_state_timers_running = 1;
1774 switch (inm->inm_state) {
1775 case IGMP_NOT_MEMBER:
1776 case IGMP_SILENT_MEMBER:
1777 case IGMP_IDLE_MEMBER:
1778 case IGMP_LAZY_MEMBER:
1779 case IGMP_SLEEPING_MEMBER:
1780 case IGMP_AWAKENING_MEMBER:
1782 case IGMP_REPORTING_MEMBER:
1783 if (report_timer_expired) {
1784 inm->inm_state = IGMP_IDLE_MEMBER;
1785 (void)igmp_v1v2_queue_report(inm,
1786 (version == IGMP_VERSION_2) ?
1787 IGMP_v2_HOST_MEMBERSHIP_REPORT :
1788 IGMP_v1_HOST_MEMBERSHIP_REPORT);
1791 case IGMP_G_QUERY_PENDING_MEMBER:
1792 case IGMP_SG_QUERY_PENDING_MEMBER:
1793 case IGMP_LEAVING_MEMBER:
1799 * Update a group's timers for IGMPv3.
1800 * Will update the global pending timer flags.
1801 * Note: Unlocked read from igi.
1804 igmp_v3_process_group_timers(struct igmp_ifinfo *igi,
1805 struct ifqueue *qrq, struct ifqueue *scq,
1806 struct in_multi *inm, const int uri_fasthz)
1808 INIT_VNET_INET(curvnet);
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 %s/%s", __func__,
1897 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
1900 * If we are leaving the group for good, make sure
1901 * we release IGMP's reference to it.
1902 * This release must be deferred using a SLIST,
1903 * as we are called from a loop which traverses
1904 * the in_ifmultiaddr TAILQ.
1906 if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1907 inm->inm_scrv == 0) {
1908 inm->inm_state = IGMP_NOT_MEMBER;
1909 SLIST_INSERT_HEAD(&igi->igi_relinmhead,
1919 * Suppress a group's pending response to a group or source/group query.
1921 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1922 * Do NOT update ST1/ST0 as this operation merely suppresses
1923 * the currently pending group record.
1924 * Do NOT suppress the response to a general query. It is possible but
1925 * it would require adding another state or flag.
1928 igmp_v3_suppress_group_record(struct in_multi *inm)
1931 IN_MULTI_LOCK_ASSERT();
1933 KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1934 ("%s: not IGMPv3 mode on link", __func__));
1936 if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1937 inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1940 if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1941 inm_clear_recorded(inm);
1944 inm->inm_state = IGMP_REPORTING_MEMBER;
1948 * Switch to a different IGMP version on the given interface,
1949 * as per Section 7.2.1.
1952 igmp_set_version(struct igmp_ifinfo *igi, const int version)
1954 int old_version_timer;
1958 CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1959 version, igi->igi_ifp, igi->igi_ifp->if_xname);
1961 if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1963 * Compute the "Older Version Querier Present" timer as per
1966 old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1967 old_version_timer *= PR_SLOWHZ;
1969 if (version == IGMP_VERSION_1) {
1970 igi->igi_v1_timer = old_version_timer;
1971 igi->igi_v2_timer = 0;
1972 } else if (version == IGMP_VERSION_2) {
1973 igi->igi_v1_timer = 0;
1974 igi->igi_v2_timer = old_version_timer;
1978 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
1979 if (igi->igi_version != IGMP_VERSION_2) {
1980 igi->igi_version = IGMP_VERSION_2;
1981 igmp_v3_cancel_link_timers(igi);
1983 } else if (igi->igi_v1_timer > 0) {
1984 if (igi->igi_version != IGMP_VERSION_1) {
1985 igi->igi_version = IGMP_VERSION_1;
1986 igmp_v3_cancel_link_timers(igi);
1992 * Cancel pending IGMPv3 timers for the given link and all groups
1993 * joined on it; state-change, general-query, and group-query timers.
1995 * Only ever called on a transition from v3 to Compatibility mode. Kill
1996 * the timers stone dead (this may be expensive for large N groups), they
1997 * will be restarted if Compatibility Mode deems that they must be due to
2001 igmp_v3_cancel_link_timers(struct igmp_ifinfo *igi)
2003 struct ifmultiaddr *ifma;
2005 struct in_multi *inm;
2007 CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2008 igi->igi_ifp, igi->igi_ifp->if_xname);
2010 IN_MULTI_LOCK_ASSERT();
2014 * Stop the v3 General Query Response on this link stone dead.
2015 * If fasttimo is woken up due to V_interface_timers_running,
2016 * the flag will be cleared if there are no pending link timers.
2018 igi->igi_v3_timer = 0;
2021 * Now clear the current-state and state-change report timers
2022 * for all memberships scoped to this link.
2026 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2027 if (ifma->ifma_addr->sa_family != AF_INET ||
2028 ifma->ifma_protospec == NULL)
2030 inm = (struct in_multi *)ifma->ifma_protospec;
2031 switch (inm->inm_state) {
2032 case IGMP_NOT_MEMBER:
2033 case IGMP_SILENT_MEMBER:
2034 case IGMP_IDLE_MEMBER:
2035 case IGMP_LAZY_MEMBER:
2036 case IGMP_SLEEPING_MEMBER:
2037 case IGMP_AWAKENING_MEMBER:
2039 * These states are either not relevant in v3 mode,
2040 * or are unreported. Do nothing.
2043 case IGMP_LEAVING_MEMBER:
2045 * If we are leaving the group and switching to
2046 * compatibility mode, we need to release the final
2047 * reference held for issuing the INCLUDE {}, and
2048 * transition to REPORTING to ensure the host leave
2049 * message is sent upstream to the old querier --
2050 * transition to NOT would lose the leave and race.
2052 * SMPNG: Must drop and re-acquire IF_ADDR_LOCK
2053 * around inm_release_locked(), as it is not
2054 * a recursive mutex.
2056 IF_ADDR_UNLOCK(ifp);
2057 inm_release_locked(inm);
2060 case IGMP_G_QUERY_PENDING_MEMBER:
2061 case IGMP_SG_QUERY_PENDING_MEMBER:
2062 inm_clear_recorded(inm);
2064 case IGMP_REPORTING_MEMBER:
2065 inm->inm_state = IGMP_REPORTING_MEMBER;
2069 * Always clear state-change and group report timers.
2070 * Free any pending IGMPv3 state-change records.
2072 inm->inm_sctimer = 0;
2074 _IF_DRAIN(&inm->inm_scq);
2076 IF_ADDR_UNLOCK(ifp);
2080 * Update the Older Version Querier Present timers for a link.
2081 * See Section 7.2.1 of RFC 3376.
2084 igmp_v1v2_process_querier_timers(struct igmp_ifinfo *igi)
2086 INIT_VNET_INET(curvnet);
2090 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2092 * IGMPv1 and IGMPv2 Querier Present timers expired.
2096 if (igi->igi_version != IGMP_VERSION_3) {
2098 "%s: transition from v%d -> v%d on %p(%s)",
2099 __func__, igi->igi_version, IGMP_VERSION_3,
2100 igi->igi_ifp, igi->igi_ifp->if_xname);
2101 igi->igi_version = IGMP_VERSION_3;
2103 } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2105 * IGMPv1 Querier Present timer expired,
2106 * IGMPv2 Querier Present timer running.
2107 * If IGMPv2 was disabled since last timeout,
2109 * If IGMPv2 is enabled, revert to IGMPv2.
2111 if (!V_igmp_v2enable) {
2113 "%s: transition from v%d -> v%d on %p(%s)",
2114 __func__, igi->igi_version, IGMP_VERSION_3,
2115 igi->igi_ifp, igi->igi_ifp->if_xname);
2116 igi->igi_v2_timer = 0;
2117 igi->igi_version = IGMP_VERSION_3;
2119 --igi->igi_v2_timer;
2120 if (igi->igi_version != IGMP_VERSION_2) {
2122 "%s: transition from v%d -> v%d on %p(%s)",
2123 __func__, igi->igi_version, IGMP_VERSION_2,
2124 igi->igi_ifp, igi->igi_ifp->if_xname);
2125 igi->igi_version = IGMP_VERSION_2;
2128 } else if (igi->igi_v1_timer > 0) {
2130 * IGMPv1 Querier Present timer running.
2131 * Stop IGMPv2 timer if running.
2133 * If IGMPv1 was disabled since last timeout,
2135 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2137 if (!V_igmp_v1enable) {
2139 "%s: transition from v%d -> v%d on %p(%s)",
2140 __func__, igi->igi_version, IGMP_VERSION_3,
2141 igi->igi_ifp, igi->igi_ifp->if_xname);
2142 igi->igi_v1_timer = 0;
2143 igi->igi_version = IGMP_VERSION_3;
2145 --igi->igi_v1_timer;
2147 if (igi->igi_v2_timer > 0) {
2149 "%s: cancel v2 timer on %p(%s)",
2150 __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2151 igi->igi_v2_timer = 0;
2157 * Global slowtimo handler.
2158 * VIMAGE: Timeout handlers are expected to service all vimages.
2163 VNET_ITERATOR_DECL(vnet_iter);
2166 VNET_FOREACH(vnet_iter) {
2167 CURVNET_SET(vnet_iter);
2168 igmp_slowtimo_vnet();
2171 VNET_LIST_RUNLOCK();
2175 * Per-vnet slowtimo handler.
2178 igmp_slowtimo_vnet(void)
2180 INIT_VNET_INET(curvnet);
2181 struct igmp_ifinfo *igi;
2185 LIST_FOREACH(igi, &V_igi_head, igi_link) {
2186 igmp_v1v2_process_querier_timers(igi);
2193 * Dispatch an IGMPv1/v2 host report or leave message.
2194 * These are always small enough to fit inside a single mbuf.
2197 igmp_v1v2_queue_report(struct in_multi *inm, const int type)
2204 IN_MULTI_LOCK_ASSERT();
2209 MGETHDR(m, M_DONTWAIT, MT_DATA);
2212 MH_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
2214 m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2216 m->m_data += sizeof(struct ip);
2217 m->m_len = sizeof(struct igmp);
2219 igmp = mtod(m, struct igmp *);
2220 igmp->igmp_type = type;
2221 igmp->igmp_code = 0;
2222 igmp->igmp_group = inm->inm_addr;
2223 igmp->igmp_cksum = 0;
2224 igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
2226 m->m_data -= sizeof(struct ip);
2227 m->m_len += sizeof(struct ip);
2229 ip = mtod(m, struct ip *);
2231 ip->ip_len = sizeof(struct ip) + sizeof(struct igmp);
2233 ip->ip_p = IPPROTO_IGMP;
2234 ip->ip_src.s_addr = INADDR_ANY;
2236 if (type == IGMP_HOST_LEAVE_MESSAGE)
2237 ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2239 ip->ip_dst = inm->inm_addr;
2241 igmp_save_context(m, ifp);
2243 m->m_flags |= M_IGMPV2;
2244 if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2245 m->m_flags |= M_IGMP_LOOP;
2247 CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2248 netisr_dispatch(NETISR_IGMP, m);
2254 * Process a state change from the upper layer for the given IPv4 group.
2256 * Each socket holds a reference on the in_multi in its own ip_moptions.
2257 * The socket layer will have made the necessary updates to.the group
2258 * state, it is now up to IGMP to issue a state change report if there
2259 * has been any change between T0 (when the last state-change was issued)
2262 * We use the IGMPv3 state machine at group level. The IGMP module
2263 * however makes the decision as to which IGMP protocol version to speak.
2264 * A state change *from* INCLUDE {} always means an initial join.
2265 * A state change *to* INCLUDE {} always means a final leave.
2267 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2268 * save ourselves a bunch of work; any exclusive mode groups need not
2269 * compute source filter lists.
2271 * VIMAGE: curvnet should have been set by caller, as this routine
2272 * is called from the socket option handlers.
2275 igmp_change_state(struct in_multi *inm)
2277 struct igmp_ifinfo *igi;
2281 IN_MULTI_LOCK_ASSERT();
2286 * Try to detect if the upper layer just asked us to change state
2287 * for an interface which has now gone away.
2289 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2290 ifp = inm->inm_ifma->ifma_ifp;
2293 * Sanity check that netinet's notion of ifp is the
2296 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2301 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2302 KASSERT(igi != NULL, ("%s: no igmp_ifinfo for ifp %p", __func__, ifp));
2305 * If we detect a state transition to or from MCAST_UNDEFINED
2306 * for this group, then we are starting or finishing an IGMP
2307 * life cycle for this group.
2309 if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2310 CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2311 inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2312 if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2313 CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2314 error = igmp_initial_join(inm, igi);
2316 } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2317 CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2318 igmp_final_leave(inm, igi);
2322 CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2325 error = igmp_handle_state_change(inm, igi);
2333 * Perform the initial join for an IGMP group.
2335 * When joining a group:
2336 * If the group should have its IGMP traffic suppressed, do nothing.
2337 * IGMPv1 starts sending IGMPv1 host membership reports.
2338 * IGMPv2 starts sending IGMPv2 host membership reports.
2339 * IGMPv3 will schedule an IGMPv3 state-change report containing the
2340 * initial state of the membership.
2343 igmp_initial_join(struct in_multi *inm, struct igmp_ifinfo *igi)
2345 INIT_VNET_INET(curvnet);
2347 struct ifqueue *ifq;
2348 int error, retval, syncstates;
2350 CTR4(KTR_IGMPV3, "%s: initial join %s on ifp %p(%s)",
2351 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2352 inm->inm_ifp->if_xname);
2359 IN_MULTI_LOCK_ASSERT();
2362 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2365 * Groups joined on loopback or marked as 'not reported',
2366 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2367 * are never reported in any IGMP protocol exchanges.
2368 * All other groups enter the appropriate IGMP state machine
2369 * for the version in use on this link.
2370 * A link marked as IGIF_SILENT causes IGMP to be completely
2371 * disabled for the link.
2373 if ((ifp->if_flags & IFF_LOOPBACK) ||
2374 (igi->igi_flags & IGIF_SILENT) ||
2375 !igmp_isgroupreported(inm->inm_addr)) {
2377 "%s: not kicking state machine for silent group", __func__);
2378 inm->inm_state = IGMP_SILENT_MEMBER;
2382 * Deal with overlapping in_multi lifecycle.
2383 * If this group was LEAVING, then make sure
2384 * we drop the reference we picked up to keep the
2385 * group around for the final INCLUDE {} enqueue.
2387 if (igi->igi_version == IGMP_VERSION_3 &&
2388 inm->inm_state == IGMP_LEAVING_MEMBER)
2389 inm_release_locked(inm);
2391 inm->inm_state = IGMP_REPORTING_MEMBER;
2393 switch (igi->igi_version) {
2394 case IGMP_VERSION_1:
2395 case IGMP_VERSION_2:
2396 inm->inm_state = IGMP_IDLE_MEMBER;
2397 error = igmp_v1v2_queue_report(inm,
2398 (igi->igi_version == IGMP_VERSION_2) ?
2399 IGMP_v2_HOST_MEMBERSHIP_REPORT :
2400 IGMP_v1_HOST_MEMBERSHIP_REPORT);
2402 inm->inm_timer = IGMP_RANDOM_DELAY(
2403 IGMP_V1V2_MAX_RI * PR_FASTHZ);
2404 V_current_state_timers_running = 1;
2408 case IGMP_VERSION_3:
2410 * Defer update of T0 to T1, until the first copy
2411 * of the state change has been transmitted.
2416 * Immediately enqueue a State-Change Report for
2417 * this interface, freeing any previous reports.
2418 * Don't kick the timers if there is nothing to do,
2419 * or if an error occurred.
2421 ifq = &inm->inm_scq;
2423 retval = igmp_v3_enqueue_group_record(ifq, inm, 1,
2425 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2428 error = retval * -1;
2433 * Schedule transmission of pending state-change
2434 * report up to RV times for this link. The timer
2435 * will fire at the next igmp_fasttimo (~200ms),
2436 * giving us an opportunity to merge the reports.
2438 if (igi->igi_flags & IGIF_LOOPBACK) {
2441 KASSERT(igi->igi_rv > 1,
2442 ("%s: invalid robustness %d", __func__,
2444 inm->inm_scrv = igi->igi_rv;
2446 inm->inm_sctimer = 1;
2447 V_state_change_timers_running = 1;
2455 * Only update the T0 state if state change is atomic,
2456 * i.e. we don't need to wait for a timer to fire before we
2457 * can consider the state change to have been communicated.
2461 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2462 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2469 * Issue an intermediate state change during the IGMP life-cycle.
2472 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifinfo *igi)
2474 INIT_VNET_INET(curvnet);
2478 CTR4(KTR_IGMPV3, "%s: state change for %s on ifp %p(%s)",
2479 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2480 inm->inm_ifp->if_xname);
2484 IN_MULTI_LOCK_ASSERT();
2487 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2489 if ((ifp->if_flags & IFF_LOOPBACK) ||
2490 (igi->igi_flags & IGIF_SILENT) ||
2491 !igmp_isgroupreported(inm->inm_addr) ||
2492 (igi->igi_version != IGMP_VERSION_3)) {
2493 if (!igmp_isgroupreported(inm->inm_addr)) {
2495 "%s: not kicking state machine for silent group", __func__);
2497 CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2499 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2500 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2504 _IF_DRAIN(&inm->inm_scq);
2506 retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2507 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2512 * If record(s) were enqueued, start the state-change
2513 * report timer for this group.
2515 inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2516 inm->inm_sctimer = 1;
2517 V_state_change_timers_running = 1;
2523 * Perform the final leave for an IGMP group.
2525 * When leaving a group:
2526 * IGMPv1 does nothing.
2527 * IGMPv2 sends a host leave message, if and only if we are the reporter.
2528 * IGMPv3 enqueues a state-change report containing a transition
2529 * to INCLUDE {} for immediate transmission.
2532 igmp_final_leave(struct in_multi *inm, struct igmp_ifinfo *igi)
2534 INIT_VNET_INET(curvnet);
2539 CTR4(KTR_IGMPV3, "%s: final leave %s on ifp %p(%s)",
2540 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp,
2541 inm->inm_ifp->if_xname);
2543 IN_MULTI_LOCK_ASSERT();
2546 switch (inm->inm_state) {
2547 case IGMP_NOT_MEMBER:
2548 case IGMP_SILENT_MEMBER:
2549 case IGMP_LEAVING_MEMBER:
2550 /* Already leaving or left; do nothing. */
2552 "%s: not kicking state machine for silent group", __func__);
2554 case IGMP_REPORTING_MEMBER:
2555 case IGMP_IDLE_MEMBER:
2556 case IGMP_G_QUERY_PENDING_MEMBER:
2557 case IGMP_SG_QUERY_PENDING_MEMBER:
2558 if (igi->igi_version == IGMP_VERSION_2) {
2560 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2561 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2562 panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2565 igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2566 inm->inm_state = IGMP_NOT_MEMBER;
2567 } else if (igi->igi_version == IGMP_VERSION_3) {
2569 * Stop group timer and all pending reports.
2570 * Immediately enqueue a state-change report
2571 * TO_IN {} to be sent on the next fast timeout,
2572 * giving us an opportunity to merge reports.
2574 _IF_DRAIN(&inm->inm_scq);
2576 if (igi->igi_flags & IGIF_LOOPBACK) {
2579 inm->inm_scrv = igi->igi_rv;
2581 CTR4(KTR_IGMPV3, "%s: Leaving %s/%s with %d "
2582 "pending retransmissions.", __func__,
2583 inet_ntoa(inm->inm_addr),
2584 inm->inm_ifp->if_xname, inm->inm_scrv);
2585 if (inm->inm_scrv == 0) {
2586 inm->inm_state = IGMP_NOT_MEMBER;
2587 inm->inm_sctimer = 0;
2591 inm_acquire_locked(inm);
2593 retval = igmp_v3_enqueue_group_record(
2594 &inm->inm_scq, inm, 1, 0, 0);
2595 KASSERT(retval != 0,
2596 ("%s: enqueue record = %d", __func__,
2599 inm->inm_state = IGMP_LEAVING_MEMBER;
2600 inm->inm_sctimer = 1;
2601 V_state_change_timers_running = 1;
2607 case IGMP_LAZY_MEMBER:
2608 case IGMP_SLEEPING_MEMBER:
2609 case IGMP_AWAKENING_MEMBER:
2610 /* Our reports are suppressed; do nothing. */
2616 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for %s/%s", __func__,
2617 inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2618 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2619 CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for %s/%s",
2620 __func__, inet_ntoa(inm->inm_addr), inm->inm_ifp->if_xname);
2625 * Enqueue an IGMPv3 group record to the given output queue.
2627 * XXX This function could do with having the allocation code
2628 * split out, and the multiple-tree-walks coalesced into a single
2629 * routine as has been done in igmp_v3_enqueue_filter_change().
2631 * If is_state_change is zero, a current-state record is appended.
2632 * If is_state_change is non-zero, a state-change report is appended.
2634 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2635 * If is_group_query is zero, and if there is a packet with free space
2636 * at the tail of the queue, it will be appended to providing there
2637 * is enough free space.
2638 * Otherwise a new mbuf packet chain is allocated.
2640 * If is_source_query is non-zero, each source is checked to see if
2641 * it was recorded for a Group-Source query, and will be omitted if
2642 * it is not both in-mode and recorded.
2644 * The function will attempt to allocate leading space in the packet
2645 * for the IP/IGMP header to be prepended without fragmenting the chain.
2647 * If successful the size of all data appended to the queue is returned,
2648 * otherwise an error code less than zero is returned, or zero if
2649 * no record(s) were appended.
2652 igmp_v3_enqueue_group_record(struct ifqueue *ifq, struct in_multi *inm,
2653 const int is_state_change, const int is_group_query,
2654 const int is_source_query)
2656 struct igmp_grouprec ig;
2657 struct igmp_grouprec *pig;
2659 struct ip_msource *ims, *nims;
2660 struct mbuf *m0, *m, *md;
2661 int error, is_filter_list_change;
2662 int minrec0len, m0srcs, msrcs, nbytes, off;
2663 int record_has_sources;
2669 IN_MULTI_LOCK_ASSERT();
2673 is_filter_list_change = 0;
2680 record_has_sources = 1;
2682 type = IGMP_DO_NOTHING;
2683 mode = inm->inm_st[1].iss_fmode;
2686 * If we did not transition out of ASM mode during t0->t1,
2687 * and there are no source nodes to process, we can skip
2688 * the generation of source records.
2690 if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2692 record_has_sources = 0;
2694 if (is_state_change) {
2696 * Queue a state change record.
2697 * If the mode did not change, and there are non-ASM
2698 * listeners or source filters present,
2699 * we potentially need to issue two records for the group.
2700 * If we are transitioning to MCAST_UNDEFINED, we need
2701 * not send any sources.
2702 * If there are ASM listeners, and there was no filter
2703 * mode transition of any kind, do nothing.
2705 if (mode != inm->inm_st[0].iss_fmode) {
2706 if (mode == MCAST_EXCLUDE) {
2707 CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2709 type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2711 CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2713 type = IGMP_CHANGE_TO_INCLUDE_MODE;
2714 if (mode == MCAST_UNDEFINED)
2715 record_has_sources = 0;
2718 if (record_has_sources) {
2719 is_filter_list_change = 1;
2721 type = IGMP_DO_NOTHING;
2726 * Queue a current state record.
2728 if (mode == MCAST_EXCLUDE) {
2729 type = IGMP_MODE_IS_EXCLUDE;
2730 } else if (mode == MCAST_INCLUDE) {
2731 type = IGMP_MODE_IS_INCLUDE;
2732 KASSERT(inm->inm_st[1].iss_asm == 0,
2733 ("%s: inm %p is INCLUDE but ASM count is %d",
2734 __func__, inm, inm->inm_st[1].iss_asm));
2739 * Generate the filter list changes using a separate function.
2741 if (is_filter_list_change)
2742 return (igmp_v3_enqueue_filter_change(ifq, inm));
2744 if (type == IGMP_DO_NOTHING) {
2745 CTR3(KTR_IGMPV3, "%s: nothing to do for %s/%s",
2746 __func__, inet_ntoa(inm->inm_addr),
2747 inm->inm_ifp->if_xname);
2752 * If any sources are present, we must be able to fit at least
2753 * one in the trailing space of the tail packet's mbuf,
2756 minrec0len = sizeof(struct igmp_grouprec);
2757 if (record_has_sources)
2758 minrec0len += sizeof(in_addr_t);
2760 CTR4(KTR_IGMPV3, "%s: queueing %s for %s/%s", __func__,
2761 igmp_rec_type_to_str(type), inet_ntoa(inm->inm_addr),
2762 inm->inm_ifp->if_xname);
2765 * Check if we have a packet in the tail of the queue for this
2766 * group into which the first group record for this group will fit.
2767 * Otherwise allocate a new packet.
2768 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2769 * Note: Group records for G/GSR query responses MUST be sent
2770 * in their own packet.
2773 if (!is_group_query &&
2775 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2776 (m0->m_pkthdr.len + minrec0len) <
2777 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2778 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2779 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2781 CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2783 if (_IF_QFULL(ifq)) {
2784 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2788 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2789 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2790 if (!is_state_change && !is_group_query) {
2791 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2793 m->m_data += IGMP_LEADINGSPACE;
2796 m = m_gethdr(M_DONTWAIT, MT_DATA);
2798 MH_ALIGN(m, IGMP_LEADINGSPACE);
2803 igmp_save_context(m, ifp);
2805 CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2809 * Append group record.
2810 * If we have sources, we don't know how many yet.
2815 ig.ig_group = inm->inm_addr;
2816 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2819 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2822 nbytes += sizeof(struct igmp_grouprec);
2825 * Append as many sources as will fit in the first packet.
2826 * If we are appending to a new packet, the chain allocation
2827 * may potentially use clusters; use m_getptr() in this case.
2828 * If we are appending to an existing packet, we need to obtain
2829 * a pointer to the group record after m_append(), in case a new
2830 * mbuf was allocated.
2831 * Only append sources which are in-mode at t1. If we are
2832 * transitioning to MCAST_UNDEFINED state on the group, do not
2833 * include source entries.
2834 * Only report recorded sources in our filter set when responding
2835 * to a group-source query.
2837 if (record_has_sources) {
2840 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2841 md->m_len - nbytes);
2843 md = m_getptr(m, 0, &off);
2844 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2848 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2849 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2850 inet_ntoa_haddr(ims->ims_haddr));
2851 now = ims_get_mode(inm, ims, 1);
2852 CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2853 if ((now != mode) ||
2854 (now == mode && mode == MCAST_UNDEFINED)) {
2855 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2858 if (is_source_query && ims->ims_stp == 0) {
2859 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2863 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2864 naddr = htonl(ims->ims_haddr);
2865 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2868 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2872 nbytes += sizeof(in_addr_t);
2874 if (msrcs == m0srcs)
2877 CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2879 pig->ig_numsrc = htons(msrcs);
2880 nbytes += (msrcs * sizeof(in_addr_t));
2883 if (is_source_query && msrcs == 0) {
2884 CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2891 * We are good to go with first packet.
2894 CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2895 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2896 _IF_ENQUEUE(ifq, m);
2898 m->m_pkthdr.PH_vt.vt_nrecs++;
2901 * No further work needed if no source list in packet(s).
2903 if (!record_has_sources)
2907 * Whilst sources remain to be announced, we need to allocate
2908 * a new packet and fill out as many sources as will fit.
2909 * Always try for a cluster first.
2911 while (nims != NULL) {
2912 if (_IF_QFULL(ifq)) {
2913 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2916 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2918 m->m_data += IGMP_LEADINGSPACE;
2920 m = m_gethdr(M_DONTWAIT, MT_DATA);
2922 MH_ALIGN(m, IGMP_LEADINGSPACE);
2926 igmp_save_context(m, ifp);
2927 md = m_getptr(m, 0, &off);
2928 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2929 CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2931 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2934 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2937 m->m_pkthdr.PH_vt.vt_nrecs = 1;
2938 nbytes += sizeof(struct igmp_grouprec);
2940 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2941 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2944 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2945 CTR2(KTR_IGMPV3, "%s: visit node %s", __func__,
2946 inet_ntoa_haddr(ims->ims_haddr));
2947 now = ims_get_mode(inm, ims, 1);
2948 if ((now != mode) ||
2949 (now == mode && mode == MCAST_UNDEFINED)) {
2950 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2953 if (is_source_query && ims->ims_stp == 0) {
2954 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2958 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2959 naddr = htonl(ims->ims_haddr);
2960 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2963 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2968 if (msrcs == m0srcs)
2971 pig->ig_numsrc = htons(msrcs);
2972 nbytes += (msrcs * sizeof(in_addr_t));
2974 CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
2975 _IF_ENQUEUE(ifq, m);
2982 * Type used to mark record pass completion.
2983 * We exploit the fact we can cast to this easily from the
2984 * current filter modes on each ip_msource node.
2987 REC_NONE = 0x00, /* MCAST_UNDEFINED */
2988 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
2989 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
2990 REC_FULL = REC_ALLOW | REC_BLOCK
2994 * Enqueue an IGMPv3 filter list change to the given output queue.
2996 * Source list filter state is held in an RB-tree. When the filter list
2997 * for a group is changed without changing its mode, we need to compute
2998 * the deltas between T0 and T1 for each source in the filter set,
2999 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
3001 * As we may potentially queue two record types, and the entire R-B tree
3002 * needs to be walked at once, we break this out into its own function
3003 * so we can generate a tightly packed queue of packets.
3005 * XXX This could be written to only use one tree walk, although that makes
3006 * serializing into the mbuf chains a bit harder. For now we do two walks
3007 * which makes things easier on us, and it may or may not be harder on
3010 * If successful the size of all data appended to the queue is returned,
3011 * otherwise an error code less than zero is returned, or zero if
3012 * no record(s) were appended.
3015 igmp_v3_enqueue_filter_change(struct ifqueue *ifq, struct in_multi *inm)
3017 static const int MINRECLEN =
3018 sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3020 struct igmp_grouprec ig;
3021 struct igmp_grouprec *pig;
3022 struct ip_msource *ims, *nims;
3023 struct mbuf *m, *m0, *md;
3025 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3027 uint8_t mode, now, then;
3028 rectype_t crt, drt, nrt;
3030 IN_MULTI_LOCK_ASSERT();
3032 if (inm->inm_nsrc == 0 ||
3033 (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3036 ifp = inm->inm_ifp; /* interface */
3037 mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */
3038 crt = REC_NONE; /* current group record type */
3039 drt = REC_NONE; /* mask of completed group record types */
3040 nrt = REC_NONE; /* record type for current node */
3041 m0srcs = 0; /* # source which will fit in current mbuf chain */
3042 nbytes = 0; /* # of bytes appended to group's state-change queue */
3043 npbytes = 0; /* # of bytes appended this packet */
3044 rsrcs = 0; /* # sources encoded in current record */
3045 schanged = 0; /* # nodes encoded in overall filter change */
3046 nallow = 0; /* # of source entries in ALLOW_NEW */
3047 nblock = 0; /* # of source entries in BLOCK_OLD */
3048 nims = NULL; /* next tree node pointer */
3051 * For each possible filter record mode.
3052 * The first kind of source we encounter tells us which
3053 * is the first kind of record we start appending.
3054 * If a node transitioned to UNDEFINED at t1, its mode is treated
3055 * as the inverse of the group's filter mode.
3057 while (drt != REC_FULL) {
3061 (m0->m_pkthdr.PH_vt.vt_nrecs + 1 <=
3062 IGMP_V3_REPORT_MAXRECS) &&
3063 (m0->m_pkthdr.len + MINRECLEN) <
3064 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3066 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3067 sizeof(struct igmp_grouprec)) /
3070 "%s: use previous packet", __func__);
3072 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
3074 m->m_data += IGMP_LEADINGSPACE;
3076 m = m_gethdr(M_DONTWAIT, MT_DATA);
3078 MH_ALIGN(m, IGMP_LEADINGSPACE);
3082 "%s: m_get*() failed", __func__);
3085 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3086 igmp_save_context(m, ifp);
3087 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3088 sizeof(struct igmp_grouprec)) /
3092 "%s: allocated new packet", __func__);
3095 * Append the IGMP group record header to the
3096 * current packet's data area.
3097 * Recalculate pointer to free space for next
3098 * group record, in case m_append() allocated
3099 * a new mbuf or cluster.
3101 memset(&ig, 0, sizeof(ig));
3102 ig.ig_group = inm->inm_addr;
3103 if (!m_append(m, sizeof(ig), (void *)&ig)) {
3107 "%s: m_append() failed", __func__);
3110 npbytes += sizeof(struct igmp_grouprec);
3112 /* new packet; offset in c hain */
3113 md = m_getptr(m, npbytes -
3114 sizeof(struct igmp_grouprec), &off);
3115 pig = (struct igmp_grouprec *)(mtod(md,
3118 /* current packet; offset from last append */
3120 pig = (struct igmp_grouprec *)(mtod(md,
3121 uint8_t *) + md->m_len -
3122 sizeof(struct igmp_grouprec));
3125 * Begin walking the tree for this record type
3126 * pass, or continue from where we left off
3127 * previously if we had to allocate a new packet.
3128 * Only report deltas in-mode at t1.
3129 * We need not report included sources as allowed
3130 * if we are in inclusive mode on the group,
3131 * however the converse is not true.
3135 nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3136 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3137 CTR2(KTR_IGMPV3, "%s: visit node %s",
3138 __func__, inet_ntoa_haddr(ims->ims_haddr));
3139 now = ims_get_mode(inm, ims, 1);
3140 then = ims_get_mode(inm, ims, 0);
3141 CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3142 __func__, then, now);
3145 "%s: skip unchanged", __func__);
3148 if (mode == MCAST_EXCLUDE &&
3149 now == MCAST_INCLUDE) {
3151 "%s: skip IN src on EX group",
3155 nrt = (rectype_t)now;
3156 if (nrt == REC_NONE)
3157 nrt = (rectype_t)(~mode & REC_FULL);
3158 if (schanged++ == 0) {
3160 } else if (crt != nrt)
3162 naddr = htonl(ims->ims_haddr);
3163 if (!m_append(m, sizeof(in_addr_t),
3168 "%s: m_append() failed", __func__);
3171 nallow += !!(crt == REC_ALLOW);
3172 nblock += !!(crt == REC_BLOCK);
3173 if (++rsrcs == m0srcs)
3177 * If we did not append any tree nodes on this
3178 * pass, back out of allocations.
3181 npbytes -= sizeof(struct igmp_grouprec);
3184 "%s: m_free(m)", __func__);
3188 "%s: m_adj(m, -ig)", __func__);
3189 m_adj(m, -((int)sizeof(
3190 struct igmp_grouprec)));
3194 npbytes += (rsrcs * sizeof(in_addr_t));
3195 if (crt == REC_ALLOW)
3196 pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3197 else if (crt == REC_BLOCK)
3198 pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3199 pig->ig_numsrc = htons(rsrcs);
3201 * Count the new group record, and enqueue this
3202 * packet if it wasn't already queued.
3204 m->m_pkthdr.PH_vt.vt_nrecs++;
3206 _IF_ENQUEUE(ifq, m);
3208 } while (nims != NULL);
3210 crt = (~crt & REC_FULL);
3213 CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3220 igmp_v3_merge_state_changes(struct in_multi *inm, struct ifqueue *ifscq)
3223 struct mbuf *m; /* pending state-change */
3224 struct mbuf *m0; /* copy of pending state-change */
3225 struct mbuf *mt; /* last state-change in packet */
3226 int docopy, domerge;
3233 IN_MULTI_LOCK_ASSERT();
3237 * If there are further pending retransmissions, make a writable
3238 * copy of each queued state-change message before merging.
3240 if (inm->inm_scrv > 0)
3245 if (gq->ifq_head == NULL) {
3246 CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3254 * Only merge the report into the current packet if
3255 * there is sufficient space to do so; an IGMPv3 report
3256 * packet may only contain 65,535 group records.
3257 * Always use a simple mbuf chain concatentation to do this,
3258 * as large state changes for single groups may have
3259 * allocated clusters.
3262 mt = ifscq->ifq_tail;
3264 recslen = m_length(m, NULL);
3266 if ((mt->m_pkthdr.PH_vt.vt_nrecs +
3267 m->m_pkthdr.PH_vt.vt_nrecs <=
3268 IGMP_V3_REPORT_MAXRECS) &&
3269 (mt->m_pkthdr.len + recslen <=
3270 (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3274 if (!domerge && _IF_QFULL(gq)) {
3276 "%s: outbound queue full, skipping whole packet %p",
3286 CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3287 _IF_DEQUEUE(gq, m0);
3290 CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3291 m0 = m_dup(m, M_NOWAIT);
3294 m0->m_nextpkt = NULL;
3299 CTR3(KTR_IGMPV3, "%s: queueing %p to ifscq %p)",
3300 __func__, m0, ifscq);
3301 _IF_ENQUEUE(ifscq, m0);
3303 struct mbuf *mtl; /* last mbuf of packet mt */
3305 CTR3(KTR_IGMPV3, "%s: merging %p with ifscq tail %p)",
3309 m0->m_flags &= ~M_PKTHDR;
3310 mt->m_pkthdr.len += recslen;
3311 mt->m_pkthdr.PH_vt.vt_nrecs +=
3312 m0->m_pkthdr.PH_vt.vt_nrecs;
3322 * Respond to a pending IGMPv3 General Query.
3325 igmp_v3_dispatch_general_query(struct igmp_ifinfo *igi)
3327 INIT_VNET_INET(curvnet);
3328 struct ifmultiaddr *ifma, *tifma;
3330 struct in_multi *inm;
3333 IN_MULTI_LOCK_ASSERT();
3336 KASSERT(igi->igi_version == IGMP_VERSION_3,
3337 ("%s: called when version %d", __func__, igi->igi_version));
3342 TAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, tifma) {
3343 if (ifma->ifma_addr->sa_family != AF_INET ||
3344 ifma->ifma_protospec == NULL)
3347 inm = (struct in_multi *)ifma->ifma_protospec;
3348 KASSERT(ifp == inm->inm_ifp,
3349 ("%s: inconsistent ifp", __func__));
3351 switch (inm->inm_state) {
3352 case IGMP_NOT_MEMBER:
3353 case IGMP_SILENT_MEMBER:
3355 case IGMP_REPORTING_MEMBER:
3356 case IGMP_IDLE_MEMBER:
3357 case IGMP_LAZY_MEMBER:
3358 case IGMP_SLEEPING_MEMBER:
3359 case IGMP_AWAKENING_MEMBER:
3360 inm->inm_state = IGMP_REPORTING_MEMBER;
3361 retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3363 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3366 case IGMP_G_QUERY_PENDING_MEMBER:
3367 case IGMP_SG_QUERY_PENDING_MEMBER:
3368 case IGMP_LEAVING_MEMBER:
3372 IF_ADDR_UNLOCK(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 (igi->igi_gq.ifq_head != 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.header));
3415 INIT_VNET_NET(curvnet);
3416 INIT_VNET_INET(curvnet);
3417 ifindex = igmp_restore_context(m);
3420 * Check if the ifnet still exists. This limits the scope of
3421 * any race in the absence of a global ifp lock for low cost
3422 * (an array lookup).
3424 ifp = ifnet_byindex(ifindex);
3426 CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3427 __func__, m, ifindex);
3429 IPSTAT_INC(ips_noroute);
3433 ipopts = V_igmp_sendra ? m_raopt : NULL;
3435 imo.imo_multicast_ttl = 1;
3436 imo.imo_multicast_vif = -1;
3437 imo.imo_multicast_loop = (V_ip_mrouter != NULL);
3440 * If the user requested that IGMP traffic be explicitly
3441 * redirected to the loopback interface (e.g. they are running a
3442 * MANET interface and the routing protocol needs to see the
3443 * updates), handle this now.
3445 if (m->m_flags & M_IGMP_LOOP)
3446 imo.imo_multicast_ifp = V_loif;
3448 imo.imo_multicast_ifp = ifp;
3450 if (m->m_flags & M_IGMPV2) {
3453 m0 = igmp_v3_encap_report(ifp, m);
3455 CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3457 IPSTAT_INC(ips_odropped);
3462 igmp_scrub_context(m0);
3463 m->m_flags &= ~(M_PROTOFLAGS);
3464 m0->m_pkthdr.rcvif = V_loif;
3466 mac_netinet_igmp_send(ifp, m0);
3468 error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3470 CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3474 IGMPSTAT_INC(igps_snd_reports);
3478 * We must restore the existing vnet pointer before
3479 * continuing as we are run from netisr context.
3485 * Encapsulate an IGMPv3 report.
3487 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3488 * chain has already had its IP/IGMPv3 header prepended. In this case
3489 * the function will not attempt to prepend; the lengths and checksums
3490 * will however be re-computed.
3492 * Returns a pointer to the new mbuf chain head, or NULL if the
3493 * allocation failed.
3495 static struct mbuf *
3496 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3498 INIT_VNET_INET(curvnet);
3499 struct igmp_report *igmp;
3501 int hdrlen, igmpreclen;
3503 KASSERT((m->m_flags & M_PKTHDR),
3504 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3506 igmpreclen = m_length(m, NULL);
3507 hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3509 if (m->m_flags & M_IGMPV3_HDR) {
3510 igmpreclen -= hdrlen;
3512 M_PREPEND(m, hdrlen, M_DONTWAIT);
3515 m->m_flags |= M_IGMPV3_HDR;
3518 CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3520 m->m_data += sizeof(struct ip);
3521 m->m_len -= sizeof(struct ip);
3523 igmp = mtod(m, struct igmp_report *);
3524 igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3527 igmp->ir_numgrps = htons(m->m_pkthdr.PH_vt.vt_nrecs);
3529 igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3530 m->m_pkthdr.PH_vt.vt_nrecs = 0;
3532 m->m_data -= sizeof(struct ip);
3533 m->m_len += sizeof(struct ip);
3535 ip = mtod(m, struct ip *);
3536 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3537 ip->ip_len = hdrlen + igmpreclen;
3539 ip->ip_p = IPPROTO_IGMP;
3542 ip->ip_src.s_addr = INADDR_ANY;
3544 if (m->m_flags & M_IGMP_LOOP) {
3545 struct in_ifaddr *ia;
3549 ip->ip_src = ia->ia_addr.sin_addr;
3550 ifa_free(&ia->ia_ifa);
3554 ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3561 igmp_rec_type_to_str(const int type)
3565 case IGMP_CHANGE_TO_EXCLUDE_MODE:
3568 case IGMP_CHANGE_TO_INCLUDE_MODE:
3571 case IGMP_MODE_IS_EXCLUDE:
3574 case IGMP_MODE_IS_INCLUDE:
3577 case IGMP_ALLOW_NEW_SOURCES:
3580 case IGMP_BLOCK_OLD_SOURCES:
3594 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3598 m_raopt = igmp_ra_alloc();
3600 netisr_register(&igmp_nh);
3604 igmp_sysuninit(void)
3607 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3609 netisr_unregister(&igmp_nh);
3614 IGMP_LOCK_DESTROY();
3618 * Initialize an IGMPv3 instance.
3619 * VIMAGE: Assumes curvnet set by caller and called per vimage.
3622 vnet_igmp_iattach(const void *unused __unused)
3624 INIT_VNET_INET(curvnet);
3626 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3628 LIST_INIT(&V_igi_head);
3630 V_current_state_timers_running = 0;
3631 V_state_change_timers_running = 0;
3632 V_interface_timers_running = 0;
3635 * Initialize sysctls to default values.
3637 V_igmp_recvifkludge = 1;
3639 V_igmp_sendlocal = 1;
3640 V_igmp_v1enable = 1;
3641 V_igmp_v2enable = 1;
3642 V_igmp_legacysupp = 0;
3643 V_igmp_default_version = IGMP_VERSION_3;
3644 V_igmp_gsrdelay.tv_sec = 10;
3645 V_igmp_gsrdelay.tv_usec = 0;
3647 memset(&V_igmpstat, 0, sizeof(struct igmpstat));
3648 V_igmpstat.igps_version = IGPS_VERSION_3;
3649 V_igmpstat.igps_len = sizeof(struct igmpstat);
3655 vnet_igmp_idetach(const void *unused __unused)
3658 INIT_VNET_INET(curvnet);
3661 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3663 KASSERT(LIST_EMPTY(&V_igi_head),
3664 ("%s: igi list not empty; ifnets not detached?", __func__));
3669 #ifndef VIMAGE_GLOBALS
3670 static vnet_modinfo_t vnet_igmp_modinfo = {
3671 .vmi_id = VNET_MOD_IGMP,
3673 .vmi_dependson = VNET_MOD_INET,
3674 .vmi_iattach = vnet_igmp_iattach,
3675 .vmi_idetach = vnet_igmp_idetach
3680 igmp_modevent(module_t mod, int type, void *unused __unused)
3686 #ifndef VIMAGE_GLOBALS
3687 vnet_mod_register(&vnet_igmp_modinfo);
3689 vnet_igmp_iattach(NULL);
3693 #ifndef VIMAGE_GLOBALS
3694 vnet_mod_deregister(&vnet_igmp_modinfo);
3696 vnet_igmp_idetach(NULL);
3701 return (EOPNOTSUPP);
3706 static moduledata_t igmp_mod = {
3711 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PSEUDO, SI_ORDER_ANY);