]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/igmp.c
701558564e96863d0d3b01ee679687aa72f1c8e9
[linux-2.6-omap-h63xx.git] / net / ipv4 / igmp.c
1 /*
2  *      Linux NET3:     Internet Group Management Protocol  [IGMP]
3  *
4  *      This code implements the IGMP protocol as defined in RFC1112. There has
5  *      been a further revision of this protocol since which is now supported.
6  *
7  *      If you have trouble with this module be careful what gcc you have used,
8  *      the older version didn't come out right using gcc 2.5.8, the newer one
9  *      seems to fall out with gcc 2.6.2.
10  *
11  *      Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
12  *
13  *      Authors:
14  *              Alan Cox <Alan.Cox@linux.org>
15  *
16  *      This program is free software; you can redistribute it and/or
17  *      modify it under the terms of the GNU General Public License
18  *      as published by the Free Software Foundation; either version
19  *      2 of the License, or (at your option) any later version.
20  *
21  *      Fixes:
22  *
23  *              Alan Cox        :       Added lots of __inline__ to optimise
24  *                                      the memory usage of all the tiny little
25  *                                      functions.
26  *              Alan Cox        :       Dumped the header building experiment.
27  *              Alan Cox        :       Minor tweaks ready for multicast routing
28  *                                      and extended IGMP protocol.
29  *              Alan Cox        :       Removed a load of inline directives. Gcc 2.5.8
30  *                                      writes utterly bogus code otherwise (sigh)
31  *                                      fixed IGMP loopback to behave in the manner
32  *                                      desired by mrouted, fixed the fact it has been
33  *                                      broken since 1.3.6 and cleaned up a few minor
34  *                                      points.
35  *
36  *              Chih-Jen Chang  :       Tried to revise IGMP to Version 2
37  *              Tsu-Sheng Tsao          E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
38  *                                      The enhancements are mainly based on Steve Deering's
39  *                                      ipmulti-3.5 source code.
40  *              Chih-Jen Chang  :       Added the igmp_get_mrouter_info and
41  *              Tsu-Sheng Tsao          igmp_set_mrouter_info to keep track of
42  *                                      the mrouted version on that device.
43  *              Chih-Jen Chang  :       Added the max_resp_time parameter to
44  *              Tsu-Sheng Tsao          igmp_heard_query(). Using this parameter
45  *                                      to identify the multicast router version
46  *                                      and do what the IGMP version 2 specified.
47  *              Chih-Jen Chang  :       Added a timer to revert to IGMP V2 router
48  *              Tsu-Sheng Tsao          if the specified time expired.
49  *              Alan Cox        :       Stop IGMP from 0.0.0.0 being accepted.
50  *              Alan Cox        :       Use GFP_ATOMIC in the right places.
51  *              Christian Daudt :       igmp timer wasn't set for local group
52  *                                      memberships but was being deleted,
53  *                                      which caused a "del_timer() called
54  *                                      from %p with timer not initialized\n"
55  *                                      message (960131).
56  *              Christian Daudt :       removed del_timer from
57  *                                      igmp_timer_expire function (960205).
58  *             Christian Daudt :       igmp_heard_report now only calls
59  *                                     igmp_timer_expire if tm->running is
60  *                                     true (960216).
61  *              Malcolm Beattie :       ttl comparison wrong in igmp_rcv made
62  *                                      igmp_heard_query never trigger. Expiry
63  *                                      miscalculation fixed in igmp_heard_query
64  *                                      and random() made to return unsigned to
65  *                                      prevent negative expiry times.
66  *              Alexey Kuznetsov:       Wrong group leaving behaviour, backport
67  *                                      fix from pending 2.1.x patches.
68  *              Alan Cox:               Forget to enable FDDI support earlier.
69  *              Alexey Kuznetsov:       Fixed leaving groups on device down.
70  *              Alexey Kuznetsov:       Accordance to igmp-v2-06 draft.
71  *              David L Stevens:        IGMPv3 support, with help from
72  *                                      Vinay Kulkarni
73  */
74
75 #include <linux/module.h>
76 #include <asm/uaccess.h>
77 #include <asm/system.h>
78 #include <linux/types.h>
79 #include <linux/kernel.h>
80 #include <linux/jiffies.h>
81 #include <linux/string.h>
82 #include <linux/socket.h>
83 #include <linux/sockios.h>
84 #include <linux/in.h>
85 #include <linux/inet.h>
86 #include <linux/netdevice.h>
87 #include <linux/skbuff.h>
88 #include <linux/inetdevice.h>
89 #include <linux/igmp.h>
90 #include <linux/if_arp.h>
91 #include <linux/rtnetlink.h>
92 #include <linux/times.h>
93
94 #include <net/net_namespace.h>
95 #include <net/arp.h>
96 #include <net/ip.h>
97 #include <net/protocol.h>
98 #include <net/route.h>
99 #include <net/sock.h>
100 #include <net/checksum.h>
101 #include <linux/netfilter_ipv4.h>
102 #ifdef CONFIG_IP_MROUTE
103 #include <linux/mroute.h>
104 #endif
105 #ifdef CONFIG_PROC_FS
106 #include <linux/proc_fs.h>
107 #include <linux/seq_file.h>
108 #endif
109
110 #define IP_MAX_MEMBERSHIPS      20
111 #define IP_MAX_MSF              10
112
113 #ifdef CONFIG_IP_MULTICAST
114 /* Parameter names and values are taken from igmp-v2-06 draft */
115
116 #define IGMP_V1_Router_Present_Timeout          (400*HZ)
117 #define IGMP_V2_Router_Present_Timeout          (400*HZ)
118 #define IGMP_Unsolicited_Report_Interval        (10*HZ)
119 #define IGMP_Query_Response_Interval            (10*HZ)
120 #define IGMP_Unsolicited_Report_Count           2
121
122
123 #define IGMP_Initial_Report_Delay               (1)
124
125 /* IGMP_Initial_Report_Delay is not from IGMP specs!
126  * IGMP specs require to report membership immediately after
127  * joining a group, but we delay the first report by a
128  * small interval. It seems more natural and still does not
129  * contradict to specs provided this delay is small enough.
130  */
131
132 #define IGMP_V1_SEEN(in_dev) \
133         (IPV4_DEVCONF_ALL(FORCE_IGMP_VERSION) == 1 || \
134          IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135          ((in_dev)->mr_v1_seen && \
136           time_before(jiffies, (in_dev)->mr_v1_seen)))
137 #define IGMP_V2_SEEN(in_dev) \
138         (IPV4_DEVCONF_ALL(FORCE_IGMP_VERSION) == 2 || \
139          IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140          ((in_dev)->mr_v2_seen && \
141           time_before(jiffies, (in_dev)->mr_v2_seen)))
142
143 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
144 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
145 static void igmpv3_clear_delrec(struct in_device *in_dev);
146 static int sf_setstate(struct ip_mc_list *pmc);
147 static void sf_markstate(struct ip_mc_list *pmc);
148 #endif
149 static void ip_mc_clear_src(struct ip_mc_list *pmc);
150 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
151                          int sfcount, __be32 *psfsrc, int delta);
152
153 static void ip_ma_put(struct ip_mc_list *im)
154 {
155         if (atomic_dec_and_test(&im->refcnt)) {
156                 in_dev_put(im->interface);
157                 kfree(im);
158         }
159 }
160
161 #ifdef CONFIG_IP_MULTICAST
162
163 /*
164  *      Timer management
165  */
166
167 static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
168 {
169         spin_lock_bh(&im->lock);
170         if (del_timer(&im->timer))
171                 atomic_dec(&im->refcnt);
172         im->tm_running=0;
173         im->reporter = 0;
174         im->unsolicit_count = 0;
175         spin_unlock_bh(&im->lock);
176 }
177
178 /* It must be called with locked im->lock */
179 static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
180 {
181         int tv=net_random() % max_delay;
182
183         im->tm_running=1;
184         if (!mod_timer(&im->timer, jiffies+tv+2))
185                 atomic_inc(&im->refcnt);
186 }
187
188 static void igmp_gq_start_timer(struct in_device *in_dev)
189 {
190         int tv = net_random() % in_dev->mr_maxdelay;
191
192         in_dev->mr_gq_running = 1;
193         if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
194                 in_dev_hold(in_dev);
195 }
196
197 static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
198 {
199         int tv = net_random() % delay;
200
201         if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
202                 in_dev_hold(in_dev);
203 }
204
205 static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
206 {
207         spin_lock_bh(&im->lock);
208         im->unsolicit_count = 0;
209         if (del_timer(&im->timer)) {
210                 if ((long)(im->timer.expires-jiffies) < max_delay) {
211                         add_timer(&im->timer);
212                         im->tm_running=1;
213                         spin_unlock_bh(&im->lock);
214                         return;
215                 }
216                 atomic_dec(&im->refcnt);
217         }
218         igmp_start_timer(im, max_delay);
219         spin_unlock_bh(&im->lock);
220 }
221
222
223 /*
224  *      Send an IGMP report.
225  */
226
227 #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
228
229
230 static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
231         int gdeleted, int sdeleted)
232 {
233         switch (type) {
234         case IGMPV3_MODE_IS_INCLUDE:
235         case IGMPV3_MODE_IS_EXCLUDE:
236                 if (gdeleted || sdeleted)
237                         return 0;
238                 if (!(pmc->gsquery && !psf->sf_gsresp)) {
239                         if (pmc->sfmode == MCAST_INCLUDE)
240                                 return 1;
241                         /* don't include if this source is excluded
242                          * in all filters
243                          */
244                         if (psf->sf_count[MCAST_INCLUDE])
245                                 return type == IGMPV3_MODE_IS_INCLUDE;
246                         return pmc->sfcount[MCAST_EXCLUDE] ==
247                                 psf->sf_count[MCAST_EXCLUDE];
248                 }
249                 return 0;
250         case IGMPV3_CHANGE_TO_INCLUDE:
251                 if (gdeleted || sdeleted)
252                         return 0;
253                 return psf->sf_count[MCAST_INCLUDE] != 0;
254         case IGMPV3_CHANGE_TO_EXCLUDE:
255                 if (gdeleted || sdeleted)
256                         return 0;
257                 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
258                     psf->sf_count[MCAST_INCLUDE])
259                         return 0;
260                 return pmc->sfcount[MCAST_EXCLUDE] ==
261                         psf->sf_count[MCAST_EXCLUDE];
262         case IGMPV3_ALLOW_NEW_SOURCES:
263                 if (gdeleted || !psf->sf_crcount)
264                         return 0;
265                 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
266         case IGMPV3_BLOCK_OLD_SOURCES:
267                 if (pmc->sfmode == MCAST_INCLUDE)
268                         return gdeleted || (psf->sf_crcount && sdeleted);
269                 return psf->sf_crcount && !gdeleted && !sdeleted;
270         }
271         return 0;
272 }
273
274 static int
275 igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
276 {
277         struct ip_sf_list *psf;
278         int scount = 0;
279
280         for (psf=pmc->sources; psf; psf=psf->sf_next) {
281                 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
282                         continue;
283                 scount++;
284         }
285         return scount;
286 }
287
288 static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
289 {
290         struct sk_buff *skb;
291         struct rtable *rt;
292         struct iphdr *pip;
293         struct igmpv3_report *pig;
294
295         skb = alloc_skb(size + LL_RESERVED_SPACE(dev), GFP_ATOMIC);
296         if (skb == NULL)
297                 return NULL;
298
299         {
300                 struct flowi fl = { .oif = dev->ifindex,
301                                     .nl_u = { .ip4_u = {
302                                     .daddr = IGMPV3_ALL_MCR } },
303                                     .proto = IPPROTO_IGMP };
304                 if (ip_route_output_key(&rt, &fl)) {
305                         kfree_skb(skb);
306                         return NULL;
307                 }
308         }
309         if (rt->rt_src == 0) {
310                 kfree_skb(skb);
311                 ip_rt_put(rt);
312                 return NULL;
313         }
314
315         skb->dst = &rt->u.dst;
316         skb->dev = dev;
317
318         skb_reserve(skb, LL_RESERVED_SPACE(dev));
319
320         skb_reset_network_header(skb);
321         pip = ip_hdr(skb);
322         skb_put(skb, sizeof(struct iphdr) + 4);
323
324         pip->version  = 4;
325         pip->ihl      = (sizeof(struct iphdr)+4)>>2;
326         pip->tos      = 0xc0;
327         pip->frag_off = htons(IP_DF);
328         pip->ttl      = 1;
329         pip->daddr    = rt->rt_dst;
330         pip->saddr    = rt->rt_src;
331         pip->protocol = IPPROTO_IGMP;
332         pip->tot_len  = 0;      /* filled in later */
333         ip_select_ident(pip, &rt->u.dst, NULL);
334         ((u8*)&pip[1])[0] = IPOPT_RA;
335         ((u8*)&pip[1])[1] = 4;
336         ((u8*)&pip[1])[2] = 0;
337         ((u8*)&pip[1])[3] = 0;
338
339         skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
340         skb_put(skb, sizeof(*pig));
341         pig = igmpv3_report_hdr(skb);
342         pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
343         pig->resv1 = 0;
344         pig->csum = 0;
345         pig->resv2 = 0;
346         pig->ngrec = 0;
347         return skb;
348 }
349
350 static int igmpv3_sendpack(struct sk_buff *skb)
351 {
352         struct iphdr *pip = ip_hdr(skb);
353         struct igmphdr *pig = igmp_hdr(skb);
354         const int iplen = skb->tail - skb->network_header;
355         const int igmplen = skb->tail - skb->transport_header;
356
357         pip->tot_len = htons(iplen);
358         ip_send_check(pip);
359         pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
360
361         return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
362                        dst_output);
363 }
364
365 static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
366 {
367         return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
368 }
369
370 static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
371         int type, struct igmpv3_grec **ppgr)
372 {
373         struct net_device *dev = pmc->interface->dev;
374         struct igmpv3_report *pih;
375         struct igmpv3_grec *pgr;
376
377         if (!skb)
378                 skb = igmpv3_newpack(dev, dev->mtu);
379         if (!skb)
380                 return NULL;
381         pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
382         pgr->grec_type = type;
383         pgr->grec_auxwords = 0;
384         pgr->grec_nsrcs = 0;
385         pgr->grec_mca = pmc->multiaddr;
386         pih = igmpv3_report_hdr(skb);
387         pih->ngrec = htons(ntohs(pih->ngrec)+1);
388         *ppgr = pgr;
389         return skb;
390 }
391
392 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
393         skb_tailroom(skb)) : 0)
394
395 static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
396         int type, int gdeleted, int sdeleted)
397 {
398         struct net_device *dev = pmc->interface->dev;
399         struct igmpv3_report *pih;
400         struct igmpv3_grec *pgr = NULL;
401         struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
402         int scount, stotal, first, isquery, truncate;
403
404         if (pmc->multiaddr == IGMP_ALL_HOSTS)
405                 return skb;
406
407         isquery = type == IGMPV3_MODE_IS_INCLUDE ||
408                   type == IGMPV3_MODE_IS_EXCLUDE;
409         truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
410                     type == IGMPV3_CHANGE_TO_EXCLUDE;
411
412         stotal = scount = 0;
413
414         psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
415
416         if (!*psf_list)
417                 goto empty_source;
418
419         pih = skb ? igmpv3_report_hdr(skb) : NULL;
420
421         /* EX and TO_EX get a fresh packet, if needed */
422         if (truncate) {
423                 if (pih && pih->ngrec &&
424                     AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
425                         if (skb)
426                                 igmpv3_sendpack(skb);
427                         skb = igmpv3_newpack(dev, dev->mtu);
428                 }
429         }
430         first = 1;
431         psf_prev = NULL;
432         for (psf=*psf_list; psf; psf=psf_next) {
433                 __be32 *psrc;
434
435                 psf_next = psf->sf_next;
436
437                 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
438                         psf_prev = psf;
439                         continue;
440                 }
441
442                 /* clear marks on query responses */
443                 if (isquery)
444                         psf->sf_gsresp = 0;
445
446                 if (AVAILABLE(skb) < sizeof(__be32) +
447                     first*sizeof(struct igmpv3_grec)) {
448                         if (truncate && !first)
449                                 break;   /* truncate these */
450                         if (pgr)
451                                 pgr->grec_nsrcs = htons(scount);
452                         if (skb)
453                                 igmpv3_sendpack(skb);
454                         skb = igmpv3_newpack(dev, dev->mtu);
455                         first = 1;
456                         scount = 0;
457                 }
458                 if (first) {
459                         skb = add_grhead(skb, pmc, type, &pgr);
460                         first = 0;
461                 }
462                 if (!skb)
463                         return NULL;
464                 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
465                 *psrc = psf->sf_inaddr;
466                 scount++; stotal++;
467                 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
468                      type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
469                         psf->sf_crcount--;
470                         if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
471                                 if (psf_prev)
472                                         psf_prev->sf_next = psf->sf_next;
473                                 else
474                                         *psf_list = psf->sf_next;
475                                 kfree(psf);
476                                 continue;
477                         }
478                 }
479                 psf_prev = psf;
480         }
481
482 empty_source:
483         if (!stotal) {
484                 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
485                     type == IGMPV3_BLOCK_OLD_SOURCES)
486                         return skb;
487                 if (pmc->crcount || isquery) {
488                         /* make sure we have room for group header */
489                         if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
490                                 igmpv3_sendpack(skb);
491                                 skb = NULL; /* add_grhead will get a new one */
492                         }
493                         skb = add_grhead(skb, pmc, type, &pgr);
494                 }
495         }
496         if (pgr)
497                 pgr->grec_nsrcs = htons(scount);
498
499         if (isquery)
500                 pmc->gsquery = 0;       /* clear query state on report */
501         return skb;
502 }
503
504 static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
505 {
506         struct sk_buff *skb = NULL;
507         int type;
508
509         if (!pmc) {
510                 read_lock(&in_dev->mc_list_lock);
511                 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
512                         if (pmc->multiaddr == IGMP_ALL_HOSTS)
513                                 continue;
514                         spin_lock_bh(&pmc->lock);
515                         if (pmc->sfcount[MCAST_EXCLUDE])
516                                 type = IGMPV3_MODE_IS_EXCLUDE;
517                         else
518                                 type = IGMPV3_MODE_IS_INCLUDE;
519                         skb = add_grec(skb, pmc, type, 0, 0);
520                         spin_unlock_bh(&pmc->lock);
521                 }
522                 read_unlock(&in_dev->mc_list_lock);
523         } else {
524                 spin_lock_bh(&pmc->lock);
525                 if (pmc->sfcount[MCAST_EXCLUDE])
526                         type = IGMPV3_MODE_IS_EXCLUDE;
527                 else
528                         type = IGMPV3_MODE_IS_INCLUDE;
529                 skb = add_grec(skb, pmc, type, 0, 0);
530                 spin_unlock_bh(&pmc->lock);
531         }
532         if (!skb)
533                 return 0;
534         return igmpv3_sendpack(skb);
535 }
536
537 /*
538  * remove zero-count source records from a source filter list
539  */
540 static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
541 {
542         struct ip_sf_list *psf_prev, *psf_next, *psf;
543
544         psf_prev = NULL;
545         for (psf=*ppsf; psf; psf = psf_next) {
546                 psf_next = psf->sf_next;
547                 if (psf->sf_crcount == 0) {
548                         if (psf_prev)
549                                 psf_prev->sf_next = psf->sf_next;
550                         else
551                                 *ppsf = psf->sf_next;
552                         kfree(psf);
553                 } else
554                         psf_prev = psf;
555         }
556 }
557
558 static void igmpv3_send_cr(struct in_device *in_dev)
559 {
560         struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
561         struct sk_buff *skb = NULL;
562         int type, dtype;
563
564         read_lock(&in_dev->mc_list_lock);
565         spin_lock_bh(&in_dev->mc_tomb_lock);
566
567         /* deleted MCA's */
568         pmc_prev = NULL;
569         for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
570                 pmc_next = pmc->next;
571                 if (pmc->sfmode == MCAST_INCLUDE) {
572                         type = IGMPV3_BLOCK_OLD_SOURCES;
573                         dtype = IGMPV3_BLOCK_OLD_SOURCES;
574                         skb = add_grec(skb, pmc, type, 1, 0);
575                         skb = add_grec(skb, pmc, dtype, 1, 1);
576                 }
577                 if (pmc->crcount) {
578                         if (pmc->sfmode == MCAST_EXCLUDE) {
579                                 type = IGMPV3_CHANGE_TO_INCLUDE;
580                                 skb = add_grec(skb, pmc, type, 1, 0);
581                         }
582                         pmc->crcount--;
583                         if (pmc->crcount == 0) {
584                                 igmpv3_clear_zeros(&pmc->tomb);
585                                 igmpv3_clear_zeros(&pmc->sources);
586                         }
587                 }
588                 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
589                         if (pmc_prev)
590                                 pmc_prev->next = pmc_next;
591                         else
592                                 in_dev->mc_tomb = pmc_next;
593                         in_dev_put(pmc->interface);
594                         kfree(pmc);
595                 } else
596                         pmc_prev = pmc;
597         }
598         spin_unlock_bh(&in_dev->mc_tomb_lock);
599
600         /* change recs */
601         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
602                 spin_lock_bh(&pmc->lock);
603                 if (pmc->sfcount[MCAST_EXCLUDE]) {
604                         type = IGMPV3_BLOCK_OLD_SOURCES;
605                         dtype = IGMPV3_ALLOW_NEW_SOURCES;
606                 } else {
607                         type = IGMPV3_ALLOW_NEW_SOURCES;
608                         dtype = IGMPV3_BLOCK_OLD_SOURCES;
609                 }
610                 skb = add_grec(skb, pmc, type, 0, 0);
611                 skb = add_grec(skb, pmc, dtype, 0, 1);  /* deleted sources */
612
613                 /* filter mode changes */
614                 if (pmc->crcount) {
615                         if (pmc->sfmode == MCAST_EXCLUDE)
616                                 type = IGMPV3_CHANGE_TO_EXCLUDE;
617                         else
618                                 type = IGMPV3_CHANGE_TO_INCLUDE;
619                         skb = add_grec(skb, pmc, type, 0, 0);
620                         pmc->crcount--;
621                 }
622                 spin_unlock_bh(&pmc->lock);
623         }
624         read_unlock(&in_dev->mc_list_lock);
625
626         if (!skb)
627                 return;
628         (void) igmpv3_sendpack(skb);
629 }
630
631 static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
632         int type)
633 {
634         struct sk_buff *skb;
635         struct iphdr *iph;
636         struct igmphdr *ih;
637         struct rtable *rt;
638         struct net_device *dev = in_dev->dev;
639         __be32  group = pmc ? pmc->multiaddr : 0;
640         __be32  dst;
641
642         if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
643                 return igmpv3_send_report(in_dev, pmc);
644         else if (type == IGMP_HOST_LEAVE_MESSAGE)
645                 dst = IGMP_ALL_ROUTER;
646         else
647                 dst = group;
648
649         {
650                 struct flowi fl = { .oif = dev->ifindex,
651                                     .nl_u = { .ip4_u = { .daddr = dst } },
652                                     .proto = IPPROTO_IGMP };
653                 if (ip_route_output_key(&rt, &fl))
654                         return -1;
655         }
656         if (rt->rt_src == 0) {
657                 ip_rt_put(rt);
658                 return -1;
659         }
660
661         skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
662         if (skb == NULL) {
663                 ip_rt_put(rt);
664                 return -1;
665         }
666
667         skb->dst = &rt->u.dst;
668
669         skb_reserve(skb, LL_RESERVED_SPACE(dev));
670
671         skb_reset_network_header(skb);
672         iph = ip_hdr(skb);
673         skb_put(skb, sizeof(struct iphdr) + 4);
674
675         iph->version  = 4;
676         iph->ihl      = (sizeof(struct iphdr)+4)>>2;
677         iph->tos      = 0xc0;
678         iph->frag_off = htons(IP_DF);
679         iph->ttl      = 1;
680         iph->daddr    = dst;
681         iph->saddr    = rt->rt_src;
682         iph->protocol = IPPROTO_IGMP;
683         iph->tot_len  = htons(IGMP_SIZE);
684         ip_select_ident(iph, &rt->u.dst, NULL);
685         ((u8*)&iph[1])[0] = IPOPT_RA;
686         ((u8*)&iph[1])[1] = 4;
687         ((u8*)&iph[1])[2] = 0;
688         ((u8*)&iph[1])[3] = 0;
689         ip_send_check(iph);
690
691         ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
692         ih->type=type;
693         ih->code=0;
694         ih->csum=0;
695         ih->group=group;
696         ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
697
698         return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
699                        dst_output);
700 }
701
702 static void igmp_gq_timer_expire(unsigned long data)
703 {
704         struct in_device *in_dev = (struct in_device *)data;
705
706         in_dev->mr_gq_running = 0;
707         igmpv3_send_report(in_dev, NULL);
708         __in_dev_put(in_dev);
709 }
710
711 static void igmp_ifc_timer_expire(unsigned long data)
712 {
713         struct in_device *in_dev = (struct in_device *)data;
714
715         igmpv3_send_cr(in_dev);
716         if (in_dev->mr_ifc_count) {
717                 in_dev->mr_ifc_count--;
718                 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
719         }
720         __in_dev_put(in_dev);
721 }
722
723 static void igmp_ifc_event(struct in_device *in_dev)
724 {
725         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
726                 return;
727         in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
728                 IGMP_Unsolicited_Report_Count;
729         igmp_ifc_start_timer(in_dev, 1);
730 }
731
732
733 static void igmp_timer_expire(unsigned long data)
734 {
735         struct ip_mc_list *im=(struct ip_mc_list *)data;
736         struct in_device *in_dev = im->interface;
737
738         spin_lock(&im->lock);
739         im->tm_running=0;
740
741         if (im->unsolicit_count) {
742                 im->unsolicit_count--;
743                 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
744         }
745         im->reporter = 1;
746         spin_unlock(&im->lock);
747
748         if (IGMP_V1_SEEN(in_dev))
749                 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
750         else if (IGMP_V2_SEEN(in_dev))
751                 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
752         else
753                 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
754
755         ip_ma_put(im);
756 }
757
758 /* mark EXCLUDE-mode sources */
759 static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
760 {
761         struct ip_sf_list *psf;
762         int i, scount;
763
764         scount = 0;
765         for (psf=pmc->sources; psf; psf=psf->sf_next) {
766                 if (scount == nsrcs)
767                         break;
768                 for (i=0; i<nsrcs; i++) {
769                         /* skip inactive filters */
770                         if (pmc->sfcount[MCAST_INCLUDE] ||
771                             pmc->sfcount[MCAST_EXCLUDE] !=
772                             psf->sf_count[MCAST_EXCLUDE])
773                                 continue;
774                         if (srcs[i] == psf->sf_inaddr) {
775                                 scount++;
776                                 break;
777                         }
778                 }
779         }
780         pmc->gsquery = 0;
781         if (scount == nsrcs)    /* all sources excluded */
782                 return 0;
783         return 1;
784 }
785
786 static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
787 {
788         struct ip_sf_list *psf;
789         int i, scount;
790
791         if (pmc->sfmode == MCAST_EXCLUDE)
792                 return igmp_xmarksources(pmc, nsrcs, srcs);
793
794         /* mark INCLUDE-mode sources */
795         scount = 0;
796         for (psf=pmc->sources; psf; psf=psf->sf_next) {
797                 if (scount == nsrcs)
798                         break;
799                 for (i=0; i<nsrcs; i++)
800                         if (srcs[i] == psf->sf_inaddr) {
801                                 psf->sf_gsresp = 1;
802                                 scount++;
803                                 break;
804                         }
805         }
806         if (!scount) {
807                 pmc->gsquery = 0;
808                 return 0;
809         }
810         pmc->gsquery = 1;
811         return 1;
812 }
813
814 static void igmp_heard_report(struct in_device *in_dev, __be32 group)
815 {
816         struct ip_mc_list *im;
817
818         /* Timers are only set for non-local groups */
819
820         if (group == IGMP_ALL_HOSTS)
821                 return;
822
823         read_lock(&in_dev->mc_list_lock);
824         for (im=in_dev->mc_list; im!=NULL; im=im->next) {
825                 if (im->multiaddr == group) {
826                         igmp_stop_timer(im);
827                         break;
828                 }
829         }
830         read_unlock(&in_dev->mc_list_lock);
831 }
832
833 static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
834         int len)
835 {
836         struct igmphdr          *ih = igmp_hdr(skb);
837         struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
838         struct ip_mc_list       *im;
839         __be32                  group = ih->group;
840         int                     max_delay;
841         int                     mark = 0;
842
843
844         if (len == 8) {
845                 if (ih->code == 0) {
846                         /* Alas, old v1 router presents here. */
847
848                         max_delay = IGMP_Query_Response_Interval;
849                         in_dev->mr_v1_seen = jiffies +
850                                 IGMP_V1_Router_Present_Timeout;
851                         group = 0;
852                 } else {
853                         /* v2 router present */
854                         max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
855                         in_dev->mr_v2_seen = jiffies +
856                                 IGMP_V2_Router_Present_Timeout;
857                 }
858                 /* cancel the interface change timer */
859                 in_dev->mr_ifc_count = 0;
860                 if (del_timer(&in_dev->mr_ifc_timer))
861                         __in_dev_put(in_dev);
862                 /* clear deleted report items */
863                 igmpv3_clear_delrec(in_dev);
864         } else if (len < 12) {
865                 return; /* ignore bogus packet; freed by caller */
866         } else { /* v3 */
867                 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
868                         return;
869
870                 ih3 = igmpv3_query_hdr(skb);
871                 if (ih3->nsrcs) {
872                         if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
873                                            + ntohs(ih3->nsrcs)*sizeof(__be32)))
874                                 return;
875                         ih3 = igmpv3_query_hdr(skb);
876                 }
877
878                 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
879                 if (!max_delay)
880                         max_delay = 1;  /* can't mod w/ 0 */
881                 in_dev->mr_maxdelay = max_delay;
882                 if (ih3->qrv)
883                         in_dev->mr_qrv = ih3->qrv;
884                 if (!group) { /* general query */
885                         if (ih3->nsrcs)
886                                 return; /* no sources allowed */
887                         igmp_gq_start_timer(in_dev);
888                         return;
889                 }
890                 /* mark sources to include, if group & source-specific */
891                 mark = ih3->nsrcs != 0;
892         }
893
894         /*
895          * - Start the timers in all of our membership records
896          *   that the query applies to for the interface on
897          *   which the query arrived excl. those that belong
898          *   to a "local" group (224.0.0.X)
899          * - For timers already running check if they need to
900          *   be reset.
901          * - Use the igmp->igmp_code field as the maximum
902          *   delay possible
903          */
904         read_lock(&in_dev->mc_list_lock);
905         for (im=in_dev->mc_list; im!=NULL; im=im->next) {
906                 int changed;
907
908                 if (group && group != im->multiaddr)
909                         continue;
910                 if (im->multiaddr == IGMP_ALL_HOSTS)
911                         continue;
912                 spin_lock_bh(&im->lock);
913                 if (im->tm_running)
914                         im->gsquery = im->gsquery && mark;
915                 else
916                         im->gsquery = mark;
917                 changed = !im->gsquery ||
918                         igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
919                 spin_unlock_bh(&im->lock);
920                 if (changed)
921                         igmp_mod_timer(im, max_delay);
922         }
923         read_unlock(&in_dev->mc_list_lock);
924 }
925
926 int igmp_rcv(struct sk_buff *skb)
927 {
928         /* This basically follows the spec line by line -- see RFC1112 */
929         struct igmphdr *ih;
930         struct in_device *in_dev = in_dev_get(skb->dev);
931         int len = skb->len;
932
933         if (in_dev==NULL) {
934                 kfree_skb(skb);
935                 return 0;
936         }
937
938         if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
939                 goto drop;
940
941         switch (skb->ip_summed) {
942         case CHECKSUM_COMPLETE:
943                 if (!csum_fold(skb->csum))
944                         break;
945                 /* fall through */
946         case CHECKSUM_NONE:
947                 skb->csum = 0;
948                 if (__skb_checksum_complete(skb))
949                         goto drop;
950         }
951
952         ih = igmp_hdr(skb);
953         switch (ih->type) {
954         case IGMP_HOST_MEMBERSHIP_QUERY:
955                 igmp_heard_query(in_dev, skb, len);
956                 break;
957         case IGMP_HOST_MEMBERSHIP_REPORT:
958         case IGMPV2_HOST_MEMBERSHIP_REPORT:
959         case IGMPV3_HOST_MEMBERSHIP_REPORT:
960                 /* Is it our report looped back? */
961                 if (((struct rtable*)skb->dst)->fl.iif == 0)
962                         break;
963                 /* don't rely on MC router hearing unicast reports */
964                 if (skb->pkt_type == PACKET_MULTICAST ||
965                     skb->pkt_type == PACKET_BROADCAST)
966                         igmp_heard_report(in_dev, ih->group);
967                 break;
968         case IGMP_PIM:
969 #ifdef CONFIG_IP_PIMSM_V1
970                 in_dev_put(in_dev);
971                 return pim_rcv_v1(skb);
972 #endif
973         case IGMP_DVMRP:
974         case IGMP_TRACE:
975         case IGMP_HOST_LEAVE_MESSAGE:
976         case IGMP_MTRACE:
977         case IGMP_MTRACE_RESP:
978                 break;
979         default:
980                 break;
981         }
982
983 drop:
984         in_dev_put(in_dev);
985         kfree_skb(skb);
986         return 0;
987 }
988
989 #endif
990
991
992 /*
993  *      Add a filter to a device
994  */
995
996 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
997 {
998         char buf[MAX_ADDR_LEN];
999         struct net_device *dev = in_dev->dev;
1000
1001         /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1002            We will get multicast token leakage, when IFF_MULTICAST
1003            is changed. This check should be done in dev->set_multicast_list
1004            routine. Something sort of:
1005            if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1006            --ANK
1007            */
1008         if (arp_mc_map(addr, buf, dev, 0) == 0)
1009                 dev_mc_add(dev,buf,dev->addr_len,0);
1010 }
1011
1012 /*
1013  *      Remove a filter from a device
1014  */
1015
1016 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1017 {
1018         char buf[MAX_ADDR_LEN];
1019         struct net_device *dev = in_dev->dev;
1020
1021         if (arp_mc_map(addr, buf, dev, 0) == 0)
1022                 dev_mc_delete(dev,buf,dev->addr_len,0);
1023 }
1024
1025 #ifdef CONFIG_IP_MULTICAST
1026 /*
1027  * deleted ip_mc_list manipulation
1028  */
1029 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1030 {
1031         struct ip_mc_list *pmc;
1032
1033         /* this is an "ip_mc_list" for convenience; only the fields below
1034          * are actually used. In particular, the refcnt and users are not
1035          * used for management of the delete list. Using the same structure
1036          * for deleted items allows change reports to use common code with
1037          * non-deleted or query-response MCA's.
1038          */
1039         pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1040         if (!pmc)
1041                 return;
1042         spin_lock_bh(&im->lock);
1043         pmc->interface = im->interface;
1044         in_dev_hold(in_dev);
1045         pmc->multiaddr = im->multiaddr;
1046         pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1047                 IGMP_Unsolicited_Report_Count;
1048         pmc->sfmode = im->sfmode;
1049         if (pmc->sfmode == MCAST_INCLUDE) {
1050                 struct ip_sf_list *psf;
1051
1052                 pmc->tomb = im->tomb;
1053                 pmc->sources = im->sources;
1054                 im->tomb = im->sources = NULL;
1055                 for (psf=pmc->sources; psf; psf=psf->sf_next)
1056                         psf->sf_crcount = pmc->crcount;
1057         }
1058         spin_unlock_bh(&im->lock);
1059
1060         spin_lock_bh(&in_dev->mc_tomb_lock);
1061         pmc->next = in_dev->mc_tomb;
1062         in_dev->mc_tomb = pmc;
1063         spin_unlock_bh(&in_dev->mc_tomb_lock);
1064 }
1065
1066 static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
1067 {
1068         struct ip_mc_list *pmc, *pmc_prev;
1069         struct ip_sf_list *psf, *psf_next;
1070
1071         spin_lock_bh(&in_dev->mc_tomb_lock);
1072         pmc_prev = NULL;
1073         for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1074                 if (pmc->multiaddr == multiaddr)
1075                         break;
1076                 pmc_prev = pmc;
1077         }
1078         if (pmc) {
1079                 if (pmc_prev)
1080                         pmc_prev->next = pmc->next;
1081                 else
1082                         in_dev->mc_tomb = pmc->next;
1083         }
1084         spin_unlock_bh(&in_dev->mc_tomb_lock);
1085         if (pmc) {
1086                 for (psf=pmc->tomb; psf; psf=psf_next) {
1087                         psf_next = psf->sf_next;
1088                         kfree(psf);
1089                 }
1090                 in_dev_put(pmc->interface);
1091                 kfree(pmc);
1092         }
1093 }
1094
1095 static void igmpv3_clear_delrec(struct in_device *in_dev)
1096 {
1097         struct ip_mc_list *pmc, *nextpmc;
1098
1099         spin_lock_bh(&in_dev->mc_tomb_lock);
1100         pmc = in_dev->mc_tomb;
1101         in_dev->mc_tomb = NULL;
1102         spin_unlock_bh(&in_dev->mc_tomb_lock);
1103
1104         for (; pmc; pmc = nextpmc) {
1105                 nextpmc = pmc->next;
1106                 ip_mc_clear_src(pmc);
1107                 in_dev_put(pmc->interface);
1108                 kfree(pmc);
1109         }
1110         /* clear dead sources, too */
1111         read_lock(&in_dev->mc_list_lock);
1112         for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1113                 struct ip_sf_list *psf, *psf_next;
1114
1115                 spin_lock_bh(&pmc->lock);
1116                 psf = pmc->tomb;
1117                 pmc->tomb = NULL;
1118                 spin_unlock_bh(&pmc->lock);
1119                 for (; psf; psf=psf_next) {
1120                         psf_next = psf->sf_next;
1121                         kfree(psf);
1122                 }
1123         }
1124         read_unlock(&in_dev->mc_list_lock);
1125 }
1126 #endif
1127
1128 static void igmp_group_dropped(struct ip_mc_list *im)
1129 {
1130         struct in_device *in_dev = im->interface;
1131 #ifdef CONFIG_IP_MULTICAST
1132         int reporter;
1133 #endif
1134
1135         if (im->loaded) {
1136                 im->loaded = 0;
1137                 ip_mc_filter_del(in_dev, im->multiaddr);
1138         }
1139
1140 #ifdef CONFIG_IP_MULTICAST
1141         if (im->multiaddr == IGMP_ALL_HOSTS)
1142                 return;
1143
1144         reporter = im->reporter;
1145         igmp_stop_timer(im);
1146
1147         if (!in_dev->dead) {
1148                 if (IGMP_V1_SEEN(in_dev))
1149                         goto done;
1150                 if (IGMP_V2_SEEN(in_dev)) {
1151                         if (reporter)
1152                                 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1153                         goto done;
1154                 }
1155                 /* IGMPv3 */
1156                 igmpv3_add_delrec(in_dev, im);
1157
1158                 igmp_ifc_event(in_dev);
1159         }
1160 done:
1161 #endif
1162         ip_mc_clear_src(im);
1163 }
1164
1165 static void igmp_group_added(struct ip_mc_list *im)
1166 {
1167         struct in_device *in_dev = im->interface;
1168
1169         if (im->loaded == 0) {
1170                 im->loaded = 1;
1171                 ip_mc_filter_add(in_dev, im->multiaddr);
1172         }
1173
1174 #ifdef CONFIG_IP_MULTICAST
1175         if (im->multiaddr == IGMP_ALL_HOSTS)
1176                 return;
1177
1178         if (in_dev->dead)
1179                 return;
1180         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1181                 spin_lock_bh(&im->lock);
1182                 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1183                 spin_unlock_bh(&im->lock);
1184                 return;
1185         }
1186         /* else, v3 */
1187
1188         im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1189                 IGMP_Unsolicited_Report_Count;
1190         igmp_ifc_event(in_dev);
1191 #endif
1192 }
1193
1194
1195 /*
1196  *      Multicast list managers
1197  */
1198
1199
1200 /*
1201  *      A socket has joined a multicast group on device dev.
1202  */
1203
1204 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1205 {
1206         struct ip_mc_list *im;
1207
1208         ASSERT_RTNL();
1209
1210         for (im=in_dev->mc_list; im; im=im->next) {
1211                 if (im->multiaddr == addr) {
1212                         im->users++;
1213                         ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1214                         goto out;
1215                 }
1216         }
1217
1218         im = kmalloc(sizeof(*im), GFP_KERNEL);
1219         if (!im)
1220                 goto out;
1221
1222         im->users=1;
1223         im->interface=in_dev;
1224         in_dev_hold(in_dev);
1225         im->multiaddr=addr;
1226         /* initial mode is (EX, empty) */
1227         im->sfmode = MCAST_EXCLUDE;
1228         im->sfcount[MCAST_INCLUDE] = 0;
1229         im->sfcount[MCAST_EXCLUDE] = 1;
1230         im->sources = NULL;
1231         im->tomb = NULL;
1232         im->crcount = 0;
1233         atomic_set(&im->refcnt, 1);
1234         spin_lock_init(&im->lock);
1235 #ifdef CONFIG_IP_MULTICAST
1236         im->tm_running=0;
1237         setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
1238         im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1239         im->reporter = 0;
1240         im->gsquery = 0;
1241 #endif
1242         im->loaded = 0;
1243         write_lock_bh(&in_dev->mc_list_lock);
1244         im->next=in_dev->mc_list;
1245         in_dev->mc_list=im;
1246         write_unlock_bh(&in_dev->mc_list_lock);
1247 #ifdef CONFIG_IP_MULTICAST
1248         igmpv3_del_delrec(in_dev, im->multiaddr);
1249 #endif
1250         igmp_group_added(im);
1251         if (!in_dev->dead)
1252                 ip_rt_multicast_event(in_dev);
1253 out:
1254         return;
1255 }
1256
1257 /*
1258  *      Resend IGMP JOIN report; used for bonding.
1259  */
1260 void ip_mc_rejoin_group(struct ip_mc_list *im)
1261 {
1262 #ifdef CONFIG_IP_MULTICAST
1263         struct in_device *in_dev = im->interface;
1264
1265         if (im->multiaddr == IGMP_ALL_HOSTS)
1266                 return;
1267
1268         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1269                 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1270                 return;
1271         }
1272         /* else, v3 */
1273         im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1274                 IGMP_Unsolicited_Report_Count;
1275         igmp_ifc_event(in_dev);
1276 #endif
1277 }
1278
1279 /*
1280  *      A socket has left a multicast group on device dev
1281  */
1282
1283 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1284 {
1285         struct ip_mc_list *i, **ip;
1286
1287         ASSERT_RTNL();
1288
1289         for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1290                 if (i->multiaddr==addr) {
1291                         if (--i->users == 0) {
1292                                 write_lock_bh(&in_dev->mc_list_lock);
1293                                 *ip = i->next;
1294                                 write_unlock_bh(&in_dev->mc_list_lock);
1295                                 igmp_group_dropped(i);
1296
1297                                 if (!in_dev->dead)
1298                                         ip_rt_multicast_event(in_dev);
1299
1300                                 ip_ma_put(i);
1301                                 return;
1302                         }
1303                         break;
1304                 }
1305         }
1306 }
1307
1308 /* Device going down */
1309
1310 void ip_mc_down(struct in_device *in_dev)
1311 {
1312         struct ip_mc_list *i;
1313
1314         ASSERT_RTNL();
1315
1316         for (i=in_dev->mc_list; i; i=i->next)
1317                 igmp_group_dropped(i);
1318
1319 #ifdef CONFIG_IP_MULTICAST
1320         in_dev->mr_ifc_count = 0;
1321         if (del_timer(&in_dev->mr_ifc_timer))
1322                 __in_dev_put(in_dev);
1323         in_dev->mr_gq_running = 0;
1324         if (del_timer(&in_dev->mr_gq_timer))
1325                 __in_dev_put(in_dev);
1326         igmpv3_clear_delrec(in_dev);
1327 #endif
1328
1329         ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1330 }
1331
1332 void ip_mc_init_dev(struct in_device *in_dev)
1333 {
1334         ASSERT_RTNL();
1335
1336         in_dev->mc_tomb = NULL;
1337 #ifdef CONFIG_IP_MULTICAST
1338         in_dev->mr_gq_running = 0;
1339         setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1340                         (unsigned long)in_dev);
1341         in_dev->mr_ifc_count = 0;
1342         setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1343                         (unsigned long)in_dev);
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 ip_sf_list *psf;
1695                 in_dev = pmc->interface;
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 == NULL) {
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_storage ss;
2167
2168                 psin = (struct sockaddr_in *)&ss;
2169                 memset(&ss, 0, sizeof(ss));
2170                 psin->sin_family = AF_INET;
2171                 psin->sin_addr.s_addr = psl->sl_addr[i];
2172                 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2173                         return -EFAULT;
2174         }
2175         return 0;
2176 done:
2177         rtnl_unlock();
2178         return err;
2179 }
2180
2181 /*
2182  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2183  */
2184 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2185 {
2186         struct inet_sock *inet = inet_sk(sk);
2187         struct ip_mc_socklist *pmc;
2188         struct ip_sf_socklist *psl;
2189         int i;
2190
2191         if (!MULTICAST(loc_addr))
2192                 return 1;
2193
2194         for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2195                 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2196                     pmc->multi.imr_ifindex == dif)
2197                         break;
2198         }
2199         if (!pmc)
2200                 return 1;
2201         psl = pmc->sflist;
2202         if (!psl)
2203                 return pmc->sfmode == MCAST_EXCLUDE;
2204
2205         for (i=0; i<psl->sl_count; i++) {
2206                 if (psl->sl_addr[i] == rmt_addr)
2207                         break;
2208         }
2209         if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2210                 return 0;
2211         if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2212                 return 0;
2213         return 1;
2214 }
2215
2216 /*
2217  *      A socket is closing.
2218  */
2219
2220 void ip_mc_drop_socket(struct sock *sk)
2221 {
2222         struct inet_sock *inet = inet_sk(sk);
2223         struct ip_mc_socklist *iml;
2224
2225         if (inet->mc_list == NULL)
2226                 return;
2227
2228         rtnl_lock();
2229         while ((iml = inet->mc_list) != NULL) {
2230                 struct in_device *in_dev;
2231                 inet->mc_list = iml->next;
2232
2233                 in_dev = inetdev_by_index(iml->multi.imr_ifindex);
2234                 (void) ip_mc_leave_src(sk, iml, in_dev);
2235                 if (in_dev != NULL) {
2236                         ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2237                         in_dev_put(in_dev);
2238                 }
2239                 sock_kfree_s(sk, iml, sizeof(*iml));
2240         }
2241         rtnl_unlock();
2242 }
2243
2244 int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
2245 {
2246         struct ip_mc_list *im;
2247         struct ip_sf_list *psf;
2248         int rv = 0;
2249
2250         read_lock(&in_dev->mc_list_lock);
2251         for (im=in_dev->mc_list; im; im=im->next) {
2252                 if (im->multiaddr == mc_addr)
2253                         break;
2254         }
2255         if (im && proto == IPPROTO_IGMP) {
2256                 rv = 1;
2257         } else if (im) {
2258                 if (src_addr) {
2259                         for (psf=im->sources; psf; psf=psf->sf_next) {
2260                                 if (psf->sf_inaddr == src_addr)
2261                                         break;
2262                         }
2263                         if (psf)
2264                                 rv = psf->sf_count[MCAST_INCLUDE] ||
2265                                         psf->sf_count[MCAST_EXCLUDE] !=
2266                                         im->sfcount[MCAST_EXCLUDE];
2267                         else
2268                                 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2269                 } else
2270                         rv = 1; /* unspecified source; tentatively allow */
2271         }
2272         read_unlock(&in_dev->mc_list_lock);
2273         return rv;
2274 }
2275
2276 #if defined(CONFIG_PROC_FS)
2277 struct igmp_mc_iter_state {
2278         struct net_device *dev;
2279         struct in_device *in_dev;
2280 };
2281
2282 #define igmp_mc_seq_private(seq)        ((struct igmp_mc_iter_state *)(seq)->private)
2283
2284 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2285 {
2286         struct ip_mc_list *im = NULL;
2287         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2288
2289         state->in_dev = NULL;
2290         for_each_netdev(&init_net, state->dev) {
2291                 struct in_device *in_dev;
2292                 in_dev = in_dev_get(state->dev);
2293                 if (!in_dev)
2294                         continue;
2295                 read_lock(&in_dev->mc_list_lock);
2296                 im = in_dev->mc_list;
2297                 if (im) {
2298                         state->in_dev = in_dev;
2299                         break;
2300                 }
2301                 read_unlock(&in_dev->mc_list_lock);
2302                 in_dev_put(in_dev);
2303         }
2304         return im;
2305 }
2306
2307 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2308 {
2309         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2310         im = im->next;
2311         while (!im) {
2312                 if (likely(state->in_dev != NULL)) {
2313                         read_unlock(&state->in_dev->mc_list_lock);
2314                         in_dev_put(state->in_dev);
2315                 }
2316                 state->dev = next_net_device(state->dev);
2317                 if (!state->dev) {
2318                         state->in_dev = NULL;
2319                         break;
2320                 }
2321                 state->in_dev = in_dev_get(state->dev);
2322                 if (!state->in_dev)
2323                         continue;
2324                 read_lock(&state->in_dev->mc_list_lock);
2325                 im = state->in_dev->mc_list;
2326         }
2327         return im;
2328 }
2329
2330 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2331 {
2332         struct ip_mc_list *im = igmp_mc_get_first(seq);
2333         if (im)
2334                 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2335                         --pos;
2336         return pos ? NULL : im;
2337 }
2338
2339 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2340 {
2341         read_lock(&dev_base_lock);
2342         return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2343 }
2344
2345 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2346 {
2347         struct ip_mc_list *im;
2348         if (v == SEQ_START_TOKEN)
2349                 im = igmp_mc_get_first(seq);
2350         else
2351                 im = igmp_mc_get_next(seq, v);
2352         ++*pos;
2353         return im;
2354 }
2355
2356 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2357 {
2358         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2359         if (likely(state->in_dev != NULL)) {
2360                 read_unlock(&state->in_dev->mc_list_lock);
2361                 in_dev_put(state->in_dev);
2362                 state->in_dev = NULL;
2363         }
2364         state->dev = NULL;
2365         read_unlock(&dev_base_lock);
2366 }
2367
2368 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2369 {
2370         if (v == SEQ_START_TOKEN)
2371                 seq_puts(seq,
2372                          "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2373         else {
2374                 struct ip_mc_list *im = (struct ip_mc_list *)v;
2375                 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2376                 char   *querier;
2377 #ifdef CONFIG_IP_MULTICAST
2378                 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2379                           IGMP_V2_SEEN(state->in_dev) ? "V2" :
2380                           "V3";
2381 #else
2382                 querier = "NONE";
2383 #endif
2384
2385                 if (state->in_dev->mc_list == im) {
2386                         seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2387                                    state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2388                 }
2389
2390                 seq_printf(seq,
2391                            "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2392                            im->multiaddr, im->users,
2393                            im->tm_running, im->tm_running ?
2394                            jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2395                            im->reporter);
2396         }
2397         return 0;
2398 }
2399
2400 static const struct seq_operations igmp_mc_seq_ops = {
2401         .start  =       igmp_mc_seq_start,
2402         .next   =       igmp_mc_seq_next,
2403         .stop   =       igmp_mc_seq_stop,
2404         .show   =       igmp_mc_seq_show,
2405 };
2406
2407 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2408 {
2409         return seq_open_private(file, &igmp_mc_seq_ops,
2410                         sizeof(struct igmp_mc_iter_state));
2411 }
2412
2413 static const struct file_operations igmp_mc_seq_fops = {
2414         .owner          =       THIS_MODULE,
2415         .open           =       igmp_mc_seq_open,
2416         .read           =       seq_read,
2417         .llseek         =       seq_lseek,
2418         .release        =       seq_release_private,
2419 };
2420
2421 struct igmp_mcf_iter_state {
2422         struct net_device *dev;
2423         struct in_device *idev;
2424         struct ip_mc_list *im;
2425 };
2426
2427 #define igmp_mcf_seq_private(seq)       ((struct igmp_mcf_iter_state *)(seq)->private)
2428
2429 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2430 {
2431         struct ip_sf_list *psf = NULL;
2432         struct ip_mc_list *im = NULL;
2433         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2434
2435         state->idev = NULL;
2436         state->im = NULL;
2437         for_each_netdev(&init_net, state->dev) {
2438                 struct in_device *idev;
2439                 idev = in_dev_get(state->dev);
2440                 if (unlikely(idev == NULL))
2441                         continue;
2442                 read_lock(&idev->mc_list_lock);
2443                 im = idev->mc_list;
2444                 if (likely(im != NULL)) {
2445                         spin_lock_bh(&im->lock);
2446                         psf = im->sources;
2447                         if (likely(psf != NULL)) {
2448                                 state->im = im;
2449                                 state->idev = idev;
2450                                 break;
2451                         }
2452                         spin_unlock_bh(&im->lock);
2453                 }
2454                 read_unlock(&idev->mc_list_lock);
2455                 in_dev_put(idev);
2456         }
2457         return psf;
2458 }
2459
2460 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2461 {
2462         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2463
2464         psf = psf->sf_next;
2465         while (!psf) {
2466                 spin_unlock_bh(&state->im->lock);
2467                 state->im = state->im->next;
2468                 while (!state->im) {
2469                         if (likely(state->idev != NULL)) {
2470                                 read_unlock(&state->idev->mc_list_lock);
2471                                 in_dev_put(state->idev);
2472                         }
2473                         state->dev = next_net_device(state->dev);
2474                         if (!state->dev) {
2475                                 state->idev = NULL;
2476                                 goto out;
2477                         }
2478                         state->idev = in_dev_get(state->dev);
2479                         if (!state->idev)
2480                                 continue;
2481                         read_lock(&state->idev->mc_list_lock);
2482                         state->im = state->idev->mc_list;
2483                 }
2484                 if (!state->im)
2485                         break;
2486                 spin_lock_bh(&state->im->lock);
2487                 psf = state->im->sources;
2488         }
2489 out:
2490         return psf;
2491 }
2492
2493 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2494 {
2495         struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2496         if (psf)
2497                 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2498                         --pos;
2499         return pos ? NULL : psf;
2500 }
2501
2502 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2503 {
2504         read_lock(&dev_base_lock);
2505         return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2506 }
2507
2508 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2509 {
2510         struct ip_sf_list *psf;
2511         if (v == SEQ_START_TOKEN)
2512                 psf = igmp_mcf_get_first(seq);
2513         else
2514                 psf = igmp_mcf_get_next(seq, v);
2515         ++*pos;
2516         return psf;
2517 }
2518
2519 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2520 {
2521         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2522         if (likely(state->im != NULL)) {
2523                 spin_unlock_bh(&state->im->lock);
2524                 state->im = NULL;
2525         }
2526         if (likely(state->idev != NULL)) {
2527                 read_unlock(&state->idev->mc_list_lock);
2528                 in_dev_put(state->idev);
2529                 state->idev = NULL;
2530         }
2531         state->dev = NULL;
2532         read_unlock(&dev_base_lock);
2533 }
2534
2535 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2536 {
2537         struct ip_sf_list *psf = (struct ip_sf_list *)v;
2538         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2539
2540         if (v == SEQ_START_TOKEN) {
2541                 seq_printf(seq,
2542                            "%3s %6s "
2543                            "%10s %10s %6s %6s\n", "Idx",
2544                            "Device", "MCA",
2545                            "SRC", "INC", "EXC");
2546         } else {
2547                 seq_printf(seq,
2548                            "%3d %6.6s 0x%08x "
2549                            "0x%08x %6lu %6lu\n",
2550                            state->dev->ifindex, state->dev->name,
2551                            ntohl(state->im->multiaddr),
2552                            ntohl(psf->sf_inaddr),
2553                            psf->sf_count[MCAST_INCLUDE],
2554                            psf->sf_count[MCAST_EXCLUDE]);
2555         }
2556         return 0;
2557 }
2558
2559 static const struct seq_operations igmp_mcf_seq_ops = {
2560         .start  =       igmp_mcf_seq_start,
2561         .next   =       igmp_mcf_seq_next,
2562         .stop   =       igmp_mcf_seq_stop,
2563         .show   =       igmp_mcf_seq_show,
2564 };
2565
2566 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2567 {
2568         return seq_open_private(file, &igmp_mcf_seq_ops,
2569                         sizeof(struct igmp_mcf_iter_state));
2570 }
2571
2572 static const struct file_operations igmp_mcf_seq_fops = {
2573         .owner          =       THIS_MODULE,
2574         .open           =       igmp_mcf_seq_open,
2575         .read           =       seq_read,
2576         .llseek         =       seq_lseek,
2577         .release        =       seq_release_private,
2578 };
2579
2580 int __init igmp_mc_proc_init(void)
2581 {
2582         proc_net_fops_create(&init_net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2583         proc_net_fops_create(&init_net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2584         return 0;
2585 }
2586 #endif
2587
2588 EXPORT_SYMBOL(ip_mc_dec_group);
2589 EXPORT_SYMBOL(ip_mc_inc_group);
2590 EXPORT_SYMBOL(ip_mc_join_group);
2591 EXPORT_SYMBOL(ip_mc_rejoin_group);