2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <linux/uaccess.h>
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/jiffies.h>
79 #include <linux/string.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
83 #include <linux/inet.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/if_arp.h>
89 #include <linux/rtnetlink.h>
90 #include <linux/times.h>
91 #include <linux/pkt_sched.h>
93 #include <net/net_namespace.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
99 #include <net/checksum.h>
100 #include <net/inet_common.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
110 #ifdef CONFIG_IP_MULTICAST
111 /* Parameter names and values are taken from igmp-v2-06 draft */
113 #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
114 #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
115 #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
116 #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
117 #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
118 #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
121 #define IGMP_INITIAL_REPORT_DELAY (1)
123 /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
124 * IGMP specs require to report membership immediately after
125 * joining a group, but we delay the first report by a
126 * small interval. It seems more natural and still does not
127 * contradict to specs provided this delay is small enough.
130 #define IGMP_V1_SEEN(in_dev) \
131 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
132 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
133 ((in_dev)->mr_v1_seen && \
134 time_before(jiffies, (in_dev)->mr_v1_seen)))
135 #define IGMP_V2_SEEN(in_dev) \
136 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
137 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
141 static int unsolicited_report_interval(struct in_device
*in_dev
)
143 int interval_ms
, interval_jiffies
;
145 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
146 interval_ms
= IN_DEV_CONF_GET(
148 IGMPV2_UNSOLICITED_REPORT_INTERVAL
);
150 interval_ms
= IN_DEV_CONF_GET(
152 IGMPV3_UNSOLICITED_REPORT_INTERVAL
);
154 interval_jiffies
= msecs_to_jiffies(interval_ms
);
156 /* _timer functions can't handle a delay of 0 jiffies so ensure
157 * we always return a positive value.
159 if (interval_jiffies
<= 0)
160 interval_jiffies
= 1;
161 return interval_jiffies
;
164 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
165 static void igmpv3_del_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
166 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
167 static int sf_setstate(struct ip_mc_list
*pmc
);
168 static void sf_markstate(struct ip_mc_list
*pmc
);
170 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
171 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
172 int sfcount
, __be32
*psfsrc
, int delta
);
174 static void ip_ma_put(struct ip_mc_list
*im
)
176 if (refcount_dec_and_test(&im
->refcnt
)) {
177 in_dev_put(im
->interface
);
182 #define for_each_pmc_rcu(in_dev, pmc) \
183 for (pmc = rcu_dereference(in_dev->mc_list); \
185 pmc = rcu_dereference(pmc->next_rcu))
187 #define for_each_pmc_rtnl(in_dev, pmc) \
188 for (pmc = rtnl_dereference(in_dev->mc_list); \
190 pmc = rtnl_dereference(pmc->next_rcu))
192 #ifdef CONFIG_IP_MULTICAST
198 static void igmp_stop_timer(struct ip_mc_list
*im
)
200 spin_lock_bh(&im
->lock
);
201 if (del_timer(&im
->timer
))
202 refcount_dec(&im
->refcnt
);
205 im
->unsolicit_count
= 0;
206 spin_unlock_bh(&im
->lock
);
209 /* It must be called with locked im->lock */
210 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
212 int tv
= prandom_u32() % max_delay
;
215 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
216 refcount_inc(&im
->refcnt
);
219 static void igmp_gq_start_timer(struct in_device
*in_dev
)
221 int tv
= prandom_u32() % in_dev
->mr_maxdelay
;
222 unsigned long exp
= jiffies
+ tv
+ 2;
224 if (in_dev
->mr_gq_running
&&
225 time_after_eq(exp
, (in_dev
->mr_gq_timer
).expires
))
228 in_dev
->mr_gq_running
= 1;
229 if (!mod_timer(&in_dev
->mr_gq_timer
, exp
))
233 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
235 int tv
= prandom_u32() % delay
;
237 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
241 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
243 spin_lock_bh(&im
->lock
);
244 im
->unsolicit_count
= 0;
245 if (del_timer(&im
->timer
)) {
246 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
247 add_timer(&im
->timer
);
249 spin_unlock_bh(&im
->lock
);
252 refcount_dec(&im
->refcnt
);
254 igmp_start_timer(im
, max_delay
);
255 spin_unlock_bh(&im
->lock
);
260 * Send an IGMP report.
263 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
266 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
267 int gdeleted
, int sdeleted
)
270 case IGMPV3_MODE_IS_INCLUDE
:
271 case IGMPV3_MODE_IS_EXCLUDE
:
272 if (gdeleted
|| sdeleted
)
274 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
275 if (pmc
->sfmode
== MCAST_INCLUDE
)
277 /* don't include if this source is excluded
280 if (psf
->sf_count
[MCAST_INCLUDE
])
281 return type
== IGMPV3_MODE_IS_INCLUDE
;
282 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
283 psf
->sf_count
[MCAST_EXCLUDE
];
286 case IGMPV3_CHANGE_TO_INCLUDE
:
287 if (gdeleted
|| sdeleted
)
289 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
290 case IGMPV3_CHANGE_TO_EXCLUDE
:
291 if (gdeleted
|| sdeleted
)
293 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
294 psf
->sf_count
[MCAST_INCLUDE
])
296 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
297 psf
->sf_count
[MCAST_EXCLUDE
];
298 case IGMPV3_ALLOW_NEW_SOURCES
:
299 if (gdeleted
|| !psf
->sf_crcount
)
301 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
302 case IGMPV3_BLOCK_OLD_SOURCES
:
303 if (pmc
->sfmode
== MCAST_INCLUDE
)
304 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
305 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
311 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
313 struct ip_sf_list
*psf
;
316 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
317 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
324 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, unsigned int mtu
)
329 struct igmpv3_report
*pig
;
330 struct net
*net
= dev_net(dev
);
332 int hlen
= LL_RESERVED_SPACE(dev
);
333 int tlen
= dev
->needed_tailroom
;
334 unsigned int size
= mtu
;
337 skb
= alloc_skb(size
+ hlen
+ tlen
,
338 GFP_ATOMIC
| __GFP_NOWARN
);
345 skb
->priority
= TC_PRIO_CONTROL
;
347 rt
= ip_route_output_ports(net
, &fl4
, NULL
, IGMPV3_ALL_MCR
, 0,
349 IPPROTO_IGMP
, 0, dev
->ifindex
);
355 skb_dst_set(skb
, &rt
->dst
);
358 skb_reserve(skb
, hlen
);
359 skb_tailroom_reserve(skb
, mtu
, tlen
);
361 skb_reset_network_header(skb
);
363 skb_put(skb
, sizeof(struct iphdr
) + 4);
366 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
368 pip
->frag_off
= htons(IP_DF
);
370 pip
->daddr
= fl4
.daddr
;
371 pip
->saddr
= fl4
.saddr
;
372 pip
->protocol
= IPPROTO_IGMP
;
373 pip
->tot_len
= 0; /* filled in later */
374 ip_select_ident(net
, skb
, NULL
);
375 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
376 ((u8
*)&pip
[1])[1] = 4;
377 ((u8
*)&pip
[1])[2] = 0;
378 ((u8
*)&pip
[1])[3] = 0;
380 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
381 skb_put(skb
, sizeof(*pig
));
382 pig
= igmpv3_report_hdr(skb
);
383 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
391 static int igmpv3_sendpack(struct sk_buff
*skb
)
393 struct igmphdr
*pig
= igmp_hdr(skb
);
394 const int igmplen
= skb_tail_pointer(skb
) - skb_transport_header(skb
);
396 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
398 return ip_local_out(dev_net(skb_dst(skb
)->dev
), skb
->sk
, skb
);
401 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
403 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
406 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
407 int type
, struct igmpv3_grec
**ppgr
)
409 struct net_device
*dev
= pmc
->interface
->dev
;
410 struct igmpv3_report
*pih
;
411 struct igmpv3_grec
*pgr
;
414 skb
= igmpv3_newpack(dev
, dev
->mtu
);
417 pgr
= skb_put(skb
, sizeof(struct igmpv3_grec
));
418 pgr
->grec_type
= type
;
419 pgr
->grec_auxwords
= 0;
421 pgr
->grec_mca
= pmc
->multiaddr
;
422 pih
= igmpv3_report_hdr(skb
);
423 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
428 #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
430 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
431 int type
, int gdeleted
, int sdeleted
)
433 struct net_device
*dev
= pmc
->interface
->dev
;
434 struct net
*net
= dev_net(dev
);
435 struct igmpv3_report
*pih
;
436 struct igmpv3_grec
*pgr
= NULL
;
437 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
438 int scount
, stotal
, first
, isquery
, truncate
;
440 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
442 if (ipv4_is_local_multicast(pmc
->multiaddr
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
445 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
446 type
== IGMPV3_MODE_IS_EXCLUDE
;
447 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
448 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
452 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
457 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
459 /* EX and TO_EX get a fresh packet, if needed */
461 if (pih
&& pih
->ngrec
&&
462 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
464 igmpv3_sendpack(skb
);
465 skb
= igmpv3_newpack(dev
, dev
->mtu
);
470 for (psf
= *psf_list
; psf
; psf
= psf_next
) {
473 psf_next
= psf
->sf_next
;
475 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
480 /* Based on RFC3376 5.1. Should not send source-list change
481 * records when there is a filter mode change.
483 if (((gdeleted
&& pmc
->sfmode
== MCAST_EXCLUDE
) ||
484 (!gdeleted
&& pmc
->crcount
)) &&
485 (type
== IGMPV3_ALLOW_NEW_SOURCES
||
486 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
)
487 goto decrease_sf_crcount
;
489 /* clear marks on query responses */
493 if (AVAILABLE(skb
) < sizeof(__be32
) +
494 first
*sizeof(struct igmpv3_grec
)) {
495 if (truncate
&& !first
)
496 break; /* truncate these */
498 pgr
->grec_nsrcs
= htons(scount
);
500 igmpv3_sendpack(skb
);
501 skb
= igmpv3_newpack(dev
, dev
->mtu
);
506 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
511 psrc
= skb_put(skb
, sizeof(__be32
));
512 *psrc
= psf
->sf_inaddr
;
514 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
515 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
518 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
520 psf_prev
->sf_next
= psf
->sf_next
;
522 *psf_list
= psf
->sf_next
;
532 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
533 type
== IGMPV3_BLOCK_OLD_SOURCES
)
535 if (pmc
->crcount
|| isquery
) {
536 /* make sure we have room for group header */
537 if (skb
&& AVAILABLE(skb
) < sizeof(struct igmpv3_grec
)) {
538 igmpv3_sendpack(skb
);
539 skb
= NULL
; /* add_grhead will get a new one */
541 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
545 pgr
->grec_nsrcs
= htons(scount
);
548 pmc
->gsquery
= 0; /* clear query state on report */
552 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
554 struct sk_buff
*skb
= NULL
;
555 struct net
*net
= dev_net(in_dev
->dev
);
560 for_each_pmc_rcu(in_dev
, pmc
) {
561 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
563 if (ipv4_is_local_multicast(pmc
->multiaddr
) &&
564 !net
->ipv4
.sysctl_igmp_llm_reports
)
566 spin_lock_bh(&pmc
->lock
);
567 if (pmc
->sfcount
[MCAST_EXCLUDE
])
568 type
= IGMPV3_MODE_IS_EXCLUDE
;
570 type
= IGMPV3_MODE_IS_INCLUDE
;
571 skb
= add_grec(skb
, pmc
, type
, 0, 0);
572 spin_unlock_bh(&pmc
->lock
);
576 spin_lock_bh(&pmc
->lock
);
577 if (pmc
->sfcount
[MCAST_EXCLUDE
])
578 type
= IGMPV3_MODE_IS_EXCLUDE
;
580 type
= IGMPV3_MODE_IS_INCLUDE
;
581 skb
= add_grec(skb
, pmc
, type
, 0, 0);
582 spin_unlock_bh(&pmc
->lock
);
586 return igmpv3_sendpack(skb
);
590 * remove zero-count source records from a source filter list
592 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
594 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
597 for (psf
= *ppsf
; psf
; psf
= psf_next
) {
598 psf_next
= psf
->sf_next
;
599 if (psf
->sf_crcount
== 0) {
601 psf_prev
->sf_next
= psf
->sf_next
;
603 *ppsf
= psf
->sf_next
;
610 static void igmpv3_send_cr(struct in_device
*in_dev
)
612 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
613 struct sk_buff
*skb
= NULL
;
617 spin_lock_bh(&in_dev
->mc_tomb_lock
);
621 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc_next
) {
622 pmc_next
= pmc
->next
;
623 if (pmc
->sfmode
== MCAST_INCLUDE
) {
624 type
= IGMPV3_BLOCK_OLD_SOURCES
;
625 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
626 skb
= add_grec(skb
, pmc
, type
, 1, 0);
627 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
630 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
631 type
= IGMPV3_CHANGE_TO_INCLUDE
;
632 skb
= add_grec(skb
, pmc
, type
, 1, 0);
635 if (pmc
->crcount
== 0) {
636 igmpv3_clear_zeros(&pmc
->tomb
);
637 igmpv3_clear_zeros(&pmc
->sources
);
640 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
642 pmc_prev
->next
= pmc_next
;
644 in_dev
->mc_tomb
= pmc_next
;
645 in_dev_put(pmc
->interface
);
650 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
653 for_each_pmc_rcu(in_dev
, pmc
) {
654 spin_lock_bh(&pmc
->lock
);
655 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
656 type
= IGMPV3_BLOCK_OLD_SOURCES
;
657 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
659 type
= IGMPV3_ALLOW_NEW_SOURCES
;
660 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
662 skb
= add_grec(skb
, pmc
, type
, 0, 0);
663 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
665 /* filter mode changes */
667 if (pmc
->sfmode
== MCAST_EXCLUDE
)
668 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
670 type
= IGMPV3_CHANGE_TO_INCLUDE
;
671 skb
= add_grec(skb
, pmc
, type
, 0, 0);
674 spin_unlock_bh(&pmc
->lock
);
680 (void) igmpv3_sendpack(skb
);
683 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
690 struct net_device
*dev
= in_dev
->dev
;
691 struct net
*net
= dev_net(dev
);
692 __be32 group
= pmc
? pmc
->multiaddr
: 0;
697 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
698 return igmpv3_send_report(in_dev
, pmc
);
700 if (ipv4_is_local_multicast(group
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
703 if (type
== IGMP_HOST_LEAVE_MESSAGE
)
704 dst
= IGMP_ALL_ROUTER
;
708 rt
= ip_route_output_ports(net
, &fl4
, NULL
, dst
, 0,
710 IPPROTO_IGMP
, 0, dev
->ifindex
);
714 hlen
= LL_RESERVED_SPACE(dev
);
715 tlen
= dev
->needed_tailroom
;
716 skb
= alloc_skb(IGMP_SIZE
+ hlen
+ tlen
, GFP_ATOMIC
);
721 skb
->priority
= TC_PRIO_CONTROL
;
723 skb_dst_set(skb
, &rt
->dst
);
725 skb_reserve(skb
, hlen
);
727 skb_reset_network_header(skb
);
729 skb_put(skb
, sizeof(struct iphdr
) + 4);
732 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
734 iph
->frag_off
= htons(IP_DF
);
737 iph
->saddr
= fl4
.saddr
;
738 iph
->protocol
= IPPROTO_IGMP
;
739 ip_select_ident(net
, skb
, NULL
);
740 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
741 ((u8
*)&iph
[1])[1] = 4;
742 ((u8
*)&iph
[1])[2] = 0;
743 ((u8
*)&iph
[1])[3] = 0;
745 ih
= skb_put(skb
, sizeof(struct igmphdr
));
750 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
752 return ip_local_out(net
, skb
->sk
, skb
);
755 static void igmp_gq_timer_expire(unsigned long data
)
757 struct in_device
*in_dev
= (struct in_device
*)data
;
759 in_dev
->mr_gq_running
= 0;
760 igmpv3_send_report(in_dev
, NULL
);
764 static void igmp_ifc_timer_expire(unsigned long data
)
766 struct in_device
*in_dev
= (struct in_device
*)data
;
768 igmpv3_send_cr(in_dev
);
769 if (in_dev
->mr_ifc_count
) {
770 in_dev
->mr_ifc_count
--;
771 igmp_ifc_start_timer(in_dev
,
772 unsolicited_report_interval(in_dev
));
777 static void igmp_ifc_event(struct in_device
*in_dev
)
779 struct net
*net
= dev_net(in_dev
->dev
);
780 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
782 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
783 igmp_ifc_start_timer(in_dev
, 1);
787 static void igmp_timer_expire(unsigned long data
)
789 struct ip_mc_list
*im
= (struct ip_mc_list
*)data
;
790 struct in_device
*in_dev
= im
->interface
;
792 spin_lock(&im
->lock
);
795 if (im
->unsolicit_count
) {
796 im
->unsolicit_count
--;
797 igmp_start_timer(im
, unsolicited_report_interval(in_dev
));
800 spin_unlock(&im
->lock
);
802 if (IGMP_V1_SEEN(in_dev
))
803 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
804 else if (IGMP_V2_SEEN(in_dev
))
805 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
807 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
812 /* mark EXCLUDE-mode sources */
813 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
815 struct ip_sf_list
*psf
;
819 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
822 for (i
= 0; i
< nsrcs
; i
++) {
823 /* skip inactive filters */
824 if (psf
->sf_count
[MCAST_INCLUDE
] ||
825 pmc
->sfcount
[MCAST_EXCLUDE
] !=
826 psf
->sf_count
[MCAST_EXCLUDE
])
828 if (srcs
[i
] == psf
->sf_inaddr
) {
835 if (scount
== nsrcs
) /* all sources excluded */
840 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
842 struct ip_sf_list
*psf
;
845 if (pmc
->sfmode
== MCAST_EXCLUDE
)
846 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
848 /* mark INCLUDE-mode sources */
850 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
853 for (i
= 0; i
< nsrcs
; i
++)
854 if (srcs
[i
] == psf
->sf_inaddr
) {
868 /* return true if packet was dropped */
869 static bool igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
871 struct ip_mc_list
*im
;
872 struct net
*net
= dev_net(in_dev
->dev
);
874 /* Timers are only set for non-local groups */
876 if (group
== IGMP_ALL_HOSTS
)
878 if (ipv4_is_local_multicast(group
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
882 for_each_pmc_rcu(in_dev
, im
) {
883 if (im
->multiaddr
== group
) {
892 /* return true if packet was dropped */
893 static bool igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
896 struct igmphdr
*ih
= igmp_hdr(skb
);
897 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
898 struct ip_mc_list
*im
;
899 __be32 group
= ih
->group
;
902 struct net
*net
= dev_net(in_dev
->dev
);
907 /* Alas, old v1 router presents here. */
909 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
910 in_dev
->mr_v1_seen
= jiffies
+
911 IGMP_V1_ROUTER_PRESENT_TIMEOUT
;
914 /* v2 router present */
915 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
916 in_dev
->mr_v2_seen
= jiffies
+
917 IGMP_V2_ROUTER_PRESENT_TIMEOUT
;
919 /* cancel the interface change timer */
920 in_dev
->mr_ifc_count
= 0;
921 if (del_timer(&in_dev
->mr_ifc_timer
))
922 __in_dev_put(in_dev
);
923 /* clear deleted report items */
924 igmpv3_clear_delrec(in_dev
);
925 } else if (len
< 12) {
926 return true; /* ignore bogus packet; freed by caller */
927 } else if (IGMP_V1_SEEN(in_dev
)) {
928 /* This is a v3 query with v1 queriers present */
929 max_delay
= IGMP_QUERY_RESPONSE_INTERVAL
;
931 } else if (IGMP_V2_SEEN(in_dev
)) {
932 /* this is a v3 query with v2 queriers present;
933 * Interpretation of the max_delay code is problematic here.
934 * A real v2 host would use ih_code directly, while v3 has a
935 * different encoding. We use the v3 encoding as more likely
936 * to be intended in a v3 query.
938 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
940 max_delay
= 1; /* can't mod w/ 0 */
942 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
945 ih3
= igmpv3_query_hdr(skb
);
947 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
948 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
950 ih3
= igmpv3_query_hdr(skb
);
953 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
955 max_delay
= 1; /* can't mod w/ 0 */
956 in_dev
->mr_maxdelay
= max_delay
;
958 in_dev
->mr_qrv
= ih3
->qrv
;
959 if (!group
) { /* general query */
961 return true; /* no sources allowed */
962 igmp_gq_start_timer(in_dev
);
965 /* mark sources to include, if group & source-specific */
966 mark
= ih3
->nsrcs
!= 0;
970 * - Start the timers in all of our membership records
971 * that the query applies to for the interface on
972 * which the query arrived excl. those that belong
973 * to a "local" group (224.0.0.X)
974 * - For timers already running check if they need to
976 * - Use the igmp->igmp_code field as the maximum
980 for_each_pmc_rcu(in_dev
, im
) {
983 if (group
&& group
!= im
->multiaddr
)
985 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
987 if (ipv4_is_local_multicast(im
->multiaddr
) &&
988 !net
->ipv4
.sysctl_igmp_llm_reports
)
990 spin_lock_bh(&im
->lock
);
992 im
->gsquery
= im
->gsquery
&& mark
;
995 changed
= !im
->gsquery
||
996 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
997 spin_unlock_bh(&im
->lock
);
999 igmp_mod_timer(im
, max_delay
);
1005 /* called in rcu_read_lock() section */
1006 int igmp_rcv(struct sk_buff
*skb
)
1008 /* This basically follows the spec line by line -- see RFC1112 */
1010 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
1012 bool dropped
= true;
1017 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
1020 if (skb_checksum_simple_validate(skb
))
1025 case IGMP_HOST_MEMBERSHIP_QUERY
:
1026 dropped
= igmp_heard_query(in_dev
, skb
, len
);
1028 case IGMP_HOST_MEMBERSHIP_REPORT
:
1029 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1030 /* Is it our report looped back? */
1031 if (rt_is_output_route(skb_rtable(skb
)))
1033 /* don't rely on MC router hearing unicast reports */
1034 if (skb
->pkt_type
== PACKET_MULTICAST
||
1035 skb
->pkt_type
== PACKET_BROADCAST
)
1036 dropped
= igmp_heard_report(in_dev
, ih
->group
);
1039 #ifdef CONFIG_IP_PIMSM_V1
1040 return pim_rcv_v1(skb
);
1042 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1045 case IGMP_HOST_LEAVE_MESSAGE
:
1047 case IGMP_MTRACE_RESP
:
1065 * Add a filter to a device
1068 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1070 char buf
[MAX_ADDR_LEN
];
1071 struct net_device
*dev
= in_dev
->dev
;
1073 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1074 We will get multicast token leakage, when IFF_MULTICAST
1075 is changed. This check should be done in ndo_set_rx_mode
1076 routine. Something sort of:
1077 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1080 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1081 dev_mc_add(dev
, buf
);
1085 * Remove a filter from a device
1088 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1090 char buf
[MAX_ADDR_LEN
];
1091 struct net_device
*dev
= in_dev
->dev
;
1093 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1094 dev_mc_del(dev
, buf
);
1097 #ifdef CONFIG_IP_MULTICAST
1099 * deleted ip_mc_list manipulation
1101 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1103 struct ip_mc_list
*pmc
;
1104 struct net
*net
= dev_net(in_dev
->dev
);
1106 /* this is an "ip_mc_list" for convenience; only the fields below
1107 * are actually used. In particular, the refcnt and users are not
1108 * used for management of the delete list. Using the same structure
1109 * for deleted items allows change reports to use common code with
1110 * non-deleted or query-response MCA's.
1112 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1115 spin_lock_init(&pmc
->lock
);
1116 spin_lock_bh(&im
->lock
);
1117 pmc
->interface
= im
->interface
;
1118 in_dev_hold(in_dev
);
1119 pmc
->multiaddr
= im
->multiaddr
;
1120 pmc
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1121 pmc
->sfmode
= im
->sfmode
;
1122 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1123 struct ip_sf_list
*psf
;
1125 pmc
->tomb
= im
->tomb
;
1126 pmc
->sources
= im
->sources
;
1127 im
->tomb
= im
->sources
= NULL
;
1128 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1129 psf
->sf_crcount
= pmc
->crcount
;
1131 spin_unlock_bh(&im
->lock
);
1133 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1134 pmc
->next
= in_dev
->mc_tomb
;
1135 in_dev
->mc_tomb
= pmc
;
1136 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1140 * restore ip_mc_list deleted records
1142 static void igmpv3_del_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1144 struct ip_mc_list
*pmc
, *pmc_prev
;
1145 struct ip_sf_list
*psf
;
1146 struct net
*net
= dev_net(in_dev
->dev
);
1147 __be32 multiaddr
= im
->multiaddr
;
1149 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1151 for (pmc
= in_dev
->mc_tomb
; pmc
; pmc
= pmc
->next
) {
1152 if (pmc
->multiaddr
== multiaddr
)
1158 pmc_prev
->next
= pmc
->next
;
1160 in_dev
->mc_tomb
= pmc
->next
;
1162 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1164 spin_lock_bh(&im
->lock
);
1166 im
->interface
= pmc
->interface
;
1167 im
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1168 im
->sfmode
= pmc
->sfmode
;
1169 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1170 im
->tomb
= pmc
->tomb
;
1171 im
->sources
= pmc
->sources
;
1172 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
)
1173 psf
->sf_crcount
= im
->crcount
;
1175 in_dev_put(pmc
->interface
);
1178 spin_unlock_bh(&im
->lock
);
1182 * flush ip_mc_list deleted records
1184 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1186 struct ip_mc_list
*pmc
, *nextpmc
;
1188 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1189 pmc
= in_dev
->mc_tomb
;
1190 in_dev
->mc_tomb
= NULL
;
1191 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1193 for (; pmc
; pmc
= nextpmc
) {
1194 nextpmc
= pmc
->next
;
1195 ip_mc_clear_src(pmc
);
1196 in_dev_put(pmc
->interface
);
1199 /* clear dead sources, too */
1201 for_each_pmc_rcu(in_dev
, pmc
) {
1202 struct ip_sf_list
*psf
, *psf_next
;
1204 spin_lock_bh(&pmc
->lock
);
1207 spin_unlock_bh(&pmc
->lock
);
1208 for (; psf
; psf
= psf_next
) {
1209 psf_next
= psf
->sf_next
;
1217 static void igmp_group_dropped(struct ip_mc_list
*im
)
1219 struct in_device
*in_dev
= im
->interface
;
1220 #ifdef CONFIG_IP_MULTICAST
1221 struct net
*net
= dev_net(in_dev
->dev
);
1227 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1230 #ifdef CONFIG_IP_MULTICAST
1231 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1233 if (ipv4_is_local_multicast(im
->multiaddr
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
1236 reporter
= im
->reporter
;
1237 igmp_stop_timer(im
);
1239 if (!in_dev
->dead
) {
1240 if (IGMP_V1_SEEN(in_dev
))
1242 if (IGMP_V2_SEEN(in_dev
)) {
1244 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1248 igmpv3_add_delrec(in_dev
, im
);
1250 igmp_ifc_event(in_dev
);
1255 static void igmp_group_added(struct ip_mc_list
*im
)
1257 struct in_device
*in_dev
= im
->interface
;
1258 #ifdef CONFIG_IP_MULTICAST
1259 struct net
*net
= dev_net(in_dev
->dev
);
1262 if (im
->loaded
== 0) {
1264 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1267 #ifdef CONFIG_IP_MULTICAST
1268 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1270 if (ipv4_is_local_multicast(im
->multiaddr
) && !net
->ipv4
.sysctl_igmp_llm_reports
)
1275 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1276 spin_lock_bh(&im
->lock
);
1277 igmp_start_timer(im
, IGMP_INITIAL_REPORT_DELAY
);
1278 spin_unlock_bh(&im
->lock
);
1283 im
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1284 igmp_ifc_event(in_dev
);
1290 * Multicast list managers
1293 static u32
ip_mc_hash(const struct ip_mc_list
*im
)
1295 return hash_32((__force u32
)im
->multiaddr
, MC_HASH_SZ_LOG
);
1298 static void ip_mc_hash_add(struct in_device
*in_dev
,
1299 struct ip_mc_list
*im
)
1301 struct ip_mc_list __rcu
**mc_hash
;
1304 mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1306 hash
= ip_mc_hash(im
);
1307 im
->next_hash
= mc_hash
[hash
];
1308 rcu_assign_pointer(mc_hash
[hash
], im
);
1312 /* do not use a hash table for small number of items */
1313 if (in_dev
->mc_count
< 4)
1316 mc_hash
= kzalloc(sizeof(struct ip_mc_list
*) << MC_HASH_SZ_LOG
,
1321 for_each_pmc_rtnl(in_dev
, im
) {
1322 hash
= ip_mc_hash(im
);
1323 im
->next_hash
= mc_hash
[hash
];
1324 RCU_INIT_POINTER(mc_hash
[hash
], im
);
1327 rcu_assign_pointer(in_dev
->mc_hash
, mc_hash
);
1330 static void ip_mc_hash_remove(struct in_device
*in_dev
,
1331 struct ip_mc_list
*im
)
1333 struct ip_mc_list __rcu
**mc_hash
= rtnl_dereference(in_dev
->mc_hash
);
1334 struct ip_mc_list
*aux
;
1338 mc_hash
+= ip_mc_hash(im
);
1339 while ((aux
= rtnl_dereference(*mc_hash
)) != im
)
1340 mc_hash
= &aux
->next_hash
;
1341 *mc_hash
= im
->next_hash
;
1346 * A socket has joined a multicast group on device dev.
1349 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1351 struct ip_mc_list
*im
;
1352 #ifdef CONFIG_IP_MULTICAST
1353 struct net
*net
= dev_net(in_dev
->dev
);
1358 for_each_pmc_rtnl(in_dev
, im
) {
1359 if (im
->multiaddr
== addr
) {
1361 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1366 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1371 im
->interface
= in_dev
;
1372 in_dev_hold(in_dev
);
1373 im
->multiaddr
= addr
;
1374 /* initial mode is (EX, empty) */
1375 im
->sfmode
= MCAST_EXCLUDE
;
1376 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1377 refcount_set(&im
->refcnt
, 1);
1378 spin_lock_init(&im
->lock
);
1379 #ifdef CONFIG_IP_MULTICAST
1380 setup_timer(&im
->timer
, igmp_timer_expire
, (unsigned long)im
);
1381 im
->unsolicit_count
= net
->ipv4
.sysctl_igmp_qrv
;
1384 im
->next_rcu
= in_dev
->mc_list
;
1386 rcu_assign_pointer(in_dev
->mc_list
, im
);
1388 ip_mc_hash_add(in_dev
, im
);
1390 #ifdef CONFIG_IP_MULTICAST
1391 igmpv3_del_delrec(in_dev
, im
);
1393 igmp_group_added(im
);
1395 ip_rt_multicast_event(in_dev
);
1399 EXPORT_SYMBOL(ip_mc_inc_group
);
1401 static int ip_mc_check_iphdr(struct sk_buff
*skb
)
1403 const struct iphdr
*iph
;
1405 unsigned int offset
= skb_network_offset(skb
) + sizeof(*iph
);
1407 if (!pskb_may_pull(skb
, offset
))
1412 if (iph
->version
!= 4 || ip_hdrlen(skb
) < sizeof(*iph
))
1415 offset
+= ip_hdrlen(skb
) - sizeof(*iph
);
1417 if (!pskb_may_pull(skb
, offset
))
1422 if (unlikely(ip_fast_csum((u8
*)iph
, iph
->ihl
)))
1425 len
= skb_network_offset(skb
) + ntohs(iph
->tot_len
);
1426 if (skb
->len
< len
|| len
< offset
)
1429 skb_set_transport_header(skb
, offset
);
1434 static int ip_mc_check_igmp_reportv3(struct sk_buff
*skb
)
1436 unsigned int len
= skb_transport_offset(skb
);
1438 len
+= sizeof(struct igmpv3_report
);
1440 return pskb_may_pull(skb
, len
) ? 0 : -EINVAL
;
1443 static int ip_mc_check_igmp_query(struct sk_buff
*skb
)
1445 unsigned int len
= skb_transport_offset(skb
);
1447 len
+= sizeof(struct igmphdr
);
1452 if (skb
->len
!= len
) {
1454 len
+= sizeof(struct igmpv3_query
) - sizeof(struct igmphdr
);
1455 if (skb
->len
< len
|| !pskb_may_pull(skb
, len
))
1459 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1460 * all-systems destination addresses (224.0.0.1) for general queries
1462 if (!igmp_hdr(skb
)->group
&&
1463 ip_hdr(skb
)->daddr
!= htonl(INADDR_ALLHOSTS_GROUP
))
1469 static int ip_mc_check_igmp_msg(struct sk_buff
*skb
)
1471 switch (igmp_hdr(skb
)->type
) {
1472 case IGMP_HOST_LEAVE_MESSAGE
:
1473 case IGMP_HOST_MEMBERSHIP_REPORT
:
1474 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
1477 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
1478 return ip_mc_check_igmp_reportv3(skb
);
1479 case IGMP_HOST_MEMBERSHIP_QUERY
:
1480 return ip_mc_check_igmp_query(skb
);
1486 static inline __sum16
ip_mc_validate_checksum(struct sk_buff
*skb
)
1488 return skb_checksum_simple_validate(skb
);
1491 static int __ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
)
1494 struct sk_buff
*skb_chk
;
1495 unsigned int transport_len
;
1496 unsigned int len
= skb_transport_offset(skb
) + sizeof(struct igmphdr
);
1499 transport_len
= ntohs(ip_hdr(skb
)->tot_len
) - ip_hdrlen(skb
);
1501 skb_chk
= skb_checksum_trimmed(skb
, transport_len
,
1502 ip_mc_validate_checksum
);
1506 if (!pskb_may_pull(skb_chk
, len
))
1509 ret
= ip_mc_check_igmp_msg(skb_chk
);
1514 *skb_trimmed
= skb_chk
;
1515 /* free now unneeded clone */
1516 else if (skb_chk
!= skb
)
1522 if (ret
&& skb_chk
&& skb_chk
!= skb
)
1529 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1530 * @skb: the skb to validate
1531 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1533 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1534 * skb transport header accordingly and returns zero.
1536 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1538 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1539 * -ENOMEM: A memory allocation failure happened.
1541 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1542 * to NULL): After parsing an IGMP packet successfully it will point to
1543 * an skb which has its tail aligned to the IP packet end. This might
1544 * either be the originally provided skb or a trimmed, cloned version if
1545 * the skb frame had data beyond the IP packet. A cloned skb allows us
1546 * to leave the original skb and its full frame unchanged (which might be
1547 * desirable for layer 2 frame jugglers).
1549 * Caller needs to set the skb network header and free any returned skb if it
1550 * differs from the provided skb.
1552 int ip_mc_check_igmp(struct sk_buff
*skb
, struct sk_buff
**skb_trimmed
)
1554 int ret
= ip_mc_check_iphdr(skb
);
1559 if (ip_hdr(skb
)->protocol
!= IPPROTO_IGMP
)
1562 return __ip_mc_check_igmp(skb
, skb_trimmed
);
1564 EXPORT_SYMBOL(ip_mc_check_igmp
);
1567 * Resend IGMP JOIN report; used by netdev notifier.
1569 static void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1571 #ifdef CONFIG_IP_MULTICAST
1572 struct ip_mc_list
*im
;
1574 struct net
*net
= dev_net(in_dev
->dev
);
1578 for_each_pmc_rtnl(in_dev
, im
) {
1579 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1581 if (ipv4_is_local_multicast(im
->multiaddr
) &&
1582 !net
->ipv4
.sysctl_igmp_llm_reports
)
1585 /* a failover is happening and switches
1586 * must be notified immediately
1588 if (IGMP_V1_SEEN(in_dev
))
1589 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1590 else if (IGMP_V2_SEEN(in_dev
))
1591 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1593 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1594 igmp_send_report(in_dev
, im
, type
);
1600 * A socket has left a multicast group on device dev
1603 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1605 struct ip_mc_list
*i
;
1606 struct ip_mc_list __rcu
**ip
;
1610 for (ip
= &in_dev
->mc_list
;
1611 (i
= rtnl_dereference(*ip
)) != NULL
;
1612 ip
= &i
->next_rcu
) {
1613 if (i
->multiaddr
== addr
) {
1614 if (--i
->users
== 0) {
1615 ip_mc_hash_remove(in_dev
, i
);
1618 igmp_group_dropped(i
);
1622 ip_rt_multicast_event(in_dev
);
1631 EXPORT_SYMBOL(ip_mc_dec_group
);
1633 /* Device changing type */
1635 void ip_mc_unmap(struct in_device
*in_dev
)
1637 struct ip_mc_list
*pmc
;
1641 for_each_pmc_rtnl(in_dev
, pmc
)
1642 igmp_group_dropped(pmc
);
1645 void ip_mc_remap(struct in_device
*in_dev
)
1647 struct ip_mc_list
*pmc
;
1651 for_each_pmc_rtnl(in_dev
, pmc
) {
1652 #ifdef CONFIG_IP_MULTICAST
1653 igmpv3_del_delrec(in_dev
, pmc
);
1655 igmp_group_added(pmc
);
1659 /* Device going down */
1661 void ip_mc_down(struct in_device
*in_dev
)
1663 struct ip_mc_list
*pmc
;
1667 for_each_pmc_rtnl(in_dev
, pmc
)
1668 igmp_group_dropped(pmc
);
1670 #ifdef CONFIG_IP_MULTICAST
1671 in_dev
->mr_ifc_count
= 0;
1672 if (del_timer(&in_dev
->mr_ifc_timer
))
1673 __in_dev_put(in_dev
);
1674 in_dev
->mr_gq_running
= 0;
1675 if (del_timer(&in_dev
->mr_gq_timer
))
1676 __in_dev_put(in_dev
);
1679 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1682 void ip_mc_init_dev(struct in_device
*in_dev
)
1684 #ifdef CONFIG_IP_MULTICAST
1685 struct net
*net
= dev_net(in_dev
->dev
);
1689 #ifdef CONFIG_IP_MULTICAST
1690 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1691 (unsigned long)in_dev
);
1692 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1693 (unsigned long)in_dev
);
1694 in_dev
->mr_qrv
= net
->ipv4
.sysctl_igmp_qrv
;
1697 spin_lock_init(&in_dev
->mc_tomb_lock
);
1700 /* Device going up */
1702 void ip_mc_up(struct in_device
*in_dev
)
1704 struct ip_mc_list
*pmc
;
1705 #ifdef CONFIG_IP_MULTICAST
1706 struct net
*net
= dev_net(in_dev
->dev
);
1711 #ifdef CONFIG_IP_MULTICAST
1712 in_dev
->mr_qrv
= net
->ipv4
.sysctl_igmp_qrv
;
1714 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1716 for_each_pmc_rtnl(in_dev
, pmc
) {
1717 #ifdef CONFIG_IP_MULTICAST
1718 igmpv3_del_delrec(in_dev
, pmc
);
1720 igmp_group_added(pmc
);
1725 * Device is about to be destroyed: clean up.
1728 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1730 struct ip_mc_list
*i
;
1734 /* Deactivate timers */
1736 #ifdef CONFIG_IP_MULTICAST
1737 igmpv3_clear_delrec(in_dev
);
1740 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1741 in_dev
->mc_list
= i
->next_rcu
;
1747 /* RTNL is locked */
1748 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1750 struct net_device
*dev
= NULL
;
1751 struct in_device
*idev
= NULL
;
1753 if (imr
->imr_ifindex
) {
1754 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1757 if (imr
->imr_address
.s_addr
) {
1758 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1764 struct rtable
*rt
= ip_route_output(net
,
1765 imr
->imr_multiaddr
.s_addr
,
1773 imr
->imr_ifindex
= dev
->ifindex
;
1774 idev
= __in_dev_get_rtnl(dev
);
1780 * Join a socket to a group
1783 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1786 struct ip_sf_list
*psf
, *psf_prev
;
1790 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1791 if (psf
->sf_inaddr
== *psfsrc
)
1795 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1796 /* source filter not found, or count wrong => bug */
1799 psf
->sf_count
[sfmode
]--;
1800 if (psf
->sf_count
[sfmode
] == 0) {
1801 ip_rt_multicast_event(pmc
->interface
);
1803 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1804 #ifdef CONFIG_IP_MULTICAST
1805 struct in_device
*in_dev
= pmc
->interface
;
1806 struct net
*net
= dev_net(in_dev
->dev
);
1809 /* no more filters for this source */
1811 psf_prev
->sf_next
= psf
->sf_next
;
1813 pmc
->sources
= psf
->sf_next
;
1814 #ifdef CONFIG_IP_MULTICAST
1815 if (psf
->sf_oldin
&&
1816 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1817 psf
->sf_crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1818 psf
->sf_next
= pmc
->tomb
;
1828 #ifndef CONFIG_IP_MULTICAST
1829 #define igmp_ifc_event(x) do { } while (0)
1832 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1833 int sfcount
, __be32
*psfsrc
, int delta
)
1835 struct ip_mc_list
*pmc
;
1842 for_each_pmc_rcu(in_dev
, pmc
) {
1843 if (*pmca
== pmc
->multiaddr
)
1847 /* MCA not found?? bug */
1851 spin_lock_bh(&pmc
->lock
);
1853 #ifdef CONFIG_IP_MULTICAST
1858 if (!pmc
->sfcount
[sfmode
])
1860 pmc
->sfcount
[sfmode
]--;
1863 for (i
= 0; i
< sfcount
; i
++) {
1864 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1866 changerec
|= rv
> 0;
1870 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1871 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1872 pmc
->sfcount
[MCAST_INCLUDE
]) {
1873 #ifdef CONFIG_IP_MULTICAST
1874 struct ip_sf_list
*psf
;
1875 struct net
*net
= dev_net(in_dev
->dev
);
1878 /* filter mode change */
1879 pmc
->sfmode
= MCAST_INCLUDE
;
1880 #ifdef CONFIG_IP_MULTICAST
1881 pmc
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
1882 in_dev
->mr_ifc_count
= pmc
->crcount
;
1883 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1884 psf
->sf_crcount
= 0;
1885 igmp_ifc_event(pmc
->interface
);
1886 } else if (sf_setstate(pmc
) || changerec
) {
1887 igmp_ifc_event(pmc
->interface
);
1891 spin_unlock_bh(&pmc
->lock
);
1896 * Add multicast single-source filter to the interface list
1898 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1901 struct ip_sf_list
*psf
, *psf_prev
;
1904 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1905 if (psf
->sf_inaddr
== *psfsrc
)
1910 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1913 psf
->sf_inaddr
= *psfsrc
;
1915 psf_prev
->sf_next
= psf
;
1919 psf
->sf_count
[sfmode
]++;
1920 if (psf
->sf_count
[sfmode
] == 1) {
1921 ip_rt_multicast_event(pmc
->interface
);
1926 #ifdef CONFIG_IP_MULTICAST
1927 static void sf_markstate(struct ip_mc_list
*pmc
)
1929 struct ip_sf_list
*psf
;
1930 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1932 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
1933 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1934 psf
->sf_oldin
= mca_xcount
==
1935 psf
->sf_count
[MCAST_EXCLUDE
] &&
1936 !psf
->sf_count
[MCAST_INCLUDE
];
1938 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1941 static int sf_setstate(struct ip_mc_list
*pmc
)
1943 struct ip_sf_list
*psf
, *dpsf
;
1944 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1945 int qrv
= pmc
->interface
->mr_qrv
;
1949 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
) {
1950 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1951 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1952 !psf
->sf_count
[MCAST_INCLUDE
];
1954 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1956 if (!psf
->sf_oldin
) {
1957 struct ip_sf_list
*prev
= NULL
;
1959 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
) {
1960 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1966 prev
->sf_next
= dpsf
->sf_next
;
1968 pmc
->tomb
= dpsf
->sf_next
;
1971 psf
->sf_crcount
= qrv
;
1974 } else if (psf
->sf_oldin
) {
1976 psf
->sf_crcount
= 0;
1978 * add or update "delete" records if an active filter
1981 for (dpsf
= pmc
->tomb
; dpsf
; dpsf
= dpsf
->sf_next
)
1982 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1985 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1989 /* pmc->lock held by callers */
1990 dpsf
->sf_next
= pmc
->tomb
;
1993 dpsf
->sf_crcount
= qrv
;
2002 * Add multicast source filter list to the interface list
2004 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
2005 int sfcount
, __be32
*psfsrc
, int delta
)
2007 struct ip_mc_list
*pmc
;
2014 for_each_pmc_rcu(in_dev
, pmc
) {
2015 if (*pmca
== pmc
->multiaddr
)
2019 /* MCA not found?? bug */
2023 spin_lock_bh(&pmc
->lock
);
2026 #ifdef CONFIG_IP_MULTICAST
2029 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
2031 pmc
->sfcount
[sfmode
]++;
2033 for (i
= 0; i
< sfcount
; i
++) {
2034 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
]);
2042 pmc
->sfcount
[sfmode
]--;
2043 for (j
= 0; j
< i
; j
++)
2044 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[j
]);
2045 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
2046 #ifdef CONFIG_IP_MULTICAST
2047 struct ip_sf_list
*psf
;
2048 struct net
*net
= dev_net(pmc
->interface
->dev
);
2049 in_dev
= pmc
->interface
;
2052 /* filter mode change */
2053 if (pmc
->sfcount
[MCAST_EXCLUDE
])
2054 pmc
->sfmode
= MCAST_EXCLUDE
;
2055 else if (pmc
->sfcount
[MCAST_INCLUDE
])
2056 pmc
->sfmode
= MCAST_INCLUDE
;
2057 #ifdef CONFIG_IP_MULTICAST
2058 /* else no filters; keep old mode for reports */
2060 pmc
->crcount
= in_dev
->mr_qrv
?: net
->ipv4
.sysctl_igmp_qrv
;
2061 in_dev
->mr_ifc_count
= pmc
->crcount
;
2062 for (psf
= pmc
->sources
; psf
; psf
= psf
->sf_next
)
2063 psf
->sf_crcount
= 0;
2064 igmp_ifc_event(in_dev
);
2065 } else if (sf_setstate(pmc
)) {
2066 igmp_ifc_event(in_dev
);
2069 spin_unlock_bh(&pmc
->lock
);
2073 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
2075 struct ip_sf_list
*psf
, *nextpsf
, *tomb
, *sources
;
2077 spin_lock_bh(&pmc
->lock
);
2080 sources
= pmc
->sources
;
2081 pmc
->sources
= NULL
;
2082 pmc
->sfmode
= MCAST_EXCLUDE
;
2083 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
2084 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
2085 spin_unlock_bh(&pmc
->lock
);
2087 for (psf
= tomb
; psf
; psf
= nextpsf
) {
2088 nextpsf
= psf
->sf_next
;
2091 for (psf
= sources
; psf
; psf
= nextpsf
) {
2092 nextpsf
= psf
->sf_next
;
2097 /* Join a multicast group
2100 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2102 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
2103 struct ip_mc_socklist
*iml
, *i
;
2104 struct in_device
*in_dev
;
2105 struct inet_sock
*inet
= inet_sk(sk
);
2106 struct net
*net
= sock_net(sk
);
2113 if (!ipv4_is_multicast(addr
))
2116 in_dev
= ip_mc_find_dev(net
, imr
);
2124 ifindex
= imr
->imr_ifindex
;
2125 for_each_pmc_rtnl(inet
, i
) {
2126 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
2127 i
->multi
.imr_ifindex
== ifindex
)
2132 if (count
>= net
->ipv4
.sysctl_igmp_max_memberships
)
2134 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
2138 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
2139 iml
->next_rcu
= inet
->mc_list
;
2141 iml
->sfmode
= MCAST_EXCLUDE
;
2142 rcu_assign_pointer(inet
->mc_list
, iml
);
2143 ip_mc_inc_group(in_dev
, addr
);
2148 EXPORT_SYMBOL(ip_mc_join_group
);
2150 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
2151 struct in_device
*in_dev
)
2153 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
2157 /* any-source empty exclude case */
2158 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2159 iml
->sfmode
, 0, NULL
, 0);
2161 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
2162 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
2163 RCU_INIT_POINTER(iml
->sflist
, NULL
);
2164 /* decrease mem now to avoid the memleak warning */
2165 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
2166 kfree_rcu(psf
, rcu
);
2170 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
2172 struct inet_sock
*inet
= inet_sk(sk
);
2173 struct ip_mc_socklist
*iml
;
2174 struct ip_mc_socklist __rcu
**imlp
;
2175 struct in_device
*in_dev
;
2176 struct net
*net
= sock_net(sk
);
2177 __be32 group
= imr
->imr_multiaddr
.s_addr
;
2179 int ret
= -EADDRNOTAVAIL
;
2183 in_dev
= ip_mc_find_dev(net
, imr
);
2184 if (!imr
->imr_ifindex
&& !imr
->imr_address
.s_addr
&& !in_dev
) {
2188 ifindex
= imr
->imr_ifindex
;
2189 for (imlp
= &inet
->mc_list
;
2190 (iml
= rtnl_dereference(*imlp
)) != NULL
;
2191 imlp
= &iml
->next_rcu
) {
2192 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
2195 if (iml
->multi
.imr_ifindex
!= ifindex
)
2197 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
2198 iml
->multi
.imr_address
.s_addr
)
2201 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2203 *imlp
= iml
->next_rcu
;
2206 ip_mc_dec_group(in_dev
, group
);
2208 /* decrease mem now to avoid the memleak warning */
2209 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2210 kfree_rcu(iml
, rcu
);
2216 EXPORT_SYMBOL(ip_mc_leave_group
);
2218 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
2219 ip_mreq_source
*mreqs
, int ifindex
)
2222 struct ip_mreqn imr
;
2223 __be32 addr
= mreqs
->imr_multiaddr
;
2224 struct ip_mc_socklist
*pmc
;
2225 struct in_device
*in_dev
= NULL
;
2226 struct inet_sock
*inet
= inet_sk(sk
);
2227 struct ip_sf_socklist
*psl
;
2228 struct net
*net
= sock_net(sk
);
2232 if (!ipv4_is_multicast(addr
))
2237 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
2238 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
2239 imr
.imr_ifindex
= ifindex
;
2240 in_dev
= ip_mc_find_dev(net
, &imr
);
2246 err
= -EADDRNOTAVAIL
;
2248 for_each_pmc_rtnl(inet
, pmc
) {
2249 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
2250 imr
.imr_multiaddr
.s_addr
) &&
2251 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
2254 if (!pmc
) { /* must have a prior join */
2258 /* if a source filter was set, must be the same mode as before */
2260 if (pmc
->sfmode
!= omode
) {
2264 } else if (pmc
->sfmode
!= omode
) {
2265 /* allow mode switches for empty-set filters */
2266 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
2267 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
2269 pmc
->sfmode
= omode
;
2272 psl
= rtnl_dereference(pmc
->sflist
);
2275 goto done
; /* err = -EADDRNOTAVAIL */
2277 for (i
= 0; i
< psl
->sl_count
; i
++) {
2278 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2283 if (rv
) /* source not found */
2284 goto done
; /* err = -EADDRNOTAVAIL */
2286 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2287 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
2292 /* update the interface filter */
2293 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2294 &mreqs
->imr_sourceaddr
, 1);
2296 for (j
= i
+1; j
< psl
->sl_count
; j
++)
2297 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2302 /* else, add a new source to the filter */
2304 if (psl
&& psl
->sl_count
>= net
->ipv4
.sysctl_igmp_max_msf
) {
2308 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2309 struct ip_sf_socklist
*newpsl
;
2310 int count
= IP_SFBLOCK
;
2313 count
+= psl
->sl_max
;
2314 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2319 newpsl
->sl_max
= count
;
2320 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2322 for (i
= 0; i
< psl
->sl_count
; i
++)
2323 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2324 /* decrease mem now to avoid the memleak warning */
2325 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2326 kfree_rcu(psl
, rcu
);
2328 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2331 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2332 for (i
= 0; i
< psl
->sl_count
; i
++) {
2333 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2338 if (rv
== 0) /* address already there is an error */
2340 for (j
= psl
->sl_count
-1; j
>= i
; j
--)
2341 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2342 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2345 /* update the interface list */
2346 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2347 &mreqs
->imr_sourceaddr
, 1);
2350 err
= ip_mc_leave_group(sk
, &imr
);
2354 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2357 struct ip_mreqn imr
;
2358 __be32 addr
= msf
->imsf_multiaddr
;
2359 struct ip_mc_socklist
*pmc
;
2360 struct in_device
*in_dev
;
2361 struct inet_sock
*inet
= inet_sk(sk
);
2362 struct ip_sf_socklist
*newpsl
, *psl
;
2363 struct net
*net
= sock_net(sk
);
2366 if (!ipv4_is_multicast(addr
))
2368 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2369 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2374 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2375 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2376 imr
.imr_ifindex
= ifindex
;
2377 in_dev
= ip_mc_find_dev(net
, &imr
);
2384 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2385 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2390 for_each_pmc_rtnl(inet
, pmc
) {
2391 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2392 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2395 if (!pmc
) { /* must have a prior join */
2399 if (msf
->imsf_numsrc
) {
2400 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2406 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2407 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2408 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2409 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2410 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2412 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2417 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2418 msf
->imsf_fmode
, 0, NULL
, 0);
2420 psl
= rtnl_dereference(pmc
->sflist
);
2422 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2423 psl
->sl_count
, psl
->sl_addr
, 0);
2424 /* decrease mem now to avoid the memleak warning */
2425 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2426 kfree_rcu(psl
, rcu
);
2428 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2430 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2431 pmc
->sfmode
= msf
->imsf_fmode
;
2435 err
= ip_mc_leave_group(sk
, &imr
);
2439 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2440 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2442 int err
, len
, count
, copycount
;
2443 struct ip_mreqn imr
;
2444 __be32 addr
= msf
->imsf_multiaddr
;
2445 struct ip_mc_socklist
*pmc
;
2446 struct in_device
*in_dev
;
2447 struct inet_sock
*inet
= inet_sk(sk
);
2448 struct ip_sf_socklist
*psl
;
2449 struct net
*net
= sock_net(sk
);
2453 if (!ipv4_is_multicast(addr
))
2456 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2457 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2458 imr
.imr_ifindex
= 0;
2459 in_dev
= ip_mc_find_dev(net
, &imr
);
2465 err
= -EADDRNOTAVAIL
;
2467 for_each_pmc_rtnl(inet
, pmc
) {
2468 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2469 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2472 if (!pmc
) /* must have a prior join */
2474 msf
->imsf_fmode
= pmc
->sfmode
;
2475 psl
= rtnl_dereference(pmc
->sflist
);
2480 count
= psl
->sl_count
;
2482 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2483 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2484 msf
->imsf_numsrc
= count
;
2485 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2486 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2490 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2497 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2498 struct group_filter __user
*optval
, int __user
*optlen
)
2500 int err
, i
, count
, copycount
;
2501 struct sockaddr_in
*psin
;
2503 struct ip_mc_socklist
*pmc
;
2504 struct inet_sock
*inet
= inet_sk(sk
);
2505 struct ip_sf_socklist
*psl
;
2509 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2510 if (psin
->sin_family
!= AF_INET
)
2512 addr
= psin
->sin_addr
.s_addr
;
2513 if (!ipv4_is_multicast(addr
))
2516 err
= -EADDRNOTAVAIL
;
2518 for_each_pmc_rtnl(inet
, pmc
) {
2519 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2520 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2523 if (!pmc
) /* must have a prior join */
2525 gsf
->gf_fmode
= pmc
->sfmode
;
2526 psl
= rtnl_dereference(pmc
->sflist
);
2527 count
= psl
? psl
->sl_count
: 0;
2528 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2529 gsf
->gf_numsrc
= count
;
2530 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2531 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2534 for (i
= 0; i
< copycount
; i
++) {
2535 struct sockaddr_storage ss
;
2537 psin
= (struct sockaddr_in
*)&ss
;
2538 memset(&ss
, 0, sizeof(ss
));
2539 psin
->sin_family
= AF_INET
;
2540 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2541 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2550 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2552 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2554 struct inet_sock
*inet
= inet_sk(sk
);
2555 struct ip_mc_socklist
*pmc
;
2556 struct ip_sf_socklist
*psl
;
2561 if (!ipv4_is_multicast(loc_addr
))
2565 for_each_pmc_rcu(inet
, pmc
) {
2566 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2567 pmc
->multi
.imr_ifindex
== dif
)
2573 psl
= rcu_dereference(pmc
->sflist
);
2574 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2578 for (i
= 0; i
< psl
->sl_count
; i
++) {
2579 if (psl
->sl_addr
[i
] == rmt_addr
)
2583 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2585 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2595 * A socket is closing.
2598 void ip_mc_drop_socket(struct sock
*sk
)
2600 struct inet_sock
*inet
= inet_sk(sk
);
2601 struct ip_mc_socklist
*iml
;
2602 struct net
*net
= sock_net(sk
);
2608 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2609 struct in_device
*in_dev
;
2611 inet
->mc_list
= iml
->next_rcu
;
2612 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2613 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2615 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2616 /* decrease mem now to avoid the memleak warning */
2617 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2618 kfree_rcu(iml
, rcu
);
2623 /* called with rcu_read_lock() */
2624 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u8 proto
)
2626 struct ip_mc_list
*im
;
2627 struct ip_mc_list __rcu
**mc_hash
;
2628 struct ip_sf_list
*psf
;
2631 mc_hash
= rcu_dereference(in_dev
->mc_hash
);
2633 u32 hash
= hash_32((__force u32
)mc_addr
, MC_HASH_SZ_LOG
);
2635 for (im
= rcu_dereference(mc_hash
[hash
]);
2637 im
= rcu_dereference(im
->next_hash
)) {
2638 if (im
->multiaddr
== mc_addr
)
2642 for_each_pmc_rcu(in_dev
, im
) {
2643 if (im
->multiaddr
== mc_addr
)
2647 if (im
&& proto
== IPPROTO_IGMP
) {
2651 for (psf
= im
->sources
; psf
; psf
= psf
->sf_next
) {
2652 if (psf
->sf_inaddr
== src_addr
)
2656 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2657 psf
->sf_count
[MCAST_EXCLUDE
] !=
2658 im
->sfcount
[MCAST_EXCLUDE
];
2660 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2662 rv
= 1; /* unspecified source; tentatively allow */
2667 #if defined(CONFIG_PROC_FS)
2668 struct igmp_mc_iter_state
{
2669 struct seq_net_private p
;
2670 struct net_device
*dev
;
2671 struct in_device
*in_dev
;
2674 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2676 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2678 struct net
*net
= seq_file_net(seq
);
2679 struct ip_mc_list
*im
= NULL
;
2680 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2682 state
->in_dev
= NULL
;
2683 for_each_netdev_rcu(net
, state
->dev
) {
2684 struct in_device
*in_dev
;
2686 in_dev
= __in_dev_get_rcu(state
->dev
);
2689 im
= rcu_dereference(in_dev
->mc_list
);
2691 state
->in_dev
= in_dev
;
2698 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2700 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2702 im
= rcu_dereference(im
->next_rcu
);
2704 state
->dev
= next_net_device_rcu(state
->dev
);
2706 state
->in_dev
= NULL
;
2709 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2712 im
= rcu_dereference(state
->in_dev
->mc_list
);
2717 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2719 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2721 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2723 return pos
? NULL
: im
;
2726 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2730 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2733 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2735 struct ip_mc_list
*im
;
2736 if (v
== SEQ_START_TOKEN
)
2737 im
= igmp_mc_get_first(seq
);
2739 im
= igmp_mc_get_next(seq
, v
);
2744 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2747 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2749 state
->in_dev
= NULL
;
2754 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2756 if (v
== SEQ_START_TOKEN
)
2758 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2760 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2761 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2765 #ifdef CONFIG_IP_MULTICAST
2766 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2767 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2773 if (rcu_access_pointer(state
->in_dev
->mc_list
) == im
) {
2774 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2775 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2778 delta
= im
->timer
.expires
- jiffies
;
2780 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2781 im
->multiaddr
, im
->users
,
2783 im
->tm_running
? jiffies_delta_to_clock_t(delta
) : 0,
2789 static const struct seq_operations igmp_mc_seq_ops
= {
2790 .start
= igmp_mc_seq_start
,
2791 .next
= igmp_mc_seq_next
,
2792 .stop
= igmp_mc_seq_stop
,
2793 .show
= igmp_mc_seq_show
,
2796 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2798 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2799 sizeof(struct igmp_mc_iter_state
));
2802 static const struct file_operations igmp_mc_seq_fops
= {
2803 .owner
= THIS_MODULE
,
2804 .open
= igmp_mc_seq_open
,
2806 .llseek
= seq_lseek
,
2807 .release
= seq_release_net
,
2810 struct igmp_mcf_iter_state
{
2811 struct seq_net_private p
;
2812 struct net_device
*dev
;
2813 struct in_device
*idev
;
2814 struct ip_mc_list
*im
;
2817 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2819 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2821 struct net
*net
= seq_file_net(seq
);
2822 struct ip_sf_list
*psf
= NULL
;
2823 struct ip_mc_list
*im
= NULL
;
2824 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2828 for_each_netdev_rcu(net
, state
->dev
) {
2829 struct in_device
*idev
;
2830 idev
= __in_dev_get_rcu(state
->dev
);
2831 if (unlikely(!idev
))
2833 im
= rcu_dereference(idev
->mc_list
);
2835 spin_lock_bh(&im
->lock
);
2842 spin_unlock_bh(&im
->lock
);
2848 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2850 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2854 spin_unlock_bh(&state
->im
->lock
);
2855 state
->im
= state
->im
->next
;
2856 while (!state
->im
) {
2857 state
->dev
= next_net_device_rcu(state
->dev
);
2862 state
->idev
= __in_dev_get_rcu(state
->dev
);
2865 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2869 spin_lock_bh(&state
->im
->lock
);
2870 psf
= state
->im
->sources
;
2876 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2878 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2880 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2882 return pos
? NULL
: psf
;
2885 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2889 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2892 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2894 struct ip_sf_list
*psf
;
2895 if (v
== SEQ_START_TOKEN
)
2896 psf
= igmp_mcf_get_first(seq
);
2898 psf
= igmp_mcf_get_next(seq
, v
);
2903 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2906 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2907 if (likely(state
->im
)) {
2908 spin_unlock_bh(&state
->im
->lock
);
2916 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2918 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2919 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2921 if (v
== SEQ_START_TOKEN
) {
2922 seq_puts(seq
, "Idx Device MCA SRC INC EXC\n");
2926 "0x%08x %6lu %6lu\n",
2927 state
->dev
->ifindex
, state
->dev
->name
,
2928 ntohl(state
->im
->multiaddr
),
2929 ntohl(psf
->sf_inaddr
),
2930 psf
->sf_count
[MCAST_INCLUDE
],
2931 psf
->sf_count
[MCAST_EXCLUDE
]);
2936 static const struct seq_operations igmp_mcf_seq_ops
= {
2937 .start
= igmp_mcf_seq_start
,
2938 .next
= igmp_mcf_seq_next
,
2939 .stop
= igmp_mcf_seq_stop
,
2940 .show
= igmp_mcf_seq_show
,
2943 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2945 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2946 sizeof(struct igmp_mcf_iter_state
));
2949 static const struct file_operations igmp_mcf_seq_fops
= {
2950 .owner
= THIS_MODULE
,
2951 .open
= igmp_mcf_seq_open
,
2953 .llseek
= seq_lseek
,
2954 .release
= seq_release_net
,
2957 static int __net_init
igmp_net_init(struct net
*net
)
2959 struct proc_dir_entry
*pde
;
2962 pde
= proc_create("igmp", S_IRUGO
, net
->proc_net
, &igmp_mc_seq_fops
);
2965 pde
= proc_create("mcfilter", S_IRUGO
, net
->proc_net
,
2966 &igmp_mcf_seq_fops
);
2969 err
= inet_ctl_sock_create(&net
->ipv4
.mc_autojoin_sk
, AF_INET
,
2970 SOCK_DGRAM
, 0, net
);
2972 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2977 /* Sysctl initialization */
2978 net
->ipv4
.sysctl_igmp_max_memberships
= 20;
2979 net
->ipv4
.sysctl_igmp_max_msf
= 10;
2980 /* IGMP reports for link-local multicast groups are enabled by default */
2981 net
->ipv4
.sysctl_igmp_llm_reports
= 1;
2982 net
->ipv4
.sysctl_igmp_qrv
= 2;
2986 remove_proc_entry("mcfilter", net
->proc_net
);
2988 remove_proc_entry("igmp", net
->proc_net
);
2993 static void __net_exit
igmp_net_exit(struct net
*net
)
2995 remove_proc_entry("mcfilter", net
->proc_net
);
2996 remove_proc_entry("igmp", net
->proc_net
);
2997 inet_ctl_sock_destroy(net
->ipv4
.mc_autojoin_sk
);
3000 static struct pernet_operations igmp_net_ops
= {
3001 .init
= igmp_net_init
,
3002 .exit
= igmp_net_exit
,
3006 static int igmp_netdev_event(struct notifier_block
*this,
3007 unsigned long event
, void *ptr
)
3009 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
3010 struct in_device
*in_dev
;
3013 case NETDEV_RESEND_IGMP
:
3014 in_dev
= __in_dev_get_rtnl(dev
);
3016 ip_mc_rejoin_groups(in_dev
);
3024 static struct notifier_block igmp_notifier
= {
3025 .notifier_call
= igmp_netdev_event
,
3028 int __init
igmp_mc_init(void)
3030 #if defined(CONFIG_PROC_FS)
3033 err
= register_pernet_subsys(&igmp_net_ops
);
3036 err
= register_netdevice_notifier(&igmp_notifier
);
3038 goto reg_notif_fail
;
3042 unregister_pernet_subsys(&igmp_net_ops
);
3045 return register_netdevice_notifier(&igmp_notifier
);