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 <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/types.h>
78 #include <linux/kernel.h>
79 #include <linux/jiffies.h>
80 #include <linux/string.h>
81 #include <linux/socket.h>
82 #include <linux/sockios.h>
84 #include <linux/inet.h>
85 #include <linux/netdevice.h>
86 #include <linux/skbuff.h>
87 #include <linux/inetdevice.h>
88 #include <linux/igmp.h>
89 #include <linux/if_arp.h>
90 #include <linux/rtnetlink.h>
91 #include <linux/times.h>
93 #include <net/net_namespace.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
99 #include <net/checksum.h>
100 #include <linux/netfilter_ipv4.h>
101 #ifdef CONFIG_IP_MROUTE
102 #include <linux/mroute.h>
104 #ifdef CONFIG_PROC_FS
105 #include <linux/proc_fs.h>
106 #include <linux/seq_file.h>
109 #define IP_MAX_MEMBERSHIPS 20
110 #define IP_MAX_MSF 10
112 #ifdef CONFIG_IP_MULTICAST
113 /* Parameter names and values are taken from igmp-v2-06 draft */
115 #define IGMP_V1_Router_Present_Timeout (400*HZ)
116 #define IGMP_V2_Router_Present_Timeout (400*HZ)
117 #define IGMP_Unsolicited_Report_Interval (10*HZ)
118 #define IGMP_Query_Response_Interval (10*HZ)
119 #define IGMP_Unsolicited_Report_Count 2
122 #define IGMP_Initial_Report_Delay (1)
124 /* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
131 #define IGMP_V1_SEEN(in_dev) \
132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
142 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
);
143 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
);
144 static void igmpv3_clear_delrec(struct in_device
*in_dev
);
145 static int sf_setstate(struct ip_mc_list
*pmc
);
146 static void sf_markstate(struct ip_mc_list
*pmc
);
148 static void ip_mc_clear_src(struct ip_mc_list
*pmc
);
149 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
150 int sfcount
, __be32
*psfsrc
, int delta
);
152 static void ip_ma_put(struct ip_mc_list
*im
)
154 if (atomic_dec_and_test(&im
->refcnt
)) {
155 in_dev_put(im
->interface
);
160 #define for_each_pmc_rcu(in_dev, pmc) \
161 for (pmc = rcu_dereference(in_dev->mc_list); \
163 pmc = rcu_dereference(pmc->next_rcu))
165 #define for_each_pmc_rtnl(in_dev, pmc) \
166 for (pmc = rtnl_dereference(in_dev->mc_list); \
168 pmc = rtnl_dereference(pmc->next_rcu))
170 #ifdef CONFIG_IP_MULTICAST
176 static void igmp_stop_timer(struct ip_mc_list
*im
)
178 spin_lock_bh(&im
->lock
);
179 if (del_timer(&im
->timer
))
180 atomic_dec(&im
->refcnt
);
183 im
->unsolicit_count
= 0;
184 spin_unlock_bh(&im
->lock
);
187 /* It must be called with locked im->lock */
188 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
190 int tv
= net_random() % max_delay
;
193 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
194 atomic_inc(&im
->refcnt
);
197 static void igmp_gq_start_timer(struct in_device
*in_dev
)
199 int tv
= net_random() % in_dev
->mr_maxdelay
;
201 in_dev
->mr_gq_running
= 1;
202 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
206 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
208 int tv
= net_random() % delay
;
210 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
214 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
216 spin_lock_bh(&im
->lock
);
217 im
->unsolicit_count
= 0;
218 if (del_timer(&im
->timer
)) {
219 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
220 add_timer(&im
->timer
);
222 spin_unlock_bh(&im
->lock
);
225 atomic_dec(&im
->refcnt
);
227 igmp_start_timer(im
, max_delay
);
228 spin_unlock_bh(&im
->lock
);
233 * Send an IGMP report.
236 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
239 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
240 int gdeleted
, int sdeleted
)
243 case IGMPV3_MODE_IS_INCLUDE
:
244 case IGMPV3_MODE_IS_EXCLUDE
:
245 if (gdeleted
|| sdeleted
)
247 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
248 if (pmc
->sfmode
== MCAST_INCLUDE
)
250 /* don't include if this source is excluded
253 if (psf
->sf_count
[MCAST_INCLUDE
])
254 return type
== IGMPV3_MODE_IS_INCLUDE
;
255 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
256 psf
->sf_count
[MCAST_EXCLUDE
];
259 case IGMPV3_CHANGE_TO_INCLUDE
:
260 if (gdeleted
|| sdeleted
)
262 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
263 case IGMPV3_CHANGE_TO_EXCLUDE
:
264 if (gdeleted
|| sdeleted
)
266 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
267 psf
->sf_count
[MCAST_INCLUDE
])
269 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
270 psf
->sf_count
[MCAST_EXCLUDE
];
271 case IGMPV3_ALLOW_NEW_SOURCES
:
272 if (gdeleted
|| !psf
->sf_crcount
)
274 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
275 case IGMPV3_BLOCK_OLD_SOURCES
:
276 if (pmc
->sfmode
== MCAST_INCLUDE
)
277 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
278 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
284 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
286 struct ip_sf_list
*psf
;
289 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
290 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
297 #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
299 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, int size
)
304 struct igmpv3_report
*pig
;
305 struct net
*net
= dev_net(dev
);
308 skb
= alloc_skb(size
+ LL_ALLOCATED_SPACE(dev
),
309 GFP_ATOMIC
| __GFP_NOWARN
);
316 igmp_skb_size(skb
) = size
;
318 rt
= ip_route_output_ports(net
, NULL
, IGMPV3_ALL_MCR
, 0,
320 IPPROTO_IGMP
, 0, dev
->ifindex
);
325 if (rt
->rt_src
== 0) {
331 skb_dst_set(skb
, &rt
->dst
);
334 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
336 skb_reset_network_header(skb
);
338 skb_put(skb
, sizeof(struct iphdr
) + 4);
341 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
343 pip
->frag_off
= htons(IP_DF
);
345 pip
->daddr
= rt
->rt_dst
;
346 pip
->saddr
= rt
->rt_src
;
347 pip
->protocol
= IPPROTO_IGMP
;
348 pip
->tot_len
= 0; /* filled in later */
349 ip_select_ident(pip
, &rt
->dst
, NULL
);
350 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
351 ((u8
*)&pip
[1])[1] = 4;
352 ((u8
*)&pip
[1])[2] = 0;
353 ((u8
*)&pip
[1])[3] = 0;
355 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
356 skb_put(skb
, sizeof(*pig
));
357 pig
= igmpv3_report_hdr(skb
);
358 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
366 static int igmpv3_sendpack(struct sk_buff
*skb
)
368 struct igmphdr
*pig
= igmp_hdr(skb
);
369 const int igmplen
= skb
->tail
- skb
->transport_header
;
371 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
373 return ip_local_out(skb
);
376 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
378 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
381 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
382 int type
, struct igmpv3_grec
**ppgr
)
384 struct net_device
*dev
= pmc
->interface
->dev
;
385 struct igmpv3_report
*pih
;
386 struct igmpv3_grec
*pgr
;
389 skb
= igmpv3_newpack(dev
, dev
->mtu
);
392 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
393 pgr
->grec_type
= type
;
394 pgr
->grec_auxwords
= 0;
396 pgr
->grec_mca
= pmc
->multiaddr
;
397 pih
= igmpv3_report_hdr(skb
);
398 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
403 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
404 skb_tailroom(skb)) : 0)
406 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
407 int type
, int gdeleted
, int sdeleted
)
409 struct net_device
*dev
= pmc
->interface
->dev
;
410 struct igmpv3_report
*pih
;
411 struct igmpv3_grec
*pgr
= NULL
;
412 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
413 int scount
, stotal
, first
, isquery
, truncate
;
415 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
418 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
419 type
== IGMPV3_MODE_IS_EXCLUDE
;
420 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
421 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
425 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
430 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
432 /* EX and TO_EX get a fresh packet, if needed */
434 if (pih
&& pih
->ngrec
&&
435 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
437 igmpv3_sendpack(skb
);
438 skb
= igmpv3_newpack(dev
, dev
->mtu
);
443 for (psf
=*psf_list
; psf
; psf
=psf_next
) {
446 psf_next
= psf
->sf_next
;
448 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
453 /* clear marks on query responses */
457 if (AVAILABLE(skb
) < sizeof(__be32
) +
458 first
*sizeof(struct igmpv3_grec
)) {
459 if (truncate
&& !first
)
460 break; /* truncate these */
462 pgr
->grec_nsrcs
= htons(scount
);
464 igmpv3_sendpack(skb
);
465 skb
= igmpv3_newpack(dev
, dev
->mtu
);
470 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
475 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
476 *psrc
= psf
->sf_inaddr
;
478 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
479 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
481 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
483 psf_prev
->sf_next
= psf
->sf_next
;
485 *psf_list
= psf
->sf_next
;
495 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
496 type
== IGMPV3_BLOCK_OLD_SOURCES
)
498 if (pmc
->crcount
|| isquery
) {
499 /* make sure we have room for group header */
500 if (skb
&& AVAILABLE(skb
)<sizeof(struct igmpv3_grec
)) {
501 igmpv3_sendpack(skb
);
502 skb
= NULL
; /* add_grhead will get a new one */
504 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
508 pgr
->grec_nsrcs
= htons(scount
);
511 pmc
->gsquery
= 0; /* clear query state on report */
515 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
517 struct sk_buff
*skb
= NULL
;
522 for_each_pmc_rcu(in_dev
, pmc
) {
523 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
525 spin_lock_bh(&pmc
->lock
);
526 if (pmc
->sfcount
[MCAST_EXCLUDE
])
527 type
= IGMPV3_MODE_IS_EXCLUDE
;
529 type
= IGMPV3_MODE_IS_INCLUDE
;
530 skb
= add_grec(skb
, pmc
, type
, 0, 0);
531 spin_unlock_bh(&pmc
->lock
);
535 spin_lock_bh(&pmc
->lock
);
536 if (pmc
->sfcount
[MCAST_EXCLUDE
])
537 type
= IGMPV3_MODE_IS_EXCLUDE
;
539 type
= IGMPV3_MODE_IS_INCLUDE
;
540 skb
= add_grec(skb
, pmc
, type
, 0, 0);
541 spin_unlock_bh(&pmc
->lock
);
545 return igmpv3_sendpack(skb
);
549 * remove zero-count source records from a source filter list
551 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
553 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
556 for (psf
=*ppsf
; psf
; psf
= psf_next
) {
557 psf_next
= psf
->sf_next
;
558 if (psf
->sf_crcount
== 0) {
560 psf_prev
->sf_next
= psf
->sf_next
;
562 *ppsf
= psf
->sf_next
;
569 static void igmpv3_send_cr(struct in_device
*in_dev
)
571 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
572 struct sk_buff
*skb
= NULL
;
576 spin_lock_bh(&in_dev
->mc_tomb_lock
);
580 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc_next
) {
581 pmc_next
= pmc
->next
;
582 if (pmc
->sfmode
== MCAST_INCLUDE
) {
583 type
= IGMPV3_BLOCK_OLD_SOURCES
;
584 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
585 skb
= add_grec(skb
, pmc
, type
, 1, 0);
586 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
589 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
590 type
= IGMPV3_CHANGE_TO_INCLUDE
;
591 skb
= add_grec(skb
, pmc
, type
, 1, 0);
594 if (pmc
->crcount
== 0) {
595 igmpv3_clear_zeros(&pmc
->tomb
);
596 igmpv3_clear_zeros(&pmc
->sources
);
599 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
601 pmc_prev
->next
= pmc_next
;
603 in_dev
->mc_tomb
= pmc_next
;
604 in_dev_put(pmc
->interface
);
609 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
612 for_each_pmc_rcu(in_dev
, pmc
) {
613 spin_lock_bh(&pmc
->lock
);
614 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
615 type
= IGMPV3_BLOCK_OLD_SOURCES
;
616 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
618 type
= IGMPV3_ALLOW_NEW_SOURCES
;
619 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
621 skb
= add_grec(skb
, pmc
, type
, 0, 0);
622 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
624 /* filter mode changes */
626 if (pmc
->sfmode
== MCAST_EXCLUDE
)
627 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
629 type
= IGMPV3_CHANGE_TO_INCLUDE
;
630 skb
= add_grec(skb
, pmc
, type
, 0, 0);
633 spin_unlock_bh(&pmc
->lock
);
639 (void) igmpv3_sendpack(skb
);
642 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
649 struct net_device
*dev
= in_dev
->dev
;
650 struct net
*net
= dev_net(dev
);
651 __be32 group
= pmc
? pmc
->multiaddr
: 0;
654 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
655 return igmpv3_send_report(in_dev
, pmc
);
656 else if (type
== IGMP_HOST_LEAVE_MESSAGE
)
657 dst
= IGMP_ALL_ROUTER
;
661 rt
= ip_route_output_ports(net
, NULL
, dst
, 0,
663 IPPROTO_IGMP
, 0, dev
->ifindex
);
667 if (rt
->rt_src
== 0) {
672 skb
= alloc_skb(IGMP_SIZE
+LL_ALLOCATED_SPACE(dev
), GFP_ATOMIC
);
678 skb_dst_set(skb
, &rt
->dst
);
680 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
682 skb_reset_network_header(skb
);
684 skb_put(skb
, sizeof(struct iphdr
) + 4);
687 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
689 iph
->frag_off
= htons(IP_DF
);
692 iph
->saddr
= rt
->rt_src
;
693 iph
->protocol
= IPPROTO_IGMP
;
694 ip_select_ident(iph
, &rt
->dst
, NULL
);
695 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
696 ((u8
*)&iph
[1])[1] = 4;
697 ((u8
*)&iph
[1])[2] = 0;
698 ((u8
*)&iph
[1])[3] = 0;
700 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
705 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
707 return ip_local_out(skb
);
710 static void igmp_gq_timer_expire(unsigned long data
)
712 struct in_device
*in_dev
= (struct in_device
*)data
;
714 in_dev
->mr_gq_running
= 0;
715 igmpv3_send_report(in_dev
, NULL
);
716 __in_dev_put(in_dev
);
719 static void igmp_ifc_timer_expire(unsigned long data
)
721 struct in_device
*in_dev
= (struct in_device
*)data
;
723 igmpv3_send_cr(in_dev
);
724 if (in_dev
->mr_ifc_count
) {
725 in_dev
->mr_ifc_count
--;
726 igmp_ifc_start_timer(in_dev
, IGMP_Unsolicited_Report_Interval
);
728 __in_dev_put(in_dev
);
731 static void igmp_ifc_event(struct in_device
*in_dev
)
733 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
735 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
736 IGMP_Unsolicited_Report_Count
;
737 igmp_ifc_start_timer(in_dev
, 1);
741 static void igmp_timer_expire(unsigned long data
)
743 struct ip_mc_list
*im
=(struct ip_mc_list
*)data
;
744 struct in_device
*in_dev
= im
->interface
;
746 spin_lock(&im
->lock
);
749 if (im
->unsolicit_count
) {
750 im
->unsolicit_count
--;
751 igmp_start_timer(im
, IGMP_Unsolicited_Report_Interval
);
754 spin_unlock(&im
->lock
);
756 if (IGMP_V1_SEEN(in_dev
))
757 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
758 else if (IGMP_V2_SEEN(in_dev
))
759 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
761 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
766 /* mark EXCLUDE-mode sources */
767 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
769 struct ip_sf_list
*psf
;
773 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
776 for (i
=0; i
<nsrcs
; i
++) {
777 /* skip inactive filters */
778 if (pmc
->sfcount
[MCAST_INCLUDE
] ||
779 pmc
->sfcount
[MCAST_EXCLUDE
] !=
780 psf
->sf_count
[MCAST_EXCLUDE
])
782 if (srcs
[i
] == psf
->sf_inaddr
) {
789 if (scount
== nsrcs
) /* all sources excluded */
794 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
796 struct ip_sf_list
*psf
;
799 if (pmc
->sfmode
== MCAST_EXCLUDE
)
800 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
802 /* mark INCLUDE-mode sources */
804 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
807 for (i
=0; i
<nsrcs
; i
++)
808 if (srcs
[i
] == psf
->sf_inaddr
) {
822 static void igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
824 struct ip_mc_list
*im
;
826 /* Timers are only set for non-local groups */
828 if (group
== IGMP_ALL_HOSTS
)
832 for_each_pmc_rcu(in_dev
, im
) {
833 if (im
->multiaddr
== group
) {
841 static void igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
844 struct igmphdr
*ih
= igmp_hdr(skb
);
845 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
846 struct ip_mc_list
*im
;
847 __be32 group
= ih
->group
;
854 /* Alas, old v1 router presents here. */
856 max_delay
= IGMP_Query_Response_Interval
;
857 in_dev
->mr_v1_seen
= jiffies
+
858 IGMP_V1_Router_Present_Timeout
;
861 /* v2 router present */
862 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
863 in_dev
->mr_v2_seen
= jiffies
+
864 IGMP_V2_Router_Present_Timeout
;
866 /* cancel the interface change timer */
867 in_dev
->mr_ifc_count
= 0;
868 if (del_timer(&in_dev
->mr_ifc_timer
))
869 __in_dev_put(in_dev
);
870 /* clear deleted report items */
871 igmpv3_clear_delrec(in_dev
);
872 } else if (len
< 12) {
873 return; /* ignore bogus packet; freed by caller */
874 } else if (IGMP_V1_SEEN(in_dev
)) {
875 /* This is a v3 query with v1 queriers present */
876 max_delay
= IGMP_Query_Response_Interval
;
878 } else if (IGMP_V2_SEEN(in_dev
)) {
879 /* this is a v3 query with v2 queriers present;
880 * Interpretation of the max_delay code is problematic here.
881 * A real v2 host would use ih_code directly, while v3 has a
882 * different encoding. We use the v3 encoding as more likely
883 * to be intended in a v3 query.
885 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
887 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
890 ih3
= igmpv3_query_hdr(skb
);
892 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
893 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
895 ih3
= igmpv3_query_hdr(skb
);
898 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
900 max_delay
= 1; /* can't mod w/ 0 */
901 in_dev
->mr_maxdelay
= max_delay
;
903 in_dev
->mr_qrv
= ih3
->qrv
;
904 if (!group
) { /* general query */
906 return; /* no sources allowed */
907 igmp_gq_start_timer(in_dev
);
910 /* mark sources to include, if group & source-specific */
911 mark
= ih3
->nsrcs
!= 0;
915 * - Start the timers in all of our membership records
916 * that the query applies to for the interface on
917 * which the query arrived excl. those that belong
918 * to a "local" group (224.0.0.X)
919 * - For timers already running check if they need to
921 * - Use the igmp->igmp_code field as the maximum
925 for_each_pmc_rcu(in_dev
, im
) {
928 if (group
&& group
!= im
->multiaddr
)
930 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
932 spin_lock_bh(&im
->lock
);
934 im
->gsquery
= im
->gsquery
&& mark
;
937 changed
= !im
->gsquery
||
938 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
939 spin_unlock_bh(&im
->lock
);
941 igmp_mod_timer(im
, max_delay
);
946 /* called in rcu_read_lock() section */
947 int igmp_rcv(struct sk_buff
*skb
)
949 /* This basically follows the spec line by line -- see RFC1112 */
951 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
957 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
960 switch (skb
->ip_summed
) {
961 case CHECKSUM_COMPLETE
:
962 if (!csum_fold(skb
->csum
))
967 if (__skb_checksum_complete(skb
))
973 case IGMP_HOST_MEMBERSHIP_QUERY
:
974 igmp_heard_query(in_dev
, skb
, len
);
976 case IGMP_HOST_MEMBERSHIP_REPORT
:
977 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
978 /* Is it our report looped back? */
979 if (rt_is_output_route(skb_rtable(skb
)))
981 /* don't rely on MC router hearing unicast reports */
982 if (skb
->pkt_type
== PACKET_MULTICAST
||
983 skb
->pkt_type
== PACKET_BROADCAST
)
984 igmp_heard_report(in_dev
, ih
->group
);
987 #ifdef CONFIG_IP_PIMSM_V1
988 return pim_rcv_v1(skb
);
990 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
993 case IGMP_HOST_LEAVE_MESSAGE
:
995 case IGMP_MTRACE_RESP
:
1010 * Add a filter to a device
1013 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1015 char buf
[MAX_ADDR_LEN
];
1016 struct net_device
*dev
= in_dev
->dev
;
1018 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1019 We will get multicast token leakage, when IFF_MULTICAST
1020 is changed. This check should be done in dev->set_multicast_list
1021 routine. Something sort of:
1022 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1025 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1026 dev_mc_add(dev
, buf
);
1030 * Remove a filter from a device
1033 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1035 char buf
[MAX_ADDR_LEN
];
1036 struct net_device
*dev
= in_dev
->dev
;
1038 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1039 dev_mc_del(dev
, buf
);
1042 #ifdef CONFIG_IP_MULTICAST
1044 * deleted ip_mc_list manipulation
1046 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1048 struct ip_mc_list
*pmc
;
1050 /* this is an "ip_mc_list" for convenience; only the fields below
1051 * are actually used. In particular, the refcnt and users are not
1052 * used for management of the delete list. Using the same structure
1053 * for deleted items allows change reports to use common code with
1054 * non-deleted or query-response MCA's.
1056 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1059 spin_lock_bh(&im
->lock
);
1060 pmc
->interface
= im
->interface
;
1061 in_dev_hold(in_dev
);
1062 pmc
->multiaddr
= im
->multiaddr
;
1063 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1064 IGMP_Unsolicited_Report_Count
;
1065 pmc
->sfmode
= im
->sfmode
;
1066 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1067 struct ip_sf_list
*psf
;
1069 pmc
->tomb
= im
->tomb
;
1070 pmc
->sources
= im
->sources
;
1071 im
->tomb
= im
->sources
= NULL
;
1072 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1073 psf
->sf_crcount
= pmc
->crcount
;
1075 spin_unlock_bh(&im
->lock
);
1077 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1078 pmc
->next
= in_dev
->mc_tomb
;
1079 in_dev
->mc_tomb
= pmc
;
1080 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1083 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1085 struct ip_mc_list
*pmc
, *pmc_prev
;
1086 struct ip_sf_list
*psf
, *psf_next
;
1088 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1090 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
1091 if (pmc
->multiaddr
== multiaddr
)
1097 pmc_prev
->next
= pmc
->next
;
1099 in_dev
->mc_tomb
= pmc
->next
;
1101 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1103 for (psf
=pmc
->tomb
; psf
; psf
=psf_next
) {
1104 psf_next
= psf
->sf_next
;
1107 in_dev_put(pmc
->interface
);
1112 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1114 struct ip_mc_list
*pmc
, *nextpmc
;
1116 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1117 pmc
= in_dev
->mc_tomb
;
1118 in_dev
->mc_tomb
= NULL
;
1119 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1121 for (; pmc
; pmc
= nextpmc
) {
1122 nextpmc
= pmc
->next
;
1123 ip_mc_clear_src(pmc
);
1124 in_dev_put(pmc
->interface
);
1127 /* clear dead sources, too */
1129 for_each_pmc_rcu(in_dev
, pmc
) {
1130 struct ip_sf_list
*psf
, *psf_next
;
1132 spin_lock_bh(&pmc
->lock
);
1135 spin_unlock_bh(&pmc
->lock
);
1136 for (; psf
; psf
=psf_next
) {
1137 psf_next
= psf
->sf_next
;
1145 static void igmp_group_dropped(struct ip_mc_list
*im
)
1147 struct in_device
*in_dev
= im
->interface
;
1148 #ifdef CONFIG_IP_MULTICAST
1154 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1157 #ifdef CONFIG_IP_MULTICAST
1158 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1161 reporter
= im
->reporter
;
1162 igmp_stop_timer(im
);
1164 if (!in_dev
->dead
) {
1165 if (IGMP_V1_SEEN(in_dev
))
1167 if (IGMP_V2_SEEN(in_dev
)) {
1169 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1173 igmpv3_add_delrec(in_dev
, im
);
1175 igmp_ifc_event(in_dev
);
1179 ip_mc_clear_src(im
);
1182 static void igmp_group_added(struct ip_mc_list
*im
)
1184 struct in_device
*in_dev
= im
->interface
;
1186 if (im
->loaded
== 0) {
1188 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1191 #ifdef CONFIG_IP_MULTICAST
1192 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1197 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1198 spin_lock_bh(&im
->lock
);
1199 igmp_start_timer(im
, IGMP_Initial_Report_Delay
);
1200 spin_unlock_bh(&im
->lock
);
1205 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1206 IGMP_Unsolicited_Report_Count
;
1207 igmp_ifc_event(in_dev
);
1213 * Multicast list managers
1218 * A socket has joined a multicast group on device dev.
1221 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1223 struct ip_mc_list
*im
;
1227 for_each_pmc_rtnl(in_dev
, im
) {
1228 if (im
->multiaddr
== addr
) {
1230 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1235 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1240 im
->interface
= in_dev
;
1241 in_dev_hold(in_dev
);
1242 im
->multiaddr
= addr
;
1243 /* initial mode is (EX, empty) */
1244 im
->sfmode
= MCAST_EXCLUDE
;
1245 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1246 atomic_set(&im
->refcnt
, 1);
1247 spin_lock_init(&im
->lock
);
1248 #ifdef CONFIG_IP_MULTICAST
1249 setup_timer(&im
->timer
, &igmp_timer_expire
, (unsigned long)im
);
1250 im
->unsolicit_count
= IGMP_Unsolicited_Report_Count
;
1253 im
->next_rcu
= in_dev
->mc_list
;
1255 rcu_assign_pointer(in_dev
->mc_list
, im
);
1257 #ifdef CONFIG_IP_MULTICAST
1258 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1260 igmp_group_added(im
);
1262 ip_rt_multicast_event(in_dev
);
1266 EXPORT_SYMBOL(ip_mc_inc_group
);
1269 * Resend IGMP JOIN report; used for bonding.
1270 * Called with rcu_read_lock()
1272 void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1274 #ifdef CONFIG_IP_MULTICAST
1275 struct ip_mc_list
*im
;
1278 for_each_pmc_rcu(in_dev
, im
) {
1279 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1282 /* a failover is happening and switches
1283 * must be notified immediately
1285 if (IGMP_V1_SEEN(in_dev
))
1286 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1287 else if (IGMP_V2_SEEN(in_dev
))
1288 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1290 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1291 igmp_send_report(in_dev
, im
, type
);
1295 EXPORT_SYMBOL(ip_mc_rejoin_groups
);
1298 * A socket has left a multicast group on device dev
1301 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1303 struct ip_mc_list
*i
;
1304 struct ip_mc_list __rcu
**ip
;
1308 for (ip
= &in_dev
->mc_list
;
1309 (i
= rtnl_dereference(*ip
)) != NULL
;
1310 ip
= &i
->next_rcu
) {
1311 if (i
->multiaddr
== addr
) {
1312 if (--i
->users
== 0) {
1315 igmp_group_dropped(i
);
1318 ip_rt_multicast_event(in_dev
);
1327 EXPORT_SYMBOL(ip_mc_dec_group
);
1329 /* Device changing type */
1331 void ip_mc_unmap(struct in_device
*in_dev
)
1333 struct ip_mc_list
*pmc
;
1337 for_each_pmc_rtnl(in_dev
, pmc
)
1338 igmp_group_dropped(pmc
);
1341 void ip_mc_remap(struct in_device
*in_dev
)
1343 struct ip_mc_list
*pmc
;
1347 for_each_pmc_rtnl(in_dev
, pmc
)
1348 igmp_group_added(pmc
);
1351 /* Device going down */
1353 void ip_mc_down(struct in_device
*in_dev
)
1355 struct ip_mc_list
*pmc
;
1359 for_each_pmc_rtnl(in_dev
, pmc
)
1360 igmp_group_dropped(pmc
);
1362 #ifdef CONFIG_IP_MULTICAST
1363 in_dev
->mr_ifc_count
= 0;
1364 if (del_timer(&in_dev
->mr_ifc_timer
))
1365 __in_dev_put(in_dev
);
1366 in_dev
->mr_gq_running
= 0;
1367 if (del_timer(&in_dev
->mr_gq_timer
))
1368 __in_dev_put(in_dev
);
1369 igmpv3_clear_delrec(in_dev
);
1372 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1375 void ip_mc_init_dev(struct in_device
*in_dev
)
1379 in_dev
->mc_tomb
= NULL
;
1380 #ifdef CONFIG_IP_MULTICAST
1381 in_dev
->mr_gq_running
= 0;
1382 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1383 (unsigned long)in_dev
);
1384 in_dev
->mr_ifc_count
= 0;
1385 in_dev
->mc_count
= 0;
1386 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1387 (unsigned long)in_dev
);
1388 in_dev
->mr_qrv
= IGMP_Unsolicited_Report_Count
;
1391 spin_lock_init(&in_dev
->mc_tomb_lock
);
1394 /* Device going up */
1396 void ip_mc_up(struct in_device
*in_dev
)
1398 struct ip_mc_list
*pmc
;
1402 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1404 for_each_pmc_rtnl(in_dev
, pmc
)
1405 igmp_group_added(pmc
);
1409 * Device is about to be destroyed: clean up.
1412 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1414 struct ip_mc_list
*i
;
1418 /* Deactivate timers */
1421 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1422 in_dev
->mc_list
= i
->next_rcu
;
1425 igmp_group_dropped(i
);
1430 /* RTNL is locked */
1431 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1433 struct net_device
*dev
= NULL
;
1434 struct in_device
*idev
= NULL
;
1436 if (imr
->imr_ifindex
) {
1437 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1440 if (imr
->imr_address
.s_addr
) {
1441 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1447 struct rtable
*rt
= ip_route_output(net
,
1448 imr
->imr_multiaddr
.s_addr
,
1456 imr
->imr_ifindex
= dev
->ifindex
;
1457 idev
= __in_dev_get_rtnl(dev
);
1463 * Join a socket to a group
1465 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1466 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1469 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1472 struct ip_sf_list
*psf
, *psf_prev
;
1476 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1477 if (psf
->sf_inaddr
== *psfsrc
)
1481 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1482 /* source filter not found, or count wrong => bug */
1485 psf
->sf_count
[sfmode
]--;
1486 if (psf
->sf_count
[sfmode
] == 0) {
1487 ip_rt_multicast_event(pmc
->interface
);
1489 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1490 #ifdef CONFIG_IP_MULTICAST
1491 struct in_device
*in_dev
= pmc
->interface
;
1494 /* no more filters for this source */
1496 psf_prev
->sf_next
= psf
->sf_next
;
1498 pmc
->sources
= psf
->sf_next
;
1499 #ifdef CONFIG_IP_MULTICAST
1500 if (psf
->sf_oldin
&&
1501 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1502 psf
->sf_crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1503 IGMP_Unsolicited_Report_Count
;
1504 psf
->sf_next
= pmc
->tomb
;
1514 #ifndef CONFIG_IP_MULTICAST
1515 #define igmp_ifc_event(x) do { } while (0)
1518 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1519 int sfcount
, __be32
*psfsrc
, int delta
)
1521 struct ip_mc_list
*pmc
;
1528 for_each_pmc_rcu(in_dev
, pmc
) {
1529 if (*pmca
== pmc
->multiaddr
)
1533 /* MCA not found?? bug */
1537 spin_lock_bh(&pmc
->lock
);
1539 #ifdef CONFIG_IP_MULTICAST
1544 if (!pmc
->sfcount
[sfmode
])
1546 pmc
->sfcount
[sfmode
]--;
1549 for (i
=0; i
<sfcount
; i
++) {
1550 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1552 changerec
|= rv
> 0;
1556 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1557 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1558 pmc
->sfcount
[MCAST_INCLUDE
]) {
1559 #ifdef CONFIG_IP_MULTICAST
1560 struct ip_sf_list
*psf
;
1563 /* filter mode change */
1564 pmc
->sfmode
= MCAST_INCLUDE
;
1565 #ifdef CONFIG_IP_MULTICAST
1566 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1567 IGMP_Unsolicited_Report_Count
;
1568 in_dev
->mr_ifc_count
= pmc
->crcount
;
1569 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1570 psf
->sf_crcount
= 0;
1571 igmp_ifc_event(pmc
->interface
);
1572 } else if (sf_setstate(pmc
) || changerec
) {
1573 igmp_ifc_event(pmc
->interface
);
1577 spin_unlock_bh(&pmc
->lock
);
1582 * Add multicast single-source filter to the interface list
1584 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1585 __be32
*psfsrc
, int delta
)
1587 struct ip_sf_list
*psf
, *psf_prev
;
1590 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1591 if (psf
->sf_inaddr
== *psfsrc
)
1596 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1599 psf
->sf_inaddr
= *psfsrc
;
1601 psf_prev
->sf_next
= psf
;
1605 psf
->sf_count
[sfmode
]++;
1606 if (psf
->sf_count
[sfmode
] == 1) {
1607 ip_rt_multicast_event(pmc
->interface
);
1612 #ifdef CONFIG_IP_MULTICAST
1613 static void sf_markstate(struct ip_mc_list
*pmc
)
1615 struct ip_sf_list
*psf
;
1616 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1618 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1619 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1620 psf
->sf_oldin
= mca_xcount
==
1621 psf
->sf_count
[MCAST_EXCLUDE
] &&
1622 !psf
->sf_count
[MCAST_INCLUDE
];
1624 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1627 static int sf_setstate(struct ip_mc_list
*pmc
)
1629 struct ip_sf_list
*psf
, *dpsf
;
1630 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1631 int qrv
= pmc
->interface
->mr_qrv
;
1635 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1636 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1637 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1638 !psf
->sf_count
[MCAST_INCLUDE
];
1640 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1642 if (!psf
->sf_oldin
) {
1643 struct ip_sf_list
*prev
= NULL
;
1645 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
) {
1646 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1652 prev
->sf_next
= dpsf
->sf_next
;
1654 pmc
->tomb
= dpsf
->sf_next
;
1657 psf
->sf_crcount
= qrv
;
1660 } else if (psf
->sf_oldin
) {
1662 psf
->sf_crcount
= 0;
1664 * add or update "delete" records if an active filter
1667 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
)
1668 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1671 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1675 /* pmc->lock held by callers */
1676 dpsf
->sf_next
= pmc
->tomb
;
1679 dpsf
->sf_crcount
= qrv
;
1688 * Add multicast source filter list to the interface list
1690 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1691 int sfcount
, __be32
*psfsrc
, int delta
)
1693 struct ip_mc_list
*pmc
;
1700 for_each_pmc_rcu(in_dev
, pmc
) {
1701 if (*pmca
== pmc
->multiaddr
)
1705 /* MCA not found?? bug */
1709 spin_lock_bh(&pmc
->lock
);
1712 #ifdef CONFIG_IP_MULTICAST
1715 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1717 pmc
->sfcount
[sfmode
]++;
1719 for (i
=0; i
<sfcount
; i
++) {
1720 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1727 pmc
->sfcount
[sfmode
]--;
1729 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1730 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1731 #ifdef CONFIG_IP_MULTICAST
1732 struct ip_sf_list
*psf
;
1733 in_dev
= pmc
->interface
;
1736 /* filter mode change */
1737 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1738 pmc
->sfmode
= MCAST_EXCLUDE
;
1739 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1740 pmc
->sfmode
= MCAST_INCLUDE
;
1741 #ifdef CONFIG_IP_MULTICAST
1742 /* else no filters; keep old mode for reports */
1744 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1745 IGMP_Unsolicited_Report_Count
;
1746 in_dev
->mr_ifc_count
= pmc
->crcount
;
1747 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1748 psf
->sf_crcount
= 0;
1749 igmp_ifc_event(in_dev
);
1750 } else if (sf_setstate(pmc
)) {
1751 igmp_ifc_event(in_dev
);
1754 spin_unlock_bh(&pmc
->lock
);
1758 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1760 struct ip_sf_list
*psf
, *nextpsf
;
1762 for (psf
=pmc
->tomb
; psf
; psf
=nextpsf
) {
1763 nextpsf
= psf
->sf_next
;
1767 for (psf
=pmc
->sources
; psf
; psf
=nextpsf
) {
1768 nextpsf
= psf
->sf_next
;
1771 pmc
->sources
= NULL
;
1772 pmc
->sfmode
= MCAST_EXCLUDE
;
1773 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1774 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1779 * Join a multicast group
1781 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1784 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
1785 struct ip_mc_socklist
*iml
= NULL
, *i
;
1786 struct in_device
*in_dev
;
1787 struct inet_sock
*inet
= inet_sk(sk
);
1788 struct net
*net
= sock_net(sk
);
1792 if (!ipv4_is_multicast(addr
))
1797 in_dev
= ip_mc_find_dev(net
, imr
);
1806 ifindex
= imr
->imr_ifindex
;
1807 for_each_pmc_rtnl(inet
, i
) {
1808 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1809 i
->multi
.imr_ifindex
== ifindex
)
1814 if (count
>= sysctl_igmp_max_memberships
)
1816 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
1820 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1821 iml
->next_rcu
= inet
->mc_list
;
1823 iml
->sfmode
= MCAST_EXCLUDE
;
1824 rcu_assign_pointer(inet
->mc_list
, iml
);
1825 ip_mc_inc_group(in_dev
, addr
);
1831 EXPORT_SYMBOL(ip_mc_join_group
);
1833 static void ip_sf_socklist_reclaim(struct rcu_head
*rp
)
1835 kfree(container_of(rp
, struct ip_sf_socklist
, rcu
));
1836 /* sk_omem_alloc should have been decreased by the caller*/
1839 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1840 struct in_device
*in_dev
)
1842 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
1846 /* any-source empty exclude case */
1847 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1848 iml
->sfmode
, 0, NULL
, 0);
1850 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1851 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
1852 rcu_assign_pointer(iml
->sflist
, NULL
);
1853 /* decrease mem now to avoid the memleak warning */
1854 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
1855 call_rcu(&psf
->rcu
, ip_sf_socklist_reclaim
);
1860 static void ip_mc_socklist_reclaim(struct rcu_head
*rp
)
1862 kfree(container_of(rp
, struct ip_mc_socklist
, rcu
));
1863 /* sk_omem_alloc should have been decreased by the caller*/
1868 * Ask a socket to leave a group.
1871 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1873 struct inet_sock
*inet
= inet_sk(sk
);
1874 struct ip_mc_socklist
*iml
;
1875 struct ip_mc_socklist __rcu
**imlp
;
1876 struct in_device
*in_dev
;
1877 struct net
*net
= sock_net(sk
);
1878 __be32 group
= imr
->imr_multiaddr
.s_addr
;
1880 int ret
= -EADDRNOTAVAIL
;
1883 in_dev
= ip_mc_find_dev(net
, imr
);
1884 ifindex
= imr
->imr_ifindex
;
1885 for (imlp
= &inet
->mc_list
;
1886 (iml
= rtnl_dereference(*imlp
)) != NULL
;
1887 imlp
= &iml
->next_rcu
) {
1888 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
1891 if (iml
->multi
.imr_ifindex
!= ifindex
)
1893 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
1894 iml
->multi
.imr_address
.s_addr
)
1897 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1899 *imlp
= iml
->next_rcu
;
1902 ip_mc_dec_group(in_dev
, group
);
1904 /* decrease mem now to avoid the memleak warning */
1905 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
1906 call_rcu(&iml
->rcu
, ip_mc_socklist_reclaim
);
1915 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1916 ip_mreq_source
*mreqs
, int ifindex
)
1919 struct ip_mreqn imr
;
1920 __be32 addr
= mreqs
->imr_multiaddr
;
1921 struct ip_mc_socklist
*pmc
;
1922 struct in_device
*in_dev
= NULL
;
1923 struct inet_sock
*inet
= inet_sk(sk
);
1924 struct ip_sf_socklist
*psl
;
1925 struct net
*net
= sock_net(sk
);
1929 if (!ipv4_is_multicast(addr
))
1934 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1935 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1936 imr
.imr_ifindex
= ifindex
;
1937 in_dev
= ip_mc_find_dev(net
, &imr
);
1943 err
= -EADDRNOTAVAIL
;
1945 for_each_pmc_rtnl(inet
, pmc
) {
1946 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
1947 imr
.imr_multiaddr
.s_addr
) &&
1948 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
1951 if (!pmc
) { /* must have a prior join */
1955 /* if a source filter was set, must be the same mode as before */
1957 if (pmc
->sfmode
!= omode
) {
1961 } else if (pmc
->sfmode
!= omode
) {
1962 /* allow mode switches for empty-set filters */
1963 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
1964 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
1966 pmc
->sfmode
= omode
;
1969 psl
= rtnl_dereference(pmc
->sflist
);
1972 goto done
; /* err = -EADDRNOTAVAIL */
1974 for (i
=0; i
<psl
->sl_count
; i
++) {
1975 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1980 if (rv
) /* source not found */
1981 goto done
; /* err = -EADDRNOTAVAIL */
1983 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1984 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
1989 /* update the interface filter */
1990 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1991 &mreqs
->imr_sourceaddr
, 1);
1993 for (j
=i
+1; j
<psl
->sl_count
; j
++)
1994 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
1999 /* else, add a new source to the filter */
2001 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
2005 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2006 struct ip_sf_socklist
*newpsl
;
2007 int count
= IP_SFBLOCK
;
2010 count
+= psl
->sl_max
;
2011 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2016 newpsl
->sl_max
= count
;
2017 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2019 for (i
=0; i
<psl
->sl_count
; i
++)
2020 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2021 /* decrease mem now to avoid the memleak warning */
2022 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2023 call_rcu(&psl
->rcu
, ip_sf_socklist_reclaim
);
2025 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2028 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2029 for (i
=0; i
<psl
->sl_count
; i
++) {
2030 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2035 if (rv
== 0) /* address already there is an error */
2037 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
2038 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2039 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2042 /* update the interface list */
2043 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2044 &mreqs
->imr_sourceaddr
, 1);
2048 return ip_mc_leave_group(sk
, &imr
);
2052 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2055 struct ip_mreqn imr
;
2056 __be32 addr
= msf
->imsf_multiaddr
;
2057 struct ip_mc_socklist
*pmc
;
2058 struct in_device
*in_dev
;
2059 struct inet_sock
*inet
= inet_sk(sk
);
2060 struct ip_sf_socklist
*newpsl
, *psl
;
2061 struct net
*net
= sock_net(sk
);
2064 if (!ipv4_is_multicast(addr
))
2066 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2067 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2072 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2073 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2074 imr
.imr_ifindex
= ifindex
;
2075 in_dev
= ip_mc_find_dev(net
, &imr
);
2082 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2083 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2088 for_each_pmc_rtnl(inet
, pmc
) {
2089 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2090 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2093 if (!pmc
) { /* must have a prior join */
2097 if (msf
->imsf_numsrc
) {
2098 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2104 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2105 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2106 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2107 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2108 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2110 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2115 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2116 msf
->imsf_fmode
, 0, NULL
, 0);
2118 psl
= rtnl_dereference(pmc
->sflist
);
2120 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2121 psl
->sl_count
, psl
->sl_addr
, 0);
2122 /* decrease mem now to avoid the memleak warning */
2123 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2124 call_rcu(&psl
->rcu
, ip_sf_socklist_reclaim
);
2126 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2128 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2129 pmc
->sfmode
= msf
->imsf_fmode
;
2134 err
= ip_mc_leave_group(sk
, &imr
);
2138 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2139 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2141 int err
, len
, count
, copycount
;
2142 struct ip_mreqn imr
;
2143 __be32 addr
= msf
->imsf_multiaddr
;
2144 struct ip_mc_socklist
*pmc
;
2145 struct in_device
*in_dev
;
2146 struct inet_sock
*inet
= inet_sk(sk
);
2147 struct ip_sf_socklist
*psl
;
2148 struct net
*net
= sock_net(sk
);
2150 if (!ipv4_is_multicast(addr
))
2155 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2156 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2157 imr
.imr_ifindex
= 0;
2158 in_dev
= ip_mc_find_dev(net
, &imr
);
2164 err
= -EADDRNOTAVAIL
;
2166 for_each_pmc_rtnl(inet
, pmc
) {
2167 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2168 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2171 if (!pmc
) /* must have a prior join */
2173 msf
->imsf_fmode
= pmc
->sfmode
;
2174 psl
= rtnl_dereference(pmc
->sflist
);
2180 count
= psl
->sl_count
;
2182 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2183 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2184 msf
->imsf_numsrc
= count
;
2185 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2186 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2190 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2198 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2199 struct group_filter __user
*optval
, int __user
*optlen
)
2201 int err
, i
, count
, copycount
;
2202 struct sockaddr_in
*psin
;
2204 struct ip_mc_socklist
*pmc
;
2205 struct inet_sock
*inet
= inet_sk(sk
);
2206 struct ip_sf_socklist
*psl
;
2208 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2209 if (psin
->sin_family
!= AF_INET
)
2211 addr
= psin
->sin_addr
.s_addr
;
2212 if (!ipv4_is_multicast(addr
))
2217 err
= -EADDRNOTAVAIL
;
2219 for_each_pmc_rtnl(inet
, pmc
) {
2220 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2221 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2224 if (!pmc
) /* must have a prior join */
2226 gsf
->gf_fmode
= pmc
->sfmode
;
2227 psl
= rtnl_dereference(pmc
->sflist
);
2229 count
= psl
? psl
->sl_count
: 0;
2230 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2231 gsf
->gf_numsrc
= count
;
2232 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2233 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2236 for (i
=0; i
<copycount
; i
++) {
2237 struct sockaddr_storage ss
;
2239 psin
= (struct sockaddr_in
*)&ss
;
2240 memset(&ss
, 0, sizeof(ss
));
2241 psin
->sin_family
= AF_INET
;
2242 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2243 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2253 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2255 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2257 struct inet_sock
*inet
= inet_sk(sk
);
2258 struct ip_mc_socklist
*pmc
;
2259 struct ip_sf_socklist
*psl
;
2264 if (!ipv4_is_multicast(loc_addr
))
2268 for_each_pmc_rcu(inet
, pmc
) {
2269 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2270 pmc
->multi
.imr_ifindex
== dif
)
2276 psl
= rcu_dereference(pmc
->sflist
);
2277 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2281 for (i
=0; i
<psl
->sl_count
; i
++) {
2282 if (psl
->sl_addr
[i
] == rmt_addr
)
2286 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2288 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2298 * A socket is closing.
2301 void ip_mc_drop_socket(struct sock
*sk
)
2303 struct inet_sock
*inet
= inet_sk(sk
);
2304 struct ip_mc_socklist
*iml
;
2305 struct net
*net
= sock_net(sk
);
2307 if (inet
->mc_list
== NULL
)
2311 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2312 struct in_device
*in_dev
;
2314 inet
->mc_list
= iml
->next_rcu
;
2315 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2316 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2318 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2319 /* decrease mem now to avoid the memleak warning */
2320 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2321 call_rcu(&iml
->rcu
, ip_mc_socklist_reclaim
);
2326 /* called with rcu_read_lock() */
2327 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u16 proto
)
2329 struct ip_mc_list
*im
;
2330 struct ip_sf_list
*psf
;
2333 for_each_pmc_rcu(in_dev
, im
) {
2334 if (im
->multiaddr
== mc_addr
)
2337 if (im
&& proto
== IPPROTO_IGMP
) {
2341 for (psf
=im
->sources
; psf
; psf
=psf
->sf_next
) {
2342 if (psf
->sf_inaddr
== src_addr
)
2346 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2347 psf
->sf_count
[MCAST_EXCLUDE
] !=
2348 im
->sfcount
[MCAST_EXCLUDE
];
2350 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2352 rv
= 1; /* unspecified source; tentatively allow */
2357 #if defined(CONFIG_PROC_FS)
2358 struct igmp_mc_iter_state
{
2359 struct seq_net_private p
;
2360 struct net_device
*dev
;
2361 struct in_device
*in_dev
;
2364 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2366 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2368 struct net
*net
= seq_file_net(seq
);
2369 struct ip_mc_list
*im
= NULL
;
2370 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2372 state
->in_dev
= NULL
;
2373 for_each_netdev_rcu(net
, state
->dev
) {
2374 struct in_device
*in_dev
;
2376 in_dev
= __in_dev_get_rcu(state
->dev
);
2379 im
= rcu_dereference(in_dev
->mc_list
);
2381 state
->in_dev
= in_dev
;
2388 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2390 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2392 im
= rcu_dereference(im
->next_rcu
);
2394 state
->dev
= next_net_device_rcu(state
->dev
);
2396 state
->in_dev
= NULL
;
2399 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2402 im
= rcu_dereference(state
->in_dev
->mc_list
);
2407 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2409 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2411 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2413 return pos
? NULL
: im
;
2416 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2420 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2423 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2425 struct ip_mc_list
*im
;
2426 if (v
== SEQ_START_TOKEN
)
2427 im
= igmp_mc_get_first(seq
);
2429 im
= igmp_mc_get_next(seq
, v
);
2434 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2437 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2439 state
->in_dev
= NULL
;
2444 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2446 if (v
== SEQ_START_TOKEN
)
2448 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2450 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2451 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2453 #ifdef CONFIG_IP_MULTICAST
2454 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2455 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2461 if (rcu_dereference(state
->in_dev
->mc_list
) == im
) {
2462 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2463 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2467 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2468 im
->multiaddr
, im
->users
,
2469 im
->tm_running
, im
->tm_running
?
2470 jiffies_to_clock_t(im
->timer
.expires
-jiffies
) : 0,
2476 static const struct seq_operations igmp_mc_seq_ops
= {
2477 .start
= igmp_mc_seq_start
,
2478 .next
= igmp_mc_seq_next
,
2479 .stop
= igmp_mc_seq_stop
,
2480 .show
= igmp_mc_seq_show
,
2483 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2485 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2486 sizeof(struct igmp_mc_iter_state
));
2489 static const struct file_operations igmp_mc_seq_fops
= {
2490 .owner
= THIS_MODULE
,
2491 .open
= igmp_mc_seq_open
,
2493 .llseek
= seq_lseek
,
2494 .release
= seq_release_net
,
2497 struct igmp_mcf_iter_state
{
2498 struct seq_net_private p
;
2499 struct net_device
*dev
;
2500 struct in_device
*idev
;
2501 struct ip_mc_list
*im
;
2504 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2506 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2508 struct net
*net
= seq_file_net(seq
);
2509 struct ip_sf_list
*psf
= NULL
;
2510 struct ip_mc_list
*im
= NULL
;
2511 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2515 for_each_netdev_rcu(net
, state
->dev
) {
2516 struct in_device
*idev
;
2517 idev
= __in_dev_get_rcu(state
->dev
);
2518 if (unlikely(idev
== NULL
))
2520 im
= rcu_dereference(idev
->mc_list
);
2521 if (likely(im
!= NULL
)) {
2522 spin_lock_bh(&im
->lock
);
2524 if (likely(psf
!= NULL
)) {
2529 spin_unlock_bh(&im
->lock
);
2535 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2537 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2541 spin_unlock_bh(&state
->im
->lock
);
2542 state
->im
= state
->im
->next
;
2543 while (!state
->im
) {
2544 state
->dev
= next_net_device_rcu(state
->dev
);
2549 state
->idev
= __in_dev_get_rcu(state
->dev
);
2552 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2556 spin_lock_bh(&state
->im
->lock
);
2557 psf
= state
->im
->sources
;
2563 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2565 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2567 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2569 return pos
? NULL
: psf
;
2572 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2576 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2579 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2581 struct ip_sf_list
*psf
;
2582 if (v
== SEQ_START_TOKEN
)
2583 psf
= igmp_mcf_get_first(seq
);
2585 psf
= igmp_mcf_get_next(seq
, v
);
2590 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2593 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2594 if (likely(state
->im
!= NULL
)) {
2595 spin_unlock_bh(&state
->im
->lock
);
2603 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2605 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2606 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2608 if (v
== SEQ_START_TOKEN
) {
2611 "%10s %10s %6s %6s\n", "Idx",
2613 "SRC", "INC", "EXC");
2617 "0x%08x %6lu %6lu\n",
2618 state
->dev
->ifindex
, state
->dev
->name
,
2619 ntohl(state
->im
->multiaddr
),
2620 ntohl(psf
->sf_inaddr
),
2621 psf
->sf_count
[MCAST_INCLUDE
],
2622 psf
->sf_count
[MCAST_EXCLUDE
]);
2627 static const struct seq_operations igmp_mcf_seq_ops
= {
2628 .start
= igmp_mcf_seq_start
,
2629 .next
= igmp_mcf_seq_next
,
2630 .stop
= igmp_mcf_seq_stop
,
2631 .show
= igmp_mcf_seq_show
,
2634 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2636 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2637 sizeof(struct igmp_mcf_iter_state
));
2640 static const struct file_operations igmp_mcf_seq_fops
= {
2641 .owner
= THIS_MODULE
,
2642 .open
= igmp_mcf_seq_open
,
2644 .llseek
= seq_lseek
,
2645 .release
= seq_release_net
,
2648 static int __net_init
igmp_net_init(struct net
*net
)
2650 struct proc_dir_entry
*pde
;
2652 pde
= proc_net_fops_create(net
, "igmp", S_IRUGO
, &igmp_mc_seq_fops
);
2655 pde
= proc_net_fops_create(net
, "mcfilter", S_IRUGO
, &igmp_mcf_seq_fops
);
2661 proc_net_remove(net
, "igmp");
2666 static void __net_exit
igmp_net_exit(struct net
*net
)
2668 proc_net_remove(net
, "mcfilter");
2669 proc_net_remove(net
, "igmp");
2672 static struct pernet_operations igmp_net_ops
= {
2673 .init
= igmp_net_init
,
2674 .exit
= igmp_net_exit
,
2677 int __init
igmp_mc_proc_init(void)
2679 return register_pernet_subsys(&igmp_net_ops
);