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