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