2 * Copyright (c) 1988 Stephen Deering.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
34 * $FreeBSD: src/sys/netinet/igmp.c,v 1.29.2.2 2003/01/23 21:06:44 sam Exp $
38 * Internet Group Management Protocol (IGMP) routines.
40 * Written by Steve Deering, Stanford, May 1988.
41 * Modified by Rosen Sharma, Stanford, Aug 1994.
42 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
43 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
45 * MULTICAST Revision: 3.5.1.4
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
52 #include <sys/socket.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
56 #include <sys/in_cksum.h>
57 #include <sys/thread2.h>
59 #include <machine/stdarg.h>
62 #include <net/route.h>
63 #include <net/netmsg2.h>
64 #include <net/netisr2.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/igmp.h>
72 #include <netinet/igmp_var.h>
74 #define IGMP_FASTTIMO (hz / PR_FASTHZ)
75 #define IGMP_SLOWTIMO (hz / PR_SLOWHZ)
77 static MALLOC_DEFINE(M_IGMP
, "igmp", "igmp state");
79 static struct router_info
*
80 find_rti (struct ifnet
*ifp
);
82 static struct igmpstat igmpstat
;
84 SYSCTL_STRUCT(_net_inet_igmp
, IGMPCTL_STATS
, stats
, CTLFLAG_RW
,
85 &igmpstat
, igmpstat
, "IGMP statistics");
87 static int igmp_timers_are_running
;
88 static u_long igmp_all_hosts_group
;
89 static u_long igmp_all_rtrs_group
;
90 static struct mbuf
*router_alert
;
91 static struct router_info
*Head
;
93 static void igmp_sendpkt (struct in_multi
*, int, unsigned long);
94 static void igmp_fasttimo_dispatch(netmsg_t
);
95 static void igmp_fasttimo(void *);
96 static void igmp_slowtimo_dispatch(netmsg_t
);
97 static void igmp_slowtimo(void *);
99 static struct netmsg_base igmp_slowtimo_netmsg
;
100 static struct callout igmp_slowtimo_ch
;
101 static struct netmsg_base igmp_fasttimo_netmsg
;
102 static struct callout igmp_fasttimo_ch
;
110 * To avoid byte-swapping the same value over and over again.
112 igmp_all_hosts_group
= htonl(INADDR_ALLHOSTS_GROUP
);
113 igmp_all_rtrs_group
= htonl(INADDR_ALLRTRS_GROUP
);
115 igmp_timers_are_running
= 0;
118 * Construct a Router Alert option to use in outgoing packets
120 MGET(router_alert
, M_NOWAIT
, MT_DATA
);
121 ra
= mtod(router_alert
, struct ipoption
*);
122 ra
->ipopt_dst
.s_addr
= 0;
123 ra
->ipopt_list
[0] = IPOPT_RA
; /* Router Alert Option */
124 ra
->ipopt_list
[1] = 0x04; /* 4 bytes long */
125 ra
->ipopt_list
[2] = 0x00;
126 ra
->ipopt_list
[3] = 0x00;
127 router_alert
->m_len
= sizeof(ra
->ipopt_dst
) + ra
->ipopt_list
[1];
131 callout_init_mp(&igmp_slowtimo_ch
);
132 netmsg_init(&igmp_slowtimo_netmsg
, NULL
, &netisr_adone_rport
,
133 MSGF_PRIORITY
, igmp_slowtimo_dispatch
);
135 callout_init_mp(&igmp_fasttimo_ch
);
136 netmsg_init(&igmp_fasttimo_netmsg
, NULL
, &netisr_adone_rport
,
137 MSGF_PRIORITY
, igmp_fasttimo_dispatch
);
139 callout_reset_bycpu(&igmp_slowtimo_ch
, IGMP_SLOWTIMO
,
140 igmp_slowtimo
, NULL
, 0);
141 callout_reset_bycpu(&igmp_fasttimo_ch
, IGMP_FASTTIMO
,
142 igmp_fasttimo
, NULL
, 0);
145 static struct router_info
*
146 find_rti(struct ifnet
*ifp
)
148 struct router_info
*rti
= Head
;
151 kprintf("[igmp.c, _find_rti] --> entering \n");
154 if (rti
->rti_ifp
== ifp
) {
156 kprintf("[igmp.c, _find_rti] --> found old entry \n");
162 rti
= kmalloc(sizeof *rti
, M_IGMP
, M_INTWAIT
);
164 rti
->rti_type
= IGMP_V2_ROUTER
;
166 rti
->rti_next
= Head
;
169 kprintf("[igmp.c, _find_rti] --> created an entry \n");
175 igmp_input(struct mbuf
**mp
, int *offp
, int proto
)
177 struct mbuf
*m
= *mp
;
182 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
184 struct in_multi
*inm
;
185 struct in_ifaddr
*ia
;
186 struct in_multistep step
;
187 struct router_info
*rti
;
188 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
;
204 return(IPPROTO_DONE
);
206 minlen
= iphlen
+ IGMP_MINLEN
;
207 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
208 (m
= m_pullup(m
, minlen
)) == NULL
) {
209 ++igmpstat
.igps_rcv_tooshort
;
210 return(IPPROTO_DONE
);
218 igmp
= mtod(m
, struct igmp
*);
219 if (in_cksum(m
, igmplen
)) {
220 ++igmpstat
.igps_rcv_badsum
;
222 return(IPPROTO_DONE
);
227 ip
= mtod(m
, struct ip
*);
228 timer
= igmp
->igmp_code
* PR_FASTHZ
/ IGMP_TIMER_SCALE
;
234 * In the IGMPv2 specification, there are 3 states and a flag.
236 * In Non-Member state, we simply don't have a membership record.
237 * In Delaying Member state, our timer is running (inm->inm_timer)
238 * In Idle Member state, our timer is not running (inm->inm_timer==0)
240 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
241 * we have heard a report from another member, or IGMP_IREPORTEDLAST
242 * if I sent the last report.
244 switch (igmp
->igmp_type
) {
246 case IGMP_MEMBERSHIP_QUERY
:
247 ++igmpstat
.igps_rcv_queries
;
249 if (ifp
->if_flags
& IFF_LOOPBACK
)
252 if (igmp
->igmp_code
== 0) {
254 * Old router. Remember that the querier on this
255 * interface is old, and set the timer to the
259 rti
->rti_type
= IGMP_V1_ROUTER
;
262 timer
= IGMP_MAX_HOST_REPORT_DELAY
* PR_FASTHZ
;
264 if (ip
->ip_dst
.s_addr
!= igmp_all_hosts_group
||
265 igmp
->igmp_group
.s_addr
!= 0) {
266 ++igmpstat
.igps_rcv_badqueries
;
268 return(IPPROTO_DONE
);
272 * New router. Simply do the new validity check.
275 if (igmp
->igmp_group
.s_addr
!= 0 &&
276 !IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
277 ++igmpstat
.igps_rcv_badqueries
;
279 return(IPPROTO_DONE
);
284 * - Start the timers in all of our membership records
285 * that the query applies to for the interface on
286 * which the query arrived excl. those that belong
287 * to the "all-hosts" group (224.0.0.1).
288 * - Restart any timer that is already running but has
289 * a value longer than the requested timeout.
290 * - Use the value specified in the query message as
291 * the maximum timeout.
293 IN_FIRST_MULTI(step
, inm
);
294 while (inm
!= NULL
) {
295 if (inm
->inm_ifp
== ifp
&&
296 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
297 (igmp
->igmp_group
.s_addr
== 0 ||
298 igmp
->igmp_group
.s_addr
== inm
->inm_addr
.s_addr
)) {
299 if (inm
->inm_timer
== 0 ||
300 inm
->inm_timer
> timer
) {
302 IGMP_RANDOM_DELAY(timer
);
303 igmp_timers_are_running
= 1;
306 IN_NEXT_MULTI(step
, inm
);
311 case IGMP_V1_MEMBERSHIP_REPORT
:
312 case IGMP_V2_MEMBERSHIP_REPORT
:
314 * For fast leave to work, we have to know that we are the
315 * last person to send a report for this group. Reports
316 * can potentially get looped back if we are a multicast
317 * router, so discard reports sourced by me.
320 if (ia
&& ip
->ip_src
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
)
323 ++igmpstat
.igps_rcv_reports
;
325 if (ifp
->if_flags
& IFF_LOOPBACK
)
328 if (!IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
329 ++igmpstat
.igps_rcv_badreports
;
331 return(IPPROTO_DONE
);
335 * KLUDGE: if the IP source address of the report has an
336 * unspecified (i.e., zero) subnet number, as is allowed for
337 * a booting host, replace it with the correct subnet number
338 * so that a process-level multicast routing demon can
339 * determine which subnet it arrived from. This is necessary
340 * to compensate for the lack of any way for a process to
341 * determine the arrival interface of an incoming packet.
343 if ((ntohl(ip
->ip_src
.s_addr
) & IN_CLASSA_NET
) == 0)
344 if (ia
) ip
->ip_src
.s_addr
= htonl(ia
->ia_subnet
);
347 * If we belong to the group being reported, stop
348 * our timer for that group.
350 inm
= IN_LOOKUP_MULTI(&igmp
->igmp_group
, ifp
);
354 ++igmpstat
.igps_rcv_ourreports
;
356 inm
->inm_state
= IGMP_OTHERMEMBER
;
363 * Pass all valid IGMP packets up to any process(es) listening
364 * on a raw IGMP socket.
367 rip_input(mp
, offp
, proto
);
368 return(IPPROTO_DONE
);
372 igmp_joingroup(struct in_multi
*inm
)
375 if (inm
->inm_addr
.s_addr
== igmp_all_hosts_group
376 || inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) {
378 inm
->inm_state
= IGMP_OTHERMEMBER
;
380 inm
->inm_rti
= find_rti(inm
->inm_ifp
);
381 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
382 inm
->inm_timer
= IGMP_RANDOM_DELAY(
383 IGMP_MAX_HOST_REPORT_DELAY
*PR_FASTHZ
);
384 inm
->inm_state
= IGMP_IREPORTEDLAST
;
385 igmp_timers_are_running
= 1;
391 igmp_leavegroup(struct in_multi
*inm
)
393 if (inm
->inm_state
== IGMP_IREPORTEDLAST
&&
394 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
395 !(inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) &&
396 inm
->inm_rti
->rti_type
!= IGMP_V1_ROUTER
)
397 igmp_sendpkt(inm
, IGMP_V2_LEAVE_GROUP
, igmp_all_rtrs_group
);
401 igmp_fasttimo(void *dummy __unused
)
403 struct netmsg_base
*msg
= &igmp_fasttimo_netmsg
;
405 KKASSERT(mycpuid
== 0);
408 if (msg
->lmsg
.ms_flags
& MSGF_DONE
)
409 netisr_sendmsg_oncpu(msg
);
414 igmp_fasttimo_dispatch(netmsg_t nmsg
)
416 struct in_multi
*inm
;
417 struct in_multistep step
;
422 netisr_replymsg(&nmsg
->base
, 0); /* reply ASAP */
426 * Quick check to see if any work needs to be done, in order
427 * to minimize the overhead of fasttimo processing.
430 if (!igmp_timers_are_running
)
433 igmp_timers_are_running
= 0;
434 IN_FIRST_MULTI(step
, inm
);
435 while (inm
!= NULL
) {
436 if (inm
->inm_timer
== 0) {
438 } else if (--inm
->inm_timer
== 0) {
439 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
440 inm
->inm_state
= IGMP_IREPORTEDLAST
;
442 igmp_timers_are_running
= 1;
444 IN_NEXT_MULTI(step
, inm
);
447 callout_reset(&igmp_fasttimo_ch
, IGMP_FASTTIMO
, igmp_fasttimo
, NULL
);
451 igmp_slowtimo(void *dummy __unused
)
453 struct netmsg_base
*msg
= &igmp_slowtimo_netmsg
;
455 KKASSERT(mycpuid
== 0);
458 if (msg
->lmsg
.ms_flags
& MSGF_DONE
)
459 netisr_sendmsg_oncpu(msg
);
464 igmp_slowtimo_dispatch(netmsg_t nmsg
)
466 struct router_info
*rti
= Head
;
471 netisr_replymsg(&nmsg
->base
, 0); /* reply ASAP */
475 kprintf("[igmp.c,_slowtimo] -- > entering \n");
478 if (rti
->rti_type
== IGMP_V1_ROUTER
) {
480 if (rti
->rti_time
>= IGMP_AGE_THRESHOLD
) {
481 rti
->rti_type
= IGMP_V2_ROUTER
;
487 kprintf("[igmp.c,_slowtimo] -- > exiting \n");
489 callout_reset(&igmp_slowtimo_ch
, IGMP_SLOWTIMO
, igmp_slowtimo
, NULL
);
492 static struct route igmprt
;
495 igmp_sendpkt(struct in_multi
*inm
, int type
, unsigned long addr
)
500 struct ip_moptions imo
;
502 MGETHDR(m
, M_NOWAIT
, MT_HEADER
);
506 m
->m_pkthdr
.rcvif
= loif
;
507 m
->m_pkthdr
.len
= sizeof(struct ip
) + IGMP_MINLEN
;
508 MH_ALIGN(m
, IGMP_MINLEN
+ sizeof(struct ip
));
509 m
->m_data
+= sizeof(struct ip
);
510 m
->m_len
= IGMP_MINLEN
;
511 igmp
= mtod(m
, struct igmp
*);
512 igmp
->igmp_type
= type
;
514 igmp
->igmp_group
= inm
->inm_addr
;
515 igmp
->igmp_cksum
= 0;
516 igmp
->igmp_cksum
= in_cksum(m
, IGMP_MINLEN
);
518 m
->m_data
-= sizeof(struct ip
);
519 m
->m_len
+= sizeof(struct ip
);
520 ip
= mtod(m
, struct ip
*);
522 ip
->ip_len
= sizeof(struct ip
) + IGMP_MINLEN
;
524 ip
->ip_p
= IPPROTO_IGMP
;
525 ip
->ip_src
.s_addr
= INADDR_ANY
;
526 ip
->ip_dst
.s_addr
= addr
? addr
: igmp
->igmp_group
.s_addr
;
528 imo
.imo_multicast_ifp
= inm
->inm_ifp
;
529 imo
.imo_multicast_ttl
= 1;
530 imo
.imo_multicast_vif
= -1;
532 * Request loopback of the report if we are acting as a multicast
533 * router, so that the process-level routing demon can hear it.
535 imo
.imo_multicast_loop
= (ip_mrouter
!= NULL
);
539 * Do we have to worry about reentrancy here? Don't think so.
541 ip_output(m
, router_alert
, &igmprt
, 0, &imo
, NULL
);
543 ++igmpstat
.igps_snd_reports
;