blob: 2769dc4a4c84418ddafd0316a96574dfe50d0911 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
3 *
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
6 *
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
10 *
11 * Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
12 *
13 * Authors:
14 * Alan Cox <Alan.Cox@linux.org>
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version
19 * 2 of the License, or (at your option) any later version.
20 *
21 * Fixes:
22 *
23 * Alan Cox : Added lots of __inline__ to optimise
24 * the memory usage of all the tiny little
25 * functions.
26 * Alan Cox : Dumped the header building experiment.
27 * Alan Cox : Minor tweaks ready for multicast routing
28 * and extended IGMP protocol.
29 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
30 * writes utterly bogus code otherwise (sigh)
31 * fixed IGMP loopback to behave in the manner
32 * desired by mrouted, fixed the fact it has been
33 * broken since 1.3.6 and cleaned up a few minor
34 * points.
35 *
36 * Chih-Jen Chang : Tried to revise IGMP to Version 2
37 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090038 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * ipmulti-3.5 source code.
40 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
41 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
42 * the mrouted version on that device.
43 * Chih-Jen Chang : Added the max_resp_time parameter to
44 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
45 * to identify the multicast router version
46 * and do what the IGMP version 2 specified.
47 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
48 * Tsu-Sheng Tsao if the specified time expired.
49 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
50 * Alan Cox : Use GFP_ATOMIC in the right places.
51 * Christian Daudt : igmp timer wasn't set for local group
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090052 * memberships but was being deleted,
53 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * from %p with timer not initialized\n"
55 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090056 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 * igmp_timer_expire function (960205).
58 * Christian Daudt : igmp_heard_report now only calls
59 * igmp_timer_expire if tm->running is
60 * true (960216).
61 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
62 * igmp_heard_query never trigger. Expiry
63 * miscalculation fixed in igmp_heard_query
64 * and random() made to return unsigned to
65 * prevent negative expiry times.
66 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
67 * fix from pending 2.1.x patches.
68 * Alan Cox: Forget to enable FDDI support earlier.
69 * Alexey Kuznetsov: Fixed leaving groups on device down.
70 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
71 * David L Stevens: IGMPv3 support, with help from
72 * Vinay Kulkarni
73 */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <linux/module.h>
76#include <asm/uaccess.h>
77#include <asm/system.h>
78#include <linux/types.h>
79#include <linux/kernel.h>
80#include <linux/jiffies.h>
81#include <linux/string.h>
82#include <linux/socket.h>
83#include <linux/sockios.h>
84#include <linux/in.h>
85#include <linux/inet.h>
86#include <linux/netdevice.h>
87#include <linux/skbuff.h>
88#include <linux/inetdevice.h>
89#include <linux/igmp.h>
90#include <linux/if_arp.h>
91#include <linux/rtnetlink.h>
92#include <linux/times.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020093
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020094#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020095#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <net/ip.h>
97#include <net/protocol.h>
98#include <net/route.h>
99#include <net/sock.h>
100#include <net/checksum.h>
101#include <linux/netfilter_ipv4.h>
102#ifdef CONFIG_IP_MROUTE
103#include <linux/mroute.h>
104#endif
105#ifdef CONFIG_PROC_FS
106#include <linux/proc_fs.h>
107#include <linux/seq_file.h>
108#endif
109
110#define IP_MAX_MEMBERSHIPS 20
111#define IP_MAX_MSF 10
112
113#ifdef CONFIG_IP_MULTICAST
114/* Parameter names and values are taken from igmp-v2-06 draft */
115
116#define IGMP_V1_Router_Present_Timeout (400*HZ)
117#define IGMP_V2_Router_Present_Timeout (400*HZ)
118#define IGMP_Unsolicited_Report_Interval (10*HZ)
119#define IGMP_Query_Response_Interval (10*HZ)
120#define IGMP_Unsolicited_Report_Count 2
121
122
123#define IGMP_Initial_Report_Delay (1)
124
125/* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
130 */
131
Herbert Xu42f811b2007-06-04 23:34:44 -0700132#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900133 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700134 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135 ((in_dev)->mr_v1_seen && \
136 time_before(jiffies, (in_dev)->mr_v1_seen)))
137#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900138 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140 ((in_dev)->mr_v2_seen && \
141 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700144static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static void igmpv3_clear_delrec(struct in_device *in_dev);
146static int sf_setstate(struct ip_mc_list *pmc);
147static void sf_markstate(struct ip_mc_list *pmc);
148#endif
149static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700150static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
151 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153static void ip_ma_put(struct ip_mc_list *im)
154{
155 if (atomic_dec_and_test(&im->refcnt)) {
156 in_dev_put(im->interface);
157 kfree(im);
158 }
159}
160
161#ifdef CONFIG_IP_MULTICAST
162
163/*
164 * Timer management
165 */
166
167static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
168{
169 spin_lock_bh(&im->lock);
170 if (del_timer(&im->timer))
171 atomic_dec(&im->refcnt);
172 im->tm_running=0;
173 im->reporter = 0;
174 im->unsolicit_count = 0;
175 spin_unlock_bh(&im->lock);
176}
177
178/* It must be called with locked im->lock */
179static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
180{
181 int tv=net_random() % max_delay;
182
183 im->tm_running=1;
184 if (!mod_timer(&im->timer, jiffies+tv+2))
185 atomic_inc(&im->refcnt);
186}
187
188static void igmp_gq_start_timer(struct in_device *in_dev)
189{
190 int tv = net_random() % in_dev->mr_maxdelay;
191
192 in_dev->mr_gq_running = 1;
193 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
194 in_dev_hold(in_dev);
195}
196
197static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
198{
199 int tv = net_random() % delay;
200
201 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
202 in_dev_hold(in_dev);
203}
204
205static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
206{
207 spin_lock_bh(&im->lock);
208 im->unsolicit_count = 0;
209 if (del_timer(&im->timer)) {
210 if ((long)(im->timer.expires-jiffies) < max_delay) {
211 add_timer(&im->timer);
212 im->tm_running=1;
213 spin_unlock_bh(&im->lock);
214 return;
215 }
216 atomic_dec(&im->refcnt);
217 }
218 igmp_start_timer(im, max_delay);
219 spin_unlock_bh(&im->lock);
220}
221
222
223/*
224 * Send an IGMP report.
225 */
226
227#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
228
229
230static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
231 int gdeleted, int sdeleted)
232{
233 switch (type) {
234 case IGMPV3_MODE_IS_INCLUDE:
235 case IGMPV3_MODE_IS_EXCLUDE:
236 if (gdeleted || sdeleted)
237 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800238 if (!(pmc->gsquery && !psf->sf_gsresp)) {
239 if (pmc->sfmode == MCAST_INCLUDE)
240 return 1;
241 /* don't include if this source is excluded
242 * in all filters
243 */
244 if (psf->sf_count[MCAST_INCLUDE])
245 return type == IGMPV3_MODE_IS_INCLUDE;
246 return pmc->sfcount[MCAST_EXCLUDE] ==
247 psf->sf_count[MCAST_EXCLUDE];
248 }
249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 case IGMPV3_CHANGE_TO_INCLUDE:
251 if (gdeleted || sdeleted)
252 return 0;
253 return psf->sf_count[MCAST_INCLUDE] != 0;
254 case IGMPV3_CHANGE_TO_EXCLUDE:
255 if (gdeleted || sdeleted)
256 return 0;
257 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
258 psf->sf_count[MCAST_INCLUDE])
259 return 0;
260 return pmc->sfcount[MCAST_EXCLUDE] ==
261 psf->sf_count[MCAST_EXCLUDE];
262 case IGMPV3_ALLOW_NEW_SOURCES:
263 if (gdeleted || !psf->sf_crcount)
264 return 0;
265 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
266 case IGMPV3_BLOCK_OLD_SOURCES:
267 if (pmc->sfmode == MCAST_INCLUDE)
268 return gdeleted || (psf->sf_crcount && sdeleted);
269 return psf->sf_crcount && !gdeleted && !sdeleted;
270 }
271 return 0;
272}
273
274static int
275igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
276{
277 struct ip_sf_list *psf;
278 int scount = 0;
279
280 for (psf=pmc->sources; psf; psf=psf->sf_next) {
281 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
282 continue;
283 scount++;
284 }
285 return scount;
286}
287
288static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
289{
290 struct sk_buff *skb;
291 struct rtable *rt;
292 struct iphdr *pip;
293 struct igmpv3_report *pig;
294
Johannes Bergf5184d22008-05-12 20:48:31 -0700295 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 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 };
Denis V. Lunevf2063512008-01-22 22:07:34 -0800304 if (ip_route_output_key(&init_net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 kfree_skb(skb);
306 return NULL;
307 }
308 }
309 if (rt->rt_src == 0) {
310 kfree_skb(skb);
311 ip_rt_put(rt);
312 return NULL;
313 }
314
315 skb->dst = &rt->u.dst;
316 skb->dev = dev;
317
318 skb_reserve(skb, LL_RESERVED_SPACE(dev));
319
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300320 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700321 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300322 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 pip->version = 4;
325 pip->ihl = (sizeof(struct iphdr)+4)>>2;
326 pip->tos = 0xc0;
327 pip->frag_off = htons(IP_DF);
328 pip->ttl = 1;
329 pip->daddr = rt->rt_dst;
330 pip->saddr = rt->rt_src;
331 pip->protocol = IPPROTO_IGMP;
332 pip->tot_len = 0; /* filled in later */
333 ip_select_ident(pip, &rt->u.dst, NULL);
334 ((u8*)&pip[1])[0] = IPOPT_RA;
335 ((u8*)&pip[1])[1] = 4;
336 ((u8*)&pip[1])[2] = 0;
337 ((u8*)&pip[1])[3] = 0;
338
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700339 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300340 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300341 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 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{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300352 struct igmphdr *pig = igmp_hdr(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700353 const int igmplen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300355 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Herbert Xuc439cb22008-01-11 19:14:00 -0800357 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
361{
362 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
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;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300381 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 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;
David L Stevensad125832006-01-18 14:20:56 -0800397 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
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
David L Stevensad125832006-01-18 14:20:56 -0800407 stotal = scount = 0;
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
410
David L Stevensad125832006-01-18 14:20:56 -0800411 if (!*psf_list)
412 goto empty_source;
413
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300414 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 psf_prev = NULL;
427 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700428 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
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
Al Viro63007722006-09-27 18:31:32 -0700441 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 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 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800457 if (!skb)
458 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700459 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800461 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 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 }
David L Stevensad125832006-01-18 14:20:56 -0800476
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (pmc->sfmode == MCAST_EXCLUDE) {
574 type = IGMPV3_CHANGE_TO_INCLUDE;
575 skb = add_grec(skb, pmc, type, 1, 0);
576 }
David L Stevensad125832006-01-18 14:20:56 -0800577 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 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);
David L Stevensad125832006-01-18 14:20:56 -0800615 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
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;
Al Viro63007722006-09-27 18:31:32 -0700634 __be32 group = pmc ? pmc->multiaddr : 0;
635 __be32 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
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 };
Denis V. Lunevf2063512008-01-22 22:07:34 -0800648 if (ip_route_output_key(&init_net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -1;
650 }
651 if (rt->rt_src == 0) {
652 ip_rt_put(rt);
653 return -1;
654 }
655
Johannes Bergf5184d22008-05-12 20:48:31 -0700656 skb=alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 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
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300666 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700667 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300668 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
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
Herbert Xuc439cb22008-01-11 19:14:00 -0800691 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
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;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900719 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 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
David L Stevensad125832006-01-18 14:20:56 -0800750/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700751static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct ip_sf_list *psf;
754 int i, scount;
755
756 scount = 0;
757 for (psf=pmc->sources; psf; psf=psf->sf_next) {
758 if (scount == nsrcs)
759 break;
David L Stevensad125832006-01-18 14:20:56 -0800760 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
Al Viroea4d9e72006-09-27 18:30:52 -0700778static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800779{
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 */
787 scount = 0;
788 for (psf=pmc->sources; psf; psf=psf->sf_next) {
789 if (scount == nsrcs)
790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 for (i=0; i<nsrcs; i++)
792 if (srcs[i] == psf->sf_inaddr) {
793 psf->sf_gsresp = 1;
794 scount++;
795 break;
796 }
797 }
David L Stevensad125832006-01-18 14:20:56 -0800798 if (!scount) {
799 pmc->gsquery = 0;
800 return 0;
801 }
802 pmc->gsquery = 1;
803 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Al Viro63007722006-09-27 18:31:32 -0700806static void igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
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{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300828 struct igmphdr *ih = igmp_hdr(skb);
829 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700831 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 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. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 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;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900861
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300862 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900864 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700865 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300867 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
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) {
David L Stevensad125832006-01-18 14:20:56 -0800898 int changed;
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 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;
David L Stevensad125832006-01-18 14:20:56 -0800909 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900910 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800912 if (changed)
913 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
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
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800925 if (in_dev == NULL)
926 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Herbert Xufb286bb2005-11-10 13:01:24 -0800928 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800929 goto drop_ref;
Herbert Xufb286bb2005-11-10 13:01:24 -0800930
931 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700932 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800933 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800934 break;
935 /* fall through */
936 case CHECKSUM_NONE:
937 skb->csum = 0;
938 if (__skb_checksum_complete(skb))
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800939 goto drop_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300942 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 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? */
Eric Dumazetee6b9672008-03-05 18:30:47 -0800951 if (skb->rtable->fl.iif == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
David Stevens24c69272005-12-02 20:32:59 -0800953 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 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:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -0800970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Herbert Xufb286bb2005-11-10 13:01:24 -0800972
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800973drop_ref:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 in_dev_put(in_dev);
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800975drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 kfree_skb(skb);
977 return 0;
978}
979
980#endif
981
982
983/*
984 * Add a filter to a device
985 */
986
Al Viro63007722006-09-27 18:31:32 -0700987static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
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
Al Viro63007722006-09-27 18:31:32 -07001007static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
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 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001030 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (!pmc)
1032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 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
Al Viro63007722006-09-27 18:31:32 -07001057static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
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
Al Viro8f935bb2006-09-27 18:30:07 -07001195void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct ip_mc_list *im;
1198
1199 ASSERT_RTNL();
1200
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001201 if (dev_net(in_dev->dev) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001202 return;
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 for (im=in_dev->mc_list; im; im=im->next) {
1205 if (im->multiaddr == addr) {
1206 im->users++;
1207 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1208 goto out;
1209 }
1210 }
1211
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001212 im = kmalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (!im)
1214 goto out;
1215
1216 im->users=1;
1217 im->interface=in_dev;
1218 in_dev_hold(in_dev);
1219 im->multiaddr=addr;
1220 /* initial mode is (EX, empty) */
1221 im->sfmode = MCAST_EXCLUDE;
1222 im->sfcount[MCAST_INCLUDE] = 0;
1223 im->sfcount[MCAST_EXCLUDE] = 1;
1224 im->sources = NULL;
1225 im->tomb = NULL;
1226 im->crcount = 0;
1227 atomic_set(&im->refcnt, 1);
1228 spin_lock_init(&im->lock);
1229#ifdef CONFIG_IP_MULTICAST
1230 im->tm_running=0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001231 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1233 im->reporter = 0;
1234 im->gsquery = 0;
1235#endif
1236 im->loaded = 0;
1237 write_lock_bh(&in_dev->mc_list_lock);
1238 im->next=in_dev->mc_list;
1239 in_dev->mc_list=im;
1240 write_unlock_bh(&in_dev->mc_list_lock);
1241#ifdef CONFIG_IP_MULTICAST
1242 igmpv3_del_delrec(in_dev, im->multiaddr);
1243#endif
1244 igmp_group_added(im);
1245 if (!in_dev->dead)
1246 ip_rt_multicast_event(in_dev);
1247out:
1248 return;
1249}
1250
1251/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001252 * Resend IGMP JOIN report; used for bonding.
1253 */
1254void ip_mc_rejoin_group(struct ip_mc_list *im)
1255{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001256#ifdef CONFIG_IP_MULTICAST
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001257 struct in_device *in_dev = im->interface;
1258
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001259 if (im->multiaddr == IGMP_ALL_HOSTS)
1260 return;
1261
1262 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1263 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1264 return;
1265 }
1266 /* else, v3 */
1267 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1268 IGMP_Unsolicited_Report_Count;
1269 igmp_ifc_event(in_dev);
1270#endif
1271}
1272
1273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * A socket has left a multicast group on device dev
1275 */
1276
Al Viro8f935bb2006-09-27 18:30:07 -07001277void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
1279 struct ip_mc_list *i, **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001282
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001283 if (dev_net(in_dev->dev) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001284 return;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1287 if (i->multiaddr==addr) {
1288 if (--i->users == 0) {
1289 write_lock_bh(&in_dev->mc_list_lock);
1290 *ip = i->next;
1291 write_unlock_bh(&in_dev->mc_list_lock);
1292 igmp_group_dropped(i);
1293
1294 if (!in_dev->dead)
1295 ip_rt_multicast_event(in_dev);
1296
1297 ip_ma_put(i);
1298 return;
1299 }
1300 break;
1301 }
1302 }
1303}
1304
1305/* Device going down */
1306
1307void ip_mc_down(struct in_device *in_dev)
1308{
1309 struct ip_mc_list *i;
1310
1311 ASSERT_RTNL();
1312
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001313 if (dev_net(in_dev->dev) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001314 return;
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 for (i=in_dev->mc_list; i; i=i->next)
1317 igmp_group_dropped(i);
1318
1319#ifdef CONFIG_IP_MULTICAST
1320 in_dev->mr_ifc_count = 0;
1321 if (del_timer(&in_dev->mr_ifc_timer))
1322 __in_dev_put(in_dev);
1323 in_dev->mr_gq_running = 0;
1324 if (del_timer(&in_dev->mr_gq_timer))
1325 __in_dev_put(in_dev);
1326 igmpv3_clear_delrec(in_dev);
1327#endif
1328
1329 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1330}
1331
1332void ip_mc_init_dev(struct in_device *in_dev)
1333{
1334 ASSERT_RTNL();
1335
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001336 if (dev_net(in_dev->dev) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001337 return;
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 in_dev->mc_tomb = NULL;
1340#ifdef CONFIG_IP_MULTICAST
1341 in_dev->mr_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001342 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1343 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 in_dev->mr_ifc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001345 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1346 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1348#endif
1349
1350 rwlock_init(&in_dev->mc_list_lock);
1351 spin_lock_init(&in_dev->mc_tomb_lock);
1352}
1353
1354/* Device going up */
1355
1356void ip_mc_up(struct in_device *in_dev)
1357{
1358 struct ip_mc_list *i;
1359
1360 ASSERT_RTNL();
1361
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001362 if (dev_net(in_dev->dev) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001363 return;
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1366
1367 for (i=in_dev->mc_list; i; i=i->next)
1368 igmp_group_added(i);
1369}
1370
1371/*
1372 * Device is about to be destroyed: clean up.
1373 */
1374
1375void ip_mc_destroy_dev(struct in_device *in_dev)
1376{
1377 struct ip_mc_list *i;
1378
1379 ASSERT_RTNL();
1380
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001381 if (dev_net(in_dev->dev) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001382 return;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* Deactivate timers */
1385 ip_mc_down(in_dev);
1386
1387 write_lock_bh(&in_dev->mc_list_lock);
1388 while ((i = in_dev->mc_list) != NULL) {
1389 in_dev->mc_list = i->next;
1390 write_unlock_bh(&in_dev->mc_list_lock);
1391
1392 igmp_group_dropped(i);
1393 ip_ma_put(i);
1394
1395 write_lock_bh(&in_dev->mc_list_lock);
1396 }
1397 write_unlock_bh(&in_dev->mc_list_lock);
1398}
1399
1400static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1401{
1402 struct flowi fl = { .nl_u = { .ip4_u =
1403 { .daddr = imr->imr_multiaddr.s_addr } } };
1404 struct rtable *rt;
1405 struct net_device *dev = NULL;
1406 struct in_device *idev = NULL;
1407
1408 if (imr->imr_ifindex) {
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -08001409 idev = inetdev_by_index(&init_net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (idev)
1411 __in_dev_put(idev);
1412 return idev;
1413 }
1414 if (imr->imr_address.s_addr) {
Denis V. Lunev1ab35272008-01-22 22:04:30 -08001415 dev = ip_dev_find(&init_net, imr->imr_address.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!dev)
1417 return NULL;
Stephen Hemminger15333062006-03-20 22:32:28 -08001418 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
Denis V. Lunevf2063512008-01-22 22:07:34 -08001421 if (!dev && !ip_route_output_key(&init_net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 dev = rt->u.dst.dev;
1423 ip_rt_put(rt);
1424 }
1425 if (dev) {
1426 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001427 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 return idev;
1430}
1431
1432/*
1433 * Join a socket to a group
1434 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001435int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1436int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438
1439static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001440 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 struct ip_sf_list *psf, *psf_prev;
1443 int rv = 0;
1444
1445 psf_prev = NULL;
1446 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1447 if (psf->sf_inaddr == *psfsrc)
1448 break;
1449 psf_prev = psf;
1450 }
1451 if (!psf || psf->sf_count[sfmode] == 0) {
1452 /* source filter not found, or count wrong => bug */
1453 return -ESRCH;
1454 }
1455 psf->sf_count[sfmode]--;
1456 if (psf->sf_count[sfmode] == 0) {
1457 ip_rt_multicast_event(pmc->interface);
1458 }
1459 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1460#ifdef CONFIG_IP_MULTICAST
1461 struct in_device *in_dev = pmc->interface;
1462#endif
1463
1464 /* no more filters for this source */
1465 if (psf_prev)
1466 psf_prev->sf_next = psf->sf_next;
1467 else
1468 pmc->sources = psf->sf_next;
1469#ifdef CONFIG_IP_MULTICAST
1470 if (psf->sf_oldin &&
1471 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001472 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 IGMP_Unsolicited_Report_Count;
1474 psf->sf_next = pmc->tomb;
1475 pmc->tomb = psf;
1476 rv = 1;
1477 } else
1478#endif
1479 kfree(psf);
1480 }
1481 return rv;
1482}
1483
1484#ifndef CONFIG_IP_MULTICAST
1485#define igmp_ifc_event(x) do { } while (0)
1486#endif
1487
Al Viro8f935bb2006-09-27 18:30:07 -07001488static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1489 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490{
1491 struct ip_mc_list *pmc;
1492 int changerec = 0;
1493 int i, err;
1494
1495 if (!in_dev)
1496 return -ENODEV;
1497 read_lock(&in_dev->mc_list_lock);
1498 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1499 if (*pmca == pmc->multiaddr)
1500 break;
1501 }
1502 if (!pmc) {
1503 /* MCA not found?? bug */
1504 read_unlock(&in_dev->mc_list_lock);
1505 return -ESRCH;
1506 }
1507 spin_lock_bh(&pmc->lock);
1508 read_unlock(&in_dev->mc_list_lock);
1509#ifdef CONFIG_IP_MULTICAST
1510 sf_markstate(pmc);
1511#endif
1512 if (!delta) {
1513 err = -EINVAL;
1514 if (!pmc->sfcount[sfmode])
1515 goto out_unlock;
1516 pmc->sfcount[sfmode]--;
1517 }
1518 err = 0;
1519 for (i=0; i<sfcount; i++) {
1520 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1521
1522 changerec |= rv > 0;
1523 if (!err && rv < 0)
1524 err = rv;
1525 }
1526 if (pmc->sfmode == MCAST_EXCLUDE &&
1527 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1528 pmc->sfcount[MCAST_INCLUDE]) {
1529#ifdef CONFIG_IP_MULTICAST
1530 struct ip_sf_list *psf;
1531#endif
1532
1533 /* filter mode change */
1534 pmc->sfmode = MCAST_INCLUDE;
1535#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001536 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 IGMP_Unsolicited_Report_Count;
1538 in_dev->mr_ifc_count = pmc->crcount;
1539 for (psf=pmc->sources; psf; psf = psf->sf_next)
1540 psf->sf_crcount = 0;
1541 igmp_ifc_event(pmc->interface);
1542 } else if (sf_setstate(pmc) || changerec) {
1543 igmp_ifc_event(pmc->interface);
1544#endif
1545 }
1546out_unlock:
1547 spin_unlock_bh(&pmc->lock);
1548 return err;
1549}
1550
1551/*
1552 * Add multicast single-source filter to the interface list
1553 */
1554static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001555 __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556{
1557 struct ip_sf_list *psf, *psf_prev;
1558
1559 psf_prev = NULL;
1560 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1561 if (psf->sf_inaddr == *psfsrc)
1562 break;
1563 psf_prev = psf;
1564 }
1565 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001566 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (!psf)
1568 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 psf->sf_inaddr = *psfsrc;
1570 if (psf_prev) {
1571 psf_prev->sf_next = psf;
1572 } else
1573 pmc->sources = psf;
1574 }
1575 psf->sf_count[sfmode]++;
1576 if (psf->sf_count[sfmode] == 1) {
1577 ip_rt_multicast_event(pmc->interface);
1578 }
1579 return 0;
1580}
1581
1582#ifdef CONFIG_IP_MULTICAST
1583static void sf_markstate(struct ip_mc_list *pmc)
1584{
1585 struct ip_sf_list *psf;
1586 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1587
1588 for (psf=pmc->sources; psf; psf=psf->sf_next)
1589 if (pmc->sfcount[MCAST_EXCLUDE]) {
1590 psf->sf_oldin = mca_xcount ==
1591 psf->sf_count[MCAST_EXCLUDE] &&
1592 !psf->sf_count[MCAST_INCLUDE];
1593 } else
1594 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1595}
1596
1597static int sf_setstate(struct ip_mc_list *pmc)
1598{
David L Stevensad125832006-01-18 14:20:56 -08001599 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1601 int qrv = pmc->interface->mr_qrv;
1602 int new_in, rv;
1603
1604 rv = 0;
1605 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1606 if (pmc->sfcount[MCAST_EXCLUDE]) {
1607 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1608 !psf->sf_count[MCAST_INCLUDE];
1609 } else
1610 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001611 if (new_in) {
1612 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001613 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001614
1615 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1616 if (dpsf->sf_inaddr == psf->sf_inaddr)
1617 break;
1618 prev = dpsf;
1619 }
1620 if (dpsf) {
1621 if (prev)
1622 prev->sf_next = dpsf->sf_next;
1623 else
1624 pmc->tomb = dpsf->sf_next;
1625 kfree(dpsf);
1626 }
1627 psf->sf_crcount = qrv;
1628 rv++;
1629 }
1630 } else if (psf->sf_oldin) {
1631
1632 psf->sf_crcount = 0;
1633 /*
1634 * add or update "delete" records if an active filter
1635 * is now inactive
1636 */
1637 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1638 if (dpsf->sf_inaddr == psf->sf_inaddr)
1639 break;
1640 if (!dpsf) {
1641 dpsf = (struct ip_sf_list *)
1642 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1643 if (!dpsf)
1644 continue;
1645 *dpsf = *psf;
1646 /* pmc->lock held by callers */
1647 dpsf->sf_next = pmc->tomb;
1648 pmc->tomb = dpsf;
1649 }
1650 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 rv++;
1652 }
1653 }
1654 return rv;
1655}
1656#endif
1657
1658/*
1659 * Add multicast source filter list to the interface list
1660 */
Al Viro8f935bb2006-09-27 18:30:07 -07001661static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1662 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
1664 struct ip_mc_list *pmc;
1665 int isexclude;
1666 int i, err;
1667
1668 if (!in_dev)
1669 return -ENODEV;
1670 read_lock(&in_dev->mc_list_lock);
1671 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1672 if (*pmca == pmc->multiaddr)
1673 break;
1674 }
1675 if (!pmc) {
1676 /* MCA not found?? bug */
1677 read_unlock(&in_dev->mc_list_lock);
1678 return -ESRCH;
1679 }
1680 spin_lock_bh(&pmc->lock);
1681 read_unlock(&in_dev->mc_list_lock);
1682
1683#ifdef CONFIG_IP_MULTICAST
1684 sf_markstate(pmc);
1685#endif
1686 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1687 if (!delta)
1688 pmc->sfcount[sfmode]++;
1689 err = 0;
1690 for (i=0; i<sfcount; i++) {
1691 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1692 if (err)
1693 break;
1694 }
1695 if (err) {
1696 int j;
1697
1698 pmc->sfcount[sfmode]--;
1699 for (j=0; j<i; j++)
1700 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1701 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1702#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001704 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705#endif
1706
1707 /* filter mode change */
1708 if (pmc->sfcount[MCAST_EXCLUDE])
1709 pmc->sfmode = MCAST_EXCLUDE;
1710 else if (pmc->sfcount[MCAST_INCLUDE])
1711 pmc->sfmode = MCAST_INCLUDE;
1712#ifdef CONFIG_IP_MULTICAST
1713 /* else no filters; keep old mode for reports */
1714
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001715 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 IGMP_Unsolicited_Report_Count;
1717 in_dev->mr_ifc_count = pmc->crcount;
1718 for (psf=pmc->sources; psf; psf = psf->sf_next)
1719 psf->sf_crcount = 0;
1720 igmp_ifc_event(in_dev);
1721 } else if (sf_setstate(pmc)) {
1722 igmp_ifc_event(in_dev);
1723#endif
1724 }
1725 spin_unlock_bh(&pmc->lock);
1726 return err;
1727}
1728
1729static void ip_mc_clear_src(struct ip_mc_list *pmc)
1730{
1731 struct ip_sf_list *psf, *nextpsf;
1732
1733 for (psf=pmc->tomb; psf; psf=nextpsf) {
1734 nextpsf = psf->sf_next;
1735 kfree(psf);
1736 }
1737 pmc->tomb = NULL;
1738 for (psf=pmc->sources; psf; psf=nextpsf) {
1739 nextpsf = psf->sf_next;
1740 kfree(psf);
1741 }
1742 pmc->sources = NULL;
1743 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001744 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 pmc->sfcount[MCAST_EXCLUDE] = 1;
1746}
1747
1748
1749/*
1750 * Join a multicast group
1751 */
1752int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1753{
1754 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001755 __be32 addr = imr->imr_multiaddr.s_addr;
David L Stevensca9b9072005-07-08 17:38:07 -07001756 struct ip_mc_socklist *iml=NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct in_device *in_dev;
1758 struct inet_sock *inet = inet_sk(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001759 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 int count = 0;
1761
Joe Perchesf97c1e02007-12-16 13:45:43 -08001762 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return -EINVAL;
1764
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001765 if (sock_net(sk) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001766 return -EPROTONOSUPPORT;
1767
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001768 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 in_dev = ip_mc_find_dev(imr);
1771
1772 if (!in_dev) {
1773 iml = NULL;
1774 err = -ENODEV;
1775 goto done;
1776 }
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001779 ifindex = imr->imr_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 for (i = inet->mc_list; i; i = i->next) {
David L Stevensca9b9072005-07-08 17:38:07 -07001781 if (i->multi.imr_multiaddr.s_addr == addr &&
1782 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 count++;
1785 }
1786 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001787 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 goto done;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001789 iml = sock_kmalloc(sk,sizeof(*iml),GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001790 if (iml == NULL)
1791 goto done;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 memcpy(&iml->multi, imr, sizeof(*imr));
1794 iml->next = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 iml->sflist = NULL;
1796 iml->sfmode = MCAST_EXCLUDE;
1797 inet->mc_list = iml;
1798 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001801 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return err;
1803}
1804
1805static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1806 struct in_device *in_dev)
1807{
1808 int err;
1809
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001810 if (iml->sflist == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /* any-source empty exclude case */
1812 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1813 iml->sfmode, 0, NULL, 0);
1814 }
1815 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1816 iml->sfmode, iml->sflist->sl_count,
1817 iml->sflist->sl_addr, 0);
1818 sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1819 iml->sflist = NULL;
1820 return err;
1821}
1822
1823/*
1824 * Ask a socket to leave a group.
1825 */
1826
1827int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1828{
1829 struct inet_sock *inet = inet_sk(sk);
1830 struct ip_mc_socklist *iml, **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001831 struct in_device *in_dev;
Al Viro8f935bb2006-09-27 18:30:07 -07001832 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001833 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001834 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001836 if (sock_net(sk) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001837 return -EPROTONOSUPPORT;
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 rtnl_lock();
David L Stevens84b42ba2005-07-08 17:48:38 -07001840 in_dev = ip_mc_find_dev(imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001841 ifindex = imr->imr_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
David L Stevensacd6e002006-08-17 16:27:39 -07001843 if (iml->multi.imr_multiaddr.s_addr != group)
1844 continue;
1845 if (ifindex) {
1846 if (iml->multi.imr_ifindex != ifindex)
1847 continue;
1848 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1849 iml->multi.imr_address.s_addr)
1850 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
David L Stevensacd6e002006-08-17 16:27:39 -07001852 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
David L Stevensacd6e002006-08-17 16:27:39 -07001854 *imlp = iml->next;
1855
1856 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001857 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001858 rtnl_unlock();
1859 sock_kfree_s(sk, iml, sizeof(*iml));
1860 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
David L Stevensacd6e002006-08-17 16:27:39 -07001862 if (!in_dev)
1863 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001865 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
1868int ip_mc_source(int add, int omode, struct sock *sk, struct
1869 ip_mreq_source *mreqs, int ifindex)
1870{
1871 int err;
1872 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001873 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 struct ip_mc_socklist *pmc;
1875 struct in_device *in_dev = NULL;
1876 struct inet_sock *inet = inet_sk(sk);
1877 struct ip_sf_socklist *psl;
David L Stevens8cdaaa12005-07-08 17:39:23 -07001878 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 int i, j, rv;
1880
Joe Perchesf97c1e02007-12-16 13:45:43 -08001881 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return -EINVAL;
1883
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001884 if (sock_net(sk) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08001885 return -EPROTONOSUPPORT;
1886
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001887 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1890 imr.imr_address.s_addr = mreqs->imr_interface;
1891 imr.imr_ifindex = ifindex;
1892 in_dev = ip_mc_find_dev(&imr);
1893
1894 if (!in_dev) {
1895 err = -ENODEV;
1896 goto done;
1897 }
1898 err = -EADDRNOTAVAIL;
1899
1900 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
David L Stevensca9b9072005-07-08 17:38:07 -07001901 if (pmc->multi.imr_multiaddr.s_addr == imr.imr_multiaddr.s_addr
1902 && pmc->multi.imr_ifindex == imr.imr_ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 break;
1904 }
David L Stevens917f2f12005-07-08 17:45:16 -07001905 if (!pmc) { /* must have a prior join */
1906 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /* if a source filter was set, must be the same mode as before */
1910 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07001911 if (pmc->sfmode != omode) {
1912 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 } else if (pmc->sfmode != omode) {
1916 /* allow mode switches for empty-set filters */
1917 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001918 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 NULL, 0);
1920 pmc->sfmode = omode;
1921 }
1922
1923 psl = pmc->sflist;
1924 if (!add) {
1925 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07001926 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 rv = !0;
1928 for (i=0; i<psl->sl_count; i++) {
1929 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001930 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 if (rv == 0)
1932 break;
1933 }
1934 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07001935 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
David L Stevens8cdaaa12005-07-08 17:39:23 -07001937 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1938 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1939 leavegroup = 1;
1940 goto done;
1941 }
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001944 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 &mreqs->imr_sourceaddr, 1);
1946
1947 for (j=i+1; j<psl->sl_count; j++)
1948 psl->sl_addr[j-1] = psl->sl_addr[j];
1949 psl->sl_count--;
1950 err = 0;
1951 goto done;
1952 }
1953 /* else, add a new source to the filter */
1954
1955 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1956 err = -ENOBUFS;
1957 goto done;
1958 }
1959 if (!psl || psl->sl_count == psl->sl_max) {
1960 struct ip_sf_socklist *newpsl;
1961 int count = IP_SFBLOCK;
1962
1963 if (psl)
1964 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001965 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 if (!newpsl) {
1967 err = -ENOBUFS;
1968 goto done;
1969 }
1970 newpsl->sl_max = count;
1971 newpsl->sl_count = count - IP_SFBLOCK;
1972 if (psl) {
1973 for (i=0; i<psl->sl_count; i++)
1974 newpsl->sl_addr[i] = psl->sl_addr[i];
1975 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1976 }
1977 pmc->sflist = psl = newpsl;
1978 }
1979 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1980 for (i=0; i<psl->sl_count; i++) {
1981 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001982 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (rv == 0)
1984 break;
1985 }
1986 if (rv == 0) /* address already there is an error */
1987 goto done;
1988 for (j=psl->sl_count-1; j>=i; j--)
1989 psl->sl_addr[j+1] = psl->sl_addr[j];
1990 psl->sl_addr[i] = mreqs->imr_sourceaddr;
1991 psl->sl_count++;
1992 err = 0;
1993 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001994 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 &mreqs->imr_sourceaddr, 1);
1996done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001997 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07001998 if (leavegroup)
1999 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return err;
2001}
2002
2003int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2004{
David L Stevens9951f032005-07-08 17:47:28 -07002005 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002007 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 struct ip_mc_socklist *pmc;
2009 struct in_device *in_dev;
2010 struct inet_sock *inet = inet_sk(sk);
2011 struct ip_sf_socklist *newpsl, *psl;
David L Stevens9951f032005-07-08 17:47:28 -07002012 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Joe Perchesf97c1e02007-12-16 13:45:43 -08002014 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 return -EINVAL;
2016 if (msf->imsf_fmode != MCAST_INCLUDE &&
2017 msf->imsf_fmode != MCAST_EXCLUDE)
2018 return -EINVAL;
2019
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002020 if (sock_net(sk) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08002021 return -EPROTONOSUPPORT;
2022
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002023 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2026 imr.imr_address.s_addr = msf->imsf_interface;
2027 imr.imr_ifindex = ifindex;
2028 in_dev = ip_mc_find_dev(&imr);
2029
2030 if (!in_dev) {
2031 err = -ENODEV;
2032 goto done;
2033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
David L Stevens9951f032005-07-08 17:47:28 -07002035 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2036 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2037 leavegroup = 1;
2038 goto done;
2039 }
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2042 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2043 pmc->multi.imr_ifindex == imr.imr_ifindex)
2044 break;
2045 }
David L Stevens917f2f12005-07-08 17:45:16 -07002046 if (!pmc) { /* must have a prior join */
2047 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002051 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2052 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (!newpsl) {
2054 err = -ENOBUFS;
2055 goto done;
2056 }
2057 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2058 memcpy(newpsl->sl_addr, msf->imsf_slist,
2059 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2060 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2061 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2062 if (err) {
2063 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2064 goto done;
2065 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002066 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002068 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2069 msf->imsf_fmode, 0, NULL, 0);
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 psl = pmc->sflist;
2072 if (psl) {
2073 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2074 psl->sl_count, psl->sl_addr, 0);
2075 sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
2076 } else
2077 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2078 0, NULL, 0);
2079 pmc->sflist = newpsl;
2080 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002081 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002083 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002084 if (leavegroup)
2085 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return err;
2087}
2088
2089int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2090 struct ip_msfilter __user *optval, int __user *optlen)
2091{
2092 int err, len, count, copycount;
2093 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002094 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct ip_mc_socklist *pmc;
2096 struct in_device *in_dev;
2097 struct inet_sock *inet = inet_sk(sk);
2098 struct ip_sf_socklist *psl;
2099
Joe Perchesf97c1e02007-12-16 13:45:43 -08002100 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 return -EINVAL;
2102
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002103 if (sock_net(sk) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08002104 return -EPROTONOSUPPORT;
2105
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002106 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2109 imr.imr_address.s_addr = msf->imsf_interface;
2110 imr.imr_ifindex = 0;
2111 in_dev = ip_mc_find_dev(&imr);
2112
2113 if (!in_dev) {
2114 err = -ENODEV;
2115 goto done;
2116 }
2117 err = -EADDRNOTAVAIL;
2118
2119 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2120 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2121 pmc->multi.imr_ifindex == imr.imr_ifindex)
2122 break;
2123 }
2124 if (!pmc) /* must have a prior join */
2125 goto done;
2126 msf->imsf_fmode = pmc->sfmode;
2127 psl = pmc->sflist;
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002128 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (!psl) {
2130 len = 0;
2131 count = 0;
2132 } else {
2133 count = psl->sl_count;
2134 }
2135 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2136 len = copycount * sizeof(psl->sl_addr[0]);
2137 msf->imsf_numsrc = count;
2138 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2139 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2140 return -EFAULT;
2141 }
2142 if (len &&
2143 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2144 return -EFAULT;
2145 return 0;
2146done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002147 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return err;
2149}
2150
2151int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2152 struct group_filter __user *optval, int __user *optlen)
2153{
2154 int err, i, count, copycount;
2155 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002156 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 struct ip_mc_socklist *pmc;
2158 struct inet_sock *inet = inet_sk(sk);
2159 struct ip_sf_socklist *psl;
2160
2161 psin = (struct sockaddr_in *)&gsf->gf_group;
2162 if (psin->sin_family != AF_INET)
2163 return -EINVAL;
2164 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002165 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return -EINVAL;
2167
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002168 if (sock_net(sk) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08002169 return -EPROTONOSUPPORT;
2170
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002171 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 err = -EADDRNOTAVAIL;
2174
2175 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2176 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2177 pmc->multi.imr_ifindex == gsf->gf_interface)
2178 break;
2179 }
2180 if (!pmc) /* must have a prior join */
2181 goto done;
2182 gsf->gf_fmode = pmc->sfmode;
2183 psl = pmc->sflist;
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002184 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 count = psl ? psl->sl_count : 0;
2186 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2187 gsf->gf_numsrc = count;
2188 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2189 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2190 return -EFAULT;
2191 }
2192 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 struct sockaddr_storage ss;
2194
2195 psin = (struct sockaddr_in *)&ss;
2196 memset(&ss, 0, sizeof(ss));
2197 psin->sin_family = AF_INET;
2198 psin->sin_addr.s_addr = psl->sl_addr[i];
2199 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2200 return -EFAULT;
2201 }
2202 return 0;
2203done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002204 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return err;
2206}
2207
2208/*
2209 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2210 */
Al Viroc0cda062006-09-27 18:31:10 -07002211int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
2213 struct inet_sock *inet = inet_sk(sk);
2214 struct ip_mc_socklist *pmc;
2215 struct ip_sf_socklist *psl;
2216 int i;
2217
Joe Perchesf97c1e02007-12-16 13:45:43 -08002218 if (!ipv4_is_multicast(loc_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return 1;
2220
2221 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2222 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2223 pmc->multi.imr_ifindex == dif)
2224 break;
2225 }
2226 if (!pmc)
2227 return 1;
2228 psl = pmc->sflist;
2229 if (!psl)
2230 return pmc->sfmode == MCAST_EXCLUDE;
2231
2232 for (i=0; i<psl->sl_count; i++) {
2233 if (psl->sl_addr[i] == rmt_addr)
2234 break;
2235 }
2236 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2237 return 0;
2238 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2239 return 0;
2240 return 1;
2241}
2242
2243/*
2244 * A socket is closing.
2245 */
2246
2247void ip_mc_drop_socket(struct sock *sk)
2248{
2249 struct inet_sock *inet = inet_sk(sk);
2250 struct ip_mc_socklist *iml;
2251
2252 if (inet->mc_list == NULL)
2253 return;
2254
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002255 if (sock_net(sk) != &init_net)
Denis V. Lunev2430aa82008-02-28 20:48:49 -08002256 return;
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 rtnl_lock();
2259 while ((iml = inet->mc_list) != NULL) {
2260 struct in_device *in_dev;
2261 inet->mc_list = iml->next;
2262
Denis V. Lunev7fee0ca2008-01-21 17:32:38 -08002263 in_dev = inetdev_by_index(&init_net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002264 (void) ip_mc_leave_src(sk, iml, in_dev);
2265 if (in_dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2267 in_dev_put(in_dev);
2268 }
2269 sock_kfree_s(sk, iml, sizeof(*iml));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271 rtnl_unlock();
2272}
2273
Al Viroa60c4922006-09-26 21:28:34 -07002274int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 struct ip_mc_list *im;
2277 struct ip_sf_list *psf;
2278 int rv = 0;
2279
2280 read_lock(&in_dev->mc_list_lock);
2281 for (im=in_dev->mc_list; im; im=im->next) {
2282 if (im->multiaddr == mc_addr)
2283 break;
2284 }
2285 if (im && proto == IPPROTO_IGMP) {
2286 rv = 1;
2287 } else if (im) {
2288 if (src_addr) {
2289 for (psf=im->sources; psf; psf=psf->sf_next) {
2290 if (psf->sf_inaddr == src_addr)
2291 break;
2292 }
2293 if (psf)
2294 rv = psf->sf_count[MCAST_INCLUDE] ||
2295 psf->sf_count[MCAST_EXCLUDE] !=
2296 im->sfcount[MCAST_EXCLUDE];
2297 else
2298 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2299 } else
2300 rv = 1; /* unspecified source; tentatively allow */
2301 }
2302 read_unlock(&in_dev->mc_list_lock);
2303 return rv;
2304}
2305
2306#if defined(CONFIG_PROC_FS)
2307struct igmp_mc_iter_state {
2308 struct net_device *dev;
2309 struct in_device *in_dev;
2310};
2311
2312#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2313
2314static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2315{
2316 struct ip_mc_list *im = NULL;
2317 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2318
Pavel Emelianov7562f872007-05-03 15:13:45 -07002319 state->in_dev = NULL;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002320 for_each_netdev(&init_net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 struct in_device *in_dev;
2322 in_dev = in_dev_get(state->dev);
2323 if (!in_dev)
2324 continue;
2325 read_lock(&in_dev->mc_list_lock);
2326 im = in_dev->mc_list;
2327 if (im) {
2328 state->in_dev = in_dev;
2329 break;
2330 }
2331 read_unlock(&in_dev->mc_list_lock);
2332 in_dev_put(in_dev);
2333 }
2334 return im;
2335}
2336
2337static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2338{
2339 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2340 im = im->next;
2341 while (!im) {
2342 if (likely(state->in_dev != NULL)) {
2343 read_unlock(&state->in_dev->mc_list_lock);
2344 in_dev_put(state->in_dev);
2345 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002346 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (!state->dev) {
2348 state->in_dev = NULL;
2349 break;
2350 }
2351 state->in_dev = in_dev_get(state->dev);
2352 if (!state->in_dev)
2353 continue;
2354 read_lock(&state->in_dev->mc_list_lock);
2355 im = state->in_dev->mc_list;
2356 }
2357 return im;
2358}
2359
2360static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2361{
2362 struct ip_mc_list *im = igmp_mc_get_first(seq);
2363 if (im)
2364 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2365 --pos;
2366 return pos ? NULL : im;
2367}
2368
2369static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerdd329bf2008-01-21 17:28:31 -08002370 __acquires(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 read_lock(&dev_base_lock);
2373 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2374}
2375
2376static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2377{
2378 struct ip_mc_list *im;
2379 if (v == SEQ_START_TOKEN)
2380 im = igmp_mc_get_first(seq);
2381 else
2382 im = igmp_mc_get_next(seq, v);
2383 ++*pos;
2384 return im;
2385}
2386
2387static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerdd329bf2008-01-21 17:28:31 -08002388 __releases(dev_base_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389{
2390 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2391 if (likely(state->in_dev != NULL)) {
2392 read_unlock(&state->in_dev->mc_list_lock);
2393 in_dev_put(state->in_dev);
2394 state->in_dev = NULL;
2395 }
2396 state->dev = NULL;
2397 read_unlock(&dev_base_lock);
2398}
2399
2400static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2401{
2402 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002403 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2405 else {
2406 struct ip_mc_list *im = (struct ip_mc_list *)v;
2407 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2408 char *querier;
2409#ifdef CONFIG_IP_MULTICAST
2410 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2411 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2412 "V3";
2413#else
2414 querier = "NONE";
2415#endif
2416
2417 if (state->in_dev->mc_list == im) {
2418 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2419 state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2420 }
2421
2422 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002423 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 im->multiaddr, im->users,
2425 im->tm_running, im->tm_running ?
2426 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2427 im->reporter);
2428 }
2429 return 0;
2430}
2431
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002432static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 .start = igmp_mc_seq_start,
2434 .next = igmp_mc_seq_next,
2435 .stop = igmp_mc_seq_stop,
2436 .show = igmp_mc_seq_show,
2437};
2438
2439static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2440{
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002441 return seq_open_private(file, &igmp_mc_seq_ops,
2442 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
Arjan van de Ven9a321442007-02-12 00:55:35 -08002445static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 .owner = THIS_MODULE,
2447 .open = igmp_mc_seq_open,
2448 .read = seq_read,
2449 .llseek = seq_lseek,
2450 .release = seq_release_private,
2451};
2452
2453struct igmp_mcf_iter_state {
2454 struct net_device *dev;
2455 struct in_device *idev;
2456 struct ip_mc_list *im;
2457};
2458
2459#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2460
2461static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2462{
2463 struct ip_sf_list *psf = NULL;
2464 struct ip_mc_list *im = NULL;
2465 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2466
Pavel Emelianov7562f872007-05-03 15:13:45 -07002467 state->idev = NULL;
2468 state->im = NULL;
Eric W. Biederman881d9662007-09-17 11:56:21 -07002469 for_each_netdev(&init_net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct in_device *idev;
2471 idev = in_dev_get(state->dev);
2472 if (unlikely(idev == NULL))
2473 continue;
2474 read_lock(&idev->mc_list_lock);
2475 im = idev->mc_list;
2476 if (likely(im != NULL)) {
2477 spin_lock_bh(&im->lock);
2478 psf = im->sources;
2479 if (likely(psf != NULL)) {
2480 state->im = im;
2481 state->idev = idev;
2482 break;
2483 }
2484 spin_unlock_bh(&im->lock);
2485 }
2486 read_unlock(&idev->mc_list_lock);
2487 in_dev_put(idev);
2488 }
2489 return psf;
2490}
2491
2492static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2493{
2494 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2495
2496 psf = psf->sf_next;
2497 while (!psf) {
2498 spin_unlock_bh(&state->im->lock);
2499 state->im = state->im->next;
2500 while (!state->im) {
2501 if (likely(state->idev != NULL)) {
2502 read_unlock(&state->idev->mc_list_lock);
2503 in_dev_put(state->idev);
2504 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07002505 state->dev = next_net_device(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 if (!state->dev) {
2507 state->idev = NULL;
2508 goto out;
2509 }
2510 state->idev = in_dev_get(state->dev);
2511 if (!state->idev)
2512 continue;
2513 read_lock(&state->idev->mc_list_lock);
2514 state->im = state->idev->mc_list;
2515 }
2516 if (!state->im)
2517 break;
2518 spin_lock_bh(&state->im->lock);
2519 psf = state->im->sources;
2520 }
2521out:
2522 return psf;
2523}
2524
2525static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2526{
2527 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2528 if (psf)
2529 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2530 --pos;
2531 return pos ? NULL : psf;
2532}
2533
2534static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2535{
2536 read_lock(&dev_base_lock);
2537 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2538}
2539
2540static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2541{
2542 struct ip_sf_list *psf;
2543 if (v == SEQ_START_TOKEN)
2544 psf = igmp_mcf_get_first(seq);
2545 else
2546 psf = igmp_mcf_get_next(seq, v);
2547 ++*pos;
2548 return psf;
2549}
2550
2551static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2552{
2553 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2554 if (likely(state->im != NULL)) {
2555 spin_unlock_bh(&state->im->lock);
2556 state->im = NULL;
2557 }
2558 if (likely(state->idev != NULL)) {
2559 read_unlock(&state->idev->mc_list_lock);
2560 in_dev_put(state->idev);
2561 state->idev = NULL;
2562 }
2563 state->dev = NULL;
2564 read_unlock(&dev_base_lock);
2565}
2566
2567static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2568{
2569 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2570 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2571
2572 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002573 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 "%3s %6s "
2575 "%10s %10s %6s %6s\n", "Idx",
2576 "Device", "MCA",
2577 "SRC", "INC", "EXC");
2578 } else {
2579 seq_printf(seq,
2580 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002581 "0x%08x %6lu %6lu\n",
2582 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 ntohl(state->im->multiaddr),
2584 ntohl(psf->sf_inaddr),
2585 psf->sf_count[MCAST_INCLUDE],
2586 psf->sf_count[MCAST_EXCLUDE]);
2587 }
2588 return 0;
2589}
2590
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002591static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 .start = igmp_mcf_seq_start,
2593 .next = igmp_mcf_seq_next,
2594 .stop = igmp_mcf_seq_stop,
2595 .show = igmp_mcf_seq_show,
2596};
2597
2598static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2599{
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002600 return seq_open_private(file, &igmp_mcf_seq_ops,
2601 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602}
2603
Arjan van de Ven9a321442007-02-12 00:55:35 -08002604static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 .owner = THIS_MODULE,
2606 .open = igmp_mcf_seq_open,
2607 .read = seq_read,
2608 .llseek = seq_lseek,
2609 .release = seq_release_private,
2610};
2611
2612int __init igmp_mc_proc_init(void)
2613{
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02002614 proc_net_fops_create(&init_net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2615 proc_net_fops_create(&init_net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return 0;
2617}
2618#endif
2619
2620EXPORT_SYMBOL(ip_mc_dec_group);
2621EXPORT_SYMBOL(ip_mc_inc_group);
2622EXPORT_SYMBOL(ip_mc_join_group);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08002623EXPORT_SYMBOL(ip_mc_rejoin_group);