blob: e0e77e297de32148356da4c309856dacabee2387 [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
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000303#define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
306{
307 struct sk_buff *skb;
308 struct rtable *rt;
309 struct iphdr *pip;
310 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700311 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000313 while (1) {
314 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
315 GFP_ATOMIC | __GFP_NOWARN);
316 if (skb)
317 break;
318 size >>= 1;
319 if (size < 256)
320 return NULL;
321 }
322 igmp_skb_size(skb) = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 {
325 struct flowi fl = { .oif = dev->ifindex,
Changli Gao58116622010-11-12 18:43:55 +0000326 .fl4_dst = IGMPV3_ALL_MCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 .proto = IPPROTO_IGMP };
Daniel Lezcano877aced2008-08-13 16:15:57 -0700328 if (ip_route_output_key(net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 kfree_skb(skb);
330 return NULL;
331 }
332 }
333 if (rt->rt_src == 0) {
334 kfree_skb(skb);
335 ip_rt_put(rt);
336 return NULL;
337 }
338
Changli Gaod8d1f302010-06-10 23:31:35 -0700339 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 skb->dev = dev;
341
342 skb_reserve(skb, LL_RESERVED_SPACE(dev));
343
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300344 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700345 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300346 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 pip->version = 4;
349 pip->ihl = (sizeof(struct iphdr)+4)>>2;
350 pip->tos = 0xc0;
351 pip->frag_off = htons(IP_DF);
352 pip->ttl = 1;
353 pip->daddr = rt->rt_dst;
354 pip->saddr = rt->rt_src;
355 pip->protocol = IPPROTO_IGMP;
356 pip->tot_len = 0; /* filled in later */
Changli Gaod8d1f302010-06-10 23:31:35 -0700357 ip_select_ident(pip, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 ((u8*)&pip[1])[0] = IPOPT_RA;
359 ((u8*)&pip[1])[1] = 4;
360 ((u8*)&pip[1])[2] = 0;
361 ((u8*)&pip[1])[3] = 0;
362
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700363 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300364 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300365 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
367 pig->resv1 = 0;
368 pig->csum = 0;
369 pig->resv2 = 0;
370 pig->ngrec = 0;
371 return skb;
372}
373
374static int igmpv3_sendpack(struct sk_buff *skb)
375{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300376 struct igmphdr *pig = igmp_hdr(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700377 const int igmplen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300379 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Herbert Xuc439cb22008-01-11 19:14:00 -0800381 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
385{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800386 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
390 int type, struct igmpv3_grec **ppgr)
391{
392 struct net_device *dev = pmc->interface->dev;
393 struct igmpv3_report *pih;
394 struct igmpv3_grec *pgr;
395
396 if (!skb)
397 skb = igmpv3_newpack(dev, dev->mtu);
398 if (!skb)
399 return NULL;
400 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
401 pgr->grec_type = type;
402 pgr->grec_auxwords = 0;
403 pgr->grec_nsrcs = 0;
404 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300405 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 pih->ngrec = htons(ntohs(pih->ngrec)+1);
407 *ppgr = pgr;
408 return skb;
409}
410
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000411#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 skb_tailroom(skb)) : 0)
413
414static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
415 int type, int gdeleted, int sdeleted)
416{
417 struct net_device *dev = pmc->interface->dev;
418 struct igmpv3_report *pih;
419 struct igmpv3_grec *pgr = NULL;
420 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800421 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (pmc->multiaddr == IGMP_ALL_HOSTS)
424 return skb;
425
426 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
427 type == IGMPV3_MODE_IS_EXCLUDE;
428 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
429 type == IGMPV3_CHANGE_TO_EXCLUDE;
430
David L Stevensad125832006-01-18 14:20:56 -0800431 stotal = scount = 0;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
434
David L Stevensad125832006-01-18 14:20:56 -0800435 if (!*psf_list)
436 goto empty_source;
437
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300438 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 /* EX and TO_EX get a fresh packet, if needed */
441 if (truncate) {
442 if (pih && pih->ngrec &&
443 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
444 if (skb)
445 igmpv3_sendpack(skb);
446 skb = igmpv3_newpack(dev, dev->mtu);
447 }
448 }
449 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 psf_prev = NULL;
451 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700452 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 psf_next = psf->sf_next;
455
456 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
457 psf_prev = psf;
458 continue;
459 }
460
461 /* clear marks on query responses */
462 if (isquery)
463 psf->sf_gsresp = 0;
464
Al Viro63007722006-09-27 18:31:32 -0700465 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 first*sizeof(struct igmpv3_grec)) {
467 if (truncate && !first)
468 break; /* truncate these */
469 if (pgr)
470 pgr->grec_nsrcs = htons(scount);
471 if (skb)
472 igmpv3_sendpack(skb);
473 skb = igmpv3_newpack(dev, dev->mtu);
474 first = 1;
475 scount = 0;
476 }
477 if (first) {
478 skb = add_grhead(skb, pmc, type, &pgr);
479 first = 0;
480 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800481 if (!skb)
482 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700483 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800485 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
487 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
488 psf->sf_crcount--;
489 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
490 if (psf_prev)
491 psf_prev->sf_next = psf->sf_next;
492 else
493 *psf_list = psf->sf_next;
494 kfree(psf);
495 continue;
496 }
497 }
498 psf_prev = psf;
499 }
David L Stevensad125832006-01-18 14:20:56 -0800500
501empty_source:
502 if (!stotal) {
503 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
504 type == IGMPV3_BLOCK_OLD_SOURCES)
505 return skb;
506 if (pmc->crcount || isquery) {
507 /* make sure we have room for group header */
508 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
509 igmpv3_sendpack(skb);
510 skb = NULL; /* add_grhead will get a new one */
511 }
512 skb = add_grhead(skb, pmc, type, &pgr);
513 }
514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (pgr)
516 pgr->grec_nsrcs = htons(scount);
517
518 if (isquery)
519 pmc->gsquery = 0; /* clear query state on report */
520 return skb;
521}
522
523static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
524{
525 struct sk_buff *skb = NULL;
526 int type;
527
528 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000529 rcu_read_lock();
530 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (pmc->multiaddr == IGMP_ALL_HOSTS)
532 continue;
533 spin_lock_bh(&pmc->lock);
534 if (pmc->sfcount[MCAST_EXCLUDE])
535 type = IGMPV3_MODE_IS_EXCLUDE;
536 else
537 type = IGMPV3_MODE_IS_INCLUDE;
538 skb = add_grec(skb, pmc, type, 0, 0);
539 spin_unlock_bh(&pmc->lock);
540 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000541 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
543 spin_lock_bh(&pmc->lock);
544 if (pmc->sfcount[MCAST_EXCLUDE])
545 type = IGMPV3_MODE_IS_EXCLUDE;
546 else
547 type = IGMPV3_MODE_IS_INCLUDE;
548 skb = add_grec(skb, pmc, type, 0, 0);
549 spin_unlock_bh(&pmc->lock);
550 }
551 if (!skb)
552 return 0;
553 return igmpv3_sendpack(skb);
554}
555
556/*
557 * remove zero-count source records from a source filter list
558 */
559static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
560{
561 struct ip_sf_list *psf_prev, *psf_next, *psf;
562
563 psf_prev = NULL;
564 for (psf=*ppsf; psf; psf = psf_next) {
565 psf_next = psf->sf_next;
566 if (psf->sf_crcount == 0) {
567 if (psf_prev)
568 psf_prev->sf_next = psf->sf_next;
569 else
570 *ppsf = psf->sf_next;
571 kfree(psf);
572 } else
573 psf_prev = psf;
574 }
575}
576
577static void igmpv3_send_cr(struct in_device *in_dev)
578{
579 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
580 struct sk_buff *skb = NULL;
581 int type, dtype;
582
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000583 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 spin_lock_bh(&in_dev->mc_tomb_lock);
585
586 /* deleted MCA's */
587 pmc_prev = NULL;
588 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
589 pmc_next = pmc->next;
590 if (pmc->sfmode == MCAST_INCLUDE) {
591 type = IGMPV3_BLOCK_OLD_SOURCES;
592 dtype = IGMPV3_BLOCK_OLD_SOURCES;
593 skb = add_grec(skb, pmc, type, 1, 0);
594 skb = add_grec(skb, pmc, dtype, 1, 1);
595 }
596 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (pmc->sfmode == MCAST_EXCLUDE) {
598 type = IGMPV3_CHANGE_TO_INCLUDE;
599 skb = add_grec(skb, pmc, type, 1, 0);
600 }
David L Stevensad125832006-01-18 14:20:56 -0800601 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (pmc->crcount == 0) {
603 igmpv3_clear_zeros(&pmc->tomb);
604 igmpv3_clear_zeros(&pmc->sources);
605 }
606 }
607 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
608 if (pmc_prev)
609 pmc_prev->next = pmc_next;
610 else
611 in_dev->mc_tomb = pmc_next;
612 in_dev_put(pmc->interface);
613 kfree(pmc);
614 } else
615 pmc_prev = pmc;
616 }
617 spin_unlock_bh(&in_dev->mc_tomb_lock);
618
619 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000620 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 spin_lock_bh(&pmc->lock);
622 if (pmc->sfcount[MCAST_EXCLUDE]) {
623 type = IGMPV3_BLOCK_OLD_SOURCES;
624 dtype = IGMPV3_ALLOW_NEW_SOURCES;
625 } else {
626 type = IGMPV3_ALLOW_NEW_SOURCES;
627 dtype = IGMPV3_BLOCK_OLD_SOURCES;
628 }
629 skb = add_grec(skb, pmc, type, 0, 0);
630 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
631
632 /* filter mode changes */
633 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (pmc->sfmode == MCAST_EXCLUDE)
635 type = IGMPV3_CHANGE_TO_EXCLUDE;
636 else
637 type = IGMPV3_CHANGE_TO_INCLUDE;
638 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800639 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641 spin_unlock_bh(&pmc->lock);
642 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000643 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645 if (!skb)
646 return;
647 (void) igmpv3_sendpack(skb);
648}
649
650static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
651 int type)
652{
653 struct sk_buff *skb;
654 struct iphdr *iph;
655 struct igmphdr *ih;
656 struct rtable *rt;
657 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700658 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700659 __be32 group = pmc ? pmc->multiaddr : 0;
660 __be32 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
663 return igmpv3_send_report(in_dev, pmc);
664 else if (type == IGMP_HOST_LEAVE_MESSAGE)
665 dst = IGMP_ALL_ROUTER;
666 else
667 dst = group;
668
669 {
670 struct flowi fl = { .oif = dev->ifindex,
Changli Gao58116622010-11-12 18:43:55 +0000671 .fl4_dst = dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 .proto = IPPROTO_IGMP };
Daniel Lezcano877aced2008-08-13 16:15:57 -0700673 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return -1;
675 }
676 if (rt->rt_src == 0) {
677 ip_rt_put(rt);
678 return -1;
679 }
680
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800681 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (skb == NULL) {
683 ip_rt_put(rt);
684 return -1;
685 }
686
Changli Gaod8d1f302010-06-10 23:31:35 -0700687 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 skb_reserve(skb, LL_RESERVED_SPACE(dev));
690
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300691 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700692 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300693 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 iph->version = 4;
696 iph->ihl = (sizeof(struct iphdr)+4)>>2;
697 iph->tos = 0xc0;
698 iph->frag_off = htons(IP_DF);
699 iph->ttl = 1;
700 iph->daddr = dst;
701 iph->saddr = rt->rt_src;
702 iph->protocol = IPPROTO_IGMP;
Changli Gaod8d1f302010-06-10 23:31:35 -0700703 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 ((u8*)&iph[1])[0] = IPOPT_RA;
705 ((u8*)&iph[1])[1] = 4;
706 ((u8*)&iph[1])[2] = 0;
707 ((u8*)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800710 ih->type = type;
711 ih->code = 0;
712 ih->csum = 0;
713 ih->group = group;
714 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Herbert Xuc439cb22008-01-11 19:14:00 -0800716 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719static void igmp_gq_timer_expire(unsigned long data)
720{
721 struct in_device *in_dev = (struct in_device *)data;
722
723 in_dev->mr_gq_running = 0;
724 igmpv3_send_report(in_dev, NULL);
725 __in_dev_put(in_dev);
726}
727
728static void igmp_ifc_timer_expire(unsigned long data)
729{
730 struct in_device *in_dev = (struct in_device *)data;
731
732 igmpv3_send_cr(in_dev);
733 if (in_dev->mr_ifc_count) {
734 in_dev->mr_ifc_count--;
735 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
736 }
737 __in_dev_put(in_dev);
738}
739
740static void igmp_ifc_event(struct in_device *in_dev)
741{
742 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
743 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900744 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 IGMP_Unsolicited_Report_Count;
746 igmp_ifc_start_timer(in_dev, 1);
747}
748
749
750static void igmp_timer_expire(unsigned long data)
751{
752 struct ip_mc_list *im=(struct ip_mc_list *)data;
753 struct in_device *in_dev = im->interface;
754
755 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800756 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (im->unsolicit_count) {
759 im->unsolicit_count--;
760 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
761 }
762 im->reporter = 1;
763 spin_unlock(&im->lock);
764
765 if (IGMP_V1_SEEN(in_dev))
766 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
767 else if (IGMP_V2_SEEN(in_dev))
768 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
769 else
770 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
771
772 ip_ma_put(im);
773}
774
David L Stevensad125832006-01-18 14:20:56 -0800775/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700776static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct ip_sf_list *psf;
779 int i, scount;
780
781 scount = 0;
782 for (psf=pmc->sources; psf; psf=psf->sf_next) {
783 if (scount == nsrcs)
784 break;
David L Stevensad125832006-01-18 14:20:56 -0800785 for (i=0; i<nsrcs; i++) {
786 /* skip inactive filters */
787 if (pmc->sfcount[MCAST_INCLUDE] ||
788 pmc->sfcount[MCAST_EXCLUDE] !=
789 psf->sf_count[MCAST_EXCLUDE])
790 continue;
791 if (srcs[i] == psf->sf_inaddr) {
792 scount++;
793 break;
794 }
795 }
796 }
797 pmc->gsquery = 0;
798 if (scount == nsrcs) /* all sources excluded */
799 return 0;
800 return 1;
801}
802
Al Viroea4d9e72006-09-27 18:30:52 -0700803static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800804{
805 struct ip_sf_list *psf;
806 int i, scount;
807
808 if (pmc->sfmode == MCAST_EXCLUDE)
809 return igmp_xmarksources(pmc, nsrcs, srcs);
810
811 /* mark INCLUDE-mode sources */
812 scount = 0;
813 for (psf=pmc->sources; psf; psf=psf->sf_next) {
814 if (scount == nsrcs)
815 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 for (i=0; i<nsrcs; i++)
817 if (srcs[i] == psf->sf_inaddr) {
818 psf->sf_gsresp = 1;
819 scount++;
820 break;
821 }
822 }
David L Stevensad125832006-01-18 14:20:56 -0800823 if (!scount) {
824 pmc->gsquery = 0;
825 return 0;
826 }
827 pmc->gsquery = 1;
828 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Al Viro63007722006-09-27 18:31:32 -0700831static void igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 struct ip_mc_list *im;
834
835 /* Timers are only set for non-local groups */
836
837 if (group == IGMP_ALL_HOSTS)
838 return;
839
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000840 rcu_read_lock();
841 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (im->multiaddr == group) {
843 igmp_stop_timer(im);
844 break;
845 }
846 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000847 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
851 int len)
852{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300853 struct igmphdr *ih = igmp_hdr(skb);
854 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700856 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 int max_delay;
858 int mark = 0;
859
860
David Stevens5b7c8402010-09-30 14:29:40 +0000861 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (ih->code == 0) {
863 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 max_delay = IGMP_Query_Response_Interval;
866 in_dev->mr_v1_seen = jiffies +
867 IGMP_V1_Router_Present_Timeout;
868 group = 0;
869 } else {
870 /* v2 router present */
871 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
872 in_dev->mr_v2_seen = jiffies +
873 IGMP_V2_Router_Present_Timeout;
874 }
875 /* cancel the interface change timer */
876 in_dev->mr_ifc_count = 0;
877 if (del_timer(&in_dev->mr_ifc_timer))
878 __in_dev_put(in_dev);
879 /* clear deleted report items */
880 igmpv3_clear_delrec(in_dev);
881 } else if (len < 12) {
882 return; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000883 } else if (IGMP_V1_SEEN(in_dev)) {
884 /* This is a v3 query with v1 queriers present */
885 max_delay = IGMP_Query_Response_Interval;
886 group = 0;
887 } else if (IGMP_V2_SEEN(in_dev)) {
888 /* this is a v3 query with v2 queriers present;
889 * Interpretation of the max_delay code is problematic here.
890 * A real v2 host would use ih_code directly, while v3 has a
891 * different encoding. We use the v3 encoding as more likely
892 * to be intended in a v3 query.
893 */
894 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 } else { /* v3 */
896 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
897 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900898
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300899 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900901 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700902 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300904 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906
907 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
908 if (!max_delay)
909 max_delay = 1; /* can't mod w/ 0 */
910 in_dev->mr_maxdelay = max_delay;
911 if (ih3->qrv)
912 in_dev->mr_qrv = ih3->qrv;
913 if (!group) { /* general query */
914 if (ih3->nsrcs)
915 return; /* no sources allowed */
916 igmp_gq_start_timer(in_dev);
917 return;
918 }
919 /* mark sources to include, if group & source-specific */
920 mark = ih3->nsrcs != 0;
921 }
922
923 /*
924 * - Start the timers in all of our membership records
925 * that the query applies to for the interface on
926 * which the query arrived excl. those that belong
927 * to a "local" group (224.0.0.X)
928 * - For timers already running check if they need to
929 * be reset.
930 * - Use the igmp->igmp_code field as the maximum
931 * delay possible
932 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000933 rcu_read_lock();
934 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800935 int changed;
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (group && group != im->multiaddr)
938 continue;
939 if (im->multiaddr == IGMP_ALL_HOSTS)
940 continue;
941 spin_lock_bh(&im->lock);
942 if (im->tm_running)
943 im->gsquery = im->gsquery && mark;
944 else
945 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800946 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900947 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800949 if (changed)
950 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000952 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000955/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956int igmp_rcv(struct sk_buff *skb)
957{
958 /* This basically follows the spec line by line -- see RFC1112 */
959 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000960 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int len = skb->len;
962
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800963 if (in_dev == NULL)
964 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Herbert Xufb286bb2005-11-10 13:01:24 -0800966 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000967 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800968
969 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700970 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800971 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800972 break;
973 /* fall through */
974 case CHECKSUM_NONE:
975 skb->csum = 0;
976 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000977 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300980 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 switch (ih->type) {
982 case IGMP_HOST_MEMBERSHIP_QUERY:
983 igmp_heard_query(in_dev, skb, len);
984 break;
985 case IGMP_HOST_MEMBERSHIP_REPORT:
986 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -0800988 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 break;
David Stevens24c69272005-12-02 20:32:59 -0800990 /* don't rely on MC router hearing unicast reports */
991 if (skb->pkt_type == PACKET_MULTICAST ||
992 skb->pkt_type == PACKET_BROADCAST)
993 igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 break;
995 case IGMP_PIM:
996#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return pim_rcv_v1(skb);
998#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +0000999 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 case IGMP_DVMRP:
1001 case IGMP_TRACE:
1002 case IGMP_HOST_LEAVE_MESSAGE:
1003 case IGMP_MTRACE:
1004 case IGMP_MTRACE_RESP:
1005 break;
1006 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001007 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001009
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001010drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 kfree_skb(skb);
1012 return 0;
1013}
1014
1015#endif
1016
1017
1018/*
1019 * Add a filter to a device
1020 */
1021
Al Viro63007722006-09-27 18:31:32 -07001022static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 char buf[MAX_ADDR_LEN];
1025 struct net_device *dev = in_dev->dev;
1026
1027 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1028 We will get multicast token leakage, when IFF_MULTICAST
1029 is changed. This check should be done in dev->set_multicast_list
1030 routine. Something sort of:
1031 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1032 --ANK
1033 */
1034 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001035 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/*
1039 * Remove a filter from a device
1040 */
1041
Al Viro63007722006-09-27 18:31:32 -07001042static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 char buf[MAX_ADDR_LEN];
1045 struct net_device *dev = in_dev->dev;
1046
1047 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001048 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051#ifdef CONFIG_IP_MULTICAST
1052/*
1053 * deleted ip_mc_list manipulation
1054 */
1055static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1056{
1057 struct ip_mc_list *pmc;
1058
1059 /* this is an "ip_mc_list" for convenience; only the fields below
1060 * are actually used. In particular, the refcnt and users are not
1061 * used for management of the delete list. Using the same structure
1062 * for deleted items allows change reports to use common code with
1063 * non-deleted or query-response MCA's.
1064 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001065 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!pmc)
1067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 spin_lock_bh(&im->lock);
1069 pmc->interface = im->interface;
1070 in_dev_hold(in_dev);
1071 pmc->multiaddr = im->multiaddr;
1072 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1073 IGMP_Unsolicited_Report_Count;
1074 pmc->sfmode = im->sfmode;
1075 if (pmc->sfmode == MCAST_INCLUDE) {
1076 struct ip_sf_list *psf;
1077
1078 pmc->tomb = im->tomb;
1079 pmc->sources = im->sources;
1080 im->tomb = im->sources = NULL;
1081 for (psf=pmc->sources; psf; psf=psf->sf_next)
1082 psf->sf_crcount = pmc->crcount;
1083 }
1084 spin_unlock_bh(&im->lock);
1085
1086 spin_lock_bh(&in_dev->mc_tomb_lock);
1087 pmc->next = in_dev->mc_tomb;
1088 in_dev->mc_tomb = pmc;
1089 spin_unlock_bh(&in_dev->mc_tomb_lock);
1090}
1091
Al Viro63007722006-09-27 18:31:32 -07001092static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct ip_mc_list *pmc, *pmc_prev;
1095 struct ip_sf_list *psf, *psf_next;
1096
1097 spin_lock_bh(&in_dev->mc_tomb_lock);
1098 pmc_prev = NULL;
1099 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1100 if (pmc->multiaddr == multiaddr)
1101 break;
1102 pmc_prev = pmc;
1103 }
1104 if (pmc) {
1105 if (pmc_prev)
1106 pmc_prev->next = pmc->next;
1107 else
1108 in_dev->mc_tomb = pmc->next;
1109 }
1110 spin_unlock_bh(&in_dev->mc_tomb_lock);
1111 if (pmc) {
1112 for (psf=pmc->tomb; psf; psf=psf_next) {
1113 psf_next = psf->sf_next;
1114 kfree(psf);
1115 }
1116 in_dev_put(pmc->interface);
1117 kfree(pmc);
1118 }
1119}
1120
1121static void igmpv3_clear_delrec(struct in_device *in_dev)
1122{
1123 struct ip_mc_list *pmc, *nextpmc;
1124
1125 spin_lock_bh(&in_dev->mc_tomb_lock);
1126 pmc = in_dev->mc_tomb;
1127 in_dev->mc_tomb = NULL;
1128 spin_unlock_bh(&in_dev->mc_tomb_lock);
1129
1130 for (; pmc; pmc = nextpmc) {
1131 nextpmc = pmc->next;
1132 ip_mc_clear_src(pmc);
1133 in_dev_put(pmc->interface);
1134 kfree(pmc);
1135 }
1136 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001137 rcu_read_lock();
1138 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct ip_sf_list *psf, *psf_next;
1140
1141 spin_lock_bh(&pmc->lock);
1142 psf = pmc->tomb;
1143 pmc->tomb = NULL;
1144 spin_unlock_bh(&pmc->lock);
1145 for (; psf; psf=psf_next) {
1146 psf_next = psf->sf_next;
1147 kfree(psf);
1148 }
1149 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001150 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152#endif
1153
1154static void igmp_group_dropped(struct ip_mc_list *im)
1155{
1156 struct in_device *in_dev = im->interface;
1157#ifdef CONFIG_IP_MULTICAST
1158 int reporter;
1159#endif
1160
1161 if (im->loaded) {
1162 im->loaded = 0;
1163 ip_mc_filter_del(in_dev, im->multiaddr);
1164 }
1165
1166#ifdef CONFIG_IP_MULTICAST
1167 if (im->multiaddr == IGMP_ALL_HOSTS)
1168 return;
1169
1170 reporter = im->reporter;
1171 igmp_stop_timer(im);
1172
1173 if (!in_dev->dead) {
1174 if (IGMP_V1_SEEN(in_dev))
1175 goto done;
1176 if (IGMP_V2_SEEN(in_dev)) {
1177 if (reporter)
1178 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1179 goto done;
1180 }
1181 /* IGMPv3 */
1182 igmpv3_add_delrec(in_dev, im);
1183
1184 igmp_ifc_event(in_dev);
1185 }
1186done:
1187#endif
1188 ip_mc_clear_src(im);
1189}
1190
1191static void igmp_group_added(struct ip_mc_list *im)
1192{
1193 struct in_device *in_dev = im->interface;
1194
1195 if (im->loaded == 0) {
1196 im->loaded = 1;
1197 ip_mc_filter_add(in_dev, im->multiaddr);
1198 }
1199
1200#ifdef CONFIG_IP_MULTICAST
1201 if (im->multiaddr == IGMP_ALL_HOSTS)
1202 return;
1203
1204 if (in_dev->dead)
1205 return;
1206 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1207 spin_lock_bh(&im->lock);
1208 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1209 spin_unlock_bh(&im->lock);
1210 return;
1211 }
1212 /* else, v3 */
1213
1214 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1215 IGMP_Unsolicited_Report_Count;
1216 igmp_ifc_event(in_dev);
1217#endif
1218}
1219
1220
1221/*
1222 * Multicast list managers
1223 */
1224
1225
1226/*
1227 * A socket has joined a multicast group on device dev.
1228 */
1229
Al Viro8f935bb2006-09-27 18:30:07 -07001230void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct ip_mc_list *im;
1233
1234 ASSERT_RTNL();
1235
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001236 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (im->multiaddr == addr) {
1238 im->users++;
1239 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1240 goto out;
1241 }
1242 }
1243
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001244 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!im)
1246 goto out;
1247
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001248 im->users = 1;
1249 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001251 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 /* initial mode is (EX, empty) */
1253 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 atomic_set(&im->refcnt, 1);
1256 spin_lock_init(&im->lock);
1257#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001258 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001261
1262 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001263 in_dev->mc_count++;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001264 rcu_assign_pointer(in_dev->mc_list, im);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266#ifdef CONFIG_IP_MULTICAST
1267 igmpv3_del_delrec(in_dev, im->multiaddr);
1268#endif
1269 igmp_group_added(im);
1270 if (!in_dev->dead)
1271 ip_rt_multicast_event(in_dev);
1272out:
1273 return;
1274}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001275EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001278 * Resend IGMP JOIN report; used for bonding.
Eric Dumazet866f3b22010-11-18 09:33:19 -08001279 * Called with rcu_read_lock()
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001280 */
Eric Dumazet866f3b22010-11-18 09:33:19 -08001281void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001282{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001283#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001284 struct ip_mc_list *im;
1285 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001286
Eric Dumazet866f3b22010-11-18 09:33:19 -08001287 for_each_pmc_rcu(in_dev, im) {
1288 if (im->multiaddr == IGMP_ALL_HOSTS)
1289 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001290
Eric Dumazet866f3b22010-11-18 09:33:19 -08001291 /* a failover is happening and switches
1292 * must be notified immediately
1293 */
1294 if (IGMP_V1_SEEN(in_dev))
1295 type = IGMP_HOST_MEMBERSHIP_REPORT;
1296 else if (IGMP_V2_SEEN(in_dev))
1297 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1298 else
1299 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1300 igmp_send_report(in_dev, im, type);
1301 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001302#endif
1303}
Eric Dumazet866f3b22010-11-18 09:33:19 -08001304EXPORT_SYMBOL(ip_mc_rejoin_groups);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001305
1306/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * A socket has left a multicast group on device dev
1308 */
1309
Al Viro8f935bb2006-09-27 18:30:07 -07001310void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001312 struct ip_mc_list *i;
1313 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001316
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001317 for (ip = &in_dev->mc_list;
1318 (i = rtnl_dereference(*ip)) != NULL;
1319 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001320 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 if (--i->users == 0) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001322 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001323 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 igmp_group_dropped(i);
1325
1326 if (!in_dev->dead)
1327 ip_rt_multicast_event(in_dev);
1328
1329 ip_ma_put(i);
1330 return;
1331 }
1332 break;
1333 }
1334 }
1335}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001336EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Moni Shoua75c78502009-09-15 02:37:40 -07001338/* Device changing type */
1339
1340void ip_mc_unmap(struct in_device *in_dev)
1341{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001342 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001343
1344 ASSERT_RTNL();
1345
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001346 for_each_pmc_rtnl(in_dev, pmc)
1347 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001348}
1349
1350void ip_mc_remap(struct in_device *in_dev)
1351{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001352 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001353
1354 ASSERT_RTNL();
1355
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001356 for_each_pmc_rtnl(in_dev, pmc)
1357 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/* Device going down */
1361
1362void ip_mc_down(struct in_device *in_dev)
1363{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001364 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 ASSERT_RTNL();
1367
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001368 for_each_pmc_rtnl(in_dev, pmc)
1369 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371#ifdef CONFIG_IP_MULTICAST
1372 in_dev->mr_ifc_count = 0;
1373 if (del_timer(&in_dev->mr_ifc_timer))
1374 __in_dev_put(in_dev);
1375 in_dev->mr_gq_running = 0;
1376 if (del_timer(&in_dev->mr_gq_timer))
1377 __in_dev_put(in_dev);
1378 igmpv3_clear_delrec(in_dev);
1379#endif
1380
1381 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1382}
1383
1384void ip_mc_init_dev(struct in_device *in_dev)
1385{
1386 ASSERT_RTNL();
1387
1388 in_dev->mc_tomb = NULL;
1389#ifdef CONFIG_IP_MULTICAST
1390 in_dev->mr_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001391 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1392 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 in_dev->mr_ifc_count = 0;
Rami Rosenb8bae412008-10-07 15:34:37 -07001394 in_dev->mc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001395 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1396 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1398#endif
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 spin_lock_init(&in_dev->mc_tomb_lock);
1401}
1402
1403/* Device going up */
1404
1405void ip_mc_up(struct in_device *in_dev)
1406{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001407 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 ASSERT_RTNL();
1410
1411 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1412
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001413 for_each_pmc_rtnl(in_dev, pmc)
1414 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417/*
1418 * Device is about to be destroyed: clean up.
1419 */
1420
1421void ip_mc_destroy_dev(struct in_device *in_dev)
1422{
1423 struct ip_mc_list *i;
1424
1425 ASSERT_RTNL();
1426
1427 /* Deactivate timers */
1428 ip_mc_down(in_dev);
1429
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001430 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1431 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001432 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 igmp_group_dropped(i);
1435 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001439/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001440static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Changli Gao58116622010-11-12 18:43:55 +00001442 struct flowi fl = { .fl4_dst = imr->imr_multiaddr.s_addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct rtable *rt;
1444 struct net_device *dev = NULL;
1445 struct in_device *idev = NULL;
1446
1447 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001448 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 return idev;
1450 }
1451 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001452 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (!dev)
1454 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456
Daniel Lezcano877aced2008-08-13 16:15:57 -07001457 if (!dev && !ip_route_output_key(net, &rt, &fl)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001458 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 ip_rt_put(rt);
1460 }
1461 if (dev) {
1462 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001463 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465 return idev;
1466}
1467
1468/*
1469 * Join a socket to a group
1470 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001471int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1472int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474
1475static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001476 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct ip_sf_list *psf, *psf_prev;
1479 int rv = 0;
1480
1481 psf_prev = NULL;
1482 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1483 if (psf->sf_inaddr == *psfsrc)
1484 break;
1485 psf_prev = psf;
1486 }
1487 if (!psf || psf->sf_count[sfmode] == 0) {
1488 /* source filter not found, or count wrong => bug */
1489 return -ESRCH;
1490 }
1491 psf->sf_count[sfmode]--;
1492 if (psf->sf_count[sfmode] == 0) {
1493 ip_rt_multicast_event(pmc->interface);
1494 }
1495 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1496#ifdef CONFIG_IP_MULTICAST
1497 struct in_device *in_dev = pmc->interface;
1498#endif
1499
1500 /* no more filters for this source */
1501 if (psf_prev)
1502 psf_prev->sf_next = psf->sf_next;
1503 else
1504 pmc->sources = psf->sf_next;
1505#ifdef CONFIG_IP_MULTICAST
1506 if (psf->sf_oldin &&
1507 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001508 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 IGMP_Unsolicited_Report_Count;
1510 psf->sf_next = pmc->tomb;
1511 pmc->tomb = psf;
1512 rv = 1;
1513 } else
1514#endif
1515 kfree(psf);
1516 }
1517 return rv;
1518}
1519
1520#ifndef CONFIG_IP_MULTICAST
1521#define igmp_ifc_event(x) do { } while (0)
1522#endif
1523
Al Viro8f935bb2006-09-27 18:30:07 -07001524static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1525 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct ip_mc_list *pmc;
1528 int changerec = 0;
1529 int i, err;
1530
1531 if (!in_dev)
1532 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001533 rcu_read_lock();
1534 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (*pmca == pmc->multiaddr)
1536 break;
1537 }
1538 if (!pmc) {
1539 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001540 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return -ESRCH;
1542 }
1543 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001544 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545#ifdef CONFIG_IP_MULTICAST
1546 sf_markstate(pmc);
1547#endif
1548 if (!delta) {
1549 err = -EINVAL;
1550 if (!pmc->sfcount[sfmode])
1551 goto out_unlock;
1552 pmc->sfcount[sfmode]--;
1553 }
1554 err = 0;
1555 for (i=0; i<sfcount; i++) {
1556 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1557
1558 changerec |= rv > 0;
1559 if (!err && rv < 0)
1560 err = rv;
1561 }
1562 if (pmc->sfmode == MCAST_EXCLUDE &&
1563 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1564 pmc->sfcount[MCAST_INCLUDE]) {
1565#ifdef CONFIG_IP_MULTICAST
1566 struct ip_sf_list *psf;
1567#endif
1568
1569 /* filter mode change */
1570 pmc->sfmode = MCAST_INCLUDE;
1571#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001572 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 IGMP_Unsolicited_Report_Count;
1574 in_dev->mr_ifc_count = pmc->crcount;
1575 for (psf=pmc->sources; psf; psf = psf->sf_next)
1576 psf->sf_crcount = 0;
1577 igmp_ifc_event(pmc->interface);
1578 } else if (sf_setstate(pmc) || changerec) {
1579 igmp_ifc_event(pmc->interface);
1580#endif
1581 }
1582out_unlock:
1583 spin_unlock_bh(&pmc->lock);
1584 return err;
1585}
1586
1587/*
1588 * Add multicast single-source filter to the interface list
1589 */
1590static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001591 __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 struct ip_sf_list *psf, *psf_prev;
1594
1595 psf_prev = NULL;
1596 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1597 if (psf->sf_inaddr == *psfsrc)
1598 break;
1599 psf_prev = psf;
1600 }
1601 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001602 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if (!psf)
1604 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 psf->sf_inaddr = *psfsrc;
1606 if (psf_prev) {
1607 psf_prev->sf_next = psf;
1608 } else
1609 pmc->sources = psf;
1610 }
1611 psf->sf_count[sfmode]++;
1612 if (psf->sf_count[sfmode] == 1) {
1613 ip_rt_multicast_event(pmc->interface);
1614 }
1615 return 0;
1616}
1617
1618#ifdef CONFIG_IP_MULTICAST
1619static void sf_markstate(struct ip_mc_list *pmc)
1620{
1621 struct ip_sf_list *psf;
1622 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1623
1624 for (psf=pmc->sources; psf; psf=psf->sf_next)
1625 if (pmc->sfcount[MCAST_EXCLUDE]) {
1626 psf->sf_oldin = mca_xcount ==
1627 psf->sf_count[MCAST_EXCLUDE] &&
1628 !psf->sf_count[MCAST_INCLUDE];
1629 } else
1630 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1631}
1632
1633static int sf_setstate(struct ip_mc_list *pmc)
1634{
David L Stevensad125832006-01-18 14:20:56 -08001635 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1637 int qrv = pmc->interface->mr_qrv;
1638 int new_in, rv;
1639
1640 rv = 0;
1641 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1642 if (pmc->sfcount[MCAST_EXCLUDE]) {
1643 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1644 !psf->sf_count[MCAST_INCLUDE];
1645 } else
1646 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001647 if (new_in) {
1648 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001649 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001650
1651 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1652 if (dpsf->sf_inaddr == psf->sf_inaddr)
1653 break;
1654 prev = dpsf;
1655 }
1656 if (dpsf) {
1657 if (prev)
1658 prev->sf_next = dpsf->sf_next;
1659 else
1660 pmc->tomb = dpsf->sf_next;
1661 kfree(dpsf);
1662 }
1663 psf->sf_crcount = qrv;
1664 rv++;
1665 }
1666 } else if (psf->sf_oldin) {
1667
1668 psf->sf_crcount = 0;
1669 /*
1670 * add or update "delete" records if an active filter
1671 * is now inactive
1672 */
1673 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1674 if (dpsf->sf_inaddr == psf->sf_inaddr)
1675 break;
1676 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001677 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001678 if (!dpsf)
1679 continue;
1680 *dpsf = *psf;
1681 /* pmc->lock held by callers */
1682 dpsf->sf_next = pmc->tomb;
1683 pmc->tomb = dpsf;
1684 }
1685 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 rv++;
1687 }
1688 }
1689 return rv;
1690}
1691#endif
1692
1693/*
1694 * Add multicast source filter list to the interface list
1695 */
Al Viro8f935bb2006-09-27 18:30:07 -07001696static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1697 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 struct ip_mc_list *pmc;
1700 int isexclude;
1701 int i, err;
1702
1703 if (!in_dev)
1704 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001705 rcu_read_lock();
1706 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (*pmca == pmc->multiaddr)
1708 break;
1709 }
1710 if (!pmc) {
1711 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001712 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return -ESRCH;
1714 }
1715 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001716 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718#ifdef CONFIG_IP_MULTICAST
1719 sf_markstate(pmc);
1720#endif
1721 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1722 if (!delta)
1723 pmc->sfcount[sfmode]++;
1724 err = 0;
1725 for (i=0; i<sfcount; i++) {
1726 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1727 if (err)
1728 break;
1729 }
1730 if (err) {
1731 int j;
1732
1733 pmc->sfcount[sfmode]--;
1734 for (j=0; j<i; j++)
1735 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1736 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1737#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001739 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740#endif
1741
1742 /* filter mode change */
1743 if (pmc->sfcount[MCAST_EXCLUDE])
1744 pmc->sfmode = MCAST_EXCLUDE;
1745 else if (pmc->sfcount[MCAST_INCLUDE])
1746 pmc->sfmode = MCAST_INCLUDE;
1747#ifdef CONFIG_IP_MULTICAST
1748 /* else no filters; keep old mode for reports */
1749
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001750 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 IGMP_Unsolicited_Report_Count;
1752 in_dev->mr_ifc_count = pmc->crcount;
1753 for (psf=pmc->sources; psf; psf = psf->sf_next)
1754 psf->sf_crcount = 0;
1755 igmp_ifc_event(in_dev);
1756 } else if (sf_setstate(pmc)) {
1757 igmp_ifc_event(in_dev);
1758#endif
1759 }
1760 spin_unlock_bh(&pmc->lock);
1761 return err;
1762}
1763
1764static void ip_mc_clear_src(struct ip_mc_list *pmc)
1765{
1766 struct ip_sf_list *psf, *nextpsf;
1767
1768 for (psf=pmc->tomb; psf; psf=nextpsf) {
1769 nextpsf = psf->sf_next;
1770 kfree(psf);
1771 }
1772 pmc->tomb = NULL;
1773 for (psf=pmc->sources; psf; psf=nextpsf) {
1774 nextpsf = psf->sf_next;
1775 kfree(psf);
1776 }
1777 pmc->sources = NULL;
1778 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001779 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 pmc->sfcount[MCAST_EXCLUDE] = 1;
1781}
1782
1783
1784/*
1785 * Join a multicast group
1786 */
1787int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1788{
1789 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001790 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001791 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct in_device *in_dev;
1793 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001794 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001795 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 int count = 0;
1797
Joe Perchesf97c1e02007-12-16 13:45:43 -08001798 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 return -EINVAL;
1800
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001801 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Daniel Lezcano877aced2008-08-13 16:15:57 -07001803 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 if (!in_dev) {
1806 iml = NULL;
1807 err = -ENODEV;
1808 goto done;
1809 }
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001812 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001813 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001814 if (i->multi.imr_multiaddr.s_addr == addr &&
1815 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 count++;
1818 }
1819 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001820 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001822 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001823 if (iml == NULL)
1824 goto done;
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001827 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 iml->sflist = NULL;
1829 iml->sfmode = MCAST_EXCLUDE;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001830 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001834 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return err;
1836}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001837EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001839static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1840{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001841 kfree(container_of(rp, struct ip_sf_socklist, rcu));
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001842 /* sk_omem_alloc should have been decreased by the caller*/
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001843}
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1846 struct in_device *in_dev)
1847{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001848 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 int err;
1850
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001851 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /* any-source empty exclude case */
1853 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1854 iml->sfmode, 0, NULL, 0);
1855 }
1856 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001857 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1858 rcu_assign_pointer(iml->sflist, NULL);
1859 /* decrease mem now to avoid the memleak warning */
1860 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1861 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return err;
1863}
1864
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001865
1866static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1867{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001868 kfree(container_of(rp, struct ip_mc_socklist, rcu));
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001869 /* sk_omem_alloc should have been decreased by the caller*/
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001870}
1871
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873/*
1874 * Ask a socket to leave a group.
1875 */
1876
1877int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1878{
1879 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001880 struct ip_mc_socklist *iml;
1881 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001882 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001883 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001884 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001885 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001886 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001889 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001890 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001891 for (imlp = &inet->mc_list;
1892 (iml = rtnl_dereference(*imlp)) != NULL;
1893 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001894 if (iml->multi.imr_multiaddr.s_addr != group)
1895 continue;
1896 if (ifindex) {
1897 if (iml->multi.imr_ifindex != ifindex)
1898 continue;
1899 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1900 iml->multi.imr_address.s_addr)
1901 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
David L Stevensacd6e002006-08-17 16:27:39 -07001903 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001905 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001906
1907 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001908 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001909 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001910 /* decrease mem now to avoid the memleak warning */
1911 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1912 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
David L Stevensacd6e002006-08-17 16:27:39 -07001913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
David L Stevensacd6e002006-08-17 16:27:39 -07001915 if (!in_dev)
1916 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001918 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
1921int ip_mc_source(int add, int omode, struct sock *sk, struct
1922 ip_mreq_source *mreqs, int ifindex)
1923{
1924 int err;
1925 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001926 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 struct ip_mc_socklist *pmc;
1928 struct in_device *in_dev = NULL;
1929 struct inet_sock *inet = inet_sk(sk);
1930 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001931 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001932 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 int i, j, rv;
1934
Joe Perchesf97c1e02007-12-16 13:45:43 -08001935 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return -EINVAL;
1937
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001938 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1941 imr.imr_address.s_addr = mreqs->imr_interface;
1942 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001943 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 if (!in_dev) {
1946 err = -ENODEV;
1947 goto done;
1948 }
1949 err = -EADDRNOTAVAIL;
1950
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001951 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08001952 if ((pmc->multi.imr_multiaddr.s_addr ==
1953 imr.imr_multiaddr.s_addr) &&
1954 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
1956 }
David L Stevens917f2f12005-07-08 17:45:16 -07001957 if (!pmc) { /* must have a prior join */
1958 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /* if a source filter was set, must be the same mode as before */
1962 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07001963 if (pmc->sfmode != omode) {
1964 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 } else if (pmc->sfmode != omode) {
1968 /* allow mode switches for empty-set filters */
1969 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001970 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 NULL, 0);
1972 pmc->sfmode = omode;
1973 }
1974
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001975 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (!add) {
1977 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07001978 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 rv = !0;
1980 for (i=0; i<psl->sl_count; i++) {
1981 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001982 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (rv == 0)
1984 break;
1985 }
1986 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07001987 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
David L Stevens8cdaaa12005-07-08 17:39:23 -07001989 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1990 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1991 leavegroup = 1;
1992 goto done;
1993 }
1994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001996 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 &mreqs->imr_sourceaddr, 1);
1998
1999 for (j=i+1; j<psl->sl_count; j++)
2000 psl->sl_addr[j-1] = psl->sl_addr[j];
2001 psl->sl_count--;
2002 err = 0;
2003 goto done;
2004 }
2005 /* else, add a new source to the filter */
2006
2007 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2008 err = -ENOBUFS;
2009 goto done;
2010 }
2011 if (!psl || psl->sl_count == psl->sl_max) {
2012 struct ip_sf_socklist *newpsl;
2013 int count = IP_SFBLOCK;
2014
2015 if (psl)
2016 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002017 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (!newpsl) {
2019 err = -ENOBUFS;
2020 goto done;
2021 }
2022 newpsl->sl_max = count;
2023 newpsl->sl_count = count - IP_SFBLOCK;
2024 if (psl) {
2025 for (i=0; i<psl->sl_count; i++)
2026 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002027 /* decrease mem now to avoid the memleak warning */
2028 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2029 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002031 rcu_assign_pointer(pmc->sflist, newpsl);
2032 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2035 for (i=0; i<psl->sl_count; i++) {
2036 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002037 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (rv == 0)
2039 break;
2040 }
2041 if (rv == 0) /* address already there is an error */
2042 goto done;
2043 for (j=psl->sl_count-1; j>=i; j--)
2044 psl->sl_addr[j+1] = psl->sl_addr[j];
2045 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2046 psl->sl_count++;
2047 err = 0;
2048 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002049 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 &mreqs->imr_sourceaddr, 1);
2051done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002052 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002053 if (leavegroup)
2054 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 return err;
2056}
2057
2058int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2059{
David L Stevens9951f032005-07-08 17:47:28 -07002060 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002062 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct ip_mc_socklist *pmc;
2064 struct in_device *in_dev;
2065 struct inet_sock *inet = inet_sk(sk);
2066 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002067 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002068 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Joe Perchesf97c1e02007-12-16 13:45:43 -08002070 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return -EINVAL;
2072 if (msf->imsf_fmode != MCAST_INCLUDE &&
2073 msf->imsf_fmode != MCAST_EXCLUDE)
2074 return -EINVAL;
2075
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002076 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2079 imr.imr_address.s_addr = msf->imsf_interface;
2080 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002081 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 if (!in_dev) {
2084 err = -ENODEV;
2085 goto done;
2086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
David L Stevens9951f032005-07-08 17:47:28 -07002088 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2089 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2090 leavegroup = 1;
2091 goto done;
2092 }
2093
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002094 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2096 pmc->multi.imr_ifindex == imr.imr_ifindex)
2097 break;
2098 }
David L Stevens917f2f12005-07-08 17:45:16 -07002099 if (!pmc) { /* must have a prior join */
2100 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002104 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2105 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (!newpsl) {
2107 err = -ENOBUFS;
2108 goto done;
2109 }
2110 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2111 memcpy(newpsl->sl_addr, msf->imsf_slist,
2112 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2113 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2114 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2115 if (err) {
2116 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2117 goto done;
2118 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002119 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002121 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2122 msf->imsf_fmode, 0, NULL, 0);
2123 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002124 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (psl) {
2126 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2127 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002128 /* decrease mem now to avoid the memleak warning */
2129 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2130 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 } else
2132 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2133 0, NULL, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002134 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002136 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002138 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002139 if (leavegroup)
2140 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return err;
2142}
2143
2144int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2145 struct ip_msfilter __user *optval, int __user *optlen)
2146{
2147 int err, len, count, copycount;
2148 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002149 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 struct ip_mc_socklist *pmc;
2151 struct in_device *in_dev;
2152 struct inet_sock *inet = inet_sk(sk);
2153 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002154 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Joe Perchesf97c1e02007-12-16 13:45:43 -08002156 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return -EINVAL;
2158
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002159 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2162 imr.imr_address.s_addr = msf->imsf_interface;
2163 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002164 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166 if (!in_dev) {
2167 err = -ENODEV;
2168 goto done;
2169 }
2170 err = -EADDRNOTAVAIL;
2171
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002172 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2174 pmc->multi.imr_ifindex == imr.imr_ifindex)
2175 break;
2176 }
2177 if (!pmc) /* must have a prior join */
2178 goto done;
2179 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002180 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002181 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 if (!psl) {
2183 len = 0;
2184 count = 0;
2185 } else {
2186 count = psl->sl_count;
2187 }
2188 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2189 len = copycount * sizeof(psl->sl_addr[0]);
2190 msf->imsf_numsrc = count;
2191 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2192 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2193 return -EFAULT;
2194 }
2195 if (len &&
2196 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2197 return -EFAULT;
2198 return 0;
2199done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002200 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return err;
2202}
2203
2204int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2205 struct group_filter __user *optval, int __user *optlen)
2206{
2207 int err, i, count, copycount;
2208 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002209 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 struct ip_mc_socklist *pmc;
2211 struct inet_sock *inet = inet_sk(sk);
2212 struct ip_sf_socklist *psl;
2213
2214 psin = (struct sockaddr_in *)&gsf->gf_group;
2215 if (psin->sin_family != AF_INET)
2216 return -EINVAL;
2217 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002218 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return -EINVAL;
2220
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002221 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 err = -EADDRNOTAVAIL;
2224
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002225 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2227 pmc->multi.imr_ifindex == gsf->gf_interface)
2228 break;
2229 }
2230 if (!pmc) /* must have a prior join */
2231 goto done;
2232 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002233 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002234 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 count = psl ? psl->sl_count : 0;
2236 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2237 gsf->gf_numsrc = count;
2238 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2239 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2240 return -EFAULT;
2241 }
2242 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 struct sockaddr_storage ss;
2244
2245 psin = (struct sockaddr_in *)&ss;
2246 memset(&ss, 0, sizeof(ss));
2247 psin->sin_family = AF_INET;
2248 psin->sin_addr.s_addr = psl->sl_addr[i];
2249 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2250 return -EFAULT;
2251 }
2252 return 0;
2253done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002254 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return err;
2256}
2257
2258/*
2259 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2260 */
Al Viroc0cda062006-09-27 18:31:10 -07002261int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262{
2263 struct inet_sock *inet = inet_sk(sk);
2264 struct ip_mc_socklist *pmc;
2265 struct ip_sf_socklist *psl;
2266 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002267 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002269 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002270 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002271 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002273 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002274 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2276 pmc->multi.imr_ifindex == dif)
2277 break;
2278 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002279 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002281 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002282 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002283 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002285 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 for (i=0; i<psl->sl_count; i++) {
2288 if (psl->sl_addr[i] == rmt_addr)
2289 break;
2290 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002291 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002293 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002295 goto unlock;
2296 ret = 1;
2297unlock:
2298 rcu_read_unlock();
2299out:
2300 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303/*
2304 * A socket is closing.
2305 */
2306
2307void ip_mc_drop_socket(struct sock *sk)
2308{
2309 struct inet_sock *inet = inet_sk(sk);
2310 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002311 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 if (inet->mc_list == NULL)
2314 return;
2315
2316 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002317 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002320 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002321 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002322 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002323 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002325 /* decrease mem now to avoid the memleak warning */
2326 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2327 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329 rtnl_unlock();
2330}
2331
Al Viroa60c4922006-09-26 21:28:34 -07002332int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
2334 struct ip_mc_list *im;
2335 struct ip_sf_list *psf;
2336 int rv = 0;
2337
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002338 rcu_read_lock();
2339 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (im->multiaddr == mc_addr)
2341 break;
2342 }
2343 if (im && proto == IPPROTO_IGMP) {
2344 rv = 1;
2345 } else if (im) {
2346 if (src_addr) {
2347 for (psf=im->sources; psf; psf=psf->sf_next) {
2348 if (psf->sf_inaddr == src_addr)
2349 break;
2350 }
2351 if (psf)
2352 rv = psf->sf_count[MCAST_INCLUDE] ||
2353 psf->sf_count[MCAST_EXCLUDE] !=
2354 im->sfcount[MCAST_EXCLUDE];
2355 else
2356 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2357 } else
2358 rv = 1; /* unspecified source; tentatively allow */
2359 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return rv;
2362}
2363
2364#if defined(CONFIG_PROC_FS)
2365struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002366 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct net_device *dev;
2368 struct in_device *in_dev;
2369};
2370
2371#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2372
2373static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2374{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002375 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 struct ip_mc_list *im = NULL;
2377 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2378
Pavel Emelianov7562f872007-05-03 15:13:45 -07002379 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002380 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002382
2383 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if (!in_dev)
2385 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002386 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (im) {
2388 state->in_dev = in_dev;
2389 break;
2390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392 return im;
2393}
2394
2395static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2396{
2397 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002398
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002399 im = rcu_dereference(im->next_rcu);
2400 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002401 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (!state->dev) {
2403 state->in_dev = NULL;
2404 break;
2405 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002406 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (!state->in_dev)
2408 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002409 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411 return im;
2412}
2413
2414static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2415{
2416 struct ip_mc_list *im = igmp_mc_get_first(seq);
2417 if (im)
2418 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2419 --pos;
2420 return pos ? NULL : im;
2421}
2422
2423static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002424 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425{
stephen hemminger61fbab72009-11-10 07:54:55 +00002426 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2428}
2429
2430static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2431{
2432 struct ip_mc_list *im;
2433 if (v == SEQ_START_TOKEN)
2434 im = igmp_mc_get_first(seq);
2435 else
2436 im = igmp_mc_get_next(seq, v);
2437 ++*pos;
2438 return im;
2439}
2440
2441static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002442 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
2444 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002445
2446 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002448 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
2451static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2452{
2453 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002454 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2456 else {
2457 struct ip_mc_list *im = (struct ip_mc_list *)v;
2458 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2459 char *querier;
2460#ifdef CONFIG_IP_MULTICAST
2461 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2462 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2463 "V3";
2464#else
2465 querier = "NONE";
2466#endif
2467
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002468 if (rcu_dereference(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002470 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472
2473 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002474 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 im->multiaddr, im->users,
2476 im->tm_running, im->tm_running ?
2477 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2478 im->reporter);
2479 }
2480 return 0;
2481}
2482
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002483static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 .start = igmp_mc_seq_start,
2485 .next = igmp_mc_seq_next,
2486 .stop = igmp_mc_seq_stop,
2487 .show = igmp_mc_seq_show,
2488};
2489
2490static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2491{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002492 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002493 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
Arjan van de Ven9a321442007-02-12 00:55:35 -08002496static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 .owner = THIS_MODULE,
2498 .open = igmp_mc_seq_open,
2499 .read = seq_read,
2500 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002501 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502};
2503
2504struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002505 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 struct net_device *dev;
2507 struct in_device *idev;
2508 struct ip_mc_list *im;
2509};
2510
2511#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2512
2513static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2514{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002515 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct ip_sf_list *psf = NULL;
2517 struct ip_mc_list *im = NULL;
2518 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2519
Pavel Emelianov7562f872007-05-03 15:13:45 -07002520 state->idev = NULL;
2521 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002522 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002524 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (unlikely(idev == NULL))
2526 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002527 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (likely(im != NULL)) {
2529 spin_lock_bh(&im->lock);
2530 psf = im->sources;
2531 if (likely(psf != NULL)) {
2532 state->im = im;
2533 state->idev = idev;
2534 break;
2535 }
2536 spin_unlock_bh(&im->lock);
2537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539 return psf;
2540}
2541
2542static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2543{
2544 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2545
2546 psf = psf->sf_next;
2547 while (!psf) {
2548 spin_unlock_bh(&state->im->lock);
2549 state->im = state->im->next;
2550 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002551 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (!state->dev) {
2553 state->idev = NULL;
2554 goto out;
2555 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002556 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (!state->idev)
2558 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002559 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 if (!state->im)
2562 break;
2563 spin_lock_bh(&state->im->lock);
2564 psf = state->im->sources;
2565 }
2566out:
2567 return psf;
2568}
2569
2570static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2571{
2572 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2573 if (psf)
2574 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2575 --pos;
2576 return pos ? NULL : psf;
2577}
2578
2579static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002580 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
stephen hemminger61fbab72009-11-10 07:54:55 +00002582 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2584}
2585
2586static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2587{
2588 struct ip_sf_list *psf;
2589 if (v == SEQ_START_TOKEN)
2590 psf = igmp_mcf_get_first(seq);
2591 else
2592 psf = igmp_mcf_get_next(seq, v);
2593 ++*pos;
2594 return psf;
2595}
2596
2597static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002598 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
2600 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2601 if (likely(state->im != NULL)) {
2602 spin_unlock_bh(&state->im->lock);
2603 state->im = NULL;
2604 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002605 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002607 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
2610static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2611{
2612 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2613 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2614
2615 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002616 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 "%3s %6s "
2618 "%10s %10s %6s %6s\n", "Idx",
2619 "Device", "MCA",
2620 "SRC", "INC", "EXC");
2621 } else {
2622 seq_printf(seq,
2623 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002624 "0x%08x %6lu %6lu\n",
2625 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 ntohl(state->im->multiaddr),
2627 ntohl(psf->sf_inaddr),
2628 psf->sf_count[MCAST_INCLUDE],
2629 psf->sf_count[MCAST_EXCLUDE]);
2630 }
2631 return 0;
2632}
2633
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002634static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 .start = igmp_mcf_seq_start,
2636 .next = igmp_mcf_seq_next,
2637 .stop = igmp_mcf_seq_stop,
2638 .show = igmp_mcf_seq_show,
2639};
2640
2641static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2642{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002643 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002644 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
Arjan van de Ven9a321442007-02-12 00:55:35 -08002647static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 .owner = THIS_MODULE,
2649 .open = igmp_mcf_seq_open,
2650 .read = seq_read,
2651 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002652 .release = seq_release_net,
2653};
2654
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002655static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002656{
2657 struct proc_dir_entry *pde;
2658
2659 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2660 if (!pde)
2661 goto out_igmp;
2662 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2663 if (!pde)
2664 goto out_mcfilter;
2665 return 0;
2666
2667out_mcfilter:
2668 proc_net_remove(net, "igmp");
2669out_igmp:
2670 return -ENOMEM;
2671}
2672
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002673static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002674{
2675 proc_net_remove(net, "mcfilter");
2676 proc_net_remove(net, "igmp");
2677}
2678
2679static struct pernet_operations igmp_net_ops = {
2680 .init = igmp_net_init,
2681 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682};
2683
2684int __init igmp_mc_proc_init(void)
2685{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002686 return register_pernet_subsys(&igmp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687}
2688#endif