GNU Linux-libre 4.19.211-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                         kfree_rcu(psl, rcu);
2420                 }
2421                 rcu_assign_pointer(pmc->sflist, newpsl);
2422                 psl = newpsl;
2423         }
2424         rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2425         for (i = 0; i < psl->sl_count; i++) {
2426                 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2427                         sizeof(__be32));
2428                 if (rv == 0)
2429                         break;
2430         }
2431         if (rv == 0)            /* address already there is an error */
2432                 goto done;
2433         for (j = psl->sl_count-1; j >= i; j--)
2434                 psl->sl_addr[j+1] = psl->sl_addr[j];
2435         psl->sl_addr[i] = mreqs->imr_sourceaddr;
2436         psl->sl_count++;
2437         err = 0;
2438         /* update the interface list */
2439         ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2440                 &mreqs->imr_sourceaddr, 1);
2441 done:
2442         if (leavegroup)
2443                 err = ip_mc_leave_group(sk, &imr);
2444         return err;
2445 }
2446
2447 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2448 {
2449         int err = 0;
2450         struct ip_mreqn imr;
2451         __be32 addr = msf->imsf_multiaddr;
2452         struct ip_mc_socklist *pmc;
2453         struct in_device *in_dev;
2454         struct inet_sock *inet = inet_sk(sk);
2455         struct ip_sf_socklist *newpsl, *psl;
2456         struct net *net = sock_net(sk);
2457         int leavegroup = 0;
2458
2459         if (!ipv4_is_multicast(addr))
2460                 return -EINVAL;
2461         if (msf->imsf_fmode != MCAST_INCLUDE &&
2462             msf->imsf_fmode != MCAST_EXCLUDE)
2463                 return -EINVAL;
2464
2465         ASSERT_RTNL();
2466
2467         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2468         imr.imr_address.s_addr = msf->imsf_interface;
2469         imr.imr_ifindex = ifindex;
2470         in_dev = ip_mc_find_dev(net, &imr);
2471
2472         if (!in_dev) {
2473                 err = -ENODEV;
2474                 goto done;
2475         }
2476
2477         /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2478         if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2479                 leavegroup = 1;
2480                 goto done;
2481         }
2482
2483         for_each_pmc_rtnl(inet, pmc) {
2484                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2485                     pmc->multi.imr_ifindex == imr.imr_ifindex)
2486                         break;
2487         }
2488         if (!pmc) {             /* must have a prior join */
2489                 err = -EINVAL;
2490                 goto done;
2491         }
2492         if (msf->imsf_numsrc) {
2493                 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2494                                                            GFP_KERNEL);
2495                 if (!newpsl) {
2496                         err = -ENOBUFS;
2497                         goto done;
2498                 }
2499                 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2500                 memcpy(newpsl->sl_addr, msf->imsf_slist,
2501                         msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2502                 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2503                         msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2504                 if (err) {
2505                         sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2506                         goto done;
2507                 }
2508         } else {
2509                 newpsl = NULL;
2510                 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2511                                      msf->imsf_fmode, 0, NULL, 0);
2512         }
2513         psl = rtnl_dereference(pmc->sflist);
2514         if (psl) {
2515                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2516                         psl->sl_count, psl->sl_addr, 0);
2517                 /* decrease mem now to avoid the memleak warning */
2518                 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2519                 kfree_rcu(psl, rcu);
2520         } else
2521                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2522                         0, NULL, 0);
2523         rcu_assign_pointer(pmc->sflist, newpsl);
2524         pmc->sfmode = msf->imsf_fmode;
2525         err = 0;
2526 done:
2527         if (leavegroup)
2528                 err = ip_mc_leave_group(sk, &imr);
2529         return err;
2530 }
2531
2532 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2533         struct ip_msfilter __user *optval, int __user *optlen)
2534 {
2535         int err, len, count, copycount;
2536         struct ip_mreqn imr;
2537         __be32 addr = msf->imsf_multiaddr;
2538         struct ip_mc_socklist *pmc;
2539         struct in_device *in_dev;
2540         struct inet_sock *inet = inet_sk(sk);
2541         struct ip_sf_socklist *psl;
2542         struct net *net = sock_net(sk);
2543
2544         ASSERT_RTNL();
2545
2546         if (!ipv4_is_multicast(addr))
2547                 return -EINVAL;
2548
2549         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2550         imr.imr_address.s_addr = msf->imsf_interface;
2551         imr.imr_ifindex = 0;
2552         in_dev = ip_mc_find_dev(net, &imr);
2553
2554         if (!in_dev) {
2555                 err = -ENODEV;
2556                 goto done;
2557         }
2558         err = -EADDRNOTAVAIL;
2559
2560         for_each_pmc_rtnl(inet, pmc) {
2561                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2562                     pmc->multi.imr_ifindex == imr.imr_ifindex)
2563                         break;
2564         }
2565         if (!pmc)               /* must have a prior join */
2566                 goto done;
2567         msf->imsf_fmode = pmc->sfmode;
2568         psl = rtnl_dereference(pmc->sflist);
2569         if (!psl) {
2570                 len = 0;
2571                 count = 0;
2572         } else {
2573                 count = psl->sl_count;
2574         }
2575         copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2576         len = copycount * sizeof(psl->sl_addr[0]);
2577         msf->imsf_numsrc = count;
2578         if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2579             copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2580                 return -EFAULT;
2581         }
2582         if (len &&
2583             copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2584                 return -EFAULT;
2585         return 0;
2586 done:
2587         return err;
2588 }
2589
2590 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2591         struct group_filter __user *optval, int __user *optlen)
2592 {
2593         int err, i, count, copycount;
2594         struct sockaddr_in *psin;
2595         __be32 addr;
2596         struct ip_mc_socklist *pmc;
2597         struct inet_sock *inet = inet_sk(sk);
2598         struct ip_sf_socklist *psl;
2599
2600         ASSERT_RTNL();
2601
2602         psin = (struct sockaddr_in *)&gsf->gf_group;
2603         if (psin->sin_family != AF_INET)
2604                 return -EINVAL;
2605         addr = psin->sin_addr.s_addr;
2606         if (!ipv4_is_multicast(addr))
2607                 return -EINVAL;
2608
2609         err = -EADDRNOTAVAIL;
2610
2611         for_each_pmc_rtnl(inet, pmc) {
2612                 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2613                     pmc->multi.imr_ifindex == gsf->gf_interface)
2614                         break;
2615         }
2616         if (!pmc)               /* must have a prior join */
2617                 goto done;
2618         gsf->gf_fmode = pmc->sfmode;
2619         psl = rtnl_dereference(pmc->sflist);
2620         count = psl ? psl->sl_count : 0;
2621         copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2622         gsf->gf_numsrc = count;
2623         if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2624             copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2625                 return -EFAULT;
2626         }
2627         for (i = 0; i < copycount; i++) {
2628                 struct sockaddr_storage ss;
2629
2630                 psin = (struct sockaddr_in *)&ss;
2631                 memset(&ss, 0, sizeof(ss));
2632                 psin->sin_family = AF_INET;
2633                 psin->sin_addr.s_addr = psl->sl_addr[i];
2634                 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2635                         return -EFAULT;
2636         }
2637         return 0;
2638 done:
2639         return err;
2640 }
2641
2642 /*
2643  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2644  */
2645 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2646                    int dif, int sdif)
2647 {
2648         struct inet_sock *inet = inet_sk(sk);
2649         struct ip_mc_socklist *pmc;
2650         struct ip_sf_socklist *psl;
2651         int i;
2652         int ret;
2653
2654         ret = 1;
2655         if (!ipv4_is_multicast(loc_addr))
2656                 goto out;
2657
2658         rcu_read_lock();
2659         for_each_pmc_rcu(inet, pmc) {
2660                 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2661                     (pmc->multi.imr_ifindex == dif ||
2662                      (sdif && pmc->multi.imr_ifindex == sdif)))
2663                         break;
2664         }
2665         ret = inet->mc_all;
2666         if (!pmc)
2667                 goto unlock;
2668         psl = rcu_dereference(pmc->sflist);
2669         ret = (pmc->sfmode == MCAST_EXCLUDE);
2670         if (!psl)
2671                 goto unlock;
2672
2673         for (i = 0; i < psl->sl_count; i++) {
2674                 if (psl->sl_addr[i] == rmt_addr)
2675                         break;
2676         }
2677         ret = 0;
2678         if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2679                 goto unlock;
2680         if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2681                 goto unlock;
2682         ret = 1;
2683 unlock:
2684         rcu_read_unlock();
2685 out:
2686         return ret;
2687 }
2688
2689 /*
2690  *      A socket is closing.
2691  */
2692
2693 void ip_mc_drop_socket(struct sock *sk)
2694 {
2695         struct inet_sock *inet = inet_sk(sk);
2696         struct ip_mc_socklist *iml;
2697         struct net *net = sock_net(sk);
2698
2699         if (!inet->mc_list)
2700                 return;
2701
2702         rtnl_lock();
2703         while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2704                 struct in_device *in_dev;
2705
2706                 inet->mc_list = iml->next_rcu;
2707                 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2708                 (void) ip_mc_leave_src(sk, iml, in_dev);
2709                 if (in_dev)
2710                         ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2711                 /* decrease mem now to avoid the memleak warning */
2712                 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2713                 kfree_rcu(iml, rcu);
2714         }
2715         rtnl_unlock();
2716 }
2717
2718 /* called with rcu_read_lock() */
2719 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2720 {
2721         struct ip_mc_list *im;
2722         struct ip_mc_list __rcu **mc_hash;
2723         struct ip_sf_list *psf;
2724         int rv = 0;
2725
2726         mc_hash = rcu_dereference(in_dev->mc_hash);
2727         if (mc_hash) {
2728                 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2729
2730                 for (im = rcu_dereference(mc_hash[hash]);
2731                      im != NULL;
2732                      im = rcu_dereference(im->next_hash)) {
2733                         if (im->multiaddr == mc_addr)
2734                                 break;
2735                 }
2736         } else {
2737                 for_each_pmc_rcu(in_dev, im) {
2738                         if (im->multiaddr == mc_addr)
2739                                 break;
2740                 }
2741         }
2742         if (im && proto == IPPROTO_IGMP) {
2743                 rv = 1;
2744         } else if (im) {
2745                 if (src_addr) {
2746                         spin_lock_bh(&im->lock);
2747                         for (psf = im->sources; psf; psf = psf->sf_next) {
2748                                 if (psf->sf_inaddr == src_addr)
2749                                         break;
2750                         }
2751                         if (psf)
2752                                 rv = psf->sf_count[MCAST_INCLUDE] ||
2753                                         psf->sf_count[MCAST_EXCLUDE] !=
2754                                         im->sfcount[MCAST_EXCLUDE];
2755                         else
2756                                 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2757                         spin_unlock_bh(&im->lock);
2758                 } else
2759                         rv = 1; /* unspecified source; tentatively allow */
2760         }
2761         return rv;
2762 }
2763
2764 #if defined(CONFIG_PROC_FS)
2765 struct igmp_mc_iter_state {
2766         struct seq_net_private p;
2767         struct net_device *dev;
2768         struct in_device *in_dev;
2769 };
2770
2771 #define igmp_mc_seq_private(seq)        ((struct igmp_mc_iter_state *)(seq)->private)
2772
2773 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2774 {
2775         struct net *net = seq_file_net(seq);
2776         struct ip_mc_list *im = NULL;
2777         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2778
2779         state->in_dev = NULL;
2780         for_each_netdev_rcu(net, state->dev) {
2781                 struct in_device *in_dev;
2782
2783                 in_dev = __in_dev_get_rcu(state->dev);
2784                 if (!in_dev)
2785                         continue;
2786                 im = rcu_dereference(in_dev->mc_list);
2787                 if (im) {
2788                         state->in_dev = in_dev;
2789                         break;
2790                 }
2791         }
2792         return im;
2793 }
2794
2795 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2796 {
2797         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2798
2799         im = rcu_dereference(im->next_rcu);
2800         while (!im) {
2801                 state->dev = next_net_device_rcu(state->dev);
2802                 if (!state->dev) {
2803                         state->in_dev = NULL;
2804                         break;
2805                 }
2806                 state->in_dev = __in_dev_get_rcu(state->dev);
2807                 if (!state->in_dev)
2808                         continue;
2809                 im = rcu_dereference(state->in_dev->mc_list);
2810         }
2811         return im;
2812 }
2813
2814 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2815 {
2816         struct ip_mc_list *im = igmp_mc_get_first(seq);
2817         if (im)
2818                 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2819                         --pos;
2820         return pos ? NULL : im;
2821 }
2822
2823 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2824         __acquires(rcu)
2825 {
2826         rcu_read_lock();
2827         return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2828 }
2829
2830 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2831 {
2832         struct ip_mc_list *im;
2833         if (v == SEQ_START_TOKEN)
2834                 im = igmp_mc_get_first(seq);
2835         else
2836                 im = igmp_mc_get_next(seq, v);
2837         ++*pos;
2838         return im;
2839 }
2840
2841 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2842         __releases(rcu)
2843 {
2844         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2845
2846         state->in_dev = NULL;
2847         state->dev = NULL;
2848         rcu_read_unlock();
2849 }
2850
2851 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2852 {
2853         if (v == SEQ_START_TOKEN)
2854                 seq_puts(seq,
2855                          "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2856         else {
2857                 struct ip_mc_list *im = (struct ip_mc_list *)v;
2858                 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2859                 char   *querier;
2860                 long delta;
2861
2862 #ifdef CONFIG_IP_MULTICAST
2863                 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2864                           IGMP_V2_SEEN(state->in_dev) ? "V2" :
2865                           "V3";
2866 #else
2867                 querier = "NONE";
2868 #endif
2869
2870                 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2871                         seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2872                                    state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2873                 }
2874
2875                 delta = im->timer.expires - jiffies;
2876                 seq_printf(seq,
2877                            "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2878                            im->multiaddr, im->users,
2879                            im->tm_running,
2880                            im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2881                            im->reporter);
2882         }
2883         return 0;
2884 }
2885
2886 static const struct seq_operations igmp_mc_seq_ops = {
2887         .start  =       igmp_mc_seq_start,
2888         .next   =       igmp_mc_seq_next,
2889         .stop   =       igmp_mc_seq_stop,
2890         .show   =       igmp_mc_seq_show,
2891 };
2892
2893 struct igmp_mcf_iter_state {
2894         struct seq_net_private p;
2895         struct net_device *dev;
2896         struct in_device *idev;
2897         struct ip_mc_list *im;
2898 };
2899
2900 #define igmp_mcf_seq_private(seq)       ((struct igmp_mcf_iter_state *)(seq)->private)
2901
2902 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2903 {
2904         struct net *net = seq_file_net(seq);
2905         struct ip_sf_list *psf = NULL;
2906         struct ip_mc_list *im = NULL;
2907         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2908
2909         state->idev = NULL;
2910         state->im = NULL;
2911         for_each_netdev_rcu(net, state->dev) {
2912                 struct in_device *idev;
2913                 idev = __in_dev_get_rcu(state->dev);
2914                 if (unlikely(!idev))
2915                         continue;
2916                 im = rcu_dereference(idev->mc_list);
2917                 if (likely(im)) {
2918                         spin_lock_bh(&im->lock);
2919                         psf = im->sources;
2920                         if (likely(psf)) {
2921                                 state->im = im;
2922                                 state->idev = idev;
2923                                 break;
2924                         }
2925                         spin_unlock_bh(&im->lock);
2926                 }
2927         }
2928         return psf;
2929 }
2930
2931 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2932 {
2933         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2934
2935         psf = psf->sf_next;
2936         while (!psf) {
2937                 spin_unlock_bh(&state->im->lock);
2938                 state->im = state->im->next;
2939                 while (!state->im) {
2940                         state->dev = next_net_device_rcu(state->dev);
2941                         if (!state->dev) {
2942                                 state->idev = NULL;
2943                                 goto out;
2944                         }
2945                         state->idev = __in_dev_get_rcu(state->dev);
2946                         if (!state->idev)
2947                                 continue;
2948                         state->im = rcu_dereference(state->idev->mc_list);
2949                 }
2950                 if (!state->im)
2951                         break;
2952                 spin_lock_bh(&state->im->lock);
2953                 psf = state->im->sources;
2954         }
2955 out:
2956         return psf;
2957 }
2958
2959 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2960 {
2961         struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2962         if (psf)
2963                 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2964                         --pos;
2965         return pos ? NULL : psf;
2966 }
2967
2968 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2969         __acquires(rcu)
2970 {
2971         rcu_read_lock();
2972         return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2973 }
2974
2975 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2976 {
2977         struct ip_sf_list *psf;
2978         if (v == SEQ_START_TOKEN)
2979                 psf = igmp_mcf_get_first(seq);
2980         else
2981                 psf = igmp_mcf_get_next(seq, v);
2982         ++*pos;
2983         return psf;
2984 }
2985
2986 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2987         __releases(rcu)
2988 {
2989         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2990         if (likely(state->im)) {
2991                 spin_unlock_bh(&state->im->lock);
2992                 state->im = NULL;
2993         }
2994         state->idev = NULL;
2995         state->dev = NULL;
2996         rcu_read_unlock();
2997 }
2998
2999 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
3000 {
3001         struct ip_sf_list *psf = (struct ip_sf_list *)v;
3002         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
3003
3004         if (v == SEQ_START_TOKEN) {
3005                 seq_puts(seq, "Idx Device        MCA        SRC    INC    EXC\n");
3006         } else {
3007                 seq_printf(seq,
3008                            "%3d %6.6s 0x%08x "
3009                            "0x%08x %6lu %6lu\n",
3010                            state->dev->ifindex, state->dev->name,
3011                            ntohl(state->im->multiaddr),
3012                            ntohl(psf->sf_inaddr),
3013                            psf->sf_count[MCAST_INCLUDE],
3014                            psf->sf_count[MCAST_EXCLUDE]);
3015         }
3016         return 0;
3017 }
3018
3019 static const struct seq_operations igmp_mcf_seq_ops = {
3020         .start  =       igmp_mcf_seq_start,
3021         .next   =       igmp_mcf_seq_next,
3022         .stop   =       igmp_mcf_seq_stop,
3023         .show   =       igmp_mcf_seq_show,
3024 };
3025
3026 static int __net_init igmp_net_init(struct net *net)
3027 {
3028         struct proc_dir_entry *pde;
3029         int err;
3030
3031         pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3032                         sizeof(struct igmp_mc_iter_state));
3033         if (!pde)
3034                 goto out_igmp;
3035         pde = proc_create_net("mcfilter", 0444, net->proc_net,
3036                         &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
3037         if (!pde)
3038                 goto out_mcfilter;
3039         err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3040                                    SOCK_DGRAM, 0, net);
3041         if (err < 0) {
3042                 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3043                        err);
3044                 goto out_sock;
3045         }
3046
3047         return 0;
3048
3049 out_sock:
3050         remove_proc_entry("mcfilter", net->proc_net);
3051 out_mcfilter:
3052         remove_proc_entry("igmp", net->proc_net);
3053 out_igmp:
3054         return -ENOMEM;
3055 }
3056
3057 static void __net_exit igmp_net_exit(struct net *net)
3058 {
3059         remove_proc_entry("mcfilter", net->proc_net);
3060         remove_proc_entry("igmp", net->proc_net);
3061         inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3062 }
3063
3064 static struct pernet_operations igmp_net_ops = {
3065         .init = igmp_net_init,
3066         .exit = igmp_net_exit,
3067 };
3068 #endif
3069
3070 static int igmp_netdev_event(struct notifier_block *this,
3071                              unsigned long event, void *ptr)
3072 {
3073         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3074         struct in_device *in_dev;
3075
3076         switch (event) {
3077         case NETDEV_RESEND_IGMP:
3078                 in_dev = __in_dev_get_rtnl(dev);
3079                 if (in_dev)
3080                         ip_mc_rejoin_groups(in_dev);
3081                 break;
3082         default:
3083                 break;
3084         }
3085         return NOTIFY_DONE;
3086 }
3087
3088 static struct notifier_block igmp_notifier = {
3089         .notifier_call = igmp_netdev_event,
3090 };
3091
3092 int __init igmp_mc_init(void)
3093 {
3094 #if defined(CONFIG_PROC_FS)
3095         int err;
3096
3097         err = register_pernet_subsys(&igmp_net_ops);
3098         if (err)
3099                 return err;
3100         err = register_netdevice_notifier(&igmp_notifier);
3101         if (err)
3102                 goto reg_notif_fail;
3103         return 0;
3104
3105 reg_notif_fail:
3106         unregister_pernet_subsys(&igmp_net_ops);
3107         return err;
3108 #else
3109         return register_netdevice_notifier(&igmp_notifier);
3110 #endif
3111 }