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