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