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