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