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