2 * Copyright (c) 2000-2007 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) 1988 Stephen Deering.
30 * Copyright (c) 1992, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software contributed to Berkeley by
34 * Stephen Deering of Stanford University.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
67 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
68 * support for mandatory and extensible security protections. This notice
69 * is included in support of clause 2.2 (b) of the Apple Public License,
74 * Internet Group Management Protocol (IGMP) routines.
76 * Written by Steve Deering, Stanford, May 1988.
77 * Modified by Rosen Sharma, Stanford, Aug 1994.
78 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
79 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
81 * MULTICAST Revision: 3.5.1.4
84 #include <sys/param.h>
85 #include <sys/systm.h>
86 #include <sys/malloc.h>
88 #include <sys/socket.h>
89 #include <sys/protosw.h>
90 #include <sys/kernel.h>
91 #include <sys/sysctl.h>
94 #include <net/route.h>
96 #include <netinet/in.h>
97 #include <netinet/in_var.h>
98 #include <netinet/in_systm.h>
99 #include <netinet/ip.h>
100 #include <netinet/ip_var.h>
101 #include <netinet/igmp.h>
102 #include <netinet/igmp_var.h>
105 #include <security/mac_framework.h>
109 static MALLOC_DEFINE(M_IGMP
, "igmp", "igmp state");
112 static struct router_info
*
113 find_rti(struct ifnet
*ifp
, int wait
);
115 static struct igmpstat igmpstat
;
117 SYSCTL_STRUCT(_net_inet_igmp
, IGMPCTL_STATS
, stats
, CTLFLAG_RD
,
118 &igmpstat
, igmpstat
, "");
120 static int igmp_timers_are_running
;
121 static u_long igmp_all_hosts_group
;
122 static u_long igmp_all_rtrs_group
;
123 static struct mbuf
*router_alert
;
124 static struct router_info
*Head
;
126 static void igmp_sendpkt(struct in_multi
*, int, unsigned long);
134 * To avoid byte-swapping the same value over and over again.
136 igmp_all_hosts_group
= htonl(INADDR_ALLHOSTS_GROUP
);
137 igmp_all_rtrs_group
= htonl(INADDR_ALLRTRS_GROUP
);
139 igmp_timers_are_running
= 0;
142 * Construct a Router Alert option to use in outgoing packets
144 MGET(router_alert
, M_DONTWAIT
, MT_DATA
);
145 ra
= mtod(router_alert
, struct ipoption
*);
146 ra
->ipopt_dst
.s_addr
= 0;
147 ra
->ipopt_list
[0] = IPOPT_RA
; /* Router Alert Option */
148 ra
->ipopt_list
[1] = 0x04; /* 4 bytes long */
149 ra
->ipopt_list
[2] = 0x00;
150 ra
->ipopt_list
[3] = 0x00;
151 router_alert
->m_len
= sizeof(ra
->ipopt_dst
) + ra
->ipopt_list
[1];
153 Head
= (struct router_info
*) 0;
156 static struct router_info
*
158 struct ifnet
*ifp
, int wait
)
160 struct router_info
*rti
= Head
;
164 printf("[igmp.c, _find_rti] --> entering \n");
167 if (rti
->rti_ifp
== ifp
) {
169 printf("[igmp.c, _find_rti] --> found old entry \n");
176 MALLOC(rti
, struct router_info
*, sizeof *rti
, M_IGMP
, wait
);
180 rti
->rti_type
= IGMP_V2_ROUTER
;
182 rti
->rti_next
= Head
;
186 if (rti
) printf("[igmp.c, _find_rti] --> created an entry \n");
199 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
201 struct in_multi
*inm
;
202 struct in_ifaddr
*ia
;
203 struct in_multistep step
;
204 struct router_info
*rti
;
206 int timer
; /** timer value in the igmp query header **/
208 ++igmpstat
.igps_rcv_total
;
210 ip
= mtod(m
, struct ip
*);
211 igmplen
= ip
->ip_len
;
216 if (igmplen
< IGMP_MINLEN
) {
217 ++igmpstat
.igps_rcv_tooshort
;
221 minlen
= iphlen
+ IGMP_MINLEN
;
222 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
223 (m
= m_pullup(m
, minlen
)) == 0) {
224 ++igmpstat
.igps_rcv_tooshort
;
233 igmp
= mtod(m
, struct igmp
*);
234 if (in_cksum(m
, igmplen
)) {
235 ++igmpstat
.igps_rcv_badsum
;
242 ip
= mtod(m
, struct ip
*);
243 timer
= igmp
->igmp_code
* PR_FASTHZ
/ IGMP_TIMER_SCALE
;
246 rti
= find_rti(ifp
, M_NOWAIT
);
253 * In the IGMPv2 specification, there are 3 states and a flag.
255 * In Non-Member state, we simply don't have a membership record.
256 * In Delaying Member state, our timer is running (inm->inm_timer)
257 * In Idle Member state, our timer is not running (inm->inm_timer==0)
259 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
260 * we have heard a report from another member, or IGMP_IREPORTEDLAST
261 * if I sent the last report.
263 switch (igmp
->igmp_type
) {
265 case IGMP_MEMBERSHIP_QUERY
:
266 ++igmpstat
.igps_rcv_queries
;
268 if (ifp
->if_flags
& IFF_LOOPBACK
)
271 if (igmp
->igmp_code
== 0) {
273 * Old router. Remember that the querier on this
274 * interface is old, and set the timer to the
278 rti
->rti_type
= IGMP_V1_ROUTER
;
281 timer
= IGMP_MAX_HOST_REPORT_DELAY
* PR_FASTHZ
;
283 if (ip
->ip_dst
.s_addr
!= igmp_all_hosts_group
||
284 igmp
->igmp_group
.s_addr
!= 0) {
285 ++igmpstat
.igps_rcv_badqueries
;
291 * New router. Simply do the new validity check.
294 if (igmp
->igmp_group
.s_addr
!= 0 &&
295 !IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
296 ++igmpstat
.igps_rcv_badqueries
;
303 * - Start the timers in all of our membership records
304 * that the query applies to for the interface on
305 * which the query arrived excl. those that belong
306 * to the "all-hosts" group (224.0.0.1).
307 * - Restart any timer that is already running but has
308 * a value longer than the requested timeout.
309 * - Use the value specified in the query message as
310 * the maximum timeout.
312 lck_mtx_lock(rt_mtx
);
313 IN_FIRST_MULTI(step
, inm
);
314 while (inm
!= NULL
) {
315 if (inm
->inm_ifp
== ifp
&&
316 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
317 (igmp
->igmp_group
.s_addr
== 0 ||
318 igmp
->igmp_group
.s_addr
== inm
->inm_addr
.s_addr
)) {
319 if (inm
->inm_timer
== 0 ||
320 inm
->inm_timer
> timer
) {
322 IGMP_RANDOM_DELAY(timer
);
323 igmp_timers_are_running
= 1;
326 IN_NEXT_MULTI(step
, inm
);
328 lck_mtx_unlock(rt_mtx
);
332 case IGMP_V1_MEMBERSHIP_REPORT
:
333 case IGMP_V2_MEMBERSHIP_REPORT
:
335 * For fast leave to work, we have to know that we are the
336 * last person to send a report for this group. Reports
337 * can potentially get looped back if we are a multicast
338 * router, so discard reports sourced by me.
341 if (ia
&& ip
->ip_src
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
)
344 ++igmpstat
.igps_rcv_reports
;
346 if (ifp
->if_flags
& IFF_LOOPBACK
)
349 if (!IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
350 ++igmpstat
.igps_rcv_badreports
;
356 * KLUDGE: if the IP source address of the report has an
357 * unspecified (i.e., zero) subnet number, as is allowed for
358 * a booting host, replace it with the correct subnet number
359 * so that a process-level multicast routing demon can
360 * determine which subnet it arrived from. This is necessary
361 * to compensate for the lack of any way for a process to
362 * determine the arrival interface of an incoming packet.
364 if ((ntohl(ip
->ip_src
.s_addr
) & IN_CLASSA_NET
) == 0)
365 if (ia
) ip
->ip_src
.s_addr
= htonl(ia
->ia_subnet
);
368 * If we belong to the group being reported, stop
369 * our timer for that group.
371 ifnet_lock_shared(ifp
);
372 IN_LOOKUP_MULTI(igmp
->igmp_group
, ifp
, inm
);
373 ifnet_lock_done(ifp
);
377 ++igmpstat
.igps_rcv_ourreports
;
379 inm
->inm_state
= IGMP_OTHERMEMBER
;
386 * Pass all valid IGMP packets up to any process(es) listening
387 * on a raw IGMP socket.
389 rip_input(m
, iphlen
);
393 igmp_joingroup(struct in_multi
*inm
)
396 if (inm
->inm_addr
.s_addr
== igmp_all_hosts_group
397 || inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) {
399 inm
->inm_state
= IGMP_OTHERMEMBER
;
401 inm
->inm_rti
= find_rti(inm
->inm_ifp
, M_WAITOK
);
402 if (inm
->inm_rti
== NULL
) return ENOMEM
;
403 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
404 inm
->inm_timer
= IGMP_RANDOM_DELAY(
405 IGMP_MAX_HOST_REPORT_DELAY
*PR_FASTHZ
);
406 inm
->inm_state
= IGMP_IREPORTEDLAST
;
407 igmp_timers_are_running
= 1;
413 igmp_leavegroup(struct in_multi
*inm
)
415 if (inm
->inm_state
== IGMP_IREPORTEDLAST
&&
416 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
417 !(inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) &&
418 inm
->inm_rti
->rti_type
!= IGMP_V1_ROUTER
)
419 igmp_sendpkt(inm
, IGMP_V2_LEAVE_GROUP
, igmp_all_rtrs_group
);
425 struct in_multi
*inm
;
426 struct in_multistep step
;
429 * Quick check to see if any work needs to be done, in order
430 * to minimize the overhead of fasttimo processing.
433 if (!igmp_timers_are_running
)
436 igmp_timers_are_running
= 0;
437 IN_FIRST_MULTI(step
, inm
);
438 while (inm
!= NULL
) {
439 if (inm
->inm_timer
== 0) {
441 } else if ((--inm
->inm_timer
== 0) && (inm
->inm_rti
!= NULL
)) {
442 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
443 inm
->inm_state
= IGMP_IREPORTEDLAST
;
445 igmp_timers_are_running
= 1;
447 IN_NEXT_MULTI(step
, inm
);
454 struct router_info
*rti
= Head
;
457 printf("[igmp.c,_slowtimo] -- > entering \n");
460 if (rti
->rti_type
== IGMP_V1_ROUTER
) {
462 if (rti
->rti_time
>= IGMP_AGE_THRESHOLD
) {
463 rti
->rti_type
= IGMP_V2_ROUTER
;
469 printf("[igmp.c,_slowtimo] -- > exiting \n");
473 static struct route igmprt
;
476 igmp_sendpkt(struct in_multi
*inm
, int type
, unsigned long addr
)
481 struct ip_moptions imo
;
483 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
); /* MAC-OK */
487 m
->m_pkthdr
.rcvif
= lo_ifp
;
489 mac_mbuf_label_associate_linklayer(inm
->inm_ifp
, m
);
491 m
->m_pkthdr
.len
= sizeof(struct ip
) + IGMP_MINLEN
;
492 MH_ALIGN(m
, IGMP_MINLEN
+ sizeof(struct ip
));
493 m
->m_data
+= sizeof(struct ip
);
494 m
->m_len
= IGMP_MINLEN
;
495 m
->m_pkthdr
.csum_flags
= 0;
496 m
->m_pkthdr
.csum_data
= 0;
497 igmp
= mtod(m
, struct igmp
*);
498 igmp
->igmp_type
= type
;
500 igmp
->igmp_group
= inm
->inm_addr
;
501 igmp
->igmp_cksum
= 0;
502 igmp
->igmp_cksum
= in_cksum(m
, IGMP_MINLEN
);
504 m
->m_data
-= sizeof(struct ip
);
505 m
->m_len
+= sizeof(struct ip
);
506 ip
= mtod(m
, struct ip
*);
508 ip
->ip_len
= sizeof(struct ip
) + IGMP_MINLEN
;
510 ip
->ip_p
= IPPROTO_IGMP
;
511 ip
->ip_src
.s_addr
= INADDR_ANY
;
512 ip
->ip_dst
.s_addr
= addr
? addr
: igmp
->igmp_group
.s_addr
;
514 imo
.imo_multicast_ifp
= inm
->inm_ifp
;
515 imo
.imo_multicast_ttl
= 1;
516 imo
.imo_multicast_vif
= -1;
519 * Request loopback of the report if we are acting as a multicast
520 * router, so that the process-level routing demon can hear it.
522 imo
.imo_multicast_loop
= (ip_mrouter
!= NULL
);
524 imo
.imo_multicast_loop
= 0;
529 * Do we have to worry about reentrancy here? Don't think so.
531 ip_output(m
, router_alert
, &igmprt
, 0, &imo
, NULL
);
533 ++igmpstat
.igps_snd_reports
;