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