blob: e0d42dbb33feb9a040cdf2a467028afbd20da2e7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
3 *
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
6 *
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Authors:
Alan Cox113aa832008-10-13 19:01:08 -070012 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * Fixes:
20 *
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
23 * functions.
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
32 * points.
33 *
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090036 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050 * memberships but was being deleted,
51 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * from %p with timer not initialized\n"
53 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090054 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
58 * true (960216).
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
70 * Vinay Kulkarni
71 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/uaccess.h>
76#include <asm/system.h>
77#include <linux/types.h>
78#include <linux/kernel.h>
79#include <linux/jiffies.h>
80#include <linux/string.h>
81#include <linux/socket.h>
82#include <linux/sockios.h>
83#include <linux/in.h>
84#include <linux/inet.h>
85#include <linux/netdevice.h>
86#include <linux/skbuff.h>
87#include <linux/inetdevice.h>
88#include <linux/igmp.h>
89#include <linux/if_arp.h>
90#include <linux/rtnetlink.h>
91#include <linux/times.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020092
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020093#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020094#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <net/ip.h>
96#include <net/protocol.h>
97#include <net/route.h>
98#include <net/sock.h>
99#include <net/checksum.h>
100#include <linux/netfilter_ipv4.h>
101#ifdef CONFIG_IP_MROUTE
102#include <linux/mroute.h>
103#endif
104#ifdef CONFIG_PROC_FS
105#include <linux/proc_fs.h>
106#include <linux/seq_file.h>
107#endif
108
109#define IP_MAX_MEMBERSHIPS 20
110#define IP_MAX_MSF 10
111
112#ifdef CONFIG_IP_MULTICAST
113/* Parameter names and values are taken from igmp-v2-06 draft */
114
115#define IGMP_V1_Router_Present_Timeout (400*HZ)
116#define IGMP_V2_Router_Present_Timeout (400*HZ)
117#define IGMP_Unsolicited_Report_Interval (10*HZ)
118#define IGMP_Query_Response_Interval (10*HZ)
119#define IGMP_Unsolicited_Report_Count 2
120
121
122#define IGMP_Initial_Report_Delay (1)
123
124/* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
129 */
130
Herbert Xu42f811b2007-06-04 23:34:44 -0700131#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700143static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static void igmpv3_clear_delrec(struct in_device *in_dev);
145static int sf_setstate(struct ip_mc_list *pmc);
146static void sf_markstate(struct ip_mc_list *pmc);
147#endif
148static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700149static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static void ip_ma_put(struct ip_mc_list *im)
153{
154 if (atomic_dec_and_test(&im->refcnt)) {
155 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800156 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158}
159
David S. Millerd9aa9382010-11-15 08:52:02 -0800160#define for_each_pmc_rcu(in_dev, pmc) \
161 for (pmc = rcu_dereference(in_dev->mc_list); \
162 pmc != NULL; \
163 pmc = rcu_dereference(pmc->next_rcu))
164
165#define for_each_pmc_rtnl(in_dev, pmc) \
166 for (pmc = rtnl_dereference(in_dev->mc_list); \
167 pmc != NULL; \
168 pmc = rtnl_dereference(pmc->next_rcu))
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#ifdef CONFIG_IP_MULTICAST
171
172/*
173 * Timer management
174 */
175
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000176static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 spin_lock_bh(&im->lock);
179 if (del_timer(&im->timer))
180 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800181 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 im->reporter = 0;
183 im->unsolicit_count = 0;
184 spin_unlock_bh(&im->lock);
185}
186
187/* It must be called with locked im->lock */
188static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
189{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800190 int tv = net_random() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800192 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!mod_timer(&im->timer, jiffies+tv+2))
194 atomic_inc(&im->refcnt);
195}
196
197static void igmp_gq_start_timer(struct in_device *in_dev)
198{
199 int tv = net_random() % in_dev->mr_maxdelay;
200
201 in_dev->mr_gq_running = 1;
202 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
203 in_dev_hold(in_dev);
204}
205
206static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
207{
208 int tv = net_random() % delay;
209
210 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
211 in_dev_hold(in_dev);
212}
213
214static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
215{
216 spin_lock_bh(&im->lock);
217 im->unsolicit_count = 0;
218 if (del_timer(&im->timer)) {
219 if ((long)(im->timer.expires-jiffies) < max_delay) {
220 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800221 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 spin_unlock_bh(&im->lock);
223 return;
224 }
225 atomic_dec(&im->refcnt);
226 }
227 igmp_start_timer(im, max_delay);
228 spin_unlock_bh(&im->lock);
229}
230
231
232/*
233 * Send an IGMP report.
234 */
235
236#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
237
238
239static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
240 int gdeleted, int sdeleted)
241{
242 switch (type) {
243 case IGMPV3_MODE_IS_INCLUDE:
244 case IGMPV3_MODE_IS_EXCLUDE:
245 if (gdeleted || sdeleted)
246 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800247 if (!(pmc->gsquery && !psf->sf_gsresp)) {
248 if (pmc->sfmode == MCAST_INCLUDE)
249 return 1;
250 /* don't include if this source is excluded
251 * in all filters
252 */
253 if (psf->sf_count[MCAST_INCLUDE])
254 return type == IGMPV3_MODE_IS_INCLUDE;
255 return pmc->sfcount[MCAST_EXCLUDE] ==
256 psf->sf_count[MCAST_EXCLUDE];
257 }
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 case IGMPV3_CHANGE_TO_INCLUDE:
260 if (gdeleted || sdeleted)
261 return 0;
262 return psf->sf_count[MCAST_INCLUDE] != 0;
263 case IGMPV3_CHANGE_TO_EXCLUDE:
264 if (gdeleted || sdeleted)
265 return 0;
266 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
267 psf->sf_count[MCAST_INCLUDE])
268 return 0;
269 return pmc->sfcount[MCAST_EXCLUDE] ==
270 psf->sf_count[MCAST_EXCLUDE];
271 case IGMPV3_ALLOW_NEW_SOURCES:
272 if (gdeleted || !psf->sf_crcount)
273 return 0;
274 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
275 case IGMPV3_BLOCK_OLD_SOURCES:
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return gdeleted || (psf->sf_crcount && sdeleted);
278 return psf->sf_crcount && !gdeleted && !sdeleted;
279 }
280 return 0;
281}
282
283static int
284igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
285{
286 struct ip_sf_list *psf;
287 int scount = 0;
288
289 for (psf=pmc->sources; psf; psf=psf->sf_next) {
290 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
291 continue;
292 scount++;
293 }
294 return scount;
295}
296
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000297#define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
300{
301 struct sk_buff *skb;
302 struct rtable *rt;
303 struct iphdr *pip;
304 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700305 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700306 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000308 while (1) {
309 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
310 GFP_ATOMIC | __GFP_NOWARN);
311 if (skb)
312 break;
313 size >>= 1;
314 if (size < 256)
315 return NULL;
316 }
317 igmp_skb_size(skb) = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
David S. Miller31e45432011-05-03 20:25:42 -0700319 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500320 0, 0,
321 IPPROTO_IGMP, 0, dev->ifindex);
322 if (IS_ERR(rt)) {
323 kfree_skb(skb);
324 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Changli Gaod8d1f302010-06-10 23:31:35 -0700327 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 skb->dev = dev;
329
330 skb_reserve(skb, LL_RESERVED_SPACE(dev));
331
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300332 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700333 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300334 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 pip->version = 4;
337 pip->ihl = (sizeof(struct iphdr)+4)>>2;
338 pip->tos = 0xc0;
339 pip->frag_off = htons(IP_DF);
340 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700341 pip->daddr = fl4.daddr;
342 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 pip->protocol = IPPROTO_IGMP;
344 pip->tot_len = 0; /* filled in later */
Changli Gaod8d1f302010-06-10 23:31:35 -0700345 ip_select_ident(pip, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 ((u8*)&pip[1])[0] = IPOPT_RA;
347 ((u8*)&pip[1])[1] = 4;
348 ((u8*)&pip[1])[2] = 0;
349 ((u8*)&pip[1])[3] = 0;
350
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700351 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300352 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300353 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
355 pig->resv1 = 0;
356 pig->csum = 0;
357 pig->resv2 = 0;
358 pig->ngrec = 0;
359 return skb;
360}
361
362static int igmpv3_sendpack(struct sk_buff *skb)
363{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300364 struct igmphdr *pig = igmp_hdr(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700365 const int igmplen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300367 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Herbert Xuc439cb22008-01-11 19:14:00 -0800369 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
373{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800374 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
378 int type, struct igmpv3_grec **ppgr)
379{
380 struct net_device *dev = pmc->interface->dev;
381 struct igmpv3_report *pih;
382 struct igmpv3_grec *pgr;
383
384 if (!skb)
385 skb = igmpv3_newpack(dev, dev->mtu);
386 if (!skb)
387 return NULL;
388 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
389 pgr->grec_type = type;
390 pgr->grec_auxwords = 0;
391 pgr->grec_nsrcs = 0;
392 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300393 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 pih->ngrec = htons(ntohs(pih->ngrec)+1);
395 *ppgr = pgr;
396 return skb;
397}
398
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000399#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 skb_tailroom(skb)) : 0)
401
402static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
403 int type, int gdeleted, int sdeleted)
404{
405 struct net_device *dev = pmc->interface->dev;
406 struct igmpv3_report *pih;
407 struct igmpv3_grec *pgr = NULL;
408 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800409 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 if (pmc->multiaddr == IGMP_ALL_HOSTS)
412 return skb;
413
414 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
415 type == IGMPV3_MODE_IS_EXCLUDE;
416 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
417 type == IGMPV3_CHANGE_TO_EXCLUDE;
418
David L Stevensad125832006-01-18 14:20:56 -0800419 stotal = scount = 0;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
422
David L Stevensad125832006-01-18 14:20:56 -0800423 if (!*psf_list)
424 goto empty_source;
425
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300426 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 /* EX and TO_EX get a fresh packet, if needed */
429 if (truncate) {
430 if (pih && pih->ngrec &&
431 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
432 if (skb)
433 igmpv3_sendpack(skb);
434 skb = igmpv3_newpack(dev, dev->mtu);
435 }
436 }
437 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 psf_prev = NULL;
439 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700440 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 psf_next = psf->sf_next;
443
444 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
445 psf_prev = psf;
446 continue;
447 }
448
449 /* clear marks on query responses */
450 if (isquery)
451 psf->sf_gsresp = 0;
452
Al Viro63007722006-09-27 18:31:32 -0700453 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 first*sizeof(struct igmpv3_grec)) {
455 if (truncate && !first)
456 break; /* truncate these */
457 if (pgr)
458 pgr->grec_nsrcs = htons(scount);
459 if (skb)
460 igmpv3_sendpack(skb);
461 skb = igmpv3_newpack(dev, dev->mtu);
462 first = 1;
463 scount = 0;
464 }
465 if (first) {
466 skb = add_grhead(skb, pmc, type, &pgr);
467 first = 0;
468 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800469 if (!skb)
470 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700471 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800473 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
475 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
476 psf->sf_crcount--;
477 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
478 if (psf_prev)
479 psf_prev->sf_next = psf->sf_next;
480 else
481 *psf_list = psf->sf_next;
482 kfree(psf);
483 continue;
484 }
485 }
486 psf_prev = psf;
487 }
David L Stevensad125832006-01-18 14:20:56 -0800488
489empty_source:
490 if (!stotal) {
491 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
492 type == IGMPV3_BLOCK_OLD_SOURCES)
493 return skb;
494 if (pmc->crcount || isquery) {
495 /* make sure we have room for group header */
496 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
497 igmpv3_sendpack(skb);
498 skb = NULL; /* add_grhead will get a new one */
499 }
500 skb = add_grhead(skb, pmc, type, &pgr);
501 }
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (pgr)
504 pgr->grec_nsrcs = htons(scount);
505
506 if (isquery)
507 pmc->gsquery = 0; /* clear query state on report */
508 return skb;
509}
510
511static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
512{
513 struct sk_buff *skb = NULL;
514 int type;
515
516 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000517 rcu_read_lock();
518 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (pmc->multiaddr == IGMP_ALL_HOSTS)
520 continue;
521 spin_lock_bh(&pmc->lock);
522 if (pmc->sfcount[MCAST_EXCLUDE])
523 type = IGMPV3_MODE_IS_EXCLUDE;
524 else
525 type = IGMPV3_MODE_IS_INCLUDE;
526 skb = add_grec(skb, pmc, type, 0, 0);
527 spin_unlock_bh(&pmc->lock);
528 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000529 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 } else {
531 spin_lock_bh(&pmc->lock);
532 if (pmc->sfcount[MCAST_EXCLUDE])
533 type = IGMPV3_MODE_IS_EXCLUDE;
534 else
535 type = IGMPV3_MODE_IS_INCLUDE;
536 skb = add_grec(skb, pmc, type, 0, 0);
537 spin_unlock_bh(&pmc->lock);
538 }
539 if (!skb)
540 return 0;
541 return igmpv3_sendpack(skb);
542}
543
544/*
545 * remove zero-count source records from a source filter list
546 */
547static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
548{
549 struct ip_sf_list *psf_prev, *psf_next, *psf;
550
551 psf_prev = NULL;
552 for (psf=*ppsf; psf; psf = psf_next) {
553 psf_next = psf->sf_next;
554 if (psf->sf_crcount == 0) {
555 if (psf_prev)
556 psf_prev->sf_next = psf->sf_next;
557 else
558 *ppsf = psf->sf_next;
559 kfree(psf);
560 } else
561 psf_prev = psf;
562 }
563}
564
565static void igmpv3_send_cr(struct in_device *in_dev)
566{
567 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
568 struct sk_buff *skb = NULL;
569 int type, dtype;
570
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000571 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 spin_lock_bh(&in_dev->mc_tomb_lock);
573
574 /* deleted MCA's */
575 pmc_prev = NULL;
576 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
577 pmc_next = pmc->next;
578 if (pmc->sfmode == MCAST_INCLUDE) {
579 type = IGMPV3_BLOCK_OLD_SOURCES;
580 dtype = IGMPV3_BLOCK_OLD_SOURCES;
581 skb = add_grec(skb, pmc, type, 1, 0);
582 skb = add_grec(skb, pmc, dtype, 1, 1);
583 }
584 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (pmc->sfmode == MCAST_EXCLUDE) {
586 type = IGMPV3_CHANGE_TO_INCLUDE;
587 skb = add_grec(skb, pmc, type, 1, 0);
588 }
David L Stevensad125832006-01-18 14:20:56 -0800589 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (pmc->crcount == 0) {
591 igmpv3_clear_zeros(&pmc->tomb);
592 igmpv3_clear_zeros(&pmc->sources);
593 }
594 }
595 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
596 if (pmc_prev)
597 pmc_prev->next = pmc_next;
598 else
599 in_dev->mc_tomb = pmc_next;
600 in_dev_put(pmc->interface);
601 kfree(pmc);
602 } else
603 pmc_prev = pmc;
604 }
605 spin_unlock_bh(&in_dev->mc_tomb_lock);
606
607 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000608 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 spin_lock_bh(&pmc->lock);
610 if (pmc->sfcount[MCAST_EXCLUDE]) {
611 type = IGMPV3_BLOCK_OLD_SOURCES;
612 dtype = IGMPV3_ALLOW_NEW_SOURCES;
613 } else {
614 type = IGMPV3_ALLOW_NEW_SOURCES;
615 dtype = IGMPV3_BLOCK_OLD_SOURCES;
616 }
617 skb = add_grec(skb, pmc, type, 0, 0);
618 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
619
620 /* filter mode changes */
621 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (pmc->sfmode == MCAST_EXCLUDE)
623 type = IGMPV3_CHANGE_TO_EXCLUDE;
624 else
625 type = IGMPV3_CHANGE_TO_INCLUDE;
626 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800627 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
629 spin_unlock_bh(&pmc->lock);
630 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000631 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (!skb)
634 return;
635 (void) igmpv3_sendpack(skb);
636}
637
638static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
639 int type)
640{
641 struct sk_buff *skb;
642 struct iphdr *iph;
643 struct igmphdr *ih;
644 struct rtable *rt;
645 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700646 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700647 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700648 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700649 __be32 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
652 return igmpv3_send_report(in_dev, pmc);
653 else if (type == IGMP_HOST_LEAVE_MESSAGE)
654 dst = IGMP_ALL_ROUTER;
655 else
656 dst = group;
657
David S. Miller31e45432011-05-03 20:25:42 -0700658 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500659 0, 0,
660 IPPROTO_IGMP, 0, dev->ifindex);
661 if (IS_ERR(rt))
662 return -1;
663
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800664 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (skb == NULL) {
666 ip_rt_put(rt);
667 return -1;
668 }
669
Changli Gaod8d1f302010-06-10 23:31:35 -0700670 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 skb_reserve(skb, LL_RESERVED_SPACE(dev));
673
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300674 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700675 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300676 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 iph->version = 4;
679 iph->ihl = (sizeof(struct iphdr)+4)>>2;
680 iph->tos = 0xc0;
681 iph->frag_off = htons(IP_DF);
682 iph->ttl = 1;
683 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700684 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 iph->protocol = IPPROTO_IGMP;
Changli Gaod8d1f302010-06-10 23:31:35 -0700686 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 ((u8*)&iph[1])[0] = IPOPT_RA;
688 ((u8*)&iph[1])[1] = 4;
689 ((u8*)&iph[1])[2] = 0;
690 ((u8*)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800693 ih->type = type;
694 ih->code = 0;
695 ih->csum = 0;
696 ih->group = group;
697 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Herbert Xuc439cb22008-01-11 19:14:00 -0800699 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702static void igmp_gq_timer_expire(unsigned long data)
703{
704 struct in_device *in_dev = (struct in_device *)data;
705
706 in_dev->mr_gq_running = 0;
707 igmpv3_send_report(in_dev, NULL);
708 __in_dev_put(in_dev);
709}
710
711static void igmp_ifc_timer_expire(unsigned long data)
712{
713 struct in_device *in_dev = (struct in_device *)data;
714
715 igmpv3_send_cr(in_dev);
716 if (in_dev->mr_ifc_count) {
717 in_dev->mr_ifc_count--;
718 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
719 }
720 __in_dev_put(in_dev);
721}
722
723static void igmp_ifc_event(struct in_device *in_dev)
724{
725 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
726 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900727 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 IGMP_Unsolicited_Report_Count;
729 igmp_ifc_start_timer(in_dev, 1);
730}
731
732
733static void igmp_timer_expire(unsigned long data)
734{
735 struct ip_mc_list *im=(struct ip_mc_list *)data;
736 struct in_device *in_dev = im->interface;
737
738 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800739 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (im->unsolicit_count) {
742 im->unsolicit_count--;
743 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
744 }
745 im->reporter = 1;
746 spin_unlock(&im->lock);
747
748 if (IGMP_V1_SEEN(in_dev))
749 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
750 else if (IGMP_V2_SEEN(in_dev))
751 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
752 else
753 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
754
755 ip_ma_put(im);
756}
757
David L Stevensad125832006-01-18 14:20:56 -0800758/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700759static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
761 struct ip_sf_list *psf;
762 int i, scount;
763
764 scount = 0;
765 for (psf=pmc->sources; psf; psf=psf->sf_next) {
766 if (scount == nsrcs)
767 break;
David L Stevensad125832006-01-18 14:20:56 -0800768 for (i=0; i<nsrcs; i++) {
769 /* skip inactive filters */
Yan, Zheng2ce655e2011-08-23 22:54:37 +0000770 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800771 pmc->sfcount[MCAST_EXCLUDE] !=
772 psf->sf_count[MCAST_EXCLUDE])
773 continue;
774 if (srcs[i] == psf->sf_inaddr) {
775 scount++;
776 break;
777 }
778 }
779 }
780 pmc->gsquery = 0;
781 if (scount == nsrcs) /* all sources excluded */
782 return 0;
783 return 1;
784}
785
Al Viroea4d9e72006-09-27 18:30:52 -0700786static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800787{
788 struct ip_sf_list *psf;
789 int i, scount;
790
791 if (pmc->sfmode == MCAST_EXCLUDE)
792 return igmp_xmarksources(pmc, nsrcs, srcs);
793
794 /* mark INCLUDE-mode sources */
795 scount = 0;
796 for (psf=pmc->sources; psf; psf=psf->sf_next) {
797 if (scount == nsrcs)
798 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 for (i=0; i<nsrcs; i++)
800 if (srcs[i] == psf->sf_inaddr) {
801 psf->sf_gsresp = 1;
802 scount++;
803 break;
804 }
805 }
David L Stevensad125832006-01-18 14:20:56 -0800806 if (!scount) {
807 pmc->gsquery = 0;
808 return 0;
809 }
810 pmc->gsquery = 1;
811 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Al Viro63007722006-09-27 18:31:32 -0700814static void igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 struct ip_mc_list *im;
817
818 /* Timers are only set for non-local groups */
819
820 if (group == IGMP_ALL_HOSTS)
821 return;
822
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000823 rcu_read_lock();
824 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (im->multiaddr == group) {
826 igmp_stop_timer(im);
827 break;
828 }
829 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000830 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
834 int len)
835{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300836 struct igmphdr *ih = igmp_hdr(skb);
837 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700839 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 int max_delay;
841 int mark = 0;
842
843
David Stevens5b7c8402010-09-30 14:29:40 +0000844 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (ih->code == 0) {
846 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 max_delay = IGMP_Query_Response_Interval;
849 in_dev->mr_v1_seen = jiffies +
850 IGMP_V1_Router_Present_Timeout;
851 group = 0;
852 } else {
853 /* v2 router present */
854 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
855 in_dev->mr_v2_seen = jiffies +
856 IGMP_V2_Router_Present_Timeout;
857 }
858 /* cancel the interface change timer */
859 in_dev->mr_ifc_count = 0;
860 if (del_timer(&in_dev->mr_ifc_timer))
861 __in_dev_put(in_dev);
862 /* clear deleted report items */
863 igmpv3_clear_delrec(in_dev);
864 } else if (len < 12) {
865 return; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000866 } else if (IGMP_V1_SEEN(in_dev)) {
867 /* This is a v3 query with v1 queriers present */
868 max_delay = IGMP_Query_Response_Interval;
869 group = 0;
870 } else if (IGMP_V2_SEEN(in_dev)) {
871 /* this is a v3 query with v2 queriers present;
872 * Interpretation of the max_delay code is problematic here.
873 * A real v2 host would use ih_code directly, while v3 has a
874 * different encoding. We use the v3 encoding as more likely
875 * to be intended in a v3 query.
876 */
877 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchings49ffa262012-01-09 14:06:46 -0800878 if (!max_delay)
879 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 } else { /* v3 */
881 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
882 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900883
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300884 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900886 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700887 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300889 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891
892 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
893 if (!max_delay)
894 max_delay = 1; /* can't mod w/ 0 */
895 in_dev->mr_maxdelay = max_delay;
896 if (ih3->qrv)
897 in_dev->mr_qrv = ih3->qrv;
898 if (!group) { /* general query */
899 if (ih3->nsrcs)
900 return; /* no sources allowed */
901 igmp_gq_start_timer(in_dev);
902 return;
903 }
904 /* mark sources to include, if group & source-specific */
905 mark = ih3->nsrcs != 0;
906 }
907
908 /*
909 * - Start the timers in all of our membership records
910 * that the query applies to for the interface on
911 * which the query arrived excl. those that belong
912 * to a "local" group (224.0.0.X)
913 * - For timers already running check if they need to
914 * be reset.
915 * - Use the igmp->igmp_code field as the maximum
916 * delay possible
917 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000918 rcu_read_lock();
919 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800920 int changed;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (group && group != im->multiaddr)
923 continue;
924 if (im->multiaddr == IGMP_ALL_HOSTS)
925 continue;
926 spin_lock_bh(&im->lock);
927 if (im->tm_running)
928 im->gsquery = im->gsquery && mark;
929 else
930 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800931 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900932 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800934 if (changed)
935 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000937 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000940/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941int igmp_rcv(struct sk_buff *skb)
942{
943 /* This basically follows the spec line by line -- see RFC1112 */
944 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000945 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int len = skb->len;
947
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800948 if (in_dev == NULL)
949 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Herbert Xufb286bb2005-11-10 13:01:24 -0800951 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000952 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800953
954 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700955 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800956 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800957 break;
958 /* fall through */
959 case CHECKSUM_NONE:
960 skb->csum = 0;
961 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000962 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300965 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 switch (ih->type) {
967 case IGMP_HOST_MEMBERSHIP_QUERY:
968 igmp_heard_query(in_dev, skb, len);
969 break;
970 case IGMP_HOST_MEMBERSHIP_REPORT:
971 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -0800973 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 break;
David Stevens24c69272005-12-02 20:32:59 -0800975 /* don't rely on MC router hearing unicast reports */
976 if (skb->pkt_type == PACKET_MULTICAST ||
977 skb->pkt_type == PACKET_BROADCAST)
978 igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 break;
980 case IGMP_PIM:
981#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 return pim_rcv_v1(skb);
983#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +0000984 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 case IGMP_DVMRP:
986 case IGMP_TRACE:
987 case IGMP_HOST_LEAVE_MESSAGE:
988 case IGMP_MTRACE:
989 case IGMP_MTRACE_RESP:
990 break;
991 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -0800992 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Herbert Xufb286bb2005-11-10 13:01:24 -0800994
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800995drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 kfree_skb(skb);
997 return 0;
998}
999
1000#endif
1001
1002
1003/*
1004 * Add a filter to a device
1005 */
1006
Al Viro63007722006-09-27 18:31:32 -07001007static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 char buf[MAX_ADDR_LEN];
1010 struct net_device *dev = in_dev->dev;
1011
1012 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1013 We will get multicast token leakage, when IFF_MULTICAST
1014 is changed. This check should be done in dev->set_multicast_list
1015 routine. Something sort of:
1016 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1017 --ANK
1018 */
1019 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001020 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
1023/*
1024 * Remove a filter from a device
1025 */
1026
Al Viro63007722006-09-27 18:31:32 -07001027static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 char buf[MAX_ADDR_LEN];
1030 struct net_device *dev = in_dev->dev;
1031
1032 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001033 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
1036#ifdef CONFIG_IP_MULTICAST
1037/*
1038 * deleted ip_mc_list manipulation
1039 */
1040static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1041{
1042 struct ip_mc_list *pmc;
1043
1044 /* this is an "ip_mc_list" for convenience; only the fields below
1045 * are actually used. In particular, the refcnt and users are not
1046 * used for management of the delete list. Using the same structure
1047 * for deleted items allows change reports to use common code with
1048 * non-deleted or query-response MCA's.
1049 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001050 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!pmc)
1052 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 spin_lock_bh(&im->lock);
1054 pmc->interface = im->interface;
1055 in_dev_hold(in_dev);
1056 pmc->multiaddr = im->multiaddr;
1057 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1058 IGMP_Unsolicited_Report_Count;
1059 pmc->sfmode = im->sfmode;
1060 if (pmc->sfmode == MCAST_INCLUDE) {
1061 struct ip_sf_list *psf;
1062
1063 pmc->tomb = im->tomb;
1064 pmc->sources = im->sources;
1065 im->tomb = im->sources = NULL;
1066 for (psf=pmc->sources; psf; psf=psf->sf_next)
1067 psf->sf_crcount = pmc->crcount;
1068 }
1069 spin_unlock_bh(&im->lock);
1070
1071 spin_lock_bh(&in_dev->mc_tomb_lock);
1072 pmc->next = in_dev->mc_tomb;
1073 in_dev->mc_tomb = pmc;
1074 spin_unlock_bh(&in_dev->mc_tomb_lock);
1075}
1076
Al Viro63007722006-09-27 18:31:32 -07001077static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct ip_mc_list *pmc, *pmc_prev;
1080 struct ip_sf_list *psf, *psf_next;
1081
1082 spin_lock_bh(&in_dev->mc_tomb_lock);
1083 pmc_prev = NULL;
1084 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1085 if (pmc->multiaddr == multiaddr)
1086 break;
1087 pmc_prev = pmc;
1088 }
1089 if (pmc) {
1090 if (pmc_prev)
1091 pmc_prev->next = pmc->next;
1092 else
1093 in_dev->mc_tomb = pmc->next;
1094 }
1095 spin_unlock_bh(&in_dev->mc_tomb_lock);
1096 if (pmc) {
1097 for (psf=pmc->tomb; psf; psf=psf_next) {
1098 psf_next = psf->sf_next;
1099 kfree(psf);
1100 }
1101 in_dev_put(pmc->interface);
1102 kfree(pmc);
1103 }
1104}
1105
1106static void igmpv3_clear_delrec(struct in_device *in_dev)
1107{
1108 struct ip_mc_list *pmc, *nextpmc;
1109
1110 spin_lock_bh(&in_dev->mc_tomb_lock);
1111 pmc = in_dev->mc_tomb;
1112 in_dev->mc_tomb = NULL;
1113 spin_unlock_bh(&in_dev->mc_tomb_lock);
1114
1115 for (; pmc; pmc = nextpmc) {
1116 nextpmc = pmc->next;
1117 ip_mc_clear_src(pmc);
1118 in_dev_put(pmc->interface);
1119 kfree(pmc);
1120 }
1121 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001122 rcu_read_lock();
1123 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 struct ip_sf_list *psf, *psf_next;
1125
1126 spin_lock_bh(&pmc->lock);
1127 psf = pmc->tomb;
1128 pmc->tomb = NULL;
1129 spin_unlock_bh(&pmc->lock);
1130 for (; psf; psf=psf_next) {
1131 psf_next = psf->sf_next;
1132 kfree(psf);
1133 }
1134 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001135 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137#endif
1138
1139static void igmp_group_dropped(struct ip_mc_list *im)
1140{
1141 struct in_device *in_dev = im->interface;
1142#ifdef CONFIG_IP_MULTICAST
1143 int reporter;
1144#endif
1145
1146 if (im->loaded) {
1147 im->loaded = 0;
1148 ip_mc_filter_del(in_dev, im->multiaddr);
1149 }
1150
1151#ifdef CONFIG_IP_MULTICAST
1152 if (im->multiaddr == IGMP_ALL_HOSTS)
1153 return;
1154
1155 reporter = im->reporter;
1156 igmp_stop_timer(im);
1157
1158 if (!in_dev->dead) {
1159 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (IGMP_V2_SEEN(in_dev)) {
1162 if (reporter)
1163 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001164 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
1166 /* IGMPv3 */
1167 igmpv3_add_delrec(in_dev, im);
1168
1169 igmp_ifc_event(in_dev);
1170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172}
1173
1174static void igmp_group_added(struct ip_mc_list *im)
1175{
1176 struct in_device *in_dev = im->interface;
1177
1178 if (im->loaded == 0) {
1179 im->loaded = 1;
1180 ip_mc_filter_add(in_dev, im->multiaddr);
1181 }
1182
1183#ifdef CONFIG_IP_MULTICAST
1184 if (im->multiaddr == IGMP_ALL_HOSTS)
1185 return;
1186
1187 if (in_dev->dead)
1188 return;
1189 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1190 spin_lock_bh(&im->lock);
1191 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1192 spin_unlock_bh(&im->lock);
1193 return;
1194 }
1195 /* else, v3 */
1196
1197 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1198 IGMP_Unsolicited_Report_Count;
1199 igmp_ifc_event(in_dev);
1200#endif
1201}
1202
1203
1204/*
1205 * Multicast list managers
1206 */
1207
1208
1209/*
1210 * A socket has joined a multicast group on device dev.
1211 */
1212
Al Viro8f935bb2006-09-27 18:30:07 -07001213void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
1215 struct ip_mc_list *im;
1216
1217 ASSERT_RTNL();
1218
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001219 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (im->multiaddr == addr) {
1221 im->users++;
1222 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1223 goto out;
1224 }
1225 }
1226
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001227 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (!im)
1229 goto out;
1230
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001231 im->users = 1;
1232 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001234 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 /* initial mode is (EX, empty) */
1236 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 atomic_set(&im->refcnt, 1);
1239 spin_lock_init(&im->lock);
1240#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001241 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001244
1245 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001246 in_dev->mc_count++;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001247 rcu_assign_pointer(in_dev->mc_list, im);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249#ifdef CONFIG_IP_MULTICAST
1250 igmpv3_del_delrec(in_dev, im->multiaddr);
1251#endif
1252 igmp_group_added(im);
1253 if (!in_dev->dead)
1254 ip_rt_multicast_event(in_dev);
1255out:
1256 return;
1257}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001258EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001261 * Resend IGMP JOIN report; used for bonding.
Eric Dumazet866f3b22010-11-18 09:33:19 -08001262 * Called with rcu_read_lock()
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001263 */
Eric Dumazet866f3b22010-11-18 09:33:19 -08001264void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001265{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001266#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001267 struct ip_mc_list *im;
1268 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001269
Eric Dumazet866f3b22010-11-18 09:33:19 -08001270 for_each_pmc_rcu(in_dev, im) {
1271 if (im->multiaddr == IGMP_ALL_HOSTS)
1272 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001273
Eric Dumazet866f3b22010-11-18 09:33:19 -08001274 /* a failover is happening and switches
1275 * must be notified immediately
1276 */
1277 if (IGMP_V1_SEEN(in_dev))
1278 type = IGMP_HOST_MEMBERSHIP_REPORT;
1279 else if (IGMP_V2_SEEN(in_dev))
1280 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1281 else
1282 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1283 igmp_send_report(in_dev, im, type);
1284 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001285#endif
1286}
Eric Dumazet866f3b22010-11-18 09:33:19 -08001287EXPORT_SYMBOL(ip_mc_rejoin_groups);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001288
1289/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 * A socket has left a multicast group on device dev
1291 */
1292
Al Viro8f935bb2006-09-27 18:30:07 -07001293void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001295 struct ip_mc_list *i;
1296 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001299
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001300 for (ip = &in_dev->mc_list;
1301 (i = rtnl_dereference(*ip)) != NULL;
1302 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001303 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 if (--i->users == 0) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001305 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001306 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001308 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (!in_dev->dead)
1311 ip_rt_multicast_event(in_dev);
1312
1313 ip_ma_put(i);
1314 return;
1315 }
1316 break;
1317 }
1318 }
1319}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001320EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Moni Shoua75c78502009-09-15 02:37:40 -07001322/* Device changing type */
1323
1324void ip_mc_unmap(struct in_device *in_dev)
1325{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001326 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001327
1328 ASSERT_RTNL();
1329
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001330 for_each_pmc_rtnl(in_dev, pmc)
1331 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001332}
1333
1334void ip_mc_remap(struct in_device *in_dev)
1335{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001336 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001337
1338 ASSERT_RTNL();
1339
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001340 for_each_pmc_rtnl(in_dev, pmc)
1341 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001342}
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344/* Device going down */
1345
1346void ip_mc_down(struct in_device *in_dev)
1347{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001348 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 ASSERT_RTNL();
1351
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001352 for_each_pmc_rtnl(in_dev, pmc)
1353 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355#ifdef CONFIG_IP_MULTICAST
1356 in_dev->mr_ifc_count = 0;
1357 if (del_timer(&in_dev->mr_ifc_timer))
1358 __in_dev_put(in_dev);
1359 in_dev->mr_gq_running = 0;
1360 if (del_timer(&in_dev->mr_gq_timer))
1361 __in_dev_put(in_dev);
1362 igmpv3_clear_delrec(in_dev);
1363#endif
1364
1365 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1366}
1367
1368void ip_mc_init_dev(struct in_device *in_dev)
1369{
1370 ASSERT_RTNL();
1371
1372 in_dev->mc_tomb = NULL;
1373#ifdef CONFIG_IP_MULTICAST
1374 in_dev->mr_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001375 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1376 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 in_dev->mr_ifc_count = 0;
Rami Rosenb8bae412008-10-07 15:34:37 -07001378 in_dev->mc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001379 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1380 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1382#endif
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 spin_lock_init(&in_dev->mc_tomb_lock);
1385}
1386
1387/* Device going up */
1388
1389void ip_mc_up(struct in_device *in_dev)
1390{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001391 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 ASSERT_RTNL();
1394
1395 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1396
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001397 for_each_pmc_rtnl(in_dev, pmc)
1398 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399}
1400
1401/*
1402 * Device is about to be destroyed: clean up.
1403 */
1404
1405void ip_mc_destroy_dev(struct in_device *in_dev)
1406{
1407 struct ip_mc_list *i;
1408
1409 ASSERT_RTNL();
1410
1411 /* Deactivate timers */
1412 ip_mc_down(in_dev);
1413
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001414 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1415 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001416 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001417
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001418 /* We've dropped the groups in ip_mc_down already */
1419 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001424/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001425static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct net_device *dev = NULL;
1428 struct in_device *idev = NULL;
1429
1430 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001431 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 return idev;
1433 }
1434 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001435 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!dev)
1437 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
David S. Millerb23dd4f2011-03-02 14:31:35 -08001440 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001441 struct rtable *rt = ip_route_output(net,
1442 imr->imr_multiaddr.s_addr,
1443 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001444 if (!IS_ERR(rt)) {
1445 dev = rt->dst.dev;
1446 ip_rt_put(rt);
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 if (dev) {
1450 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001451 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 return idev;
1454}
1455
1456/*
1457 * Join a socket to a group
1458 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001459int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1460int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462
1463static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001464 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
1466 struct ip_sf_list *psf, *psf_prev;
1467 int rv = 0;
1468
1469 psf_prev = NULL;
1470 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1471 if (psf->sf_inaddr == *psfsrc)
1472 break;
1473 psf_prev = psf;
1474 }
1475 if (!psf || psf->sf_count[sfmode] == 0) {
1476 /* source filter not found, or count wrong => bug */
1477 return -ESRCH;
1478 }
1479 psf->sf_count[sfmode]--;
1480 if (psf->sf_count[sfmode] == 0) {
1481 ip_rt_multicast_event(pmc->interface);
1482 }
1483 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1484#ifdef CONFIG_IP_MULTICAST
1485 struct in_device *in_dev = pmc->interface;
1486#endif
1487
1488 /* no more filters for this source */
1489 if (psf_prev)
1490 psf_prev->sf_next = psf->sf_next;
1491 else
1492 pmc->sources = psf->sf_next;
1493#ifdef CONFIG_IP_MULTICAST
1494 if (psf->sf_oldin &&
1495 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001496 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 IGMP_Unsolicited_Report_Count;
1498 psf->sf_next = pmc->tomb;
1499 pmc->tomb = psf;
1500 rv = 1;
1501 } else
1502#endif
1503 kfree(psf);
1504 }
1505 return rv;
1506}
1507
1508#ifndef CONFIG_IP_MULTICAST
1509#define igmp_ifc_event(x) do { } while (0)
1510#endif
1511
Al Viro8f935bb2006-09-27 18:30:07 -07001512static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1513 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 struct ip_mc_list *pmc;
1516 int changerec = 0;
1517 int i, err;
1518
1519 if (!in_dev)
1520 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001521 rcu_read_lock();
1522 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (*pmca == pmc->multiaddr)
1524 break;
1525 }
1526 if (!pmc) {
1527 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001528 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return -ESRCH;
1530 }
1531 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001532 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533#ifdef CONFIG_IP_MULTICAST
1534 sf_markstate(pmc);
1535#endif
1536 if (!delta) {
1537 err = -EINVAL;
1538 if (!pmc->sfcount[sfmode])
1539 goto out_unlock;
1540 pmc->sfcount[sfmode]--;
1541 }
1542 err = 0;
1543 for (i=0; i<sfcount; i++) {
1544 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1545
1546 changerec |= rv > 0;
1547 if (!err && rv < 0)
1548 err = rv;
1549 }
1550 if (pmc->sfmode == MCAST_EXCLUDE &&
1551 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1552 pmc->sfcount[MCAST_INCLUDE]) {
1553#ifdef CONFIG_IP_MULTICAST
1554 struct ip_sf_list *psf;
1555#endif
1556
1557 /* filter mode change */
1558 pmc->sfmode = MCAST_INCLUDE;
1559#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001560 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 IGMP_Unsolicited_Report_Count;
1562 in_dev->mr_ifc_count = pmc->crcount;
1563 for (psf=pmc->sources; psf; psf = psf->sf_next)
1564 psf->sf_crcount = 0;
1565 igmp_ifc_event(pmc->interface);
1566 } else if (sf_setstate(pmc) || changerec) {
1567 igmp_ifc_event(pmc->interface);
1568#endif
1569 }
1570out_unlock:
1571 spin_unlock_bh(&pmc->lock);
1572 return err;
1573}
1574
1575/*
1576 * Add multicast single-source filter to the interface list
1577 */
1578static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001579 __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 struct ip_sf_list *psf, *psf_prev;
1582
1583 psf_prev = NULL;
1584 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1585 if (psf->sf_inaddr == *psfsrc)
1586 break;
1587 psf_prev = psf;
1588 }
1589 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001590 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (!psf)
1592 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 psf->sf_inaddr = *psfsrc;
1594 if (psf_prev) {
1595 psf_prev->sf_next = psf;
1596 } else
1597 pmc->sources = psf;
1598 }
1599 psf->sf_count[sfmode]++;
1600 if (psf->sf_count[sfmode] == 1) {
1601 ip_rt_multicast_event(pmc->interface);
1602 }
1603 return 0;
1604}
1605
1606#ifdef CONFIG_IP_MULTICAST
1607static void sf_markstate(struct ip_mc_list *pmc)
1608{
1609 struct ip_sf_list *psf;
1610 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1611
1612 for (psf=pmc->sources; psf; psf=psf->sf_next)
1613 if (pmc->sfcount[MCAST_EXCLUDE]) {
1614 psf->sf_oldin = mca_xcount ==
1615 psf->sf_count[MCAST_EXCLUDE] &&
1616 !psf->sf_count[MCAST_INCLUDE];
1617 } else
1618 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1619}
1620
1621static int sf_setstate(struct ip_mc_list *pmc)
1622{
David L Stevensad125832006-01-18 14:20:56 -08001623 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1625 int qrv = pmc->interface->mr_qrv;
1626 int new_in, rv;
1627
1628 rv = 0;
1629 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1630 if (pmc->sfcount[MCAST_EXCLUDE]) {
1631 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1632 !psf->sf_count[MCAST_INCLUDE];
1633 } else
1634 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001635 if (new_in) {
1636 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001637 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001638
1639 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1640 if (dpsf->sf_inaddr == psf->sf_inaddr)
1641 break;
1642 prev = dpsf;
1643 }
1644 if (dpsf) {
1645 if (prev)
1646 prev->sf_next = dpsf->sf_next;
1647 else
1648 pmc->tomb = dpsf->sf_next;
1649 kfree(dpsf);
1650 }
1651 psf->sf_crcount = qrv;
1652 rv++;
1653 }
1654 } else if (psf->sf_oldin) {
1655
1656 psf->sf_crcount = 0;
1657 /*
1658 * add or update "delete" records if an active filter
1659 * is now inactive
1660 */
1661 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1662 if (dpsf->sf_inaddr == psf->sf_inaddr)
1663 break;
1664 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001665 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001666 if (!dpsf)
1667 continue;
1668 *dpsf = *psf;
1669 /* pmc->lock held by callers */
1670 dpsf->sf_next = pmc->tomb;
1671 pmc->tomb = dpsf;
1672 }
1673 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 rv++;
1675 }
1676 }
1677 return rv;
1678}
1679#endif
1680
1681/*
1682 * Add multicast source filter list to the interface list
1683 */
Al Viro8f935bb2006-09-27 18:30:07 -07001684static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1685 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 struct ip_mc_list *pmc;
1688 int isexclude;
1689 int i, err;
1690
1691 if (!in_dev)
1692 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001693 rcu_read_lock();
1694 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 if (*pmca == pmc->multiaddr)
1696 break;
1697 }
1698 if (!pmc) {
1699 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001700 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return -ESRCH;
1702 }
1703 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001704 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706#ifdef CONFIG_IP_MULTICAST
1707 sf_markstate(pmc);
1708#endif
1709 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1710 if (!delta)
1711 pmc->sfcount[sfmode]++;
1712 err = 0;
1713 for (i=0; i<sfcount; i++) {
1714 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1715 if (err)
1716 break;
1717 }
1718 if (err) {
1719 int j;
1720
1721 pmc->sfcount[sfmode]--;
1722 for (j=0; j<i; j++)
Julia Lawalld9399f32011-07-28 02:46:01 +00001723 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1725#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001727 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728#endif
1729
1730 /* filter mode change */
1731 if (pmc->sfcount[MCAST_EXCLUDE])
1732 pmc->sfmode = MCAST_EXCLUDE;
1733 else if (pmc->sfcount[MCAST_INCLUDE])
1734 pmc->sfmode = MCAST_INCLUDE;
1735#ifdef CONFIG_IP_MULTICAST
1736 /* else no filters; keep old mode for reports */
1737
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001738 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 IGMP_Unsolicited_Report_Count;
1740 in_dev->mr_ifc_count = pmc->crcount;
1741 for (psf=pmc->sources; psf; psf = psf->sf_next)
1742 psf->sf_crcount = 0;
1743 igmp_ifc_event(in_dev);
1744 } else if (sf_setstate(pmc)) {
1745 igmp_ifc_event(in_dev);
1746#endif
1747 }
1748 spin_unlock_bh(&pmc->lock);
1749 return err;
1750}
1751
1752static void ip_mc_clear_src(struct ip_mc_list *pmc)
1753{
1754 struct ip_sf_list *psf, *nextpsf;
1755
1756 for (psf=pmc->tomb; psf; psf=nextpsf) {
1757 nextpsf = psf->sf_next;
1758 kfree(psf);
1759 }
1760 pmc->tomb = NULL;
1761 for (psf=pmc->sources; psf; psf=nextpsf) {
1762 nextpsf = psf->sf_next;
1763 kfree(psf);
1764 }
1765 pmc->sources = NULL;
1766 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001767 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 pmc->sfcount[MCAST_EXCLUDE] = 1;
1769}
1770
1771
1772/*
1773 * Join a multicast group
1774 */
1775int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1776{
1777 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001778 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001779 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 struct in_device *in_dev;
1781 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001782 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001783 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 int count = 0;
1785
Joe Perchesf97c1e02007-12-16 13:45:43 -08001786 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return -EINVAL;
1788
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001789 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Daniel Lezcano877aced2008-08-13 16:15:57 -07001791 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 if (!in_dev) {
1794 iml = NULL;
1795 err = -ENODEV;
1796 goto done;
1797 }
1798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001800 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001801 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001802 if (i->multi.imr_multiaddr.s_addr == addr &&
1803 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 count++;
1806 }
1807 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001808 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001810 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001811 if (iml == NULL)
1812 goto done;
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001815 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 iml->sflist = NULL;
1817 iml->sfmode = MCAST_EXCLUDE;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001818 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001822 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 return err;
1824}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001825EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1828 struct in_device *in_dev)
1829{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001830 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int err;
1832
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001833 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 /* any-source empty exclude case */
1835 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1836 iml->sfmode, 0, NULL, 0);
1837 }
1838 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001839 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1840 rcu_assign_pointer(iml->sflist, NULL);
1841 /* decrease mem now to avoid the memleak warning */
1842 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08001843 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return err;
1845}
1846
1847/*
1848 * Ask a socket to leave a group.
1849 */
1850
1851int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1852{
1853 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001854 struct ip_mc_socklist *iml;
1855 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001856 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001857 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001858 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001859 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001860 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
1862 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001863 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001864 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001865 for (imlp = &inet->mc_list;
1866 (iml = rtnl_dereference(*imlp)) != NULL;
1867 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001868 if (iml->multi.imr_multiaddr.s_addr != group)
1869 continue;
1870 if (ifindex) {
1871 if (iml->multi.imr_ifindex != ifindex)
1872 continue;
1873 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1874 iml->multi.imr_address.s_addr)
1875 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
David L Stevensacd6e002006-08-17 16:27:39 -07001877 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001879 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001880
1881 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001882 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001883 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001884 /* decrease mem now to avoid the memleak warning */
1885 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08001886 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07001887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
David L Stevensacd6e002006-08-17 16:27:39 -07001889 if (!in_dev)
1890 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001892 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
1895int ip_mc_source(int add, int omode, struct sock *sk, struct
1896 ip_mreq_source *mreqs, int ifindex)
1897{
1898 int err;
1899 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001900 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct ip_mc_socklist *pmc;
1902 struct in_device *in_dev = NULL;
1903 struct inet_sock *inet = inet_sk(sk);
1904 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001905 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001906 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 int i, j, rv;
1908
Joe Perchesf97c1e02007-12-16 13:45:43 -08001909 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return -EINVAL;
1911
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001912 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1915 imr.imr_address.s_addr = mreqs->imr_interface;
1916 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001917 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 if (!in_dev) {
1920 err = -ENODEV;
1921 goto done;
1922 }
1923 err = -EADDRNOTAVAIL;
1924
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001925 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08001926 if ((pmc->multi.imr_multiaddr.s_addr ==
1927 imr.imr_multiaddr.s_addr) &&
1928 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 break;
1930 }
David L Stevens917f2f12005-07-08 17:45:16 -07001931 if (!pmc) { /* must have a prior join */
1932 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 /* if a source filter was set, must be the same mode as before */
1936 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07001937 if (pmc->sfmode != omode) {
1938 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 } else if (pmc->sfmode != omode) {
1942 /* allow mode switches for empty-set filters */
1943 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001944 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 NULL, 0);
1946 pmc->sfmode = omode;
1947 }
1948
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001949 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (!add) {
1951 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07001952 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 rv = !0;
1954 for (i=0; i<psl->sl_count; i++) {
1955 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001956 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (rv == 0)
1958 break;
1959 }
1960 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07001961 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
David L Stevens8cdaaa12005-07-08 17:39:23 -07001963 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1964 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1965 leavegroup = 1;
1966 goto done;
1967 }
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001970 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 &mreqs->imr_sourceaddr, 1);
1972
1973 for (j=i+1; j<psl->sl_count; j++)
1974 psl->sl_addr[j-1] = psl->sl_addr[j];
1975 psl->sl_count--;
1976 err = 0;
1977 goto done;
1978 }
1979 /* else, add a new source to the filter */
1980
1981 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1982 err = -ENOBUFS;
1983 goto done;
1984 }
1985 if (!psl || psl->sl_count == psl->sl_max) {
1986 struct ip_sf_socklist *newpsl;
1987 int count = IP_SFBLOCK;
1988
1989 if (psl)
1990 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08001991 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (!newpsl) {
1993 err = -ENOBUFS;
1994 goto done;
1995 }
1996 newpsl->sl_max = count;
1997 newpsl->sl_count = count - IP_SFBLOCK;
1998 if (psl) {
1999 for (i=0; i<psl->sl_count; i++)
2000 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002001 /* decrease mem now to avoid the memleak warning */
2002 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002003 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002005 rcu_assign_pointer(pmc->sflist, newpsl);
2006 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2009 for (i=0; i<psl->sl_count; i++) {
2010 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002011 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (rv == 0)
2013 break;
2014 }
2015 if (rv == 0) /* address already there is an error */
2016 goto done;
2017 for (j=psl->sl_count-1; j>=i; j--)
2018 psl->sl_addr[j+1] = psl->sl_addr[j];
2019 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2020 psl->sl_count++;
2021 err = 0;
2022 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002023 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 &mreqs->imr_sourceaddr, 1);
2025done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002026 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002027 if (leavegroup)
2028 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 return err;
2030}
2031
2032int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2033{
David L Stevens9951f032005-07-08 17:47:28 -07002034 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002036 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 struct ip_mc_socklist *pmc;
2038 struct in_device *in_dev;
2039 struct inet_sock *inet = inet_sk(sk);
2040 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002041 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002042 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Joe Perchesf97c1e02007-12-16 13:45:43 -08002044 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 return -EINVAL;
2046 if (msf->imsf_fmode != MCAST_INCLUDE &&
2047 msf->imsf_fmode != MCAST_EXCLUDE)
2048 return -EINVAL;
2049
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002050 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2053 imr.imr_address.s_addr = msf->imsf_interface;
2054 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002055 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 if (!in_dev) {
2058 err = -ENODEV;
2059 goto done;
2060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
David L Stevens9951f032005-07-08 17:47:28 -07002062 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2063 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2064 leavegroup = 1;
2065 goto done;
2066 }
2067
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002068 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2070 pmc->multi.imr_ifindex == imr.imr_ifindex)
2071 break;
2072 }
David L Stevens917f2f12005-07-08 17:45:16 -07002073 if (!pmc) { /* must have a prior join */
2074 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002078 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2079 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (!newpsl) {
2081 err = -ENOBUFS;
2082 goto done;
2083 }
2084 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2085 memcpy(newpsl->sl_addr, msf->imsf_slist,
2086 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2087 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2088 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2089 if (err) {
2090 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2091 goto done;
2092 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002093 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002095 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2096 msf->imsf_fmode, 0, NULL, 0);
2097 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002098 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (psl) {
2100 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2101 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002102 /* decrease mem now to avoid the memleak warning */
2103 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002104 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 } else
2106 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2107 0, NULL, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002108 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002110 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002112 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002113 if (leavegroup)
2114 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return err;
2116}
2117
2118int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2119 struct ip_msfilter __user *optval, int __user *optlen)
2120{
2121 int err, len, count, copycount;
2122 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002123 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct ip_mc_socklist *pmc;
2125 struct in_device *in_dev;
2126 struct inet_sock *inet = inet_sk(sk);
2127 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002128 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
Joe Perchesf97c1e02007-12-16 13:45:43 -08002130 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return -EINVAL;
2132
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002133 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2136 imr.imr_address.s_addr = msf->imsf_interface;
2137 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002138 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 if (!in_dev) {
2141 err = -ENODEV;
2142 goto done;
2143 }
2144 err = -EADDRNOTAVAIL;
2145
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002146 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2148 pmc->multi.imr_ifindex == imr.imr_ifindex)
2149 break;
2150 }
2151 if (!pmc) /* must have a prior join */
2152 goto done;
2153 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002154 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002155 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (!psl) {
2157 len = 0;
2158 count = 0;
2159 } else {
2160 count = psl->sl_count;
2161 }
2162 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2163 len = copycount * sizeof(psl->sl_addr[0]);
2164 msf->imsf_numsrc = count;
2165 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2166 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2167 return -EFAULT;
2168 }
2169 if (len &&
2170 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2171 return -EFAULT;
2172 return 0;
2173done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002174 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return err;
2176}
2177
2178int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2179 struct group_filter __user *optval, int __user *optlen)
2180{
2181 int err, i, count, copycount;
2182 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002183 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct ip_mc_socklist *pmc;
2185 struct inet_sock *inet = inet_sk(sk);
2186 struct ip_sf_socklist *psl;
2187
2188 psin = (struct sockaddr_in *)&gsf->gf_group;
2189 if (psin->sin_family != AF_INET)
2190 return -EINVAL;
2191 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002192 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return -EINVAL;
2194
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002195 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 err = -EADDRNOTAVAIL;
2198
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002199 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2201 pmc->multi.imr_ifindex == gsf->gf_interface)
2202 break;
2203 }
2204 if (!pmc) /* must have a prior join */
2205 goto done;
2206 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002207 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002208 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 count = psl ? psl->sl_count : 0;
2210 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2211 gsf->gf_numsrc = count;
2212 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2213 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2214 return -EFAULT;
2215 }
2216 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 struct sockaddr_storage ss;
2218
2219 psin = (struct sockaddr_in *)&ss;
2220 memset(&ss, 0, sizeof(ss));
2221 psin->sin_family = AF_INET;
2222 psin->sin_addr.s_addr = psl->sl_addr[i];
2223 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2224 return -EFAULT;
2225 }
2226 return 0;
2227done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002228 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return err;
2230}
2231
2232/*
2233 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2234 */
Al Viroc0cda062006-09-27 18:31:10 -07002235int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
2237 struct inet_sock *inet = inet_sk(sk);
2238 struct ip_mc_socklist *pmc;
2239 struct ip_sf_socklist *psl;
2240 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002241 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002243 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002244 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002245 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002247 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002248 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2250 pmc->multi.imr_ifindex == dif)
2251 break;
2252 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002253 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002255 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002256 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002257 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002259 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
2261 for (i=0; i<psl->sl_count; i++) {
2262 if (psl->sl_addr[i] == rmt_addr)
2263 break;
2264 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002265 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002267 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002269 goto unlock;
2270 ret = 1;
2271unlock:
2272 rcu_read_unlock();
2273out:
2274 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
2277/*
2278 * A socket is closing.
2279 */
2280
2281void ip_mc_drop_socket(struct sock *sk)
2282{
2283 struct inet_sock *inet = inet_sk(sk);
2284 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002285 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 if (inet->mc_list == NULL)
2288 return;
2289
2290 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002291 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002294 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002295 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002296 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002297 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002299 /* decrease mem now to avoid the memleak warning */
2300 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002301 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
2303 rtnl_unlock();
2304}
2305
David S. Millerdbdd9a52011-03-10 16:34:38 -08002306/* called with rcu_read_lock() */
2307int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
2309 struct ip_mc_list *im;
2310 struct ip_sf_list *psf;
2311 int rv = 0;
2312
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002313 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (im->multiaddr == mc_addr)
2315 break;
2316 }
2317 if (im && proto == IPPROTO_IGMP) {
2318 rv = 1;
2319 } else if (im) {
2320 if (src_addr) {
2321 for (psf=im->sources; psf; psf=psf->sf_next) {
2322 if (psf->sf_inaddr == src_addr)
2323 break;
2324 }
2325 if (psf)
2326 rv = psf->sf_count[MCAST_INCLUDE] ||
2327 psf->sf_count[MCAST_EXCLUDE] !=
2328 im->sfcount[MCAST_EXCLUDE];
2329 else
2330 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2331 } else
2332 rv = 1; /* unspecified source; tentatively allow */
2333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 return rv;
2335}
2336
2337#if defined(CONFIG_PROC_FS)
2338struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002339 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 struct net_device *dev;
2341 struct in_device *in_dev;
2342};
2343
2344#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2345
2346static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2347{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002348 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 struct ip_mc_list *im = NULL;
2350 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2351
Pavel Emelianov7562f872007-05-03 15:13:45 -07002352 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002353 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002355
2356 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 if (!in_dev)
2358 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002359 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (im) {
2361 state->in_dev = in_dev;
2362 break;
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365 return im;
2366}
2367
2368static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2369{
2370 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002371
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002372 im = rcu_dereference(im->next_rcu);
2373 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002374 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (!state->dev) {
2376 state->in_dev = NULL;
2377 break;
2378 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002379 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if (!state->in_dev)
2381 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002382 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384 return im;
2385}
2386
2387static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2388{
2389 struct ip_mc_list *im = igmp_mc_get_first(seq);
2390 if (im)
2391 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2392 --pos;
2393 return pos ? NULL : im;
2394}
2395
2396static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002397 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398{
stephen hemminger61fbab72009-11-10 07:54:55 +00002399 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2401}
2402
2403static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2404{
2405 struct ip_mc_list *im;
2406 if (v == SEQ_START_TOKEN)
2407 im = igmp_mc_get_first(seq);
2408 else
2409 im = igmp_mc_get_next(seq, v);
2410 ++*pos;
2411 return im;
2412}
2413
2414static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002415 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
2417 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002418
2419 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002421 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422}
2423
2424static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2425{
2426 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002427 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2429 else {
2430 struct ip_mc_list *im = (struct ip_mc_list *)v;
2431 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2432 char *querier;
2433#ifdef CONFIG_IP_MULTICAST
2434 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2435 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2436 "V3";
2437#else
2438 querier = "NONE";
2439#endif
2440
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002441 if (rcu_dereference(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002443 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445
2446 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002447 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 im->multiaddr, im->users,
2449 im->tm_running, im->tm_running ?
2450 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2451 im->reporter);
2452 }
2453 return 0;
2454}
2455
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002456static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 .start = igmp_mc_seq_start,
2458 .next = igmp_mc_seq_next,
2459 .stop = igmp_mc_seq_stop,
2460 .show = igmp_mc_seq_show,
2461};
2462
2463static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2464{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002465 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002466 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Arjan van de Ven9a321442007-02-12 00:55:35 -08002469static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 .owner = THIS_MODULE,
2471 .open = igmp_mc_seq_open,
2472 .read = seq_read,
2473 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002474 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475};
2476
2477struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002478 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct net_device *dev;
2480 struct in_device *idev;
2481 struct ip_mc_list *im;
2482};
2483
2484#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2485
2486static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2487{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002488 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 struct ip_sf_list *psf = NULL;
2490 struct ip_mc_list *im = NULL;
2491 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2492
Pavel Emelianov7562f872007-05-03 15:13:45 -07002493 state->idev = NULL;
2494 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002495 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002497 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (unlikely(idev == NULL))
2499 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002500 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (likely(im != NULL)) {
2502 spin_lock_bh(&im->lock);
2503 psf = im->sources;
2504 if (likely(psf != NULL)) {
2505 state->im = im;
2506 state->idev = idev;
2507 break;
2508 }
2509 spin_unlock_bh(&im->lock);
2510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 }
2512 return psf;
2513}
2514
2515static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2516{
2517 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2518
2519 psf = psf->sf_next;
2520 while (!psf) {
2521 spin_unlock_bh(&state->im->lock);
2522 state->im = state->im->next;
2523 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002524 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (!state->dev) {
2526 state->idev = NULL;
2527 goto out;
2528 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002529 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (!state->idev)
2531 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002532 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
2534 if (!state->im)
2535 break;
2536 spin_lock_bh(&state->im->lock);
2537 psf = state->im->sources;
2538 }
2539out:
2540 return psf;
2541}
2542
2543static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2544{
2545 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2546 if (psf)
2547 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2548 --pos;
2549 return pos ? NULL : psf;
2550}
2551
2552static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002553 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
stephen hemminger61fbab72009-11-10 07:54:55 +00002555 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2557}
2558
2559static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2560{
2561 struct ip_sf_list *psf;
2562 if (v == SEQ_START_TOKEN)
2563 psf = igmp_mcf_get_first(seq);
2564 else
2565 psf = igmp_mcf_get_next(seq, v);
2566 ++*pos;
2567 return psf;
2568}
2569
2570static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002571 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2574 if (likely(state->im != NULL)) {
2575 spin_unlock_bh(&state->im->lock);
2576 state->im = NULL;
2577 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002578 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002580 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
2583static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2584{
2585 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2586 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2587
2588 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002589 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 "%3s %6s "
2591 "%10s %10s %6s %6s\n", "Idx",
2592 "Device", "MCA",
2593 "SRC", "INC", "EXC");
2594 } else {
2595 seq_printf(seq,
2596 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002597 "0x%08x %6lu %6lu\n",
2598 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 ntohl(state->im->multiaddr),
2600 ntohl(psf->sf_inaddr),
2601 psf->sf_count[MCAST_INCLUDE],
2602 psf->sf_count[MCAST_EXCLUDE]);
2603 }
2604 return 0;
2605}
2606
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002607static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 .start = igmp_mcf_seq_start,
2609 .next = igmp_mcf_seq_next,
2610 .stop = igmp_mcf_seq_stop,
2611 .show = igmp_mcf_seq_show,
2612};
2613
2614static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2615{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002616 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002617 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Arjan van de Ven9a321442007-02-12 00:55:35 -08002620static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 .owner = THIS_MODULE,
2622 .open = igmp_mcf_seq_open,
2623 .read = seq_read,
2624 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002625 .release = seq_release_net,
2626};
2627
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002628static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002629{
2630 struct proc_dir_entry *pde;
2631
2632 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2633 if (!pde)
2634 goto out_igmp;
2635 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2636 if (!pde)
2637 goto out_mcfilter;
2638 return 0;
2639
2640out_mcfilter:
2641 proc_net_remove(net, "igmp");
2642out_igmp:
2643 return -ENOMEM;
2644}
2645
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002646static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002647{
2648 proc_net_remove(net, "mcfilter");
2649 proc_net_remove(net, "igmp");
2650}
2651
2652static struct pernet_operations igmp_net_ops = {
2653 .init = igmp_net_init,
2654 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655};
2656
2657int __init igmp_mc_proc_init(void)
2658{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002659 return register_pernet_subsys(&igmp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661#endif