2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1988 Stephen Deering.
32 * Copyright (c) 1992, 1993
33 * The Regents of the University of California. All rights reserved.
35 * This code is derived from software contributed to Berkeley by
36 * Stephen Deering of Stanford University.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
70 * Internet Group Management Protocol (IGMP) routines.
72 * Written by Steve Deering, Stanford, May 1988.
73 * Modified by Rosen Sharma, Stanford, Aug 1994.
74 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
75 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
77 * MULTICAST Revision: 3.5.1.4
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/malloc.h>
84 #include <sys/socket.h>
85 #include <sys/protosw.h>
86 #include <sys/kernel.h>
87 #include <sys/sysctl.h>
90 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_var.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/igmp.h>
98 #include <netinet/igmp_var.h>
101 static MALLOC_DEFINE(M_IGMP
, "igmp", "igmp state");
104 static struct router_info
*
105 find_rti(struct ifnet
*ifp
);
107 static struct igmpstat igmpstat
;
109 SYSCTL_STRUCT(_net_inet_igmp
, IGMPCTL_STATS
, stats
, CTLFLAG_RD
,
110 &igmpstat
, igmpstat
, "");
112 static int igmp_timers_are_running
;
113 static u_long igmp_all_hosts_group
;
114 static u_long igmp_all_rtrs_group
;
115 static struct mbuf
*router_alert
;
116 static struct router_info
*Head
;
118 static void igmp_sendpkt(struct in_multi
*, int, unsigned long);
126 * To avoid byte-swapping the same value over and over again.
128 igmp_all_hosts_group
= htonl(INADDR_ALLHOSTS_GROUP
);
129 igmp_all_rtrs_group
= htonl(INADDR_ALLRTRS_GROUP
);
131 igmp_timers_are_running
= 0;
134 * Construct a Router Alert option to use in outgoing packets
136 MGET(router_alert
, M_DONTWAIT
, MT_DATA
);
137 ra
= mtod(router_alert
, struct ipoption
*);
138 ra
->ipopt_dst
.s_addr
= 0;
139 ra
->ipopt_list
[0] = IPOPT_RA
; /* Router Alert Option */
140 ra
->ipopt_list
[1] = 0x04; /* 4 bytes long */
141 ra
->ipopt_list
[2] = 0x00;
142 ra
->ipopt_list
[3] = 0x00;
143 router_alert
->m_len
= sizeof(ra
->ipopt_dst
) + ra
->ipopt_list
[1];
145 Head
= (struct router_info
*) 0;
148 static struct router_info
*
152 struct router_info
*rti
= Head
;
156 printf("[igmp.c, _find_rti] --> entering \n");
159 if (rti
->rti_ifp
== ifp
) {
161 printf("[igmp.c, _find_rti] --> found old entry \n");
168 MALLOC(rti
, struct router_info
*, sizeof *rti
, M_IGMP
, M_NOWAIT
);
172 rti
->rti_type
= IGMP_V2_ROUTER
;
174 rti
->rti_next
= Head
;
178 if (rti
) printf("[igmp.c, _find_rti] --> created an entry \n");
191 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
193 struct in_multi
*inm
;
194 struct in_ifaddr
*ia
;
195 struct in_multistep step
;
196 struct router_info
*rti
;
198 int timer
; /** timer value in the igmp query header **/
200 ++igmpstat
.igps_rcv_total
;
202 ip
= mtod(m
, struct ip
*);
203 igmplen
= ip
->ip_len
;
208 if (igmplen
< IGMP_MINLEN
) {
209 ++igmpstat
.igps_rcv_tooshort
;
213 minlen
= iphlen
+ IGMP_MINLEN
;
214 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
215 (m
= m_pullup(m
, minlen
)) == 0) {
216 ++igmpstat
.igps_rcv_tooshort
;
225 igmp
= mtod(m
, struct igmp
*);
226 if (in_cksum(m
, igmplen
)) {
227 ++igmpstat
.igps_rcv_badsum
;
234 ip
= mtod(m
, struct ip
*);
235 timer
= igmp
->igmp_code
* PR_FASTHZ
/ IGMP_TIMER_SCALE
;
245 * In the IGMPv2 specification, there are 3 states and a flag.
247 * In Non-Member state, we simply don't have a membership record.
248 * In Delaying Member state, our timer is running (inm->inm_timer)
249 * In Idle Member state, our timer is not running (inm->inm_timer==0)
251 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
252 * we have heard a report from another member, or IGMP_IREPORTEDLAST
253 * if I sent the last report.
255 switch (igmp
->igmp_type
) {
257 case IGMP_MEMBERSHIP_QUERY
:
258 ++igmpstat
.igps_rcv_queries
;
260 if (ifp
->if_flags
& IFF_LOOPBACK
)
263 if (igmp
->igmp_code
== 0) {
265 * Old router. Remember that the querier on this
266 * interface is old, and set the timer to the
270 rti
->rti_type
= IGMP_V1_ROUTER
;
273 timer
= IGMP_MAX_HOST_REPORT_DELAY
* PR_FASTHZ
;
275 if (ip
->ip_dst
.s_addr
!= igmp_all_hosts_group
||
276 igmp
->igmp_group
.s_addr
!= 0) {
277 ++igmpstat
.igps_rcv_badqueries
;
283 * New router. Simply do the new validity check.
286 if (igmp
->igmp_group
.s_addr
!= 0 &&
287 !IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
288 ++igmpstat
.igps_rcv_badqueries
;
295 * - Start the timers in all of our membership records
296 * that the query applies to for the interface on
297 * which the query arrived excl. those that belong
298 * to the "all-hosts" group (224.0.0.1).
299 * - Restart any timer that is already running but has
300 * a value longer than the requested timeout.
301 * - Use the value specified in the query message as
302 * the maximum timeout.
304 lck_mtx_lock(rt_mtx
);
305 IN_FIRST_MULTI(step
, inm
);
306 while (inm
!= NULL
) {
307 if (inm
->inm_ifp
== ifp
&&
308 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
309 (igmp
->igmp_group
.s_addr
== 0 ||
310 igmp
->igmp_group
.s_addr
== inm
->inm_addr
.s_addr
)) {
311 if (inm
->inm_timer
== 0 ||
312 inm
->inm_timer
> timer
) {
314 IGMP_RANDOM_DELAY(timer
);
315 igmp_timers_are_running
= 1;
318 IN_NEXT_MULTI(step
, inm
);
320 lck_mtx_unlock(rt_mtx
);
324 case IGMP_V1_MEMBERSHIP_REPORT
:
325 case IGMP_V2_MEMBERSHIP_REPORT
:
327 * For fast leave to work, we have to know that we are the
328 * last person to send a report for this group. Reports
329 * can potentially get looped back if we are a multicast
330 * router, so discard reports sourced by me.
333 if (ia
&& ip
->ip_src
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
)
336 ++igmpstat
.igps_rcv_reports
;
338 if (ifp
->if_flags
& IFF_LOOPBACK
)
341 if (!IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
342 ++igmpstat
.igps_rcv_badreports
;
348 * KLUDGE: if the IP source address of the report has an
349 * unspecified (i.e., zero) subnet number, as is allowed for
350 * a booting host, replace it with the correct subnet number
351 * so that a process-level multicast routing demon can
352 * determine which subnet it arrived from. This is necessary
353 * to compensate for the lack of any way for a process to
354 * determine the arrival interface of an incoming packet.
356 if ((ntohl(ip
->ip_src
.s_addr
) & IN_CLASSA_NET
) == 0)
357 if (ia
) ip
->ip_src
.s_addr
= htonl(ia
->ia_subnet
);
360 * If we belong to the group being reported, stop
361 * our timer for that group.
363 ifnet_lock_shared(ifp
);
364 IN_LOOKUP_MULTI(igmp
->igmp_group
, ifp
, inm
);
365 ifnet_lock_done(ifp
);
369 ++igmpstat
.igps_rcv_ourreports
;
371 inm
->inm_state
= IGMP_OTHERMEMBER
;
378 * Pass all valid IGMP packets up to any process(es) listening
379 * on a raw IGMP socket.
381 rip_input(m
, iphlen
);
386 struct in_multi
*inm
;
389 if (inm
->inm_addr
.s_addr
== igmp_all_hosts_group
390 || inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) {
392 inm
->inm_state
= IGMP_OTHERMEMBER
;
394 inm
->inm_rti
= find_rti(inm
->inm_ifp
);
395 if (inm
->inm_rti
== NULL
) return ENOMEM
;
396 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
397 inm
->inm_timer
= IGMP_RANDOM_DELAY(
398 IGMP_MAX_HOST_REPORT_DELAY
*PR_FASTHZ
);
399 inm
->inm_state
= IGMP_IREPORTEDLAST
;
400 igmp_timers_are_running
= 1;
407 struct in_multi
*inm
;
409 if (inm
->inm_state
== IGMP_IREPORTEDLAST
&&
410 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
411 !(inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) &&
412 inm
->inm_rti
->rti_type
!= IGMP_V1_ROUTER
)
413 igmp_sendpkt(inm
, IGMP_V2_LEAVE_GROUP
, igmp_all_rtrs_group
);
419 struct in_multi
*inm
;
420 struct in_multistep step
;
423 * Quick check to see if any work needs to be done, in order
424 * to minimize the overhead of fasttimo processing.
427 if (!igmp_timers_are_running
)
430 igmp_timers_are_running
= 0;
431 IN_FIRST_MULTI(step
, inm
);
432 while (inm
!= NULL
) {
433 if (inm
->inm_timer
== 0) {
435 } else if (--inm
->inm_timer
== 0) {
436 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
437 inm
->inm_state
= IGMP_IREPORTEDLAST
;
439 igmp_timers_are_running
= 1;
441 IN_NEXT_MULTI(step
, inm
);
448 struct router_info
*rti
= Head
;
451 printf("[igmp.c,_slowtimo] -- > entering \n");
454 if (rti
->rti_type
== IGMP_V1_ROUTER
) {
456 if (rti
->rti_time
>= IGMP_AGE_THRESHOLD
) {
457 rti
->rti_type
= IGMP_V2_ROUTER
;
463 printf("[igmp.c,_slowtimo] -- > exiting \n");
467 static struct route igmprt
;
470 igmp_sendpkt(inm
, type
, addr
)
471 struct in_multi
*inm
;
478 struct ip_moptions imo
;
480 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
484 m
->m_pkthdr
.rcvif
= loif
;
485 m
->m_pkthdr
.len
= sizeof(struct ip
) + IGMP_MINLEN
;
486 MH_ALIGN(m
, IGMP_MINLEN
+ sizeof(struct ip
));
487 m
->m_data
+= sizeof(struct ip
);
488 m
->m_len
= IGMP_MINLEN
;
489 m
->m_pkthdr
.csum_flags
= 0;
490 m
->m_pkthdr
.csum_data
= 0;
491 igmp
= mtod(m
, struct igmp
*);
492 igmp
->igmp_type
= type
;
494 igmp
->igmp_group
= inm
->inm_addr
;
495 igmp
->igmp_cksum
= 0;
496 igmp
->igmp_cksum
= in_cksum(m
, IGMP_MINLEN
);
498 m
->m_data
-= sizeof(struct ip
);
499 m
->m_len
+= sizeof(struct ip
);
500 ip
= mtod(m
, struct ip
*);
502 ip
->ip_len
= sizeof(struct ip
) + IGMP_MINLEN
;
504 ip
->ip_p
= IPPROTO_IGMP
;
505 ip
->ip_src
.s_addr
= INADDR_ANY
;
506 ip
->ip_dst
.s_addr
= addr
? addr
: igmp
->igmp_group
.s_addr
;
508 imo
.imo_multicast_ifp
= inm
->inm_ifp
;
509 imo
.imo_multicast_ttl
= 1;
510 imo
.imo_multicast_vif
= -1;
512 * Request loopback of the report if we are acting as a multicast
513 * router, so that the process-level routing demon can hear it.
515 imo
.imo_multicast_loop
= (ip_mrouter
!= NULL
);
519 * Do we have to worry about reentrancy here? Don't think so.
521 ip_output(m
, router_alert
, &igmprt
, 0, &imo
);
523 ++igmpstat
.igps_snd_reports
;