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, struct ip_mc_list *im);
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;
200 unsigned long exp = jiffies + tv + 2;
202 if (in_dev->mr_gq_running &&
203 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
206 in_dev->mr_gq_running = 1;
207 if (!mod_timer(&in_dev->mr_gq_timer, exp))
211 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
213 int tv = net_random() % delay;
215 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
219 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
221 spin_lock_bh(&im->lock);
222 im->unsolicit_count = 0;
223 if (del_timer(&im->timer)) {
224 if ((long)(im->timer.expires-jiffies) < max_delay) {
225 add_timer(&im->timer);
227 spin_unlock_bh(&im->lock);
230 atomic_dec(&im->refcnt);
232 igmp_start_timer(im, max_delay);
233 spin_unlock_bh(&im->lock);
238 * Send an IGMP report.
241 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
244 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
245 int gdeleted, int sdeleted)
248 case IGMPV3_MODE_IS_INCLUDE:
249 case IGMPV3_MODE_IS_EXCLUDE:
250 if (gdeleted || sdeleted)
252 if (!(pmc->gsquery && !psf->sf_gsresp)) {
253 if (pmc->sfmode == MCAST_INCLUDE)
255 /* don't include if this source is excluded
258 if (psf->sf_count[MCAST_INCLUDE])
259 return type == IGMPV3_MODE_IS_INCLUDE;
260 return pmc->sfcount[MCAST_EXCLUDE] ==
261 psf->sf_count[MCAST_EXCLUDE];
264 case IGMPV3_CHANGE_TO_INCLUDE:
265 if (gdeleted || sdeleted)
267 return psf->sf_count[MCAST_INCLUDE] != 0;
268 case IGMPV3_CHANGE_TO_EXCLUDE:
269 if (gdeleted || sdeleted)
271 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
272 psf->sf_count[MCAST_INCLUDE])
274 return pmc->sfcount[MCAST_EXCLUDE] ==
275 psf->sf_count[MCAST_EXCLUDE];
276 case IGMPV3_ALLOW_NEW_SOURCES:
277 if (gdeleted || !psf->sf_crcount)
279 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
280 case IGMPV3_BLOCK_OLD_SOURCES:
281 if (pmc->sfmode == MCAST_INCLUDE)
282 return gdeleted || (psf->sf_crcount && sdeleted);
283 return psf->sf_crcount && !gdeleted && !sdeleted;
289 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
291 struct ip_sf_list *psf;
294 for (psf=pmc->sources; psf; psf=psf->sf_next) {
295 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
302 static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
307 struct igmpv3_report *pig;
308 struct net *net = dev_net(dev);
310 int hlen = LL_RESERVED_SPACE(dev);
311 int tlen = dev->needed_tailroom;
312 unsigned int size = mtu;
315 skb = alloc_skb(size + hlen + tlen,
316 GFP_ATOMIC | __GFP_NOWARN);
324 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
326 IPPROTO_IGMP, 0, dev->ifindex);
332 skb_dst_set(skb, &rt->dst);
335 skb_reserve(skb, hlen);
336 skb_tailroom_reserve(skb, mtu, tlen);
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(skb, 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_availroom(skb) : 0)
407 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
408 int type, int gdeleted, int sdeleted)
410 struct net_device *dev = pmc->interface->dev;
411 struct igmpv3_report *pih;
412 struct igmpv3_grec *pgr = NULL;
413 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
414 int scount, stotal, first, isquery, truncate;
416 if (pmc->multiaddr == IGMP_ALL_HOSTS)
419 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
420 type == IGMPV3_MODE_IS_EXCLUDE;
421 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
422 type == IGMPV3_CHANGE_TO_EXCLUDE;
426 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
431 pih = skb ? igmpv3_report_hdr(skb) : NULL;
433 /* EX and TO_EX get a fresh packet, if needed */
435 if (pih && pih->ngrec &&
436 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
438 igmpv3_sendpack(skb);
439 skb = igmpv3_newpack(dev, dev->mtu);
444 for (psf=*psf_list; psf; psf=psf_next) {
447 psf_next = psf->sf_next;
449 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
454 /* clear marks on query responses */
458 if (AVAILABLE(skb) < sizeof(__be32) +
459 first*sizeof(struct igmpv3_grec)) {
460 if (truncate && !first)
461 break; /* truncate these */
463 pgr->grec_nsrcs = htons(scount);
465 igmpv3_sendpack(skb);
466 skb = igmpv3_newpack(dev, dev->mtu);
471 skb = add_grhead(skb, pmc, type, &pgr);
476 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
477 *psrc = psf->sf_inaddr;
479 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
480 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
482 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
484 psf_prev->sf_next = psf->sf_next;
486 *psf_list = psf->sf_next;
496 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
497 type == IGMPV3_BLOCK_OLD_SOURCES)
499 if (pmc->crcount || isquery) {
500 /* make sure we have room for group header */
501 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
502 igmpv3_sendpack(skb);
503 skb = NULL; /* add_grhead will get a new one */
505 skb = add_grhead(skb, pmc, type, &pgr);
509 pgr->grec_nsrcs = htons(scount);
512 pmc->gsquery = 0; /* clear query state on report */
516 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
518 struct sk_buff *skb = NULL;
523 for_each_pmc_rcu(in_dev, pmc) {
524 if (pmc->multiaddr == IGMP_ALL_HOSTS)
526 spin_lock_bh(&pmc->lock);
527 if (pmc->sfcount[MCAST_EXCLUDE])
528 type = IGMPV3_MODE_IS_EXCLUDE;
530 type = IGMPV3_MODE_IS_INCLUDE;
531 skb = add_grec(skb, pmc, type, 0, 0);
532 spin_unlock_bh(&pmc->lock);
536 spin_lock_bh(&pmc->lock);
537 if (pmc->sfcount[MCAST_EXCLUDE])
538 type = IGMPV3_MODE_IS_EXCLUDE;
540 type = IGMPV3_MODE_IS_INCLUDE;
541 skb = add_grec(skb, pmc, type, 0, 0);
542 spin_unlock_bh(&pmc->lock);
546 return igmpv3_sendpack(skb);
550 * remove zero-count source records from a source filter list
552 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
554 struct ip_sf_list *psf_prev, *psf_next, *psf;
557 for (psf=*ppsf; psf; psf = psf_next) {
558 psf_next = psf->sf_next;
559 if (psf->sf_crcount == 0) {
561 psf_prev->sf_next = psf->sf_next;
563 *ppsf = psf->sf_next;
570 static void igmpv3_send_cr(struct in_device *in_dev)
572 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
573 struct sk_buff *skb = NULL;
577 spin_lock_bh(&in_dev->mc_tomb_lock);
581 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
582 pmc_next = pmc->next;
583 if (pmc->sfmode == MCAST_INCLUDE) {
584 type = IGMPV3_BLOCK_OLD_SOURCES;
585 dtype = IGMPV3_BLOCK_OLD_SOURCES;
586 skb = add_grec(skb, pmc, type, 1, 0);
587 skb = add_grec(skb, pmc, dtype, 1, 1);
590 if (pmc->sfmode == MCAST_EXCLUDE) {
591 type = IGMPV3_CHANGE_TO_INCLUDE;
592 skb = add_grec(skb, pmc, type, 1, 0);
595 if (pmc->crcount == 0) {
596 igmpv3_clear_zeros(&pmc->tomb);
597 igmpv3_clear_zeros(&pmc->sources);
600 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
602 pmc_prev->next = pmc_next;
604 in_dev->mc_tomb = pmc_next;
605 in_dev_put(pmc->interface);
610 spin_unlock_bh(&in_dev->mc_tomb_lock);
613 for_each_pmc_rcu(in_dev, pmc) {
614 spin_lock_bh(&pmc->lock);
615 if (pmc->sfcount[MCAST_EXCLUDE]) {
616 type = IGMPV3_BLOCK_OLD_SOURCES;
617 dtype = IGMPV3_ALLOW_NEW_SOURCES;
619 type = IGMPV3_ALLOW_NEW_SOURCES;
620 dtype = IGMPV3_BLOCK_OLD_SOURCES;
622 skb = add_grec(skb, pmc, type, 0, 0);
623 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
625 /* filter mode changes */
627 if (pmc->sfmode == MCAST_EXCLUDE)
628 type = IGMPV3_CHANGE_TO_EXCLUDE;
630 type = IGMPV3_CHANGE_TO_INCLUDE;
631 skb = add_grec(skb, pmc, type, 0, 0);
634 spin_unlock_bh(&pmc->lock);
640 (void) igmpv3_sendpack(skb);
643 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
650 struct net_device *dev = in_dev->dev;
651 struct net *net = dev_net(dev);
652 __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 hlen = LL_RESERVED_SPACE(dev);
671 tlen = dev->needed_tailroom;
672 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
678 skb_dst_set(skb, &rt->dst);
680 skb_reserve(skb, hlen);
682 skb_reset_network_header(skb);
684 skb_put(skb, sizeof(struct iphdr) + 4);
687 iph->ihl = (sizeof(struct iphdr)+4)>>2;
689 iph->frag_off = htons(IP_DF);
692 iph->saddr = fl4.saddr;
693 iph->protocol = IPPROTO_IGMP;
694 ip_select_ident(skb, NULL);
695 ((u8*)&iph[1])[0] = IPOPT_RA;
696 ((u8*)&iph[1])[1] = 4;
697 ((u8*)&iph[1])[2] = 0;
698 ((u8*)&iph[1])[3] = 0;
700 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
705 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
707 return ip_local_out(skb);
710 static void igmp_gq_timer_expire(unsigned long data)
712 struct in_device *in_dev = (struct in_device *)data;
714 in_dev->mr_gq_running = 0;
715 igmpv3_send_report(in_dev, NULL);
719 static void igmp_ifc_timer_expire(unsigned long data)
721 struct in_device *in_dev = (struct in_device *)data;
723 igmpv3_send_cr(in_dev);
724 if (in_dev->mr_ifc_count) {
725 in_dev->mr_ifc_count--;
726 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
731 static void igmp_ifc_event(struct in_device *in_dev)
733 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
735 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
736 IGMP_Unsolicited_Report_Count;
737 igmp_ifc_start_timer(in_dev, 1);
741 static void igmp_timer_expire(unsigned long data)
743 struct ip_mc_list *im=(struct ip_mc_list *)data;
744 struct in_device *in_dev = im->interface;
746 spin_lock(&im->lock);
749 if (im->unsolicit_count) {
750 im->unsolicit_count--;
751 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
754 spin_unlock(&im->lock);
756 if (IGMP_V1_SEEN(in_dev))
757 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
758 else if (IGMP_V2_SEEN(in_dev))
759 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
761 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
766 /* mark EXCLUDE-mode sources */
767 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
769 struct ip_sf_list *psf;
773 for (psf=pmc->sources; psf; psf=psf->sf_next) {
776 for (i=0; i<nsrcs; i++) {
777 /* skip inactive filters */
778 if (psf->sf_count[MCAST_INCLUDE] ||
779 pmc->sfcount[MCAST_EXCLUDE] !=
780 psf->sf_count[MCAST_EXCLUDE])
782 if (srcs[i] == psf->sf_inaddr) {
789 if (scount == nsrcs) /* all sources excluded */
794 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
796 struct ip_sf_list *psf;
799 if (pmc->sfmode == MCAST_EXCLUDE)
800 return igmp_xmarksources(pmc, nsrcs, srcs);
802 /* mark INCLUDE-mode sources */
804 for (psf=pmc->sources; psf; psf=psf->sf_next) {
807 for (i=0; i<nsrcs; i++)
808 if (srcs[i] == psf->sf_inaddr) {
822 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
824 struct ip_mc_list *im;
826 /* Timers are only set for non-local groups */
828 if (group == IGMP_ALL_HOSTS)
832 for_each_pmc_rcu(in_dev, im) {
833 if (im->multiaddr == group) {
841 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
844 struct igmphdr *ih = igmp_hdr(skb);
845 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
846 struct ip_mc_list *im;
847 __be32 group = ih->group;
854 /* Alas, old v1 router presents here. */
856 max_delay = IGMP_Query_Response_Interval;
857 in_dev->mr_v1_seen = jiffies +
858 IGMP_V1_Router_Present_Timeout;
861 /* v2 router present */
862 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
863 in_dev->mr_v2_seen = jiffies +
864 IGMP_V2_Router_Present_Timeout;
866 /* cancel the interface change timer */
867 in_dev->mr_ifc_count = 0;
868 if (del_timer(&in_dev->mr_ifc_timer))
869 __in_dev_put(in_dev);
870 /* clear deleted report items */
871 igmpv3_clear_delrec(in_dev);
872 } else if (len < 12) {
873 return; /* ignore bogus packet; freed by caller */
874 } else if (IGMP_V1_SEEN(in_dev)) {
875 /* This is a v3 query with v1 queriers present */
876 max_delay = IGMP_Query_Response_Interval;
878 } else if (IGMP_V2_SEEN(in_dev)) {
879 /* this is a v3 query with v2 queriers present;
880 * Interpretation of the max_delay code is problematic here.
881 * A real v2 host would use ih_code directly, while v3 has a
882 * different encoding. We use the v3 encoding as more likely
883 * to be intended in a v3 query.
885 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
887 max_delay = 1; /* can't mod w/ 0 */
889 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
892 ih3 = igmpv3_query_hdr(skb);
894 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
895 + ntohs(ih3->nsrcs)*sizeof(__be32)))
897 ih3 = igmpv3_query_hdr(skb);
900 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
902 max_delay = 1; /* can't mod w/ 0 */
903 in_dev->mr_maxdelay = max_delay;
905 in_dev->mr_qrv = ih3->qrv;
906 if (!group) { /* general query */
908 return; /* no sources allowed */
909 igmp_gq_start_timer(in_dev);
912 /* mark sources to include, if group & source-specific */
913 mark = ih3->nsrcs != 0;
917 * - Start the timers in all of our membership records
918 * that the query applies to for the interface on
919 * which the query arrived excl. those that belong
920 * to a "local" group (224.0.0.X)
921 * - For timers already running check if they need to
923 * - Use the igmp->igmp_code field as the maximum
927 for_each_pmc_rcu(in_dev, im) {
930 if (group && group != im->multiaddr)
932 if (im->multiaddr == IGMP_ALL_HOSTS)
934 spin_lock_bh(&im->lock);
936 im->gsquery = im->gsquery && mark;
939 changed = !im->gsquery ||
940 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
941 spin_unlock_bh(&im->lock);
943 igmp_mod_timer(im, max_delay);
948 /* called in rcu_read_lock() section */
949 int igmp_rcv(struct sk_buff *skb)
951 /* This basically follows the spec line by line -- see RFC1112 */
953 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
959 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
962 switch (skb->ip_summed) {
963 case CHECKSUM_COMPLETE:
964 if (!csum_fold(skb->csum))
969 if (__skb_checksum_complete(skb))
975 case IGMP_HOST_MEMBERSHIP_QUERY:
976 igmp_heard_query(in_dev, skb, len);
978 case IGMP_HOST_MEMBERSHIP_REPORT:
979 case IGMPV2_HOST_MEMBERSHIP_REPORT:
980 /* Is it our report looped back? */
981 if (rt_is_output_route(skb_rtable(skb)))
983 /* don't rely on MC router hearing unicast reports */
984 if (skb->pkt_type == PACKET_MULTICAST ||
985 skb->pkt_type == PACKET_BROADCAST)
986 igmp_heard_report(in_dev, ih->group);
989 #ifdef CONFIG_IP_PIMSM_V1
990 return pim_rcv_v1(skb);
992 case IGMPV3_HOST_MEMBERSHIP_REPORT:
995 case IGMP_HOST_LEAVE_MESSAGE:
997 case IGMP_MTRACE_RESP:
1012 * Add a filter to a device
1015 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1017 char buf[MAX_ADDR_LEN];
1018 struct net_device *dev = in_dev->dev;
1020 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1021 We will get multicast token leakage, when IFF_MULTICAST
1022 is changed. This check should be done in ndo_set_rx_mode
1023 routine. Something sort of:
1024 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1027 if (arp_mc_map(addr, buf, dev, 0) == 0)
1028 dev_mc_add(dev, buf);
1032 * Remove a filter from a device
1035 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1037 char buf[MAX_ADDR_LEN];
1038 struct net_device *dev = in_dev->dev;
1040 if (arp_mc_map(addr, buf, dev, 0) == 0)
1041 dev_mc_del(dev, buf);
1044 #ifdef CONFIG_IP_MULTICAST
1046 * deleted ip_mc_list manipulation
1048 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1050 struct ip_mc_list *pmc;
1052 /* this is an "ip_mc_list" for convenience; only the fields below
1053 * are actually used. In particular, the refcnt and users are not
1054 * used for management of the delete list. Using the same structure
1055 * for deleted items allows change reports to use common code with
1056 * non-deleted or query-response MCA's.
1058 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1061 spin_lock_bh(&im->lock);
1062 pmc->interface = im->interface;
1063 in_dev_hold(in_dev);
1064 pmc->multiaddr = im->multiaddr;
1065 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1066 IGMP_Unsolicited_Report_Count;
1067 pmc->sfmode = im->sfmode;
1068 if (pmc->sfmode == MCAST_INCLUDE) {
1069 struct ip_sf_list *psf;
1071 pmc->tomb = im->tomb;
1072 pmc->sources = im->sources;
1073 im->tomb = im->sources = NULL;
1074 for (psf=pmc->sources; psf; psf=psf->sf_next)
1075 psf->sf_crcount = pmc->crcount;
1077 spin_unlock_bh(&im->lock);
1079 spin_lock_bh(&in_dev->mc_tomb_lock);
1080 pmc->next = in_dev->mc_tomb;
1081 in_dev->mc_tomb = pmc;
1082 spin_unlock_bh(&in_dev->mc_tomb_lock);
1086 * restore ip_mc_list deleted records
1088 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1090 struct ip_mc_list *pmc, *pmc_prev;
1091 struct ip_sf_list *psf;
1092 __be32 multiaddr = im->multiaddr;
1094 spin_lock_bh(&in_dev->mc_tomb_lock);
1096 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1097 if (pmc->multiaddr == multiaddr)
1103 pmc_prev->next = pmc->next;
1105 in_dev->mc_tomb = pmc->next;
1107 spin_unlock_bh(&in_dev->mc_tomb_lock);
1109 spin_lock_bh(&im->lock);
1111 im->interface = pmc->interface;
1112 im->crcount = in_dev->mr_qrv ?: IGMP_Unsolicited_Report_Count;
1113 im->sfmode = pmc->sfmode;
1114 if (pmc->sfmode == MCAST_INCLUDE) {
1115 im->tomb = pmc->tomb;
1116 im->sources = pmc->sources;
1117 for (psf = im->sources; psf; psf = psf->sf_next)
1118 psf->sf_crcount = im->crcount;
1120 in_dev_put(pmc->interface);
1122 spin_unlock_bh(&im->lock);
1126 * flush ip_mc_list deleted records
1128 static void igmpv3_clear_delrec(struct in_device *in_dev)
1130 struct ip_mc_list *pmc, *nextpmc;
1132 spin_lock_bh(&in_dev->mc_tomb_lock);
1133 pmc = in_dev->mc_tomb;
1134 in_dev->mc_tomb = NULL;
1135 spin_unlock_bh(&in_dev->mc_tomb_lock);
1137 for (; pmc; pmc = nextpmc) {
1138 nextpmc = pmc->next;
1139 ip_mc_clear_src(pmc);
1140 in_dev_put(pmc->interface);
1143 /* clear dead sources, too */
1145 for_each_pmc_rcu(in_dev, pmc) {
1146 struct ip_sf_list *psf, *psf_next;
1148 spin_lock_bh(&pmc->lock);
1151 spin_unlock_bh(&pmc->lock);
1152 for (; psf; psf=psf_next) {
1153 psf_next = psf->sf_next;
1161 static void igmp_group_dropped(struct ip_mc_list *im)
1163 struct in_device *in_dev = im->interface;
1164 #ifdef CONFIG_IP_MULTICAST
1170 ip_mc_filter_del(in_dev, im->multiaddr);
1173 #ifdef CONFIG_IP_MULTICAST
1174 if (im->multiaddr == IGMP_ALL_HOSTS)
1177 reporter = im->reporter;
1178 igmp_stop_timer(im);
1180 if (!in_dev->dead) {
1181 if (IGMP_V1_SEEN(in_dev))
1183 if (IGMP_V2_SEEN(in_dev)) {
1185 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1189 igmpv3_add_delrec(in_dev, im);
1191 igmp_ifc_event(in_dev);
1196 static void igmp_group_added(struct ip_mc_list *im)
1198 struct in_device *in_dev = im->interface;
1200 if (im->loaded == 0) {
1202 ip_mc_filter_add(in_dev, im->multiaddr);
1205 #ifdef CONFIG_IP_MULTICAST
1206 if (im->multiaddr == IGMP_ALL_HOSTS)
1211 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1212 spin_lock_bh(&im->lock);
1213 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1214 spin_unlock_bh(&im->lock);
1219 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1220 IGMP_Unsolicited_Report_Count;
1221 igmp_ifc_event(in_dev);
1227 * Multicast list managers
1232 * A socket has joined a multicast group on device dev.
1235 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1237 struct ip_mc_list *im;
1241 for_each_pmc_rtnl(in_dev, im) {
1242 if (im->multiaddr == addr) {
1244 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1249 im = kzalloc(sizeof(*im), GFP_KERNEL);
1254 im->interface = in_dev;
1255 in_dev_hold(in_dev);
1256 im->multiaddr = addr;
1257 /* initial mode is (EX, empty) */
1258 im->sfmode = MCAST_EXCLUDE;
1259 im->sfcount[MCAST_EXCLUDE] = 1;
1260 atomic_set(&im->refcnt, 1);
1261 spin_lock_init(&im->lock);
1262 #ifdef CONFIG_IP_MULTICAST
1263 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1264 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1267 im->next_rcu = in_dev->mc_list;
1269 rcu_assign_pointer(in_dev->mc_list, im);
1271 #ifdef CONFIG_IP_MULTICAST
1272 igmpv3_del_delrec(in_dev, im);
1274 igmp_group_added(im);
1276 ip_rt_multicast_event(in_dev);
1280 EXPORT_SYMBOL(ip_mc_inc_group);
1283 * Resend IGMP JOIN report; used for bonding.
1284 * Called with rcu_read_lock()
1286 void ip_mc_rejoin_groups(struct in_device *in_dev)
1288 #ifdef CONFIG_IP_MULTICAST
1289 struct ip_mc_list *im;
1292 for_each_pmc_rcu(in_dev, im) {
1293 if (im->multiaddr == IGMP_ALL_HOSTS)
1296 /* a failover is happening and switches
1297 * must be notified immediately
1299 if (IGMP_V1_SEEN(in_dev))
1300 type = IGMP_HOST_MEMBERSHIP_REPORT;
1301 else if (IGMP_V2_SEEN(in_dev))
1302 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1304 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1305 igmp_send_report(in_dev, im, type);
1309 EXPORT_SYMBOL(ip_mc_rejoin_groups);
1312 * A socket has left a multicast group on device dev
1315 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1317 struct ip_mc_list *i;
1318 struct ip_mc_list __rcu **ip;
1322 for (ip = &in_dev->mc_list;
1323 (i = rtnl_dereference(*ip)) != NULL;
1324 ip = &i->next_rcu) {
1325 if (i->multiaddr == addr) {
1326 if (--i->users == 0) {
1329 igmp_group_dropped(i);
1333 ip_rt_multicast_event(in_dev);
1342 EXPORT_SYMBOL(ip_mc_dec_group);
1344 /* Device changing type */
1346 void ip_mc_unmap(struct in_device *in_dev)
1348 struct ip_mc_list *pmc;
1352 for_each_pmc_rtnl(in_dev, pmc)
1353 igmp_group_dropped(pmc);
1356 void ip_mc_remap(struct in_device *in_dev)
1358 struct ip_mc_list *pmc;
1362 for_each_pmc_rtnl(in_dev, pmc) {
1363 #ifdef CONFIG_IP_MULTICAST
1364 igmpv3_del_delrec(in_dev, pmc);
1366 igmp_group_added(pmc);
1370 /* Device going down */
1372 void ip_mc_down(struct in_device *in_dev)
1374 struct ip_mc_list *pmc;
1378 for_each_pmc_rtnl(in_dev, pmc)
1379 igmp_group_dropped(pmc);
1381 #ifdef CONFIG_IP_MULTICAST
1382 in_dev->mr_ifc_count = 0;
1383 if (del_timer(&in_dev->mr_ifc_timer))
1384 __in_dev_put(in_dev);
1385 in_dev->mr_gq_running = 0;
1386 if (del_timer(&in_dev->mr_gq_timer))
1387 __in_dev_put(in_dev);
1390 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1393 void ip_mc_init_dev(struct in_device *in_dev)
1397 in_dev->mc_tomb = NULL;
1398 #ifdef CONFIG_IP_MULTICAST
1399 in_dev->mr_gq_running = 0;
1400 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1401 (unsigned long)in_dev);
1402 in_dev->mr_ifc_count = 0;
1403 in_dev->mc_count = 0;
1404 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1405 (unsigned long)in_dev);
1406 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1409 spin_lock_init(&in_dev->mc_tomb_lock);
1412 /* Device going up */
1414 void ip_mc_up(struct in_device *in_dev)
1416 struct ip_mc_list *pmc;
1420 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1422 for_each_pmc_rtnl(in_dev, pmc) {
1423 #ifdef CONFIG_IP_MULTICAST
1424 igmpv3_del_delrec(in_dev, pmc);
1426 igmp_group_added(pmc);
1431 * Device is about to be destroyed: clean up.
1434 void ip_mc_destroy_dev(struct in_device *in_dev)
1436 struct ip_mc_list *i;
1440 /* Deactivate timers */
1442 #ifdef CONFIG_IP_MULTICAST
1443 igmpv3_clear_delrec(in_dev);
1446 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1447 in_dev->mc_list = i->next_rcu;
1453 /* RTNL is locked */
1454 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1456 struct net_device *dev = NULL;
1457 struct in_device *idev = NULL;
1459 if (imr->imr_ifindex) {
1460 idev = inetdev_by_index(net, imr->imr_ifindex);
1463 if (imr->imr_address.s_addr) {
1464 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1470 struct rtable *rt = ip_route_output(net,
1471 imr->imr_multiaddr.s_addr,
1479 imr->imr_ifindex = dev->ifindex;
1480 idev = __in_dev_get_rtnl(dev);
1486 * Join a socket to a group
1488 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1489 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1492 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1495 struct ip_sf_list *psf, *psf_prev;
1499 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1500 if (psf->sf_inaddr == *psfsrc)
1504 if (!psf || psf->sf_count[sfmode] == 0) {
1505 /* source filter not found, or count wrong => bug */
1508 psf->sf_count[sfmode]--;
1509 if (psf->sf_count[sfmode] == 0) {
1510 ip_rt_multicast_event(pmc->interface);
1512 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1513 #ifdef CONFIG_IP_MULTICAST
1514 struct in_device *in_dev = pmc->interface;
1517 /* no more filters for this source */
1519 psf_prev->sf_next = psf->sf_next;
1521 pmc->sources = psf->sf_next;
1522 #ifdef CONFIG_IP_MULTICAST
1523 if (psf->sf_oldin &&
1524 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1525 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1526 IGMP_Unsolicited_Report_Count;
1527 psf->sf_next = pmc->tomb;
1537 #ifndef CONFIG_IP_MULTICAST
1538 #define igmp_ifc_event(x) do { } while (0)
1541 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1542 int sfcount, __be32 *psfsrc, int delta)
1544 struct ip_mc_list *pmc;
1551 for_each_pmc_rcu(in_dev, pmc) {
1552 if (*pmca == pmc->multiaddr)
1556 /* MCA not found?? bug */
1560 spin_lock_bh(&pmc->lock);
1562 #ifdef CONFIG_IP_MULTICAST
1567 if (!pmc->sfcount[sfmode])
1569 pmc->sfcount[sfmode]--;
1572 for (i=0; i<sfcount; i++) {
1573 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1575 changerec |= rv > 0;
1579 if (pmc->sfmode == MCAST_EXCLUDE &&
1580 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1581 pmc->sfcount[MCAST_INCLUDE]) {
1582 #ifdef CONFIG_IP_MULTICAST
1583 struct ip_sf_list *psf;
1586 /* filter mode change */
1587 pmc->sfmode = MCAST_INCLUDE;
1588 #ifdef CONFIG_IP_MULTICAST
1589 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1590 IGMP_Unsolicited_Report_Count;
1591 in_dev->mr_ifc_count = pmc->crcount;
1592 for (psf=pmc->sources; psf; psf = psf->sf_next)
1593 psf->sf_crcount = 0;
1594 igmp_ifc_event(pmc->interface);
1595 } else if (sf_setstate(pmc) || changerec) {
1596 igmp_ifc_event(pmc->interface);
1600 spin_unlock_bh(&pmc->lock);
1605 * Add multicast single-source filter to the interface list
1607 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1608 __be32 *psfsrc, int delta)
1610 struct ip_sf_list *psf, *psf_prev;
1613 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1614 if (psf->sf_inaddr == *psfsrc)
1619 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1622 psf->sf_inaddr = *psfsrc;
1624 psf_prev->sf_next = psf;
1628 psf->sf_count[sfmode]++;
1629 if (psf->sf_count[sfmode] == 1) {
1630 ip_rt_multicast_event(pmc->interface);
1635 #ifdef CONFIG_IP_MULTICAST
1636 static void sf_markstate(struct ip_mc_list *pmc)
1638 struct ip_sf_list *psf;
1639 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1641 for (psf=pmc->sources; psf; psf=psf->sf_next)
1642 if (pmc->sfcount[MCAST_EXCLUDE]) {
1643 psf->sf_oldin = mca_xcount ==
1644 psf->sf_count[MCAST_EXCLUDE] &&
1645 !psf->sf_count[MCAST_INCLUDE];
1647 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1650 static int sf_setstate(struct ip_mc_list *pmc)
1652 struct ip_sf_list *psf, *dpsf;
1653 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1654 int qrv = pmc->interface->mr_qrv;
1658 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1659 if (pmc->sfcount[MCAST_EXCLUDE]) {
1660 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1661 !psf->sf_count[MCAST_INCLUDE];
1663 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1665 if (!psf->sf_oldin) {
1666 struct ip_sf_list *prev = NULL;
1668 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1669 if (dpsf->sf_inaddr == psf->sf_inaddr)
1675 prev->sf_next = dpsf->sf_next;
1677 pmc->tomb = dpsf->sf_next;
1680 psf->sf_crcount = qrv;
1683 } else if (psf->sf_oldin) {
1685 psf->sf_crcount = 0;
1687 * add or update "delete" records if an active filter
1690 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1691 if (dpsf->sf_inaddr == psf->sf_inaddr)
1694 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1698 /* pmc->lock held by callers */
1699 dpsf->sf_next = pmc->tomb;
1702 dpsf->sf_crcount = qrv;
1711 * Add multicast source filter list to the interface list
1713 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1714 int sfcount, __be32 *psfsrc, int delta)
1716 struct ip_mc_list *pmc;
1723 for_each_pmc_rcu(in_dev, pmc) {
1724 if (*pmca == pmc->multiaddr)
1728 /* MCA not found?? bug */
1732 spin_lock_bh(&pmc->lock);
1735 #ifdef CONFIG_IP_MULTICAST
1738 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1740 pmc->sfcount[sfmode]++;
1742 for (i=0; i<sfcount; i++) {
1743 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1751 pmc->sfcount[sfmode]--;
1753 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
1754 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1755 #ifdef CONFIG_IP_MULTICAST
1756 struct ip_sf_list *psf;
1757 in_dev = pmc->interface;
1760 /* filter mode change */
1761 if (pmc->sfcount[MCAST_EXCLUDE])
1762 pmc->sfmode = MCAST_EXCLUDE;
1763 else if (pmc->sfcount[MCAST_INCLUDE])
1764 pmc->sfmode = MCAST_INCLUDE;
1765 #ifdef CONFIG_IP_MULTICAST
1766 /* else no filters; keep old mode for reports */
1768 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1769 IGMP_Unsolicited_Report_Count;
1770 in_dev->mr_ifc_count = pmc->crcount;
1771 for (psf=pmc->sources; psf; psf = psf->sf_next)
1772 psf->sf_crcount = 0;
1773 igmp_ifc_event(in_dev);
1774 } else if (sf_setstate(pmc)) {
1775 igmp_ifc_event(in_dev);
1778 spin_unlock_bh(&pmc->lock);
1782 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1784 struct ip_sf_list *psf, *nextpsf;
1786 for (psf=pmc->tomb; psf; psf=nextpsf) {
1787 nextpsf = psf->sf_next;
1791 for (psf=pmc->sources; psf; psf=nextpsf) {
1792 nextpsf = psf->sf_next;
1795 pmc->sources = NULL;
1796 pmc->sfmode = MCAST_EXCLUDE;
1797 pmc->sfcount[MCAST_INCLUDE] = 0;
1798 pmc->sfcount[MCAST_EXCLUDE] = 1;
1803 * Join a multicast group
1805 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1808 __be32 addr = imr->imr_multiaddr.s_addr;
1809 struct ip_mc_socklist *iml = NULL, *i;
1810 struct in_device *in_dev;
1811 struct inet_sock *inet = inet_sk(sk);
1812 struct net *net = sock_net(sk);
1816 if (!ipv4_is_multicast(addr))
1821 in_dev = ip_mc_find_dev(net, imr);
1830 ifindex = imr->imr_ifindex;
1831 for_each_pmc_rtnl(inet, i) {
1832 if (i->multi.imr_multiaddr.s_addr == addr &&
1833 i->multi.imr_ifindex == ifindex)
1838 if (count >= sysctl_igmp_max_memberships)
1840 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1844 memcpy(&iml->multi, imr, sizeof(*imr));
1845 iml->next_rcu = inet->mc_list;
1847 iml->sfmode = MCAST_EXCLUDE;
1848 rcu_assign_pointer(inet->mc_list, iml);
1849 ip_mc_inc_group(in_dev, addr);
1855 EXPORT_SYMBOL(ip_mc_join_group);
1857 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1858 struct in_device *in_dev)
1860 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1864 /* any-source empty exclude case */
1865 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1866 iml->sfmode, 0, NULL, 0);
1868 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1869 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1870 RCU_INIT_POINTER(iml->sflist, NULL);
1871 /* decrease mem now to avoid the memleak warning */
1872 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1873 kfree_rcu(psf, rcu);
1878 * Ask a socket to leave a group.
1881 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1883 struct inet_sock *inet = inet_sk(sk);
1884 struct ip_mc_socklist *iml;
1885 struct ip_mc_socklist __rcu **imlp;
1886 struct in_device *in_dev;
1887 struct net *net = sock_net(sk);
1888 __be32 group = imr->imr_multiaddr.s_addr;
1890 int ret = -EADDRNOTAVAIL;
1893 in_dev = ip_mc_find_dev(net, imr);
1894 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
1898 ifindex = imr->imr_ifindex;
1899 for (imlp = &inet->mc_list;
1900 (iml = rtnl_dereference(*imlp)) != NULL;
1901 imlp = &iml->next_rcu) {
1902 if (iml->multi.imr_multiaddr.s_addr != group)
1905 if (iml->multi.imr_ifindex != ifindex)
1907 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1908 iml->multi.imr_address.s_addr)
1911 (void) ip_mc_leave_src(sk, iml, in_dev);
1913 *imlp = iml->next_rcu;
1916 ip_mc_dec_group(in_dev, group);
1918 /* decrease mem now to avoid the memleak warning */
1919 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1920 kfree_rcu(iml, rcu);
1928 int ip_mc_source(int add, int omode, struct sock *sk, struct
1929 ip_mreq_source *mreqs, int ifindex)
1932 struct ip_mreqn imr;
1933 __be32 addr = mreqs->imr_multiaddr;
1934 struct ip_mc_socklist *pmc;
1935 struct in_device *in_dev = NULL;
1936 struct inet_sock *inet = inet_sk(sk);
1937 struct ip_sf_socklist *psl;
1938 struct net *net = sock_net(sk);
1942 if (!ipv4_is_multicast(addr))
1947 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1948 imr.imr_address.s_addr = mreqs->imr_interface;
1949 imr.imr_ifindex = ifindex;
1950 in_dev = ip_mc_find_dev(net, &imr);
1956 err = -EADDRNOTAVAIL;
1958 for_each_pmc_rtnl(inet, pmc) {
1959 if ((pmc->multi.imr_multiaddr.s_addr ==
1960 imr.imr_multiaddr.s_addr) &&
1961 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1964 if (!pmc) { /* must have a prior join */
1968 /* if a source filter was set, must be the same mode as before */
1970 if (pmc->sfmode != omode) {
1974 } else if (pmc->sfmode != omode) {
1975 /* allow mode switches for empty-set filters */
1976 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1977 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1979 pmc->sfmode = omode;
1982 psl = rtnl_dereference(pmc->sflist);
1985 goto done; /* err = -EADDRNOTAVAIL */
1987 for (i=0; i<psl->sl_count; i++) {
1988 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1993 if (rv) /* source not found */
1994 goto done; /* err = -EADDRNOTAVAIL */
1996 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1997 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2002 /* update the interface filter */
2003 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2004 &mreqs->imr_sourceaddr, 1);
2006 for (j=i+1; j<psl->sl_count; j++)
2007 psl->sl_addr[j-1] = psl->sl_addr[j];
2012 /* else, add a new source to the filter */
2014 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2018 if (!psl || psl->sl_count == psl->sl_max) {
2019 struct ip_sf_socklist *newpsl;
2020 int count = IP_SFBLOCK;
2023 count += psl->sl_max;
2024 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2029 newpsl->sl_max = count;
2030 newpsl->sl_count = count - IP_SFBLOCK;
2032 for (i=0; i<psl->sl_count; i++)
2033 newpsl->sl_addr[i] = psl->sl_addr[i];
2034 /* decrease mem now to avoid the memleak warning */
2035 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2036 kfree_rcu(psl, rcu);
2038 rcu_assign_pointer(pmc->sflist, newpsl);
2041 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2042 for (i=0; i<psl->sl_count; i++) {
2043 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2048 if (rv == 0) /* address already there is an error */
2050 for (j=psl->sl_count-1; j>=i; j--)
2051 psl->sl_addr[j+1] = psl->sl_addr[j];
2052 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2055 /* update the interface list */
2056 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2057 &mreqs->imr_sourceaddr, 1);
2061 return ip_mc_leave_group(sk, &imr);
2065 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2068 struct ip_mreqn imr;
2069 __be32 addr = msf->imsf_multiaddr;
2070 struct ip_mc_socklist *pmc;
2071 struct in_device *in_dev;
2072 struct inet_sock *inet = inet_sk(sk);
2073 struct ip_sf_socklist *newpsl, *psl;
2074 struct net *net = sock_net(sk);
2077 if (!ipv4_is_multicast(addr))
2079 if (msf->imsf_fmode != MCAST_INCLUDE &&
2080 msf->imsf_fmode != MCAST_EXCLUDE)
2085 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2086 imr.imr_address.s_addr = msf->imsf_interface;
2087 imr.imr_ifindex = ifindex;
2088 in_dev = ip_mc_find_dev(net, &imr);
2095 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2096 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2101 for_each_pmc_rtnl(inet, pmc) {
2102 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2103 pmc->multi.imr_ifindex == imr.imr_ifindex)
2106 if (!pmc) { /* must have a prior join */
2110 if (msf->imsf_numsrc) {
2111 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2117 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2118 memcpy(newpsl->sl_addr, msf->imsf_slist,
2119 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2120 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2121 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2123 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2128 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2129 msf->imsf_fmode, 0, NULL, 0);
2131 psl = rtnl_dereference(pmc->sflist);
2133 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2134 psl->sl_count, psl->sl_addr, 0);
2135 /* decrease mem now to avoid the memleak warning */
2136 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2137 kfree_rcu(psl, rcu);
2139 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2141 rcu_assign_pointer(pmc->sflist, newpsl);
2142 pmc->sfmode = msf->imsf_fmode;
2147 err = ip_mc_leave_group(sk, &imr);
2151 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2152 struct ip_msfilter __user *optval, int __user *optlen)
2154 int err, len, count, copycount;
2155 struct ip_mreqn imr;
2156 __be32 addr = msf->imsf_multiaddr;
2157 struct ip_mc_socklist *pmc;
2158 struct in_device *in_dev;
2159 struct inet_sock *inet = inet_sk(sk);
2160 struct ip_sf_socklist *psl;
2161 struct net *net = sock_net(sk);
2163 if (!ipv4_is_multicast(addr))
2168 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2169 imr.imr_address.s_addr = msf->imsf_interface;
2170 imr.imr_ifindex = 0;
2171 in_dev = ip_mc_find_dev(net, &imr);
2177 err = -EADDRNOTAVAIL;
2179 for_each_pmc_rtnl(inet, pmc) {
2180 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2181 pmc->multi.imr_ifindex == imr.imr_ifindex)
2184 if (!pmc) /* must have a prior join */
2186 msf->imsf_fmode = pmc->sfmode;
2187 psl = rtnl_dereference(pmc->sflist);
2193 count = psl->sl_count;
2195 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2196 len = copycount * sizeof(psl->sl_addr[0]);
2197 msf->imsf_numsrc = count;
2198 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2199 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2203 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2211 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2212 struct group_filter __user *optval, int __user *optlen)
2214 int err, i, count, copycount;
2215 struct sockaddr_in *psin;
2217 struct ip_mc_socklist *pmc;
2218 struct inet_sock *inet = inet_sk(sk);
2219 struct ip_sf_socklist *psl;
2221 psin = (struct sockaddr_in *)&gsf->gf_group;
2222 if (psin->sin_family != AF_INET)
2224 addr = psin->sin_addr.s_addr;
2225 if (!ipv4_is_multicast(addr))
2230 err = -EADDRNOTAVAIL;
2232 for_each_pmc_rtnl(inet, pmc) {
2233 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2234 pmc->multi.imr_ifindex == gsf->gf_interface)
2237 if (!pmc) /* must have a prior join */
2239 gsf->gf_fmode = pmc->sfmode;
2240 psl = rtnl_dereference(pmc->sflist);
2242 count = psl ? psl->sl_count : 0;
2243 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2244 gsf->gf_numsrc = count;
2245 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2246 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2249 for (i=0; i<copycount; i++) {
2250 struct sockaddr_storage ss;
2252 psin = (struct sockaddr_in *)&ss;
2253 memset(&ss, 0, sizeof(ss));
2254 psin->sin_family = AF_INET;
2255 psin->sin_addr.s_addr = psl->sl_addr[i];
2256 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2266 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2268 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2270 struct inet_sock *inet = inet_sk(sk);
2271 struct ip_mc_socklist *pmc;
2272 struct ip_sf_socklist *psl;
2277 if (!ipv4_is_multicast(loc_addr))
2281 for_each_pmc_rcu(inet, pmc) {
2282 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2283 pmc->multi.imr_ifindex == dif)
2289 psl = rcu_dereference(pmc->sflist);
2290 ret = (pmc->sfmode == MCAST_EXCLUDE);
2294 for (i=0; i<psl->sl_count; i++) {
2295 if (psl->sl_addr[i] == rmt_addr)
2299 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2301 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2311 * A socket is closing.
2314 void ip_mc_drop_socket(struct sock *sk)
2316 struct inet_sock *inet = inet_sk(sk);
2317 struct ip_mc_socklist *iml;
2318 struct net *net = sock_net(sk);
2320 if (inet->mc_list == NULL)
2324 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2325 struct in_device *in_dev;
2327 inet->mc_list = iml->next_rcu;
2328 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2329 (void) ip_mc_leave_src(sk, iml, in_dev);
2331 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2332 /* decrease mem now to avoid the memleak warning */
2333 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2334 kfree_rcu(iml, rcu);
2339 /* called with rcu_read_lock() */
2340 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2342 struct ip_mc_list *im;
2343 struct ip_sf_list *psf;
2346 for_each_pmc_rcu(in_dev, im) {
2347 if (im->multiaddr == mc_addr)
2350 if (im && proto == IPPROTO_IGMP) {
2354 for (psf=im->sources; psf; psf=psf->sf_next) {
2355 if (psf->sf_inaddr == src_addr)
2359 rv = psf->sf_count[MCAST_INCLUDE] ||
2360 psf->sf_count[MCAST_EXCLUDE] !=
2361 im->sfcount[MCAST_EXCLUDE];
2363 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2365 rv = 1; /* unspecified source; tentatively allow */
2370 #if defined(CONFIG_PROC_FS)
2371 struct igmp_mc_iter_state {
2372 struct seq_net_private p;
2373 struct net_device *dev;
2374 struct in_device *in_dev;
2377 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2379 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2381 struct net *net = seq_file_net(seq);
2382 struct ip_mc_list *im = NULL;
2383 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2385 state->in_dev = NULL;
2386 for_each_netdev_rcu(net, state->dev) {
2387 struct in_device *in_dev;
2389 in_dev = __in_dev_get_rcu(state->dev);
2392 im = rcu_dereference(in_dev->mc_list);
2394 state->in_dev = in_dev;
2401 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2403 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2405 im = rcu_dereference(im->next_rcu);
2407 state->dev = next_net_device_rcu(state->dev);
2409 state->in_dev = NULL;
2412 state->in_dev = __in_dev_get_rcu(state->dev);
2415 im = rcu_dereference(state->in_dev->mc_list);
2420 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2422 struct ip_mc_list *im = igmp_mc_get_first(seq);
2424 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2426 return pos ? NULL : im;
2429 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2433 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2436 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2438 struct ip_mc_list *im;
2439 if (v == SEQ_START_TOKEN)
2440 im = igmp_mc_get_first(seq);
2442 im = igmp_mc_get_next(seq, v);
2447 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2450 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2452 state->in_dev = NULL;
2457 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2459 if (v == SEQ_START_TOKEN)
2461 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2463 struct ip_mc_list *im = (struct ip_mc_list *)v;
2464 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2466 #ifdef CONFIG_IP_MULTICAST
2467 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2468 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2474 if (rcu_dereference(state->in_dev->mc_list) == im) {
2475 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2476 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2480 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2481 im->multiaddr, im->users,
2482 im->tm_running, im->tm_running ?
2483 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2489 static const struct seq_operations igmp_mc_seq_ops = {
2490 .start = igmp_mc_seq_start,
2491 .next = igmp_mc_seq_next,
2492 .stop = igmp_mc_seq_stop,
2493 .show = igmp_mc_seq_show,
2496 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2498 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2499 sizeof(struct igmp_mc_iter_state));
2502 static const struct file_operations igmp_mc_seq_fops = {
2503 .owner = THIS_MODULE,
2504 .open = igmp_mc_seq_open,
2506 .llseek = seq_lseek,
2507 .release = seq_release_net,
2510 struct igmp_mcf_iter_state {
2511 struct seq_net_private p;
2512 struct net_device *dev;
2513 struct in_device *idev;
2514 struct ip_mc_list *im;
2517 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2519 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2521 struct net *net = seq_file_net(seq);
2522 struct ip_sf_list *psf = NULL;
2523 struct ip_mc_list *im = NULL;
2524 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2528 for_each_netdev_rcu(net, state->dev) {
2529 struct in_device *idev;
2530 idev = __in_dev_get_rcu(state->dev);
2531 if (unlikely(idev == NULL))
2533 im = rcu_dereference(idev->mc_list);
2534 if (likely(im != NULL)) {
2535 spin_lock_bh(&im->lock);
2537 if (likely(psf != NULL)) {
2542 spin_unlock_bh(&im->lock);
2548 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2550 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2554 spin_unlock_bh(&state->im->lock);
2555 state->im = state->im->next;
2556 while (!state->im) {
2557 state->dev = next_net_device_rcu(state->dev);
2562 state->idev = __in_dev_get_rcu(state->dev);
2565 state->im = rcu_dereference(state->idev->mc_list);
2569 spin_lock_bh(&state->im->lock);
2570 psf = state->im->sources;
2576 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2578 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2580 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2582 return pos ? NULL : psf;
2585 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2589 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2592 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2594 struct ip_sf_list *psf;
2595 if (v == SEQ_START_TOKEN)
2596 psf = igmp_mcf_get_first(seq);
2598 psf = igmp_mcf_get_next(seq, v);
2603 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2606 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2607 if (likely(state->im != NULL)) {
2608 spin_unlock_bh(&state->im->lock);
2616 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2618 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2619 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2621 if (v == SEQ_START_TOKEN) {
2624 "%10s %10s %6s %6s\n", "Idx",
2626 "SRC", "INC", "EXC");
2630 "0x%08x %6lu %6lu\n",
2631 state->dev->ifindex, state->dev->name,
2632 ntohl(state->im->multiaddr),
2633 ntohl(psf->sf_inaddr),
2634 psf->sf_count[MCAST_INCLUDE],
2635 psf->sf_count[MCAST_EXCLUDE]);
2640 static const struct seq_operations igmp_mcf_seq_ops = {
2641 .start = igmp_mcf_seq_start,
2642 .next = igmp_mcf_seq_next,
2643 .stop = igmp_mcf_seq_stop,
2644 .show = igmp_mcf_seq_show,
2647 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2649 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2650 sizeof(struct igmp_mcf_iter_state));
2653 static const struct file_operations igmp_mcf_seq_fops = {
2654 .owner = THIS_MODULE,
2655 .open = igmp_mcf_seq_open,
2657 .llseek = seq_lseek,
2658 .release = seq_release_net,
2661 static int __net_init igmp_net_init(struct net *net)
2663 struct proc_dir_entry *pde;
2665 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2668 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2674 proc_net_remove(net, "igmp");
2679 static void __net_exit igmp_net_exit(struct net *net)
2681 proc_net_remove(net, "mcfilter");
2682 proc_net_remove(net, "igmp");
2685 static struct pernet_operations igmp_net_ops = {
2686 .init = igmp_net_init,
2687 .exit = igmp_net_exit,
2690 int __init igmp_mc_proc_init(void)
2692 return register_pernet_subsys(&igmp_net_ops);