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.
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
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/types.h>
78 #include <linux/kernel.h>
79 #include <linux/jiffies.h>
80 #include <linux/string.h>
81 #include <linux/socket.h>
82 #include <linux/sockios.h>
84 #include <linux/inet.h>
85 #include <linux/netdevice.h>
86 #include <linux/skbuff.h>
87 #include <linux/inetdevice.h>
88 #include <linux/igmp.h>
89 #include <linux/if_arp.h>
90 #include <linux/rtnetlink.h>
91 #include <linux/times.h>
93 #include <net/net_namespace.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
99 #include <net/checksum.h>
100 #include <linux/netfilter_ipv4.h>
101 #ifdef CONFIG_IP_MROUTE
102 #include <linux/mroute.h>
104 #ifdef CONFIG_PROC_FS
105 #include <linux/proc_fs.h>
106 #include <linux/seq_file.h>
109 #define IP_MAX_MEMBERSHIPS 20
110 #define IP_MAX_MSF 10
112 #ifdef CONFIG_IP_MULTICAST
113 /* Parameter names and values are taken from igmp-v2-06 draft */
115 #define IGMP_V1_Router_Present_Timeout (400*HZ)
116 #define IGMP_V2_Router_Present_Timeout (400*HZ)
117 #define IGMP_Unsolicited_Report_Interval (10*HZ)
118 #define IGMP_Query_Response_Interval (10*HZ)
119 #define IGMP_Unsolicited_Report_Count 2
122 #define IGMP_Initial_Report_Delay (1)
124 /* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
131 #define IGMP_V1_SEEN(in_dev) \
132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
142 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
143 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
144 static void igmpv3_clear_delrec(struct in_device *in_dev);
145 static int sf_setstate(struct ip_mc_list *pmc);
146 static void sf_markstate(struct ip_mc_list *pmc);
148 static void ip_mc_clear_src(struct ip_mc_list *pmc);
149 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
152 static void ip_ma_put(struct ip_mc_list *im)
154 if (atomic_dec_and_test(&im->refcnt)) {
155 in_dev_put(im->interface);
160 #define for_each_pmc_rcu(in_dev, pmc) \
161 for (pmc = rcu_dereference(in_dev->mc_list); \
163 pmc = rcu_dereference(pmc->next_rcu))
165 #define for_each_pmc_rtnl(in_dev, pmc) \
166 for (pmc = rtnl_dereference(in_dev->mc_list); \
168 pmc = rtnl_dereference(pmc->next_rcu))
170 #ifdef CONFIG_IP_MULTICAST
176 static void igmp_stop_timer(struct ip_mc_list *im)
178 spin_lock_bh(&im->lock);
179 if (del_timer(&im->timer))
180 atomic_dec(&im->refcnt);
183 im->unsolicit_count = 0;
184 spin_unlock_bh(&im->lock);
187 /* It must be called with locked im->lock */
188 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
190 int tv = net_random() % max_delay;
193 if (!mod_timer(&im->timer, jiffies+tv+2))
194 atomic_inc(&im->refcnt);
197 static void igmp_gq_start_timer(struct in_device *in_dev)
199 int tv = net_random() % in_dev->mr_maxdelay;
201 in_dev->mr_gq_running = 1;
202 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
206 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
208 int tv = net_random() % delay;
210 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
214 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
216 spin_lock_bh(&im->lock);
217 im->unsolicit_count = 0;
218 if (del_timer(&im->timer)) {
219 if ((long)(im->timer.expires-jiffies) < max_delay) {
220 add_timer(&im->timer);
222 spin_unlock_bh(&im->lock);
225 atomic_dec(&im->refcnt);
227 igmp_start_timer(im, max_delay);
228 spin_unlock_bh(&im->lock);
233 * Send an IGMP report.
236 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
239 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
240 int gdeleted, int sdeleted)
243 case IGMPV3_MODE_IS_INCLUDE:
244 case IGMPV3_MODE_IS_EXCLUDE:
245 if (gdeleted || sdeleted)
247 if (!(pmc->gsquery && !psf->sf_gsresp)) {
248 if (pmc->sfmode == MCAST_INCLUDE)
250 /* don't include if this source is excluded
253 if (psf->sf_count[MCAST_INCLUDE])
254 return type == IGMPV3_MODE_IS_INCLUDE;
255 return pmc->sfcount[MCAST_EXCLUDE] ==
256 psf->sf_count[MCAST_EXCLUDE];
259 case IGMPV3_CHANGE_TO_INCLUDE:
260 if (gdeleted || sdeleted)
262 return psf->sf_count[MCAST_INCLUDE] != 0;
263 case IGMPV3_CHANGE_TO_EXCLUDE:
264 if (gdeleted || sdeleted)
266 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
267 psf->sf_count[MCAST_INCLUDE])
269 return pmc->sfcount[MCAST_EXCLUDE] ==
270 psf->sf_count[MCAST_EXCLUDE];
271 case IGMPV3_ALLOW_NEW_SOURCES:
272 if (gdeleted || !psf->sf_crcount)
274 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
275 case IGMPV3_BLOCK_OLD_SOURCES:
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return gdeleted || (psf->sf_crcount && sdeleted);
278 return psf->sf_crcount && !gdeleted && !sdeleted;
284 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
286 struct ip_sf_list *psf;
289 for (psf=pmc->sources; psf; psf=psf->sf_next) {
290 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
297 #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
299 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
304 struct igmpv3_report *pig;
305 struct net *net = dev_net(dev);
309 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
310 GFP_ATOMIC | __GFP_NOWARN);
317 igmp_skb_size(skb) = size;
319 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
321 IPPROTO_IGMP, 0, dev->ifindex);
327 skb_dst_set(skb, &rt->dst);
330 skb_reserve(skb, LL_RESERVED_SPACE(dev));
332 skb_reset_network_header(skb);
334 skb_put(skb, sizeof(struct iphdr) + 4);
337 pip->ihl = (sizeof(struct iphdr)+4)>>2;
339 pip->frag_off = htons(IP_DF);
341 pip->daddr = fl4.daddr;
342 pip->saddr = fl4.saddr;
343 pip->protocol = IPPROTO_IGMP;
344 pip->tot_len = 0; /* filled in later */
345 ip_select_ident(pip, &rt->dst, NULL);
346 ((u8*)&pip[1])[0] = IPOPT_RA;
347 ((u8*)&pip[1])[1] = 4;
348 ((u8*)&pip[1])[2] = 0;
349 ((u8*)&pip[1])[3] = 0;
351 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
352 skb_put(skb, sizeof(*pig));
353 pig = igmpv3_report_hdr(skb);
354 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
362 static int igmpv3_sendpack(struct sk_buff *skb)
364 struct igmphdr *pig = igmp_hdr(skb);
365 const int igmplen = skb->tail - skb->transport_header;
367 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
369 return ip_local_out(skb);
372 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
374 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
377 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
378 int type, struct igmpv3_grec **ppgr)
380 struct net_device *dev = pmc->interface->dev;
381 struct igmpv3_report *pih;
382 struct igmpv3_grec *pgr;
385 skb = igmpv3_newpack(dev, dev->mtu);
388 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
389 pgr->grec_type = type;
390 pgr->grec_auxwords = 0;
392 pgr->grec_mca = pmc->multiaddr;
393 pih = igmpv3_report_hdr(skb);
394 pih->ngrec = htons(ntohs(pih->ngrec)+1);
399 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
400 skb_tailroom(skb)) : 0)
402 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
403 int type, int gdeleted, int sdeleted)
405 struct net_device *dev = pmc->interface->dev;
406 struct igmpv3_report *pih;
407 struct igmpv3_grec *pgr = NULL;
408 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
409 int scount, stotal, first, isquery, truncate;
411 if (pmc->multiaddr == IGMP_ALL_HOSTS)
414 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
415 type == IGMPV3_MODE_IS_EXCLUDE;
416 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
417 type == IGMPV3_CHANGE_TO_EXCLUDE;
421 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
426 pih = skb ? igmpv3_report_hdr(skb) : NULL;
428 /* EX and TO_EX get a fresh packet, if needed */
430 if (pih && pih->ngrec &&
431 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
433 igmpv3_sendpack(skb);
434 skb = igmpv3_newpack(dev, dev->mtu);
439 for (psf=*psf_list; psf; psf=psf_next) {
442 psf_next = psf->sf_next;
444 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
449 /* clear marks on query responses */
453 if (AVAILABLE(skb) < sizeof(__be32) +
454 first*sizeof(struct igmpv3_grec)) {
455 if (truncate && !first)
456 break; /* truncate these */
458 pgr->grec_nsrcs = htons(scount);
460 igmpv3_sendpack(skb);
461 skb = igmpv3_newpack(dev, dev->mtu);
466 skb = add_grhead(skb, pmc, type, &pgr);
471 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
472 *psrc = psf->sf_inaddr;
474 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
475 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
477 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
479 psf_prev->sf_next = psf->sf_next;
481 *psf_list = psf->sf_next;
491 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
492 type == IGMPV3_BLOCK_OLD_SOURCES)
494 if (pmc->crcount || isquery) {
495 /* make sure we have room for group header */
496 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
497 igmpv3_sendpack(skb);
498 skb = NULL; /* add_grhead will get a new one */
500 skb = add_grhead(skb, pmc, type, &pgr);
504 pgr->grec_nsrcs = htons(scount);
507 pmc->gsquery = 0; /* clear query state on report */
511 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
513 struct sk_buff *skb = NULL;
518 for_each_pmc_rcu(in_dev, pmc) {
519 if (pmc->multiaddr == IGMP_ALL_HOSTS)
521 spin_lock_bh(&pmc->lock);
522 if (pmc->sfcount[MCAST_EXCLUDE])
523 type = IGMPV3_MODE_IS_EXCLUDE;
525 type = IGMPV3_MODE_IS_INCLUDE;
526 skb = add_grec(skb, pmc, type, 0, 0);
527 spin_unlock_bh(&pmc->lock);
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 return igmpv3_sendpack(skb);
545 * remove zero-count source records from a source filter list
547 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
549 struct ip_sf_list *psf_prev, *psf_next, *psf;
552 for (psf=*ppsf; psf; psf = psf_next) {
553 psf_next = psf->sf_next;
554 if (psf->sf_crcount == 0) {
556 psf_prev->sf_next = psf->sf_next;
558 *ppsf = psf->sf_next;
565 static void igmpv3_send_cr(struct in_device *in_dev)
567 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
568 struct sk_buff *skb = NULL;
572 spin_lock_bh(&in_dev->mc_tomb_lock);
576 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
577 pmc_next = pmc->next;
578 if (pmc->sfmode == MCAST_INCLUDE) {
579 type = IGMPV3_BLOCK_OLD_SOURCES;
580 dtype = IGMPV3_BLOCK_OLD_SOURCES;
581 skb = add_grec(skb, pmc, type, 1, 0);
582 skb = add_grec(skb, pmc, dtype, 1, 1);
585 if (pmc->sfmode == MCAST_EXCLUDE) {
586 type = IGMPV3_CHANGE_TO_INCLUDE;
587 skb = add_grec(skb, pmc, type, 1, 0);
590 if (pmc->crcount == 0) {
591 igmpv3_clear_zeros(&pmc->tomb);
592 igmpv3_clear_zeros(&pmc->sources);
595 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
597 pmc_prev->next = pmc_next;
599 in_dev->mc_tomb = pmc_next;
600 in_dev_put(pmc->interface);
605 spin_unlock_bh(&in_dev->mc_tomb_lock);
608 for_each_pmc_rcu(in_dev, pmc) {
609 spin_lock_bh(&pmc->lock);
610 if (pmc->sfcount[MCAST_EXCLUDE]) {
611 type = IGMPV3_BLOCK_OLD_SOURCES;
612 dtype = IGMPV3_ALLOW_NEW_SOURCES;
614 type = IGMPV3_ALLOW_NEW_SOURCES;
615 dtype = IGMPV3_BLOCK_OLD_SOURCES;
617 skb = add_grec(skb, pmc, type, 0, 0);
618 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
620 /* filter mode changes */
622 if (pmc->sfmode == MCAST_EXCLUDE)
623 type = IGMPV3_CHANGE_TO_EXCLUDE;
625 type = IGMPV3_CHANGE_TO_INCLUDE;
626 skb = add_grec(skb, pmc, type, 0, 0);
629 spin_unlock_bh(&pmc->lock);
635 (void) igmpv3_sendpack(skb);
638 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
645 struct net_device *dev = in_dev->dev;
646 struct net *net = dev_net(dev);
647 __be32 group = pmc ? pmc->multiaddr : 0;
651 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
652 return igmpv3_send_report(in_dev, pmc);
653 else if (type == IGMP_HOST_LEAVE_MESSAGE)
654 dst = IGMP_ALL_ROUTER;
658 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
660 IPPROTO_IGMP, 0, dev->ifindex);
664 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
670 skb_dst_set(skb, &rt->dst);
672 skb_reserve(skb, LL_RESERVED_SPACE(dev));
674 skb_reset_network_header(skb);
676 skb_put(skb, sizeof(struct iphdr) + 4);
679 iph->ihl = (sizeof(struct iphdr)+4)>>2;
681 iph->frag_off = htons(IP_DF);
684 iph->saddr = fl4.saddr;
685 iph->protocol = IPPROTO_IGMP;
686 ip_select_ident(iph, &rt->dst, NULL);
687 ((u8*)&iph[1])[0] = IPOPT_RA;
688 ((u8*)&iph[1])[1] = 4;
689 ((u8*)&iph[1])[2] = 0;
690 ((u8*)&iph[1])[3] = 0;
692 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
697 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
699 return ip_local_out(skb);
702 static void igmp_gq_timer_expire(unsigned long data)
704 struct in_device *in_dev = (struct in_device *)data;
706 in_dev->mr_gq_running = 0;
707 igmpv3_send_report(in_dev, NULL);
708 __in_dev_put(in_dev);
711 static void igmp_ifc_timer_expire(unsigned long data)
713 struct in_device *in_dev = (struct in_device *)data;
715 igmpv3_send_cr(in_dev);
716 if (in_dev->mr_ifc_count) {
717 in_dev->mr_ifc_count--;
718 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
720 __in_dev_put(in_dev);
723 static void igmp_ifc_event(struct in_device *in_dev)
725 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
727 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
728 IGMP_Unsolicited_Report_Count;
729 igmp_ifc_start_timer(in_dev, 1);
733 static void igmp_timer_expire(unsigned long data)
735 struct ip_mc_list *im=(struct ip_mc_list *)data;
736 struct in_device *in_dev = im->interface;
738 spin_lock(&im->lock);
741 if (im->unsolicit_count) {
742 im->unsolicit_count--;
743 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
746 spin_unlock(&im->lock);
748 if (IGMP_V1_SEEN(in_dev))
749 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
750 else if (IGMP_V2_SEEN(in_dev))
751 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
753 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
758 /* mark EXCLUDE-mode sources */
759 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
761 struct ip_sf_list *psf;
765 for (psf=pmc->sources; psf; psf=psf->sf_next) {
768 for (i=0; i<nsrcs; i++) {
769 /* skip inactive filters */
770 if (psf->sf_count[MCAST_INCLUDE] ||
771 pmc->sfcount[MCAST_EXCLUDE] !=
772 psf->sf_count[MCAST_EXCLUDE])
774 if (srcs[i] == psf->sf_inaddr) {
781 if (scount == nsrcs) /* all sources excluded */
786 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
788 struct ip_sf_list *psf;
791 if (pmc->sfmode == MCAST_EXCLUDE)
792 return igmp_xmarksources(pmc, nsrcs, srcs);
794 /* mark INCLUDE-mode sources */
796 for (psf=pmc->sources; psf; psf=psf->sf_next) {
799 for (i=0; i<nsrcs; i++)
800 if (srcs[i] == psf->sf_inaddr) {
814 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
816 struct ip_mc_list *im;
818 /* Timers are only set for non-local groups */
820 if (group == IGMP_ALL_HOSTS)
824 for_each_pmc_rcu(in_dev, im) {
825 if (im->multiaddr == group) {
833 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
836 struct igmphdr *ih = igmp_hdr(skb);
837 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
838 struct ip_mc_list *im;
839 __be32 group = ih->group;
846 /* Alas, old v1 router presents here. */
848 max_delay = IGMP_Query_Response_Interval;
849 in_dev->mr_v1_seen = jiffies +
850 IGMP_V1_Router_Present_Timeout;
853 /* v2 router present */
854 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
855 in_dev->mr_v2_seen = jiffies +
856 IGMP_V2_Router_Present_Timeout;
858 /* cancel the interface change timer */
859 in_dev->mr_ifc_count = 0;
860 if (del_timer(&in_dev->mr_ifc_timer))
861 __in_dev_put(in_dev);
862 /* clear deleted report items */
863 igmpv3_clear_delrec(in_dev);
864 } else if (len < 12) {
865 return; /* ignore bogus packet; freed by caller */
866 } else if (IGMP_V1_SEEN(in_dev)) {
867 /* This is a v3 query with v1 queriers present */
868 max_delay = IGMP_Query_Response_Interval;
870 } else if (IGMP_V2_SEEN(in_dev)) {
871 /* this is a v3 query with v2 queriers present;
872 * Interpretation of the max_delay code is problematic here.
873 * A real v2 host would use ih_code directly, while v3 has a
874 * different encoding. We use the v3 encoding as more likely
875 * to be intended in a v3 query.
877 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
879 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
882 ih3 = igmpv3_query_hdr(skb);
884 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
885 + ntohs(ih3->nsrcs)*sizeof(__be32)))
887 ih3 = igmpv3_query_hdr(skb);
890 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
892 max_delay = 1; /* can't mod w/ 0 */
893 in_dev->mr_maxdelay = max_delay;
895 in_dev->mr_qrv = ih3->qrv;
896 if (!group) { /* general query */
898 return; /* no sources allowed */
899 igmp_gq_start_timer(in_dev);
902 /* mark sources to include, if group & source-specific */
903 mark = ih3->nsrcs != 0;
907 * - Start the timers in all of our membership records
908 * that the query applies to for the interface on
909 * which the query arrived excl. those that belong
910 * to a "local" group (224.0.0.X)
911 * - For timers already running check if they need to
913 * - Use the igmp->igmp_code field as the maximum
917 for_each_pmc_rcu(in_dev, im) {
920 if (group && group != im->multiaddr)
922 if (im->multiaddr == IGMP_ALL_HOSTS)
924 spin_lock_bh(&im->lock);
926 im->gsquery = im->gsquery && mark;
929 changed = !im->gsquery ||
930 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
931 spin_unlock_bh(&im->lock);
933 igmp_mod_timer(im, max_delay);
938 /* called in rcu_read_lock() section */
939 int igmp_rcv(struct sk_buff *skb)
941 /* This basically follows the spec line by line -- see RFC1112 */
943 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
949 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
952 switch (skb->ip_summed) {
953 case CHECKSUM_COMPLETE:
954 if (!csum_fold(skb->csum))
959 if (__skb_checksum_complete(skb))
965 case IGMP_HOST_MEMBERSHIP_QUERY:
966 igmp_heard_query(in_dev, skb, len);
968 case IGMP_HOST_MEMBERSHIP_REPORT:
969 case IGMPV2_HOST_MEMBERSHIP_REPORT:
970 /* Is it our report looped back? */
971 if (rt_is_output_route(skb_rtable(skb)))
973 /* don't rely on MC router hearing unicast reports */
974 if (skb->pkt_type == PACKET_MULTICAST ||
975 skb->pkt_type == PACKET_BROADCAST)
976 igmp_heard_report(in_dev, ih->group);
979 #ifdef CONFIG_IP_PIMSM_V1
980 return pim_rcv_v1(skb);
982 case IGMPV3_HOST_MEMBERSHIP_REPORT:
985 case IGMP_HOST_LEAVE_MESSAGE:
987 case IGMP_MTRACE_RESP:
1002 * Add a filter to a device
1005 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1007 char buf[MAX_ADDR_LEN];
1008 struct net_device *dev = in_dev->dev;
1010 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1011 We will get multicast token leakage, when IFF_MULTICAST
1012 is changed. This check should be done in ndo_set_rx_mode
1013 routine. Something sort of:
1014 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1017 if (arp_mc_map(addr, buf, dev, 0) == 0)
1018 dev_mc_add(dev, buf);
1022 * Remove a filter from a device
1025 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1027 char buf[MAX_ADDR_LEN];
1028 struct net_device *dev = in_dev->dev;
1030 if (arp_mc_map(addr, buf, dev, 0) == 0)
1031 dev_mc_del(dev, buf);
1034 #ifdef CONFIG_IP_MULTICAST
1036 * deleted ip_mc_list manipulation
1038 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1040 struct ip_mc_list *pmc;
1042 /* this is an "ip_mc_list" for convenience; only the fields below
1043 * are actually used. In particular, the refcnt and users are not
1044 * used for management of the delete list. Using the same structure
1045 * for deleted items allows change reports to use common code with
1046 * non-deleted or query-response MCA's.
1048 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1051 spin_lock_bh(&im->lock);
1052 pmc->interface = im->interface;
1053 in_dev_hold(in_dev);
1054 pmc->multiaddr = im->multiaddr;
1055 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1056 IGMP_Unsolicited_Report_Count;
1057 pmc->sfmode = im->sfmode;
1058 if (pmc->sfmode == MCAST_INCLUDE) {
1059 struct ip_sf_list *psf;
1061 pmc->tomb = im->tomb;
1062 pmc->sources = im->sources;
1063 im->tomb = im->sources = NULL;
1064 for (psf=pmc->sources; psf; psf=psf->sf_next)
1065 psf->sf_crcount = pmc->crcount;
1067 spin_unlock_bh(&im->lock);
1069 spin_lock_bh(&in_dev->mc_tomb_lock);
1070 pmc->next = in_dev->mc_tomb;
1071 in_dev->mc_tomb = pmc;
1072 spin_unlock_bh(&in_dev->mc_tomb_lock);
1075 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1077 struct ip_mc_list *pmc, *pmc_prev;
1078 struct ip_sf_list *psf, *psf_next;
1080 spin_lock_bh(&in_dev->mc_tomb_lock);
1082 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1083 if (pmc->multiaddr == multiaddr)
1089 pmc_prev->next = pmc->next;
1091 in_dev->mc_tomb = pmc->next;
1093 spin_unlock_bh(&in_dev->mc_tomb_lock);
1095 for (psf=pmc->tomb; psf; psf=psf_next) {
1096 psf_next = psf->sf_next;
1099 in_dev_put(pmc->interface);
1104 static void igmpv3_clear_delrec(struct in_device *in_dev)
1106 struct ip_mc_list *pmc, *nextpmc;
1108 spin_lock_bh(&in_dev->mc_tomb_lock);
1109 pmc = in_dev->mc_tomb;
1110 in_dev->mc_tomb = NULL;
1111 spin_unlock_bh(&in_dev->mc_tomb_lock);
1113 for (; pmc; pmc = nextpmc) {
1114 nextpmc = pmc->next;
1115 ip_mc_clear_src(pmc);
1116 in_dev_put(pmc->interface);
1119 /* clear dead sources, too */
1121 for_each_pmc_rcu(in_dev, pmc) {
1122 struct ip_sf_list *psf, *psf_next;
1124 spin_lock_bh(&pmc->lock);
1127 spin_unlock_bh(&pmc->lock);
1128 for (; psf; psf=psf_next) {
1129 psf_next = psf->sf_next;
1137 static void igmp_group_dropped(struct ip_mc_list *im)
1139 struct in_device *in_dev = im->interface;
1140 #ifdef CONFIG_IP_MULTICAST
1146 ip_mc_filter_del(in_dev, im->multiaddr);
1149 #ifdef CONFIG_IP_MULTICAST
1150 if (im->multiaddr == IGMP_ALL_HOSTS)
1153 reporter = im->reporter;
1154 igmp_stop_timer(im);
1156 if (!in_dev->dead) {
1157 if (IGMP_V1_SEEN(in_dev))
1159 if (IGMP_V2_SEEN(in_dev)) {
1161 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1165 igmpv3_add_delrec(in_dev, im);
1167 igmp_ifc_event(in_dev);
1172 static void igmp_group_added(struct ip_mc_list *im)
1174 struct in_device *in_dev = im->interface;
1176 if (im->loaded == 0) {
1178 ip_mc_filter_add(in_dev, im->multiaddr);
1181 #ifdef CONFIG_IP_MULTICAST
1182 if (im->multiaddr == IGMP_ALL_HOSTS)
1187 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1188 spin_lock_bh(&im->lock);
1189 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1190 spin_unlock_bh(&im->lock);
1195 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1196 IGMP_Unsolicited_Report_Count;
1197 igmp_ifc_event(in_dev);
1203 * Multicast list managers
1208 * A socket has joined a multicast group on device dev.
1211 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1213 struct ip_mc_list *im;
1217 for_each_pmc_rtnl(in_dev, im) {
1218 if (im->multiaddr == addr) {
1220 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1225 im = kzalloc(sizeof(*im), GFP_KERNEL);
1230 im->interface = in_dev;
1231 in_dev_hold(in_dev);
1232 im->multiaddr = addr;
1233 /* initial mode is (EX, empty) */
1234 im->sfmode = MCAST_EXCLUDE;
1235 im->sfcount[MCAST_EXCLUDE] = 1;
1236 atomic_set(&im->refcnt, 1);
1237 spin_lock_init(&im->lock);
1238 #ifdef CONFIG_IP_MULTICAST
1239 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1240 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1243 im->next_rcu = in_dev->mc_list;
1245 RCU_INIT_POINTER(in_dev->mc_list, im);
1247 #ifdef CONFIG_IP_MULTICAST
1248 igmpv3_del_delrec(in_dev, im->multiaddr);
1250 igmp_group_added(im);
1252 ip_rt_multicast_event(in_dev);
1256 EXPORT_SYMBOL(ip_mc_inc_group);
1259 * Resend IGMP JOIN report; used for bonding.
1260 * Called with rcu_read_lock()
1262 void ip_mc_rejoin_groups(struct in_device *in_dev)
1264 #ifdef CONFIG_IP_MULTICAST
1265 struct ip_mc_list *im;
1268 for_each_pmc_rcu(in_dev, im) {
1269 if (im->multiaddr == IGMP_ALL_HOSTS)
1272 /* a failover is happening and switches
1273 * must be notified immediately
1275 if (IGMP_V1_SEEN(in_dev))
1276 type = IGMP_HOST_MEMBERSHIP_REPORT;
1277 else if (IGMP_V2_SEEN(in_dev))
1278 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1280 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1281 igmp_send_report(in_dev, im, type);
1285 EXPORT_SYMBOL(ip_mc_rejoin_groups);
1288 * A socket has left a multicast group on device dev
1291 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1293 struct ip_mc_list *i;
1294 struct ip_mc_list __rcu **ip;
1298 for (ip = &in_dev->mc_list;
1299 (i = rtnl_dereference(*ip)) != NULL;
1300 ip = &i->next_rcu) {
1301 if (i->multiaddr == addr) {
1302 if (--i->users == 0) {
1305 igmp_group_dropped(i);
1309 ip_rt_multicast_event(in_dev);
1318 EXPORT_SYMBOL(ip_mc_dec_group);
1320 /* Device changing type */
1322 void ip_mc_unmap(struct in_device *in_dev)
1324 struct ip_mc_list *pmc;
1328 for_each_pmc_rtnl(in_dev, pmc)
1329 igmp_group_dropped(pmc);
1332 void ip_mc_remap(struct in_device *in_dev)
1334 struct ip_mc_list *pmc;
1338 for_each_pmc_rtnl(in_dev, pmc)
1339 igmp_group_added(pmc);
1342 /* Device going down */
1344 void ip_mc_down(struct in_device *in_dev)
1346 struct ip_mc_list *pmc;
1350 for_each_pmc_rtnl(in_dev, pmc)
1351 igmp_group_dropped(pmc);
1353 #ifdef CONFIG_IP_MULTICAST
1354 in_dev->mr_ifc_count = 0;
1355 if (del_timer(&in_dev->mr_ifc_timer))
1356 __in_dev_put(in_dev);
1357 in_dev->mr_gq_running = 0;
1358 if (del_timer(&in_dev->mr_gq_timer))
1359 __in_dev_put(in_dev);
1360 igmpv3_clear_delrec(in_dev);
1363 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1366 void ip_mc_init_dev(struct in_device *in_dev)
1370 in_dev->mc_tomb = NULL;
1371 #ifdef CONFIG_IP_MULTICAST
1372 in_dev->mr_gq_running = 0;
1373 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1374 (unsigned long)in_dev);
1375 in_dev->mr_ifc_count = 0;
1376 in_dev->mc_count = 0;
1377 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1378 (unsigned long)in_dev);
1379 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1382 spin_lock_init(&in_dev->mc_tomb_lock);
1385 /* Device going up */
1387 void ip_mc_up(struct in_device *in_dev)
1389 struct ip_mc_list *pmc;
1393 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1395 for_each_pmc_rtnl(in_dev, pmc)
1396 igmp_group_added(pmc);
1400 * Device is about to be destroyed: clean up.
1403 void ip_mc_destroy_dev(struct in_device *in_dev)
1405 struct ip_mc_list *i;
1409 /* Deactivate timers */
1412 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1413 in_dev->mc_list = i->next_rcu;
1416 /* We've dropped the groups in ip_mc_down already */
1422 /* RTNL is locked */
1423 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1425 struct net_device *dev = NULL;
1426 struct in_device *idev = NULL;
1428 if (imr->imr_ifindex) {
1429 idev = inetdev_by_index(net, imr->imr_ifindex);
1432 if (imr->imr_address.s_addr) {
1433 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1439 struct rtable *rt = ip_route_output(net,
1440 imr->imr_multiaddr.s_addr,
1448 imr->imr_ifindex = dev->ifindex;
1449 idev = __in_dev_get_rtnl(dev);
1455 * Join a socket to a group
1457 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1458 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1461 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1464 struct ip_sf_list *psf, *psf_prev;
1468 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1469 if (psf->sf_inaddr == *psfsrc)
1473 if (!psf || psf->sf_count[sfmode] == 0) {
1474 /* source filter not found, or count wrong => bug */
1477 psf->sf_count[sfmode]--;
1478 if (psf->sf_count[sfmode] == 0) {
1479 ip_rt_multicast_event(pmc->interface);
1481 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1482 #ifdef CONFIG_IP_MULTICAST
1483 struct in_device *in_dev = pmc->interface;
1486 /* no more filters for this source */
1488 psf_prev->sf_next = psf->sf_next;
1490 pmc->sources = psf->sf_next;
1491 #ifdef CONFIG_IP_MULTICAST
1492 if (psf->sf_oldin &&
1493 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1494 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1495 IGMP_Unsolicited_Report_Count;
1496 psf->sf_next = pmc->tomb;
1506 #ifndef CONFIG_IP_MULTICAST
1507 #define igmp_ifc_event(x) do { } while (0)
1510 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1511 int sfcount, __be32 *psfsrc, int delta)
1513 struct ip_mc_list *pmc;
1520 for_each_pmc_rcu(in_dev, pmc) {
1521 if (*pmca == pmc->multiaddr)
1525 /* MCA not found?? bug */
1529 spin_lock_bh(&pmc->lock);
1531 #ifdef CONFIG_IP_MULTICAST
1536 if (!pmc->sfcount[sfmode])
1538 pmc->sfcount[sfmode]--;
1541 for (i=0; i<sfcount; i++) {
1542 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1544 changerec |= rv > 0;
1548 if (pmc->sfmode == MCAST_EXCLUDE &&
1549 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1550 pmc->sfcount[MCAST_INCLUDE]) {
1551 #ifdef CONFIG_IP_MULTICAST
1552 struct ip_sf_list *psf;
1555 /* filter mode change */
1556 pmc->sfmode = MCAST_INCLUDE;
1557 #ifdef CONFIG_IP_MULTICAST
1558 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1559 IGMP_Unsolicited_Report_Count;
1560 in_dev->mr_ifc_count = pmc->crcount;
1561 for (psf=pmc->sources; psf; psf = psf->sf_next)
1562 psf->sf_crcount = 0;
1563 igmp_ifc_event(pmc->interface);
1564 } else if (sf_setstate(pmc) || changerec) {
1565 igmp_ifc_event(pmc->interface);
1569 spin_unlock_bh(&pmc->lock);
1574 * Add multicast single-source filter to the interface list
1576 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1577 __be32 *psfsrc, int delta)
1579 struct ip_sf_list *psf, *psf_prev;
1582 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1583 if (psf->sf_inaddr == *psfsrc)
1588 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1591 psf->sf_inaddr = *psfsrc;
1593 psf_prev->sf_next = psf;
1597 psf->sf_count[sfmode]++;
1598 if (psf->sf_count[sfmode] == 1) {
1599 ip_rt_multicast_event(pmc->interface);
1604 #ifdef CONFIG_IP_MULTICAST
1605 static void sf_markstate(struct ip_mc_list *pmc)
1607 struct ip_sf_list *psf;
1608 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1610 for (psf=pmc->sources; psf; psf=psf->sf_next)
1611 if (pmc->sfcount[MCAST_EXCLUDE]) {
1612 psf->sf_oldin = mca_xcount ==
1613 psf->sf_count[MCAST_EXCLUDE] &&
1614 !psf->sf_count[MCAST_INCLUDE];
1616 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1619 static int sf_setstate(struct ip_mc_list *pmc)
1621 struct ip_sf_list *psf, *dpsf;
1622 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1623 int qrv = pmc->interface->mr_qrv;
1627 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1628 if (pmc->sfcount[MCAST_EXCLUDE]) {
1629 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1630 !psf->sf_count[MCAST_INCLUDE];
1632 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1634 if (!psf->sf_oldin) {
1635 struct ip_sf_list *prev = NULL;
1637 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1638 if (dpsf->sf_inaddr == psf->sf_inaddr)
1644 prev->sf_next = dpsf->sf_next;
1646 pmc->tomb = dpsf->sf_next;
1649 psf->sf_crcount = qrv;
1652 } else if (psf->sf_oldin) {
1654 psf->sf_crcount = 0;
1656 * add or update "delete" records if an active filter
1659 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1660 if (dpsf->sf_inaddr == psf->sf_inaddr)
1663 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1667 /* pmc->lock held by callers */
1668 dpsf->sf_next = pmc->tomb;
1671 dpsf->sf_crcount = qrv;
1680 * Add multicast source filter list to the interface list
1682 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1683 int sfcount, __be32 *psfsrc, int delta)
1685 struct ip_mc_list *pmc;
1692 for_each_pmc_rcu(in_dev, pmc) {
1693 if (*pmca == pmc->multiaddr)
1697 /* MCA not found?? bug */
1701 spin_lock_bh(&pmc->lock);
1704 #ifdef CONFIG_IP_MULTICAST
1707 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1709 pmc->sfcount[sfmode]++;
1711 for (i=0; i<sfcount; i++) {
1712 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1720 pmc->sfcount[sfmode]--;
1722 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
1723 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1724 #ifdef CONFIG_IP_MULTICAST
1725 struct ip_sf_list *psf;
1726 in_dev = pmc->interface;
1729 /* filter mode change */
1730 if (pmc->sfcount[MCAST_EXCLUDE])
1731 pmc->sfmode = MCAST_EXCLUDE;
1732 else if (pmc->sfcount[MCAST_INCLUDE])
1733 pmc->sfmode = MCAST_INCLUDE;
1734 #ifdef CONFIG_IP_MULTICAST
1735 /* else no filters; keep old mode for reports */
1737 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1738 IGMP_Unsolicited_Report_Count;
1739 in_dev->mr_ifc_count = pmc->crcount;
1740 for (psf=pmc->sources; psf; psf = psf->sf_next)
1741 psf->sf_crcount = 0;
1742 igmp_ifc_event(in_dev);
1743 } else if (sf_setstate(pmc)) {
1744 igmp_ifc_event(in_dev);
1747 spin_unlock_bh(&pmc->lock);
1751 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1753 struct ip_sf_list *psf, *nextpsf;
1755 for (psf=pmc->tomb; psf; psf=nextpsf) {
1756 nextpsf = psf->sf_next;
1760 for (psf=pmc->sources; psf; psf=nextpsf) {
1761 nextpsf = psf->sf_next;
1764 pmc->sources = NULL;
1765 pmc->sfmode = MCAST_EXCLUDE;
1766 pmc->sfcount[MCAST_INCLUDE] = 0;
1767 pmc->sfcount[MCAST_EXCLUDE] = 1;
1772 * Join a multicast group
1774 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1777 __be32 addr = imr->imr_multiaddr.s_addr;
1778 struct ip_mc_socklist *iml = NULL, *i;
1779 struct in_device *in_dev;
1780 struct inet_sock *inet = inet_sk(sk);
1781 struct net *net = sock_net(sk);
1785 if (!ipv4_is_multicast(addr))
1790 in_dev = ip_mc_find_dev(net, imr);
1799 ifindex = imr->imr_ifindex;
1800 for_each_pmc_rtnl(inet, i) {
1801 if (i->multi.imr_multiaddr.s_addr == addr &&
1802 i->multi.imr_ifindex == ifindex)
1807 if (count >= sysctl_igmp_max_memberships)
1809 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1813 memcpy(&iml->multi, imr, sizeof(*imr));
1814 iml->next_rcu = inet->mc_list;
1816 iml->sfmode = MCAST_EXCLUDE;
1817 RCU_INIT_POINTER(inet->mc_list, iml);
1818 ip_mc_inc_group(in_dev, addr);
1824 EXPORT_SYMBOL(ip_mc_join_group);
1826 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1827 struct in_device *in_dev)
1829 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1833 /* any-source empty exclude case */
1834 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1835 iml->sfmode, 0, NULL, 0);
1837 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1838 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1839 RCU_INIT_POINTER(iml->sflist, NULL);
1840 /* decrease mem now to avoid the memleak warning */
1841 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1842 kfree_rcu(psf, rcu);
1847 * Ask a socket to leave a group.
1850 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1852 struct inet_sock *inet = inet_sk(sk);
1853 struct ip_mc_socklist *iml;
1854 struct ip_mc_socklist __rcu **imlp;
1855 struct in_device *in_dev;
1856 struct net *net = sock_net(sk);
1857 __be32 group = imr->imr_multiaddr.s_addr;
1859 int ret = -EADDRNOTAVAIL;
1862 in_dev = ip_mc_find_dev(net, imr);
1863 ifindex = imr->imr_ifindex;
1864 for (imlp = &inet->mc_list;
1865 (iml = rtnl_dereference(*imlp)) != NULL;
1866 imlp = &iml->next_rcu) {
1867 if (iml->multi.imr_multiaddr.s_addr != group)
1870 if (iml->multi.imr_ifindex != ifindex)
1872 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1873 iml->multi.imr_address.s_addr)
1876 (void) ip_mc_leave_src(sk, iml, in_dev);
1878 *imlp = iml->next_rcu;
1881 ip_mc_dec_group(in_dev, group);
1883 /* decrease mem now to avoid the memleak warning */
1884 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1885 kfree_rcu(iml, rcu);
1894 int ip_mc_source(int add, int omode, struct sock *sk, struct
1895 ip_mreq_source *mreqs, int ifindex)
1898 struct ip_mreqn imr;
1899 __be32 addr = mreqs->imr_multiaddr;
1900 struct ip_mc_socklist *pmc;
1901 struct in_device *in_dev = NULL;
1902 struct inet_sock *inet = inet_sk(sk);
1903 struct ip_sf_socklist *psl;
1904 struct net *net = sock_net(sk);
1908 if (!ipv4_is_multicast(addr))
1913 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1914 imr.imr_address.s_addr = mreqs->imr_interface;
1915 imr.imr_ifindex = ifindex;
1916 in_dev = ip_mc_find_dev(net, &imr);
1922 err = -EADDRNOTAVAIL;
1924 for_each_pmc_rtnl(inet, pmc) {
1925 if ((pmc->multi.imr_multiaddr.s_addr ==
1926 imr.imr_multiaddr.s_addr) &&
1927 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1930 if (!pmc) { /* must have a prior join */
1934 /* if a source filter was set, must be the same mode as before */
1936 if (pmc->sfmode != omode) {
1940 } else if (pmc->sfmode != omode) {
1941 /* allow mode switches for empty-set filters */
1942 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1943 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1945 pmc->sfmode = omode;
1948 psl = rtnl_dereference(pmc->sflist);
1951 goto done; /* err = -EADDRNOTAVAIL */
1953 for (i=0; i<psl->sl_count; i++) {
1954 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1959 if (rv) /* source not found */
1960 goto done; /* err = -EADDRNOTAVAIL */
1962 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1963 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1968 /* update the interface filter */
1969 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1970 &mreqs->imr_sourceaddr, 1);
1972 for (j=i+1; j<psl->sl_count; j++)
1973 psl->sl_addr[j-1] = psl->sl_addr[j];
1978 /* else, add a new source to the filter */
1980 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1984 if (!psl || psl->sl_count == psl->sl_max) {
1985 struct ip_sf_socklist *newpsl;
1986 int count = IP_SFBLOCK;
1989 count += psl->sl_max;
1990 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1995 newpsl->sl_max = count;
1996 newpsl->sl_count = count - IP_SFBLOCK;
1998 for (i=0; i<psl->sl_count; i++)
1999 newpsl->sl_addr[i] = psl->sl_addr[i];
2000 /* decrease mem now to avoid the memleak warning */
2001 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2002 kfree_rcu(psl, rcu);
2004 RCU_INIT_POINTER(pmc->sflist, newpsl);
2007 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2008 for (i=0; i<psl->sl_count; i++) {
2009 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2014 if (rv == 0) /* address already there is an error */
2016 for (j=psl->sl_count-1; j>=i; j--)
2017 psl->sl_addr[j+1] = psl->sl_addr[j];
2018 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2021 /* update the interface list */
2022 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2023 &mreqs->imr_sourceaddr, 1);
2027 return ip_mc_leave_group(sk, &imr);
2031 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2034 struct ip_mreqn imr;
2035 __be32 addr = msf->imsf_multiaddr;
2036 struct ip_mc_socklist *pmc;
2037 struct in_device *in_dev;
2038 struct inet_sock *inet = inet_sk(sk);
2039 struct ip_sf_socklist *newpsl, *psl;
2040 struct net *net = sock_net(sk);
2043 if (!ipv4_is_multicast(addr))
2045 if (msf->imsf_fmode != MCAST_INCLUDE &&
2046 msf->imsf_fmode != MCAST_EXCLUDE)
2051 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2052 imr.imr_address.s_addr = msf->imsf_interface;
2053 imr.imr_ifindex = ifindex;
2054 in_dev = ip_mc_find_dev(net, &imr);
2061 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2062 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2067 for_each_pmc_rtnl(inet, pmc) {
2068 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2069 pmc->multi.imr_ifindex == imr.imr_ifindex)
2072 if (!pmc) { /* must have a prior join */
2076 if (msf->imsf_numsrc) {
2077 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2083 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2084 memcpy(newpsl->sl_addr, msf->imsf_slist,
2085 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2086 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2087 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2089 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2094 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2095 msf->imsf_fmode, 0, NULL, 0);
2097 psl = rtnl_dereference(pmc->sflist);
2099 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2100 psl->sl_count, psl->sl_addr, 0);
2101 /* decrease mem now to avoid the memleak warning */
2102 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2103 kfree_rcu(psl, rcu);
2105 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2107 RCU_INIT_POINTER(pmc->sflist, newpsl);
2108 pmc->sfmode = msf->imsf_fmode;
2113 err = ip_mc_leave_group(sk, &imr);
2117 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2118 struct ip_msfilter __user *optval, int __user *optlen)
2120 int err, len, count, copycount;
2121 struct ip_mreqn imr;
2122 __be32 addr = msf->imsf_multiaddr;
2123 struct ip_mc_socklist *pmc;
2124 struct in_device *in_dev;
2125 struct inet_sock *inet = inet_sk(sk);
2126 struct ip_sf_socklist *psl;
2127 struct net *net = sock_net(sk);
2129 if (!ipv4_is_multicast(addr))
2134 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2135 imr.imr_address.s_addr = msf->imsf_interface;
2136 imr.imr_ifindex = 0;
2137 in_dev = ip_mc_find_dev(net, &imr);
2143 err = -EADDRNOTAVAIL;
2145 for_each_pmc_rtnl(inet, pmc) {
2146 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2147 pmc->multi.imr_ifindex == imr.imr_ifindex)
2150 if (!pmc) /* must have a prior join */
2152 msf->imsf_fmode = pmc->sfmode;
2153 psl = rtnl_dereference(pmc->sflist);
2159 count = psl->sl_count;
2161 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2162 len = copycount * sizeof(psl->sl_addr[0]);
2163 msf->imsf_numsrc = count;
2164 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2165 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2169 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2177 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2178 struct group_filter __user *optval, int __user *optlen)
2180 int err, i, count, copycount;
2181 struct sockaddr_in *psin;
2183 struct ip_mc_socklist *pmc;
2184 struct inet_sock *inet = inet_sk(sk);
2185 struct ip_sf_socklist *psl;
2187 psin = (struct sockaddr_in *)&gsf->gf_group;
2188 if (psin->sin_family != AF_INET)
2190 addr = psin->sin_addr.s_addr;
2191 if (!ipv4_is_multicast(addr))
2196 err = -EADDRNOTAVAIL;
2198 for_each_pmc_rtnl(inet, pmc) {
2199 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2200 pmc->multi.imr_ifindex == gsf->gf_interface)
2203 if (!pmc) /* must have a prior join */
2205 gsf->gf_fmode = pmc->sfmode;
2206 psl = rtnl_dereference(pmc->sflist);
2208 count = psl ? psl->sl_count : 0;
2209 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2210 gsf->gf_numsrc = count;
2211 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2212 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2215 for (i=0; i<copycount; i++) {
2216 struct sockaddr_storage ss;
2218 psin = (struct sockaddr_in *)&ss;
2219 memset(&ss, 0, sizeof(ss));
2220 psin->sin_family = AF_INET;
2221 psin->sin_addr.s_addr = psl->sl_addr[i];
2222 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2232 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2234 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2236 struct inet_sock *inet = inet_sk(sk);
2237 struct ip_mc_socklist *pmc;
2238 struct ip_sf_socklist *psl;
2243 if (!ipv4_is_multicast(loc_addr))
2247 for_each_pmc_rcu(inet, pmc) {
2248 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2249 pmc->multi.imr_ifindex == dif)
2255 psl = rcu_dereference(pmc->sflist);
2256 ret = (pmc->sfmode == MCAST_EXCLUDE);
2260 for (i=0; i<psl->sl_count; i++) {
2261 if (psl->sl_addr[i] == rmt_addr)
2265 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2267 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2277 * A socket is closing.
2280 void ip_mc_drop_socket(struct sock *sk)
2282 struct inet_sock *inet = inet_sk(sk);
2283 struct ip_mc_socklist *iml;
2284 struct net *net = sock_net(sk);
2286 if (inet->mc_list == NULL)
2290 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2291 struct in_device *in_dev;
2293 inet->mc_list = iml->next_rcu;
2294 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2295 (void) ip_mc_leave_src(sk, iml, in_dev);
2297 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2298 /* decrease mem now to avoid the memleak warning */
2299 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2300 kfree_rcu(iml, rcu);
2305 /* called with rcu_read_lock() */
2306 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2308 struct ip_mc_list *im;
2309 struct ip_sf_list *psf;
2312 for_each_pmc_rcu(in_dev, im) {
2313 if (im->multiaddr == mc_addr)
2316 if (im && proto == IPPROTO_IGMP) {
2320 for (psf=im->sources; psf; psf=psf->sf_next) {
2321 if (psf->sf_inaddr == src_addr)
2325 rv = psf->sf_count[MCAST_INCLUDE] ||
2326 psf->sf_count[MCAST_EXCLUDE] !=
2327 im->sfcount[MCAST_EXCLUDE];
2329 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2331 rv = 1; /* unspecified source; tentatively allow */
2336 #if defined(CONFIG_PROC_FS)
2337 struct igmp_mc_iter_state {
2338 struct seq_net_private p;
2339 struct net_device *dev;
2340 struct in_device *in_dev;
2343 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2345 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2347 struct net *net = seq_file_net(seq);
2348 struct ip_mc_list *im = NULL;
2349 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2351 state->in_dev = NULL;
2352 for_each_netdev_rcu(net, state->dev) {
2353 struct in_device *in_dev;
2355 in_dev = __in_dev_get_rcu(state->dev);
2358 im = rcu_dereference(in_dev->mc_list);
2360 state->in_dev = in_dev;
2367 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2369 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2371 im = rcu_dereference(im->next_rcu);
2373 state->dev = next_net_device_rcu(state->dev);
2375 state->in_dev = NULL;
2378 state->in_dev = __in_dev_get_rcu(state->dev);
2381 im = rcu_dereference(state->in_dev->mc_list);
2386 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2388 struct ip_mc_list *im = igmp_mc_get_first(seq);
2390 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2392 return pos ? NULL : im;
2395 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2399 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2402 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2404 struct ip_mc_list *im;
2405 if (v == SEQ_START_TOKEN)
2406 im = igmp_mc_get_first(seq);
2408 im = igmp_mc_get_next(seq, v);
2413 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2416 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2418 state->in_dev = NULL;
2423 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2425 if (v == SEQ_START_TOKEN)
2427 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2429 struct ip_mc_list *im = (struct ip_mc_list *)v;
2430 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2432 #ifdef CONFIG_IP_MULTICAST
2433 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2434 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2440 if (rcu_dereference(state->in_dev->mc_list) == im) {
2441 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2442 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2446 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2447 im->multiaddr, im->users,
2448 im->tm_running, im->tm_running ?
2449 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2455 static const struct seq_operations igmp_mc_seq_ops = {
2456 .start = igmp_mc_seq_start,
2457 .next = igmp_mc_seq_next,
2458 .stop = igmp_mc_seq_stop,
2459 .show = igmp_mc_seq_show,
2462 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2464 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2465 sizeof(struct igmp_mc_iter_state));
2468 static const struct file_operations igmp_mc_seq_fops = {
2469 .owner = THIS_MODULE,
2470 .open = igmp_mc_seq_open,
2472 .llseek = seq_lseek,
2473 .release = seq_release_net,
2476 struct igmp_mcf_iter_state {
2477 struct seq_net_private p;
2478 struct net_device *dev;
2479 struct in_device *idev;
2480 struct ip_mc_list *im;
2483 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2485 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2487 struct net *net = seq_file_net(seq);
2488 struct ip_sf_list *psf = NULL;
2489 struct ip_mc_list *im = NULL;
2490 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2494 for_each_netdev_rcu(net, state->dev) {
2495 struct in_device *idev;
2496 idev = __in_dev_get_rcu(state->dev);
2497 if (unlikely(idev == NULL))
2499 im = rcu_dereference(idev->mc_list);
2500 if (likely(im != NULL)) {
2501 spin_lock_bh(&im->lock);
2503 if (likely(psf != NULL)) {
2508 spin_unlock_bh(&im->lock);
2514 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2516 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2520 spin_unlock_bh(&state->im->lock);
2521 state->im = state->im->next;
2522 while (!state->im) {
2523 state->dev = next_net_device_rcu(state->dev);
2528 state->idev = __in_dev_get_rcu(state->dev);
2531 state->im = rcu_dereference(state->idev->mc_list);
2535 spin_lock_bh(&state->im->lock);
2536 psf = state->im->sources;
2542 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2544 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2546 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2548 return pos ? NULL : psf;
2551 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2555 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2558 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2560 struct ip_sf_list *psf;
2561 if (v == SEQ_START_TOKEN)
2562 psf = igmp_mcf_get_first(seq);
2564 psf = igmp_mcf_get_next(seq, v);
2569 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2572 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2573 if (likely(state->im != NULL)) {
2574 spin_unlock_bh(&state->im->lock);
2582 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2584 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2585 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2587 if (v == SEQ_START_TOKEN) {
2590 "%10s %10s %6s %6s\n", "Idx",
2592 "SRC", "INC", "EXC");
2596 "0x%08x %6lu %6lu\n",
2597 state->dev->ifindex, state->dev->name,
2598 ntohl(state->im->multiaddr),
2599 ntohl(psf->sf_inaddr),
2600 psf->sf_count[MCAST_INCLUDE],
2601 psf->sf_count[MCAST_EXCLUDE]);
2606 static const struct seq_operations igmp_mcf_seq_ops = {
2607 .start = igmp_mcf_seq_start,
2608 .next = igmp_mcf_seq_next,
2609 .stop = igmp_mcf_seq_stop,
2610 .show = igmp_mcf_seq_show,
2613 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2615 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2616 sizeof(struct igmp_mcf_iter_state));
2619 static const struct file_operations igmp_mcf_seq_fops = {
2620 .owner = THIS_MODULE,
2621 .open = igmp_mcf_seq_open,
2623 .llseek = seq_lseek,
2624 .release = seq_release_net,
2627 static int __net_init igmp_net_init(struct net *net)
2629 struct proc_dir_entry *pde;
2631 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2634 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2640 proc_net_remove(net, "igmp");
2645 static void __net_exit igmp_net_exit(struct net *net)
2647 proc_net_remove(net, "mcfilter");
2648 proc_net_remove(net, "igmp");
2651 static struct pernet_operations igmp_net_ops = {
2652 .init = igmp_net_init,
2653 .exit = igmp_net_exit,
2656 int __init igmp_mc_proc_init(void)
2658 return register_pernet_subsys(&igmp_net_ops);