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);
315 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
316 GFP_ATOMIC | __GFP_NOWARN);
323 igmp_skb_size(skb) = size;
325 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
327 IPPROTO_IGMP, 0, dev->ifindex);
333 skb_dst_set(skb, &rt->dst);
336 skb_reserve(skb, LL_RESERVED_SPACE(dev));
338 skb_reset_network_header(skb);
340 skb_put(skb, sizeof(struct iphdr) + 4);
343 pip->ihl = (sizeof(struct iphdr)+4)>>2;
345 pip->frag_off = htons(IP_DF);
347 pip->daddr = fl4.daddr;
348 pip->saddr = fl4.saddr;
349 pip->protocol = IPPROTO_IGMP;
350 pip->tot_len = 0; /* filled in later */
351 ip_select_ident(pip, &rt->dst, NULL);
352 ((u8*)&pip[1])[0] = IPOPT_RA;
353 ((u8*)&pip[1])[1] = 4;
354 ((u8*)&pip[1])[2] = 0;
355 ((u8*)&pip[1])[3] = 0;
357 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
358 skb_put(skb, sizeof(*pig));
359 pig = igmpv3_report_hdr(skb);
360 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
368 static int igmpv3_sendpack(struct sk_buff *skb)
370 struct igmphdr *pig = igmp_hdr(skb);
371 const int igmplen = skb->tail - skb->transport_header;
373 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
375 return ip_local_out(skb);
378 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
380 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
383 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
384 int type, struct igmpv3_grec **ppgr)
386 struct net_device *dev = pmc->interface->dev;
387 struct igmpv3_report *pih;
388 struct igmpv3_grec *pgr;
391 skb = igmpv3_newpack(dev, dev->mtu);
394 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
395 pgr->grec_type = type;
396 pgr->grec_auxwords = 0;
398 pgr->grec_mca = pmc->multiaddr;
399 pih = igmpv3_report_hdr(skb);
400 pih->ngrec = htons(ntohs(pih->ngrec)+1);
405 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
406 skb_tailroom(skb)) : 0)
408 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
409 int type, int gdeleted, int sdeleted)
411 struct net_device *dev = pmc->interface->dev;
412 struct igmpv3_report *pih;
413 struct igmpv3_grec *pgr = NULL;
414 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
415 int scount, stotal, first, isquery, truncate;
417 if (pmc->multiaddr == IGMP_ALL_HOSTS)
420 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
421 type == IGMPV3_MODE_IS_EXCLUDE;
422 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
423 type == IGMPV3_CHANGE_TO_EXCLUDE;
427 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
432 pih = skb ? igmpv3_report_hdr(skb) : NULL;
434 /* EX and TO_EX get a fresh packet, if needed */
436 if (pih && pih->ngrec &&
437 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
439 igmpv3_sendpack(skb);
440 skb = igmpv3_newpack(dev, dev->mtu);
445 for (psf=*psf_list; psf; psf=psf_next) {
448 psf_next = psf->sf_next;
450 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
455 /* clear marks on query responses */
459 if (AVAILABLE(skb) < sizeof(__be32) +
460 first*sizeof(struct igmpv3_grec)) {
461 if (truncate && !first)
462 break; /* truncate these */
464 pgr->grec_nsrcs = htons(scount);
466 igmpv3_sendpack(skb);
467 skb = igmpv3_newpack(dev, dev->mtu);
472 skb = add_grhead(skb, pmc, type, &pgr);
477 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
478 *psrc = psf->sf_inaddr;
480 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
481 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
483 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
485 psf_prev->sf_next = psf->sf_next;
487 *psf_list = psf->sf_next;
497 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
498 type == IGMPV3_BLOCK_OLD_SOURCES)
500 if (pmc->crcount || isquery) {
501 /* make sure we have room for group header */
502 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
503 igmpv3_sendpack(skb);
504 skb = NULL; /* add_grhead will get a new one */
506 skb = add_grhead(skb, pmc, type, &pgr);
510 pgr->grec_nsrcs = htons(scount);
513 pmc->gsquery = 0; /* clear query state on report */
517 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
519 struct sk_buff *skb = NULL;
524 for_each_pmc_rcu(in_dev, pmc) {
525 if (pmc->multiaddr == IGMP_ALL_HOSTS)
527 spin_lock_bh(&pmc->lock);
528 if (pmc->sfcount[MCAST_EXCLUDE])
529 type = IGMPV3_MODE_IS_EXCLUDE;
531 type = IGMPV3_MODE_IS_INCLUDE;
532 skb = add_grec(skb, pmc, type, 0, 0);
533 spin_unlock_bh(&pmc->lock);
537 spin_lock_bh(&pmc->lock);
538 if (pmc->sfcount[MCAST_EXCLUDE])
539 type = IGMPV3_MODE_IS_EXCLUDE;
541 type = IGMPV3_MODE_IS_INCLUDE;
542 skb = add_grec(skb, pmc, type, 0, 0);
543 spin_unlock_bh(&pmc->lock);
547 return igmpv3_sendpack(skb);
551 * remove zero-count source records from a source filter list
553 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
555 struct ip_sf_list *psf_prev, *psf_next, *psf;
558 for (psf=*ppsf; psf; psf = psf_next) {
559 psf_next = psf->sf_next;
560 if (psf->sf_crcount == 0) {
562 psf_prev->sf_next = psf->sf_next;
564 *ppsf = psf->sf_next;
571 static void igmpv3_send_cr(struct in_device *in_dev)
573 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
574 struct sk_buff *skb = NULL;
578 spin_lock_bh(&in_dev->mc_tomb_lock);
582 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
583 pmc_next = pmc->next;
584 if (pmc->sfmode == MCAST_INCLUDE) {
585 type = IGMPV3_BLOCK_OLD_SOURCES;
586 dtype = IGMPV3_BLOCK_OLD_SOURCES;
587 skb = add_grec(skb, pmc, type, 1, 0);
588 skb = add_grec(skb, pmc, dtype, 1, 1);
591 if (pmc->sfmode == MCAST_EXCLUDE) {
592 type = IGMPV3_CHANGE_TO_INCLUDE;
593 skb = add_grec(skb, pmc, type, 1, 0);
596 if (pmc->crcount == 0) {
597 igmpv3_clear_zeros(&pmc->tomb);
598 igmpv3_clear_zeros(&pmc->sources);
601 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
603 pmc_prev->next = pmc_next;
605 in_dev->mc_tomb = pmc_next;
606 in_dev_put(pmc->interface);
611 spin_unlock_bh(&in_dev->mc_tomb_lock);
614 for_each_pmc_rcu(in_dev, pmc) {
615 spin_lock_bh(&pmc->lock);
616 if (pmc->sfcount[MCAST_EXCLUDE]) {
617 type = IGMPV3_BLOCK_OLD_SOURCES;
618 dtype = IGMPV3_ALLOW_NEW_SOURCES;
620 type = IGMPV3_ALLOW_NEW_SOURCES;
621 dtype = IGMPV3_BLOCK_OLD_SOURCES;
623 skb = add_grec(skb, pmc, type, 0, 0);
624 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
626 /* filter mode changes */
628 if (pmc->sfmode == MCAST_EXCLUDE)
629 type = IGMPV3_CHANGE_TO_EXCLUDE;
631 type = IGMPV3_CHANGE_TO_INCLUDE;
632 skb = add_grec(skb, pmc, type, 0, 0);
635 spin_unlock_bh(&pmc->lock);
641 (void) igmpv3_sendpack(skb);
644 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
651 struct net_device *dev = in_dev->dev;
652 struct net *net = dev_net(dev);
653 __be32 group = pmc ? pmc->multiaddr : 0;
657 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
658 return igmpv3_send_report(in_dev, pmc);
659 else if (type == IGMP_HOST_LEAVE_MESSAGE)
660 dst = IGMP_ALL_ROUTER;
664 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
666 IPPROTO_IGMP, 0, dev->ifindex);
670 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
676 skb_dst_set(skb, &rt->dst);
678 skb_reserve(skb, LL_RESERVED_SPACE(dev));
680 skb_reset_network_header(skb);
682 skb_put(skb, sizeof(struct iphdr) + 4);
685 iph->ihl = (sizeof(struct iphdr)+4)>>2;
687 iph->frag_off = htons(IP_DF);
690 iph->saddr = fl4.saddr;
691 iph->protocol = IPPROTO_IGMP;
692 ip_select_ident(iph, &rt->dst, NULL);
693 ((u8*)&iph[1])[0] = IPOPT_RA;
694 ((u8*)&iph[1])[1] = 4;
695 ((u8*)&iph[1])[2] = 0;
696 ((u8*)&iph[1])[3] = 0;
698 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
703 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
705 return ip_local_out(skb);
708 static void igmp_gq_timer_expire(unsigned long data)
710 struct in_device *in_dev = (struct in_device *)data;
712 in_dev->mr_gq_running = 0;
713 igmpv3_send_report(in_dev, NULL);
714 __in_dev_put(in_dev);
717 static void igmp_ifc_timer_expire(unsigned long data)
719 struct in_device *in_dev = (struct in_device *)data;
721 igmpv3_send_cr(in_dev);
722 if (in_dev->mr_ifc_count) {
723 in_dev->mr_ifc_count--;
724 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
726 __in_dev_put(in_dev);
729 static void igmp_ifc_event(struct in_device *in_dev)
731 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
733 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
734 IGMP_Unsolicited_Report_Count;
735 igmp_ifc_start_timer(in_dev, 1);
739 static void igmp_timer_expire(unsigned long data)
741 struct ip_mc_list *im=(struct ip_mc_list *)data;
742 struct in_device *in_dev = im->interface;
744 spin_lock(&im->lock);
747 if (im->unsolicit_count) {
748 im->unsolicit_count--;
749 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
752 spin_unlock(&im->lock);
754 if (IGMP_V1_SEEN(in_dev))
755 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
756 else if (IGMP_V2_SEEN(in_dev))
757 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
759 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
764 /* mark EXCLUDE-mode sources */
765 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
767 struct ip_sf_list *psf;
771 for (psf=pmc->sources; psf; psf=psf->sf_next) {
774 for (i=0; i<nsrcs; i++) {
775 /* skip inactive filters */
776 if (pmc->sfcount[MCAST_INCLUDE] ||
777 pmc->sfcount[MCAST_EXCLUDE] !=
778 psf->sf_count[MCAST_EXCLUDE])
780 if (srcs[i] == psf->sf_inaddr) {
787 if (scount == nsrcs) /* all sources excluded */
792 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
794 struct ip_sf_list *psf;
797 if (pmc->sfmode == MCAST_EXCLUDE)
798 return igmp_xmarksources(pmc, nsrcs, srcs);
800 /* mark INCLUDE-mode sources */
802 for (psf=pmc->sources; psf; psf=psf->sf_next) {
805 for (i=0; i<nsrcs; i++)
806 if (srcs[i] == psf->sf_inaddr) {
820 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
822 struct ip_mc_list *im;
824 /* Timers are only set for non-local groups */
826 if (group == IGMP_ALL_HOSTS)
830 for_each_pmc_rcu(in_dev, im) {
831 if (im->multiaddr == group) {
839 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
842 struct igmphdr *ih = igmp_hdr(skb);
843 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
844 struct ip_mc_list *im;
845 __be32 group = ih->group;
852 /* Alas, old v1 router presents here. */
854 max_delay = IGMP_Query_Response_Interval;
855 in_dev->mr_v1_seen = jiffies +
856 IGMP_V1_Router_Present_Timeout;
859 /* v2 router present */
860 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
861 in_dev->mr_v2_seen = jiffies +
862 IGMP_V2_Router_Present_Timeout;
864 /* cancel the interface change timer */
865 in_dev->mr_ifc_count = 0;
866 if (del_timer(&in_dev->mr_ifc_timer))
867 __in_dev_put(in_dev);
868 /* clear deleted report items */
869 igmpv3_clear_delrec(in_dev);
870 } else if (len < 12) {
871 return; /* ignore bogus packet; freed by caller */
872 } else if (IGMP_V1_SEEN(in_dev)) {
873 /* This is a v3 query with v1 queriers present */
874 max_delay = IGMP_Query_Response_Interval;
876 } else if (IGMP_V2_SEEN(in_dev)) {
877 /* this is a v3 query with v2 queriers present;
878 * Interpretation of the max_delay code is problematic here.
879 * A real v2 host would use ih_code directly, while v3 has a
880 * different encoding. We use the v3 encoding as more likely
881 * to be intended in a v3 query.
883 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
885 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
888 ih3 = igmpv3_query_hdr(skb);
890 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
891 + ntohs(ih3->nsrcs)*sizeof(__be32)))
893 ih3 = igmpv3_query_hdr(skb);
896 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
898 max_delay = 1; /* can't mod w/ 0 */
899 in_dev->mr_maxdelay = max_delay;
901 in_dev->mr_qrv = ih3->qrv;
902 if (!group) { /* general query */
904 return; /* no sources allowed */
905 igmp_gq_start_timer(in_dev);
908 /* mark sources to include, if group & source-specific */
909 mark = ih3->nsrcs != 0;
913 * - Start the timers in all of our membership records
914 * that the query applies to for the interface on
915 * which the query arrived excl. those that belong
916 * to a "local" group (224.0.0.X)
917 * - For timers already running check if they need to
919 * - Use the igmp->igmp_code field as the maximum
923 for_each_pmc_rcu(in_dev, im) {
926 if (group && group != im->multiaddr)
928 if (im->multiaddr == IGMP_ALL_HOSTS)
930 spin_lock_bh(&im->lock);
932 im->gsquery = im->gsquery && mark;
935 changed = !im->gsquery ||
936 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
937 spin_unlock_bh(&im->lock);
939 igmp_mod_timer(im, max_delay);
944 /* called in rcu_read_lock() section */
945 int igmp_rcv(struct sk_buff *skb)
947 /* This basically follows the spec line by line -- see RFC1112 */
949 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
955 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
958 switch (skb->ip_summed) {
959 case CHECKSUM_COMPLETE:
960 if (!csum_fold(skb->csum))
965 if (__skb_checksum_complete(skb))
971 case IGMP_HOST_MEMBERSHIP_QUERY:
972 igmp_heard_query(in_dev, skb, len);
974 case IGMP_HOST_MEMBERSHIP_REPORT:
975 case IGMPV2_HOST_MEMBERSHIP_REPORT:
976 /* Is it our report looped back? */
977 if (rt_is_output_route(skb_rtable(skb)))
979 /* don't rely on MC router hearing unicast reports */
980 if (skb->pkt_type == PACKET_MULTICAST ||
981 skb->pkt_type == PACKET_BROADCAST)
982 igmp_heard_report(in_dev, ih->group);
985 #ifdef CONFIG_IP_PIMSM_V1
986 return pim_rcv_v1(skb);
988 case IGMPV3_HOST_MEMBERSHIP_REPORT:
991 case IGMP_HOST_LEAVE_MESSAGE:
993 case IGMP_MTRACE_RESP:
1008 * Add a filter to a device
1011 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1013 char buf[MAX_ADDR_LEN];
1014 struct net_device *dev = in_dev->dev;
1016 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1017 We will get multicast token leakage, when IFF_MULTICAST
1018 is changed. This check should be done in dev->set_multicast_list
1019 routine. Something sort of:
1020 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1023 if (arp_mc_map(addr, buf, dev, 0) == 0)
1024 dev_mc_add(dev, buf);
1028 * Remove a filter from a device
1031 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1033 char buf[MAX_ADDR_LEN];
1034 struct net_device *dev = in_dev->dev;
1036 if (arp_mc_map(addr, buf, dev, 0) == 0)
1037 dev_mc_del(dev, buf);
1040 #ifdef CONFIG_IP_MULTICAST
1042 * deleted ip_mc_list manipulation
1044 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1046 struct ip_mc_list *pmc;
1048 /* this is an "ip_mc_list" for convenience; only the fields below
1049 * are actually used. In particular, the refcnt and users are not
1050 * used for management of the delete list. Using the same structure
1051 * for deleted items allows change reports to use common code with
1052 * non-deleted or query-response MCA's.
1054 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1057 spin_lock_bh(&im->lock);
1058 pmc->interface = im->interface;
1059 in_dev_hold(in_dev);
1060 pmc->multiaddr = im->multiaddr;
1061 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1062 IGMP_Unsolicited_Report_Count;
1063 pmc->sfmode = im->sfmode;
1064 if (pmc->sfmode == MCAST_INCLUDE) {
1065 struct ip_sf_list *psf;
1067 pmc->tomb = im->tomb;
1068 pmc->sources = im->sources;
1069 im->tomb = im->sources = NULL;
1070 for (psf=pmc->sources; psf; psf=psf->sf_next)
1071 psf->sf_crcount = pmc->crcount;
1073 spin_unlock_bh(&im->lock);
1075 spin_lock_bh(&in_dev->mc_tomb_lock);
1076 pmc->next = in_dev->mc_tomb;
1077 in_dev->mc_tomb = pmc;
1078 spin_unlock_bh(&in_dev->mc_tomb_lock);
1081 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1083 struct ip_mc_list *pmc, *pmc_prev;
1084 struct ip_sf_list *psf, *psf_next;
1086 spin_lock_bh(&in_dev->mc_tomb_lock);
1088 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1089 if (pmc->multiaddr == multiaddr)
1095 pmc_prev->next = pmc->next;
1097 in_dev->mc_tomb = pmc->next;
1099 spin_unlock_bh(&in_dev->mc_tomb_lock);
1101 for (psf=pmc->tomb; psf; psf=psf_next) {
1102 psf_next = psf->sf_next;
1105 in_dev_put(pmc->interface);
1110 static void igmpv3_clear_delrec(struct in_device *in_dev)
1112 struct ip_mc_list *pmc, *nextpmc;
1114 spin_lock_bh(&in_dev->mc_tomb_lock);
1115 pmc = in_dev->mc_tomb;
1116 in_dev->mc_tomb = NULL;
1117 spin_unlock_bh(&in_dev->mc_tomb_lock);
1119 for (; pmc; pmc = nextpmc) {
1120 nextpmc = pmc->next;
1121 ip_mc_clear_src(pmc);
1122 in_dev_put(pmc->interface);
1125 /* clear dead sources, too */
1127 for_each_pmc_rcu(in_dev, pmc) {
1128 struct ip_sf_list *psf, *psf_next;
1130 spin_lock_bh(&pmc->lock);
1133 spin_unlock_bh(&pmc->lock);
1134 for (; psf; psf=psf_next) {
1135 psf_next = psf->sf_next;
1143 static void igmp_group_dropped(struct ip_mc_list *im)
1145 struct in_device *in_dev = im->interface;
1146 #ifdef CONFIG_IP_MULTICAST
1152 ip_mc_filter_del(in_dev, im->multiaddr);
1155 #ifdef CONFIG_IP_MULTICAST
1156 if (im->multiaddr == IGMP_ALL_HOSTS)
1159 reporter = im->reporter;
1160 igmp_stop_timer(im);
1162 if (!in_dev->dead) {
1163 if (IGMP_V1_SEEN(in_dev))
1165 if (IGMP_V2_SEEN(in_dev)) {
1167 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1171 igmpv3_add_delrec(in_dev, im);
1173 igmp_ifc_event(in_dev);
1177 ip_mc_clear_src(im);
1180 static void igmp_group_added(struct ip_mc_list *im)
1182 struct in_device *in_dev = im->interface;
1184 if (im->loaded == 0) {
1186 ip_mc_filter_add(in_dev, im->multiaddr);
1189 #ifdef CONFIG_IP_MULTICAST
1190 if (im->multiaddr == IGMP_ALL_HOSTS)
1195 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1196 spin_lock_bh(&im->lock);
1197 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1198 spin_unlock_bh(&im->lock);
1203 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1204 IGMP_Unsolicited_Report_Count;
1205 igmp_ifc_event(in_dev);
1211 * Multicast list managers
1216 * A socket has joined a multicast group on device dev.
1219 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1221 struct ip_mc_list *im;
1225 for_each_pmc_rtnl(in_dev, im) {
1226 if (im->multiaddr == addr) {
1228 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1233 im = kzalloc(sizeof(*im), GFP_KERNEL);
1238 im->interface = in_dev;
1239 in_dev_hold(in_dev);
1240 im->multiaddr = addr;
1241 /* initial mode is (EX, empty) */
1242 im->sfmode = MCAST_EXCLUDE;
1243 im->sfcount[MCAST_EXCLUDE] = 1;
1244 atomic_set(&im->refcnt, 1);
1245 spin_lock_init(&im->lock);
1246 #ifdef CONFIG_IP_MULTICAST
1247 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1248 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1251 im->next_rcu = in_dev->mc_list;
1253 rcu_assign_pointer(in_dev->mc_list, im);
1255 #ifdef CONFIG_IP_MULTICAST
1256 igmpv3_del_delrec(in_dev, im->multiaddr);
1258 igmp_group_added(im);
1260 ip_rt_multicast_event(in_dev);
1264 EXPORT_SYMBOL(ip_mc_inc_group);
1267 * Resend IGMP JOIN report; used for bonding.
1268 * Called with rcu_read_lock()
1270 void ip_mc_rejoin_groups(struct in_device *in_dev)
1272 #ifdef CONFIG_IP_MULTICAST
1273 struct ip_mc_list *im;
1276 for_each_pmc_rcu(in_dev, im) {
1277 if (im->multiaddr == IGMP_ALL_HOSTS)
1280 /* a failover is happening and switches
1281 * must be notified immediately
1283 if (IGMP_V1_SEEN(in_dev))
1284 type = IGMP_HOST_MEMBERSHIP_REPORT;
1285 else if (IGMP_V2_SEEN(in_dev))
1286 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1288 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1289 igmp_send_report(in_dev, im, type);
1293 EXPORT_SYMBOL(ip_mc_rejoin_groups);
1296 * A socket has left a multicast group on device dev
1299 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1301 struct ip_mc_list *i;
1302 struct ip_mc_list __rcu **ip;
1306 for (ip = &in_dev->mc_list;
1307 (i = rtnl_dereference(*ip)) != NULL;
1308 ip = &i->next_rcu) {
1309 if (i->multiaddr == addr) {
1310 if (--i->users == 0) {
1313 igmp_group_dropped(i);
1316 ip_rt_multicast_event(in_dev);
1325 EXPORT_SYMBOL(ip_mc_dec_group);
1327 /* Device changing type */
1329 void ip_mc_unmap(struct in_device *in_dev)
1331 struct ip_mc_list *pmc;
1335 for_each_pmc_rtnl(in_dev, pmc)
1336 igmp_group_dropped(pmc);
1339 void ip_mc_remap(struct in_device *in_dev)
1341 struct ip_mc_list *pmc;
1345 for_each_pmc_rtnl(in_dev, pmc)
1346 igmp_group_added(pmc);
1349 /* Device going down */
1351 void ip_mc_down(struct in_device *in_dev)
1353 struct ip_mc_list *pmc;
1357 for_each_pmc_rtnl(in_dev, pmc)
1358 igmp_group_dropped(pmc);
1360 #ifdef CONFIG_IP_MULTICAST
1361 in_dev->mr_ifc_count = 0;
1362 if (del_timer(&in_dev->mr_ifc_timer))
1363 __in_dev_put(in_dev);
1364 in_dev->mr_gq_running = 0;
1365 if (del_timer(&in_dev->mr_gq_timer))
1366 __in_dev_put(in_dev);
1367 igmpv3_clear_delrec(in_dev);
1370 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1373 void ip_mc_init_dev(struct in_device *in_dev)
1377 in_dev->mc_tomb = NULL;
1378 #ifdef CONFIG_IP_MULTICAST
1379 in_dev->mr_gq_running = 0;
1380 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1381 (unsigned long)in_dev);
1382 in_dev->mr_ifc_count = 0;
1383 in_dev->mc_count = 0;
1384 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1385 (unsigned long)in_dev);
1386 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1389 spin_lock_init(&in_dev->mc_tomb_lock);
1392 /* Device going up */
1394 void ip_mc_up(struct in_device *in_dev)
1396 struct ip_mc_list *pmc;
1400 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1402 for_each_pmc_rtnl(in_dev, pmc)
1403 igmp_group_added(pmc);
1407 * Device is about to be destroyed: clean up.
1410 void ip_mc_destroy_dev(struct in_device *in_dev)
1412 struct ip_mc_list *i;
1416 /* Deactivate timers */
1419 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1420 in_dev->mc_list = i->next_rcu;
1423 igmp_group_dropped(i);
1428 /* RTNL is locked */
1429 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1431 struct net_device *dev = NULL;
1432 struct in_device *idev = NULL;
1434 if (imr->imr_ifindex) {
1435 idev = inetdev_by_index(net, imr->imr_ifindex);
1438 if (imr->imr_address.s_addr) {
1439 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1445 struct rtable *rt = ip_route_output(net,
1446 imr->imr_multiaddr.s_addr,
1454 imr->imr_ifindex = dev->ifindex;
1455 idev = __in_dev_get_rtnl(dev);
1461 * Join a socket to a group
1463 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1464 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1467 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1470 struct ip_sf_list *psf, *psf_prev;
1474 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1475 if (psf->sf_inaddr == *psfsrc)
1479 if (!psf || psf->sf_count[sfmode] == 0) {
1480 /* source filter not found, or count wrong => bug */
1483 psf->sf_count[sfmode]--;
1484 if (psf->sf_count[sfmode] == 0) {
1485 ip_rt_multicast_event(pmc->interface);
1487 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1488 #ifdef CONFIG_IP_MULTICAST
1489 struct in_device *in_dev = pmc->interface;
1492 /* no more filters for this source */
1494 psf_prev->sf_next = psf->sf_next;
1496 pmc->sources = psf->sf_next;
1497 #ifdef CONFIG_IP_MULTICAST
1498 if (psf->sf_oldin &&
1499 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1500 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1501 IGMP_Unsolicited_Report_Count;
1502 psf->sf_next = pmc->tomb;
1512 #ifndef CONFIG_IP_MULTICAST
1513 #define igmp_ifc_event(x) do { } while (0)
1516 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1517 int sfcount, __be32 *psfsrc, int delta)
1519 struct ip_mc_list *pmc;
1526 for_each_pmc_rcu(in_dev, pmc) {
1527 if (*pmca == pmc->multiaddr)
1531 /* MCA not found?? bug */
1535 spin_lock_bh(&pmc->lock);
1537 #ifdef CONFIG_IP_MULTICAST
1542 if (!pmc->sfcount[sfmode])
1544 pmc->sfcount[sfmode]--;
1547 for (i=0; i<sfcount; i++) {
1548 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1550 changerec |= rv > 0;
1554 if (pmc->sfmode == MCAST_EXCLUDE &&
1555 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1556 pmc->sfcount[MCAST_INCLUDE]) {
1557 #ifdef CONFIG_IP_MULTICAST
1558 struct ip_sf_list *psf;
1561 /* filter mode change */
1562 pmc->sfmode = MCAST_INCLUDE;
1563 #ifdef CONFIG_IP_MULTICAST
1564 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1565 IGMP_Unsolicited_Report_Count;
1566 in_dev->mr_ifc_count = pmc->crcount;
1567 for (psf=pmc->sources; psf; psf = psf->sf_next)
1568 psf->sf_crcount = 0;
1569 igmp_ifc_event(pmc->interface);
1570 } else if (sf_setstate(pmc) || changerec) {
1571 igmp_ifc_event(pmc->interface);
1575 spin_unlock_bh(&pmc->lock);
1580 * Add multicast single-source filter to the interface list
1582 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1583 __be32 *psfsrc, int delta)
1585 struct ip_sf_list *psf, *psf_prev;
1588 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1589 if (psf->sf_inaddr == *psfsrc)
1594 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1597 psf->sf_inaddr = *psfsrc;
1599 psf_prev->sf_next = psf;
1603 psf->sf_count[sfmode]++;
1604 if (psf->sf_count[sfmode] == 1) {
1605 ip_rt_multicast_event(pmc->interface);
1610 #ifdef CONFIG_IP_MULTICAST
1611 static void sf_markstate(struct ip_mc_list *pmc)
1613 struct ip_sf_list *psf;
1614 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1616 for (psf=pmc->sources; psf; psf=psf->sf_next)
1617 if (pmc->sfcount[MCAST_EXCLUDE]) {
1618 psf->sf_oldin = mca_xcount ==
1619 psf->sf_count[MCAST_EXCLUDE] &&
1620 !psf->sf_count[MCAST_INCLUDE];
1622 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1625 static int sf_setstate(struct ip_mc_list *pmc)
1627 struct ip_sf_list *psf, *dpsf;
1628 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1629 int qrv = pmc->interface->mr_qrv;
1633 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1634 if (pmc->sfcount[MCAST_EXCLUDE]) {
1635 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1636 !psf->sf_count[MCAST_INCLUDE];
1638 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1640 if (!psf->sf_oldin) {
1641 struct ip_sf_list *prev = NULL;
1643 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1644 if (dpsf->sf_inaddr == psf->sf_inaddr)
1650 prev->sf_next = dpsf->sf_next;
1652 pmc->tomb = dpsf->sf_next;
1655 psf->sf_crcount = qrv;
1658 } else if (psf->sf_oldin) {
1660 psf->sf_crcount = 0;
1662 * add or update "delete" records if an active filter
1665 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1666 if (dpsf->sf_inaddr == psf->sf_inaddr)
1669 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1673 /* pmc->lock held by callers */
1674 dpsf->sf_next = pmc->tomb;
1677 dpsf->sf_crcount = qrv;
1686 * Add multicast source filter list to the interface list
1688 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1689 int sfcount, __be32 *psfsrc, int delta)
1691 struct ip_mc_list *pmc;
1698 for_each_pmc_rcu(in_dev, pmc) {
1699 if (*pmca == pmc->multiaddr)
1703 /* MCA not found?? bug */
1707 spin_lock_bh(&pmc->lock);
1710 #ifdef CONFIG_IP_MULTICAST
1713 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1715 pmc->sfcount[sfmode]++;
1717 for (i=0; i<sfcount; i++) {
1718 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1725 pmc->sfcount[sfmode]--;
1727 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1728 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1729 #ifdef CONFIG_IP_MULTICAST
1730 struct ip_sf_list *psf;
1731 in_dev = pmc->interface;
1734 /* filter mode change */
1735 if (pmc->sfcount[MCAST_EXCLUDE])
1736 pmc->sfmode = MCAST_EXCLUDE;
1737 else if (pmc->sfcount[MCAST_INCLUDE])
1738 pmc->sfmode = MCAST_INCLUDE;
1739 #ifdef CONFIG_IP_MULTICAST
1740 /* else no filters; keep old mode for reports */
1742 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1743 IGMP_Unsolicited_Report_Count;
1744 in_dev->mr_ifc_count = pmc->crcount;
1745 for (psf=pmc->sources; psf; psf = psf->sf_next)
1746 psf->sf_crcount = 0;
1747 igmp_ifc_event(in_dev);
1748 } else if (sf_setstate(pmc)) {
1749 igmp_ifc_event(in_dev);
1752 spin_unlock_bh(&pmc->lock);
1756 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1758 struct ip_sf_list *psf, *nextpsf;
1760 for (psf=pmc->tomb; psf; psf=nextpsf) {
1761 nextpsf = psf->sf_next;
1765 for (psf=pmc->sources; psf; psf=nextpsf) {
1766 nextpsf = psf->sf_next;
1769 pmc->sources = NULL;
1770 pmc->sfmode = MCAST_EXCLUDE;
1771 pmc->sfcount[MCAST_INCLUDE] = 0;
1772 pmc->sfcount[MCAST_EXCLUDE] = 1;
1777 * Join a multicast group
1779 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1782 __be32 addr = imr->imr_multiaddr.s_addr;
1783 struct ip_mc_socklist *iml = NULL, *i;
1784 struct in_device *in_dev;
1785 struct inet_sock *inet = inet_sk(sk);
1786 struct net *net = sock_net(sk);
1790 if (!ipv4_is_multicast(addr))
1795 in_dev = ip_mc_find_dev(net, imr);
1804 ifindex = imr->imr_ifindex;
1805 for_each_pmc_rtnl(inet, i) {
1806 if (i->multi.imr_multiaddr.s_addr == addr &&
1807 i->multi.imr_ifindex == ifindex)
1812 if (count >= sysctl_igmp_max_memberships)
1814 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1818 memcpy(&iml->multi, imr, sizeof(*imr));
1819 iml->next_rcu = inet->mc_list;
1821 iml->sfmode = MCAST_EXCLUDE;
1822 rcu_assign_pointer(inet->mc_list, iml);
1823 ip_mc_inc_group(in_dev, addr);
1829 EXPORT_SYMBOL(ip_mc_join_group);
1831 static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1833 kfree(container_of(rp, struct ip_sf_socklist, rcu));
1834 /* sk_omem_alloc should have been decreased by the caller*/
1837 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1838 struct in_device *in_dev)
1840 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1844 /* any-source empty exclude case */
1845 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1846 iml->sfmode, 0, NULL, 0);
1848 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1849 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1850 rcu_assign_pointer(iml->sflist, NULL);
1851 /* decrease mem now to avoid the memleak warning */
1852 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1853 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
1858 static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1860 kfree(container_of(rp, struct ip_mc_socklist, rcu));
1861 /* sk_omem_alloc should have been decreased by the caller*/
1866 * Ask a socket to leave a group.
1869 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1871 struct inet_sock *inet = inet_sk(sk);
1872 struct ip_mc_socklist *iml;
1873 struct ip_mc_socklist __rcu **imlp;
1874 struct in_device *in_dev;
1875 struct net *net = sock_net(sk);
1876 __be32 group = imr->imr_multiaddr.s_addr;
1878 int ret = -EADDRNOTAVAIL;
1881 in_dev = ip_mc_find_dev(net, imr);
1882 ifindex = imr->imr_ifindex;
1883 for (imlp = &inet->mc_list;
1884 (iml = rtnl_dereference(*imlp)) != NULL;
1885 imlp = &iml->next_rcu) {
1886 if (iml->multi.imr_multiaddr.s_addr != group)
1889 if (iml->multi.imr_ifindex != ifindex)
1891 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1892 iml->multi.imr_address.s_addr)
1895 (void) ip_mc_leave_src(sk, iml, in_dev);
1897 *imlp = iml->next_rcu;
1900 ip_mc_dec_group(in_dev, group);
1902 /* decrease mem now to avoid the memleak warning */
1903 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1904 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
1913 int ip_mc_source(int add, int omode, struct sock *sk, struct
1914 ip_mreq_source *mreqs, int ifindex)
1917 struct ip_mreqn imr;
1918 __be32 addr = mreqs->imr_multiaddr;
1919 struct ip_mc_socklist *pmc;
1920 struct in_device *in_dev = NULL;
1921 struct inet_sock *inet = inet_sk(sk);
1922 struct ip_sf_socklist *psl;
1923 struct net *net = sock_net(sk);
1927 if (!ipv4_is_multicast(addr))
1932 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1933 imr.imr_address.s_addr = mreqs->imr_interface;
1934 imr.imr_ifindex = ifindex;
1935 in_dev = ip_mc_find_dev(net, &imr);
1941 err = -EADDRNOTAVAIL;
1943 for_each_pmc_rtnl(inet, pmc) {
1944 if ((pmc->multi.imr_multiaddr.s_addr ==
1945 imr.imr_multiaddr.s_addr) &&
1946 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1949 if (!pmc) { /* must have a prior join */
1953 /* if a source filter was set, must be the same mode as before */
1955 if (pmc->sfmode != omode) {
1959 } else if (pmc->sfmode != omode) {
1960 /* allow mode switches for empty-set filters */
1961 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1962 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1964 pmc->sfmode = omode;
1967 psl = rtnl_dereference(pmc->sflist);
1970 goto done; /* err = -EADDRNOTAVAIL */
1972 for (i=0; i<psl->sl_count; i++) {
1973 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1978 if (rv) /* source not found */
1979 goto done; /* err = -EADDRNOTAVAIL */
1981 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1982 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1987 /* update the interface filter */
1988 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1989 &mreqs->imr_sourceaddr, 1);
1991 for (j=i+1; j<psl->sl_count; j++)
1992 psl->sl_addr[j-1] = psl->sl_addr[j];
1997 /* else, add a new source to the filter */
1999 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2003 if (!psl || psl->sl_count == psl->sl_max) {
2004 struct ip_sf_socklist *newpsl;
2005 int count = IP_SFBLOCK;
2008 count += psl->sl_max;
2009 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2014 newpsl->sl_max = count;
2015 newpsl->sl_count = count - IP_SFBLOCK;
2017 for (i=0; i<psl->sl_count; i++)
2018 newpsl->sl_addr[i] = psl->sl_addr[i];
2019 /* decrease mem now to avoid the memleak warning */
2020 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2021 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2023 rcu_assign_pointer(pmc->sflist, newpsl);
2026 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2027 for (i=0; i<psl->sl_count; i++) {
2028 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2033 if (rv == 0) /* address already there is an error */
2035 for (j=psl->sl_count-1; j>=i; j--)
2036 psl->sl_addr[j+1] = psl->sl_addr[j];
2037 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2040 /* update the interface list */
2041 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2042 &mreqs->imr_sourceaddr, 1);
2046 return ip_mc_leave_group(sk, &imr);
2050 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2053 struct ip_mreqn imr;
2054 __be32 addr = msf->imsf_multiaddr;
2055 struct ip_mc_socklist *pmc;
2056 struct in_device *in_dev;
2057 struct inet_sock *inet = inet_sk(sk);
2058 struct ip_sf_socklist *newpsl, *psl;
2059 struct net *net = sock_net(sk);
2062 if (!ipv4_is_multicast(addr))
2064 if (msf->imsf_fmode != MCAST_INCLUDE &&
2065 msf->imsf_fmode != MCAST_EXCLUDE)
2070 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2071 imr.imr_address.s_addr = msf->imsf_interface;
2072 imr.imr_ifindex = ifindex;
2073 in_dev = ip_mc_find_dev(net, &imr);
2080 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2081 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2086 for_each_pmc_rtnl(inet, pmc) {
2087 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2088 pmc->multi.imr_ifindex == imr.imr_ifindex)
2091 if (!pmc) { /* must have a prior join */
2095 if (msf->imsf_numsrc) {
2096 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2102 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2103 memcpy(newpsl->sl_addr, msf->imsf_slist,
2104 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2105 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2106 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2108 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2113 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2114 msf->imsf_fmode, 0, NULL, 0);
2116 psl = rtnl_dereference(pmc->sflist);
2118 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2119 psl->sl_count, psl->sl_addr, 0);
2120 /* decrease mem now to avoid the memleak warning */
2121 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2122 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2124 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2126 rcu_assign_pointer(pmc->sflist, newpsl);
2127 pmc->sfmode = msf->imsf_fmode;
2132 err = ip_mc_leave_group(sk, &imr);
2136 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2137 struct ip_msfilter __user *optval, int __user *optlen)
2139 int err, len, count, copycount;
2140 struct ip_mreqn imr;
2141 __be32 addr = msf->imsf_multiaddr;
2142 struct ip_mc_socklist *pmc;
2143 struct in_device *in_dev;
2144 struct inet_sock *inet = inet_sk(sk);
2145 struct ip_sf_socklist *psl;
2146 struct net *net = sock_net(sk);
2148 if (!ipv4_is_multicast(addr))
2153 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2154 imr.imr_address.s_addr = msf->imsf_interface;
2155 imr.imr_ifindex = 0;
2156 in_dev = ip_mc_find_dev(net, &imr);
2162 err = -EADDRNOTAVAIL;
2164 for_each_pmc_rtnl(inet, pmc) {
2165 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2166 pmc->multi.imr_ifindex == imr.imr_ifindex)
2169 if (!pmc) /* must have a prior join */
2171 msf->imsf_fmode = pmc->sfmode;
2172 psl = rtnl_dereference(pmc->sflist);
2178 count = psl->sl_count;
2180 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2181 len = copycount * sizeof(psl->sl_addr[0]);
2182 msf->imsf_numsrc = count;
2183 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2184 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2188 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2196 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2197 struct group_filter __user *optval, int __user *optlen)
2199 int err, i, count, copycount;
2200 struct sockaddr_in *psin;
2202 struct ip_mc_socklist *pmc;
2203 struct inet_sock *inet = inet_sk(sk);
2204 struct ip_sf_socklist *psl;
2206 psin = (struct sockaddr_in *)&gsf->gf_group;
2207 if (psin->sin_family != AF_INET)
2209 addr = psin->sin_addr.s_addr;
2210 if (!ipv4_is_multicast(addr))
2215 err = -EADDRNOTAVAIL;
2217 for_each_pmc_rtnl(inet, pmc) {
2218 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2219 pmc->multi.imr_ifindex == gsf->gf_interface)
2222 if (!pmc) /* must have a prior join */
2224 gsf->gf_fmode = pmc->sfmode;
2225 psl = rtnl_dereference(pmc->sflist);
2227 count = psl ? psl->sl_count : 0;
2228 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2229 gsf->gf_numsrc = count;
2230 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2231 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2234 for (i=0; i<copycount; i++) {
2235 struct sockaddr_storage ss;
2237 psin = (struct sockaddr_in *)&ss;
2238 memset(&ss, 0, sizeof(ss));
2239 psin->sin_family = AF_INET;
2240 psin->sin_addr.s_addr = psl->sl_addr[i];
2241 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2251 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2253 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2255 struct inet_sock *inet = inet_sk(sk);
2256 struct ip_mc_socklist *pmc;
2257 struct ip_sf_socklist *psl;
2262 if (!ipv4_is_multicast(loc_addr))
2266 for_each_pmc_rcu(inet, pmc) {
2267 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2268 pmc->multi.imr_ifindex == dif)
2274 psl = rcu_dereference(pmc->sflist);
2275 ret = (pmc->sfmode == MCAST_EXCLUDE);
2279 for (i=0; i<psl->sl_count; i++) {
2280 if (psl->sl_addr[i] == rmt_addr)
2284 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2286 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2296 * A socket is closing.
2299 void ip_mc_drop_socket(struct sock *sk)
2301 struct inet_sock *inet = inet_sk(sk);
2302 struct ip_mc_socklist *iml;
2303 struct net *net = sock_net(sk);
2305 if (inet->mc_list == NULL)
2309 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2310 struct in_device *in_dev;
2312 inet->mc_list = iml->next_rcu;
2313 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2314 (void) ip_mc_leave_src(sk, iml, in_dev);
2316 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2317 /* decrease mem now to avoid the memleak warning */
2318 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2319 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
2324 /* called with rcu_read_lock() */
2325 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2327 struct ip_mc_list *im;
2328 struct ip_sf_list *psf;
2331 for_each_pmc_rcu(in_dev, im) {
2332 if (im->multiaddr == mc_addr)
2335 if (im && proto == IPPROTO_IGMP) {
2339 for (psf=im->sources; psf; psf=psf->sf_next) {
2340 if (psf->sf_inaddr == src_addr)
2344 rv = psf->sf_count[MCAST_INCLUDE] ||
2345 psf->sf_count[MCAST_EXCLUDE] !=
2346 im->sfcount[MCAST_EXCLUDE];
2348 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2350 rv = 1; /* unspecified source; tentatively allow */
2355 #if defined(CONFIG_PROC_FS)
2356 struct igmp_mc_iter_state {
2357 struct seq_net_private p;
2358 struct net_device *dev;
2359 struct in_device *in_dev;
2362 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2364 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2366 struct net *net = seq_file_net(seq);
2367 struct ip_mc_list *im = NULL;
2368 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2370 state->in_dev = NULL;
2371 for_each_netdev_rcu(net, state->dev) {
2372 struct in_device *in_dev;
2374 in_dev = __in_dev_get_rcu(state->dev);
2377 im = rcu_dereference(in_dev->mc_list);
2379 state->in_dev = in_dev;
2386 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2388 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2390 im = rcu_dereference(im->next_rcu);
2392 state->dev = next_net_device_rcu(state->dev);
2394 state->in_dev = NULL;
2397 state->in_dev = __in_dev_get_rcu(state->dev);
2400 im = rcu_dereference(state->in_dev->mc_list);
2405 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2407 struct ip_mc_list *im = igmp_mc_get_first(seq);
2409 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2411 return pos ? NULL : im;
2414 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2418 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2421 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2423 struct ip_mc_list *im;
2424 if (v == SEQ_START_TOKEN)
2425 im = igmp_mc_get_first(seq);
2427 im = igmp_mc_get_next(seq, v);
2432 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2435 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2437 state->in_dev = NULL;
2442 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2444 if (v == SEQ_START_TOKEN)
2446 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2448 struct ip_mc_list *im = (struct ip_mc_list *)v;
2449 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2451 #ifdef CONFIG_IP_MULTICAST
2452 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2453 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2459 if (rcu_dereference(state->in_dev->mc_list) == im) {
2460 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2461 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2465 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2466 im->multiaddr, im->users,
2467 im->tm_running, im->tm_running ?
2468 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2474 static const struct seq_operations igmp_mc_seq_ops = {
2475 .start = igmp_mc_seq_start,
2476 .next = igmp_mc_seq_next,
2477 .stop = igmp_mc_seq_stop,
2478 .show = igmp_mc_seq_show,
2481 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2483 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2484 sizeof(struct igmp_mc_iter_state));
2487 static const struct file_operations igmp_mc_seq_fops = {
2488 .owner = THIS_MODULE,
2489 .open = igmp_mc_seq_open,
2491 .llseek = seq_lseek,
2492 .release = seq_release_net,
2495 struct igmp_mcf_iter_state {
2496 struct seq_net_private p;
2497 struct net_device *dev;
2498 struct in_device *idev;
2499 struct ip_mc_list *im;
2502 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2504 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2506 struct net *net = seq_file_net(seq);
2507 struct ip_sf_list *psf = NULL;
2508 struct ip_mc_list *im = NULL;
2509 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2513 for_each_netdev_rcu(net, state->dev) {
2514 struct in_device *idev;
2515 idev = __in_dev_get_rcu(state->dev);
2516 if (unlikely(idev == NULL))
2518 im = rcu_dereference(idev->mc_list);
2519 if (likely(im != NULL)) {
2520 spin_lock_bh(&im->lock);
2522 if (likely(psf != NULL)) {
2527 spin_unlock_bh(&im->lock);
2533 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2535 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2539 spin_unlock_bh(&state->im->lock);
2540 state->im = state->im->next;
2541 while (!state->im) {
2542 state->dev = next_net_device_rcu(state->dev);
2547 state->idev = __in_dev_get_rcu(state->dev);
2550 state->im = rcu_dereference(state->idev->mc_list);
2554 spin_lock_bh(&state->im->lock);
2555 psf = state->im->sources;
2561 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2563 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2565 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2567 return pos ? NULL : psf;
2570 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2574 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2577 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2579 struct ip_sf_list *psf;
2580 if (v == SEQ_START_TOKEN)
2581 psf = igmp_mcf_get_first(seq);
2583 psf = igmp_mcf_get_next(seq, v);
2588 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2591 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2592 if (likely(state->im != NULL)) {
2593 spin_unlock_bh(&state->im->lock);
2601 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2603 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2604 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2606 if (v == SEQ_START_TOKEN) {
2609 "%10s %10s %6s %6s\n", "Idx",
2611 "SRC", "INC", "EXC");
2615 "0x%08x %6lu %6lu\n",
2616 state->dev->ifindex, state->dev->name,
2617 ntohl(state->im->multiaddr),
2618 ntohl(psf->sf_inaddr),
2619 psf->sf_count[MCAST_INCLUDE],
2620 psf->sf_count[MCAST_EXCLUDE]);
2625 static const struct seq_operations igmp_mcf_seq_ops = {
2626 .start = igmp_mcf_seq_start,
2627 .next = igmp_mcf_seq_next,
2628 .stop = igmp_mcf_seq_stop,
2629 .show = igmp_mcf_seq_show,
2632 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2634 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2635 sizeof(struct igmp_mcf_iter_state));
2638 static const struct file_operations igmp_mcf_seq_fops = {
2639 .owner = THIS_MODULE,
2640 .open = igmp_mcf_seq_open,
2642 .llseek = seq_lseek,
2643 .release = seq_release_net,
2646 static int __net_init igmp_net_init(struct net *net)
2648 struct proc_dir_entry *pde;
2650 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2653 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2659 proc_net_remove(net, "igmp");
2664 static void __net_exit igmp_net_exit(struct net *net)
2666 proc_net_remove(net, "mcfilter");
2667 proc_net_remove(net, "igmp");
2670 static struct pernet_operations igmp_net_ops = {
2671 .init = igmp_net_init,
2672 .exit = igmp_net_exit,
2675 int __init igmp_mc_proc_init(void)
2677 return register_pernet_subsys(&igmp_net_ops);