blob: 15d3eeda92f5c7876698dac44048ff1c4bbc09b7 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Authors:
Alan Cox113aa832008-10-13 19:01:08 -070012 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090036 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * 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
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050 * memberships but was being deleted,
51 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * from %p with timer not initialized\n"
53 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090054 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020092
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020093#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020094#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#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
Herbert Xu42f811b2007-06-04 23:34:44 -0700131#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700133 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) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700138 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)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700143static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static 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);
Al Viro8f935bb2006-09-27 18:30:07 -0700149static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
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);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800171 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 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{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800180 int tv = net_random() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800182 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 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);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800211 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 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;
David L Stevensad125832006-01-18 14:20:56 -0800237 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 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;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700293 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
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 };
Daniel Lezcano877aced2008-08-13 16:15:57 -0700304 if (ip_route_output_key(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
Eric Dumazetadf30902009-06-02 05:19:30 +0000315 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 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{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800362 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
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;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700634 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700635 __be32 group = pmc ? pmc->multiaddr : 0;
636 __be32 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
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 };
Daniel Lezcano877aced2008-08-13 16:15:57 -0700649 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -1;
651 }
652 if (rt->rt_src == 0) {
653 ip_rt_put(rt);
654 return -1;
655 }
656
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800657 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (skb == NULL) {
659 ip_rt_put(rt);
660 return -1;
661 }
662
Eric Dumazetadf30902009-06-02 05:19:30 +0000663 skb_dst_set(skb, &rt->u.dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 skb_reserve(skb, LL_RESERVED_SPACE(dev));
666
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300667 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700668 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300669 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 ip_select_ident(iph, &rt->u.dst, NULL);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800686 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Herbert Xuc439cb22008-01-11 19:14:00 -0800692 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
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;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900720 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 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);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800732 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
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
David L Stevensad125832006-01-18 14:20:56 -0800751/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700752static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 struct ip_sf_list *psf;
755 int i, scount;
756
757 scount = 0;
758 for (psf=pmc->sources; psf; psf=psf->sf_next) {
759 if (scount == nsrcs)
760 break;
David L Stevensad125832006-01-18 14:20:56 -0800761 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
Al Viroea4d9e72006-09-27 18:30:52 -0700779static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800780{
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 */
788 scount = 0;
789 for (psf=pmc->sources; psf; psf=psf->sf_next) {
790 if (scount == nsrcs)
791 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 for (i=0; i<nsrcs; i++)
793 if (srcs[i] == psf->sf_inaddr) {
794 psf->sf_gsresp = 1;
795 scount++;
796 break;
797 }
798 }
David L Stevensad125832006-01-18 14:20:56 -0800799 if (!scount) {
800 pmc->gsquery = 0;
801 return 0;
802 }
803 pmc->gsquery = 1;
804 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Al Viro63007722006-09-27 18:31:32 -0700807static void igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
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{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300829 struct igmphdr *ih = igmp_hdr(skb);
830 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700832 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 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. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 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;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900862
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300863 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900865 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700866 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300868 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
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) {
David L Stevensad125832006-01-18 14:20:56 -0800899 int changed;
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 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;
David L Stevensad125832006-01-18 14:20:56 -0800910 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900911 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800913 if (changed)
914 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 read_unlock(&in_dev->mc_list_lock);
917}
918
919int igmp_rcv(struct sk_buff *skb)
920{
921 /* This basically follows the spec line by line -- see RFC1112 */
922 struct igmphdr *ih;
923 struct in_device *in_dev = in_dev_get(skb->dev);
924 int len = skb->len;
925
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800926 if (in_dev == NULL)
927 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Herbert Xufb286bb2005-11-10 13:01:24 -0800929 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800930 goto drop_ref;
Herbert Xufb286bb2005-11-10 13:01:24 -0800931
932 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700933 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800934 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800935 break;
936 /* fall through */
937 case CHECKSUM_NONE:
938 skb->csum = 0;
939 if (__skb_checksum_complete(skb))
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800940 goto drop_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300943 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 switch (ih->type) {
945 case IGMP_HOST_MEMBERSHIP_QUERY:
946 igmp_heard_query(in_dev, skb, len);
947 break;
948 case IGMP_HOST_MEMBERSHIP_REPORT:
949 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 /* Is it our report looped back? */
Eric Dumazet511c3f92009-06-02 05:14:27 +0000951 if (skb_rtable(skb)->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
Herbert Xuc6b471e2010-02-07 17:26:30 +0000963 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 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:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -0800971 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Herbert Xufb286bb2005-11-10 13:01:24 -0800973
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800974drop_ref:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 in_dev_put(in_dev);
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800976drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 kfree_skb(skb);
978 return 0;
979}
980
981#endif
982
983
984/*
985 * Add a filter to a device
986 */
987
Al Viro63007722006-09-27 18:31:32 -0700988static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
990 char buf[MAX_ADDR_LEN];
991 struct net_device *dev = in_dev->dev;
992
993 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
994 We will get multicast token leakage, when IFF_MULTICAST
995 is changed. This check should be done in dev->set_multicast_list
996 routine. Something sort of:
997 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
998 --ANK
999 */
1000 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001001 dev_mc_add(dev, buf, dev->addr_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004/*
1005 * Remove a filter from a device
1006 */
1007
Al Viro63007722006-09-27 18:31:32 -07001008static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 char buf[MAX_ADDR_LEN];
1011 struct net_device *dev = in_dev->dev;
1012
1013 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001014 dev_mc_delete(dev, buf, dev->addr_len, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
1017#ifdef CONFIG_IP_MULTICAST
1018/*
1019 * deleted ip_mc_list manipulation
1020 */
1021static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1022{
1023 struct ip_mc_list *pmc;
1024
1025 /* this is an "ip_mc_list" for convenience; only the fields below
1026 * are actually used. In particular, the refcnt and users are not
1027 * used for management of the delete list. Using the same structure
1028 * for deleted items allows change reports to use common code with
1029 * non-deleted or query-response MCA's.
1030 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001031 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (!pmc)
1033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 spin_lock_bh(&im->lock);
1035 pmc->interface = im->interface;
1036 in_dev_hold(in_dev);
1037 pmc->multiaddr = im->multiaddr;
1038 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1039 IGMP_Unsolicited_Report_Count;
1040 pmc->sfmode = im->sfmode;
1041 if (pmc->sfmode == MCAST_INCLUDE) {
1042 struct ip_sf_list *psf;
1043
1044 pmc->tomb = im->tomb;
1045 pmc->sources = im->sources;
1046 im->tomb = im->sources = NULL;
1047 for (psf=pmc->sources; psf; psf=psf->sf_next)
1048 psf->sf_crcount = pmc->crcount;
1049 }
1050 spin_unlock_bh(&im->lock);
1051
1052 spin_lock_bh(&in_dev->mc_tomb_lock);
1053 pmc->next = in_dev->mc_tomb;
1054 in_dev->mc_tomb = pmc;
1055 spin_unlock_bh(&in_dev->mc_tomb_lock);
1056}
1057
Al Viro63007722006-09-27 18:31:32 -07001058static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
1060 struct ip_mc_list *pmc, *pmc_prev;
1061 struct ip_sf_list *psf, *psf_next;
1062
1063 spin_lock_bh(&in_dev->mc_tomb_lock);
1064 pmc_prev = NULL;
1065 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1066 if (pmc->multiaddr == multiaddr)
1067 break;
1068 pmc_prev = pmc;
1069 }
1070 if (pmc) {
1071 if (pmc_prev)
1072 pmc_prev->next = pmc->next;
1073 else
1074 in_dev->mc_tomb = pmc->next;
1075 }
1076 spin_unlock_bh(&in_dev->mc_tomb_lock);
1077 if (pmc) {
1078 for (psf=pmc->tomb; psf; psf=psf_next) {
1079 psf_next = psf->sf_next;
1080 kfree(psf);
1081 }
1082 in_dev_put(pmc->interface);
1083 kfree(pmc);
1084 }
1085}
1086
1087static void igmpv3_clear_delrec(struct in_device *in_dev)
1088{
1089 struct ip_mc_list *pmc, *nextpmc;
1090
1091 spin_lock_bh(&in_dev->mc_tomb_lock);
1092 pmc = in_dev->mc_tomb;
1093 in_dev->mc_tomb = NULL;
1094 spin_unlock_bh(&in_dev->mc_tomb_lock);
1095
1096 for (; pmc; pmc = nextpmc) {
1097 nextpmc = pmc->next;
1098 ip_mc_clear_src(pmc);
1099 in_dev_put(pmc->interface);
1100 kfree(pmc);
1101 }
1102 /* clear dead sources, too */
1103 read_lock(&in_dev->mc_list_lock);
1104 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1105 struct ip_sf_list *psf, *psf_next;
1106
1107 spin_lock_bh(&pmc->lock);
1108 psf = pmc->tomb;
1109 pmc->tomb = NULL;
1110 spin_unlock_bh(&pmc->lock);
1111 for (; psf; psf=psf_next) {
1112 psf_next = psf->sf_next;
1113 kfree(psf);
1114 }
1115 }
1116 read_unlock(&in_dev->mc_list_lock);
1117}
1118#endif
1119
1120static void igmp_group_dropped(struct ip_mc_list *im)
1121{
1122 struct in_device *in_dev = im->interface;
1123#ifdef CONFIG_IP_MULTICAST
1124 int reporter;
1125#endif
1126
1127 if (im->loaded) {
1128 im->loaded = 0;
1129 ip_mc_filter_del(in_dev, im->multiaddr);
1130 }
1131
1132#ifdef CONFIG_IP_MULTICAST
1133 if (im->multiaddr == IGMP_ALL_HOSTS)
1134 return;
1135
1136 reporter = im->reporter;
1137 igmp_stop_timer(im);
1138
1139 if (!in_dev->dead) {
1140 if (IGMP_V1_SEEN(in_dev))
1141 goto done;
1142 if (IGMP_V2_SEEN(in_dev)) {
1143 if (reporter)
1144 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1145 goto done;
1146 }
1147 /* IGMPv3 */
1148 igmpv3_add_delrec(in_dev, im);
1149
1150 igmp_ifc_event(in_dev);
1151 }
1152done:
1153#endif
1154 ip_mc_clear_src(im);
1155}
1156
1157static void igmp_group_added(struct ip_mc_list *im)
1158{
1159 struct in_device *in_dev = im->interface;
1160
1161 if (im->loaded == 0) {
1162 im->loaded = 1;
1163 ip_mc_filter_add(in_dev, im->multiaddr);
1164 }
1165
1166#ifdef CONFIG_IP_MULTICAST
1167 if (im->multiaddr == IGMP_ALL_HOSTS)
1168 return;
1169
1170 if (in_dev->dead)
1171 return;
1172 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1173 spin_lock_bh(&im->lock);
1174 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1175 spin_unlock_bh(&im->lock);
1176 return;
1177 }
1178 /* else, v3 */
1179
1180 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1181 IGMP_Unsolicited_Report_Count;
1182 igmp_ifc_event(in_dev);
1183#endif
1184}
1185
1186
1187/*
1188 * Multicast list managers
1189 */
1190
1191
1192/*
1193 * A socket has joined a multicast group on device dev.
1194 */
1195
Al Viro8f935bb2006-09-27 18:30:07 -07001196void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
1198 struct ip_mc_list *im;
1199
1200 ASSERT_RTNL();
1201
1202 for (im=in_dev->mc_list; im; im=im->next) {
1203 if (im->multiaddr == addr) {
1204 im->users++;
1205 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1206 goto out;
1207 }
1208 }
1209
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001210 im = kmalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (!im)
1212 goto out;
1213
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001214 im->users = 1;
1215 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001217 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 /* initial mode is (EX, empty) */
1219 im->sfmode = MCAST_EXCLUDE;
1220 im->sfcount[MCAST_INCLUDE] = 0;
1221 im->sfcount[MCAST_EXCLUDE] = 1;
1222 im->sources = NULL;
1223 im->tomb = NULL;
1224 im->crcount = 0;
1225 atomic_set(&im->refcnt, 1);
1226 spin_lock_init(&im->lock);
1227#ifdef CONFIG_IP_MULTICAST
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001228 im->tm_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001229 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1231 im->reporter = 0;
1232 im->gsquery = 0;
1233#endif
1234 im->loaded = 0;
1235 write_lock_bh(&in_dev->mc_list_lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001236 im->next = in_dev->mc_list;
1237 in_dev->mc_list = im;
Rami Rosenb8bae412008-10-07 15:34:37 -07001238 in_dev->mc_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 write_unlock_bh(&in_dev->mc_list_lock);
1240#ifdef CONFIG_IP_MULTICAST
1241 igmpv3_del_delrec(in_dev, im->multiaddr);
1242#endif
1243 igmp_group_added(im);
1244 if (!in_dev->dead)
1245 ip_rt_multicast_event(in_dev);
1246out:
1247 return;
1248}
1249
1250/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001251 * Resend IGMP JOIN report; used for bonding.
1252 */
1253void ip_mc_rejoin_group(struct ip_mc_list *im)
1254{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001255#ifdef CONFIG_IP_MULTICAST
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001256 struct in_device *in_dev = im->interface;
1257
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001258 if (im->multiaddr == IGMP_ALL_HOSTS)
1259 return;
1260
1261 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1262 igmp_mod_timer(im, IGMP_Initial_Report_Delay);
1263 return;
1264 }
1265 /* else, v3 */
1266 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1267 IGMP_Unsolicited_Report_Count;
1268 igmp_ifc_event(in_dev);
1269#endif
1270}
1271
1272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 * A socket has left a multicast group on device dev
1274 */
1275
Al Viro8f935bb2006-09-27 18:30:07 -07001276void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct ip_mc_list *i, **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001283 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (--i->users == 0) {
1285 write_lock_bh(&in_dev->mc_list_lock);
1286 *ip = i->next;
Rami Rosenb8bae412008-10-07 15:34:37 -07001287 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 write_unlock_bh(&in_dev->mc_list_lock);
1289 igmp_group_dropped(i);
1290
1291 if (!in_dev->dead)
1292 ip_rt_multicast_event(in_dev);
1293
1294 ip_ma_put(i);
1295 return;
1296 }
1297 break;
1298 }
1299 }
1300}
1301
Moni Shoua75c78502009-09-15 02:37:40 -07001302/* Device changing type */
1303
1304void ip_mc_unmap(struct in_device *in_dev)
1305{
1306 struct ip_mc_list *i;
1307
1308 ASSERT_RTNL();
1309
1310 for (i = in_dev->mc_list; i; i = i->next)
1311 igmp_group_dropped(i);
1312}
1313
1314void ip_mc_remap(struct in_device *in_dev)
1315{
1316 struct ip_mc_list *i;
1317
1318 ASSERT_RTNL();
1319
1320 for (i = in_dev->mc_list; i; i = i->next)
1321 igmp_group_added(i);
1322}
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324/* Device going down */
1325
1326void ip_mc_down(struct in_device *in_dev)
1327{
1328 struct ip_mc_list *i;
1329
1330 ASSERT_RTNL();
1331
1332 for (i=in_dev->mc_list; i; i=i->next)
1333 igmp_group_dropped(i);
1334
1335#ifdef CONFIG_IP_MULTICAST
1336 in_dev->mr_ifc_count = 0;
1337 if (del_timer(&in_dev->mr_ifc_timer))
1338 __in_dev_put(in_dev);
1339 in_dev->mr_gq_running = 0;
1340 if (del_timer(&in_dev->mr_gq_timer))
1341 __in_dev_put(in_dev);
1342 igmpv3_clear_delrec(in_dev);
1343#endif
1344
1345 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1346}
1347
1348void ip_mc_init_dev(struct in_device *in_dev)
1349{
1350 ASSERT_RTNL();
1351
1352 in_dev->mc_tomb = NULL;
1353#ifdef CONFIG_IP_MULTICAST
1354 in_dev->mr_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001355 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1356 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 in_dev->mr_ifc_count = 0;
Rami Rosenb8bae412008-10-07 15:34:37 -07001358 in_dev->mc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001359 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1360 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1362#endif
1363
1364 rwlock_init(&in_dev->mc_list_lock);
1365 spin_lock_init(&in_dev->mc_tomb_lock);
1366}
1367
1368/* Device going up */
1369
1370void ip_mc_up(struct in_device *in_dev)
1371{
1372 struct ip_mc_list *i;
1373
1374 ASSERT_RTNL();
1375
1376 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1377
1378 for (i=in_dev->mc_list; i; i=i->next)
1379 igmp_group_added(i);
1380}
1381
1382/*
1383 * Device is about to be destroyed: clean up.
1384 */
1385
1386void ip_mc_destroy_dev(struct in_device *in_dev)
1387{
1388 struct ip_mc_list *i;
1389
1390 ASSERT_RTNL();
1391
1392 /* Deactivate timers */
1393 ip_mc_down(in_dev);
1394
1395 write_lock_bh(&in_dev->mc_list_lock);
1396 while ((i = in_dev->mc_list) != NULL) {
1397 in_dev->mc_list = i->next;
Rami Rosenb8bae412008-10-07 15:34:37 -07001398 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 write_unlock_bh(&in_dev->mc_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 igmp_group_dropped(i);
1401 ip_ma_put(i);
1402
1403 write_lock_bh(&in_dev->mc_list_lock);
1404 }
1405 write_unlock_bh(&in_dev->mc_list_lock);
1406}
1407
Daniel Lezcano877aced2008-08-13 16:15:57 -07001408static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct flowi fl = { .nl_u = { .ip4_u =
1411 { .daddr = imr->imr_multiaddr.s_addr } } };
1412 struct rtable *rt;
1413 struct net_device *dev = NULL;
1414 struct in_device *idev = NULL;
1415
1416 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001417 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (idev)
1419 __in_dev_put(idev);
1420 return idev;
1421 }
1422 if (imr->imr_address.s_addr) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001423 dev = ip_dev_find(net, imr->imr_address.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (!dev)
1425 return NULL;
Stephen Hemminger15333062006-03-20 22:32:28 -08001426 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
Daniel Lezcano877aced2008-08-13 16:15:57 -07001429 if (!dev && !ip_route_output_key(net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 dev = rt->u.dst.dev;
1431 ip_rt_put(rt);
1432 }
1433 if (dev) {
1434 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001435 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
1437 return idev;
1438}
1439
1440/*
1441 * Join a socket to a group
1442 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001443int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1444int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446
1447static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001448 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
1450 struct ip_sf_list *psf, *psf_prev;
1451 int rv = 0;
1452
1453 psf_prev = NULL;
1454 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1455 if (psf->sf_inaddr == *psfsrc)
1456 break;
1457 psf_prev = psf;
1458 }
1459 if (!psf || psf->sf_count[sfmode] == 0) {
1460 /* source filter not found, or count wrong => bug */
1461 return -ESRCH;
1462 }
1463 psf->sf_count[sfmode]--;
1464 if (psf->sf_count[sfmode] == 0) {
1465 ip_rt_multicast_event(pmc->interface);
1466 }
1467 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1468#ifdef CONFIG_IP_MULTICAST
1469 struct in_device *in_dev = pmc->interface;
1470#endif
1471
1472 /* no more filters for this source */
1473 if (psf_prev)
1474 psf_prev->sf_next = psf->sf_next;
1475 else
1476 pmc->sources = psf->sf_next;
1477#ifdef CONFIG_IP_MULTICAST
1478 if (psf->sf_oldin &&
1479 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001480 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 IGMP_Unsolicited_Report_Count;
1482 psf->sf_next = pmc->tomb;
1483 pmc->tomb = psf;
1484 rv = 1;
1485 } else
1486#endif
1487 kfree(psf);
1488 }
1489 return rv;
1490}
1491
1492#ifndef CONFIG_IP_MULTICAST
1493#define igmp_ifc_event(x) do { } while (0)
1494#endif
1495
Al Viro8f935bb2006-09-27 18:30:07 -07001496static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1497 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct ip_mc_list *pmc;
1500 int changerec = 0;
1501 int i, err;
1502
1503 if (!in_dev)
1504 return -ENODEV;
1505 read_lock(&in_dev->mc_list_lock);
1506 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1507 if (*pmca == pmc->multiaddr)
1508 break;
1509 }
1510 if (!pmc) {
1511 /* MCA not found?? bug */
1512 read_unlock(&in_dev->mc_list_lock);
1513 return -ESRCH;
1514 }
1515 spin_lock_bh(&pmc->lock);
1516 read_unlock(&in_dev->mc_list_lock);
1517#ifdef CONFIG_IP_MULTICAST
1518 sf_markstate(pmc);
1519#endif
1520 if (!delta) {
1521 err = -EINVAL;
1522 if (!pmc->sfcount[sfmode])
1523 goto out_unlock;
1524 pmc->sfcount[sfmode]--;
1525 }
1526 err = 0;
1527 for (i=0; i<sfcount; i++) {
1528 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1529
1530 changerec |= rv > 0;
1531 if (!err && rv < 0)
1532 err = rv;
1533 }
1534 if (pmc->sfmode == MCAST_EXCLUDE &&
1535 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1536 pmc->sfcount[MCAST_INCLUDE]) {
1537#ifdef CONFIG_IP_MULTICAST
1538 struct ip_sf_list *psf;
1539#endif
1540
1541 /* filter mode change */
1542 pmc->sfmode = MCAST_INCLUDE;
1543#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001544 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 IGMP_Unsolicited_Report_Count;
1546 in_dev->mr_ifc_count = pmc->crcount;
1547 for (psf=pmc->sources; psf; psf = psf->sf_next)
1548 psf->sf_crcount = 0;
1549 igmp_ifc_event(pmc->interface);
1550 } else if (sf_setstate(pmc) || changerec) {
1551 igmp_ifc_event(pmc->interface);
1552#endif
1553 }
1554out_unlock:
1555 spin_unlock_bh(&pmc->lock);
1556 return err;
1557}
1558
1559/*
1560 * Add multicast single-source filter to the interface list
1561 */
1562static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001563 __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 struct ip_sf_list *psf, *psf_prev;
1566
1567 psf_prev = NULL;
1568 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1569 if (psf->sf_inaddr == *psfsrc)
1570 break;
1571 psf_prev = psf;
1572 }
1573 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001574 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 if (!psf)
1576 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 psf->sf_inaddr = *psfsrc;
1578 if (psf_prev) {
1579 psf_prev->sf_next = psf;
1580 } else
1581 pmc->sources = psf;
1582 }
1583 psf->sf_count[sfmode]++;
1584 if (psf->sf_count[sfmode] == 1) {
1585 ip_rt_multicast_event(pmc->interface);
1586 }
1587 return 0;
1588}
1589
1590#ifdef CONFIG_IP_MULTICAST
1591static void sf_markstate(struct ip_mc_list *pmc)
1592{
1593 struct ip_sf_list *psf;
1594 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1595
1596 for (psf=pmc->sources; psf; psf=psf->sf_next)
1597 if (pmc->sfcount[MCAST_EXCLUDE]) {
1598 psf->sf_oldin = mca_xcount ==
1599 psf->sf_count[MCAST_EXCLUDE] &&
1600 !psf->sf_count[MCAST_INCLUDE];
1601 } else
1602 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1603}
1604
1605static int sf_setstate(struct ip_mc_list *pmc)
1606{
David L Stevensad125832006-01-18 14:20:56 -08001607 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1609 int qrv = pmc->interface->mr_qrv;
1610 int new_in, rv;
1611
1612 rv = 0;
1613 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1614 if (pmc->sfcount[MCAST_EXCLUDE]) {
1615 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1616 !psf->sf_count[MCAST_INCLUDE];
1617 } else
1618 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001619 if (new_in) {
1620 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001621 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001622
1623 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1624 if (dpsf->sf_inaddr == psf->sf_inaddr)
1625 break;
1626 prev = dpsf;
1627 }
1628 if (dpsf) {
1629 if (prev)
1630 prev->sf_next = dpsf->sf_next;
1631 else
1632 pmc->tomb = dpsf->sf_next;
1633 kfree(dpsf);
1634 }
1635 psf->sf_crcount = qrv;
1636 rv++;
1637 }
1638 } else if (psf->sf_oldin) {
1639
1640 psf->sf_crcount = 0;
1641 /*
1642 * add or update "delete" records if an active filter
1643 * is now inactive
1644 */
1645 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1646 if (dpsf->sf_inaddr == psf->sf_inaddr)
1647 break;
1648 if (!dpsf) {
1649 dpsf = (struct ip_sf_list *)
1650 kmalloc(sizeof(*dpsf), GFP_ATOMIC);
1651 if (!dpsf)
1652 continue;
1653 *dpsf = *psf;
1654 /* pmc->lock held by callers */
1655 dpsf->sf_next = pmc->tomb;
1656 pmc->tomb = dpsf;
1657 }
1658 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 rv++;
1660 }
1661 }
1662 return rv;
1663}
1664#endif
1665
1666/*
1667 * Add multicast source filter list to the interface list
1668 */
Al Viro8f935bb2006-09-27 18:30:07 -07001669static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1670 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
1672 struct ip_mc_list *pmc;
1673 int isexclude;
1674 int i, err;
1675
1676 if (!in_dev)
1677 return -ENODEV;
1678 read_lock(&in_dev->mc_list_lock);
1679 for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1680 if (*pmca == pmc->multiaddr)
1681 break;
1682 }
1683 if (!pmc) {
1684 /* MCA not found?? bug */
1685 read_unlock(&in_dev->mc_list_lock);
1686 return -ESRCH;
1687 }
1688 spin_lock_bh(&pmc->lock);
1689 read_unlock(&in_dev->mc_list_lock);
1690
1691#ifdef CONFIG_IP_MULTICAST
1692 sf_markstate(pmc);
1693#endif
1694 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1695 if (!delta)
1696 pmc->sfcount[sfmode]++;
1697 err = 0;
1698 for (i=0; i<sfcount; i++) {
1699 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1700 if (err)
1701 break;
1702 }
1703 if (err) {
1704 int j;
1705
1706 pmc->sfcount[sfmode]--;
1707 for (j=0; j<i; j++)
1708 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1709 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1710#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001712 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713#endif
1714
1715 /* filter mode change */
1716 if (pmc->sfcount[MCAST_EXCLUDE])
1717 pmc->sfmode = MCAST_EXCLUDE;
1718 else if (pmc->sfcount[MCAST_INCLUDE])
1719 pmc->sfmode = MCAST_INCLUDE;
1720#ifdef CONFIG_IP_MULTICAST
1721 /* else no filters; keep old mode for reports */
1722
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001723 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 IGMP_Unsolicited_Report_Count;
1725 in_dev->mr_ifc_count = pmc->crcount;
1726 for (psf=pmc->sources; psf; psf = psf->sf_next)
1727 psf->sf_crcount = 0;
1728 igmp_ifc_event(in_dev);
1729 } else if (sf_setstate(pmc)) {
1730 igmp_ifc_event(in_dev);
1731#endif
1732 }
1733 spin_unlock_bh(&pmc->lock);
1734 return err;
1735}
1736
1737static void ip_mc_clear_src(struct ip_mc_list *pmc)
1738{
1739 struct ip_sf_list *psf, *nextpsf;
1740
1741 for (psf=pmc->tomb; psf; psf=nextpsf) {
1742 nextpsf = psf->sf_next;
1743 kfree(psf);
1744 }
1745 pmc->tomb = NULL;
1746 for (psf=pmc->sources; psf; psf=nextpsf) {
1747 nextpsf = psf->sf_next;
1748 kfree(psf);
1749 }
1750 pmc->sources = NULL;
1751 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001752 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 pmc->sfcount[MCAST_EXCLUDE] = 1;
1754}
1755
1756
1757/*
1758 * Join a multicast group
1759 */
1760int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1761{
1762 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001763 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001764 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 struct in_device *in_dev;
1766 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001767 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001768 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 int count = 0;
1770
Joe Perchesf97c1e02007-12-16 13:45:43 -08001771 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return -EINVAL;
1773
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001774 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775
Daniel Lezcano877aced2008-08-13 16:15:57 -07001776 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 if (!in_dev) {
1779 iml = NULL;
1780 err = -ENODEV;
1781 goto done;
1782 }
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001785 ifindex = imr->imr_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 for (i = inet->mc_list; i; i = i->next) {
David L Stevensca9b9072005-07-08 17:38:07 -07001787 if (i->multi.imr_multiaddr.s_addr == addr &&
1788 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 count++;
1791 }
1792 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001793 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001795 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001796 if (iml == NULL)
1797 goto done;
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 memcpy(&iml->multi, imr, sizeof(*imr));
1800 iml->next = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 iml->sflist = NULL;
1802 iml->sfmode = MCAST_EXCLUDE;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001803 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001807 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return err;
1809}
1810
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001811static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1812{
1813 struct ip_sf_socklist *psf;
1814
1815 psf = container_of(rp, struct ip_sf_socklist, rcu);
1816 /* sk_omem_alloc should have been decreased by the caller*/
1817 kfree(psf);
1818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1821 struct in_device *in_dev)
1822{
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001823 struct ip_sf_socklist *psf = iml->sflist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 int err;
1825
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001826 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /* any-source empty exclude case */
1828 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1829 iml->sfmode, 0, NULL, 0);
1830 }
1831 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001832 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1833 rcu_assign_pointer(iml->sflist, NULL);
1834 /* decrease mem now to avoid the memleak warning */
1835 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1836 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 return err;
1838}
1839
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001840
1841static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1842{
1843 struct ip_mc_socklist *iml;
1844
1845 iml = container_of(rp, struct ip_mc_socklist, rcu);
1846 /* sk_omem_alloc should have been decreased by the caller*/
1847 kfree(iml);
1848}
1849
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851/*
1852 * Ask a socket to leave a group.
1853 */
1854
1855int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1856{
1857 struct inet_sock *inet = inet_sk(sk);
1858 struct ip_mc_socklist *iml, **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001859 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001860 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001861 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001862 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001863 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001866 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001867 ifindex = imr->imr_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
David L Stevensacd6e002006-08-17 16:27:39 -07001869 if (iml->multi.imr_multiaddr.s_addr != group)
1870 continue;
1871 if (ifindex) {
1872 if (iml->multi.imr_ifindex != ifindex)
1873 continue;
1874 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1875 iml->multi.imr_address.s_addr)
1876 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
David L Stevensacd6e002006-08-17 16:27:39 -07001878 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001880 rcu_assign_pointer(*imlp, iml->next);
David L Stevensacd6e002006-08-17 16:27:39 -07001881
1882 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001883 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001884 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001885 /* decrease mem now to avoid the memleak warning */
1886 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1887 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
David L Stevensacd6e002006-08-17 16:27:39 -07001888 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
David L Stevensacd6e002006-08-17 16:27:39 -07001890 if (!in_dev)
1891 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001893 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
1896int ip_mc_source(int add, int omode, struct sock *sk, struct
1897 ip_mreq_source *mreqs, int ifindex)
1898{
1899 int err;
1900 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001901 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 struct ip_mc_socklist *pmc;
1903 struct in_device *in_dev = NULL;
1904 struct inet_sock *inet = inet_sk(sk);
1905 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001906 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001907 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 int i, j, rv;
1909
Joe Perchesf97c1e02007-12-16 13:45:43 -08001910 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return -EINVAL;
1912
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001913 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1916 imr.imr_address.s_addr = mreqs->imr_interface;
1917 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001918 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 if (!in_dev) {
1921 err = -ENODEV;
1922 goto done;
1923 }
1924 err = -EADDRNOTAVAIL;
1925
1926 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08001927 if ((pmc->multi.imr_multiaddr.s_addr ==
1928 imr.imr_multiaddr.s_addr) &&
1929 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931 }
David L Stevens917f2f12005-07-08 17:45:16 -07001932 if (!pmc) { /* must have a prior join */
1933 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 /* if a source filter was set, must be the same mode as before */
1937 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07001938 if (pmc->sfmode != omode) {
1939 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 } else if (pmc->sfmode != omode) {
1943 /* allow mode switches for empty-set filters */
1944 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001945 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 NULL, 0);
1947 pmc->sfmode = omode;
1948 }
1949
1950 psl = pmc->sflist;
1951 if (!add) {
1952 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07001953 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 rv = !0;
1955 for (i=0; i<psl->sl_count; i++) {
1956 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001957 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (rv == 0)
1959 break;
1960 }
1961 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07001962 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
David L Stevens8cdaaa12005-07-08 17:39:23 -07001964 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1965 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1966 leavegroup = 1;
1967 goto done;
1968 }
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001971 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 &mreqs->imr_sourceaddr, 1);
1973
1974 for (j=i+1; j<psl->sl_count; j++)
1975 psl->sl_addr[j-1] = psl->sl_addr[j];
1976 psl->sl_count--;
1977 err = 0;
1978 goto done;
1979 }
1980 /* else, add a new source to the filter */
1981
1982 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1983 err = -ENOBUFS;
1984 goto done;
1985 }
1986 if (!psl || psl->sl_count == psl->sl_max) {
1987 struct ip_sf_socklist *newpsl;
1988 int count = IP_SFBLOCK;
1989
1990 if (psl)
1991 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001992 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (!newpsl) {
1994 err = -ENOBUFS;
1995 goto done;
1996 }
1997 newpsl->sl_max = count;
1998 newpsl->sl_count = count - IP_SFBLOCK;
1999 if (psl) {
2000 for (i=0; i<psl->sl_count; i++)
2001 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002002 /* decrease mem now to avoid the memleak warning */
2003 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2004 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002006 rcu_assign_pointer(pmc->sflist, newpsl);
2007 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2010 for (i=0; i<psl->sl_count; i++) {
2011 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002012 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (rv == 0)
2014 break;
2015 }
2016 if (rv == 0) /* address already there is an error */
2017 goto done;
2018 for (j=psl->sl_count-1; j>=i; j--)
2019 psl->sl_addr[j+1] = psl->sl_addr[j];
2020 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2021 psl->sl_count++;
2022 err = 0;
2023 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002024 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 &mreqs->imr_sourceaddr, 1);
2026done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002027 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002028 if (leavegroup)
2029 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 return err;
2031}
2032
2033int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2034{
David L Stevens9951f032005-07-08 17:47:28 -07002035 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002037 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 struct ip_mc_socklist *pmc;
2039 struct in_device *in_dev;
2040 struct inet_sock *inet = inet_sk(sk);
2041 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002042 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002043 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Joe Perchesf97c1e02007-12-16 13:45:43 -08002045 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return -EINVAL;
2047 if (msf->imsf_fmode != MCAST_INCLUDE &&
2048 msf->imsf_fmode != MCAST_EXCLUDE)
2049 return -EINVAL;
2050
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002051 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
2053 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2054 imr.imr_address.s_addr = msf->imsf_interface;
2055 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002056 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 if (!in_dev) {
2059 err = -ENODEV;
2060 goto done;
2061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
David L Stevens9951f032005-07-08 17:47:28 -07002063 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2064 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2065 leavegroup = 1;
2066 goto done;
2067 }
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2070 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2071 pmc->multi.imr_ifindex == imr.imr_ifindex)
2072 break;
2073 }
David L Stevens917f2f12005-07-08 17:45:16 -07002074 if (!pmc) { /* must have a prior join */
2075 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002079 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2080 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (!newpsl) {
2082 err = -ENOBUFS;
2083 goto done;
2084 }
2085 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2086 memcpy(newpsl->sl_addr, msf->imsf_slist,
2087 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2088 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2089 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2090 if (err) {
2091 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2092 goto done;
2093 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002094 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002096 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2097 msf->imsf_fmode, 0, NULL, 0);
2098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 psl = pmc->sflist;
2100 if (psl) {
2101 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2102 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002103 /* decrease mem now to avoid the memleak warning */
2104 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2105 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 } else
2107 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2108 0, NULL, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002109 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002111 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002113 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002114 if (leavegroup)
2115 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 return err;
2117}
2118
2119int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2120 struct ip_msfilter __user *optval, int __user *optlen)
2121{
2122 int err, len, count, copycount;
2123 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002124 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 struct ip_mc_socklist *pmc;
2126 struct in_device *in_dev;
2127 struct inet_sock *inet = inet_sk(sk);
2128 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002129 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Joe Perchesf97c1e02007-12-16 13:45:43 -08002131 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return -EINVAL;
2133
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002134 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
2136 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2137 imr.imr_address.s_addr = msf->imsf_interface;
2138 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002139 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (!in_dev) {
2142 err = -ENODEV;
2143 goto done;
2144 }
2145 err = -EADDRNOTAVAIL;
2146
2147 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2148 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2149 pmc->multi.imr_ifindex == imr.imr_ifindex)
2150 break;
2151 }
2152 if (!pmc) /* must have a prior join */
2153 goto done;
2154 msf->imsf_fmode = pmc->sfmode;
2155 psl = pmc->sflist;
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002156 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (!psl) {
2158 len = 0;
2159 count = 0;
2160 } else {
2161 count = psl->sl_count;
2162 }
2163 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2164 len = copycount * sizeof(psl->sl_addr[0]);
2165 msf->imsf_numsrc = count;
2166 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2167 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2168 return -EFAULT;
2169 }
2170 if (len &&
2171 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2172 return -EFAULT;
2173 return 0;
2174done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002175 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return err;
2177}
2178
2179int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2180 struct group_filter __user *optval, int __user *optlen)
2181{
2182 int err, i, count, copycount;
2183 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002184 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 struct ip_mc_socklist *pmc;
2186 struct inet_sock *inet = inet_sk(sk);
2187 struct ip_sf_socklist *psl;
2188
2189 psin = (struct sockaddr_in *)&gsf->gf_group;
2190 if (psin->sin_family != AF_INET)
2191 return -EINVAL;
2192 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002193 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return -EINVAL;
2195
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002196 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 err = -EADDRNOTAVAIL;
2199
2200 for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2201 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2202 pmc->multi.imr_ifindex == gsf->gf_interface)
2203 break;
2204 }
2205 if (!pmc) /* must have a prior join */
2206 goto done;
2207 gsf->gf_fmode = pmc->sfmode;
2208 psl = pmc->sflist;
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002209 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 count = psl ? psl->sl_count : 0;
2211 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2212 gsf->gf_numsrc = count;
2213 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2214 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2215 return -EFAULT;
2216 }
2217 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct sockaddr_storage ss;
2219
2220 psin = (struct sockaddr_in *)&ss;
2221 memset(&ss, 0, sizeof(ss));
2222 psin->sin_family = AF_INET;
2223 psin->sin_addr.s_addr = psl->sl_addr[i];
2224 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2225 return -EFAULT;
2226 }
2227 return 0;
2228done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002229 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return err;
2231}
2232
2233/*
2234 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2235 */
Al Viroc0cda062006-09-27 18:31:10 -07002236int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
2238 struct inet_sock *inet = inet_sk(sk);
2239 struct ip_mc_socklist *pmc;
2240 struct ip_sf_socklist *psl;
2241 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002242 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002244 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002245 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002248 rcu_read_lock();
2249 for (pmc=rcu_dereference(inet->mc_list); pmc; pmc=rcu_dereference(pmc->next)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2251 pmc->multi.imr_ifindex == dif)
2252 break;
2253 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002254 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002256 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 psl = pmc->sflist;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002258 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002260 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
2262 for (i=0; i<psl->sl_count; i++) {
2263 if (psl->sl_addr[i] == rmt_addr)
2264 break;
2265 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002266 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002268 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002270 goto unlock;
2271 ret = 1;
2272unlock:
2273 rcu_read_unlock();
2274out:
2275 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278/*
2279 * A socket is closing.
2280 */
2281
2282void ip_mc_drop_socket(struct sock *sk)
2283{
2284 struct inet_sock *inet = inet_sk(sk);
2285 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002286 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
2288 if (inet->mc_list == NULL)
2289 return;
2290
2291 rtnl_lock();
2292 while ((iml = inet->mc_list) != NULL) {
2293 struct in_device *in_dev;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002294 rcu_assign_pointer(inet->mc_list, iml->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Daniel Lezcano877aced2008-08-13 16:15:57 -07002296 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002297 (void) ip_mc_leave_src(sk, iml, in_dev);
2298 if (in_dev != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2300 in_dev_put(in_dev);
2301 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002302 /* decrease mem now to avoid the memleak warning */
2303 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2304 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306 rtnl_unlock();
2307}
2308
Al Viroa60c4922006-09-26 21:28:34 -07002309int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
2311 struct ip_mc_list *im;
2312 struct ip_sf_list *psf;
2313 int rv = 0;
2314
2315 read_lock(&in_dev->mc_list_lock);
2316 for (im=in_dev->mc_list; im; im=im->next) {
2317 if (im->multiaddr == mc_addr)
2318 break;
2319 }
2320 if (im && proto == IPPROTO_IGMP) {
2321 rv = 1;
2322 } else if (im) {
2323 if (src_addr) {
2324 for (psf=im->sources; psf; psf=psf->sf_next) {
2325 if (psf->sf_inaddr == src_addr)
2326 break;
2327 }
2328 if (psf)
2329 rv = psf->sf_count[MCAST_INCLUDE] ||
2330 psf->sf_count[MCAST_EXCLUDE] !=
2331 im->sfcount[MCAST_EXCLUDE];
2332 else
2333 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2334 } else
2335 rv = 1; /* unspecified source; tentatively allow */
2336 }
2337 read_unlock(&in_dev->mc_list_lock);
2338 return rv;
2339}
2340
2341#if defined(CONFIG_PROC_FS)
2342struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002343 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 struct net_device *dev;
2345 struct in_device *in_dev;
2346};
2347
2348#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2349
2350static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2351{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002352 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 struct ip_mc_list *im = NULL;
2354 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2355
Pavel Emelianov7562f872007-05-03 15:13:45 -07002356 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002357 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002359
2360 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (!in_dev)
2362 continue;
2363 read_lock(&in_dev->mc_list_lock);
2364 im = in_dev->mc_list;
2365 if (im) {
2366 state->in_dev = in_dev;
2367 break;
2368 }
2369 read_unlock(&in_dev->mc_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 return im;
2372}
2373
2374static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2375{
2376 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2377 im = im->next;
2378 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002379 if (likely(state->in_dev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 read_unlock(&state->in_dev->mc_list_lock);
Eric Dumazet6baff152009-11-11 17:48:52 +00002381
2382 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (!state->dev) {
2384 state->in_dev = NULL;
2385 break;
2386 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002387 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (!state->in_dev)
2389 continue;
2390 read_lock(&state->in_dev->mc_list_lock);
2391 im = state->in_dev->mc_list;
2392 }
2393 return im;
2394}
2395
2396static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2397{
2398 struct ip_mc_list *im = igmp_mc_get_first(seq);
2399 if (im)
2400 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2401 --pos;
2402 return pos ? NULL : im;
2403}
2404
2405static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002406 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
stephen hemminger61fbab72009-11-10 07:54:55 +00002408 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2410}
2411
2412static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2413{
2414 struct ip_mc_list *im;
2415 if (v == SEQ_START_TOKEN)
2416 im = igmp_mc_get_first(seq);
2417 else
2418 im = igmp_mc_get_next(seq, v);
2419 ++*pos;
2420 return im;
2421}
2422
2423static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002424 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
2426 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2427 if (likely(state->in_dev != NULL)) {
2428 read_unlock(&state->in_dev->mc_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 state->in_dev = NULL;
2430 }
2431 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002432 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
2435static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2436{
2437 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002438 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2440 else {
2441 struct ip_mc_list *im = (struct ip_mc_list *)v;
2442 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2443 char *querier;
2444#ifdef CONFIG_IP_MULTICAST
2445 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2446 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2447 "V3";
2448#else
2449 querier = "NONE";
2450#endif
2451
2452 if (state->in_dev->mc_list == im) {
2453 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002454 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 }
2456
2457 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002458 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 im->multiaddr, im->users,
2460 im->tm_running, im->tm_running ?
2461 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2462 im->reporter);
2463 }
2464 return 0;
2465}
2466
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002467static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 .start = igmp_mc_seq_start,
2469 .next = igmp_mc_seq_next,
2470 .stop = igmp_mc_seq_stop,
2471 .show = igmp_mc_seq_show,
2472};
2473
2474static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2475{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002476 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002477 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478}
2479
Arjan van de Ven9a321442007-02-12 00:55:35 -08002480static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 .owner = THIS_MODULE,
2482 .open = igmp_mc_seq_open,
2483 .read = seq_read,
2484 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002485 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486};
2487
2488struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002489 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 struct net_device *dev;
2491 struct in_device *idev;
2492 struct ip_mc_list *im;
2493};
2494
2495#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2496
2497static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2498{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002499 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 struct ip_sf_list *psf = NULL;
2501 struct ip_mc_list *im = NULL;
2502 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2503
Pavel Emelianov7562f872007-05-03 15:13:45 -07002504 state->idev = NULL;
2505 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002506 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002508 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (unlikely(idev == NULL))
2510 continue;
2511 read_lock(&idev->mc_list_lock);
2512 im = idev->mc_list;
2513 if (likely(im != NULL)) {
2514 spin_lock_bh(&im->lock);
2515 psf = im->sources;
2516 if (likely(psf != NULL)) {
2517 state->im = im;
2518 state->idev = idev;
2519 break;
2520 }
2521 spin_unlock_bh(&im->lock);
2522 }
2523 read_unlock(&idev->mc_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525 return psf;
2526}
2527
2528static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2529{
2530 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2531
2532 psf = psf->sf_next;
2533 while (!psf) {
2534 spin_unlock_bh(&state->im->lock);
2535 state->im = state->im->next;
2536 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002537 if (likely(state->idev != NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 read_unlock(&state->idev->mc_list_lock);
Eric Dumazet6baff152009-11-11 17:48:52 +00002539
2540 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 if (!state->dev) {
2542 state->idev = NULL;
2543 goto out;
2544 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002545 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if (!state->idev)
2547 continue;
2548 read_lock(&state->idev->mc_list_lock);
2549 state->im = state->idev->mc_list;
2550 }
2551 if (!state->im)
2552 break;
2553 spin_lock_bh(&state->im->lock);
2554 psf = state->im->sources;
2555 }
2556out:
2557 return psf;
2558}
2559
2560static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2561{
2562 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2563 if (psf)
2564 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2565 --pos;
2566 return pos ? NULL : psf;
2567}
2568
2569static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002570 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
stephen hemminger61fbab72009-11-10 07:54:55 +00002572 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2574}
2575
2576static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2577{
2578 struct ip_sf_list *psf;
2579 if (v == SEQ_START_TOKEN)
2580 psf = igmp_mcf_get_first(seq);
2581 else
2582 psf = igmp_mcf_get_next(seq, v);
2583 ++*pos;
2584 return psf;
2585}
2586
2587static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002588 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2591 if (likely(state->im != NULL)) {
2592 spin_unlock_bh(&state->im->lock);
2593 state->im = NULL;
2594 }
2595 if (likely(state->idev != NULL)) {
2596 read_unlock(&state->idev->mc_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 state->idev = NULL;
2598 }
2599 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002600 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601}
2602
2603static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2604{
2605 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2606 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2607
2608 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002609 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 "%3s %6s "
2611 "%10s %10s %6s %6s\n", "Idx",
2612 "Device", "MCA",
2613 "SRC", "INC", "EXC");
2614 } else {
2615 seq_printf(seq,
2616 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002617 "0x%08x %6lu %6lu\n",
2618 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 ntohl(state->im->multiaddr),
2620 ntohl(psf->sf_inaddr),
2621 psf->sf_count[MCAST_INCLUDE],
2622 psf->sf_count[MCAST_EXCLUDE]);
2623 }
2624 return 0;
2625}
2626
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002627static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 .start = igmp_mcf_seq_start,
2629 .next = igmp_mcf_seq_next,
2630 .stop = igmp_mcf_seq_stop,
2631 .show = igmp_mcf_seq_show,
2632};
2633
2634static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2635{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002636 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002637 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Arjan van de Ven9a321442007-02-12 00:55:35 -08002640static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 .owner = THIS_MODULE,
2642 .open = igmp_mcf_seq_open,
2643 .read = seq_read,
2644 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002645 .release = seq_release_net,
2646};
2647
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002648static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002649{
2650 struct proc_dir_entry *pde;
2651
2652 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2653 if (!pde)
2654 goto out_igmp;
2655 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2656 if (!pde)
2657 goto out_mcfilter;
2658 return 0;
2659
2660out_mcfilter:
2661 proc_net_remove(net, "igmp");
2662out_igmp:
2663 return -ENOMEM;
2664}
2665
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002666static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002667{
2668 proc_net_remove(net, "mcfilter");
2669 proc_net_remove(net, "igmp");
2670}
2671
2672static struct pernet_operations igmp_net_ops = {
2673 .init = igmp_net_init,
2674 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675};
2676
2677int __init igmp_mc_proc_init(void)
2678{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002679 return register_pernet_subsys(&igmp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680}
2681#endif
2682
2683EXPORT_SYMBOL(ip_mc_dec_group);
2684EXPORT_SYMBOL(ip_mc_inc_group);
2685EXPORT_SYMBOL(ip_mc_join_group);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08002686EXPORT_SYMBOL(ip_mc_rejoin_group);