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 #ifdef CONFIG_IP_MULTICAST
172 static void igmp_stop_timer(struct ip_mc_list *im)
174 spin_lock_bh(&im->lock);
175 if (del_timer(&im->timer))
176 atomic_dec(&im->refcnt);
179 im->unsolicit_count = 0;
180 spin_unlock_bh(&im->lock);
183 /* It must be called with locked im->lock */
184 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
186 int tv = net_random() % max_delay;
189 if (!mod_timer(&im->timer, jiffies+tv+2))
190 atomic_inc(&im->refcnt);
193 static void igmp_gq_start_timer(struct in_device *in_dev)
195 int tv = net_random() % in_dev->mr_maxdelay;
197 in_dev->mr_gq_running = 1;
198 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
202 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
204 int tv = net_random() % delay;
206 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
210 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
212 spin_lock_bh(&im->lock);
213 im->unsolicit_count = 0;
214 if (del_timer(&im->timer)) {
215 if ((long)(im->timer.expires-jiffies) < max_delay) {
216 add_timer(&im->timer);
218 spin_unlock_bh(&im->lock);
221 atomic_dec(&im->refcnt);
223 igmp_start_timer(im, max_delay);
224 spin_unlock_bh(&im->lock);
229 * Send an IGMP report.
232 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
235 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
236 int gdeleted, int sdeleted)
239 case IGMPV3_MODE_IS_INCLUDE:
240 case IGMPV3_MODE_IS_EXCLUDE:
241 if (gdeleted || sdeleted)
243 if (!(pmc->gsquery && !psf->sf_gsresp)) {
244 if (pmc->sfmode == MCAST_INCLUDE)
246 /* don't include if this source is excluded
249 if (psf->sf_count[MCAST_INCLUDE])
250 return type == IGMPV3_MODE_IS_INCLUDE;
251 return pmc->sfcount[MCAST_EXCLUDE] ==
252 psf->sf_count[MCAST_EXCLUDE];
255 case IGMPV3_CHANGE_TO_INCLUDE:
256 if (gdeleted || sdeleted)
258 return psf->sf_count[MCAST_INCLUDE] != 0;
259 case IGMPV3_CHANGE_TO_EXCLUDE:
260 if (gdeleted || sdeleted)
262 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
263 psf->sf_count[MCAST_INCLUDE])
265 return pmc->sfcount[MCAST_EXCLUDE] ==
266 psf->sf_count[MCAST_EXCLUDE];
267 case IGMPV3_ALLOW_NEW_SOURCES:
268 if (gdeleted || !psf->sf_crcount)
270 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
271 case IGMPV3_BLOCK_OLD_SOURCES:
272 if (pmc->sfmode == MCAST_INCLUDE)
273 return gdeleted || (psf->sf_crcount && sdeleted);
274 return psf->sf_crcount && !gdeleted && !sdeleted;
280 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
282 struct ip_sf_list *psf;
285 for (psf=pmc->sources; psf; psf=psf->sf_next) {
286 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
293 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
298 struct igmpv3_report *pig;
299 struct net *net = dev_net(dev);
301 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
306 struct flowi fl = { .oif = dev->ifindex,
308 .daddr = IGMPV3_ALL_MCR } },
309 .proto = IPPROTO_IGMP };
310 if (ip_route_output_key(net, &rt, &fl)) {
315 if (rt->rt_src == 0) {
321 skb_dst_set(skb, &rt->dst);
324 skb_reserve(skb, LL_RESERVED_SPACE(dev));
326 skb_reset_network_header(skb);
328 skb_put(skb, sizeof(struct iphdr) + 4);
331 pip->ihl = (sizeof(struct iphdr)+4)>>2;
333 pip->frag_off = htons(IP_DF);
335 pip->daddr = rt->rt_dst;
336 pip->saddr = rt->rt_src;
337 pip->protocol = IPPROTO_IGMP;
338 pip->tot_len = 0; /* filled in later */
339 ip_select_ident(pip, &rt->dst, NULL);
340 ((u8*)&pip[1])[0] = IPOPT_RA;
341 ((u8*)&pip[1])[1] = 4;
342 ((u8*)&pip[1])[2] = 0;
343 ((u8*)&pip[1])[3] = 0;
345 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
346 skb_put(skb, sizeof(*pig));
347 pig = igmpv3_report_hdr(skb);
348 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
356 static int igmpv3_sendpack(struct sk_buff *skb)
358 struct igmphdr *pig = igmp_hdr(skb);
359 const int igmplen = skb->tail - skb->transport_header;
361 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
363 return ip_local_out(skb);
366 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
368 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
371 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
372 int type, struct igmpv3_grec **ppgr)
374 struct net_device *dev = pmc->interface->dev;
375 struct igmpv3_report *pih;
376 struct igmpv3_grec *pgr;
379 skb = igmpv3_newpack(dev, dev->mtu);
382 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
383 pgr->grec_type = type;
384 pgr->grec_auxwords = 0;
386 pgr->grec_mca = pmc->multiaddr;
387 pih = igmpv3_report_hdr(skb);
388 pih->ngrec = htons(ntohs(pih->ngrec)+1);
393 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
394 skb_tailroom(skb)) : 0)
396 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
397 int type, int gdeleted, int sdeleted)
399 struct net_device *dev = pmc->interface->dev;
400 struct igmpv3_report *pih;
401 struct igmpv3_grec *pgr = NULL;
402 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
403 int scount, stotal, first, isquery, truncate;
405 if (pmc->multiaddr == IGMP_ALL_HOSTS)
408 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
409 type == IGMPV3_MODE_IS_EXCLUDE;
410 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
411 type == IGMPV3_CHANGE_TO_EXCLUDE;
415 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
420 pih = skb ? igmpv3_report_hdr(skb) : NULL;
422 /* EX and TO_EX get a fresh packet, if needed */
424 if (pih && pih->ngrec &&
425 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
427 igmpv3_sendpack(skb);
428 skb = igmpv3_newpack(dev, dev->mtu);
433 for (psf=*psf_list; psf; psf=psf_next) {
436 psf_next = psf->sf_next;
438 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
443 /* clear marks on query responses */
447 if (AVAILABLE(skb) < sizeof(__be32) +
448 first*sizeof(struct igmpv3_grec)) {
449 if (truncate && !first)
450 break; /* truncate these */
452 pgr->grec_nsrcs = htons(scount);
454 igmpv3_sendpack(skb);
455 skb = igmpv3_newpack(dev, dev->mtu);
460 skb = add_grhead(skb, pmc, type, &pgr);
465 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
466 *psrc = psf->sf_inaddr;
468 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
469 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
471 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
473 psf_prev->sf_next = psf->sf_next;
475 *psf_list = psf->sf_next;
485 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
486 type == IGMPV3_BLOCK_OLD_SOURCES)
488 if (pmc->crcount || isquery) {
489 /* make sure we have room for group header */
490 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
491 igmpv3_sendpack(skb);
492 skb = NULL; /* add_grhead will get a new one */
494 skb = add_grhead(skb, pmc, type, &pgr);
498 pgr->grec_nsrcs = htons(scount);
501 pmc->gsquery = 0; /* clear query state on report */
505 #define for_each_pmc_rcu(in_dev, pmc) \
506 for (pmc = rcu_dereference(in_dev->mc_list); \
508 pmc = rcu_dereference(pmc->next_rcu))
510 #define for_each_pmc_rtnl(in_dev, pmc) \
511 for (pmc = rtnl_dereference(in_dev->mc_list); \
513 pmc = rtnl_dereference(pmc->next_rcu))
515 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
517 struct sk_buff *skb = NULL;
522 for_each_pmc_rcu(in_dev, pmc) {
523 if (pmc->multiaddr == IGMP_ALL_HOSTS)
525 spin_lock_bh(&pmc->lock);
526 if (pmc->sfcount[MCAST_EXCLUDE])
527 type = IGMPV3_MODE_IS_EXCLUDE;
529 type = IGMPV3_MODE_IS_INCLUDE;
530 skb = add_grec(skb, pmc, type, 0, 0);
531 spin_unlock_bh(&pmc->lock);
535 spin_lock_bh(&pmc->lock);
536 if (pmc->sfcount[MCAST_EXCLUDE])
537 type = IGMPV3_MODE_IS_EXCLUDE;
539 type = IGMPV3_MODE_IS_INCLUDE;
540 skb = add_grec(skb, pmc, type, 0, 0);
541 spin_unlock_bh(&pmc->lock);
545 return igmpv3_sendpack(skb);
549 * remove zero-count source records from a source filter list
551 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
553 struct ip_sf_list *psf_prev, *psf_next, *psf;
556 for (psf=*ppsf; psf; psf = psf_next) {
557 psf_next = psf->sf_next;
558 if (psf->sf_crcount == 0) {
560 psf_prev->sf_next = psf->sf_next;
562 *ppsf = psf->sf_next;
569 static void igmpv3_send_cr(struct in_device *in_dev)
571 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
572 struct sk_buff *skb = NULL;
576 spin_lock_bh(&in_dev->mc_tomb_lock);
580 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
581 pmc_next = pmc->next;
582 if (pmc->sfmode == MCAST_INCLUDE) {
583 type = IGMPV3_BLOCK_OLD_SOURCES;
584 dtype = IGMPV3_BLOCK_OLD_SOURCES;
585 skb = add_grec(skb, pmc, type, 1, 0);
586 skb = add_grec(skb, pmc, dtype, 1, 1);
589 if (pmc->sfmode == MCAST_EXCLUDE) {
590 type = IGMPV3_CHANGE_TO_INCLUDE;
591 skb = add_grec(skb, pmc, type, 1, 0);
594 if (pmc->crcount == 0) {
595 igmpv3_clear_zeros(&pmc->tomb);
596 igmpv3_clear_zeros(&pmc->sources);
599 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
601 pmc_prev->next = pmc_next;
603 in_dev->mc_tomb = pmc_next;
604 in_dev_put(pmc->interface);
609 spin_unlock_bh(&in_dev->mc_tomb_lock);
612 for_each_pmc_rcu(in_dev, pmc) {
613 spin_lock_bh(&pmc->lock);
614 if (pmc->sfcount[MCAST_EXCLUDE]) {
615 type = IGMPV3_BLOCK_OLD_SOURCES;
616 dtype = IGMPV3_ALLOW_NEW_SOURCES;
618 type = IGMPV3_ALLOW_NEW_SOURCES;
619 dtype = IGMPV3_BLOCK_OLD_SOURCES;
621 skb = add_grec(skb, pmc, type, 0, 0);
622 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
624 /* filter mode changes */
626 if (pmc->sfmode == MCAST_EXCLUDE)
627 type = IGMPV3_CHANGE_TO_EXCLUDE;
629 type = IGMPV3_CHANGE_TO_INCLUDE;
630 skb = add_grec(skb, pmc, type, 0, 0);
633 spin_unlock_bh(&pmc->lock);
639 (void) igmpv3_sendpack(skb);
642 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
649 struct net_device *dev = in_dev->dev;
650 struct net *net = dev_net(dev);
651 __be32 group = pmc ? pmc->multiaddr : 0;
654 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
655 return igmpv3_send_report(in_dev, pmc);
656 else if (type == IGMP_HOST_LEAVE_MESSAGE)
657 dst = IGMP_ALL_ROUTER;
662 struct flowi fl = { .oif = dev->ifindex,
663 .nl_u = { .ip4_u = { .daddr = dst } },
664 .proto = IPPROTO_IGMP };
665 if (ip_route_output_key(net, &rt, &fl))
668 if (rt->rt_src == 0) {
673 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
679 skb_dst_set(skb, &rt->dst);
681 skb_reserve(skb, LL_RESERVED_SPACE(dev));
683 skb_reset_network_header(skb);
685 skb_put(skb, sizeof(struct iphdr) + 4);
688 iph->ihl = (sizeof(struct iphdr)+4)>>2;
690 iph->frag_off = htons(IP_DF);
693 iph->saddr = rt->rt_src;
694 iph->protocol = IPPROTO_IGMP;
695 ip_select_ident(iph, &rt->dst, NULL);
696 ((u8*)&iph[1])[0] = IPOPT_RA;
697 ((u8*)&iph[1])[1] = 4;
698 ((u8*)&iph[1])[2] = 0;
699 ((u8*)&iph[1])[3] = 0;
701 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
706 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
708 return ip_local_out(skb);
711 static void igmp_gq_timer_expire(unsigned long data)
713 struct in_device *in_dev = (struct in_device *)data;
715 in_dev->mr_gq_running = 0;
716 igmpv3_send_report(in_dev, NULL);
717 __in_dev_put(in_dev);
720 static void igmp_ifc_timer_expire(unsigned long data)
722 struct in_device *in_dev = (struct in_device *)data;
724 igmpv3_send_cr(in_dev);
725 if (in_dev->mr_ifc_count) {
726 in_dev->mr_ifc_count--;
727 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
729 __in_dev_put(in_dev);
732 static void igmp_ifc_event(struct in_device *in_dev)
734 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
736 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
737 IGMP_Unsolicited_Report_Count;
738 igmp_ifc_start_timer(in_dev, 1);
742 static void igmp_timer_expire(unsigned long data)
744 struct ip_mc_list *im=(struct ip_mc_list *)data;
745 struct in_device *in_dev = im->interface;
747 spin_lock(&im->lock);
750 if (im->unsolicit_count) {
751 im->unsolicit_count--;
752 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
755 spin_unlock(&im->lock);
757 if (IGMP_V1_SEEN(in_dev))
758 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
759 else if (IGMP_V2_SEEN(in_dev))
760 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
762 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
767 /* mark EXCLUDE-mode sources */
768 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
770 struct ip_sf_list *psf;
774 for (psf=pmc->sources; psf; psf=psf->sf_next) {
777 for (i=0; i<nsrcs; i++) {
778 /* skip inactive filters */
779 if (pmc->sfcount[MCAST_INCLUDE] ||
780 pmc->sfcount[MCAST_EXCLUDE] !=
781 psf->sf_count[MCAST_EXCLUDE])
783 if (srcs[i] == psf->sf_inaddr) {
790 if (scount == nsrcs) /* all sources excluded */
795 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
797 struct ip_sf_list *psf;
800 if (pmc->sfmode == MCAST_EXCLUDE)
801 return igmp_xmarksources(pmc, nsrcs, srcs);
803 /* mark INCLUDE-mode sources */
805 for (psf=pmc->sources; psf; psf=psf->sf_next) {
808 for (i=0; i<nsrcs; i++)
809 if (srcs[i] == psf->sf_inaddr) {
823 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
825 struct ip_mc_list *im;
827 /* Timers are only set for non-local groups */
829 if (group == IGMP_ALL_HOSTS)
833 for_each_pmc_rcu(in_dev, im) {
834 if (im->multiaddr == group) {
842 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
845 struct igmphdr *ih = igmp_hdr(skb);
846 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
847 struct ip_mc_list *im;
848 __be32 group = ih->group;
855 /* Alas, old v1 router presents here. */
857 max_delay = IGMP_Query_Response_Interval;
858 in_dev->mr_v1_seen = jiffies +
859 IGMP_V1_Router_Present_Timeout;
862 /* v2 router present */
863 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
864 in_dev->mr_v2_seen = jiffies +
865 IGMP_V2_Router_Present_Timeout;
867 /* cancel the interface change timer */
868 in_dev->mr_ifc_count = 0;
869 if (del_timer(&in_dev->mr_ifc_timer))
870 __in_dev_put(in_dev);
871 /* clear deleted report items */
872 igmpv3_clear_delrec(in_dev);
873 } else if (len < 12) {
874 return; /* ignore bogus packet; freed by caller */
875 } else if (IGMP_V1_SEEN(in_dev)) {
876 /* This is a v3 query with v1 queriers present */
877 max_delay = IGMP_Query_Response_Interval;
879 } else if (IGMP_V2_SEEN(in_dev)) {
880 /* this is a v3 query with v2 queriers present;
881 * Interpretation of the max_delay code is problematic here.
882 * A real v2 host would use ih_code directly, while v3 has a
883 * different encoding. We use the v3 encoding as more likely
884 * to be intended in a v3 query.
886 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
888 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
891 ih3 = igmpv3_query_hdr(skb);
893 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
894 + ntohs(ih3->nsrcs)*sizeof(__be32)))
896 ih3 = igmpv3_query_hdr(skb);
899 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
901 max_delay = 1; /* can't mod w/ 0 */
902 in_dev->mr_maxdelay = max_delay;
904 in_dev->mr_qrv = ih3->qrv;
905 if (!group) { /* general query */
907 return; /* no sources allowed */
908 igmp_gq_start_timer(in_dev);
911 /* mark sources to include, if group & source-specific */
912 mark = ih3->nsrcs != 0;
916 * - Start the timers in all of our membership records
917 * that the query applies to for the interface on
918 * which the query arrived excl. those that belong
919 * to a "local" group (224.0.0.X)
920 * - For timers already running check if they need to
922 * - Use the igmp->igmp_code field as the maximum
926 for_each_pmc_rcu(in_dev, im) {
929 if (group && group != im->multiaddr)
931 if (im->multiaddr == IGMP_ALL_HOSTS)
933 spin_lock_bh(&im->lock);
935 im->gsquery = im->gsquery && mark;
938 changed = !im->gsquery ||
939 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
940 spin_unlock_bh(&im->lock);
942 igmp_mod_timer(im, max_delay);
947 /* called in rcu_read_lock() section */
948 int igmp_rcv(struct sk_buff *skb)
950 /* This basically follows the spec line by line -- see RFC1112 */
952 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
958 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
961 switch (skb->ip_summed) {
962 case CHECKSUM_COMPLETE:
963 if (!csum_fold(skb->csum))
968 if (__skb_checksum_complete(skb))
974 case IGMP_HOST_MEMBERSHIP_QUERY:
975 igmp_heard_query(in_dev, skb, len);
977 case IGMP_HOST_MEMBERSHIP_REPORT:
978 case IGMPV2_HOST_MEMBERSHIP_REPORT:
979 /* Is it our report looped back? */
980 if (rt_is_output_route(skb_rtable(skb)))
982 /* don't rely on MC router hearing unicast reports */
983 if (skb->pkt_type == PACKET_MULTICAST ||
984 skb->pkt_type == PACKET_BROADCAST)
985 igmp_heard_report(in_dev, ih->group);
988 #ifdef CONFIG_IP_PIMSM_V1
989 return pim_rcv_v1(skb);
991 case IGMPV3_HOST_MEMBERSHIP_REPORT:
994 case IGMP_HOST_LEAVE_MESSAGE:
996 case IGMP_MTRACE_RESP:
1011 * Add a filter to a device
1014 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1016 char buf[MAX_ADDR_LEN];
1017 struct net_device *dev = in_dev->dev;
1019 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1020 We will get multicast token leakage, when IFF_MULTICAST
1021 is changed. This check should be done in dev->set_multicast_list
1022 routine. Something sort of:
1023 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1026 if (arp_mc_map(addr, buf, dev, 0) == 0)
1027 dev_mc_add(dev, buf);
1031 * Remove a filter from a device
1034 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1036 char buf[MAX_ADDR_LEN];
1037 struct net_device *dev = in_dev->dev;
1039 if (arp_mc_map(addr, buf, dev, 0) == 0)
1040 dev_mc_del(dev, buf);
1043 #ifdef CONFIG_IP_MULTICAST
1045 * deleted ip_mc_list manipulation
1047 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1049 struct ip_mc_list *pmc;
1051 /* this is an "ip_mc_list" for convenience; only the fields below
1052 * are actually used. In particular, the refcnt and users are not
1053 * used for management of the delete list. Using the same structure
1054 * for deleted items allows change reports to use common code with
1055 * non-deleted or query-response MCA's.
1057 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1060 spin_lock_bh(&im->lock);
1061 pmc->interface = im->interface;
1062 in_dev_hold(in_dev);
1063 pmc->multiaddr = im->multiaddr;
1064 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1065 IGMP_Unsolicited_Report_Count;
1066 pmc->sfmode = im->sfmode;
1067 if (pmc->sfmode == MCAST_INCLUDE) {
1068 struct ip_sf_list *psf;
1070 pmc->tomb = im->tomb;
1071 pmc->sources = im->sources;
1072 im->tomb = im->sources = NULL;
1073 for (psf=pmc->sources; psf; psf=psf->sf_next)
1074 psf->sf_crcount = pmc->crcount;
1076 spin_unlock_bh(&im->lock);
1078 spin_lock_bh(&in_dev->mc_tomb_lock);
1079 pmc->next = in_dev->mc_tomb;
1080 in_dev->mc_tomb = pmc;
1081 spin_unlock_bh(&in_dev->mc_tomb_lock);
1084 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1086 struct ip_mc_list *pmc, *pmc_prev;
1087 struct ip_sf_list *psf, *psf_next;
1089 spin_lock_bh(&in_dev->mc_tomb_lock);
1091 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1092 if (pmc->multiaddr == multiaddr)
1098 pmc_prev->next = pmc->next;
1100 in_dev->mc_tomb = pmc->next;
1102 spin_unlock_bh(&in_dev->mc_tomb_lock);
1104 for (psf=pmc->tomb; psf; psf=psf_next) {
1105 psf_next = psf->sf_next;
1108 in_dev_put(pmc->interface);
1113 static void igmpv3_clear_delrec(struct in_device *in_dev)
1115 struct ip_mc_list *pmc, *nextpmc;
1117 spin_lock_bh(&in_dev->mc_tomb_lock);
1118 pmc = in_dev->mc_tomb;
1119 in_dev->mc_tomb = NULL;
1120 spin_unlock_bh(&in_dev->mc_tomb_lock);
1122 for (; pmc; pmc = nextpmc) {
1123 nextpmc = pmc->next;
1124 ip_mc_clear_src(pmc);
1125 in_dev_put(pmc->interface);
1128 /* clear dead sources, too */
1130 for_each_pmc_rcu(in_dev, pmc) {
1131 struct ip_sf_list *psf, *psf_next;
1133 spin_lock_bh(&pmc->lock);
1136 spin_unlock_bh(&pmc->lock);
1137 for (; psf; psf=psf_next) {
1138 psf_next = psf->sf_next;
1146 static void igmp_group_dropped(struct ip_mc_list *im)
1148 struct in_device *in_dev = im->interface;
1149 #ifdef CONFIG_IP_MULTICAST
1155 ip_mc_filter_del(in_dev, im->multiaddr);
1158 #ifdef CONFIG_IP_MULTICAST
1159 if (im->multiaddr == IGMP_ALL_HOSTS)
1162 reporter = im->reporter;
1163 igmp_stop_timer(im);
1165 if (!in_dev->dead) {
1166 if (IGMP_V1_SEEN(in_dev))
1168 if (IGMP_V2_SEEN(in_dev)) {
1170 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1174 igmpv3_add_delrec(in_dev, im);
1176 igmp_ifc_event(in_dev);
1180 ip_mc_clear_src(im);
1183 static void igmp_group_added(struct ip_mc_list *im)
1185 struct in_device *in_dev = im->interface;
1187 if (im->loaded == 0) {
1189 ip_mc_filter_add(in_dev, im->multiaddr);
1192 #ifdef CONFIG_IP_MULTICAST
1193 if (im->multiaddr == IGMP_ALL_HOSTS)
1198 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1199 spin_lock_bh(&im->lock);
1200 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1201 spin_unlock_bh(&im->lock);
1206 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1207 IGMP_Unsolicited_Report_Count;
1208 igmp_ifc_event(in_dev);
1214 * Multicast list managers
1219 * A socket has joined a multicast group on device dev.
1222 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1224 struct ip_mc_list *im;
1228 for_each_pmc_rtnl(in_dev, im) {
1229 if (im->multiaddr == addr) {
1231 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1236 im = kzalloc(sizeof(*im), GFP_KERNEL);
1241 im->interface = in_dev;
1242 in_dev_hold(in_dev);
1243 im->multiaddr = addr;
1244 /* initial mode is (EX, empty) */
1245 im->sfmode = MCAST_EXCLUDE;
1246 im->sfcount[MCAST_EXCLUDE] = 1;
1247 atomic_set(&im->refcnt, 1);
1248 spin_lock_init(&im->lock);
1249 #ifdef CONFIG_IP_MULTICAST
1250 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1251 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1254 im->next_rcu = in_dev->mc_list;
1256 rcu_assign_pointer(in_dev->mc_list, im);
1258 #ifdef CONFIG_IP_MULTICAST
1259 igmpv3_del_delrec(in_dev, im->multiaddr);
1261 igmp_group_added(im);
1263 ip_rt_multicast_event(in_dev);
1267 EXPORT_SYMBOL(ip_mc_inc_group);
1270 * Resend IGMP JOIN report; used for bonding.
1272 void ip_mc_rejoin_group(struct ip_mc_list *im)
1274 #ifdef CONFIG_IP_MULTICAST
1275 struct in_device *in_dev = im->interface;
1277 if (im->multiaddr == IGMP_ALL_HOSTS)
1280 /* a failover is happening and switches
1281 * must be notified immediately */
1282 if (IGMP_V1_SEEN(in_dev))
1283 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
1284 else if (IGMP_V2_SEEN(in_dev))
1285 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
1287 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
1290 EXPORT_SYMBOL(ip_mc_rejoin_group);
1293 * A socket has left a multicast group on device dev
1296 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1298 struct ip_mc_list *i;
1299 struct ip_mc_list __rcu **ip;
1303 for (ip = &in_dev->mc_list;
1304 (i = rtnl_dereference(*ip)) != NULL;
1305 ip = &i->next_rcu) {
1306 if (i->multiaddr == addr) {
1307 if (--i->users == 0) {
1310 igmp_group_dropped(i);
1313 ip_rt_multicast_event(in_dev);
1322 EXPORT_SYMBOL(ip_mc_dec_group);
1324 /* Device changing type */
1326 void ip_mc_unmap(struct in_device *in_dev)
1328 struct ip_mc_list *pmc;
1332 for_each_pmc_rtnl(in_dev, pmc)
1333 igmp_group_dropped(pmc);
1336 void ip_mc_remap(struct in_device *in_dev)
1338 struct ip_mc_list *pmc;
1342 for_each_pmc_rtnl(in_dev, pmc)
1343 igmp_group_added(pmc);
1346 /* Device going down */
1348 void ip_mc_down(struct in_device *in_dev)
1350 struct ip_mc_list *pmc;
1354 for_each_pmc_rtnl(in_dev, pmc)
1355 igmp_group_dropped(pmc);
1357 #ifdef CONFIG_IP_MULTICAST
1358 in_dev->mr_ifc_count = 0;
1359 if (del_timer(&in_dev->mr_ifc_timer))
1360 __in_dev_put(in_dev);
1361 in_dev->mr_gq_running = 0;
1362 if (del_timer(&in_dev->mr_gq_timer))
1363 __in_dev_put(in_dev);
1364 igmpv3_clear_delrec(in_dev);
1367 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1370 void ip_mc_init_dev(struct in_device *in_dev)
1374 in_dev->mc_tomb = NULL;
1375 #ifdef CONFIG_IP_MULTICAST
1376 in_dev->mr_gq_running = 0;
1377 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1378 (unsigned long)in_dev);
1379 in_dev->mr_ifc_count = 0;
1380 in_dev->mc_count = 0;
1381 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1382 (unsigned long)in_dev);
1383 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1386 spin_lock_init(&in_dev->mc_tomb_lock);
1389 /* Device going up */
1391 void ip_mc_up(struct in_device *in_dev)
1393 struct ip_mc_list *pmc;
1397 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1399 for_each_pmc_rtnl(in_dev, pmc)
1400 igmp_group_added(pmc);
1404 * Device is about to be destroyed: clean up.
1407 void ip_mc_destroy_dev(struct in_device *in_dev)
1409 struct ip_mc_list *i;
1413 /* Deactivate timers */
1416 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1417 in_dev->mc_list = i->next_rcu;
1420 igmp_group_dropped(i);
1425 /* RTNL is locked */
1426 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1428 struct flowi fl = { .nl_u = { .ip4_u =
1429 { .daddr = imr->imr_multiaddr.s_addr } } };
1431 struct net_device *dev = NULL;
1432 struct in_device *idev = NULL;
1434 if (imr->imr_ifindex) {
1435 idev = inetdev_by_index(net, imr->imr_ifindex);
1438 if (imr->imr_address.s_addr) {
1439 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1444 if (!dev && !ip_route_output_key(net, &rt, &fl)) {
1449 imr->imr_ifindex = dev->ifindex;
1450 idev = __in_dev_get_rtnl(dev);
1456 * Join a socket to a group
1458 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1459 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1462 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1465 struct ip_sf_list *psf, *psf_prev;
1469 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1470 if (psf->sf_inaddr == *psfsrc)
1474 if (!psf || psf->sf_count[sfmode] == 0) {
1475 /* source filter not found, or count wrong => bug */
1478 psf->sf_count[sfmode]--;
1479 if (psf->sf_count[sfmode] == 0) {
1480 ip_rt_multicast_event(pmc->interface);
1482 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1483 #ifdef CONFIG_IP_MULTICAST
1484 struct in_device *in_dev = pmc->interface;
1487 /* no more filters for this source */
1489 psf_prev->sf_next = psf->sf_next;
1491 pmc->sources = psf->sf_next;
1492 #ifdef CONFIG_IP_MULTICAST
1493 if (psf->sf_oldin &&
1494 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1495 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1496 IGMP_Unsolicited_Report_Count;
1497 psf->sf_next = pmc->tomb;
1507 #ifndef CONFIG_IP_MULTICAST
1508 #define igmp_ifc_event(x) do { } while (0)
1511 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1512 int sfcount, __be32 *psfsrc, int delta)
1514 struct ip_mc_list *pmc;
1521 for_each_pmc_rcu(in_dev, pmc) {
1522 if (*pmca == pmc->multiaddr)
1526 /* MCA not found?? bug */
1530 spin_lock_bh(&pmc->lock);
1532 #ifdef CONFIG_IP_MULTICAST
1537 if (!pmc->sfcount[sfmode])
1539 pmc->sfcount[sfmode]--;
1542 for (i=0; i<sfcount; i++) {
1543 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1545 changerec |= rv > 0;
1549 if (pmc->sfmode == MCAST_EXCLUDE &&
1550 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1551 pmc->sfcount[MCAST_INCLUDE]) {
1552 #ifdef CONFIG_IP_MULTICAST
1553 struct ip_sf_list *psf;
1556 /* filter mode change */
1557 pmc->sfmode = MCAST_INCLUDE;
1558 #ifdef CONFIG_IP_MULTICAST
1559 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1560 IGMP_Unsolicited_Report_Count;
1561 in_dev->mr_ifc_count = pmc->crcount;
1562 for (psf=pmc->sources; psf; psf = psf->sf_next)
1563 psf->sf_crcount = 0;
1564 igmp_ifc_event(pmc->interface);
1565 } else if (sf_setstate(pmc) || changerec) {
1566 igmp_ifc_event(pmc->interface);
1570 spin_unlock_bh(&pmc->lock);
1575 * Add multicast single-source filter to the interface list
1577 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1578 __be32 *psfsrc, int delta)
1580 struct ip_sf_list *psf, *psf_prev;
1583 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1584 if (psf->sf_inaddr == *psfsrc)
1589 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1592 psf->sf_inaddr = *psfsrc;
1594 psf_prev->sf_next = psf;
1598 psf->sf_count[sfmode]++;
1599 if (psf->sf_count[sfmode] == 1) {
1600 ip_rt_multicast_event(pmc->interface);
1605 #ifdef CONFIG_IP_MULTICAST
1606 static void sf_markstate(struct ip_mc_list *pmc)
1608 struct ip_sf_list *psf;
1609 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1611 for (psf=pmc->sources; psf; psf=psf->sf_next)
1612 if (pmc->sfcount[MCAST_EXCLUDE]) {
1613 psf->sf_oldin = mca_xcount ==
1614 psf->sf_count[MCAST_EXCLUDE] &&
1615 !psf->sf_count[MCAST_INCLUDE];
1617 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1620 static int sf_setstate(struct ip_mc_list *pmc)
1622 struct ip_sf_list *psf, *dpsf;
1623 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1624 int qrv = pmc->interface->mr_qrv;
1628 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1629 if (pmc->sfcount[MCAST_EXCLUDE]) {
1630 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1631 !psf->sf_count[MCAST_INCLUDE];
1633 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1635 if (!psf->sf_oldin) {
1636 struct ip_sf_list *prev = NULL;
1638 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1639 if (dpsf->sf_inaddr == psf->sf_inaddr)
1645 prev->sf_next = dpsf->sf_next;
1647 pmc->tomb = dpsf->sf_next;
1650 psf->sf_crcount = qrv;
1653 } else if (psf->sf_oldin) {
1655 psf->sf_crcount = 0;
1657 * add or update "delete" records if an active filter
1660 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1661 if (dpsf->sf_inaddr == psf->sf_inaddr)
1664 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1668 /* pmc->lock held by callers */
1669 dpsf->sf_next = pmc->tomb;
1672 dpsf->sf_crcount = qrv;
1681 * Add multicast source filter list to the interface list
1683 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1684 int sfcount, __be32 *psfsrc, int delta)
1686 struct ip_mc_list *pmc;
1693 for_each_pmc_rcu(in_dev, pmc) {
1694 if (*pmca == pmc->multiaddr)
1698 /* MCA not found?? bug */
1702 spin_lock_bh(&pmc->lock);
1705 #ifdef CONFIG_IP_MULTICAST
1708 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1710 pmc->sfcount[sfmode]++;
1712 for (i=0; i<sfcount; i++) {
1713 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1720 pmc->sfcount[sfmode]--;
1722 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1723 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1724 #ifdef CONFIG_IP_MULTICAST
1725 struct ip_sf_list *psf;
1726 in_dev = pmc->interface;
1729 /* filter mode change */
1730 if (pmc->sfcount[MCAST_EXCLUDE])
1731 pmc->sfmode = MCAST_EXCLUDE;
1732 else if (pmc->sfcount[MCAST_INCLUDE])
1733 pmc->sfmode = MCAST_INCLUDE;
1734 #ifdef CONFIG_IP_MULTICAST
1735 /* else no filters; keep old mode for reports */
1737 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1738 IGMP_Unsolicited_Report_Count;
1739 in_dev->mr_ifc_count = pmc->crcount;
1740 for (psf=pmc->sources; psf; psf = psf->sf_next)
1741 psf->sf_crcount = 0;
1742 igmp_ifc_event(in_dev);
1743 } else if (sf_setstate(pmc)) {
1744 igmp_ifc_event(in_dev);
1747 spin_unlock_bh(&pmc->lock);
1751 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1753 struct ip_sf_list *psf, *nextpsf;
1755 for (psf=pmc->tomb; psf; psf=nextpsf) {
1756 nextpsf = psf->sf_next;
1760 for (psf=pmc->sources; psf; psf=nextpsf) {
1761 nextpsf = psf->sf_next;
1764 pmc->sources = NULL;
1765 pmc->sfmode = MCAST_EXCLUDE;
1766 pmc->sfcount[MCAST_INCLUDE] = 0;
1767 pmc->sfcount[MCAST_EXCLUDE] = 1;
1772 * Join a multicast group
1774 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1777 __be32 addr = imr->imr_multiaddr.s_addr;
1778 struct ip_mc_socklist *iml = NULL, *i;
1779 struct in_device *in_dev;
1780 struct inet_sock *inet = inet_sk(sk);
1781 struct net *net = sock_net(sk);
1785 if (!ipv4_is_multicast(addr))
1790 in_dev = ip_mc_find_dev(net, imr);
1799 ifindex = imr->imr_ifindex;
1800 for_each_pmc_rtnl(inet, i) {
1801 if (i->multi.imr_multiaddr.s_addr == addr &&
1802 i->multi.imr_ifindex == ifindex)
1807 if (count >= sysctl_igmp_max_memberships)
1809 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1813 memcpy(&iml->multi, imr, sizeof(*imr));
1814 iml->next_rcu = inet->mc_list;
1816 iml->sfmode = MCAST_EXCLUDE;
1817 rcu_assign_pointer(inet->mc_list, iml);
1818 ip_mc_inc_group(in_dev, addr);
1824 EXPORT_SYMBOL(ip_mc_join_group);
1826 static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1828 kfree(container_of(rp, struct ip_sf_socklist, rcu));
1829 /* sk_omem_alloc should have been decreased by the caller*/
1832 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1833 struct in_device *in_dev)
1835 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1839 /* any-source empty exclude case */
1840 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1841 iml->sfmode, 0, NULL, 0);
1843 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1844 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1845 rcu_assign_pointer(iml->sflist, NULL);
1846 /* decrease mem now to avoid the memleak warning */
1847 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1848 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
1853 static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1855 kfree(container_of(rp, struct ip_mc_socklist, rcu));
1856 /* sk_omem_alloc should have been decreased by the caller*/
1861 * Ask a socket to leave a group.
1864 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1866 struct inet_sock *inet = inet_sk(sk);
1867 struct ip_mc_socklist *iml;
1868 struct ip_mc_socklist __rcu **imlp;
1869 struct in_device *in_dev;
1870 struct net *net = sock_net(sk);
1871 __be32 group = imr->imr_multiaddr.s_addr;
1873 int ret = -EADDRNOTAVAIL;
1876 in_dev = ip_mc_find_dev(net, imr);
1877 ifindex = imr->imr_ifindex;
1878 for (imlp = &inet->mc_list;
1879 (iml = rtnl_dereference(*imlp)) != NULL;
1880 imlp = &iml->next_rcu) {
1881 if (iml->multi.imr_multiaddr.s_addr != group)
1884 if (iml->multi.imr_ifindex != ifindex)
1886 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1887 iml->multi.imr_address.s_addr)
1890 (void) ip_mc_leave_src(sk, iml, in_dev);
1892 *imlp = iml->next_rcu;
1895 ip_mc_dec_group(in_dev, group);
1897 /* decrease mem now to avoid the memleak warning */
1898 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1899 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
1908 int ip_mc_source(int add, int omode, struct sock *sk, struct
1909 ip_mreq_source *mreqs, int ifindex)
1912 struct ip_mreqn imr;
1913 __be32 addr = mreqs->imr_multiaddr;
1914 struct ip_mc_socklist *pmc;
1915 struct in_device *in_dev = NULL;
1916 struct inet_sock *inet = inet_sk(sk);
1917 struct ip_sf_socklist *psl;
1918 struct net *net = sock_net(sk);
1922 if (!ipv4_is_multicast(addr))
1927 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1928 imr.imr_address.s_addr = mreqs->imr_interface;
1929 imr.imr_ifindex = ifindex;
1930 in_dev = ip_mc_find_dev(net, &imr);
1936 err = -EADDRNOTAVAIL;
1938 for_each_pmc_rtnl(inet, pmc) {
1939 if ((pmc->multi.imr_multiaddr.s_addr ==
1940 imr.imr_multiaddr.s_addr) &&
1941 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1944 if (!pmc) { /* must have a prior join */
1948 /* if a source filter was set, must be the same mode as before */
1950 if (pmc->sfmode != omode) {
1954 } else if (pmc->sfmode != omode) {
1955 /* allow mode switches for empty-set filters */
1956 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1957 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1959 pmc->sfmode = omode;
1962 psl = rtnl_dereference(pmc->sflist);
1965 goto done; /* err = -EADDRNOTAVAIL */
1967 for (i=0; i<psl->sl_count; i++) {
1968 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1973 if (rv) /* source not found */
1974 goto done; /* err = -EADDRNOTAVAIL */
1976 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1977 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1982 /* update the interface filter */
1983 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1984 &mreqs->imr_sourceaddr, 1);
1986 for (j=i+1; j<psl->sl_count; j++)
1987 psl->sl_addr[j-1] = psl->sl_addr[j];
1992 /* else, add a new source to the filter */
1994 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1998 if (!psl || psl->sl_count == psl->sl_max) {
1999 struct ip_sf_socklist *newpsl;
2000 int count = IP_SFBLOCK;
2003 count += psl->sl_max;
2004 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2009 newpsl->sl_max = count;
2010 newpsl->sl_count = count - IP_SFBLOCK;
2012 for (i=0; i<psl->sl_count; i++)
2013 newpsl->sl_addr[i] = psl->sl_addr[i];
2014 /* decrease mem now to avoid the memleak warning */
2015 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2016 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2018 rcu_assign_pointer(pmc->sflist, newpsl);
2021 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2022 for (i=0; i<psl->sl_count; i++) {
2023 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2028 if (rv == 0) /* address already there is an error */
2030 for (j=psl->sl_count-1; j>=i; j--)
2031 psl->sl_addr[j+1] = psl->sl_addr[j];
2032 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2035 /* update the interface list */
2036 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2037 &mreqs->imr_sourceaddr, 1);
2041 return ip_mc_leave_group(sk, &imr);
2045 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2048 struct ip_mreqn imr;
2049 __be32 addr = msf->imsf_multiaddr;
2050 struct ip_mc_socklist *pmc;
2051 struct in_device *in_dev;
2052 struct inet_sock *inet = inet_sk(sk);
2053 struct ip_sf_socklist *newpsl, *psl;
2054 struct net *net = sock_net(sk);
2057 if (!ipv4_is_multicast(addr))
2059 if (msf->imsf_fmode != MCAST_INCLUDE &&
2060 msf->imsf_fmode != MCAST_EXCLUDE)
2065 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2066 imr.imr_address.s_addr = msf->imsf_interface;
2067 imr.imr_ifindex = ifindex;
2068 in_dev = ip_mc_find_dev(net, &imr);
2075 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2076 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2081 for_each_pmc_rtnl(inet, pmc) {
2082 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2083 pmc->multi.imr_ifindex == imr.imr_ifindex)
2086 if (!pmc) { /* must have a prior join */
2090 if (msf->imsf_numsrc) {
2091 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2097 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2098 memcpy(newpsl->sl_addr, msf->imsf_slist,
2099 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2100 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2101 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2103 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2108 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2109 msf->imsf_fmode, 0, NULL, 0);
2111 psl = rtnl_dereference(pmc->sflist);
2113 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2114 psl->sl_count, psl->sl_addr, 0);
2115 /* decrease mem now to avoid the memleak warning */
2116 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2117 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2119 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2121 rcu_assign_pointer(pmc->sflist, newpsl);
2122 pmc->sfmode = msf->imsf_fmode;
2127 err = ip_mc_leave_group(sk, &imr);
2131 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2132 struct ip_msfilter __user *optval, int __user *optlen)
2134 int err, len, count, copycount;
2135 struct ip_mreqn imr;
2136 __be32 addr = msf->imsf_multiaddr;
2137 struct ip_mc_socklist *pmc;
2138 struct in_device *in_dev;
2139 struct inet_sock *inet = inet_sk(sk);
2140 struct ip_sf_socklist *psl;
2141 struct net *net = sock_net(sk);
2143 if (!ipv4_is_multicast(addr))
2148 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2149 imr.imr_address.s_addr = msf->imsf_interface;
2150 imr.imr_ifindex = 0;
2151 in_dev = ip_mc_find_dev(net, &imr);
2157 err = -EADDRNOTAVAIL;
2159 for_each_pmc_rtnl(inet, pmc) {
2160 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2161 pmc->multi.imr_ifindex == imr.imr_ifindex)
2164 if (!pmc) /* must have a prior join */
2166 msf->imsf_fmode = pmc->sfmode;
2167 psl = rtnl_dereference(pmc->sflist);
2173 count = psl->sl_count;
2175 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2176 len = copycount * sizeof(psl->sl_addr[0]);
2177 msf->imsf_numsrc = count;
2178 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2179 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2183 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2191 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2192 struct group_filter __user *optval, int __user *optlen)
2194 int err, i, count, copycount;
2195 struct sockaddr_in *psin;
2197 struct ip_mc_socklist *pmc;
2198 struct inet_sock *inet = inet_sk(sk);
2199 struct ip_sf_socklist *psl;
2201 psin = (struct sockaddr_in *)&gsf->gf_group;
2202 if (psin->sin_family != AF_INET)
2204 addr = psin->sin_addr.s_addr;
2205 if (!ipv4_is_multicast(addr))
2210 err = -EADDRNOTAVAIL;
2212 for_each_pmc_rtnl(inet, pmc) {
2213 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2214 pmc->multi.imr_ifindex == gsf->gf_interface)
2217 if (!pmc) /* must have a prior join */
2219 gsf->gf_fmode = pmc->sfmode;
2220 psl = rtnl_dereference(pmc->sflist);
2222 count = psl ? psl->sl_count : 0;
2223 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2224 gsf->gf_numsrc = count;
2225 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2226 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2229 for (i=0; i<copycount; i++) {
2230 struct sockaddr_storage ss;
2232 psin = (struct sockaddr_in *)&ss;
2233 memset(&ss, 0, sizeof(ss));
2234 psin->sin_family = AF_INET;
2235 psin->sin_addr.s_addr = psl->sl_addr[i];
2236 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2246 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2248 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2250 struct inet_sock *inet = inet_sk(sk);
2251 struct ip_mc_socklist *pmc;
2252 struct ip_sf_socklist *psl;
2257 if (!ipv4_is_multicast(loc_addr))
2261 for_each_pmc_rcu(inet, pmc) {
2262 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2263 pmc->multi.imr_ifindex == dif)
2269 psl = rcu_dereference(pmc->sflist);
2270 ret = (pmc->sfmode == MCAST_EXCLUDE);
2274 for (i=0; i<psl->sl_count; i++) {
2275 if (psl->sl_addr[i] == rmt_addr)
2279 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2281 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2291 * A socket is closing.
2294 void ip_mc_drop_socket(struct sock *sk)
2296 struct inet_sock *inet = inet_sk(sk);
2297 struct ip_mc_socklist *iml;
2298 struct net *net = sock_net(sk);
2300 if (inet->mc_list == NULL)
2304 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2305 struct in_device *in_dev;
2307 inet->mc_list = iml->next_rcu;
2308 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2309 (void) ip_mc_leave_src(sk, iml, in_dev);
2311 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2312 /* decrease mem now to avoid the memleak warning */
2313 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2314 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
2319 int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2321 struct ip_mc_list *im;
2322 struct ip_sf_list *psf;
2326 for_each_pmc_rcu(in_dev, im) {
2327 if (im->multiaddr == mc_addr)
2330 if (im && proto == IPPROTO_IGMP) {
2334 for (psf=im->sources; psf; psf=psf->sf_next) {
2335 if (psf->sf_inaddr == src_addr)
2339 rv = psf->sf_count[MCAST_INCLUDE] ||
2340 psf->sf_count[MCAST_EXCLUDE] !=
2341 im->sfcount[MCAST_EXCLUDE];
2343 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2345 rv = 1; /* unspecified source; tentatively allow */
2351 #if defined(CONFIG_PROC_FS)
2352 struct igmp_mc_iter_state {
2353 struct seq_net_private p;
2354 struct net_device *dev;
2355 struct in_device *in_dev;
2358 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2360 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2362 struct net *net = seq_file_net(seq);
2363 struct ip_mc_list *im = NULL;
2364 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2366 state->in_dev = NULL;
2367 for_each_netdev_rcu(net, state->dev) {
2368 struct in_device *in_dev;
2370 in_dev = __in_dev_get_rcu(state->dev);
2373 im = rcu_dereference(in_dev->mc_list);
2375 state->in_dev = in_dev;
2382 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2384 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2386 im = rcu_dereference(im->next_rcu);
2388 state->dev = next_net_device_rcu(state->dev);
2390 state->in_dev = NULL;
2393 state->in_dev = __in_dev_get_rcu(state->dev);
2396 im = rcu_dereference(state->in_dev->mc_list);
2401 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2403 struct ip_mc_list *im = igmp_mc_get_first(seq);
2405 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2407 return pos ? NULL : im;
2410 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2414 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2417 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2419 struct ip_mc_list *im;
2420 if (v == SEQ_START_TOKEN)
2421 im = igmp_mc_get_first(seq);
2423 im = igmp_mc_get_next(seq, v);
2428 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2431 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2433 state->in_dev = NULL;
2438 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2440 if (v == SEQ_START_TOKEN)
2442 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2444 struct ip_mc_list *im = (struct ip_mc_list *)v;
2445 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2447 #ifdef CONFIG_IP_MULTICAST
2448 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2449 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2455 if (rcu_dereference(state->in_dev->mc_list) == im) {
2456 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2457 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2461 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2462 im->multiaddr, im->users,
2463 im->tm_running, im->tm_running ?
2464 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2470 static const struct seq_operations igmp_mc_seq_ops = {
2471 .start = igmp_mc_seq_start,
2472 .next = igmp_mc_seq_next,
2473 .stop = igmp_mc_seq_stop,
2474 .show = igmp_mc_seq_show,
2477 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2479 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2480 sizeof(struct igmp_mc_iter_state));
2483 static const struct file_operations igmp_mc_seq_fops = {
2484 .owner = THIS_MODULE,
2485 .open = igmp_mc_seq_open,
2487 .llseek = seq_lseek,
2488 .release = seq_release_net,
2491 struct igmp_mcf_iter_state {
2492 struct seq_net_private p;
2493 struct net_device *dev;
2494 struct in_device *idev;
2495 struct ip_mc_list *im;
2498 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2500 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2502 struct net *net = seq_file_net(seq);
2503 struct ip_sf_list *psf = NULL;
2504 struct ip_mc_list *im = NULL;
2505 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2509 for_each_netdev_rcu(net, state->dev) {
2510 struct in_device *idev;
2511 idev = __in_dev_get_rcu(state->dev);
2512 if (unlikely(idev == NULL))
2514 im = rcu_dereference(idev->mc_list);
2515 if (likely(im != NULL)) {
2516 spin_lock_bh(&im->lock);
2518 if (likely(psf != NULL)) {
2523 spin_unlock_bh(&im->lock);
2529 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2531 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2535 spin_unlock_bh(&state->im->lock);
2536 state->im = state->im->next;
2537 while (!state->im) {
2538 state->dev = next_net_device_rcu(state->dev);
2543 state->idev = __in_dev_get_rcu(state->dev);
2546 state->im = rcu_dereference(state->idev->mc_list);
2550 spin_lock_bh(&state->im->lock);
2551 psf = state->im->sources;
2557 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2559 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2561 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2563 return pos ? NULL : psf;
2566 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2570 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2573 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2575 struct ip_sf_list *psf;
2576 if (v == SEQ_START_TOKEN)
2577 psf = igmp_mcf_get_first(seq);
2579 psf = igmp_mcf_get_next(seq, v);
2584 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2587 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2588 if (likely(state->im != NULL)) {
2589 spin_unlock_bh(&state->im->lock);
2597 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2599 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2600 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2602 if (v == SEQ_START_TOKEN) {
2605 "%10s %10s %6s %6s\n", "Idx",
2607 "SRC", "INC", "EXC");
2611 "0x%08x %6lu %6lu\n",
2612 state->dev->ifindex, state->dev->name,
2613 ntohl(state->im->multiaddr),
2614 ntohl(psf->sf_inaddr),
2615 psf->sf_count[MCAST_INCLUDE],
2616 psf->sf_count[MCAST_EXCLUDE]);
2621 static const struct seq_operations igmp_mcf_seq_ops = {
2622 .start = igmp_mcf_seq_start,
2623 .next = igmp_mcf_seq_next,
2624 .stop = igmp_mcf_seq_stop,
2625 .show = igmp_mcf_seq_show,
2628 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2630 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2631 sizeof(struct igmp_mcf_iter_state));
2634 static const struct file_operations igmp_mcf_seq_fops = {
2635 .owner = THIS_MODULE,
2636 .open = igmp_mcf_seq_open,
2638 .llseek = seq_lseek,
2639 .release = seq_release_net,
2642 static int __net_init igmp_net_init(struct net *net)
2644 struct proc_dir_entry *pde;
2646 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2649 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2655 proc_net_remove(net, "igmp");
2660 static void __net_exit igmp_net_exit(struct net *net)
2662 proc_net_remove(net, "mcfilter");
2663 proc_net_remove(net, "igmp");
2666 static struct pernet_operations igmp_net_ops = {
2667 .init = igmp_net_init,
2668 .exit = igmp_net_exit,
2671 int __init igmp_mc_proc_init(void)
2673 return register_pernet_subsys(&igmp_net_ops);