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