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