blob: 7c2ef59e3f7dba5368e814ee58b0190dc87187ed [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
3 *
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
6 *
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Authors:
Alan Cox113aa832008-10-13 19:01:08 -070012 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * Fixes:
20 *
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
23 * functions.
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
32 * points.
33 *
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090036 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050 * memberships but was being deleted,
51 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * from %p with timer not initialized\n"
53 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090054 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
58 * true (960216).
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
70 * Vinay Kulkarni
71 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/uaccess.h>
76#include <asm/system.h>
77#include <linux/types.h>
78#include <linux/kernel.h>
79#include <linux/jiffies.h>
80#include <linux/string.h>
81#include <linux/socket.h>
82#include <linux/sockios.h>
83#include <linux/in.h>
84#include <linux/inet.h>
85#include <linux/netdevice.h>
86#include <linux/skbuff.h>
87#include <linux/inetdevice.h>
88#include <linux/igmp.h>
89#include <linux/if_arp.h>
90#include <linux/rtnetlink.h>
91#include <linux/times.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020092
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020093#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020094#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <net/ip.h>
96#include <net/protocol.h>
97#include <net/route.h>
98#include <net/sock.h>
99#include <net/checksum.h>
100#include <linux/netfilter_ipv4.h>
101#ifdef CONFIG_IP_MROUTE
102#include <linux/mroute.h>
103#endif
104#ifdef CONFIG_PROC_FS
105#include <linux/proc_fs.h>
106#include <linux/seq_file.h>
107#endif
108
109#define IP_MAX_MEMBERSHIPS 20
110#define IP_MAX_MSF 10
111
112#ifdef CONFIG_IP_MULTICAST
113/* Parameter names and values are taken from igmp-v2-06 draft */
114
115#define IGMP_V1_Router_Present_Timeout (400*HZ)
116#define IGMP_V2_Router_Present_Timeout (400*HZ)
117#define IGMP_Unsolicited_Report_Interval (10*HZ)
118#define IGMP_Query_Response_Interval (10*HZ)
119#define IGMP_Unsolicited_Report_Count 2
120
121
122#define IGMP_Initial_Report_Delay (1)
123
124/* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
129 */
130
Herbert Xu42f811b2007-06-04 23:34:44 -0700131#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700143static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static void igmpv3_clear_delrec(struct in_device *in_dev);
145static int sf_setstate(struct ip_mc_list *pmc);
146static void sf_markstate(struct ip_mc_list *pmc);
147#endif
148static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700149static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000152
153static void ip_mc_list_reclaim(struct rcu_head *head)
154{
155 kfree(container_of(head, struct ip_mc_list, rcu));
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void ip_ma_put(struct ip_mc_list *im)
159{
160 if (atomic_dec_and_test(&im->refcnt)) {
161 in_dev_put(im->interface);
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000162 call_rcu(&im->rcu, ip_mc_list_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164}
165
David S. Millerd9aa9382010-11-15 08:52:02 -0800166#define for_each_pmc_rcu(in_dev, pmc) \
167 for (pmc = rcu_dereference(in_dev->mc_list); \
168 pmc != NULL; \
169 pmc = rcu_dereference(pmc->next_rcu))
170
171#define for_each_pmc_rtnl(in_dev, pmc) \
172 for (pmc = rtnl_dereference(in_dev->mc_list); \
173 pmc != NULL; \
174 pmc = rtnl_dereference(pmc->next_rcu))
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#ifdef CONFIG_IP_MULTICAST
177
178/*
179 * Timer management
180 */
181
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000182static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 spin_lock_bh(&im->lock);
185 if (del_timer(&im->timer))
186 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800187 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 im->reporter = 0;
189 im->unsolicit_count = 0;
190 spin_unlock_bh(&im->lock);
191}
192
193/* It must be called with locked im->lock */
194static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
195{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800196 int tv = net_random() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800198 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!mod_timer(&im->timer, jiffies+tv+2))
200 atomic_inc(&im->refcnt);
201}
202
203static void igmp_gq_start_timer(struct in_device *in_dev)
204{
205 int tv = net_random() % in_dev->mr_maxdelay;
206
207 in_dev->mr_gq_running = 1;
208 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
209 in_dev_hold(in_dev);
210}
211
212static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
213{
214 int tv = net_random() % delay;
215
216 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
217 in_dev_hold(in_dev);
218}
219
220static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
221{
222 spin_lock_bh(&im->lock);
223 im->unsolicit_count = 0;
224 if (del_timer(&im->timer)) {
225 if ((long)(im->timer.expires-jiffies) < max_delay) {
226 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800227 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spin_unlock_bh(&im->lock);
229 return;
230 }
231 atomic_dec(&im->refcnt);
232 }
233 igmp_start_timer(im, max_delay);
234 spin_unlock_bh(&im->lock);
235}
236
237
238/*
239 * Send an IGMP report.
240 */
241
242#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
243
244
245static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
246 int gdeleted, int sdeleted)
247{
248 switch (type) {
249 case IGMPV3_MODE_IS_INCLUDE:
250 case IGMPV3_MODE_IS_EXCLUDE:
251 if (gdeleted || sdeleted)
252 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800253 if (!(pmc->gsquery && !psf->sf_gsresp)) {
254 if (pmc->sfmode == MCAST_INCLUDE)
255 return 1;
256 /* don't include if this source is excluded
257 * in all filters
258 */
259 if (psf->sf_count[MCAST_INCLUDE])
260 return type == IGMPV3_MODE_IS_INCLUDE;
261 return pmc->sfcount[MCAST_EXCLUDE] ==
262 psf->sf_count[MCAST_EXCLUDE];
263 }
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 case IGMPV3_CHANGE_TO_INCLUDE:
266 if (gdeleted || sdeleted)
267 return 0;
268 return psf->sf_count[MCAST_INCLUDE] != 0;
269 case IGMPV3_CHANGE_TO_EXCLUDE:
270 if (gdeleted || sdeleted)
271 return 0;
272 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
273 psf->sf_count[MCAST_INCLUDE])
274 return 0;
275 return pmc->sfcount[MCAST_EXCLUDE] ==
276 psf->sf_count[MCAST_EXCLUDE];
277 case IGMPV3_ALLOW_NEW_SOURCES:
278 if (gdeleted || !psf->sf_crcount)
279 return 0;
280 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
281 case IGMPV3_BLOCK_OLD_SOURCES:
282 if (pmc->sfmode == MCAST_INCLUDE)
283 return gdeleted || (psf->sf_crcount && sdeleted);
284 return psf->sf_crcount && !gdeleted && !sdeleted;
285 }
286 return 0;
287}
288
289static int
290igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
291{
292 struct ip_sf_list *psf;
293 int scount = 0;
294
295 for (psf=pmc->sources; psf; psf=psf->sf_next) {
296 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
297 continue;
298 scount++;
299 }
300 return scount;
301}
302
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000303#define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
304
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
306{
307 struct sk_buff *skb;
308 struct rtable *rt;
309 struct iphdr *pip;
310 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700311 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700312 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000314 while (1) {
315 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev),
316 GFP_ATOMIC | __GFP_NOWARN);
317 if (skb)
318 break;
319 size >>= 1;
320 if (size < 256)
321 return NULL;
322 }
323 igmp_skb_size(skb) = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
David S. Miller31e45432011-05-03 20:25:42 -0700325 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500326 0, 0,
327 IPPROTO_IGMP, 0, dev->ifindex);
328 if (IS_ERR(rt)) {
329 kfree_skb(skb);
330 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Changli Gaod8d1f302010-06-10 23:31:35 -0700333 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 skb->dev = dev;
335
336 skb_reserve(skb, LL_RESERVED_SPACE(dev));
337
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300338 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700339 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300340 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 pip->version = 4;
343 pip->ihl = (sizeof(struct iphdr)+4)>>2;
344 pip->tos = 0xc0;
345 pip->frag_off = htons(IP_DF);
346 pip->ttl = 1;
347 pip->daddr = rt->rt_dst;
348 pip->saddr = rt->rt_src;
349 pip->protocol = IPPROTO_IGMP;
350 pip->tot_len = 0; /* filled in later */
Changli Gaod8d1f302010-06-10 23:31:35 -0700351 ip_select_ident(pip, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 ((u8*)&pip[1])[0] = IPOPT_RA;
353 ((u8*)&pip[1])[1] = 4;
354 ((u8*)&pip[1])[2] = 0;
355 ((u8*)&pip[1])[3] = 0;
356
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700357 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300358 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300359 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
361 pig->resv1 = 0;
362 pig->csum = 0;
363 pig->resv2 = 0;
364 pig->ngrec = 0;
365 return skb;
366}
367
368static int igmpv3_sendpack(struct sk_buff *skb)
369{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300370 struct igmphdr *pig = igmp_hdr(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700371 const int igmplen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300373 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Herbert Xuc439cb22008-01-11 19:14:00 -0800375 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
379{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800380 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
384 int type, struct igmpv3_grec **ppgr)
385{
386 struct net_device *dev = pmc->interface->dev;
387 struct igmpv3_report *pih;
388 struct igmpv3_grec *pgr;
389
390 if (!skb)
391 skb = igmpv3_newpack(dev, dev->mtu);
392 if (!skb)
393 return NULL;
394 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
395 pgr->grec_type = type;
396 pgr->grec_auxwords = 0;
397 pgr->grec_nsrcs = 0;
398 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300399 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 pih->ngrec = htons(ntohs(pih->ngrec)+1);
401 *ppgr = pgr;
402 return skb;
403}
404
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000405#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 skb_tailroom(skb)) : 0)
407
408static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
409 int type, int gdeleted, int sdeleted)
410{
411 struct net_device *dev = pmc->interface->dev;
412 struct igmpv3_report *pih;
413 struct igmpv3_grec *pgr = NULL;
414 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800415 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if (pmc->multiaddr == IGMP_ALL_HOSTS)
418 return skb;
419
420 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
421 type == IGMPV3_MODE_IS_EXCLUDE;
422 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
423 type == IGMPV3_CHANGE_TO_EXCLUDE;
424
David L Stevensad125832006-01-18 14:20:56 -0800425 stotal = scount = 0;
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
428
David L Stevensad125832006-01-18 14:20:56 -0800429 if (!*psf_list)
430 goto empty_source;
431
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300432 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* EX and TO_EX get a fresh packet, if needed */
435 if (truncate) {
436 if (pih && pih->ngrec &&
437 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
438 if (skb)
439 igmpv3_sendpack(skb);
440 skb = igmpv3_newpack(dev, dev->mtu);
441 }
442 }
443 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 psf_prev = NULL;
445 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700446 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 psf_next = psf->sf_next;
449
450 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
451 psf_prev = psf;
452 continue;
453 }
454
455 /* clear marks on query responses */
456 if (isquery)
457 psf->sf_gsresp = 0;
458
Al Viro63007722006-09-27 18:31:32 -0700459 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 first*sizeof(struct igmpv3_grec)) {
461 if (truncate && !first)
462 break; /* truncate these */
463 if (pgr)
464 pgr->grec_nsrcs = htons(scount);
465 if (skb)
466 igmpv3_sendpack(skb);
467 skb = igmpv3_newpack(dev, dev->mtu);
468 first = 1;
469 scount = 0;
470 }
471 if (first) {
472 skb = add_grhead(skb, pmc, type, &pgr);
473 first = 0;
474 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800475 if (!skb)
476 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700477 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800479 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
481 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
482 psf->sf_crcount--;
483 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
484 if (psf_prev)
485 psf_prev->sf_next = psf->sf_next;
486 else
487 *psf_list = psf->sf_next;
488 kfree(psf);
489 continue;
490 }
491 }
492 psf_prev = psf;
493 }
David L Stevensad125832006-01-18 14:20:56 -0800494
495empty_source:
496 if (!stotal) {
497 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
498 type == IGMPV3_BLOCK_OLD_SOURCES)
499 return skb;
500 if (pmc->crcount || isquery) {
501 /* make sure we have room for group header */
502 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
503 igmpv3_sendpack(skb);
504 skb = NULL; /* add_grhead will get a new one */
505 }
506 skb = add_grhead(skb, pmc, type, &pgr);
507 }
508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (pgr)
510 pgr->grec_nsrcs = htons(scount);
511
512 if (isquery)
513 pmc->gsquery = 0; /* clear query state on report */
514 return skb;
515}
516
517static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
518{
519 struct sk_buff *skb = NULL;
520 int type;
521
522 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000523 rcu_read_lock();
524 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (pmc->multiaddr == IGMP_ALL_HOSTS)
526 continue;
527 spin_lock_bh(&pmc->lock);
528 if (pmc->sfcount[MCAST_EXCLUDE])
529 type = IGMPV3_MODE_IS_EXCLUDE;
530 else
531 type = IGMPV3_MODE_IS_INCLUDE;
532 skb = add_grec(skb, pmc, type, 0, 0);
533 spin_unlock_bh(&pmc->lock);
534 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000535 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 } else {
537 spin_lock_bh(&pmc->lock);
538 if (pmc->sfcount[MCAST_EXCLUDE])
539 type = IGMPV3_MODE_IS_EXCLUDE;
540 else
541 type = IGMPV3_MODE_IS_INCLUDE;
542 skb = add_grec(skb, pmc, type, 0, 0);
543 spin_unlock_bh(&pmc->lock);
544 }
545 if (!skb)
546 return 0;
547 return igmpv3_sendpack(skb);
548}
549
550/*
551 * remove zero-count source records from a source filter list
552 */
553static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
554{
555 struct ip_sf_list *psf_prev, *psf_next, *psf;
556
557 psf_prev = NULL;
558 for (psf=*ppsf; psf; psf = psf_next) {
559 psf_next = psf->sf_next;
560 if (psf->sf_crcount == 0) {
561 if (psf_prev)
562 psf_prev->sf_next = psf->sf_next;
563 else
564 *ppsf = psf->sf_next;
565 kfree(psf);
566 } else
567 psf_prev = psf;
568 }
569}
570
571static void igmpv3_send_cr(struct in_device *in_dev)
572{
573 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
574 struct sk_buff *skb = NULL;
575 int type, dtype;
576
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000577 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 spin_lock_bh(&in_dev->mc_tomb_lock);
579
580 /* deleted MCA's */
581 pmc_prev = NULL;
582 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
583 pmc_next = pmc->next;
584 if (pmc->sfmode == MCAST_INCLUDE) {
585 type = IGMPV3_BLOCK_OLD_SOURCES;
586 dtype = IGMPV3_BLOCK_OLD_SOURCES;
587 skb = add_grec(skb, pmc, type, 1, 0);
588 skb = add_grec(skb, pmc, dtype, 1, 1);
589 }
590 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (pmc->sfmode == MCAST_EXCLUDE) {
592 type = IGMPV3_CHANGE_TO_INCLUDE;
593 skb = add_grec(skb, pmc, type, 1, 0);
594 }
David L Stevensad125832006-01-18 14:20:56 -0800595 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (pmc->crcount == 0) {
597 igmpv3_clear_zeros(&pmc->tomb);
598 igmpv3_clear_zeros(&pmc->sources);
599 }
600 }
601 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
602 if (pmc_prev)
603 pmc_prev->next = pmc_next;
604 else
605 in_dev->mc_tomb = pmc_next;
606 in_dev_put(pmc->interface);
607 kfree(pmc);
608 } else
609 pmc_prev = pmc;
610 }
611 spin_unlock_bh(&in_dev->mc_tomb_lock);
612
613 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000614 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 spin_lock_bh(&pmc->lock);
616 if (pmc->sfcount[MCAST_EXCLUDE]) {
617 type = IGMPV3_BLOCK_OLD_SOURCES;
618 dtype = IGMPV3_ALLOW_NEW_SOURCES;
619 } else {
620 type = IGMPV3_ALLOW_NEW_SOURCES;
621 dtype = IGMPV3_BLOCK_OLD_SOURCES;
622 }
623 skb = add_grec(skb, pmc, type, 0, 0);
624 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
625
626 /* filter mode changes */
627 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (pmc->sfmode == MCAST_EXCLUDE)
629 type = IGMPV3_CHANGE_TO_EXCLUDE;
630 else
631 type = IGMPV3_CHANGE_TO_INCLUDE;
632 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800633 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635 spin_unlock_bh(&pmc->lock);
636 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000637 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 if (!skb)
640 return;
641 (void) igmpv3_sendpack(skb);
642}
643
644static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
645 int type)
646{
647 struct sk_buff *skb;
648 struct iphdr *iph;
649 struct igmphdr *ih;
650 struct rtable *rt;
651 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700652 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700653 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700654 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700655 __be32 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
658 return igmpv3_send_report(in_dev, pmc);
659 else if (type == IGMP_HOST_LEAVE_MESSAGE)
660 dst = IGMP_ALL_ROUTER;
661 else
662 dst = group;
663
David S. Miller31e45432011-05-03 20:25:42 -0700664 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500665 0, 0,
666 IPPROTO_IGMP, 0, dev->ifindex);
667 if (IS_ERR(rt))
668 return -1;
669
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800670 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (skb == NULL) {
672 ip_rt_put(rt);
673 return -1;
674 }
675
Changli Gaod8d1f302010-06-10 23:31:35 -0700676 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 skb_reserve(skb, LL_RESERVED_SPACE(dev));
679
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300680 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700681 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300682 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 iph->version = 4;
685 iph->ihl = (sizeof(struct iphdr)+4)>>2;
686 iph->tos = 0xc0;
687 iph->frag_off = htons(IP_DF);
688 iph->ttl = 1;
689 iph->daddr = dst;
690 iph->saddr = rt->rt_src;
691 iph->protocol = IPPROTO_IGMP;
Changli Gaod8d1f302010-06-10 23:31:35 -0700692 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ((u8*)&iph[1])[0] = IPOPT_RA;
694 ((u8*)&iph[1])[1] = 4;
695 ((u8*)&iph[1])[2] = 0;
696 ((u8*)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800699 ih->type = type;
700 ih->code = 0;
701 ih->csum = 0;
702 ih->group = group;
703 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Herbert Xuc439cb22008-01-11 19:14:00 -0800705 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708static void igmp_gq_timer_expire(unsigned long data)
709{
710 struct in_device *in_dev = (struct in_device *)data;
711
712 in_dev->mr_gq_running = 0;
713 igmpv3_send_report(in_dev, NULL);
714 __in_dev_put(in_dev);
715}
716
717static void igmp_ifc_timer_expire(unsigned long data)
718{
719 struct in_device *in_dev = (struct in_device *)data;
720
721 igmpv3_send_cr(in_dev);
722 if (in_dev->mr_ifc_count) {
723 in_dev->mr_ifc_count--;
724 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
725 }
726 __in_dev_put(in_dev);
727}
728
729static void igmp_ifc_event(struct in_device *in_dev)
730{
731 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
732 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900733 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 IGMP_Unsolicited_Report_Count;
735 igmp_ifc_start_timer(in_dev, 1);
736}
737
738
739static void igmp_timer_expire(unsigned long data)
740{
741 struct ip_mc_list *im=(struct ip_mc_list *)data;
742 struct in_device *in_dev = im->interface;
743
744 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800745 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 if (im->unsolicit_count) {
748 im->unsolicit_count--;
749 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
750 }
751 im->reporter = 1;
752 spin_unlock(&im->lock);
753
754 if (IGMP_V1_SEEN(in_dev))
755 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
756 else if (IGMP_V2_SEEN(in_dev))
757 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
758 else
759 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
760
761 ip_ma_put(im);
762}
763
David L Stevensad125832006-01-18 14:20:56 -0800764/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700765static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct ip_sf_list *psf;
768 int i, scount;
769
770 scount = 0;
771 for (psf=pmc->sources; psf; psf=psf->sf_next) {
772 if (scount == nsrcs)
773 break;
David L Stevensad125832006-01-18 14:20:56 -0800774 for (i=0; i<nsrcs; i++) {
775 /* skip inactive filters */
776 if (pmc->sfcount[MCAST_INCLUDE] ||
777 pmc->sfcount[MCAST_EXCLUDE] !=
778 psf->sf_count[MCAST_EXCLUDE])
779 continue;
780 if (srcs[i] == psf->sf_inaddr) {
781 scount++;
782 break;
783 }
784 }
785 }
786 pmc->gsquery = 0;
787 if (scount == nsrcs) /* all sources excluded */
788 return 0;
789 return 1;
790}
791
Al Viroea4d9e72006-09-27 18:30:52 -0700792static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800793{
794 struct ip_sf_list *psf;
795 int i, scount;
796
797 if (pmc->sfmode == MCAST_EXCLUDE)
798 return igmp_xmarksources(pmc, nsrcs, srcs);
799
800 /* mark INCLUDE-mode sources */
801 scount = 0;
802 for (psf=pmc->sources; psf; psf=psf->sf_next) {
803 if (scount == nsrcs)
804 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 for (i=0; i<nsrcs; i++)
806 if (srcs[i] == psf->sf_inaddr) {
807 psf->sf_gsresp = 1;
808 scount++;
809 break;
810 }
811 }
David L Stevensad125832006-01-18 14:20:56 -0800812 if (!scount) {
813 pmc->gsquery = 0;
814 return 0;
815 }
816 pmc->gsquery = 1;
817 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Al Viro63007722006-09-27 18:31:32 -0700820static void igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 struct ip_mc_list *im;
823
824 /* Timers are only set for non-local groups */
825
826 if (group == IGMP_ALL_HOSTS)
827 return;
828
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000829 rcu_read_lock();
830 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (im->multiaddr == group) {
832 igmp_stop_timer(im);
833 break;
834 }
835 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000836 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
840 int len)
841{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300842 struct igmphdr *ih = igmp_hdr(skb);
843 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700845 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int max_delay;
847 int mark = 0;
848
849
David Stevens5b7c8402010-09-30 14:29:40 +0000850 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (ih->code == 0) {
852 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 max_delay = IGMP_Query_Response_Interval;
855 in_dev->mr_v1_seen = jiffies +
856 IGMP_V1_Router_Present_Timeout;
857 group = 0;
858 } else {
859 /* v2 router present */
860 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
861 in_dev->mr_v2_seen = jiffies +
862 IGMP_V2_Router_Present_Timeout;
863 }
864 /* cancel the interface change timer */
865 in_dev->mr_ifc_count = 0;
866 if (del_timer(&in_dev->mr_ifc_timer))
867 __in_dev_put(in_dev);
868 /* clear deleted report items */
869 igmpv3_clear_delrec(in_dev);
870 } else if (len < 12) {
871 return; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000872 } else if (IGMP_V1_SEEN(in_dev)) {
873 /* This is a v3 query with v1 queriers present */
874 max_delay = IGMP_Query_Response_Interval;
875 group = 0;
876 } else if (IGMP_V2_SEEN(in_dev)) {
877 /* this is a v3 query with v2 queriers present;
878 * Interpretation of the max_delay code is problematic here.
879 * A real v2 host would use ih_code directly, while v3 has a
880 * different encoding. We use the v3 encoding as more likely
881 * to be intended in a v3 query.
882 */
883 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 } else { /* v3 */
885 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
886 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900887
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300888 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900890 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700891 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300893 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
896 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
897 if (!max_delay)
898 max_delay = 1; /* can't mod w/ 0 */
899 in_dev->mr_maxdelay = max_delay;
900 if (ih3->qrv)
901 in_dev->mr_qrv = ih3->qrv;
902 if (!group) { /* general query */
903 if (ih3->nsrcs)
904 return; /* no sources allowed */
905 igmp_gq_start_timer(in_dev);
906 return;
907 }
908 /* mark sources to include, if group & source-specific */
909 mark = ih3->nsrcs != 0;
910 }
911
912 /*
913 * - Start the timers in all of our membership records
914 * that the query applies to for the interface on
915 * which the query arrived excl. those that belong
916 * to a "local" group (224.0.0.X)
917 * - For timers already running check if they need to
918 * be reset.
919 * - Use the igmp->igmp_code field as the maximum
920 * delay possible
921 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000922 rcu_read_lock();
923 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800924 int changed;
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (group && group != im->multiaddr)
927 continue;
928 if (im->multiaddr == IGMP_ALL_HOSTS)
929 continue;
930 spin_lock_bh(&im->lock);
931 if (im->tm_running)
932 im->gsquery = im->gsquery && mark;
933 else
934 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800935 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900936 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800938 if (changed)
939 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000941 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000944/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945int igmp_rcv(struct sk_buff *skb)
946{
947 /* This basically follows the spec line by line -- see RFC1112 */
948 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000949 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 int len = skb->len;
951
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800952 if (in_dev == NULL)
953 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Herbert Xufb286bb2005-11-10 13:01:24 -0800955 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000956 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800957
958 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700959 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800960 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800961 break;
962 /* fall through */
963 case CHECKSUM_NONE:
964 skb->csum = 0;
965 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000966 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300969 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 switch (ih->type) {
971 case IGMP_HOST_MEMBERSHIP_QUERY:
972 igmp_heard_query(in_dev, skb, len);
973 break;
974 case IGMP_HOST_MEMBERSHIP_REPORT:
975 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -0800977 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
David Stevens24c69272005-12-02 20:32:59 -0800979 /* don't rely on MC router hearing unicast reports */
980 if (skb->pkt_type == PACKET_MULTICAST ||
981 skb->pkt_type == PACKET_BROADCAST)
982 igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
984 case IGMP_PIM:
985#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 return pim_rcv_v1(skb);
987#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +0000988 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 case IGMP_DVMRP:
990 case IGMP_TRACE:
991 case IGMP_HOST_LEAVE_MESSAGE:
992 case IGMP_MTRACE:
993 case IGMP_MTRACE_RESP:
994 break;
995 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -0800996 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Herbert Xufb286bb2005-11-10 13:01:24 -0800998
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800999drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 kfree_skb(skb);
1001 return 0;
1002}
1003
1004#endif
1005
1006
1007/*
1008 * Add a filter to a device
1009 */
1010
Al Viro63007722006-09-27 18:31:32 -07001011static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 char buf[MAX_ADDR_LEN];
1014 struct net_device *dev = in_dev->dev;
1015
1016 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1017 We will get multicast token leakage, when IFF_MULTICAST
1018 is changed. This check should be done in dev->set_multicast_list
1019 routine. Something sort of:
1020 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1021 --ANK
1022 */
1023 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001024 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
1027/*
1028 * Remove a filter from a device
1029 */
1030
Al Viro63007722006-09-27 18:31:32 -07001031static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 char buf[MAX_ADDR_LEN];
1034 struct net_device *dev = in_dev->dev;
1035
1036 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001037 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040#ifdef CONFIG_IP_MULTICAST
1041/*
1042 * deleted ip_mc_list manipulation
1043 */
1044static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1045{
1046 struct ip_mc_list *pmc;
1047
1048 /* this is an "ip_mc_list" for convenience; only the fields below
1049 * are actually used. In particular, the refcnt and users are not
1050 * used for management of the delete list. Using the same structure
1051 * for deleted items allows change reports to use common code with
1052 * non-deleted or query-response MCA's.
1053 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001054 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (!pmc)
1056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 spin_lock_bh(&im->lock);
1058 pmc->interface = im->interface;
1059 in_dev_hold(in_dev);
1060 pmc->multiaddr = im->multiaddr;
1061 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1062 IGMP_Unsolicited_Report_Count;
1063 pmc->sfmode = im->sfmode;
1064 if (pmc->sfmode == MCAST_INCLUDE) {
1065 struct ip_sf_list *psf;
1066
1067 pmc->tomb = im->tomb;
1068 pmc->sources = im->sources;
1069 im->tomb = im->sources = NULL;
1070 for (psf=pmc->sources; psf; psf=psf->sf_next)
1071 psf->sf_crcount = pmc->crcount;
1072 }
1073 spin_unlock_bh(&im->lock);
1074
1075 spin_lock_bh(&in_dev->mc_tomb_lock);
1076 pmc->next = in_dev->mc_tomb;
1077 in_dev->mc_tomb = pmc;
1078 spin_unlock_bh(&in_dev->mc_tomb_lock);
1079}
1080
Al Viro63007722006-09-27 18:31:32 -07001081static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct ip_mc_list *pmc, *pmc_prev;
1084 struct ip_sf_list *psf, *psf_next;
1085
1086 spin_lock_bh(&in_dev->mc_tomb_lock);
1087 pmc_prev = NULL;
1088 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1089 if (pmc->multiaddr == multiaddr)
1090 break;
1091 pmc_prev = pmc;
1092 }
1093 if (pmc) {
1094 if (pmc_prev)
1095 pmc_prev->next = pmc->next;
1096 else
1097 in_dev->mc_tomb = pmc->next;
1098 }
1099 spin_unlock_bh(&in_dev->mc_tomb_lock);
1100 if (pmc) {
1101 for (psf=pmc->tomb; psf; psf=psf_next) {
1102 psf_next = psf->sf_next;
1103 kfree(psf);
1104 }
1105 in_dev_put(pmc->interface);
1106 kfree(pmc);
1107 }
1108}
1109
1110static void igmpv3_clear_delrec(struct in_device *in_dev)
1111{
1112 struct ip_mc_list *pmc, *nextpmc;
1113
1114 spin_lock_bh(&in_dev->mc_tomb_lock);
1115 pmc = in_dev->mc_tomb;
1116 in_dev->mc_tomb = NULL;
1117 spin_unlock_bh(&in_dev->mc_tomb_lock);
1118
1119 for (; pmc; pmc = nextpmc) {
1120 nextpmc = pmc->next;
1121 ip_mc_clear_src(pmc);
1122 in_dev_put(pmc->interface);
1123 kfree(pmc);
1124 }
1125 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001126 rcu_read_lock();
1127 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct ip_sf_list *psf, *psf_next;
1129
1130 spin_lock_bh(&pmc->lock);
1131 psf = pmc->tomb;
1132 pmc->tomb = NULL;
1133 spin_unlock_bh(&pmc->lock);
1134 for (; psf; psf=psf_next) {
1135 psf_next = psf->sf_next;
1136 kfree(psf);
1137 }
1138 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001139 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141#endif
1142
1143static void igmp_group_dropped(struct ip_mc_list *im)
1144{
1145 struct in_device *in_dev = im->interface;
1146#ifdef CONFIG_IP_MULTICAST
1147 int reporter;
1148#endif
1149
1150 if (im->loaded) {
1151 im->loaded = 0;
1152 ip_mc_filter_del(in_dev, im->multiaddr);
1153 }
1154
1155#ifdef CONFIG_IP_MULTICAST
1156 if (im->multiaddr == IGMP_ALL_HOSTS)
1157 return;
1158
1159 reporter = im->reporter;
1160 igmp_stop_timer(im);
1161
1162 if (!in_dev->dead) {
1163 if (IGMP_V1_SEEN(in_dev))
1164 goto done;
1165 if (IGMP_V2_SEEN(in_dev)) {
1166 if (reporter)
1167 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1168 goto done;
1169 }
1170 /* IGMPv3 */
1171 igmpv3_add_delrec(in_dev, im);
1172
1173 igmp_ifc_event(in_dev);
1174 }
1175done:
1176#endif
1177 ip_mc_clear_src(im);
1178}
1179
1180static void igmp_group_added(struct ip_mc_list *im)
1181{
1182 struct in_device *in_dev = im->interface;
1183
1184 if (im->loaded == 0) {
1185 im->loaded = 1;
1186 ip_mc_filter_add(in_dev, im->multiaddr);
1187 }
1188
1189#ifdef CONFIG_IP_MULTICAST
1190 if (im->multiaddr == IGMP_ALL_HOSTS)
1191 return;
1192
1193 if (in_dev->dead)
1194 return;
1195 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1196 spin_lock_bh(&im->lock);
1197 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1198 spin_unlock_bh(&im->lock);
1199 return;
1200 }
1201 /* else, v3 */
1202
1203 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1204 IGMP_Unsolicited_Report_Count;
1205 igmp_ifc_event(in_dev);
1206#endif
1207}
1208
1209
1210/*
1211 * Multicast list managers
1212 */
1213
1214
1215/*
1216 * A socket has joined a multicast group on device dev.
1217 */
1218
Al Viro8f935bb2006-09-27 18:30:07 -07001219void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
1221 struct ip_mc_list *im;
1222
1223 ASSERT_RTNL();
1224
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001225 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (im->multiaddr == addr) {
1227 im->users++;
1228 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1229 goto out;
1230 }
1231 }
1232
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001233 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (!im)
1235 goto out;
1236
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001237 im->users = 1;
1238 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001240 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 /* initial mode is (EX, empty) */
1242 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 atomic_set(&im->refcnt, 1);
1245 spin_lock_init(&im->lock);
1246#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001247 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001250
1251 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001252 in_dev->mc_count++;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001253 rcu_assign_pointer(in_dev->mc_list, im);
1254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255#ifdef CONFIG_IP_MULTICAST
1256 igmpv3_del_delrec(in_dev, im->multiaddr);
1257#endif
1258 igmp_group_added(im);
1259 if (!in_dev->dead)
1260 ip_rt_multicast_event(in_dev);
1261out:
1262 return;
1263}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001264EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001267 * Resend IGMP JOIN report; used for bonding.
Eric Dumazet866f3b22010-11-18 09:33:19 -08001268 * Called with rcu_read_lock()
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001269 */
Eric Dumazet866f3b22010-11-18 09:33:19 -08001270void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001271{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001272#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001273 struct ip_mc_list *im;
1274 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001275
Eric Dumazet866f3b22010-11-18 09:33:19 -08001276 for_each_pmc_rcu(in_dev, im) {
1277 if (im->multiaddr == IGMP_ALL_HOSTS)
1278 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001279
Eric Dumazet866f3b22010-11-18 09:33:19 -08001280 /* a failover is happening and switches
1281 * must be notified immediately
1282 */
1283 if (IGMP_V1_SEEN(in_dev))
1284 type = IGMP_HOST_MEMBERSHIP_REPORT;
1285 else if (IGMP_V2_SEEN(in_dev))
1286 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1287 else
1288 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1289 igmp_send_report(in_dev, im, type);
1290 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001291#endif
1292}
Eric Dumazet866f3b22010-11-18 09:33:19 -08001293EXPORT_SYMBOL(ip_mc_rejoin_groups);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001294
1295/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * A socket has left a multicast group on device dev
1297 */
1298
Al Viro8f935bb2006-09-27 18:30:07 -07001299void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001301 struct ip_mc_list *i;
1302 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001305
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001306 for (ip = &in_dev->mc_list;
1307 (i = rtnl_dereference(*ip)) != NULL;
1308 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001309 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 if (--i->users == 0) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001311 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001312 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 igmp_group_dropped(i);
1314
1315 if (!in_dev->dead)
1316 ip_rt_multicast_event(in_dev);
1317
1318 ip_ma_put(i);
1319 return;
1320 }
1321 break;
1322 }
1323 }
1324}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001325EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Moni Shoua75c78502009-09-15 02:37:40 -07001327/* Device changing type */
1328
1329void ip_mc_unmap(struct in_device *in_dev)
1330{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001331 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001332
1333 ASSERT_RTNL();
1334
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001335 for_each_pmc_rtnl(in_dev, pmc)
1336 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001337}
1338
1339void ip_mc_remap(struct in_device *in_dev)
1340{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001341 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001342
1343 ASSERT_RTNL();
1344
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001345 for_each_pmc_rtnl(in_dev, pmc)
1346 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001347}
1348
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349/* Device going down */
1350
1351void ip_mc_down(struct in_device *in_dev)
1352{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001353 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 ASSERT_RTNL();
1356
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001357 for_each_pmc_rtnl(in_dev, pmc)
1358 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360#ifdef CONFIG_IP_MULTICAST
1361 in_dev->mr_ifc_count = 0;
1362 if (del_timer(&in_dev->mr_ifc_timer))
1363 __in_dev_put(in_dev);
1364 in_dev->mr_gq_running = 0;
1365 if (del_timer(&in_dev->mr_gq_timer))
1366 __in_dev_put(in_dev);
1367 igmpv3_clear_delrec(in_dev);
1368#endif
1369
1370 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1371}
1372
1373void ip_mc_init_dev(struct in_device *in_dev)
1374{
1375 ASSERT_RTNL();
1376
1377 in_dev->mc_tomb = NULL;
1378#ifdef CONFIG_IP_MULTICAST
1379 in_dev->mr_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001380 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1381 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 in_dev->mr_ifc_count = 0;
Rami Rosenb8bae412008-10-07 15:34:37 -07001383 in_dev->mc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001384 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1385 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1387#endif
1388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 spin_lock_init(&in_dev->mc_tomb_lock);
1390}
1391
1392/* Device going up */
1393
1394void ip_mc_up(struct in_device *in_dev)
1395{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001396 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 ASSERT_RTNL();
1399
1400 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1401
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001402 for_each_pmc_rtnl(in_dev, pmc)
1403 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406/*
1407 * Device is about to be destroyed: clean up.
1408 */
1409
1410void ip_mc_destroy_dev(struct in_device *in_dev)
1411{
1412 struct ip_mc_list *i;
1413
1414 ASSERT_RTNL();
1415
1416 /* Deactivate timers */
1417 ip_mc_down(in_dev);
1418
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001419 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1420 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001421 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 igmp_group_dropped(i);
1424 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001428/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001429static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 struct net_device *dev = NULL;
1432 struct in_device *idev = NULL;
1433
1434 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001435 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return idev;
1437 }
1438 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001439 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 if (!dev)
1441 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 }
1443
David S. Millerb23dd4f2011-03-02 14:31:35 -08001444 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001445 struct rtable *rt = ip_route_output(net,
1446 imr->imr_multiaddr.s_addr,
1447 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001448 if (!IS_ERR(rt)) {
1449 dev = rt->dst.dev;
1450 ip_rt_put(rt);
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453 if (dev) {
1454 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001455 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
1457 return idev;
1458}
1459
1460/*
1461 * Join a socket to a group
1462 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001463int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1464int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
1466
1467static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001468 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
1470 struct ip_sf_list *psf, *psf_prev;
1471 int rv = 0;
1472
1473 psf_prev = NULL;
1474 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1475 if (psf->sf_inaddr == *psfsrc)
1476 break;
1477 psf_prev = psf;
1478 }
1479 if (!psf || psf->sf_count[sfmode] == 0) {
1480 /* source filter not found, or count wrong => bug */
1481 return -ESRCH;
1482 }
1483 psf->sf_count[sfmode]--;
1484 if (psf->sf_count[sfmode] == 0) {
1485 ip_rt_multicast_event(pmc->interface);
1486 }
1487 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1488#ifdef CONFIG_IP_MULTICAST
1489 struct in_device *in_dev = pmc->interface;
1490#endif
1491
1492 /* no more filters for this source */
1493 if (psf_prev)
1494 psf_prev->sf_next = psf->sf_next;
1495 else
1496 pmc->sources = psf->sf_next;
1497#ifdef CONFIG_IP_MULTICAST
1498 if (psf->sf_oldin &&
1499 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001500 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 IGMP_Unsolicited_Report_Count;
1502 psf->sf_next = pmc->tomb;
1503 pmc->tomb = psf;
1504 rv = 1;
1505 } else
1506#endif
1507 kfree(psf);
1508 }
1509 return rv;
1510}
1511
1512#ifndef CONFIG_IP_MULTICAST
1513#define igmp_ifc_event(x) do { } while (0)
1514#endif
1515
Al Viro8f935bb2006-09-27 18:30:07 -07001516static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1517 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
1519 struct ip_mc_list *pmc;
1520 int changerec = 0;
1521 int i, err;
1522
1523 if (!in_dev)
1524 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001525 rcu_read_lock();
1526 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (*pmca == pmc->multiaddr)
1528 break;
1529 }
1530 if (!pmc) {
1531 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001532 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -ESRCH;
1534 }
1535 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001536 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537#ifdef CONFIG_IP_MULTICAST
1538 sf_markstate(pmc);
1539#endif
1540 if (!delta) {
1541 err = -EINVAL;
1542 if (!pmc->sfcount[sfmode])
1543 goto out_unlock;
1544 pmc->sfcount[sfmode]--;
1545 }
1546 err = 0;
1547 for (i=0; i<sfcount; i++) {
1548 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1549
1550 changerec |= rv > 0;
1551 if (!err && rv < 0)
1552 err = rv;
1553 }
1554 if (pmc->sfmode == MCAST_EXCLUDE &&
1555 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1556 pmc->sfcount[MCAST_INCLUDE]) {
1557#ifdef CONFIG_IP_MULTICAST
1558 struct ip_sf_list *psf;
1559#endif
1560
1561 /* filter mode change */
1562 pmc->sfmode = MCAST_INCLUDE;
1563#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001564 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 IGMP_Unsolicited_Report_Count;
1566 in_dev->mr_ifc_count = pmc->crcount;
1567 for (psf=pmc->sources; psf; psf = psf->sf_next)
1568 psf->sf_crcount = 0;
1569 igmp_ifc_event(pmc->interface);
1570 } else if (sf_setstate(pmc) || changerec) {
1571 igmp_ifc_event(pmc->interface);
1572#endif
1573 }
1574out_unlock:
1575 spin_unlock_bh(&pmc->lock);
1576 return err;
1577}
1578
1579/*
1580 * Add multicast single-source filter to the interface list
1581 */
1582static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001583 __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 struct ip_sf_list *psf, *psf_prev;
1586
1587 psf_prev = NULL;
1588 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1589 if (psf->sf_inaddr == *psfsrc)
1590 break;
1591 psf_prev = psf;
1592 }
1593 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001594 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 if (!psf)
1596 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 psf->sf_inaddr = *psfsrc;
1598 if (psf_prev) {
1599 psf_prev->sf_next = psf;
1600 } else
1601 pmc->sources = psf;
1602 }
1603 psf->sf_count[sfmode]++;
1604 if (psf->sf_count[sfmode] == 1) {
1605 ip_rt_multicast_event(pmc->interface);
1606 }
1607 return 0;
1608}
1609
1610#ifdef CONFIG_IP_MULTICAST
1611static void sf_markstate(struct ip_mc_list *pmc)
1612{
1613 struct ip_sf_list *psf;
1614 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1615
1616 for (psf=pmc->sources; psf; psf=psf->sf_next)
1617 if (pmc->sfcount[MCAST_EXCLUDE]) {
1618 psf->sf_oldin = mca_xcount ==
1619 psf->sf_count[MCAST_EXCLUDE] &&
1620 !psf->sf_count[MCAST_INCLUDE];
1621 } else
1622 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1623}
1624
1625static int sf_setstate(struct ip_mc_list *pmc)
1626{
David L Stevensad125832006-01-18 14:20:56 -08001627 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1629 int qrv = pmc->interface->mr_qrv;
1630 int new_in, rv;
1631
1632 rv = 0;
1633 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1634 if (pmc->sfcount[MCAST_EXCLUDE]) {
1635 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1636 !psf->sf_count[MCAST_INCLUDE];
1637 } else
1638 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001639 if (new_in) {
1640 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001641 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001642
1643 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1644 if (dpsf->sf_inaddr == psf->sf_inaddr)
1645 break;
1646 prev = dpsf;
1647 }
1648 if (dpsf) {
1649 if (prev)
1650 prev->sf_next = dpsf->sf_next;
1651 else
1652 pmc->tomb = dpsf->sf_next;
1653 kfree(dpsf);
1654 }
1655 psf->sf_crcount = qrv;
1656 rv++;
1657 }
1658 } else if (psf->sf_oldin) {
1659
1660 psf->sf_crcount = 0;
1661 /*
1662 * add or update "delete" records if an active filter
1663 * is now inactive
1664 */
1665 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1666 if (dpsf->sf_inaddr == psf->sf_inaddr)
1667 break;
1668 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001669 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001670 if (!dpsf)
1671 continue;
1672 *dpsf = *psf;
1673 /* pmc->lock held by callers */
1674 dpsf->sf_next = pmc->tomb;
1675 pmc->tomb = dpsf;
1676 }
1677 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 rv++;
1679 }
1680 }
1681 return rv;
1682}
1683#endif
1684
1685/*
1686 * Add multicast source filter list to the interface list
1687 */
Al Viro8f935bb2006-09-27 18:30:07 -07001688static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1689 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 struct ip_mc_list *pmc;
1692 int isexclude;
1693 int i, err;
1694
1695 if (!in_dev)
1696 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001697 rcu_read_lock();
1698 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (*pmca == pmc->multiaddr)
1700 break;
1701 }
1702 if (!pmc) {
1703 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001704 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return -ESRCH;
1706 }
1707 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001708 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710#ifdef CONFIG_IP_MULTICAST
1711 sf_markstate(pmc);
1712#endif
1713 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1714 if (!delta)
1715 pmc->sfcount[sfmode]++;
1716 err = 0;
1717 for (i=0; i<sfcount; i++) {
1718 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1719 if (err)
1720 break;
1721 }
1722 if (err) {
1723 int j;
1724
1725 pmc->sfcount[sfmode]--;
1726 for (j=0; j<i; j++)
1727 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1728 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1729#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001731 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732#endif
1733
1734 /* filter mode change */
1735 if (pmc->sfcount[MCAST_EXCLUDE])
1736 pmc->sfmode = MCAST_EXCLUDE;
1737 else if (pmc->sfcount[MCAST_INCLUDE])
1738 pmc->sfmode = MCAST_INCLUDE;
1739#ifdef CONFIG_IP_MULTICAST
1740 /* else no filters; keep old mode for reports */
1741
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001742 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 IGMP_Unsolicited_Report_Count;
1744 in_dev->mr_ifc_count = pmc->crcount;
1745 for (psf=pmc->sources; psf; psf = psf->sf_next)
1746 psf->sf_crcount = 0;
1747 igmp_ifc_event(in_dev);
1748 } else if (sf_setstate(pmc)) {
1749 igmp_ifc_event(in_dev);
1750#endif
1751 }
1752 spin_unlock_bh(&pmc->lock);
1753 return err;
1754}
1755
1756static void ip_mc_clear_src(struct ip_mc_list *pmc)
1757{
1758 struct ip_sf_list *psf, *nextpsf;
1759
1760 for (psf=pmc->tomb; psf; psf=nextpsf) {
1761 nextpsf = psf->sf_next;
1762 kfree(psf);
1763 }
1764 pmc->tomb = NULL;
1765 for (psf=pmc->sources; psf; psf=nextpsf) {
1766 nextpsf = psf->sf_next;
1767 kfree(psf);
1768 }
1769 pmc->sources = NULL;
1770 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001771 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 pmc->sfcount[MCAST_EXCLUDE] = 1;
1773}
1774
1775
1776/*
1777 * Join a multicast group
1778 */
1779int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1780{
1781 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001782 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001783 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 struct in_device *in_dev;
1785 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001786 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001787 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 int count = 0;
1789
Joe Perchesf97c1e02007-12-16 13:45:43 -08001790 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return -EINVAL;
1792
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001793 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Daniel Lezcano877aced2008-08-13 16:15:57 -07001795 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 if (!in_dev) {
1798 iml = NULL;
1799 err = -ENODEV;
1800 goto done;
1801 }
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001804 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001805 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001806 if (i->multi.imr_multiaddr.s_addr == addr &&
1807 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 count++;
1810 }
1811 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001812 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001814 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001815 if (iml == NULL)
1816 goto done;
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001819 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 iml->sflist = NULL;
1821 iml->sfmode = MCAST_EXCLUDE;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001822 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001826 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return err;
1828}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001829EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001831static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1832{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001833 kfree(container_of(rp, struct ip_sf_socklist, rcu));
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001834 /* sk_omem_alloc should have been decreased by the caller*/
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1838 struct in_device *in_dev)
1839{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001840 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 int err;
1842
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001843 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /* any-source empty exclude case */
1845 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1846 iml->sfmode, 0, NULL, 0);
1847 }
1848 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001849 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1850 rcu_assign_pointer(iml->sflist, NULL);
1851 /* decrease mem now to avoid the memleak warning */
1852 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1853 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return err;
1855}
1856
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001857
1858static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1859{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001860 kfree(container_of(rp, struct ip_mc_socklist, rcu));
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001861 /* sk_omem_alloc should have been decreased by the caller*/
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001862}
1863
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865/*
1866 * Ask a socket to leave a group.
1867 */
1868
1869int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1870{
1871 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001872 struct ip_mc_socklist *iml;
1873 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001874 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001875 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001876 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001877 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001878 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001881 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001882 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001883 for (imlp = &inet->mc_list;
1884 (iml = rtnl_dereference(*imlp)) != NULL;
1885 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001886 if (iml->multi.imr_multiaddr.s_addr != group)
1887 continue;
1888 if (ifindex) {
1889 if (iml->multi.imr_ifindex != ifindex)
1890 continue;
1891 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1892 iml->multi.imr_address.s_addr)
1893 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
David L Stevensacd6e002006-08-17 16:27:39 -07001895 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001897 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001898
1899 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001900 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001901 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001902 /* decrease mem now to avoid the memleak warning */
1903 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1904 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
David L Stevensacd6e002006-08-17 16:27:39 -07001905 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
David L Stevensacd6e002006-08-17 16:27:39 -07001907 if (!in_dev)
1908 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001910 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
1913int ip_mc_source(int add, int omode, struct sock *sk, struct
1914 ip_mreq_source *mreqs, int ifindex)
1915{
1916 int err;
1917 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001918 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct ip_mc_socklist *pmc;
1920 struct in_device *in_dev = NULL;
1921 struct inet_sock *inet = inet_sk(sk);
1922 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001923 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001924 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 int i, j, rv;
1926
Joe Perchesf97c1e02007-12-16 13:45:43 -08001927 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return -EINVAL;
1929
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001930 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1933 imr.imr_address.s_addr = mreqs->imr_interface;
1934 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001935 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 if (!in_dev) {
1938 err = -ENODEV;
1939 goto done;
1940 }
1941 err = -EADDRNOTAVAIL;
1942
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001943 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08001944 if ((pmc->multi.imr_multiaddr.s_addr ==
1945 imr.imr_multiaddr.s_addr) &&
1946 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 break;
1948 }
David L Stevens917f2f12005-07-08 17:45:16 -07001949 if (!pmc) { /* must have a prior join */
1950 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /* if a source filter was set, must be the same mode as before */
1954 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07001955 if (pmc->sfmode != omode) {
1956 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 } else if (pmc->sfmode != omode) {
1960 /* allow mode switches for empty-set filters */
1961 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001962 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 NULL, 0);
1964 pmc->sfmode = omode;
1965 }
1966
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001967 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (!add) {
1969 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07001970 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 rv = !0;
1972 for (i=0; i<psl->sl_count; i++) {
1973 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001974 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (rv == 0)
1976 break;
1977 }
1978 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07001979 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
David L Stevens8cdaaa12005-07-08 17:39:23 -07001981 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1982 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1983 leavegroup = 1;
1984 goto done;
1985 }
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001988 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 &mreqs->imr_sourceaddr, 1);
1990
1991 for (j=i+1; j<psl->sl_count; j++)
1992 psl->sl_addr[j-1] = psl->sl_addr[j];
1993 psl->sl_count--;
1994 err = 0;
1995 goto done;
1996 }
1997 /* else, add a new source to the filter */
1998
1999 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2000 err = -ENOBUFS;
2001 goto done;
2002 }
2003 if (!psl || psl->sl_count == psl->sl_max) {
2004 struct ip_sf_socklist *newpsl;
2005 int count = IP_SFBLOCK;
2006
2007 if (psl)
2008 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002009 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 if (!newpsl) {
2011 err = -ENOBUFS;
2012 goto done;
2013 }
2014 newpsl->sl_max = count;
2015 newpsl->sl_count = count - IP_SFBLOCK;
2016 if (psl) {
2017 for (i=0; i<psl->sl_count; i++)
2018 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002019 /* decrease mem now to avoid the memleak warning */
2020 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2021 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002023 rcu_assign_pointer(pmc->sflist, newpsl);
2024 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2027 for (i=0; i<psl->sl_count; i++) {
2028 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002029 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (rv == 0)
2031 break;
2032 }
2033 if (rv == 0) /* address already there is an error */
2034 goto done;
2035 for (j=psl->sl_count-1; j>=i; j--)
2036 psl->sl_addr[j+1] = psl->sl_addr[j];
2037 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2038 psl->sl_count++;
2039 err = 0;
2040 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002041 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 &mreqs->imr_sourceaddr, 1);
2043done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002044 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002045 if (leavegroup)
2046 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return err;
2048}
2049
2050int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2051{
David L Stevens9951f032005-07-08 17:47:28 -07002052 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002054 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 struct ip_mc_socklist *pmc;
2056 struct in_device *in_dev;
2057 struct inet_sock *inet = inet_sk(sk);
2058 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002059 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002060 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Joe Perchesf97c1e02007-12-16 13:45:43 -08002062 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -EINVAL;
2064 if (msf->imsf_fmode != MCAST_INCLUDE &&
2065 msf->imsf_fmode != MCAST_EXCLUDE)
2066 return -EINVAL;
2067
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002068 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2071 imr.imr_address.s_addr = msf->imsf_interface;
2072 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002073 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 if (!in_dev) {
2076 err = -ENODEV;
2077 goto done;
2078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
David L Stevens9951f032005-07-08 17:47:28 -07002080 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2081 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2082 leavegroup = 1;
2083 goto done;
2084 }
2085
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002086 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2088 pmc->multi.imr_ifindex == imr.imr_ifindex)
2089 break;
2090 }
David L Stevens917f2f12005-07-08 17:45:16 -07002091 if (!pmc) { /* must have a prior join */
2092 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002096 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2097 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (!newpsl) {
2099 err = -ENOBUFS;
2100 goto done;
2101 }
2102 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2103 memcpy(newpsl->sl_addr, msf->imsf_slist,
2104 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2105 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2106 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2107 if (err) {
2108 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2109 goto done;
2110 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002113 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2114 msf->imsf_fmode, 0, NULL, 0);
2115 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002116 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (psl) {
2118 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2119 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002120 /* decrease mem now to avoid the memleak warning */
2121 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2122 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 } else
2124 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2125 0, NULL, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002126 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002128 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002130 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002131 if (leavegroup)
2132 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 return err;
2134}
2135
2136int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2137 struct ip_msfilter __user *optval, int __user *optlen)
2138{
2139 int err, len, count, copycount;
2140 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002141 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 struct ip_mc_socklist *pmc;
2143 struct in_device *in_dev;
2144 struct inet_sock *inet = inet_sk(sk);
2145 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002146 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Joe Perchesf97c1e02007-12-16 13:45:43 -08002148 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return -EINVAL;
2150
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002151 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2154 imr.imr_address.s_addr = msf->imsf_interface;
2155 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002156 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 if (!in_dev) {
2159 err = -ENODEV;
2160 goto done;
2161 }
2162 err = -EADDRNOTAVAIL;
2163
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002164 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2166 pmc->multi.imr_ifindex == imr.imr_ifindex)
2167 break;
2168 }
2169 if (!pmc) /* must have a prior join */
2170 goto done;
2171 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002172 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002173 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!psl) {
2175 len = 0;
2176 count = 0;
2177 } else {
2178 count = psl->sl_count;
2179 }
2180 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2181 len = copycount * sizeof(psl->sl_addr[0]);
2182 msf->imsf_numsrc = count;
2183 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2184 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2185 return -EFAULT;
2186 }
2187 if (len &&
2188 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2189 return -EFAULT;
2190 return 0;
2191done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002192 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return err;
2194}
2195
2196int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2197 struct group_filter __user *optval, int __user *optlen)
2198{
2199 int err, i, count, copycount;
2200 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002201 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 struct ip_mc_socklist *pmc;
2203 struct inet_sock *inet = inet_sk(sk);
2204 struct ip_sf_socklist *psl;
2205
2206 psin = (struct sockaddr_in *)&gsf->gf_group;
2207 if (psin->sin_family != AF_INET)
2208 return -EINVAL;
2209 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002210 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return -EINVAL;
2212
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002213 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 err = -EADDRNOTAVAIL;
2216
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002217 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2219 pmc->multi.imr_ifindex == gsf->gf_interface)
2220 break;
2221 }
2222 if (!pmc) /* must have a prior join */
2223 goto done;
2224 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002225 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002226 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 count = psl ? psl->sl_count : 0;
2228 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2229 gsf->gf_numsrc = count;
2230 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2231 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2232 return -EFAULT;
2233 }
2234 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 struct sockaddr_storage ss;
2236
2237 psin = (struct sockaddr_in *)&ss;
2238 memset(&ss, 0, sizeof(ss));
2239 psin->sin_family = AF_INET;
2240 psin->sin_addr.s_addr = psl->sl_addr[i];
2241 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2242 return -EFAULT;
2243 }
2244 return 0;
2245done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002246 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return err;
2248}
2249
2250/*
2251 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2252 */
Al Viroc0cda062006-09-27 18:31:10 -07002253int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
2255 struct inet_sock *inet = inet_sk(sk);
2256 struct ip_mc_socklist *pmc;
2257 struct ip_sf_socklist *psl;
2258 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002259 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002261 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002262 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002265 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002266 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2268 pmc->multi.imr_ifindex == dif)
2269 break;
2270 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002271 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002273 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002274 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002275 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002277 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
2279 for (i=0; i<psl->sl_count; i++) {
2280 if (psl->sl_addr[i] == rmt_addr)
2281 break;
2282 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002283 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002285 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002287 goto unlock;
2288 ret = 1;
2289unlock:
2290 rcu_read_unlock();
2291out:
2292 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
2295/*
2296 * A socket is closing.
2297 */
2298
2299void ip_mc_drop_socket(struct sock *sk)
2300{
2301 struct inet_sock *inet = inet_sk(sk);
2302 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002303 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 if (inet->mc_list == NULL)
2306 return;
2307
2308 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002309 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002312 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002313 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002314 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002315 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002317 /* decrease mem now to avoid the memleak warning */
2318 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2319 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321 rtnl_unlock();
2322}
2323
David S. Millerdbdd9a52011-03-10 16:34:38 -08002324/* called with rcu_read_lock() */
2325int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
2327 struct ip_mc_list *im;
2328 struct ip_sf_list *psf;
2329 int rv = 0;
2330
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002331 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (im->multiaddr == mc_addr)
2333 break;
2334 }
2335 if (im && proto == IPPROTO_IGMP) {
2336 rv = 1;
2337 } else if (im) {
2338 if (src_addr) {
2339 for (psf=im->sources; psf; psf=psf->sf_next) {
2340 if (psf->sf_inaddr == src_addr)
2341 break;
2342 }
2343 if (psf)
2344 rv = psf->sf_count[MCAST_INCLUDE] ||
2345 psf->sf_count[MCAST_EXCLUDE] !=
2346 im->sfcount[MCAST_EXCLUDE];
2347 else
2348 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2349 } else
2350 rv = 1; /* unspecified source; tentatively allow */
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return rv;
2353}
2354
2355#if defined(CONFIG_PROC_FS)
2356struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002357 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 struct net_device *dev;
2359 struct in_device *in_dev;
2360};
2361
2362#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2363
2364static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2365{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002366 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct ip_mc_list *im = NULL;
2368 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2369
Pavel Emelianov7562f872007-05-03 15:13:45 -07002370 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002371 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002373
2374 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (!in_dev)
2376 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002377 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (im) {
2379 state->in_dev = in_dev;
2380 break;
2381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 }
2383 return im;
2384}
2385
2386static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2387{
2388 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002389
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002390 im = rcu_dereference(im->next_rcu);
2391 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002392 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (!state->dev) {
2394 state->in_dev = NULL;
2395 break;
2396 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002397 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (!state->in_dev)
2399 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002400 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 return im;
2403}
2404
2405static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2406{
2407 struct ip_mc_list *im = igmp_mc_get_first(seq);
2408 if (im)
2409 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2410 --pos;
2411 return pos ? NULL : im;
2412}
2413
2414static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002415 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
stephen hemminger61fbab72009-11-10 07:54:55 +00002417 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2419}
2420
2421static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2422{
2423 struct ip_mc_list *im;
2424 if (v == SEQ_START_TOKEN)
2425 im = igmp_mc_get_first(seq);
2426 else
2427 im = igmp_mc_get_next(seq, v);
2428 ++*pos;
2429 return im;
2430}
2431
2432static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002433 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
2435 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002436
2437 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002439 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
2442static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2443{
2444 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002445 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2447 else {
2448 struct ip_mc_list *im = (struct ip_mc_list *)v;
2449 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2450 char *querier;
2451#ifdef CONFIG_IP_MULTICAST
2452 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2453 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2454 "V3";
2455#else
2456 querier = "NONE";
2457#endif
2458
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002459 if (rcu_dereference(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002461 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463
2464 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002465 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 im->multiaddr, im->users,
2467 im->tm_running, im->tm_running ?
2468 jiffies_to_clock_t(im->timer.expires-jiffies) : 0,
2469 im->reporter);
2470 }
2471 return 0;
2472}
2473
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002474static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 .start = igmp_mc_seq_start,
2476 .next = igmp_mc_seq_next,
2477 .stop = igmp_mc_seq_stop,
2478 .show = igmp_mc_seq_show,
2479};
2480
2481static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2482{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002483 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002484 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486
Arjan van de Ven9a321442007-02-12 00:55:35 -08002487static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 .owner = THIS_MODULE,
2489 .open = igmp_mc_seq_open,
2490 .read = seq_read,
2491 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002492 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493};
2494
2495struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002496 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 struct net_device *dev;
2498 struct in_device *idev;
2499 struct ip_mc_list *im;
2500};
2501
2502#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2503
2504static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2505{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002506 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct ip_sf_list *psf = NULL;
2508 struct ip_mc_list *im = NULL;
2509 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2510
Pavel Emelianov7562f872007-05-03 15:13:45 -07002511 state->idev = NULL;
2512 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002513 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002515 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (unlikely(idev == NULL))
2517 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002518 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (likely(im != NULL)) {
2520 spin_lock_bh(&im->lock);
2521 psf = im->sources;
2522 if (likely(psf != NULL)) {
2523 state->im = im;
2524 state->idev = idev;
2525 break;
2526 }
2527 spin_unlock_bh(&im->lock);
2528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530 return psf;
2531}
2532
2533static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2534{
2535 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2536
2537 psf = psf->sf_next;
2538 while (!psf) {
2539 spin_unlock_bh(&state->im->lock);
2540 state->im = state->im->next;
2541 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002542 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (!state->dev) {
2544 state->idev = NULL;
2545 goto out;
2546 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002547 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (!state->idev)
2549 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002550 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552 if (!state->im)
2553 break;
2554 spin_lock_bh(&state->im->lock);
2555 psf = state->im->sources;
2556 }
2557out:
2558 return psf;
2559}
2560
2561static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2562{
2563 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2564 if (psf)
2565 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2566 --pos;
2567 return pos ? NULL : psf;
2568}
2569
2570static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002571 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
stephen hemminger61fbab72009-11-10 07:54:55 +00002573 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2575}
2576
2577static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2578{
2579 struct ip_sf_list *psf;
2580 if (v == SEQ_START_TOKEN)
2581 psf = igmp_mcf_get_first(seq);
2582 else
2583 psf = igmp_mcf_get_next(seq, v);
2584 ++*pos;
2585 return psf;
2586}
2587
2588static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002589 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
2591 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2592 if (likely(state->im != NULL)) {
2593 spin_unlock_bh(&state->im->lock);
2594 state->im = NULL;
2595 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002596 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002598 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
2601static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2602{
2603 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2604 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2605
2606 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002607 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 "%3s %6s "
2609 "%10s %10s %6s %6s\n", "Idx",
2610 "Device", "MCA",
2611 "SRC", "INC", "EXC");
2612 } else {
2613 seq_printf(seq,
2614 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002615 "0x%08x %6lu %6lu\n",
2616 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 ntohl(state->im->multiaddr),
2618 ntohl(psf->sf_inaddr),
2619 psf->sf_count[MCAST_INCLUDE],
2620 psf->sf_count[MCAST_EXCLUDE]);
2621 }
2622 return 0;
2623}
2624
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002625static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 .start = igmp_mcf_seq_start,
2627 .next = igmp_mcf_seq_next,
2628 .stop = igmp_mcf_seq_stop,
2629 .show = igmp_mcf_seq_show,
2630};
2631
2632static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2633{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002634 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002635 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
Arjan van de Ven9a321442007-02-12 00:55:35 -08002638static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 .owner = THIS_MODULE,
2640 .open = igmp_mcf_seq_open,
2641 .read = seq_read,
2642 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002643 .release = seq_release_net,
2644};
2645
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002646static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002647{
2648 struct proc_dir_entry *pde;
2649
2650 pde = proc_net_fops_create(net, "igmp", S_IRUGO, &igmp_mc_seq_fops);
2651 if (!pde)
2652 goto out_igmp;
2653 pde = proc_net_fops_create(net, "mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2654 if (!pde)
2655 goto out_mcfilter;
2656 return 0;
2657
2658out_mcfilter:
2659 proc_net_remove(net, "igmp");
2660out_igmp:
2661 return -ENOMEM;
2662}
2663
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002664static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002665{
2666 proc_net_remove(net, "mcfilter");
2667 proc_net_remove(net, "igmp");
2668}
2669
2670static struct pernet_operations igmp_net_ops = {
2671 .init = igmp_net_init,
2672 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673};
2674
2675int __init igmp_mc_proc_init(void)
2676{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002677 return register_pernet_subsys(&igmp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679#endif