blob: 50f6bc1a002acc7324dd46586d5698fed1090b62 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
3 *
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
6 *
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Authors:
Alan Cox113aa832008-10-13 19:01:08 -070012 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * Fixes:
20 *
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
23 * functions.
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
32 * points.
33 *
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090036 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050 * memberships but was being deleted,
51 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * from %p with timer not initialized\n"
53 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090054 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
58 * true (960216).
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
70 * Vinay Kulkarni
71 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/uaccess.h>
76#include <asm/system.h>
77#include <linux/types.h>
78#include <linux/kernel.h>
79#include <linux/jiffies.h>
80#include <linux/string.h>
81#include <linux/socket.h>
82#include <linux/sockios.h>
83#include <linux/in.h>
84#include <linux/inet.h>
85#include <linux/netdevice.h>
86#include <linux/skbuff.h>
87#include <linux/inetdevice.h>
88#include <linux/igmp.h>
89#include <linux/if_arp.h>
90#include <linux/rtnetlink.h>
91#include <linux/times.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020092
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020093#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020094#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <net/ip.h>
96#include <net/protocol.h>
97#include <net/route.h>
98#include <net/sock.h>
99#include <net/checksum.h>
100#include <linux/netfilter_ipv4.h>
101#ifdef CONFIG_IP_MROUTE
102#include <linux/mroute.h>
103#endif
104#ifdef CONFIG_PROC_FS
105#include <linux/proc_fs.h>
106#include <linux/seq_file.h>
107#endif
108
109#define IP_MAX_MEMBERSHIPS 20
110#define IP_MAX_MSF 10
111
112#ifdef CONFIG_IP_MULTICAST
113/* Parameter names and values are taken from igmp-v2-06 draft */
114
115#define IGMP_V1_Router_Present_Timeout (400*HZ)
116#define IGMP_V2_Router_Present_Timeout (400*HZ)
117#define IGMP_Unsolicited_Report_Interval (10*HZ)
118#define IGMP_Query_Response_Interval (10*HZ)
119#define IGMP_Unsolicited_Report_Count 2
120
121
122#define IGMP_Initial_Report_Delay (1)
123
124/* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
129 */
130
Herbert Xu42f811b2007-06-04 23:34:44 -0700131#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700143static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static void igmpv3_clear_delrec(struct in_device *in_dev);
145static int sf_setstate(struct ip_mc_list *pmc);
146static void sf_markstate(struct ip_mc_list *pmc);
147#endif
148static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700149static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000152
153static void ip_mc_list_reclaim(struct rcu_head *head)
154{
155 kfree(container_of(head, struct ip_mc_list, rcu));
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static void ip_ma_put(struct ip_mc_list *im)
159{
160 if (atomic_dec_and_test(&im->refcnt)) {
161 in_dev_put(im->interface);
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000162 call_rcu(&im->rcu, ip_mc_list_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 }
164}
165
David S. Millerd9aa9382010-11-15 08:52:02 -0800166#define for_each_pmc_rcu(in_dev, pmc) \
167 for (pmc = rcu_dereference(in_dev->mc_list); \
168 pmc != NULL; \
169 pmc = rcu_dereference(pmc->next_rcu))
170
171#define for_each_pmc_rtnl(in_dev, pmc) \
172 for (pmc = rtnl_dereference(in_dev->mc_list); \
173 pmc != NULL; \
174 pmc = rtnl_dereference(pmc->next_rcu))
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176#ifdef CONFIG_IP_MULTICAST
177
178/*
179 * Timer management
180 */
181
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000182static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 spin_lock_bh(&im->lock);
185 if (del_timer(&im->timer))
186 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800187 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 im->reporter = 0;
189 im->unsolicit_count = 0;
190 spin_unlock_bh(&im->lock);
191}
192
193/* It must be called with locked im->lock */
194static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
195{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800196 int tv = net_random() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800198 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!mod_timer(&im->timer, jiffies+tv+2))
200 atomic_inc(&im->refcnt);
201}
202
203static void igmp_gq_start_timer(struct in_device *in_dev)
204{
205 int tv = net_random() % in_dev->mr_maxdelay;
206
207 in_dev->mr_gq_running = 1;
208 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
209 in_dev_hold(in_dev);
210}
211
212static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
213{
214 int tv = net_random() % delay;
215
216 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
217 in_dev_hold(in_dev);
218}
219
220static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
221{
222 spin_lock_bh(&im->lock);
223 im->unsolicit_count = 0;
224 if (del_timer(&im->timer)) {
225 if ((long)(im->timer.expires-jiffies) < max_delay) {
226 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800227 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 spin_unlock_bh(&im->lock);
229 return;
230 }
231 atomic_dec(&im->refcnt);
232 }
233 igmp_start_timer(im, max_delay);
234 spin_unlock_bh(&im->lock);
235}
236
237
238/*
239 * Send an IGMP report.
240 */
241
242#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
243
244
245static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
246 int gdeleted, int sdeleted)
247{
248 switch (type) {
249 case IGMPV3_MODE_IS_INCLUDE:
250 case IGMPV3_MODE_IS_EXCLUDE:
251 if (gdeleted || sdeleted)
252 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800253 if (!(pmc->gsquery && !psf->sf_gsresp)) {
254 if (pmc->sfmode == MCAST_INCLUDE)
255 return 1;
256 /* don't include if this source is excluded
257 * in all filters
258 */
259 if (psf->sf_count[MCAST_INCLUDE])
260 return type == IGMPV3_MODE_IS_INCLUDE;
261 return pmc->sfcount[MCAST_EXCLUDE] ==
262 psf->sf_count[MCAST_EXCLUDE];
263 }
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 case IGMPV3_CHANGE_TO_INCLUDE:
266 if (gdeleted || sdeleted)
267 return 0;
268 return psf->sf_count[MCAST_INCLUDE] != 0;
269 case IGMPV3_CHANGE_TO_EXCLUDE:
270 if (gdeleted || sdeleted)
271 return 0;
272 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
273 psf->sf_count[MCAST_INCLUDE])
274 return 0;
275 return pmc->sfcount[MCAST_EXCLUDE] ==
276 psf->sf_count[MCAST_EXCLUDE];
277 case IGMPV3_ALLOW_NEW_SOURCES:
278 if (gdeleted || !psf->sf_crcount)
279 return 0;
280 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
281 case IGMPV3_BLOCK_OLD_SOURCES:
282 if (pmc->sfmode == MCAST_INCLUDE)
283 return gdeleted || (psf->sf_crcount && sdeleted);
284 return psf->sf_crcount && !gdeleted && !sdeleted;
285 }
286 return 0;
287}
288
289static int
290igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
291{
292 struct ip_sf_list *psf;
293 int scount = 0;
294
295 for (psf=pmc->sources; psf; psf=psf->sf_next) {
296 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
297 continue;
298 scount++;
299 }
300 return scount;
301}
302
303static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
304{
305 struct sk_buff *skb;
306 struct rtable *rt;
307 struct iphdr *pip;
308 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700309 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Johannes Bergf5184d22008-05-12 20:48:31 -0700311 skb = alloc_skb(size + LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (skb == NULL)
313 return NULL;
314
315 {
316 struct flowi fl = { .oif = dev->ifindex,
Changli Gao58116622010-11-12 18:43:55 +0000317 .fl4_dst = IGMPV3_ALL_MCR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 .proto = IPPROTO_IGMP };
Daniel Lezcano877aced2008-08-13 16:15:57 -0700319 if (ip_route_output_key(net, &rt, &fl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 kfree_skb(skb);
321 return NULL;
322 }
323 }
324 if (rt->rt_src == 0) {
325 kfree_skb(skb);
326 ip_rt_put(rt);
327 return NULL;
328 }
329
Changli Gaod8d1f302010-06-10 23:31:35 -0700330 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 skb->dev = dev;
332
333 skb_reserve(skb, LL_RESERVED_SPACE(dev));
334
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300335 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700336 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300337 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 pip->version = 4;
340 pip->ihl = (sizeof(struct iphdr)+4)>>2;
341 pip->tos = 0xc0;
342 pip->frag_off = htons(IP_DF);
343 pip->ttl = 1;
344 pip->daddr = rt->rt_dst;
345 pip->saddr = rt->rt_src;
346 pip->protocol = IPPROTO_IGMP;
347 pip->tot_len = 0; /* filled in later */
Changli Gaod8d1f302010-06-10 23:31:35 -0700348 ip_select_ident(pip, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 ((u8*)&pip[1])[0] = IPOPT_RA;
350 ((u8*)&pip[1])[1] = 4;
351 ((u8*)&pip[1])[2] = 0;
352 ((u8*)&pip[1])[3] = 0;
353
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700354 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300355 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300356 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
358 pig->resv1 = 0;
359 pig->csum = 0;
360 pig->resv2 = 0;
361 pig->ngrec = 0;
362 return skb;
363}
364
365static int igmpv3_sendpack(struct sk_buff *skb)
366{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300367 struct igmphdr *pig = igmp_hdr(skb);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700368 const int igmplen = skb->tail - skb->transport_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300370 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Herbert Xuc439cb22008-01-11 19:14:00 -0800372 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
376{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800377 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
381 int type, struct igmpv3_grec **ppgr)
382{
383 struct net_device *dev = pmc->interface->dev;
384 struct igmpv3_report *pih;
385 struct igmpv3_grec *pgr;
386
387 if (!skb)
388 skb = igmpv3_newpack(dev, dev->mtu);
389 if (!skb)
390 return NULL;
391 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
392 pgr->grec_type = type;
393 pgr->grec_auxwords = 0;
394 pgr->grec_nsrcs = 0;
395 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300396 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 pih->ngrec = htons(ntohs(pih->ngrec)+1);
398 *ppgr = pgr;
399 return skb;
400}
401
402#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
403 skb_tailroom(skb)) : 0)
404
405static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
406 int type, int gdeleted, int sdeleted)
407{
408 struct net_device *dev = pmc->interface->dev;
409 struct igmpv3_report *pih;
410 struct igmpv3_grec *pgr = NULL;
411 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800412 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (pmc->multiaddr == IGMP_ALL_HOSTS)
415 return skb;
416
417 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
418 type == IGMPV3_MODE_IS_EXCLUDE;
419 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
420 type == IGMPV3_CHANGE_TO_EXCLUDE;
421
David L Stevensad125832006-01-18 14:20:56 -0800422 stotal = scount = 0;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
425
David L Stevensad125832006-01-18 14:20:56 -0800426 if (!*psf_list)
427 goto empty_source;
428
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300429 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* EX and TO_EX get a fresh packet, if needed */
432 if (truncate) {
433 if (pih && pih->ngrec &&
434 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
435 if (skb)
436 igmpv3_sendpack(skb);
437 skb = igmpv3_newpack(dev, dev->mtu);
438 }
439 }
440 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 psf_prev = NULL;
442 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700443 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 psf_next = psf->sf_next;
446
447 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
448 psf_prev = psf;
449 continue;
450 }
451
452 /* clear marks on query responses */
453 if (isquery)
454 psf->sf_gsresp = 0;
455
Al Viro63007722006-09-27 18:31:32 -0700456 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 first*sizeof(struct igmpv3_grec)) {
458 if (truncate && !first)
459 break; /* truncate these */
460 if (pgr)
461 pgr->grec_nsrcs = htons(scount);
462 if (skb)
463 igmpv3_sendpack(skb);
464 skb = igmpv3_newpack(dev, dev->mtu);
465 first = 1;
466 scount = 0;
467 }
468 if (first) {
469 skb = add_grhead(skb, pmc, type, &pgr);
470 first = 0;
471 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800472 if (!skb)
473 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700474 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800476 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
478 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
479 psf->sf_crcount--;
480 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
481 if (psf_prev)
482 psf_prev->sf_next = psf->sf_next;
483 else
484 *psf_list = psf->sf_next;
485 kfree(psf);
486 continue;
487 }
488 }
489 psf_prev = psf;
490 }
David L Stevensad125832006-01-18 14:20:56 -0800491
492empty_source:
493 if (!stotal) {
494 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
495 type == IGMPV3_BLOCK_OLD_SOURCES)
496 return skb;
497 if (pmc->crcount || isquery) {
498 /* make sure we have room for group header */
499 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
500 igmpv3_sendpack(skb);
501 skb = NULL; /* add_grhead will get a new one */
502 }
503 skb = add_grhead(skb, pmc, type, &pgr);
504 }
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (pgr)
507 pgr->grec_nsrcs = htons(scount);
508
509 if (isquery)
510 pmc->gsquery = 0; /* clear query state on report */
511 return skb;
512}
513
514static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
515{
516 struct sk_buff *skb = NULL;
517 int type;
518
519 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000520 rcu_read_lock();
521 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (pmc->multiaddr == IGMP_ALL_HOSTS)
523 continue;
524 spin_lock_bh(&pmc->lock);
525 if (pmc->sfcount[MCAST_EXCLUDE])
526 type = IGMPV3_MODE_IS_EXCLUDE;
527 else
528 type = IGMPV3_MODE_IS_INCLUDE;
529 skb = add_grec(skb, pmc, type, 0, 0);
530 spin_unlock_bh(&pmc->lock);
531 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000532 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
534 spin_lock_bh(&pmc->lock);
535 if (pmc->sfcount[MCAST_EXCLUDE])
536 type = IGMPV3_MODE_IS_EXCLUDE;
537 else
538 type = IGMPV3_MODE_IS_INCLUDE;
539 skb = add_grec(skb, pmc, type, 0, 0);
540 spin_unlock_bh(&pmc->lock);
541 }
542 if (!skb)
543 return 0;
544 return igmpv3_sendpack(skb);
545}
546
547/*
548 * remove zero-count source records from a source filter list
549 */
550static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
551{
552 struct ip_sf_list *psf_prev, *psf_next, *psf;
553
554 psf_prev = NULL;
555 for (psf=*ppsf; psf; psf = psf_next) {
556 psf_next = psf->sf_next;
557 if (psf->sf_crcount == 0) {
558 if (psf_prev)
559 psf_prev->sf_next = psf->sf_next;
560 else
561 *ppsf = psf->sf_next;
562 kfree(psf);
563 } else
564 psf_prev = psf;
565 }
566}
567
568static void igmpv3_send_cr(struct in_device *in_dev)
569{
570 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
571 struct sk_buff *skb = NULL;
572 int type, dtype;
573
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000574 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 spin_lock_bh(&in_dev->mc_tomb_lock);
576
577 /* deleted MCA's */
578 pmc_prev = NULL;
579 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
580 pmc_next = pmc->next;
581 if (pmc->sfmode == MCAST_INCLUDE) {
582 type = IGMPV3_BLOCK_OLD_SOURCES;
583 dtype = IGMPV3_BLOCK_OLD_SOURCES;
584 skb = add_grec(skb, pmc, type, 1, 0);
585 skb = add_grec(skb, pmc, dtype, 1, 1);
586 }
587 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (pmc->sfmode == MCAST_EXCLUDE) {
589 type = IGMPV3_CHANGE_TO_INCLUDE;
590 skb = add_grec(skb, pmc, type, 1, 0);
591 }
David L Stevensad125832006-01-18 14:20:56 -0800592 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (pmc->crcount == 0) {
594 igmpv3_clear_zeros(&pmc->tomb);
595 igmpv3_clear_zeros(&pmc->sources);
596 }
597 }
598 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
599 if (pmc_prev)
600 pmc_prev->next = pmc_next;
601 else
602 in_dev->mc_tomb = pmc_next;
603 in_dev_put(pmc->interface);
604 kfree(pmc);
605 } else
606 pmc_prev = pmc;
607 }
608 spin_unlock_bh(&in_dev->mc_tomb_lock);
609
610 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000611 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 spin_lock_bh(&pmc->lock);
613 if (pmc->sfcount[MCAST_EXCLUDE]) {
614 type = IGMPV3_BLOCK_OLD_SOURCES;
615 dtype = IGMPV3_ALLOW_NEW_SOURCES;
616 } else {
617 type = IGMPV3_ALLOW_NEW_SOURCES;
618 dtype = IGMPV3_BLOCK_OLD_SOURCES;
619 }
620 skb = add_grec(skb, pmc, type, 0, 0);
621 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
622
623 /* filter mode changes */
624 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (pmc->sfmode == MCAST_EXCLUDE)
626 type = IGMPV3_CHANGE_TO_EXCLUDE;
627 else
628 type = IGMPV3_CHANGE_TO_INCLUDE;
629 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800630 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632 spin_unlock_bh(&pmc->lock);
633 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000634 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (!skb)
637 return;
638 (void) igmpv3_sendpack(skb);
639}
640
641static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
642 int type)
643{
644 struct sk_buff *skb;
645 struct iphdr *iph;
646 struct igmphdr *ih;
647 struct rtable *rt;
648 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700649 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700650 __be32 group = pmc ? pmc->multiaddr : 0;
651 __be32 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
654 return igmpv3_send_report(in_dev, pmc);
655 else if (type == IGMP_HOST_LEAVE_MESSAGE)
656 dst = IGMP_ALL_ROUTER;
657 else
658 dst = group;
659
660 {
661 struct flowi fl = { .oif = dev->ifindex,
Changli Gao58116622010-11-12 18:43:55 +0000662 .fl4_dst = dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 .proto = IPPROTO_IGMP };
Daniel Lezcano877aced2008-08-13 16:15:57 -0700664 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -1;
666 }
667 if (rt->rt_src == 0) {
668 ip_rt_put(rt);
669 return -1;
670 }
671
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800672 skb = alloc_skb(IGMP_SIZE+LL_ALLOCATED_SPACE(dev), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (skb == NULL) {
674 ip_rt_put(rt);
675 return -1;
676 }
677
Changli Gaod8d1f302010-06-10 23:31:35 -0700678 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 skb_reserve(skb, LL_RESERVED_SPACE(dev));
681
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300682 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700683 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300684 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 iph->version = 4;
687 iph->ihl = (sizeof(struct iphdr)+4)>>2;
688 iph->tos = 0xc0;
689 iph->frag_off = htons(IP_DF);
690 iph->ttl = 1;
691 iph->daddr = dst;
692 iph->saddr = rt->rt_src;
693 iph->protocol = IPPROTO_IGMP;
Changli Gaod8d1f302010-06-10 23:31:35 -0700694 ip_select_ident(iph, &rt->dst, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 ((u8*)&iph[1])[0] = IPOPT_RA;
696 ((u8*)&iph[1])[1] = 4;
697 ((u8*)&iph[1])[2] = 0;
698 ((u8*)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800701 ih->type = type;
702 ih->code = 0;
703 ih->csum = 0;
704 ih->group = group;
705 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Herbert Xuc439cb22008-01-11 19:14:00 -0800707 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static void igmp_gq_timer_expire(unsigned long data)
711{
712 struct in_device *in_dev = (struct in_device *)data;
713
714 in_dev->mr_gq_running = 0;
715 igmpv3_send_report(in_dev, NULL);
716 __in_dev_put(in_dev);
717}
718
719static void igmp_ifc_timer_expire(unsigned long data)
720{
721 struct in_device *in_dev = (struct in_device *)data;
722
723 igmpv3_send_cr(in_dev);
724 if (in_dev->mr_ifc_count) {
725 in_dev->mr_ifc_count--;
726 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
727 }
728 __in_dev_put(in_dev);
729}
730
731static void igmp_ifc_event(struct in_device *in_dev)
732{
733 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
734 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900735 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 IGMP_Unsolicited_Report_Count;
737 igmp_ifc_start_timer(in_dev, 1);
738}
739
740
741static void igmp_timer_expire(unsigned long data)
742{
743 struct ip_mc_list *im=(struct ip_mc_list *)data;
744 struct in_device *in_dev = im->interface;
745
746 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800747 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 if (im->unsolicit_count) {
750 im->unsolicit_count--;
751 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
752 }
753 im->reporter = 1;
754 spin_unlock(&im->lock);
755
756 if (IGMP_V1_SEEN(in_dev))
757 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
758 else if (IGMP_V2_SEEN(in_dev))
759 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
760 else
761 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
762
763 ip_ma_put(im);
764}
765
David L Stevensad125832006-01-18 14:20:56 -0800766/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700767static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct ip_sf_list *psf;
770 int i, scount;
771
772 scount = 0;
773 for (psf=pmc->sources; psf; psf=psf->sf_next) {
774 if (scount == nsrcs)
775 break;
David L Stevensad125832006-01-18 14:20:56 -0800776 for (i=0; i<nsrcs; i++) {
777 /* skip inactive filters */
778 if (pmc->sfcount[MCAST_INCLUDE] ||
779 pmc->sfcount[MCAST_EXCLUDE] !=
780 psf->sf_count[MCAST_EXCLUDE])
781 continue;
782 if (srcs[i] == psf->sf_inaddr) {
783 scount++;
784 break;
785 }
786 }
787 }
788 pmc->gsquery = 0;
789 if (scount == nsrcs) /* all sources excluded */
790 return 0;
791 return 1;
792}
793
Al Viroea4d9e72006-09-27 18:30:52 -0700794static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800795{
796 struct ip_sf_list *psf;
797 int i, scount;
798
799 if (pmc->sfmode == MCAST_EXCLUDE)
800 return igmp_xmarksources(pmc, nsrcs, srcs);
801
802 /* mark INCLUDE-mode sources */
803 scount = 0;
804 for (psf=pmc->sources; psf; psf=psf->sf_next) {
805 if (scount == nsrcs)
806 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 for (i=0; i<nsrcs; i++)
808 if (srcs[i] == psf->sf_inaddr) {
809 psf->sf_gsresp = 1;
810 scount++;
811 break;
812 }
813 }
David L Stevensad125832006-01-18 14:20:56 -0800814 if (!scount) {
815 pmc->gsquery = 0;
816 return 0;
817 }
818 pmc->gsquery = 1;
819 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820}
821
Al Viro63007722006-09-27 18:31:32 -0700822static void igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct ip_mc_list *im;
825
826 /* Timers are only set for non-local groups */
827
828 if (group == IGMP_ALL_HOSTS)
829 return;
830
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000831 rcu_read_lock();
832 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (im->multiaddr == group) {
834 igmp_stop_timer(im);
835 break;
836 }
837 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000838 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
842 int len)
843{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300844 struct igmphdr *ih = igmp_hdr(skb);
845 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700847 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int max_delay;
849 int mark = 0;
850
851
David Stevens5b7c8402010-09-30 14:29:40 +0000852 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (ih->code == 0) {
854 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 max_delay = IGMP_Query_Response_Interval;
857 in_dev->mr_v1_seen = jiffies +
858 IGMP_V1_Router_Present_Timeout;
859 group = 0;
860 } else {
861 /* v2 router present */
862 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
863 in_dev->mr_v2_seen = jiffies +
864 IGMP_V2_Router_Present_Timeout;
865 }
866 /* cancel the interface change timer */
867 in_dev->mr_ifc_count = 0;
868 if (del_timer(&in_dev->mr_ifc_timer))
869 __in_dev_put(in_dev);
870 /* clear deleted report items */
871 igmpv3_clear_delrec(in_dev);
872 } else if (len < 12) {
873 return; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000874 } else if (IGMP_V1_SEEN(in_dev)) {
875 /* This is a v3 query with v1 queriers present */
876 max_delay = IGMP_Query_Response_Interval;
877 group = 0;
878 } else if (IGMP_V2_SEEN(in_dev)) {
879 /* this is a v3 query with v2 queriers present;
880 * Interpretation of the max_delay code is problematic here.
881 * A real v2 host would use ih_code directly, while v3 has a
882 * different encoding. We use the v3 encoding as more likely
883 * to be intended in a v3 query.
884 */
885 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 } else { /* v3 */
887 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
888 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900889
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300890 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900892 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700893 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300895 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
898 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
899 if (!max_delay)
900 max_delay = 1; /* can't mod w/ 0 */
901 in_dev->mr_maxdelay = max_delay;
902 if (ih3->qrv)
903 in_dev->mr_qrv = ih3->qrv;
904 if (!group) { /* general query */
905 if (ih3->nsrcs)
906 return; /* no sources allowed */
907 igmp_gq_start_timer(in_dev);
908 return;
909 }
910 /* mark sources to include, if group & source-specific */
911 mark = ih3->nsrcs != 0;
912 }
913
914 /*
915 * - Start the timers in all of our membership records
916 * that the query applies to for the interface on
917 * which the query arrived excl. those that belong
918 * to a "local" group (224.0.0.X)
919 * - For timers already running check if they need to
920 * be reset.
921 * - Use the igmp->igmp_code field as the maximum
922 * delay possible
923 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000924 rcu_read_lock();
925 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800926 int changed;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 if (group && group != im->multiaddr)
929 continue;
930 if (im->multiaddr == IGMP_ALL_HOSTS)
931 continue;
932 spin_lock_bh(&im->lock);
933 if (im->tm_running)
934 im->gsquery = im->gsquery && mark;
935 else
936 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800937 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900938 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800940 if (changed)
941 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000943 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000946/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947int igmp_rcv(struct sk_buff *skb)
948{
949 /* This basically follows the spec line by line -- see RFC1112 */
950 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000951 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 int len = skb->len;
953
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800954 if (in_dev == NULL)
955 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Herbert Xufb286bb2005-11-10 13:01:24 -0800957 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000958 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800959
960 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700961 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800962 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800963 break;
964 /* fall through */
965 case CHECKSUM_NONE:
966 skb->csum = 0;
967 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000968 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300971 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 switch (ih->type) {
973 case IGMP_HOST_MEMBERSHIP_QUERY:
974 igmp_heard_query(in_dev, skb, len);
975 break;
976 case IGMP_HOST_MEMBERSHIP_REPORT:
977 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -0800979 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 break;
David Stevens24c69272005-12-02 20:32:59 -0800981 /* don't rely on MC router hearing unicast reports */
982 if (skb->pkt_type == PACKET_MULTICAST ||
983 skb->pkt_type == PACKET_BROADCAST)
984 igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 break;
986 case IGMP_PIM:
987#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return pim_rcv_v1(skb);
989#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +0000990 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 case IGMP_DVMRP:
992 case IGMP_TRACE:
993 case IGMP_HOST_LEAVE_MESSAGE:
994 case IGMP_MTRACE:
995 case IGMP_MTRACE_RESP:
996 break;
997 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -0800998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001000
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001001drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 kfree_skb(skb);
1003 return 0;
1004}
1005
1006#endif
1007
1008
1009/*
1010 * Add a filter to a device
1011 */
1012
Al Viro63007722006-09-27 18:31:32 -07001013static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 char buf[MAX_ADDR_LEN];
1016 struct net_device *dev = in_dev->dev;
1017
1018 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1019 We will get multicast token leakage, when IFF_MULTICAST
1020 is changed. This check should be done in dev->set_multicast_list
1021 routine. Something sort of:
1022 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1023 --ANK
1024 */
1025 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001026 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029/*
1030 * Remove a filter from a device
1031 */
1032
Al Viro63007722006-09-27 18:31:32 -07001033static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
1035 char buf[MAX_ADDR_LEN];
1036 struct net_device *dev = in_dev->dev;
1037
1038 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad2010-04-01 21:22:57 +00001039 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042#ifdef CONFIG_IP_MULTICAST
1043/*
1044 * deleted ip_mc_list manipulation
1045 */
1046static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1047{
1048 struct ip_mc_list *pmc;
1049
1050 /* this is an "ip_mc_list" for convenience; only the fields below
1051 * are actually used. In particular, the refcnt and users are not
1052 * used for management of the delete list. Using the same structure
1053 * for deleted items allows change reports to use common code with
1054 * non-deleted or query-response MCA's.
1055 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001056 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (!pmc)
1058 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 spin_lock_bh(&im->lock);
1060 pmc->interface = im->interface;
1061 in_dev_hold(in_dev);
1062 pmc->multiaddr = im->multiaddr;
1063 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1064 IGMP_Unsolicited_Report_Count;
1065 pmc->sfmode = im->sfmode;
1066 if (pmc->sfmode == MCAST_INCLUDE) {
1067 struct ip_sf_list *psf;
1068
1069 pmc->tomb = im->tomb;
1070 pmc->sources = im->sources;
1071 im->tomb = im->sources = NULL;
1072 for (psf=pmc->sources; psf; psf=psf->sf_next)
1073 psf->sf_crcount = pmc->crcount;
1074 }
1075 spin_unlock_bh(&im->lock);
1076
1077 spin_lock_bh(&in_dev->mc_tomb_lock);
1078 pmc->next = in_dev->mc_tomb;
1079 in_dev->mc_tomb = pmc;
1080 spin_unlock_bh(&in_dev->mc_tomb_lock);
1081}
1082
Al Viro63007722006-09-27 18:31:32 -07001083static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
1085 struct ip_mc_list *pmc, *pmc_prev;
1086 struct ip_sf_list *psf, *psf_next;
1087
1088 spin_lock_bh(&in_dev->mc_tomb_lock);
1089 pmc_prev = NULL;
1090 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1091 if (pmc->multiaddr == multiaddr)
1092 break;
1093 pmc_prev = pmc;
1094 }
1095 if (pmc) {
1096 if (pmc_prev)
1097 pmc_prev->next = pmc->next;
1098 else
1099 in_dev->mc_tomb = pmc->next;
1100 }
1101 spin_unlock_bh(&in_dev->mc_tomb_lock);
1102 if (pmc) {
1103 for (psf=pmc->tomb; psf; psf=psf_next) {
1104 psf_next = psf->sf_next;
1105 kfree(psf);
1106 }
1107 in_dev_put(pmc->interface);
1108 kfree(pmc);
1109 }
1110}
1111
1112static void igmpv3_clear_delrec(struct in_device *in_dev)
1113{
1114 struct ip_mc_list *pmc, *nextpmc;
1115
1116 spin_lock_bh(&in_dev->mc_tomb_lock);
1117 pmc = in_dev->mc_tomb;
1118 in_dev->mc_tomb = NULL;
1119 spin_unlock_bh(&in_dev->mc_tomb_lock);
1120
1121 for (; pmc; pmc = nextpmc) {
1122 nextpmc = pmc->next;
1123 ip_mc_clear_src(pmc);
1124 in_dev_put(pmc->interface);
1125 kfree(pmc);
1126 }
1127 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001128 rcu_read_lock();
1129 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 struct ip_sf_list *psf, *psf_next;
1131
1132 spin_lock_bh(&pmc->lock);
1133 psf = pmc->tomb;
1134 pmc->tomb = NULL;
1135 spin_unlock_bh(&pmc->lock);
1136 for (; psf; psf=psf_next) {
1137 psf_next = psf->sf_next;
1138 kfree(psf);
1139 }
1140 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143#endif
1144
1145static void igmp_group_dropped(struct ip_mc_list *im)
1146{
1147 struct in_device *in_dev = im->interface;
1148#ifdef CONFIG_IP_MULTICAST
1149 int reporter;
1150#endif
1151
1152 if (im->loaded) {
1153 im->loaded = 0;
1154 ip_mc_filter_del(in_dev, im->multiaddr);
1155 }
1156
1157#ifdef CONFIG_IP_MULTICAST
1158 if (im->multiaddr == IGMP_ALL_HOSTS)
1159 return;
1160
1161 reporter = im->reporter;
1162 igmp_stop_timer(im);
1163
1164 if (!in_dev->dead) {
1165 if (IGMP_V1_SEEN(in_dev))
1166 goto done;
1167 if (IGMP_V2_SEEN(in_dev)) {
1168 if (reporter)
1169 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1170 goto done;
1171 }
1172 /* IGMPv3 */
1173 igmpv3_add_delrec(in_dev, im);
1174
1175 igmp_ifc_event(in_dev);
1176 }
1177done:
1178#endif
1179 ip_mc_clear_src(im);
1180}
1181
1182static void igmp_group_added(struct ip_mc_list *im)
1183{
1184 struct in_device *in_dev = im->interface;
1185
1186 if (im->loaded == 0) {
1187 im->loaded = 1;
1188 ip_mc_filter_add(in_dev, im->multiaddr);
1189 }
1190
1191#ifdef CONFIG_IP_MULTICAST
1192 if (im->multiaddr == IGMP_ALL_HOSTS)
1193 return;
1194
1195 if (in_dev->dead)
1196 return;
1197 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1198 spin_lock_bh(&im->lock);
1199 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1200 spin_unlock_bh(&im->lock);
1201 return;
1202 }
1203 /* else, v3 */
1204
1205 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1206 IGMP_Unsolicited_Report_Count;
1207 igmp_ifc_event(in_dev);
1208#endif
1209}
1210
1211
1212/*
1213 * Multicast list managers
1214 */
1215
1216
1217/*
1218 * A socket has joined a multicast group on device dev.
1219 */
1220
Al Viro8f935bb2006-09-27 18:30:07 -07001221void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
1223 struct ip_mc_list *im;
1224
1225 ASSERT_RTNL();
1226
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001227 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if (im->multiaddr == addr) {
1229 im->users++;
1230 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1231 goto out;
1232 }
1233 }
1234
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001235 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 if (!im)
1237 goto out;
1238
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001239 im->users = 1;
1240 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001242 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /* initial mode is (EX, empty) */
1244 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 atomic_set(&im->refcnt, 1);
1247 spin_lock_init(&im->lock);
1248#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001249 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001252
1253 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001254 in_dev->mc_count++;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001255 rcu_assign_pointer(in_dev->mc_list, im);
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257#ifdef CONFIG_IP_MULTICAST
1258 igmpv3_del_delrec(in_dev, im->multiaddr);
1259#endif
1260 igmp_group_added(im);
1261 if (!in_dev->dead)
1262 ip_rt_multicast_event(in_dev);
1263out:
1264 return;
1265}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001266EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001269 * Resend IGMP JOIN report; used for bonding.
Eric Dumazet866f3b22010-11-18 09:33:19 -08001270 * Called with rcu_read_lock()
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001271 */
Eric Dumazet866f3b22010-11-18 09:33:19 -08001272void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001273{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001274#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001275 struct ip_mc_list *im;
1276 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001277
Eric Dumazet866f3b22010-11-18 09:33:19 -08001278 for_each_pmc_rcu(in_dev, im) {
1279 if (im->multiaddr == IGMP_ALL_HOSTS)
1280 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001281
Eric Dumazet866f3b22010-11-18 09:33:19 -08001282 /* a failover is happening and switches
1283 * must be notified immediately
1284 */
1285 if (IGMP_V1_SEEN(in_dev))
1286 type = IGMP_HOST_MEMBERSHIP_REPORT;
1287 else if (IGMP_V2_SEEN(in_dev))
1288 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1289 else
1290 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1291 igmp_send_report(in_dev, im, type);
1292 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001293#endif
1294}
Eric Dumazet866f3b22010-11-18 09:33:19 -08001295EXPORT_SYMBOL(ip_mc_rejoin_groups);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001296
1297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 * A socket has left a multicast group on device dev
1299 */
1300
Al Viro8f935bb2006-09-27 18:30:07 -07001301void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001303 struct ip_mc_list *i;
1304 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001307
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001308 for (ip = &in_dev->mc_list;
1309 (i = rtnl_dereference(*ip)) != NULL;
1310 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001311 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (--i->users == 0) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001313 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001314 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 igmp_group_dropped(i);
1316
1317 if (!in_dev->dead)
1318 ip_rt_multicast_event(in_dev);
1319
1320 ip_ma_put(i);
1321 return;
1322 }
1323 break;
1324 }
1325 }
1326}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001327EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Moni Shoua75c78502009-09-15 02:37:40 -07001329/* Device changing type */
1330
1331void ip_mc_unmap(struct in_device *in_dev)
1332{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001333 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001334
1335 ASSERT_RTNL();
1336
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001337 for_each_pmc_rtnl(in_dev, pmc)
1338 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001339}
1340
1341void ip_mc_remap(struct in_device *in_dev)
1342{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001343 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001344
1345 ASSERT_RTNL();
1346
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001347 for_each_pmc_rtnl(in_dev, pmc)
1348 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/* Device going down */
1352
1353void ip_mc_down(struct in_device *in_dev)
1354{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001355 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 ASSERT_RTNL();
1358
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001359 for_each_pmc_rtnl(in_dev, pmc)
1360 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362#ifdef CONFIG_IP_MULTICAST
1363 in_dev->mr_ifc_count = 0;
1364 if (del_timer(&in_dev->mr_ifc_timer))
1365 __in_dev_put(in_dev);
1366 in_dev->mr_gq_running = 0;
1367 if (del_timer(&in_dev->mr_gq_timer))
1368 __in_dev_put(in_dev);
1369 igmpv3_clear_delrec(in_dev);
1370#endif
1371
1372 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1373}
1374
1375void ip_mc_init_dev(struct in_device *in_dev)
1376{
1377 ASSERT_RTNL();
1378
1379 in_dev->mc_tomb = NULL;
1380#ifdef CONFIG_IP_MULTICAST
1381 in_dev->mr_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001382 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1383 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 in_dev->mr_ifc_count = 0;
Rami Rosenb8bae412008-10-07 15:34:37 -07001385 in_dev->mc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001386 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1387 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1389#endif
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 spin_lock_init(&in_dev->mc_tomb_lock);
1392}
1393
1394/* Device going up */
1395
1396void ip_mc_up(struct in_device *in_dev)
1397{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001398 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 ASSERT_RTNL();
1401
1402 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1403
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001404 for_each_pmc_rtnl(in_dev, pmc)
1405 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
1408/*
1409 * Device is about to be destroyed: clean up.
1410 */
1411
1412void ip_mc_destroy_dev(struct in_device *in_dev)
1413{
1414 struct ip_mc_list *i;
1415
1416 ASSERT_RTNL();
1417
1418 /* Deactivate timers */
1419 ip_mc_down(in_dev);
1420
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001421 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1422 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001423 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 igmp_group_dropped(i);
1426 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428}
1429
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001430/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001431static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Changli Gao58116622010-11-12 18:43:55 +00001433 struct flowi fl = { .fl4_dst = imr->imr_multiaddr.s_addr };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 struct rtable *rt;
1435 struct net_device *dev = NULL;
1436 struct in_device *idev = NULL;
1437
1438 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001439 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 return idev;
1441 }
1442 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001443 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if (!dev)
1445 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
1447
Daniel Lezcano877aced2008-08-13 16:15:57 -07001448 if (!dev && !ip_route_output_key(net, &rt, &fl)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07001449 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 ip_rt_put(rt);
1451 }
1452 if (dev) {
1453 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001454 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456 return idev;
1457}
1458
1459/*
1460 * Join a socket to a group
1461 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001462int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1463int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465
1466static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001467 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct ip_sf_list *psf, *psf_prev;
1470 int rv = 0;
1471
1472 psf_prev = NULL;
1473 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1474 if (psf->sf_inaddr == *psfsrc)
1475 break;
1476 psf_prev = psf;
1477 }
1478 if (!psf || psf->sf_count[sfmode] == 0) {
1479 /* source filter not found, or count wrong => bug */
1480 return -ESRCH;
1481 }
1482 psf->sf_count[sfmode]--;
1483 if (psf->sf_count[sfmode] == 0) {
1484 ip_rt_multicast_event(pmc->interface);
1485 }
1486 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1487#ifdef CONFIG_IP_MULTICAST
1488 struct in_device *in_dev = pmc->interface;
1489#endif
1490
1491 /* no more filters for this source */
1492 if (psf_prev)
1493 psf_prev->sf_next = psf->sf_next;
1494 else
1495 pmc->sources = psf->sf_next;
1496#ifdef CONFIG_IP_MULTICAST
1497 if (psf->sf_oldin &&
1498 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001499 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 IGMP_Unsolicited_Report_Count;
1501 psf->sf_next = pmc->tomb;
1502 pmc->tomb = psf;
1503 rv = 1;
1504 } else
1505#endif
1506 kfree(psf);
1507 }
1508 return rv;
1509}
1510
1511#ifndef CONFIG_IP_MULTICAST
1512#define igmp_ifc_event(x) do { } while (0)
1513#endif
1514
Al Viro8f935bb2006-09-27 18:30:07 -07001515static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1516 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 struct ip_mc_list *pmc;
1519 int changerec = 0;
1520 int i, err;
1521
1522 if (!in_dev)
1523 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001524 rcu_read_lock();
1525 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (*pmca == pmc->multiaddr)
1527 break;
1528 }
1529 if (!pmc) {
1530 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001531 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return -ESRCH;
1533 }
1534 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001535 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536#ifdef CONFIG_IP_MULTICAST
1537 sf_markstate(pmc);
1538#endif
1539 if (!delta) {
1540 err = -EINVAL;
1541 if (!pmc->sfcount[sfmode])
1542 goto out_unlock;
1543 pmc->sfcount[sfmode]--;
1544 }
1545 err = 0;
1546 for (i=0; i<sfcount; i++) {
1547 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1548
1549 changerec |= rv > 0;
1550 if (!err && rv < 0)
1551 err = rv;
1552 }
1553 if (pmc->sfmode == MCAST_EXCLUDE &&
1554 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1555 pmc->sfcount[MCAST_INCLUDE]) {
1556#ifdef CONFIG_IP_MULTICAST
1557 struct ip_sf_list *psf;
1558#endif
1559
1560 /* filter mode change */
1561 pmc->sfmode = MCAST_INCLUDE;
1562#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001563 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 IGMP_Unsolicited_Report_Count;
1565 in_dev->mr_ifc_count = pmc->crcount;
1566 for (psf=pmc->sources; psf; psf = psf->sf_next)
1567 psf->sf_crcount = 0;
1568 igmp_ifc_event(pmc->interface);
1569 } else if (sf_setstate(pmc) || changerec) {
1570 igmp_ifc_event(pmc->interface);
1571#endif
1572 }
1573out_unlock:
1574 spin_unlock_bh(&pmc->lock);
1575 return err;
1576}
1577
1578/*
1579 * Add multicast single-source filter to the interface list
1580 */
1581static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001582 __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 struct ip_sf_list *psf, *psf_prev;
1585
1586 psf_prev = NULL;
1587 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1588 if (psf->sf_inaddr == *psfsrc)
1589 break;
1590 psf_prev = psf;
1591 }
1592 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001593 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!psf)
1595 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 psf->sf_inaddr = *psfsrc;
1597 if (psf_prev) {
1598 psf_prev->sf_next = psf;
1599 } else
1600 pmc->sources = psf;
1601 }
1602 psf->sf_count[sfmode]++;
1603 if (psf->sf_count[sfmode] == 1) {
1604 ip_rt_multicast_event(pmc->interface);
1605 }
1606 return 0;
1607}
1608
1609#ifdef CONFIG_IP_MULTICAST
1610static void sf_markstate(struct ip_mc_list *pmc)
1611{
1612 struct ip_sf_list *psf;
1613 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1614
1615 for (psf=pmc->sources; psf; psf=psf->sf_next)
1616 if (pmc->sfcount[MCAST_EXCLUDE]) {
1617 psf->sf_oldin = mca_xcount ==
1618 psf->sf_count[MCAST_EXCLUDE] &&
1619 !psf->sf_count[MCAST_INCLUDE];
1620 } else
1621 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1622}
1623
1624static int sf_setstate(struct ip_mc_list *pmc)
1625{
David L Stevensad125832006-01-18 14:20:56 -08001626 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1628 int qrv = pmc->interface->mr_qrv;
1629 int new_in, rv;
1630
1631 rv = 0;
1632 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1633 if (pmc->sfcount[MCAST_EXCLUDE]) {
1634 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1635 !psf->sf_count[MCAST_INCLUDE];
1636 } else
1637 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001638 if (new_in) {
1639 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001640 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001641
1642 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1643 if (dpsf->sf_inaddr == psf->sf_inaddr)
1644 break;
1645 prev = dpsf;
1646 }
1647 if (dpsf) {
1648 if (prev)
1649 prev->sf_next = dpsf->sf_next;
1650 else
1651 pmc->tomb = dpsf->sf_next;
1652 kfree(dpsf);
1653 }
1654 psf->sf_crcount = qrv;
1655 rv++;
1656 }
1657 } else if (psf->sf_oldin) {
1658
1659 psf->sf_crcount = 0;
1660 /*
1661 * add or update "delete" records if an active filter
1662 * is now inactive
1663 */
1664 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1665 if (dpsf->sf_inaddr == psf->sf_inaddr)
1666 break;
1667 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001668 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001669 if (!dpsf)
1670 continue;
1671 *dpsf = *psf;
1672 /* pmc->lock held by callers */
1673 dpsf->sf_next = pmc->tomb;
1674 pmc->tomb = dpsf;
1675 }
1676 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 rv++;
1678 }
1679 }
1680 return rv;
1681}
1682#endif
1683
1684/*
1685 * Add multicast source filter list to the interface list
1686 */
Al Viro8f935bb2006-09-27 18:30:07 -07001687static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1688 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
1690 struct ip_mc_list *pmc;
1691 int isexclude;
1692 int i, err;
1693
1694 if (!in_dev)
1695 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001696 rcu_read_lock();
1697 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (*pmca == pmc->multiaddr)
1699 break;
1700 }
1701 if (!pmc) {
1702 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001703 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return -ESRCH;
1705 }
1706 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001707 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709#ifdef CONFIG_IP_MULTICAST
1710 sf_markstate(pmc);
1711#endif
1712 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1713 if (!delta)
1714 pmc->sfcount[sfmode]++;
1715 err = 0;
1716 for (i=0; i<sfcount; i++) {
1717 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1718 if (err)
1719 break;
1720 }
1721 if (err) {
1722 int j;
1723
1724 pmc->sfcount[sfmode]--;
1725 for (j=0; j<i; j++)
1726 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1727 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1728#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001730 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731#endif
1732
1733 /* filter mode change */
1734 if (pmc->sfcount[MCAST_EXCLUDE])
1735 pmc->sfmode = MCAST_EXCLUDE;
1736 else if (pmc->sfcount[MCAST_INCLUDE])
1737 pmc->sfmode = MCAST_INCLUDE;
1738#ifdef CONFIG_IP_MULTICAST
1739 /* else no filters; keep old mode for reports */
1740
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001741 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 IGMP_Unsolicited_Report_Count;
1743 in_dev->mr_ifc_count = pmc->crcount;
1744 for (psf=pmc->sources; psf; psf = psf->sf_next)
1745 psf->sf_crcount = 0;
1746 igmp_ifc_event(in_dev);
1747 } else if (sf_setstate(pmc)) {
1748 igmp_ifc_event(in_dev);
1749#endif
1750 }
1751 spin_unlock_bh(&pmc->lock);
1752 return err;
1753}
1754
1755static void ip_mc_clear_src(struct ip_mc_list *pmc)
1756{
1757 struct ip_sf_list *psf, *nextpsf;
1758
1759 for (psf=pmc->tomb; psf; psf=nextpsf) {
1760 nextpsf = psf->sf_next;
1761 kfree(psf);
1762 }
1763 pmc->tomb = NULL;
1764 for (psf=pmc->sources; psf; psf=nextpsf) {
1765 nextpsf = psf->sf_next;
1766 kfree(psf);
1767 }
1768 pmc->sources = NULL;
1769 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001770 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 pmc->sfcount[MCAST_EXCLUDE] = 1;
1772}
1773
1774
1775/*
1776 * Join a multicast group
1777 */
1778int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1779{
1780 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001781 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001782 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct in_device *in_dev;
1784 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001785 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001786 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 int count = 0;
1788
Joe Perchesf97c1e02007-12-16 13:45:43 -08001789 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 return -EINVAL;
1791
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001792 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Daniel Lezcano877aced2008-08-13 16:15:57 -07001794 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 if (!in_dev) {
1797 iml = NULL;
1798 err = -ENODEV;
1799 goto done;
1800 }
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001803 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001804 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001805 if (i->multi.imr_multiaddr.s_addr == addr &&
1806 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 count++;
1809 }
1810 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001811 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001813 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001814 if (iml == NULL)
1815 goto done;
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001818 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 iml->sflist = NULL;
1820 iml->sfmode = MCAST_EXCLUDE;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001821 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001825 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return err;
1827}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001828EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001830static void ip_sf_socklist_reclaim(struct rcu_head *rp)
1831{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001832 kfree(container_of(rp, struct ip_sf_socklist, rcu));
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001833 /* sk_omem_alloc should have been decreased by the caller*/
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1837 struct in_device *in_dev)
1838{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001839 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 int err;
1841
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001842 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /* any-source empty exclude case */
1844 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1845 iml->sfmode, 0, NULL, 0);
1846 }
1847 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001848 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
1849 rcu_assign_pointer(iml->sflist, NULL);
1850 /* decrease mem now to avoid the memleak warning */
1851 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
1852 call_rcu(&psf->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return err;
1854}
1855
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001856
1857static void ip_mc_socklist_reclaim(struct rcu_head *rp)
1858{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001859 kfree(container_of(rp, struct ip_mc_socklist, rcu));
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001860 /* sk_omem_alloc should have been decreased by the caller*/
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001861}
1862
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864/*
1865 * Ask a socket to leave a group.
1866 */
1867
1868int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1869{
1870 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001871 struct ip_mc_socklist *iml;
1872 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001873 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001874 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001875 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001876 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001877 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001880 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001881 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001882 for (imlp = &inet->mc_list;
1883 (iml = rtnl_dereference(*imlp)) != NULL;
1884 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001885 if (iml->multi.imr_multiaddr.s_addr != group)
1886 continue;
1887 if (ifindex) {
1888 if (iml->multi.imr_ifindex != ifindex)
1889 continue;
1890 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1891 iml->multi.imr_address.s_addr)
1892 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
David L Stevensacd6e002006-08-17 16:27:39 -07001894 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001896 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001897
1898 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001899 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001900 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001901 /* decrease mem now to avoid the memleak warning */
1902 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1903 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
David L Stevensacd6e002006-08-17 16:27:39 -07001904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
David L Stevensacd6e002006-08-17 16:27:39 -07001906 if (!in_dev)
1907 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001909 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
1912int ip_mc_source(int add, int omode, struct sock *sk, struct
1913 ip_mreq_source *mreqs, int ifindex)
1914{
1915 int err;
1916 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001917 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct ip_mc_socklist *pmc;
1919 struct in_device *in_dev = NULL;
1920 struct inet_sock *inet = inet_sk(sk);
1921 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001922 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001923 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 int i, j, rv;
1925
Joe Perchesf97c1e02007-12-16 13:45:43 -08001926 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return -EINVAL;
1928
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001929 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1932 imr.imr_address.s_addr = mreqs->imr_interface;
1933 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001934 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 if (!in_dev) {
1937 err = -ENODEV;
1938 goto done;
1939 }
1940 err = -EADDRNOTAVAIL;
1941
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001942 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08001943 if ((pmc->multi.imr_multiaddr.s_addr ==
1944 imr.imr_multiaddr.s_addr) &&
1945 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 break;
1947 }
David L Stevens917f2f12005-07-08 17:45:16 -07001948 if (!pmc) { /* must have a prior join */
1949 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /* if a source filter was set, must be the same mode as before */
1953 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07001954 if (pmc->sfmode != omode) {
1955 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07001957 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 } else if (pmc->sfmode != omode) {
1959 /* allow mode switches for empty-set filters */
1960 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001961 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 NULL, 0);
1963 pmc->sfmode = omode;
1964 }
1965
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001966 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!add) {
1968 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07001969 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 rv = !0;
1971 for (i=0; i<psl->sl_count; i++) {
1972 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07001973 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (rv == 0)
1975 break;
1976 }
1977 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07001978 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
David L Stevens8cdaaa12005-07-08 17:39:23 -07001980 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
1981 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
1982 leavegroup = 1;
1983 goto done;
1984 }
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001987 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 &mreqs->imr_sourceaddr, 1);
1989
1990 for (j=i+1; j<psl->sl_count; j++)
1991 psl->sl_addr[j-1] = psl->sl_addr[j];
1992 psl->sl_count--;
1993 err = 0;
1994 goto done;
1995 }
1996 /* else, add a new source to the filter */
1997
1998 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
1999 err = -ENOBUFS;
2000 goto done;
2001 }
2002 if (!psl || psl->sl_count == psl->sl_max) {
2003 struct ip_sf_socklist *newpsl;
2004 int count = IP_SFBLOCK;
2005
2006 if (psl)
2007 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002008 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 if (!newpsl) {
2010 err = -ENOBUFS;
2011 goto done;
2012 }
2013 newpsl->sl_max = count;
2014 newpsl->sl_count = count - IP_SFBLOCK;
2015 if (psl) {
2016 for (i=0; i<psl->sl_count; i++)
2017 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002018 /* decrease mem now to avoid the memleak warning */
2019 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2020 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002022 rcu_assign_pointer(pmc->sflist, newpsl);
2023 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2026 for (i=0; i<psl->sl_count; i++) {
2027 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002028 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 if (rv == 0)
2030 break;
2031 }
2032 if (rv == 0) /* address already there is an error */
2033 goto done;
2034 for (j=psl->sl_count-1; j>=i; j--)
2035 psl->sl_addr[j+1] = psl->sl_addr[j];
2036 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2037 psl->sl_count++;
2038 err = 0;
2039 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002040 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 &mreqs->imr_sourceaddr, 1);
2042done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002043 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002044 if (leavegroup)
2045 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 return err;
2047}
2048
2049int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2050{
David L Stevens9951f032005-07-08 17:47:28 -07002051 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002053 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 struct ip_mc_socklist *pmc;
2055 struct in_device *in_dev;
2056 struct inet_sock *inet = inet_sk(sk);
2057 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002058 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002059 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Joe Perchesf97c1e02007-12-16 13:45:43 -08002061 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 return -EINVAL;
2063 if (msf->imsf_fmode != MCAST_INCLUDE &&
2064 msf->imsf_fmode != MCAST_EXCLUDE)
2065 return -EINVAL;
2066
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002067 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2070 imr.imr_address.s_addr = msf->imsf_interface;
2071 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002072 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
2074 if (!in_dev) {
2075 err = -ENODEV;
2076 goto done;
2077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
David L Stevens9951f032005-07-08 17:47:28 -07002079 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2080 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2081 leavegroup = 1;
2082 goto done;
2083 }
2084
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002085 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2087 pmc->multi.imr_ifindex == imr.imr_ifindex)
2088 break;
2089 }
David L Stevens917f2f12005-07-08 17:45:16 -07002090 if (!pmc) { /* must have a prior join */
2091 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002095 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2096 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (!newpsl) {
2098 err = -ENOBUFS;
2099 goto done;
2100 }
2101 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2102 memcpy(newpsl->sl_addr, msf->imsf_slist,
2103 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2104 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2105 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2106 if (err) {
2107 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2108 goto done;
2109 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002110 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002112 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2113 msf->imsf_fmode, 0, NULL, 0);
2114 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002115 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (psl) {
2117 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2118 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002119 /* decrease mem now to avoid the memleak warning */
2120 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
2121 call_rcu(&psl->rcu, ip_sf_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 } else
2123 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2124 0, NULL, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002125 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002127 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002129 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002130 if (leavegroup)
2131 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return err;
2133}
2134
2135int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2136 struct ip_msfilter __user *optval, int __user *optlen)
2137{
2138 int err, len, count, copycount;
2139 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002140 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 struct ip_mc_socklist *pmc;
2142 struct in_device *in_dev;
2143 struct inet_sock *inet = inet_sk(sk);
2144 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002145 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Joe Perchesf97c1e02007-12-16 13:45:43 -08002147 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return -EINVAL;
2149
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002150 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2153 imr.imr_address.s_addr = msf->imsf_interface;
2154 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002155 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 if (!in_dev) {
2158 err = -ENODEV;
2159 goto done;
2160 }
2161 err = -EADDRNOTAVAIL;
2162
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002163 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2165 pmc->multi.imr_ifindex == imr.imr_ifindex)
2166 break;
2167 }
2168 if (!pmc) /* must have a prior join */
2169 goto done;
2170 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002171 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002172 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (!psl) {
2174 len = 0;
2175 count = 0;
2176 } else {
2177 count = psl->sl_count;
2178 }
2179 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2180 len = copycount * sizeof(psl->sl_addr[0]);
2181 msf->imsf_numsrc = count;
2182 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2183 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2184 return -EFAULT;
2185 }
2186 if (len &&
2187 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2188 return -EFAULT;
2189 return 0;
2190done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002191 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return err;
2193}
2194
2195int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2196 struct group_filter __user *optval, int __user *optlen)
2197{
2198 int err, i, count, copycount;
2199 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002200 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 struct ip_mc_socklist *pmc;
2202 struct inet_sock *inet = inet_sk(sk);
2203 struct ip_sf_socklist *psl;
2204
2205 psin = (struct sockaddr_in *)&gsf->gf_group;
2206 if (psin->sin_family != AF_INET)
2207 return -EINVAL;
2208 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002209 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return -EINVAL;
2211
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002212 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 err = -EADDRNOTAVAIL;
2215
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002216 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2218 pmc->multi.imr_ifindex == gsf->gf_interface)
2219 break;
2220 }
2221 if (!pmc) /* must have a prior join */
2222 goto done;
2223 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002224 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002225 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 count = psl ? psl->sl_count : 0;
2227 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2228 gsf->gf_numsrc = count;
2229 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2230 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2231 return -EFAULT;
2232 }
2233 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct sockaddr_storage ss;
2235
2236 psin = (struct sockaddr_in *)&ss;
2237 memset(&ss, 0, sizeof(ss));
2238 psin->sin_family = AF_INET;
2239 psin->sin_addr.s_addr = psl->sl_addr[i];
2240 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2241 return -EFAULT;
2242 }
2243 return 0;
2244done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002245 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return err;
2247}
2248
2249/*
2250 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2251 */
Al Viroc0cda062006-09-27 18:31:10 -07002252int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 struct inet_sock *inet = inet_sk(sk);
2255 struct ip_mc_socklist *pmc;
2256 struct ip_sf_socklist *psl;
2257 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002258 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002260 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002261 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002264 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002265 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2267 pmc->multi.imr_ifindex == dif)
2268 break;
2269 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002270 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002272 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002273 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002274 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002276 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 for (i=0; i<psl->sl_count; i++) {
2279 if (psl->sl_addr[i] == rmt_addr)
2280 break;
2281 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002282 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002284 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002286 goto unlock;
2287 ret = 1;
2288unlock:
2289 rcu_read_unlock();
2290out:
2291 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292}
2293
2294/*
2295 * A socket is closing.
2296 */
2297
2298void ip_mc_drop_socket(struct sock *sk)
2299{
2300 struct inet_sock *inet = inet_sk(sk);
2301 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002302 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 if (inet->mc_list == NULL)
2305 return;
2306
2307 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002308 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002311 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002312 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002313 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002314 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002316 /* decrease mem now to avoid the memleak warning */
2317 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
2318 call_rcu(&iml->rcu, ip_mc_socklist_reclaim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 }
2320 rtnl_unlock();
2321}
2322
Al Viroa60c4922006-09-26 21:28:34 -07002323int ip_check_mc(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 struct ip_mc_list *im;
2326 struct ip_sf_list *psf;
2327 int rv = 0;
2328
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002329 rcu_read_lock();
2330 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (im->multiaddr == mc_addr)
2332 break;
2333 }
2334 if (im && proto == IPPROTO_IGMP) {
2335 rv = 1;
2336 } else if (im) {
2337 if (src_addr) {
2338 for (psf=im->sources; psf; psf=psf->sf_next) {
2339 if (psf->sf_inaddr == src_addr)
2340 break;
2341 }
2342 if (psf)
2343 rv = psf->sf_count[MCAST_INCLUDE] ||
2344 psf->sf_count[MCAST_EXCLUDE] !=
2345 im->sfcount[MCAST_EXCLUDE];
2346 else
2347 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2348 } else
2349 rv = 1; /* unspecified source; tentatively allow */
2350 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002351 rcu_read_unlock();
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