2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1988 Stephen Deering.
24 * Copyright (c) 1992, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software contributed to Berkeley by
28 * Stephen Deering of Stanford University.
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
62 * Internet Group Management Protocol (IGMP) routines.
64 * Written by Steve Deering, Stanford, May 1988.
65 * Modified by Rosen Sharma, Stanford, Aug 1994.
66 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
67 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
69 * MULTICAST Revision: 3.5.1.4
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/malloc.h>
76 #include <sys/socket.h>
77 #include <sys/protosw.h>
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
82 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_var.h>
89 #include <netinet/igmp.h>
90 #include <netinet/igmp_var.h>
92 static MALLOC_DEFINE(M_IGMP
, "igmp", "igmp state");
94 static struct router_info
*
95 find_rti
__P((struct ifnet
*ifp
));
97 static struct igmpstat igmpstat
;
99 SYSCTL_STRUCT(_net_inet_igmp
, IGMPCTL_STATS
, stats
, CTLFLAG_RD
,
100 &igmpstat
, igmpstat
, "");
102 static int igmp_timers_are_running
;
103 static u_long igmp_all_hosts_group
;
104 static u_long igmp_all_rtrs_group
;
105 static struct mbuf
*router_alert
;
106 static struct router_info
*Head
;
108 static void igmp_sendpkt
__P((struct in_multi
*, int, unsigned long));
116 * To avoid byte-swapping the same value over and over again.
118 igmp_all_hosts_group
= htonl(INADDR_ALLHOSTS_GROUP
);
119 igmp_all_rtrs_group
= htonl(INADDR_ALLRTRS_GROUP
);
121 igmp_timers_are_running
= 0;
124 * Construct a Router Alert option to use in outgoing packets
126 MGET(router_alert
, M_DONTWAIT
, MT_DATA
);
127 ra
= mtod(router_alert
, struct ipoption
*);
128 ra
->ipopt_dst
.s_addr
= 0;
129 ra
->ipopt_list
[0] = IPOPT_RA
; /* Router Alert Option */
130 ra
->ipopt_list
[1] = 0x04; /* 4 bytes long */
131 ra
->ipopt_list
[2] = 0x00;
132 ra
->ipopt_list
[3] = 0x00;
133 router_alert
->m_len
= sizeof(ra
->ipopt_dst
) + ra
->ipopt_list
[1];
135 Head
= (struct router_info
*) 0;
138 static struct router_info
*
142 register struct router_info
*rti
= Head
;
145 printf("[igmp.c, _find_rti] --> entering \n");
148 if (rti
->rti_ifp
== ifp
) {
150 printf("[igmp.c, _find_rti] --> found old entry \n");
158 MALLOC(rti
, struct router_info
*, sizeof *rti
, M_IGMP
, M_NOWAIT
);
160 MALLOC(rti
, struct router_info
*, sizeof *rti
, M_TEMP
, M_WAITOK
);
163 rti
->rti_type
= IGMP_V2_ROUTER
;
165 rti
->rti_next
= Head
;
168 printf("[igmp.c, _find_rti] --> created an entry \n");
174 igmp_input(m
, iphlen
)
175 register struct mbuf
*m
;
178 register struct igmp
*igmp
;
179 register struct ip
*ip
;
180 register int igmplen
;
181 register struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
183 register struct in_multi
*inm
;
184 register struct in_ifaddr
*ia
;
185 struct in_multistep step
;
186 struct router_info
*rti
;
188 int timer
; /** timer value in the igmp query header **/
190 ++igmpstat
.igps_rcv_total
;
192 ip
= mtod(m
, struct ip
*);
193 igmplen
= ip
->ip_len
;
198 if (igmplen
< IGMP_MINLEN
) {
199 ++igmpstat
.igps_rcv_tooshort
;
203 minlen
= iphlen
+ IGMP_MINLEN
;
204 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
205 (m
= m_pullup(m
, minlen
)) == 0) {
206 ++igmpstat
.igps_rcv_tooshort
;
215 igmp
= mtod(m
, struct igmp
*);
216 if (in_cksum(m
, igmplen
)) {
217 ++igmpstat
.igps_rcv_badsum
;
224 ip
= mtod(m
, struct ip
*);
225 timer
= igmp
->igmp_code
* PR_FASTHZ
/ IGMP_TIMER_SCALE
;
231 * In the IGMPv2 specification, there are 3 states and a flag.
233 * In Non-Member state, we simply don't have a membership record.
234 * In Delaying Member state, our timer is running (inm->inm_timer)
235 * In Idle Member state, our timer is not running (inm->inm_timer==0)
237 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
238 * we have heard a report from another member, or IGMP_IREPORTEDLAST
239 * if I sent the last report.
241 switch (igmp
->igmp_type
) {
243 case IGMP_MEMBERSHIP_QUERY
:
244 ++igmpstat
.igps_rcv_queries
;
246 if (ifp
->if_flags
& IFF_LOOPBACK
)
249 if (igmp
->igmp_code
== 0) {
251 * Old router. Remember that the querier on this
252 * interface is old, and set the timer to the
256 rti
->rti_type
= IGMP_V1_ROUTER
;
259 timer
= IGMP_MAX_HOST_REPORT_DELAY
* PR_FASTHZ
;
261 if (ip
->ip_dst
.s_addr
!= igmp_all_hosts_group
||
262 igmp
->igmp_group
.s_addr
!= 0) {
263 ++igmpstat
.igps_rcv_badqueries
;
269 * New router. Simply do the new validity check.
272 if (igmp
->igmp_group
.s_addr
!= 0 &&
273 !IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
274 ++igmpstat
.igps_rcv_badqueries
;
281 * - Start the timers in all of our membership records
282 * that the query applies to for the interface on
283 * which the query arrived excl. those that belong
284 * to the "all-hosts" group (224.0.0.1).
285 * - Restart any timer that is already running but has
286 * a value longer than the requested timeout.
287 * - Use the value specified in the query message as
288 * the maximum timeout.
290 IN_FIRST_MULTI(step
, inm
);
291 while (inm
!= NULL
) {
292 if (inm
->inm_ifp
== ifp
&&
293 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
294 (igmp
->igmp_group
.s_addr
== 0 ||
295 igmp
->igmp_group
.s_addr
== inm
->inm_addr
.s_addr
)) {
296 if (inm
->inm_timer
== 0 ||
297 inm
->inm_timer
> timer
) {
299 IGMP_RANDOM_DELAY(timer
);
300 igmp_timers_are_running
= 1;
303 IN_NEXT_MULTI(step
, inm
);
308 case IGMP_V1_MEMBERSHIP_REPORT
:
309 case IGMP_V2_MEMBERSHIP_REPORT
:
311 * For fast leave to work, we have to know that we are the
312 * last person to send a report for this group. Reports
313 * can potentially get looped back if we are a multicast
314 * router, so discard reports sourced by me.
317 if (ia
&& ip
->ip_src
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
)
320 ++igmpstat
.igps_rcv_reports
;
322 if (ifp
->if_flags
& IFF_LOOPBACK
)
325 if (!IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
326 ++igmpstat
.igps_rcv_badreports
;
332 * KLUDGE: if the IP source address of the report has an
333 * unspecified (i.e., zero) subnet number, as is allowed for
334 * a booting host, replace it with the correct subnet number
335 * so that a process-level multicast routing demon can
336 * determine which subnet it arrived from. This is necessary
337 * to compensate for the lack of any way for a process to
338 * determine the arrival interface of an incoming packet.
340 if ((ntohl(ip
->ip_src
.s_addr
) & IN_CLASSA_NET
) == 0)
341 if (ia
) ip
->ip_src
.s_addr
= htonl(ia
->ia_subnet
);
344 * If we belong to the group being reported, stop
345 * our timer for that group.
347 IN_LOOKUP_MULTI(igmp
->igmp_group
, ifp
, inm
);
351 ++igmpstat
.igps_rcv_ourreports
;
353 inm
->inm_state
= IGMP_OTHERMEMBER
;
360 * Pass all valid IGMP packets up to any process(es) listening
361 * on a raw IGMP socket.
363 rip_input(m
, iphlen
);
368 struct in_multi
*inm
;
372 if (inm
->inm_addr
.s_addr
== igmp_all_hosts_group
373 || inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) {
375 inm
->inm_state
= IGMP_OTHERMEMBER
;
377 inm
->inm_rti
= find_rti(inm
->inm_ifp
);
378 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
379 inm
->inm_timer
= IGMP_RANDOM_DELAY(
380 IGMP_MAX_HOST_REPORT_DELAY
*PR_FASTHZ
);
381 inm
->inm_state
= IGMP_IREPORTEDLAST
;
382 igmp_timers_are_running
= 1;
389 struct in_multi
*inm
;
391 if (inm
->inm_state
== IGMP_IREPORTEDLAST
&&
392 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
393 !(inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) &&
394 inm
->inm_rti
->rti_type
!= IGMP_V1_ROUTER
)
395 igmp_sendpkt(inm
, IGMP_V2_LEAVE_GROUP
, igmp_all_rtrs_group
);
401 register struct in_multi
*inm
;
402 struct in_multistep step
;
406 * Quick check to see if any work needs to be done, in order
407 * to minimize the overhead of fasttimo processing.
410 if (!igmp_timers_are_running
)
414 igmp_timers_are_running
= 0;
415 IN_FIRST_MULTI(step
, inm
);
416 while (inm
!= NULL
) {
417 if (inm
->inm_timer
== 0) {
419 } else if (--inm
->inm_timer
== 0) {
420 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
421 inm
->inm_state
= IGMP_IREPORTEDLAST
;
423 igmp_timers_are_running
= 1;
425 IN_NEXT_MULTI(step
, inm
);
434 register struct router_info
*rti
= Head
;
437 printf("[igmp.c,_slowtimo] -- > entering \n");
440 if (rti
->rti_type
== IGMP_V1_ROUTER
) {
442 if (rti
->rti_time
>= IGMP_AGE_THRESHOLD
) {
443 rti
->rti_type
= IGMP_V2_ROUTER
;
449 printf("[igmp.c,_slowtimo] -- > exiting \n");
454 static struct route igmprt
;
457 igmp_sendpkt(inm
, type
, addr
)
458 struct in_multi
*inm
;
465 struct ip_moptions imo
;
467 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
471 m
->m_pkthdr
.rcvif
= loif
;
472 m
->m_pkthdr
.len
= sizeof(struct ip
) + IGMP_MINLEN
;
473 MH_ALIGN(m
, IGMP_MINLEN
+ sizeof(struct ip
));
474 m
->m_data
+= sizeof(struct ip
);
475 m
->m_len
= IGMP_MINLEN
;
476 m
->m_pkthdr
.csum_flags
= 0;
477 m
->m_pkthdr
.csum_data
= 0;
478 igmp
= mtod(m
, struct igmp
*);
479 igmp
->igmp_type
= type
;
481 igmp
->igmp_group
= inm
->inm_addr
;
482 igmp
->igmp_cksum
= 0;
483 igmp
->igmp_cksum
= in_cksum(m
, IGMP_MINLEN
);
485 m
->m_data
-= sizeof(struct ip
);
486 m
->m_len
+= sizeof(struct ip
);
487 ip
= mtod(m
, struct ip
*);
489 ip
->ip_len
= sizeof(struct ip
) + IGMP_MINLEN
;
491 ip
->ip_p
= IPPROTO_IGMP
;
492 ip
->ip_src
.s_addr
= INADDR_ANY
;
493 ip
->ip_dst
.s_addr
= addr
? addr
: igmp
->igmp_group
.s_addr
;
495 imo
.imo_multicast_ifp
= inm
->inm_ifp
;
496 imo
.imo_multicast_ttl
= 1;
497 imo
.imo_multicast_vif
= -1;
499 * Request loopback of the report if we are acting as a multicast
500 * router, so that the process-level routing demon can hear it.
502 imo
.imo_multicast_loop
= (ip_mrouter
!= NULL
);
506 * Do we have to worry about reentrancy here? Don't think so.
509 m
->m_pkthdr
.rcvif
= NULL
;
511 ip_output(m
, router_alert
, &igmprt
, 0, &imo
);
513 ++igmpstat
.igps_snd_reports
;