blob: ef7618630f3614d00fb05b8a94f14891caa57932 [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>
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +020091#include <linux/pkt_sched.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020092
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020093#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020094#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <net/ip.h>
96#include <net/protocol.h>
97#include <net/route.h>
98#include <net/sock.h>
99#include <net/checksum.h>
100#include <linux/netfilter_ipv4.h>
101#ifdef CONFIG_IP_MROUTE
102#include <linux/mroute.h>
103#endif
104#ifdef CONFIG_PROC_FS
105#include <linux/proc_fs.h>
106#include <linux/seq_file.h>
107#endif
108
109#define IP_MAX_MEMBERSHIPS 20
110#define IP_MAX_MSF 10
111
112#ifdef CONFIG_IP_MULTICAST
113/* Parameter names and values are taken from igmp-v2-06 draft */
114
115#define IGMP_V1_Router_Present_Timeout (400*HZ)
116#define IGMP_V2_Router_Present_Timeout (400*HZ)
117#define IGMP_Unsolicited_Report_Interval (10*HZ)
118#define IGMP_Query_Response_Interval (10*HZ)
119#define IGMP_Unsolicited_Report_Count 2
120
121
122#define IGMP_Initial_Report_Delay (1)
123
124/* IGMP_Initial_Report_Delay is not from IGMP specs!
125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
129 */
130
Herbert Xu42f811b2007-06-04 23:34:44 -0700131#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700143static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static void igmpv3_clear_delrec(struct in_device *in_dev);
145static int sf_setstate(struct ip_mc_list *pmc);
146static void sf_markstate(struct ip_mc_list *pmc);
147#endif
148static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700149static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
150 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152static void ip_ma_put(struct ip_mc_list *im)
153{
154 if (atomic_dec_and_test(&im->refcnt)) {
155 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800156 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158}
159
David S. Millerd9aa9382010-11-15 08:52:02 -0800160#define for_each_pmc_rcu(in_dev, pmc) \
161 for (pmc = rcu_dereference(in_dev->mc_list); \
162 pmc != NULL; \
163 pmc = rcu_dereference(pmc->next_rcu))
164
165#define for_each_pmc_rtnl(in_dev, pmc) \
166 for (pmc = rtnl_dereference(in_dev->mc_list); \
167 pmc != NULL; \
168 pmc = rtnl_dereference(pmc->next_rcu))
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170#ifdef CONFIG_IP_MULTICAST
171
172/*
173 * Timer management
174 */
175
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000176static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 spin_lock_bh(&im->lock);
179 if (del_timer(&im->timer))
180 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800181 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 im->reporter = 0;
183 im->unsolicit_count = 0;
184 spin_unlock_bh(&im->lock);
185}
186
187/* It must be called with locked im->lock */
188static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
189{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800190 int tv = net_random() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800192 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!mod_timer(&im->timer, jiffies+tv+2))
194 atomic_inc(&im->refcnt);
195}
196
197static void igmp_gq_start_timer(struct in_device *in_dev)
198{
199 int tv = net_random() % in_dev->mr_maxdelay;
200
201 in_dev->mr_gq_running = 1;
202 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
203 in_dev_hold(in_dev);
204}
205
206static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
207{
208 int tv = net_random() % delay;
209
210 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
211 in_dev_hold(in_dev);
212}
213
214static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
215{
216 spin_lock_bh(&im->lock);
217 im->unsolicit_count = 0;
218 if (del_timer(&im->timer)) {
219 if ((long)(im->timer.expires-jiffies) < max_delay) {
220 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800221 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 spin_unlock_bh(&im->lock);
223 return;
224 }
225 atomic_dec(&im->refcnt);
226 }
227 igmp_start_timer(im, max_delay);
228 spin_unlock_bh(&im->lock);
229}
230
231
232/*
233 * Send an IGMP report.
234 */
235
236#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
237
238
239static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
240 int gdeleted, int sdeleted)
241{
242 switch (type) {
243 case IGMPV3_MODE_IS_INCLUDE:
244 case IGMPV3_MODE_IS_EXCLUDE:
245 if (gdeleted || sdeleted)
246 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800247 if (!(pmc->gsquery && !psf->sf_gsresp)) {
248 if (pmc->sfmode == MCAST_INCLUDE)
249 return 1;
250 /* don't include if this source is excluded
251 * in all filters
252 */
253 if (psf->sf_count[MCAST_INCLUDE])
254 return type == IGMPV3_MODE_IS_INCLUDE;
255 return pmc->sfcount[MCAST_EXCLUDE] ==
256 psf->sf_count[MCAST_EXCLUDE];
257 }
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 case IGMPV3_CHANGE_TO_INCLUDE:
260 if (gdeleted || sdeleted)
261 return 0;
262 return psf->sf_count[MCAST_INCLUDE] != 0;
263 case IGMPV3_CHANGE_TO_EXCLUDE:
264 if (gdeleted || sdeleted)
265 return 0;
266 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
267 psf->sf_count[MCAST_INCLUDE])
268 return 0;
269 return pmc->sfcount[MCAST_EXCLUDE] ==
270 psf->sf_count[MCAST_EXCLUDE];
271 case IGMPV3_ALLOW_NEW_SOURCES:
272 if (gdeleted || !psf->sf_crcount)
273 return 0;
274 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
275 case IGMPV3_BLOCK_OLD_SOURCES:
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return gdeleted || (psf->sf_crcount && sdeleted);
278 return psf->sf_crcount && !gdeleted && !sdeleted;
279 }
280 return 0;
281}
282
283static int
284igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
285{
286 struct ip_sf_list *psf;
287 int scount = 0;
288
289 for (psf=pmc->sources; psf; psf=psf->sf_next) {
290 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
291 continue;
292 scount++;
293 }
294 return scount;
295}
296
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000297#define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
300{
301 struct sk_buff *skb;
302 struct rtable *rt;
303 struct iphdr *pip;
304 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700305 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700306 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000307 int hlen = LL_RESERVED_SPACE(dev);
308 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000310 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000311 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000312 GFP_ATOMIC | __GFP_NOWARN);
313 if (skb)
314 break;
315 size >>= 1;
316 if (size < 256)
317 return NULL;
318 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200319 skb->priority = TC_PRIO_CONTROL;
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000320 igmp_skb_size(skb) = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
David S. Miller31e45432011-05-03 20:25:42 -0700322 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500323 0, 0,
324 IPPROTO_IGMP, 0, dev->ifindex);
325 if (IS_ERR(rt)) {
326 kfree_skb(skb);
327 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Changli Gaod8d1f302010-06-10 23:31:35 -0700330 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 skb->dev = dev;
332
Herbert Xu66088242011-11-18 02:20:04 +0000333 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300335 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700336 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300337 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 pip->version = 4;
340 pip->ihl = (sizeof(struct iphdr)+4)>>2;
341 pip->tos = 0xc0;
342 pip->frag_off = htons(IP_DF);
343 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700344 pip->daddr = fl4.daddr;
345 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 pip->protocol = IPPROTO_IGMP;
347 pip->tot_len = 0; /* filled in later */
Changli Gaod8d1f302010-06-10 23:31:35 -0700348 ip_select_ident(pip, &rt->dst, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000349 ((u8 *)&pip[1])[0] = IPOPT_RA;
350 ((u8 *)&pip[1])[1] = 4;
351 ((u8 *)&pip[1])[2] = 0;
352 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700354 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300355 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300356 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
358 pig->resv1 = 0;
359 pig->csum = 0;
360 pig->resv2 = 0;
361 pig->ngrec = 0;
362 return skb;
363}
364
365static int igmpv3_sendpack(struct sk_buff *skb)
366{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300367 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000368 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300370 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Herbert Xuc439cb22008-01-11 19:14:00 -0800372 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
376{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800377 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
381 int type, struct igmpv3_grec **ppgr)
382{
383 struct net_device *dev = pmc->interface->dev;
384 struct igmpv3_report *pih;
385 struct igmpv3_grec *pgr;
386
387 if (!skb)
388 skb = igmpv3_newpack(dev, dev->mtu);
389 if (!skb)
390 return NULL;
391 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
392 pgr->grec_type = type;
393 pgr->grec_auxwords = 0;
394 pgr->grec_nsrcs = 0;
395 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300396 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 pih->ngrec = htons(ntohs(pih->ngrec)+1);
398 *ppgr = pgr;
399 return skb;
400}
401
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000402#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 skb_tailroom(skb)) : 0)
404
405static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
406 int type, int gdeleted, int sdeleted)
407{
408 struct net_device *dev = pmc->interface->dev;
409 struct igmpv3_report *pih;
410 struct igmpv3_grec *pgr = NULL;
411 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800412 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (pmc->multiaddr == IGMP_ALL_HOSTS)
415 return skb;
416
417 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
418 type == IGMPV3_MODE_IS_EXCLUDE;
419 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
420 type == IGMPV3_CHANGE_TO_EXCLUDE;
421
David L Stevensad125832006-01-18 14:20:56 -0800422 stotal = scount = 0;
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
425
David L Stevensad125832006-01-18 14:20:56 -0800426 if (!*psf_list)
427 goto empty_source;
428
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300429 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* EX and TO_EX get a fresh packet, if needed */
432 if (truncate) {
433 if (pih && pih->ngrec &&
434 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
435 if (skb)
436 igmpv3_sendpack(skb);
437 skb = igmpv3_newpack(dev, dev->mtu);
438 }
439 }
440 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 psf_prev = NULL;
442 for (psf=*psf_list; psf; psf=psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700443 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 psf_next = psf->sf_next;
446
447 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
448 psf_prev = psf;
449 continue;
450 }
451
452 /* clear marks on query responses */
453 if (isquery)
454 psf->sf_gsresp = 0;
455
Al Viro63007722006-09-27 18:31:32 -0700456 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 first*sizeof(struct igmpv3_grec)) {
458 if (truncate && !first)
459 break; /* truncate these */
460 if (pgr)
461 pgr->grec_nsrcs = htons(scount);
462 if (skb)
463 igmpv3_sendpack(skb);
464 skb = igmpv3_newpack(dev, dev->mtu);
465 first = 1;
466 scount = 0;
467 }
468 if (first) {
469 skb = add_grhead(skb, pmc, type, &pgr);
470 first = 0;
471 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800472 if (!skb)
473 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700474 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800476 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
478 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
479 psf->sf_crcount--;
480 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
481 if (psf_prev)
482 psf_prev->sf_next = psf->sf_next;
483 else
484 *psf_list = psf->sf_next;
485 kfree(psf);
486 continue;
487 }
488 }
489 psf_prev = psf;
490 }
David L Stevensad125832006-01-18 14:20:56 -0800491
492empty_source:
493 if (!stotal) {
494 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
495 type == IGMPV3_BLOCK_OLD_SOURCES)
496 return skb;
497 if (pmc->crcount || isquery) {
498 /* make sure we have room for group header */
499 if (skb && AVAILABLE(skb)<sizeof(struct igmpv3_grec)) {
500 igmpv3_sendpack(skb);
501 skb = NULL; /* add_grhead will get a new one */
502 }
503 skb = add_grhead(skb, pmc, type, &pgr);
504 }
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (pgr)
507 pgr->grec_nsrcs = htons(scount);
508
509 if (isquery)
510 pmc->gsquery = 0; /* clear query state on report */
511 return skb;
512}
513
514static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
515{
516 struct sk_buff *skb = NULL;
517 int type;
518
519 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000520 rcu_read_lock();
521 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (pmc->multiaddr == IGMP_ALL_HOSTS)
523 continue;
524 spin_lock_bh(&pmc->lock);
525 if (pmc->sfcount[MCAST_EXCLUDE])
526 type = IGMPV3_MODE_IS_EXCLUDE;
527 else
528 type = IGMPV3_MODE_IS_INCLUDE;
529 skb = add_grec(skb, pmc, type, 0, 0);
530 spin_unlock_bh(&pmc->lock);
531 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000532 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
534 spin_lock_bh(&pmc->lock);
535 if (pmc->sfcount[MCAST_EXCLUDE])
536 type = IGMPV3_MODE_IS_EXCLUDE;
537 else
538 type = IGMPV3_MODE_IS_INCLUDE;
539 skb = add_grec(skb, pmc, type, 0, 0);
540 spin_unlock_bh(&pmc->lock);
541 }
542 if (!skb)
543 return 0;
544 return igmpv3_sendpack(skb);
545}
546
547/*
548 * remove zero-count source records from a source filter list
549 */
550static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
551{
552 struct ip_sf_list *psf_prev, *psf_next, *psf;
553
554 psf_prev = NULL;
555 for (psf=*ppsf; psf; psf = psf_next) {
556 psf_next = psf->sf_next;
557 if (psf->sf_crcount == 0) {
558 if (psf_prev)
559 psf_prev->sf_next = psf->sf_next;
560 else
561 *ppsf = psf->sf_next;
562 kfree(psf);
563 } else
564 psf_prev = psf;
565 }
566}
567
568static void igmpv3_send_cr(struct in_device *in_dev)
569{
570 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
571 struct sk_buff *skb = NULL;
572 int type, dtype;
573
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000574 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 spin_lock_bh(&in_dev->mc_tomb_lock);
576
577 /* deleted MCA's */
578 pmc_prev = NULL;
579 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
580 pmc_next = pmc->next;
581 if (pmc->sfmode == MCAST_INCLUDE) {
582 type = IGMPV3_BLOCK_OLD_SOURCES;
583 dtype = IGMPV3_BLOCK_OLD_SOURCES;
584 skb = add_grec(skb, pmc, type, 1, 0);
585 skb = add_grec(skb, pmc, dtype, 1, 1);
586 }
587 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (pmc->sfmode == MCAST_EXCLUDE) {
589 type = IGMPV3_CHANGE_TO_INCLUDE;
590 skb = add_grec(skb, pmc, type, 1, 0);
591 }
David L Stevensad125832006-01-18 14:20:56 -0800592 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (pmc->crcount == 0) {
594 igmpv3_clear_zeros(&pmc->tomb);
595 igmpv3_clear_zeros(&pmc->sources);
596 }
597 }
598 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
599 if (pmc_prev)
600 pmc_prev->next = pmc_next;
601 else
602 in_dev->mc_tomb = pmc_next;
603 in_dev_put(pmc->interface);
604 kfree(pmc);
605 } else
606 pmc_prev = pmc;
607 }
608 spin_unlock_bh(&in_dev->mc_tomb_lock);
609
610 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000611 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 spin_lock_bh(&pmc->lock);
613 if (pmc->sfcount[MCAST_EXCLUDE]) {
614 type = IGMPV3_BLOCK_OLD_SOURCES;
615 dtype = IGMPV3_ALLOW_NEW_SOURCES;
616 } else {
617 type = IGMPV3_ALLOW_NEW_SOURCES;
618 dtype = IGMPV3_BLOCK_OLD_SOURCES;
619 }
620 skb = add_grec(skb, pmc, type, 0, 0);
621 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
622
623 /* filter mode changes */
624 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (pmc->sfmode == MCAST_EXCLUDE)
626 type = IGMPV3_CHANGE_TO_EXCLUDE;
627 else
628 type = IGMPV3_CHANGE_TO_INCLUDE;
629 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800630 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
632 spin_unlock_bh(&pmc->lock);
633 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000634 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 if (!skb)
637 return;
638 (void) igmpv3_sendpack(skb);
639}
640
641static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
642 int type)
643{
644 struct sk_buff *skb;
645 struct iphdr *iph;
646 struct igmphdr *ih;
647 struct rtable *rt;
648 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700649 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700650 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700651 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700652 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000653 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
656 return igmpv3_send_report(in_dev, pmc);
657 else if (type == IGMP_HOST_LEAVE_MESSAGE)
658 dst = IGMP_ALL_ROUTER;
659 else
660 dst = group;
661
David S. Miller31e45432011-05-03 20:25:42 -0700662 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500663 0, 0,
664 IPPROTO_IGMP, 0, dev->ifindex);
665 if (IS_ERR(rt))
666 return -1;
667
Herbert Xu66088242011-11-18 02:20:04 +0000668 hlen = LL_RESERVED_SPACE(dev);
669 tlen = dev->needed_tailroom;
670 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (skb == NULL) {
672 ip_rt_put(rt);
673 return -1;
674 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200675 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Changli Gaod8d1f302010-06-10 23:31:35 -0700677 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Herbert Xu66088242011-11-18 02:20:04 +0000679 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300681 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700682 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300683 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 iph->version = 4;
686 iph->ihl = (sizeof(struct iphdr)+4)>>2;
687 iph->tos = 0xc0;
688 iph->frag_off = htons(IP_DF);
689 iph->ttl = 1;
690 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700691 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 iph->protocol = IPPROTO_IGMP;
Changli Gaod8d1f302010-06-10 23:31:35 -0700693 ip_select_ident(iph, &rt->dst, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000694 ((u8 *)&iph[1])[0] = IPOPT_RA;
695 ((u8 *)&iph[1])[1] = 4;
696 ((u8 *)&iph[1])[2] = 0;
697 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800700 ih->type = type;
701 ih->code = 0;
702 ih->csum = 0;
703 ih->group = group;
704 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Herbert Xuc439cb22008-01-11 19:14:00 -0800706 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
709static void igmp_gq_timer_expire(unsigned long data)
710{
711 struct in_device *in_dev = (struct in_device *)data;
712
713 in_dev->mr_gq_running = 0;
714 igmpv3_send_report(in_dev, NULL);
715 __in_dev_put(in_dev);
716}
717
718static void igmp_ifc_timer_expire(unsigned long data)
719{
720 struct in_device *in_dev = (struct in_device *)data;
721
722 igmpv3_send_cr(in_dev);
723 if (in_dev->mr_ifc_count) {
724 in_dev->mr_ifc_count--;
725 igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
726 }
727 __in_dev_put(in_dev);
728}
729
730static void igmp_ifc_event(struct in_device *in_dev)
731{
732 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
733 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900734 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 IGMP_Unsolicited_Report_Count;
736 igmp_ifc_start_timer(in_dev, 1);
737}
738
739
740static void igmp_timer_expire(unsigned long data)
741{
742 struct ip_mc_list *im=(struct ip_mc_list *)data;
743 struct in_device *in_dev = im->interface;
744
745 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800746 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 if (im->unsolicit_count) {
749 im->unsolicit_count--;
750 igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
751 }
752 im->reporter = 1;
753 spin_unlock(&im->lock);
754
755 if (IGMP_V1_SEEN(in_dev))
756 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
757 else if (IGMP_V2_SEEN(in_dev))
758 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
759 else
760 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
761
762 ip_ma_put(im);
763}
764
David L Stevensad125832006-01-18 14:20:56 -0800765/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700766static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct ip_sf_list *psf;
769 int i, scount;
770
771 scount = 0;
772 for (psf=pmc->sources; psf; psf=psf->sf_next) {
773 if (scount == nsrcs)
774 break;
David L Stevensad125832006-01-18 14:20:56 -0800775 for (i=0; i<nsrcs; i++) {
776 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000777 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800778 pmc->sfcount[MCAST_EXCLUDE] !=
779 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800780 break;
David L Stevensad125832006-01-18 14:20:56 -0800781 if (srcs[i] == psf->sf_inaddr) {
782 scount++;
783 break;
784 }
785 }
786 }
787 pmc->gsquery = 0;
788 if (scount == nsrcs) /* all sources excluded */
789 return 0;
790 return 1;
791}
792
Al Viroea4d9e72006-09-27 18:30:52 -0700793static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800794{
795 struct ip_sf_list *psf;
796 int i, scount;
797
798 if (pmc->sfmode == MCAST_EXCLUDE)
799 return igmp_xmarksources(pmc, nsrcs, srcs);
800
801 /* mark INCLUDE-mode sources */
802 scount = 0;
803 for (psf=pmc->sources; psf; psf=psf->sf_next) {
804 if (scount == nsrcs)
805 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 for (i=0; i<nsrcs; i++)
807 if (srcs[i] == psf->sf_inaddr) {
808 psf->sf_gsresp = 1;
809 scount++;
810 break;
811 }
812 }
David L Stevensad125832006-01-18 14:20:56 -0800813 if (!scount) {
814 pmc->gsquery = 0;
815 return 0;
816 }
817 pmc->gsquery = 1;
818 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Eric Dumazetd679c532012-09-06 20:37:06 +0000821/* return true if packet was dropped */
822static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct ip_mc_list *im;
825
826 /* Timers are only set for non-local groups */
827
828 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000829 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000831 rcu_read_lock();
832 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 if (im->multiaddr == group) {
834 igmp_stop_timer(im);
835 break;
836 }
837 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000838 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000839 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Eric Dumazetd679c532012-09-06 20:37:06 +0000842/* return true if packet was dropped */
843static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 int len)
845{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300846 struct igmphdr *ih = igmp_hdr(skb);
847 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700849 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 int max_delay;
851 int mark = 0;
852
853
David Stevens5b7c8402010-09-30 14:29:40 +0000854 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (ih->code == 0) {
856 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 max_delay = IGMP_Query_Response_Interval;
859 in_dev->mr_v1_seen = jiffies +
860 IGMP_V1_Router_Present_Timeout;
861 group = 0;
862 } else {
863 /* v2 router present */
864 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
865 in_dev->mr_v2_seen = jiffies +
866 IGMP_V2_Router_Present_Timeout;
867 }
868 /* cancel the interface change timer */
869 in_dev->mr_ifc_count = 0;
870 if (del_timer(&in_dev->mr_ifc_timer))
871 __in_dev_put(in_dev);
872 /* clear deleted report items */
873 igmpv3_clear_delrec(in_dev);
874 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000875 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000876 } else if (IGMP_V1_SEEN(in_dev)) {
877 /* This is a v3 query with v1 queriers present */
878 max_delay = IGMP_Query_Response_Interval;
879 group = 0;
880 } else if (IGMP_V2_SEEN(in_dev)) {
881 /* this is a v3 query with v2 queriers present;
882 * Interpretation of the max_delay code is problematic here.
883 * A real v2 host would use ih_code directly, while v3 has a
884 * different encoding. We use the v3 encoding as more likely
885 * to be intended in a v3 query.
886 */
887 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800888 if (!max_delay)
889 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 } else { /* v3 */
891 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000892 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900893
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300894 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900896 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700897 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000898 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300899 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901
902 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
903 if (!max_delay)
904 max_delay = 1; /* can't mod w/ 0 */
905 in_dev->mr_maxdelay = max_delay;
906 if (ih3->qrv)
907 in_dev->mr_qrv = ih3->qrv;
908 if (!group) { /* general query */
909 if (ih3->nsrcs)
Eric Dumazetd679c532012-09-06 20:37:06 +0000910 return false; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000912 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
914 /* mark sources to include, if group & source-specific */
915 mark = ih3->nsrcs != 0;
916 }
917
918 /*
919 * - Start the timers in all of our membership records
920 * that the query applies to for the interface on
921 * which the query arrived excl. those that belong
922 * to a "local" group (224.0.0.X)
923 * - For timers already running check if they need to
924 * be reset.
925 * - Use the igmp->igmp_code field as the maximum
926 * delay possible
927 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000928 rcu_read_lock();
929 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800930 int changed;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (group && group != im->multiaddr)
933 continue;
934 if (im->multiaddr == IGMP_ALL_HOSTS)
935 continue;
936 spin_lock_bh(&im->lock);
937 if (im->tm_running)
938 im->gsquery = im->gsquery && mark;
939 else
940 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800941 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900942 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800944 if (changed)
945 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000947 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000948 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000951/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952int igmp_rcv(struct sk_buff *skb)
953{
954 /* This basically follows the spec line by line -- see RFC1112 */
955 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000956 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +0000958 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800960 if (in_dev == NULL)
961 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Herbert Xufb286bb2005-11-10 13:01:24 -0800963 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000964 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800965
966 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700967 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800968 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800969 break;
970 /* fall through */
971 case CHECKSUM_NONE:
972 skb->csum = 0;
973 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000974 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300977 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 switch (ih->type) {
979 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +0000980 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 case IGMP_HOST_MEMBERSHIP_REPORT:
983 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -0800985 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 break;
David Stevens24c69272005-12-02 20:32:59 -0800987 /* don't rely on MC router hearing unicast reports */
988 if (skb->pkt_type == PACKET_MULTICAST ||
989 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +0000990 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 break;
992 case IGMP_PIM:
993#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return pim_rcv_v1(skb);
995#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +0000996 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 case IGMP_DVMRP:
998 case IGMP_TRACE:
999 case IGMP_HOST_LEAVE_MESSAGE:
1000 case IGMP_MTRACE:
1001 case IGMP_MTRACE_RESP:
1002 break;
1003 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001006
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001007drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001008 if (dropped)
1009 kfree_skb(skb);
1010 else
1011 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return 0;
1013}
1014
1015#endif
1016
1017
1018/*
1019 * Add a filter to a device
1020 */
1021
Al Viro63007722006-09-27 18:31:32 -07001022static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
1024 char buf[MAX_ADDR_LEN];
1025 struct net_device *dev = in_dev->dev;
1026
1027 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1028 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001029 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 routine. Something sort of:
1031 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1032 --ANK
1033 */
1034 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001035 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
1038/*
1039 * Remove a filter from a device
1040 */
1041
Al Viro63007722006-09-27 18:31:32 -07001042static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043{
1044 char buf[MAX_ADDR_LEN];
1045 struct net_device *dev = in_dev->dev;
1046
1047 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001048 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051#ifdef CONFIG_IP_MULTICAST
1052/*
1053 * deleted ip_mc_list manipulation
1054 */
1055static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1056{
1057 struct ip_mc_list *pmc;
1058
1059 /* this is an "ip_mc_list" for convenience; only the fields below
1060 * are actually used. In particular, the refcnt and users are not
1061 * used for management of the delete list. Using the same structure
1062 * for deleted items allows change reports to use common code with
1063 * non-deleted or query-response MCA's.
1064 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001065 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (!pmc)
1067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 spin_lock_bh(&im->lock);
1069 pmc->interface = im->interface;
1070 in_dev_hold(in_dev);
1071 pmc->multiaddr = im->multiaddr;
1072 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1073 IGMP_Unsolicited_Report_Count;
1074 pmc->sfmode = im->sfmode;
1075 if (pmc->sfmode == MCAST_INCLUDE) {
1076 struct ip_sf_list *psf;
1077
1078 pmc->tomb = im->tomb;
1079 pmc->sources = im->sources;
1080 im->tomb = im->sources = NULL;
1081 for (psf=pmc->sources; psf; psf=psf->sf_next)
1082 psf->sf_crcount = pmc->crcount;
1083 }
1084 spin_unlock_bh(&im->lock);
1085
1086 spin_lock_bh(&in_dev->mc_tomb_lock);
1087 pmc->next = in_dev->mc_tomb;
1088 in_dev->mc_tomb = pmc;
1089 spin_unlock_bh(&in_dev->mc_tomb_lock);
1090}
1091
Al Viro63007722006-09-27 18:31:32 -07001092static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
1094 struct ip_mc_list *pmc, *pmc_prev;
1095 struct ip_sf_list *psf, *psf_next;
1096
1097 spin_lock_bh(&in_dev->mc_tomb_lock);
1098 pmc_prev = NULL;
1099 for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
1100 if (pmc->multiaddr == multiaddr)
1101 break;
1102 pmc_prev = pmc;
1103 }
1104 if (pmc) {
1105 if (pmc_prev)
1106 pmc_prev->next = pmc->next;
1107 else
1108 in_dev->mc_tomb = pmc->next;
1109 }
1110 spin_unlock_bh(&in_dev->mc_tomb_lock);
1111 if (pmc) {
1112 for (psf=pmc->tomb; psf; psf=psf_next) {
1113 psf_next = psf->sf_next;
1114 kfree(psf);
1115 }
1116 in_dev_put(pmc->interface);
1117 kfree(pmc);
1118 }
1119}
1120
1121static void igmpv3_clear_delrec(struct in_device *in_dev)
1122{
1123 struct ip_mc_list *pmc, *nextpmc;
1124
1125 spin_lock_bh(&in_dev->mc_tomb_lock);
1126 pmc = in_dev->mc_tomb;
1127 in_dev->mc_tomb = NULL;
1128 spin_unlock_bh(&in_dev->mc_tomb_lock);
1129
1130 for (; pmc; pmc = nextpmc) {
1131 nextpmc = pmc->next;
1132 ip_mc_clear_src(pmc);
1133 in_dev_put(pmc->interface);
1134 kfree(pmc);
1135 }
1136 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001137 rcu_read_lock();
1138 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 struct ip_sf_list *psf, *psf_next;
1140
1141 spin_lock_bh(&pmc->lock);
1142 psf = pmc->tomb;
1143 pmc->tomb = NULL;
1144 spin_unlock_bh(&pmc->lock);
1145 for (; psf; psf=psf_next) {
1146 psf_next = psf->sf_next;
1147 kfree(psf);
1148 }
1149 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001150 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152#endif
1153
1154static void igmp_group_dropped(struct ip_mc_list *im)
1155{
1156 struct in_device *in_dev = im->interface;
1157#ifdef CONFIG_IP_MULTICAST
1158 int reporter;
1159#endif
1160
1161 if (im->loaded) {
1162 im->loaded = 0;
1163 ip_mc_filter_del(in_dev, im->multiaddr);
1164 }
1165
1166#ifdef CONFIG_IP_MULTICAST
1167 if (im->multiaddr == IGMP_ALL_HOSTS)
1168 return;
1169
1170 reporter = im->reporter;
1171 igmp_stop_timer(im);
1172
1173 if (!in_dev->dead) {
1174 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (IGMP_V2_SEEN(in_dev)) {
1177 if (reporter)
1178 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001179 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 /* IGMPv3 */
1182 igmpv3_add_delrec(in_dev, im);
1183
1184 igmp_ifc_event(in_dev);
1185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
1189static void igmp_group_added(struct ip_mc_list *im)
1190{
1191 struct in_device *in_dev = im->interface;
1192
1193 if (im->loaded == 0) {
1194 im->loaded = 1;
1195 ip_mc_filter_add(in_dev, im->multiaddr);
1196 }
1197
1198#ifdef CONFIG_IP_MULTICAST
1199 if (im->multiaddr == IGMP_ALL_HOSTS)
1200 return;
1201
1202 if (in_dev->dead)
1203 return;
1204 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1205 spin_lock_bh(&im->lock);
1206 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1207 spin_unlock_bh(&im->lock);
1208 return;
1209 }
1210 /* else, v3 */
1211
1212 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1213 IGMP_Unsolicited_Report_Count;
1214 igmp_ifc_event(in_dev);
1215#endif
1216}
1217
1218
1219/*
1220 * Multicast list managers
1221 */
1222
Eric Dumazete9897072013-06-07 08:48:57 -07001223static u32 ip_mc_hash(const struct ip_mc_list *im)
1224{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001225 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001226}
1227
1228static void ip_mc_hash_add(struct in_device *in_dev,
1229 struct ip_mc_list *im)
1230{
1231 struct ip_mc_list __rcu **mc_hash;
1232 u32 hash;
1233
1234 mc_hash = rtnl_dereference(in_dev->mc_hash);
1235 if (mc_hash) {
1236 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001237 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001238 rcu_assign_pointer(mc_hash[hash], im);
1239 return;
1240 }
1241
1242 /* do not use a hash table for small number of items */
1243 if (in_dev->mc_count < 4)
1244 return;
1245
1246 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1247 GFP_KERNEL);
1248 if (!mc_hash)
1249 return;
1250
1251 for_each_pmc_rtnl(in_dev, im) {
1252 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001253 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001254 RCU_INIT_POINTER(mc_hash[hash], im);
1255 }
1256
1257 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1258}
1259
1260static void ip_mc_hash_remove(struct in_device *in_dev,
1261 struct ip_mc_list *im)
1262{
1263 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1264 struct ip_mc_list *aux;
1265
1266 if (!mc_hash)
1267 return;
1268 mc_hash += ip_mc_hash(im);
1269 while ((aux = rtnl_dereference(*mc_hash)) != im)
1270 mc_hash = &aux->next_hash;
1271 *mc_hash = im->next_hash;
1272}
1273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275/*
1276 * A socket has joined a multicast group on device dev.
1277 */
1278
Al Viro8f935bb2006-09-27 18:30:07 -07001279void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
1281 struct ip_mc_list *im;
1282
1283 ASSERT_RTNL();
1284
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001285 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (im->multiaddr == addr) {
1287 im->users++;
1288 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1289 goto out;
1290 }
1291 }
1292
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001293 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (!im)
1295 goto out;
1296
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001297 im->users = 1;
1298 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001300 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 /* initial mode is (EX, empty) */
1302 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 atomic_set(&im->refcnt, 1);
1305 spin_lock_init(&im->lock);
1306#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001307 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001310
1311 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001312 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001313 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001314
Eric Dumazete9897072013-06-07 08:48:57 -07001315 ip_mc_hash_add(in_dev, im);
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317#ifdef CONFIG_IP_MULTICAST
1318 igmpv3_del_delrec(in_dev, im->multiaddr);
1319#endif
1320 igmp_group_added(im);
1321 if (!in_dev->dead)
1322 ip_rt_multicast_event(in_dev);
1323out:
1324 return;
1325}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001326EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001329 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001330 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001331static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001332{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001333#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001334 struct ip_mc_list *im;
1335 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001336
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001337 ASSERT_RTNL();
1338
1339 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001340 if (im->multiaddr == IGMP_ALL_HOSTS)
1341 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001342
Eric Dumazet866f3b22010-11-18 09:33:19 -08001343 /* a failover is happening and switches
1344 * must be notified immediately
1345 */
1346 if (IGMP_V1_SEEN(in_dev))
1347 type = IGMP_HOST_MEMBERSHIP_REPORT;
1348 else if (IGMP_V2_SEEN(in_dev))
1349 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1350 else
1351 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1352 igmp_send_report(in_dev, im, type);
1353 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001354#endif
1355}
1356
1357/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 * A socket has left a multicast group on device dev
1359 */
1360
Al Viro8f935bb2006-09-27 18:30:07 -07001361void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001363 struct ip_mc_list *i;
1364 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001367
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001368 for (ip = &in_dev->mc_list;
1369 (i = rtnl_dereference(*ip)) != NULL;
1370 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001371 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001373 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001374 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001375 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001377 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 if (!in_dev->dead)
1380 ip_rt_multicast_event(in_dev);
1381
1382 ip_ma_put(i);
1383 return;
1384 }
1385 break;
1386 }
1387 }
1388}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001389EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Moni Shoua75c78502009-09-15 02:37:40 -07001391/* Device changing type */
1392
1393void ip_mc_unmap(struct in_device *in_dev)
1394{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001395 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001396
1397 ASSERT_RTNL();
1398
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001399 for_each_pmc_rtnl(in_dev, pmc)
1400 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001401}
1402
1403void ip_mc_remap(struct in_device *in_dev)
1404{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001405 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001406
1407 ASSERT_RTNL();
1408
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001409 for_each_pmc_rtnl(in_dev, pmc)
1410 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/* Device going down */
1414
1415void ip_mc_down(struct in_device *in_dev)
1416{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001417 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 ASSERT_RTNL();
1420
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001421 for_each_pmc_rtnl(in_dev, pmc)
1422 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424#ifdef CONFIG_IP_MULTICAST
1425 in_dev->mr_ifc_count = 0;
1426 if (del_timer(&in_dev->mr_ifc_timer))
1427 __in_dev_put(in_dev);
1428 in_dev->mr_gq_running = 0;
1429 if (del_timer(&in_dev->mr_gq_timer))
1430 __in_dev_put(in_dev);
1431 igmpv3_clear_delrec(in_dev);
1432#endif
1433
1434 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1435}
1436
1437void ip_mc_init_dev(struct in_device *in_dev)
1438{
1439 ASSERT_RTNL();
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001442 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1443 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001444 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1445 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1447#endif
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 spin_lock_init(&in_dev->mc_tomb_lock);
1450}
1451
1452/* Device going up */
1453
1454void ip_mc_up(struct in_device *in_dev)
1455{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001456 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 ASSERT_RTNL();
1459
1460 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1461
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001462 for_each_pmc_rtnl(in_dev, pmc)
1463 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466/*
1467 * Device is about to be destroyed: clean up.
1468 */
1469
1470void ip_mc_destroy_dev(struct in_device *in_dev)
1471{
1472 struct ip_mc_list *i;
1473
1474 ASSERT_RTNL();
1475
1476 /* Deactivate timers */
1477 ip_mc_down(in_dev);
1478
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001479 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1480 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001481 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001482
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001483 /* We've dropped the groups in ip_mc_down already */
1484 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001489/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001490static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 struct net_device *dev = NULL;
1493 struct in_device *idev = NULL;
1494
1495 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001496 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return idev;
1498 }
1499 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001500 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 if (!dev)
1502 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 }
1504
David S. Millerb23dd4f2011-03-02 14:31:35 -08001505 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001506 struct rtable *rt = ip_route_output(net,
1507 imr->imr_multiaddr.s_addr,
1508 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001509 if (!IS_ERR(rt)) {
1510 dev = rt->dst.dev;
1511 ip_rt_put(rt);
1512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
1514 if (dev) {
1515 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001516 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 }
1518 return idev;
1519}
1520
1521/*
1522 * Join a socket to a group
1523 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001524int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1525int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527
1528static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001529 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 struct ip_sf_list *psf, *psf_prev;
1532 int rv = 0;
1533
1534 psf_prev = NULL;
1535 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1536 if (psf->sf_inaddr == *psfsrc)
1537 break;
1538 psf_prev = psf;
1539 }
1540 if (!psf || psf->sf_count[sfmode] == 0) {
1541 /* source filter not found, or count wrong => bug */
1542 return -ESRCH;
1543 }
1544 psf->sf_count[sfmode]--;
1545 if (psf->sf_count[sfmode] == 0) {
1546 ip_rt_multicast_event(pmc->interface);
1547 }
1548 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1549#ifdef CONFIG_IP_MULTICAST
1550 struct in_device *in_dev = pmc->interface;
1551#endif
1552
1553 /* no more filters for this source */
1554 if (psf_prev)
1555 psf_prev->sf_next = psf->sf_next;
1556 else
1557 pmc->sources = psf->sf_next;
1558#ifdef CONFIG_IP_MULTICAST
1559 if (psf->sf_oldin &&
1560 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001561 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 IGMP_Unsolicited_Report_Count;
1563 psf->sf_next = pmc->tomb;
1564 pmc->tomb = psf;
1565 rv = 1;
1566 } else
1567#endif
1568 kfree(psf);
1569 }
1570 return rv;
1571}
1572
1573#ifndef CONFIG_IP_MULTICAST
1574#define igmp_ifc_event(x) do { } while (0)
1575#endif
1576
Al Viro8f935bb2006-09-27 18:30:07 -07001577static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1578 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
1580 struct ip_mc_list *pmc;
1581 int changerec = 0;
1582 int i, err;
1583
1584 if (!in_dev)
1585 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001586 rcu_read_lock();
1587 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 if (*pmca == pmc->multiaddr)
1589 break;
1590 }
1591 if (!pmc) {
1592 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001593 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 return -ESRCH;
1595 }
1596 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001597 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598#ifdef CONFIG_IP_MULTICAST
1599 sf_markstate(pmc);
1600#endif
1601 if (!delta) {
1602 err = -EINVAL;
1603 if (!pmc->sfcount[sfmode])
1604 goto out_unlock;
1605 pmc->sfcount[sfmode]--;
1606 }
1607 err = 0;
1608 for (i=0; i<sfcount; i++) {
1609 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1610
1611 changerec |= rv > 0;
1612 if (!err && rv < 0)
1613 err = rv;
1614 }
1615 if (pmc->sfmode == MCAST_EXCLUDE &&
1616 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1617 pmc->sfcount[MCAST_INCLUDE]) {
1618#ifdef CONFIG_IP_MULTICAST
1619 struct ip_sf_list *psf;
1620#endif
1621
1622 /* filter mode change */
1623 pmc->sfmode = MCAST_INCLUDE;
1624#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001625 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 IGMP_Unsolicited_Report_Count;
1627 in_dev->mr_ifc_count = pmc->crcount;
1628 for (psf=pmc->sources; psf; psf = psf->sf_next)
1629 psf->sf_crcount = 0;
1630 igmp_ifc_event(pmc->interface);
1631 } else if (sf_setstate(pmc) || changerec) {
1632 igmp_ifc_event(pmc->interface);
1633#endif
1634 }
1635out_unlock:
1636 spin_unlock_bh(&pmc->lock);
1637 return err;
1638}
1639
1640/*
1641 * Add multicast single-source filter to the interface list
1642 */
1643static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001644 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
1646 struct ip_sf_list *psf, *psf_prev;
1647
1648 psf_prev = NULL;
1649 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1650 if (psf->sf_inaddr == *psfsrc)
1651 break;
1652 psf_prev = psf;
1653 }
1654 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001655 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (!psf)
1657 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 psf->sf_inaddr = *psfsrc;
1659 if (psf_prev) {
1660 psf_prev->sf_next = psf;
1661 } else
1662 pmc->sources = psf;
1663 }
1664 psf->sf_count[sfmode]++;
1665 if (psf->sf_count[sfmode] == 1) {
1666 ip_rt_multicast_event(pmc->interface);
1667 }
1668 return 0;
1669}
1670
1671#ifdef CONFIG_IP_MULTICAST
1672static void sf_markstate(struct ip_mc_list *pmc)
1673{
1674 struct ip_sf_list *psf;
1675 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1676
1677 for (psf=pmc->sources; psf; psf=psf->sf_next)
1678 if (pmc->sfcount[MCAST_EXCLUDE]) {
1679 psf->sf_oldin = mca_xcount ==
1680 psf->sf_count[MCAST_EXCLUDE] &&
1681 !psf->sf_count[MCAST_INCLUDE];
1682 } else
1683 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1684}
1685
1686static int sf_setstate(struct ip_mc_list *pmc)
1687{
David L Stevensad125832006-01-18 14:20:56 -08001688 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1690 int qrv = pmc->interface->mr_qrv;
1691 int new_in, rv;
1692
1693 rv = 0;
1694 for (psf=pmc->sources; psf; psf=psf->sf_next) {
1695 if (pmc->sfcount[MCAST_EXCLUDE]) {
1696 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1697 !psf->sf_count[MCAST_INCLUDE];
1698 } else
1699 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001700 if (new_in) {
1701 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001702 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001703
1704 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next) {
1705 if (dpsf->sf_inaddr == psf->sf_inaddr)
1706 break;
1707 prev = dpsf;
1708 }
1709 if (dpsf) {
1710 if (prev)
1711 prev->sf_next = dpsf->sf_next;
1712 else
1713 pmc->tomb = dpsf->sf_next;
1714 kfree(dpsf);
1715 }
1716 psf->sf_crcount = qrv;
1717 rv++;
1718 }
1719 } else if (psf->sf_oldin) {
1720
1721 psf->sf_crcount = 0;
1722 /*
1723 * add or update "delete" records if an active filter
1724 * is now inactive
1725 */
1726 for (dpsf=pmc->tomb; dpsf; dpsf=dpsf->sf_next)
1727 if (dpsf->sf_inaddr == psf->sf_inaddr)
1728 break;
1729 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001730 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001731 if (!dpsf)
1732 continue;
1733 *dpsf = *psf;
1734 /* pmc->lock held by callers */
1735 dpsf->sf_next = pmc->tomb;
1736 pmc->tomb = dpsf;
1737 }
1738 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 rv++;
1740 }
1741 }
1742 return rv;
1743}
1744#endif
1745
1746/*
1747 * Add multicast source filter list to the interface list
1748 */
Al Viro8f935bb2006-09-27 18:30:07 -07001749static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1750 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
1752 struct ip_mc_list *pmc;
1753 int isexclude;
1754 int i, err;
1755
1756 if (!in_dev)
1757 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001758 rcu_read_lock();
1759 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (*pmca == pmc->multiaddr)
1761 break;
1762 }
1763 if (!pmc) {
1764 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001765 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return -ESRCH;
1767 }
1768 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001769 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771#ifdef CONFIG_IP_MULTICAST
1772 sf_markstate(pmc);
1773#endif
1774 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1775 if (!delta)
1776 pmc->sfcount[sfmode]++;
1777 err = 0;
1778 for (i=0; i<sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00001779 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (err)
1781 break;
1782 }
1783 if (err) {
1784 int j;
1785
Jun Zhao685f94e62011-11-22 17:19:03 +00001786 if (!delta)
1787 pmc->sfcount[sfmode]--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 for (j=0; j<i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00001789 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1791#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001793 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794#endif
1795
1796 /* filter mode change */
1797 if (pmc->sfcount[MCAST_EXCLUDE])
1798 pmc->sfmode = MCAST_EXCLUDE;
1799 else if (pmc->sfcount[MCAST_INCLUDE])
1800 pmc->sfmode = MCAST_INCLUDE;
1801#ifdef CONFIG_IP_MULTICAST
1802 /* else no filters; keep old mode for reports */
1803
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001804 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 IGMP_Unsolicited_Report_Count;
1806 in_dev->mr_ifc_count = pmc->crcount;
1807 for (psf=pmc->sources; psf; psf = psf->sf_next)
1808 psf->sf_crcount = 0;
1809 igmp_ifc_event(in_dev);
1810 } else if (sf_setstate(pmc)) {
1811 igmp_ifc_event(in_dev);
1812#endif
1813 }
1814 spin_unlock_bh(&pmc->lock);
1815 return err;
1816}
1817
1818static void ip_mc_clear_src(struct ip_mc_list *pmc)
1819{
1820 struct ip_sf_list *psf, *nextpsf;
1821
1822 for (psf=pmc->tomb; psf; psf=nextpsf) {
1823 nextpsf = psf->sf_next;
1824 kfree(psf);
1825 }
1826 pmc->tomb = NULL;
1827 for (psf=pmc->sources; psf; psf=nextpsf) {
1828 nextpsf = psf->sf_next;
1829 kfree(psf);
1830 }
1831 pmc->sources = NULL;
1832 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001833 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 pmc->sfcount[MCAST_EXCLUDE] = 1;
1835}
1836
1837
1838/*
1839 * Join a multicast group
1840 */
1841int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1842{
1843 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001844 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001845 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 struct in_device *in_dev;
1847 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001848 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001849 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 int count = 0;
1851
Joe Perchesf97c1e02007-12-16 13:45:43 -08001852 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return -EINVAL;
1854
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001855 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Daniel Lezcano877aced2008-08-13 16:15:57 -07001857 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 if (!in_dev) {
1860 iml = NULL;
1861 err = -ENODEV;
1862 goto done;
1863 }
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001866 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001867 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001868 if (i->multi.imr_multiaddr.s_addr == addr &&
1869 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 count++;
1872 }
1873 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001874 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001876 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001877 if (iml == NULL)
1878 goto done;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001881 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 iml->sflist = NULL;
1883 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001884 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001888 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return err;
1890}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001891EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1894 struct in_device *in_dev)
1895{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001896 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 int err;
1898
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001899 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /* any-source empty exclude case */
1901 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1902 iml->sfmode, 0, NULL, 0);
1903 }
1904 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001905 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001906 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001907 /* decrease mem now to avoid the memleak warning */
1908 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08001909 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 return err;
1911}
1912
1913/*
1914 * Ask a socket to leave a group.
1915 */
1916
1917int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1918{
1919 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001920 struct ip_mc_socklist *iml;
1921 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001922 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001923 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001924 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001925 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001926 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001929 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001930 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001931 for (imlp = &inet->mc_list;
1932 (iml = rtnl_dereference(*imlp)) != NULL;
1933 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001934 if (iml->multi.imr_multiaddr.s_addr != group)
1935 continue;
1936 if (ifindex) {
1937 if (iml->multi.imr_ifindex != ifindex)
1938 continue;
1939 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1940 iml->multi.imr_address.s_addr)
1941 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
David L Stevensacd6e002006-08-17 16:27:39 -07001943 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001945 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001946
1947 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001948 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001949 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001950 /* decrease mem now to avoid the memleak warning */
1951 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08001952 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07001953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
David L Stevensacd6e002006-08-17 16:27:39 -07001955 if (!in_dev)
1956 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001958 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
stephen hemminger193ba922012-10-01 12:32:34 +00001960EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
1962int ip_mc_source(int add, int omode, struct sock *sk, struct
1963 ip_mreq_source *mreqs, int ifindex)
1964{
1965 int err;
1966 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001967 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 struct ip_mc_socklist *pmc;
1969 struct in_device *in_dev = NULL;
1970 struct inet_sock *inet = inet_sk(sk);
1971 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001972 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001973 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 int i, j, rv;
1975
Joe Perchesf97c1e02007-12-16 13:45:43 -08001976 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 return -EINVAL;
1978
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001979 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1982 imr.imr_address.s_addr = mreqs->imr_interface;
1983 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001984 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 if (!in_dev) {
1987 err = -ENODEV;
1988 goto done;
1989 }
1990 err = -EADDRNOTAVAIL;
1991
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001992 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08001993 if ((pmc->multi.imr_multiaddr.s_addr ==
1994 imr.imr_multiaddr.s_addr) &&
1995 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
1997 }
David L Stevens917f2f12005-07-08 17:45:16 -07001998 if (!pmc) { /* must have a prior join */
1999 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* if a source filter was set, must be the same mode as before */
2003 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002004 if (pmc->sfmode != omode) {
2005 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 } else if (pmc->sfmode != omode) {
2009 /* allow mode switches for empty-set filters */
2010 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002011 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 NULL, 0);
2013 pmc->sfmode = omode;
2014 }
2015
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002016 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (!add) {
2018 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002019 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 rv = !0;
2021 for (i=0; i<psl->sl_count; i++) {
2022 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002023 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (rv == 0)
2025 break;
2026 }
2027 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002028 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
David L Stevens8cdaaa12005-07-08 17:39:23 -07002030 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2031 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2032 leavegroup = 1;
2033 goto done;
2034 }
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002037 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 &mreqs->imr_sourceaddr, 1);
2039
2040 for (j=i+1; j<psl->sl_count; j++)
2041 psl->sl_addr[j-1] = psl->sl_addr[j];
2042 psl->sl_count--;
2043 err = 0;
2044 goto done;
2045 }
2046 /* else, add a new source to the filter */
2047
2048 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2049 err = -ENOBUFS;
2050 goto done;
2051 }
2052 if (!psl || psl->sl_count == psl->sl_max) {
2053 struct ip_sf_socklist *newpsl;
2054 int count = IP_SFBLOCK;
2055
2056 if (psl)
2057 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002058 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (!newpsl) {
2060 err = -ENOBUFS;
2061 goto done;
2062 }
2063 newpsl->sl_max = count;
2064 newpsl->sl_count = count - IP_SFBLOCK;
2065 if (psl) {
2066 for (i=0; i<psl->sl_count; i++)
2067 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002068 /* decrease mem now to avoid the memleak warning */
2069 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002070 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002072 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002073 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
2076 for (i=0; i<psl->sl_count; i++) {
2077 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002078 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (rv == 0)
2080 break;
2081 }
2082 if (rv == 0) /* address already there is an error */
2083 goto done;
2084 for (j=psl->sl_count-1; j>=i; j--)
2085 psl->sl_addr[j+1] = psl->sl_addr[j];
2086 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2087 psl->sl_count++;
2088 err = 0;
2089 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002090 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 &mreqs->imr_sourceaddr, 1);
2092done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002093 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002094 if (leavegroup)
2095 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return err;
2097}
2098
2099int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2100{
David L Stevens9951f032005-07-08 17:47:28 -07002101 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002103 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 struct ip_mc_socklist *pmc;
2105 struct in_device *in_dev;
2106 struct inet_sock *inet = inet_sk(sk);
2107 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002108 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002109 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110
Joe Perchesf97c1e02007-12-16 13:45:43 -08002111 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return -EINVAL;
2113 if (msf->imsf_fmode != MCAST_INCLUDE &&
2114 msf->imsf_fmode != MCAST_EXCLUDE)
2115 return -EINVAL;
2116
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002117 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2120 imr.imr_address.s_addr = msf->imsf_interface;
2121 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002122 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 if (!in_dev) {
2125 err = -ENODEV;
2126 goto done;
2127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
David L Stevens9951f032005-07-08 17:47:28 -07002129 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2130 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2131 leavegroup = 1;
2132 goto done;
2133 }
2134
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002135 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2137 pmc->multi.imr_ifindex == imr.imr_ifindex)
2138 break;
2139 }
David L Stevens917f2f12005-07-08 17:45:16 -07002140 if (!pmc) { /* must have a prior join */
2141 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002145 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2146 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (!newpsl) {
2148 err = -ENOBUFS;
2149 goto done;
2150 }
2151 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2152 memcpy(newpsl->sl_addr, msf->imsf_slist,
2153 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2154 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2155 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2156 if (err) {
2157 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2158 goto done;
2159 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002160 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002162 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2163 msf->imsf_fmode, 0, NULL, 0);
2164 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002165 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (psl) {
2167 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2168 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002169 /* decrease mem now to avoid the memleak warning */
2170 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002171 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 } else
2173 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2174 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002175 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002177 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002179 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002180 if (leavegroup)
2181 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return err;
2183}
2184
2185int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2186 struct ip_msfilter __user *optval, int __user *optlen)
2187{
2188 int err, len, count, copycount;
2189 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002190 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct ip_mc_socklist *pmc;
2192 struct in_device *in_dev;
2193 struct inet_sock *inet = inet_sk(sk);
2194 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002195 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Joe Perchesf97c1e02007-12-16 13:45:43 -08002197 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return -EINVAL;
2199
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002200 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2203 imr.imr_address.s_addr = msf->imsf_interface;
2204 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002205 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 if (!in_dev) {
2208 err = -ENODEV;
2209 goto done;
2210 }
2211 err = -EADDRNOTAVAIL;
2212
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002213 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2215 pmc->multi.imr_ifindex == imr.imr_ifindex)
2216 break;
2217 }
2218 if (!pmc) /* must have a prior join */
2219 goto done;
2220 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002221 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002222 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (!psl) {
2224 len = 0;
2225 count = 0;
2226 } else {
2227 count = psl->sl_count;
2228 }
2229 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2230 len = copycount * sizeof(psl->sl_addr[0]);
2231 msf->imsf_numsrc = count;
2232 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2233 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2234 return -EFAULT;
2235 }
2236 if (len &&
2237 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2238 return -EFAULT;
2239 return 0;
2240done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002241 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 return err;
2243}
2244
2245int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2246 struct group_filter __user *optval, int __user *optlen)
2247{
2248 int err, i, count, copycount;
2249 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002250 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct ip_mc_socklist *pmc;
2252 struct inet_sock *inet = inet_sk(sk);
2253 struct ip_sf_socklist *psl;
2254
2255 psin = (struct sockaddr_in *)&gsf->gf_group;
2256 if (psin->sin_family != AF_INET)
2257 return -EINVAL;
2258 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002259 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return -EINVAL;
2261
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002262 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 err = -EADDRNOTAVAIL;
2265
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002266 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2268 pmc->multi.imr_ifindex == gsf->gf_interface)
2269 break;
2270 }
2271 if (!pmc) /* must have a prior join */
2272 goto done;
2273 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002274 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002275 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 count = psl ? psl->sl_count : 0;
2277 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2278 gsf->gf_numsrc = count;
2279 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2280 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2281 return -EFAULT;
2282 }
2283 for (i=0; i<copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct sockaddr_storage ss;
2285
2286 psin = (struct sockaddr_in *)&ss;
2287 memset(&ss, 0, sizeof(ss));
2288 psin->sin_family = AF_INET;
2289 psin->sin_addr.s_addr = psl->sl_addr[i];
2290 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2291 return -EFAULT;
2292 }
2293 return 0;
2294done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002295 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return err;
2297}
2298
2299/*
2300 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2301 */
Al Viroc0cda062006-09-27 18:31:10 -07002302int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303{
2304 struct inet_sock *inet = inet_sk(sk);
2305 struct ip_mc_socklist *pmc;
2306 struct ip_sf_socklist *psl;
2307 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002308 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002310 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002311 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002314 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002315 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2317 pmc->multi.imr_ifindex == dif)
2318 break;
2319 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002320 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002322 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002323 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002324 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002326 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
2328 for (i=0; i<psl->sl_count; i++) {
2329 if (psl->sl_addr[i] == rmt_addr)
2330 break;
2331 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002332 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002334 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002336 goto unlock;
2337 ret = 1;
2338unlock:
2339 rcu_read_unlock();
2340out:
2341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342}
2343
2344/*
2345 * A socket is closing.
2346 */
2347
2348void ip_mc_drop_socket(struct sock *sk)
2349{
2350 struct inet_sock *inet = inet_sk(sk);
2351 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002352 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 if (inet->mc_list == NULL)
2355 return;
2356
2357 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002358 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002361 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002362 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002363 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002364 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002366 /* decrease mem now to avoid the memleak warning */
2367 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002368 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 rtnl_unlock();
2371}
2372
David S. Millerdbdd9a52011-03-10 16:34:38 -08002373/* called with rcu_read_lock() */
2374int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
2376 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002377 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct ip_sf_list *psf;
2379 int rv = 0;
2380
Eric Dumazete9897072013-06-07 08:48:57 -07002381 mc_hash = rcu_dereference(in_dev->mc_hash);
2382 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002383 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002384
2385 for (im = rcu_dereference(mc_hash[hash]);
2386 im != NULL;
2387 im = rcu_dereference(im->next_hash)) {
2388 if (im->multiaddr == mc_addr)
2389 break;
2390 }
2391 } else {
2392 for_each_pmc_rcu(in_dev, im) {
2393 if (im->multiaddr == mc_addr)
2394 break;
2395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397 if (im && proto == IPPROTO_IGMP) {
2398 rv = 1;
2399 } else if (im) {
2400 if (src_addr) {
2401 for (psf=im->sources; psf; psf=psf->sf_next) {
2402 if (psf->sf_inaddr == src_addr)
2403 break;
2404 }
2405 if (psf)
2406 rv = psf->sf_count[MCAST_INCLUDE] ||
2407 psf->sf_count[MCAST_EXCLUDE] !=
2408 im->sfcount[MCAST_EXCLUDE];
2409 else
2410 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2411 } else
2412 rv = 1; /* unspecified source; tentatively allow */
2413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return rv;
2415}
2416
2417#if defined(CONFIG_PROC_FS)
2418struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002419 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 struct net_device *dev;
2421 struct in_device *in_dev;
2422};
2423
2424#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2425
2426static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2427{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002428 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct ip_mc_list *im = NULL;
2430 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2431
Pavel Emelianov7562f872007-05-03 15:13:45 -07002432 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002433 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002435
2436 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (!in_dev)
2438 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002439 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (im) {
2441 state->in_dev = in_dev;
2442 break;
2443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
2445 return im;
2446}
2447
2448static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2449{
2450 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002451
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002452 im = rcu_dereference(im->next_rcu);
2453 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002454 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (!state->dev) {
2456 state->in_dev = NULL;
2457 break;
2458 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002459 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 if (!state->in_dev)
2461 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002462 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464 return im;
2465}
2466
2467static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2468{
2469 struct ip_mc_list *im = igmp_mc_get_first(seq);
2470 if (im)
2471 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2472 --pos;
2473 return pos ? NULL : im;
2474}
2475
2476static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002477 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
stephen hemminger61fbab72009-11-10 07:54:55 +00002479 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2481}
2482
2483static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2484{
2485 struct ip_mc_list *im;
2486 if (v == SEQ_START_TOKEN)
2487 im = igmp_mc_get_first(seq);
2488 else
2489 im = igmp_mc_get_next(seq, v);
2490 ++*pos;
2491 return im;
2492}
2493
2494static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002495 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496{
2497 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002498
2499 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002501 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2505{
2506 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002507 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2509 else {
2510 struct ip_mc_list *im = (struct ip_mc_list *)v;
2511 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2512 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002513 long delta;
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515#ifdef CONFIG_IP_MULTICAST
2516 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2517 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2518 "V3";
2519#else
2520 querier = "NONE";
2521#endif
2522
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002523 if (rcu_dereference(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002525 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
2527
Eric Dumazeta399a802012-08-08 21:13:53 +00002528 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002530 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002532 im->tm_running,
2533 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 im->reporter);
2535 }
2536 return 0;
2537}
2538
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002539static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 .start = igmp_mc_seq_start,
2541 .next = igmp_mc_seq_next,
2542 .stop = igmp_mc_seq_stop,
2543 .show = igmp_mc_seq_show,
2544};
2545
2546static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2547{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002548 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002549 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
Arjan van de Ven9a321442007-02-12 00:55:35 -08002552static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 .owner = THIS_MODULE,
2554 .open = igmp_mc_seq_open,
2555 .read = seq_read,
2556 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002557 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558};
2559
2560struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002561 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 struct net_device *dev;
2563 struct in_device *idev;
2564 struct ip_mc_list *im;
2565};
2566
2567#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2568
2569static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2570{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002571 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct ip_sf_list *psf = NULL;
2573 struct ip_mc_list *im = NULL;
2574 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2575
Pavel Emelianov7562f872007-05-03 15:13:45 -07002576 state->idev = NULL;
2577 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002578 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002580 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (unlikely(idev == NULL))
2582 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002583 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (likely(im != NULL)) {
2585 spin_lock_bh(&im->lock);
2586 psf = im->sources;
2587 if (likely(psf != NULL)) {
2588 state->im = im;
2589 state->idev = idev;
2590 break;
2591 }
2592 spin_unlock_bh(&im->lock);
2593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595 return psf;
2596}
2597
2598static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2599{
2600 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2601
2602 psf = psf->sf_next;
2603 while (!psf) {
2604 spin_unlock_bh(&state->im->lock);
2605 state->im = state->im->next;
2606 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002607 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (!state->dev) {
2609 state->idev = NULL;
2610 goto out;
2611 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002612 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (!state->idev)
2614 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002615 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617 if (!state->im)
2618 break;
2619 spin_lock_bh(&state->im->lock);
2620 psf = state->im->sources;
2621 }
2622out:
2623 return psf;
2624}
2625
2626static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2627{
2628 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2629 if (psf)
2630 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2631 --pos;
2632 return pos ? NULL : psf;
2633}
2634
2635static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002636 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
stephen hemminger61fbab72009-11-10 07:54:55 +00002638 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2640}
2641
2642static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2643{
2644 struct ip_sf_list *psf;
2645 if (v == SEQ_START_TOKEN)
2646 psf = igmp_mcf_get_first(seq);
2647 else
2648 psf = igmp_mcf_get_next(seq, v);
2649 ++*pos;
2650 return psf;
2651}
2652
2653static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002654 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2657 if (likely(state->im != NULL)) {
2658 spin_unlock_bh(&state->im->lock);
2659 state->im = NULL;
2660 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002661 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002663 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664}
2665
2666static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2667{
2668 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2669 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2670
2671 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002672 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 "%3s %6s "
2674 "%10s %10s %6s %6s\n", "Idx",
2675 "Device", "MCA",
2676 "SRC", "INC", "EXC");
2677 } else {
2678 seq_printf(seq,
2679 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002680 "0x%08x %6lu %6lu\n",
2681 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 ntohl(state->im->multiaddr),
2683 ntohl(psf->sf_inaddr),
2684 psf->sf_count[MCAST_INCLUDE],
2685 psf->sf_count[MCAST_EXCLUDE]);
2686 }
2687 return 0;
2688}
2689
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002690static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 .start = igmp_mcf_seq_start,
2692 .next = igmp_mcf_seq_next,
2693 .stop = igmp_mcf_seq_stop,
2694 .show = igmp_mcf_seq_show,
2695};
2696
2697static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2698{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002699 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002700 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Arjan van de Ven9a321442007-02-12 00:55:35 -08002703static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 .owner = THIS_MODULE,
2705 .open = igmp_mcf_seq_open,
2706 .read = seq_read,
2707 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002708 .release = seq_release_net,
2709};
2710
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002711static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002712{
2713 struct proc_dir_entry *pde;
2714
Gao fengd4beaa62013-02-18 01:34:54 +00002715 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002716 if (!pde)
2717 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002718 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2719 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002720 if (!pde)
2721 goto out_mcfilter;
2722 return 0;
2723
2724out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002725 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002726out_igmp:
2727 return -ENOMEM;
2728}
2729
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002730static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002731{
Gao fengece31ff2013-02-18 01:34:56 +00002732 remove_proc_entry("mcfilter", net->proc_net);
2733 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002734}
2735
2736static struct pernet_operations igmp_net_ops = {
2737 .init = igmp_net_init,
2738 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739};
2740
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002741static int igmp_netdev_event(struct notifier_block *this,
2742 unsigned long event, void *ptr)
2743{
2744 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2745 struct in_device *in_dev;
2746
2747 switch (event) {
2748 case NETDEV_RESEND_IGMP:
2749 in_dev = __in_dev_get_rtnl(dev);
2750 if (in_dev)
2751 ip_mc_rejoin_groups(in_dev);
2752 break;
2753 default:
2754 break;
2755 }
2756 return NOTIFY_DONE;
2757}
2758
2759static struct notifier_block igmp_notifier = {
2760 .notifier_call = igmp_netdev_event,
2761};
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763int __init igmp_mc_proc_init(void)
2764{
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002765 int err;
2766
2767 err = register_pernet_subsys(&igmp_net_ops);
2768 if (err)
2769 return err;
2770 err = register_netdevice_notifier(&igmp_notifier);
2771 if (err)
2772 goto reg_notif_fail;
2773 return 0;
2774
2775reg_notif_fail:
2776 unregister_pernet_subsys(&igmp_net_ops);
2777 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779#endif