2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright (c) 1988 Stephen Deering.
25 * Copyright (c) 1992, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software contributed to Berkeley by
29 * Stephen Deering of Stanford University.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed by the University of
42 * California, Berkeley and its contributors.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
63 * Internet Group Management Protocol (IGMP) routines.
65 * Written by Steve Deering, Stanford, May 1988.
66 * Modified by Rosen Sharma, Stanford, Aug 1994.
67 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
68 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
70 * MULTICAST Revision: 3.5.1.4
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
77 #include <sys/socket.h>
78 #include <sys/protosw.h>
79 #include <sys/kernel.h>
80 #include <sys/sysctl.h>
83 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/igmp.h>
91 #include <netinet/igmp_var.h>
94 static MALLOC_DEFINE(M_IGMP
, "igmp", "igmp state");
97 static struct router_info
*
98 find_rti(struct ifnet
*ifp
);
100 static struct igmpstat igmpstat
;
102 SYSCTL_STRUCT(_net_inet_igmp
, IGMPCTL_STATS
, stats
, CTLFLAG_RD
,
103 &igmpstat
, igmpstat
, "");
105 static int igmp_timers_are_running
;
106 static u_long igmp_all_hosts_group
;
107 static u_long igmp_all_rtrs_group
;
108 static struct mbuf
*router_alert
;
109 static struct router_info
*Head
;
111 static void igmp_sendpkt(struct in_multi
*, int, unsigned long);
119 * To avoid byte-swapping the same value over and over again.
121 igmp_all_hosts_group
= htonl(INADDR_ALLHOSTS_GROUP
);
122 igmp_all_rtrs_group
= htonl(INADDR_ALLRTRS_GROUP
);
124 igmp_timers_are_running
= 0;
127 * Construct a Router Alert option to use in outgoing packets
129 MGET(router_alert
, M_DONTWAIT
, MT_DATA
);
130 ra
= mtod(router_alert
, struct ipoption
*);
131 ra
->ipopt_dst
.s_addr
= 0;
132 ra
->ipopt_list
[0] = IPOPT_RA
; /* Router Alert Option */
133 ra
->ipopt_list
[1] = 0x04; /* 4 bytes long */
134 ra
->ipopt_list
[2] = 0x00;
135 ra
->ipopt_list
[3] = 0x00;
136 router_alert
->m_len
= sizeof(ra
->ipopt_dst
) + ra
->ipopt_list
[1];
138 Head
= (struct router_info
*) 0;
141 static struct router_info
*
145 struct router_info
*rti
= Head
;
149 printf("[igmp.c, _find_rti] --> entering \n");
152 if (rti
->rti_ifp
== ifp
) {
154 printf("[igmp.c, _find_rti] --> found old entry \n");
161 MALLOC(rti
, struct router_info
*, sizeof *rti
, M_IGMP
, M_NOWAIT
);
165 rti
->rti_type
= IGMP_V2_ROUTER
;
167 rti
->rti_next
= Head
;
171 if (rti
) printf("[igmp.c, _find_rti] --> created an entry \n");
184 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
186 struct in_multi
*inm
;
187 struct in_ifaddr
*ia
;
188 struct in_multistep step
;
189 struct router_info
*rti
;
191 int timer
; /** timer value in the igmp query header **/
193 ++igmpstat
.igps_rcv_total
;
195 ip
= mtod(m
, struct ip
*);
196 igmplen
= ip
->ip_len
;
201 if (igmplen
< IGMP_MINLEN
) {
202 ++igmpstat
.igps_rcv_tooshort
;
206 minlen
= iphlen
+ IGMP_MINLEN
;
207 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
208 (m
= m_pullup(m
, minlen
)) == 0) {
209 ++igmpstat
.igps_rcv_tooshort
;
218 igmp
= mtod(m
, struct igmp
*);
219 if (in_cksum(m
, igmplen
)) {
220 ++igmpstat
.igps_rcv_badsum
;
227 ip
= mtod(m
, struct ip
*);
228 timer
= igmp
->igmp_code
* PR_FASTHZ
/ IGMP_TIMER_SCALE
;
238 * In the IGMPv2 specification, there are 3 states and a flag.
240 * In Non-Member state, we simply don't have a membership record.
241 * In Delaying Member state, our timer is running (inm->inm_timer)
242 * In Idle Member state, our timer is not running (inm->inm_timer==0)
244 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
245 * we have heard a report from another member, or IGMP_IREPORTEDLAST
246 * if I sent the last report.
248 switch (igmp
->igmp_type
) {
250 case IGMP_MEMBERSHIP_QUERY
:
251 ++igmpstat
.igps_rcv_queries
;
253 if (ifp
->if_flags
& IFF_LOOPBACK
)
256 if (igmp
->igmp_code
== 0) {
258 * Old router. Remember that the querier on this
259 * interface is old, and set the timer to the
263 rti
->rti_type
= IGMP_V1_ROUTER
;
266 timer
= IGMP_MAX_HOST_REPORT_DELAY
* PR_FASTHZ
;
268 if (ip
->ip_dst
.s_addr
!= igmp_all_hosts_group
||
269 igmp
->igmp_group
.s_addr
!= 0) {
270 ++igmpstat
.igps_rcv_badqueries
;
276 * New router. Simply do the new validity check.
279 if (igmp
->igmp_group
.s_addr
!= 0 &&
280 !IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
281 ++igmpstat
.igps_rcv_badqueries
;
288 * - Start the timers in all of our membership records
289 * that the query applies to for the interface on
290 * which the query arrived excl. those that belong
291 * to the "all-hosts" group (224.0.0.1).
292 * - Restart any timer that is already running but has
293 * a value longer than the requested timeout.
294 * - Use the value specified in the query message as
295 * the maximum timeout.
297 lck_mtx_lock(rt_mtx
);
298 IN_FIRST_MULTI(step
, inm
);
299 while (inm
!= NULL
) {
300 if (inm
->inm_ifp
== ifp
&&
301 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
302 (igmp
->igmp_group
.s_addr
== 0 ||
303 igmp
->igmp_group
.s_addr
== inm
->inm_addr
.s_addr
)) {
304 if (inm
->inm_timer
== 0 ||
305 inm
->inm_timer
> timer
) {
307 IGMP_RANDOM_DELAY(timer
);
308 igmp_timers_are_running
= 1;
311 IN_NEXT_MULTI(step
, inm
);
313 lck_mtx_unlock(rt_mtx
);
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 ifnet_lock_shared(ifp
);
357 IN_LOOKUP_MULTI(igmp
->igmp_group
, ifp
, inm
);
358 ifnet_lock_done(ifp
);
362 ++igmpstat
.igps_rcv_ourreports
;
364 inm
->inm_state
= IGMP_OTHERMEMBER
;
371 * Pass all valid IGMP packets up to any process(es) listening
372 * on a raw IGMP socket.
374 rip_input(m
, iphlen
);
379 struct in_multi
*inm
;
382 if (inm
->inm_addr
.s_addr
== igmp_all_hosts_group
383 || inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) {
385 inm
->inm_state
= IGMP_OTHERMEMBER
;
387 inm
->inm_rti
= find_rti(inm
->inm_ifp
);
388 if (inm
->inm_rti
== NULL
) return ENOMEM
;
389 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
390 inm
->inm_timer
= IGMP_RANDOM_DELAY(
391 IGMP_MAX_HOST_REPORT_DELAY
*PR_FASTHZ
);
392 inm
->inm_state
= IGMP_IREPORTEDLAST
;
393 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 struct in_multi
*inm
;
413 struct in_multistep step
;
416 * Quick check to see if any work needs to be done, in order
417 * to minimize the overhead of fasttimo processing.
420 if (!igmp_timers_are_running
)
423 igmp_timers_are_running
= 0;
424 IN_FIRST_MULTI(step
, inm
);
425 while (inm
!= NULL
) {
426 if (inm
->inm_timer
== 0) {
428 } else if (--inm
->inm_timer
== 0) {
429 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
430 inm
->inm_state
= IGMP_IREPORTEDLAST
;
432 igmp_timers_are_running
= 1;
434 IN_NEXT_MULTI(step
, inm
);
441 struct router_info
*rti
= Head
;
444 printf("[igmp.c,_slowtimo] -- > entering \n");
447 if (rti
->rti_type
== IGMP_V1_ROUTER
) {
449 if (rti
->rti_time
>= IGMP_AGE_THRESHOLD
) {
450 rti
->rti_type
= IGMP_V2_ROUTER
;
456 printf("[igmp.c,_slowtimo] -- > exiting \n");
460 static struct route igmprt
;
463 igmp_sendpkt(inm
, type
, addr
)
464 struct in_multi
*inm
;
471 struct ip_moptions imo
;
473 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
477 m
->m_pkthdr
.rcvif
= loif
;
478 m
->m_pkthdr
.len
= sizeof(struct ip
) + IGMP_MINLEN
;
479 MH_ALIGN(m
, IGMP_MINLEN
+ sizeof(struct ip
));
480 m
->m_data
+= sizeof(struct ip
);
481 m
->m_len
= IGMP_MINLEN
;
482 m
->m_pkthdr
.csum_flags
= 0;
483 m
->m_pkthdr
.csum_data
= 0;
484 igmp
= mtod(m
, struct igmp
*);
485 igmp
->igmp_type
= type
;
487 igmp
->igmp_group
= inm
->inm_addr
;
488 igmp
->igmp_cksum
= 0;
489 igmp
->igmp_cksum
= in_cksum(m
, IGMP_MINLEN
);
491 m
->m_data
-= sizeof(struct ip
);
492 m
->m_len
+= sizeof(struct ip
);
493 ip
= mtod(m
, struct ip
*);
495 ip
->ip_len
= sizeof(struct ip
) + IGMP_MINLEN
;
497 ip
->ip_p
= IPPROTO_IGMP
;
498 ip
->ip_src
.s_addr
= INADDR_ANY
;
499 ip
->ip_dst
.s_addr
= addr
? addr
: igmp
->igmp_group
.s_addr
;
501 imo
.imo_multicast_ifp
= inm
->inm_ifp
;
502 imo
.imo_multicast_ttl
= 1;
503 imo
.imo_multicast_vif
= -1;
505 * Request loopback of the report if we are acting as a multicast
506 * router, so that the process-level routing demon can hear it.
508 imo
.imo_multicast_loop
= (ip_mrouter
!= NULL
);
512 * Do we have to worry about reentrancy here? Don't think so.
514 ip_output(m
, router_alert
, &igmprt
, 0, &imo
);
516 ++igmpstat
.igps_snd_reports
;