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
);
153 static void ip_mc_list_reclaim(struct rcu_head
*head
)
155 kfree(container_of(head
, struct ip_mc_list
, rcu
));
158 static void ip_ma_put(struct ip_mc_list
*im
)
160 if (atomic_dec_and_test(&im
->refcnt
)) {
161 in_dev_put(im
->interface
);
162 call_rcu(&im
->rcu
, ip_mc_list_reclaim
);
166 #define for_each_pmc_rcu(in_dev, pmc) \
167 for (pmc = rcu_dereference(in_dev->mc_list); \
169 pmc = rcu_dereference(pmc->next_rcu))
171 #define for_each_pmc_rtnl(in_dev, pmc) \
172 for (pmc = rtnl_dereference(in_dev->mc_list); \
174 pmc = rtnl_dereference(pmc->next_rcu))
176 #ifdef CONFIG_IP_MULTICAST
182 static void igmp_stop_timer(struct ip_mc_list
*im
)
184 spin_lock_bh(&im
->lock
);
185 if (del_timer(&im
->timer
))
186 atomic_dec(&im
->refcnt
);
189 im
->unsolicit_count
= 0;
190 spin_unlock_bh(&im
->lock
);
193 /* It must be called with locked im->lock */
194 static void igmp_start_timer(struct ip_mc_list
*im
, int max_delay
)
196 int tv
= net_random() % max_delay
;
199 if (!mod_timer(&im
->timer
, jiffies
+tv
+2))
200 atomic_inc(&im
->refcnt
);
203 static void igmp_gq_start_timer(struct in_device
*in_dev
)
205 int tv
= net_random() % in_dev
->mr_maxdelay
;
207 in_dev
->mr_gq_running
= 1;
208 if (!mod_timer(&in_dev
->mr_gq_timer
, jiffies
+tv
+2))
212 static void igmp_ifc_start_timer(struct in_device
*in_dev
, int delay
)
214 int tv
= net_random() % delay
;
216 if (!mod_timer(&in_dev
->mr_ifc_timer
, jiffies
+tv
+2))
220 static void igmp_mod_timer(struct ip_mc_list
*im
, int max_delay
)
222 spin_lock_bh(&im
->lock
);
223 im
->unsolicit_count
= 0;
224 if (del_timer(&im
->timer
)) {
225 if ((long)(im
->timer
.expires
-jiffies
) < max_delay
) {
226 add_timer(&im
->timer
);
228 spin_unlock_bh(&im
->lock
);
231 atomic_dec(&im
->refcnt
);
233 igmp_start_timer(im
, max_delay
);
234 spin_unlock_bh(&im
->lock
);
239 * Send an IGMP report.
242 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
245 static int is_in(struct ip_mc_list
*pmc
, struct ip_sf_list
*psf
, int type
,
246 int gdeleted
, int sdeleted
)
249 case IGMPV3_MODE_IS_INCLUDE
:
250 case IGMPV3_MODE_IS_EXCLUDE
:
251 if (gdeleted
|| sdeleted
)
253 if (!(pmc
->gsquery
&& !psf
->sf_gsresp
)) {
254 if (pmc
->sfmode
== MCAST_INCLUDE
)
256 /* don't include if this source is excluded
259 if (psf
->sf_count
[MCAST_INCLUDE
])
260 return type
== IGMPV3_MODE_IS_INCLUDE
;
261 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
262 psf
->sf_count
[MCAST_EXCLUDE
];
265 case IGMPV3_CHANGE_TO_INCLUDE
:
266 if (gdeleted
|| sdeleted
)
268 return psf
->sf_count
[MCAST_INCLUDE
] != 0;
269 case IGMPV3_CHANGE_TO_EXCLUDE
:
270 if (gdeleted
|| sdeleted
)
272 if (pmc
->sfcount
[MCAST_EXCLUDE
] == 0 ||
273 psf
->sf_count
[MCAST_INCLUDE
])
275 return pmc
->sfcount
[MCAST_EXCLUDE
] ==
276 psf
->sf_count
[MCAST_EXCLUDE
];
277 case IGMPV3_ALLOW_NEW_SOURCES
:
278 if (gdeleted
|| !psf
->sf_crcount
)
280 return (pmc
->sfmode
== MCAST_INCLUDE
) ^ sdeleted
;
281 case IGMPV3_BLOCK_OLD_SOURCES
:
282 if (pmc
->sfmode
== MCAST_INCLUDE
)
283 return gdeleted
|| (psf
->sf_crcount
&& sdeleted
);
284 return psf
->sf_crcount
&& !gdeleted
&& !sdeleted
;
290 igmp_scount(struct ip_mc_list
*pmc
, int type
, int gdeleted
, int sdeleted
)
292 struct ip_sf_list
*psf
;
295 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
296 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
))
303 #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
305 static struct sk_buff
*igmpv3_newpack(struct net_device
*dev
, int size
)
310 struct igmpv3_report
*pig
;
311 struct net
*net
= dev_net(dev
);
314 skb
= alloc_skb(size
+ LL_ALLOCATED_SPACE(dev
),
315 GFP_ATOMIC
| __GFP_NOWARN
);
322 igmp_skb_size(skb
) = size
;
324 rt
= ip_route_output_ports(net
, NULL
, IGMPV3_ALL_MCR
, 0,
326 IPPROTO_IGMP
, 0, dev
->ifindex
);
331 if (rt
->rt_src
== 0) {
337 skb_dst_set(skb
, &rt
->dst
);
340 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
342 skb_reset_network_header(skb
);
344 skb_put(skb
, sizeof(struct iphdr
) + 4);
347 pip
->ihl
= (sizeof(struct iphdr
)+4)>>2;
349 pip
->frag_off
= htons(IP_DF
);
351 pip
->daddr
= rt
->rt_dst
;
352 pip
->saddr
= rt
->rt_src
;
353 pip
->protocol
= IPPROTO_IGMP
;
354 pip
->tot_len
= 0; /* filled in later */
355 ip_select_ident(pip
, &rt
->dst
, NULL
);
356 ((u8
*)&pip
[1])[0] = IPOPT_RA
;
357 ((u8
*)&pip
[1])[1] = 4;
358 ((u8
*)&pip
[1])[2] = 0;
359 ((u8
*)&pip
[1])[3] = 0;
361 skb
->transport_header
= skb
->network_header
+ sizeof(struct iphdr
) + 4;
362 skb_put(skb
, sizeof(*pig
));
363 pig
= igmpv3_report_hdr(skb
);
364 pig
->type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
372 static int igmpv3_sendpack(struct sk_buff
*skb
)
374 struct igmphdr
*pig
= igmp_hdr(skb
);
375 const int igmplen
= skb
->tail
- skb
->transport_header
;
377 pig
->csum
= ip_compute_csum(igmp_hdr(skb
), igmplen
);
379 return ip_local_out(skb
);
382 static int grec_size(struct ip_mc_list
*pmc
, int type
, int gdel
, int sdel
)
384 return sizeof(struct igmpv3_grec
) + 4*igmp_scount(pmc
, type
, gdel
, sdel
);
387 static struct sk_buff
*add_grhead(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
388 int type
, struct igmpv3_grec
**ppgr
)
390 struct net_device
*dev
= pmc
->interface
->dev
;
391 struct igmpv3_report
*pih
;
392 struct igmpv3_grec
*pgr
;
395 skb
= igmpv3_newpack(dev
, dev
->mtu
);
398 pgr
= (struct igmpv3_grec
*)skb_put(skb
, sizeof(struct igmpv3_grec
));
399 pgr
->grec_type
= type
;
400 pgr
->grec_auxwords
= 0;
402 pgr
->grec_mca
= pmc
->multiaddr
;
403 pih
= igmpv3_report_hdr(skb
);
404 pih
->ngrec
= htons(ntohs(pih
->ngrec
)+1);
409 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
410 skb_tailroom(skb)) : 0)
412 static struct sk_buff
*add_grec(struct sk_buff
*skb
, struct ip_mc_list
*pmc
,
413 int type
, int gdeleted
, int sdeleted
)
415 struct net_device
*dev
= pmc
->interface
->dev
;
416 struct igmpv3_report
*pih
;
417 struct igmpv3_grec
*pgr
= NULL
;
418 struct ip_sf_list
*psf
, *psf_next
, *psf_prev
, **psf_list
;
419 int scount
, stotal
, first
, isquery
, truncate
;
421 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
424 isquery
= type
== IGMPV3_MODE_IS_INCLUDE
||
425 type
== IGMPV3_MODE_IS_EXCLUDE
;
426 truncate
= type
== IGMPV3_MODE_IS_EXCLUDE
||
427 type
== IGMPV3_CHANGE_TO_EXCLUDE
;
431 psf_list
= sdeleted
? &pmc
->tomb
: &pmc
->sources
;
436 pih
= skb
? igmpv3_report_hdr(skb
) : NULL
;
438 /* EX and TO_EX get a fresh packet, if needed */
440 if (pih
&& pih
->ngrec
&&
441 AVAILABLE(skb
) < grec_size(pmc
, type
, gdeleted
, sdeleted
)) {
443 igmpv3_sendpack(skb
);
444 skb
= igmpv3_newpack(dev
, dev
->mtu
);
449 for (psf
=*psf_list
; psf
; psf
=psf_next
) {
452 psf_next
= psf
->sf_next
;
454 if (!is_in(pmc
, psf
, type
, gdeleted
, sdeleted
)) {
459 /* clear marks on query responses */
463 if (AVAILABLE(skb
) < sizeof(__be32
) +
464 first
*sizeof(struct igmpv3_grec
)) {
465 if (truncate
&& !first
)
466 break; /* truncate these */
468 pgr
->grec_nsrcs
= htons(scount
);
470 igmpv3_sendpack(skb
);
471 skb
= igmpv3_newpack(dev
, dev
->mtu
);
476 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
481 psrc
= (__be32
*)skb_put(skb
, sizeof(__be32
));
482 *psrc
= psf
->sf_inaddr
;
484 if ((type
== IGMPV3_ALLOW_NEW_SOURCES
||
485 type
== IGMPV3_BLOCK_OLD_SOURCES
) && psf
->sf_crcount
) {
487 if ((sdeleted
|| gdeleted
) && psf
->sf_crcount
== 0) {
489 psf_prev
->sf_next
= psf
->sf_next
;
491 *psf_list
= psf
->sf_next
;
501 if (type
== IGMPV3_ALLOW_NEW_SOURCES
||
502 type
== IGMPV3_BLOCK_OLD_SOURCES
)
504 if (pmc
->crcount
|| isquery
) {
505 /* make sure we have room for group header */
506 if (skb
&& AVAILABLE(skb
)<sizeof(struct igmpv3_grec
)) {
507 igmpv3_sendpack(skb
);
508 skb
= NULL
; /* add_grhead will get a new one */
510 skb
= add_grhead(skb
, pmc
, type
, &pgr
);
514 pgr
->grec_nsrcs
= htons(scount
);
517 pmc
->gsquery
= 0; /* clear query state on report */
521 static int igmpv3_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
)
523 struct sk_buff
*skb
= NULL
;
528 for_each_pmc_rcu(in_dev
, pmc
) {
529 if (pmc
->multiaddr
== IGMP_ALL_HOSTS
)
531 spin_lock_bh(&pmc
->lock
);
532 if (pmc
->sfcount
[MCAST_EXCLUDE
])
533 type
= IGMPV3_MODE_IS_EXCLUDE
;
535 type
= IGMPV3_MODE_IS_INCLUDE
;
536 skb
= add_grec(skb
, pmc
, type
, 0, 0);
537 spin_unlock_bh(&pmc
->lock
);
541 spin_lock_bh(&pmc
->lock
);
542 if (pmc
->sfcount
[MCAST_EXCLUDE
])
543 type
= IGMPV3_MODE_IS_EXCLUDE
;
545 type
= IGMPV3_MODE_IS_INCLUDE
;
546 skb
= add_grec(skb
, pmc
, type
, 0, 0);
547 spin_unlock_bh(&pmc
->lock
);
551 return igmpv3_sendpack(skb
);
555 * remove zero-count source records from a source filter list
557 static void igmpv3_clear_zeros(struct ip_sf_list
**ppsf
)
559 struct ip_sf_list
*psf_prev
, *psf_next
, *psf
;
562 for (psf
=*ppsf
; psf
; psf
= psf_next
) {
563 psf_next
= psf
->sf_next
;
564 if (psf
->sf_crcount
== 0) {
566 psf_prev
->sf_next
= psf
->sf_next
;
568 *ppsf
= psf
->sf_next
;
575 static void igmpv3_send_cr(struct in_device
*in_dev
)
577 struct ip_mc_list
*pmc
, *pmc_prev
, *pmc_next
;
578 struct sk_buff
*skb
= NULL
;
582 spin_lock_bh(&in_dev
->mc_tomb_lock
);
586 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc_next
) {
587 pmc_next
= pmc
->next
;
588 if (pmc
->sfmode
== MCAST_INCLUDE
) {
589 type
= IGMPV3_BLOCK_OLD_SOURCES
;
590 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
591 skb
= add_grec(skb
, pmc
, type
, 1, 0);
592 skb
= add_grec(skb
, pmc
, dtype
, 1, 1);
595 if (pmc
->sfmode
== MCAST_EXCLUDE
) {
596 type
= IGMPV3_CHANGE_TO_INCLUDE
;
597 skb
= add_grec(skb
, pmc
, type
, 1, 0);
600 if (pmc
->crcount
== 0) {
601 igmpv3_clear_zeros(&pmc
->tomb
);
602 igmpv3_clear_zeros(&pmc
->sources
);
605 if (pmc
->crcount
== 0 && !pmc
->tomb
&& !pmc
->sources
) {
607 pmc_prev
->next
= pmc_next
;
609 in_dev
->mc_tomb
= pmc_next
;
610 in_dev_put(pmc
->interface
);
615 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
618 for_each_pmc_rcu(in_dev
, pmc
) {
619 spin_lock_bh(&pmc
->lock
);
620 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
621 type
= IGMPV3_BLOCK_OLD_SOURCES
;
622 dtype
= IGMPV3_ALLOW_NEW_SOURCES
;
624 type
= IGMPV3_ALLOW_NEW_SOURCES
;
625 dtype
= IGMPV3_BLOCK_OLD_SOURCES
;
627 skb
= add_grec(skb
, pmc
, type
, 0, 0);
628 skb
= add_grec(skb
, pmc
, dtype
, 0, 1); /* deleted sources */
630 /* filter mode changes */
632 if (pmc
->sfmode
== MCAST_EXCLUDE
)
633 type
= IGMPV3_CHANGE_TO_EXCLUDE
;
635 type
= IGMPV3_CHANGE_TO_INCLUDE
;
636 skb
= add_grec(skb
, pmc
, type
, 0, 0);
639 spin_unlock_bh(&pmc
->lock
);
645 (void) igmpv3_sendpack(skb
);
648 static int igmp_send_report(struct in_device
*in_dev
, struct ip_mc_list
*pmc
,
655 struct net_device
*dev
= in_dev
->dev
;
656 struct net
*net
= dev_net(dev
);
657 __be32 group
= pmc
? pmc
->multiaddr
: 0;
660 if (type
== IGMPV3_HOST_MEMBERSHIP_REPORT
)
661 return igmpv3_send_report(in_dev
, pmc
);
662 else if (type
== IGMP_HOST_LEAVE_MESSAGE
)
663 dst
= IGMP_ALL_ROUTER
;
667 rt
= ip_route_output_ports(net
, NULL
, dst
, 0,
669 IPPROTO_IGMP
, 0, dev
->ifindex
);
673 if (rt
->rt_src
== 0) {
678 skb
= alloc_skb(IGMP_SIZE
+LL_ALLOCATED_SPACE(dev
), GFP_ATOMIC
);
684 skb_dst_set(skb
, &rt
->dst
);
686 skb_reserve(skb
, LL_RESERVED_SPACE(dev
));
688 skb_reset_network_header(skb
);
690 skb_put(skb
, sizeof(struct iphdr
) + 4);
693 iph
->ihl
= (sizeof(struct iphdr
)+4)>>2;
695 iph
->frag_off
= htons(IP_DF
);
698 iph
->saddr
= rt
->rt_src
;
699 iph
->protocol
= IPPROTO_IGMP
;
700 ip_select_ident(iph
, &rt
->dst
, NULL
);
701 ((u8
*)&iph
[1])[0] = IPOPT_RA
;
702 ((u8
*)&iph
[1])[1] = 4;
703 ((u8
*)&iph
[1])[2] = 0;
704 ((u8
*)&iph
[1])[3] = 0;
706 ih
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
711 ih
->csum
= ip_compute_csum((void *)ih
, sizeof(struct igmphdr
));
713 return ip_local_out(skb
);
716 static void igmp_gq_timer_expire(unsigned long data
)
718 struct in_device
*in_dev
= (struct in_device
*)data
;
720 in_dev
->mr_gq_running
= 0;
721 igmpv3_send_report(in_dev
, NULL
);
722 __in_dev_put(in_dev
);
725 static void igmp_ifc_timer_expire(unsigned long data
)
727 struct in_device
*in_dev
= (struct in_device
*)data
;
729 igmpv3_send_cr(in_dev
);
730 if (in_dev
->mr_ifc_count
) {
731 in_dev
->mr_ifc_count
--;
732 igmp_ifc_start_timer(in_dev
, IGMP_Unsolicited_Report_Interval
);
734 __in_dev_put(in_dev
);
737 static void igmp_ifc_event(struct in_device
*in_dev
)
739 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
))
741 in_dev
->mr_ifc_count
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
742 IGMP_Unsolicited_Report_Count
;
743 igmp_ifc_start_timer(in_dev
, 1);
747 static void igmp_timer_expire(unsigned long data
)
749 struct ip_mc_list
*im
=(struct ip_mc_list
*)data
;
750 struct in_device
*in_dev
= im
->interface
;
752 spin_lock(&im
->lock
);
755 if (im
->unsolicit_count
) {
756 im
->unsolicit_count
--;
757 igmp_start_timer(im
, IGMP_Unsolicited_Report_Interval
);
760 spin_unlock(&im
->lock
);
762 if (IGMP_V1_SEEN(in_dev
))
763 igmp_send_report(in_dev
, im
, IGMP_HOST_MEMBERSHIP_REPORT
);
764 else if (IGMP_V2_SEEN(in_dev
))
765 igmp_send_report(in_dev
, im
, IGMPV2_HOST_MEMBERSHIP_REPORT
);
767 igmp_send_report(in_dev
, im
, IGMPV3_HOST_MEMBERSHIP_REPORT
);
772 /* mark EXCLUDE-mode sources */
773 static int igmp_xmarksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
775 struct ip_sf_list
*psf
;
779 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
782 for (i
=0; i
<nsrcs
; i
++) {
783 /* skip inactive filters */
784 if (pmc
->sfcount
[MCAST_INCLUDE
] ||
785 pmc
->sfcount
[MCAST_EXCLUDE
] !=
786 psf
->sf_count
[MCAST_EXCLUDE
])
788 if (srcs
[i
] == psf
->sf_inaddr
) {
795 if (scount
== nsrcs
) /* all sources excluded */
800 static int igmp_marksources(struct ip_mc_list
*pmc
, int nsrcs
, __be32
*srcs
)
802 struct ip_sf_list
*psf
;
805 if (pmc
->sfmode
== MCAST_EXCLUDE
)
806 return igmp_xmarksources(pmc
, nsrcs
, srcs
);
808 /* mark INCLUDE-mode sources */
810 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
813 for (i
=0; i
<nsrcs
; i
++)
814 if (srcs
[i
] == psf
->sf_inaddr
) {
828 static void igmp_heard_report(struct in_device
*in_dev
, __be32 group
)
830 struct ip_mc_list
*im
;
832 /* Timers are only set for non-local groups */
834 if (group
== IGMP_ALL_HOSTS
)
838 for_each_pmc_rcu(in_dev
, im
) {
839 if (im
->multiaddr
== group
) {
847 static void igmp_heard_query(struct in_device
*in_dev
, struct sk_buff
*skb
,
850 struct igmphdr
*ih
= igmp_hdr(skb
);
851 struct igmpv3_query
*ih3
= igmpv3_query_hdr(skb
);
852 struct ip_mc_list
*im
;
853 __be32 group
= ih
->group
;
860 /* Alas, old v1 router presents here. */
862 max_delay
= IGMP_Query_Response_Interval
;
863 in_dev
->mr_v1_seen
= jiffies
+
864 IGMP_V1_Router_Present_Timeout
;
867 /* v2 router present */
868 max_delay
= ih
->code
*(HZ
/IGMP_TIMER_SCALE
);
869 in_dev
->mr_v2_seen
= jiffies
+
870 IGMP_V2_Router_Present_Timeout
;
872 /* cancel the interface change timer */
873 in_dev
->mr_ifc_count
= 0;
874 if (del_timer(&in_dev
->mr_ifc_timer
))
875 __in_dev_put(in_dev
);
876 /* clear deleted report items */
877 igmpv3_clear_delrec(in_dev
);
878 } else if (len
< 12) {
879 return; /* ignore bogus packet; freed by caller */
880 } else if (IGMP_V1_SEEN(in_dev
)) {
881 /* This is a v3 query with v1 queriers present */
882 max_delay
= IGMP_Query_Response_Interval
;
884 } else if (IGMP_V2_SEEN(in_dev
)) {
885 /* this is a v3 query with v2 queriers present;
886 * Interpretation of the max_delay code is problematic here.
887 * A real v2 host would use ih_code directly, while v3 has a
888 * different encoding. We use the v3 encoding as more likely
889 * to be intended in a v3 query.
891 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
893 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)))
896 ih3
= igmpv3_query_hdr(skb
);
898 if (!pskb_may_pull(skb
, sizeof(struct igmpv3_query
)
899 + ntohs(ih3
->nsrcs
)*sizeof(__be32
)))
901 ih3
= igmpv3_query_hdr(skb
);
904 max_delay
= IGMPV3_MRC(ih3
->code
)*(HZ
/IGMP_TIMER_SCALE
);
906 max_delay
= 1; /* can't mod w/ 0 */
907 in_dev
->mr_maxdelay
= max_delay
;
909 in_dev
->mr_qrv
= ih3
->qrv
;
910 if (!group
) { /* general query */
912 return; /* no sources allowed */
913 igmp_gq_start_timer(in_dev
);
916 /* mark sources to include, if group & source-specific */
917 mark
= ih3
->nsrcs
!= 0;
921 * - Start the timers in all of our membership records
922 * that the query applies to for the interface on
923 * which the query arrived excl. those that belong
924 * to a "local" group (224.0.0.X)
925 * - For timers already running check if they need to
927 * - Use the igmp->igmp_code field as the maximum
931 for_each_pmc_rcu(in_dev
, im
) {
934 if (group
&& group
!= im
->multiaddr
)
936 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
938 spin_lock_bh(&im
->lock
);
940 im
->gsquery
= im
->gsquery
&& mark
;
943 changed
= !im
->gsquery
||
944 igmp_marksources(im
, ntohs(ih3
->nsrcs
), ih3
->srcs
);
945 spin_unlock_bh(&im
->lock
);
947 igmp_mod_timer(im
, max_delay
);
952 /* called in rcu_read_lock() section */
953 int igmp_rcv(struct sk_buff
*skb
)
955 /* This basically follows the spec line by line -- see RFC1112 */
957 struct in_device
*in_dev
= __in_dev_get_rcu(skb
->dev
);
963 if (!pskb_may_pull(skb
, sizeof(struct igmphdr
)))
966 switch (skb
->ip_summed
) {
967 case CHECKSUM_COMPLETE
:
968 if (!csum_fold(skb
->csum
))
973 if (__skb_checksum_complete(skb
))
979 case IGMP_HOST_MEMBERSHIP_QUERY
:
980 igmp_heard_query(in_dev
, skb
, len
);
982 case IGMP_HOST_MEMBERSHIP_REPORT
:
983 case IGMPV2_HOST_MEMBERSHIP_REPORT
:
984 /* Is it our report looped back? */
985 if (rt_is_output_route(skb_rtable(skb
)))
987 /* don't rely on MC router hearing unicast reports */
988 if (skb
->pkt_type
== PACKET_MULTICAST
||
989 skb
->pkt_type
== PACKET_BROADCAST
)
990 igmp_heard_report(in_dev
, ih
->group
);
993 #ifdef CONFIG_IP_PIMSM_V1
994 return pim_rcv_v1(skb
);
996 case IGMPV3_HOST_MEMBERSHIP_REPORT
:
999 case IGMP_HOST_LEAVE_MESSAGE
:
1001 case IGMP_MTRACE_RESP
:
1016 * Add a filter to a device
1019 static void ip_mc_filter_add(struct in_device
*in_dev
, __be32 addr
)
1021 char buf
[MAX_ADDR_LEN
];
1022 struct net_device
*dev
= in_dev
->dev
;
1024 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1025 We will get multicast token leakage, when IFF_MULTICAST
1026 is changed. This check should be done in dev->set_multicast_list
1027 routine. Something sort of:
1028 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1031 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1032 dev_mc_add(dev
, buf
);
1036 * Remove a filter from a device
1039 static void ip_mc_filter_del(struct in_device
*in_dev
, __be32 addr
)
1041 char buf
[MAX_ADDR_LEN
];
1042 struct net_device
*dev
= in_dev
->dev
;
1044 if (arp_mc_map(addr
, buf
, dev
, 0) == 0)
1045 dev_mc_del(dev
, buf
);
1048 #ifdef CONFIG_IP_MULTICAST
1050 * deleted ip_mc_list manipulation
1052 static void igmpv3_add_delrec(struct in_device
*in_dev
, struct ip_mc_list
*im
)
1054 struct ip_mc_list
*pmc
;
1056 /* this is an "ip_mc_list" for convenience; only the fields below
1057 * are actually used. In particular, the refcnt and users are not
1058 * used for management of the delete list. Using the same structure
1059 * for deleted items allows change reports to use common code with
1060 * non-deleted or query-response MCA's.
1062 pmc
= kzalloc(sizeof(*pmc
), GFP_KERNEL
);
1065 spin_lock_bh(&im
->lock
);
1066 pmc
->interface
= im
->interface
;
1067 in_dev_hold(in_dev
);
1068 pmc
->multiaddr
= im
->multiaddr
;
1069 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1070 IGMP_Unsolicited_Report_Count
;
1071 pmc
->sfmode
= im
->sfmode
;
1072 if (pmc
->sfmode
== MCAST_INCLUDE
) {
1073 struct ip_sf_list
*psf
;
1075 pmc
->tomb
= im
->tomb
;
1076 pmc
->sources
= im
->sources
;
1077 im
->tomb
= im
->sources
= NULL
;
1078 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1079 psf
->sf_crcount
= pmc
->crcount
;
1081 spin_unlock_bh(&im
->lock
);
1083 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1084 pmc
->next
= in_dev
->mc_tomb
;
1085 in_dev
->mc_tomb
= pmc
;
1086 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1089 static void igmpv3_del_delrec(struct in_device
*in_dev
, __be32 multiaddr
)
1091 struct ip_mc_list
*pmc
, *pmc_prev
;
1092 struct ip_sf_list
*psf
, *psf_next
;
1094 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1096 for (pmc
=in_dev
->mc_tomb
; pmc
; pmc
=pmc
->next
) {
1097 if (pmc
->multiaddr
== multiaddr
)
1103 pmc_prev
->next
= pmc
->next
;
1105 in_dev
->mc_tomb
= pmc
->next
;
1107 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1109 for (psf
=pmc
->tomb
; psf
; psf
=psf_next
) {
1110 psf_next
= psf
->sf_next
;
1113 in_dev_put(pmc
->interface
);
1118 static void igmpv3_clear_delrec(struct in_device
*in_dev
)
1120 struct ip_mc_list
*pmc
, *nextpmc
;
1122 spin_lock_bh(&in_dev
->mc_tomb_lock
);
1123 pmc
= in_dev
->mc_tomb
;
1124 in_dev
->mc_tomb
= NULL
;
1125 spin_unlock_bh(&in_dev
->mc_tomb_lock
);
1127 for (; pmc
; pmc
= nextpmc
) {
1128 nextpmc
= pmc
->next
;
1129 ip_mc_clear_src(pmc
);
1130 in_dev_put(pmc
->interface
);
1133 /* clear dead sources, too */
1135 for_each_pmc_rcu(in_dev
, pmc
) {
1136 struct ip_sf_list
*psf
, *psf_next
;
1138 spin_lock_bh(&pmc
->lock
);
1141 spin_unlock_bh(&pmc
->lock
);
1142 for (; psf
; psf
=psf_next
) {
1143 psf_next
= psf
->sf_next
;
1151 static void igmp_group_dropped(struct ip_mc_list
*im
)
1153 struct in_device
*in_dev
= im
->interface
;
1154 #ifdef CONFIG_IP_MULTICAST
1160 ip_mc_filter_del(in_dev
, im
->multiaddr
);
1163 #ifdef CONFIG_IP_MULTICAST
1164 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1167 reporter
= im
->reporter
;
1168 igmp_stop_timer(im
);
1170 if (!in_dev
->dead
) {
1171 if (IGMP_V1_SEEN(in_dev
))
1173 if (IGMP_V2_SEEN(in_dev
)) {
1175 igmp_send_report(in_dev
, im
, IGMP_HOST_LEAVE_MESSAGE
);
1179 igmpv3_add_delrec(in_dev
, im
);
1181 igmp_ifc_event(in_dev
);
1186 static void igmp_group_added(struct ip_mc_list
*im
)
1188 struct in_device
*in_dev
= im
->interface
;
1190 if (im
->loaded
== 0) {
1192 ip_mc_filter_add(in_dev
, im
->multiaddr
);
1195 #ifdef CONFIG_IP_MULTICAST
1196 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1201 if (IGMP_V1_SEEN(in_dev
) || IGMP_V2_SEEN(in_dev
)) {
1202 spin_lock_bh(&im
->lock
);
1203 igmp_start_timer(im
, IGMP_Initial_Report_Delay
);
1204 spin_unlock_bh(&im
->lock
);
1209 im
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1210 IGMP_Unsolicited_Report_Count
;
1211 igmp_ifc_event(in_dev
);
1217 * Multicast list managers
1222 * A socket has joined a multicast group on device dev.
1225 void ip_mc_inc_group(struct in_device
*in_dev
, __be32 addr
)
1227 struct ip_mc_list
*im
;
1231 for_each_pmc_rtnl(in_dev
, im
) {
1232 if (im
->multiaddr
== addr
) {
1234 ip_mc_add_src(in_dev
, &addr
, MCAST_EXCLUDE
, 0, NULL
, 0);
1239 im
= kzalloc(sizeof(*im
), GFP_KERNEL
);
1244 im
->interface
= in_dev
;
1245 in_dev_hold(in_dev
);
1246 im
->multiaddr
= addr
;
1247 /* initial mode is (EX, empty) */
1248 im
->sfmode
= MCAST_EXCLUDE
;
1249 im
->sfcount
[MCAST_EXCLUDE
] = 1;
1250 atomic_set(&im
->refcnt
, 1);
1251 spin_lock_init(&im
->lock
);
1252 #ifdef CONFIG_IP_MULTICAST
1253 setup_timer(&im
->timer
, &igmp_timer_expire
, (unsigned long)im
);
1254 im
->unsolicit_count
= IGMP_Unsolicited_Report_Count
;
1257 im
->next_rcu
= in_dev
->mc_list
;
1259 rcu_assign_pointer(in_dev
->mc_list
, im
);
1261 #ifdef CONFIG_IP_MULTICAST
1262 igmpv3_del_delrec(in_dev
, im
->multiaddr
);
1264 igmp_group_added(im
);
1266 ip_rt_multicast_event(in_dev
);
1270 EXPORT_SYMBOL(ip_mc_inc_group
);
1273 * Resend IGMP JOIN report; used for bonding.
1274 * Called with rcu_read_lock()
1276 void ip_mc_rejoin_groups(struct in_device
*in_dev
)
1278 #ifdef CONFIG_IP_MULTICAST
1279 struct ip_mc_list
*im
;
1282 for_each_pmc_rcu(in_dev
, im
) {
1283 if (im
->multiaddr
== IGMP_ALL_HOSTS
)
1286 /* a failover is happening and switches
1287 * must be notified immediately
1289 if (IGMP_V1_SEEN(in_dev
))
1290 type
= IGMP_HOST_MEMBERSHIP_REPORT
;
1291 else if (IGMP_V2_SEEN(in_dev
))
1292 type
= IGMPV2_HOST_MEMBERSHIP_REPORT
;
1294 type
= IGMPV3_HOST_MEMBERSHIP_REPORT
;
1295 igmp_send_report(in_dev
, im
, type
);
1299 EXPORT_SYMBOL(ip_mc_rejoin_groups
);
1302 * A socket has left a multicast group on device dev
1305 void ip_mc_dec_group(struct in_device
*in_dev
, __be32 addr
)
1307 struct ip_mc_list
*i
;
1308 struct ip_mc_list __rcu
**ip
;
1312 for (ip
= &in_dev
->mc_list
;
1313 (i
= rtnl_dereference(*ip
)) != NULL
;
1314 ip
= &i
->next_rcu
) {
1315 if (i
->multiaddr
== addr
) {
1316 if (--i
->users
== 0) {
1319 igmp_group_dropped(i
);
1323 ip_rt_multicast_event(in_dev
);
1332 EXPORT_SYMBOL(ip_mc_dec_group
);
1334 /* Device changing type */
1336 void ip_mc_unmap(struct in_device
*in_dev
)
1338 struct ip_mc_list
*pmc
;
1342 for_each_pmc_rtnl(in_dev
, pmc
)
1343 igmp_group_dropped(pmc
);
1346 void ip_mc_remap(struct in_device
*in_dev
)
1348 struct ip_mc_list
*pmc
;
1352 for_each_pmc_rtnl(in_dev
, pmc
)
1353 igmp_group_added(pmc
);
1356 /* Device going down */
1358 void ip_mc_down(struct in_device
*in_dev
)
1360 struct ip_mc_list
*pmc
;
1364 for_each_pmc_rtnl(in_dev
, pmc
)
1365 igmp_group_dropped(pmc
);
1367 #ifdef CONFIG_IP_MULTICAST
1368 in_dev
->mr_ifc_count
= 0;
1369 if (del_timer(&in_dev
->mr_ifc_timer
))
1370 __in_dev_put(in_dev
);
1371 in_dev
->mr_gq_running
= 0;
1372 if (del_timer(&in_dev
->mr_gq_timer
))
1373 __in_dev_put(in_dev
);
1374 igmpv3_clear_delrec(in_dev
);
1377 ip_mc_dec_group(in_dev
, IGMP_ALL_HOSTS
);
1380 void ip_mc_init_dev(struct in_device
*in_dev
)
1384 in_dev
->mc_tomb
= NULL
;
1385 #ifdef CONFIG_IP_MULTICAST
1386 in_dev
->mr_gq_running
= 0;
1387 setup_timer(&in_dev
->mr_gq_timer
, igmp_gq_timer_expire
,
1388 (unsigned long)in_dev
);
1389 in_dev
->mr_ifc_count
= 0;
1390 in_dev
->mc_count
= 0;
1391 setup_timer(&in_dev
->mr_ifc_timer
, igmp_ifc_timer_expire
,
1392 (unsigned long)in_dev
);
1393 in_dev
->mr_qrv
= IGMP_Unsolicited_Report_Count
;
1396 spin_lock_init(&in_dev
->mc_tomb_lock
);
1399 /* Device going up */
1401 void ip_mc_up(struct in_device
*in_dev
)
1403 struct ip_mc_list
*pmc
;
1407 ip_mc_inc_group(in_dev
, IGMP_ALL_HOSTS
);
1409 for_each_pmc_rtnl(in_dev
, pmc
)
1410 igmp_group_added(pmc
);
1414 * Device is about to be destroyed: clean up.
1417 void ip_mc_destroy_dev(struct in_device
*in_dev
)
1419 struct ip_mc_list
*i
;
1423 /* Deactivate timers */
1426 while ((i
= rtnl_dereference(in_dev
->mc_list
)) != NULL
) {
1427 in_dev
->mc_list
= i
->next_rcu
;
1430 /* We've dropped the groups in ip_mc_down already */
1436 /* RTNL is locked */
1437 static struct in_device
*ip_mc_find_dev(struct net
*net
, struct ip_mreqn
*imr
)
1439 struct net_device
*dev
= NULL
;
1440 struct in_device
*idev
= NULL
;
1442 if (imr
->imr_ifindex
) {
1443 idev
= inetdev_by_index(net
, imr
->imr_ifindex
);
1446 if (imr
->imr_address
.s_addr
) {
1447 dev
= __ip_dev_find(net
, imr
->imr_address
.s_addr
, false);
1453 struct rtable
*rt
= ip_route_output(net
,
1454 imr
->imr_multiaddr
.s_addr
,
1462 imr
->imr_ifindex
= dev
->ifindex
;
1463 idev
= __in_dev_get_rtnl(dev
);
1469 * Join a socket to a group
1471 int sysctl_igmp_max_memberships __read_mostly
= IP_MAX_MEMBERSHIPS
;
1472 int sysctl_igmp_max_msf __read_mostly
= IP_MAX_MSF
;
1475 static int ip_mc_del1_src(struct ip_mc_list
*pmc
, int sfmode
,
1478 struct ip_sf_list
*psf
, *psf_prev
;
1482 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1483 if (psf
->sf_inaddr
== *psfsrc
)
1487 if (!psf
|| psf
->sf_count
[sfmode
] == 0) {
1488 /* source filter not found, or count wrong => bug */
1491 psf
->sf_count
[sfmode
]--;
1492 if (psf
->sf_count
[sfmode
] == 0) {
1493 ip_rt_multicast_event(pmc
->interface
);
1495 if (!psf
->sf_count
[MCAST_INCLUDE
] && !psf
->sf_count
[MCAST_EXCLUDE
]) {
1496 #ifdef CONFIG_IP_MULTICAST
1497 struct in_device
*in_dev
= pmc
->interface
;
1500 /* no more filters for this source */
1502 psf_prev
->sf_next
= psf
->sf_next
;
1504 pmc
->sources
= psf
->sf_next
;
1505 #ifdef CONFIG_IP_MULTICAST
1506 if (psf
->sf_oldin
&&
1507 !IGMP_V1_SEEN(in_dev
) && !IGMP_V2_SEEN(in_dev
)) {
1508 psf
->sf_crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1509 IGMP_Unsolicited_Report_Count
;
1510 psf
->sf_next
= pmc
->tomb
;
1520 #ifndef CONFIG_IP_MULTICAST
1521 #define igmp_ifc_event(x) do { } while (0)
1524 static int ip_mc_del_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1525 int sfcount
, __be32
*psfsrc
, int delta
)
1527 struct ip_mc_list
*pmc
;
1534 for_each_pmc_rcu(in_dev
, pmc
) {
1535 if (*pmca
== pmc
->multiaddr
)
1539 /* MCA not found?? bug */
1543 spin_lock_bh(&pmc
->lock
);
1545 #ifdef CONFIG_IP_MULTICAST
1550 if (!pmc
->sfcount
[sfmode
])
1552 pmc
->sfcount
[sfmode
]--;
1555 for (i
=0; i
<sfcount
; i
++) {
1556 int rv
= ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1558 changerec
|= rv
> 0;
1562 if (pmc
->sfmode
== MCAST_EXCLUDE
&&
1563 pmc
->sfcount
[MCAST_EXCLUDE
] == 0 &&
1564 pmc
->sfcount
[MCAST_INCLUDE
]) {
1565 #ifdef CONFIG_IP_MULTICAST
1566 struct ip_sf_list
*psf
;
1569 /* filter mode change */
1570 pmc
->sfmode
= MCAST_INCLUDE
;
1571 #ifdef CONFIG_IP_MULTICAST
1572 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1573 IGMP_Unsolicited_Report_Count
;
1574 in_dev
->mr_ifc_count
= pmc
->crcount
;
1575 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1576 psf
->sf_crcount
= 0;
1577 igmp_ifc_event(pmc
->interface
);
1578 } else if (sf_setstate(pmc
) || changerec
) {
1579 igmp_ifc_event(pmc
->interface
);
1583 spin_unlock_bh(&pmc
->lock
);
1588 * Add multicast single-source filter to the interface list
1590 static int ip_mc_add1_src(struct ip_mc_list
*pmc
, int sfmode
,
1591 __be32
*psfsrc
, int delta
)
1593 struct ip_sf_list
*psf
, *psf_prev
;
1596 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1597 if (psf
->sf_inaddr
== *psfsrc
)
1602 psf
= kzalloc(sizeof(*psf
), GFP_ATOMIC
);
1605 psf
->sf_inaddr
= *psfsrc
;
1607 psf_prev
->sf_next
= psf
;
1611 psf
->sf_count
[sfmode
]++;
1612 if (psf
->sf_count
[sfmode
] == 1) {
1613 ip_rt_multicast_event(pmc
->interface
);
1618 #ifdef CONFIG_IP_MULTICAST
1619 static void sf_markstate(struct ip_mc_list
*pmc
)
1621 struct ip_sf_list
*psf
;
1622 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1624 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
)
1625 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1626 psf
->sf_oldin
= mca_xcount
==
1627 psf
->sf_count
[MCAST_EXCLUDE
] &&
1628 !psf
->sf_count
[MCAST_INCLUDE
];
1630 psf
->sf_oldin
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1633 static int sf_setstate(struct ip_mc_list
*pmc
)
1635 struct ip_sf_list
*psf
, *dpsf
;
1636 int mca_xcount
= pmc
->sfcount
[MCAST_EXCLUDE
];
1637 int qrv
= pmc
->interface
->mr_qrv
;
1641 for (psf
=pmc
->sources
; psf
; psf
=psf
->sf_next
) {
1642 if (pmc
->sfcount
[MCAST_EXCLUDE
]) {
1643 new_in
= mca_xcount
== psf
->sf_count
[MCAST_EXCLUDE
] &&
1644 !psf
->sf_count
[MCAST_INCLUDE
];
1646 new_in
= psf
->sf_count
[MCAST_INCLUDE
] != 0;
1648 if (!psf
->sf_oldin
) {
1649 struct ip_sf_list
*prev
= NULL
;
1651 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
) {
1652 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1658 prev
->sf_next
= dpsf
->sf_next
;
1660 pmc
->tomb
= dpsf
->sf_next
;
1663 psf
->sf_crcount
= qrv
;
1666 } else if (psf
->sf_oldin
) {
1668 psf
->sf_crcount
= 0;
1670 * add or update "delete" records if an active filter
1673 for (dpsf
=pmc
->tomb
; dpsf
; dpsf
=dpsf
->sf_next
)
1674 if (dpsf
->sf_inaddr
== psf
->sf_inaddr
)
1677 dpsf
= kmalloc(sizeof(*dpsf
), GFP_ATOMIC
);
1681 /* pmc->lock held by callers */
1682 dpsf
->sf_next
= pmc
->tomb
;
1685 dpsf
->sf_crcount
= qrv
;
1694 * Add multicast source filter list to the interface list
1696 static int ip_mc_add_src(struct in_device
*in_dev
, __be32
*pmca
, int sfmode
,
1697 int sfcount
, __be32
*psfsrc
, int delta
)
1699 struct ip_mc_list
*pmc
;
1706 for_each_pmc_rcu(in_dev
, pmc
) {
1707 if (*pmca
== pmc
->multiaddr
)
1711 /* MCA not found?? bug */
1715 spin_lock_bh(&pmc
->lock
);
1718 #ifdef CONFIG_IP_MULTICAST
1721 isexclude
= pmc
->sfmode
== MCAST_EXCLUDE
;
1723 pmc
->sfcount
[sfmode
]++;
1725 for (i
=0; i
<sfcount
; i
++) {
1726 err
= ip_mc_add1_src(pmc
, sfmode
, &psfsrc
[i
], delta
);
1733 pmc
->sfcount
[sfmode
]--;
1735 (void) ip_mc_del1_src(pmc
, sfmode
, &psfsrc
[i
]);
1736 } else if (isexclude
!= (pmc
->sfcount
[MCAST_EXCLUDE
] != 0)) {
1737 #ifdef CONFIG_IP_MULTICAST
1738 struct ip_sf_list
*psf
;
1739 in_dev
= pmc
->interface
;
1742 /* filter mode change */
1743 if (pmc
->sfcount
[MCAST_EXCLUDE
])
1744 pmc
->sfmode
= MCAST_EXCLUDE
;
1745 else if (pmc
->sfcount
[MCAST_INCLUDE
])
1746 pmc
->sfmode
= MCAST_INCLUDE
;
1747 #ifdef CONFIG_IP_MULTICAST
1748 /* else no filters; keep old mode for reports */
1750 pmc
->crcount
= in_dev
->mr_qrv
? in_dev
->mr_qrv
:
1751 IGMP_Unsolicited_Report_Count
;
1752 in_dev
->mr_ifc_count
= pmc
->crcount
;
1753 for (psf
=pmc
->sources
; psf
; psf
= psf
->sf_next
)
1754 psf
->sf_crcount
= 0;
1755 igmp_ifc_event(in_dev
);
1756 } else if (sf_setstate(pmc
)) {
1757 igmp_ifc_event(in_dev
);
1760 spin_unlock_bh(&pmc
->lock
);
1764 static void ip_mc_clear_src(struct ip_mc_list
*pmc
)
1766 struct ip_sf_list
*psf
, *nextpsf
;
1768 for (psf
=pmc
->tomb
; psf
; psf
=nextpsf
) {
1769 nextpsf
= psf
->sf_next
;
1773 for (psf
=pmc
->sources
; psf
; psf
=nextpsf
) {
1774 nextpsf
= psf
->sf_next
;
1777 pmc
->sources
= NULL
;
1778 pmc
->sfmode
= MCAST_EXCLUDE
;
1779 pmc
->sfcount
[MCAST_INCLUDE
] = 0;
1780 pmc
->sfcount
[MCAST_EXCLUDE
] = 1;
1785 * Join a multicast group
1787 int ip_mc_join_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1790 __be32 addr
= imr
->imr_multiaddr
.s_addr
;
1791 struct ip_mc_socklist
*iml
= NULL
, *i
;
1792 struct in_device
*in_dev
;
1793 struct inet_sock
*inet
= inet_sk(sk
);
1794 struct net
*net
= sock_net(sk
);
1798 if (!ipv4_is_multicast(addr
))
1803 in_dev
= ip_mc_find_dev(net
, imr
);
1812 ifindex
= imr
->imr_ifindex
;
1813 for_each_pmc_rtnl(inet
, i
) {
1814 if (i
->multi
.imr_multiaddr
.s_addr
== addr
&&
1815 i
->multi
.imr_ifindex
== ifindex
)
1820 if (count
>= sysctl_igmp_max_memberships
)
1822 iml
= sock_kmalloc(sk
, sizeof(*iml
), GFP_KERNEL
);
1826 memcpy(&iml
->multi
, imr
, sizeof(*imr
));
1827 iml
->next_rcu
= inet
->mc_list
;
1829 iml
->sfmode
= MCAST_EXCLUDE
;
1830 rcu_assign_pointer(inet
->mc_list
, iml
);
1831 ip_mc_inc_group(in_dev
, addr
);
1837 EXPORT_SYMBOL(ip_mc_join_group
);
1839 static void ip_sf_socklist_reclaim(struct rcu_head
*rp
)
1841 kfree(container_of(rp
, struct ip_sf_socklist
, rcu
));
1842 /* sk_omem_alloc should have been decreased by the caller*/
1845 static int ip_mc_leave_src(struct sock
*sk
, struct ip_mc_socklist
*iml
,
1846 struct in_device
*in_dev
)
1848 struct ip_sf_socklist
*psf
= rtnl_dereference(iml
->sflist
);
1852 /* any-source empty exclude case */
1853 return ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1854 iml
->sfmode
, 0, NULL
, 0);
1856 err
= ip_mc_del_src(in_dev
, &iml
->multi
.imr_multiaddr
.s_addr
,
1857 iml
->sfmode
, psf
->sl_count
, psf
->sl_addr
, 0);
1858 rcu_assign_pointer(iml
->sflist
, NULL
);
1859 /* decrease mem now to avoid the memleak warning */
1860 atomic_sub(IP_SFLSIZE(psf
->sl_max
), &sk
->sk_omem_alloc
);
1861 call_rcu(&psf
->rcu
, ip_sf_socklist_reclaim
);
1866 static void ip_mc_socklist_reclaim(struct rcu_head
*rp
)
1868 kfree(container_of(rp
, struct ip_mc_socklist
, rcu
));
1869 /* sk_omem_alloc should have been decreased by the caller*/
1874 * Ask a socket to leave a group.
1877 int ip_mc_leave_group(struct sock
*sk
, struct ip_mreqn
*imr
)
1879 struct inet_sock
*inet
= inet_sk(sk
);
1880 struct ip_mc_socklist
*iml
;
1881 struct ip_mc_socklist __rcu
**imlp
;
1882 struct in_device
*in_dev
;
1883 struct net
*net
= sock_net(sk
);
1884 __be32 group
= imr
->imr_multiaddr
.s_addr
;
1886 int ret
= -EADDRNOTAVAIL
;
1889 in_dev
= ip_mc_find_dev(net
, imr
);
1890 ifindex
= imr
->imr_ifindex
;
1891 for (imlp
= &inet
->mc_list
;
1892 (iml
= rtnl_dereference(*imlp
)) != NULL
;
1893 imlp
= &iml
->next_rcu
) {
1894 if (iml
->multi
.imr_multiaddr
.s_addr
!= group
)
1897 if (iml
->multi
.imr_ifindex
!= ifindex
)
1899 } else if (imr
->imr_address
.s_addr
&& imr
->imr_address
.s_addr
!=
1900 iml
->multi
.imr_address
.s_addr
)
1903 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
1905 *imlp
= iml
->next_rcu
;
1908 ip_mc_dec_group(in_dev
, group
);
1910 /* decrease mem now to avoid the memleak warning */
1911 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
1912 call_rcu(&iml
->rcu
, ip_mc_socklist_reclaim
);
1921 int ip_mc_source(int add
, int omode
, struct sock
*sk
, struct
1922 ip_mreq_source
*mreqs
, int ifindex
)
1925 struct ip_mreqn imr
;
1926 __be32 addr
= mreqs
->imr_multiaddr
;
1927 struct ip_mc_socklist
*pmc
;
1928 struct in_device
*in_dev
= NULL
;
1929 struct inet_sock
*inet
= inet_sk(sk
);
1930 struct ip_sf_socklist
*psl
;
1931 struct net
*net
= sock_net(sk
);
1935 if (!ipv4_is_multicast(addr
))
1940 imr
.imr_multiaddr
.s_addr
= mreqs
->imr_multiaddr
;
1941 imr
.imr_address
.s_addr
= mreqs
->imr_interface
;
1942 imr
.imr_ifindex
= ifindex
;
1943 in_dev
= ip_mc_find_dev(net
, &imr
);
1949 err
= -EADDRNOTAVAIL
;
1951 for_each_pmc_rtnl(inet
, pmc
) {
1952 if ((pmc
->multi
.imr_multiaddr
.s_addr
==
1953 imr
.imr_multiaddr
.s_addr
) &&
1954 (pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
))
1957 if (!pmc
) { /* must have a prior join */
1961 /* if a source filter was set, must be the same mode as before */
1963 if (pmc
->sfmode
!= omode
) {
1967 } else if (pmc
->sfmode
!= omode
) {
1968 /* allow mode switches for empty-set filters */
1969 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 0, NULL
, 0);
1970 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, pmc
->sfmode
, 0,
1972 pmc
->sfmode
= omode
;
1975 psl
= rtnl_dereference(pmc
->sflist
);
1978 goto done
; /* err = -EADDRNOTAVAIL */
1980 for (i
=0; i
<psl
->sl_count
; i
++) {
1981 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
1986 if (rv
) /* source not found */
1987 goto done
; /* err = -EADDRNOTAVAIL */
1989 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1990 if (psl
->sl_count
== 1 && omode
== MCAST_INCLUDE
) {
1995 /* update the interface filter */
1996 ip_mc_del_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
1997 &mreqs
->imr_sourceaddr
, 1);
1999 for (j
=i
+1; j
<psl
->sl_count
; j
++)
2000 psl
->sl_addr
[j
-1] = psl
->sl_addr
[j
];
2005 /* else, add a new source to the filter */
2007 if (psl
&& psl
->sl_count
>= sysctl_igmp_max_msf
) {
2011 if (!psl
|| psl
->sl_count
== psl
->sl_max
) {
2012 struct ip_sf_socklist
*newpsl
;
2013 int count
= IP_SFBLOCK
;
2016 count
+= psl
->sl_max
;
2017 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(count
), GFP_KERNEL
);
2022 newpsl
->sl_max
= count
;
2023 newpsl
->sl_count
= count
- IP_SFBLOCK
;
2025 for (i
=0; i
<psl
->sl_count
; i
++)
2026 newpsl
->sl_addr
[i
] = psl
->sl_addr
[i
];
2027 /* decrease mem now to avoid the memleak warning */
2028 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2029 call_rcu(&psl
->rcu
, ip_sf_socklist_reclaim
);
2031 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2034 rv
= 1; /* > 0 for insert logic below if sl_count is 0 */
2035 for (i
=0; i
<psl
->sl_count
; i
++) {
2036 rv
= memcmp(&psl
->sl_addr
[i
], &mreqs
->imr_sourceaddr
,
2041 if (rv
== 0) /* address already there is an error */
2043 for (j
=psl
->sl_count
-1; j
>=i
; j
--)
2044 psl
->sl_addr
[j
+1] = psl
->sl_addr
[j
];
2045 psl
->sl_addr
[i
] = mreqs
->imr_sourceaddr
;
2048 /* update the interface list */
2049 ip_mc_add_src(in_dev
, &mreqs
->imr_multiaddr
, omode
, 1,
2050 &mreqs
->imr_sourceaddr
, 1);
2054 return ip_mc_leave_group(sk
, &imr
);
2058 int ip_mc_msfilter(struct sock
*sk
, struct ip_msfilter
*msf
, int ifindex
)
2061 struct ip_mreqn imr
;
2062 __be32 addr
= msf
->imsf_multiaddr
;
2063 struct ip_mc_socklist
*pmc
;
2064 struct in_device
*in_dev
;
2065 struct inet_sock
*inet
= inet_sk(sk
);
2066 struct ip_sf_socklist
*newpsl
, *psl
;
2067 struct net
*net
= sock_net(sk
);
2070 if (!ipv4_is_multicast(addr
))
2072 if (msf
->imsf_fmode
!= MCAST_INCLUDE
&&
2073 msf
->imsf_fmode
!= MCAST_EXCLUDE
)
2078 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2079 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2080 imr
.imr_ifindex
= ifindex
;
2081 in_dev
= ip_mc_find_dev(net
, &imr
);
2088 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2089 if (msf
->imsf_fmode
== MCAST_INCLUDE
&& msf
->imsf_numsrc
== 0) {
2094 for_each_pmc_rtnl(inet
, pmc
) {
2095 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2096 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2099 if (!pmc
) { /* must have a prior join */
2103 if (msf
->imsf_numsrc
) {
2104 newpsl
= sock_kmalloc(sk
, IP_SFLSIZE(msf
->imsf_numsrc
),
2110 newpsl
->sl_max
= newpsl
->sl_count
= msf
->imsf_numsrc
;
2111 memcpy(newpsl
->sl_addr
, msf
->imsf_slist
,
2112 msf
->imsf_numsrc
* sizeof(msf
->imsf_slist
[0]));
2113 err
= ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2114 msf
->imsf_fmode
, newpsl
->sl_count
, newpsl
->sl_addr
, 0);
2116 sock_kfree_s(sk
, newpsl
, IP_SFLSIZE(newpsl
->sl_max
));
2121 (void) ip_mc_add_src(in_dev
, &msf
->imsf_multiaddr
,
2122 msf
->imsf_fmode
, 0, NULL
, 0);
2124 psl
= rtnl_dereference(pmc
->sflist
);
2126 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2127 psl
->sl_count
, psl
->sl_addr
, 0);
2128 /* decrease mem now to avoid the memleak warning */
2129 atomic_sub(IP_SFLSIZE(psl
->sl_max
), &sk
->sk_omem_alloc
);
2130 call_rcu(&psl
->rcu
, ip_sf_socklist_reclaim
);
2132 (void) ip_mc_del_src(in_dev
, &msf
->imsf_multiaddr
, pmc
->sfmode
,
2134 rcu_assign_pointer(pmc
->sflist
, newpsl
);
2135 pmc
->sfmode
= msf
->imsf_fmode
;
2140 err
= ip_mc_leave_group(sk
, &imr
);
2144 int ip_mc_msfget(struct sock
*sk
, struct ip_msfilter
*msf
,
2145 struct ip_msfilter __user
*optval
, int __user
*optlen
)
2147 int err
, len
, count
, copycount
;
2148 struct ip_mreqn imr
;
2149 __be32 addr
= msf
->imsf_multiaddr
;
2150 struct ip_mc_socklist
*pmc
;
2151 struct in_device
*in_dev
;
2152 struct inet_sock
*inet
= inet_sk(sk
);
2153 struct ip_sf_socklist
*psl
;
2154 struct net
*net
= sock_net(sk
);
2156 if (!ipv4_is_multicast(addr
))
2161 imr
.imr_multiaddr
.s_addr
= msf
->imsf_multiaddr
;
2162 imr
.imr_address
.s_addr
= msf
->imsf_interface
;
2163 imr
.imr_ifindex
= 0;
2164 in_dev
= ip_mc_find_dev(net
, &imr
);
2170 err
= -EADDRNOTAVAIL
;
2172 for_each_pmc_rtnl(inet
, pmc
) {
2173 if (pmc
->multi
.imr_multiaddr
.s_addr
== msf
->imsf_multiaddr
&&
2174 pmc
->multi
.imr_ifindex
== imr
.imr_ifindex
)
2177 if (!pmc
) /* must have a prior join */
2179 msf
->imsf_fmode
= pmc
->sfmode
;
2180 psl
= rtnl_dereference(pmc
->sflist
);
2186 count
= psl
->sl_count
;
2188 copycount
= count
< msf
->imsf_numsrc
? count
: msf
->imsf_numsrc
;
2189 len
= copycount
* sizeof(psl
->sl_addr
[0]);
2190 msf
->imsf_numsrc
= count
;
2191 if (put_user(IP_MSFILTER_SIZE(copycount
), optlen
) ||
2192 copy_to_user(optval
, msf
, IP_MSFILTER_SIZE(0))) {
2196 copy_to_user(&optval
->imsf_slist
[0], psl
->sl_addr
, len
))
2204 int ip_mc_gsfget(struct sock
*sk
, struct group_filter
*gsf
,
2205 struct group_filter __user
*optval
, int __user
*optlen
)
2207 int err
, i
, count
, copycount
;
2208 struct sockaddr_in
*psin
;
2210 struct ip_mc_socklist
*pmc
;
2211 struct inet_sock
*inet
= inet_sk(sk
);
2212 struct ip_sf_socklist
*psl
;
2214 psin
= (struct sockaddr_in
*)&gsf
->gf_group
;
2215 if (psin
->sin_family
!= AF_INET
)
2217 addr
= psin
->sin_addr
.s_addr
;
2218 if (!ipv4_is_multicast(addr
))
2223 err
= -EADDRNOTAVAIL
;
2225 for_each_pmc_rtnl(inet
, pmc
) {
2226 if (pmc
->multi
.imr_multiaddr
.s_addr
== addr
&&
2227 pmc
->multi
.imr_ifindex
== gsf
->gf_interface
)
2230 if (!pmc
) /* must have a prior join */
2232 gsf
->gf_fmode
= pmc
->sfmode
;
2233 psl
= rtnl_dereference(pmc
->sflist
);
2235 count
= psl
? psl
->sl_count
: 0;
2236 copycount
= count
< gsf
->gf_numsrc
? count
: gsf
->gf_numsrc
;
2237 gsf
->gf_numsrc
= count
;
2238 if (put_user(GROUP_FILTER_SIZE(copycount
), optlen
) ||
2239 copy_to_user(optval
, gsf
, GROUP_FILTER_SIZE(0))) {
2242 for (i
=0; i
<copycount
; i
++) {
2243 struct sockaddr_storage ss
;
2245 psin
= (struct sockaddr_in
*)&ss
;
2246 memset(&ss
, 0, sizeof(ss
));
2247 psin
->sin_family
= AF_INET
;
2248 psin
->sin_addr
.s_addr
= psl
->sl_addr
[i
];
2249 if (copy_to_user(&optval
->gf_slist
[i
], &ss
, sizeof(ss
)))
2259 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2261 int ip_mc_sf_allow(struct sock
*sk
, __be32 loc_addr
, __be32 rmt_addr
, int dif
)
2263 struct inet_sock
*inet
= inet_sk(sk
);
2264 struct ip_mc_socklist
*pmc
;
2265 struct ip_sf_socklist
*psl
;
2270 if (!ipv4_is_multicast(loc_addr
))
2274 for_each_pmc_rcu(inet
, pmc
) {
2275 if (pmc
->multi
.imr_multiaddr
.s_addr
== loc_addr
&&
2276 pmc
->multi
.imr_ifindex
== dif
)
2282 psl
= rcu_dereference(pmc
->sflist
);
2283 ret
= (pmc
->sfmode
== MCAST_EXCLUDE
);
2287 for (i
=0; i
<psl
->sl_count
; i
++) {
2288 if (psl
->sl_addr
[i
] == rmt_addr
)
2292 if (pmc
->sfmode
== MCAST_INCLUDE
&& i
>= psl
->sl_count
)
2294 if (pmc
->sfmode
== MCAST_EXCLUDE
&& i
< psl
->sl_count
)
2304 * A socket is closing.
2307 void ip_mc_drop_socket(struct sock
*sk
)
2309 struct inet_sock
*inet
= inet_sk(sk
);
2310 struct ip_mc_socklist
*iml
;
2311 struct net
*net
= sock_net(sk
);
2313 if (inet
->mc_list
== NULL
)
2317 while ((iml
= rtnl_dereference(inet
->mc_list
)) != NULL
) {
2318 struct in_device
*in_dev
;
2320 inet
->mc_list
= iml
->next_rcu
;
2321 in_dev
= inetdev_by_index(net
, iml
->multi
.imr_ifindex
);
2322 (void) ip_mc_leave_src(sk
, iml
, in_dev
);
2324 ip_mc_dec_group(in_dev
, iml
->multi
.imr_multiaddr
.s_addr
);
2325 /* decrease mem now to avoid the memleak warning */
2326 atomic_sub(sizeof(*iml
), &sk
->sk_omem_alloc
);
2327 call_rcu(&iml
->rcu
, ip_mc_socklist_reclaim
);
2332 /* called with rcu_read_lock() */
2333 int ip_check_mc_rcu(struct in_device
*in_dev
, __be32 mc_addr
, __be32 src_addr
, u16 proto
)
2335 struct ip_mc_list
*im
;
2336 struct ip_sf_list
*psf
;
2339 for_each_pmc_rcu(in_dev
, im
) {
2340 if (im
->multiaddr
== mc_addr
)
2343 if (im
&& proto
== IPPROTO_IGMP
) {
2347 for (psf
=im
->sources
; psf
; psf
=psf
->sf_next
) {
2348 if (psf
->sf_inaddr
== src_addr
)
2352 rv
= psf
->sf_count
[MCAST_INCLUDE
] ||
2353 psf
->sf_count
[MCAST_EXCLUDE
] !=
2354 im
->sfcount
[MCAST_EXCLUDE
];
2356 rv
= im
->sfcount
[MCAST_EXCLUDE
] != 0;
2358 rv
= 1; /* unspecified source; tentatively allow */
2363 #if defined(CONFIG_PROC_FS)
2364 struct igmp_mc_iter_state
{
2365 struct seq_net_private p
;
2366 struct net_device
*dev
;
2367 struct in_device
*in_dev
;
2370 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2372 static inline struct ip_mc_list
*igmp_mc_get_first(struct seq_file
*seq
)
2374 struct net
*net
= seq_file_net(seq
);
2375 struct ip_mc_list
*im
= NULL
;
2376 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2378 state
->in_dev
= NULL
;
2379 for_each_netdev_rcu(net
, state
->dev
) {
2380 struct in_device
*in_dev
;
2382 in_dev
= __in_dev_get_rcu(state
->dev
);
2385 im
= rcu_dereference(in_dev
->mc_list
);
2387 state
->in_dev
= in_dev
;
2394 static struct ip_mc_list
*igmp_mc_get_next(struct seq_file
*seq
, struct ip_mc_list
*im
)
2396 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2398 im
= rcu_dereference(im
->next_rcu
);
2400 state
->dev
= next_net_device_rcu(state
->dev
);
2402 state
->in_dev
= NULL
;
2405 state
->in_dev
= __in_dev_get_rcu(state
->dev
);
2408 im
= rcu_dereference(state
->in_dev
->mc_list
);
2413 static struct ip_mc_list
*igmp_mc_get_idx(struct seq_file
*seq
, loff_t pos
)
2415 struct ip_mc_list
*im
= igmp_mc_get_first(seq
);
2417 while (pos
&& (im
= igmp_mc_get_next(seq
, im
)) != NULL
)
2419 return pos
? NULL
: im
;
2422 static void *igmp_mc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2426 return *pos
? igmp_mc_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2429 static void *igmp_mc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2431 struct ip_mc_list
*im
;
2432 if (v
== SEQ_START_TOKEN
)
2433 im
= igmp_mc_get_first(seq
);
2435 im
= igmp_mc_get_next(seq
, v
);
2440 static void igmp_mc_seq_stop(struct seq_file
*seq
, void *v
)
2443 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2445 state
->in_dev
= NULL
;
2450 static int igmp_mc_seq_show(struct seq_file
*seq
, void *v
)
2452 if (v
== SEQ_START_TOKEN
)
2454 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2456 struct ip_mc_list
*im
= (struct ip_mc_list
*)v
;
2457 struct igmp_mc_iter_state
*state
= igmp_mc_seq_private(seq
);
2459 #ifdef CONFIG_IP_MULTICAST
2460 querier
= IGMP_V1_SEEN(state
->in_dev
) ? "V1" :
2461 IGMP_V2_SEEN(state
->in_dev
) ? "V2" :
2467 if (rcu_dereference(state
->in_dev
->mc_list
) == im
) {
2468 seq_printf(seq
, "%d\t%-10s: %5d %7s\n",
2469 state
->dev
->ifindex
, state
->dev
->name
, state
->in_dev
->mc_count
, querier
);
2473 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2474 im
->multiaddr
, im
->users
,
2475 im
->tm_running
, im
->tm_running
?
2476 jiffies_to_clock_t(im
->timer
.expires
-jiffies
) : 0,
2482 static const struct seq_operations igmp_mc_seq_ops
= {
2483 .start
= igmp_mc_seq_start
,
2484 .next
= igmp_mc_seq_next
,
2485 .stop
= igmp_mc_seq_stop
,
2486 .show
= igmp_mc_seq_show
,
2489 static int igmp_mc_seq_open(struct inode
*inode
, struct file
*file
)
2491 return seq_open_net(inode
, file
, &igmp_mc_seq_ops
,
2492 sizeof(struct igmp_mc_iter_state
));
2495 static const struct file_operations igmp_mc_seq_fops
= {
2496 .owner
= THIS_MODULE
,
2497 .open
= igmp_mc_seq_open
,
2499 .llseek
= seq_lseek
,
2500 .release
= seq_release_net
,
2503 struct igmp_mcf_iter_state
{
2504 struct seq_net_private p
;
2505 struct net_device
*dev
;
2506 struct in_device
*idev
;
2507 struct ip_mc_list
*im
;
2510 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2512 static inline struct ip_sf_list
*igmp_mcf_get_first(struct seq_file
*seq
)
2514 struct net
*net
= seq_file_net(seq
);
2515 struct ip_sf_list
*psf
= NULL
;
2516 struct ip_mc_list
*im
= NULL
;
2517 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2521 for_each_netdev_rcu(net
, state
->dev
) {
2522 struct in_device
*idev
;
2523 idev
= __in_dev_get_rcu(state
->dev
);
2524 if (unlikely(idev
== NULL
))
2526 im
= rcu_dereference(idev
->mc_list
);
2527 if (likely(im
!= NULL
)) {
2528 spin_lock_bh(&im
->lock
);
2530 if (likely(psf
!= NULL
)) {
2535 spin_unlock_bh(&im
->lock
);
2541 static struct ip_sf_list
*igmp_mcf_get_next(struct seq_file
*seq
, struct ip_sf_list
*psf
)
2543 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2547 spin_unlock_bh(&state
->im
->lock
);
2548 state
->im
= state
->im
->next
;
2549 while (!state
->im
) {
2550 state
->dev
= next_net_device_rcu(state
->dev
);
2555 state
->idev
= __in_dev_get_rcu(state
->dev
);
2558 state
->im
= rcu_dereference(state
->idev
->mc_list
);
2562 spin_lock_bh(&state
->im
->lock
);
2563 psf
= state
->im
->sources
;
2569 static struct ip_sf_list
*igmp_mcf_get_idx(struct seq_file
*seq
, loff_t pos
)
2571 struct ip_sf_list
*psf
= igmp_mcf_get_first(seq
);
2573 while (pos
&& (psf
= igmp_mcf_get_next(seq
, psf
)) != NULL
)
2575 return pos
? NULL
: psf
;
2578 static void *igmp_mcf_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2582 return *pos
? igmp_mcf_get_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2585 static void *igmp_mcf_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2587 struct ip_sf_list
*psf
;
2588 if (v
== SEQ_START_TOKEN
)
2589 psf
= igmp_mcf_get_first(seq
);
2591 psf
= igmp_mcf_get_next(seq
, v
);
2596 static void igmp_mcf_seq_stop(struct seq_file
*seq
, void *v
)
2599 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2600 if (likely(state
->im
!= NULL
)) {
2601 spin_unlock_bh(&state
->im
->lock
);
2609 static int igmp_mcf_seq_show(struct seq_file
*seq
, void *v
)
2611 struct ip_sf_list
*psf
= (struct ip_sf_list
*)v
;
2612 struct igmp_mcf_iter_state
*state
= igmp_mcf_seq_private(seq
);
2614 if (v
== SEQ_START_TOKEN
) {
2617 "%10s %10s %6s %6s\n", "Idx",
2619 "SRC", "INC", "EXC");
2623 "0x%08x %6lu %6lu\n",
2624 state
->dev
->ifindex
, state
->dev
->name
,
2625 ntohl(state
->im
->multiaddr
),
2626 ntohl(psf
->sf_inaddr
),
2627 psf
->sf_count
[MCAST_INCLUDE
],
2628 psf
->sf_count
[MCAST_EXCLUDE
]);
2633 static const struct seq_operations igmp_mcf_seq_ops
= {
2634 .start
= igmp_mcf_seq_start
,
2635 .next
= igmp_mcf_seq_next
,
2636 .stop
= igmp_mcf_seq_stop
,
2637 .show
= igmp_mcf_seq_show
,
2640 static int igmp_mcf_seq_open(struct inode
*inode
, struct file
*file
)
2642 return seq_open_net(inode
, file
, &igmp_mcf_seq_ops
,
2643 sizeof(struct igmp_mcf_iter_state
));
2646 static const struct file_operations igmp_mcf_seq_fops
= {
2647 .owner
= THIS_MODULE
,
2648 .open
= igmp_mcf_seq_open
,
2650 .llseek
= seq_lseek
,
2651 .release
= seq_release_net
,
2654 static int __net_init
igmp_net_init(struct net
*net
)
2656 struct proc_dir_entry
*pde
;
2658 pde
= proc_net_fops_create(net
, "igmp", S_IRUGO
, &igmp_mc_seq_fops
);
2661 pde
= proc_net_fops_create(net
, "mcfilter", S_IRUGO
, &igmp_mcf_seq_fops
);
2667 proc_net_remove(net
, "igmp");
2672 static void __net_exit
igmp_net_exit(struct net
*net
)
2674 proc_net_remove(net
, "mcfilter");
2675 proc_net_remove(net
, "igmp");
2678 static struct pernet_operations igmp_net_ops
= {
2679 .init
= igmp_net_init
,
2680 .exit
= igmp_net_exit
,
2683 int __init
igmp_mc_proc_init(void)
2685 return register_pernet_subsys(&igmp_net_ops
);