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 static MALLOC_DEFINE(M_IGMP
, "igmp", "igmp state");
76 static struct router_info
*
77 find_rti (struct ifnet
*ifp
);
79 static struct igmpstat igmpstat
;
81 SYSCTL_STRUCT(_net_inet_igmp
, IGMPCTL_STATS
, stats
, CTLFLAG_RW
,
82 &igmpstat
, igmpstat
, "IGMP statistics");
84 static int igmp_timers_are_running
;
85 static u_long igmp_all_hosts_group
;
86 static u_long igmp_all_rtrs_group
;
87 static struct mbuf
*router_alert
;
88 static struct router_info
*Head
;
90 static struct netmsg_base igmp_slowtimo_netmsg
;
91 static struct netmsg_base igmp_fasttimo_netmsg
;
93 static void igmp_sendpkt (struct in_multi
*, int, unsigned long);
94 static void igmp_slowtimo_dispatch(netmsg_t
);
95 static void igmp_fasttimo_dispatch(netmsg_t
);
103 * To avoid byte-swapping the same value over and over again.
105 igmp_all_hosts_group
= htonl(INADDR_ALLHOSTS_GROUP
);
106 igmp_all_rtrs_group
= htonl(INADDR_ALLRTRS_GROUP
);
108 igmp_timers_are_running
= 0;
111 * Construct a Router Alert option to use in outgoing packets
113 MGET(router_alert
, M_NOWAIT
, MT_DATA
);
114 ra
= mtod(router_alert
, struct ipoption
*);
115 ra
->ipopt_dst
.s_addr
= 0;
116 ra
->ipopt_list
[0] = IPOPT_RA
; /* Router Alert Option */
117 ra
->ipopt_list
[1] = 0x04; /* 4 bytes long */
118 ra
->ipopt_list
[2] = 0x00;
119 ra
->ipopt_list
[3] = 0x00;
120 router_alert
->m_len
= sizeof(ra
->ipopt_dst
) + ra
->ipopt_list
[1];
124 netmsg_init(&igmp_slowtimo_netmsg
, NULL
, &netisr_adone_rport
,
125 MSGF_PRIORITY
, igmp_slowtimo_dispatch
);
126 netmsg_init(&igmp_fasttimo_netmsg
, NULL
, &netisr_adone_rport
,
127 MSGF_PRIORITY
, igmp_fasttimo_dispatch
);
130 static struct router_info
*
131 find_rti(struct ifnet
*ifp
)
133 struct router_info
*rti
= Head
;
136 kprintf("[igmp.c, _find_rti] --> entering \n");
139 if (rti
->rti_ifp
== ifp
) {
141 kprintf("[igmp.c, _find_rti] --> found old entry \n");
147 rti
= kmalloc(sizeof *rti
, M_IGMP
, M_INTWAIT
);
149 rti
->rti_type
= IGMP_V2_ROUTER
;
151 rti
->rti_next
= Head
;
154 kprintf("[igmp.c, _find_rti] --> created an entry \n");
160 igmp_input(struct mbuf
**mp
, int *offp
, int proto
)
162 struct mbuf
*m
= *mp
;
167 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
169 struct in_multi
*inm
;
170 struct in_ifaddr
*ia
;
171 struct in_multistep step
;
172 struct router_info
*rti
;
173 int timer
; /** timer value in the igmp query header **/
178 ++igmpstat
.igps_rcv_total
;
180 ip
= mtod(m
, struct ip
*);
181 igmplen
= ip
->ip_len
;
186 if (igmplen
< IGMP_MINLEN
) {
187 ++igmpstat
.igps_rcv_tooshort
;
189 return(IPPROTO_DONE
);
191 minlen
= iphlen
+ IGMP_MINLEN
;
192 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
193 (m
= m_pullup(m
, minlen
)) == NULL
) {
194 ++igmpstat
.igps_rcv_tooshort
;
195 return(IPPROTO_DONE
);
203 igmp
= mtod(m
, struct igmp
*);
204 if (in_cksum(m
, igmplen
)) {
205 ++igmpstat
.igps_rcv_badsum
;
207 return(IPPROTO_DONE
);
212 ip
= mtod(m
, struct ip
*);
213 timer
= igmp
->igmp_code
* PR_FASTHZ
/ IGMP_TIMER_SCALE
;
219 * In the IGMPv2 specification, there are 3 states and a flag.
221 * In Non-Member state, we simply don't have a membership record.
222 * In Delaying Member state, our timer is running (inm->inm_timer)
223 * In Idle Member state, our timer is not running (inm->inm_timer==0)
225 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
226 * we have heard a report from another member, or IGMP_IREPORTEDLAST
227 * if I sent the last report.
229 switch (igmp
->igmp_type
) {
231 case IGMP_MEMBERSHIP_QUERY
:
232 ++igmpstat
.igps_rcv_queries
;
234 if (ifp
->if_flags
& IFF_LOOPBACK
)
237 if (igmp
->igmp_code
== 0) {
239 * Old router. Remember that the querier on this
240 * interface is old, and set the timer to the
244 rti
->rti_type
= IGMP_V1_ROUTER
;
247 timer
= IGMP_MAX_HOST_REPORT_DELAY
* PR_FASTHZ
;
249 if (ip
->ip_dst
.s_addr
!= igmp_all_hosts_group
||
250 igmp
->igmp_group
.s_addr
!= 0) {
251 ++igmpstat
.igps_rcv_badqueries
;
253 return(IPPROTO_DONE
);
257 * New router. Simply do the new validity check.
260 if (igmp
->igmp_group
.s_addr
!= 0 &&
261 !IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
262 ++igmpstat
.igps_rcv_badqueries
;
264 return(IPPROTO_DONE
);
269 * - Start the timers in all of our membership records
270 * that the query applies to for the interface on
271 * which the query arrived excl. those that belong
272 * to the "all-hosts" group (224.0.0.1).
273 * - Restart any timer that is already running but has
274 * a value longer than the requested timeout.
275 * - Use the value specified in the query message as
276 * the maximum timeout.
278 IN_FIRST_MULTI(step
, inm
);
279 while (inm
!= NULL
) {
280 if (inm
->inm_ifp
== ifp
&&
281 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
282 (igmp
->igmp_group
.s_addr
== 0 ||
283 igmp
->igmp_group
.s_addr
== inm
->inm_addr
.s_addr
)) {
284 if (inm
->inm_timer
== 0 ||
285 inm
->inm_timer
> timer
) {
287 IGMP_RANDOM_DELAY(timer
);
288 igmp_timers_are_running
= 1;
291 IN_NEXT_MULTI(step
, inm
);
296 case IGMP_V1_MEMBERSHIP_REPORT
:
297 case IGMP_V2_MEMBERSHIP_REPORT
:
299 * For fast leave to work, we have to know that we are the
300 * last person to send a report for this group. Reports
301 * can potentially get looped back if we are a multicast
302 * router, so discard reports sourced by me.
305 if (ia
&& ip
->ip_src
.s_addr
== IA_SIN(ia
)->sin_addr
.s_addr
)
308 ++igmpstat
.igps_rcv_reports
;
310 if (ifp
->if_flags
& IFF_LOOPBACK
)
313 if (!IN_MULTICAST(ntohl(igmp
->igmp_group
.s_addr
))) {
314 ++igmpstat
.igps_rcv_badreports
;
316 return(IPPROTO_DONE
);
320 * KLUDGE: if the IP source address of the report has an
321 * unspecified (i.e., zero) subnet number, as is allowed for
322 * a booting host, replace it with the correct subnet number
323 * so that a process-level multicast routing demon can
324 * determine which subnet it arrived from. This is necessary
325 * to compensate for the lack of any way for a process to
326 * determine the arrival interface of an incoming packet.
328 if ((ntohl(ip
->ip_src
.s_addr
) & IN_CLASSA_NET
) == 0)
329 if (ia
) ip
->ip_src
.s_addr
= htonl(ia
->ia_subnet
);
332 * If we belong to the group being reported, stop
333 * our timer for that group.
335 inm
= IN_LOOKUP_MULTI(&igmp
->igmp_group
, ifp
);
339 ++igmpstat
.igps_rcv_ourreports
;
341 inm
->inm_state
= IGMP_OTHERMEMBER
;
348 * Pass all valid IGMP packets up to any process(es) listening
349 * on a raw IGMP socket.
352 rip_input(mp
, offp
, proto
);
353 return(IPPROTO_DONE
);
357 igmp_joingroup(struct in_multi
*inm
)
360 if (inm
->inm_addr
.s_addr
== igmp_all_hosts_group
361 || inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) {
363 inm
->inm_state
= IGMP_OTHERMEMBER
;
365 inm
->inm_rti
= find_rti(inm
->inm_ifp
);
366 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
367 inm
->inm_timer
= IGMP_RANDOM_DELAY(
368 IGMP_MAX_HOST_REPORT_DELAY
*PR_FASTHZ
);
369 inm
->inm_state
= IGMP_IREPORTEDLAST
;
370 igmp_timers_are_running
= 1;
376 igmp_leavegroup(struct in_multi
*inm
)
378 if (inm
->inm_state
== IGMP_IREPORTEDLAST
&&
379 inm
->inm_addr
.s_addr
!= igmp_all_hosts_group
&&
380 !(inm
->inm_ifp
->if_flags
& IFF_LOOPBACK
) &&
381 inm
->inm_rti
->rti_type
!= IGMP_V1_ROUTER
)
382 igmp_sendpkt(inm
, IGMP_V2_LEAVE_GROUP
, igmp_all_rtrs_group
);
386 igmp_fasttimo_ipi(void *arg __unused
)
388 struct lwkt_msg
*msg
= &igmp_fasttimo_netmsg
.lmsg
;
391 if (msg
->ms_flags
& MSGF_DONE
)
392 lwkt_sendmsg_oncpu(netisr_cpuport(0), msg
);
399 lwkt_send_ipiq_bycpu(0, igmp_fasttimo_ipi
, NULL
);
403 igmp_fasttimo_dispatch(netmsg_t nmsg
)
405 struct in_multi
*inm
;
406 struct in_multistep step
;
409 lwkt_replymsg(&nmsg
->lmsg
, 0); /* reply ASAP */
413 * Quick check to see if any work needs to be done, in order
414 * to minimize the overhead of fasttimo processing.
417 if (!igmp_timers_are_running
)
420 igmp_timers_are_running
= 0;
421 IN_FIRST_MULTI(step
, inm
);
422 while (inm
!= NULL
) {
423 if (inm
->inm_timer
== 0) {
425 } else if (--inm
->inm_timer
== 0) {
426 igmp_sendpkt(inm
, inm
->inm_rti
->rti_type
, 0);
427 inm
->inm_state
= IGMP_IREPORTEDLAST
;
429 igmp_timers_are_running
= 1;
431 IN_NEXT_MULTI(step
, inm
);
436 igmp_slowtimo_ipi(void *arg __unused
)
438 struct lwkt_msg
*msg
= &igmp_slowtimo_netmsg
.lmsg
;
441 if (msg
->ms_flags
& MSGF_DONE
)
442 lwkt_sendmsg_oncpu(netisr_cpuport(0), msg
);
449 lwkt_send_ipiq_bycpu(0, igmp_slowtimo_ipi
, NULL
);
453 igmp_slowtimo_dispatch(netmsg_t nmsg
)
455 struct router_info
*rti
= Head
;
458 lwkt_replymsg(&nmsg
->lmsg
, 0); /* reply ASAP */
462 kprintf("[igmp.c,_slowtimo] -- > entering \n");
465 if (rti
->rti_type
== IGMP_V1_ROUTER
) {
467 if (rti
->rti_time
>= IGMP_AGE_THRESHOLD
) {
468 rti
->rti_type
= IGMP_V2_ROUTER
;
474 kprintf("[igmp.c,_slowtimo] -- > exiting \n");
478 static struct route igmprt
;
481 igmp_sendpkt(struct in_multi
*inm
, int type
, unsigned long addr
)
486 struct ip_moptions imo
;
488 MGETHDR(m
, M_NOWAIT
, MT_HEADER
);
492 m
->m_pkthdr
.rcvif
= loif
;
493 m
->m_pkthdr
.len
= sizeof(struct ip
) + IGMP_MINLEN
;
494 MH_ALIGN(m
, IGMP_MINLEN
+ sizeof(struct ip
));
495 m
->m_data
+= sizeof(struct ip
);
496 m
->m_len
= IGMP_MINLEN
;
497 igmp
= mtod(m
, struct igmp
*);
498 igmp
->igmp_type
= type
;
500 igmp
->igmp_group
= inm
->inm_addr
;
501 igmp
->igmp_cksum
= 0;
502 igmp
->igmp_cksum
= in_cksum(m
, IGMP_MINLEN
);
504 m
->m_data
-= sizeof(struct ip
);
505 m
->m_len
+= sizeof(struct ip
);
506 ip
= mtod(m
, struct ip
*);
508 ip
->ip_len
= sizeof(struct ip
) + IGMP_MINLEN
;
510 ip
->ip_p
= IPPROTO_IGMP
;
511 ip
->ip_src
.s_addr
= INADDR_ANY
;
512 ip
->ip_dst
.s_addr
= addr
? addr
: igmp
->igmp_group
.s_addr
;
514 imo
.imo_multicast_ifp
= inm
->inm_ifp
;
515 imo
.imo_multicast_ttl
= 1;
516 imo
.imo_multicast_vif
= -1;
518 * Request loopback of the report if we are acting as a multicast
519 * router, so that the process-level routing demon can hear it.
521 imo
.imo_multicast_loop
= (ip_mrouter
!= NULL
);
525 * Do we have to worry about reentrancy here? Don't think so.
527 ip_output(m
, router_alert
, &igmprt
, 0, &imo
, NULL
);
529 ++igmpstat
.igps_snd_reports
;