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.
11 * Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
14 * Alan Cox <Alan.Cox@linux.org>
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
23 * Alan Cox : Added lots of __inline__ to optimise
24 * the memory usage of all the tiny little
26 * Alan Cox : Dumped the header building experiment.
27 * Alan Cox : Minor tweaks ready for multicast routing
28 * and extended IGMP protocol.
29 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
30 * writes utterly bogus code otherwise (sigh)
31 * fixed IGMP loopback to behave in the manner
32 * desired by mrouted, fixed the fact it has been
33 * broken since 1.3.6 and cleaned up a few minor
36 * Chih-Jen Chang : Tried to revise IGMP to Version 2
37 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
38 * The enhancements are mainly based on Steve Deering's
39 * ipmulti-3.5 source code.
40 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
41 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
42 * the mrouted version on that device.
43 * Chih-Jen Chang : Added the max_resp_time parameter to
44 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
45 * to identify the multicast router version
46 * and do what the IGMP version 2 specified.
47 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
48 * Tsu-Sheng Tsao if the specified time expired.
49 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
50 * Alan Cox : Use GFP_ATOMIC in the right places.
51 * Christian Daudt : igmp timer wasn't set for local group
52 * memberships but was being deleted,
53 * which caused a "del_timer() called
54 * from %p with timer not initialized\n"
56 * Christian Daudt : removed del_timer from
57 * igmp_timer_expire function (960205).
58 * Christian Daudt : igmp_heard_report now only calls
59 * igmp_timer_expire if tm->running is
61 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
62 * igmp_heard_query never trigger. Expiry
63 * miscalculation fixed in igmp_heard_query
64 * and random() made to return unsigned to
65 * prevent negative expiry times.
66 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
67 * fix from pending 2.1.x patches.
68 * Alan Cox: Forget to enable FDDI support earlier.
69 * Alexey Kuznetsov: Fixed leaving groups on device down.
70 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
71 * David L Stevens: IGMPv3 support, with help from
75 #include <linux/module.h>
76 #include <asm/uaccess.h>
77 #include <asm/system.h>
78 #include <linux/types.h>
79 #include <linux/kernel.h>
80 #include <linux/jiffies.h>
81 #include <linux/string.h>
82 #include <linux/socket.h>
83 #include <linux/sockios.h>
85 #include <linux/inet.h>
86 #include <linux/netdevice.h>
87 #include <linux/skbuff.h>
88 #include <linux/inetdevice.h>
89 #include <linux/igmp.h>
90 #include <linux/if_arp.h>
91 #include <linux/rtnetlink.h>
92 #include <linux/times.h>
94 #include <net/net_namespace.h>
97 #include <net/protocol.h>
98 #include <net/route.h>
100 #include <net/checksum.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
110 #define IP_MAX_MEMBERSHIPS 20
111 #define IP_MAX_MSF 10
113 #ifdef CONFIG_IP_MULTICAST
114 /* Parameter names and values are taken from igmp-v2-06 draft */
116 #define IGMP_V1_Router_Present_Timeout (400*HZ)
117 #define IGMP_V2_Router_Present_Timeout (400*HZ)
118 #define IGMP_Unsolicited_Report_Interval (10*HZ)
119 #define IGMP_Query_Response_Interval (10*HZ)
120 #define IGMP_Unsolicited_Report_Count 2
123 #define IGMP_Initial_Report_Delay (1)
125 /* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
132 #define IGMP_V1_SEEN(in_dev) \
133 (IPV4_DEVCONF_ALL(FORCE_IGMP_VERSION) == 1 || \
134 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135 ((in_dev)->mr_v1_seen && \
136 time_before(jiffies, (in_dev)->mr_v1_seen)))
137 #define IGMP_V2_SEEN(in_dev) \
138 (IPV4_DEVCONF_ALL(FORCE_IGMP_VERSION) == 2 || \
139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140 ((in_dev)->mr_v2_seen && \
141 time_before(jiffies, (in_dev)->mr_v2_seen)))
143 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
144 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
145 static void igmpv3_clear_delrec(struct in_device *in_dev);
146 static int sf_setstate(struct ip_mc_list *pmc);
147 static void sf_markstate(struct ip_mc_list *pmc);
149 static void ip_mc_clear_src(struct ip_mc_list *pmc);
150 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
151 int sfcount, __be32 *psfsrc, int delta);
153 static void ip_ma_put(struct ip_mc_list *im)
155 if (atomic_dec_and_test(&im->refcnt)) {
156 in_dev_put(im->interface);
161 #ifdef CONFIG_IP_MULTICAST
167 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
169 spin_lock_bh(&im->lock);
170 if (del_timer(&im->timer))
171 atomic_dec(&im->refcnt);
174 im->unsolicit_count = 0;
175 spin_unlock_bh(&im->lock);
178 /* It must be called with locked im->lock */
179 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
181 int tv=net_random() % max_delay;
184 if (!mod_timer(&im->timer, jiffies+tv+2))
185 atomic_inc(&im->refcnt);
188 static void igmp_gq_start_timer(struct in_device *in_dev)
190 int tv = net_random() % in_dev->mr_maxdelay;
192 in_dev->mr_gq_running = 1;
193 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
197 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
199 int tv = net_random() % delay;
201 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
205 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
207 spin_lock_bh(&im->lock);
208 im->unsolicit_count = 0;
209 if (del_timer(&im->timer)) {
210 if ((long)(im->timer.expires-jiffies) < max_delay) {
211 add_timer(&im->timer);
213 spin_unlock_bh(&im->lock);
216 atomic_dec(&im->refcnt);
218 igmp_start_timer(im, max_delay);
219 spin_unlock_bh(&im->lock);
224 * Send an IGMP report.
227 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
230 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
231 int gdeleted, int sdeleted)
234 case IGMPV3_MODE_IS_INCLUDE:
235 case IGMPV3_MODE_IS_EXCLUDE:
236 if (gdeleted || sdeleted)
238 if (!(pmc->gsquery && !psf->sf_gsresp)) {
239 if (pmc->sfmode == MCAST_INCLUDE)
241 /* don't include if this source is excluded
244 if (psf->sf_count[MCAST_INCLUDE])
245 return type == IGMPV3_MODE_IS_INCLUDE;
246 return pmc->sfcount[MCAST_EXCLUDE] ==
247 psf->sf_count[MCAST_EXCLUDE];
250 case IGMPV3_CHANGE_TO_INCLUDE:
251 if (gdeleted || sdeleted)
253 return psf->sf_count[MCAST_INCLUDE] != 0;
254 case IGMPV3_CHANGE_TO_EXCLUDE:
255 if (gdeleted || sdeleted)
257 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
258 psf->sf_count[MCAST_INCLUDE])
260 return pmc->sfcount[MCAST_EXCLUDE] ==
261 psf->sf_count[MCAST_EXCLUDE];
262 case IGMPV3_ALLOW_NEW_SOURCES:
263 if (gdeleted || !psf->sf_crcount)
265 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
266 case IGMPV3_BLOCK_OLD_SOURCES:
267 if (pmc->sfmode == MCAST_INCLUDE)
268 return gdeleted || (psf->sf_crcount && sdeleted);
269 return psf->sf_crcount && !gdeleted && !sdeleted;
275 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
277 struct ip_sf_list *psf;
280 for (psf=pmc->sources; psf; psf=psf->sf_next) {
281 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
288 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
293 struct igmpv3_report *pig;
295 skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
300 struct flowi fl = { .oif = dev->ifindex,
302 .daddr = IGMPV3_ALL_MCR } },
303 .proto = IPPROTO_IGMP };
304 if (ip_route_output_key(&rt, &fl)) {
309 if (rt->rt_src == 0) {
315 skb->dst = &rt->u.dst;
318 skb_reserve(skb, LL_RESERVED_SPACE(dev));
320 skb_reset_network_header(skb);
322 skb_put(skb, sizeof(struct iphdr) + 4);
325 pip->ihl = (sizeof(struct iphdr)+4)>>2;
327 pip->frag_off = htons(IP_DF);
329 pip->daddr = rt->rt_dst;
330 pip->saddr = rt->rt_src;
331 pip->protocol = IPPROTO_IGMP;
332 pip->tot_len = 0; /* filled in later */
333 ip_select_ident(pip, &rt->u.dst, NULL);
334 ((u8*)&pip[1])[0] = IPOPT_RA;
335 ((u8*)&pip[1])[1] = 4;
336 ((u8*)&pip[1])[2] = 0;
337 ((u8*)&pip[1])[3] = 0;
339 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
340 skb_put(skb, sizeof(*pig));
341 pig = igmpv3_report_hdr(skb);
342 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
350 static int igmpv3_sendpack(struct sk_buff *skb)
352 struct iphdr *pip = ip_hdr(skb);
353 struct igmphdr *pig = igmp_hdr(skb);
354 const int iplen = skb->tail - skb->network_header;
355 const int igmplen = skb->tail - skb->transport_header;
357 pip->tot_len = htons(iplen);
359 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
361 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
365 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
367 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
370 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
371 int type, struct igmpv3_grec **ppgr)
373 struct net_device *dev = pmc->interface->dev;
374 struct igmpv3_report *pih;
375 struct igmpv3_grec *pgr;
378 skb = igmpv3_newpack(dev, dev->mtu);
381 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
382 pgr->grec_type = type;
383 pgr->grec_auxwords = 0;
385 pgr->grec_mca = pmc->multiaddr;
386 pih = igmpv3_report_hdr(skb);
387 pih->ngrec = htons(ntohs(pih->ngrec)+1);
392 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
393 skb_tailroom(skb)) : 0)
395 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
396 int type, int gdeleted, int sdeleted)
398 struct net_device *dev = pmc->interface->dev;
399 struct igmpv3_report *pih;
400 struct igmpv3_grec *pgr = NULL;
401 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
402 int scount, stotal, first, isquery, truncate;
404 if (pmc->multiaddr == IGMP_ALL_HOSTS)
407 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
408 type == IGMPV3_MODE_IS_EXCLUDE;
409 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
410 type == IGMPV3_CHANGE_TO_EXCLUDE;
414 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
419 pih = skb ? igmpv3_report_hdr(skb) : NULL;
421 /* EX and TO_EX get a fresh packet, if needed */
423 if (pih && pih->ngrec &&
424 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
426 igmpv3_sendpack(skb);
427 skb = igmpv3_newpack(dev, dev->mtu);
432 for (psf=*psf_list; psf; psf=psf_next) {
435 psf_next = psf->sf_next;
437 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
442 /* clear marks on query responses */
446 if (AVAILABLE(skb) < sizeof(__be32) +
447 first*sizeof(struct igmpv3_grec)) {
448 if (truncate && !first)
449 break; /* truncate these */
451 pgr->grec_nsrcs = htons(scount);
453 igmpv3_sendpack(skb);
454 skb = igmpv3_newpack(dev, dev->mtu);
459 skb = add_grhead(skb, pmc, type, &pgr);
464 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
465 *psrc = psf->sf_inaddr;
467 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
468 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
470 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
472 psf_prev->sf_next = psf->sf_next;
474 *psf_list = psf->sf_next;
484 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
485 type == IGMPV3_BLOCK_OLD_SOURCES)
487 if (pmc->crcount || isquery) {
488 /* make sure we have room for group header */
489 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
490 igmpv3_sendpack(skb);
491 skb = NULL; /* add_grhead will get a new one */
493 skb = add_grhead(skb, pmc, type, &pgr);
497 pgr->grec_nsrcs = htons(scount);
500 pmc->gsquery = 0; /* clear query state on report */
504 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
506 struct sk_buff *skb = NULL;
510 read_lock(&in_dev->mc_list_lock);
511 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
512 if (pmc->multiaddr == IGMP_ALL_HOSTS)
514 spin_lock_bh(&pmc->lock);
515 if (pmc->sfcount[MCAST_EXCLUDE])
516 type = IGMPV3_MODE_IS_EXCLUDE;
518 type = IGMPV3_MODE_IS_INCLUDE;
519 skb = add_grec(skb, pmc, type, 0, 0);
520 spin_unlock_bh(&pmc->lock);
522 read_unlock(&in_dev->mc_list_lock);
524 spin_lock_bh(&pmc->lock);
525 if (pmc->sfcount[MCAST_EXCLUDE])
526 type = IGMPV3_MODE_IS_EXCLUDE;
528 type = IGMPV3_MODE_IS_INCLUDE;
529 skb = add_grec(skb, pmc, type, 0, 0);
530 spin_unlock_bh(&pmc->lock);
534 return igmpv3_sendpack(skb);
538 * remove zero-count source records from a source filter list
540 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
542 struct ip_sf_list *psf_prev, *psf_next, *psf;
545 for (psf=*ppsf; psf; psf = psf_next) {
546 psf_next = psf->sf_next;
547 if (psf->sf_crcount == 0) {
549 psf_prev->sf_next = psf->sf_next;
551 *ppsf = psf->sf_next;
558 static void igmpv3_send_cr(struct in_device *in_dev)
560 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
561 struct sk_buff *skb = NULL;
564 read_lock(&in_dev->mc_list_lock);
565 spin_lock_bh(&in_dev->mc_tomb_lock);
569 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
570 pmc_next = pmc->next;
571 if (pmc->sfmode == MCAST_INCLUDE) {
572 type = IGMPV3_BLOCK_OLD_SOURCES;
573 dtype = IGMPV3_BLOCK_OLD_SOURCES;
574 skb = add_grec(skb, pmc, type, 1, 0);
575 skb = add_grec(skb, pmc, dtype, 1, 1);
578 if (pmc->sfmode == MCAST_EXCLUDE) {
579 type = IGMPV3_CHANGE_TO_INCLUDE;
580 skb = add_grec(skb, pmc, type, 1, 0);
583 if (pmc->crcount == 0) {
584 igmpv3_clear_zeros(&pmc->tomb);
585 igmpv3_clear_zeros(&pmc->sources);
588 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
590 pmc_prev->next = pmc_next;
592 in_dev->mc_tomb = pmc_next;
593 in_dev_put(pmc->interface);
598 spin_unlock_bh(&in_dev->mc_tomb_lock);
601 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
602 spin_lock_bh(&pmc->lock);
603 if (pmc->sfcount[MCAST_EXCLUDE]) {
604 type = IGMPV3_BLOCK_OLD_SOURCES;
605 dtype = IGMPV3_ALLOW_NEW_SOURCES;
607 type = IGMPV3_ALLOW_NEW_SOURCES;
608 dtype = IGMPV3_BLOCK_OLD_SOURCES;
610 skb = add_grec(skb, pmc, type, 0, 0);
611 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
613 /* filter mode changes */
615 if (pmc->sfmode == MCAST_EXCLUDE)
616 type = IGMPV3_CHANGE_TO_EXCLUDE;
618 type = IGMPV3_CHANGE_TO_INCLUDE;
619 skb = add_grec(skb, pmc, type, 0, 0);
622 spin_unlock_bh(&pmc->lock);
624 read_unlock(&in_dev->mc_list_lock);
628 (void) igmpv3_sendpack(skb);
631 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
638 struct net_device *dev = in_dev->dev;
639 __be32 group = pmc ? pmc->multiaddr : 0;
642 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
643 return igmpv3_send_report(in_dev, pmc);
644 else if (type == IGMP_HOST_LEAVE_MESSAGE)
645 dst = IGMP_ALL_ROUTER;
650 struct flowi fl = { .oif = dev->ifindex,
651 .nl_u = { .ip4_u = { .daddr = dst } },
652 .proto = IPPROTO_IGMP };
653 if (ip_route_output_key(&rt, &fl))
656 if (rt->rt_src == 0) {
661 skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
667 skb->dst = &rt->u.dst;
669 skb_reserve(skb, LL_RESERVED_SPACE(dev));
671 skb_reset_network_header(skb);
673 skb_put(skb, sizeof(struct iphdr) + 4);
676 iph->ihl = (sizeof(struct iphdr)+4)>>2;
678 iph->frag_off = htons(IP_DF);
681 iph->saddr = rt->rt_src;
682 iph->protocol = IPPROTO_IGMP;
683 iph->tot_len = htons(IGMP_SIZE);
684 ip_select_ident(iph, &rt->u.dst, NULL);
685 ((u8*)&iph[1])[0] = IPOPT_RA;
686 ((u8*)&iph[1])[1] = 4;
687 ((u8*)&iph[1])[2] = 0;
688 ((u8*)&iph[1])[3] = 0;
691 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
696 ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
698 return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
702 static void igmp_gq_timer_expire(unsigned long data)
704 struct in_device *in_dev = (struct in_device *)data;
706 in_dev->mr_gq_running = 0;
707 igmpv3_send_report(in_dev, NULL);
708 __in_dev_put(in_dev);
711 static void igmp_ifc_timer_expire(unsigned long data)
713 struct in_device *in_dev = (struct in_device *)data;
715 igmpv3_send_cr(in_dev);
716 if (in_dev->mr_ifc_count) {
717 in_dev->mr_ifc_count--;
718 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
720 __in_dev_put(in_dev);
723 static void igmp_ifc_event(struct in_device *in_dev)
725 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
727 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
728 IGMP_Unsolicited_Report_Count;
729 igmp_ifc_start_timer(in_dev, 1);
733 static void igmp_timer_expire(unsigned long data)
735 struct ip_mc_list *im=(struct ip_mc_list *)data;
736 struct in_device *in_dev = im->interface;
738 spin_lock(&im->lock);
741 if (im->unsolicit_count) {
742 im->unsolicit_count--;
743 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
746 spin_unlock(&im->lock);
748 if (IGMP_V1_SEEN(in_dev))
749 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
750 else if (IGMP_V2_SEEN(in_dev))
751 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
753 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
758 /* mark EXCLUDE-mode sources */
759 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
761 struct ip_sf_list *psf;
765 for (psf=pmc->sources; psf; psf=psf->sf_next) {
768 for (i=0; i<nsrcs; i++) {
769 /* skip inactive filters */
770 if (pmc->sfcount[MCAST_INCLUDE] ||
771 pmc->sfcount[MCAST_EXCLUDE] !=
772 psf->sf_count[MCAST_EXCLUDE])
774 if (srcs[i] == psf->sf_inaddr) {
781 if (scount == nsrcs) /* all sources excluded */
786 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
788 struct ip_sf_list *psf;
791 if (pmc->sfmode == MCAST_EXCLUDE)
792 return igmp_xmarksources(pmc, nsrcs, srcs);
794 /* mark INCLUDE-mode sources */
796 for (psf=pmc->sources; psf; psf=psf->sf_next) {
799 for (i=0; i<nsrcs; i++)
800 if (srcs[i] == psf->sf_inaddr) {
814 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
816 struct ip_mc_list *im;
818 /* Timers are only set for non-local groups */
820 if (group == IGMP_ALL_HOSTS)
823 read_lock(&in_dev->mc_list_lock);
824 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
825 if (im->multiaddr == group) {
830 read_unlock(&in_dev->mc_list_lock);
833 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
836 struct igmphdr *ih = igmp_hdr(skb);
837 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
838 struct ip_mc_list *im;
839 __be32 group = ih->group;
846 /* Alas, old v1 router presents here. */
848 max_delay = IGMP_Query_Response_Interval;
849 in_dev->mr_v1_seen = jiffies +
850 IGMP_V1_Router_Present_Timeout;
853 /* v2 router present */
854 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
855 in_dev->mr_v2_seen = jiffies +
856 IGMP_V2_Router_Present_Timeout;
858 /* cancel the interface change timer */
859 in_dev->mr_ifc_count = 0;
860 if (del_timer(&in_dev->mr_ifc_timer))
861 __in_dev_put(in_dev);
862 /* clear deleted report items */
863 igmpv3_clear_delrec(in_dev);
864 } else if (len < 12) {
865 return; /* ignore bogus packet; freed by caller */
867 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
870 ih3 = igmpv3_query_hdr(skb);
872 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
873 + ntohs(ih3->nsrcs)*sizeof(__be32)))
875 ih3 = igmpv3_query_hdr(skb);
878 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
880 max_delay = 1; /* can't mod w/ 0 */
881 in_dev->mr_maxdelay = max_delay;
883 in_dev->mr_qrv = ih3->qrv;
884 if (!group) { /* general query */
886 return; /* no sources allowed */
887 igmp_gq_start_timer(in_dev);
890 /* mark sources to include, if group & source-specific */
891 mark = ih3->nsrcs != 0;
895 * - Start the timers in all of our membership records
896 * that the query applies to for the interface on
897 * which the query arrived excl. those that belong
898 * to a "local" group (224.0.0.X)
899 * - For timers already running check if they need to
901 * - Use the igmp->igmp_code field as the maximum
904 read_lock(&in_dev->mc_list_lock);
905 for (im=in_dev->mc_list; im!=NULL; im=im->next) {
908 if (group && group != im->multiaddr)
910 if (im->multiaddr == IGMP_ALL_HOSTS)
912 spin_lock_bh(&im->lock);
914 im->gsquery = im->gsquery && mark;
917 changed = !im->gsquery ||
918 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
919 spin_unlock_bh(&im->lock);
921 igmp_mod_timer(im, max_delay);
923 read_unlock(&in_dev->mc_list_lock);
926 int igmp_rcv(struct sk_buff *skb)
928 /* This basically follows the spec line by line -- see RFC1112 */
930 struct in_device *in_dev = in_dev_get(skb->dev);
938 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
941 switch (skb->ip_summed) {
942 case CHECKSUM_COMPLETE:
943 if (!csum_fold(skb->csum))
948 if (__skb_checksum_complete(skb))
954 case IGMP_HOST_MEMBERSHIP_QUERY:
955 igmp_heard_query(in_dev, skb, len);
957 case IGMP_HOST_MEMBERSHIP_REPORT:
958 case IGMPV2_HOST_MEMBERSHIP_REPORT:
959 case IGMPV3_HOST_MEMBERSHIP_REPORT:
960 /* Is it our report looped back? */
961 if (((struct rtable*)skb->dst)->fl.iif == 0)
963 /* don't rely on MC router hearing unicast reports */
964 if (skb->pkt_type == PACKET_MULTICAST ||
965 skb->pkt_type == PACKET_BROADCAST)
966 igmp_heard_report(in_dev, ih->group);
969 #ifdef CONFIG_IP_PIMSM_V1
971 return pim_rcv_v1(skb);
975 case IGMP_HOST_LEAVE_MESSAGE:
977 case IGMP_MTRACE_RESP:
993 * Add a filter to a device
996 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
998 char buf[MAX_ADDR_LEN];
999 struct net_device *dev = in_dev->dev;
1001 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1002 We will get multicast token leakage, when IFF_MULTICAST
1003 is changed. This check should be done in dev->set_multicast_list
1004 routine. Something sort of:
1005 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1008 if (arp_mc_map(addr, buf, dev, 0) == 0)
1009 dev_mc_add(dev,buf,dev->addr_len,0);
1013 * Remove a filter from a device
1016 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1018 char buf[MAX_ADDR_LEN];
1019 struct net_device *dev = in_dev->dev;
1021 if (arp_mc_map(addr, buf, dev, 0) == 0)
1022 dev_mc_delete(dev,buf,dev->addr_len,0);
1025 #ifdef CONFIG_IP_MULTICAST
1027 * deleted ip_mc_list manipulation
1029 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1031 struct ip_mc_list *pmc;
1033 /* this is an "ip_mc_list" for convenience; only the fields below
1034 * are actually used. In particular, the refcnt and users are not
1035 * used for management of the delete list. Using the same structure
1036 * for deleted items allows change reports to use common code with
1037 * non-deleted or query-response MCA's.
1039 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1042 spin_lock_bh(&im->lock);
1043 pmc->interface = im->interface;
1044 in_dev_hold(in_dev);
1045 pmc->multiaddr = im->multiaddr;
1046 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1047 IGMP_Unsolicited_Report_Count;
1048 pmc->sfmode = im->sfmode;
1049 if (pmc->sfmode == MCAST_INCLUDE) {
1050 struct ip_sf_list *psf;
1052 pmc->tomb = im->tomb;
1053 pmc->sources = im->sources;
1054 im->tomb = im->sources = NULL;
1055 for (psf=pmc->sources; psf; psf=psf->sf_next)
1056 psf->sf_crcount = pmc->crcount;
1058 spin_unlock_bh(&im->lock);
1060 spin_lock_bh(&in_dev->mc_tomb_lock);
1061 pmc->next = in_dev->mc_tomb;
1062 in_dev->mc_tomb = pmc;
1063 spin_unlock_bh(&in_dev->mc_tomb_lock);
1066 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1068 struct ip_mc_list *pmc, *pmc_prev;
1069 struct ip_sf_list *psf, *psf_next;
1071 spin_lock_bh(&in_dev->mc_tomb_lock);
1073 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1074 if (pmc->multiaddr == multiaddr)
1080 pmc_prev->next = pmc->next;
1082 in_dev->mc_tomb = pmc->next;
1084 spin_unlock_bh(&in_dev->mc_tomb_lock);
1086 for (psf=pmc->tomb; psf; psf=psf_next) {
1087 psf_next = psf->sf_next;
1090 in_dev_put(pmc->interface);
1095 static void igmpv3_clear_delrec(struct in_device *in_dev)
1097 struct ip_mc_list *pmc, *nextpmc;
1099 spin_lock_bh(&in_dev->mc_tomb_lock);
1100 pmc = in_dev->mc_tomb;
1101 in_dev->mc_tomb = NULL;
1102 spin_unlock_bh(&in_dev->mc_tomb_lock);
1104 for (; pmc; pmc = nextpmc) {
1105 nextpmc = pmc->next;
1106 ip_mc_clear_src(pmc);
1107 in_dev_put(pmc->interface);
1110 /* clear dead sources, too */
1111 read_lock(&in_dev->mc_list_lock);
1112 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1113 struct ip_sf_list *psf, *psf_next;
1115 spin_lock_bh(&pmc->lock);
1118 spin_unlock_bh(&pmc->lock);
1119 for (; psf; psf=psf_next) {
1120 psf_next = psf->sf_next;
1124 read_unlock(&in_dev->mc_list_lock);
1128 static void igmp_group_dropped(struct ip_mc_list *im)
1130 struct in_device *in_dev = im->interface;
1131 #ifdef CONFIG_IP_MULTICAST
1137 ip_mc_filter_del(in_dev, im->multiaddr);
1140 #ifdef CONFIG_IP_MULTICAST
1141 if (im->multiaddr == IGMP_ALL_HOSTS)
1144 reporter = im->reporter;
1145 igmp_stop_timer(im);
1147 if (!in_dev->dead) {
1148 if (IGMP_V1_SEEN(in_dev))
1150 if (IGMP_V2_SEEN(in_dev)) {
1152 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1156 igmpv3_add_delrec(in_dev, im);
1158 igmp_ifc_event(in_dev);
1162 ip_mc_clear_src(im);
1165 static void igmp_group_added(struct ip_mc_list *im)
1167 struct in_device *in_dev = im->interface;
1169 if (im->loaded == 0) {
1171 ip_mc_filter_add(in_dev, im->multiaddr);
1174 #ifdef CONFIG_IP_MULTICAST
1175 if (im->multiaddr == IGMP_ALL_HOSTS)
1180 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1181 spin_lock_bh(&im->lock);
1182 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1183 spin_unlock_bh(&im->lock);
1188 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1189 IGMP_Unsolicited_Report_Count;
1190 igmp_ifc_event(in_dev);
1196 * Multicast list managers
1201 * A socket has joined a multicast group on device dev.
1204 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1206 struct ip_mc_list *im;
1210 for (im=in_dev->mc_list; im; im=im->next) {
1211 if (im->multiaddr == addr) {
1213 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1218 im = kmalloc(sizeof(*im), GFP_KERNEL);
1223 im->interface=in_dev;
1224 in_dev_hold(in_dev);
1226 /* initial mode is (EX, empty) */
1227 im->sfmode = MCAST_EXCLUDE;
1228 im->sfcount[MCAST_INCLUDE] = 0;
1229 im->sfcount[MCAST_EXCLUDE] = 1;
1233 atomic_set(&im->refcnt, 1);
1234 spin_lock_init(&im->lock);
1235 #ifdef CONFIG_IP_MULTICAST
1237 init_timer(&im->timer);
1238 im->timer.data=(unsigned long)im;
1239 im->timer.function=&igmp_timer_expire;
1240 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1245 write_lock_bh(&in_dev->mc_list_lock);
1246 im->next=in_dev->mc_list;
1248 write_unlock_bh(&in_dev->mc_list_lock);
1249 #ifdef CONFIG_IP_MULTICAST
1250 igmpv3_del_delrec(in_dev, im->multiaddr);
1252 igmp_group_added(im);
1254 ip_rt_multicast_event(in_dev);
1260 * Resend IGMP JOIN report; used for bonding.
1262 void ip_mc_rejoin_group(struct ip_mc_list *im)
1264 #ifdef CONFIG_IP_MULTICAST
1265 struct in_device *in_dev = im->interface;
1267 if (im->multiaddr == IGMP_ALL_HOSTS)
1270 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1271 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1275 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1276 IGMP_Unsolicited_Report_Count;
1277 igmp_ifc_event(in_dev);
1282 * A socket has left a multicast group on device dev
1285 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1287 struct ip_mc_list *i, **ip;
1291 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1292 if (i->multiaddr==addr) {
1293 if (--i->users == 0) {
1294 write_lock_bh(&in_dev->mc_list_lock);
1296 write_unlock_bh(&in_dev->mc_list_lock);
1297 igmp_group_dropped(i);
1300 ip_rt_multicast_event(in_dev);
1310 /* Device going down */
1312 void ip_mc_down(struct in_device *in_dev)
1314 struct ip_mc_list *i;
1318 for (i=in_dev->mc_list; i; i=i->next)
1319 igmp_group_dropped(i);
1321 #ifdef CONFIG_IP_MULTICAST
1322 in_dev->mr_ifc_count = 0;
1323 if (del_timer(&in_dev->mr_ifc_timer))
1324 __in_dev_put(in_dev);
1325 in_dev->mr_gq_running = 0;
1326 if (del_timer(&in_dev->mr_gq_timer))
1327 __in_dev_put(in_dev);
1328 igmpv3_clear_delrec(in_dev);
1331 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1334 void ip_mc_init_dev(struct in_device *in_dev)
1338 in_dev->mc_tomb = NULL;
1339 #ifdef CONFIG_IP_MULTICAST
1340 in_dev->mr_gq_running = 0;
1341 init_timer(&in_dev->mr_gq_timer);
1342 in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1343 in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1344 in_dev->mr_ifc_count = 0;
1345 init_timer(&in_dev->mr_ifc_timer);
1346 in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1347 in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1348 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1351 rwlock_init(&in_dev->mc_list_lock);
1352 spin_lock_init(&in_dev->mc_tomb_lock);
1355 /* Device going up */
1357 void ip_mc_up(struct in_device *in_dev)
1359 struct ip_mc_list *i;
1363 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1365 for (i=in_dev->mc_list; i; i=i->next)
1366 igmp_group_added(i);
1370 * Device is about to be destroyed: clean up.
1373 void ip_mc_destroy_dev(struct in_device *in_dev)
1375 struct ip_mc_list *i;
1379 /* Deactivate timers */
1382 write_lock_bh(&in_dev->mc_list_lock);
1383 while ((i = in_dev->mc_list) != NULL) {
1384 in_dev->mc_list = i->next;
1385 write_unlock_bh(&in_dev->mc_list_lock);
1387 igmp_group_dropped(i);
1390 write_lock_bh(&in_dev->mc_list_lock);
1392 write_unlock_bh(&in_dev->mc_list_lock);
1395 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1397 struct flowi fl = { .nl_u = { .ip4_u =
1398 { .daddr = imr->imr_multiaddr.s_addr } } };
1400 struct net_device *dev = NULL;
1401 struct in_device *idev = NULL;
1403 if (imr->imr_ifindex) {
1404 idev = inetdev_by_index(imr->imr_ifindex);
1409 if (imr->imr_address.s_addr) {
1410 dev = ip_dev_find(imr->imr_address.s_addr);
1416 if (!dev && !ip_route_output_key(&rt, &fl)) {
1417 dev = rt->u.dst.dev;
1421 imr->imr_ifindex = dev->ifindex;
1422 idev = __in_dev_get_rtnl(dev);
1428 * Join a socket to a group
1430 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1431 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1434 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1437 struct ip_sf_list *psf, *psf_prev;
1441 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1442 if (psf->sf_inaddr == *psfsrc)
1446 if (!psf || psf->sf_count[sfmode] == 0) {
1447 /* source filter not found, or count wrong => bug */
1450 psf->sf_count[sfmode]--;
1451 if (psf->sf_count[sfmode] == 0) {
1452 ip_rt_multicast_event(pmc->interface);
1454 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1455 #ifdef CONFIG_IP_MULTICAST
1456 struct in_device *in_dev = pmc->interface;
1459 /* no more filters for this source */
1461 psf_prev->sf_next = psf->sf_next;
1463 pmc->sources = psf->sf_next;
1464 #ifdef CONFIG_IP_MULTICAST
1465 if (psf->sf_oldin &&
1466 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1467 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1468 IGMP_Unsolicited_Report_Count;
1469 psf->sf_next = pmc->tomb;
1479 #ifndef CONFIG_IP_MULTICAST
1480 #define igmp_ifc_event(x) do { } while (0)
1483 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1484 int sfcount, __be32 *psfsrc, int delta)
1486 struct ip_mc_list *pmc;
1492 read_lock(&in_dev->mc_list_lock);
1493 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1494 if (*pmca == pmc->multiaddr)
1498 /* MCA not found?? bug */
1499 read_unlock(&in_dev->mc_list_lock);
1502 spin_lock_bh(&pmc->lock);
1503 read_unlock(&in_dev->mc_list_lock);
1504 #ifdef CONFIG_IP_MULTICAST
1509 if (!pmc->sfcount[sfmode])
1511 pmc->sfcount[sfmode]--;
1514 for (i=0; i<sfcount; i++) {
1515 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1517 changerec |= rv > 0;
1521 if (pmc->sfmode == MCAST_EXCLUDE &&
1522 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1523 pmc->sfcount[MCAST_INCLUDE]) {
1524 #ifdef CONFIG_IP_MULTICAST
1525 struct ip_sf_list *psf;
1528 /* filter mode change */
1529 pmc->sfmode = MCAST_INCLUDE;
1530 #ifdef CONFIG_IP_MULTICAST
1531 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1532 IGMP_Unsolicited_Report_Count;
1533 in_dev->mr_ifc_count = pmc->crcount;
1534 for (psf=pmc->sources; psf; psf = psf->sf_next)
1535 psf->sf_crcount = 0;
1536 igmp_ifc_event(pmc->interface);
1537 } else if (sf_setstate(pmc) || changerec) {
1538 igmp_ifc_event(pmc->interface);
1542 spin_unlock_bh(&pmc->lock);
1547 * Add multicast single-source filter to the interface list
1549 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1550 __be32 *psfsrc, int delta)
1552 struct ip_sf_list *psf, *psf_prev;
1555 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1556 if (psf->sf_inaddr == *psfsrc)
1561 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1564 psf->sf_inaddr = *psfsrc;
1566 psf_prev->sf_next = psf;
1570 psf->sf_count[sfmode]++;
1571 if (psf->sf_count[sfmode] == 1) {
1572 ip_rt_multicast_event(pmc->interface);
1577 #ifdef CONFIG_IP_MULTICAST
1578 static void sf_markstate(struct ip_mc_list *pmc)
1580 struct ip_sf_list *psf;
1581 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1583 for (psf=pmc->sources; psf; psf=psf->sf_next)
1584 if (pmc->sfcount[MCAST_EXCLUDE]) {
1585 psf->sf_oldin = mca_xcount ==
1586 psf->sf_count[MCAST_EXCLUDE] &&
1587 !psf->sf_count[MCAST_INCLUDE];
1589 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1592 static int sf_setstate(struct ip_mc_list *pmc)
1594 struct ip_sf_list *psf, *dpsf;
1595 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1596 int qrv = pmc->interface->mr_qrv;
1600 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1601 if (pmc->sfcount[MCAST_EXCLUDE]) {
1602 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1603 !psf->sf_count[MCAST_INCLUDE];
1605 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1607 if (!psf->sf_oldin) {
1608 struct ip_sf_list *prev = NULL;
1610 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1611 if (dpsf->sf_inaddr == psf->sf_inaddr)
1617 prev->sf_next = dpsf->sf_next;
1619 pmc->tomb = dpsf->sf_next;
1622 psf->sf_crcount = qrv;
1625 } else if (psf->sf_oldin) {
1627 psf->sf_crcount = 0;
1629 * add or update "delete" records if an active filter
1632 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1633 if (dpsf->sf_inaddr == psf->sf_inaddr)
1636 dpsf = (struct ip_sf_list *)
1637 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1641 /* pmc->lock held by callers */
1642 dpsf->sf_next = pmc->tomb;
1645 dpsf->sf_crcount = qrv;
1654 * Add multicast source filter list to the interface list
1656 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1657 int sfcount, __be32 *psfsrc, int delta)
1659 struct ip_mc_list *pmc;
1665 read_lock(&in_dev->mc_list_lock);
1666 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1667 if (*pmca == pmc->multiaddr)
1671 /* MCA not found?? bug */
1672 read_unlock(&in_dev->mc_list_lock);
1675 spin_lock_bh(&pmc->lock);
1676 read_unlock(&in_dev->mc_list_lock);
1678 #ifdef CONFIG_IP_MULTICAST
1681 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1683 pmc->sfcount[sfmode]++;
1685 for (i=0; i<sfcount; i++) {
1686 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1693 pmc->sfcount[sfmode]--;
1695 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1696 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1697 #ifdef CONFIG_IP_MULTICAST
1698 struct in_device *in_dev = pmc->interface;
1699 struct ip_sf_list *psf;
1702 /* filter mode change */
1703 if (pmc->sfcount[MCAST_EXCLUDE])
1704 pmc->sfmode = MCAST_EXCLUDE;
1705 else if (pmc->sfcount[MCAST_INCLUDE])
1706 pmc->sfmode = MCAST_INCLUDE;
1707 #ifdef CONFIG_IP_MULTICAST
1708 /* else no filters; keep old mode for reports */
1710 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1711 IGMP_Unsolicited_Report_Count;
1712 in_dev->mr_ifc_count = pmc->crcount;
1713 for (psf=pmc->sources; psf; psf = psf->sf_next)
1714 psf->sf_crcount = 0;
1715 igmp_ifc_event(in_dev);
1716 } else if (sf_setstate(pmc)) {
1717 igmp_ifc_event(in_dev);
1720 spin_unlock_bh(&pmc->lock);
1724 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1726 struct ip_sf_list *psf, *nextpsf;
1728 for (psf=pmc->tomb; psf; psf=nextpsf) {
1729 nextpsf = psf->sf_next;
1733 for (psf=pmc->sources; psf; psf=nextpsf) {
1734 nextpsf = psf->sf_next;
1737 pmc->sources = NULL;
1738 pmc->sfmode = MCAST_EXCLUDE;
1739 pmc->sfcount[MCAST_INCLUDE] = 0;
1740 pmc->sfcount[MCAST_EXCLUDE] = 1;
1745 * Join a multicast group
1747 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1750 __be32 addr = imr->imr_multiaddr.s_addr;
1751 struct ip_mc_socklist *iml=NULL, *i;
1752 struct in_device *in_dev;
1753 struct inet_sock *inet = inet_sk(sk);
1757 if (!MULTICAST(addr))
1762 in_dev = ip_mc_find_dev(imr);
1771 ifindex = imr->imr_ifindex;
1772 for (i = inet->mc_list; i; i = i->next) {
1773 if (i->multi.imr_multiaddr.s_addr == addr &&
1774 i->multi.imr_ifindex == ifindex)
1779 if (count >= sysctl_igmp_max_memberships)
1781 iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1785 memcpy(&iml->multi, imr, sizeof(*imr));
1786 iml->next = inet->mc_list;
1788 iml->sfmode = MCAST_EXCLUDE;
1789 inet->mc_list = iml;
1790 ip_mc_inc_group(in_dev, addr);
1797 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1798 struct in_device *in_dev)
1802 if (iml->sflist == 0) {
1803 /* any-source empty exclude case */
1804 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1805 iml->sfmode, 0, NULL, 0);
1807 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1808 iml->sfmode, iml->sflist->sl_count,
1809 iml->sflist->sl_addr, 0);
1810 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1816 * Ask a socket to leave a group.
1819 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1821 struct inet_sock *inet = inet_sk(sk);
1822 struct ip_mc_socklist *iml, **imlp;
1823 struct in_device *in_dev;
1824 __be32 group = imr->imr_multiaddr.s_addr;
1826 int ret = -EADDRNOTAVAIL;
1829 in_dev = ip_mc_find_dev(imr);
1830 ifindex = imr->imr_ifindex;
1831 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1832 if (iml->multi.imr_multiaddr.s_addr != group)
1835 if (iml->multi.imr_ifindex != ifindex)
1837 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1838 iml->multi.imr_address.s_addr)
1841 (void) ip_mc_leave_src(sk, iml, in_dev);
1846 ip_mc_dec_group(in_dev, group);
1848 sock_kfree_s(sk, iml, sizeof(*iml));
1857 int ip_mc_source(int add, int omode, struct sock *sk, struct
1858 ip_mreq_source *mreqs, int ifindex)
1861 struct ip_mreqn imr;
1862 __be32 addr = mreqs->imr_multiaddr;
1863 struct ip_mc_socklist *pmc;
1864 struct in_device *in_dev = NULL;
1865 struct inet_sock *inet = inet_sk(sk);
1866 struct ip_sf_socklist *psl;
1870 if (!MULTICAST(addr))
1875 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1876 imr.imr_address.s_addr = mreqs->imr_interface;
1877 imr.imr_ifindex = ifindex;
1878 in_dev = ip_mc_find_dev(&imr);
1884 err = -EADDRNOTAVAIL;
1886 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1887 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1888 && pmc->multi.imr_ifindex == imr.imr_ifindex)
1891 if (!pmc) { /* must have a prior join */
1895 /* if a source filter was set, must be the same mode as before */
1897 if (pmc->sfmode != omode) {
1901 } else if (pmc->sfmode != omode) {
1902 /* allow mode switches for empty-set filters */
1903 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1904 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1906 pmc->sfmode = omode;
1912 goto done; /* err = -EADDRNOTAVAIL */
1914 for (i=0; i<psl->sl_count; i++) {
1915 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1920 if (rv) /* source not found */
1921 goto done; /* err = -EADDRNOTAVAIL */
1923 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1924 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1929 /* update the interface filter */
1930 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1931 &mreqs->imr_sourceaddr, 1);
1933 for (j=i+1; j<psl->sl_count; j++)
1934 psl->sl_addr[j-1] = psl->sl_addr[j];
1939 /* else, add a new source to the filter */
1941 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1945 if (!psl || psl->sl_count == psl->sl_max) {
1946 struct ip_sf_socklist *newpsl;
1947 int count = IP_SFBLOCK;
1950 count += psl->sl_max;
1951 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1956 newpsl->sl_max = count;
1957 newpsl->sl_count = count - IP_SFBLOCK;
1959 for (i=0; i<psl->sl_count; i++)
1960 newpsl->sl_addr[i] = psl->sl_addr[i];
1961 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1963 pmc->sflist = psl = newpsl;
1965 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1966 for (i=0; i<psl->sl_count; i++) {
1967 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1972 if (rv == 0) /* address already there is an error */
1974 for (j=psl->sl_count-1; j>=i; j--)
1975 psl->sl_addr[j+1] = psl->sl_addr[j];
1976 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1979 /* update the interface list */
1980 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1981 &mreqs->imr_sourceaddr, 1);
1985 return ip_mc_leave_group(sk, &imr);
1989 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1992 struct ip_mreqn imr;
1993 __be32 addr = msf->imsf_multiaddr;
1994 struct ip_mc_socklist *pmc;
1995 struct in_device *in_dev;
1996 struct inet_sock *inet = inet_sk(sk);
1997 struct ip_sf_socklist *newpsl, *psl;
2000 if (!MULTICAST(addr))
2002 if (msf->imsf_fmode != MCAST_INCLUDE &&
2003 msf->imsf_fmode != MCAST_EXCLUDE)
2008 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2009 imr.imr_address.s_addr = msf->imsf_interface;
2010 imr.imr_ifindex = ifindex;
2011 in_dev = ip_mc_find_dev(&imr);
2018 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2019 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2024 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2025 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2026 pmc->multi.imr_ifindex == imr.imr_ifindex)
2029 if (!pmc) { /* must have a prior join */
2033 if (msf->imsf_numsrc) {
2034 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2040 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2041 memcpy(newpsl->sl_addr, msf->imsf_slist,
2042 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2043 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2044 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2046 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2051 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2052 msf->imsf_fmode, 0, NULL, 0);
2056 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2057 psl->sl_count, psl->sl_addr, 0);
2058 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2060 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2062 pmc->sflist = newpsl;
2063 pmc->sfmode = msf->imsf_fmode;
2068 err = ip_mc_leave_group(sk, &imr);
2072 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2073 struct ip_msfilter __user *optval, int __user *optlen)
2075 int err, len, count, copycount;
2076 struct ip_mreqn imr;
2077 __be32 addr = msf->imsf_multiaddr;
2078 struct ip_mc_socklist *pmc;
2079 struct in_device *in_dev;
2080 struct inet_sock *inet = inet_sk(sk);
2081 struct ip_sf_socklist *psl;
2083 if (!MULTICAST(addr))
2088 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2089 imr.imr_address.s_addr = msf->imsf_interface;
2090 imr.imr_ifindex = 0;
2091 in_dev = ip_mc_find_dev(&imr);
2097 err = -EADDRNOTAVAIL;
2099 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2100 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2101 pmc->multi.imr_ifindex == imr.imr_ifindex)
2104 if (!pmc) /* must have a prior join */
2106 msf->imsf_fmode = pmc->sfmode;
2113 count = psl->sl_count;
2115 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2116 len = copycount * sizeof(psl->sl_addr[0]);
2117 msf->imsf_numsrc = count;
2118 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2119 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2123 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2131 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2132 struct group_filter __user *optval, int __user *optlen)
2134 int err, i, count, copycount;
2135 struct sockaddr_in *psin;
2137 struct ip_mc_socklist *pmc;
2138 struct inet_sock *inet = inet_sk(sk);
2139 struct ip_sf_socklist *psl;
2141 psin = (struct sockaddr_in *)&gsf->gf_group;
2142 if (psin->sin_family != AF_INET)
2144 addr = psin->sin_addr.s_addr;
2145 if (!MULTICAST(addr))
2150 err = -EADDRNOTAVAIL;
2152 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2153 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2154 pmc->multi.imr_ifindex == gsf->gf_interface)
2157 if (!pmc) /* must have a prior join */
2159 gsf->gf_fmode = pmc->sfmode;
2162 count = psl ? psl->sl_count : 0;
2163 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2164 gsf->gf_numsrc = count;
2165 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2166 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2169 for (i=0; i<copycount; i++) {
2170 struct sockaddr_in *psin;
2171 struct sockaddr_storage ss;
2173 psin = (struct sockaddr_in *)&ss;
2174 memset(&ss, 0, sizeof(ss));
2175 psin->sin_family = AF_INET;
2176 psin->sin_addr.s_addr = psl->sl_addr[i];
2177 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2187 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2189 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2191 struct inet_sock *inet = inet_sk(sk);
2192 struct ip_mc_socklist *pmc;
2193 struct ip_sf_socklist *psl;
2196 if (!MULTICAST(loc_addr))
2199 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2200 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2201 pmc->multi.imr_ifindex == dif)
2208 return pmc->sfmode == MCAST_EXCLUDE;
2210 for (i=0; i<psl->sl_count; i++) {
2211 if (psl->sl_addr[i] == rmt_addr)
2214 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2216 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2222 * A socket is closing.
2225 void ip_mc_drop_socket(struct sock *sk)
2227 struct inet_sock *inet = inet_sk(sk);
2228 struct ip_mc_socklist *iml;
2230 if (inet->mc_list == NULL)
2234 while ((iml = inet->mc_list) != NULL) {
2235 struct in_device *in_dev;
2236 inet->mc_list = iml->next;
2238 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
2239 (void) ip_mc_leave_src(sk, iml, in_dev);
2240 if (in_dev != NULL) {
2241 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2244 sock_kfree_s(sk, iml, sizeof(*iml));
2249 int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2251 struct ip_mc_list *im;
2252 struct ip_sf_list *psf;
2255 read_lock(&in_dev->mc_list_lock);
2256 for (im=in_dev->mc_list; im; im=im->next) {
2257 if (im->multiaddr == mc_addr)
2260 if (im && proto == IPPROTO_IGMP) {
2264 for (psf=im->sources; psf; psf=psf->sf_next) {
2265 if (psf->sf_inaddr == src_addr)
2269 rv = psf->sf_count[MCAST_INCLUDE] ||
2270 psf->sf_count[MCAST_EXCLUDE] !=
2271 im->sfcount[MCAST_EXCLUDE];
2273 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2275 rv = 1; /* unspecified source; tentatively allow */
2277 read_unlock(&in_dev->mc_list_lock);
2281 #if defined(CONFIG_PROC_FS)
2282 struct igmp_mc_iter_state {
2283 struct net_device *dev;
2284 struct in_device *in_dev;
2287 #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2289 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2291 struct ip_mc_list *im = NULL;
2292 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2294 state->in_dev = NULL;
2295 for_each_netdev(&init_net, state->dev) {
2296 struct in_device *in_dev;
2297 in_dev = in_dev_get(state->dev);
2300 read_lock(&in_dev->mc_list_lock);
2301 im = in_dev->mc_list;
2303 state->in_dev = in_dev;
2306 read_unlock(&in_dev->mc_list_lock);
2312 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2314 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2317 if (likely(state->in_dev != NULL)) {
2318 read_unlock(&state->in_dev->mc_list_lock);
2319 in_dev_put(state->in_dev);
2321 state->dev = next_net_device(state->dev);
2323 state->in_dev = NULL;
2326 state->in_dev = in_dev_get(state->dev);
2329 read_lock(&state->in_dev->mc_list_lock);
2330 im = state->in_dev->mc_list;
2335 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2337 struct ip_mc_list *im = igmp_mc_get_first(seq);
2339 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2341 return pos ? NULL : im;
2344 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2346 read_lock(&dev_base_lock);
2347 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2350 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2352 struct ip_mc_list *im;
2353 if (v == SEQ_START_TOKEN)
2354 im = igmp_mc_get_first(seq);
2356 im = igmp_mc_get_next(seq, v);
2361 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2363 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2364 if (likely(state->in_dev != NULL)) {
2365 read_unlock(&state->in_dev->mc_list_lock);
2366 in_dev_put(state->in_dev);
2367 state->in_dev = NULL;
2370 read_unlock(&dev_base_lock);
2373 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2375 if (v == SEQ_START_TOKEN)
2377 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2379 struct ip_mc_list *im = (struct ip_mc_list *)v;
2380 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2382 #ifdef CONFIG_IP_MULTICAST
2383 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2384 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2390 if (state->in_dev->mc_list == im) {
2391 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2392 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2396 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2397 im->multiaddr, im->users,
2398 im->tm_running, im->tm_running ?
2399 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2405 static const struct seq_operations igmp_mc_seq_ops = {
2406 .start = igmp_mc_seq_start,
2407 .next = igmp_mc_seq_next,
2408 .stop = igmp_mc_seq_stop,
2409 .show = igmp_mc_seq_show,
2412 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2414 struct seq_file *seq;
2416 struct igmp_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2420 rc = seq_open(file, &igmp_mc_seq_ops);
2424 seq = file->private_data;
2433 static const struct file_operations igmp_mc_seq_fops = {
2434 .owner = THIS_MODULE,
2435 .open = igmp_mc_seq_open,
2437 .llseek = seq_lseek,
2438 .release = seq_release_private,
2441 struct igmp_mcf_iter_state {
2442 struct net_device *dev;
2443 struct in_device *idev;
2444 struct ip_mc_list *im;
2447 #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2449 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2451 struct ip_sf_list *psf = NULL;
2452 struct ip_mc_list *im = NULL;
2453 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2457 for_each_netdev(&init_net, state->dev) {
2458 struct in_device *idev;
2459 idev = in_dev_get(state->dev);
2460 if (unlikely(idev == NULL))
2462 read_lock(&idev->mc_list_lock);
2464 if (likely(im != NULL)) {
2465 spin_lock_bh(&im->lock);
2467 if (likely(psf != NULL)) {
2472 spin_unlock_bh(&im->lock);
2474 read_unlock(&idev->mc_list_lock);
2480 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2482 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2486 spin_unlock_bh(&state->im->lock);
2487 state->im = state->im->next;
2488 while (!state->im) {
2489 if (likely(state->idev != NULL)) {
2490 read_unlock(&state->idev->mc_list_lock);
2491 in_dev_put(state->idev);
2493 state->dev = next_net_device(state->dev);
2498 state->idev = in_dev_get(state->dev);
2501 read_lock(&state->idev->mc_list_lock);
2502 state->im = state->idev->mc_list;
2506 spin_lock_bh(&state->im->lock);
2507 psf = state->im->sources;
2513 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2515 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2517 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2519 return pos ? NULL : psf;
2522 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2524 read_lock(&dev_base_lock);
2525 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2528 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2530 struct ip_sf_list *psf;
2531 if (v == SEQ_START_TOKEN)
2532 psf = igmp_mcf_get_first(seq);
2534 psf = igmp_mcf_get_next(seq, v);
2539 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2541 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2542 if (likely(state->im != NULL)) {
2543 spin_unlock_bh(&state->im->lock);
2546 if (likely(state->idev != NULL)) {
2547 read_unlock(&state->idev->mc_list_lock);
2548 in_dev_put(state->idev);
2552 read_unlock(&dev_base_lock);
2555 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2557 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2558 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2560 if (v == SEQ_START_TOKEN) {
2563 "%10s %10s %6s %6s\n", "Idx",
2565 "SRC", "INC", "EXC");
2569 "0x%08x %6lu %6lu\n",
2570 state->dev->ifindex, state->dev->name,
2571 ntohl(state->im->multiaddr),
2572 ntohl(psf->sf_inaddr),
2573 psf->sf_count[MCAST_INCLUDE],
2574 psf->sf_count[MCAST_EXCLUDE]);
2579 static const struct seq_operations igmp_mcf_seq_ops = {
2580 .start = igmp_mcf_seq_start,
2581 .next = igmp_mcf_seq_next,
2582 .stop = igmp_mcf_seq_stop,
2583 .show = igmp_mcf_seq_show,
2586 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2588 struct seq_file *seq;
2590 struct igmp_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2594 rc = seq_open(file, &igmp_mcf_seq_ops);
2598 seq = file->private_data;
2607 static const struct file_operations igmp_mcf_seq_fops = {
2608 .owner = THIS_MODULE,
2609 .open = igmp_mcf_seq_open,
2611 .llseek = seq_lseek,
2612 .release = seq_release_private,
2615 int __init igmp_mc_proc_init(void)
2617 proc_net_fops_create(&init_net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2618 proc_net_fops_create(&init_net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2623 EXPORT_SYMBOL(ip_mc_dec_group);
2624 EXPORT_SYMBOL(ip_mc_inc_group);
2625 EXPORT_SYMBOL(ip_mc_join_group);
2626 EXPORT_SYMBOL(ip_mc_rejoin_group);