GNU Linux-libre 4.9.333-gnu1
[releases.git] / net / ipv4 / igmp.c
1 /*
2  *      Linux NET3:     Internet Group Management Protocol  [IGMP]
3  *
4  *      This code implements the IGMP protocol as defined in RFC1112. There has
5  *      been a further revision of this protocol since which is now supported.
6  *
7  *      If you have trouble with this module be careful what gcc you have used,
8  *      the older version didn't come out right using gcc 2.5.8, the newer one
9  *      seems to fall out with gcc 2.6.2.
10  *
11  *      Authors:
12  *              Alan Cox <alan@lxorguk.ukuu.org.uk>
13  *
14  *      This program is free software; you can redistribute it and/or
15  *      modify it under the terms of the GNU General Public License
16  *      as published by the Free Software Foundation; either version
17  *      2 of the License, or (at your option) any later version.
18  *
19  *      Fixes:
20  *
21  *              Alan Cox        :       Added lots of __inline__ to optimise
22  *                                      the memory usage of all the tiny little
23  *                                      functions.
24  *              Alan Cox        :       Dumped the header building experiment.
25  *              Alan Cox        :       Minor tweaks ready for multicast routing
26  *                                      and extended IGMP protocol.
27  *              Alan Cox        :       Removed a load of inline directives. Gcc 2.5.8
28  *                                      writes utterly bogus code otherwise (sigh)
29  *                                      fixed IGMP loopback to behave in the manner
30  *                                      desired by mrouted, fixed the fact it has been
31  *                                      broken since 1.3.6 and cleaned up a few minor
32  *                                      points.
33  *
34  *              Chih-Jen Chang  :       Tried to revise IGMP to Version 2
35  *              Tsu-Sheng Tsao          E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
36  *                                      The enhancements are mainly based on Steve Deering's
37  *                                      ipmulti-3.5 source code.
38  *              Chih-Jen Chang  :       Added the igmp_get_mrouter_info and
39  *              Tsu-Sheng Tsao          igmp_set_mrouter_info to keep track of
40  *                                      the mrouted version on that device.
41  *              Chih-Jen Chang  :       Added the max_resp_time parameter to
42  *              Tsu-Sheng Tsao          igmp_heard_query(). Using this parameter
43  *                                      to identify the multicast router version
44  *                                      and do what the IGMP version 2 specified.
45  *              Chih-Jen Chang  :       Added a timer to revert to IGMP V2 router
46  *              Tsu-Sheng Tsao          if the specified time expired.
47  *              Alan Cox        :       Stop IGMP from 0.0.0.0 being accepted.
48  *              Alan Cox        :       Use GFP_ATOMIC in the right places.
49  *              Christian Daudt :       igmp timer wasn't set for local group
50  *                                      memberships but was being deleted,
51  *                                      which caused a "del_timer() called
52  *                                      from %p with timer not initialized\n"
53  *                                      message (960131).
54  *              Christian Daudt :       removed del_timer from
55  *                                      igmp_timer_expire function (960205).
56  *             Christian Daudt :       igmp_heard_report now only calls
57  *                                     igmp_timer_expire if tm->running is
58  *                                     true (960216).
59  *              Malcolm Beattie :       ttl comparison wrong in igmp_rcv made
60  *                                      igmp_heard_query never trigger. Expiry
61  *                                      miscalculation fixed in igmp_heard_query
62  *                                      and random() made to return unsigned to
63  *                                      prevent negative expiry times.
64  *              Alexey Kuznetsov:       Wrong group leaving behaviour, backport
65  *                                      fix from pending 2.1.x patches.
66  *              Alan Cox:               Forget to enable FDDI support earlier.
67  *              Alexey Kuznetsov:       Fixed leaving groups on device down.
68  *              Alexey Kuznetsov:       Accordance to igmp-v2-06 draft.
69  *              David L Stevens:        IGMPv3 support, with help from
70  *                                      Vinay Kulkarni
71  */
72
73 #include <linux/module.h>
74 #include <linux/slab.h>
75 #include <asm/uaccess.h>
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/jiffies.h>
79 #include <linux/string.h>
80 #include <linux/socket.h>
81 #include <linux/sockios.h>
82 #include <linux/in.h>
83 #include <linux/inet.h>
84 #include <linux/netdevice.h>
85 #include <linux/skbuff.h>
86 #include <linux/inetdevice.h>
87 #include <linux/igmp.h>
88 #include <linux/if_arp.h>
89 #include <linux/rtnetlink.h>
90 #include <linux/times.h>
91 #include <linux/pkt_sched.h>
92 #include <linux/byteorder/generic.h>
93
94 #include <net/net_namespace.h>
95 #include <net/arp.h>
96 #include <net/ip.h>
97 #include <net/protocol.h>
98 #include <net/route.h>
99 #include <net/sock.h>
100 #include <net/checksum.h>
101 #include <net/inet_common.h>
102 #include <linux/netfilter_ipv4.h>
103 #ifdef CONFIG_IP_MROUTE
104 #include <linux/mroute.h>
105 #endif
106 #ifdef CONFIG_PROC_FS
107 #include <linux/proc_fs.h>
108 #include <linux/seq_file.h>
109 #endif
110
111 #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 (atomic_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                 atomic_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                 atomic_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                 atomic_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 = (struct igmpv3_grec *)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 = (__be32 *)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 = (struct igmphdr *)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 in_device *in_dev = __in_dev_get_rcu(skb->dev);
1060         int len = skb->len;
1061         bool dropped = true;
1062
1063         if (!in_dev)
1064                 goto drop;
1065
1066         if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
1067                 goto drop;
1068
1069         if (skb_checksum_simple_validate(skb))
1070                 goto drop;
1071
1072         ih = igmp_hdr(skb);
1073         switch (ih->type) {
1074         case IGMP_HOST_MEMBERSHIP_QUERY:
1075                 dropped = igmp_heard_query(in_dev, skb, len);
1076                 break;
1077         case IGMP_HOST_MEMBERSHIP_REPORT:
1078         case IGMPV2_HOST_MEMBERSHIP_REPORT:
1079                 /* Is it our report looped back? */
1080                 if (rt_is_output_route(skb_rtable(skb)))
1081                         break;
1082                 /* don't rely on MC router hearing unicast reports */
1083                 if (skb->pkt_type == PACKET_MULTICAST ||
1084                     skb->pkt_type == PACKET_BROADCAST)
1085                         dropped = igmp_heard_report(in_dev, ih->group);
1086                 break;
1087         case IGMP_PIM:
1088 #ifdef CONFIG_IP_PIMSM_V1
1089                 return pim_rcv_v1(skb);
1090 #endif
1091         case IGMPV3_HOST_MEMBERSHIP_REPORT:
1092         case IGMP_DVMRP:
1093         case IGMP_TRACE:
1094         case IGMP_HOST_LEAVE_MESSAGE:
1095         case IGMP_MTRACE:
1096         case IGMP_MTRACE_RESP:
1097                 break;
1098         default:
1099                 break;
1100         }
1101
1102 drop:
1103         if (dropped)
1104                 kfree_skb(skb);
1105         else
1106                 consume_skb(skb);
1107         return 0;
1108 }
1109
1110 #endif
1111
1112
1113 /*
1114  *      Add a filter to a device
1115  */
1116
1117 static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
1118 {
1119         char buf[MAX_ADDR_LEN];
1120         struct net_device *dev = in_dev->dev;
1121
1122         /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1123            We will get multicast token leakage, when IFF_MULTICAST
1124            is changed. This check should be done in ndo_set_rx_mode
1125            routine. Something sort of:
1126            if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1127            --ANK
1128            */
1129         if (arp_mc_map(addr, buf, dev, 0) == 0)
1130                 dev_mc_add(dev, buf);
1131 }
1132
1133 /*
1134  *      Remove a filter from a device
1135  */
1136
1137 static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
1138 {
1139         char buf[MAX_ADDR_LEN];
1140         struct net_device *dev = in_dev->dev;
1141
1142         if (arp_mc_map(addr, buf, dev, 0) == 0)
1143                 dev_mc_del(dev, buf);
1144 }
1145
1146 #ifdef CONFIG_IP_MULTICAST
1147 /*
1148  * deleted ip_mc_list manipulation
1149  */
1150 static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1151 {
1152         struct ip_mc_list *pmc;
1153         struct net *net = dev_net(in_dev->dev);
1154
1155         /* this is an "ip_mc_list" for convenience; only the fields below
1156          * are actually used. In particular, the refcnt and users are not
1157          * used for management of the delete list. Using the same structure
1158          * for deleted items allows change reports to use common code with
1159          * non-deleted or query-response MCA's.
1160          */
1161         pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
1162         if (!pmc)
1163                 return;
1164         spin_lock_init(&pmc->lock);
1165         spin_lock_bh(&im->lock);
1166         pmc->interface = im->interface;
1167         in_dev_hold(in_dev);
1168         pmc->multiaddr = im->multiaddr;
1169         pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1170         pmc->sfmode = im->sfmode;
1171         if (pmc->sfmode == MCAST_INCLUDE) {
1172                 struct ip_sf_list *psf;
1173
1174                 pmc->tomb = im->tomb;
1175                 pmc->sources = im->sources;
1176                 im->tomb = im->sources = NULL;
1177                 for (psf = pmc->sources; psf; psf = psf->sf_next)
1178                         psf->sf_crcount = pmc->crcount;
1179         }
1180         spin_unlock_bh(&im->lock);
1181
1182         spin_lock_bh(&in_dev->mc_tomb_lock);
1183         pmc->next = in_dev->mc_tomb;
1184         in_dev->mc_tomb = pmc;
1185         spin_unlock_bh(&in_dev->mc_tomb_lock);
1186 }
1187
1188 /*
1189  * restore ip_mc_list deleted records
1190  */
1191 static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1192 {
1193         struct ip_mc_list *pmc, *pmc_prev;
1194         struct ip_sf_list *psf;
1195         struct net *net = dev_net(in_dev->dev);
1196         __be32 multiaddr = im->multiaddr;
1197
1198         spin_lock_bh(&in_dev->mc_tomb_lock);
1199         pmc_prev = NULL;
1200         for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
1201                 if (pmc->multiaddr == multiaddr)
1202                         break;
1203                 pmc_prev = pmc;
1204         }
1205         if (pmc) {
1206                 if (pmc_prev)
1207                         pmc_prev->next = pmc->next;
1208                 else
1209                         in_dev->mc_tomb = pmc->next;
1210         }
1211         spin_unlock_bh(&in_dev->mc_tomb_lock);
1212
1213         spin_lock_bh(&im->lock);
1214         if (pmc) {
1215                 im->interface = pmc->interface;
1216                 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1217                 if (im->sfmode == MCAST_INCLUDE) {
1218                         swap(im->tomb, pmc->tomb);
1219                         swap(im->sources, pmc->sources);
1220                         for (psf = im->sources; psf; psf = psf->sf_next)
1221                                 psf->sf_crcount = im->crcount;
1222                 }
1223                 in_dev_put(pmc->interface);
1224                 kfree_pmc(pmc);
1225         }
1226         spin_unlock_bh(&im->lock);
1227 }
1228
1229 /*
1230  * flush ip_mc_list deleted records
1231  */
1232 static void igmpv3_clear_delrec(struct in_device *in_dev)
1233 {
1234         struct ip_mc_list *pmc, *nextpmc;
1235
1236         spin_lock_bh(&in_dev->mc_tomb_lock);
1237         pmc = in_dev->mc_tomb;
1238         in_dev->mc_tomb = NULL;
1239         spin_unlock_bh(&in_dev->mc_tomb_lock);
1240
1241         for (; pmc; pmc = nextpmc) {
1242                 nextpmc = pmc->next;
1243                 ip_mc_clear_src(pmc);
1244                 in_dev_put(pmc->interface);
1245                 kfree_pmc(pmc);
1246         }
1247         /* clear dead sources, too */
1248         rcu_read_lock();
1249         for_each_pmc_rcu(in_dev, pmc) {
1250                 struct ip_sf_list *psf;
1251
1252                 spin_lock_bh(&pmc->lock);
1253                 psf = pmc->tomb;
1254                 pmc->tomb = NULL;
1255                 spin_unlock_bh(&pmc->lock);
1256                 ip_sf_list_clear_all(psf);
1257         }
1258         rcu_read_unlock();
1259 }
1260 #endif
1261
1262 static void igmp_group_dropped(struct ip_mc_list *im)
1263 {
1264         struct in_device *in_dev = im->interface;
1265 #ifdef CONFIG_IP_MULTICAST
1266         struct net *net = dev_net(in_dev->dev);
1267         int reporter;
1268 #endif
1269
1270         if (im->loaded) {
1271                 im->loaded = 0;
1272                 ip_mc_filter_del(in_dev, im->multiaddr);
1273         }
1274
1275 #ifdef CONFIG_IP_MULTICAST
1276         if (im->multiaddr == IGMP_ALL_HOSTS)
1277                 return;
1278         if (ipv4_is_local_multicast(im->multiaddr) &&
1279             !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1280                 return;
1281
1282         reporter = im->reporter;
1283         igmp_stop_timer(im);
1284
1285         if (!in_dev->dead) {
1286                 if (IGMP_V1_SEEN(in_dev))
1287                         return;
1288                 if (IGMP_V2_SEEN(in_dev)) {
1289                         if (reporter)
1290                                 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1291                         return;
1292                 }
1293                 /* IGMPv3 */
1294                 igmpv3_add_delrec(in_dev, im);
1295
1296                 igmp_ifc_event(in_dev);
1297         }
1298 #endif
1299 }
1300
1301 static void igmp_group_added(struct ip_mc_list *im)
1302 {
1303         struct in_device *in_dev = im->interface;
1304 #ifdef CONFIG_IP_MULTICAST
1305         struct net *net = dev_net(in_dev->dev);
1306 #endif
1307
1308         if (im->loaded == 0) {
1309                 im->loaded = 1;
1310                 ip_mc_filter_add(in_dev, im->multiaddr);
1311         }
1312
1313 #ifdef CONFIG_IP_MULTICAST
1314         if (im->multiaddr == IGMP_ALL_HOSTS)
1315                 return;
1316         if (ipv4_is_local_multicast(im->multiaddr) &&
1317             !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1318                 return;
1319
1320         if (in_dev->dead)
1321                 return;
1322         if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1323                 spin_lock_bh(&im->lock);
1324                 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
1325                 spin_unlock_bh(&im->lock);
1326                 return;
1327         }
1328         /* else, v3 */
1329
1330         im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1331         igmp_ifc_event(in_dev);
1332 #endif
1333 }
1334
1335
1336 /*
1337  *      Multicast list managers
1338  */
1339
1340 static u32 ip_mc_hash(const struct ip_mc_list *im)
1341 {
1342         return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
1343 }
1344
1345 static void ip_mc_hash_add(struct in_device *in_dev,
1346                            struct ip_mc_list *im)
1347 {
1348         struct ip_mc_list __rcu **mc_hash;
1349         u32 hash;
1350
1351         mc_hash = rtnl_dereference(in_dev->mc_hash);
1352         if (mc_hash) {
1353                 hash = ip_mc_hash(im);
1354                 im->next_hash = mc_hash[hash];
1355                 rcu_assign_pointer(mc_hash[hash], im);
1356                 return;
1357         }
1358
1359         /* do not use a hash table for small number of items */
1360         if (in_dev->mc_count < 4)
1361                 return;
1362
1363         mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1364                           GFP_KERNEL);
1365         if (!mc_hash)
1366                 return;
1367
1368         for_each_pmc_rtnl(in_dev, im) {
1369                 hash = ip_mc_hash(im);
1370                 im->next_hash = mc_hash[hash];
1371                 RCU_INIT_POINTER(mc_hash[hash], im);
1372         }
1373
1374         rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1375 }
1376
1377 static void ip_mc_hash_remove(struct in_device *in_dev,
1378                               struct ip_mc_list *im)
1379 {
1380         struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1381         struct ip_mc_list *aux;
1382
1383         if (!mc_hash)
1384                 return;
1385         mc_hash += ip_mc_hash(im);
1386         while ((aux = rtnl_dereference(*mc_hash)) != im)
1387                 mc_hash = &aux->next_hash;
1388         *mc_hash = im->next_hash;
1389 }
1390
1391
1392 /*
1393  *      A socket has joined a multicast group on device dev.
1394  */
1395
1396 void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1397 {
1398         struct ip_mc_list *im;
1399 #ifdef CONFIG_IP_MULTICAST
1400         struct net *net = dev_net(in_dev->dev);
1401 #endif
1402
1403         ASSERT_RTNL();
1404
1405         for_each_pmc_rtnl(in_dev, im) {
1406                 if (im->multiaddr == addr) {
1407                         im->users++;
1408                         ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1409                         goto out;
1410                 }
1411         }
1412
1413         im = kzalloc(sizeof(*im), GFP_KERNEL);
1414         if (!im)
1415                 goto out;
1416
1417         im->users = 1;
1418         im->interface = in_dev;
1419         in_dev_hold(in_dev);
1420         im->multiaddr = addr;
1421         /* initial mode is (EX, empty) */
1422         im->sfmode = MCAST_EXCLUDE;
1423         im->sfcount[MCAST_EXCLUDE] = 1;
1424         atomic_set(&im->refcnt, 1);
1425         spin_lock_init(&im->lock);
1426 #ifdef CONFIG_IP_MULTICAST
1427         setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
1428         im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
1429 #endif
1430
1431         im->next_rcu = in_dev->mc_list;
1432         in_dev->mc_count++;
1433         rcu_assign_pointer(in_dev->mc_list, im);
1434
1435         ip_mc_hash_add(in_dev, im);
1436
1437 #ifdef CONFIG_IP_MULTICAST
1438         igmpv3_del_delrec(in_dev, im);
1439 #endif
1440         igmp_group_added(im);
1441         if (!in_dev->dead)
1442                 ip_rt_multicast_event(in_dev);
1443 out:
1444         return;
1445 }
1446 EXPORT_SYMBOL(ip_mc_inc_group);
1447
1448 static int ip_mc_check_iphdr(struct sk_buff *skb)
1449 {
1450         const struct iphdr *iph;
1451         unsigned int len;
1452         unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1453
1454         if (!pskb_may_pull(skb, offset))
1455                 return -EINVAL;
1456
1457         iph = ip_hdr(skb);
1458
1459         if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1460                 return -EINVAL;
1461
1462         offset += ip_hdrlen(skb) - sizeof(*iph);
1463
1464         if (!pskb_may_pull(skb, offset))
1465                 return -EINVAL;
1466
1467         iph = ip_hdr(skb);
1468
1469         if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1470                 return -EINVAL;
1471
1472         len = skb_network_offset(skb) + ntohs(iph->tot_len);
1473         if (skb->len < len || len < offset)
1474                 return -EINVAL;
1475
1476         skb_set_transport_header(skb, offset);
1477
1478         return 0;
1479 }
1480
1481 static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1482 {
1483         unsigned int len = skb_transport_offset(skb);
1484
1485         len += sizeof(struct igmpv3_report);
1486
1487         return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1488 }
1489
1490 static int ip_mc_check_igmp_query(struct sk_buff *skb)
1491 {
1492         unsigned int len = skb_transport_offset(skb);
1493
1494         len += sizeof(struct igmphdr);
1495         if (skb->len < len)
1496                 return -EINVAL;
1497
1498         /* IGMPv{1,2}? */
1499         if (skb->len != len) {
1500                 /* or IGMPv3? */
1501                 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1502                 if (skb->len < len || !pskb_may_pull(skb, len))
1503                         return -EINVAL;
1504         }
1505
1506         /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1507          * all-systems destination addresses (224.0.0.1) for general queries
1508          */
1509         if (!igmp_hdr(skb)->group &&
1510             ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1511                 return -EINVAL;
1512
1513         return 0;
1514 }
1515
1516 static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1517 {
1518         switch (igmp_hdr(skb)->type) {
1519         case IGMP_HOST_LEAVE_MESSAGE:
1520         case IGMP_HOST_MEMBERSHIP_REPORT:
1521         case IGMPV2_HOST_MEMBERSHIP_REPORT:
1522                 /* fall through */
1523                 return 0;
1524         case IGMPV3_HOST_MEMBERSHIP_REPORT:
1525                 return ip_mc_check_igmp_reportv3(skb);
1526         case IGMP_HOST_MEMBERSHIP_QUERY:
1527                 return ip_mc_check_igmp_query(skb);
1528         default:
1529                 return -ENOMSG;
1530         }
1531 }
1532
1533 static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1534 {
1535         return skb_checksum_simple_validate(skb);
1536 }
1537
1538 static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1539
1540 {
1541         struct sk_buff *skb_chk;
1542         unsigned int transport_len;
1543         unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
1544         int ret = -EINVAL;
1545
1546         transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1547
1548         skb_chk = skb_checksum_trimmed(skb, transport_len,
1549                                        ip_mc_validate_checksum);
1550         if (!skb_chk)
1551                 goto err;
1552
1553         if (!pskb_may_pull(skb_chk, len))
1554                 goto err;
1555
1556         ret = ip_mc_check_igmp_msg(skb_chk);
1557         if (ret)
1558                 goto err;
1559
1560         if (skb_trimmed)
1561                 *skb_trimmed = skb_chk;
1562         /* free now unneeded clone */
1563         else if (skb_chk != skb)
1564                 kfree_skb(skb_chk);
1565
1566         ret = 0;
1567
1568 err:
1569         if (ret && skb_chk && skb_chk != skb)
1570                 kfree_skb(skb_chk);
1571
1572         return ret;
1573 }
1574
1575 /**
1576  * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1577  * @skb: the skb to validate
1578  * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1579  *
1580  * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
1581  * skb transport header accordingly and returns zero.
1582  *
1583  * -EINVAL: A broken packet was detected, i.e. it violates some internet
1584  *  standard
1585  * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1586  * -ENOMEM: A memory allocation failure happened.
1587  *
1588  * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1589  * to NULL): After parsing an IGMP packet successfully it will point to
1590  * an skb which has its tail aligned to the IP packet end. This might
1591  * either be the originally provided skb or a trimmed, cloned version if
1592  * the skb frame had data beyond the IP packet. A cloned skb allows us
1593  * to leave the original skb and its full frame unchanged (which might be
1594  * desirable for layer 2 frame jugglers).
1595  *
1596  * Caller needs to set the skb network header and free any returned skb if it
1597  * differs from the provided skb.
1598  */
1599 int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1600 {
1601         int ret = ip_mc_check_iphdr(skb);
1602
1603         if (ret < 0)
1604                 return ret;
1605
1606         if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1607                 return -ENOMSG;
1608
1609         return __ip_mc_check_igmp(skb, skb_trimmed);
1610 }
1611 EXPORT_SYMBOL(ip_mc_check_igmp);
1612
1613 /*
1614  *      Resend IGMP JOIN report; used by netdev notifier.
1615  */
1616 static void ip_mc_rejoin_groups(struct in_device *in_dev)
1617 {
1618 #ifdef CONFIG_IP_MULTICAST
1619         struct ip_mc_list *im;
1620         int type;
1621         struct net *net = dev_net(in_dev->dev);
1622
1623         ASSERT_RTNL();
1624
1625         for_each_pmc_rtnl(in_dev, im) {
1626                 if (im->multiaddr == IGMP_ALL_HOSTS)
1627                         continue;
1628                 if (ipv4_is_local_multicast(im->multiaddr) &&
1629                     !READ_ONCE(net->ipv4.sysctl_igmp_llm_reports))
1630                         continue;
1631
1632                 /* a failover is happening and switches
1633                  * must be notified immediately
1634                  */
1635                 if (IGMP_V1_SEEN(in_dev))
1636                         type = IGMP_HOST_MEMBERSHIP_REPORT;
1637                 else if (IGMP_V2_SEEN(in_dev))
1638                         type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1639                 else
1640                         type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1641                 igmp_send_report(in_dev, im, type);
1642         }
1643 #endif
1644 }
1645
1646 /*
1647  *      A socket has left a multicast group on device dev
1648  */
1649
1650 void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
1651 {
1652         struct ip_mc_list *i;
1653         struct ip_mc_list __rcu **ip;
1654
1655         ASSERT_RTNL();
1656
1657         for (ip = &in_dev->mc_list;
1658              (i = rtnl_dereference(*ip)) != NULL;
1659              ip = &i->next_rcu) {
1660                 if (i->multiaddr == addr) {
1661                         if (--i->users == 0) {
1662                                 ip_mc_hash_remove(in_dev, i);
1663                                 *ip = i->next_rcu;
1664                                 in_dev->mc_count--;
1665                                 igmp_group_dropped(i);
1666                                 ip_mc_clear_src(i);
1667
1668                                 if (!in_dev->dead)
1669                                         ip_rt_multicast_event(in_dev);
1670
1671                                 ip_ma_put(i);
1672                                 return;
1673                         }
1674                         break;
1675                 }
1676         }
1677 }
1678 EXPORT_SYMBOL(ip_mc_dec_group);
1679
1680 /* Device changing type */
1681
1682 void ip_mc_unmap(struct in_device *in_dev)
1683 {
1684         struct ip_mc_list *pmc;
1685
1686         ASSERT_RTNL();
1687
1688         for_each_pmc_rtnl(in_dev, pmc)
1689                 igmp_group_dropped(pmc);
1690 }
1691
1692 void ip_mc_remap(struct in_device *in_dev)
1693 {
1694         struct ip_mc_list *pmc;
1695
1696         ASSERT_RTNL();
1697
1698         for_each_pmc_rtnl(in_dev, pmc) {
1699 #ifdef CONFIG_IP_MULTICAST
1700                 igmpv3_del_delrec(in_dev, pmc);
1701 #endif
1702                 igmp_group_added(pmc);
1703         }
1704 }
1705
1706 /* Device going down */
1707
1708 void ip_mc_down(struct in_device *in_dev)
1709 {
1710         struct ip_mc_list *pmc;
1711
1712         ASSERT_RTNL();
1713
1714         for_each_pmc_rtnl(in_dev, pmc)
1715                 igmp_group_dropped(pmc);
1716
1717 #ifdef CONFIG_IP_MULTICAST
1718         in_dev->mr_ifc_count = 0;
1719         if (del_timer(&in_dev->mr_ifc_timer))
1720                 __in_dev_put(in_dev);
1721         in_dev->mr_gq_running = 0;
1722         if (del_timer(&in_dev->mr_gq_timer))
1723                 __in_dev_put(in_dev);
1724 #endif
1725
1726         ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1727 }
1728
1729 void ip_mc_init_dev(struct in_device *in_dev)
1730 {
1731 #ifdef CONFIG_IP_MULTICAST
1732         struct net *net = dev_net(in_dev->dev);
1733 #endif
1734         ASSERT_RTNL();
1735
1736 #ifdef CONFIG_IP_MULTICAST
1737         setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1738                         (unsigned long)in_dev);
1739         setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1740                         (unsigned long)in_dev);
1741         in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1742 #endif
1743
1744         spin_lock_init(&in_dev->mc_tomb_lock);
1745 }
1746
1747 /* Device going up */
1748
1749 void ip_mc_up(struct in_device *in_dev)
1750 {
1751         struct ip_mc_list *pmc;
1752 #ifdef CONFIG_IP_MULTICAST
1753         struct net *net = dev_net(in_dev->dev);
1754 #endif
1755
1756         ASSERT_RTNL();
1757
1758 #ifdef CONFIG_IP_MULTICAST
1759         in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1760 #endif
1761         ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1762
1763         for_each_pmc_rtnl(in_dev, pmc) {
1764 #ifdef CONFIG_IP_MULTICAST
1765                 igmpv3_del_delrec(in_dev, pmc);
1766 #endif
1767                 igmp_group_added(pmc);
1768         }
1769 }
1770
1771 /*
1772  *      Device is about to be destroyed: clean up.
1773  */
1774
1775 void ip_mc_destroy_dev(struct in_device *in_dev)
1776 {
1777         struct ip_mc_list *i;
1778
1779         ASSERT_RTNL();
1780
1781         /* Deactivate timers */
1782         ip_mc_down(in_dev);
1783 #ifdef CONFIG_IP_MULTICAST
1784         igmpv3_clear_delrec(in_dev);
1785 #endif
1786
1787         while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1788                 in_dev->mc_list = i->next_rcu;
1789                 in_dev->mc_count--;
1790                 ip_mc_clear_src(i);
1791                 ip_ma_put(i);
1792         }
1793 }
1794
1795 /* RTNL is locked */
1796 static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
1797 {
1798         struct net_device *dev = NULL;
1799         struct in_device *idev = NULL;
1800
1801         if (imr->imr_ifindex) {
1802                 idev = inetdev_by_index(net, imr->imr_ifindex);
1803                 return idev;
1804         }
1805         if (imr->imr_address.s_addr) {
1806                 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
1807                 if (!dev)
1808                         return NULL;
1809         }
1810
1811         if (!dev) {
1812                 struct rtable *rt = ip_route_output(net,
1813                                                     imr->imr_multiaddr.s_addr,
1814                                                     0, 0, 0);
1815                 if (!IS_ERR(rt)) {
1816                         dev = rt->dst.dev;
1817                         ip_rt_put(rt);
1818                 }
1819         }
1820         if (dev) {
1821                 imr->imr_ifindex = dev->ifindex;
1822                 idev = __in_dev_get_rtnl(dev);
1823         }
1824         return idev;
1825 }
1826
1827 /*
1828  *      Join a socket to a group
1829  */
1830
1831 static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1832         __be32 *psfsrc)
1833 {
1834         struct ip_sf_list *psf, *psf_prev;
1835         int rv = 0;
1836
1837         psf_prev = NULL;
1838         for (psf = pmc->sources; psf; psf = psf->sf_next) {
1839                 if (psf->sf_inaddr == *psfsrc)
1840                         break;
1841                 psf_prev = psf;
1842         }
1843         if (!psf || psf->sf_count[sfmode] == 0) {
1844                 /* source filter not found, or count wrong =>  bug */
1845                 return -ESRCH;
1846         }
1847         psf->sf_count[sfmode]--;
1848         if (psf->sf_count[sfmode] == 0) {
1849                 ip_rt_multicast_event(pmc->interface);
1850         }
1851         if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1852 #ifdef CONFIG_IP_MULTICAST
1853                 struct in_device *in_dev = pmc->interface;
1854                 struct net *net = dev_net(in_dev->dev);
1855 #endif
1856
1857                 /* no more filters for this source */
1858                 if (psf_prev)
1859                         psf_prev->sf_next = psf->sf_next;
1860                 else
1861                         pmc->sources = psf->sf_next;
1862 #ifdef CONFIG_IP_MULTICAST
1863                 if (psf->sf_oldin &&
1864                     !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1865                         psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1866                         psf->sf_next = pmc->tomb;
1867                         pmc->tomb = psf;
1868                         rv = 1;
1869                 } else
1870 #endif
1871                         kfree(psf);
1872         }
1873         return rv;
1874 }
1875
1876 #ifndef CONFIG_IP_MULTICAST
1877 #define igmp_ifc_event(x)       do { } while (0)
1878 #endif
1879
1880 static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1881                          int sfcount, __be32 *psfsrc, int delta)
1882 {
1883         struct ip_mc_list *pmc;
1884         int     changerec = 0;
1885         int     i, err;
1886
1887         if (!in_dev)
1888                 return -ENODEV;
1889         rcu_read_lock();
1890         for_each_pmc_rcu(in_dev, pmc) {
1891                 if (*pmca == pmc->multiaddr)
1892                         break;
1893         }
1894         if (!pmc) {
1895                 /* MCA not found?? bug */
1896                 rcu_read_unlock();
1897                 return -ESRCH;
1898         }
1899         spin_lock_bh(&pmc->lock);
1900         rcu_read_unlock();
1901 #ifdef CONFIG_IP_MULTICAST
1902         sf_markstate(pmc);
1903 #endif
1904         if (!delta) {
1905                 err = -EINVAL;
1906                 if (!pmc->sfcount[sfmode])
1907                         goto out_unlock;
1908                 pmc->sfcount[sfmode]--;
1909         }
1910         err = 0;
1911         for (i = 0; i < sfcount; i++) {
1912                 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1913
1914                 changerec |= rv > 0;
1915                 if (!err && rv < 0)
1916                         err = rv;
1917         }
1918         if (pmc->sfmode == MCAST_EXCLUDE &&
1919             pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1920             pmc->sfcount[MCAST_INCLUDE]) {
1921 #ifdef CONFIG_IP_MULTICAST
1922                 struct ip_sf_list *psf;
1923                 struct net *net = dev_net(in_dev->dev);
1924 #endif
1925
1926                 /* filter mode change */
1927                 pmc->sfmode = MCAST_INCLUDE;
1928 #ifdef CONFIG_IP_MULTICAST
1929                 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1930                 in_dev->mr_ifc_count = pmc->crcount;
1931                 for (psf = pmc->sources; psf; psf = psf->sf_next)
1932                         psf->sf_crcount = 0;
1933                 igmp_ifc_event(pmc->interface);
1934         } else if (sf_setstate(pmc) || changerec) {
1935                 igmp_ifc_event(pmc->interface);
1936 #endif
1937         }
1938 out_unlock:
1939         spin_unlock_bh(&pmc->lock);
1940         return err;
1941 }
1942
1943 /*
1944  * Add multicast single-source filter to the interface list
1945  */
1946 static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1947         __be32 *psfsrc)
1948 {
1949         struct ip_sf_list *psf, *psf_prev;
1950
1951         psf_prev = NULL;
1952         for (psf = pmc->sources; psf; psf = psf->sf_next) {
1953                 if (psf->sf_inaddr == *psfsrc)
1954                         break;
1955                 psf_prev = psf;
1956         }
1957         if (!psf) {
1958                 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
1959                 if (!psf)
1960                         return -ENOBUFS;
1961                 psf->sf_inaddr = *psfsrc;
1962                 if (psf_prev) {
1963                         psf_prev->sf_next = psf;
1964                 } else
1965                         pmc->sources = psf;
1966         }
1967         psf->sf_count[sfmode]++;
1968         if (psf->sf_count[sfmode] == 1) {
1969                 ip_rt_multicast_event(pmc->interface);
1970         }
1971         return 0;
1972 }
1973
1974 #ifdef CONFIG_IP_MULTICAST
1975 static void sf_markstate(struct ip_mc_list *pmc)
1976 {
1977         struct ip_sf_list *psf;
1978         int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1979
1980         for (psf = pmc->sources; psf; psf = psf->sf_next)
1981                 if (pmc->sfcount[MCAST_EXCLUDE]) {
1982                         psf->sf_oldin = mca_xcount ==
1983                                 psf->sf_count[MCAST_EXCLUDE] &&
1984                                 !psf->sf_count[MCAST_INCLUDE];
1985                 } else
1986                         psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1987 }
1988
1989 static int sf_setstate(struct ip_mc_list *pmc)
1990 {
1991         struct ip_sf_list *psf, *dpsf;
1992         int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1993         int qrv = pmc->interface->mr_qrv;
1994         int new_in, rv;
1995
1996         rv = 0;
1997         for (psf = pmc->sources; psf; psf = psf->sf_next) {
1998                 if (pmc->sfcount[MCAST_EXCLUDE]) {
1999                         new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2000                                 !psf->sf_count[MCAST_INCLUDE];
2001                 } else
2002                         new_in = psf->sf_count[MCAST_INCLUDE] != 0;
2003                 if (new_in) {
2004                         if (!psf->sf_oldin) {
2005                                 struct ip_sf_list *prev = NULL;
2006
2007                                 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
2008                                         if (dpsf->sf_inaddr == psf->sf_inaddr)
2009                                                 break;
2010                                         prev = dpsf;
2011                                 }
2012                                 if (dpsf) {
2013                                         if (prev)
2014                                                 prev->sf_next = dpsf->sf_next;
2015                                         else
2016                                                 pmc->tomb = dpsf->sf_next;
2017                                         kfree(dpsf);
2018                                 }
2019                                 psf->sf_crcount = qrv;
2020                                 rv++;
2021                         }
2022                 } else if (psf->sf_oldin) {
2023
2024                         psf->sf_crcount = 0;
2025                         /*
2026                          * add or update "delete" records if an active filter
2027                          * is now inactive
2028                          */
2029                         for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
2030                                 if (dpsf->sf_inaddr == psf->sf_inaddr)
2031                                         break;
2032                         if (!dpsf) {
2033                                 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
2034                                 if (!dpsf)
2035                                         continue;
2036                                 *dpsf = *psf;
2037                                 /* pmc->lock held by callers */
2038                                 dpsf->sf_next = pmc->tomb;
2039                                 pmc->tomb = dpsf;
2040                         }
2041                         dpsf->sf_crcount = qrv;
2042                         rv++;
2043                 }
2044         }
2045         return rv;
2046 }
2047 #endif
2048
2049 /*
2050  * Add multicast source filter list to the interface list
2051  */
2052 static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2053                          int sfcount, __be32 *psfsrc, int delta)
2054 {
2055         struct ip_mc_list *pmc;
2056         int     isexclude;
2057         int     i, err;
2058
2059         if (!in_dev)
2060                 return -ENODEV;
2061         rcu_read_lock();
2062         for_each_pmc_rcu(in_dev, pmc) {
2063                 if (*pmca == pmc->multiaddr)
2064                         break;
2065         }
2066         if (!pmc) {
2067                 /* MCA not found?? bug */
2068                 rcu_read_unlock();
2069                 return -ESRCH;
2070         }
2071         spin_lock_bh(&pmc->lock);
2072         rcu_read_unlock();
2073
2074 #ifdef CONFIG_IP_MULTICAST
2075         sf_markstate(pmc);
2076 #endif
2077         isexclude = pmc->sfmode == MCAST_EXCLUDE;
2078         if (!delta)
2079                 pmc->sfcount[sfmode]++;
2080         err = 0;
2081         for (i = 0; i < sfcount; i++) {
2082                 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2083                 if (err)
2084                         break;
2085         }
2086         if (err) {
2087                 int j;
2088
2089                 if (!delta)
2090                         pmc->sfcount[sfmode]--;
2091                 for (j = 0; j < i; j++)
2092                         (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2093         } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2094 #ifdef CONFIG_IP_MULTICAST
2095                 struct ip_sf_list *psf;
2096                 struct net *net = dev_net(pmc->interface->dev);
2097                 in_dev = pmc->interface;
2098 #endif
2099
2100                 /* filter mode change */
2101                 if (pmc->sfcount[MCAST_EXCLUDE])
2102                         pmc->sfmode = MCAST_EXCLUDE;
2103                 else if (pmc->sfcount[MCAST_INCLUDE])
2104                         pmc->sfmode = MCAST_INCLUDE;
2105 #ifdef CONFIG_IP_MULTICAST
2106                 /* else no filters; keep old mode for reports */
2107
2108                 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
2109                 in_dev->mr_ifc_count = pmc->crcount;
2110                 for (psf = pmc->sources; psf; psf = psf->sf_next)
2111                         psf->sf_crcount = 0;
2112                 igmp_ifc_event(in_dev);
2113         } else if (sf_setstate(pmc)) {
2114                 igmp_ifc_event(in_dev);
2115 #endif
2116         }
2117         spin_unlock_bh(&pmc->lock);
2118         return err;
2119 }
2120
2121 static void ip_mc_clear_src(struct ip_mc_list *pmc)
2122 {
2123         struct ip_sf_list *tomb, *sources;
2124
2125         spin_lock_bh(&pmc->lock);
2126         tomb = pmc->tomb;
2127         pmc->tomb = NULL;
2128         sources = pmc->sources;
2129         pmc->sources = NULL;
2130         pmc->sfmode = MCAST_EXCLUDE;
2131         pmc->sfcount[MCAST_INCLUDE] = 0;
2132         pmc->sfcount[MCAST_EXCLUDE] = 1;
2133         spin_unlock_bh(&pmc->lock);
2134
2135         ip_sf_list_clear_all(tomb);
2136         ip_sf_list_clear_all(sources);
2137 }
2138
2139 /* Join a multicast group
2140  */
2141
2142 int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2143 {
2144         __be32 addr = imr->imr_multiaddr.s_addr;
2145         struct ip_mc_socklist *iml, *i;
2146         struct in_device *in_dev;
2147         struct inet_sock *inet = inet_sk(sk);
2148         struct net *net = sock_net(sk);
2149         int ifindex;
2150         int count = 0;
2151         int err;
2152
2153         ASSERT_RTNL();
2154
2155         if (!ipv4_is_multicast(addr))
2156                 return -EINVAL;
2157
2158         in_dev = ip_mc_find_dev(net, imr);
2159
2160         if (!in_dev) {
2161                 err = -ENODEV;
2162                 goto done;
2163         }
2164
2165         err = -EADDRINUSE;
2166         ifindex = imr->imr_ifindex;
2167         for_each_pmc_rtnl(inet, i) {
2168                 if (i->multi.imr_multiaddr.s_addr == addr &&
2169                     i->multi.imr_ifindex == ifindex)
2170                         goto done;
2171                 count++;
2172         }
2173         err = -ENOBUFS;
2174         if (count >= READ_ONCE(net->ipv4.sysctl_igmp_max_memberships))
2175                 goto done;
2176         iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
2177         if (!iml)
2178                 goto done;
2179
2180         memcpy(&iml->multi, imr, sizeof(*imr));
2181         iml->next_rcu = inet->mc_list;
2182         iml->sflist = NULL;
2183         iml->sfmode = MCAST_EXCLUDE;
2184         rcu_assign_pointer(inet->mc_list, iml);
2185         ip_mc_inc_group(in_dev, addr);
2186         err = 0;
2187 done:
2188         return err;
2189 }
2190 EXPORT_SYMBOL(ip_mc_join_group);
2191
2192 static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2193                            struct in_device *in_dev)
2194 {
2195         struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
2196         int err;
2197
2198         if (!psf) {
2199                 /* any-source empty exclude case */
2200                 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2201                         iml->sfmode, 0, NULL, 0);
2202         }
2203         err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2204                         iml->sfmode, psf->sl_count, psf->sl_addr, 0);
2205         RCU_INIT_POINTER(iml->sflist, NULL);
2206         /* decrease mem now to avoid the memleak warning */
2207         atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
2208         kfree_rcu(psf, rcu);
2209         return err;
2210 }
2211
2212 int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
2213 {
2214         struct inet_sock *inet = inet_sk(sk);
2215         struct ip_mc_socklist *iml;
2216         struct ip_mc_socklist __rcu **imlp;
2217         struct in_device *in_dev;
2218         struct net *net = sock_net(sk);
2219         __be32 group = imr->imr_multiaddr.s_addr;
2220         u32 ifindex;
2221         int ret = -EADDRNOTAVAIL;
2222
2223         ASSERT_RTNL();
2224
2225         in_dev = ip_mc_find_dev(net, imr);
2226         if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
2227                 ret = -ENODEV;
2228                 goto out;
2229         }
2230         ifindex = imr->imr_ifindex;
2231         for (imlp = &inet->mc_list;
2232              (iml = rtnl_dereference(*imlp)) != NULL;
2233              imlp = &iml->next_rcu) {
2234                 if (iml->multi.imr_multiaddr.s_addr != group)
2235                         continue;
2236                 if (ifindex) {
2237                         if (iml->multi.imr_ifindex != ifindex)
2238                                 continue;
2239                 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2240                                 iml->multi.imr_address.s_addr)
2241                         continue;
2242
2243                 (void) ip_mc_leave_src(sk, iml, in_dev);
2244
2245                 *imlp = iml->next_rcu;
2246
2247                 if (in_dev)
2248                         ip_mc_dec_group(in_dev, group);
2249
2250                 /* decrease mem now to avoid the memleak warning */
2251                 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2252                 kfree_rcu(iml, rcu);
2253                 return 0;
2254         }
2255 out:
2256         return ret;
2257 }
2258 EXPORT_SYMBOL(ip_mc_leave_group);
2259
2260 int ip_mc_source(int add, int omode, struct sock *sk, struct
2261         ip_mreq_source *mreqs, int ifindex)
2262 {
2263         int err;
2264         struct ip_mreqn imr;
2265         __be32 addr = mreqs->imr_multiaddr;
2266         struct ip_mc_socklist *pmc;
2267         struct in_device *in_dev = NULL;
2268         struct inet_sock *inet = inet_sk(sk);
2269         struct ip_sf_socklist *psl;
2270         struct net *net = sock_net(sk);
2271         int leavegroup = 0;
2272         int i, j, rv;
2273
2274         if (!ipv4_is_multicast(addr))
2275                 return -EINVAL;
2276
2277         ASSERT_RTNL();
2278
2279         imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2280         imr.imr_address.s_addr = mreqs->imr_interface;
2281         imr.imr_ifindex = ifindex;
2282         in_dev = ip_mc_find_dev(net, &imr);
2283
2284         if (!in_dev) {
2285                 err = -ENODEV;
2286                 goto done;
2287         }
2288         err = -EADDRNOTAVAIL;
2289
2290         for_each_pmc_rtnl(inet, pmc) {
2291                 if ((pmc->multi.imr_multiaddr.s_addr ==
2292                      imr.imr_multiaddr.s_addr) &&
2293                     (pmc->multi.imr_ifindex == imr.imr_ifindex))
2294                         break;
2295         }
2296         if (!pmc) {             /* must have a prior join */
2297                 err = -EINVAL;
2298                 goto done;
2299         }
2300         /* if a source filter was set, must be the same mode as before */
2301         if (pmc->sflist) {
2302                 if (pmc->sfmode != omode) {
2303                         err = -EINVAL;
2304                         goto done;
2305                 }
2306         } else if (pmc->sfmode != omode) {
2307                 /* allow mode switches for empty-set filters */
2308                 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
2309                 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
2310                         NULL, 0);
2311                 pmc->sfmode = omode;
2312         }
2313
2314         psl = rtnl_dereference(pmc->sflist);
2315         if (!add) {
2316                 if (!psl)
2317                         goto done;      /* err = -EADDRNOTAVAIL */
2318                 rv = !0;
2319                 for (i = 0; i < psl->sl_count; i++) {
2320                         rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2321                                 sizeof(__be32));
2322                         if (rv == 0)
2323                                 break;
2324                 }
2325                 if (rv)         /* source not found */
2326                         goto done;      /* err = -EADDRNOTAVAIL */
2327
2328                 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2329                 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2330                         leavegroup = 1;
2331                         goto done;
2332                 }
2333
2334                 /* update the interface filter */
2335                 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2336                         &mreqs->imr_sourceaddr, 1);
2337
2338                 for (j = i+1; j < psl->sl_count; j++)
2339                         psl->sl_addr[j-1] = psl->sl_addr[j];
2340                 psl->sl_count--;
2341                 err = 0;
2342                 goto done;
2343         }
2344         /* else, add a new source to the filter */
2345
2346         if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
2347                 err = -ENOBUFS;
2348                 goto done;
2349         }
2350         if (!psl || psl->sl_count == psl->sl_max) {
2351                 struct ip_sf_socklist *newpsl;
2352                 int count = IP_SFBLOCK;
2353
2354                 if (psl)
2355                         count += psl->sl_max;
2356                 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
2357                 if (!newpsl) {
2358                         err = -ENOBUFS;
2359                         goto done;
2360                 }
2361                 newpsl->sl_max = count;
2362                 newpsl->sl_count = count - IP_SFBLOCK;
2363                 if (psl) {
2364                         for (i = 0; i < psl->sl_count; i++)
2365                                 newpsl->sl_addr[i] = psl->sl_addr[i];
2366                         /* decrease mem now to avoid the memleak warning */
2367                         atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2368                 }
2369                 rcu_assign_pointer(pmc->sflist, newpsl);
2370                 if (psl)
2371                         kfree_rcu(psl, rcu);
2372                 psl = newpsl;
2373         }
2374         rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2375         for (i = 0; i < psl->sl_count; i++) {
2376                 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
2377                         sizeof(__be32));
2378                 if (rv == 0)
2379                         break;
2380         }
2381         if (rv == 0)            /* address already there is an error */
2382                 goto done;
2383         for (j = psl->sl_count-1; j >= i; j--)
2384                 psl->sl_addr[j+1] = psl->sl_addr[j];
2385         psl->sl_addr[i] = mreqs->imr_sourceaddr;
2386         psl->sl_count++;
2387         err = 0;
2388         /* update the interface list */
2389         ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2390                 &mreqs->imr_sourceaddr, 1);
2391 done:
2392         if (leavegroup)
2393                 err = ip_mc_leave_group(sk, &imr);
2394         return err;
2395 }
2396
2397 int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2398 {
2399         int err = 0;
2400         struct ip_mreqn imr;
2401         __be32 addr = msf->imsf_multiaddr;
2402         struct ip_mc_socklist *pmc;
2403         struct in_device *in_dev;
2404         struct inet_sock *inet = inet_sk(sk);
2405         struct ip_sf_socklist *newpsl, *psl;
2406         struct net *net = sock_net(sk);
2407         int leavegroup = 0;
2408
2409         if (!ipv4_is_multicast(addr))
2410                 return -EINVAL;
2411         if (msf->imsf_fmode != MCAST_INCLUDE &&
2412             msf->imsf_fmode != MCAST_EXCLUDE)
2413                 return -EINVAL;
2414
2415         ASSERT_RTNL();
2416
2417         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2418         imr.imr_address.s_addr = msf->imsf_interface;
2419         imr.imr_ifindex = ifindex;
2420         in_dev = ip_mc_find_dev(net, &imr);
2421
2422         if (!in_dev) {
2423                 err = -ENODEV;
2424                 goto done;
2425         }
2426
2427         /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2428         if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2429                 leavegroup = 1;
2430                 goto done;
2431         }
2432
2433         for_each_pmc_rtnl(inet, pmc) {
2434                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2435                     pmc->multi.imr_ifindex == imr.imr_ifindex)
2436                         break;
2437         }
2438         if (!pmc) {             /* must have a prior join */
2439                 err = -EINVAL;
2440                 goto done;
2441         }
2442         if (msf->imsf_numsrc) {
2443                 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2444                                                            GFP_KERNEL);
2445                 if (!newpsl) {
2446                         err = -ENOBUFS;
2447                         goto done;
2448                 }
2449                 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2450                 memcpy(newpsl->sl_addr, msf->imsf_slist,
2451                         msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2452                 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2453                         msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2454                 if (err) {
2455                         sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2456                         goto done;
2457                 }
2458         } else {
2459                 newpsl = NULL;
2460                 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2461                                      msf->imsf_fmode, 0, NULL, 0);
2462         }
2463         psl = rtnl_dereference(pmc->sflist);
2464         if (psl) {
2465                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2466                         psl->sl_count, psl->sl_addr, 0);
2467                 /* decrease mem now to avoid the memleak warning */
2468                 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2469         } else {
2470                 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2471                         0, NULL, 0);
2472         }
2473         rcu_assign_pointer(pmc->sflist, newpsl);
2474         if (psl)
2475                 kfree_rcu(psl, rcu);
2476         pmc->sfmode = msf->imsf_fmode;
2477         err = 0;
2478 done:
2479         if (leavegroup)
2480                 err = ip_mc_leave_group(sk, &imr);
2481         return err;
2482 }
2483
2484 int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2485         struct ip_msfilter __user *optval, int __user *optlen)
2486 {
2487         int err, len, count, copycount;
2488         struct ip_mreqn imr;
2489         __be32 addr = msf->imsf_multiaddr;
2490         struct ip_mc_socklist *pmc;
2491         struct in_device *in_dev;
2492         struct inet_sock *inet = inet_sk(sk);
2493         struct ip_sf_socklist *psl;
2494         struct net *net = sock_net(sk);
2495
2496         ASSERT_RTNL();
2497
2498         if (!ipv4_is_multicast(addr))
2499                 return -EINVAL;
2500
2501         imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2502         imr.imr_address.s_addr = msf->imsf_interface;
2503         imr.imr_ifindex = 0;
2504         in_dev = ip_mc_find_dev(net, &imr);
2505
2506         if (!in_dev) {
2507                 err = -ENODEV;
2508                 goto done;
2509         }
2510         err = -EADDRNOTAVAIL;
2511
2512         for_each_pmc_rtnl(inet, pmc) {
2513                 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2514                     pmc->multi.imr_ifindex == imr.imr_ifindex)
2515                         break;
2516         }
2517         if (!pmc)               /* must have a prior join */
2518                 goto done;
2519         msf->imsf_fmode = pmc->sfmode;
2520         psl = rtnl_dereference(pmc->sflist);
2521         if (!psl) {
2522                 len = 0;
2523                 count = 0;
2524         } else {
2525                 count = psl->sl_count;
2526         }
2527         copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2528         len = copycount * sizeof(psl->sl_addr[0]);
2529         msf->imsf_numsrc = count;
2530         if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2531             copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2532                 return -EFAULT;
2533         }
2534         if (len &&
2535             copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2536                 return -EFAULT;
2537         return 0;
2538 done:
2539         return err;
2540 }
2541
2542 int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2543         struct group_filter __user *optval, int __user *optlen)
2544 {
2545         int err, i, count, copycount;
2546         struct sockaddr_in *psin;
2547         __be32 addr;
2548         struct ip_mc_socklist *pmc;
2549         struct inet_sock *inet = inet_sk(sk);
2550         struct ip_sf_socklist *psl;
2551
2552         ASSERT_RTNL();
2553
2554         psin = (struct sockaddr_in *)&gsf->gf_group;
2555         if (psin->sin_family != AF_INET)
2556                 return -EINVAL;
2557         addr = psin->sin_addr.s_addr;
2558         if (!ipv4_is_multicast(addr))
2559                 return -EINVAL;
2560
2561         err = -EADDRNOTAVAIL;
2562
2563         for_each_pmc_rtnl(inet, pmc) {
2564                 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2565                     pmc->multi.imr_ifindex == gsf->gf_interface)
2566                         break;
2567         }
2568         if (!pmc)               /* must have a prior join */
2569                 goto done;
2570         gsf->gf_fmode = pmc->sfmode;
2571         psl = rtnl_dereference(pmc->sflist);
2572         count = psl ? psl->sl_count : 0;
2573         copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2574         gsf->gf_numsrc = count;
2575         if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2576             copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2577                 return -EFAULT;
2578         }
2579         for (i = 0; i < copycount; i++) {
2580                 struct sockaddr_storage ss;
2581
2582                 psin = (struct sockaddr_in *)&ss;
2583                 memset(&ss, 0, sizeof(ss));
2584                 psin->sin_family = AF_INET;
2585                 psin->sin_addr.s_addr = psl->sl_addr[i];
2586                 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2587                         return -EFAULT;
2588         }
2589         return 0;
2590 done:
2591         return err;
2592 }
2593
2594 /*
2595  * check if a multicast source filter allows delivery for a given <src,dst,intf>
2596  */
2597 int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
2598 {
2599         struct inet_sock *inet = inet_sk(sk);
2600         struct ip_mc_socklist *pmc;
2601         struct ip_sf_socklist *psl;
2602         int i;
2603         int ret;
2604
2605         ret = 1;
2606         if (!ipv4_is_multicast(loc_addr))
2607                 goto out;
2608
2609         rcu_read_lock();
2610         for_each_pmc_rcu(inet, pmc) {
2611                 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2612                     pmc->multi.imr_ifindex == dif)
2613                         break;
2614         }
2615         ret = inet->mc_all;
2616         if (!pmc)
2617                 goto unlock;
2618         psl = rcu_dereference(pmc->sflist);
2619         ret = (pmc->sfmode == MCAST_EXCLUDE);
2620         if (!psl)
2621                 goto unlock;
2622
2623         for (i = 0; i < psl->sl_count; i++) {
2624                 if (psl->sl_addr[i] == rmt_addr)
2625                         break;
2626         }
2627         ret = 0;
2628         if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2629                 goto unlock;
2630         if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2631                 goto unlock;
2632         ret = 1;
2633 unlock:
2634         rcu_read_unlock();
2635 out:
2636         return ret;
2637 }
2638
2639 /*
2640  *      A socket is closing.
2641  */
2642
2643 void ip_mc_drop_socket(struct sock *sk)
2644 {
2645         struct inet_sock *inet = inet_sk(sk);
2646         struct ip_mc_socklist *iml;
2647         struct net *net = sock_net(sk);
2648
2649         if (!inet->mc_list)
2650                 return;
2651
2652         rtnl_lock();
2653         while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
2654                 struct in_device *in_dev;
2655
2656                 inet->mc_list = iml->next_rcu;
2657                 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2658                 (void) ip_mc_leave_src(sk, iml, in_dev);
2659                 if (in_dev)
2660                         ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2661                 /* decrease mem now to avoid the memleak warning */
2662                 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2663                 kfree_rcu(iml, rcu);
2664         }
2665         rtnl_unlock();
2666 }
2667
2668 /* called with rcu_read_lock() */
2669 int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
2670 {
2671         struct ip_mc_list *im;
2672         struct ip_mc_list __rcu **mc_hash;
2673         struct ip_sf_list *psf;
2674         int rv = 0;
2675
2676         mc_hash = rcu_dereference(in_dev->mc_hash);
2677         if (mc_hash) {
2678                 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
2679
2680                 for (im = rcu_dereference(mc_hash[hash]);
2681                      im != NULL;
2682                      im = rcu_dereference(im->next_hash)) {
2683                         if (im->multiaddr == mc_addr)
2684                                 break;
2685                 }
2686         } else {
2687                 for_each_pmc_rcu(in_dev, im) {
2688                         if (im->multiaddr == mc_addr)
2689                                 break;
2690                 }
2691         }
2692         if (im && proto == IPPROTO_IGMP) {
2693                 rv = 1;
2694         } else if (im) {
2695                 if (src_addr) {
2696                         spin_lock_bh(&im->lock);
2697                         for (psf = im->sources; psf; psf = psf->sf_next) {
2698                                 if (psf->sf_inaddr == src_addr)
2699                                         break;
2700                         }
2701                         if (psf)
2702                                 rv = psf->sf_count[MCAST_INCLUDE] ||
2703                                         psf->sf_count[MCAST_EXCLUDE] !=
2704                                         im->sfcount[MCAST_EXCLUDE];
2705                         else
2706                                 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2707                         spin_unlock_bh(&im->lock);
2708                 } else
2709                         rv = 1; /* unspecified source; tentatively allow */
2710         }
2711         return rv;
2712 }
2713
2714 #if defined(CONFIG_PROC_FS)
2715 struct igmp_mc_iter_state {
2716         struct seq_net_private p;
2717         struct net_device *dev;
2718         struct in_device *in_dev;
2719 };
2720
2721 #define igmp_mc_seq_private(seq)        ((struct igmp_mc_iter_state *)(seq)->private)
2722
2723 static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2724 {
2725         struct net *net = seq_file_net(seq);
2726         struct ip_mc_list *im = NULL;
2727         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2728
2729         state->in_dev = NULL;
2730         for_each_netdev_rcu(net, state->dev) {
2731                 struct in_device *in_dev;
2732
2733                 in_dev = __in_dev_get_rcu(state->dev);
2734                 if (!in_dev)
2735                         continue;
2736                 im = rcu_dereference(in_dev->mc_list);
2737                 if (im) {
2738                         state->in_dev = in_dev;
2739                         break;
2740                 }
2741         }
2742         return im;
2743 }
2744
2745 static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2746 {
2747         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2748
2749         im = rcu_dereference(im->next_rcu);
2750         while (!im) {
2751                 state->dev = next_net_device_rcu(state->dev);
2752                 if (!state->dev) {
2753                         state->in_dev = NULL;
2754                         break;
2755                 }
2756                 state->in_dev = __in_dev_get_rcu(state->dev);
2757                 if (!state->in_dev)
2758                         continue;
2759                 im = rcu_dereference(state->in_dev->mc_list);
2760         }
2761         return im;
2762 }
2763
2764 static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2765 {
2766         struct ip_mc_list *im = igmp_mc_get_first(seq);
2767         if (im)
2768                 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2769                         --pos;
2770         return pos ? NULL : im;
2771 }
2772
2773 static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2774         __acquires(rcu)
2775 {
2776         rcu_read_lock();
2777         return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2778 }
2779
2780 static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2781 {
2782         struct ip_mc_list *im;
2783         if (v == SEQ_START_TOKEN)
2784                 im = igmp_mc_get_first(seq);
2785         else
2786                 im = igmp_mc_get_next(seq, v);
2787         ++*pos;
2788         return im;
2789 }
2790
2791 static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2792         __releases(rcu)
2793 {
2794         struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2795
2796         state->in_dev = NULL;
2797         state->dev = NULL;
2798         rcu_read_unlock();
2799 }
2800
2801 static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2802 {
2803         if (v == SEQ_START_TOKEN)
2804                 seq_puts(seq,
2805                          "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2806         else {
2807                 struct ip_mc_list *im = (struct ip_mc_list *)v;
2808                 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2809                 char   *querier;
2810                 long delta;
2811
2812 #ifdef CONFIG_IP_MULTICAST
2813                 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2814                           IGMP_V2_SEEN(state->in_dev) ? "V2" :
2815                           "V3";
2816 #else
2817                 querier = "NONE";
2818 #endif
2819
2820                 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
2821                         seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2822                                    state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
2823                 }
2824
2825                 delta = im->timer.expires - jiffies;
2826                 seq_printf(seq,
2827                            "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
2828                            im->multiaddr, im->users,
2829                            im->tm_running,
2830                            im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
2831                            im->reporter);
2832         }
2833         return 0;
2834 }
2835
2836 static const struct seq_operations igmp_mc_seq_ops = {
2837         .start  =       igmp_mc_seq_start,
2838         .next   =       igmp_mc_seq_next,
2839         .stop   =       igmp_mc_seq_stop,
2840         .show   =       igmp_mc_seq_show,
2841 };
2842
2843 static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2844 {
2845         return seq_open_net(inode, file, &igmp_mc_seq_ops,
2846                         sizeof(struct igmp_mc_iter_state));
2847 }
2848
2849 static const struct file_operations igmp_mc_seq_fops = {
2850         .owner          =       THIS_MODULE,
2851         .open           =       igmp_mc_seq_open,
2852         .read           =       seq_read,
2853         .llseek         =       seq_lseek,
2854         .release        =       seq_release_net,
2855 };
2856
2857 struct igmp_mcf_iter_state {
2858         struct seq_net_private p;
2859         struct net_device *dev;
2860         struct in_device *idev;
2861         struct ip_mc_list *im;
2862 };
2863
2864 #define igmp_mcf_seq_private(seq)       ((struct igmp_mcf_iter_state *)(seq)->private)
2865
2866 static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2867 {
2868         struct net *net = seq_file_net(seq);
2869         struct ip_sf_list *psf = NULL;
2870         struct ip_mc_list *im = NULL;
2871         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2872
2873         state->idev = NULL;
2874         state->im = NULL;
2875         for_each_netdev_rcu(net, state->dev) {
2876                 struct in_device *idev;
2877                 idev = __in_dev_get_rcu(state->dev);
2878                 if (unlikely(!idev))
2879                         continue;
2880                 im = rcu_dereference(idev->mc_list);
2881                 if (likely(im)) {
2882                         spin_lock_bh(&im->lock);
2883                         psf = im->sources;
2884                         if (likely(psf)) {
2885                                 state->im = im;
2886                                 state->idev = idev;
2887                                 break;
2888                         }
2889                         spin_unlock_bh(&im->lock);
2890                 }
2891         }
2892         return psf;
2893 }
2894
2895 static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2896 {
2897         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2898
2899         psf = psf->sf_next;
2900         while (!psf) {
2901                 spin_unlock_bh(&state->im->lock);
2902                 state->im = state->im->next;
2903                 while (!state->im) {
2904                         state->dev = next_net_device_rcu(state->dev);
2905                         if (!state->dev) {
2906                                 state->idev = NULL;
2907                                 goto out;
2908                         }
2909                         state->idev = __in_dev_get_rcu(state->dev);
2910                         if (!state->idev)
2911                                 continue;
2912                         state->im = rcu_dereference(state->idev->mc_list);
2913                 }
2914                 if (!state->im)
2915                         break;
2916                 spin_lock_bh(&state->im->lock);
2917                 psf = state->im->sources;
2918         }
2919 out:
2920         return psf;
2921 }
2922
2923 static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2924 {
2925         struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2926         if (psf)
2927                 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2928                         --pos;
2929         return pos ? NULL : psf;
2930 }
2931
2932 static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2933         __acquires(rcu)
2934 {
2935         rcu_read_lock();
2936         return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2937 }
2938
2939 static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2940 {
2941         struct ip_sf_list *psf;
2942         if (v == SEQ_START_TOKEN)
2943                 psf = igmp_mcf_get_first(seq);
2944         else
2945                 psf = igmp_mcf_get_next(seq, v);
2946         ++*pos;
2947         return psf;
2948 }
2949
2950 static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2951         __releases(rcu)
2952 {
2953         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2954         if (likely(state->im)) {
2955                 spin_unlock_bh(&state->im->lock);
2956                 state->im = NULL;
2957         }
2958         state->idev = NULL;
2959         state->dev = NULL;
2960         rcu_read_unlock();
2961 }
2962
2963 static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2964 {
2965         struct ip_sf_list *psf = (struct ip_sf_list *)v;
2966         struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2967
2968         if (v == SEQ_START_TOKEN) {
2969                 seq_puts(seq, "Idx Device        MCA        SRC    INC    EXC\n");
2970         } else {
2971                 seq_printf(seq,
2972                            "%3d %6.6s 0x%08x "
2973                            "0x%08x %6lu %6lu\n",
2974                            state->dev->ifindex, state->dev->name,
2975                            ntohl(state->im->multiaddr),
2976                            ntohl(psf->sf_inaddr),
2977                            psf->sf_count[MCAST_INCLUDE],
2978                            psf->sf_count[MCAST_EXCLUDE]);
2979         }
2980         return 0;
2981 }
2982
2983 static const struct seq_operations igmp_mcf_seq_ops = {
2984         .start  =       igmp_mcf_seq_start,
2985         .next   =       igmp_mcf_seq_next,
2986         .stop   =       igmp_mcf_seq_stop,
2987         .show   =       igmp_mcf_seq_show,
2988 };
2989
2990 static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2991 {
2992         return seq_open_net(inode, file, &igmp_mcf_seq_ops,
2993                         sizeof(struct igmp_mcf_iter_state));
2994 }
2995
2996 static const struct file_operations igmp_mcf_seq_fops = {
2997         .owner          =       THIS_MODULE,
2998         .open           =       igmp_mcf_seq_open,
2999         .read           =       seq_read,
3000         .llseek         =       seq_lseek,
3001         .release        =       seq_release_net,
3002 };
3003
3004 static int __net_init igmp_net_init(struct net *net)
3005 {
3006         struct proc_dir_entry *pde;
3007         int err;
3008
3009         pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
3010         if (!pde)
3011                 goto out_igmp;
3012         pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
3013                           &igmp_mcf_seq_fops);
3014         if (!pde)
3015                 goto out_mcfilter;
3016         err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3017                                    SOCK_DGRAM, 0, net);
3018         if (err < 0) {
3019                 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3020                        err);
3021                 goto out_sock;
3022         }
3023
3024         return 0;
3025
3026 out_sock:
3027         remove_proc_entry("mcfilter", net->proc_net);
3028 out_mcfilter:
3029         remove_proc_entry("igmp", net->proc_net);
3030 out_igmp:
3031         return -ENOMEM;
3032 }
3033
3034 static void __net_exit igmp_net_exit(struct net *net)
3035 {
3036         remove_proc_entry("mcfilter", net->proc_net);
3037         remove_proc_entry("igmp", net->proc_net);
3038         inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
3039 }
3040
3041 static struct pernet_operations igmp_net_ops = {
3042         .init = igmp_net_init,
3043         .exit = igmp_net_exit,
3044 };
3045 #endif
3046
3047 static int igmp_netdev_event(struct notifier_block *this,
3048                              unsigned long event, void *ptr)
3049 {
3050         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3051         struct in_device *in_dev;
3052
3053         switch (event) {
3054         case NETDEV_RESEND_IGMP:
3055                 in_dev = __in_dev_get_rtnl(dev);
3056                 if (in_dev)
3057                         ip_mc_rejoin_groups(in_dev);
3058                 break;
3059         default:
3060                 break;
3061         }
3062         return NOTIFY_DONE;
3063 }
3064
3065 static struct notifier_block igmp_notifier = {
3066         .notifier_call = igmp_netdev_event,
3067 };
3068
3069 int __init igmp_mc_init(void)
3070 {
3071 #if defined(CONFIG_PROC_FS)
3072         int err;
3073
3074         err = register_pernet_subsys(&igmp_net_ops);
3075         if (err)
3076                 return err;
3077         err = register_netdevice_notifier(&igmp_notifier);
3078         if (err)
3079                 goto reg_notif_fail;
3080         return 0;
3081
3082 reg_notif_fail:
3083         unregister_pernet_subsys(&igmp_net_ops);
3084         return err;
3085 #else
3086         return register_netdevice_notifier(&igmp_notifier);
3087 #endif
3088 }