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;
324 rt = ip_route_output_ports(net, NULL, IGMPV3_ALL_MCR, 0,
326 IPPROTO_IGMP, 0, dev->ifindex);
332 skb_dst_set(skb, &rt->dst);
335 skb_reserve(skb, LL_RESERVED_SPACE(dev));
337 skb_reset_network_header(skb);
339 skb_put(skb, sizeof(struct iphdr) + 4);
342 pip->ihl = (sizeof(struct iphdr)+4)>>2;
344 pip->frag_off = htons(IP_DF);
346 pip->daddr = rt->rt_dst;
347 pip->saddr = rt->rt_src;
348 pip->protocol = IPPROTO_IGMP;
349 pip->tot_len = 0; /* filled in later */
350 ip_select_ident(pip, &rt->dst, NULL);
351 ((u8*)&pip[1])[0] = IPOPT_RA;
352 ((u8*)&pip[1])[1] = 4;
353 ((u8*)&pip[1])[2] = 0;
354 ((u8*)&pip[1])[3] = 0;
356 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
357 skb_put(skb, sizeof(*pig));
358 pig = igmpv3_report_hdr(skb);
359 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
367 static int igmpv3_sendpack(struct sk_buff *skb)
369 struct igmphdr *pig = igmp_hdr(skb);
370 const int igmplen = skb->tail - skb->transport_header;
372 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
374 return ip_local_out(skb);
377 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
379 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
382 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
383 int type, struct igmpv3_grec **ppgr)
385 struct net_device *dev = pmc->interface->dev;
386 struct igmpv3_report *pih;
387 struct igmpv3_grec *pgr;
390 skb = igmpv3_newpack(dev, dev->mtu);
393 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
394 pgr->grec_type = type;
395 pgr->grec_auxwords = 0;
397 pgr->grec_mca = pmc->multiaddr;
398 pih = igmpv3_report_hdr(skb);
399 pih->ngrec = htons(ntohs(pih->ngrec)+1);
404 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
405 skb_tailroom(skb)) : 0)
407 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
408 int type, int gdeleted, int sdeleted)
410 struct net_device *dev = pmc->interface->dev;
411 struct igmpv3_report *pih;
412 struct igmpv3_grec *pgr = NULL;
413 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
414 int scount, stotal, first, isquery, truncate;
416 if (pmc->multiaddr == IGMP_ALL_HOSTS)
419 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
420 type == IGMPV3_MODE_IS_EXCLUDE;
421 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
422 type == IGMPV3_CHANGE_TO_EXCLUDE;
426 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
431 pih = skb ? igmpv3_report_hdr(skb) : NULL;
433 /* EX and TO_EX get a fresh packet, if needed */
435 if (pih && pih->ngrec &&
436 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
438 igmpv3_sendpack(skb);
439 skb = igmpv3_newpack(dev, dev->mtu);
444 for (psf=*psf_list; psf; psf=psf_next) {
447 psf_next = psf->sf_next;
449 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
454 /* clear marks on query responses */
458 if (AVAILABLE(skb) < sizeof(__be32) +
459 first*sizeof(struct igmpv3_grec)) {
460 if (truncate && !first)
461 break; /* truncate these */
463 pgr->grec_nsrcs = htons(scount);
465 igmpv3_sendpack(skb);
466 skb = igmpv3_newpack(dev, dev->mtu);
471 skb = add_grhead(skb, pmc, type, &pgr);
476 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
477 *psrc = psf->sf_inaddr;
479 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
480 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
482 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
484 psf_prev->sf_next = psf->sf_next;
486 *psf_list = psf->sf_next;
496 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
497 type == IGMPV3_BLOCK_OLD_SOURCES)
499 if (pmc->crcount || isquery) {
500 /* make sure we have room for group header */
501 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
502 igmpv3_sendpack(skb);
503 skb = NULL; /* add_grhead will get a new one */
505 skb = add_grhead(skb, pmc, type, &pgr);
509 pgr->grec_nsrcs = htons(scount);
512 pmc->gsquery = 0; /* clear query state on report */
516 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
518 struct sk_buff *skb = NULL;
523 for_each_pmc_rcu(in_dev, pmc) {
524 if (pmc->multiaddr == IGMP_ALL_HOSTS)
526 spin_lock_bh(&pmc->lock);
527 if (pmc->sfcount[MCAST_EXCLUDE])
528 type = IGMPV3_MODE_IS_EXCLUDE;
530 type = IGMPV3_MODE_IS_INCLUDE;
531 skb = add_grec(skb, pmc, type, 0, 0);
532 spin_unlock_bh(&pmc->lock);
536 spin_lock_bh(&pmc->lock);
537 if (pmc->sfcount[MCAST_EXCLUDE])
538 type = IGMPV3_MODE_IS_EXCLUDE;
540 type = IGMPV3_MODE_IS_INCLUDE;
541 skb = add_grec(skb, pmc, type, 0, 0);
542 spin_unlock_bh(&pmc->lock);
546 return igmpv3_sendpack(skb);
550 * remove zero-count source records from a source filter list
552 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
554 struct ip_sf_list *psf_prev, *psf_next, *psf;
557 for (psf=*ppsf; psf; psf = psf_next) {
558 psf_next = psf->sf_next;
559 if (psf->sf_crcount == 0) {
561 psf_prev->sf_next = psf->sf_next;
563 *ppsf = psf->sf_next;
570 static void igmpv3_send_cr(struct in_device *in_dev)
572 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
573 struct sk_buff *skb = NULL;
577 spin_lock_bh(&in_dev->mc_tomb_lock);
581 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
582 pmc_next = pmc->next;
583 if (pmc->sfmode == MCAST_INCLUDE) {
584 type = IGMPV3_BLOCK_OLD_SOURCES;
585 dtype = IGMPV3_BLOCK_OLD_SOURCES;
586 skb = add_grec(skb, pmc, type, 1, 0);
587 skb = add_grec(skb, pmc, dtype, 1, 1);
590 if (pmc->sfmode == MCAST_EXCLUDE) {
591 type = IGMPV3_CHANGE_TO_INCLUDE;
592 skb = add_grec(skb, pmc, type, 1, 0);
595 if (pmc->crcount == 0) {
596 igmpv3_clear_zeros(&pmc->tomb);
597 igmpv3_clear_zeros(&pmc->sources);
600 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
602 pmc_prev->next = pmc_next;
604 in_dev->mc_tomb = pmc_next;
605 in_dev_put(pmc->interface);
610 spin_unlock_bh(&in_dev->mc_tomb_lock);
613 for_each_pmc_rcu(in_dev, pmc) {
614 spin_lock_bh(&pmc->lock);
615 if (pmc->sfcount[MCAST_EXCLUDE]) {
616 type = IGMPV3_BLOCK_OLD_SOURCES;
617 dtype = IGMPV3_ALLOW_NEW_SOURCES;
619 type = IGMPV3_ALLOW_NEW_SOURCES;
620 dtype = IGMPV3_BLOCK_OLD_SOURCES;
622 skb = add_grec(skb, pmc, type, 0, 0);
623 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
625 /* filter mode changes */
627 if (pmc->sfmode == MCAST_EXCLUDE)
628 type = IGMPV3_CHANGE_TO_EXCLUDE;
630 type = IGMPV3_CHANGE_TO_INCLUDE;
631 skb = add_grec(skb, pmc, type, 0, 0);
634 spin_unlock_bh(&pmc->lock);
640 (void) igmpv3_sendpack(skb);
643 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
650 struct net_device *dev = in_dev->dev;
651 struct net *net = dev_net(dev);
652 __be32 group = pmc ? pmc->multiaddr : 0;
655 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
656 return igmpv3_send_report(in_dev, pmc);
657 else if (type == IGMP_HOST_LEAVE_MESSAGE)
658 dst = IGMP_ALL_ROUTER;
662 rt = ip_route_output_ports(net, NULL, dst, 0,
664 IPPROTO_IGMP, 0, dev->ifindex);
668 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
674 skb_dst_set(skb, &rt->dst);
676 skb_reserve(skb, LL_RESERVED_SPACE(dev));
678 skb_reset_network_header(skb);
680 skb_put(skb, sizeof(struct iphdr) + 4);
683 iph->ihl = (sizeof(struct iphdr)+4)>>2;
685 iph->frag_off = htons(IP_DF);
688 iph->saddr = rt->rt_src;
689 iph->protocol = IPPROTO_IGMP;
690 ip_select_ident(iph, &rt->dst, NULL);
691 ((u8*)&iph[1])[0] = IPOPT_RA;
692 ((u8*)&iph[1])[1] = 4;
693 ((u8*)&iph[1])[2] = 0;
694 ((u8*)&iph[1])[3] = 0;
696 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
701 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
703 return ip_local_out(skb);
706 static void igmp_gq_timer_expire(unsigned long data)
708 struct in_device *in_dev = (struct in_device *)data;
710 in_dev->mr_gq_running = 0;
711 igmpv3_send_report(in_dev, NULL);
712 __in_dev_put(in_dev);
715 static void igmp_ifc_timer_expire(unsigned long data)
717 struct in_device *in_dev = (struct in_device *)data;
719 igmpv3_send_cr(in_dev);
720 if (in_dev->mr_ifc_count) {
721 in_dev->mr_ifc_count--;
722 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
724 __in_dev_put(in_dev);
727 static void igmp_ifc_event(struct in_device *in_dev)
729 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
731 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
732 IGMP_Unsolicited_Report_Count;
733 igmp_ifc_start_timer(in_dev, 1);
737 static void igmp_timer_expire(unsigned long data)
739 struct ip_mc_list *im=(struct ip_mc_list *)data;
740 struct in_device *in_dev = im->interface;
742 spin_lock(&im->lock);
745 if (im->unsolicit_count) {
746 im->unsolicit_count--;
747 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
750 spin_unlock(&im->lock);
752 if (IGMP_V1_SEEN(in_dev))
753 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
754 else if (IGMP_V2_SEEN(in_dev))
755 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
757 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
762 /* mark EXCLUDE-mode sources */
763 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
765 struct ip_sf_list *psf;
769 for (psf=pmc->sources; psf; psf=psf->sf_next) {
772 for (i=0; i<nsrcs; i++) {
773 /* skip inactive filters */
774 if (pmc->sfcount[MCAST_INCLUDE] ||
775 pmc->sfcount[MCAST_EXCLUDE] !=
776 psf->sf_count[MCAST_EXCLUDE])
778 if (srcs[i] == psf->sf_inaddr) {
785 if (scount == nsrcs) /* all sources excluded */
790 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
792 struct ip_sf_list *psf;
795 if (pmc->sfmode == MCAST_EXCLUDE)
796 return igmp_xmarksources(pmc, nsrcs, srcs);
798 /* mark INCLUDE-mode sources */
800 for (psf=pmc->sources; psf; psf=psf->sf_next) {
803 for (i=0; i<nsrcs; i++)
804 if (srcs[i] == psf->sf_inaddr) {
818 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
820 struct ip_mc_list *im;
822 /* Timers are only set for non-local groups */
824 if (group == IGMP_ALL_HOSTS)
828 for_each_pmc_rcu(in_dev, im) {
829 if (im->multiaddr == group) {
837 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
840 struct igmphdr *ih = igmp_hdr(skb);
841 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
842 struct ip_mc_list *im;
843 __be32 group = ih->group;
850 /* Alas, old v1 router presents here. */
852 max_delay = IGMP_Query_Response_Interval;
853 in_dev->mr_v1_seen = jiffies +
854 IGMP_V1_Router_Present_Timeout;
857 /* v2 router present */
858 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
859 in_dev->mr_v2_seen = jiffies +
860 IGMP_V2_Router_Present_Timeout;
862 /* cancel the interface change timer */
863 in_dev->mr_ifc_count = 0;
864 if (del_timer(&in_dev->mr_ifc_timer))
865 __in_dev_put(in_dev);
866 /* clear deleted report items */
867 igmpv3_clear_delrec(in_dev);
868 } else if (len < 12) {
869 return; /* ignore bogus packet; freed by caller */
870 } else if (IGMP_V1_SEEN(in_dev)) {
871 /* This is a v3 query with v1 queriers present */
872 max_delay = IGMP_Query_Response_Interval;
874 } else if (IGMP_V2_SEEN(in_dev)) {
875 /* this is a v3 query with v2 queriers present;
876 * Interpretation of the max_delay code is problematic here.
877 * A real v2 host would use ih_code directly, while v3 has a
878 * different encoding. We use the v3 encoding as more likely
879 * to be intended in a v3 query.
881 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
883 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
886 ih3 = igmpv3_query_hdr(skb);
888 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
889 + ntohs(ih3->nsrcs)*sizeof(__be32)))
891 ih3 = igmpv3_query_hdr(skb);
894 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
896 max_delay = 1; /* can't mod w/ 0 */
897 in_dev->mr_maxdelay = max_delay;
899 in_dev->mr_qrv = ih3->qrv;
900 if (!group) { /* general query */
902 return; /* no sources allowed */
903 igmp_gq_start_timer(in_dev);
906 /* mark sources to include, if group & source-specific */
907 mark = ih3->nsrcs != 0;
911 * - Start the timers in all of our membership records
912 * that the query applies to for the interface on
913 * which the query arrived excl. those that belong
914 * to a "local" group (224.0.0.X)
915 * - For timers already running check if they need to
917 * - Use the igmp->igmp_code field as the maximum
921 for_each_pmc_rcu(in_dev, im) {
924 if (group && group != im->multiaddr)
926 if (im->multiaddr == IGMP_ALL_HOSTS)
928 spin_lock_bh(&im->lock);
930 im->gsquery = im->gsquery && mark;
933 changed = !im->gsquery ||
934 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
935 spin_unlock_bh(&im->lock);
937 igmp_mod_timer(im, max_delay);
942 /* called in rcu_read_lock() section */
943 int igmp_rcv(struct sk_buff *skb)
945 /* This basically follows the spec line by line -- see RFC1112 */
947 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
953 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
956 switch (skb->ip_summed) {
957 case CHECKSUM_COMPLETE:
958 if (!csum_fold(skb->csum))
963 if (__skb_checksum_complete(skb))
969 case IGMP_HOST_MEMBERSHIP_QUERY:
970 igmp_heard_query(in_dev, skb, len);
972 case IGMP_HOST_MEMBERSHIP_REPORT:
973 case IGMPV2_HOST_MEMBERSHIP_REPORT:
974 /* Is it our report looped back? */
975 if (rt_is_output_route(skb_rtable(skb)))
977 /* don't rely on MC router hearing unicast reports */
978 if (skb->pkt_type == PACKET_MULTICAST ||
979 skb->pkt_type == PACKET_BROADCAST)
980 igmp_heard_report(in_dev, ih->group);
983 #ifdef CONFIG_IP_PIMSM_V1
984 return pim_rcv_v1(skb);
986 case IGMPV3_HOST_MEMBERSHIP_REPORT:
989 case IGMP_HOST_LEAVE_MESSAGE:
991 case IGMP_MTRACE_RESP:
1006 * Add a filter to a device
1009 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1011 char buf[MAX_ADDR_LEN];
1012 struct net_device *dev = in_dev->dev;
1014 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1015 We will get multicast token leakage, when IFF_MULTICAST
1016 is changed. This check should be done in dev->set_multicast_list
1017 routine. Something sort of:
1018 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1021 if (arp_mc_map(addr, buf, dev, 0) == 0)
1022 dev_mc_add(dev, buf);
1026 * Remove a filter from a device
1029 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1031 char buf[MAX_ADDR_LEN];
1032 struct net_device *dev = in_dev->dev;
1034 if (arp_mc_map(addr, buf, dev, 0) == 0)
1035 dev_mc_del(dev, buf);
1038 #ifdef CONFIG_IP_MULTICAST
1040 * deleted ip_mc_list manipulation
1042 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1044 struct ip_mc_list *pmc;
1046 /* this is an "ip_mc_list" for convenience; only the fields below
1047 * are actually used. In particular, the refcnt and users are not
1048 * used for management of the delete list. Using the same structure
1049 * for deleted items allows change reports to use common code with
1050 * non-deleted or query-response MCA's.
1052 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1055 spin_lock_bh(&im->lock);
1056 pmc->interface = im->interface;
1057 in_dev_hold(in_dev);
1058 pmc->multiaddr = im->multiaddr;
1059 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1060 IGMP_Unsolicited_Report_Count;
1061 pmc->sfmode = im->sfmode;
1062 if (pmc->sfmode == MCAST_INCLUDE) {
1063 struct ip_sf_list *psf;
1065 pmc->tomb = im->tomb;
1066 pmc->sources = im->sources;
1067 im->tomb = im->sources = NULL;
1068 for (psf=pmc->sources; psf; psf=psf->sf_next)
1069 psf->sf_crcount = pmc->crcount;
1071 spin_unlock_bh(&im->lock);
1073 spin_lock_bh(&in_dev->mc_tomb_lock);
1074 pmc->next = in_dev->mc_tomb;
1075 in_dev->mc_tomb = pmc;
1076 spin_unlock_bh(&in_dev->mc_tomb_lock);
1079 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1081 struct ip_mc_list *pmc, *pmc_prev;
1082 struct ip_sf_list *psf, *psf_next;
1084 spin_lock_bh(&in_dev->mc_tomb_lock);
1086 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1087 if (pmc->multiaddr == multiaddr)
1093 pmc_prev->next = pmc->next;
1095 in_dev->mc_tomb = pmc->next;
1097 spin_unlock_bh(&in_dev->mc_tomb_lock);
1099 for (psf=pmc->tomb; psf; psf=psf_next) {
1100 psf_next = psf->sf_next;
1103 in_dev_put(pmc->interface);
1108 static void igmpv3_clear_delrec(struct in_device *in_dev)
1110 struct ip_mc_list *pmc, *nextpmc;
1112 spin_lock_bh(&in_dev->mc_tomb_lock);
1113 pmc = in_dev->mc_tomb;
1114 in_dev->mc_tomb = NULL;
1115 spin_unlock_bh(&in_dev->mc_tomb_lock);
1117 for (; pmc; pmc = nextpmc) {
1118 nextpmc = pmc->next;
1119 ip_mc_clear_src(pmc);
1120 in_dev_put(pmc->interface);
1123 /* clear dead sources, too */
1125 for_each_pmc_rcu(in_dev, pmc) {
1126 struct ip_sf_list *psf, *psf_next;
1128 spin_lock_bh(&pmc->lock);
1131 spin_unlock_bh(&pmc->lock);
1132 for (; psf; psf=psf_next) {
1133 psf_next = psf->sf_next;
1141 static void igmp_group_dropped(struct ip_mc_list *im)
1143 struct in_device *in_dev = im->interface;
1144 #ifdef CONFIG_IP_MULTICAST
1150 ip_mc_filter_del(in_dev, im->multiaddr);
1153 #ifdef CONFIG_IP_MULTICAST
1154 if (im->multiaddr == IGMP_ALL_HOSTS)
1157 reporter = im->reporter;
1158 igmp_stop_timer(im);
1160 if (!in_dev->dead) {
1161 if (IGMP_V1_SEEN(in_dev))
1163 if (IGMP_V2_SEEN(in_dev)) {
1165 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1169 igmpv3_add_delrec(in_dev, im);
1171 igmp_ifc_event(in_dev);
1175 ip_mc_clear_src(im);
1178 static void igmp_group_added(struct ip_mc_list *im)
1180 struct in_device *in_dev = im->interface;
1182 if (im->loaded == 0) {
1184 ip_mc_filter_add(in_dev, im->multiaddr);
1187 #ifdef CONFIG_IP_MULTICAST
1188 if (im->multiaddr == IGMP_ALL_HOSTS)
1193 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1194 spin_lock_bh(&im->lock);
1195 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1196 spin_unlock_bh(&im->lock);
1201 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1202 IGMP_Unsolicited_Report_Count;
1203 igmp_ifc_event(in_dev);
1209 * Multicast list managers
1214 * A socket has joined a multicast group on device dev.
1217 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1219 struct ip_mc_list *im;
1223 for_each_pmc_rtnl(in_dev, im) {
1224 if (im->multiaddr == addr) {
1226 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1231 im = kzalloc(sizeof(*im), GFP_KERNEL);
1236 im->interface = in_dev;
1237 in_dev_hold(in_dev);
1238 im->multiaddr = addr;
1239 /* initial mode is (EX, empty) */
1240 im->sfmode = MCAST_EXCLUDE;
1241 im->sfcount[MCAST_EXCLUDE] = 1;
1242 atomic_set(&im->refcnt, 1);
1243 spin_lock_init(&im->lock);
1244 #ifdef CONFIG_IP_MULTICAST
1245 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1246 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1249 im->next_rcu = in_dev->mc_list;
1251 rcu_assign_pointer(in_dev->mc_list, im);
1253 #ifdef CONFIG_IP_MULTICAST
1254 igmpv3_del_delrec(in_dev, im->multiaddr);
1256 igmp_group_added(im);
1258 ip_rt_multicast_event(in_dev);
1262 EXPORT_SYMBOL(ip_mc_inc_group);
1265 * Resend IGMP JOIN report; used for bonding.
1266 * Called with rcu_read_lock()
1268 void ip_mc_rejoin_groups(struct in_device *in_dev)
1270 #ifdef CONFIG_IP_MULTICAST
1271 struct ip_mc_list *im;
1274 for_each_pmc_rcu(in_dev, im) {
1275 if (im->multiaddr == IGMP_ALL_HOSTS)
1278 /* a failover is happening and switches
1279 * must be notified immediately
1281 if (IGMP_V1_SEEN(in_dev))
1282 type = IGMP_HOST_MEMBERSHIP_REPORT;
1283 else if (IGMP_V2_SEEN(in_dev))
1284 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1286 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1287 igmp_send_report(in_dev, im, type);
1291 EXPORT_SYMBOL(ip_mc_rejoin_groups);
1294 * A socket has left a multicast group on device dev
1297 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1299 struct ip_mc_list *i;
1300 struct ip_mc_list __rcu **ip;
1304 for (ip = &in_dev->mc_list;
1305 (i = rtnl_dereference(*ip)) != NULL;
1306 ip = &i->next_rcu) {
1307 if (i->multiaddr == addr) {
1308 if (--i->users == 0) {
1311 igmp_group_dropped(i);
1314 ip_rt_multicast_event(in_dev);
1323 EXPORT_SYMBOL(ip_mc_dec_group);
1325 /* Device changing type */
1327 void ip_mc_unmap(struct in_device *in_dev)
1329 struct ip_mc_list *pmc;
1333 for_each_pmc_rtnl(in_dev, pmc)
1334 igmp_group_dropped(pmc);
1337 void ip_mc_remap(struct in_device *in_dev)
1339 struct ip_mc_list *pmc;
1343 for_each_pmc_rtnl(in_dev, pmc)
1344 igmp_group_added(pmc);
1347 /* Device going down */
1349 void ip_mc_down(struct in_device *in_dev)
1351 struct ip_mc_list *pmc;
1355 for_each_pmc_rtnl(in_dev, pmc)
1356 igmp_group_dropped(pmc);
1358 #ifdef CONFIG_IP_MULTICAST
1359 in_dev->mr_ifc_count = 0;
1360 if (del_timer(&in_dev->mr_ifc_timer))
1361 __in_dev_put(in_dev);
1362 in_dev->mr_gq_running = 0;
1363 if (del_timer(&in_dev->mr_gq_timer))
1364 __in_dev_put(in_dev);
1365 igmpv3_clear_delrec(in_dev);
1368 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1371 void ip_mc_init_dev(struct in_device *in_dev)
1375 in_dev->mc_tomb = NULL;
1376 #ifdef CONFIG_IP_MULTICAST
1377 in_dev->mr_gq_running = 0;
1378 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1379 (unsigned long)in_dev);
1380 in_dev->mr_ifc_count = 0;
1381 in_dev->mc_count = 0;
1382 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1383 (unsigned long)in_dev);
1384 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1387 spin_lock_init(&in_dev->mc_tomb_lock);
1390 /* Device going up */
1392 void ip_mc_up(struct in_device *in_dev)
1394 struct ip_mc_list *pmc;
1398 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1400 for_each_pmc_rtnl(in_dev, pmc)
1401 igmp_group_added(pmc);
1405 * Device is about to be destroyed: clean up.
1408 void ip_mc_destroy_dev(struct in_device *in_dev)
1410 struct ip_mc_list *i;
1414 /* Deactivate timers */
1417 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1418 in_dev->mc_list = i->next_rcu;
1421 igmp_group_dropped(i);
1426 /* RTNL is locked */
1427 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1429 struct net_device *dev = NULL;
1430 struct in_device *idev = NULL;
1432 if (imr->imr_ifindex) {
1433 idev = inetdev_by_index(net, imr->imr_ifindex);
1436 if (imr->imr_address.s_addr) {
1437 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1443 struct rtable *rt = ip_route_output(net,
1444 imr->imr_multiaddr.s_addr,
1452 imr->imr_ifindex = dev->ifindex;
1453 idev = __in_dev_get_rtnl(dev);
1459 * Join a socket to a group
1461 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1462 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1465 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1468 struct ip_sf_list *psf, *psf_prev;
1472 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1473 if (psf->sf_inaddr == *psfsrc)
1477 if (!psf || psf->sf_count[sfmode] == 0) {
1478 /* source filter not found, or count wrong => bug */
1481 psf->sf_count[sfmode]--;
1482 if (psf->sf_count[sfmode] == 0) {
1483 ip_rt_multicast_event(pmc->interface);
1485 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1486 #ifdef CONFIG_IP_MULTICAST
1487 struct in_device *in_dev = pmc->interface;
1490 /* no more filters for this source */
1492 psf_prev->sf_next = psf->sf_next;
1494 pmc->sources = psf->sf_next;
1495 #ifdef CONFIG_IP_MULTICAST
1496 if (psf->sf_oldin &&
1497 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1498 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1499 IGMP_Unsolicited_Report_Count;
1500 psf->sf_next = pmc->tomb;
1510 #ifndef CONFIG_IP_MULTICAST
1511 #define igmp_ifc_event(x) do { } while (0)
1514 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1515 int sfcount, __be32 *psfsrc, int delta)
1517 struct ip_mc_list *pmc;
1524 for_each_pmc_rcu(in_dev, pmc) {
1525 if (*pmca == pmc->multiaddr)
1529 /* MCA not found?? bug */
1533 spin_lock_bh(&pmc->lock);
1535 #ifdef CONFIG_IP_MULTICAST
1540 if (!pmc->sfcount[sfmode])
1542 pmc->sfcount[sfmode]--;
1545 for (i=0; i<sfcount; i++) {
1546 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1548 changerec |= rv > 0;
1552 if (pmc->sfmode == MCAST_EXCLUDE &&
1553 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1554 pmc->sfcount[MCAST_INCLUDE]) {
1555 #ifdef CONFIG_IP_MULTICAST
1556 struct ip_sf_list *psf;
1559 /* filter mode change */
1560 pmc->sfmode = MCAST_INCLUDE;
1561 #ifdef CONFIG_IP_MULTICAST
1562 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1563 IGMP_Unsolicited_Report_Count;
1564 in_dev->mr_ifc_count = pmc->crcount;
1565 for (psf=pmc->sources; psf; psf = psf->sf_next)
1566 psf->sf_crcount = 0;
1567 igmp_ifc_event(pmc->interface);
1568 } else if (sf_setstate(pmc) || changerec) {
1569 igmp_ifc_event(pmc->interface);
1573 spin_unlock_bh(&pmc->lock);
1578 * Add multicast single-source filter to the interface list
1580 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1581 __be32 *psfsrc, int delta)
1583 struct ip_sf_list *psf, *psf_prev;
1586 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1587 if (psf->sf_inaddr == *psfsrc)
1592 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1595 psf->sf_inaddr = *psfsrc;
1597 psf_prev->sf_next = psf;
1601 psf->sf_count[sfmode]++;
1602 if (psf->sf_count[sfmode] == 1) {
1603 ip_rt_multicast_event(pmc->interface);
1608 #ifdef CONFIG_IP_MULTICAST
1609 static void sf_markstate(struct ip_mc_list *pmc)
1611 struct ip_sf_list *psf;
1612 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1614 for (psf=pmc->sources; psf; psf=psf->sf_next)
1615 if (pmc->sfcount[MCAST_EXCLUDE]) {
1616 psf->sf_oldin = mca_xcount ==
1617 psf->sf_count[MCAST_EXCLUDE] &&
1618 !psf->sf_count[MCAST_INCLUDE];
1620 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1623 static int sf_setstate(struct ip_mc_list *pmc)
1625 struct ip_sf_list *psf, *dpsf;
1626 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1627 int qrv = pmc->interface->mr_qrv;
1631 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1632 if (pmc->sfcount[MCAST_EXCLUDE]) {
1633 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1634 !psf->sf_count[MCAST_INCLUDE];
1636 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1638 if (!psf->sf_oldin) {
1639 struct ip_sf_list *prev = NULL;
1641 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1642 if (dpsf->sf_inaddr == psf->sf_inaddr)
1648 prev->sf_next = dpsf->sf_next;
1650 pmc->tomb = dpsf->sf_next;
1653 psf->sf_crcount = qrv;
1656 } else if (psf->sf_oldin) {
1658 psf->sf_crcount = 0;
1660 * add or update "delete" records if an active filter
1663 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1664 if (dpsf->sf_inaddr == psf->sf_inaddr)
1667 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1671 /* pmc->lock held by callers */
1672 dpsf->sf_next = pmc->tomb;
1675 dpsf->sf_crcount = qrv;
1684 * Add multicast source filter list to the interface list
1686 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1687 int sfcount, __be32 *psfsrc, int delta)
1689 struct ip_mc_list *pmc;
1696 for_each_pmc_rcu(in_dev, pmc) {
1697 if (*pmca == pmc->multiaddr)
1701 /* MCA not found?? bug */
1705 spin_lock_bh(&pmc->lock);
1708 #ifdef CONFIG_IP_MULTICAST
1711 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1713 pmc->sfcount[sfmode]++;
1715 for (i=0; i<sfcount; i++) {
1716 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1723 pmc->sfcount[sfmode]--;
1725 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1726 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1727 #ifdef CONFIG_IP_MULTICAST
1728 struct ip_sf_list *psf;
1729 in_dev = pmc->interface;
1732 /* filter mode change */
1733 if (pmc->sfcount[MCAST_EXCLUDE])
1734 pmc->sfmode = MCAST_EXCLUDE;
1735 else if (pmc->sfcount[MCAST_INCLUDE])
1736 pmc->sfmode = MCAST_INCLUDE;
1737 #ifdef CONFIG_IP_MULTICAST
1738 /* else no filters; keep old mode for reports */
1740 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1741 IGMP_Unsolicited_Report_Count;
1742 in_dev->mr_ifc_count = pmc->crcount;
1743 for (psf=pmc->sources; psf; psf = psf->sf_next)
1744 psf->sf_crcount = 0;
1745 igmp_ifc_event(in_dev);
1746 } else if (sf_setstate(pmc)) {
1747 igmp_ifc_event(in_dev);
1750 spin_unlock_bh(&pmc->lock);
1754 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1756 struct ip_sf_list *psf, *nextpsf;
1758 for (psf=pmc->tomb; psf; psf=nextpsf) {
1759 nextpsf = psf->sf_next;
1763 for (psf=pmc->sources; psf; psf=nextpsf) {
1764 nextpsf = psf->sf_next;
1767 pmc->sources = NULL;
1768 pmc->sfmode = MCAST_EXCLUDE;
1769 pmc->sfcount[MCAST_INCLUDE] = 0;
1770 pmc->sfcount[MCAST_EXCLUDE] = 1;
1775 * Join a multicast group
1777 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1780 __be32 addr = imr->imr_multiaddr.s_addr;
1781 struct ip_mc_socklist *iml = NULL, *i;
1782 struct in_device *in_dev;
1783 struct inet_sock *inet = inet_sk(sk);
1784 struct net *net = sock_net(sk);
1788 if (!ipv4_is_multicast(addr))
1793 in_dev = ip_mc_find_dev(net, imr);
1802 ifindex = imr->imr_ifindex;
1803 for_each_pmc_rtnl(inet, i) {
1804 if (i->multi.imr_multiaddr.s_addr == addr &&
1805 i->multi.imr_ifindex == ifindex)
1810 if (count >= sysctl_igmp_max_memberships)
1812 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1816 memcpy(&iml->multi, imr, sizeof(*imr));
1817 iml->next_rcu = inet->mc_list;
1819 iml->sfmode = MCAST_EXCLUDE;
1820 rcu_assign_pointer(inet->mc_list, iml);
1821 ip_mc_inc_group(in_dev, addr);
1827 EXPORT_SYMBOL(ip_mc_join_group);
1829 static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1831 kfree(container_of(rp, struct ip_sf_socklist, rcu));
1832 /* sk_omem_alloc should have been decreased by the caller*/
1835 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1836 struct in_device *in_dev)
1838 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1842 /* any-source empty exclude case */
1843 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1844 iml->sfmode, 0, NULL, 0);
1846 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1847 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1848 rcu_assign_pointer(iml->sflist, NULL);
1849 /* decrease mem now to avoid the memleak warning */
1850 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1851 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
1856 static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1858 kfree(container_of(rp, struct ip_mc_socklist, rcu));
1859 /* sk_omem_alloc should have been decreased by the caller*/
1864 * Ask a socket to leave a group.
1867 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1869 struct inet_sock *inet = inet_sk(sk);
1870 struct ip_mc_socklist *iml;
1871 struct ip_mc_socklist __rcu **imlp;
1872 struct in_device *in_dev;
1873 struct net *net = sock_net(sk);
1874 __be32 group = imr->imr_multiaddr.s_addr;
1876 int ret = -EADDRNOTAVAIL;
1879 in_dev = ip_mc_find_dev(net, imr);
1880 ifindex = imr->imr_ifindex;
1881 for (imlp = &inet->mc_list;
1882 (iml = rtnl_dereference(*imlp)) != NULL;
1883 imlp = &iml->next_rcu) {
1884 if (iml->multi.imr_multiaddr.s_addr != group)
1887 if (iml->multi.imr_ifindex != ifindex)
1889 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1890 iml->multi.imr_address.s_addr)
1893 (void) ip_mc_leave_src(sk, iml, in_dev);
1895 *imlp = iml->next_rcu;
1898 ip_mc_dec_group(in_dev, group);
1900 /* decrease mem now to avoid the memleak warning */
1901 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1902 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
1911 int ip_mc_source(int add, int omode, struct sock *sk, struct
1912 ip_mreq_source *mreqs, int ifindex)
1915 struct ip_mreqn imr;
1916 __be32 addr = mreqs->imr_multiaddr;
1917 struct ip_mc_socklist *pmc;
1918 struct in_device *in_dev = NULL;
1919 struct inet_sock *inet = inet_sk(sk);
1920 struct ip_sf_socklist *psl;
1921 struct net *net = sock_net(sk);
1925 if (!ipv4_is_multicast(addr))
1930 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1931 imr.imr_address.s_addr = mreqs->imr_interface;
1932 imr.imr_ifindex = ifindex;
1933 in_dev = ip_mc_find_dev(net, &imr);
1939 err = -EADDRNOTAVAIL;
1941 for_each_pmc_rtnl(inet, pmc) {
1942 if ((pmc->multi.imr_multiaddr.s_addr ==
1943 imr.imr_multiaddr.s_addr) &&
1944 (pmc->multi.imr_ifindex == imr.imr_ifindex))
1947 if (!pmc) { /* must have a prior join */
1951 /* if a source filter was set, must be the same mode as before */
1953 if (pmc->sfmode != omode) {
1957 } else if (pmc->sfmode != omode) {
1958 /* allow mode switches for empty-set filters */
1959 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1960 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1962 pmc->sfmode = omode;
1965 psl = rtnl_dereference(pmc->sflist);
1968 goto done; /* err = -EADDRNOTAVAIL */
1970 for (i=0; i<psl->sl_count; i++) {
1971 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1976 if (rv) /* source not found */
1977 goto done; /* err = -EADDRNOTAVAIL */
1979 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1980 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1985 /* update the interface filter */
1986 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1987 &mreqs->imr_sourceaddr, 1);
1989 for (j=i+1; j<psl->sl_count; j++)
1990 psl->sl_addr[j-1] = psl->sl_addr[j];
1995 /* else, add a new source to the filter */
1997 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2001 if (!psl || psl->sl_count == psl->sl_max) {
2002 struct ip_sf_socklist *newpsl;
2003 int count = IP_SFBLOCK;
2006 count += psl->sl_max;
2007 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2012 newpsl->sl_max = count;
2013 newpsl->sl_count = count - IP_SFBLOCK;
2015 for (i=0; i<psl->sl_count; i++)
2016 newpsl->sl_addr[i] = psl->sl_addr[i];
2017 /* decrease mem now to avoid the memleak warning */
2018 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2019 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2021 rcu_assign_pointer(pmc->sflist, newpsl);
2024 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2025 for (i=0; i<psl->sl_count; i++) {
2026 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2031 if (rv == 0) /* address already there is an error */
2033 for (j=psl->sl_count-1; j>=i; j--)
2034 psl->sl_addr[j+1] = psl->sl_addr[j];
2035 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2038 /* update the interface list */
2039 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2040 &mreqs->imr_sourceaddr, 1);
2044 return ip_mc_leave_group(sk, &imr);
2048 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2051 struct ip_mreqn imr;
2052 __be32 addr = msf->imsf_multiaddr;
2053 struct ip_mc_socklist *pmc;
2054 struct in_device *in_dev;
2055 struct inet_sock *inet = inet_sk(sk);
2056 struct ip_sf_socklist *newpsl, *psl;
2057 struct net *net = sock_net(sk);
2060 if (!ipv4_is_multicast(addr))
2062 if (msf->imsf_fmode != MCAST_INCLUDE &&
2063 msf->imsf_fmode != MCAST_EXCLUDE)
2068 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2069 imr.imr_address.s_addr = msf->imsf_interface;
2070 imr.imr_ifindex = ifindex;
2071 in_dev = ip_mc_find_dev(net, &imr);
2078 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2079 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2084 for_each_pmc_rtnl(inet, pmc) {
2085 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2086 pmc->multi.imr_ifindex == imr.imr_ifindex)
2089 if (!pmc) { /* must have a prior join */
2093 if (msf->imsf_numsrc) {
2094 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2100 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2101 memcpy(newpsl->sl_addr, msf->imsf_slist,
2102 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2103 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2104 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2106 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2111 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2112 msf->imsf_fmode, 0, NULL, 0);
2114 psl = rtnl_dereference(pmc->sflist);
2116 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2117 psl->sl_count, psl->sl_addr, 0);
2118 /* decrease mem now to avoid the memleak warning */
2119 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2120 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
2122 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2124 rcu_assign_pointer(pmc->sflist, newpsl);
2125 pmc->sfmode = msf->imsf_fmode;
2130 err = ip_mc_leave_group(sk, &imr);
2134 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2135 struct ip_msfilter __user *optval, int __user *optlen)
2137 int err, len, count, copycount;
2138 struct ip_mreqn imr;
2139 __be32 addr = msf->imsf_multiaddr;
2140 struct ip_mc_socklist *pmc;
2141 struct in_device *in_dev;
2142 struct inet_sock *inet = inet_sk(sk);
2143 struct ip_sf_socklist *psl;
2144 struct net *net = sock_net(sk);
2146 if (!ipv4_is_multicast(addr))
2151 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2152 imr.imr_address.s_addr = msf->imsf_interface;
2153 imr.imr_ifindex = 0;
2154 in_dev = ip_mc_find_dev(net, &imr);
2160 err = -EADDRNOTAVAIL;
2162 for_each_pmc_rtnl(inet, pmc) {
2163 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2164 pmc->multi.imr_ifindex == imr.imr_ifindex)
2167 if (!pmc) /* must have a prior join */
2169 msf->imsf_fmode = pmc->sfmode;
2170 psl = rtnl_dereference(pmc->sflist);
2176 count = psl->sl_count;
2178 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2179 len = copycount * sizeof(psl->sl_addr[0]);
2180 msf->imsf_numsrc = count;
2181 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2182 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2186 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2194 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2195 struct group_filter __user *optval, int __user *optlen)
2197 int err, i, count, copycount;
2198 struct sockaddr_in *psin;
2200 struct ip_mc_socklist *pmc;
2201 struct inet_sock *inet = inet_sk(sk);
2202 struct ip_sf_socklist *psl;
2204 psin = (struct sockaddr_in *)&gsf->gf_group;
2205 if (psin->sin_family != AF_INET)
2207 addr = psin->sin_addr.s_addr;
2208 if (!ipv4_is_multicast(addr))
2213 err = -EADDRNOTAVAIL;
2215 for_each_pmc_rtnl(inet, pmc) {
2216 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2217 pmc->multi.imr_ifindex == gsf->gf_interface)
2220 if (!pmc) /* must have a prior join */
2222 gsf->gf_fmode = pmc->sfmode;
2223 psl = rtnl_dereference(pmc->sflist);
2225 count = psl ? psl->sl_count : 0;
2226 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2227 gsf->gf_numsrc = count;
2228 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2229 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2232 for (i=0; i<copycount; i++) {
2233 struct sockaddr_storage ss;
2235 psin = (struct sockaddr_in *)&ss;
2236 memset(&ss, 0, sizeof(ss));
2237 psin->sin_family = AF_INET;
2238 psin->sin_addr.s_addr = psl->sl_addr[i];
2239 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2249 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2251 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2253 struct inet_sock *inet = inet_sk(sk);
2254 struct ip_mc_socklist *pmc;
2255 struct ip_sf_socklist *psl;
2260 if (!ipv4_is_multicast(loc_addr))
2264 for_each_pmc_rcu(inet, pmc) {
2265 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2266 pmc->multi.imr_ifindex == dif)
2272 psl = rcu_dereference(pmc->sflist);
2273 ret = (pmc->sfmode == MCAST_EXCLUDE);
2277 for (i=0; i<psl->sl_count; i++) {
2278 if (psl->sl_addr[i] == rmt_addr)
2282 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2284 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2294 * A socket is closing.
2297 void ip_mc_drop_socket(struct sock *sk)
2299 struct inet_sock *inet = inet_sk(sk);
2300 struct ip_mc_socklist *iml;
2301 struct net *net = sock_net(sk);
2303 if (inet->mc_list == NULL)
2307 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2308 struct in_device *in_dev;
2310 inet->mc_list = iml->next_rcu;
2311 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2312 (void) ip_mc_leave_src(sk, iml, in_dev);
2314 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2315 /* decrease mem now to avoid the memleak warning */
2316 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2317 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
2322 /* called with rcu_read_lock() */
2323 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2325 struct ip_mc_list *im;
2326 struct ip_sf_list *psf;
2329 for_each_pmc_rcu(in_dev, im) {
2330 if (im->multiaddr == mc_addr)
2333 if (im && proto == IPPROTO_IGMP) {
2337 for (psf=im->sources; psf; psf=psf->sf_next) {
2338 if (psf->sf_inaddr == src_addr)
2342 rv = psf->sf_count[MCAST_INCLUDE] ||
2343 psf->sf_count[MCAST_EXCLUDE] !=
2344 im->sfcount[MCAST_EXCLUDE];
2346 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2348 rv = 1; /* unspecified source; tentatively allow */
2353 #if defined(CONFIG_PROC_FS)
2354 struct igmp_mc_iter_state {
2355 struct seq_net_private p;
2356 struct net_device *dev;
2357 struct in_device *in_dev;
2360 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2362 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2364 struct net *net = seq_file_net(seq);
2365 struct ip_mc_list *im = NULL;
2366 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2368 state->in_dev = NULL;
2369 for_each_netdev_rcu(net, state->dev) {
2370 struct in_device *in_dev;
2372 in_dev = __in_dev_get_rcu(state->dev);
2375 im = rcu_dereference(in_dev->mc_list);
2377 state->in_dev = in_dev;
2384 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2386 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2388 im = rcu_dereference(im->next_rcu);
2390 state->dev = next_net_device_rcu(state->dev);
2392 state->in_dev = NULL;
2395 state->in_dev = __in_dev_get_rcu(state->dev);
2398 im = rcu_dereference(state->in_dev->mc_list);
2403 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2405 struct ip_mc_list *im = igmp_mc_get_first(seq);
2407 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2409 return pos ? NULL : im;
2412 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2416 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2419 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2421 struct ip_mc_list *im;
2422 if (v == SEQ_START_TOKEN)
2423 im = igmp_mc_get_first(seq);
2425 im = igmp_mc_get_next(seq, v);
2430 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2433 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2435 state->in_dev = NULL;
2440 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2442 if (v == SEQ_START_TOKEN)
2444 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2446 struct ip_mc_list *im = (struct ip_mc_list *)v;
2447 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2449 #ifdef CONFIG_IP_MULTICAST
2450 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2451 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2457 if (rcu_dereference(state->in_dev->mc_list) == im) {
2458 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2459 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2463 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2464 im->multiaddr, im->users,
2465 im->tm_running, im->tm_running ?
2466 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2472 static const struct seq_operations igmp_mc_seq_ops = {
2473 .start = igmp_mc_seq_start,
2474 .next = igmp_mc_seq_next,
2475 .stop = igmp_mc_seq_stop,
2476 .show = igmp_mc_seq_show,
2479 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2481 return seq_open_net(inode, file, &igmp_mc_seq_ops,
2482 sizeof(struct igmp_mc_iter_state));
2485 static const struct file_operations igmp_mc_seq_fops = {
2486 .owner = THIS_MODULE,
2487 .open = igmp_mc_seq_open,
2489 .llseek = seq_lseek,
2490 .release = seq_release_net,
2493 struct igmp_mcf_iter_state {
2494 struct seq_net_private p;
2495 struct net_device *dev;
2496 struct in_device *idev;
2497 struct ip_mc_list *im;
2500 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2502 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2504 struct net *net = seq_file_net(seq);
2505 struct ip_sf_list *psf = NULL;
2506 struct ip_mc_list *im = NULL;
2507 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2511 for_each_netdev_rcu(net, state->dev) {
2512 struct in_device *idev;
2513 idev = __in_dev_get_rcu(state->dev);
2514 if (unlikely(idev == NULL))
2516 im = rcu_dereference(idev->mc_list);
2517 if (likely(im != NULL)) {
2518 spin_lock_bh(&im->lock);
2520 if (likely(psf != NULL)) {
2525 spin_unlock_bh(&im->lock);
2531 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2533 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2537 spin_unlock_bh(&state->im->lock);
2538 state->im = state->im->next;
2539 while (!state->im) {
2540 state->dev = next_net_device_rcu(state->dev);
2545 state->idev = __in_dev_get_rcu(state->dev);
2548 state->im = rcu_dereference(state->idev->mc_list);
2552 spin_lock_bh(&state->im->lock);
2553 psf = state->im->sources;
2559 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2561 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2563 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2565 return pos ? NULL : psf;
2568 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2572 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2575 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2577 struct ip_sf_list *psf;
2578 if (v == SEQ_START_TOKEN)
2579 psf = igmp_mcf_get_first(seq);
2581 psf = igmp_mcf_get_next(seq, v);
2586 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2589 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2590 if (likely(state->im != NULL)) {
2591 spin_unlock_bh(&state->im->lock);
2599 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2601 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2602 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2604 if (v == SEQ_START_TOKEN) {
2607 "%10s %10s %6s %6s\n", "Idx",
2609 "SRC", "INC", "EXC");
2613 "0x%08x %6lu %6lu\n",
2614 state->dev->ifindex, state->dev->name,
2615 ntohl(state->im->multiaddr),
2616 ntohl(psf->sf_inaddr),
2617 psf->sf_count[MCAST_INCLUDE],
2618 psf->sf_count[MCAST_EXCLUDE]);
2623 static const struct seq_operations igmp_mcf_seq_ops = {
2624 .start = igmp_mcf_seq_start,
2625 .next = igmp_mcf_seq_next,
2626 .stop = igmp_mcf_seq_stop,
2627 .show = igmp_mcf_seq_show,
2630 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2632 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2633 sizeof(struct igmp_mcf_iter_state));
2636 static const struct file_operations igmp_mcf_seq_fops = {
2637 .owner = THIS_MODULE,
2638 .open = igmp_mcf_seq_open,
2640 .llseek = seq_lseek,
2641 .release = seq_release_net,
2644 static int __net_init igmp_net_init(struct net *net)
2646 struct proc_dir_entry *pde;
2648 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2651 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2657 proc_net_remove(net, "igmp");
2662 static void __net_exit igmp_net_exit(struct net *net)
2664 proc_net_remove(net, "mcfilter");
2665 proc_net_remove(net, "igmp");
2668 static struct pernet_operations igmp_net_ops = {
2669 .init = igmp_net_init,
2670 .exit = igmp_net_exit,
2673 int __init igmp_mc_proc_init(void)
2675 return register_pernet_subsys(&igmp_net_ops);