OpenCores
URL https://opencores.org/ocsvn/test_project/test_project/trunk

Subversion Repositories test_project

[/] [test_project/] [trunk/] [linux_sd_driver/] [net/] [ipv4/] [igmp.c] - Blame information for rev 62

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.