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