2 * Linux NET3: Internet Group Management Protocol [IGMP]
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
12 * Alan Cox <alan@lxorguk.ukuu.org.uk>
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36 * The enhancements are mainly based on Steve Deering's
37 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
50 * memberships but was being deleted,
51 * which caused a "del_timer() called
52 * from %p with timer not initialized\n"
54 * Christian Daudt : removed del_timer from
55 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/types.h>
78 #include <linux/kernel.h>
79 #include <linux/jiffies.h>
80 #include <linux/string.h>
81 #include <linux/socket.h>
82 #include <linux/sockios.h>
84 #include <linux/inet.h>
85 #include <linux/netdevice.h>
86 #include <linux/skbuff.h>
87 #include <linux/inetdevice.h>
88 #include <linux/igmp.h>
89 #include <linux/if_arp.h>
90 #include <linux/rtnetlink.h>
91 #include <linux/times.h>
93 #include <net/net_namespace.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
99 #include <net/checksum.h>
100 #include <linux/netfilter_ipv4.h>
101 #ifdef CONFIG_IP_MROUTE
102 #include <linux/mroute.h>
104 #ifdef CONFIG_PROC_FS
105 #include <linux/proc_fs.h>
106 #include <linux/seq_file.h>
109 #define IP_MAX_MEMBERSHIPS 20
110 #define IP_MAX_MSF 10
112 #ifdef CONFIG_IP_MULTICAST
113 /* Parameter names and values are taken from igmp-v2-06 draft */
115 #define IGMP_V1_Router_Present_Timeout (400*HZ)
116 #define IGMP_V2_Router_Present_Timeout (400*HZ)
117 #define IGMP_Unsolicited_Report_Interval (10*HZ)
118 #define IGMP_Query_Response_Interval (10*HZ)
119 #define IGMP_Unsolicited_Report_Count 2
122 #define IGMP_Initial_Report_Delay (1)
124 /* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
131 #define IGMP_V1_SEEN(in_dev) \
132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136 #define IGMP_V2_SEEN(in_dev) \
137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
142 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
143 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
144 static void igmpv3_clear_delrec(struct in_device *in_dev);
145 static int sf_setstate(struct ip_mc_list *pmc);
146 static void sf_markstate(struct ip_mc_list *pmc);
148 static void ip_mc_clear_src(struct ip_mc_list *pmc);
149 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
153 static void ip_mc_list_reclaim(struct rcu_head *head)
155 kfree(container_of(head, struct ip_mc_list, rcu));
158 static void ip_ma_put(struct ip_mc_list *im)
160 if (atomic_dec_and_test(&im->refcnt)) {
161 in_dev_put(im->interface);
162 call_rcu(&im->rcu, ip_mc_list_reclaim);
166 #define for_each_pmc_rcu(in_dev, pmc) \
167 for (pmc = rcu_dereference(in_dev->mc_list); \
169 pmc = rcu_dereference(pmc->next_rcu))
171 #define for_each_pmc_rtnl(in_dev, pmc) \
172 for (pmc = rtnl_dereference(in_dev->mc_list); \
174 pmc = rtnl_dereference(pmc->next_rcu))
176 #ifdef CONFIG_IP_MULTICAST
182 static void igmp_stop_timer(struct ip_mc_list *im)
184 spin_lock_bh(&im->lock);
185 if (del_timer(&im->timer))
186 atomic_dec(&im->refcnt);
189 im->unsolicit_count = 0;
190 spin_unlock_bh(&im->lock);
193 /* It must be called with locked im->lock */
194 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
196 int tv = net_random() % max_delay;
199 if (!mod_timer(&im->timer, jiffies+tv+2))
200 atomic_inc(&im->refcnt);
203 static void igmp_gq_start_timer(struct in_device *in_dev)
205 int tv = net_random() % in_dev->mr_maxdelay;
207 in_dev->mr_gq_running = 1;
208 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
212 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
214 int tv = net_random() % delay;
216 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
220 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
222 spin_lock_bh(&im->lock);
223 im->unsolicit_count = 0;
224 if (del_timer(&im->timer)) {
225 if ((long)(im->timer.expires-jiffies) < max_delay) {
226 add_timer(&im->timer);
228 spin_unlock_bh(&im->lock);
231 atomic_dec(&im->refcnt);
233 igmp_start_timer(im, max_delay);
234 spin_unlock_bh(&im->lock);
239 * Send an IGMP report.
242 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
245 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
246 int gdeleted, int sdeleted)
249 case IGMPV3_MODE_IS_INCLUDE:
250 case IGMPV3_MODE_IS_EXCLUDE:
251 if (gdeleted || sdeleted)
253 if (!(pmc->gsquery && !psf->sf_gsresp)) {
254 if (pmc->sfmode == MCAST_INCLUDE)
256 /* don't include if this source is excluded
259 if (psf->sf_count[MCAST_INCLUDE])
260 return type == IGMPV3_MODE_IS_INCLUDE;
261 return pmc->sfcount[MCAST_EXCLUDE] ==
262 psf->sf_count[MCAST_EXCLUDE];
265 case IGMPV3_CHANGE_TO_INCLUDE:
266 if (gdeleted || sdeleted)
268 return psf->sf_count[MCAST_INCLUDE] != 0;
269 case IGMPV3_CHANGE_TO_EXCLUDE:
270 if (gdeleted || sdeleted)
272 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
273 psf->sf_count[MCAST_INCLUDE])
275 return pmc->sfcount[MCAST_EXCLUDE] ==
276 psf->sf_count[MCAST_EXCLUDE];
277 case IGMPV3_ALLOW_NEW_SOURCES:
278 if (gdeleted || !psf->sf_crcount)
280 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
281 case IGMPV3_BLOCK_OLD_SOURCES:
282 if (pmc->sfmode == MCAST_INCLUDE)
283 return gdeleted || (psf->sf_crcount && sdeleted);
284 return psf->sf_crcount && !gdeleted && !sdeleted;
290 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
292 struct ip_sf_list *psf;
295 for (psf=pmc->sources; psf; psf=psf->sf_next) {
296 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
303 #define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
305 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
310 struct igmpv3_report *pig;
311 struct net *net = dev_net(dev);
314 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
315 GFP_ATOMIC | __GFP_NOWARN);
322 igmp_skb_size(skb) = size;
325 struct flowi fl = { .oif = dev->ifindex,
326 .fl4_dst = IGMPV3_ALL_MCR,
327 .proto = IPPROTO_IGMP };
328 rt = ip_route_output_key(net, &fl);
334 if (rt->rt_src == 0) {
340 skb_dst_set(skb, &rt->dst);
343 skb_reserve(skb, LL_RESERVED_SPACE(dev));
345 skb_reset_network_header(skb);
347 skb_put(skb, sizeof(struct iphdr) + 4);
350 pip->ihl = (sizeof(struct iphdr)+4)>>2;
352 pip->frag_off = htons(IP_DF);
354 pip->daddr = rt->rt_dst;
355 pip->saddr = rt->rt_src;
356 pip->protocol = IPPROTO_IGMP;
357 pip->tot_len = 0; /* filled in later */
358 ip_select_ident(pip, &rt->dst, NULL);
359 ((u8*)&pip[1])[0] = IPOPT_RA;
360 ((u8*)&pip[1])[1] = 4;
361 ((u8*)&pip[1])[2] = 0;
362 ((u8*)&pip[1])[3] = 0;
364 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
365 skb_put(skb, sizeof(*pig));
366 pig = igmpv3_report_hdr(skb);
367 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
375 static int igmpv3_sendpack(struct sk_buff *skb)
377 struct igmphdr *pig = igmp_hdr(skb);
378 const int igmplen = skb->tail - skb->transport_header;
380 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
382 return ip_local_out(skb);
385 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
387 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
390 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
391 int type, struct igmpv3_grec **ppgr)
393 struct net_device *dev = pmc->interface->dev;
394 struct igmpv3_report *pih;
395 struct igmpv3_grec *pgr;
398 skb = igmpv3_newpack(dev, dev->mtu);
401 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
402 pgr->grec_type = type;
403 pgr->grec_auxwords = 0;
405 pgr->grec_mca = pmc->multiaddr;
406 pih = igmpv3_report_hdr(skb);
407 pih->ngrec = htons(ntohs(pih->ngrec)+1);
412 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
413 skb_tailroom(skb)) : 0)
415 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
416 int type, int gdeleted, int sdeleted)
418 struct net_device *dev = pmc->interface->dev;
419 struct igmpv3_report *pih;
420 struct igmpv3_grec *pgr = NULL;
421 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
422 int scount, stotal, first, isquery, truncate;
424 if (pmc->multiaddr == IGMP_ALL_HOSTS)
427 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
428 type == IGMPV3_MODE_IS_EXCLUDE;
429 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
430 type == IGMPV3_CHANGE_TO_EXCLUDE;
434 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
439 pih = skb ? igmpv3_report_hdr(skb) : NULL;
441 /* EX and TO_EX get a fresh packet, if needed */
443 if (pih && pih->ngrec &&
444 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
446 igmpv3_sendpack(skb);
447 skb = igmpv3_newpack(dev, dev->mtu);
452 for (psf=*psf_list; psf; psf=psf_next) {
455 psf_next = psf->sf_next;
457 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
462 /* clear marks on query responses */
466 if (AVAILABLE(skb) < sizeof(__be32) +
467 first*sizeof(struct igmpv3_grec)) {
468 if (truncate && !first)
469 break; /* truncate these */
471 pgr->grec_nsrcs = htons(scount);
473 igmpv3_sendpack(skb);
474 skb = igmpv3_newpack(dev, dev->mtu);
479 skb = add_grhead(skb, pmc, type, &pgr);
484 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
485 *psrc = psf->sf_inaddr;
487 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
488 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
490 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
492 psf_prev->sf_next = psf->sf_next;
494 *psf_list = psf->sf_next;
504 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
505 type == IGMPV3_BLOCK_OLD_SOURCES)
507 if (pmc->crcount || isquery) {
508 /* make sure we have room for group header */
509 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
510 igmpv3_sendpack(skb);
511 skb = NULL; /* add_grhead will get a new one */
513 skb = add_grhead(skb, pmc, type, &pgr);
517 pgr->grec_nsrcs = htons(scount);
520 pmc->gsquery = 0; /* clear query state on report */
524 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
526 struct sk_buff *skb = NULL;
531 for_each_pmc_rcu(in_dev, pmc) {
532 if (pmc->multiaddr == IGMP_ALL_HOSTS)
534 spin_lock_bh(&pmc->lock);
535 if (pmc->sfcount[MCAST_EXCLUDE])
536 type = IGMPV3_MODE_IS_EXCLUDE;
538 type = IGMPV3_MODE_IS_INCLUDE;
539 skb = add_grec(skb, pmc, type, 0, 0);
540 spin_unlock_bh(&pmc->lock);
544 spin_lock_bh(&pmc->lock);
545 if (pmc->sfcount[MCAST_EXCLUDE])
546 type = IGMPV3_MODE_IS_EXCLUDE;
548 type = IGMPV3_MODE_IS_INCLUDE;
549 skb = add_grec(skb, pmc, type, 0, 0);
550 spin_unlock_bh(&pmc->lock);
554 return igmpv3_sendpack(skb);
558 * remove zero-count source records from a source filter list
560 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
562 struct ip_sf_list *psf_prev, *psf_next, *psf;
565 for (psf=*ppsf; psf; psf = psf_next) {
566 psf_next = psf->sf_next;
567 if (psf->sf_crcount == 0) {
569 psf_prev->sf_next = psf->sf_next;
571 *ppsf = psf->sf_next;
578 static void igmpv3_send_cr(struct in_device *in_dev)
580 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
581 struct sk_buff *skb = NULL;
585 spin_lock_bh(&in_dev->mc_tomb_lock);
589 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
590 pmc_next = pmc->next;
591 if (pmc->sfmode == MCAST_INCLUDE) {
592 type = IGMPV3_BLOCK_OLD_SOURCES;
593 dtype = IGMPV3_BLOCK_OLD_SOURCES;
594 skb = add_grec(skb, pmc, type, 1, 0);
595 skb = add_grec(skb, pmc, dtype, 1, 1);
598 if (pmc->sfmode == MCAST_EXCLUDE) {
599 type = IGMPV3_CHANGE_TO_INCLUDE;
600 skb = add_grec(skb, pmc, type, 1, 0);
603 if (pmc->crcount == 0) {
604 igmpv3_clear_zeros(&pmc->tomb);
605 igmpv3_clear_zeros(&pmc->sources);
608 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
610 pmc_prev->next = pmc_next;
612 in_dev->mc_tomb = pmc_next;
613 in_dev_put(pmc->interface);
618 spin_unlock_bh(&in_dev->mc_tomb_lock);
621 for_each_pmc_rcu(in_dev, pmc) {
622 spin_lock_bh(&pmc->lock);
623 if (pmc->sfcount[MCAST_EXCLUDE]) {
624 type = IGMPV3_BLOCK_OLD_SOURCES;
625 dtype = IGMPV3_ALLOW_NEW_SOURCES;
627 type = IGMPV3_ALLOW_NEW_SOURCES;
628 dtype = IGMPV3_BLOCK_OLD_SOURCES;
630 skb = add_grec(skb, pmc, type, 0, 0);
631 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
633 /* filter mode changes */
635 if (pmc->sfmode == MCAST_EXCLUDE)
636 type = IGMPV3_CHANGE_TO_EXCLUDE;
638 type = IGMPV3_CHANGE_TO_INCLUDE;
639 skb = add_grec(skb, pmc, type, 0, 0);
642 spin_unlock_bh(&pmc->lock);
648 (void) igmpv3_sendpack(skb);
651 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
658 struct net_device *dev = in_dev->dev;
659 struct net *net = dev_net(dev);
660 __be32 group = pmc ? pmc->multiaddr : 0;
663 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
664 return igmpv3_send_report(in_dev, pmc);
665 else if (type == IGMP_HOST_LEAVE_MESSAGE)
666 dst = IGMP_ALL_ROUTER;
671 struct flowi fl = { .oif = dev->ifindex,
673 .proto = IPPROTO_IGMP };
674 rt = ip_route_output_key(net, &fl);
678 if (rt->rt_src == 0) {
683 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
689 skb_dst_set(skb, &rt->dst);
691 skb_reserve(skb, LL_RESERVED_SPACE(dev));
693 skb_reset_network_header(skb);
695 skb_put(skb, sizeof(struct iphdr) + 4);
698 iph->ihl = (sizeof(struct iphdr)+4)>>2;
700 iph->frag_off = htons(IP_DF);
703 iph->saddr = rt->rt_src;
704 iph->protocol = IPPROTO_IGMP;
705 ip_select_ident(iph, &rt->dst, NULL);
706 ((u8*)&iph[1])[0] = IPOPT_RA;
707 ((u8*)&iph[1])[1] = 4;
708 ((u8*)&iph[1])[2] = 0;
709 ((u8*)&iph[1])[3] = 0;
711 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
716 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
718 return ip_local_out(skb);
721 static void igmp_gq_timer_expire(unsigned long data)
723 struct in_device *in_dev = (struct in_device *)data;
725 in_dev->mr_gq_running = 0;
726 igmpv3_send_report(in_dev, NULL);
727 __in_dev_put(in_dev);
730 static void igmp_ifc_timer_expire(unsigned long data)
732 struct in_device *in_dev = (struct in_device *)data;
734 igmpv3_send_cr(in_dev);
735 if (in_dev->mr_ifc_count) {
736 in_dev->mr_ifc_count--;
737 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
739 __in_dev_put(in_dev);
742 static void igmp_ifc_event(struct in_device *in_dev)
744 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
746 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
747 IGMP_Unsolicited_Report_Count;
748 igmp_ifc_start_timer(in_dev, 1);
752 static void igmp_timer_expire(unsigned long data)
754 struct ip_mc_list *im=(struct ip_mc_list *)data;
755 struct in_device *in_dev = im->interface;
757 spin_lock(&im->lock);
760 if (im->unsolicit_count) {
761 im->unsolicit_count--;
762 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
765 spin_unlock(&im->lock);
767 if (IGMP_V1_SEEN(in_dev))
768 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
769 else if (IGMP_V2_SEEN(in_dev))
770 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
772 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
777 /* mark EXCLUDE-mode sources */
778 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
780 struct ip_sf_list *psf;
784 for (psf=pmc->sources; psf; psf=psf->sf_next) {
787 for (i=0; i<nsrcs; i++) {
788 /* skip inactive filters */
789 if (pmc->sfcount[MCAST_INCLUDE] ||
790 pmc->sfcount[MCAST_EXCLUDE] !=
791 psf->sf_count[MCAST_EXCLUDE])
793 if (srcs[i] == psf->sf_inaddr) {
800 if (scount == nsrcs) /* all sources excluded */
805 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
807 struct ip_sf_list *psf;
810 if (pmc->sfmode == MCAST_EXCLUDE)
811 return igmp_xmarksources(pmc, nsrcs, srcs);
813 /* mark INCLUDE-mode sources */
815 for (psf=pmc->sources; psf; psf=psf->sf_next) {
818 for (i=0; i<nsrcs; i++)
819 if (srcs[i] == psf->sf_inaddr) {
833 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
835 struct ip_mc_list *im;
837 /* Timers are only set for non-local groups */
839 if (group == IGMP_ALL_HOSTS)
843 for_each_pmc_rcu(in_dev, im) {
844 if (im->multiaddr == group) {
852 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
855 struct igmphdr *ih = igmp_hdr(skb);
856 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
857 struct ip_mc_list *im;
858 __be32 group = ih->group;
865 /* Alas, old v1 router presents here. */
867 max_delay = IGMP_Query_Response_Interval;
868 in_dev->mr_v1_seen = jiffies +
869 IGMP_V1_Router_Present_Timeout;
872 /* v2 router present */
873 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
874 in_dev->mr_v2_seen = jiffies +
875 IGMP_V2_Router_Present_Timeout;
877 /* cancel the interface change timer */
878 in_dev->mr_ifc_count = 0;
879 if (del_timer(&in_dev->mr_ifc_timer))
880 __in_dev_put(in_dev);
881 /* clear deleted report items */
882 igmpv3_clear_delrec(in_dev);
883 } else if (len < 12) {
884 return; /* ignore bogus packet; freed by caller */
885 } else if (IGMP_V1_SEEN(in_dev)) {
886 /* This is a v3 query with v1 queriers present */
887 max_delay = IGMP_Query_Response_Interval;
889 } else if (IGMP_V2_SEEN(in_dev)) {
890 /* this is a v3 query with v2 queriers present;
891 * Interpretation of the max_delay code is problematic here.
892 * A real v2 host would use ih_code directly, while v3 has a
893 * different encoding. We use the v3 encoding as more likely
894 * to be intended in a v3 query.
896 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
898 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
901 ih3 = igmpv3_query_hdr(skb);
903 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
904 + ntohs(ih3->nsrcs)*sizeof(__be32)))
906 ih3 = igmpv3_query_hdr(skb);
909 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
911 max_delay = 1; /* can't mod w/ 0 */
912 in_dev->mr_maxdelay = max_delay;
914 in_dev->mr_qrv = ih3->qrv;
915 if (!group) { /* general query */
917 return; /* no sources allowed */
918 igmp_gq_start_timer(in_dev);
921 /* mark sources to include, if group & source-specific */
922 mark = ih3->nsrcs != 0;
926 * - Start the timers in all of our membership records
927 * that the query applies to for the interface on
928 * which the query arrived excl. those that belong
929 * to a "local" group (224.0.0.X)
930 * - For timers already running check if they need to
932 * - Use the igmp->igmp_code field as the maximum
936 for_each_pmc_rcu(in_dev, im) {
939 if (group && group != im->multiaddr)
941 if (im->multiaddr == IGMP_ALL_HOSTS)
943 spin_lock_bh(&im->lock);
945 im->gsquery = im->gsquery && mark;
948 changed = !im->gsquery ||
949 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
950 spin_unlock_bh(&im->lock);
952 igmp_mod_timer(im, max_delay);
957 /* called in rcu_read_lock() section */
958 int igmp_rcv(struct sk_buff *skb)
960 /* This basically follows the spec line by line -- see RFC1112 */
962 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
968 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
971 switch (skb->ip_summed) {
972 case CHECKSUM_COMPLETE:
973 if (!csum_fold(skb->csum))
978 if (__skb_checksum_complete(skb))
984 case IGMP_HOST_MEMBERSHIP_QUERY:
985 igmp_heard_query(in_dev, skb, len);
987 case IGMP_HOST_MEMBERSHIP_REPORT:
988 case IGMPV2_HOST_MEMBERSHIP_REPORT:
989 /* Is it our report looped back? */
990 if (rt_is_output_route(skb_rtable(skb)))
992 /* don't rely on MC router hearing unicast reports */
993 if (skb->pkt_type == PACKET_MULTICAST ||
994 skb->pkt_type == PACKET_BROADCAST)
995 igmp_heard_report(in_dev, ih->group);
998 #ifdef CONFIG_IP_PIMSM_V1
999 return pim_rcv_v1(skb);
1001 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1004 case IGMP_HOST_LEAVE_MESSAGE:
1006 case IGMP_MTRACE_RESP:
1021 * Add a filter to a device
1024 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1026 char buf[MAX_ADDR_LEN];
1027 struct net_device *dev = in_dev->dev;
1029 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1030 We will get multicast token leakage, when IFF_MULTICAST
1031 is changed. This check should be done in dev->set_multicast_list
1032 routine. Something sort of:
1033 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1036 if (arp_mc_map(addr, buf, dev, 0) == 0)
1037 dev_mc_add(dev, buf);
1041 * Remove a filter from a device
1044 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1046 char buf[MAX_ADDR_LEN];
1047 struct net_device *dev = in_dev->dev;
1049 if (arp_mc_map(addr, buf, dev, 0) == 0)
1050 dev_mc_del(dev, buf);
1053 #ifdef CONFIG_IP_MULTICAST
1055 * deleted ip_mc_list manipulation
1057 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1059 struct ip_mc_list *pmc;
1061 /* this is an "ip_mc_list" for convenience; only the fields below
1062 * are actually used. In particular, the refcnt and users are not
1063 * used for management of the delete list. Using the same structure
1064 * for deleted items allows change reports to use common code with
1065 * non-deleted or query-response MCA's.
1067 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1070 spin_lock_bh(&im->lock);
1071 pmc->interface = im->interface;
1072 in_dev_hold(in_dev);
1073 pmc->multiaddr = im->multiaddr;
1074 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1075 IGMP_Unsolicited_Report_Count;
1076 pmc->sfmode = im->sfmode;
1077 if (pmc->sfmode == MCAST_INCLUDE) {
1078 struct ip_sf_list *psf;
1080 pmc->tomb = im->tomb;
1081 pmc->sources = im->sources;
1082 im->tomb = im->sources = NULL;
1083 for (psf=pmc->sources; psf; psf=psf->sf_next)
1084 psf->sf_crcount = pmc->crcount;
1086 spin_unlock_bh(&im->lock);
1088 spin_lock_bh(&in_dev->mc_tomb_lock);
1089 pmc->next = in_dev->mc_tomb;
1090 in_dev->mc_tomb = pmc;
1091 spin_unlock_bh(&in_dev->mc_tomb_lock);
1094 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1096 struct ip_mc_list *pmc, *pmc_prev;
1097 struct ip_sf_list *psf, *psf_next;
1099 spin_lock_bh(&in_dev->mc_tomb_lock);
1101 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1102 if (pmc->multiaddr == multiaddr)
1108 pmc_prev->next = pmc->next;
1110 in_dev->mc_tomb = pmc->next;
1112 spin_unlock_bh(&in_dev->mc_tomb_lock);
1114 for (psf=pmc->tomb; psf; psf=psf_next) {
1115 psf_next = psf->sf_next;
1118 in_dev_put(pmc->interface);
1123 static void igmpv3_clear_delrec(struct in_device *in_dev)
1125 struct ip_mc_list *pmc, *nextpmc;
1127 spin_lock_bh(&in_dev->mc_tomb_lock);
1128 pmc = in_dev->mc_tomb;
1129 in_dev->mc_tomb = NULL;
1130 spin_unlock_bh(&in_dev->mc_tomb_lock);
1132 for (; pmc; pmc = nextpmc) {
1133 nextpmc = pmc->next;
1134 ip_mc_clear_src(pmc);
1135 in_dev_put(pmc->interface);
1138 /* clear dead sources, too */
1140 for_each_pmc_rcu(in_dev, pmc) {
1141 struct ip_sf_list *psf, *psf_next;
1143 spin_lock_bh(&pmc->lock);
1146 spin_unlock_bh(&pmc->lock);
1147 for (; psf; psf=psf_next) {
1148 psf_next = psf->sf_next;
1156 static void igmp_group_dropped(struct ip_mc_list *im)
1158 struct in_device *in_dev = im->interface;
1159 #ifdef CONFIG_IP_MULTICAST
1165 ip_mc_filter_del(in_dev, im->multiaddr);
1168 #ifdef CONFIG_IP_MULTICAST
1169 if (im->multiaddr == IGMP_ALL_HOSTS)
1172 reporter = im->reporter;
1173 igmp_stop_timer(im);
1175 if (!in_dev->dead) {
1176 if (IGMP_V1_SEEN(in_dev))
1178 if (IGMP_V2_SEEN(in_dev)) {
1180 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1184 igmpv3_add_delrec(in_dev, im);
1186 igmp_ifc_event(in_dev);
1190 ip_mc_clear_src(im);
1193 static void igmp_group_added(struct ip_mc_list *im)
1195 struct in_device *in_dev = im->interface;
1197 if (im->loaded == 0) {
1199 ip_mc_filter_add(in_dev, im->multiaddr);
1202 #ifdef CONFIG_IP_MULTICAST
1203 if (im->multiaddr == IGMP_ALL_HOSTS)
1208 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1209 spin_lock_bh(&im->lock);
1210 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1211 spin_unlock_bh(&im->lock);
1216 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1217 IGMP_Unsolicited_Report_Count;
1218 igmp_ifc_event(in_dev);
1224 * Multicast list managers
1229 * A socket has joined a multicast group on device dev.
1232 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1234 struct ip_mc_list *im;
1238 for_each_pmc_rtnl(in_dev, im) {
1239 if (im->multiaddr == addr) {
1241 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1246 im = kzalloc(sizeof(*im), GFP_KERNEL);
1251 im->interface = in_dev;
1252 in_dev_hold(in_dev);
1253 im->multiaddr = addr;
1254 /* initial mode is (EX, empty) */
1255 im->sfmode = MCAST_EXCLUDE;
1256 im->sfcount[MCAST_EXCLUDE] = 1;
1257 atomic_set(&im->refcnt, 1);
1258 spin_lock_init(&im->lock);
1259 #ifdef CONFIG_IP_MULTICAST
1260 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1261 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1264 im->next_rcu = in_dev->mc_list;
1266 rcu_assign_pointer(in_dev->mc_list, im);
1268 #ifdef CONFIG_IP_MULTICAST
1269 igmpv3_del_delrec(in_dev, im->multiaddr);
1271 igmp_group_added(im);
1273 ip_rt_multicast_event(in_dev);
1277 EXPORT_SYMBOL(ip_mc_inc_group);
1280 * Resend IGMP JOIN report; used for bonding.
1281 * Called with rcu_read_lock()
1283 void ip_mc_rejoin_groups(struct in_device *in_dev)
1285 #ifdef CONFIG_IP_MULTICAST
1286 struct ip_mc_list *im;
1289 for_each_pmc_rcu(in_dev, im) {
1290 if (im->multiaddr == IGMP_ALL_HOSTS)
1293 /* a failover is happening and switches
1294 * must be notified immediately
1296 if (IGMP_V1_SEEN(in_dev))
1297 type = IGMP_HOST_MEMBERSHIP_REPORT;
1298 else if (IGMP_V2_SEEN(in_dev))
1299 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1301 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1302 igmp_send_report(in_dev, im, type);
1306 EXPORT_SYMBOL(ip_mc_rejoin_groups);
1309 * A socket has left a multicast group on device dev
1312 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1314 struct ip_mc_list *i;
1315 struct ip_mc_list __rcu **ip;
1319 for (ip = &in_dev->mc_list;
1320 (i = rtnl_dereference(*ip)) != NULL;
1321 ip = &i->next_rcu) {
1322 if (i->multiaddr == addr) {
1323 if (--i->users == 0) {
1326 igmp_group_dropped(i);
1329 ip_rt_multicast_event(in_dev);
1338 EXPORT_SYMBOL(ip_mc_dec_group);
1340 /* Device changing type */
1342 void ip_mc_unmap(struct in_device *in_dev)
1344 struct ip_mc_list *pmc;
1348 for_each_pmc_rtnl(in_dev, pmc)
1349 igmp_group_dropped(pmc);
1352 void ip_mc_remap(struct in_device *in_dev)
1354 struct ip_mc_list *pmc;
1358 for_each_pmc_rtnl(in_dev, pmc)
1359 igmp_group_added(pmc);
1362 /* Device going down */
1364 void ip_mc_down(struct in_device *in_dev)
1366 struct ip_mc_list *pmc;
1370 for_each_pmc_rtnl(in_dev, pmc)
1371 igmp_group_dropped(pmc);
1373 #ifdef CONFIG_IP_MULTICAST
1374 in_dev->mr_ifc_count = 0;
1375 if (del_timer(&in_dev->mr_ifc_timer))
1376 __in_dev_put(in_dev);
1377 in_dev->mr_gq_running = 0;
1378 if (del_timer(&in_dev->mr_gq_timer))
1379 __in_dev_put(in_dev);
1380 igmpv3_clear_delrec(in_dev);
1383 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1386 void ip_mc_init_dev(struct in_device *in_dev)
1390 in_dev->mc_tomb = NULL;
1391 #ifdef CONFIG_IP_MULTICAST
1392 in_dev->mr_gq_running = 0;
1393 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1394 (unsigned long)in_dev);
1395 in_dev->mr_ifc_count = 0;
1396 in_dev->mc_count = 0;
1397 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1398 (unsigned long)in_dev);
1399 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1402 spin_lock_init(&in_dev->mc_tomb_lock);
1405 /* Device going up */
1407 void ip_mc_up(struct in_device *in_dev)
1409 struct ip_mc_list *pmc;
1413 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1415 for_each_pmc_rtnl(in_dev, pmc)
1416 igmp_group_added(pmc);
1420 * Device is about to be destroyed: clean up.
1423 void ip_mc_destroy_dev(struct in_device *in_dev)
1425 struct ip_mc_list *i;
1429 /* Deactivate timers */
1432 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1433 in_dev->mc_list = i->next_rcu;
1436 igmp_group_dropped(i);
1441 /* RTNL is locked */
1442 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1444 struct flowi fl = { .fl4_dst = imr->imr_multiaddr.s_addr };
1445 struct net_device *dev = NULL;
1446 struct in_device *idev = NULL;
1448 if (imr->imr_ifindex) {
1449 idev = inetdev_by_index(net, imr->imr_ifindex);
1452 if (imr->imr_address.s_addr) {
1453 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1459 struct rtable *rt = ip_route_output_key(net, &fl);
1466 imr->imr_ifindex = dev->ifindex;
1467 idev = __in_dev_get_rtnl(dev);
1473 * Join a socket to a group
1475 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1476 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1479 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1482 struct ip_sf_list *psf, *psf_prev;
1486 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1487 if (psf->sf_inaddr == *psfsrc)
1491 if (!psf || psf->sf_count[sfmode] == 0) {
1492 /* source filter not found, or count wrong => bug */
1495 psf->sf_count[sfmode]--;
1496 if (psf->sf_count[sfmode] == 0) {
1497 ip_rt_multicast_event(pmc->interface);
1499 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1500 #ifdef CONFIG_IP_MULTICAST
1501 struct in_device *in_dev = pmc->interface;
1504 /* no more filters for this source */
1506 psf_prev->sf_next = psf->sf_next;
1508 pmc->sources = psf->sf_next;
1509 #ifdef CONFIG_IP_MULTICAST
1510 if (psf->sf_oldin &&
1511 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1512 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1513 IGMP_Unsolicited_Report_Count;
1514 psf->sf_next = pmc->tomb;
1524 #ifndef CONFIG_IP_MULTICAST
1525 #define igmp_ifc_event(x) do { } while (0)
1528 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1529 int sfcount, __be32 *psfsrc, int delta)
1531 struct ip_mc_list *pmc;
1538 for_each_pmc_rcu(in_dev, pmc) {
1539 if (*pmca == pmc->multiaddr)
1543 /* MCA not found?? bug */
1547 spin_lock_bh(&pmc->lock);
1549 #ifdef CONFIG_IP_MULTICAST
1554 if (!pmc->sfcount[sfmode])
1556 pmc->sfcount[sfmode]--;
1559 for (i=0; i<sfcount; i++) {
1560 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1562 changerec |= rv > 0;
1566 if (pmc->sfmode == MCAST_EXCLUDE &&
1567 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1568 pmc->sfcount[MCAST_INCLUDE]) {
1569 #ifdef CONFIG_IP_MULTICAST
1570 struct ip_sf_list *psf;
1573 /* filter mode change */
1574 pmc->sfmode = MCAST_INCLUDE;
1575 #ifdef CONFIG_IP_MULTICAST
1576 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1577 IGMP_Unsolicited_Report_Count;
1578 in_dev->mr_ifc_count = pmc->crcount;
1579 for (psf=pmc->sources; psf; psf = psf->sf_next)
1580 psf->sf_crcount = 0;
1581 igmp_ifc_event(pmc->interface);
1582 } else if (sf_setstate(pmc) || changerec) {
1583 igmp_ifc_event(pmc->interface);
1587 spin_unlock_bh(&pmc->lock);
1592 * Add multicast single-source filter to the interface list
1594 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1595 __be32 *psfsrc, int delta)
1597 struct ip_sf_list *psf, *psf_prev;
1600 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1601 if (psf->sf_inaddr == *psfsrc)
1606 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1609 psf->sf_inaddr = *psfsrc;
1611 psf_prev->sf_next = psf;
1615 psf->sf_count[sfmode]++;
1616 if (psf->sf_count[sfmode] == 1) {
1617 ip_rt_multicast_event(pmc->interface);
1622 #ifdef CONFIG_IP_MULTICAST
1623 static void sf_markstate(struct ip_mc_list *pmc)
1625 struct ip_sf_list *psf;
1626 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1628 for (psf=pmc->sources; psf; psf=psf->sf_next)
1629 if (pmc->sfcount[MCAST_EXCLUDE]) {
1630 psf->sf_oldin = mca_xcount ==
1631 psf->sf_count[MCAST_EXCLUDE] &&
1632 !psf->sf_count[MCAST_INCLUDE];
1634 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1637 static int sf_setstate(struct ip_mc_list *pmc)
1639 struct ip_sf_list *psf, *dpsf;
1640 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1641 int qrv = pmc->interface->mr_qrv;
1645 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1646 if (pmc->sfcount[MCAST_EXCLUDE]) {
1647 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1648 !psf->sf_count[MCAST_INCLUDE];
1650 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1652 if (!psf->sf_oldin) {
1653 struct ip_sf_list *prev = NULL;
1655 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1656 if (dpsf->sf_inaddr == psf->sf_inaddr)
1662 prev->sf_next = dpsf->sf_next;
1664 pmc->tomb = dpsf->sf_next;
1667 psf->sf_crcount = qrv;
1670 } else if (psf->sf_oldin) {
1672 psf->sf_crcount = 0;
1674 * add or update "delete" records if an active filter
1677 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1678 if (dpsf->sf_inaddr == psf->sf_inaddr)
1681 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1685 /* pmc->lock held by callers */
1686 dpsf->sf_next = pmc->tomb;
1689 dpsf->sf_crcount = qrv;
1698 * Add multicast source filter list to the interface list
1700 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1701 int sfcount, __be32 *psfsrc, int delta)
1703 struct ip_mc_list *pmc;
1710 for_each_pmc_rcu(in_dev, pmc) {
1711 if (*pmca == pmc->multiaddr)
1715 /* MCA not found?? bug */
1719 spin_lock_bh(&pmc->lock);
1722 #ifdef CONFIG_IP_MULTICAST
1725 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1727 pmc->sfcount[sfmode]++;
1729 for (i=0; i<sfcount; i++) {
1730 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1737 pmc->sfcount[sfmode]--;
1739 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1740 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1741 #ifdef CONFIG_IP_MULTICAST
1742 struct ip_sf_list *psf;
1743 in_dev = pmc->interface;
1746 /* filter mode change */
1747 if (pmc->sfcount[MCAST_EXCLUDE])
1748 pmc->sfmode = MCAST_EXCLUDE;
1749 else if (pmc->sfcount[MCAST_INCLUDE])
1750 pmc->sfmode = MCAST_INCLUDE;
1751 #ifdef CONFIG_IP_MULTICAST
1752 /* else no filters; keep old mode for reports */
1754 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1755 IGMP_Unsolicited_Report_Count;
1756 in_dev->mr_ifc_count = pmc->crcount;
1757 for (psf=pmc->sources; psf; psf = psf->sf_next)
1758 psf->sf_crcount = 0;
1759 igmp_ifc_event(in_dev);
1760 } else if (sf_setstate(pmc)) {
1761 igmp_ifc_event(in_dev);
1764 spin_unlock_bh(&pmc->lock);
1768 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1770 struct ip_sf_list *psf, *nextpsf;
1772 for (psf=pmc->tomb; psf; psf=nextpsf) {
1773 nextpsf = psf->sf_next;
1777 for (psf=pmc->sources; psf; psf=nextpsf) {
1778 nextpsf = psf->sf_next;
1781 pmc->sources = NULL;
1782 pmc->sfmode = MCAST_EXCLUDE;
1783 pmc->sfcount[MCAST_INCLUDE] = 0;
1784 pmc->sfcount[MCAST_EXCLUDE] = 1;
1789 * Join a multicast group
1791 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1794 __be32 addr = imr->imr_multiaddr.s_addr;
1795 struct ip_mc_socklist *iml = NULL, *i;
1796 struct in_device *in_dev;
1797 struct inet_sock *inet = inet_sk(sk);
1798 struct net *net = sock_net(sk);
1802 if (!ipv4_is_multicast(addr))
1807 in_dev = ip_mc_find_dev(net, imr);
1816 ifindex = imr->imr_ifindex;
1817 for_each_pmc_rtnl(inet, i) {
1818 if (i->multi.imr_multiaddr.s_addr == addr &&
1819 i->multi.imr_ifindex == ifindex)
1824 if (count >= sysctl_igmp_max_memberships)
1826 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1830 memcpy(&iml->multi, imr, sizeof(*imr));
1831 iml->next_rcu = inet->mc_list;
1833 iml->sfmode = MCAST_EXCLUDE;
1834 rcu_assign_pointer(inet->mc_list, iml);
1835 ip_mc_inc_group(in_dev, addr);
1841 EXPORT_SYMBOL(ip_mc_join_group);
1843 static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1845 kfree(container_of(rp, struct ip_sf_socklist, rcu));
1846 /* sk_omem_alloc should have been decreased by the caller*/
1849 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1850 struct in_device *in_dev)
1852 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1856 /* any-source empty exclude case */
1857 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1858 iml->sfmode, 0, NULL, 0);
1860 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1861 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1862 rcu_assign_pointer(iml->sflist, NULL);
1863 /* decrease mem now to avoid the memleak warning */
1864 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1865 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
1870 static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1872 kfree(container_of(rp, struct ip_mc_socklist, rcu));
1873 /* sk_omem_alloc should have been decreased by the caller*/
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 ifindex = imr->imr_ifindex;
1895 for (imlp = &inet->mc_list;
1896 (iml = rtnl_dereference(*imlp)) != NULL;
1897 imlp = &iml->next_rcu) {
1898 if (iml->multi.imr_multiaddr.s_addr != group)
1901 if (iml->multi.imr_ifindex != ifindex)
1903 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1904 iml->multi.imr_address.s_addr)
1907 (void) ip_mc_leave_src(sk, iml, in_dev);
1909 *imlp = iml->next_rcu;
1912 ip_mc_dec_group(in_dev, group);
1914 /* decrease mem now to avoid the memleak warning */
1915 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1916 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
1925 int ip_mc_source(int add, int omode, struct sock *sk, struct
1926 ip_mreq_source *mreqs, int ifindex)
1929 struct ip_mreqn imr;
1930 __be32 addr = mreqs->imr_multiaddr;
1931 struct ip_mc_socklist *pmc;
1932 struct in_device *in_dev = NULL;
1933 struct inet_sock *inet = inet_sk(sk);
1934 struct ip_sf_socklist *psl;
1935 struct net *net = sock_net(sk);
1939 if (!ipv4_is_multicast(addr))
1944 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1945 imr.imr_address.s_addr = mreqs->imr_interface;
1946 imr.imr_ifindex = ifindex;
1947 in_dev = ip_mc_find_dev(net, &imr);
1953 err = -EADDRNOTAVAIL;
1955 for_each_pmc_rtnl(inet, pmc) {
1956 if ((pmc->multi.imr_multiaddr.s_addr ==
1957 imr.imr_multiaddr.s_addr) &&
1958 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1961 if (!pmc) { /* must have a prior join */
1965 /* if a source filter was set, must be the same mode as before */
1967 if (pmc->sfmode != omode) {
1971 } else if (pmc->sfmode != omode) {
1972 /* allow mode switches for empty-set filters */
1973 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1974 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1976 pmc->sfmode = omode;
1979 psl = rtnl_dereference(pmc->sflist);
1982 goto done; /* err = -EADDRNOTAVAIL */
1984 for (i=0; i<psl->sl_count; i++) {
1985 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1990 if (rv) /* source not found */
1991 goto done; /* err = -EADDRNOTAVAIL */
1993 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1994 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1999 /* update the interface filter */
2000 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2001 &mreqs->imr_sourceaddr, 1);
2003 for (j=i+1; j<psl->sl_count; j++)
2004 psl->sl_addr[j-1] = psl->sl_addr[j];
2009 /* else, add a new source to the filter */
2011 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2015 if (!psl || psl->sl_count == psl->sl_max) {
2016 struct ip_sf_socklist *newpsl;
2017 int count = IP_SFBLOCK;
2020 count += psl->sl_max;
2021 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2026 newpsl->sl_max = count;
2027 newpsl->sl_count = count - IP_SFBLOCK;
2029 for (i=0; i<psl->sl_count; i++)
2030 newpsl->sl_addr[i] = psl->sl_addr[i];
2031 /* decrease mem now to avoid the memleak warning */
2032 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2033 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2035 rcu_assign_pointer(pmc->sflist, newpsl);
2038 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2039 for (i=0; i<psl->sl_count; i++) {
2040 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2045 if (rv == 0) /* address already there is an error */
2047 for (j=psl->sl_count-1; j>=i; j--)
2048 psl->sl_addr[j+1] = psl->sl_addr[j];
2049 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2052 /* update the interface list */
2053 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2054 &mreqs->imr_sourceaddr, 1);
2058 return ip_mc_leave_group(sk, &imr);
2062 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2065 struct ip_mreqn imr;
2066 __be32 addr = msf->imsf_multiaddr;
2067 struct ip_mc_socklist *pmc;
2068 struct in_device *in_dev;
2069 struct inet_sock *inet = inet_sk(sk);
2070 struct ip_sf_socklist *newpsl, *psl;
2071 struct net *net = sock_net(sk);
2074 if (!ipv4_is_multicast(addr))
2076 if (msf->imsf_fmode != MCAST_INCLUDE &&
2077 msf->imsf_fmode != MCAST_EXCLUDE)
2082 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2083 imr.imr_address.s_addr = msf->imsf_interface;
2084 imr.imr_ifindex = ifindex;
2085 in_dev = ip_mc_find_dev(net, &imr);
2092 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2093 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2098 for_each_pmc_rtnl(inet, pmc) {
2099 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2100 pmc->multi.imr_ifindex == imr.imr_ifindex)
2103 if (!pmc) { /* must have a prior join */
2107 if (msf->imsf_numsrc) {
2108 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2114 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2115 memcpy(newpsl->sl_addr, msf->imsf_slist,
2116 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2117 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2118 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2120 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2125 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2126 msf->imsf_fmode, 0, NULL, 0);
2128 psl = rtnl_dereference(pmc->sflist);
2130 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2131 psl->sl_count, psl->sl_addr, 0);
2132 /* decrease mem now to avoid the memleak warning */
2133 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2134 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2136 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2138 rcu_assign_pointer(pmc->sflist, newpsl);
2139 pmc->sfmode = msf->imsf_fmode;
2144 err = ip_mc_leave_group(sk, &imr);
2148 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2149 struct ip_msfilter __user *optval, int __user *optlen)
2151 int err, len, count, copycount;
2152 struct ip_mreqn imr;
2153 __be32 addr = msf->imsf_multiaddr;
2154 struct ip_mc_socklist *pmc;
2155 struct in_device *in_dev;
2156 struct inet_sock *inet = inet_sk(sk);
2157 struct ip_sf_socklist *psl;
2158 struct net *net = sock_net(sk);
2160 if (!ipv4_is_multicast(addr))
2165 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2166 imr.imr_address.s_addr = msf->imsf_interface;
2167 imr.imr_ifindex = 0;
2168 in_dev = ip_mc_find_dev(net, &imr);
2174 err = -EADDRNOTAVAIL;
2176 for_each_pmc_rtnl(inet, pmc) {
2177 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2178 pmc->multi.imr_ifindex == imr.imr_ifindex)
2181 if (!pmc) /* must have a prior join */
2183 msf->imsf_fmode = pmc->sfmode;
2184 psl = rtnl_dereference(pmc->sflist);
2190 count = psl->sl_count;
2192 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2193 len = copycount * sizeof(psl->sl_addr[0]);
2194 msf->imsf_numsrc = count;
2195 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2196 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2200 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2208 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2209 struct group_filter __user *optval, int __user *optlen)
2211 int err, i, count, copycount;
2212 struct sockaddr_in *psin;
2214 struct ip_mc_socklist *pmc;
2215 struct inet_sock *inet = inet_sk(sk);
2216 struct ip_sf_socklist *psl;
2218 psin = (struct sockaddr_in *)&gsf->gf_group;
2219 if (psin->sin_family != AF_INET)
2221 addr = psin->sin_addr.s_addr;
2222 if (!ipv4_is_multicast(addr))
2227 err = -EADDRNOTAVAIL;
2229 for_each_pmc_rtnl(inet, pmc) {
2230 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2231 pmc->multi.imr_ifindex == gsf->gf_interface)
2234 if (!pmc) /* must have a prior join */
2236 gsf->gf_fmode = pmc->sfmode;
2237 psl = rtnl_dereference(pmc->sflist);
2239 count = psl ? psl->sl_count : 0;
2240 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2241 gsf->gf_numsrc = count;
2242 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2243 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2246 for (i=0; i<copycount; i++) {
2247 struct sockaddr_storage ss;
2249 psin = (struct sockaddr_in *)&ss;
2250 memset(&ss, 0, sizeof(ss));
2251 psin->sin_family = AF_INET;
2252 psin->sin_addr.s_addr = psl->sl_addr[i];
2253 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2263 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2265 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2267 struct inet_sock *inet = inet_sk(sk);
2268 struct ip_mc_socklist *pmc;
2269 struct ip_sf_socklist *psl;
2274 if (!ipv4_is_multicast(loc_addr))
2278 for_each_pmc_rcu(inet, pmc) {
2279 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2280 pmc->multi.imr_ifindex == dif)
2286 psl = rcu_dereference(pmc->sflist);
2287 ret = (pmc->sfmode == MCAST_EXCLUDE);
2291 for (i=0; i<psl->sl_count; i++) {
2292 if (psl->sl_addr[i] == rmt_addr)
2296 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2298 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2308 * A socket is closing.
2311 void ip_mc_drop_socket(struct sock *sk)
2313 struct inet_sock *inet = inet_sk(sk);
2314 struct ip_mc_socklist *iml;
2315 struct net *net = sock_net(sk);
2317 if (inet->mc_list == NULL)
2321 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2322 struct in_device *in_dev;
2324 inet->mc_list = iml->next_rcu;
2325 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2326 (void) ip_mc_leave_src(sk, iml, in_dev);
2328 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2329 /* decrease mem now to avoid the memleak warning */
2330 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2331 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
2336 /* called with rcu_read_lock() */
2337 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2339 struct ip_mc_list *im;
2340 struct ip_sf_list *psf;
2343 for_each_pmc_rcu(in_dev, im) {
2344 if (im->multiaddr == mc_addr)
2347 if (im && proto == IPPROTO_IGMP) {
2351 for (psf=im->sources; psf; psf=psf->sf_next) {
2352 if (psf->sf_inaddr == src_addr)
2356 rv = psf->sf_count[MCAST_INCLUDE] ||
2357 psf->sf_count[MCAST_EXCLUDE] !=
2358 im->sfcount[MCAST_EXCLUDE];
2360 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2362 rv = 1; /* unspecified source; tentatively allow */
2367 #if defined(CONFIG_PROC_FS)
2368 struct igmp_mc_iter_state {
2369 struct seq_net_private p;
2370 struct net_device *dev;
2371 struct in_device *in_dev;
2374 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2376 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2378 struct net *net = seq_file_net(seq);
2379 struct ip_mc_list *im = NULL;
2380 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2382 state->in_dev = NULL;
2383 for_each_netdev_rcu(net, state->dev) {
2384 struct in_device *in_dev;
2386 in_dev = __in_dev_get_rcu(state->dev);
2389 im = rcu_dereference(in_dev->mc_list);
2391 state->in_dev = in_dev;
2398 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2400 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2402 im = rcu_dereference(im->next_rcu);
2404 state->dev = next_net_device_rcu(state->dev);
2406 state->in_dev = NULL;
2409 state->in_dev = __in_dev_get_rcu(state->dev);
2412 im = rcu_dereference(state->in_dev->mc_list);
2417 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2419 struct ip_mc_list *im = igmp_mc_get_first(seq);
2421 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2423 return pos ? NULL : im;
2426 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2430 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2433 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2435 struct ip_mc_list *im;
2436 if (v == SEQ_START_TOKEN)
2437 im = igmp_mc_get_first(seq);
2439 im = igmp_mc_get_next(seq, v);
2444 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2447 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2449 state->in_dev = NULL;
2454 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2456 if (v == SEQ_START_TOKEN)
2458 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2460 struct ip_mc_list *im = (struct ip_mc_list *)v;
2461 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2463 #ifdef CONFIG_IP_MULTICAST
2464 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2465 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2471 if (rcu_dereference(state->in_dev->mc_list) == im) {
2472 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2473 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2477 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2478 im->multiaddr, im->users,
2479 im->tm_running, im->tm_running ?
2480 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2486 static const struct seq_operations igmp_mc_seq_ops = {
2487 .start = igmp_mc_seq_start,
2488 .next = igmp_mc_seq_next,
2489 .stop = igmp_mc_seq_stop,
2490 .show = igmp_mc_seq_show,
2493 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2495 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2496 sizeof(struct igmp_mc_iter_state));
2499 static const struct file_operations igmp_mc_seq_fops = {
2500 .owner = THIS_MODULE,
2501 .open = igmp_mc_seq_open,
2503 .llseek = seq_lseek,
2504 .release = seq_release_net,
2507 struct igmp_mcf_iter_state {
2508 struct seq_net_private p;
2509 struct net_device *dev;
2510 struct in_device *idev;
2511 struct ip_mc_list *im;
2514 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2516 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2518 struct net *net = seq_file_net(seq);
2519 struct ip_sf_list *psf = NULL;
2520 struct ip_mc_list *im = NULL;
2521 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2525 for_each_netdev_rcu(net, state->dev) {
2526 struct in_device *idev;
2527 idev = __in_dev_get_rcu(state->dev);
2528 if (unlikely(idev == NULL))
2530 im = rcu_dereference(idev->mc_list);
2531 if (likely(im != NULL)) {
2532 spin_lock_bh(&im->lock);
2534 if (likely(psf != NULL)) {
2539 spin_unlock_bh(&im->lock);
2545 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2547 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2551 spin_unlock_bh(&state->im->lock);
2552 state->im = state->im->next;
2553 while (!state->im) {
2554 state->dev = next_net_device_rcu(state->dev);
2559 state->idev = __in_dev_get_rcu(state->dev);
2562 state->im = rcu_dereference(state->idev->mc_list);
2566 spin_lock_bh(&state->im->lock);
2567 psf = state->im->sources;
2573 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2575 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2577 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2579 return pos ? NULL : psf;
2582 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2586 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2589 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2591 struct ip_sf_list *psf;
2592 if (v == SEQ_START_TOKEN)
2593 psf = igmp_mcf_get_first(seq);
2595 psf = igmp_mcf_get_next(seq, v);
2600 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2603 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2604 if (likely(state->im != NULL)) {
2605 spin_unlock_bh(&state->im->lock);
2613 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2615 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2616 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2618 if (v == SEQ_START_TOKEN) {
2621 "%10s %10s %6s %6s\n", "Idx",
2623 "SRC", "INC", "EXC");
2627 "0x%08x %6lu %6lu\n",
2628 state->dev->ifindex, state->dev->name,
2629 ntohl(state->im->multiaddr),
2630 ntohl(psf->sf_inaddr),
2631 psf->sf_count[MCAST_INCLUDE],
2632 psf->sf_count[MCAST_EXCLUDE]);
2637 static const struct seq_operations igmp_mcf_seq_ops = {
2638 .start = igmp_mcf_seq_start,
2639 .next = igmp_mcf_seq_next,
2640 .stop = igmp_mcf_seq_stop,
2641 .show = igmp_mcf_seq_show,
2644 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2646 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2647 sizeof(struct igmp_mcf_iter_state));
2650 static const struct file_operations igmp_mcf_seq_fops = {
2651 .owner = THIS_MODULE,
2652 .open = igmp_mcf_seq_open,
2654 .llseek = seq_lseek,
2655 .release = seq_release_net,
2658 static int __net_init igmp_net_init(struct net *net)
2660 struct proc_dir_entry *pde;
2662 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2665 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2671 proc_net_remove(net, "igmp");
2676 static void __net_exit igmp_net_exit(struct net *net)
2678 proc_net_remove(net, "mcfilter");
2679 proc_net_remove(net, "igmp");
2682 static struct pernet_operations igmp_net_ops = {
2683 .init = igmp_net_init,
2684 .exit = igmp_net_exit,
2687 int __init igmp_mc_proc_init(void)
2689 return register_pernet_subsys(&igmp_net_ops);