2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 2007-2009 Bruce Simpson.
30 * Copyright (c) 1988 Stephen Deering.
31 * Copyright (c) 1992, 1993
32 * The Regents of the University of California. All rights reserved.
34 * This code is derived from software contributed to Berkeley by
35 * Stephen Deering of Stanford University.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
68 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
69 * support for mandatory and extensible security protections. This notice
70 * is included in support of clause 2.2 (b) of the Apple Public License,
75 * Internet Group Management Protocol (IGMP) routines.
76 * [RFC1112, RFC2236, RFC3376]
78 * Written by Steve Deering, Stanford, May 1988.
79 * Modified by Rosen Sharma, Stanford, Aug 1994.
80 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
81 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
82 * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson.
84 * MULTICAST Revision: 3.5.1.4
87 #include <sys/cdefs.h>
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/malloc.h>
93 #include <sys/socket.h>
94 #include <sys/protosw.h>
95 #include <sys/kernel.h>
96 #include <sys/sysctl.h>
97 #include <sys/mcache.h>
99 #include <libkern/libkern.h>
100 #include <kern/zalloc.h>
103 #include <net/route.h>
105 #include <netinet/in.h>
106 #include <netinet/in_var.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/ip.h>
109 #include <netinet/ip_var.h>
110 #include <netinet/igmp.h>
111 #include <netinet/igmp_var.h>
112 #include <netinet/kpi_ipfilter_var.h>
116 inet_ntoa(struct in_addr ina
)
118 static char buf
[4*sizeof "123"];
119 unsigned char *ucp
= (unsigned char *)&ina
;
121 snprintf(buf
, sizeof(buf
), "%d.%d.%d.%d",
130 static void igi_initvar(struct igmp_ifinfo
*, struct ifnet
*, int);
131 static struct igmp_ifinfo
*igi_alloc(int);
132 static void igi_free(struct igmp_ifinfo
*);
133 static void igi_delete(const struct ifnet
*);
134 static void igmp_dispatch_queue(struct igmp_ifinfo
*, struct ifqueue
*,
135 int, const int, struct ifnet
*);
136 static void igmp_final_leave(struct in_multi
*, struct igmp_ifinfo
*);
137 static int igmp_handle_state_change(struct in_multi
*,
138 struct igmp_ifinfo
*);
139 static int igmp_initial_join(struct in_multi
*, struct igmp_ifinfo
*);
140 static int igmp_input_v1_query(struct ifnet
*, const struct ip
*,
141 const struct igmp
*);
142 static int igmp_input_v2_query(struct ifnet
*, const struct ip
*,
143 const struct igmp
*);
144 static int igmp_input_v3_query(struct ifnet
*, const struct ip
*,
145 /*const*/ struct igmpv3
*);
146 static int igmp_input_v3_group_query(struct in_multi
*,
147 int, /*const*/ struct igmpv3
*);
148 static int igmp_input_v1_report(struct ifnet
*, /*const*/ struct ip
*,
149 /*const*/ struct igmp
*);
150 static int igmp_input_v2_report(struct ifnet
*, /*const*/ struct ip
*,
151 /*const*/ struct igmp
*);
152 void igmp_sendpkt(struct mbuf
*, struct ifnet
*);
153 static __inline__
int igmp_isgroupreported(const struct in_addr
);
157 static const char * igmp_rec_type_to_str(const int);
159 static void igmp_set_version(struct igmp_ifinfo
*, const int);
160 static void igmp_flush_relq(struct igmp_ifinfo
*);
161 static int igmp_v1v2_queue_report(struct in_multi
*, const int);
162 static void igmp_v1v2_process_group_timer(struct in_multi
*, const int);
163 static void igmp_v1v2_process_querier_timers(struct igmp_ifinfo
*);
164 static void igmp_v2_update_group(struct in_multi
*, const int);
165 static void igmp_v3_cancel_link_timers(struct igmp_ifinfo
*);
166 static void igmp_v3_dispatch_general_query(struct igmp_ifinfo
*);
168 igmp_v3_encap_report(struct ifnet
*, struct mbuf
*);
169 static int igmp_v3_enqueue_group_record(struct ifqueue
*,
170 struct in_multi
*, const int, const int, const int);
171 static int igmp_v3_enqueue_filter_change(struct ifqueue
*,
173 static void igmp_v3_process_group_timers(struct igmp_ifinfo
*,
174 struct ifqueue
*, struct ifqueue
*, struct in_multi
*,
176 static int igmp_v3_merge_state_changes(struct in_multi
*,
178 static void igmp_v3_suppress_group_record(struct in_multi
*);
179 static int sysctl_igmp_ifinfo SYSCTL_HANDLER_ARGS
;
180 static int sysctl_igmp_gsr SYSCTL_HANDLER_ARGS
;
181 static int sysctl_igmp_default_version SYSCTL_HANDLER_ARGS
;
183 struct mbuf
*m_raopt
; /* Router Alert option */
185 static int interface_timers_running
; /* IGMPv3 general
187 static int state_change_timers_running
; /* IGMPv3 state-change
189 static int current_state_timers_running
; /* IGMPv1/v2 host
190 * report; IGMPv3 g/sg
193 static LIST_HEAD(, igmp_ifinfo
) igi_head
;
194 static struct igmpstat_v3 igmpstat_v3
= {
195 .igps_version
= IGPS_VERSION_3
,
196 .igps_len
= sizeof(struct igmpstat_v3
),
198 static struct igmpstat igmpstat
; /* old IGMPv2 stats structure */
199 static struct timeval igmp_gsrdelay
= {10, 0};
201 static int igmp_recvifkludge
= 1;
202 static int igmp_sendra
= 1;
203 static int igmp_sendlocal
= 1;
204 static int igmp_v1enable
= 1;
205 static int igmp_v2enable
= 1;
206 static int igmp_legacysupp
= 0;
207 static int igmp_default_version
= IGMP_VERSION_3
;
209 SYSCTL_STRUCT(_net_inet_igmp
, IGMPCTL_STATS
, stats
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
210 &igmpstat
, igmpstat
, "");
211 SYSCTL_STRUCT(_net_inet_igmp
, OID_AUTO
, v3stats
,
212 CTLFLAG_RD
| CTLFLAG_LOCKED
, &igmpstat_v3
, igmpstat_v3
, "");
213 SYSCTL_INT(_net_inet_igmp
, OID_AUTO
, recvifkludge
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
214 &igmp_recvifkludge
, 0,
215 "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
216 SYSCTL_INT(_net_inet_igmp
, OID_AUTO
, sendra
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
218 "Send IP Router Alert option in IGMPv2/v3 messages");
219 SYSCTL_INT(_net_inet_igmp
, OID_AUTO
, sendlocal
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
221 "Send IGMP membership reports for 224.0.0.0/24 groups");
222 SYSCTL_INT(_net_inet_igmp
, OID_AUTO
, v1enable
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
224 "Enable backwards compatibility with IGMPv1");
225 SYSCTL_INT(_net_inet_igmp
, OID_AUTO
, v2enable
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
227 "Enable backwards compatibility with IGMPv2");
228 SYSCTL_INT(_net_inet_igmp
, OID_AUTO
, legacysupp
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
230 "Allow v1/v2 reports to suppress v3 group responses");
231 SYSCTL_PROC(_net_inet_igmp
, OID_AUTO
, default_version
,
232 CTLTYPE_INT
| CTLFLAG_RW
,
233 &igmp_default_version
, 0, sysctl_igmp_default_version
, "I",
234 "Default version of IGMP to run on each interface");
235 SYSCTL_PROC(_net_inet_igmp
, OID_AUTO
, gsrdelay
,
236 CTLTYPE_INT
| CTLFLAG_RW
,
237 &igmp_gsrdelay
.tv_sec
, 0, sysctl_igmp_gsr
, "I",
238 "Rate limit for IGMPv3 Group-and-Source queries in seconds");
241 SYSCTL_INT(_net_inet_igmp
, OID_AUTO
,
242 debug
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &igmp_debug
, 0, "");
245 SYSCTL_NODE(_net_inet_igmp
, OID_AUTO
, ifinfo
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
246 sysctl_igmp_ifinfo
, "Per-interface IGMPv3 state");
248 /* Lock group and attribute for igmp_mtx */
249 static lck_attr_t
*igmp_mtx_attr
;
250 static lck_grp_t
*igmp_mtx_grp
;
251 static lck_grp_attr_t
*igmp_mtx_grp_attr
;
254 * Locking and reference counting:
256 * igmp_mtx mainly protects igi_head. In cases where both igmp_mtx and
257 * in_multihead_lock must be held, the former must be acquired first in order
258 * to maintain lock ordering. It is not a requirement that igmp_mtx be
259 * acquired first before in_multihead_lock, but in case both must be acquired
260 * in succession, the correct lock ordering must be followed.
262 * Instead of walking the if_multiaddrs list at the interface and returning
263 * the ifma_protospec value of a matching entry, we search the global list
264 * of in_multi records and find it that way; this is done with in_multihead
265 * lock held. Doing so avoids the race condition issues that many other BSDs
266 * suffer from (therefore in our implementation, ifma_protospec will never be
267 * NULL for as long as the in_multi is valid.)
269 * The above creates a requirement for the in_multi to stay in in_multihead
270 * list even after the final IGMP leave (in IGMPv3 mode) until no longer needs
271 * be retransmitted (this is not required for IGMPv1/v2.) In order to handle
272 * this, the request and reference counts of the in_multi are bumped up when
273 * the state changes to IGMP_LEAVING_MEMBER, and later dropped in the timeout
274 * handler. Each in_multi holds a reference to the underlying igmp_ifinfo.
276 * Thus, the permitted lock oder is:
278 * igmp_mtx, in_multihead_lock, inm_lock, igi_lock
280 * Any may be taken independently, but if any are held at the same time,
281 * the above lock order must be followed.
283 static decl_lck_mtx_data(, igmp_mtx
);
284 static int igmp_timers_are_running
;
286 #define IGI_ZONE_MAX 64 /* maximum elements in zone */
287 #define IGI_ZONE_NAME "igmp_ifinfo" /* zone name */
289 static unsigned int igi_size
; /* size of zone element */
290 static struct zone
*igi_zone
; /* zone for igmp_ifinfo */
293 static __inline
char *
294 inet_ntoa_haddr(in_addr_t haddr
)
298 ia
.s_addr
= htonl(haddr
);
299 return (inet_ntoa(ia
));
303 * Retrieve or set default IGMP version.
306 sysctl_igmp_default_version SYSCTL_HANDLER_ARGS
308 #pragma unused(oidp, arg2)
312 lck_mtx_lock(&igmp_mtx
);
314 error
= SYSCTL_OUT(req
, arg1
, sizeof(int));
315 if (error
|| !req
->newptr
)
318 new = igmp_default_version
;
320 error
= SYSCTL_IN(req
, &new, sizeof(int));
324 if (new < IGMP_VERSION_1
|| new > IGMP_VERSION_3
) {
329 IGMP_PRINTF(("change igmp_default_version from %d to %d\n",
330 igmp_default_version
, new));
332 igmp_default_version
= new;
335 lck_mtx_unlock(&igmp_mtx
);
340 * Retrieve or set threshold between group-source queries in seconds.
344 sysctl_igmp_gsr SYSCTL_HANDLER_ARGS
346 #pragma unused(arg1, arg2)
350 lck_mtx_lock(&igmp_mtx
);
352 i
= igmp_gsrdelay
.tv_sec
;
354 error
= sysctl_handle_int(oidp
, &i
, 0, req
);
355 if (error
|| !req
->newptr
)
358 if (i
< -1 || i
>= 60) {
363 igmp_gsrdelay
.tv_sec
= i
;
366 lck_mtx_unlock(&igmp_mtx
);
371 * Expose struct igmp_ifinfo to userland, keyed by ifindex.
372 * For use by ifmcstat(8).
376 sysctl_igmp_ifinfo SYSCTL_HANDLER_ARGS
383 struct igmp_ifinfo
*igi
;
384 struct igmp_ifinfo_u igi_u
;
389 if (req
->newptr
!= USER_ADDR_NULL
)
395 lck_mtx_lock(&igmp_mtx
);
397 if (name
[0] <= 0 || name
[0] > (u_int
)if_index
) {
404 ifnet_head_lock_shared();
405 ifp
= ifindex2ifnet
[name
[0]];
410 bzero(&igi_u
, sizeof (igi_u
));
412 LIST_FOREACH(igi
, &igi_head
, igi_link
) {
414 if (ifp
!= igi
->igi_ifp
) {
418 igi_u
.igi_ifindex
= igi
->igi_ifp
->if_index
;
419 igi_u
.igi_version
= igi
->igi_version
;
420 igi_u
.igi_v1_timer
= igi
->igi_v1_timer
;
421 igi_u
.igi_v2_timer
= igi
->igi_v2_timer
;
422 igi_u
.igi_v3_timer
= igi
->igi_v3_timer
;
423 igi_u
.igi_flags
= igi
->igi_flags
;
424 igi_u
.igi_rv
= igi
->igi_rv
;
425 igi_u
.igi_qi
= igi
->igi_qi
;
426 igi_u
.igi_qri
= igi
->igi_qri
;
427 igi_u
.igi_uri
= igi
->igi_uri
;
430 error
= SYSCTL_OUT(req
, &igi_u
, sizeof (igi_u
));
435 lck_mtx_unlock(&igmp_mtx
);
440 * Dispatch an entire queue of pending packet chains
442 * Must not be called with inm_lock held.
445 igmp_dispatch_queue(struct igmp_ifinfo
*igi
, struct ifqueue
*ifq
, int limit
,
446 const int loop
, struct ifnet
*ifp
)
452 IGI_LOCK_ASSERT_HELD(igi
);
458 IGMP_PRINTF(("%s: dispatch %p from %p\n", __func__
, ifq
, m
));
459 ip
= mtod(m
, struct ip
*);
461 m
->m_flags
|= M_IGMP_LOOP
;
464 igmp_sendpkt(m
, ifp
);
472 IGI_LOCK_ASSERT_HELD(igi
);
476 * Filter outgoing IGMP report state by group.
478 * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
479 * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
480 * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
481 * this may break certain IGMP snooping switches which rely on the old
484 * Return zero if the given group is one for which IGMP reports
485 * should be suppressed, or non-zero if reports should be issued.
489 int igmp_isgroupreported(const struct in_addr addr
)
492 if (in_allhosts(addr
) ||
493 ((!igmp_sendlocal
&& IN_LOCAL_GROUP(ntohl(addr
.s_addr
)))))
500 * Construct a Router Alert option to use in outgoing packets.
508 MGET(m
, M_WAITOK
, MT_DATA
);
509 p
= mtod(m
, struct ipoption
*);
510 p
->ipopt_dst
.s_addr
= INADDR_ANY
;
511 p
->ipopt_list
[0] = IPOPT_RA
; /* Router Alert Option */
512 p
->ipopt_list
[1] = 0x04; /* 4 bytes long */
513 p
->ipopt_list
[2] = IPOPT_EOL
; /* End of IP option list */
514 p
->ipopt_list
[3] = 0x00; /* pad byte */
515 m
->m_len
= sizeof(p
->ipopt_dst
) + p
->ipopt_list
[1];
521 * Attach IGMP when PF_INET is attached to an interface.
524 igmp_domifattach(struct ifnet
*ifp
, int how
)
526 struct igmp_ifinfo
*igi
;
528 IGMP_PRINTF(("%s: called for ifp %p(%s)\n",
529 __func__
, ifp
, ifp
->if_name
));
531 igi
= igi_alloc(how
);
535 lck_mtx_lock(&igmp_mtx
);
538 igi_initvar(igi
, ifp
, 0);
539 igi
->igi_debug
|= IFD_ATTACHED
;
540 IGI_ADDREF_LOCKED(igi
); /* hold a reference for igi_head */
541 IGI_ADDREF_LOCKED(igi
); /* hold a reference for caller */
544 LIST_INSERT_HEAD(&igi_head
, igi
, igi_link
);
546 lck_mtx_unlock(&igmp_mtx
);
548 IGMP_PRINTF(("allocate igmp_ifinfo for ifp %p(%s)\n",
555 * Attach IGMP when PF_INET is reattached to an interface. Caller is
556 * expected to have an outstanding reference to the igi.
559 igmp_domifreattach(struct igmp_ifinfo
*igi
)
563 lck_mtx_lock(&igmp_mtx
);
566 VERIFY(!(igi
->igi_debug
& IFD_ATTACHED
));
569 igi_initvar(igi
, ifp
, 1);
570 igi
->igi_debug
|= IFD_ATTACHED
;
571 IGI_ADDREF_LOCKED(igi
); /* hold a reference for igi_head */
574 LIST_INSERT_HEAD(&igi_head
, igi
, igi_link
);
576 lck_mtx_unlock(&igmp_mtx
);
578 IGMP_PRINTF(("reattached igmp_ifinfo for ifp %p(%s)\n",
583 * Hook for domifdetach.
586 igmp_domifdetach(struct ifnet
*ifp
)
588 IGMP_PRINTF(("%s: called for ifp %p(%s%d)\n",
589 __func__
, ifp
, ifp
->if_name
, ifp
->if_unit
));
591 lck_mtx_lock(&igmp_mtx
);
593 lck_mtx_unlock(&igmp_mtx
);
597 * Called at interface detach time. Note that we only flush all deferred
598 * responses and record releases; all remaining inm records and their source
599 * entries related to this interface are left intact, in order to handle
603 igi_delete(const struct ifnet
*ifp
)
605 struct igmp_ifinfo
*igi
, *tigi
;
607 lck_mtx_assert(&igmp_mtx
, LCK_MTX_ASSERT_OWNED
);
609 LIST_FOREACH_SAFE(igi
, &igi_head
, igi_link
, tigi
) {
611 if (igi
->igi_ifp
== ifp
) {
613 * Free deferred General Query responses.
615 IF_DRAIN(&igi
->igi_gq
);
616 IF_DRAIN(&igi
->igi_v2q
);
617 igmp_flush_relq(igi
);
618 VERIFY(SLIST_EMPTY(&igi
->igi_relinmhead
));
619 igi
->igi_debug
&= ~IFD_ATTACHED
;
622 LIST_REMOVE(igi
, igi_link
);
623 IGI_REMREF(igi
); /* release igi_head reference */
628 panic("%s: igmp_ifinfo not found for ifp %p\n", __func__
, ifp
);
632 igi_initvar(struct igmp_ifinfo
*igi
, struct ifnet
*ifp
, int reattach
)
634 IGI_LOCK_ASSERT_HELD(igi
);
637 igi
->igi_version
= igmp_default_version
;
639 igi
->igi_rv
= IGMP_RV_INIT
;
640 igi
->igi_qi
= IGMP_QI_INIT
;
641 igi
->igi_qri
= IGMP_QRI_INIT
;
642 igi
->igi_uri
= IGMP_URI_INIT
;
644 /* ifnet is not yet attached; no need to hold ifnet lock */
645 if (!(ifp
->if_flags
& IFF_MULTICAST
))
646 igi
->igi_flags
|= IGIF_SILENT
;
649 SLIST_INIT(&igi
->igi_relinmhead
);
652 * Responses to general queries are subject to bounds.
654 igi
->igi_gq
.ifq_maxlen
= IGMP_MAX_RESPONSE_PACKETS
;
655 igi
->igi_v2q
.ifq_maxlen
= IGMP_MAX_RESPONSE_PACKETS
;
658 static struct igmp_ifinfo
*
661 struct igmp_ifinfo
*igi
;
663 igi
= (how
== M_WAITOK
) ? zalloc(igi_zone
) : zalloc_noblock(igi_zone
);
665 bzero(igi
, igi_size
);
666 lck_mtx_init(&igi
->igi_lock
, igmp_mtx_grp
, igmp_mtx_attr
);
667 igi
->igi_debug
|= IFD_ALLOC
;
673 igi_free(struct igmp_ifinfo
*igi
)
676 if (igi
->igi_debug
& IFD_ATTACHED
) {
677 panic("%s: attached igi=%p is being freed", __func__
, igi
);
679 } else if (igi
->igi_ifp
!= NULL
) {
680 panic("%s: ifp not NULL for igi=%p", __func__
, igi
);
682 } else if (!(igi
->igi_debug
& IFD_ALLOC
)) {
683 panic("%s: igi %p cannot be freed", __func__
, igi
);
685 } else if (igi
->igi_refcnt
!= 0) {
686 panic("%s: non-zero refcnt igi=%p", __func__
, igi
);
689 igi
->igi_debug
&= ~IFD_ALLOC
;
692 lck_mtx_destroy(&igi
->igi_lock
, igmp_mtx_grp
);
693 zfree(igi_zone
, igi
);
697 igi_addref(struct igmp_ifinfo
*igi
, int locked
)
702 IGI_LOCK_ASSERT_HELD(igi
);
704 if (++igi
->igi_refcnt
== 0) {
705 panic("%s: igi=%p wraparound refcnt", __func__
, igi
);
713 igi_remref(struct igmp_ifinfo
*igi
)
719 if (igi
->igi_refcnt
== 0) {
720 panic("%s: igi=%p negative refcnt", __func__
, igi
);
725 if (igi
->igi_refcnt
> 0) {
732 IF_DRAIN(&igi
->igi_gq
);
733 IF_DRAIN(&igi
->igi_v2q
);
734 igmp_flush_relq(igi
);
735 VERIFY(SLIST_EMPTY(&igi
->igi_relinmhead
));
738 IGMP_PRINTF(("%s: freeing igmp_ifinfo for ifp %p(%s%d)\n",
739 __func__
, ifp
, ifp
->if_name
, ifp
->if_unit
));
745 * Process a received IGMPv1 query.
746 * Return non-zero if the message should be dropped.
749 igmp_input_v1_query(struct ifnet
*ifp
, const struct ip
*ip
,
750 const struct igmp
*igmp
)
752 struct igmp_ifinfo
*igi
;
753 struct in_multi
*inm
;
754 struct in_multistep step
;
757 * IGMPv1 Host Membership Queries SHOULD always be addressed to
758 * 224.0.0.1. They are always treated as General Queries.
759 * igmp_group is always ignored. Do not drop it as a userland
760 * daemon may wish to see it.
762 if (!in_allhosts(ip
->ip_dst
) || !in_nullhost(igmp
->igmp_group
)) {
763 IGMPSTAT_INC(igps_rcv_badqueries
);
764 OIGMPSTAT_INC(igps_rcv_badqueries
);
767 IGMPSTAT_INC(igps_rcv_gen_queries
);
769 igi
= IGMP_IFINFO(ifp
);
773 if (igi
->igi_flags
& IGIF_LOOPBACK
) {
774 IGMP_PRINTF(("ignore v1 query on IGIF_LOOPBACK ifp %p(%s%d)\n",
775 ifp
, ifp
->if_name
, ifp
->if_unit
));
780 * Switch to IGMPv1 host compatibility mode.
782 igmp_set_version(igi
, IGMP_VERSION_1
);
785 IGMP_PRINTF(("process v1 query on ifp %p(%s%d)\n", ifp
, ifp
->if_name
,
789 * Start the timers in all of our group records
790 * for the interface on which the query arrived,
791 * except those which are already running.
793 in_multihead_lock_shared();
794 IN_FIRST_MULTI(step
, inm
);
795 while (inm
!= NULL
) {
797 if (inm
->inm_ifp
!= ifp
)
799 if (inm
->inm_timer
!= 0)
802 switch (inm
->inm_state
) {
803 case IGMP_NOT_MEMBER
:
804 case IGMP_SILENT_MEMBER
:
806 case IGMP_G_QUERY_PENDING_MEMBER
:
807 case IGMP_SG_QUERY_PENDING_MEMBER
:
808 case IGMP_REPORTING_MEMBER
:
809 case IGMP_IDLE_MEMBER
:
810 case IGMP_LAZY_MEMBER
:
811 case IGMP_SLEEPING_MEMBER
:
812 case IGMP_AWAKENING_MEMBER
:
813 inm
->inm_state
= IGMP_REPORTING_MEMBER
;
814 inm
->inm_timer
= IGMP_RANDOM_DELAY(
815 IGMP_V1V2_MAX_RI
* PR_SLOWHZ
);
816 current_state_timers_running
= 1;
818 case IGMP_LEAVING_MEMBER
:
823 IN_NEXT_MULTI(step
, inm
);
825 in_multihead_lock_done();
831 * Process a received IGMPv2 general or group-specific query.
834 igmp_input_v2_query(struct ifnet
*ifp
, const struct ip
*ip
,
835 const struct igmp
*igmp
)
837 struct igmp_ifinfo
*igi
;
838 struct in_multi
*inm
;
839 int is_general_query
;
842 is_general_query
= 0;
845 * Validate address fields upfront.
847 if (in_nullhost(igmp
->igmp_group
)) {
849 * IGMPv2 General Query.
850 * If this was not sent to the all-hosts group, ignore it.
852 if (!in_allhosts(ip
->ip_dst
))
854 IGMPSTAT_INC(igps_rcv_gen_queries
);
855 is_general_query
= 1;
857 /* IGMPv2 Group-Specific Query. */
858 IGMPSTAT_INC(igps_rcv_group_queries
);
861 igi
= IGMP_IFINFO(ifp
);
865 if (igi
->igi_flags
& IGIF_LOOPBACK
) {
866 IGMP_PRINTF(("ignore v2 query on IGIF_LOOPBACK ifp %p(%s%d)\n",
867 ifp
, ifp
->if_name
, ifp
->if_unit
));
872 * Ignore v2 query if in v1 Compatibility Mode.
874 if (igi
->igi_version
== IGMP_VERSION_1
) {
878 igmp_set_version(igi
, IGMP_VERSION_2
);
881 timer
= igmp
->igmp_code
* PR_SLOWHZ
/ IGMP_TIMER_SCALE
;
885 if (is_general_query
) {
886 struct in_multistep step
;
888 IGMP_PRINTF(("process v2 general query on ifp %p(%s%d)\n",
889 ifp
, ifp
->if_name
, ifp
->if_unit
));
891 * For each reporting group joined on this
892 * interface, kick the report timer.
894 in_multihead_lock_shared();
895 IN_FIRST_MULTI(step
, inm
);
896 while (inm
!= NULL
) {
898 if (inm
->inm_ifp
== ifp
)
899 igmp_v2_update_group(inm
, timer
);
901 IN_NEXT_MULTI(step
, inm
);
903 in_multihead_lock_done();
906 * Group-specific IGMPv2 query, we need only
907 * look up the single group to process it.
909 in_multihead_lock_shared();
910 IN_LOOKUP_MULTI(&igmp
->igmp_group
, ifp
, inm
);
911 in_multihead_lock_done();
914 IGMP_PRINTF(("process v2 query %s on ifp %p(%s%d)\n",
915 inet_ntoa(igmp
->igmp_group
), ifp
, ifp
->if_name
,
917 igmp_v2_update_group(inm
, timer
);
919 INM_REMREF(inm
); /* from IN_LOOKUP_MULTI */
927 * Update the report timer on a group in response to an IGMPv2 query.
929 * If we are becoming the reporting member for this group, start the timer.
930 * If we already are the reporting member for this group, and timer is
931 * below the threshold, reset it.
933 * We may be updating the group for the first time since we switched
934 * to IGMPv3. If we are, then we must clear any recorded source lists,
935 * and transition to REPORTING state; the group timer is overloaded
936 * for group and group-source query responses.
938 * Unlike IGMPv3, the delay per group should be jittered
939 * to avoid bursts of IGMPv2 reports.
942 igmp_v2_update_group(struct in_multi
*inm
, const int timer
)
945 IGMP_PRINTF(("%s: %s/%s%d timer=%d\n", __func__
,
946 inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
->if_name
,
947 inm
->inm_ifp
->if_unit
, timer
));
949 INM_LOCK_ASSERT_HELD(inm
);
951 switch (inm
->inm_state
) {
952 case IGMP_NOT_MEMBER
:
953 case IGMP_SILENT_MEMBER
:
955 case IGMP_REPORTING_MEMBER
:
956 if (inm
->inm_timer
!= 0 &&
957 inm
->inm_timer
<= timer
) {
958 IGMP_PRINTF(("%s: REPORTING and timer running, "
959 "skipping.\n", __func__
));
963 case IGMP_SG_QUERY_PENDING_MEMBER
:
964 case IGMP_G_QUERY_PENDING_MEMBER
:
965 case IGMP_IDLE_MEMBER
:
966 case IGMP_LAZY_MEMBER
:
967 case IGMP_AWAKENING_MEMBER
:
968 IGMP_PRINTF(("%s: ->REPORTING\n", __func__
));
969 inm
->inm_state
= IGMP_REPORTING_MEMBER
;
970 inm
->inm_timer
= IGMP_RANDOM_DELAY(timer
);
971 current_state_timers_running
= 1;
973 case IGMP_SLEEPING_MEMBER
:
974 IGMP_PRINTF(("%s: ->AWAKENING\n", __func__
));
975 inm
->inm_state
= IGMP_AWAKENING_MEMBER
;
977 case IGMP_LEAVING_MEMBER
:
983 * Process a received IGMPv3 general, group-specific or
984 * group-and-source-specific query.
985 * Assumes m has already been pulled up to the full IGMP message length.
986 * Return 0 if successful, otherwise an appropriate error code is returned.
989 igmp_input_v3_query(struct ifnet
*ifp
, const struct ip
*ip
,
990 /*const*/ struct igmpv3
*igmpv3
)
992 struct igmp_ifinfo
*igi
;
993 struct in_multi
*inm
;
994 int is_general_query
;
995 uint32_t maxresp
, nsrc
, qqi
;
999 is_general_query
= 0;
1001 IGMP_PRINTF(("process v3 query on ifp %p(%s%d)\n", ifp
, ifp
->if_name
,
1004 maxresp
= igmpv3
->igmp_code
; /* in 1/10ths of a second */
1005 if (maxresp
>= 128) {
1006 maxresp
= IGMP_MANT(igmpv3
->igmp_code
) <<
1007 (IGMP_EXP(igmpv3
->igmp_code
) + 3);
1011 * Robustness must never be less than 2 for on-wire IGMPv3.
1012 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
1013 * an exception for interfaces whose IGMPv3 state changes
1014 * are redirected to loopback (e.g. MANET).
1016 qrv
= IGMP_QRV(igmpv3
->igmp_misc
);
1018 IGMP_PRINTF(("%s: clamping qrv %d to %d\n", __func__
,
1019 qrv
, IGMP_RV_INIT
));
1023 qqi
= igmpv3
->igmp_qqi
;
1025 qqi
= IGMP_MANT(igmpv3
->igmp_qqi
) <<
1026 (IGMP_EXP(igmpv3
->igmp_qqi
) + 3);
1029 timer
= maxresp
* PR_SLOWHZ
/ IGMP_TIMER_SCALE
;
1033 nsrc
= ntohs(igmpv3
->igmp_numsrc
);
1036 * Validate address fields and versions upfront before
1037 * accepting v3 query.
1039 if (in_nullhost(igmpv3
->igmp_group
)) {
1041 * IGMPv3 General Query.
1043 * General Queries SHOULD be directed to 224.0.0.1.
1044 * A general query with a source list has undefined
1045 * behaviour; discard it.
1047 IGMPSTAT_INC(igps_rcv_gen_queries
);
1048 if (!in_allhosts(ip
->ip_dst
) || nsrc
> 0) {
1049 IGMPSTAT_INC(igps_rcv_badqueries
);
1050 OIGMPSTAT_INC(igps_rcv_badqueries
);
1053 is_general_query
= 1;
1055 /* Group or group-source specific query. */
1057 IGMPSTAT_INC(igps_rcv_group_queries
);
1059 IGMPSTAT_INC(igps_rcv_gsr_queries
);
1062 igi
= IGMP_IFINFO(ifp
);
1063 VERIFY(igi
!= NULL
);
1066 if (igi
->igi_flags
& IGIF_LOOPBACK
) {
1067 IGMP_PRINTF(("ignore v3 query on IGIF_LOOPBACK ifp %p(%s%d)\n",
1068 ifp
, ifp
->if_name
, ifp
->if_unit
));
1074 * Discard the v3 query if we're in Compatibility Mode.
1075 * The RFC is not obviously worded that hosts need to stay in
1076 * compatibility mode until the Old Version Querier Present
1079 if (igi
->igi_version
!= IGMP_VERSION_3
) {
1080 IGMP_PRINTF(("ignore v3 query in v%d mode on ifp %p(%s%d)\n",
1081 igi
->igi_version
, ifp
, ifp
->if_name
, ifp
->if_unit
));
1086 igmp_set_version(igi
, IGMP_VERSION_3
);
1089 igi
->igi_qri
= maxresp
;
1092 IGMP_PRINTF(("%s: qrv %d qi %d qri %d\n", __func__
, qrv
, qqi
,
1095 if (is_general_query
) {
1097 * Schedule a current-state report on this ifp for
1098 * all groups, possibly containing source lists.
1099 * If there is a pending General Query response
1100 * scheduled earlier than the selected delay, do
1101 * not schedule any other reports.
1102 * Otherwise, reset the interface timer.
1104 IGMP_PRINTF(("process v3 general query on ifp %p(%s%d)\n",
1105 ifp
, ifp
->if_name
, ifp
->if_unit
));
1106 if (igi
->igi_v3_timer
== 0 || igi
->igi_v3_timer
>= timer
) {
1107 igi
->igi_v3_timer
= IGMP_RANDOM_DELAY(timer
);
1108 interface_timers_running
= 1;
1114 * Group-source-specific queries are throttled on
1115 * a per-group basis to defeat denial-of-service attempts.
1116 * Queries for groups we are not a member of on this
1117 * link are simply ignored.
1119 in_multihead_lock_shared();
1120 IN_LOOKUP_MULTI(&igmpv3
->igmp_group
, ifp
, inm
);
1121 in_multihead_lock_done();
1127 /* TODO: need ratecheck equivalent */
1129 if (!ratecheck(&inm
->inm_lastgsrtv
,
1131 IGMP_PRINTF(("%s: GS query throttled.\n",
1133 IGMPSTAT_INC(igps_drop_gsr_queries
);
1135 INM_REMREF(inm
); /* from IN_LOOKUP_MULTI */
1140 IGMP_PRINTF(("process v3 %s query on ifp %p(%s%d)\n",
1141 inet_ntoa(igmpv3
->igmp_group
), ifp
, ifp
->if_name
,
1144 * If there is a pending General Query response
1145 * scheduled sooner than the selected delay, no
1146 * further report need be scheduled.
1147 * Otherwise, prepare to respond to the
1148 * group-specific or group-and-source query.
1151 if (igi
->igi_v3_timer
== 0 || igi
->igi_v3_timer
>= timer
) {
1153 igmp_input_v3_group_query(inm
, timer
, igmpv3
);
1158 INM_REMREF(inm
); /* from IN_LOOKUP_MULTI */
1165 * Process a recieved IGMPv3 group-specific or group-and-source-specific
1167 * Return <0 if any error occured. Currently this is ignored.
1170 igmp_input_v3_group_query(struct in_multi
*inm
,
1171 int timer
, /*const*/ struct igmpv3
*igmpv3
)
1176 INM_LOCK_ASSERT_HELD(inm
);
1180 switch (inm
->inm_state
) {
1181 case IGMP_NOT_MEMBER
:
1182 case IGMP_SILENT_MEMBER
:
1183 case IGMP_SLEEPING_MEMBER
:
1184 case IGMP_LAZY_MEMBER
:
1185 case IGMP_AWAKENING_MEMBER
:
1186 case IGMP_IDLE_MEMBER
:
1187 case IGMP_LEAVING_MEMBER
:
1189 case IGMP_REPORTING_MEMBER
:
1190 case IGMP_G_QUERY_PENDING_MEMBER
:
1191 case IGMP_SG_QUERY_PENDING_MEMBER
:
1195 nsrc
= ntohs(igmpv3
->igmp_numsrc
);
1198 * Deal with group-specific queries upfront.
1199 * If any group query is already pending, purge any recorded
1200 * source-list state if it exists, and schedule a query response
1201 * for this group-specific query.
1204 if (inm
->inm_state
== IGMP_G_QUERY_PENDING_MEMBER
||
1205 inm
->inm_state
== IGMP_SG_QUERY_PENDING_MEMBER
) {
1206 inm_clear_recorded(inm
);
1207 timer
= min(inm
->inm_timer
, timer
);
1209 inm
->inm_state
= IGMP_G_QUERY_PENDING_MEMBER
;
1210 inm
->inm_timer
= IGMP_RANDOM_DELAY(timer
);
1211 current_state_timers_running
= 1;
1216 * Deal with the case where a group-and-source-specific query has
1217 * been received but a group-specific query is already pending.
1219 if (inm
->inm_state
== IGMP_G_QUERY_PENDING_MEMBER
) {
1220 timer
= min(inm
->inm_timer
, timer
);
1221 inm
->inm_timer
= IGMP_RANDOM_DELAY(timer
);
1222 current_state_timers_running
= 1;
1227 * Finally, deal with the case where a group-and-source-specific
1228 * query has been received, where a response to a previous g-s-r
1229 * query exists, or none exists.
1230 * In this case, we need to parse the source-list which the Querier
1231 * has provided us with and check if we have any source list filter
1232 * entries at T1 for these sources. If we do not, there is no need
1233 * schedule a report and the query may be dropped.
1234 * If we do, we must record them and schedule a current-state
1235 * report for those sources.
1236 * FIXME: Handling source lists larger than 1 mbuf requires that
1237 * we pass the mbuf chain pointer down to this function, and use
1238 * m_getptr() to walk the chain.
1240 if (inm
->inm_nsrc
> 0) {
1241 const struct in_addr
*ap
;
1244 ap
= (const struct in_addr
*)(igmpv3
+ 1);
1246 for (i
= 0; i
< nsrc
; i
++, ap
++) {
1247 retval
= inm_record_source(inm
, ap
->s_addr
);
1250 nrecorded
+= retval
;
1252 if (nrecorded
> 0) {
1253 IGMP_PRINTF(("%s: schedule response to SG query\n",
1255 inm
->inm_state
= IGMP_SG_QUERY_PENDING_MEMBER
;
1256 inm
->inm_timer
= IGMP_RANDOM_DELAY(timer
);
1257 current_state_timers_running
= 1;
1265 * Process a received IGMPv1 host membership report.
1267 * NOTE: 0.0.0.0 workaround breaks const correctness.
1270 igmp_input_v1_report(struct ifnet
*ifp
, /*const*/ struct ip
*ip
,
1271 /*const*/ struct igmp
*igmp
)
1273 struct in_ifaddr
*ia
;
1274 struct in_multi
*inm
;
1276 IGMPSTAT_INC(igps_rcv_reports
);
1277 OIGMPSTAT_INC(igps_rcv_reports
);
1279 if (ifp
->if_flags
& IFF_LOOPBACK
)
1282 if (!IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
) ||
1283 !in_hosteq(igmp
->igmp_group
, ip
->ip_dst
))) {
1284 IGMPSTAT_INC(igps_rcv_badreports
);
1285 OIGMPSTAT_INC(igps_rcv_badreports
);
1290 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1291 * Booting clients may use the source address 0.0.0.0. Some
1292 * IGMP daemons may not know how to use IP_RECVIF to determine
1293 * the interface upon which this message was received.
1294 * Replace 0.0.0.0 with the subnet address if told to do so.
1296 if (igmp_recvifkludge
&& in_nullhost(ip
->ip_src
)) {
1299 IFA_LOCK(&ia
->ia_ifa
);
1300 ip
->ip_src
.s_addr
= htonl(ia
->ia_subnet
);
1301 IFA_UNLOCK(&ia
->ia_ifa
);
1302 IFA_REMREF(&ia
->ia_ifa
);
1306 IGMP_PRINTF(("process v1 report %s on ifp %p(%s%d)\n",
1307 inet_ntoa(igmp
->igmp_group
), ifp
, ifp
->if_name
, ifp
->if_unit
));
1310 * IGMPv1 report suppression.
1311 * If we are a member of this group, and our membership should be
1312 * reported, stop our group timer and transition to the 'lazy' state.
1314 in_multihead_lock_shared();
1315 IN_LOOKUP_MULTI(&igmp
->igmp_group
, ifp
, inm
);
1316 in_multihead_lock_done();
1318 struct igmp_ifinfo
*igi
;
1323 VERIFY(igi
!= NULL
);
1325 IGMPSTAT_INC(igps_rcv_ourreports
);
1326 OIGMPSTAT_INC(igps_rcv_ourreports
);
1329 * If we are in IGMPv3 host mode, do not allow the
1330 * other host's IGMPv1 report to suppress our reports
1331 * unless explicitly configured to do so.
1334 if (igi
->igi_version
== IGMP_VERSION_3
) {
1335 if (igmp_legacysupp
)
1336 igmp_v3_suppress_group_record(inm
);
1339 INM_REMREF(inm
); /* from IN_LOOKUP_MULTI */
1343 INM_LOCK_ASSERT_HELD(inm
);
1346 switch (inm
->inm_state
) {
1347 case IGMP_NOT_MEMBER
:
1348 case IGMP_SILENT_MEMBER
:
1350 case IGMP_IDLE_MEMBER
:
1351 case IGMP_LAZY_MEMBER
:
1352 case IGMP_AWAKENING_MEMBER
:
1353 IGMP_PRINTF(("report suppressed for %s on ifp %p(%s%d)\n",
1354 inet_ntoa(igmp
->igmp_group
), ifp
, ifp
->if_name
,
1356 case IGMP_SLEEPING_MEMBER
:
1357 inm
->inm_state
= IGMP_SLEEPING_MEMBER
;
1359 case IGMP_REPORTING_MEMBER
:
1360 IGMP_PRINTF(("report suppressed for %s on ifp %p(%s%d)\n",
1361 inet_ntoa(igmp
->igmp_group
), ifp
, ifp
->if_name
,
1363 if (igi
->igi_version
== IGMP_VERSION_1
)
1364 inm
->inm_state
= IGMP_LAZY_MEMBER
;
1365 else if (igi
->igi_version
== IGMP_VERSION_2
)
1366 inm
->inm_state
= IGMP_SLEEPING_MEMBER
;
1368 case IGMP_G_QUERY_PENDING_MEMBER
:
1369 case IGMP_SG_QUERY_PENDING_MEMBER
:
1370 case IGMP_LEAVING_MEMBER
:
1375 INM_REMREF(inm
); /* from IN_LOOKUP_MULTI */
1382 * Process a received IGMPv2 host membership report.
1384 * NOTE: 0.0.0.0 workaround breaks const correctness.
1387 igmp_input_v2_report(struct ifnet
*ifp
, /*const*/ struct ip
*ip
,
1388 /*const*/ struct igmp
*igmp
)
1390 struct in_ifaddr
*ia
;
1391 struct in_multi
*inm
;
1394 * Make sure we don't hear our own membership report. Fast
1395 * leave requires knowing that we are the only member of a
1400 IFA_LOCK(&ia
->ia_ifa
);
1401 if (in_hosteq(ip
->ip_src
, IA_SIN(ia
)->sin_addr
)) {
1402 IFA_UNLOCK(&ia
->ia_ifa
);
1403 IFA_REMREF(&ia
->ia_ifa
);
1406 IFA_UNLOCK(&ia
->ia_ifa
);
1409 IGMPSTAT_INC(igps_rcv_reports
);
1410 OIGMPSTAT_INC(igps_rcv_reports
);
1412 if (ifp
->if_flags
& IFF_LOOPBACK
) {
1414 IFA_REMREF(&ia
->ia_ifa
);
1418 if (!IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
)) ||
1419 !in_hosteq(igmp
->igmp_group
, ip
->ip_dst
)) {
1421 IFA_REMREF(&ia
->ia_ifa
);
1422 IGMPSTAT_INC(igps_rcv_badreports
);
1423 OIGMPSTAT_INC(igps_rcv_badreports
);
1428 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1429 * Booting clients may use the source address 0.0.0.0. Some
1430 * IGMP daemons may not know how to use IP_RECVIF to determine
1431 * the interface upon which this message was received.
1432 * Replace 0.0.0.0 with the subnet address if told to do so.
1434 if (igmp_recvifkludge
&& in_nullhost(ip
->ip_src
)) {
1436 IFA_LOCK(&ia
->ia_ifa
);
1437 ip
->ip_src
.s_addr
= htonl(ia
->ia_subnet
);
1438 IFA_UNLOCK(&ia
->ia_ifa
);
1442 IFA_REMREF(&ia
->ia_ifa
);
1444 IGMP_PRINTF(("process v2 report %s on ifp %p(%s%d)\n",
1445 inet_ntoa(igmp
->igmp_group
), ifp
, ifp
->if_name
, ifp
->if_unit
));
1448 * IGMPv2 report suppression.
1449 * If we are a member of this group, and our membership should be
1450 * reported, and our group timer is pending or about to be reset,
1451 * stop our group timer by transitioning to the 'lazy' state.
1453 in_multihead_lock_shared();
1454 IN_LOOKUP_MULTI(&igmp
->igmp_group
, ifp
, inm
);
1455 in_multihead_lock_done();
1457 struct igmp_ifinfo
*igi
;
1461 VERIFY(igi
!= NULL
);
1463 IGMPSTAT_INC(igps_rcv_ourreports
);
1464 OIGMPSTAT_INC(igps_rcv_ourreports
);
1467 * If we are in IGMPv3 host mode, do not allow the
1468 * other host's IGMPv1 report to suppress our reports
1469 * unless explicitly configured to do so.
1472 if (igi
->igi_version
== IGMP_VERSION_3
) {
1473 if (igmp_legacysupp
)
1474 igmp_v3_suppress_group_record(inm
);
1483 switch (inm
->inm_state
) {
1484 case IGMP_NOT_MEMBER
:
1485 case IGMP_SILENT_MEMBER
:
1486 case IGMP_SLEEPING_MEMBER
:
1488 case IGMP_REPORTING_MEMBER
:
1489 case IGMP_IDLE_MEMBER
:
1490 case IGMP_AWAKENING_MEMBER
:
1491 IGMP_PRINTF(("report suppressed for %s on ifp %p(%s%d)\n",
1492 inet_ntoa(igmp
->igmp_group
), ifp
, ifp
->if_name
,
1494 case IGMP_LAZY_MEMBER
:
1495 inm
->inm_state
= IGMP_LAZY_MEMBER
;
1497 case IGMP_G_QUERY_PENDING_MEMBER
:
1498 case IGMP_SG_QUERY_PENDING_MEMBER
:
1499 case IGMP_LEAVING_MEMBER
:
1511 igmp_input(struct mbuf
*m
, int off
)
1521 IGMP_PRINTF(("%s: called w/mbuf (%p,%d)\n", __func__
, m
, off
));
1523 ifp
= m
->m_pkthdr
.rcvif
;
1525 IGMPSTAT_INC(igps_rcv_total
);
1526 OIGMPSTAT_INC(igps_rcv_total
);
1528 ip
= mtod(m
, struct ip
*);
1531 /* By now, ip_len no longer contains the length of IP header */
1532 igmplen
= ip
->ip_len
;
1537 if (igmplen
< IGMP_MINLEN
) {
1538 IGMPSTAT_INC(igps_rcv_tooshort
);
1539 OIGMPSTAT_INC(igps_rcv_tooshort
);
1545 * Always pullup to the minimum size for v1/v2 or v3
1546 * to amortize calls to m_pulldown().
1548 if (igmplen
>= IGMP_V3_QUERY_MINLEN
)
1549 minlen
= IGMP_V3_QUERY_MINLEN
;
1551 minlen
= IGMP_MINLEN
;
1553 M_STRUCT_GET(igmp
, struct igmp
*, m
, off
, minlen
);
1555 IGMPSTAT_INC(igps_rcv_tooshort
);
1556 OIGMPSTAT_INC(igps_rcv_tooshort
);
1561 * Validate checksum.
1563 m
->m_data
+= iphlen
;
1565 if (in_cksum(m
, igmplen
)) {
1566 IGMPSTAT_INC(igps_rcv_badsum
);
1567 OIGMPSTAT_INC(igps_rcv_badsum
);
1571 m
->m_data
-= iphlen
;
1575 * IGMP control traffic is link-scope, and must have a TTL of 1.
1576 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1577 * probe packets may come from beyond the LAN.
1579 if (igmp
->igmp_type
!= IGMP_DVMRP
&& ip
->ip_ttl
!= 1) {
1580 IGMPSTAT_INC(igps_rcv_badttl
);
1585 switch (igmp
->igmp_type
) {
1586 case IGMP_HOST_MEMBERSHIP_QUERY
:
1587 if (igmplen
== IGMP_MINLEN
) {
1588 if (igmp
->igmp_code
== 0)
1589 queryver
= IGMP_VERSION_1
;
1591 queryver
= IGMP_VERSION_2
;
1592 } else if (igmplen
>= IGMP_V3_QUERY_MINLEN
) {
1593 queryver
= IGMP_VERSION_3
;
1595 IGMPSTAT_INC(igps_rcv_tooshort
);
1596 OIGMPSTAT_INC(igps_rcv_tooshort
);
1601 OIGMPSTAT_INC(igps_rcv_queries
);
1604 case IGMP_VERSION_1
:
1605 IGMPSTAT_INC(igps_rcv_v1v2_queries
);
1608 if (igmp_input_v1_query(ifp
, ip
, igmp
) != 0) {
1614 case IGMP_VERSION_2
:
1615 IGMPSTAT_INC(igps_rcv_v1v2_queries
);
1618 if (igmp_input_v2_query(ifp
, ip
, igmp
) != 0) {
1624 case IGMP_VERSION_3
: {
1625 struct igmpv3
*igmpv3
;
1630 IGMPSTAT_INC(igps_rcv_v3_queries
);
1631 igmpv3
= (struct igmpv3
*)igmp
;
1633 * Validate length based on source count.
1635 nsrc
= ntohs(igmpv3
->igmp_numsrc
);
1636 srclen
= sizeof(struct in_addr
) * nsrc
;
1637 if (igmplen
< (IGMP_V3_QUERY_MINLEN
+ srclen
)) {
1638 IGMPSTAT_INC(igps_rcv_tooshort
);
1639 OIGMPSTAT_INC(igps_rcv_tooshort
);
1643 igmpv3len
= IGMP_V3_QUERY_MINLEN
+ srclen
;
1644 M_STRUCT_GET(igmpv3
, struct igmpv3
*, m
,
1646 if (igmpv3
== NULL
) {
1647 IGMPSTAT_INC(igps_rcv_tooshort
);
1648 OIGMPSTAT_INC(igps_rcv_tooshort
);
1651 if (igmp_input_v3_query(ifp
, ip
, igmpv3
) != 0) {
1660 case IGMP_v1_HOST_MEMBERSHIP_REPORT
:
1663 if (igmp_input_v1_report(ifp
, ip
, igmp
) != 0) {
1669 case IGMP_v2_HOST_MEMBERSHIP_REPORT
:
1673 if (!ip_checkrouteralert(m
))
1674 IGMPSTAT_INC(igps_rcv_nora
);
1676 if (igmp_input_v2_report(ifp
, ip
, igmp
) != 0) {
1682 case IGMP_v3_HOST_MEMBERSHIP_REPORT
:
1684 * Hosts do not need to process IGMPv3 membership reports,
1685 * as report suppression is no longer required.
1688 if (!ip_checkrouteralert(m
))
1689 IGMPSTAT_INC(igps_rcv_nora
);
1697 lck_mtx_assert(&igmp_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1699 * Pass all valid IGMP packets up to any process(es) listening on a
1707 * IGMP slowtimo handler.
1708 * Combiles both the slow and fast timer into one. We loose some responsivness but
1709 * allows the system to avoid having a pr_fasttimo, thus allowing for power savings.
1715 struct ifqueue scq
; /* State-change packets */
1716 struct ifqueue qrq
; /* Query response packets */
1718 struct igmp_ifinfo
*igi
;
1719 struct in_multi
*inm
;
1720 int loop
= 0, uri_fasthz
= 0;
1722 lck_mtx_lock(&igmp_mtx
);
1724 LIST_FOREACH(igi
, &igi_head
, igi_link
) {
1726 igmp_v1v2_process_querier_timers(igi
);
1731 * NOTE: previously handled by fasttimo
1733 * Quick check to see if any work needs to be done, in order to
1734 * minimize the overhead of fasttimo processing.
1736 if (!current_state_timers_running
&&
1737 !interface_timers_running
&&
1738 !state_change_timers_running
) {
1739 lck_mtx_unlock(&igmp_mtx
);
1744 * IGMPv3 General Query response timer processing.
1746 if (interface_timers_running
) {
1747 interface_timers_running
= 0;
1748 LIST_FOREACH(igi
, &igi_head
, igi_link
) {
1750 if (igi
->igi_v3_timer
== 0) {
1752 } else if (--igi
->igi_v3_timer
== 0) {
1753 igmp_v3_dispatch_general_query(igi
);
1755 interface_timers_running
= 1;
1761 if (!current_state_timers_running
&&
1762 !state_change_timers_running
)
1765 current_state_timers_running
= 0;
1766 state_change_timers_running
= 0;
1768 memset(&qrq
, 0, sizeof(struct ifqueue
));
1769 qrq
.ifq_maxlen
= IGMP_MAX_G_GS_PACKETS
;
1771 memset(&scq
, 0, sizeof(struct ifqueue
));
1772 scq
.ifq_maxlen
= IGMP_MAX_STATE_CHANGE_PACKETS
;
1775 * IGMPv1/v2/v3 host report and state-change timer processing.
1776 * Note: Processing a v3 group timer may remove a node.
1778 LIST_FOREACH(igi
, &igi_head
, igi_link
) {
1779 struct in_multistep step
;
1783 loop
= (igi
->igi_flags
& IGIF_LOOPBACK
) ? 1 : 0;
1784 uri_fasthz
= IGMP_RANDOM_DELAY(igi
->igi_uri
* PR_SLOWHZ
);
1787 in_multihead_lock_shared();
1788 IN_FIRST_MULTI(step
, inm
);
1789 while (inm
!= NULL
) {
1791 if (inm
->inm_ifp
!= ifp
)
1795 switch (igi
->igi_version
) {
1796 case IGMP_VERSION_1
:
1797 case IGMP_VERSION_2
:
1798 igmp_v1v2_process_group_timer(inm
,
1801 case IGMP_VERSION_3
:
1802 igmp_v3_process_group_timers(igi
, &qrq
,
1803 &scq
, inm
, uri_fasthz
);
1809 IN_NEXT_MULTI(step
, inm
);
1811 in_multihead_lock_done();
1814 if (igi
->igi_version
== IGMP_VERSION_1
||
1815 igi
->igi_version
== IGMP_VERSION_2
) {
1816 igmp_dispatch_queue(igi
, &igi
->igi_v2q
, 0, loop
, ifp
);
1817 } else if (igi
->igi_version
== IGMP_VERSION_3
) {
1819 igmp_dispatch_queue(NULL
, &qrq
, 0, loop
, ifp
);
1820 igmp_dispatch_queue(NULL
, &scq
, 0, loop
, ifp
);
1821 VERIFY(qrq
.ifq_len
== 0);
1822 VERIFY(scq
.ifq_len
== 0);
1826 * In case there are still any pending membership reports
1827 * which didn't get drained at version change time.
1829 IF_DRAIN(&igi
->igi_v2q
);
1831 * Release all deferred inm records, and drain any locally
1832 * enqueued packets; do it even if the current IGMP version
1833 * for the link is no longer IGMPv3, in order to handle the
1834 * version change case.
1836 igmp_flush_relq(igi
);
1837 VERIFY(SLIST_EMPTY(&igi
->igi_relinmhead
));
1845 lck_mtx_unlock(&igmp_mtx
);
1849 * Free the in_multi reference(s) for this IGMP lifecycle.
1851 * Caller must be holding igi_lock.
1854 igmp_flush_relq(struct igmp_ifinfo
*igi
)
1856 struct in_multi
*inm
;
1859 IGI_LOCK_ASSERT_HELD(igi
);
1860 inm
= SLIST_FIRST(&igi
->igi_relinmhead
);
1864 SLIST_REMOVE_HEAD(&igi
->igi_relinmhead
, inm_nrele
);
1867 in_multihead_lock_exclusive();
1869 VERIFY(inm
->inm_nrelecnt
!= 0);
1870 inm
->inm_nrelecnt
--;
1871 lastref
= in_multi_detach(inm
);
1872 VERIFY(!lastref
|| (!(inm
->inm_debug
& IFD_ATTACHED
) &&
1873 inm
->inm_reqcnt
== 0));
1875 in_multihead_lock_done();
1876 /* from igi_relinmhead */
1878 /* from in_multihead list */
1888 * Update host report group timer for IGMPv1/v2.
1889 * Will update the global pending timer flags.
1892 igmp_v1v2_process_group_timer(struct in_multi
*inm
, const int igmp_version
)
1894 int report_timer_expired
;
1896 INM_LOCK_ASSERT_HELD(inm
);
1897 IGI_LOCK_ASSERT_HELD(inm
->inm_igi
);
1899 if (inm
->inm_timer
== 0) {
1900 report_timer_expired
= 0;
1901 } else if (--inm
->inm_timer
== 0) {
1902 report_timer_expired
= 1;
1904 current_state_timers_running
= 1;
1908 switch (inm
->inm_state
) {
1909 case IGMP_NOT_MEMBER
:
1910 case IGMP_SILENT_MEMBER
:
1911 case IGMP_IDLE_MEMBER
:
1912 case IGMP_LAZY_MEMBER
:
1913 case IGMP_SLEEPING_MEMBER
:
1914 case IGMP_AWAKENING_MEMBER
:
1916 case IGMP_REPORTING_MEMBER
:
1917 if (report_timer_expired
) {
1918 inm
->inm_state
= IGMP_IDLE_MEMBER
;
1919 (void) igmp_v1v2_queue_report(inm
,
1920 (igmp_version
== IGMP_VERSION_2
) ?
1921 IGMP_v2_HOST_MEMBERSHIP_REPORT
:
1922 IGMP_v1_HOST_MEMBERSHIP_REPORT
);
1923 INM_LOCK_ASSERT_HELD(inm
);
1924 IGI_LOCK_ASSERT_HELD(inm
->inm_igi
);
1927 case IGMP_G_QUERY_PENDING_MEMBER
:
1928 case IGMP_SG_QUERY_PENDING_MEMBER
:
1929 case IGMP_LEAVING_MEMBER
:
1935 * Update a group's timers for IGMPv3.
1936 * Will update the global pending timer flags.
1937 * Note: Unlocked read from igi.
1940 igmp_v3_process_group_timers(struct igmp_ifinfo
*igi
,
1941 struct ifqueue
*qrq
, struct ifqueue
*scq
,
1942 struct in_multi
*inm
, const int uri_fasthz
)
1944 int query_response_timer_expired
;
1945 int state_change_retransmit_timer_expired
;
1947 INM_LOCK_ASSERT_HELD(inm
);
1948 IGI_LOCK_ASSERT_HELD(igi
);
1949 VERIFY(igi
== inm
->inm_igi
);
1951 query_response_timer_expired
= 0;
1952 state_change_retransmit_timer_expired
= 0;
1955 * During a transition from v1/v2 compatibility mode back to v3,
1956 * a group record in REPORTING state may still have its group
1957 * timer active. This is a no-op in this function; it is easier
1958 * to deal with it here than to complicate the slow-timeout path.
1960 if (inm
->inm_timer
== 0) {
1961 query_response_timer_expired
= 0;
1962 } else if (--inm
->inm_timer
== 0) {
1963 query_response_timer_expired
= 1;
1965 current_state_timers_running
= 1;
1968 if (inm
->inm_sctimer
== 0) {
1969 state_change_retransmit_timer_expired
= 0;
1970 } else if (--inm
->inm_sctimer
== 0) {
1971 state_change_retransmit_timer_expired
= 1;
1973 state_change_timers_running
= 1;
1976 /* We are in fasttimo, so be quick about it. */
1977 if (!state_change_retransmit_timer_expired
&&
1978 !query_response_timer_expired
)
1981 switch (inm
->inm_state
) {
1982 case IGMP_NOT_MEMBER
:
1983 case IGMP_SILENT_MEMBER
:
1984 case IGMP_SLEEPING_MEMBER
:
1985 case IGMP_LAZY_MEMBER
:
1986 case IGMP_AWAKENING_MEMBER
:
1987 case IGMP_IDLE_MEMBER
:
1989 case IGMP_G_QUERY_PENDING_MEMBER
:
1990 case IGMP_SG_QUERY_PENDING_MEMBER
:
1992 * Respond to a previously pending Group-Specific
1993 * or Group-and-Source-Specific query by enqueueing
1994 * the appropriate Current-State report for
1995 * immediate transmission.
1997 if (query_response_timer_expired
) {
2000 retval
= igmp_v3_enqueue_group_record(qrq
, inm
, 0, 1,
2001 (inm
->inm_state
== IGMP_SG_QUERY_PENDING_MEMBER
));
2002 IGMP_PRINTF(("%s: enqueue record = %d\n",
2004 inm
->inm_state
= IGMP_REPORTING_MEMBER
;
2005 /* XXX Clear recorded sources for next time. */
2006 inm_clear_recorded(inm
);
2009 case IGMP_REPORTING_MEMBER
:
2010 case IGMP_LEAVING_MEMBER
:
2011 if (state_change_retransmit_timer_expired
) {
2013 * State-change retransmission timer fired.
2014 * If there are any further pending retransmissions,
2015 * set the global pending state-change flag, and
2018 if (--inm
->inm_scrv
> 0) {
2019 inm
->inm_sctimer
= uri_fasthz
;
2020 state_change_timers_running
= 1;
2023 * Retransmit the previously computed state-change
2024 * report. If there are no further pending
2025 * retransmissions, the mbuf queue will be consumed.
2026 * Update T0 state to T1 as we have now sent
2029 (void) igmp_v3_merge_state_changes(inm
, scq
);
2032 IGMP_PRINTF(("%s: T1 -> T0 for %s/%s%d\n", __func__
,
2033 inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
->if_name
,
2034 inm
->inm_ifp
->if_unit
));
2037 * If we are leaving the group for good, make sure
2038 * we release IGMP's reference to it.
2039 * This release must be deferred using a SLIST,
2040 * as we are called from a loop which traverses
2041 * the in_multihead list.
2043 if (inm
->inm_state
== IGMP_LEAVING_MEMBER
&&
2044 inm
->inm_scrv
== 0) {
2045 inm
->inm_state
= IGMP_NOT_MEMBER
;
2047 * A reference has already been held in
2048 * igmp_final_leave() for this inm, so
2049 * no need to hold another one. We also
2050 * bumped up its request count then, so
2051 * that it stays in in_multihead. Both
2052 * of them will be released when it is
2053 * dequeued later on.
2055 VERIFY(inm
->inm_nrelecnt
!= 0);
2056 SLIST_INSERT_HEAD(&igi
->igi_relinmhead
,
2065 * Suppress a group's pending response to a group or source/group query.
2067 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
2068 * Do NOT update ST1/ST0 as this operation merely suppresses
2069 * the currently pending group record.
2070 * Do NOT suppress the response to a general query. It is possible but
2071 * it would require adding another state or flag.
2074 igmp_v3_suppress_group_record(struct in_multi
*inm
)
2077 INM_LOCK_ASSERT_HELD(inm
);
2078 IGI_LOCK_ASSERT_HELD(inm
->inm_igi
);
2080 VERIFY(inm
->inm_igi
->igi_version
== IGMP_VERSION_3
);
2082 if (inm
->inm_state
!= IGMP_G_QUERY_PENDING_MEMBER
||
2083 inm
->inm_state
!= IGMP_SG_QUERY_PENDING_MEMBER
)
2086 if (inm
->inm_state
== IGMP_SG_QUERY_PENDING_MEMBER
)
2087 inm_clear_recorded(inm
);
2090 inm
->inm_state
= IGMP_REPORTING_MEMBER
;
2094 * Switch to a different IGMP version on the given interface,
2095 * as per Section 7.2.1.
2098 igmp_set_version(struct igmp_ifinfo
*igi
, const int igmp_version
)
2100 int old_version_timer
;
2102 IGI_LOCK_ASSERT_HELD(igi
);
2104 IGMP_PRINTF(("%s: switching to v%d on ifp %p(%s%d)\n", __func__
,
2105 igmp_version
, igi
->igi_ifp
, igi
->igi_ifp
->if_name
,
2106 igi
->igi_ifp
->if_unit
));
2108 if (igmp_version
== IGMP_VERSION_1
|| igmp_version
== IGMP_VERSION_2
) {
2110 * Compute the "Older Version Querier Present" timer as per
2113 old_version_timer
= igi
->igi_rv
* igi
->igi_qi
+ igi
->igi_qri
;
2114 old_version_timer
*= PR_SLOWHZ
;
2116 if (igmp_version
== IGMP_VERSION_1
) {
2117 igi
->igi_v1_timer
= old_version_timer
;
2118 igi
->igi_v2_timer
= 0;
2119 } else if (igmp_version
== IGMP_VERSION_2
) {
2120 igi
->igi_v1_timer
= 0;
2121 igi
->igi_v2_timer
= old_version_timer
;
2125 if (igi
->igi_v1_timer
== 0 && igi
->igi_v2_timer
> 0) {
2126 if (igi
->igi_version
!= IGMP_VERSION_2
) {
2127 igi
->igi_version
= IGMP_VERSION_2
;
2128 igmp_v3_cancel_link_timers(igi
);
2130 } else if (igi
->igi_v1_timer
> 0) {
2131 if (igi
->igi_version
!= IGMP_VERSION_1
) {
2132 igi
->igi_version
= IGMP_VERSION_1
;
2133 igmp_v3_cancel_link_timers(igi
);
2137 IGI_LOCK_ASSERT_HELD(igi
);
2141 * Cancel pending IGMPv3 timers for the given link and all groups
2142 * joined on it; state-change, general-query, and group-query timers.
2144 * Only ever called on a transition from v3 to Compatibility mode. Kill
2145 * the timers stone dead (this may be expensive for large N groups), they
2146 * will be restarted if Compatibility Mode deems that they must be due to
2150 igmp_v3_cancel_link_timers(struct igmp_ifinfo
*igi
)
2153 struct in_multi
*inm
;
2154 struct in_multistep step
;
2156 IGI_LOCK_ASSERT_HELD(igi
);
2158 IGMP_PRINTF(("%s: cancel v3 timers on ifp %p(%s%d)\n", __func__
,
2159 igi
->igi_ifp
, igi
->igi_ifp
->if_name
, igi
->igi_ifp
->if_unit
));
2162 * Stop the v3 General Query Response on this link stone dead.
2163 * If fasttimo is woken up due to interface_timers_running,
2164 * the flag will be cleared if there are no pending link timers.
2166 igi
->igi_v3_timer
= 0;
2169 * Now clear the current-state and state-change report timers
2170 * for all memberships scoped to this link.
2175 in_multihead_lock_shared();
2176 IN_FIRST_MULTI(step
, inm
);
2177 while (inm
!= NULL
) {
2179 if (inm
->inm_ifp
!= ifp
)
2182 switch (inm
->inm_state
) {
2183 case IGMP_NOT_MEMBER
:
2184 case IGMP_SILENT_MEMBER
:
2185 case IGMP_IDLE_MEMBER
:
2186 case IGMP_LAZY_MEMBER
:
2187 case IGMP_SLEEPING_MEMBER
:
2188 case IGMP_AWAKENING_MEMBER
:
2190 * These states are either not relevant in v3 mode,
2191 * or are unreported. Do nothing.
2194 case IGMP_LEAVING_MEMBER
:
2196 * If we are leaving the group and switching to
2197 * compatibility mode, we need to release the final
2198 * reference held for issuing the INCLUDE {}, and
2199 * transition to REPORTING to ensure the host leave
2200 * message is sent upstream to the old querier --
2201 * transition to NOT would lose the leave and race.
2202 * During igmp_final_leave(), we bumped up both the
2203 * request and reference counts. Since we cannot
2204 * call in_multi_detach() here, defer this task to
2205 * the timer routine.
2207 VERIFY(inm
->inm_nrelecnt
!= 0);
2209 SLIST_INSERT_HEAD(&igi
->igi_relinmhead
, inm
, inm_nrele
);
2212 case IGMP_G_QUERY_PENDING_MEMBER
:
2213 case IGMP_SG_QUERY_PENDING_MEMBER
:
2214 inm_clear_recorded(inm
);
2216 case IGMP_REPORTING_MEMBER
:
2217 inm
->inm_state
= IGMP_REPORTING_MEMBER
;
2221 * Always clear state-change and group report timers.
2222 * Free any pending IGMPv3 state-change records.
2224 inm
->inm_sctimer
= 0;
2226 IF_DRAIN(&inm
->inm_scq
);
2229 IN_NEXT_MULTI(step
, inm
);
2231 in_multihead_lock_done();
2237 * Update the Older Version Querier Present timers for a link.
2238 * See Section 7.2.1 of RFC 3376.
2241 igmp_v1v2_process_querier_timers(struct igmp_ifinfo
*igi
)
2243 IGI_LOCK_ASSERT_HELD(igi
);
2245 if (igi
->igi_v1_timer
== 0 && igi
->igi_v2_timer
== 0) {
2247 * IGMPv1 and IGMPv2 Querier Present timers expired.
2251 if (igi
->igi_version
!= IGMP_VERSION_3
) {
2252 IGMP_PRINTF(("%s: transition from v%d -> v%d on %p(%s%d)\n",
2253 __func__
, igi
->igi_version
, IGMP_VERSION_3
,
2254 igi
->igi_ifp
, igi
->igi_ifp
->if_name
,
2255 igi
->igi_ifp
->if_unit
));
2256 igi
->igi_version
= IGMP_VERSION_3
;
2257 IF_DRAIN(&igi
->igi_v2q
);
2259 } else if (igi
->igi_v1_timer
== 0 && igi
->igi_v2_timer
> 0) {
2261 * IGMPv1 Querier Present timer expired,
2262 * IGMPv2 Querier Present timer running.
2263 * If IGMPv2 was disabled since last timeout,
2265 * If IGMPv2 is enabled, revert to IGMPv2.
2267 if (!igmp_v2enable
) {
2268 IGMP_PRINTF(("%s: transition from v%d -> v%d on %p(%s%d)\n",
2269 __func__
, igi
->igi_version
, IGMP_VERSION_3
,
2270 igi
->igi_ifp
, igi
->igi_ifp
->if_name
,
2271 igi
->igi_ifp
->if_unit
));
2272 igi
->igi_v2_timer
= 0;
2273 igi
->igi_version
= IGMP_VERSION_3
;
2274 IF_DRAIN(&igi
->igi_v2q
);
2276 --igi
->igi_v2_timer
;
2277 if (igi
->igi_version
!= IGMP_VERSION_2
) {
2278 IGMP_PRINTF(("%s: transition from v%d -> v%d on %p(%s%d)\n",
2279 __func__
, igi
->igi_version
, IGMP_VERSION_2
,
2280 igi
->igi_ifp
, igi
->igi_ifp
->if_name
,
2281 igi
->igi_ifp
->if_unit
));
2282 igi
->igi_version
= IGMP_VERSION_2
;
2283 IF_DRAIN(&igi
->igi_gq
);
2286 } else if (igi
->igi_v1_timer
> 0) {
2288 * IGMPv1 Querier Present timer running.
2289 * Stop IGMPv2 timer if running.
2291 * If IGMPv1 was disabled since last timeout,
2293 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2295 if (!igmp_v1enable
) {
2296 IGMP_PRINTF(("%s: transition from v%d -> v%d on %p(%s%d)\n",
2297 __func__
, igi
->igi_version
, IGMP_VERSION_3
,
2298 igi
->igi_ifp
, igi
->igi_ifp
->if_name
,
2299 igi
->igi_ifp
->if_unit
));
2300 igi
->igi_v1_timer
= 0;
2301 igi
->igi_version
= IGMP_VERSION_3
;
2302 IF_DRAIN(&igi
->igi_v2q
);
2304 --igi
->igi_v1_timer
;
2306 if (igi
->igi_v2_timer
> 0) {
2307 IGMP_PRINTF(("%s: cancel v2 timer on %p(%s%d)\n",
2308 __func__
, igi
->igi_ifp
, igi
->igi_ifp
->if_name
,
2309 igi
->igi_ifp
->if_unit
));
2310 igi
->igi_v2_timer
= 0;
2316 * Dispatch an IGMPv1/v2 host report or leave message.
2317 * These are always small enough to fit inside a single mbuf.
2320 igmp_v1v2_queue_report(struct in_multi
*inm
, const int type
)
2328 INM_LOCK_ASSERT_HELD(inm
);
2329 IGI_LOCK_ASSERT_HELD(inm
->inm_igi
);
2333 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
2336 MH_ALIGN(m
, sizeof(struct ip
) + sizeof(struct igmp
));
2338 m
->m_pkthdr
.len
= sizeof(struct ip
) + sizeof(struct igmp
);
2340 m
->m_data
+= sizeof(struct ip
);
2341 m
->m_len
= sizeof(struct igmp
);
2343 igmp
= mtod(m
, struct igmp
*);
2344 igmp
->igmp_type
= type
;
2345 igmp
->igmp_code
= 0;
2346 igmp
->igmp_group
= inm
->inm_addr
;
2347 igmp
->igmp_cksum
= 0;
2348 igmp
->igmp_cksum
= in_cksum(m
, sizeof(struct igmp
));
2350 m
->m_data
-= sizeof(struct ip
);
2351 m
->m_len
+= sizeof(struct ip
);
2353 ip
= mtod(m
, struct ip
*);
2355 ip
->ip_len
= sizeof(struct ip
) + sizeof(struct igmp
);
2357 ip
->ip_p
= IPPROTO_IGMP
;
2358 ip
->ip_src
.s_addr
= INADDR_ANY
;
2360 if (type
== IGMP_HOST_LEAVE_MESSAGE
)
2361 ip
->ip_dst
.s_addr
= htonl(INADDR_ALLRTRS_GROUP
);
2363 ip
->ip_dst
= inm
->inm_addr
;
2365 m
->m_flags
|= M_IGMPV2
;
2366 if (inm
->inm_igi
->igi_flags
& IGIF_LOOPBACK
)
2367 m
->m_flags
|= M_IGMP_LOOP
;
2370 * Due to the fact that at this point we are possibly holding
2371 * in_multihead_lock in shared or exclusive mode, we can't call
2372 * igmp_sendpkt() here since that will eventually call ip_output(),
2373 * which will try to lock in_multihead_lock and cause a deadlock.
2374 * Instead we defer the work to the igmp_slowtimo() thread, thus
2375 * avoiding unlocking in_multihead_lock here.
2377 if (IF_QFULL(&inm
->inm_igi
->igi_v2q
)) {
2378 IGMP_PRINTF(("%s: v1/v2 outbound queue full\n", __func__
));
2382 IF_ENQUEUE(&inm
->inm_igi
->igi_v2q
, m
);
2388 * Process a state change from the upper layer for the given IPv4 group.
2390 * Each socket holds a reference on the in_multi in its own ip_moptions.
2391 * The socket layer will have made the necessary updates to the group
2392 * state, it is now up to IGMP to issue a state change report if there
2393 * has been any change between T0 (when the last state-change was issued)
2396 * We use the IGMPv3 state machine at group level. The IGMP module
2397 * however makes the decision as to which IGMP protocol version to speak.
2398 * A state change *from* INCLUDE {} always means an initial join.
2399 * A state change *to* INCLUDE {} always means a final leave.
2401 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2402 * save ourselves a bunch of work; any exclusive mode groups need not
2403 * compute source filter lists.
2406 igmp_change_state(struct in_multi
*inm
)
2408 struct igmp_ifinfo
*igi
;
2412 INM_LOCK_ASSERT_HELD(inm
);
2413 VERIFY(inm
->inm_igi
!= NULL
);
2414 IGI_LOCK_ASSERT_NOTHELD(inm
->inm_igi
);
2417 * Try to detect if the upper layer just asked us to change state
2418 * for an interface which has now gone away.
2420 VERIFY(inm
->inm_ifma
!= NULL
);
2421 ifp
= inm
->inm_ifma
->ifma_ifp
;
2423 * Sanity check that netinet's notion of ifp is the same as net's.
2425 VERIFY(inm
->inm_ifp
== ifp
);
2427 igi
= IGMP_IFINFO(ifp
);
2428 VERIFY(igi
!= NULL
);
2431 * If we detect a state transition to or from MCAST_UNDEFINED
2432 * for this group, then we are starting or finishing an IGMP
2433 * life cycle for this group.
2435 if (inm
->inm_st
[1].iss_fmode
!= inm
->inm_st
[0].iss_fmode
) {
2436 IGMP_PRINTF(("%s: inm transition %d -> %d\n", __func__
,
2437 inm
->inm_st
[0].iss_fmode
, inm
->inm_st
[1].iss_fmode
));
2438 if (inm
->inm_st
[0].iss_fmode
== MCAST_UNDEFINED
) {
2439 IGMP_PRINTF(("%s: initial join\n", __func__
));
2440 error
= igmp_initial_join(inm
, igi
);
2442 } else if (inm
->inm_st
[1].iss_fmode
== MCAST_UNDEFINED
) {
2443 IGMP_PRINTF(("%s: final leave\n", __func__
));
2444 igmp_final_leave(inm
, igi
);
2448 IGMP_PRINTF(("%s: filter set change\n", __func__
));
2451 error
= igmp_handle_state_change(inm
, igi
);
2457 * Perform the initial join for an IGMP group.
2459 * When joining a group:
2460 * If the group should have its IGMP traffic suppressed, do nothing.
2461 * IGMPv1 starts sending IGMPv1 host membership reports.
2462 * IGMPv2 starts sending IGMPv2 host membership reports.
2463 * IGMPv3 will schedule an IGMPv3 state-change report containing the
2464 * initial state of the membership.
2467 igmp_initial_join(struct in_multi
*inm
, struct igmp_ifinfo
*igi
)
2470 struct ifqueue
*ifq
;
2471 int error
, retval
, syncstates
;
2473 INM_LOCK_ASSERT_HELD(inm
);
2474 IGI_LOCK_ASSERT_NOTHELD(igi
);
2476 IGMP_PRINTF(("%s: initial join %s on ifp %p(%s%d)\n",
2477 __func__
, inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
,
2478 inm
->inm_ifp
->if_name
, inm
->inm_ifp
->if_unit
));
2486 VERIFY(igi
->igi_ifp
== ifp
);
2489 * Groups joined on loopback or marked as 'not reported',
2490 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2491 * are never reported in any IGMP protocol exchanges.
2492 * All other groups enter the appropriate IGMP state machine
2493 * for the version in use on this link.
2494 * A link marked as IGIF_SILENT causes IGMP to be completely
2495 * disabled for the link.
2497 if ((ifp
->if_flags
& IFF_LOOPBACK
) ||
2498 (igi
->igi_flags
& IGIF_SILENT
) ||
2499 !igmp_isgroupreported(inm
->inm_addr
)) {
2500 IGMP_PRINTF(("%s: not kicking state machine for silent group\n",
2502 inm
->inm_state
= IGMP_SILENT_MEMBER
;
2506 * Deal with overlapping in_multi lifecycle.
2507 * If this group was LEAVING, then make sure
2508 * we drop the reference we picked up to keep the
2509 * group around for the final INCLUDE {} enqueue.
2510 * Since we cannot call in_multi_detach() here,
2511 * defer this task to the timer routine.
2513 if (igi
->igi_version
== IGMP_VERSION_3
&&
2514 inm
->inm_state
== IGMP_LEAVING_MEMBER
) {
2515 VERIFY(inm
->inm_nrelecnt
!= 0);
2516 SLIST_INSERT_HEAD(&igi
->igi_relinmhead
, inm
, inm_nrele
);
2519 inm
->inm_state
= IGMP_REPORTING_MEMBER
;
2521 switch (igi
->igi_version
) {
2522 case IGMP_VERSION_1
:
2523 case IGMP_VERSION_2
:
2524 inm
->inm_state
= IGMP_IDLE_MEMBER
;
2525 error
= igmp_v1v2_queue_report(inm
,
2526 (igi
->igi_version
== IGMP_VERSION_2
) ?
2527 IGMP_v2_HOST_MEMBERSHIP_REPORT
:
2528 IGMP_v1_HOST_MEMBERSHIP_REPORT
);
2530 INM_LOCK_ASSERT_HELD(inm
);
2531 IGI_LOCK_ASSERT_HELD(igi
);
2534 inm
->inm_timer
= IGMP_RANDOM_DELAY(
2535 IGMP_V1V2_MAX_RI
* PR_SLOWHZ
);
2536 current_state_timers_running
= 1;
2540 case IGMP_VERSION_3
:
2542 * Defer update of T0 to T1, until the first copy
2543 * of the state change has been transmitted.
2548 * Immediately enqueue a State-Change Report for
2549 * this interface, freeing any previous reports.
2550 * Don't kick the timers if there is nothing to do,
2551 * or if an error occurred.
2553 ifq
= &inm
->inm_scq
;
2555 retval
= igmp_v3_enqueue_group_record(ifq
, inm
, 1,
2557 IGMP_PRINTF(("%s: enqueue record = %d\n",
2560 error
= retval
* -1;
2565 * Schedule transmission of pending state-change
2566 * report up to RV times for this link. The timer
2567 * will fire at the next igmp_fasttimo (~200ms),
2568 * giving us an opportunity to merge the reports.
2570 if (igi
->igi_flags
& IGIF_LOOPBACK
) {
2573 VERIFY(igi
->igi_rv
> 1);
2574 inm
->inm_scrv
= igi
->igi_rv
;
2576 inm
->inm_sctimer
= 1;
2577 state_change_timers_running
= 1;
2586 * Only update the T0 state if state change is atomic,
2587 * i.e. we don't need to wait for a timer to fire before we
2588 * can consider the state change to have been communicated.
2592 IGMP_PRINTF(("%s: T1 -> T0 for %s/%s%d\n", __func__
,
2593 inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
->if_name
,
2594 inm
->inm_ifp
->if_unit
));
2601 * Issue an intermediate state change during the IGMP life-cycle.
2604 igmp_handle_state_change(struct in_multi
*inm
, struct igmp_ifinfo
*igi
)
2609 INM_LOCK_ASSERT_HELD(inm
);
2610 IGI_LOCK_ASSERT_NOTHELD(igi
);
2612 IGMP_PRINTF(("%s: state change for %s on ifp %p(%s%d)\n",
2613 __func__
, inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
,
2614 inm
->inm_ifp
->if_name
, inm
->inm_ifp
->if_unit
));
2619 VERIFY(igi
->igi_ifp
== ifp
);
2621 if ((ifp
->if_flags
& IFF_LOOPBACK
) ||
2622 (igi
->igi_flags
& IGIF_SILENT
) ||
2623 !igmp_isgroupreported(inm
->inm_addr
) ||
2624 (igi
->igi_version
!= IGMP_VERSION_3
)) {
2626 if (!igmp_isgroupreported(inm
->inm_addr
)) {
2627 IGMP_PRINTF(("%s: not kicking state "
2628 "machine for silent group\n", __func__
));
2630 IGMP_PRINTF(("%s: nothing to do\n", __func__
));
2632 IGMP_PRINTF(("%s: T1 -> T0 for %s/%s\n", __func__
,
2633 inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
->if_name
));
2637 IF_DRAIN(&inm
->inm_scq
);
2639 retval
= igmp_v3_enqueue_group_record(&inm
->inm_scq
, inm
, 1, 0, 0);
2640 IGMP_PRINTF(("%s: enqueue record = %d\n", __func__
, retval
));
2646 * If record(s) were enqueued, start the state-change
2647 * report timer for this group.
2649 inm
->inm_scrv
= ((igi
->igi_flags
& IGIF_LOOPBACK
) ? 1 : igi
->igi_rv
);
2650 inm
->inm_sctimer
= 1;
2651 state_change_timers_running
= 1;
2658 * Perform the final leave for an IGMP group.
2660 * When leaving a group:
2661 * IGMPv1 does nothing.
2662 * IGMPv2 sends a host leave message, if and only if we are the reporter.
2663 * IGMPv3 enqueues a state-change report containing a transition
2664 * to INCLUDE {} for immediate transmission.
2667 igmp_final_leave(struct in_multi
*inm
, struct igmp_ifinfo
*igi
)
2671 INM_LOCK_ASSERT_HELD(inm
);
2672 IGI_LOCK_ASSERT_NOTHELD(igi
);
2674 IGMP_PRINTF(("%s: final leave %s on ifp %p(%s%d)\n",
2675 __func__
, inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
,
2676 inm
->inm_ifp
->if_name
, inm
->inm_ifp
->if_unit
));
2678 switch (inm
->inm_state
) {
2679 case IGMP_NOT_MEMBER
:
2680 case IGMP_SILENT_MEMBER
:
2681 case IGMP_LEAVING_MEMBER
:
2682 /* Already leaving or left; do nothing. */
2683 IGMP_PRINTF(("%s: not kicking state machine for silent group\n",
2686 case IGMP_REPORTING_MEMBER
:
2687 case IGMP_IDLE_MEMBER
:
2688 case IGMP_G_QUERY_PENDING_MEMBER
:
2689 case IGMP_SG_QUERY_PENDING_MEMBER
:
2691 if (igi
->igi_version
== IGMP_VERSION_2
) {
2692 if (inm
->inm_state
== IGMP_G_QUERY_PENDING_MEMBER
||
2693 inm
->inm_state
== IGMP_SG_QUERY_PENDING_MEMBER
) {
2694 panic("%s: IGMPv3 state reached, not IGMPv3 "
2695 "mode\n", __func__
);
2698 igmp_v1v2_queue_report(inm
, IGMP_HOST_LEAVE_MESSAGE
);
2700 INM_LOCK_ASSERT_HELD(inm
);
2701 IGI_LOCK_ASSERT_HELD(igi
);
2703 inm
->inm_state
= IGMP_NOT_MEMBER
;
2704 } else if (igi
->igi_version
== IGMP_VERSION_3
) {
2706 * Stop group timer and all pending reports.
2707 * Immediately enqueue a state-change report
2708 * TO_IN {} to be sent on the next fast timeout,
2709 * giving us an opportunity to merge reports.
2711 IF_DRAIN(&inm
->inm_scq
);
2713 if (igi
->igi_flags
& IGIF_LOOPBACK
) {
2716 inm
->inm_scrv
= igi
->igi_rv
;
2718 IGMP_PRINTF(("%s: Leaving %s/%s%d with %d "
2719 "pending retransmissions.\n", __func__
,
2720 inet_ntoa(inm
->inm_addr
),
2721 inm
->inm_ifp
->if_name
, inm
->inm_ifp
->if_unit
,
2723 if (inm
->inm_scrv
== 0) {
2724 inm
->inm_state
= IGMP_NOT_MEMBER
;
2725 inm
->inm_sctimer
= 0;
2729 * Stick around in the in_multihead list;
2730 * the final detach will be issued by
2731 * igmp_v3_process_group_timers() when
2732 * the retransmit timer expires.
2734 INM_ADDREF_LOCKED(inm
);
2735 VERIFY(inm
->inm_debug
& IFD_ATTACHED
);
2737 VERIFY(inm
->inm_reqcnt
>= 1);
2738 inm
->inm_nrelecnt
++;
2739 VERIFY(inm
->inm_nrelecnt
!= 0);
2741 retval
= igmp_v3_enqueue_group_record(
2742 &inm
->inm_scq
, inm
, 1, 0, 0);
2743 KASSERT(retval
!= 0,
2744 ("%s: enqueue record = %d\n", __func__
,
2747 inm
->inm_state
= IGMP_LEAVING_MEMBER
;
2748 inm
->inm_sctimer
= 1;
2749 state_change_timers_running
= 1;
2755 case IGMP_LAZY_MEMBER
:
2756 case IGMP_SLEEPING_MEMBER
:
2757 case IGMP_AWAKENING_MEMBER
:
2758 /* Our reports are suppressed; do nothing. */
2764 IGMP_PRINTF(("%s: T1 -> T0 for %s/%s%d\n", __func__
,
2765 inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
->if_name
,
2766 inm
->inm_ifp
->if_unit
));
2767 inm
->inm_st
[1].iss_fmode
= MCAST_UNDEFINED
;
2768 IGMP_PRINTF(("%s: T1 now MCAST_UNDEFINED for %s/%s%d\n",
2769 __func__
, inet_ntoa(inm
->inm_addr
), inm
->inm_ifp
->if_name
,
2770 inm
->inm_ifp
->if_unit
));
2775 * Enqueue an IGMPv3 group record to the given output queue.
2777 * XXX This function could do with having the allocation code
2778 * split out, and the multiple-tree-walks coalesced into a single
2779 * routine as has been done in igmp_v3_enqueue_filter_change().
2781 * If is_state_change is zero, a current-state record is appended.
2782 * If is_state_change is non-zero, a state-change report is appended.
2784 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2785 * If is_group_query is zero, and if there is a packet with free space
2786 * at the tail of the queue, it will be appended to providing there
2787 * is enough free space.
2788 * Otherwise a new mbuf packet chain is allocated.
2790 * If is_source_query is non-zero, each source is checked to see if
2791 * it was recorded for a Group-Source query, and will be omitted if
2792 * it is not both in-mode and recorded.
2794 * The function will attempt to allocate leading space in the packet
2795 * for the IP/IGMP header to be prepended without fragmenting the chain.
2797 * If successful the size of all data appended to the queue is returned,
2798 * otherwise an error code less than zero is returned, or zero if
2799 * no record(s) were appended.
2802 igmp_v3_enqueue_group_record(struct ifqueue
*ifq
, struct in_multi
*inm
,
2803 const int is_state_change
, const int is_group_query
,
2804 const int is_source_query
)
2806 struct igmp_grouprec ig
;
2807 struct igmp_grouprec
*pig
;
2809 struct ip_msource
*ims
, *nims
;
2810 struct mbuf
*m0
, *m
, *md
;
2811 int error
, is_filter_list_change
;
2812 int minrec0len
, m0srcs
, msrcs
, nbytes
, off
;
2813 int record_has_sources
;
2819 INM_LOCK_ASSERT_HELD(inm
);
2820 IGI_LOCK_ASSERT_HELD(inm
->inm_igi
);
2824 is_filter_list_change
= 0;
2831 record_has_sources
= 1;
2833 type
= IGMP_DO_NOTHING
;
2834 mode
= inm
->inm_st
[1].iss_fmode
;
2837 * If we did not transition out of ASM mode during t0->t1,
2838 * and there are no source nodes to process, we can skip
2839 * the generation of source records.
2841 if (inm
->inm_st
[0].iss_asm
> 0 && inm
->inm_st
[1].iss_asm
> 0 &&
2843 record_has_sources
= 0;
2845 if (is_state_change
) {
2847 * Queue a state change record.
2848 * If the mode did not change, and there are non-ASM
2849 * listeners or source filters present,
2850 * we potentially need to issue two records for the group.
2851 * If we are transitioning to MCAST_UNDEFINED, we need
2852 * not send any sources.
2853 * If there are ASM listeners, and there was no filter
2854 * mode transition of any kind, do nothing.
2856 if (mode
!= inm
->inm_st
[0].iss_fmode
) {
2857 if (mode
== MCAST_EXCLUDE
) {
2858 IGMP_PRINTF(("%s: change to EXCLUDE\n",
2860 type
= IGMP_CHANGE_TO_EXCLUDE_MODE
;
2862 IGMP_PRINTF(("%s: change to INCLUDE\n",
2864 type
= IGMP_CHANGE_TO_INCLUDE_MODE
;
2865 if (mode
== MCAST_UNDEFINED
)
2866 record_has_sources
= 0;
2869 if (record_has_sources
) {
2870 is_filter_list_change
= 1;
2872 type
= IGMP_DO_NOTHING
;
2877 * Queue a current state record.
2879 if (mode
== MCAST_EXCLUDE
) {
2880 type
= IGMP_MODE_IS_EXCLUDE
;
2881 } else if (mode
== MCAST_INCLUDE
) {
2882 type
= IGMP_MODE_IS_INCLUDE
;
2883 VERIFY(inm
->inm_st
[1].iss_asm
== 0);
2888 * Generate the filter list changes using a separate function.
2890 if (is_filter_list_change
)
2891 return (igmp_v3_enqueue_filter_change(ifq
, inm
));
2893 if (type
== IGMP_DO_NOTHING
) {
2894 IGMP_PRINTF(("%s: nothing to do for %s/%s%d\n",
2895 __func__
, inet_ntoa(inm
->inm_addr
),
2896 inm
->inm_ifp
->if_name
, inm
->inm_ifp
->if_unit
));
2901 * If any sources are present, we must be able to fit at least
2902 * one in the trailing space of the tail packet's mbuf,
2905 minrec0len
= sizeof(struct igmp_grouprec
);
2906 if (record_has_sources
)
2907 minrec0len
+= sizeof(in_addr_t
);
2909 IGMP_PRINTF(("%s: queueing %s for %s/%s%d\n", __func__
,
2910 igmp_rec_type_to_str(type
), inet_ntoa(inm
->inm_addr
),
2911 inm
->inm_ifp
->if_name
, inm
->inm_ifp
->if_unit
));
2914 * Check if we have a packet in the tail of the queue for this
2915 * group into which the first group record for this group will fit.
2916 * Otherwise allocate a new packet.
2917 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2918 * Note: Group records for G/GSR query responses MUST be sent
2919 * in their own packet.
2922 if (!is_group_query
&&
2924 (m0
->m_pkthdr
.vt_nrecs
+ 1 <= IGMP_V3_REPORT_MAXRECS
) &&
2925 (m0
->m_pkthdr
.len
+ minrec0len
) <
2926 (ifp
->if_mtu
- IGMP_LEADINGSPACE
)) {
2927 m0srcs
= (ifp
->if_mtu
- m0
->m_pkthdr
.len
-
2928 sizeof(struct igmp_grouprec
)) / sizeof(in_addr_t
);
2930 IGMP_PRINTF(("%s: use existing packet\n", __func__
));
2932 if (IF_QFULL(ifq
)) {
2933 IGMP_PRINTF(("%s: outbound queue full\n", __func__
));
2937 m0srcs
= (ifp
->if_mtu
- IGMP_LEADINGSPACE
-
2938 sizeof(struct igmp_grouprec
)) / sizeof(in_addr_t
);
2939 if (!is_state_change
&& !is_group_query
) {
2940 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
2942 m
->m_data
+= IGMP_LEADINGSPACE
;
2945 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
2947 MH_ALIGN(m
, IGMP_LEADINGSPACE
);
2952 IGMP_PRINTF(("%s: allocated first packet\n", __func__
));
2956 * Append group record.
2957 * If we have sources, we don't know how many yet.
2962 ig
.ig_group
= inm
->inm_addr
;
2963 if (!m_append(m
, sizeof(struct igmp_grouprec
), (void *)&ig
)) {
2966 IGMP_PRINTF(("%s: m_append() failed.\n", __func__
));
2969 nbytes
+= sizeof(struct igmp_grouprec
);
2972 * Append as many sources as will fit in the first packet.
2973 * If we are appending to a new packet, the chain allocation
2974 * may potentially use clusters; use m_getptr() in this case.
2975 * If we are appending to an existing packet, we need to obtain
2976 * a pointer to the group record after m_append(), in case a new
2977 * mbuf was allocated.
2978 * Only append sources which are in-mode at t1. If we are
2979 * transitioning to MCAST_UNDEFINED state on the group, do not
2980 * include source entries.
2981 * Only report recorded sources in our filter set when responding
2982 * to a group-source query.
2984 if (record_has_sources
) {
2987 pig
= (struct igmp_grouprec
*)(mtod(md
, uint8_t *) +
2988 md
->m_len
- nbytes
);
2990 md
= m_getptr(m
, 0, &off
);
2991 pig
= (struct igmp_grouprec
*)(mtod(md
, uint8_t *) +
2995 RB_FOREACH_SAFE(ims
, ip_msource_tree
, &inm
->inm_srcs
, nims
) {
2996 IGMP_PRINTF(("%s: visit node %s\n", __func__
,
2997 inet_ntoa_haddr(ims
->ims_haddr
)));
2998 now
= ims_get_mode(inm
, ims
, 1);
2999 IGMP_PRINTF(("%s: node is %d\n", __func__
, now
));
3000 if ((now
!= mode
) ||
3001 (now
== mode
&& mode
== MCAST_UNDEFINED
)) {
3002 IGMP_PRINTF(("%s: skip node\n", __func__
));
3005 if (is_source_query
&& ims
->ims_stp
== 0) {
3006 IGMP_PRINTF(("%s: skip unrecorded node\n",
3010 IGMP_PRINTF(("%s: append node\n", __func__
));
3011 naddr
= htonl(ims
->ims_haddr
);
3012 if (!m_append(m
, sizeof(in_addr_t
), (void *)&naddr
)) {
3015 IGMP_PRINTF(("%s: m_append() failed.\n",
3019 nbytes
+= sizeof(in_addr_t
);
3021 if (msrcs
== m0srcs
)
3024 IGMP_PRINTF(("%s: msrcs is %d this packet\n", __func__
,
3026 pig
->ig_numsrc
= htons(msrcs
);
3027 nbytes
+= (msrcs
* sizeof(in_addr_t
));
3030 if (is_source_query
&& msrcs
== 0) {
3031 IGMP_PRINTF(("%s: no recorded sources to report\n", __func__
));
3038 * We are good to go with first packet.
3041 IGMP_PRINTF(("%s: enqueueing first packet\n", __func__
));
3042 m
->m_pkthdr
.vt_nrecs
= 1;
3043 m
->m_pkthdr
.rcvif
= ifp
;
3046 m
->m_pkthdr
.vt_nrecs
++;
3049 * No further work needed if no source list in packet(s).
3051 if (!record_has_sources
)
3055 * Whilst sources remain to be announced, we need to allocate
3056 * a new packet and fill out as many sources as will fit.
3057 * Always try for a cluster first.
3059 while (nims
!= NULL
) {
3060 if (IF_QFULL(ifq
)) {
3061 IGMP_PRINTF(("%s: outbound queue full\n", __func__
));
3064 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
3066 m
->m_data
+= IGMP_LEADINGSPACE
;
3068 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
3070 MH_ALIGN(m
, IGMP_LEADINGSPACE
);
3074 md
= m_getptr(m
, 0, &off
);
3075 pig
= (struct igmp_grouprec
*)(mtod(md
, uint8_t *) + off
);
3076 IGMP_PRINTF(("%s: allocated next packet\n", __func__
));
3078 if (!m_append(m
, sizeof(struct igmp_grouprec
), (void *)&ig
)) {
3081 IGMP_PRINTF(("%s: m_append() failed.\n", __func__
));
3084 m
->m_pkthdr
.vt_nrecs
= 1;
3085 nbytes
+= sizeof(struct igmp_grouprec
);
3087 m0srcs
= (ifp
->if_mtu
- IGMP_LEADINGSPACE
-
3088 sizeof(struct igmp_grouprec
)) / sizeof(in_addr_t
);
3091 RB_FOREACH_FROM(ims
, ip_msource_tree
, nims
) {
3092 IGMP_PRINTF(("%s: visit node %s\n", __func__
,
3093 inet_ntoa_haddr(ims
->ims_haddr
)));
3094 now
= ims_get_mode(inm
, ims
, 1);
3095 if ((now
!= mode
) ||
3096 (now
== mode
&& mode
== MCAST_UNDEFINED
)) {
3097 IGMP_PRINTF(("%s: skip node\n", __func__
));
3100 if (is_source_query
&& ims
->ims_stp
== 0) {
3101 IGMP_PRINTF(("%s: skip unrecorded node\n",
3105 IGMP_PRINTF(("%s: append node\n", __func__
));
3106 naddr
= htonl(ims
->ims_haddr
);
3107 if (!m_append(m
, sizeof(in_addr_t
), (void *)&naddr
)) {
3110 IGMP_PRINTF(("%s: m_append() failed.\n",
3115 if (msrcs
== m0srcs
)
3118 pig
->ig_numsrc
= htons(msrcs
);
3119 nbytes
+= (msrcs
* sizeof(in_addr_t
));
3121 IGMP_PRINTF(("%s: enqueueing next packet\n", __func__
));
3122 m
->m_pkthdr
.rcvif
= ifp
;
3130 * Type used to mark record pass completion.
3131 * We exploit the fact we can cast to this easily from the
3132 * current filter modes on each ip_msource node.
3135 REC_NONE
= 0x00, /* MCAST_UNDEFINED */
3136 REC_ALLOW
= 0x01, /* MCAST_INCLUDE */
3137 REC_BLOCK
= 0x02, /* MCAST_EXCLUDE */
3138 REC_FULL
= REC_ALLOW
| REC_BLOCK
3142 * Enqueue an IGMPv3 filter list change to the given output queue.
3144 * Source list filter state is held in an RB-tree. When the filter list
3145 * for a group is changed without changing its mode, we need to compute
3146 * the deltas between T0 and T1 for each source in the filter set,
3147 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
3149 * As we may potentially queue two record types, and the entire R-B tree
3150 * needs to be walked at once, we break this out into its own function
3151 * so we can generate a tightly packed queue of packets.
3153 * XXX This could be written to only use one tree walk, although that makes
3154 * serializing into the mbuf chains a bit harder. For now we do two walks
3155 * which makes things easier on us, and it may or may not be harder on
3158 * If successful the size of all data appended to the queue is returned,
3159 * otherwise an error code less than zero is returned, or zero if
3160 * no record(s) were appended.
3163 igmp_v3_enqueue_filter_change(struct ifqueue
*ifq
, struct in_multi
*inm
)
3165 static const int MINRECLEN
=
3166 sizeof(struct igmp_grouprec
) + sizeof(in_addr_t
);
3168 struct igmp_grouprec ig
;
3169 struct igmp_grouprec
*pig
;
3170 struct ip_msource
*ims
, *nims
;
3171 struct mbuf
*m
, *m0
, *md
;
3173 int m0srcs
, nbytes
, npbytes
, off
, rsrcs
, schanged
;
3175 uint8_t mode
, now
, then
;
3176 rectype_t crt
, drt
, nrt
;
3178 INM_LOCK_ASSERT_HELD(inm
);
3180 if (inm
->inm_nsrc
== 0 ||
3181 (inm
->inm_st
[0].iss_asm
> 0 && inm
->inm_st
[1].iss_asm
> 0))
3184 ifp
= inm
->inm_ifp
; /* interface */
3185 mode
= inm
->inm_st
[1].iss_fmode
; /* filter mode at t1 */
3186 crt
= REC_NONE
; /* current group record type */
3187 drt
= REC_NONE
; /* mask of completed group record types */
3188 nrt
= REC_NONE
; /* record type for current node */
3189 m0srcs
= 0; /* # source which will fit in current mbuf chain */
3190 nbytes
= 0; /* # of bytes appended to group's state-change queue */
3191 npbytes
= 0; /* # of bytes appended this packet */
3192 rsrcs
= 0; /* # sources encoded in current record */
3193 schanged
= 0; /* # nodes encoded in overall filter change */
3194 nallow
= 0; /* # of source entries in ALLOW_NEW */
3195 nblock
= 0; /* # of source entries in BLOCK_OLD */
3196 nims
= NULL
; /* next tree node pointer */
3199 * For each possible filter record mode.
3200 * The first kind of source we encounter tells us which
3201 * is the first kind of record we start appending.
3202 * If a node transitioned to UNDEFINED at t1, its mode is treated
3203 * as the inverse of the group's filter mode.
3205 while (drt
!= REC_FULL
) {
3209 (m0
->m_pkthdr
.vt_nrecs
+ 1 <=
3210 IGMP_V3_REPORT_MAXRECS
) &&
3211 (m0
->m_pkthdr
.len
+ MINRECLEN
) <
3212 (ifp
->if_mtu
- IGMP_LEADINGSPACE
)) {
3214 m0srcs
= (ifp
->if_mtu
- m0
->m_pkthdr
.len
-
3215 sizeof(struct igmp_grouprec
)) /
3217 IGMP_PRINTF(("%s: use previous packet\n",
3220 m
= m_getcl(M_DONTWAIT
, MT_DATA
, M_PKTHDR
);
3222 m
->m_data
+= IGMP_LEADINGSPACE
;
3224 m
= m_gethdr(M_DONTWAIT
, MT_DATA
);
3226 MH_ALIGN(m
, IGMP_LEADINGSPACE
);
3229 IGMP_PRINTF(("%s: m_get*() failed\n",
3233 m
->m_pkthdr
.vt_nrecs
= 0;
3234 m0srcs
= (ifp
->if_mtu
- IGMP_LEADINGSPACE
-
3235 sizeof(struct igmp_grouprec
)) /
3238 IGMP_PRINTF(("%s: allocated new packet\n",
3242 * Append the IGMP group record header to the
3243 * current packet's data area.
3244 * Recalculate pointer to free space for next
3245 * group record, in case m_append() allocated
3246 * a new mbuf or cluster.
3248 memset(&ig
, 0, sizeof(ig
));
3249 ig
.ig_group
= inm
->inm_addr
;
3250 if (!m_append(m
, sizeof(ig
), (void *)&ig
)) {
3253 IGMP_PRINTF(("%s: m_append() failed\n",
3257 npbytes
+= sizeof(struct igmp_grouprec
);
3259 /* new packet; offset in c hain */
3260 md
= m_getptr(m
, npbytes
-
3261 sizeof(struct igmp_grouprec
), &off
);
3262 pig
= (struct igmp_grouprec
*)(mtod(md
,
3265 /* current packet; offset from last append */
3267 pig
= (struct igmp_grouprec
*)(mtod(md
,
3268 uint8_t *) + md
->m_len
-
3269 sizeof(struct igmp_grouprec
));
3272 * Begin walking the tree for this record type
3273 * pass, or continue from where we left off
3274 * previously if we had to allocate a new packet.
3275 * Only report deltas in-mode at t1.
3276 * We need not report included sources as allowed
3277 * if we are in inclusive mode on the group,
3278 * however the converse is not true.
3282 nims
= RB_MIN(ip_msource_tree
, &inm
->inm_srcs
);
3283 RB_FOREACH_FROM(ims
, ip_msource_tree
, nims
) {
3284 IGMP_PRINTF(("%s: visit node %s\n",
3285 __func__
, inet_ntoa_haddr(ims
->ims_haddr
)));
3286 now
= ims_get_mode(inm
, ims
, 1);
3287 then
= ims_get_mode(inm
, ims
, 0);
3288 IGMP_PRINTF(("%s: mode: t0 %d, t1 %d\n",
3289 __func__
, then
, now
));
3291 IGMP_PRINTF(("%s: skip unchanged\n",
3295 if (mode
== MCAST_EXCLUDE
&&
3296 now
== MCAST_INCLUDE
) {
3297 IGMP_PRINTF(("%s: skip IN src on EX "
3298 "group\n", __func__
));
3301 nrt
= (rectype_t
)now
;
3302 if (nrt
== REC_NONE
)
3303 nrt
= (rectype_t
)(~mode
& REC_FULL
);
3304 if (schanged
++ == 0) {
3306 } else if (crt
!= nrt
)
3308 naddr
= htonl(ims
->ims_haddr
);
3309 if (!m_append(m
, sizeof(in_addr_t
),
3313 IGMP_PRINTF(("%s: m_append() failed\n",
3317 nallow
+= !!(crt
== REC_ALLOW
);
3318 nblock
+= !!(crt
== REC_BLOCK
);
3319 if (++rsrcs
== m0srcs
)
3323 * If we did not append any tree nodes on this
3324 * pass, back out of allocations.
3327 npbytes
-= sizeof(struct igmp_grouprec
);
3329 IGMP_PRINTF(("%s: m_free(m)\n",
3333 IGMP_PRINTF(("%s: m_adj(m, -ig)\n",
3335 m_adj(m
, -((int)sizeof(
3336 struct igmp_grouprec
)));
3340 npbytes
+= (rsrcs
* sizeof(in_addr_t
));
3341 if (crt
== REC_ALLOW
)
3342 pig
->ig_type
= IGMP_ALLOW_NEW_SOURCES
;
3343 else if (crt
== REC_BLOCK
)
3344 pig
->ig_type
= IGMP_BLOCK_OLD_SOURCES
;
3345 pig
->ig_numsrc
= htons(rsrcs
);
3347 * Count the new group record, and enqueue this
3348 * packet if it wasn't already queued.
3350 m
->m_pkthdr
.vt_nrecs
++;
3351 m
->m_pkthdr
.rcvif
= ifp
;
3355 } while (nims
!= NULL
);
3357 crt
= (~crt
& REC_FULL
);
3360 IGMP_PRINTF(("%s: queued %d ALLOW_NEW, %d BLOCK_OLD\n", __func__
,
3367 igmp_v3_merge_state_changes(struct in_multi
*inm
, struct ifqueue
*ifscq
)
3370 struct mbuf
*m
; /* pending state-change */
3371 struct mbuf
*m0
; /* copy of pending state-change */
3372 struct mbuf
*mt
; /* last state-change in packet */
3374 int docopy
, domerge
;
3377 INM_LOCK_ASSERT_HELD(inm
);
3384 * If there are further pending retransmissions, make a writable
3385 * copy of each queued state-change message before merging.
3387 if (inm
->inm_scrv
> 0)
3392 if (gq
->ifq_head
== NULL
) {
3393 IGMP_PRINTF(("%s: WARNING: queue for inm %p is empty\n",
3399 * Use IF_REMQUEUE() instead of IF_DEQUEUE() below, since the
3400 * packet might not always be at the head of the ifqueue.
3405 * Only merge the report into the current packet if
3406 * there is sufficient space to do so; an IGMPv3 report
3407 * packet may only contain 65,535 group records.
3408 * Always use a simple mbuf chain concatentation to do this,
3409 * as large state changes for single groups may have
3410 * allocated clusters.
3413 mt
= ifscq
->ifq_tail
;
3415 recslen
= m_length(m
);
3417 if ((mt
->m_pkthdr
.vt_nrecs
+
3418 m
->m_pkthdr
.vt_nrecs
<=
3419 IGMP_V3_REPORT_MAXRECS
) &&
3420 (mt
->m_pkthdr
.len
+ recslen
<=
3421 (inm
->inm_ifp
->if_mtu
- IGMP_LEADINGSPACE
)))
3425 if (!domerge
&& IF_QFULL(gq
)) {
3426 IGMP_PRINTF(("%s: outbound queue full, skipping whole "
3427 "packet %p\n", __func__
, m
));
3438 IGMP_PRINTF(("%s: dequeueing %p\n", __func__
, m
));
3444 IGMP_PRINTF(("%s: copying %p\n", __func__
, m
));
3445 m0
= m_dup(m
, M_NOWAIT
);
3448 m0
->m_nextpkt
= NULL
;
3453 IGMP_PRINTF(("%s: queueing %p to ifscq %p)\n",
3454 __func__
, m0
, ifscq
));
3455 m0
->m_pkthdr
.rcvif
= inm
->inm_ifp
;
3456 IF_ENQUEUE(ifscq
, m0
);
3458 struct mbuf
*mtl
; /* last mbuf of packet mt */
3460 IGMP_PRINTF(("%s: merging %p with ifscq tail %p)\n",
3464 m0
->m_flags
&= ~M_PKTHDR
;
3465 mt
->m_pkthdr
.len
+= recslen
;
3466 mt
->m_pkthdr
.vt_nrecs
+=
3467 m0
->m_pkthdr
.vt_nrecs
;
3477 * Respond to a pending IGMPv3 General Query.
3480 igmp_v3_dispatch_general_query(struct igmp_ifinfo
*igi
)
3483 struct in_multi
*inm
;
3484 struct in_multistep step
;
3487 IGI_LOCK_ASSERT_HELD(igi
);
3489 VERIFY(igi
->igi_version
== IGMP_VERSION_3
);
3494 in_multihead_lock_shared();
3495 IN_FIRST_MULTI(step
, inm
);
3496 while (inm
!= NULL
) {
3498 if (inm
->inm_ifp
!= ifp
)
3501 switch (inm
->inm_state
) {
3502 case IGMP_NOT_MEMBER
:
3503 case IGMP_SILENT_MEMBER
:
3505 case IGMP_REPORTING_MEMBER
:
3506 case IGMP_IDLE_MEMBER
:
3507 case IGMP_LAZY_MEMBER
:
3508 case IGMP_SLEEPING_MEMBER
:
3509 case IGMP_AWAKENING_MEMBER
:
3510 inm
->inm_state
= IGMP_REPORTING_MEMBER
;
3512 retval
= igmp_v3_enqueue_group_record(&igi
->igi_gq
,
3515 IGMP_PRINTF(("%s: enqueue record = %d\n",
3518 case IGMP_G_QUERY_PENDING_MEMBER
:
3519 case IGMP_SG_QUERY_PENDING_MEMBER
:
3520 case IGMP_LEAVING_MEMBER
:
3525 IN_NEXT_MULTI(step
, inm
);
3527 in_multihead_lock_done();
3530 loop
= (igi
->igi_flags
& IGIF_LOOPBACK
) ? 1 : 0;
3531 igmp_dispatch_queue(igi
, &igi
->igi_gq
, IGMP_MAX_RESPONSE_BURST
,
3533 IGI_LOCK_ASSERT_HELD(igi
);
3535 * Slew transmission of bursts over 500ms intervals.
3537 if (igi
->igi_gq
.ifq_head
!= NULL
) {
3538 igi
->igi_v3_timer
= 1 + IGMP_RANDOM_DELAY(
3539 IGMP_RESPONSE_BURST_INTERVAL
);
3540 interface_timers_running
= 1;
3545 * Transmit the next pending IGMP message in the output queue.
3547 * Must not be called with inm_lock or igi_lock held.
3550 igmp_sendpkt(struct mbuf
*m
, struct ifnet
*ifp
)
3552 struct ip_moptions
*imo
;
3553 struct mbuf
*ipopts
, *m0
;
3557 IGMP_PRINTF(("%s: transmit %p\n", __func__
, m
));
3560 * Check if the ifnet is still attached.
3562 if (ifp
== NULL
|| !ifnet_is_attached(ifp
, 0)) {
3563 IGMP_PRINTF(("%s: dropped %p as ifp u went away.\n",
3566 OSAddAtomic(1, &ipstat
.ips_noroute
);
3570 ipopts
= igmp_sendra
? m_raopt
: NULL
;
3572 imo
= ip_allocmoptions(M_WAITOK
);
3578 imo
->imo_multicast_ttl
= 1;
3579 imo
->imo_multicast_vif
= -1;
3581 imo
->imo_multicast_loop
= (ip_mrouter
!= NULL
);
3583 imo
->imo_multicast_loop
= 0;
3587 * If the user requested that IGMP traffic be explicitly
3588 * redirected to the loopback interface (e.g. they are running a
3589 * MANET interface and the routing protocol needs to see the
3590 * updates), handle this now.
3592 if (m
->m_flags
& M_IGMP_LOOP
)
3593 imo
->imo_multicast_ifp
= lo_ifp
;
3595 imo
->imo_multicast_ifp
= ifp
;
3597 if (m
->m_flags
& M_IGMPV2
) {
3600 m0
= igmp_v3_encap_report(ifp
, m
);
3603 * If igmp_v3_encap_report() failed, then M_PREPEND()
3604 * already freed the original mbuf chain.
3605 * This means that we don't have to m_freem(m) here.
3607 IGMP_PRINTF(("%s: dropped %p\n", __func__
, m
));
3609 atomic_add_32(&ipstat
.ips_odropped
, 1);
3614 m
->m_flags
&= ~(M_PROTOFLAGS
| M_IGMP_LOOP
);
3615 m0
->m_pkthdr
.rcvif
= lo_ifp
;
3617 mac_netinet_igmp_send(ifp
, m0
);
3619 bzero(&ro
, sizeof (ro
));
3620 error
= ip_output(m0
, ipopts
, &ro
, 0, imo
, NULL
);
3621 if (ro
.ro_rt
!= NULL
) {
3629 IGMP_PRINTF(("%s: ip_output(%p) = %d\n", __func__
, m0
, error
));
3633 IGMPSTAT_INC(igps_snd_reports
);
3634 OIGMPSTAT_INC(igps_snd_reports
);
3637 * Encapsulate an IGMPv3 report.
3639 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3640 * chain has already had its IP/IGMPv3 header prepended. In this case
3641 * the function will not attempt to prepend; the lengths and checksums
3642 * will however be re-computed.
3644 * Returns a pointer to the new mbuf chain head, or NULL if the
3645 * allocation failed.
3647 static struct mbuf
*
3648 igmp_v3_encap_report(struct ifnet
*ifp
, struct mbuf
*m
)
3650 struct igmp_report
*igmp
;
3652 int hdrlen
, igmpreclen
;
3654 VERIFY((m
->m_flags
& M_PKTHDR
));
3656 igmpreclen
= m_length(m
);
3657 hdrlen
= sizeof(struct ip
) + sizeof(struct igmp_report
);
3659 if (m
->m_flags
& M_IGMPV3_HDR
) {
3660 igmpreclen
-= hdrlen
;
3662 M_PREPEND(m
, hdrlen
, M_DONTWAIT
);
3665 m
->m_flags
|= M_IGMPV3_HDR
;
3668 IGMP_PRINTF(("%s: igmpreclen is %d\n", __func__
, igmpreclen
));
3670 m
->m_data
+= sizeof(struct ip
);
3671 m
->m_len
-= sizeof(struct ip
);
3673 igmp
= mtod(m
, struct igmp_report
*);
3674 igmp
->ir_type
= IGMP_v3_HOST_MEMBERSHIP_REPORT
;
3677 igmp
->ir_numgrps
= htons(m
->m_pkthdr
.vt_nrecs
);
3679 igmp
->ir_cksum
= in_cksum(m
, sizeof(struct igmp_report
) + igmpreclen
);
3680 m
->m_pkthdr
.vt_nrecs
= 0;
3682 m
->m_data
-= sizeof(struct ip
);
3683 m
->m_len
+= sizeof(struct ip
);
3685 ip
= mtod(m
, struct ip
*);
3686 ip
->ip_tos
= IPTOS_PREC_INTERNETCONTROL
;
3687 ip
->ip_len
= hdrlen
+ igmpreclen
;
3689 ip
->ip_p
= IPPROTO_IGMP
;
3692 ip
->ip_src
.s_addr
= INADDR_ANY
;
3694 if (m
->m_flags
& M_IGMP_LOOP
) {
3695 struct in_ifaddr
*ia
;
3699 IFA_LOCK(&ia
->ia_ifa
);
3700 ip
->ip_src
= ia
->ia_addr
.sin_addr
;
3701 IFA_UNLOCK(&ia
->ia_ifa
);
3702 IFA_REMREF(&ia
->ia_ifa
);
3706 ip
->ip_dst
.s_addr
= htonl(INADDR_ALLRPTS_GROUP
);
3713 igmp_rec_type_to_str(const int type
)
3716 case IGMP_CHANGE_TO_EXCLUDE_MODE
:
3719 case IGMP_CHANGE_TO_INCLUDE_MODE
:
3722 case IGMP_MODE_IS_EXCLUDE
:
3725 case IGMP_MODE_IS_INCLUDE
:
3728 case IGMP_ALLOW_NEW_SOURCES
:
3731 case IGMP_BLOCK_OLD_SOURCES
:
3745 IGMP_PRINTF(("%s: initializing\n", __func__
));
3747 igmp_timers_are_running
= 0;
3749 /* Setup lock group and attribute for igmp_mtx */
3750 igmp_mtx_grp_attr
= lck_grp_attr_alloc_init();
3751 igmp_mtx_grp
= lck_grp_alloc_init("igmp_mtx", igmp_mtx_grp_attr
);
3752 igmp_mtx_attr
= lck_attr_alloc_init();
3753 lck_mtx_init(&igmp_mtx
, igmp_mtx_grp
, igmp_mtx_attr
);
3755 LIST_INIT(&igi_head
);
3756 m_raopt
= igmp_ra_alloc();
3758 igi_size
= sizeof (struct igmp_ifinfo
);
3759 igi_zone
= zinit(igi_size
, IGI_ZONE_MAX
* igi_size
,
3761 if (igi_zone
== NULL
) {
3762 panic("%s: failed allocating %s", __func__
, IGI_ZONE_NAME
);
3765 zone_change(igi_zone
, Z_EXPAND
, TRUE
);
3766 zone_change(igi_zone
, Z_CALLERACCT
, FALSE
);