blob: a1bf2f49e71662251f2303a61cdb007c5ffee49c [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
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000152
153static void ip_mc_list_reclaim(struct rcu_head *head)
154{
155 kfree(container_of(head, struct ip_mc_list, rcu));
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void ip_ma_put(struct ip_mc_list *im)
159{
160 if (atomic_dec_and_test(&im->refcnt)) {
161 in_dev_put(im->interface);
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000162 call_rcu(&im->rcu, ip_mc_list_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164}
165
David S. Millerd9aa9382010-11-15 08:52:02 -0800166#define for_each_pmc_rcu(in_dev, pmc) \
167 for (pmc = rcu_dereference(in_dev->mc_list); \
168 pmc != NULL; \
169 pmc = rcu_dereference(pmc->next_rcu))
170
171#define for_each_pmc_rtnl(in_dev, pmc) \
172 for (pmc = rtnl_dereference(in_dev->mc_list); \
173 pmc != NULL; \
174 pmc = rtnl_dereference(pmc->next_rcu))
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#ifdef CONFIG_IP_MULTICAST
177
178/*
179 * Timer management
180 */
181
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000182static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 spin_lock_bh(&im->lock);
185 if (del_timer(&im->timer))
186 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800187 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 im->reporter = 0;
189 im->unsolicit_count = 0;
190 spin_unlock_bh(&im->lock);
191}
192
193/* It must be called with locked im->lock */
194static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
195{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800196 int tv = net_random() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800198 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!mod_timer(&im->timer, jiffies+tv+2))
200 atomic_inc(&im->refcnt);
201}
202
203static void igmp_gq_start_timer(struct in_device *in_dev)
204{
205 int tv = net_random() % in_dev->mr_maxdelay;
206
207 in_dev->mr_gq_running = 1;
208 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
209 in_dev_hold(in_dev);
210}
211
212static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
213{
214 int tv = net_random() % delay;
215
216 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
217 in_dev_hold(in_dev);
218}
219
220static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
221{
222 spin_lock_bh(&im->lock);
223 im->unsolicit_count = 0;
224 if (del_timer(&im->timer)) {
225 if ((long)(im->timer.expires-jiffies) < max_delay) {
226 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800227 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spin_unlock_bh(&im->lock);
229 return;
230 }
231 atomic_dec(&im->refcnt);
232 }
233 igmp_start_timer(im, max_delay);
234 spin_unlock_bh(&im->lock);
235}
236
237
238/*
239 * Send an IGMP report.
240 */
241
242#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
243
244
245static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
246 int gdeleted, int sdeleted)
247{
248 switch (type) {
249 case IGMPV3_MODE_IS_INCLUDE:
250 case IGMPV3_MODE_IS_EXCLUDE:
251 if (gdeleted || sdeleted)
252 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800253 if (!(pmc->gsquery && !psf->sf_gsresp)) {
254 if (pmc->sfmode == MCAST_INCLUDE)
255 return 1;
256 /* don't include if this source is excluded
257 * in all filters
258 */
259 if (psf->sf_count[MCAST_INCLUDE])
260 return type == IGMPV3_MODE_IS_INCLUDE;
261 return pmc->sfcount[MCAST_EXCLUDE] ==
262 psf->sf_count[MCAST_EXCLUDE];
263 }
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 case IGMPV3_CHANGE_TO_INCLUDE:
266 if (gdeleted || sdeleted)
267 return 0;
268 return psf->sf_count[MCAST_INCLUDE] != 0;
269 case IGMPV3_CHANGE_TO_EXCLUDE:
270 if (gdeleted || sdeleted)
271 return 0;
272 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
273 psf->sf_count[MCAST_INCLUDE])
274 return 0;
275 return pmc->sfcount[MCAST_EXCLUDE] ==
276 psf->sf_count[MCAST_EXCLUDE];
277 case IGMPV3_ALLOW_NEW_SOURCES:
278 if (gdeleted || !psf->sf_crcount)
279 return 0;
280 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
281 case IGMPV3_BLOCK_OLD_SOURCES:
282 if (pmc->sfmode == MCAST_INCLUDE)
283 return gdeleted || (psf->sf_crcount && sdeleted);
284 return psf->sf_crcount && !gdeleted && !sdeleted;
285 }
286 return 0;
287}
288
289static int
290igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
291{
292 struct ip_sf_list *psf;
293 int scount = 0;
294
295 for (psf=pmc->sources; psf; psf=psf->sf_next) {
296 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
297 continue;
298 scount++;
299 }
300 return scount;
301}
302
303static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
304{
305 struct sk_buff *skb;
306 struct rtable *rt;
307 struct iphdr *pip;
308 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700309 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Johannes Bergf5184d22008-05-12 20:48:31 -0700311 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (skb == NULL)
313 return NULL;
314
315 {
316 struct flowi fl = { .oif = dev->ifindex,
317 .nl_u = { .ip4_u = {
318 .daddr = IGMPV3_ALL_MCR } },
319 .proto = IPPROTO_IGMP };
Daniel Lezcano877aced2008-08-13 16:15:57 -0700320 if (ip_route_output_key(net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 kfree_skb(skb);
322 return NULL;
323 }
324 }
325 if (rt->rt_src == 0) {
326 kfree_skb(skb);
327 ip_rt_put(rt);
328 return NULL;
329 }
330
Changli Gaod8d1f302010-06-10 23:31:35 -0700331 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 skb->dev = dev;
333
334 skb_reserve(skb, LL_RESERVED_SPACE(dev));
335
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300336 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700337 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300338 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 pip->version = 4;
341 pip->ihl = (sizeof(struct iphdr)+4)>>2;
342 pip->tos = 0xc0;
343 pip->frag_off = htons(IP_DF);
344 pip->ttl = 1;
345 pip->daddr = rt->rt_dst;
346 pip->saddr = rt->rt_src;
347 pip->protocol = IPPROTO_IGMP;
348 pip->tot_len = 0; /* filled in later */
Changli Gaod8d1f302010-06-10 23:31:35 -0700349 ip_select_ident(pip, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 ((u8*)&pip[1])[0] = IPOPT_RA;
351 ((u8*)&pip[1])[1] = 4;
352 ((u8*)&pip[1])[2] = 0;
353 ((u8*)&pip[1])[3] = 0;
354
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700355 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300356 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300357 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
359 pig->resv1 = 0;
360 pig->csum = 0;
361 pig->resv2 = 0;
362 pig->ngrec = 0;
363 return skb;
364}
365
366static int igmpv3_sendpack(struct sk_buff *skb)
367{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300368 struct igmphdr *pig = igmp_hdr(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700369 const int igmplen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300371 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Herbert Xuc439cb22008-01-11 19:14:00 -0800373 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
377{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800378 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
382 int type, struct igmpv3_grec **ppgr)
383{
384 struct net_device *dev = pmc->interface->dev;
385 struct igmpv3_report *pih;
386 struct igmpv3_grec *pgr;
387
388 if (!skb)
389 skb = igmpv3_newpack(dev, dev->mtu);
390 if (!skb)
391 return NULL;
392 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
393 pgr->grec_type = type;
394 pgr->grec_auxwords = 0;
395 pgr->grec_nsrcs = 0;
396 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300397 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 pih->ngrec = htons(ntohs(pih->ngrec)+1);
399 *ppgr = pgr;
400 return skb;
401}
402
403#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
404 skb_tailroom(skb)) : 0)
405
406static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
407 int type, int gdeleted, int sdeleted)
408{
409 struct net_device *dev = pmc->interface->dev;
410 struct igmpv3_report *pih;
411 struct igmpv3_grec *pgr = NULL;
412 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800413 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 if (pmc->multiaddr == IGMP_ALL_HOSTS)
416 return skb;
417
418 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
419 type == IGMPV3_MODE_IS_EXCLUDE;
420 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
421 type == IGMPV3_CHANGE_TO_EXCLUDE;
422
David L Stevensad125832006-01-18 14:20:56 -0800423 stotal = scount = 0;
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
426
David L Stevensad125832006-01-18 14:20:56 -0800427 if (!*psf_list)
428 goto empty_source;
429
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300430 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* EX and TO_EX get a fresh packet, if needed */
433 if (truncate) {
434 if (pih && pih->ngrec &&
435 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
436 if (skb)
437 igmpv3_sendpack(skb);
438 skb = igmpv3_newpack(dev, dev->mtu);
439 }
440 }
441 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 psf_prev = NULL;
443 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700444 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 psf_next = psf->sf_next;
447
448 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
449 psf_prev = psf;
450 continue;
451 }
452
453 /* clear marks on query responses */
454 if (isquery)
455 psf->sf_gsresp = 0;
456
Al Viro63007722006-09-27 18:31:32 -0700457 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 first*sizeof(struct igmpv3_grec)) {
459 if (truncate && !first)
460 break; /* truncate these */
461 if (pgr)
462 pgr->grec_nsrcs = htons(scount);
463 if (skb)
464 igmpv3_sendpack(skb);
465 skb = igmpv3_newpack(dev, dev->mtu);
466 first = 1;
467 scount = 0;
468 }
469 if (first) {
470 skb = add_grhead(skb, pmc, type, &pgr);
471 first = 0;
472 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800473 if (!skb)
474 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700475 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800477 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
479 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
480 psf->sf_crcount--;
481 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
482 if (psf_prev)
483 psf_prev->sf_next = psf->sf_next;
484 else
485 *psf_list = psf->sf_next;
486 kfree(psf);
487 continue;
488 }
489 }
490 psf_prev = psf;
491 }
David L Stevensad125832006-01-18 14:20:56 -0800492
493empty_source:
494 if (!stotal) {
495 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
496 type == IGMPV3_BLOCK_OLD_SOURCES)
497 return skb;
498 if (pmc->crcount || isquery) {
499 /* make sure we have room for group header */
500 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
501 igmpv3_sendpack(skb);
502 skb = NULL; /* add_grhead will get a new one */
503 }
504 skb = add_grhead(skb, pmc, type, &pgr);
505 }
506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (pgr)
508 pgr->grec_nsrcs = htons(scount);
509
510 if (isquery)
511 pmc->gsquery = 0; /* clear query state on report */
512 return skb;
513}
514
515static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
516{
517 struct sk_buff *skb = NULL;
518 int type;
519
520 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000521 rcu_read_lock();
522 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (pmc->multiaddr == IGMP_ALL_HOSTS)
524 continue;
525 spin_lock_bh(&pmc->lock);
526 if (pmc->sfcount[MCAST_EXCLUDE])
527 type = IGMPV3_MODE_IS_EXCLUDE;
528 else
529 type = IGMPV3_MODE_IS_INCLUDE;
530 skb = add_grec(skb, pmc, type, 0, 0);
531 spin_unlock_bh(&pmc->lock);
532 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000533 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 } else {
535 spin_lock_bh(&pmc->lock);
536 if (pmc->sfcount[MCAST_EXCLUDE])
537 type = IGMPV3_MODE_IS_EXCLUDE;
538 else
539 type = IGMPV3_MODE_IS_INCLUDE;
540 skb = add_grec(skb, pmc, type, 0, 0);
541 spin_unlock_bh(&pmc->lock);
542 }
543 if (!skb)
544 return 0;
545 return igmpv3_sendpack(skb);
546}
547
548/*
549 * remove zero-count source records from a source filter list
550 */
551static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
552{
553 struct ip_sf_list *psf_prev, *psf_next, *psf;
554
555 psf_prev = NULL;
556 for (psf=*ppsf; psf; psf = psf_next) {
557 psf_next = psf->sf_next;
558 if (psf->sf_crcount == 0) {
559 if (psf_prev)
560 psf_prev->sf_next = psf->sf_next;
561 else
562 *ppsf = psf->sf_next;
563 kfree(psf);
564 } else
565 psf_prev = psf;
566 }
567}
568
569static void igmpv3_send_cr(struct in_device *in_dev)
570{
571 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
572 struct sk_buff *skb = NULL;
573 int type, dtype;
574
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000575 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 spin_lock_bh(&in_dev->mc_tomb_lock);
577
578 /* deleted MCA's */
579 pmc_prev = NULL;
580 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
581 pmc_next = pmc->next;
582 if (pmc->sfmode == MCAST_INCLUDE) {
583 type = IGMPV3_BLOCK_OLD_SOURCES;
584 dtype = IGMPV3_BLOCK_OLD_SOURCES;
585 skb = add_grec(skb, pmc, type, 1, 0);
586 skb = add_grec(skb, pmc, dtype, 1, 1);
587 }
588 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (pmc->sfmode == MCAST_EXCLUDE) {
590 type = IGMPV3_CHANGE_TO_INCLUDE;
591 skb = add_grec(skb, pmc, type, 1, 0);
592 }
David L Stevensad125832006-01-18 14:20:56 -0800593 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (pmc->crcount == 0) {
595 igmpv3_clear_zeros(&pmc->tomb);
596 igmpv3_clear_zeros(&pmc->sources);
597 }
598 }
599 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
600 if (pmc_prev)
601 pmc_prev->next = pmc_next;
602 else
603 in_dev->mc_tomb = pmc_next;
604 in_dev_put(pmc->interface);
605 kfree(pmc);
606 } else
607 pmc_prev = pmc;
608 }
609 spin_unlock_bh(&in_dev->mc_tomb_lock);
610
611 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000612 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 spin_lock_bh(&pmc->lock);
614 if (pmc->sfcount[MCAST_EXCLUDE]) {
615 type = IGMPV3_BLOCK_OLD_SOURCES;
616 dtype = IGMPV3_ALLOW_NEW_SOURCES;
617 } else {
618 type = IGMPV3_ALLOW_NEW_SOURCES;
619 dtype = IGMPV3_BLOCK_OLD_SOURCES;
620 }
621 skb = add_grec(skb, pmc, type, 0, 0);
622 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
623
624 /* filter mode changes */
625 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (pmc->sfmode == MCAST_EXCLUDE)
627 type = IGMPV3_CHANGE_TO_EXCLUDE;
628 else
629 type = IGMPV3_CHANGE_TO_INCLUDE;
630 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800631 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633 spin_unlock_bh(&pmc->lock);
634 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000635 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (!skb)
638 return;
639 (void) igmpv3_sendpack(skb);
640}
641
642static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
643 int type)
644{
645 struct sk_buff *skb;
646 struct iphdr *iph;
647 struct igmphdr *ih;
648 struct rtable *rt;
649 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700650 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700651 __be32 group = pmc ? pmc->multiaddr : 0;
652 __be32 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
655 return igmpv3_send_report(in_dev, pmc);
656 else if (type == IGMP_HOST_LEAVE_MESSAGE)
657 dst = IGMP_ALL_ROUTER;
658 else
659 dst = group;
660
661 {
662 struct flowi fl = { .oif = dev->ifindex,
663 .nl_u = { .ip4_u = { .daddr = dst } },
664 .proto = IPPROTO_IGMP };
Daniel Lezcano877aced2008-08-13 16:15:57 -0700665 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return -1;
667 }
668 if (rt->rt_src == 0) {
669 ip_rt_put(rt);
670 return -1;
671 }
672
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800673 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (skb == NULL) {
675 ip_rt_put(rt);
676 return -1;
677 }
678
Changli Gaod8d1f302010-06-10 23:31:35 -0700679 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 skb_reserve(skb, LL_RESERVED_SPACE(dev));
682
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300683 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700684 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300685 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 iph->version = 4;
688 iph->ihl = (sizeof(struct iphdr)+4)>>2;
689 iph->tos = 0xc0;
690 iph->frag_off = htons(IP_DF);
691 iph->ttl = 1;
692 iph->daddr = dst;
693 iph->saddr = rt->rt_src;
694 iph->protocol = IPPROTO_IGMP;
Changli Gaod8d1f302010-06-10 23:31:35 -0700695 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 ((u8*)&iph[1])[0] = IPOPT_RA;
697 ((u8*)&iph[1])[1] = 4;
698 ((u8*)&iph[1])[2] = 0;
699 ((u8*)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800702 ih->type = type;
703 ih->code = 0;
704 ih->csum = 0;
705 ih->group = group;
706 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Herbert Xuc439cb22008-01-11 19:14:00 -0800708 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static void igmp_gq_timer_expire(unsigned long data)
712{
713 struct in_device *in_dev = (struct in_device *)data;
714
715 in_dev->mr_gq_running = 0;
716 igmpv3_send_report(in_dev, NULL);
717 __in_dev_put(in_dev);
718}
719
720static void igmp_ifc_timer_expire(unsigned long data)
721{
722 struct in_device *in_dev = (struct in_device *)data;
723
724 igmpv3_send_cr(in_dev);
725 if (in_dev->mr_ifc_count) {
726 in_dev->mr_ifc_count--;
727 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
728 }
729 __in_dev_put(in_dev);
730}
731
732static void igmp_ifc_event(struct in_device *in_dev)
733{
734 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
735 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900736 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 IGMP_Unsolicited_Report_Count;
738 igmp_ifc_start_timer(in_dev, 1);
739}
740
741
742static void igmp_timer_expire(unsigned long data)
743{
744 struct ip_mc_list *im=(struct ip_mc_list *)data;
745 struct in_device *in_dev = im->interface;
746
747 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800748 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (im->unsolicit_count) {
751 im->unsolicit_count--;
752 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
753 }
754 im->reporter = 1;
755 spin_unlock(&im->lock);
756
757 if (IGMP_V1_SEEN(in_dev))
758 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
759 else if (IGMP_V2_SEEN(in_dev))
760 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
761 else
762 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
763
764 ip_ma_put(im);
765}
766
David L Stevensad125832006-01-18 14:20:56 -0800767/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700768static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct ip_sf_list *psf;
771 int i, scount;
772
773 scount = 0;
774 for (psf=pmc->sources; psf; psf=psf->sf_next) {
775 if (scount == nsrcs)
776 break;
David L Stevensad125832006-01-18 14:20:56 -0800777 for (i=0; i<nsrcs; i++) {
778 /* skip inactive filters */
779 if (pmc->sfcount[MCAST_INCLUDE] ||
780 pmc->sfcount[MCAST_EXCLUDE] !=
781 psf->sf_count[MCAST_EXCLUDE])
782 continue;
783 if (srcs[i] == psf->sf_inaddr) {
784 scount++;
785 break;
786 }
787 }
788 }
789 pmc->gsquery = 0;
790 if (scount == nsrcs) /* all sources excluded */
791 return 0;
792 return 1;
793}
794
Al Viroea4d9e72006-09-27 18:30:52 -0700795static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800796{
797 struct ip_sf_list *psf;
798 int i, scount;
799
800 if (pmc->sfmode == MCAST_EXCLUDE)
801 return igmp_xmarksources(pmc, nsrcs, srcs);
802
803 /* mark INCLUDE-mode sources */
804 scount = 0;
805 for (psf=pmc->sources; psf; psf=psf->sf_next) {
806 if (scount == nsrcs)
807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 for (i=0; i<nsrcs; i++)
809 if (srcs[i] == psf->sf_inaddr) {
810 psf->sf_gsresp = 1;
811 scount++;
812 break;
813 }
814 }
David L Stevensad125832006-01-18 14:20:56 -0800815 if (!scount) {
816 pmc->gsquery = 0;
817 return 0;
818 }
819 pmc->gsquery = 1;
820 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
Al Viro63007722006-09-27 18:31:32 -0700823static void igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct ip_mc_list *im;
826
827 /* Timers are only set for non-local groups */
828
829 if (group == IGMP_ALL_HOSTS)
830 return;
831
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000832 rcu_read_lock();
833 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (im->multiaddr == group) {
835 igmp_stop_timer(im);
836 break;
837 }
838 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000839 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
842static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
843 int len)
844{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300845 struct igmphdr *ih = igmp_hdr(skb);
846 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700848 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 int max_delay;
850 int mark = 0;
851
852
David Stevens5b7c8402010-09-30 14:29:40 +0000853 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (ih->code == 0) {
855 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 max_delay = IGMP_Query_Response_Interval;
858 in_dev->mr_v1_seen = jiffies +
859 IGMP_V1_Router_Present_Timeout;
860 group = 0;
861 } else {
862 /* v2 router present */
863 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
864 in_dev->mr_v2_seen = jiffies +
865 IGMP_V2_Router_Present_Timeout;
866 }
867 /* cancel the interface change timer */
868 in_dev->mr_ifc_count = 0;
869 if (del_timer(&in_dev->mr_ifc_timer))
870 __in_dev_put(in_dev);
871 /* clear deleted report items */
872 igmpv3_clear_delrec(in_dev);
873 } else if (len < 12) {
874 return; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000875 } else if (IGMP_V1_SEEN(in_dev)) {
876 /* This is a v3 query with v1 queriers present */
877 max_delay = IGMP_Query_Response_Interval;
878 group = 0;
879 } else if (IGMP_V2_SEEN(in_dev)) {
880 /* this is a v3 query with v2 queriers present;
881 * Interpretation of the max_delay code is problematic here.
882 * A real v2 host would use ih_code directly, while v3 has a
883 * different encoding. We use the v3 encoding as more likely
884 * to be intended in a v3 query.
885 */
886 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } else { /* v3 */
888 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
889 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900890
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300891 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900893 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700894 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 return;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300896 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
899 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
900 if (!max_delay)
901 max_delay = 1; /* can't mod w/ 0 */
902 in_dev->mr_maxdelay = max_delay;
903 if (ih3->qrv)
904 in_dev->mr_qrv = ih3->qrv;
905 if (!group) { /* general query */
906 if (ih3->nsrcs)
907 return; /* no sources allowed */
908 igmp_gq_start_timer(in_dev);
909 return;
910 }
911 /* mark sources to include, if group & source-specific */
912 mark = ih3->nsrcs != 0;
913 }
914
915 /*
916 * - Start the timers in all of our membership records
917 * that the query applies to for the interface on
918 * which the query arrived excl. those that belong
919 * to a "local" group (224.0.0.X)
920 * - For timers already running check if they need to
921 * be reset.
922 * - Use the igmp->igmp_code field as the maximum
923 * delay possible
924 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000925 rcu_read_lock();
926 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800927 int changed;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (group && group != im->multiaddr)
930 continue;
931 if (im->multiaddr == IGMP_ALL_HOSTS)
932 continue;
933 spin_lock_bh(&im->lock);
934 if (im->tm_running)
935 im->gsquery = im->gsquery && mark;
936 else
937 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800938 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900939 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800941 if (changed)
942 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000944 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000947/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948int igmp_rcv(struct sk_buff *skb)
949{
950 /* This basically follows the spec line by line -- see RFC1112 */
951 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000952 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 int len = skb->len;
954
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800955 if (in_dev == NULL)
956 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Herbert Xufb286bb2005-11-10 13:01:24 -0800958 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000959 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800960
961 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700962 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800963 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800964 break;
965 /* fall through */
966 case CHECKSUM_NONE:
967 skb->csum = 0;
968 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000969 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300972 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 switch (ih->type) {
974 case IGMP_HOST_MEMBERSHIP_QUERY:
975 igmp_heard_query(in_dev, skb, len);
976 break;
977 case IGMP_HOST_MEMBERSHIP_REPORT:
978 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -0800980 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
David Stevens24c69272005-12-02 20:32:59 -0800982 /* don't rely on MC router hearing unicast reports */
983 if (skb->pkt_type == PACKET_MULTICAST ||
984 skb->pkt_type == PACKET_BROADCAST)
985 igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 break;
987 case IGMP_PIM:
988#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return pim_rcv_v1(skb);
990#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +0000991 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 case IGMP_DVMRP:
993 case IGMP_TRACE:
994 case IGMP_HOST_LEAVE_MESSAGE:
995 case IGMP_MTRACE:
996 case IGMP_MTRACE_RESP:
997 break;
998 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -0800999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001001
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001002drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 kfree_skb(skb);
1004 return 0;
1005}
1006
1007#endif
1008
1009
1010/*
1011 * Add a filter to a device
1012 */
1013
Al Viro63007722006-09-27 18:31:32 -07001014static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 char buf[MAX_ADDR_LEN];
1017 struct net_device *dev = in_dev->dev;
1018
1019 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1020 We will get multicast token leakage, when IFF_MULTICAST
1021 is changed. This check should be done in dev->set_multicast_list
1022 routine. Something sort of:
1023 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1024 --ANK
1025 */
1026 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001027 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/*
1031 * Remove a filter from a device
1032 */
1033
Al Viro63007722006-09-27 18:31:32 -07001034static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 char buf[MAX_ADDR_LEN];
1037 struct net_device *dev = in_dev->dev;
1038
1039 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001040 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041}
1042
1043#ifdef CONFIG_IP_MULTICAST
1044/*
1045 * deleted ip_mc_list manipulation
1046 */
1047static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1048{
1049 struct ip_mc_list *pmc;
1050
1051 /* this is an "ip_mc_list" for convenience; only the fields below
1052 * are actually used. In particular, the refcnt and users are not
1053 * used for management of the delete list. Using the same structure
1054 * for deleted items allows change reports to use common code with
1055 * non-deleted or query-response MCA's.
1056 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001057 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 if (!pmc)
1059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 spin_lock_bh(&im->lock);
1061 pmc->interface = im->interface;
1062 in_dev_hold(in_dev);
1063 pmc->multiaddr = im->multiaddr;
1064 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1065 IGMP_Unsolicited_Report_Count;
1066 pmc->sfmode = im->sfmode;
1067 if (pmc->sfmode == MCAST_INCLUDE) {
1068 struct ip_sf_list *psf;
1069
1070 pmc->tomb = im->tomb;
1071 pmc->sources = im->sources;
1072 im->tomb = im->sources = NULL;
1073 for (psf=pmc->sources; psf; psf=psf->sf_next)
1074 psf->sf_crcount = pmc->crcount;
1075 }
1076 spin_unlock_bh(&im->lock);
1077
1078 spin_lock_bh(&in_dev->mc_tomb_lock);
1079 pmc->next = in_dev->mc_tomb;
1080 in_dev->mc_tomb = pmc;
1081 spin_unlock_bh(&in_dev->mc_tomb_lock);
1082}
1083
Al Viro63007722006-09-27 18:31:32 -07001084static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
1086 struct ip_mc_list *pmc, *pmc_prev;
1087 struct ip_sf_list *psf, *psf_next;
1088
1089 spin_lock_bh(&in_dev->mc_tomb_lock);
1090 pmc_prev = NULL;
1091 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1092 if (pmc->multiaddr == multiaddr)
1093 break;
1094 pmc_prev = pmc;
1095 }
1096 if (pmc) {
1097 if (pmc_prev)
1098 pmc_prev->next = pmc->next;
1099 else
1100 in_dev->mc_tomb = pmc->next;
1101 }
1102 spin_unlock_bh(&in_dev->mc_tomb_lock);
1103 if (pmc) {
1104 for (psf=pmc->tomb; psf; psf=psf_next) {
1105 psf_next = psf->sf_next;
1106 kfree(psf);
1107 }
1108 in_dev_put(pmc->interface);
1109 kfree(pmc);
1110 }
1111}
1112
1113static void igmpv3_clear_delrec(struct in_device *in_dev)
1114{
1115 struct ip_mc_list *pmc, *nextpmc;
1116
1117 spin_lock_bh(&in_dev->mc_tomb_lock);
1118 pmc = in_dev->mc_tomb;
1119 in_dev->mc_tomb = NULL;
1120 spin_unlock_bh(&in_dev->mc_tomb_lock);
1121
1122 for (; pmc; pmc = nextpmc) {
1123 nextpmc = pmc->next;
1124 ip_mc_clear_src(pmc);
1125 in_dev_put(pmc->interface);
1126 kfree(pmc);
1127 }
1128 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001129 rcu_read_lock();
1130 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 struct ip_sf_list *psf, *psf_next;
1132
1133 spin_lock_bh(&pmc->lock);
1134 psf = pmc->tomb;
1135 pmc->tomb = NULL;
1136 spin_unlock_bh(&pmc->lock);
1137 for (; psf; psf=psf_next) {
1138 psf_next = psf->sf_next;
1139 kfree(psf);
1140 }
1141 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001142 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144#endif
1145
1146static void igmp_group_dropped(struct ip_mc_list *im)
1147{
1148 struct in_device *in_dev = im->interface;
1149#ifdef CONFIG_IP_MULTICAST
1150 int reporter;
1151#endif
1152
1153 if (im->loaded) {
1154 im->loaded = 0;
1155 ip_mc_filter_del(in_dev, im->multiaddr);
1156 }
1157
1158#ifdef CONFIG_IP_MULTICAST
1159 if (im->multiaddr == IGMP_ALL_HOSTS)
1160 return;
1161
1162 reporter = im->reporter;
1163 igmp_stop_timer(im);
1164
1165 if (!in_dev->dead) {
1166 if (IGMP_V1_SEEN(in_dev))
1167 goto done;
1168 if (IGMP_V2_SEEN(in_dev)) {
1169 if (reporter)
1170 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1171 goto done;
1172 }
1173 /* IGMPv3 */
1174 igmpv3_add_delrec(in_dev, im);
1175
1176 igmp_ifc_event(in_dev);
1177 }
1178done:
1179#endif
1180 ip_mc_clear_src(im);
1181}
1182
1183static void igmp_group_added(struct ip_mc_list *im)
1184{
1185 struct in_device *in_dev = im->interface;
1186
1187 if (im->loaded == 0) {
1188 im->loaded = 1;
1189 ip_mc_filter_add(in_dev, im->multiaddr);
1190 }
1191
1192#ifdef CONFIG_IP_MULTICAST
1193 if (im->multiaddr == IGMP_ALL_HOSTS)
1194 return;
1195
1196 if (in_dev->dead)
1197 return;
1198 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1199 spin_lock_bh(&im->lock);
1200 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1201 spin_unlock_bh(&im->lock);
1202 return;
1203 }
1204 /* else, v3 */
1205
1206 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1207 IGMP_Unsolicited_Report_Count;
1208 igmp_ifc_event(in_dev);
1209#endif
1210}
1211
1212
1213/*
1214 * Multicast list managers
1215 */
1216
1217
1218/*
1219 * A socket has joined a multicast group on device dev.
1220 */
1221
Al Viro8f935bb2006-09-27 18:30:07 -07001222void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 struct ip_mc_list *im;
1225
1226 ASSERT_RTNL();
1227
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001228 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (im->multiaddr == addr) {
1230 im->users++;
1231 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1232 goto out;
1233 }
1234 }
1235
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001236 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!im)
1238 goto out;
1239
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001240 im->users = 1;
1241 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001243 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /* initial mode is (EX, empty) */
1245 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 atomic_set(&im->refcnt, 1);
1248 spin_lock_init(&im->lock);
1249#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001250 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001253
1254 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001255 in_dev->mc_count++;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001256 rcu_assign_pointer(in_dev->mc_list, im);
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258#ifdef CONFIG_IP_MULTICAST
1259 igmpv3_del_delrec(in_dev, im->multiaddr);
1260#endif
1261 igmp_group_added(im);
1262 if (!in_dev->dead)
1263 ip_rt_multicast_event(in_dev);
1264out:
1265 return;
1266}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001267EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001270 * Resend IGMP JOIN report; used for bonding.
1271 */
1272void ip_mc_rejoin_group(struct ip_mc_list *im)
1273{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001274#ifdef CONFIG_IP_MULTICAST
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001275 struct in_device *in_dev = im->interface;
1276
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001277 if (im->multiaddr == IGMP_ALL_HOSTS)
1278 return;
1279
Flavio Leitnere12b4532010-10-05 14:23:58 +00001280 /* a failover is happening and switches
1281 * must be notified immediately */
1282 if (IGMP_V1_SEEN(in_dev))
1283 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
1284 else if (IGMP_V2_SEEN(in_dev))
1285 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
1286 else
1287 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001288#endif
1289}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001290EXPORT_SYMBOL(ip_mc_rejoin_group);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001291
1292/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * A socket has left a multicast group on device dev
1294 */
1295
Al Viro8f935bb2006-09-27 18:30:07 -07001296void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001298 struct ip_mc_list *i;
1299 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001302
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001303 for (ip = &in_dev->mc_list;
1304 (i = rtnl_dereference(*ip)) != NULL;
1305 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001306 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (--i->users == 0) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001308 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001309 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 igmp_group_dropped(i);
1311
1312 if (!in_dev->dead)
1313 ip_rt_multicast_event(in_dev);
1314
1315 ip_ma_put(i);
1316 return;
1317 }
1318 break;
1319 }
1320 }
1321}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001322EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Moni Shoua75c78502009-09-15 02:37:40 -07001324/* Device changing type */
1325
1326void ip_mc_unmap(struct in_device *in_dev)
1327{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001328 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001329
1330 ASSERT_RTNL();
1331
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001332 for_each_pmc_rtnl(in_dev, pmc)
1333 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001334}
1335
1336void ip_mc_remap(struct in_device *in_dev)
1337{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001338 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001339
1340 ASSERT_RTNL();
1341
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001342 for_each_pmc_rtnl(in_dev, pmc)
1343 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346/* Device going down */
1347
1348void ip_mc_down(struct in_device *in_dev)
1349{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001350 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 ASSERT_RTNL();
1353
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001354 for_each_pmc_rtnl(in_dev, pmc)
1355 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357#ifdef CONFIG_IP_MULTICAST
1358 in_dev->mr_ifc_count = 0;
1359 if (del_timer(&in_dev->mr_ifc_timer))
1360 __in_dev_put(in_dev);
1361 in_dev->mr_gq_running = 0;
1362 if (del_timer(&in_dev->mr_gq_timer))
1363 __in_dev_put(in_dev);
1364 igmpv3_clear_delrec(in_dev);
1365#endif
1366
1367 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1368}
1369
1370void ip_mc_init_dev(struct in_device *in_dev)
1371{
1372 ASSERT_RTNL();
1373
1374 in_dev->mc_tomb = NULL;
1375#ifdef CONFIG_IP_MULTICAST
1376 in_dev->mr_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001377 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1378 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 in_dev->mr_ifc_count = 0;
Rami Rosenb8bae412008-10-07 15:34:37 -07001380 in_dev->mc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001381 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1382 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1384#endif
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 spin_lock_init(&in_dev->mc_tomb_lock);
1387}
1388
1389/* Device going up */
1390
1391void ip_mc_up(struct in_device *in_dev)
1392{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001393 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 ASSERT_RTNL();
1396
1397 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1398
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001399 for_each_pmc_rtnl(in_dev, pmc)
1400 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403/*
1404 * Device is about to be destroyed: clean up.
1405 */
1406
1407void ip_mc_destroy_dev(struct in_device *in_dev)
1408{
1409 struct ip_mc_list *i;
1410
1411 ASSERT_RTNL();
1412
1413 /* Deactivate timers */
1414 ip_mc_down(in_dev);
1415
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001416 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1417 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001418 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 igmp_group_dropped(i);
1421 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001425/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001426static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
1428 struct flowi fl = { .nl_u = { .ip4_u =
1429 { .daddr = imr->imr_multiaddr.s_addr } } };
1430 struct rtable *rt;
1431 struct net_device *dev = NULL;
1432 struct in_device *idev = NULL;
1433
1434 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001435 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return idev;
1437 }
1438 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001439 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (!dev)
1441 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
Daniel Lezcano877aced2008-08-13 16:15:57 -07001444 if (!dev && !ip_route_output_key(net, &rt, &fl)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001445 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 ip_rt_put(rt);
1447 }
1448 if (dev) {
1449 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001450 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452 return idev;
1453}
1454
1455/*
1456 * Join a socket to a group
1457 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001458int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1459int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461
1462static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001463 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464{
1465 struct ip_sf_list *psf, *psf_prev;
1466 int rv = 0;
1467
1468 psf_prev = NULL;
1469 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1470 if (psf->sf_inaddr == *psfsrc)
1471 break;
1472 psf_prev = psf;
1473 }
1474 if (!psf || psf->sf_count[sfmode] == 0) {
1475 /* source filter not found, or count wrong => bug */
1476 return -ESRCH;
1477 }
1478 psf->sf_count[sfmode]--;
1479 if (psf->sf_count[sfmode] == 0) {
1480 ip_rt_multicast_event(pmc->interface);
1481 }
1482 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1483#ifdef CONFIG_IP_MULTICAST
1484 struct in_device *in_dev = pmc->interface;
1485#endif
1486
1487 /* no more filters for this source */
1488 if (psf_prev)
1489 psf_prev->sf_next = psf->sf_next;
1490 else
1491 pmc->sources = psf->sf_next;
1492#ifdef CONFIG_IP_MULTICAST
1493 if (psf->sf_oldin &&
1494 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001495 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 IGMP_Unsolicited_Report_Count;
1497 psf->sf_next = pmc->tomb;
1498 pmc->tomb = psf;
1499 rv = 1;
1500 } else
1501#endif
1502 kfree(psf);
1503 }
1504 return rv;
1505}
1506
1507#ifndef CONFIG_IP_MULTICAST
1508#define igmp_ifc_event(x) do { } while (0)
1509#endif
1510
Al Viro8f935bb2006-09-27 18:30:07 -07001511static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1512 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 struct ip_mc_list *pmc;
1515 int changerec = 0;
1516 int i, err;
1517
1518 if (!in_dev)
1519 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001520 rcu_read_lock();
1521 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 if (*pmca == pmc->multiaddr)
1523 break;
1524 }
1525 if (!pmc) {
1526 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001527 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 return -ESRCH;
1529 }
1530 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001531 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532#ifdef CONFIG_IP_MULTICAST
1533 sf_markstate(pmc);
1534#endif
1535 if (!delta) {
1536 err = -EINVAL;
1537 if (!pmc->sfcount[sfmode])
1538 goto out_unlock;
1539 pmc->sfcount[sfmode]--;
1540 }
1541 err = 0;
1542 for (i=0; i<sfcount; i++) {
1543 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1544
1545 changerec |= rv > 0;
1546 if (!err && rv < 0)
1547 err = rv;
1548 }
1549 if (pmc->sfmode == MCAST_EXCLUDE &&
1550 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1551 pmc->sfcount[MCAST_INCLUDE]) {
1552#ifdef CONFIG_IP_MULTICAST
1553 struct ip_sf_list *psf;
1554#endif
1555
1556 /* filter mode change */
1557 pmc->sfmode = MCAST_INCLUDE;
1558#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001559 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 IGMP_Unsolicited_Report_Count;
1561 in_dev->mr_ifc_count = pmc->crcount;
1562 for (psf=pmc->sources; psf; psf = psf->sf_next)
1563 psf->sf_crcount = 0;
1564 igmp_ifc_event(pmc->interface);
1565 } else if (sf_setstate(pmc) || changerec) {
1566 igmp_ifc_event(pmc->interface);
1567#endif
1568 }
1569out_unlock:
1570 spin_unlock_bh(&pmc->lock);
1571 return err;
1572}
1573
1574/*
1575 * Add multicast single-source filter to the interface list
1576 */
1577static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001578 __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 struct ip_sf_list *psf, *psf_prev;
1581
1582 psf_prev = NULL;
1583 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1584 if (psf->sf_inaddr == *psfsrc)
1585 break;
1586 psf_prev = psf;
1587 }
1588 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001589 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (!psf)
1591 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 psf->sf_inaddr = *psfsrc;
1593 if (psf_prev) {
1594 psf_prev->sf_next = psf;
1595 } else
1596 pmc->sources = psf;
1597 }
1598 psf->sf_count[sfmode]++;
1599 if (psf->sf_count[sfmode] == 1) {
1600 ip_rt_multicast_event(pmc->interface);
1601 }
1602 return 0;
1603}
1604
1605#ifdef CONFIG_IP_MULTICAST
1606static void sf_markstate(struct ip_mc_list *pmc)
1607{
1608 struct ip_sf_list *psf;
1609 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1610
1611 for (psf=pmc->sources; psf; psf=psf->sf_next)
1612 if (pmc->sfcount[MCAST_EXCLUDE]) {
1613 psf->sf_oldin = mca_xcount ==
1614 psf->sf_count[MCAST_EXCLUDE] &&
1615 !psf->sf_count[MCAST_INCLUDE];
1616 } else
1617 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1618}
1619
1620static int sf_setstate(struct ip_mc_list *pmc)
1621{
David L Stevensad125832006-01-18 14:20:56 -08001622 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1624 int qrv = pmc->interface->mr_qrv;
1625 int new_in, rv;
1626
1627 rv = 0;
1628 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1629 if (pmc->sfcount[MCAST_EXCLUDE]) {
1630 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1631 !psf->sf_count[MCAST_INCLUDE];
1632 } else
1633 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001634 if (new_in) {
1635 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001636 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001637
1638 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1639 if (dpsf->sf_inaddr == psf->sf_inaddr)
1640 break;
1641 prev = dpsf;
1642 }
1643 if (dpsf) {
1644 if (prev)
1645 prev->sf_next = dpsf->sf_next;
1646 else
1647 pmc->tomb = dpsf->sf_next;
1648 kfree(dpsf);
1649 }
1650 psf->sf_crcount = qrv;
1651 rv++;
1652 }
1653 } else if (psf->sf_oldin) {
1654
1655 psf->sf_crcount = 0;
1656 /*
1657 * add or update "delete" records if an active filter
1658 * is now inactive
1659 */
1660 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1661 if (dpsf->sf_inaddr == psf->sf_inaddr)
1662 break;
1663 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001664 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001665 if (!dpsf)
1666 continue;
1667 *dpsf = *psf;
1668 /* pmc->lock held by callers */
1669 dpsf->sf_next = pmc->tomb;
1670 pmc->tomb = dpsf;
1671 }
1672 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 rv++;
1674 }
1675 }
1676 return rv;
1677}
1678#endif
1679
1680/*
1681 * Add multicast source filter list to the interface list
1682 */
Al Viro8f935bb2006-09-27 18:30:07 -07001683static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1684 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 struct ip_mc_list *pmc;
1687 int isexclude;
1688 int i, err;
1689
1690 if (!in_dev)
1691 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001692 rcu_read_lock();
1693 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (*pmca == pmc->multiaddr)
1695 break;
1696 }
1697 if (!pmc) {
1698 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001699 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return -ESRCH;
1701 }
1702 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001703 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705#ifdef CONFIG_IP_MULTICAST
1706 sf_markstate(pmc);
1707#endif
1708 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1709 if (!delta)
1710 pmc->sfcount[sfmode]++;
1711 err = 0;
1712 for (i=0; i<sfcount; i++) {
1713 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1714 if (err)
1715 break;
1716 }
1717 if (err) {
1718 int j;
1719
1720 pmc->sfcount[sfmode]--;
1721 for (j=0; j<i; j++)
1722 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1723 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1724#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001726 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727#endif
1728
1729 /* filter mode change */
1730 if (pmc->sfcount[MCAST_EXCLUDE])
1731 pmc->sfmode = MCAST_EXCLUDE;
1732 else if (pmc->sfcount[MCAST_INCLUDE])
1733 pmc->sfmode = MCAST_INCLUDE;
1734#ifdef CONFIG_IP_MULTICAST
1735 /* else no filters; keep old mode for reports */
1736
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001737 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 IGMP_Unsolicited_Report_Count;
1739 in_dev->mr_ifc_count = pmc->crcount;
1740 for (psf=pmc->sources; psf; psf = psf->sf_next)
1741 psf->sf_crcount = 0;
1742 igmp_ifc_event(in_dev);
1743 } else if (sf_setstate(pmc)) {
1744 igmp_ifc_event(in_dev);
1745#endif
1746 }
1747 spin_unlock_bh(&pmc->lock);
1748 return err;
1749}
1750
1751static void ip_mc_clear_src(struct ip_mc_list *pmc)
1752{
1753 struct ip_sf_list *psf, *nextpsf;
1754
1755 for (psf=pmc->tomb; psf; psf=nextpsf) {
1756 nextpsf = psf->sf_next;
1757 kfree(psf);
1758 }
1759 pmc->tomb = NULL;
1760 for (psf=pmc->sources; psf; psf=nextpsf) {
1761 nextpsf = psf->sf_next;
1762 kfree(psf);
1763 }
1764 pmc->sources = NULL;
1765 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001766 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 pmc->sfcount[MCAST_EXCLUDE] = 1;
1768}
1769
1770
1771/*
1772 * Join a multicast group
1773 */
1774int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1775{
1776 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001777 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001778 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct in_device *in_dev;
1780 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001781 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001782 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 int count = 0;
1784
Joe Perchesf97c1e02007-12-16 13:45:43 -08001785 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 return -EINVAL;
1787
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001788 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Daniel Lezcano877aced2008-08-13 16:15:57 -07001790 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 if (!in_dev) {
1793 iml = NULL;
1794 err = -ENODEV;
1795 goto done;
1796 }
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001799 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001800 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001801 if (i->multi.imr_multiaddr.s_addr == addr &&
1802 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 count++;
1805 }
1806 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001807 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001809 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001810 if (iml == NULL)
1811 goto done;
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001814 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 iml->sflist = NULL;
1816 iml->sfmode = MCAST_EXCLUDE;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001817 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001821 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return err;
1823}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001824EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001826static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1827{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001828 kfree(container_of(rp, struct ip_sf_socklist, rcu));
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001829 /* sk_omem_alloc should have been decreased by the caller*/
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001830}
1831
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1833 struct in_device *in_dev)
1834{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001835 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 int err;
1837
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001838 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /* any-source empty exclude case */
1840 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1841 iml->sfmode, 0, NULL, 0);
1842 }
1843 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001844 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1845 rcu_assign_pointer(iml->sflist, NULL);
1846 /* decrease mem now to avoid the memleak warning */
1847 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1848 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return err;
1850}
1851
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001852
1853static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1854{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001855 kfree(container_of(rp, struct ip_mc_socklist, rcu));
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001856 /* sk_omem_alloc should have been decreased by the caller*/
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001857}
1858
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860/*
1861 * Ask a socket to leave a group.
1862 */
1863
1864int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1865{
1866 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001867 struct ip_mc_socklist *iml;
1868 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001869 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001870 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001871 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001872 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001873 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001876 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001877 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001878 for (imlp = &inet->mc_list;
1879 (iml = rtnl_dereference(*imlp)) != NULL;
1880 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001881 if (iml->multi.imr_multiaddr.s_addr != group)
1882 continue;
1883 if (ifindex) {
1884 if (iml->multi.imr_ifindex != ifindex)
1885 continue;
1886 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1887 iml->multi.imr_address.s_addr)
1888 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
David L Stevensacd6e002006-08-17 16:27:39 -07001890 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001892 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001893
1894 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001895 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001896 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001897 /* decrease mem now to avoid the memleak warning */
1898 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1899 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
David L Stevensacd6e002006-08-17 16:27:39 -07001900 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
David L Stevensacd6e002006-08-17 16:27:39 -07001902 if (!in_dev)
1903 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001905 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
1908int ip_mc_source(int add, int omode, struct sock *sk, struct
1909 ip_mreq_source *mreqs, int ifindex)
1910{
1911 int err;
1912 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001913 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct ip_mc_socklist *pmc;
1915 struct in_device *in_dev = NULL;
1916 struct inet_sock *inet = inet_sk(sk);
1917 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001918 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001919 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 int i, j, rv;
1921
Joe Perchesf97c1e02007-12-16 13:45:43 -08001922 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return -EINVAL;
1924
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001925 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1928 imr.imr_address.s_addr = mreqs->imr_interface;
1929 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001930 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 if (!in_dev) {
1933 err = -ENODEV;
1934 goto done;
1935 }
1936 err = -EADDRNOTAVAIL;
1937
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001938 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08001939 if ((pmc->multi.imr_multiaddr.s_addr ==
1940 imr.imr_multiaddr.s_addr) &&
1941 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 break;
1943 }
David L Stevens917f2f12005-07-08 17:45:16 -07001944 if (!pmc) { /* must have a prior join */
1945 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /* if a source filter was set, must be the same mode as before */
1949 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07001950 if (pmc->sfmode != omode) {
1951 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 } else if (pmc->sfmode != omode) {
1955 /* allow mode switches for empty-set filters */
1956 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001957 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 NULL, 0);
1959 pmc->sfmode = omode;
1960 }
1961
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001962 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (!add) {
1964 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07001965 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 rv = !0;
1967 for (i=0; i<psl->sl_count; i++) {
1968 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001969 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (rv == 0)
1971 break;
1972 }
1973 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07001974 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
David L Stevens8cdaaa12005-07-08 17:39:23 -07001976 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1977 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1978 leavegroup = 1;
1979 goto done;
1980 }
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001983 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 &mreqs->imr_sourceaddr, 1);
1985
1986 for (j=i+1; j<psl->sl_count; j++)
1987 psl->sl_addr[j-1] = psl->sl_addr[j];
1988 psl->sl_count--;
1989 err = 0;
1990 goto done;
1991 }
1992 /* else, add a new source to the filter */
1993
1994 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1995 err = -ENOBUFS;
1996 goto done;
1997 }
1998 if (!psl || psl->sl_count == psl->sl_max) {
1999 struct ip_sf_socklist *newpsl;
2000 int count = IP_SFBLOCK;
2001
2002 if (psl)
2003 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002004 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (!newpsl) {
2006 err = -ENOBUFS;
2007 goto done;
2008 }
2009 newpsl->sl_max = count;
2010 newpsl->sl_count = count - IP_SFBLOCK;
2011 if (psl) {
2012 for (i=0; i<psl->sl_count; i++)
2013 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002014 /* decrease mem now to avoid the memleak warning */
2015 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2016 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002018 rcu_assign_pointer(pmc->sflist, newpsl);
2019 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
2021 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2022 for (i=0; i<psl->sl_count; i++) {
2023 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002024 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (rv == 0)
2026 break;
2027 }
2028 if (rv == 0) /* address already there is an error */
2029 goto done;
2030 for (j=psl->sl_count-1; j>=i; j--)
2031 psl->sl_addr[j+1] = psl->sl_addr[j];
2032 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2033 psl->sl_count++;
2034 err = 0;
2035 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002036 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 &mreqs->imr_sourceaddr, 1);
2038done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002039 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002040 if (leavegroup)
2041 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return err;
2043}
2044
2045int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2046{
David L Stevens9951f032005-07-08 17:47:28 -07002047 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002049 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 struct ip_mc_socklist *pmc;
2051 struct in_device *in_dev;
2052 struct inet_sock *inet = inet_sk(sk);
2053 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002054 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002055 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Joe Perchesf97c1e02007-12-16 13:45:43 -08002057 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return -EINVAL;
2059 if (msf->imsf_fmode != MCAST_INCLUDE &&
2060 msf->imsf_fmode != MCAST_EXCLUDE)
2061 return -EINVAL;
2062
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002063 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2066 imr.imr_address.s_addr = msf->imsf_interface;
2067 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002068 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 if (!in_dev) {
2071 err = -ENODEV;
2072 goto done;
2073 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
David L Stevens9951f032005-07-08 17:47:28 -07002075 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2076 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2077 leavegroup = 1;
2078 goto done;
2079 }
2080
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002081 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2083 pmc->multi.imr_ifindex == imr.imr_ifindex)
2084 break;
2085 }
David L Stevens917f2f12005-07-08 17:45:16 -07002086 if (!pmc) { /* must have a prior join */
2087 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002091 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2092 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (!newpsl) {
2094 err = -ENOBUFS;
2095 goto done;
2096 }
2097 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2098 memcpy(newpsl->sl_addr, msf->imsf_slist,
2099 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2100 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2101 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2102 if (err) {
2103 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2104 goto done;
2105 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002106 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002108 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2109 msf->imsf_fmode, 0, NULL, 0);
2110 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002111 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (psl) {
2113 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2114 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002115 /* decrease mem now to avoid the memleak warning */
2116 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2117 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 } else
2119 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2120 0, NULL, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002121 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002123 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002125 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002126 if (leavegroup)
2127 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return err;
2129}
2130
2131int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2132 struct ip_msfilter __user *optval, int __user *optlen)
2133{
2134 int err, len, count, copycount;
2135 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002136 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct ip_mc_socklist *pmc;
2138 struct in_device *in_dev;
2139 struct inet_sock *inet = inet_sk(sk);
2140 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002141 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Joe Perchesf97c1e02007-12-16 13:45:43 -08002143 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return -EINVAL;
2145
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002146 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2149 imr.imr_address.s_addr = msf->imsf_interface;
2150 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002151 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 if (!in_dev) {
2154 err = -ENODEV;
2155 goto done;
2156 }
2157 err = -EADDRNOTAVAIL;
2158
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002159 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2161 pmc->multi.imr_ifindex == imr.imr_ifindex)
2162 break;
2163 }
2164 if (!pmc) /* must have a prior join */
2165 goto done;
2166 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002167 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002168 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (!psl) {
2170 len = 0;
2171 count = 0;
2172 } else {
2173 count = psl->sl_count;
2174 }
2175 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2176 len = copycount * sizeof(psl->sl_addr[0]);
2177 msf->imsf_numsrc = count;
2178 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2179 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2180 return -EFAULT;
2181 }
2182 if (len &&
2183 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2184 return -EFAULT;
2185 return 0;
2186done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002187 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 return err;
2189}
2190
2191int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2192 struct group_filter __user *optval, int __user *optlen)
2193{
2194 int err, i, count, copycount;
2195 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002196 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 struct ip_mc_socklist *pmc;
2198 struct inet_sock *inet = inet_sk(sk);
2199 struct ip_sf_socklist *psl;
2200
2201 psin = (struct sockaddr_in *)&gsf->gf_group;
2202 if (psin->sin_family != AF_INET)
2203 return -EINVAL;
2204 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002205 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return -EINVAL;
2207
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002208 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 err = -EADDRNOTAVAIL;
2211
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002212 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2214 pmc->multi.imr_ifindex == gsf->gf_interface)
2215 break;
2216 }
2217 if (!pmc) /* must have a prior join */
2218 goto done;
2219 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002220 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002221 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 count = psl ? psl->sl_count : 0;
2223 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2224 gsf->gf_numsrc = count;
2225 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2226 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2227 return -EFAULT;
2228 }
2229 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 struct sockaddr_storage ss;
2231
2232 psin = (struct sockaddr_in *)&ss;
2233 memset(&ss, 0, sizeof(ss));
2234 psin->sin_family = AF_INET;
2235 psin->sin_addr.s_addr = psl->sl_addr[i];
2236 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2237 return -EFAULT;
2238 }
2239 return 0;
2240done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002241 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return err;
2243}
2244
2245/*
2246 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2247 */
Al Viroc0cda062006-09-27 18:31:10 -07002248int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
2250 struct inet_sock *inet = inet_sk(sk);
2251 struct ip_mc_socklist *pmc;
2252 struct ip_sf_socklist *psl;
2253 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002254 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002256 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002257 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002258 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002260 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002261 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2263 pmc->multi.imr_ifindex == dif)
2264 break;
2265 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002266 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002268 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002269 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002270 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002272 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
2274 for (i=0; i<psl->sl_count; i++) {
2275 if (psl->sl_addr[i] == rmt_addr)
2276 break;
2277 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002278 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002280 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002282 goto unlock;
2283 ret = 1;
2284unlock:
2285 rcu_read_unlock();
2286out:
2287 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
2290/*
2291 * A socket is closing.
2292 */
2293
2294void ip_mc_drop_socket(struct sock *sk)
2295{
2296 struct inet_sock *inet = inet_sk(sk);
2297 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002298 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 if (inet->mc_list == NULL)
2301 return;
2302
2303 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002304 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002307 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002308 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002309 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002310 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002312 /* decrease mem now to avoid the memleak warning */
2313 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2314 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
2316 rtnl_unlock();
2317}
2318
Al Viroa60c4922006-09-26 21:28:34 -07002319int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
2321 struct ip_mc_list *im;
2322 struct ip_sf_list *psf;
2323 int rv = 0;
2324
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002325 rcu_read_lock();
2326 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (im->multiaddr == mc_addr)
2328 break;
2329 }
2330 if (im && proto == IPPROTO_IGMP) {
2331 rv = 1;
2332 } else if (im) {
2333 if (src_addr) {
2334 for (psf=im->sources; psf; psf=psf->sf_next) {
2335 if (psf->sf_inaddr == src_addr)
2336 break;
2337 }
2338 if (psf)
2339 rv = psf->sf_count[MCAST_INCLUDE] ||
2340 psf->sf_count[MCAST_EXCLUDE] !=
2341 im->sfcount[MCAST_EXCLUDE];
2342 else
2343 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2344 } else
2345 rv = 1; /* unspecified source; tentatively allow */
2346 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002347 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return rv;
2349}
2350
2351#if defined(CONFIG_PROC_FS)
2352struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002353 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 struct net_device *dev;
2355 struct in_device *in_dev;
2356};
2357
2358#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2359
2360static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2361{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002362 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 struct ip_mc_list *im = NULL;
2364 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2365
Pavel Emelianov7562f872007-05-03 15:13:45 -07002366 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002367 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002369
2370 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (!in_dev)
2372 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002373 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (im) {
2375 state->in_dev = in_dev;
2376 break;
2377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
2379 return im;
2380}
2381
2382static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2383{
2384 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002385
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002386 im = rcu_dereference(im->next_rcu);
2387 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002388 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (!state->dev) {
2390 state->in_dev = NULL;
2391 break;
2392 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002393 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (!state->in_dev)
2395 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002396 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 return im;
2399}
2400
2401static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2402{
2403 struct ip_mc_list *im = igmp_mc_get_first(seq);
2404 if (im)
2405 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2406 --pos;
2407 return pos ? NULL : im;
2408}
2409
2410static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002411 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412{
stephen hemminger61fbab72009-11-10 07:54:55 +00002413 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2415}
2416
2417static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2418{
2419 struct ip_mc_list *im;
2420 if (v == SEQ_START_TOKEN)
2421 im = igmp_mc_get_first(seq);
2422 else
2423 im = igmp_mc_get_next(seq, v);
2424 ++*pos;
2425 return im;
2426}
2427
2428static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002429 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430{
2431 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002432
2433 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002435 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
2438static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2439{
2440 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002441 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2443 else {
2444 struct ip_mc_list *im = (struct ip_mc_list *)v;
2445 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2446 char *querier;
2447#ifdef CONFIG_IP_MULTICAST
2448 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2449 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2450 "V3";
2451#else
2452 querier = "NONE";
2453#endif
2454
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002455 if (rcu_dereference(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002457 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 }
2459
2460 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002461 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 im->multiaddr, im->users,
2463 im->tm_running, im->tm_running ?
2464 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2465 im->reporter);
2466 }
2467 return 0;
2468}
2469
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002470static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 .start = igmp_mc_seq_start,
2472 .next = igmp_mc_seq_next,
2473 .stop = igmp_mc_seq_stop,
2474 .show = igmp_mc_seq_show,
2475};
2476
2477static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2478{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002479 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002480 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481}
2482
Arjan van de Ven9a321442007-02-12 00:55:35 -08002483static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 .owner = THIS_MODULE,
2485 .open = igmp_mc_seq_open,
2486 .read = seq_read,
2487 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002488 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489};
2490
2491struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002492 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 struct net_device *dev;
2494 struct in_device *idev;
2495 struct ip_mc_list *im;
2496};
2497
2498#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2499
2500static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2501{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002502 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 struct ip_sf_list *psf = NULL;
2504 struct ip_mc_list *im = NULL;
2505 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2506
Pavel Emelianov7562f872007-05-03 15:13:45 -07002507 state->idev = NULL;
2508 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002509 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002511 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if (unlikely(idev == NULL))
2513 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002514 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (likely(im != NULL)) {
2516 spin_lock_bh(&im->lock);
2517 psf = im->sources;
2518 if (likely(psf != NULL)) {
2519 state->im = im;
2520 state->idev = idev;
2521 break;
2522 }
2523 spin_unlock_bh(&im->lock);
2524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
2526 return psf;
2527}
2528
2529static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2530{
2531 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2532
2533 psf = psf->sf_next;
2534 while (!psf) {
2535 spin_unlock_bh(&state->im->lock);
2536 state->im = state->im->next;
2537 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002538 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (!state->dev) {
2540 state->idev = NULL;
2541 goto out;
2542 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002543 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (!state->idev)
2545 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002546 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548 if (!state->im)
2549 break;
2550 spin_lock_bh(&state->im->lock);
2551 psf = state->im->sources;
2552 }
2553out:
2554 return psf;
2555}
2556
2557static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2558{
2559 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2560 if (psf)
2561 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2562 --pos;
2563 return pos ? NULL : psf;
2564}
2565
2566static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002567 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
stephen hemminger61fbab72009-11-10 07:54:55 +00002569 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2571}
2572
2573static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2574{
2575 struct ip_sf_list *psf;
2576 if (v == SEQ_START_TOKEN)
2577 psf = igmp_mcf_get_first(seq);
2578 else
2579 psf = igmp_mcf_get_next(seq, v);
2580 ++*pos;
2581 return psf;
2582}
2583
2584static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002585 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
2587 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2588 if (likely(state->im != NULL)) {
2589 spin_unlock_bh(&state->im->lock);
2590 state->im = NULL;
2591 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002592 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002594 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
2597static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2598{
2599 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2600 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2601
2602 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002603 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 "%3s %6s "
2605 "%10s %10s %6s %6s\n", "Idx",
2606 "Device", "MCA",
2607 "SRC", "INC", "EXC");
2608 } else {
2609 seq_printf(seq,
2610 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002611 "0x%08x %6lu %6lu\n",
2612 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 ntohl(state->im->multiaddr),
2614 ntohl(psf->sf_inaddr),
2615 psf->sf_count[MCAST_INCLUDE],
2616 psf->sf_count[MCAST_EXCLUDE]);
2617 }
2618 return 0;
2619}
2620
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002621static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 .start = igmp_mcf_seq_start,
2623 .next = igmp_mcf_seq_next,
2624 .stop = igmp_mcf_seq_stop,
2625 .show = igmp_mcf_seq_show,
2626};
2627
2628static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2629{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002630 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002631 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
Arjan van de Ven9a321442007-02-12 00:55:35 -08002634static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 .owner = THIS_MODULE,
2636 .open = igmp_mcf_seq_open,
2637 .read = seq_read,
2638 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002639 .release = seq_release_net,
2640};
2641
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002642static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002643{
2644 struct proc_dir_entry *pde;
2645
2646 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2647 if (!pde)
2648 goto out_igmp;
2649 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2650 if (!pde)
2651 goto out_mcfilter;
2652 return 0;
2653
2654out_mcfilter:
2655 proc_net_remove(net, "igmp");
2656out_igmp:
2657 return -ENOMEM;
2658}
2659
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002660static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002661{
2662 proc_net_remove(net, "mcfilter");
2663 proc_net_remove(net, "igmp");
2664}
2665
2666static struct pernet_operations igmp_net_ops = {
2667 .init = igmp_net_init,
2668 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669};
2670
2671int __init igmp_mc_proc_init(void)
2672{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002673 return register_pernet_subsys(&igmp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675#endif