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