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