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