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