2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1988 Stephen Deering.
27 * Copyright (c) 1992, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software contributed to Berkeley by
31 * Stephen Deering of Stanford University.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
65 * Internet Group Management Protocol (IGMP) routines.
67 * Written by Steve Deering, Stanford, May 1988.
68 * Modified by Rosen Sharma, Stanford, Aug 1994.
69 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
70 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
72 * MULTICAST Revision: 3.5.1.4
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/malloc.h>
79 #include <sys/socket.h>
80 #include <sys/protosw.h>
81 #include <sys/kernel.h>
82 #include <sys/sysctl.h>
85 #include <net/route.h>
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/igmp.h>
93 #include <netinet/igmp_var.h>
96 static MALLOC_DEFINE(M_IGMP
, "igmp", "igmp state");
99 static struct router_info
*
100 find_rti
__P((struct ifnet
*ifp
));
102 static struct igmpstat igmpstat
;
104 SYSCTL_STRUCT(_net_inet_igmp
, IGMPCTL_STATS
, stats
, CTLFLAG_RD
,
105 &igmpstat
, igmpstat
, "");
107 static int igmp_timers_are_running
;
108 static u_long igmp_all_hosts_group
;
109 static u_long igmp_all_rtrs_group
;
110 static struct mbuf
*router_alert
;
111 static struct router_info
*Head
;
113 static void igmp_sendpkt
__P((struct in_multi
*, int, unsigned long));
121 * To avoid byte-swapping the same value over and over again.
123 igmp_all_hosts_group
= htonl(INADDR_ALLHOSTS_GROUP
);
124 igmp_all_rtrs_group
= htonl(INADDR_ALLRTRS_GROUP
);
126 igmp_timers_are_running
= 0;
129 * Construct a Router Alert option to use in outgoing packets
131 MGET(router_alert
, M_DONTWAIT
, MT_DATA
);
132 ra
= mtod(router_alert
, struct ipoption
*);
133 ra
->ipopt_dst
.s_addr
= 0;
134 ra
->ipopt_list
[0] = IPOPT_RA
; /* Router Alert Option */
135 ra
->ipopt_list
[1] = 0x04; /* 4 bytes long */
136 ra
->ipopt_list
[2] = 0x00;
137 ra
->ipopt_list
[3] = 0x00;
138 router_alert
->m_len
= sizeof(ra
->ipopt_dst
) + ra
->ipopt_list
[1];
140 Head
= (struct router_info
*) 0;
143 static struct router_info
*
147 register struct router_info
*rti
= Head
;
151 printf("[igmp.c, _find_rti] --> entering \n");
154 if (rti
->rti_ifp
== ifp
) {
156 printf("[igmp.c, _find_rti] --> found old entry \n");
163 MALLOC(rti
, struct router_info
*, sizeof *rti
, M_IGMP
, M_NOWAIT
);
167 rti
->rti_type
= IGMP_V2_ROUTER
;
169 rti
->rti_next
= Head
;
173 if (rti
) printf("[igmp.c, _find_rti] --> created an entry \n");
179 igmp_input(m
, iphlen
)
180 register struct mbuf
*m
;
183 register struct igmp
*igmp
;
184 register struct ip
*ip
;
185 register int igmplen
;
186 register struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
188 register struct in_multi
*inm
;
189 register struct in_ifaddr
*ia
;
190 struct in_multistep step
;
191 struct router_info
*rti
;
193 int timer
; /** timer value in the igmp query header **/
195 ++igmpstat
.igps_rcv_total
;
197 ip
= mtod(m
, struct ip
*);
198 igmplen
= ip
->ip_len
;
203 if (igmplen
< IGMP_MINLEN
) {
204 ++igmpstat
.igps_rcv_tooshort
;
208 minlen
= iphlen
+ IGMP_MINLEN
;
209 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
210 (m
= m_pullup(m
, minlen
)) == 0) {
211 ++igmpstat
.igps_rcv_tooshort
;
220 igmp
= mtod(m
, struct igmp
*);
221 if (in_cksum(m
, igmplen
)) {
222 ++igmpstat
.igps_rcv_badsum
;
229 ip
= mtod(m
, struct ip
*);
230 timer
= igmp
->igmp_code
* PR_FASTHZ
/ IGMP_TIMER_SCALE
;
240 * In the IGMPv2 specification, there are 3 states and a flag.
242 * In Non-Member state, we simply don't have a membership record.
243 * In Delaying Member state, our timer is running (inm->inm_timer)
244 * In Idle Member state, our timer is not running (inm->inm_timer==0)
246 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
247 * we have heard a report from another member, or IGMP_IREPORTEDLAST
248 * if I sent the last report.
250 switch (igmp
->igmp_type
) {
252 case IGMP_MEMBERSHIP_QUERY
:
253 ++igmpstat
.igps_rcv_queries
;
255 if (ifp
->if_flags
& IFF_LOOPBACK
)
258 if (igmp
->igmp_code
== 0) {
260 * Old router. Remember that the querier on this
261 * interface is old, and set the timer to the
265 rti
->rti_type
= IGMP_V1_ROUTER
;
268 timer
= IGMP_MAX_HOST_REPORT_DELAY
* PR_FASTHZ
;
270 if (ip
->ip_dst
.s_addr
!= igmp_all_hosts_group
||
271 igmp
->igmp_group
.s_addr
!= 0) {
272 ++igmpstat
.igps_rcv_badqueries
;
278 * New router. Simply do the new validity check.
281 if (igmp
->igmp_group
.s_addr
!= 0 &&
282 !IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
283 ++igmpstat
.igps_rcv_badqueries
;
290 * - Start the timers in all of our membership records
291 * that the query applies to for the interface on
292 * which the query arrived excl. those that belong
293 * to the "all-hosts" group (224.0.0.1).
294 * - Restart any timer that is already running but has
295 * a value longer than the requested timeout.
296 * - Use the value specified in the query message as
297 * the maximum timeout.
299 IN_FIRST_MULTI(step
, inm
);
300 while (inm
!= NULL
) {
301 if (inm
->inm_ifp
== ifp
&&
302 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
303 (igmp
->igmp_group
.s_addr
== 0 ||
304 igmp
->igmp_group
.s_addr
== inm
->inm_addr
.s_addr
)) {
305 if (inm
->inm_timer
== 0 ||
306 inm
->inm_timer
> timer
) {
308 IGMP_RANDOM_DELAY(timer
);
309 igmp_timers_are_running
= 1;
312 IN_NEXT_MULTI(step
, inm
);
317 case IGMP_V1_MEMBERSHIP_REPORT
:
318 case IGMP_V2_MEMBERSHIP_REPORT
:
320 * For fast leave to work, we have to know that we are the
321 * last person to send a report for this group. Reports
322 * can potentially get looped back if we are a multicast
323 * router, so discard reports sourced by me.
326 if (ia
&& ip
->ip_src
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
)
329 ++igmpstat
.igps_rcv_reports
;
331 if (ifp
->if_flags
& IFF_LOOPBACK
)
334 if (!IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
335 ++igmpstat
.igps_rcv_badreports
;
341 * KLUDGE: if the IP source address of the report has an
342 * unspecified (i.e., zero) subnet number, as is allowed for
343 * a booting host, replace it with the correct subnet number
344 * so that a process-level multicast routing demon can
345 * determine which subnet it arrived from. This is necessary
346 * to compensate for the lack of any way for a process to
347 * determine the arrival interface of an incoming packet.
349 if ((ntohl(ip
->ip_src
.s_addr
) & IN_CLASSA_NET
) == 0)
350 if (ia
) ip
->ip_src
.s_addr
= htonl(ia
->ia_subnet
);
353 * If we belong to the group being reported, stop
354 * our timer for that group.
356 IN_LOOKUP_MULTI(igmp
->igmp_group
, ifp
, inm
);
360 ++igmpstat
.igps_rcv_ourreports
;
362 inm
->inm_state
= IGMP_OTHERMEMBER
;
369 * Pass all valid IGMP packets up to any process(es) listening
370 * on a raw IGMP socket.
372 rip_input(m
, iphlen
);
377 struct in_multi
*inm
;
381 if (inm
->inm_addr
.s_addr
== igmp_all_hosts_group
382 || inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) {
384 inm
->inm_state
= IGMP_OTHERMEMBER
;
386 inm
->inm_rti
= find_rti(inm
->inm_ifp
);
387 if (inm
->inm_rti
== NULL
) return ENOMEM
;
388 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
389 inm
->inm_timer
= IGMP_RANDOM_DELAY(
390 IGMP_MAX_HOST_REPORT_DELAY
*PR_FASTHZ
);
391 inm
->inm_state
= IGMP_IREPORTEDLAST
;
392 igmp_timers_are_running
= 1;
400 struct in_multi
*inm
;
402 if (inm
->inm_state
== IGMP_IREPORTEDLAST
&&
403 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
404 !(inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) &&
405 inm
->inm_rti
->rti_type
!= IGMP_V1_ROUTER
)
406 igmp_sendpkt(inm
, IGMP_V2_LEAVE_GROUP
, igmp_all_rtrs_group
);
412 register struct in_multi
*inm
;
413 struct in_multistep step
;
417 * Quick check to see if any work needs to be done, in order
418 * to minimize the overhead of fasttimo processing.
421 if (!igmp_timers_are_running
)
425 igmp_timers_are_running
= 0;
426 IN_FIRST_MULTI(step
, inm
);
427 while (inm
!= NULL
) {
428 if (inm
->inm_timer
== 0) {
430 } else if (--inm
->inm_timer
== 0) {
431 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
432 inm
->inm_state
= IGMP_IREPORTEDLAST
;
434 igmp_timers_are_running
= 1;
436 IN_NEXT_MULTI(step
, inm
);
445 register struct router_info
*rti
= Head
;
448 printf("[igmp.c,_slowtimo] -- > entering \n");
451 if (rti
->rti_type
== IGMP_V1_ROUTER
) {
453 if (rti
->rti_time
>= IGMP_AGE_THRESHOLD
) {
454 rti
->rti_type
= IGMP_V2_ROUTER
;
460 printf("[igmp.c,_slowtimo] -- > exiting \n");
465 static struct route igmprt
;
468 igmp_sendpkt(inm
, type
, addr
)
469 struct in_multi
*inm
;
476 struct ip_moptions imo
;
478 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
482 m
->m_pkthdr
.rcvif
= loif
;
483 m
->m_pkthdr
.len
= sizeof(struct ip
) + IGMP_MINLEN
;
484 MH_ALIGN(m
, IGMP_MINLEN
+ sizeof(struct ip
));
485 m
->m_data
+= sizeof(struct ip
);
486 m
->m_len
= IGMP_MINLEN
;
487 m
->m_pkthdr
.csum_flags
= 0;
488 m
->m_pkthdr
.csum_data
= 0;
489 igmp
= mtod(m
, struct igmp
*);
490 igmp
->igmp_type
= type
;
492 igmp
->igmp_group
= inm
->inm_addr
;
493 igmp
->igmp_cksum
= 0;
494 igmp
->igmp_cksum
= in_cksum(m
, IGMP_MINLEN
);
496 m
->m_data
-= sizeof(struct ip
);
497 m
->m_len
+= sizeof(struct ip
);
498 ip
= mtod(m
, struct ip
*);
500 ip
->ip_len
= sizeof(struct ip
) + IGMP_MINLEN
;
502 ip
->ip_p
= IPPROTO_IGMP
;
503 ip
->ip_src
.s_addr
= INADDR_ANY
;
504 ip
->ip_dst
.s_addr
= addr
? addr
: igmp
->igmp_group
.s_addr
;
506 imo
.imo_multicast_ifp
= inm
->inm_ifp
;
507 imo
.imo_multicast_ttl
= 1;
508 imo
.imo_multicast_vif
= -1;
510 * Request loopback of the report if we are acting as a multicast
511 * router, so that the process-level routing demon can hear it.
513 imo
.imo_multicast_loop
= (ip_mrouter
!= NULL
);
517 * Do we have to worry about reentrancy here? Don't think so.
519 ip_output(m
, router_alert
, &igmprt
, 0, &imo
);
521 ++igmpstat
.igps_snd_reports
;