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