blob: f72011df9c5927ca0308bfa5c8c00d690558bdea [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/types.h>
77#include <linux/kernel.h>
78#include <linux/jiffies.h>
79#include <linux/string.h>
80#include <linux/socket.h>
81#include <linux/sockios.h>
82#include <linux/in.h>
83#include <linux/inet.h>
84#include <linux/netdevice.h>
85#include <linux/skbuff.h>
86#include <linux/inetdevice.h>
87#include <linux/igmp.h>
88#include <linux/if_arp.h>
89#include <linux/rtnetlink.h>
90#include <linux/times.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020091
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020092#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020093#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#include <net/ip.h>
95#include <net/protocol.h>
96#include <net/route.h>
97#include <net/sock.h>
98#include <net/checksum.h>
99#include <linux/netfilter_ipv4.h>
100#ifdef CONFIG_IP_MROUTE
101#include <linux/mroute.h>
102#endif
103#ifdef CONFIG_PROC_FS
104#include <linux/proc_fs.h>
105#include <linux/seq_file.h>
106#endif
107
108#define IP_MAX_MEMBERSHIPS 20
109#define IP_MAX_MSF 10
110
111#ifdef CONFIG_IP_MULTICAST
112/* Parameter names and values are taken from igmp-v2-06 draft */
113
114#define IGMP_V1_Router_Present_Timeout (400*HZ)
115#define IGMP_V2_Router_Present_Timeout (400*HZ)
116#define IGMP_Unsolicited_Report_Interval (10*HZ)
117#define IGMP_Query_Response_Interval (10*HZ)
118#define IGMP_Unsolicited_Report_Count 2
119
120
121#define IGMP_Initial_Report_Delay (1)
122
123/* IGMP_Initial_Report_Delay is not from IGMP specs!
124 * IGMP specs require to report membership immediately after
125 * joining a group, but we delay the first report by a
126 * small interval. It seems more natural and still does not
127 * contradict to specs provided this delay is small enough.
128 */
129
Herbert Xu42f811b2007-06-04 23:34:44 -0700130#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900131 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700132 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
133 ((in_dev)->mr_v1_seen && \
134 time_before(jiffies, (in_dev)->mr_v1_seen)))
135#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900136 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700137 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
138 ((in_dev)->mr_v2_seen && \
139 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700142static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143static void igmpv3_clear_delrec(struct in_device *in_dev);
144static int sf_setstate(struct ip_mc_list *pmc);
145static void sf_markstate(struct ip_mc_list *pmc);
146#endif
147static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700148static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
149 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151static void ip_ma_put(struct ip_mc_list *im)
152{
153 if (atomic_dec_and_test(&im->refcnt)) {
154 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800155 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157}
158
David S. Millerd9aa9382010-11-15 08:52:02 -0800159#define for_each_pmc_rcu(in_dev, pmc) \
160 for (pmc = rcu_dereference(in_dev->mc_list); \
161 pmc != NULL; \
162 pmc = rcu_dereference(pmc->next_rcu))
163
164#define for_each_pmc_rtnl(in_dev, pmc) \
165 for (pmc = rtnl_dereference(in_dev->mc_list); \
166 pmc != NULL; \
167 pmc = rtnl_dereference(pmc->next_rcu))
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#ifdef CONFIG_IP_MULTICAST
170
171/*
172 * Timer management
173 */
174
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000175static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 spin_lock_bh(&im->lock);
178 if (del_timer(&im->timer))
179 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800180 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 im->reporter = 0;
182 im->unsolicit_count = 0;
183 spin_unlock_bh(&im->lock);
184}
185
186/* It must be called with locked im->lock */
187static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
188{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800189 int tv = net_random() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800191 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (!mod_timer(&im->timer, jiffies+tv+2))
193 atomic_inc(&im->refcnt);
194}
195
196static void igmp_gq_start_timer(struct in_device *in_dev)
197{
198 int tv = net_random() % in_dev->mr_maxdelay;
199
200 in_dev->mr_gq_running = 1;
201 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
202 in_dev_hold(in_dev);
203}
204
205static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
206{
207 int tv = net_random() % delay;
208
209 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
210 in_dev_hold(in_dev);
211}
212
213static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
214{
215 spin_lock_bh(&im->lock);
216 im->unsolicit_count = 0;
217 if (del_timer(&im->timer)) {
218 if ((long)(im->timer.expires-jiffies) < max_delay) {
219 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800220 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 spin_unlock_bh(&im->lock);
222 return;
223 }
224 atomic_dec(&im->refcnt);
225 }
226 igmp_start_timer(im, max_delay);
227 spin_unlock_bh(&im->lock);
228}
229
230
231/*
232 * Send an IGMP report.
233 */
234
235#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
236
237
238static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
239 int gdeleted, int sdeleted)
240{
241 switch (type) {
242 case IGMPV3_MODE_IS_INCLUDE:
243 case IGMPV3_MODE_IS_EXCLUDE:
244 if (gdeleted || sdeleted)
245 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800246 if (!(pmc->gsquery && !psf->sf_gsresp)) {
247 if (pmc->sfmode == MCAST_INCLUDE)
248 return 1;
249 /* don't include if this source is excluded
250 * in all filters
251 */
252 if (psf->sf_count[MCAST_INCLUDE])
253 return type == IGMPV3_MODE_IS_INCLUDE;
254 return pmc->sfcount[MCAST_EXCLUDE] ==
255 psf->sf_count[MCAST_EXCLUDE];
256 }
257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 case IGMPV3_CHANGE_TO_INCLUDE:
259 if (gdeleted || sdeleted)
260 return 0;
261 return psf->sf_count[MCAST_INCLUDE] != 0;
262 case IGMPV3_CHANGE_TO_EXCLUDE:
263 if (gdeleted || sdeleted)
264 return 0;
265 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
266 psf->sf_count[MCAST_INCLUDE])
267 return 0;
268 return pmc->sfcount[MCAST_EXCLUDE] ==
269 psf->sf_count[MCAST_EXCLUDE];
270 case IGMPV3_ALLOW_NEW_SOURCES:
271 if (gdeleted || !psf->sf_crcount)
272 return 0;
273 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
274 case IGMPV3_BLOCK_OLD_SOURCES:
275 if (pmc->sfmode == MCAST_INCLUDE)
276 return gdeleted || (psf->sf_crcount && sdeleted);
277 return psf->sf_crcount && !gdeleted && !sdeleted;
278 }
279 return 0;
280}
281
282static int
283igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
284{
285 struct ip_sf_list *psf;
286 int scount = 0;
287
288 for (psf=pmc->sources; psf; psf=psf->sf_next) {
289 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
290 continue;
291 scount++;
292 }
293 return scount;
294}
295
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000296#define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
299{
300 struct sk_buff *skb;
301 struct rtable *rt;
302 struct iphdr *pip;
303 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700304 struct net *net = dev_net(dev);
David S. Miller31e4543d2011-05-03 20:25:42 -0700305 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000306 int hlen = LL_RESERVED_SPACE(dev);
307 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000309 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000310 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000311 GFP_ATOMIC | __GFP_NOWARN);
312 if (skb)
313 break;
314 size >>= 1;
315 if (size < 256)
316 return NULL;
317 }
318 igmp_skb_size(skb) = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
David S. Miller31e4543d2011-05-03 20:25:42 -0700320 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500321 0, 0,
322 IPPROTO_IGMP, 0, dev->ifindex);
323 if (IS_ERR(rt)) {
324 kfree_skb(skb);
325 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Changli Gaod8d1f302010-06-10 23:31:35 -0700328 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 skb->dev = dev;
330
Herbert Xu66088242011-11-18 02:20:04 +0000331 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300333 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700334 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300335 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 pip->version = 4;
338 pip->ihl = (sizeof(struct iphdr)+4)>>2;
339 pip->tos = 0xc0;
340 pip->frag_off = htons(IP_DF);
341 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700342 pip->daddr = fl4.daddr;
343 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 pip->protocol = IPPROTO_IGMP;
345 pip->tot_len = 0; /* filled in later */
Changli Gaod8d1f302010-06-10 23:31:35 -0700346 ip_select_ident(pip, &rt->dst, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000347 ((u8 *)&pip[1])[0] = IPOPT_RA;
348 ((u8 *)&pip[1])[1] = 4;
349 ((u8 *)&pip[1])[2] = 0;
350 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700352 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300353 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300354 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
356 pig->resv1 = 0;
357 pig->csum = 0;
358 pig->resv2 = 0;
359 pig->ngrec = 0;
360 return skb;
361}
362
363static int igmpv3_sendpack(struct sk_buff *skb)
364{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300365 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000366 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300368 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Herbert Xuc439cb22008-01-11 19:14:00 -0800370 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
374{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800375 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
378static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
379 int type, struct igmpv3_grec **ppgr)
380{
381 struct net_device *dev = pmc->interface->dev;
382 struct igmpv3_report *pih;
383 struct igmpv3_grec *pgr;
384
385 if (!skb)
386 skb = igmpv3_newpack(dev, dev->mtu);
387 if (!skb)
388 return NULL;
389 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
390 pgr->grec_type = type;
391 pgr->grec_auxwords = 0;
392 pgr->grec_nsrcs = 0;
393 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300394 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 pih->ngrec = htons(ntohs(pih->ngrec)+1);
396 *ppgr = pgr;
397 return skb;
398}
399
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000400#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 skb_tailroom(skb)) : 0)
402
403static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
404 int type, int gdeleted, int sdeleted)
405{
406 struct net_device *dev = pmc->interface->dev;
407 struct igmpv3_report *pih;
408 struct igmpv3_grec *pgr = NULL;
409 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800410 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 if (pmc->multiaddr == IGMP_ALL_HOSTS)
413 return skb;
414
415 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
416 type == IGMPV3_MODE_IS_EXCLUDE;
417 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
418 type == IGMPV3_CHANGE_TO_EXCLUDE;
419
David L Stevensad125832006-01-18 14:20:56 -0800420 stotal = scount = 0;
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
423
David L Stevensad125832006-01-18 14:20:56 -0800424 if (!*psf_list)
425 goto empty_source;
426
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300427 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /* EX and TO_EX get a fresh packet, if needed */
430 if (truncate) {
431 if (pih && pih->ngrec &&
432 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
433 if (skb)
434 igmpv3_sendpack(skb);
435 skb = igmpv3_newpack(dev, dev->mtu);
436 }
437 }
438 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 psf_prev = NULL;
440 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700441 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 psf_next = psf->sf_next;
444
445 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
446 psf_prev = psf;
447 continue;
448 }
449
450 /* clear marks on query responses */
451 if (isquery)
452 psf->sf_gsresp = 0;
453
Al Viro63007722006-09-27 18:31:32 -0700454 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 first*sizeof(struct igmpv3_grec)) {
456 if (truncate && !first)
457 break; /* truncate these */
458 if (pgr)
459 pgr->grec_nsrcs = htons(scount);
460 if (skb)
461 igmpv3_sendpack(skb);
462 skb = igmpv3_newpack(dev, dev->mtu);
463 first = 1;
464 scount = 0;
465 }
466 if (first) {
467 skb = add_grhead(skb, pmc, type, &pgr);
468 first = 0;
469 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800470 if (!skb)
471 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700472 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800474 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
476 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
477 psf->sf_crcount--;
478 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
479 if (psf_prev)
480 psf_prev->sf_next = psf->sf_next;
481 else
482 *psf_list = psf->sf_next;
483 kfree(psf);
484 continue;
485 }
486 }
487 psf_prev = psf;
488 }
David L Stevensad125832006-01-18 14:20:56 -0800489
490empty_source:
491 if (!stotal) {
492 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
493 type == IGMPV3_BLOCK_OLD_SOURCES)
494 return skb;
495 if (pmc->crcount || isquery) {
496 /* make sure we have room for group header */
497 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
498 igmpv3_sendpack(skb);
499 skb = NULL; /* add_grhead will get a new one */
500 }
501 skb = add_grhead(skb, pmc, type, &pgr);
502 }
503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (pgr)
505 pgr->grec_nsrcs = htons(scount);
506
507 if (isquery)
508 pmc->gsquery = 0; /* clear query state on report */
509 return skb;
510}
511
512static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
513{
514 struct sk_buff *skb = NULL;
515 int type;
516
517 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000518 rcu_read_lock();
519 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (pmc->multiaddr == IGMP_ALL_HOSTS)
521 continue;
522 spin_lock_bh(&pmc->lock);
523 if (pmc->sfcount[MCAST_EXCLUDE])
524 type = IGMPV3_MODE_IS_EXCLUDE;
525 else
526 type = IGMPV3_MODE_IS_INCLUDE;
527 skb = add_grec(skb, pmc, type, 0, 0);
528 spin_unlock_bh(&pmc->lock);
529 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000530 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 } else {
532 spin_lock_bh(&pmc->lock);
533 if (pmc->sfcount[MCAST_EXCLUDE])
534 type = IGMPV3_MODE_IS_EXCLUDE;
535 else
536 type = IGMPV3_MODE_IS_INCLUDE;
537 skb = add_grec(skb, pmc, type, 0, 0);
538 spin_unlock_bh(&pmc->lock);
539 }
540 if (!skb)
541 return 0;
542 return igmpv3_sendpack(skb);
543}
544
545/*
546 * remove zero-count source records from a source filter list
547 */
548static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
549{
550 struct ip_sf_list *psf_prev, *psf_next, *psf;
551
552 psf_prev = NULL;
553 for (psf=*ppsf; psf; psf = psf_next) {
554 psf_next = psf->sf_next;
555 if (psf->sf_crcount == 0) {
556 if (psf_prev)
557 psf_prev->sf_next = psf->sf_next;
558 else
559 *ppsf = psf->sf_next;
560 kfree(psf);
561 } else
562 psf_prev = psf;
563 }
564}
565
566static void igmpv3_send_cr(struct in_device *in_dev)
567{
568 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
569 struct sk_buff *skb = NULL;
570 int type, dtype;
571
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000572 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 spin_lock_bh(&in_dev->mc_tomb_lock);
574
575 /* deleted MCA's */
576 pmc_prev = NULL;
577 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
578 pmc_next = pmc->next;
579 if (pmc->sfmode == MCAST_INCLUDE) {
580 type = IGMPV3_BLOCK_OLD_SOURCES;
581 dtype = IGMPV3_BLOCK_OLD_SOURCES;
582 skb = add_grec(skb, pmc, type, 1, 0);
583 skb = add_grec(skb, pmc, dtype, 1, 1);
584 }
585 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 if (pmc->sfmode == MCAST_EXCLUDE) {
587 type = IGMPV3_CHANGE_TO_INCLUDE;
588 skb = add_grec(skb, pmc, type, 1, 0);
589 }
David L Stevensad125832006-01-18 14:20:56 -0800590 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (pmc->crcount == 0) {
592 igmpv3_clear_zeros(&pmc->tomb);
593 igmpv3_clear_zeros(&pmc->sources);
594 }
595 }
596 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
597 if (pmc_prev)
598 pmc_prev->next = pmc_next;
599 else
600 in_dev->mc_tomb = pmc_next;
601 in_dev_put(pmc->interface);
602 kfree(pmc);
603 } else
604 pmc_prev = pmc;
605 }
606 spin_unlock_bh(&in_dev->mc_tomb_lock);
607
608 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000609 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 spin_lock_bh(&pmc->lock);
611 if (pmc->sfcount[MCAST_EXCLUDE]) {
612 type = IGMPV3_BLOCK_OLD_SOURCES;
613 dtype = IGMPV3_ALLOW_NEW_SOURCES;
614 } else {
615 type = IGMPV3_ALLOW_NEW_SOURCES;
616 dtype = IGMPV3_BLOCK_OLD_SOURCES;
617 }
618 skb = add_grec(skb, pmc, type, 0, 0);
619 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
620
621 /* filter mode changes */
622 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 if (pmc->sfmode == MCAST_EXCLUDE)
624 type = IGMPV3_CHANGE_TO_EXCLUDE;
625 else
626 type = IGMPV3_CHANGE_TO_INCLUDE;
627 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800628 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 spin_unlock_bh(&pmc->lock);
631 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000632 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (!skb)
635 return;
636 (void) igmpv3_sendpack(skb);
637}
638
639static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
640 int type)
641{
642 struct sk_buff *skb;
643 struct iphdr *iph;
644 struct igmphdr *ih;
645 struct rtable *rt;
646 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700647 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700648 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e4543d2011-05-03 20:25:42 -0700649 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700650 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000651 int hlen, tlen;
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
David S. Miller31e4543d2011-05-03 20:25:42 -0700660 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500661 0, 0,
662 IPPROTO_IGMP, 0, dev->ifindex);
663 if (IS_ERR(rt))
664 return -1;
665
Herbert Xu66088242011-11-18 02:20:04 +0000666 hlen = LL_RESERVED_SPACE(dev);
667 tlen = dev->needed_tailroom;
668 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (skb == NULL) {
670 ip_rt_put(rt);
671 return -1;
672 }
673
Changli Gaod8d1f302010-06-10 23:31:35 -0700674 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Herbert Xu66088242011-11-18 02:20:04 +0000676 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300678 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700679 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300680 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 iph->version = 4;
683 iph->ihl = (sizeof(struct iphdr)+4)>>2;
684 iph->tos = 0xc0;
685 iph->frag_off = htons(IP_DF);
686 iph->ttl = 1;
687 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700688 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 iph->protocol = IPPROTO_IGMP;
Changli Gaod8d1f302010-06-10 23:31:35 -0700690 ip_select_ident(iph, &rt->dst, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000691 ((u8 *)&iph[1])[0] = IPOPT_RA;
692 ((u8 *)&iph[1])[1] = 4;
693 ((u8 *)&iph[1])[2] = 0;
694 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800697 ih->type = type;
698 ih->code = 0;
699 ih->csum = 0;
700 ih->group = group;
701 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Herbert Xuc439cb22008-01-11 19:14:00 -0800703 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706static void igmp_gq_timer_expire(unsigned long data)
707{
708 struct in_device *in_dev = (struct in_device *)data;
709
710 in_dev->mr_gq_running = 0;
711 igmpv3_send_report(in_dev, NULL);
712 __in_dev_put(in_dev);
713}
714
715static void igmp_ifc_timer_expire(unsigned long data)
716{
717 struct in_device *in_dev = (struct in_device *)data;
718
719 igmpv3_send_cr(in_dev);
720 if (in_dev->mr_ifc_count) {
721 in_dev->mr_ifc_count--;
722 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
723 }
724 __in_dev_put(in_dev);
725}
726
727static void igmp_ifc_event(struct in_device *in_dev)
728{
729 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
730 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900731 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 IGMP_Unsolicited_Report_Count;
733 igmp_ifc_start_timer(in_dev, 1);
734}
735
736
737static void igmp_timer_expire(unsigned long data)
738{
739 struct ip_mc_list *im=(struct ip_mc_list *)data;
740 struct in_device *in_dev = im->interface;
741
742 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800743 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 if (im->unsolicit_count) {
746 im->unsolicit_count--;
747 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
748 }
749 im->reporter = 1;
750 spin_unlock(&im->lock);
751
752 if (IGMP_V1_SEEN(in_dev))
753 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
754 else if (IGMP_V2_SEEN(in_dev))
755 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
756 else
757 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
758
759 ip_ma_put(im);
760}
761
David L Stevensad125832006-01-18 14:20:56 -0800762/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700763static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
765 struct ip_sf_list *psf;
766 int i, scount;
767
768 scount = 0;
769 for (psf=pmc->sources; psf; psf=psf->sf_next) {
770 if (scount == nsrcs)
771 break;
David L Stevensad125832006-01-18 14:20:56 -0800772 for (i=0; i<nsrcs; i++) {
773 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000774 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800775 pmc->sfcount[MCAST_EXCLUDE] !=
776 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800777 break;
David L Stevensad125832006-01-18 14:20:56 -0800778 if (srcs[i] == psf->sf_inaddr) {
779 scount++;
780 break;
781 }
782 }
783 }
784 pmc->gsquery = 0;
785 if (scount == nsrcs) /* all sources excluded */
786 return 0;
787 return 1;
788}
789
Al Viroea4d9e72006-09-27 18:30:52 -0700790static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800791{
792 struct ip_sf_list *psf;
793 int i, scount;
794
795 if (pmc->sfmode == MCAST_EXCLUDE)
796 return igmp_xmarksources(pmc, nsrcs, srcs);
797
798 /* mark INCLUDE-mode sources */
799 scount = 0;
800 for (psf=pmc->sources; psf; psf=psf->sf_next) {
801 if (scount == nsrcs)
802 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 for (i=0; i<nsrcs; i++)
804 if (srcs[i] == psf->sf_inaddr) {
805 psf->sf_gsresp = 1;
806 scount++;
807 break;
808 }
809 }
David L Stevensad125832006-01-18 14:20:56 -0800810 if (!scount) {
811 pmc->gsquery = 0;
812 return 0;
813 }
814 pmc->gsquery = 1;
815 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Eric Dumazetd679c532012-09-06 20:37:06 +0000818/* return true if packet was dropped */
819static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
821 struct ip_mc_list *im;
822
823 /* Timers are only set for non-local groups */
824
825 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000826 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000828 rcu_read_lock();
829 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (im->multiaddr == group) {
831 igmp_stop_timer(im);
832 break;
833 }
834 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000835 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000836 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Eric Dumazetd679c532012-09-06 20:37:06 +0000839/* return true if packet was dropped */
840static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 int len)
842{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300843 struct igmphdr *ih = igmp_hdr(skb);
844 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700846 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 int max_delay;
848 int mark = 0;
849
850
David Stevens5b7c84062010-09-30 14:29:40 +0000851 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (ih->code == 0) {
853 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 max_delay = IGMP_Query_Response_Interval;
856 in_dev->mr_v1_seen = jiffies +
857 IGMP_V1_Router_Present_Timeout;
858 group = 0;
859 } else {
860 /* v2 router present */
861 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
862 in_dev->mr_v2_seen = jiffies +
863 IGMP_V2_Router_Present_Timeout;
864 }
865 /* cancel the interface change timer */
866 in_dev->mr_ifc_count = 0;
867 if (del_timer(&in_dev->mr_ifc_timer))
868 __in_dev_put(in_dev);
869 /* clear deleted report items */
870 igmpv3_clear_delrec(in_dev);
871 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000872 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c84062010-09-30 14:29:40 +0000873 } else if (IGMP_V1_SEEN(in_dev)) {
874 /* This is a v3 query with v1 queriers present */
875 max_delay = IGMP_Query_Response_Interval;
876 group = 0;
877 } else if (IGMP_V2_SEEN(in_dev)) {
878 /* this is a v3 query with v2 queriers present;
879 * Interpretation of the max_delay code is problematic here.
880 * A real v2 host would use ih_code directly, while v3 has a
881 * different encoding. We use the v3 encoding as more likely
882 * to be intended in a v3 query.
883 */
884 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800885 if (!max_delay)
886 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 } else { /* v3 */
888 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000889 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900890
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300891 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900893 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700894 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000895 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300896 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898
899 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
900 if (!max_delay)
901 max_delay = 1; /* can't mod w/ 0 */
902 in_dev->mr_maxdelay = max_delay;
903 if (ih3->qrv)
904 in_dev->mr_qrv = ih3->qrv;
905 if (!group) { /* general query */
906 if (ih3->nsrcs)
Eric Dumazetd679c532012-09-06 20:37:06 +0000907 return false; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000909 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
911 /* mark sources to include, if group & source-specific */
912 mark = ih3->nsrcs != 0;
913 }
914
915 /*
916 * - Start the timers in all of our membership records
917 * that the query applies to for the interface on
918 * which the query arrived excl. those that belong
919 * to a "local" group (224.0.0.X)
920 * - For timers already running check if they need to
921 * be reset.
922 * - Use the igmp->igmp_code field as the maximum
923 * delay possible
924 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000925 rcu_read_lock();
926 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800927 int changed;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (group && group != im->multiaddr)
930 continue;
931 if (im->multiaddr == IGMP_ALL_HOSTS)
932 continue;
933 spin_lock_bh(&im->lock);
934 if (im->tm_running)
935 im->gsquery = im->gsquery && mark;
936 else
937 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800938 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900939 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800941 if (changed)
942 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000944 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000945 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000948/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949int igmp_rcv(struct sk_buff *skb)
950{
951 /* This basically follows the spec line by line -- see RFC1112 */
952 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000953 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +0000955 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800957 if (in_dev == NULL)
958 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Herbert Xufb286bb2005-11-10 13:01:24 -0800960 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000961 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800962
963 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700964 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800965 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800966 break;
967 /* fall through */
968 case CHECKSUM_NONE:
969 skb->csum = 0;
970 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000971 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300974 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 switch (ih->type) {
976 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +0000977 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 break;
979 case IGMP_HOST_MEMBERSHIP_REPORT:
980 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -0800982 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 break;
David Stevens24c69272005-12-02 20:32:59 -0800984 /* don't rely on MC router hearing unicast reports */
985 if (skb->pkt_type == PACKET_MULTICAST ||
986 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +0000987 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 break;
989 case IGMP_PIM:
990#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 return pim_rcv_v1(skb);
992#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +0000993 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 case IGMP_DVMRP:
995 case IGMP_TRACE:
996 case IGMP_HOST_LEAVE_MESSAGE:
997 case IGMP_MTRACE:
998 case IGMP_MTRACE_RESP:
999 break;
1000 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001003
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001004drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001005 if (dropped)
1006 kfree_skb(skb);
1007 else
1008 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010}
1011
1012#endif
1013
1014
1015/*
1016 * Add a filter to a device
1017 */
1018
Al Viro63007722006-09-27 18:31:32 -07001019static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
1021 char buf[MAX_ADDR_LEN];
1022 struct net_device *dev = in_dev->dev;
1023
1024 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1025 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001026 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 routine. Something sort of:
1028 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1029 --ANK
1030 */
1031 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001032 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/*
1036 * Remove a filter from a device
1037 */
1038
Al Viro63007722006-09-27 18:31:32 -07001039static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 char buf[MAX_ADDR_LEN];
1042 struct net_device *dev = in_dev->dev;
1043
1044 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001045 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
1048#ifdef CONFIG_IP_MULTICAST
1049/*
1050 * deleted ip_mc_list manipulation
1051 */
1052static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1053{
1054 struct ip_mc_list *pmc;
1055
1056 /* this is an "ip_mc_list" for convenience; only the fields below
1057 * are actually used. In particular, the refcnt and users are not
1058 * used for management of the delete list. Using the same structure
1059 * for deleted items allows change reports to use common code with
1060 * non-deleted or query-response MCA's.
1061 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001062 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (!pmc)
1064 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 spin_lock_bh(&im->lock);
1066 pmc->interface = im->interface;
1067 in_dev_hold(in_dev);
1068 pmc->multiaddr = im->multiaddr;
1069 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1070 IGMP_Unsolicited_Report_Count;
1071 pmc->sfmode = im->sfmode;
1072 if (pmc->sfmode == MCAST_INCLUDE) {
1073 struct ip_sf_list *psf;
1074
1075 pmc->tomb = im->tomb;
1076 pmc->sources = im->sources;
1077 im->tomb = im->sources = NULL;
1078 for (psf=pmc->sources; psf; psf=psf->sf_next)
1079 psf->sf_crcount = pmc->crcount;
1080 }
1081 spin_unlock_bh(&im->lock);
1082
1083 spin_lock_bh(&in_dev->mc_tomb_lock);
1084 pmc->next = in_dev->mc_tomb;
1085 in_dev->mc_tomb = pmc;
1086 spin_unlock_bh(&in_dev->mc_tomb_lock);
1087}
1088
Al Viro63007722006-09-27 18:31:32 -07001089static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct ip_mc_list *pmc, *pmc_prev;
1092 struct ip_sf_list *psf, *psf_next;
1093
1094 spin_lock_bh(&in_dev->mc_tomb_lock);
1095 pmc_prev = NULL;
1096 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1097 if (pmc->multiaddr == multiaddr)
1098 break;
1099 pmc_prev = pmc;
1100 }
1101 if (pmc) {
1102 if (pmc_prev)
1103 pmc_prev->next = pmc->next;
1104 else
1105 in_dev->mc_tomb = pmc->next;
1106 }
1107 spin_unlock_bh(&in_dev->mc_tomb_lock);
1108 if (pmc) {
1109 for (psf=pmc->tomb; psf; psf=psf_next) {
1110 psf_next = psf->sf_next;
1111 kfree(psf);
1112 }
1113 in_dev_put(pmc->interface);
1114 kfree(pmc);
1115 }
1116}
1117
1118static void igmpv3_clear_delrec(struct in_device *in_dev)
1119{
1120 struct ip_mc_list *pmc, *nextpmc;
1121
1122 spin_lock_bh(&in_dev->mc_tomb_lock);
1123 pmc = in_dev->mc_tomb;
1124 in_dev->mc_tomb = NULL;
1125 spin_unlock_bh(&in_dev->mc_tomb_lock);
1126
1127 for (; pmc; pmc = nextpmc) {
1128 nextpmc = pmc->next;
1129 ip_mc_clear_src(pmc);
1130 in_dev_put(pmc->interface);
1131 kfree(pmc);
1132 }
1133 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001134 rcu_read_lock();
1135 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 struct ip_sf_list *psf, *psf_next;
1137
1138 spin_lock_bh(&pmc->lock);
1139 psf = pmc->tomb;
1140 pmc->tomb = NULL;
1141 spin_unlock_bh(&pmc->lock);
1142 for (; psf; psf=psf_next) {
1143 psf_next = psf->sf_next;
1144 kfree(psf);
1145 }
1146 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001147 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148}
1149#endif
1150
1151static void igmp_group_dropped(struct ip_mc_list *im)
1152{
1153 struct in_device *in_dev = im->interface;
1154#ifdef CONFIG_IP_MULTICAST
1155 int reporter;
1156#endif
1157
1158 if (im->loaded) {
1159 im->loaded = 0;
1160 ip_mc_filter_del(in_dev, im->multiaddr);
1161 }
1162
1163#ifdef CONFIG_IP_MULTICAST
1164 if (im->multiaddr == IGMP_ALL_HOSTS)
1165 return;
1166
1167 reporter = im->reporter;
1168 igmp_stop_timer(im);
1169
1170 if (!in_dev->dead) {
1171 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001172 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (IGMP_V2_SEEN(in_dev)) {
1174 if (reporter)
1175 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001176 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178 /* IGMPv3 */
1179 igmpv3_add_delrec(in_dev, im);
1180
1181 igmp_ifc_event(in_dev);
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
1186static void igmp_group_added(struct ip_mc_list *im)
1187{
1188 struct in_device *in_dev = im->interface;
1189
1190 if (im->loaded == 0) {
1191 im->loaded = 1;
1192 ip_mc_filter_add(in_dev, im->multiaddr);
1193 }
1194
1195#ifdef CONFIG_IP_MULTICAST
1196 if (im->multiaddr == IGMP_ALL_HOSTS)
1197 return;
1198
1199 if (in_dev->dead)
1200 return;
1201 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1202 spin_lock_bh(&im->lock);
1203 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1204 spin_unlock_bh(&im->lock);
1205 return;
1206 }
1207 /* else, v3 */
1208
1209 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1210 IGMP_Unsolicited_Report_Count;
1211 igmp_ifc_event(in_dev);
1212#endif
1213}
1214
1215
1216/*
1217 * Multicast list managers
1218 */
1219
Eric Dumazete9897072013-06-07 08:48:57 -07001220static u32 ip_mc_hash(const struct ip_mc_list *im)
1221{
1222 return hash_32((u32)im->multiaddr, MC_HASH_SZ_LOG);
1223}
1224
1225static void ip_mc_hash_add(struct in_device *in_dev,
1226 struct ip_mc_list *im)
1227{
1228 struct ip_mc_list __rcu **mc_hash;
1229 u32 hash;
1230
1231 mc_hash = rtnl_dereference(in_dev->mc_hash);
1232 if (mc_hash) {
1233 hash = ip_mc_hash(im);
1234 im->next_hash = rtnl_dereference(mc_hash[hash]);
1235 rcu_assign_pointer(mc_hash[hash], im);
1236 return;
1237 }
1238
1239 /* do not use a hash table for small number of items */
1240 if (in_dev->mc_count < 4)
1241 return;
1242
1243 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1244 GFP_KERNEL);
1245 if (!mc_hash)
1246 return;
1247
1248 for_each_pmc_rtnl(in_dev, im) {
1249 hash = ip_mc_hash(im);
1250 im->next_hash = rtnl_dereference(mc_hash[hash]);
1251 RCU_INIT_POINTER(mc_hash[hash], im);
1252 }
1253
1254 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1255}
1256
1257static void ip_mc_hash_remove(struct in_device *in_dev,
1258 struct ip_mc_list *im)
1259{
1260 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1261 struct ip_mc_list *aux;
1262
1263 if (!mc_hash)
1264 return;
1265 mc_hash += ip_mc_hash(im);
1266 while ((aux = rtnl_dereference(*mc_hash)) != im)
1267 mc_hash = &aux->next_hash;
1268 *mc_hash = im->next_hash;
1269}
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272/*
1273 * A socket has joined a multicast group on device dev.
1274 */
1275
Al Viro8f935bb2006-09-27 18:30:07 -07001276void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct ip_mc_list *im;
1279
1280 ASSERT_RTNL();
1281
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001282 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (im->multiaddr == addr) {
1284 im->users++;
1285 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1286 goto out;
1287 }
1288 }
1289
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001290 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (!im)
1292 goto out;
1293
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001294 im->users = 1;
1295 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001297 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* initial mode is (EX, empty) */
1299 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 atomic_set(&im->refcnt, 1);
1302 spin_lock_init(&im->lock);
1303#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001304 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001307
1308 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001309 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001310 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001311
Eric Dumazete9897072013-06-07 08:48:57 -07001312 ip_mc_hash_add(in_dev, im);
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314#ifdef CONFIG_IP_MULTICAST
1315 igmpv3_del_delrec(in_dev, im->multiaddr);
1316#endif
1317 igmp_group_added(im);
1318 if (!in_dev->dead)
1319 ip_rt_multicast_event(in_dev);
1320out:
1321 return;
1322}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001323EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325/*
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001326 * Resend IGMP JOIN report; used for bonding.
Eric Dumazet866f3b22010-11-18 09:33:19 -08001327 * Called with rcu_read_lock()
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001328 */
Eric Dumazet866f3b22010-11-18 09:33:19 -08001329void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001330{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001331#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001332 struct ip_mc_list *im;
1333 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001334
Eric Dumazet866f3b22010-11-18 09:33:19 -08001335 for_each_pmc_rcu(in_dev, im) {
1336 if (im->multiaddr == IGMP_ALL_HOSTS)
1337 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001338
Eric Dumazet866f3b22010-11-18 09:33:19 -08001339 /* a failover is happening and switches
1340 * must be notified immediately
1341 */
1342 if (IGMP_V1_SEEN(in_dev))
1343 type = IGMP_HOST_MEMBERSHIP_REPORT;
1344 else if (IGMP_V2_SEEN(in_dev))
1345 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1346 else
1347 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1348 igmp_send_report(in_dev, im, type);
1349 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001350#endif
1351}
Eric Dumazet866f3b22010-11-18 09:33:19 -08001352EXPORT_SYMBOL(ip_mc_rejoin_groups);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001353
1354/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 * A socket has left a multicast group on device dev
1356 */
1357
Al Viro8f935bb2006-09-27 18:30:07 -07001358void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001360 struct ip_mc_list *i;
1361 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001362
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001364
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001365 for (ip = &in_dev->mc_list;
1366 (i = rtnl_dereference(*ip)) != NULL;
1367 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001368 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001370 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001371 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001372 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001374 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (!in_dev->dead)
1377 ip_rt_multicast_event(in_dev);
1378
1379 ip_ma_put(i);
1380 return;
1381 }
1382 break;
1383 }
1384 }
1385}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001386EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Moni Shoua75c78502009-09-15 02:37:40 -07001388/* Device changing type */
1389
1390void ip_mc_unmap(struct in_device *in_dev)
1391{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001392 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001393
1394 ASSERT_RTNL();
1395
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001396 for_each_pmc_rtnl(in_dev, pmc)
1397 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001398}
1399
1400void ip_mc_remap(struct in_device *in_dev)
1401{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001402 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001403
1404 ASSERT_RTNL();
1405
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001406 for_each_pmc_rtnl(in_dev, pmc)
1407 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001408}
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410/* Device going down */
1411
1412void ip_mc_down(struct in_device *in_dev)
1413{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001414 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416 ASSERT_RTNL();
1417
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001418 for_each_pmc_rtnl(in_dev, pmc)
1419 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421#ifdef CONFIG_IP_MULTICAST
1422 in_dev->mr_ifc_count = 0;
1423 if (del_timer(&in_dev->mr_ifc_timer))
1424 __in_dev_put(in_dev);
1425 in_dev->mr_gq_running = 0;
1426 if (del_timer(&in_dev->mr_gq_timer))
1427 __in_dev_put(in_dev);
1428 igmpv3_clear_delrec(in_dev);
1429#endif
1430
1431 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1432}
1433
1434void ip_mc_init_dev(struct in_device *in_dev)
1435{
1436 ASSERT_RTNL();
1437
1438 in_dev->mc_tomb = NULL;
1439#ifdef CONFIG_IP_MULTICAST
1440 in_dev->mr_gq_running = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001441 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1442 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 in_dev->mr_ifc_count = 0;
Rami Rosenb8bae412008-10-07 15:34:37 -07001444 in_dev->mc_count = 0;
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001445 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1446 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1448#endif
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 spin_lock_init(&in_dev->mc_tomb_lock);
1451}
1452
1453/* Device going up */
1454
1455void ip_mc_up(struct in_device *in_dev)
1456{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001457 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 ASSERT_RTNL();
1460
1461 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1462
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001463 for_each_pmc_rtnl(in_dev, pmc)
1464 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467/*
1468 * Device is about to be destroyed: clean up.
1469 */
1470
1471void ip_mc_destroy_dev(struct in_device *in_dev)
1472{
1473 struct ip_mc_list *i;
1474
1475 ASSERT_RTNL();
1476
1477 /* Deactivate timers */
1478 ip_mc_down(in_dev);
1479
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001480 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1481 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001482 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001483
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001484 /* We've dropped the groups in ip_mc_down already */
1485 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001490/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001491static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct net_device *dev = NULL;
1494 struct in_device *idev = NULL;
1495
1496 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001497 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 return idev;
1499 }
1500 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001501 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!dev)
1503 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
1505
David S. Millerb23dd4f2011-03-02 14:31:35 -08001506 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001507 struct rtable *rt = ip_route_output(net,
1508 imr->imr_multiaddr.s_addr,
1509 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001510 if (!IS_ERR(rt)) {
1511 dev = rt->dst.dev;
1512 ip_rt_put(rt);
1513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 }
1515 if (dev) {
1516 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001517 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
1519 return idev;
1520}
1521
1522/*
1523 * Join a socket to a group
1524 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001525int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1526int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528
1529static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001530 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct ip_sf_list *psf, *psf_prev;
1533 int rv = 0;
1534
1535 psf_prev = NULL;
1536 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1537 if (psf->sf_inaddr == *psfsrc)
1538 break;
1539 psf_prev = psf;
1540 }
1541 if (!psf || psf->sf_count[sfmode] == 0) {
1542 /* source filter not found, or count wrong => bug */
1543 return -ESRCH;
1544 }
1545 psf->sf_count[sfmode]--;
1546 if (psf->sf_count[sfmode] == 0) {
1547 ip_rt_multicast_event(pmc->interface);
1548 }
1549 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1550#ifdef CONFIG_IP_MULTICAST
1551 struct in_device *in_dev = pmc->interface;
1552#endif
1553
1554 /* no more filters for this source */
1555 if (psf_prev)
1556 psf_prev->sf_next = psf->sf_next;
1557 else
1558 pmc->sources = psf->sf_next;
1559#ifdef CONFIG_IP_MULTICAST
1560 if (psf->sf_oldin &&
1561 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001562 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 IGMP_Unsolicited_Report_Count;
1564 psf->sf_next = pmc->tomb;
1565 pmc->tomb = psf;
1566 rv = 1;
1567 } else
1568#endif
1569 kfree(psf);
1570 }
1571 return rv;
1572}
1573
1574#ifndef CONFIG_IP_MULTICAST
1575#define igmp_ifc_event(x) do { } while (0)
1576#endif
1577
Al Viro8f935bb2006-09-27 18:30:07 -07001578static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1579 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 struct ip_mc_list *pmc;
1582 int changerec = 0;
1583 int i, err;
1584
1585 if (!in_dev)
1586 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001587 rcu_read_lock();
1588 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if (*pmca == pmc->multiaddr)
1590 break;
1591 }
1592 if (!pmc) {
1593 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001594 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return -ESRCH;
1596 }
1597 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001598 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599#ifdef CONFIG_IP_MULTICAST
1600 sf_markstate(pmc);
1601#endif
1602 if (!delta) {
1603 err = -EINVAL;
1604 if (!pmc->sfcount[sfmode])
1605 goto out_unlock;
1606 pmc->sfcount[sfmode]--;
1607 }
1608 err = 0;
1609 for (i=0; i<sfcount; i++) {
1610 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1611
1612 changerec |= rv > 0;
1613 if (!err && rv < 0)
1614 err = rv;
1615 }
1616 if (pmc->sfmode == MCAST_EXCLUDE &&
1617 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1618 pmc->sfcount[MCAST_INCLUDE]) {
1619#ifdef CONFIG_IP_MULTICAST
1620 struct ip_sf_list *psf;
1621#endif
1622
1623 /* filter mode change */
1624 pmc->sfmode = MCAST_INCLUDE;
1625#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001626 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 IGMP_Unsolicited_Report_Count;
1628 in_dev->mr_ifc_count = pmc->crcount;
1629 for (psf=pmc->sources; psf; psf = psf->sf_next)
1630 psf->sf_crcount = 0;
1631 igmp_ifc_event(pmc->interface);
1632 } else if (sf_setstate(pmc) || changerec) {
1633 igmp_ifc_event(pmc->interface);
1634#endif
1635 }
1636out_unlock:
1637 spin_unlock_bh(&pmc->lock);
1638 return err;
1639}
1640
1641/*
1642 * Add multicast single-source filter to the interface list
1643 */
1644static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001645 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
1647 struct ip_sf_list *psf, *psf_prev;
1648
1649 psf_prev = NULL;
1650 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1651 if (psf->sf_inaddr == *psfsrc)
1652 break;
1653 psf_prev = psf;
1654 }
1655 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001656 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (!psf)
1658 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 psf->sf_inaddr = *psfsrc;
1660 if (psf_prev) {
1661 psf_prev->sf_next = psf;
1662 } else
1663 pmc->sources = psf;
1664 }
1665 psf->sf_count[sfmode]++;
1666 if (psf->sf_count[sfmode] == 1) {
1667 ip_rt_multicast_event(pmc->interface);
1668 }
1669 return 0;
1670}
1671
1672#ifdef CONFIG_IP_MULTICAST
1673static void sf_markstate(struct ip_mc_list *pmc)
1674{
1675 struct ip_sf_list *psf;
1676 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1677
1678 for (psf=pmc->sources; psf; psf=psf->sf_next)
1679 if (pmc->sfcount[MCAST_EXCLUDE]) {
1680 psf->sf_oldin = mca_xcount ==
1681 psf->sf_count[MCAST_EXCLUDE] &&
1682 !psf->sf_count[MCAST_INCLUDE];
1683 } else
1684 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1685}
1686
1687static int sf_setstate(struct ip_mc_list *pmc)
1688{
David L Stevensad125832006-01-18 14:20:56 -08001689 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1691 int qrv = pmc->interface->mr_qrv;
1692 int new_in, rv;
1693
1694 rv = 0;
1695 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1696 if (pmc->sfcount[MCAST_EXCLUDE]) {
1697 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1698 !psf->sf_count[MCAST_INCLUDE];
1699 } else
1700 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001701 if (new_in) {
1702 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001703 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001704
1705 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1706 if (dpsf->sf_inaddr == psf->sf_inaddr)
1707 break;
1708 prev = dpsf;
1709 }
1710 if (dpsf) {
1711 if (prev)
1712 prev->sf_next = dpsf->sf_next;
1713 else
1714 pmc->tomb = dpsf->sf_next;
1715 kfree(dpsf);
1716 }
1717 psf->sf_crcount = qrv;
1718 rv++;
1719 }
1720 } else if (psf->sf_oldin) {
1721
1722 psf->sf_crcount = 0;
1723 /*
1724 * add or update "delete" records if an active filter
1725 * is now inactive
1726 */
1727 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1728 if (dpsf->sf_inaddr == psf->sf_inaddr)
1729 break;
1730 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001731 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001732 if (!dpsf)
1733 continue;
1734 *dpsf = *psf;
1735 /* pmc->lock held by callers */
1736 dpsf->sf_next = pmc->tomb;
1737 pmc->tomb = dpsf;
1738 }
1739 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 rv++;
1741 }
1742 }
1743 return rv;
1744}
1745#endif
1746
1747/*
1748 * Add multicast source filter list to the interface list
1749 */
Al Viro8f935bb2006-09-27 18:30:07 -07001750static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1751 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752{
1753 struct ip_mc_list *pmc;
1754 int isexclude;
1755 int i, err;
1756
1757 if (!in_dev)
1758 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001759 rcu_read_lock();
1760 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (*pmca == pmc->multiaddr)
1762 break;
1763 }
1764 if (!pmc) {
1765 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001766 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return -ESRCH;
1768 }
1769 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001770 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772#ifdef CONFIG_IP_MULTICAST
1773 sf_markstate(pmc);
1774#endif
1775 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1776 if (!delta)
1777 pmc->sfcount[sfmode]++;
1778 err = 0;
1779 for (i=0; i<sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00001780 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 if (err)
1782 break;
1783 }
1784 if (err) {
1785 int j;
1786
Jun Zhao685f94e62011-11-22 17:19:03 +00001787 if (!delta)
1788 pmc->sfcount[sfmode]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 for (j=0; j<i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00001790 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1792#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001794 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795#endif
1796
1797 /* filter mode change */
1798 if (pmc->sfcount[MCAST_EXCLUDE])
1799 pmc->sfmode = MCAST_EXCLUDE;
1800 else if (pmc->sfcount[MCAST_INCLUDE])
1801 pmc->sfmode = MCAST_INCLUDE;
1802#ifdef CONFIG_IP_MULTICAST
1803 /* else no filters; keep old mode for reports */
1804
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001805 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 IGMP_Unsolicited_Report_Count;
1807 in_dev->mr_ifc_count = pmc->crcount;
1808 for (psf=pmc->sources; psf; psf = psf->sf_next)
1809 psf->sf_crcount = 0;
1810 igmp_ifc_event(in_dev);
1811 } else if (sf_setstate(pmc)) {
1812 igmp_ifc_event(in_dev);
1813#endif
1814 }
1815 spin_unlock_bh(&pmc->lock);
1816 return err;
1817}
1818
1819static void ip_mc_clear_src(struct ip_mc_list *pmc)
1820{
1821 struct ip_sf_list *psf, *nextpsf;
1822
1823 for (psf=pmc->tomb; psf; psf=nextpsf) {
1824 nextpsf = psf->sf_next;
1825 kfree(psf);
1826 }
1827 pmc->tomb = NULL;
1828 for (psf=pmc->sources; psf; psf=nextpsf) {
1829 nextpsf = psf->sf_next;
1830 kfree(psf);
1831 }
1832 pmc->sources = NULL;
1833 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001834 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 pmc->sfcount[MCAST_EXCLUDE] = 1;
1836}
1837
1838
1839/*
1840 * Join a multicast group
1841 */
1842int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1843{
1844 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001845 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001846 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 struct in_device *in_dev;
1848 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001849 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001850 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 int count = 0;
1852
Joe Perchesf97c1e02007-12-16 13:45:43 -08001853 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 return -EINVAL;
1855
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001856 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Daniel Lezcano877aced2008-08-13 16:15:57 -07001858 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860 if (!in_dev) {
1861 iml = NULL;
1862 err = -ENODEV;
1863 goto done;
1864 }
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001867 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001868 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001869 if (i->multi.imr_multiaddr.s_addr == addr &&
1870 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 count++;
1873 }
1874 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001875 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001877 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001878 if (iml == NULL)
1879 goto done;
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001882 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 iml->sflist = NULL;
1884 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001885 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001889 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return err;
1891}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001892EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1895 struct in_device *in_dev)
1896{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001897 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 int err;
1899
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001900 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* any-source empty exclude case */
1902 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1903 iml->sfmode, 0, NULL, 0);
1904 }
1905 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001906 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001907 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001908 /* decrease mem now to avoid the memleak warning */
1909 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08001910 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return err;
1912}
1913
1914/*
1915 * Ask a socket to leave a group.
1916 */
1917
1918int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1919{
1920 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001921 struct ip_mc_socklist *iml;
1922 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001923 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001924 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001925 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001926 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001927 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001930 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001931 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001932 for (imlp = &inet->mc_list;
1933 (iml = rtnl_dereference(*imlp)) != NULL;
1934 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001935 if (iml->multi.imr_multiaddr.s_addr != group)
1936 continue;
1937 if (ifindex) {
1938 if (iml->multi.imr_ifindex != ifindex)
1939 continue;
1940 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1941 iml->multi.imr_address.s_addr)
1942 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
David L Stevensacd6e002006-08-17 16:27:39 -07001944 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001946 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001947
1948 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001949 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001950 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001951 /* decrease mem now to avoid the memleak warning */
1952 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08001953 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07001954 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
David L Stevensacd6e002006-08-17 16:27:39 -07001956 if (!in_dev)
1957 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001959 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
stephen hemminger193ba922012-10-01 12:32:34 +00001961EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963int ip_mc_source(int add, int omode, struct sock *sk, struct
1964 ip_mreq_source *mreqs, int ifindex)
1965{
1966 int err;
1967 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001968 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 struct ip_mc_socklist *pmc;
1970 struct in_device *in_dev = NULL;
1971 struct inet_sock *inet = inet_sk(sk);
1972 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001973 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001974 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 int i, j, rv;
1976
Joe Perchesf97c1e02007-12-16 13:45:43 -08001977 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return -EINVAL;
1979
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001980 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1983 imr.imr_address.s_addr = mreqs->imr_interface;
1984 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001985 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 if (!in_dev) {
1988 err = -ENODEV;
1989 goto done;
1990 }
1991 err = -EADDRNOTAVAIL;
1992
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001993 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08001994 if ((pmc->multi.imr_multiaddr.s_addr ==
1995 imr.imr_multiaddr.s_addr) &&
1996 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998 }
David L Stevens917f2f12005-07-08 17:45:16 -07001999 if (!pmc) { /* must have a prior join */
2000 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 /* if a source filter was set, must be the same mode as before */
2004 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002005 if (pmc->sfmode != omode) {
2006 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 } else if (pmc->sfmode != omode) {
2010 /* allow mode switches for empty-set filters */
2011 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002012 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 NULL, 0);
2014 pmc->sfmode = omode;
2015 }
2016
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002017 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (!add) {
2019 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002020 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 rv = !0;
2022 for (i=0; i<psl->sl_count; i++) {
2023 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002024 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (rv == 0)
2026 break;
2027 }
2028 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002029 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
David L Stevens8cdaaa12005-07-08 17:39:23 -07002031 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2032 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2033 leavegroup = 1;
2034 goto done;
2035 }
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002038 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 &mreqs->imr_sourceaddr, 1);
2040
2041 for (j=i+1; j<psl->sl_count; j++)
2042 psl->sl_addr[j-1] = psl->sl_addr[j];
2043 psl->sl_count--;
2044 err = 0;
2045 goto done;
2046 }
2047 /* else, add a new source to the filter */
2048
2049 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2050 err = -ENOBUFS;
2051 goto done;
2052 }
2053 if (!psl || psl->sl_count == psl->sl_max) {
2054 struct ip_sf_socklist *newpsl;
2055 int count = IP_SFBLOCK;
2056
2057 if (psl)
2058 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002059 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (!newpsl) {
2061 err = -ENOBUFS;
2062 goto done;
2063 }
2064 newpsl->sl_max = count;
2065 newpsl->sl_count = count - IP_SFBLOCK;
2066 if (psl) {
2067 for (i=0; i<psl->sl_count; i++)
2068 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002069 /* decrease mem now to avoid the memleak warning */
2070 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002071 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002073 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002074 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
2076 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2077 for (i=0; i<psl->sl_count; i++) {
2078 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002079 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (rv == 0)
2081 break;
2082 }
2083 if (rv == 0) /* address already there is an error */
2084 goto done;
2085 for (j=psl->sl_count-1; j>=i; j--)
2086 psl->sl_addr[j+1] = psl->sl_addr[j];
2087 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2088 psl->sl_count++;
2089 err = 0;
2090 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002091 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 &mreqs->imr_sourceaddr, 1);
2093done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002094 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002095 if (leavegroup)
2096 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return err;
2098}
2099
2100int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2101{
David L Stevens9951f032005-07-08 17:47:28 -07002102 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002104 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 struct ip_mc_socklist *pmc;
2106 struct in_device *in_dev;
2107 struct inet_sock *inet = inet_sk(sk);
2108 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002109 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002110 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Joe Perchesf97c1e02007-12-16 13:45:43 -08002112 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return -EINVAL;
2114 if (msf->imsf_fmode != MCAST_INCLUDE &&
2115 msf->imsf_fmode != MCAST_EXCLUDE)
2116 return -EINVAL;
2117
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002118 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2121 imr.imr_address.s_addr = msf->imsf_interface;
2122 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002123 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 if (!in_dev) {
2126 err = -ENODEV;
2127 goto done;
2128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
David L Stevens9951f032005-07-08 17:47:28 -07002130 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2131 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2132 leavegroup = 1;
2133 goto done;
2134 }
2135
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002136 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2138 pmc->multi.imr_ifindex == imr.imr_ifindex)
2139 break;
2140 }
David L Stevens917f2f12005-07-08 17:45:16 -07002141 if (!pmc) { /* must have a prior join */
2142 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002146 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2147 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (!newpsl) {
2149 err = -ENOBUFS;
2150 goto done;
2151 }
2152 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2153 memcpy(newpsl->sl_addr, msf->imsf_slist,
2154 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2155 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2156 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2157 if (err) {
2158 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2159 goto done;
2160 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002161 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002163 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2164 msf->imsf_fmode, 0, NULL, 0);
2165 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002166 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (psl) {
2168 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2169 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002170 /* decrease mem now to avoid the memleak warning */
2171 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002172 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 } else
2174 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2175 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002176 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002178 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002180 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002181 if (leavegroup)
2182 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return err;
2184}
2185
2186int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2187 struct ip_msfilter __user *optval, int __user *optlen)
2188{
2189 int err, len, count, copycount;
2190 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002191 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 struct ip_mc_socklist *pmc;
2193 struct in_device *in_dev;
2194 struct inet_sock *inet = inet_sk(sk);
2195 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002196 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Joe Perchesf97c1e02007-12-16 13:45:43 -08002198 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return -EINVAL;
2200
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002201 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2204 imr.imr_address.s_addr = msf->imsf_interface;
2205 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002206 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 if (!in_dev) {
2209 err = -ENODEV;
2210 goto done;
2211 }
2212 err = -EADDRNOTAVAIL;
2213
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002214 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2216 pmc->multi.imr_ifindex == imr.imr_ifindex)
2217 break;
2218 }
2219 if (!pmc) /* must have a prior join */
2220 goto done;
2221 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002222 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002223 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 if (!psl) {
2225 len = 0;
2226 count = 0;
2227 } else {
2228 count = psl->sl_count;
2229 }
2230 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2231 len = copycount * sizeof(psl->sl_addr[0]);
2232 msf->imsf_numsrc = count;
2233 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2234 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2235 return -EFAULT;
2236 }
2237 if (len &&
2238 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2239 return -EFAULT;
2240 return 0;
2241done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002242 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 return err;
2244}
2245
2246int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2247 struct group_filter __user *optval, int __user *optlen)
2248{
2249 int err, i, count, copycount;
2250 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002251 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct ip_mc_socklist *pmc;
2253 struct inet_sock *inet = inet_sk(sk);
2254 struct ip_sf_socklist *psl;
2255
2256 psin = (struct sockaddr_in *)&gsf->gf_group;
2257 if (psin->sin_family != AF_INET)
2258 return -EINVAL;
2259 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002260 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return -EINVAL;
2262
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002263 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 err = -EADDRNOTAVAIL;
2266
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002267 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2269 pmc->multi.imr_ifindex == gsf->gf_interface)
2270 break;
2271 }
2272 if (!pmc) /* must have a prior join */
2273 goto done;
2274 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002275 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002276 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 count = psl ? psl->sl_count : 0;
2278 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2279 gsf->gf_numsrc = count;
2280 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2281 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2282 return -EFAULT;
2283 }
2284 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 struct sockaddr_storage ss;
2286
2287 psin = (struct sockaddr_in *)&ss;
2288 memset(&ss, 0, sizeof(ss));
2289 psin->sin_family = AF_INET;
2290 psin->sin_addr.s_addr = psl->sl_addr[i];
2291 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2292 return -EFAULT;
2293 }
2294 return 0;
2295done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002296 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return err;
2298}
2299
2300/*
2301 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2302 */
Al Viroc0cda062006-09-27 18:31:10 -07002303int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
2305 struct inet_sock *inet = inet_sk(sk);
2306 struct ip_mc_socklist *pmc;
2307 struct ip_sf_socklist *psl;
2308 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002309 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002311 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002312 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002313 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002315 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002316 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2318 pmc->multi.imr_ifindex == dif)
2319 break;
2320 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002321 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002323 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002324 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002325 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002327 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 for (i=0; i<psl->sl_count; i++) {
2330 if (psl->sl_addr[i] == rmt_addr)
2331 break;
2332 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002333 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002335 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002337 goto unlock;
2338 ret = 1;
2339unlock:
2340 rcu_read_unlock();
2341out:
2342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
2345/*
2346 * A socket is closing.
2347 */
2348
2349void ip_mc_drop_socket(struct sock *sk)
2350{
2351 struct inet_sock *inet = inet_sk(sk);
2352 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002353 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 if (inet->mc_list == NULL)
2356 return;
2357
2358 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002359 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002362 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002363 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002364 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002365 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002367 /* decrease mem now to avoid the memleak warning */
2368 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002369 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371 rtnl_unlock();
2372}
2373
David S. Millerdbdd9a52011-03-10 16:34:38 -08002374/* called with rcu_read_lock() */
2375int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002378 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct ip_sf_list *psf;
2380 int rv = 0;
2381
Eric Dumazete9897072013-06-07 08:48:57 -07002382 mc_hash = rcu_dereference(in_dev->mc_hash);
2383 if (mc_hash) {
2384 u32 hash = hash_32((u32)mc_addr, MC_HASH_SZ_LOG);
2385
2386 for (im = rcu_dereference(mc_hash[hash]);
2387 im != NULL;
2388 im = rcu_dereference(im->next_hash)) {
2389 if (im->multiaddr == mc_addr)
2390 break;
2391 }
2392 } else {
2393 for_each_pmc_rcu(in_dev, im) {
2394 if (im->multiaddr == mc_addr)
2395 break;
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 if (im && proto == IPPROTO_IGMP) {
2399 rv = 1;
2400 } else if (im) {
2401 if (src_addr) {
2402 for (psf=im->sources; psf; psf=psf->sf_next) {
2403 if (psf->sf_inaddr == src_addr)
2404 break;
2405 }
2406 if (psf)
2407 rv = psf->sf_count[MCAST_INCLUDE] ||
2408 psf->sf_count[MCAST_EXCLUDE] !=
2409 im->sfcount[MCAST_EXCLUDE];
2410 else
2411 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2412 } else
2413 rv = 1; /* unspecified source; tentatively allow */
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return rv;
2416}
2417
2418#if defined(CONFIG_PROC_FS)
2419struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002420 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 struct net_device *dev;
2422 struct in_device *in_dev;
2423};
2424
2425#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2426
2427static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2428{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002429 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 struct ip_mc_list *im = NULL;
2431 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2432
Pavel Emelianov7562f872007-05-03 15:13:45 -07002433 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002434 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002436
2437 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (!in_dev)
2439 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002440 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (im) {
2442 state->in_dev = in_dev;
2443 break;
2444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446 return im;
2447}
2448
2449static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2450{
2451 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002452
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002453 im = rcu_dereference(im->next_rcu);
2454 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002455 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 if (!state->dev) {
2457 state->in_dev = NULL;
2458 break;
2459 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002460 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (!state->in_dev)
2462 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002463 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
2465 return im;
2466}
2467
2468static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2469{
2470 struct ip_mc_list *im = igmp_mc_get_first(seq);
2471 if (im)
2472 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2473 --pos;
2474 return pos ? NULL : im;
2475}
2476
2477static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002478 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
stephen hemminger61fbab72009-11-10 07:54:55 +00002480 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2482}
2483
2484static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2485{
2486 struct ip_mc_list *im;
2487 if (v == SEQ_START_TOKEN)
2488 im = igmp_mc_get_first(seq);
2489 else
2490 im = igmp_mc_get_next(seq, v);
2491 ++*pos;
2492 return im;
2493}
2494
2495static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002496 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002499
2500 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002502 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
2505static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2506{
2507 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002508 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2510 else {
2511 struct ip_mc_list *im = (struct ip_mc_list *)v;
2512 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2513 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002514 long delta;
2515
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516#ifdef CONFIG_IP_MULTICAST
2517 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2518 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2519 "V3";
2520#else
2521 querier = "NONE";
2522#endif
2523
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002524 if (rcu_dereference(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002526 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
2528
Eric Dumazeta399a802012-08-08 21:13:53 +00002529 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002531 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002533 im->tm_running,
2534 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 im->reporter);
2536 }
2537 return 0;
2538}
2539
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002540static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 .start = igmp_mc_seq_start,
2542 .next = igmp_mc_seq_next,
2543 .stop = igmp_mc_seq_stop,
2544 .show = igmp_mc_seq_show,
2545};
2546
2547static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2548{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002549 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002550 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Arjan van de Ven9a321442007-02-12 00:55:35 -08002553static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 .owner = THIS_MODULE,
2555 .open = igmp_mc_seq_open,
2556 .read = seq_read,
2557 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002558 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559};
2560
2561struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002562 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 struct net_device *dev;
2564 struct in_device *idev;
2565 struct ip_mc_list *im;
2566};
2567
2568#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2569
2570static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2571{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002572 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 struct ip_sf_list *psf = NULL;
2574 struct ip_mc_list *im = NULL;
2575 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2576
Pavel Emelianov7562f872007-05-03 15:13:45 -07002577 state->idev = NULL;
2578 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002579 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002581 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (unlikely(idev == NULL))
2583 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002584 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (likely(im != NULL)) {
2586 spin_lock_bh(&im->lock);
2587 psf = im->sources;
2588 if (likely(psf != NULL)) {
2589 state->im = im;
2590 state->idev = idev;
2591 break;
2592 }
2593 spin_unlock_bh(&im->lock);
2594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 return psf;
2597}
2598
2599static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2600{
2601 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2602
2603 psf = psf->sf_next;
2604 while (!psf) {
2605 spin_unlock_bh(&state->im->lock);
2606 state->im = state->im->next;
2607 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002608 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (!state->dev) {
2610 state->idev = NULL;
2611 goto out;
2612 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002613 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (!state->idev)
2615 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002616 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618 if (!state->im)
2619 break;
2620 spin_lock_bh(&state->im->lock);
2621 psf = state->im->sources;
2622 }
2623out:
2624 return psf;
2625}
2626
2627static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2628{
2629 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2630 if (psf)
2631 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2632 --pos;
2633 return pos ? NULL : psf;
2634}
2635
2636static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002637 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
stephen hemminger61fbab72009-11-10 07:54:55 +00002639 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2641}
2642
2643static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2644{
2645 struct ip_sf_list *psf;
2646 if (v == SEQ_START_TOKEN)
2647 psf = igmp_mcf_get_first(seq);
2648 else
2649 psf = igmp_mcf_get_next(seq, v);
2650 ++*pos;
2651 return psf;
2652}
2653
2654static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002655 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2658 if (likely(state->im != NULL)) {
2659 spin_unlock_bh(&state->im->lock);
2660 state->im = NULL;
2661 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002662 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002664 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
2667static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2668{
2669 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2670 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2671
2672 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002673 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 "%3s %6s "
2675 "%10s %10s %6s %6s\n", "Idx",
2676 "Device", "MCA",
2677 "SRC", "INC", "EXC");
2678 } else {
2679 seq_printf(seq,
2680 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002681 "0x%08x %6lu %6lu\n",
2682 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 ntohl(state->im->multiaddr),
2684 ntohl(psf->sf_inaddr),
2685 psf->sf_count[MCAST_INCLUDE],
2686 psf->sf_count[MCAST_EXCLUDE]);
2687 }
2688 return 0;
2689}
2690
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002691static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 .start = igmp_mcf_seq_start,
2693 .next = igmp_mcf_seq_next,
2694 .stop = igmp_mcf_seq_stop,
2695 .show = igmp_mcf_seq_show,
2696};
2697
2698static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2699{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002700 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002701 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
Arjan van de Ven9a321442007-02-12 00:55:35 -08002704static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 .owner = THIS_MODULE,
2706 .open = igmp_mcf_seq_open,
2707 .read = seq_read,
2708 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002709 .release = seq_release_net,
2710};
2711
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002712static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002713{
2714 struct proc_dir_entry *pde;
2715
Gao fengd4beaa62013-02-18 01:34:54 +00002716 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002717 if (!pde)
2718 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002719 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2720 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002721 if (!pde)
2722 goto out_mcfilter;
2723 return 0;
2724
2725out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002726 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002727out_igmp:
2728 return -ENOMEM;
2729}
2730
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002731static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002732{
Gao fengece31ff2013-02-18 01:34:56 +00002733 remove_proc_entry("mcfilter", net->proc_net);
2734 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002735}
2736
2737static struct pernet_operations igmp_net_ops = {
2738 .init = igmp_net_init,
2739 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740};
2741
2742int __init igmp_mc_proc_init(void)
2743{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002744 return register_pernet_subsys(&igmp_net_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746#endif