clean up reading of ICR register in FEC driver
[linux-2.6.git] / net / ipv4 / igmp.c
1 /*
2  *      Linux NET3:     Internet Group Management Protocol  [IGMP]
3  *
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.
6  *
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.
10  *
11  *      Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
12  *
13  *      Authors:
14  *              Alan Cox <Alan.Cox@linux.org>
15  *
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.
20  *
21  *      Fixes:
22  *
23  *              Alan Cox        :       Added lots of __inline__ to optimise
24  *                                      the memory usage of all the tiny little
25  *                                      functions.
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
34  *                                      points.
35  *
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"
55  *                                      message (960131).
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
60  *                                     true (960216).
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
72  *                                      Vinay Kulkarni
73  */
74
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>
84 #include <linux/in.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>
93
94 #include <net/arp.h>
95 #include <net/ip.h>
96 #include <net/protocol.h>
97 #include <net/route.h>
98 #include <net/sock.h>
99 #include <net/checksum.h>
100 #include <linux/netfilter_ipv4.h>
101 #ifdef CONFIG_IP_MROUTE
102 #include <linux/mroute.h>
103 #endif
104 #ifdef CONFIG_PROC_FS
105 #include <linux/proc_fs.h>
106 #include <linux/seq_file.h>
107 #endif
108
109 #define IP_MAX_MEMBERSHIPS      20
110 #define IP_MAX_MSF              10
111
112 #ifdef CONFIG_IP_MULTICAST
113 /* Parameter names and values are taken from igmp-v2-06 draft */
114
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
120
121
122 #define IGMP_Initial_Report_Delay               (1)
123
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.
129  */
130
131 #define IGMP_V1_SEEN(in_dev) \
132         (IPV4_DEVCONF_ALL(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(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)))
141
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);
147 #endif
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);
151
152 static void ip_ma_put(struct ip_mc_list *im)
153 {
154         if (atomic_dec_and_test(&im->refcnt)) {
155                 in_dev_put(im->interface);
156                 kfree(im);
157         }
158 }
159
160 #ifdef CONFIG_IP_MULTICAST
161
162 /*
163  *      Timer management
164  */
165
166 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
167 {
168         spin_lock_bh(&im->lock);
169         if (del_timer(&im->timer))
170                 atomic_dec(&im->refcnt);
171         im->tm_running=0;
172         im->reporter = 0;
173         im->unsolicit_count = 0;
174         spin_unlock_bh(&im->lock);
175 }
176
177 /* It must be called with locked im->lock */
178 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
179 {
180         int tv=net_random() % max_delay;
181
182         im->tm_running=1;
183         if (!mod_timer(&im->timer, jiffies+tv+2))
184                 atomic_inc(&im->refcnt);
185 }
186
187 static void igmp_gq_start_timer(struct in_device *in_dev)
188 {
189         int tv = net_random() % in_dev->mr_maxdelay;
190
191         in_dev->mr_gq_running = 1;
192         if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
193                 in_dev_hold(in_dev);
194 }
195
196 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
197 {
198         int tv = net_random() % delay;
199
200         if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
201                 in_dev_hold(in_dev);
202 }
203
204 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
205 {
206         spin_lock_bh(&im->lock);
207         im->unsolicit_count = 0;
208         if (del_timer(&im->timer)) {
209                 if ((long)(im->timer.expires-jiffies) < max_delay) {
210                         add_timer(&im->timer);
211                         im->tm_running=1;
212                         spin_unlock_bh(&im->lock);
213                         return;
214                 }
215                 atomic_dec(&im->refcnt);
216         }
217         igmp_start_timer(im, max_delay);
218         spin_unlock_bh(&im->lock);
219 }
220
221
222 /*
223  *      Send an IGMP report.
224  */
225
226 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
227
228
229 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
230         int gdeleted, int sdeleted)
231 {
232         switch (type) {
233         case IGMPV3_MODE_IS_INCLUDE:
234         case IGMPV3_MODE_IS_EXCLUDE:
235                 if (gdeleted || sdeleted)
236                         return 0;
237                 if (!(pmc->gsquery && !psf->sf_gsresp)) {
238                         if (pmc->sfmode == MCAST_INCLUDE)
239                                 return 1;
240                         /* don't include if this source is excluded
241                          * in all filters
242                          */
243                         if (psf->sf_count[MCAST_INCLUDE])
244                                 return type == IGMPV3_MODE_IS_INCLUDE;
245                         return pmc->sfcount[MCAST_EXCLUDE] ==
246                                 psf->sf_count[MCAST_EXCLUDE];
247                 }
248                 return 0;
249         case IGMPV3_CHANGE_TO_INCLUDE:
250                 if (gdeleted || sdeleted)
251                         return 0;
252                 return psf->sf_count[MCAST_INCLUDE] != 0;
253         case IGMPV3_CHANGE_TO_EXCLUDE:
254                 if (gdeleted || sdeleted)
255                         return 0;
256                 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
257                     psf->sf_count[MCAST_INCLUDE])
258                         return 0;
259                 return pmc->sfcount[MCAST_EXCLUDE] ==
260                         psf->sf_count[MCAST_EXCLUDE];
261         case IGMPV3_ALLOW_NEW_SOURCES:
262                 if (gdeleted || !psf->sf_crcount)
263                         return 0;
264                 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
265         case IGMPV3_BLOCK_OLD_SOURCES:
266                 if (pmc->sfmode == MCAST_INCLUDE)
267                         return gdeleted || (psf->sf_crcount && sdeleted);
268                 return psf->sf_crcount && !gdeleted && !sdeleted;
269         }
270         return 0;
271 }
272
273 static int
274 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
275 {
276         struct ip_sf_list *psf;
277         int scount = 0;
278
279         for (psf=pmc->sources; psf; psf=psf->sf_next) {
280                 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
281                         continue;
282                 scount++;
283         }
284         return scount;
285 }
286
287 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
288 {
289         struct sk_buff *skb;
290         struct rtable *rt;
291         struct iphdr *pip;
292         struct igmpv3_report *pig;
293
294         skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
295         if (skb == NULL)
296                 return NULL;
297
298         {
299                 struct flowi fl = { .oif = dev->ifindex,
300                                     .nl_u = { .ip4_u = {
301                                     .daddr = IGMPV3_ALL_MCR } },
302                                     .proto = IPPROTO_IGMP };
303                 if (ip_route_output_key(&rt, &fl)) {
304                         kfree_skb(skb);
305                         return NULL;
306                 }
307         }
308         if (rt->rt_src == 0) {
309                 kfree_skb(skb);
310                 ip_rt_put(rt);
311                 return NULL;
312         }
313
314         skb->dst = &rt->u.dst;
315         skb->dev = dev;
316
317         skb_reserve(skb, LL_RESERVED_SPACE(dev));
318
319         skb_reset_network_header(skb);
320         pip = ip_hdr(skb);
321         skb_put(skb, sizeof(struct iphdr) + 4);
322
323         pip->version  = 4;
324         pip->ihl      = (sizeof(struct iphdr)+4)>>2;
325         pip->tos      = 0xc0;
326         pip->frag_off = htons(IP_DF);
327         pip->ttl      = 1;
328         pip->daddr    = rt->rt_dst;
329         pip->saddr    = rt->rt_src;
330         pip->protocol = IPPROTO_IGMP;
331         pip->tot_len  = 0;      /* filled in later */
332         ip_select_ident(pip, &rt->u.dst, NULL);
333         ((u8*)&pip[1])[0] = IPOPT_RA;
334         ((u8*)&pip[1])[1] = 4;
335         ((u8*)&pip[1])[2] = 0;
336         ((u8*)&pip[1])[3] = 0;
337
338         skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
339         skb_put(skb, sizeof(*pig));
340         pig = igmpv3_report_hdr(skb);
341         pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
342         pig->resv1 = 0;
343         pig->csum = 0;
344         pig->resv2 = 0;
345         pig->ngrec = 0;
346         return skb;
347 }
348
349 static int igmpv3_sendpack(struct sk_buff *skb)
350 {
351         struct iphdr *pip = ip_hdr(skb);
352         struct igmphdr *pig = igmp_hdr(skb);
353         const int iplen = skb->tail - skb->network_header;
354         const int igmplen = skb->tail - skb->transport_header;
355
356         pip->tot_len = htons(iplen);
357         ip_send_check(pip);
358         pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
359
360         return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
361                        dst_output);
362 }
363
364 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
365 {
366         return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
367 }
368
369 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
370         int type, struct igmpv3_grec **ppgr)
371 {
372         struct net_device *dev = pmc->interface->dev;
373         struct igmpv3_report *pih;
374         struct igmpv3_grec *pgr;
375
376         if (!skb)
377                 skb = igmpv3_newpack(dev, dev->mtu);
378         if (!skb)
379                 return NULL;
380         pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
381         pgr->grec_type = type;
382         pgr->grec_auxwords = 0;
383         pgr->grec_nsrcs = 0;
384         pgr->grec_mca = pmc->multiaddr;
385         pih = igmpv3_report_hdr(skb);
386         pih->ngrec = htons(ntohs(pih->ngrec)+1);
387         *ppgr = pgr;
388         return skb;
389 }
390
391 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
392         skb_tailroom(skb)) : 0)
393
394 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
395         int type, int gdeleted, int sdeleted)
396 {
397         struct net_device *dev = pmc->interface->dev;
398         struct igmpv3_report *pih;
399         struct igmpv3_grec *pgr = NULL;
400         struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
401         int scount, stotal, first, isquery, truncate;
402
403         if (pmc->multiaddr == IGMP_ALL_HOSTS)
404                 return skb;
405
406         isquery = type == IGMPV3_MODE_IS_INCLUDE ||
407                   type == IGMPV3_MODE_IS_EXCLUDE;
408         truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
409                     type == IGMPV3_CHANGE_TO_EXCLUDE;
410
411         stotal = scount = 0;
412
413         psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
414
415         if (!*psf_list)
416                 goto empty_source;
417
418         pih = skb ? igmpv3_report_hdr(skb) : NULL;
419
420         /* EX and TO_EX get a fresh packet, if needed */
421         if (truncate) {
422                 if (pih && pih->ngrec &&
423                     AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
424                         if (skb)
425                                 igmpv3_sendpack(skb);
426                         skb = igmpv3_newpack(dev, dev->mtu);
427                 }
428         }
429         first = 1;
430         psf_prev = NULL;
431         for (psf=*psf_list; psf; psf=psf_next) {
432                 __be32 *psrc;
433
434                 psf_next = psf->sf_next;
435
436                 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
437                         psf_prev = psf;
438                         continue;
439                 }
440
441                 /* clear marks on query responses */
442                 if (isquery)
443                         psf->sf_gsresp = 0;
444
445                 if (AVAILABLE(skb) < sizeof(__be32) +
446                     first*sizeof(struct igmpv3_grec)) {
447                         if (truncate && !first)
448                                 break;   /* truncate these */
449                         if (pgr)
450                                 pgr->grec_nsrcs = htons(scount);
451                         if (skb)
452                                 igmpv3_sendpack(skb);
453                         skb = igmpv3_newpack(dev, dev->mtu);
454                         first = 1;
455                         scount = 0;
456                 }
457                 if (first) {
458                         skb = add_grhead(skb, pmc, type, &pgr);
459                         first = 0;
460                 }
461                 if (!skb)
462                         return NULL;
463                 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
464                 *psrc = psf->sf_inaddr;
465                 scount++; stotal++;
466                 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
467                      type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
468                         psf->sf_crcount--;
469                         if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
470                                 if (psf_prev)
471                                         psf_prev->sf_next = psf->sf_next;
472                                 else
473                                         *psf_list = psf->sf_next;
474                                 kfree(psf);
475                                 continue;
476                         }
477                 }
478                 psf_prev = psf;
479         }
480
481 empty_source:
482         if (!stotal) {
483                 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
484                     type == IGMPV3_BLOCK_OLD_SOURCES)
485                         return skb;
486                 if (pmc->crcount || isquery) {
487                         /* make sure we have room for group header */
488                         if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
489                                 igmpv3_sendpack(skb);
490                                 skb = NULL; /* add_grhead will get a new one */
491                         }
492                         skb = add_grhead(skb, pmc, type, &pgr);
493                 }
494         }
495         if (pgr)
496                 pgr->grec_nsrcs = htons(scount);
497
498         if (isquery)
499                 pmc->gsquery = 0;       /* clear query state on report */
500         return skb;
501 }
502
503 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
504 {
505         struct sk_buff *skb = NULL;
506         int type;
507
508         if (!pmc) {
509                 read_lock(&in_dev->mc_list_lock);
510                 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
511                         if (pmc->multiaddr == IGMP_ALL_HOSTS)
512                                 continue;
513                         spin_lock_bh(&pmc->lock);
514                         if (pmc->sfcount[MCAST_EXCLUDE])
515                                 type = IGMPV3_MODE_IS_EXCLUDE;
516                         else
517                                 type = IGMPV3_MODE_IS_INCLUDE;
518                         skb = add_grec(skb, pmc, type, 0, 0);
519                         spin_unlock_bh(&pmc->lock);
520                 }
521                 read_unlock(&in_dev->mc_list_lock);
522         } else {
523                 spin_lock_bh(&pmc->lock);
524                 if (pmc->sfcount[MCAST_EXCLUDE])
525                         type = IGMPV3_MODE_IS_EXCLUDE;
526                 else
527                         type = IGMPV3_MODE_IS_INCLUDE;
528                 skb = add_grec(skb, pmc, type, 0, 0);
529                 spin_unlock_bh(&pmc->lock);
530         }
531         if (!skb)
532                 return 0;
533         return igmpv3_sendpack(skb);
534 }
535
536 /*
537  * remove zero-count source records from a source filter list
538  */
539 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
540 {
541         struct ip_sf_list *psf_prev, *psf_next, *psf;
542
543         psf_prev = NULL;
544         for (psf=*ppsf; psf; psf = psf_next) {
545                 psf_next = psf->sf_next;
546                 if (psf->sf_crcount == 0) {
547                         if (psf_prev)
548                                 psf_prev->sf_next = psf->sf_next;
549                         else
550                                 *ppsf = psf->sf_next;
551                         kfree(psf);
552                 } else
553                         psf_prev = psf;
554         }
555 }
556
557 static void igmpv3_send_cr(struct in_device *in_dev)
558 {
559         struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
560         struct sk_buff *skb = NULL;
561         int type, dtype;
562
563         read_lock(&in_dev->mc_list_lock);
564         spin_lock_bh(&in_dev->mc_tomb_lock);
565
566         /* deleted MCA's */
567         pmc_prev = NULL;
568         for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
569                 pmc_next = pmc->next;
570                 if (pmc->sfmode == MCAST_INCLUDE) {
571                         type = IGMPV3_BLOCK_OLD_SOURCES;
572                         dtype = IGMPV3_BLOCK_OLD_SOURCES;
573                         skb = add_grec(skb, pmc, type, 1, 0);
574                         skb = add_grec(skb, pmc, dtype, 1, 1);
575                 }
576                 if (pmc->crcount) {
577                         if (pmc->sfmode == MCAST_EXCLUDE) {
578                                 type = IGMPV3_CHANGE_TO_INCLUDE;
579                                 skb = add_grec(skb, pmc, type, 1, 0);
580                         }
581                         pmc->crcount--;
582                         if (pmc->crcount == 0) {
583                                 igmpv3_clear_zeros(&pmc->tomb);
584                                 igmpv3_clear_zeros(&pmc->sources);
585                         }
586                 }
587                 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
588                         if (pmc_prev)
589                                 pmc_prev->next = pmc_next;
590                         else
591                                 in_dev->mc_tomb = pmc_next;
592                         in_dev_put(pmc->interface);
593                         kfree(pmc);
594                 } else
595                         pmc_prev = pmc;
596         }
597         spin_unlock_bh(&in_dev->mc_tomb_lock);
598
599         /* change recs */
600         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
601                 spin_lock_bh(&pmc->lock);
602                 if (pmc->sfcount[MCAST_EXCLUDE]) {
603                         type = IGMPV3_BLOCK_OLD_SOURCES;
604                         dtype = IGMPV3_ALLOW_NEW_SOURCES;
605                 } else {
606                         type = IGMPV3_ALLOW_NEW_SOURCES;
607                         dtype = IGMPV3_BLOCK_OLD_SOURCES;
608                 }
609                 skb = add_grec(skb, pmc, type, 0, 0);
610                 skb = add_grec(skb, pmc, dtype, 0, 1);  /* deleted sources */
611
612                 /* filter mode changes */
613                 if (pmc->crcount) {
614                         if (pmc->sfmode == MCAST_EXCLUDE)
615                                 type = IGMPV3_CHANGE_TO_EXCLUDE;
616                         else
617                                 type = IGMPV3_CHANGE_TO_INCLUDE;
618                         skb = add_grec(skb, pmc, type, 0, 0);
619                         pmc->crcount--;
620                 }
621                 spin_unlock_bh(&pmc->lock);
622         }
623         read_unlock(&in_dev->mc_list_lock);
624
625         if (!skb)
626                 return;
627         (void) igmpv3_sendpack(skb);
628 }
629
630 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
631         int type)
632 {
633         struct sk_buff *skb;
634         struct iphdr *iph;
635         struct igmphdr *ih;
636         struct rtable *rt;
637         struct net_device *dev = in_dev->dev;
638         __be32  group = pmc ? pmc->multiaddr : 0;
639         __be32  dst;
640
641         if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
642                 return igmpv3_send_report(in_dev, pmc);
643         else if (type == IGMP_HOST_LEAVE_MESSAGE)
644                 dst = IGMP_ALL_ROUTER;
645         else
646                 dst = group;
647
648         {
649                 struct flowi fl = { .oif = dev->ifindex,
650                                     .nl_u = { .ip4_u = { .daddr = dst } },
651                                     .proto = IPPROTO_IGMP };
652                 if (ip_route_output_key(&rt, &fl))
653                         return -1;
654         }
655         if (rt->rt_src == 0) {
656                 ip_rt_put(rt);
657                 return -1;
658         }
659
660         skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
661         if (skb == NULL) {
662                 ip_rt_put(rt);
663                 return -1;
664         }
665
666         skb->dst = &rt->u.dst;
667
668         skb_reserve(skb, LL_RESERVED_SPACE(dev));
669
670         skb_reset_network_header(skb);
671         iph = ip_hdr(skb);
672         skb_put(skb, sizeof(struct iphdr) + 4);
673
674         iph->version  = 4;
675         iph->ihl      = (sizeof(struct iphdr)+4)>>2;
676         iph->tos      = 0xc0;
677         iph->frag_off = htons(IP_DF);
678         iph->ttl      = 1;
679         iph->daddr    = dst;
680         iph->saddr    = rt->rt_src;
681         iph->protocol = IPPROTO_IGMP;
682         iph->tot_len  = htons(IGMP_SIZE);
683         ip_select_ident(iph, &rt->u.dst, NULL);
684         ((u8*)&iph[1])[0] = IPOPT_RA;
685         ((u8*)&iph[1])[1] = 4;
686         ((u8*)&iph[1])[2] = 0;
687         ((u8*)&iph[1])[3] = 0;
688         ip_send_check(iph);
689
690         ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
691         ih->type=type;
692         ih->code=0;
693         ih->csum=0;
694         ih->group=group;
695         ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
696
697         return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
698                        dst_output);
699 }
700
701 static void igmp_gq_timer_expire(unsigned long data)
702 {
703         struct in_device *in_dev = (struct in_device *)data;
704
705         in_dev->mr_gq_running = 0;
706         igmpv3_send_report(in_dev, NULL);
707         __in_dev_put(in_dev);
708 }
709
710 static void igmp_ifc_timer_expire(unsigned long data)
711 {
712         struct in_device *in_dev = (struct in_device *)data;
713
714         igmpv3_send_cr(in_dev);
715         if (in_dev->mr_ifc_count) {
716                 in_dev->mr_ifc_count--;
717                 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
718         }
719         __in_dev_put(in_dev);
720 }
721
722 static void igmp_ifc_event(struct in_device *in_dev)
723 {
724         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
725                 return;
726         in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
727                 IGMP_Unsolicited_Report_Count;
728         igmp_ifc_start_timer(in_dev, 1);
729 }
730
731
732 static void igmp_timer_expire(unsigned long data)
733 {
734         struct ip_mc_list *im=(struct ip_mc_list *)data;
735         struct in_device *in_dev = im->interface;
736
737         spin_lock(&im->lock);
738         im->tm_running=0;
739
740         if (im->unsolicit_count) {
741                 im->unsolicit_count--;
742                 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
743         }
744         im->reporter = 1;
745         spin_unlock(&im->lock);
746
747         if (IGMP_V1_SEEN(in_dev))
748                 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
749         else if (IGMP_V2_SEEN(in_dev))
750                 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
751         else
752                 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
753
754         ip_ma_put(im);
755 }
756
757 /* mark EXCLUDE-mode sources */
758 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
759 {
760         struct ip_sf_list *psf;
761         int i, scount;
762
763         scount = 0;
764         for (psf=pmc->sources; psf; psf=psf->sf_next) {
765                 if (scount == nsrcs)
766                         break;
767                 for (i=0; i<nsrcs; i++) {
768                         /* skip inactive filters */
769                         if (pmc->sfcount[MCAST_INCLUDE] ||
770                             pmc->sfcount[MCAST_EXCLUDE] !=
771                             psf->sf_count[MCAST_EXCLUDE])
772                                 continue;
773                         if (srcs[i] == psf->sf_inaddr) {
774                                 scount++;
775                                 break;
776                         }
777                 }
778         }
779         pmc->gsquery = 0;
780         if (scount == nsrcs)    /* all sources excluded */
781                 return 0;
782         return 1;
783 }
784
785 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
786 {
787         struct ip_sf_list *psf;
788         int i, scount;
789
790         if (pmc->sfmode == MCAST_EXCLUDE)
791                 return igmp_xmarksources(pmc, nsrcs, srcs);
792
793         /* mark INCLUDE-mode sources */
794         scount = 0;
795         for (psf=pmc->sources; psf; psf=psf->sf_next) {
796                 if (scount == nsrcs)
797                         break;
798                 for (i=0; i<nsrcs; i++)
799                         if (srcs[i] == psf->sf_inaddr) {
800                                 psf->sf_gsresp = 1;
801                                 scount++;
802                                 break;
803                         }
804         }
805         if (!scount) {
806                 pmc->gsquery = 0;
807                 return 0;
808         }
809         pmc->gsquery = 1;
810         return 1;
811 }
812
813 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
814 {
815         struct ip_mc_list *im;
816
817         /* Timers are only set for non-local groups */
818
819         if (group == IGMP_ALL_HOSTS)
820                 return;
821
822         read_lock(&in_dev->mc_list_lock);
823         for (im=in_dev->mc_list; im!=NULL; im=im->next) {
824                 if (im->multiaddr == group) {
825                         igmp_stop_timer(im);
826                         break;
827                 }
828         }
829         read_unlock(&in_dev->mc_list_lock);
830 }
831
832 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
833         int len)
834 {
835         struct igmphdr          *ih = igmp_hdr(skb);
836         struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
837         struct ip_mc_list       *im;
838         __be32                  group = ih->group;
839         int                     max_delay;
840         int                     mark = 0;
841
842
843         if (len == 8) {
844                 if (ih->code == 0) {
845                         /* Alas, old v1 router presents here. */
846
847                         max_delay = IGMP_Query_Response_Interval;
848                         in_dev->mr_v1_seen = jiffies +
849                                 IGMP_V1_Router_Present_Timeout;
850                         group = 0;
851                 } else {
852                         /* v2 router present */
853                         max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
854                         in_dev->mr_v2_seen = jiffies +
855                                 IGMP_V2_Router_Present_Timeout;
856                 }
857                 /* cancel the interface change timer */
858                 in_dev->mr_ifc_count = 0;
859                 if (del_timer(&in_dev->mr_ifc_timer))
860                         __in_dev_put(in_dev);
861                 /* clear deleted report items */
862                 igmpv3_clear_delrec(in_dev);
863         } else if (len < 12) {
864                 return; /* ignore bogus packet; freed by caller */
865         } else { /* v3 */
866                 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
867                         return;
868
869                 ih3 = igmpv3_query_hdr(skb);
870                 if (ih3->nsrcs) {
871                         if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
872                                            + ntohs(ih3->nsrcs)*sizeof(__be32)))
873                                 return;
874                         ih3 = igmpv3_query_hdr(skb);
875                 }
876
877                 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
878                 if (!max_delay)
879                         max_delay = 1;  /* can't mod w/ 0 */
880                 in_dev->mr_maxdelay = max_delay;
881                 if (ih3->qrv)
882                         in_dev->mr_qrv = ih3->qrv;
883                 if (!group) { /* general query */
884                         if (ih3->nsrcs)
885                                 return; /* no sources allowed */
886                         igmp_gq_start_timer(in_dev);
887                         return;
888                 }
889                 /* mark sources to include, if group & source-specific */
890                 mark = ih3->nsrcs != 0;
891         }
892
893         /*
894          * - Start the timers in all of our membership records
895          *   that the query applies to for the interface on
896          *   which the query arrived excl. those that belong
897          *   to a "local" group (224.0.0.X)
898          * - For timers already running check if they need to
899          *   be reset.
900          * - Use the igmp->igmp_code field as the maximum
901          *   delay possible
902          */
903         read_lock(&in_dev->mc_list_lock);
904         for (im=in_dev->mc_list; im!=NULL; im=im->next) {
905                 int changed;
906
907                 if (group && group != im->multiaddr)
908                         continue;
909                 if (im->multiaddr == IGMP_ALL_HOSTS)
910                         continue;
911                 spin_lock_bh(&im->lock);
912                 if (im->tm_running)
913                         im->gsquery = im->gsquery && mark;
914                 else
915                         im->gsquery = mark;
916                 changed = !im->gsquery ||
917                         igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
918                 spin_unlock_bh(&im->lock);
919                 if (changed)
920                         igmp_mod_timer(im, max_delay);
921         }
922         read_unlock(&in_dev->mc_list_lock);
923 }
924
925 int igmp_rcv(struct sk_buff *skb)
926 {
927         /* This basically follows the spec line by line -- see RFC1112 */
928         struct igmphdr *ih;
929         struct in_device *in_dev = in_dev_get(skb->dev);
930         int len = skb->len;
931
932         if (in_dev==NULL) {
933                 kfree_skb(skb);
934                 return 0;
935         }
936
937         if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
938                 goto drop;
939
940         switch (skb->ip_summed) {
941         case CHECKSUM_COMPLETE:
942                 if (!csum_fold(skb->csum))
943                         break;
944                 /* fall through */
945         case CHECKSUM_NONE:
946                 skb->csum = 0;
947                 if (__skb_checksum_complete(skb))
948                         goto drop;
949         }
950
951         ih = igmp_hdr(skb);
952         switch (ih->type) {
953         case IGMP_HOST_MEMBERSHIP_QUERY:
954                 igmp_heard_query(in_dev, skb, len);
955                 break;
956         case IGMP_HOST_MEMBERSHIP_REPORT:
957         case IGMPV2_HOST_MEMBERSHIP_REPORT:
958         case IGMPV3_HOST_MEMBERSHIP_REPORT:
959                 /* Is it our report looped back? */
960                 if (((struct rtable*)skb->dst)->fl.iif == 0)
961                         break;
962                 /* don't rely on MC router hearing unicast reports */
963                 if (skb->pkt_type == PACKET_MULTICAST ||
964                     skb->pkt_type == PACKET_BROADCAST)
965                         igmp_heard_report(in_dev, ih->group);
966                 break;
967         case IGMP_PIM:
968 #ifdef CONFIG_IP_PIMSM_V1
969                 in_dev_put(in_dev);
970                 return pim_rcv_v1(skb);
971 #endif
972         case IGMP_DVMRP:
973         case IGMP_TRACE:
974         case IGMP_HOST_LEAVE_MESSAGE:
975         case IGMP_MTRACE:
976         case IGMP_MTRACE_RESP:
977                 break;
978         default:
979                 break;
980         }
981
982 drop:
983         in_dev_put(in_dev);
984         kfree_skb(skb);
985         return 0;
986 }
987
988 #endif
989
990
991 /*
992  *      Add a filter to a device
993  */
994
995 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
996 {
997         char buf[MAX_ADDR_LEN];
998         struct net_device *dev = in_dev->dev;
999
1000         /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1001            We will get multicast token leakage, when IFF_MULTICAST
1002            is changed. This check should be done in dev->set_multicast_list
1003            routine. Something sort of:
1004            if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1005            --ANK
1006            */
1007         if (arp_mc_map(addr, buf, dev, 0) == 0)
1008                 dev_mc_add(dev,buf,dev->addr_len,0);
1009 }
1010
1011 /*
1012  *      Remove a filter from a device
1013  */
1014
1015 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1016 {
1017         char buf[MAX_ADDR_LEN];
1018         struct net_device *dev = in_dev->dev;
1019
1020         if (arp_mc_map(addr, buf, dev, 0) == 0)
1021                 dev_mc_delete(dev,buf,dev->addr_len,0);
1022 }
1023
1024 #ifdef CONFIG_IP_MULTICAST
1025 /*
1026  * deleted ip_mc_list manipulation
1027  */
1028 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1029 {
1030         struct ip_mc_list *pmc;
1031
1032         /* this is an "ip_mc_list" for convenience; only the fields below
1033          * are actually used. In particular, the refcnt and users are not
1034          * used for management of the delete list. Using the same structure
1035          * for deleted items allows change reports to use common code with
1036          * non-deleted or query-response MCA's.
1037          */
1038         pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1039         if (!pmc)
1040                 return;
1041         spin_lock_bh(&im->lock);
1042         pmc->interface = im->interface;
1043         in_dev_hold(in_dev);
1044         pmc->multiaddr = im->multiaddr;
1045         pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1046                 IGMP_Unsolicited_Report_Count;
1047         pmc->sfmode = im->sfmode;
1048         if (pmc->sfmode == MCAST_INCLUDE) {
1049                 struct ip_sf_list *psf;
1050
1051                 pmc->tomb = im->tomb;
1052                 pmc->sources = im->sources;
1053                 im->tomb = im->sources = NULL;
1054                 for (psf=pmc->sources; psf; psf=psf->sf_next)
1055                         psf->sf_crcount = pmc->crcount;
1056         }
1057         spin_unlock_bh(&im->lock);
1058
1059         spin_lock_bh(&in_dev->mc_tomb_lock);
1060         pmc->next = in_dev->mc_tomb;
1061         in_dev->mc_tomb = pmc;
1062         spin_unlock_bh(&in_dev->mc_tomb_lock);
1063 }
1064
1065 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1066 {
1067         struct ip_mc_list *pmc, *pmc_prev;
1068         struct ip_sf_list *psf, *psf_next;
1069
1070         spin_lock_bh(&in_dev->mc_tomb_lock);
1071         pmc_prev = NULL;
1072         for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1073                 if (pmc->multiaddr == multiaddr)
1074                         break;
1075                 pmc_prev = pmc;
1076         }
1077         if (pmc) {
1078                 if (pmc_prev)
1079                         pmc_prev->next = pmc->next;
1080                 else
1081                         in_dev->mc_tomb = pmc->next;
1082         }
1083         spin_unlock_bh(&in_dev->mc_tomb_lock);
1084         if (pmc) {
1085                 for (psf=pmc->tomb; psf; psf=psf_next) {
1086                         psf_next = psf->sf_next;
1087                         kfree(psf);
1088                 }
1089                 in_dev_put(pmc->interface);
1090                 kfree(pmc);
1091         }
1092 }
1093
1094 static void igmpv3_clear_delrec(struct in_device *in_dev)
1095 {
1096         struct ip_mc_list *pmc, *nextpmc;
1097
1098         spin_lock_bh(&in_dev->mc_tomb_lock);
1099         pmc = in_dev->mc_tomb;
1100         in_dev->mc_tomb = NULL;
1101         spin_unlock_bh(&in_dev->mc_tomb_lock);
1102
1103         for (; pmc; pmc = nextpmc) {
1104                 nextpmc = pmc->next;
1105                 ip_mc_clear_src(pmc);
1106                 in_dev_put(pmc->interface);
1107                 kfree(pmc);
1108         }
1109         /* clear dead sources, too */
1110         read_lock(&in_dev->mc_list_lock);
1111         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1112                 struct ip_sf_list *psf, *psf_next;
1113
1114                 spin_lock_bh(&pmc->lock);
1115                 psf = pmc->tomb;
1116                 pmc->tomb = NULL;
1117                 spin_unlock_bh(&pmc->lock);
1118                 for (; psf; psf=psf_next) {
1119                         psf_next = psf->sf_next;
1120                         kfree(psf);
1121                 }
1122         }
1123         read_unlock(&in_dev->mc_list_lock);
1124 }
1125 #endif
1126
1127 static void igmp_group_dropped(struct ip_mc_list *im)
1128 {
1129         struct in_device *in_dev = im->interface;
1130 #ifdef CONFIG_IP_MULTICAST
1131         int reporter;
1132 #endif
1133
1134         if (im->loaded) {
1135                 im->loaded = 0;
1136                 ip_mc_filter_del(in_dev, im->multiaddr);
1137         }
1138
1139 #ifdef CONFIG_IP_MULTICAST
1140         if (im->multiaddr == IGMP_ALL_HOSTS)
1141                 return;
1142
1143         reporter = im->reporter;
1144         igmp_stop_timer(im);
1145
1146         if (!in_dev->dead) {
1147                 if (IGMP_V1_SEEN(in_dev))
1148                         goto done;
1149                 if (IGMP_V2_SEEN(in_dev)) {
1150                         if (reporter)
1151                                 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1152                         goto done;
1153                 }
1154                 /* IGMPv3 */
1155                 igmpv3_add_delrec(in_dev, im);
1156
1157                 igmp_ifc_event(in_dev);
1158         }
1159 done:
1160 #endif
1161         ip_mc_clear_src(im);
1162 }
1163
1164 static void igmp_group_added(struct ip_mc_list *im)
1165 {
1166         struct in_device *in_dev = im->interface;
1167
1168         if (im->loaded == 0) {
1169                 im->loaded = 1;
1170                 ip_mc_filter_add(in_dev, im->multiaddr);
1171         }
1172
1173 #ifdef CONFIG_IP_MULTICAST
1174         if (im->multiaddr == IGMP_ALL_HOSTS)
1175                 return;
1176
1177         if (in_dev->dead)
1178                 return;
1179         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1180                 spin_lock_bh(&im->lock);
1181                 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1182                 spin_unlock_bh(&im->lock);
1183                 return;
1184         }
1185         /* else, v3 */
1186
1187         im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1188                 IGMP_Unsolicited_Report_Count;
1189         igmp_ifc_event(in_dev);
1190 #endif
1191 }
1192
1193
1194 /*
1195  *      Multicast list managers
1196  */
1197
1198
1199 /*
1200  *      A socket has joined a multicast group on device dev.
1201  */
1202
1203 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1204 {
1205         struct ip_mc_list *im;
1206
1207         ASSERT_RTNL();
1208
1209         for (im=in_dev->mc_list; im; im=im->next) {
1210                 if (im->multiaddr == addr) {
1211                         im->users++;
1212                         ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1213                         goto out;
1214                 }
1215         }
1216
1217         im = kmalloc(sizeof(*im), GFP_KERNEL);
1218         if (!im)
1219                 goto out;
1220
1221         im->users=1;
1222         im->interface=in_dev;
1223         in_dev_hold(in_dev);
1224         im->multiaddr=addr;
1225         /* initial mode is (EX, empty) */
1226         im->sfmode = MCAST_EXCLUDE;
1227         im->sfcount[MCAST_INCLUDE] = 0;
1228         im->sfcount[MCAST_EXCLUDE] = 1;
1229         im->sources = NULL;
1230         im->tomb = NULL;
1231         im->crcount = 0;
1232         atomic_set(&im->refcnt, 1);
1233         spin_lock_init(&im->lock);
1234 #ifdef CONFIG_IP_MULTICAST
1235         im->tm_running=0;
1236         init_timer(&im->timer);
1237         im->timer.data=(unsigned long)im;
1238         im->timer.function=&igmp_timer_expire;
1239         im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1240         im->reporter = 0;
1241         im->gsquery = 0;
1242 #endif
1243         im->loaded = 0;
1244         write_lock_bh(&in_dev->mc_list_lock);
1245         im->next=in_dev->mc_list;
1246         in_dev->mc_list=im;
1247         write_unlock_bh(&in_dev->mc_list_lock);
1248 #ifdef CONFIG_IP_MULTICAST
1249         igmpv3_del_delrec(in_dev, im->multiaddr);
1250 #endif
1251         igmp_group_added(im);
1252         if (!in_dev->dead)
1253                 ip_rt_multicast_event(in_dev);
1254 out:
1255         return;
1256 }
1257
1258 /*
1259  *      Resend IGMP JOIN report; used for bonding.
1260  */
1261 void ip_mc_rejoin_group(struct ip_mc_list *im)
1262 {
1263 #ifdef CONFIG_IP_MULTICAST
1264         struct in_device *in_dev = im->interface;
1265
1266         if (im->multiaddr == IGMP_ALL_HOSTS)
1267                 return;
1268
1269         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1270                 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1271                 return;
1272         }
1273         /* else, v3 */
1274         im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1275                 IGMP_Unsolicited_Report_Count;
1276         igmp_ifc_event(in_dev);
1277 #endif
1278 }
1279
1280 /*
1281  *      A socket has left a multicast group on device dev
1282  */
1283
1284 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1285 {
1286         struct ip_mc_list *i, **ip;
1287
1288         ASSERT_RTNL();
1289
1290         for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1291                 if (i->multiaddr==addr) {
1292                         if (--i->users == 0) {
1293                                 write_lock_bh(&in_dev->mc_list_lock);
1294                                 *ip = i->next;
1295                                 write_unlock_bh(&in_dev->mc_list_lock);
1296                                 igmp_group_dropped(i);
1297
1298                                 if (!in_dev->dead)
1299                                         ip_rt_multicast_event(in_dev);
1300
1301                                 ip_ma_put(i);
1302                                 return;
1303                         }
1304                         break;
1305                 }
1306         }
1307 }
1308
1309 /* Device going down */
1310
1311 void ip_mc_down(struct in_device *in_dev)
1312 {
1313         struct ip_mc_list *i;
1314
1315         ASSERT_RTNL();
1316
1317         for (i=in_dev->mc_list; i; i=i->next)
1318                 igmp_group_dropped(i);
1319
1320 #ifdef CONFIG_IP_MULTICAST
1321         in_dev->mr_ifc_count = 0;
1322         if (del_timer(&in_dev->mr_ifc_timer))
1323                 __in_dev_put(in_dev);
1324         in_dev->mr_gq_running = 0;
1325         if (del_timer(&in_dev->mr_gq_timer))
1326                 __in_dev_put(in_dev);
1327         igmpv3_clear_delrec(in_dev);
1328 #endif
1329
1330         ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1331 }
1332
1333 void ip_mc_init_dev(struct in_device *in_dev)
1334 {
1335         ASSERT_RTNL();
1336
1337         in_dev->mc_tomb = NULL;
1338 #ifdef CONFIG_IP_MULTICAST
1339         in_dev->mr_gq_running = 0;
1340         init_timer(&in_dev->mr_gq_timer);
1341         in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1342         in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1343         in_dev->mr_ifc_count = 0;
1344         init_timer(&in_dev->mr_ifc_timer);
1345         in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1346         in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1347         in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1348 #endif
1349
1350         rwlock_init(&in_dev->mc_list_lock);
1351         spin_lock_init(&in_dev->mc_tomb_lock);
1352 }
1353
1354 /* Device going up */
1355
1356 void ip_mc_up(struct in_device *in_dev)
1357 {
1358         struct ip_mc_list *i;
1359
1360         ASSERT_RTNL();
1361
1362         ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1363
1364         for (i=in_dev->mc_list; i; i=i->next)
1365                 igmp_group_added(i);
1366 }
1367
1368 /*
1369  *      Device is about to be destroyed: clean up.
1370  */
1371
1372 void ip_mc_destroy_dev(struct in_device *in_dev)
1373 {
1374         struct ip_mc_list *i;
1375
1376         ASSERT_RTNL();
1377
1378         /* Deactivate timers */
1379         ip_mc_down(in_dev);
1380
1381         write_lock_bh(&in_dev->mc_list_lock);
1382         while ((i = in_dev->mc_list) != NULL) {
1383                 in_dev->mc_list = i->next;
1384                 write_unlock_bh(&in_dev->mc_list_lock);
1385
1386                 igmp_group_dropped(i);
1387                 ip_ma_put(i);
1388
1389                 write_lock_bh(&in_dev->mc_list_lock);
1390         }
1391         write_unlock_bh(&in_dev->mc_list_lock);
1392 }
1393
1394 static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1395 {
1396         struct flowi fl = { .nl_u = { .ip4_u =
1397                                       { .daddr = imr->imr_multiaddr.s_addr } } };
1398         struct rtable *rt;
1399         struct net_device *dev = NULL;
1400         struct in_device *idev = NULL;
1401
1402         if (imr->imr_ifindex) {
1403                 idev = inetdev_by_index(imr->imr_ifindex);
1404                 if (idev)
1405                         __in_dev_put(idev);
1406                 return idev;
1407         }
1408         if (imr->imr_address.s_addr) {
1409                 dev = ip_dev_find(imr->imr_address.s_addr);
1410                 if (!dev)
1411                         return NULL;
1412                 dev_put(dev);
1413         }
1414
1415         if (!dev && !ip_route_output_key(&rt, &fl)) {
1416                 dev = rt->u.dst.dev;
1417                 ip_rt_put(rt);
1418         }
1419         if (dev) {
1420                 imr->imr_ifindex = dev->ifindex;
1421                 idev = __in_dev_get_rtnl(dev);
1422         }
1423         return idev;
1424 }
1425
1426 /*
1427  *      Join a socket to a group
1428  */
1429 int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1430 int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
1431
1432
1433 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1434         __be32 *psfsrc)
1435 {
1436         struct ip_sf_list *psf, *psf_prev;
1437         int rv = 0;
1438
1439         psf_prev = NULL;
1440         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1441                 if (psf->sf_inaddr == *psfsrc)
1442                         break;
1443                 psf_prev = psf;
1444         }
1445         if (!psf || psf->sf_count[sfmode] == 0) {
1446                 /* source filter not found, or count wrong =>  bug */
1447                 return -ESRCH;
1448         }
1449         psf->sf_count[sfmode]--;
1450         if (psf->sf_count[sfmode] == 0) {
1451                 ip_rt_multicast_event(pmc->interface);
1452         }
1453         if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1454 #ifdef CONFIG_IP_MULTICAST
1455                 struct in_device *in_dev = pmc->interface;
1456 #endif
1457
1458                 /* no more filters for this source */
1459                 if (psf_prev)
1460                         psf_prev->sf_next = psf->sf_next;
1461                 else
1462                         pmc->sources = psf->sf_next;
1463 #ifdef CONFIG_IP_MULTICAST
1464                 if (psf->sf_oldin &&
1465                     !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1466                         psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1467                                 IGMP_Unsolicited_Report_Count;
1468                         psf->sf_next = pmc->tomb;
1469                         pmc->tomb = psf;
1470                         rv = 1;
1471                 } else
1472 #endif
1473                         kfree(psf);
1474         }
1475         return rv;
1476 }
1477
1478 #ifndef CONFIG_IP_MULTICAST
1479 #define igmp_ifc_event(x)       do { } while (0)
1480 #endif
1481
1482 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1483                          int sfcount, __be32 *psfsrc, int delta)
1484 {
1485         struct ip_mc_list *pmc;
1486         int     changerec = 0;
1487         int     i, err;
1488
1489         if (!in_dev)
1490                 return -ENODEV;
1491         read_lock(&in_dev->mc_list_lock);
1492         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1493                 if (*pmca == pmc->multiaddr)
1494                         break;
1495         }
1496         if (!pmc) {
1497                 /* MCA not found?? bug */
1498                 read_unlock(&in_dev->mc_list_lock);
1499                 return -ESRCH;
1500         }
1501         spin_lock_bh(&pmc->lock);
1502         read_unlock(&in_dev->mc_list_lock);
1503 #ifdef CONFIG_IP_MULTICAST
1504         sf_markstate(pmc);
1505 #endif
1506         if (!delta) {
1507                 err = -EINVAL;
1508                 if (!pmc->sfcount[sfmode])
1509                         goto out_unlock;
1510                 pmc->sfcount[sfmode]--;
1511         }
1512         err = 0;
1513         for (i=0; i<sfcount; i++) {
1514                 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1515
1516                 changerec |= rv > 0;
1517                 if (!err && rv < 0)
1518                         err = rv;
1519         }
1520         if (pmc->sfmode == MCAST_EXCLUDE &&
1521             pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1522             pmc->sfcount[MCAST_INCLUDE]) {
1523 #ifdef CONFIG_IP_MULTICAST
1524                 struct ip_sf_list *psf;
1525 #endif
1526
1527                 /* filter mode change */
1528                 pmc->sfmode = MCAST_INCLUDE;
1529 #ifdef CONFIG_IP_MULTICAST
1530                 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1531                         IGMP_Unsolicited_Report_Count;
1532                 in_dev->mr_ifc_count = pmc->crcount;
1533                 for (psf=pmc->sources; psf; psf = psf->sf_next)
1534                         psf->sf_crcount = 0;
1535                 igmp_ifc_event(pmc->interface);
1536         } else if (sf_setstate(pmc) || changerec) {
1537                 igmp_ifc_event(pmc->interface);
1538 #endif
1539         }
1540 out_unlock:
1541         spin_unlock_bh(&pmc->lock);
1542         return err;
1543 }
1544
1545 /*
1546  * Add multicast single-source filter to the interface list
1547  */
1548 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1549         __be32 *psfsrc, int delta)
1550 {
1551         struct ip_sf_list *psf, *psf_prev;
1552
1553         psf_prev = NULL;
1554         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1555                 if (psf->sf_inaddr == *psfsrc)
1556                         break;
1557                 psf_prev = psf;
1558         }
1559         if (!psf) {
1560                 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1561                 if (!psf)
1562                         return -ENOBUFS;
1563                 psf->sf_inaddr = *psfsrc;
1564                 if (psf_prev) {
1565                         psf_prev->sf_next = psf;
1566                 } else
1567                         pmc->sources = psf;
1568         }
1569         psf->sf_count[sfmode]++;
1570         if (psf->sf_count[sfmode] == 1) {
1571                 ip_rt_multicast_event(pmc->interface);
1572         }
1573         return 0;
1574 }
1575
1576 #ifdef CONFIG_IP_MULTICAST
1577 static void sf_markstate(struct ip_mc_list *pmc)
1578 {
1579         struct ip_sf_list *psf;
1580         int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1581
1582         for (psf=pmc->sources; psf; psf=psf->sf_next)
1583                 if (pmc->sfcount[MCAST_EXCLUDE]) {
1584                         psf->sf_oldin = mca_xcount ==
1585                                 psf->sf_count[MCAST_EXCLUDE] &&
1586                                 !psf->sf_count[MCAST_INCLUDE];
1587                 } else
1588                         psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1589 }
1590
1591 static int sf_setstate(struct ip_mc_list *pmc)
1592 {
1593         struct ip_sf_list *psf, *dpsf;
1594         int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1595         int qrv = pmc->interface->mr_qrv;
1596         int new_in, rv;
1597
1598         rv = 0;
1599         for (psf=pmc->sources; psf; psf=psf->sf_next) {
1600                 if (pmc->sfcount[MCAST_EXCLUDE]) {
1601                         new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1602                                 !psf->sf_count[MCAST_INCLUDE];
1603                 } else
1604                         new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1605                 if (new_in) {
1606                         if (!psf->sf_oldin) {
1607                                 struct ip_sf_list *prev = NULL;
1608
1609                                 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1610                                         if (dpsf->sf_inaddr == psf->sf_inaddr)
1611                                                 break;
1612                                         prev = dpsf;
1613                                 }
1614                                 if (dpsf) {
1615                                         if (prev)
1616                                                 prev->sf_next = dpsf->sf_next;
1617                                         else
1618                                                 pmc->tomb = dpsf->sf_next;
1619                                         kfree(dpsf);
1620                                 }
1621                                 psf->sf_crcount = qrv;
1622                                 rv++;
1623                         }
1624                 } else if (psf->sf_oldin) {
1625
1626                         psf->sf_crcount = 0;
1627                         /*
1628                          * add or update "delete" records if an active filter
1629                          * is now inactive
1630                          */
1631                         for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1632                                 if (dpsf->sf_inaddr == psf->sf_inaddr)
1633                                         break;
1634                         if (!dpsf) {
1635                                 dpsf = (struct ip_sf_list *)
1636                                         kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1637                                 if (!dpsf)
1638                                         continue;
1639                                 *dpsf = *psf;
1640                                 /* pmc->lock held by callers */
1641                                 dpsf->sf_next = pmc->tomb;
1642                                 pmc->tomb = dpsf;
1643                         }
1644                         dpsf->sf_crcount = qrv;
1645                         rv++;
1646                 }
1647         }
1648         return rv;
1649 }
1650 #endif
1651
1652 /*
1653  * Add multicast source filter list to the interface list
1654  */
1655 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1656                          int sfcount, __be32 *psfsrc, int delta)
1657 {
1658         struct ip_mc_list *pmc;
1659         int     isexclude;
1660         int     i, err;
1661
1662         if (!in_dev)
1663                 return -ENODEV;
1664         read_lock(&in_dev->mc_list_lock);
1665         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1666                 if (*pmca == pmc->multiaddr)
1667                         break;
1668         }
1669         if (!pmc) {
1670                 /* MCA not found?? bug */
1671                 read_unlock(&in_dev->mc_list_lock);
1672                 return -ESRCH;
1673         }
1674         spin_lock_bh(&pmc->lock);
1675         read_unlock(&in_dev->mc_list_lock);
1676
1677 #ifdef CONFIG_IP_MULTICAST
1678         sf_markstate(pmc);
1679 #endif
1680         isexclude = pmc->sfmode == MCAST_EXCLUDE;
1681         if (!delta)
1682                 pmc->sfcount[sfmode]++;
1683         err = 0;
1684         for (i=0; i<sfcount; i++) {
1685                 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1686                 if (err)
1687                         break;
1688         }
1689         if (err) {
1690                 int j;
1691
1692                 pmc->sfcount[sfmode]--;
1693                 for (j=0; j<i; j++)
1694                         (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1695         } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1696 #ifdef CONFIG_IP_MULTICAST
1697                 struct in_device *in_dev = pmc->interface;
1698                 struct ip_sf_list *psf;
1699 #endif
1700
1701                 /* filter mode change */
1702                 if (pmc->sfcount[MCAST_EXCLUDE])
1703                         pmc->sfmode = MCAST_EXCLUDE;
1704                 else if (pmc->sfcount[MCAST_INCLUDE])
1705                         pmc->sfmode = MCAST_INCLUDE;
1706 #ifdef CONFIG_IP_MULTICAST
1707                 /* else no filters; keep old mode for reports */
1708
1709                 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1710                         IGMP_Unsolicited_Report_Count;
1711                 in_dev->mr_ifc_count = pmc->crcount;
1712                 for (psf=pmc->sources; psf; psf = psf->sf_next)
1713                         psf->sf_crcount = 0;
1714                 igmp_ifc_event(in_dev);
1715         } else if (sf_setstate(pmc)) {
1716                 igmp_ifc_event(in_dev);
1717 #endif
1718         }
1719         spin_unlock_bh(&pmc->lock);
1720         return err;
1721 }
1722
1723 static void ip_mc_clear_src(struct ip_mc_list *pmc)
1724 {
1725         struct ip_sf_list *psf, *nextpsf;
1726
1727         for (psf=pmc->tomb; psf; psf=nextpsf) {
1728                 nextpsf = psf->sf_next;
1729                 kfree(psf);
1730         }
1731         pmc->tomb = NULL;
1732         for (psf=pmc->sources; psf; psf=nextpsf) {
1733                 nextpsf = psf->sf_next;
1734                 kfree(psf);
1735         }
1736         pmc->sources = NULL;
1737         pmc->sfmode = MCAST_EXCLUDE;
1738         pmc->sfcount[MCAST_INCLUDE] = 0;
1739         pmc->sfcount[MCAST_EXCLUDE] = 1;
1740 }
1741
1742
1743 /*
1744  * Join a multicast group
1745  */
1746 int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1747 {
1748         int err;
1749         __be32 addr = imr->imr_multiaddr.s_addr;
1750         struct ip_mc_socklist *iml=NULL, *i;
1751         struct in_device *in_dev;
1752         struct inet_sock *inet = inet_sk(sk);
1753         int ifindex;
1754         int count = 0;
1755
1756         if (!MULTICAST(addr))
1757                 return -EINVAL;
1758
1759         rtnl_lock();
1760
1761         in_dev = ip_mc_find_dev(imr);
1762
1763         if (!in_dev) {
1764                 iml = NULL;
1765                 err = -ENODEV;
1766                 goto done;
1767         }
1768
1769         err = -EADDRINUSE;
1770         ifindex = imr->imr_ifindex;
1771         for (i = inet->mc_list; i; i = i->next) {
1772                 if (i->multi.imr_multiaddr.s_addr == addr &&
1773                     i->multi.imr_ifindex == ifindex)
1774                         goto done;
1775                 count++;
1776         }
1777         err = -ENOBUFS;
1778         if (count >= sysctl_igmp_max_memberships)
1779                 goto done;
1780         iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
1781         if (iml == NULL)
1782                 goto done;
1783
1784         memcpy(&iml->multi, imr, sizeof(*imr));
1785         iml->next = inet->mc_list;
1786         iml->sflist = NULL;
1787         iml->sfmode = MCAST_EXCLUDE;
1788         inet->mc_list = iml;
1789         ip_mc_inc_group(in_dev, addr);
1790         err = 0;
1791 done:
1792         rtnl_unlock();
1793         return err;
1794 }
1795
1796 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1797                            struct in_device *in_dev)
1798 {
1799         int err;
1800
1801         if (iml->sflist == 0) {
1802                 /* any-source empty exclude case */
1803                 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1804                         iml->sfmode, 0, NULL, 0);
1805         }
1806         err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1807                         iml->sfmode, iml->sflist->sl_count,
1808                         iml->sflist->sl_addr, 0);
1809         sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1810         iml->sflist = NULL;
1811         return err;
1812 }
1813
1814 /*
1815  *      Ask a socket to leave a group.
1816  */
1817
1818 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1819 {
1820         struct inet_sock *inet = inet_sk(sk);
1821         struct ip_mc_socklist *iml, **imlp;
1822         struct in_device *in_dev;
1823         __be32 group = imr->imr_multiaddr.s_addr;
1824         u32 ifindex;
1825         int ret = -EADDRNOTAVAIL;
1826
1827         rtnl_lock();
1828         in_dev = ip_mc_find_dev(imr);
1829         ifindex = imr->imr_ifindex;
1830         for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1831                 if (iml->multi.imr_multiaddr.s_addr != group)
1832                         continue;
1833                 if (ifindex) {
1834                         if (iml->multi.imr_ifindex != ifindex)
1835                                 continue;
1836                 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1837                                 iml->multi.imr_address.s_addr)
1838                         continue;
1839
1840                 (void) ip_mc_leave_src(sk, iml, in_dev);
1841
1842                 *imlp = iml->next;
1843
1844                 if (in_dev)
1845                         ip_mc_dec_group(in_dev, group);
1846                 rtnl_unlock();
1847                 sock_kfree_s(sk, iml, sizeof(*iml));
1848                 return 0;
1849         }
1850         if (!in_dev)
1851                 ret = -ENODEV;
1852         rtnl_unlock();
1853         return ret;
1854 }
1855
1856 int ip_mc_source(int add, int omode, struct sock *sk, struct
1857         ip_mreq_source *mreqs, int ifindex)
1858 {
1859         int err;
1860         struct ip_mreqn imr;
1861         __be32 addr = mreqs->imr_multiaddr;
1862         struct ip_mc_socklist *pmc;
1863         struct in_device *in_dev = NULL;
1864         struct inet_sock *inet = inet_sk(sk);
1865         struct ip_sf_socklist *psl;
1866         int leavegroup = 0;
1867         int i, j, rv;
1868
1869         if (!MULTICAST(addr))
1870                 return -EINVAL;
1871
1872         rtnl_lock();
1873
1874         imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1875         imr.imr_address.s_addr = mreqs->imr_interface;
1876         imr.imr_ifindex = ifindex;
1877         in_dev = ip_mc_find_dev(&imr);
1878
1879         if (!in_dev) {
1880                 err = -ENODEV;
1881                 goto done;
1882         }
1883         err = -EADDRNOTAVAIL;
1884
1885         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1886                 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1887                     && pmc->multi.imr_ifindex == imr.imr_ifindex)
1888                         break;
1889         }
1890         if (!pmc) {             /* must have a prior join */
1891                 err = -EINVAL;
1892                 goto done;
1893         }
1894         /* if a source filter was set, must be the same mode as before */
1895         if (pmc->sflist) {
1896                 if (pmc->sfmode != omode) {
1897                         err = -EINVAL;
1898                         goto done;
1899                 }
1900         } else if (pmc->sfmode != omode) {
1901                 /* allow mode switches for empty-set filters */
1902                 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
1903                 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
1904                         NULL, 0);
1905                 pmc->sfmode = omode;
1906         }
1907
1908         psl = pmc->sflist;
1909         if (!add) {
1910                 if (!psl)
1911                         goto done;      /* err = -EADDRNOTAVAIL */
1912                 rv = !0;
1913                 for (i=0; i<psl->sl_count; i++) {
1914                         rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1915                                 sizeof(__be32));
1916                         if (rv == 0)
1917                                 break;
1918                 }
1919                 if (rv)         /* source not found */
1920                         goto done;      /* err = -EADDRNOTAVAIL */
1921
1922                 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1923                 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1924                         leavegroup = 1;
1925                         goto done;
1926                 }
1927
1928                 /* update the interface filter */
1929                 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1930                         &mreqs->imr_sourceaddr, 1);
1931
1932                 for (j=i+1; j<psl->sl_count; j++)
1933                         psl->sl_addr[j-1] = psl->sl_addr[j];
1934                 psl->sl_count--;
1935                 err = 0;
1936                 goto done;
1937         }
1938         /* else, add a new source to the filter */
1939
1940         if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1941                 err = -ENOBUFS;
1942                 goto done;
1943         }
1944         if (!psl || psl->sl_count == psl->sl_max) {
1945                 struct ip_sf_socklist *newpsl;
1946                 int count = IP_SFBLOCK;
1947
1948                 if (psl)
1949                         count += psl->sl_max;
1950                 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
1951                 if (!newpsl) {
1952                         err = -ENOBUFS;
1953                         goto done;
1954                 }
1955                 newpsl->sl_max = count;
1956                 newpsl->sl_count = count - IP_SFBLOCK;
1957                 if (psl) {
1958                         for (i=0; i<psl->sl_count; i++)
1959                                 newpsl->sl_addr[i] = psl->sl_addr[i];
1960                         sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1961                 }
1962                 pmc->sflist = psl = newpsl;
1963         }
1964         rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1965         for (i=0; i<psl->sl_count; i++) {
1966                 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
1967                         sizeof(__be32));
1968                 if (rv == 0)
1969                         break;
1970         }
1971         if (rv == 0)            /* address already there is an error */
1972                 goto done;
1973         for (j=psl->sl_count-1; j>=i; j--)
1974                 psl->sl_addr[j+1] = psl->sl_addr[j];
1975         psl->sl_addr[i] = mreqs->imr_sourceaddr;
1976         psl->sl_count++;
1977         err = 0;
1978         /* update the interface list */
1979         ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
1980                 &mreqs->imr_sourceaddr, 1);
1981 done:
1982         rtnl_unlock();
1983         if (leavegroup)
1984                 return ip_mc_leave_group(sk, &imr);
1985         return err;
1986 }
1987
1988 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1989 {
1990         int err = 0;
1991         struct ip_mreqn imr;
1992         __be32 addr = msf->imsf_multiaddr;
1993         struct ip_mc_socklist *pmc;
1994         struct in_device *in_dev;
1995         struct inet_sock *inet = inet_sk(sk);
1996         struct ip_sf_socklist *newpsl, *psl;
1997         int leavegroup = 0;
1998
1999         if (!MULTICAST(addr))
2000                 return -EINVAL;
2001         if (msf->imsf_fmode != MCAST_INCLUDE &&
2002             msf->imsf_fmode != MCAST_EXCLUDE)
2003                 return -EINVAL;
2004
2005         rtnl_lock();
2006
2007         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2008         imr.imr_address.s_addr = msf->imsf_interface;
2009         imr.imr_ifindex = ifindex;
2010         in_dev = ip_mc_find_dev(&imr);
2011
2012         if (!in_dev) {
2013                 err = -ENODEV;
2014                 goto done;
2015         }
2016
2017         /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2018         if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2019                 leavegroup = 1;
2020                 goto done;
2021         }
2022
2023         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2024                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2025                     pmc->multi.imr_ifindex == imr.imr_ifindex)
2026                         break;
2027         }
2028         if (!pmc) {             /* must have a prior join */
2029                 err = -EINVAL;
2030                 goto done;
2031         }
2032         if (msf->imsf_numsrc) {
2033                 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2034                                                            GFP_KERNEL);
2035                 if (!newpsl) {
2036                         err = -ENOBUFS;
2037                         goto done;
2038                 }
2039                 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2040                 memcpy(newpsl->sl_addr, msf->imsf_slist,
2041                         msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2042                 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2043                         msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2044                 if (err) {
2045                         sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2046                         goto done;
2047                 }
2048         } else {
2049                 newpsl = NULL;
2050                 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2051                                      msf->imsf_fmode, 0, NULL, 0);
2052         }
2053         psl = pmc->sflist;
2054         if (psl) {
2055                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2056                         psl->sl_count, psl->sl_addr, 0);
2057                 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2058         } else
2059                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2060                         0, NULL, 0);
2061         pmc->sflist = newpsl;
2062         pmc->sfmode = msf->imsf_fmode;
2063         err = 0;
2064 done:
2065         rtnl_unlock();
2066         if (leavegroup)
2067                 err = ip_mc_leave_group(sk, &imr);
2068         return err;
2069 }
2070
2071 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2072         struct ip_msfilter __user *optval, int __user *optlen)
2073 {
2074         int err, len, count, copycount;
2075         struct ip_mreqn imr;
2076         __be32 addr = msf->imsf_multiaddr;
2077         struct ip_mc_socklist *pmc;
2078         struct in_device *in_dev;
2079         struct inet_sock *inet = inet_sk(sk);
2080         struct ip_sf_socklist *psl;
2081
2082         if (!MULTICAST(addr))
2083                 return -EINVAL;
2084
2085         rtnl_lock();
2086
2087         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2088         imr.imr_address.s_addr = msf->imsf_interface;
2089         imr.imr_ifindex = 0;
2090         in_dev = ip_mc_find_dev(&imr);
2091
2092         if (!in_dev) {
2093                 err = -ENODEV;
2094                 goto done;
2095         }
2096         err = -EADDRNOTAVAIL;
2097
2098         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2099                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2100                     pmc->multi.imr_ifindex == imr.imr_ifindex)
2101                         break;
2102         }
2103         if (!pmc)               /* must have a prior join */
2104                 goto done;
2105         msf->imsf_fmode = pmc->sfmode;
2106         psl = pmc->sflist;
2107         rtnl_unlock();
2108         if (!psl) {
2109                 len = 0;
2110                 count = 0;
2111         } else {
2112                 count = psl->sl_count;
2113         }
2114         copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2115         len = copycount * sizeof(psl->sl_addr[0]);
2116         msf->imsf_numsrc = count;
2117         if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2118             copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2119                 return -EFAULT;
2120         }
2121         if (len &&
2122             copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2123                 return -EFAULT;
2124         return 0;
2125 done:
2126         rtnl_unlock();
2127         return err;
2128 }
2129
2130 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2131         struct group_filter __user *optval, int __user *optlen)
2132 {
2133         int err, i, count, copycount;
2134         struct sockaddr_in *psin;
2135         __be32 addr;
2136         struct ip_mc_socklist *pmc;
2137         struct inet_sock *inet = inet_sk(sk);
2138         struct ip_sf_socklist *psl;
2139
2140         psin = (struct sockaddr_in *)&gsf->gf_group;
2141         if (psin->sin_family != AF_INET)
2142                 return -EINVAL;
2143         addr = psin->sin_addr.s_addr;
2144         if (!MULTICAST(addr))
2145                 return -EINVAL;
2146
2147         rtnl_lock();
2148
2149         err = -EADDRNOTAVAIL;
2150
2151         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2152                 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2153                     pmc->multi.imr_ifindex == gsf->gf_interface)
2154                         break;
2155         }
2156         if (!pmc)               /* must have a prior join */
2157                 goto done;
2158         gsf->gf_fmode = pmc->sfmode;
2159         psl = pmc->sflist;
2160         rtnl_unlock();
2161         count = psl ? psl->sl_count : 0;
2162         copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2163         gsf->gf_numsrc = count;
2164         if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2165             copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2166                 return -EFAULT;
2167         }
2168         for (i=0; i<copycount; i++) {
2169                 struct sockaddr_in *psin;
2170                 struct sockaddr_storage ss;
2171
2172                 psin = (struct sockaddr_in *)&ss;
2173                 memset(&ss, 0, sizeof(ss));
2174                 psin->sin_family = AF_INET;
2175                 psin->sin_addr.s_addr = psl->sl_addr[i];
2176                 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2177                         return -EFAULT;
2178         }
2179         return 0;
2180 done:
2181         rtnl_unlock();
2182         return err;
2183 }
2184
2185 /*
2186  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2187  */
2188 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2189 {
2190         struct inet_sock *inet = inet_sk(sk);
2191         struct ip_mc_socklist *pmc;
2192         struct ip_sf_socklist *psl;
2193         int i;
2194
2195         if (!MULTICAST(loc_addr))
2196                 return 1;
2197
2198         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2199                 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2200                     pmc->multi.imr_ifindex == dif)
2201                         break;
2202         }
2203         if (!pmc)
2204                 return 1;
2205         psl = pmc->sflist;
2206         if (!psl)
2207                 return pmc->sfmode == MCAST_EXCLUDE;
2208
2209         for (i=0; i<psl->sl_count; i++) {
2210                 if (psl->sl_addr[i] == rmt_addr)
2211                         break;
2212         }
2213         if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2214                 return 0;
2215         if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2216                 return 0;
2217         return 1;
2218 }
2219
2220 /*
2221  *      A socket is closing.
2222  */
2223
2224 void ip_mc_drop_socket(struct sock *sk)
2225 {
2226         struct inet_sock *inet = inet_sk(sk);
2227         struct ip_mc_socklist *iml;
2228
2229         if (inet->mc_list == NULL)
2230                 return;
2231
2232         rtnl_lock();
2233         while ((iml = inet->mc_list) != NULL) {
2234                 struct in_device *in_dev;
2235                 inet->mc_list = iml->next;
2236
2237                 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
2238                 (void) ip_mc_leave_src(sk, iml, in_dev);
2239                 if (in_dev != NULL) {
2240                         ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2241                         in_dev_put(in_dev);
2242                 }
2243                 sock_kfree_s(sk, iml, sizeof(*iml));
2244         }
2245         rtnl_unlock();
2246 }
2247
2248 int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2249 {
2250         struct ip_mc_list *im;
2251         struct ip_sf_list *psf;
2252         int rv = 0;
2253
2254         read_lock(&in_dev->mc_list_lock);
2255         for (im=in_dev->mc_list; im; im=im->next) {
2256                 if (im->multiaddr == mc_addr)
2257                         break;
2258         }
2259         if (im && proto == IPPROTO_IGMP) {
2260                 rv = 1;
2261         } else if (im) {
2262                 if (src_addr) {
2263                         for (psf=im->sources; psf; psf=psf->sf_next) {
2264                                 if (psf->sf_inaddr == src_addr)
2265                                         break;
2266                         }
2267                         if (psf)
2268                                 rv = psf->sf_count[MCAST_INCLUDE] ||
2269                                         psf->sf_count[MCAST_EXCLUDE] !=
2270                                         im->sfcount[MCAST_EXCLUDE];
2271                         else
2272                                 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2273                 } else
2274                         rv = 1; /* unspecified source; tentatively allow */
2275         }
2276         read_unlock(&in_dev->mc_list_lock);
2277         return rv;
2278 }
2279
2280 #if defined(CONFIG_PROC_FS)
2281 struct igmp_mc_iter_state {
2282         struct net_device *dev;
2283         struct in_device *in_dev;
2284 };
2285
2286 #define igmp_mc_seq_private(seq)        ((struct igmp_mc_iter_state *)(seq)->private)
2287
2288 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2289 {
2290         struct ip_mc_list *im = NULL;
2291         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2292
2293         state->in_dev = NULL;
2294         for_each_netdev(state->dev) {
2295                 struct in_device *in_dev;
2296                 in_dev = in_dev_get(state->dev);
2297                 if (!in_dev)
2298                         continue;
2299                 read_lock(&in_dev->mc_list_lock);
2300                 im = in_dev->mc_list;
2301                 if (im) {
2302                         state->in_dev = in_dev;
2303                         break;
2304                 }
2305                 read_unlock(&in_dev->mc_list_lock);
2306                 in_dev_put(in_dev);
2307         }
2308         return im;
2309 }
2310
2311 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2312 {
2313         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2314         im = im->next;
2315         while (!im) {
2316                 if (likely(state->in_dev != NULL)) {
2317                         read_unlock(&state->in_dev->mc_list_lock);
2318                         in_dev_put(state->in_dev);
2319                 }
2320                 state->dev = next_net_device(state->dev);
2321                 if (!state->dev) {
2322                         state->in_dev = NULL;
2323                         break;
2324                 }
2325                 state->in_dev = in_dev_get(state->dev);
2326                 if (!state->in_dev)
2327                         continue;
2328                 read_lock(&state->in_dev->mc_list_lock);
2329                 im = state->in_dev->mc_list;
2330         }
2331         return im;
2332 }
2333
2334 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2335 {
2336         struct ip_mc_list *im = igmp_mc_get_first(seq);
2337         if (im)
2338                 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2339                         --pos;
2340         return pos ? NULL : im;
2341 }
2342
2343 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2344 {
2345         read_lock(&dev_base_lock);
2346         return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2347 }
2348
2349 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2350 {
2351         struct ip_mc_list *im;
2352         if (v == SEQ_START_TOKEN)
2353                 im = igmp_mc_get_first(seq);
2354         else
2355                 im = igmp_mc_get_next(seq, v);
2356         ++*pos;
2357         return im;
2358 }
2359
2360 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2361 {
2362         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2363         if (likely(state->in_dev != NULL)) {
2364                 read_unlock(&state->in_dev->mc_list_lock);
2365                 in_dev_put(state->in_dev);
2366                 state->in_dev = NULL;
2367         }
2368         state->dev = NULL;
2369         read_unlock(&dev_base_lock);
2370 }
2371
2372 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2373 {
2374         if (v == SEQ_START_TOKEN)
2375                 seq_puts(seq,
2376                          "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2377         else {
2378                 struct ip_mc_list *im = (struct ip_mc_list *)v;
2379                 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2380                 char   *querier;
2381 #ifdef CONFIG_IP_MULTICAST
2382                 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2383                           IGMP_V2_SEEN(state->in_dev) ? "V2" :
2384                           "V3";
2385 #else
2386                 querier = "NONE";
2387 #endif
2388
2389                 if (state->in_dev->mc_list == im) {
2390                         seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2391                                    state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2392                 }
2393
2394                 seq_printf(seq,
2395                            "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2396                            im->multiaddr, im->users,
2397                            im->tm_running, im->tm_running ?
2398                            jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2399                            im->reporter);
2400         }
2401         return 0;
2402 }
2403
2404 static const struct seq_operations igmp_mc_seq_ops = {
2405         .start  =       igmp_mc_seq_start,
2406         .next   =       igmp_mc_seq_next,
2407         .stop   =       igmp_mc_seq_stop,
2408         .show   =       igmp_mc_seq_show,
2409 };
2410
2411 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2412 {
2413         struct seq_file *seq;
2414         int rc = -ENOMEM;
2415         struct igmp_mc_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2416
2417         if (!s)
2418                 goto out;
2419         rc = seq_open(file, &igmp_mc_seq_ops);
2420         if (rc)
2421                 goto out_kfree;
2422
2423         seq = file->private_data;
2424         seq->private = s;
2425 out:
2426         return rc;
2427 out_kfree:
2428         kfree(s);
2429         goto out;
2430 }
2431
2432 static const struct file_operations igmp_mc_seq_fops = {
2433         .owner          =       THIS_MODULE,
2434         .open           =       igmp_mc_seq_open,
2435         .read           =       seq_read,
2436         .llseek         =       seq_lseek,
2437         .release        =       seq_release_private,
2438 };
2439
2440 struct igmp_mcf_iter_state {
2441         struct net_device *dev;
2442         struct in_device *idev;
2443         struct ip_mc_list *im;
2444 };
2445
2446 #define igmp_mcf_seq_private(seq)       ((struct igmp_mcf_iter_state *)(seq)->private)
2447
2448 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2449 {
2450         struct ip_sf_list *psf = NULL;
2451         struct ip_mc_list *im = NULL;
2452         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2453
2454         state->idev = NULL;
2455         state->im = NULL;
2456         for_each_netdev(state->dev) {
2457                 struct in_device *idev;
2458                 idev = in_dev_get(state->dev);
2459                 if (unlikely(idev == NULL))
2460                         continue;
2461                 read_lock(&idev->mc_list_lock);
2462                 im = idev->mc_list;
2463                 if (likely(im != NULL)) {
2464                         spin_lock_bh(&im->lock);
2465                         psf = im->sources;
2466                         if (likely(psf != NULL)) {
2467                                 state->im = im;
2468                                 state->idev = idev;
2469                                 break;
2470                         }
2471                         spin_unlock_bh(&im->lock);
2472                 }
2473                 read_unlock(&idev->mc_list_lock);
2474                 in_dev_put(idev);
2475         }
2476         return psf;
2477 }
2478
2479 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2480 {
2481         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2482
2483         psf = psf->sf_next;
2484         while (!psf) {
2485                 spin_unlock_bh(&state->im->lock);
2486                 state->im = state->im->next;
2487                 while (!state->im) {
2488                         if (likely(state->idev != NULL)) {
2489                                 read_unlock(&state->idev->mc_list_lock);
2490                                 in_dev_put(state->idev);
2491                         }
2492                         state->dev = next_net_device(state->dev);
2493                         if (!state->dev) {
2494                                 state->idev = NULL;
2495                                 goto out;
2496                         }
2497                         state->idev = in_dev_get(state->dev);
2498                         if (!state->idev)
2499                                 continue;
2500                         read_lock(&state->idev->mc_list_lock);
2501                         state->im = state->idev->mc_list;
2502                 }
2503                 if (!state->im)
2504                         break;
2505                 spin_lock_bh(&state->im->lock);
2506                 psf = state->im->sources;
2507         }
2508 out:
2509         return psf;
2510 }
2511
2512 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2513 {
2514         struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2515         if (psf)
2516                 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2517                         --pos;
2518         return pos ? NULL : psf;
2519 }
2520
2521 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2522 {
2523         read_lock(&dev_base_lock);
2524         return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2525 }
2526
2527 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2528 {
2529         struct ip_sf_list *psf;
2530         if (v == SEQ_START_TOKEN)
2531                 psf = igmp_mcf_get_first(seq);
2532         else
2533                 psf = igmp_mcf_get_next(seq, v);
2534         ++*pos;
2535         return psf;
2536 }
2537
2538 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2539 {
2540         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2541         if (likely(state->im != NULL)) {
2542                 spin_unlock_bh(&state->im->lock);
2543                 state->im = NULL;
2544         }
2545         if (likely(state->idev != NULL)) {
2546                 read_unlock(&state->idev->mc_list_lock);
2547                 in_dev_put(state->idev);
2548                 state->idev = NULL;
2549         }
2550         state->dev = NULL;
2551         read_unlock(&dev_base_lock);
2552 }
2553
2554 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2555 {
2556         struct ip_sf_list *psf = (struct ip_sf_list *)v;
2557         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2558
2559         if (v == SEQ_START_TOKEN) {
2560                 seq_printf(seq,
2561                            "%3s %6s "
2562                            "%10s %10s %6s %6s\n", "Idx",
2563                            "Device", "MCA",
2564                            "SRC", "INC", "EXC");
2565         } else {
2566                 seq_printf(seq,
2567                            "%3d %6.6s 0x%08x "
2568                            "0x%08x %6lu %6lu\n",
2569                            state->dev->ifindex, state->dev->name,
2570                            ntohl(state->im->multiaddr),
2571                            ntohl(psf->sf_inaddr),
2572                            psf->sf_count[MCAST_INCLUDE],
2573                            psf->sf_count[MCAST_EXCLUDE]);
2574         }
2575         return 0;
2576 }
2577
2578 static const struct seq_operations igmp_mcf_seq_ops = {
2579         .start  =       igmp_mcf_seq_start,
2580         .next   =       igmp_mcf_seq_next,
2581         .stop   =       igmp_mcf_seq_stop,
2582         .show   =       igmp_mcf_seq_show,
2583 };
2584
2585 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2586 {
2587         struct seq_file *seq;
2588         int rc = -ENOMEM;
2589         struct igmp_mcf_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
2590
2591         if (!s)
2592                 goto out;
2593         rc = seq_open(file, &igmp_mcf_seq_ops);
2594         if (rc)
2595                 goto out_kfree;
2596
2597         seq = file->private_data;
2598         seq->private = s;
2599 out:
2600         return rc;
2601 out_kfree:
2602         kfree(s);
2603         goto out;
2604 }
2605
2606 static const struct file_operations igmp_mcf_seq_fops = {
2607         .owner          =       THIS_MODULE,
2608         .open           =       igmp_mcf_seq_open,
2609         .read           =       seq_read,
2610         .llseek         =       seq_lseek,
2611         .release        =       seq_release_private,
2612 };
2613
2614 int __init igmp_mc_proc_init(void)
2615 {
2616         proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2617         proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2618         return 0;
2619 }
2620 #endif
2621
2622 EXPORT_SYMBOL(ip_mc_dec_group);
2623 EXPORT_SYMBOL(ip_mc_inc_group);
2624 EXPORT_SYMBOL(ip_mc_join_group);
2625 EXPORT_SYMBOL(ip_mc_rejoin_group);