blob: 97e4d1655d26bb65121c1a8954af2d7565c288a6 [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)
William Manleycab70042013-08-06 19:03:13 +0100117#define IGMP_V2_Unsolicited_Report_Interval (10*HZ)
118#define IGMP_V3_Unsolicited_Report_Interval (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119#define IGMP_Query_Response_Interval (10*HZ)
120#define IGMP_Unsolicited_Report_Count 2
121
122
123#define IGMP_Initial_Report_Delay (1)
124
125/* IGMP_Initial_Report_Delay is not from IGMP specs!
126 * IGMP specs require to report membership immediately after
127 * joining a group, but we delay the first report by a
128 * small interval. It seems more natural and still does not
129 * contradict to specs provided this delay is small enough.
130 */
131
Herbert Xu42f811b2007-06-04 23:34:44 -0700132#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900133 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700134 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
135 ((in_dev)->mr_v1_seen && \
136 time_before(jiffies, (in_dev)->mr_v1_seen)))
137#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900138 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700139 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
140 ((in_dev)->mr_v2_seen && \
141 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
William Manleycab70042013-08-06 19:03:13 +0100143static int unsolicited_report_interval(struct in_device *in_dev)
144{
William Manley26900482013-08-06 19:03:15 +0100145 int interval_ms, interval_jiffies;
146
William Manleycab70042013-08-06 19:03:13 +0100147 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100148 interval_ms = IN_DEV_CONF_GET(
149 in_dev,
150 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100151 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100152 interval_ms = IN_DEV_CONF_GET(
153 in_dev,
154 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
155
156 interval_jiffies = msecs_to_jiffies(interval_ms);
157
158 /* _timer functions can't handle a delay of 0 jiffies so ensure
159 * we always return a positive value.
160 */
161 if (interval_jiffies <= 0)
162 interval_jiffies = 1;
163 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Al Viro63007722006-09-27 18:31:32 -0700167static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static void igmpv3_clear_delrec(struct in_device *in_dev);
169static int sf_setstate(struct ip_mc_list *pmc);
170static void sf_markstate(struct ip_mc_list *pmc);
171#endif
172static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700173static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
174 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176static void ip_ma_put(struct ip_mc_list *im)
177{
178 if (atomic_dec_and_test(&im->refcnt)) {
179 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800180 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182}
183
David S. Millerd9aa9382010-11-15 08:52:02 -0800184#define for_each_pmc_rcu(in_dev, pmc) \
185 for (pmc = rcu_dereference(in_dev->mc_list); \
186 pmc != NULL; \
187 pmc = rcu_dereference(pmc->next_rcu))
188
189#define for_each_pmc_rtnl(in_dev, pmc) \
190 for (pmc = rtnl_dereference(in_dev->mc_list); \
191 pmc != NULL; \
192 pmc = rtnl_dereference(pmc->next_rcu))
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194#ifdef CONFIG_IP_MULTICAST
195
196/*
197 * Timer management
198 */
199
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000200static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 spin_lock_bh(&im->lock);
203 if (del_timer(&im->timer))
204 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800205 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 im->reporter = 0;
207 im->unsolicit_count = 0;
208 spin_unlock_bh(&im->lock);
209}
210
211/* It must be called with locked im->lock */
212static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
213{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500214 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800216 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!mod_timer(&im->timer, jiffies+tv+2))
218 atomic_inc(&im->refcnt);
219}
220
221static void igmp_gq_start_timer(struct in_device *in_dev)
222{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500223 int tv = prandom_u32() % in_dev->mr_maxdelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 in_dev->mr_gq_running = 1;
226 if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
227 in_dev_hold(in_dev);
228}
229
230static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
231{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500232 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
235 in_dev_hold(in_dev);
236}
237
238static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
239{
240 spin_lock_bh(&im->lock);
241 im->unsolicit_count = 0;
242 if (del_timer(&im->timer)) {
243 if ((long)(im->timer.expires-jiffies) < max_delay) {
244 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800245 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spin_unlock_bh(&im->lock);
247 return;
248 }
249 atomic_dec(&im->refcnt);
250 }
251 igmp_start_timer(im, max_delay);
252 spin_unlock_bh(&im->lock);
253}
254
255
256/*
257 * Send an IGMP report.
258 */
259
260#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
261
262
263static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
264 int gdeleted, int sdeleted)
265{
266 switch (type) {
267 case IGMPV3_MODE_IS_INCLUDE:
268 case IGMPV3_MODE_IS_EXCLUDE:
269 if (gdeleted || sdeleted)
270 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800271 if (!(pmc->gsquery && !psf->sf_gsresp)) {
272 if (pmc->sfmode == MCAST_INCLUDE)
273 return 1;
274 /* don't include if this source is excluded
275 * in all filters
276 */
277 if (psf->sf_count[MCAST_INCLUDE])
278 return type == IGMPV3_MODE_IS_INCLUDE;
279 return pmc->sfcount[MCAST_EXCLUDE] ==
280 psf->sf_count[MCAST_EXCLUDE];
281 }
282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 case IGMPV3_CHANGE_TO_INCLUDE:
284 if (gdeleted || sdeleted)
285 return 0;
286 return psf->sf_count[MCAST_INCLUDE] != 0;
287 case IGMPV3_CHANGE_TO_EXCLUDE:
288 if (gdeleted || sdeleted)
289 return 0;
290 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
291 psf->sf_count[MCAST_INCLUDE])
292 return 0;
293 return pmc->sfcount[MCAST_EXCLUDE] ==
294 psf->sf_count[MCAST_EXCLUDE];
295 case IGMPV3_ALLOW_NEW_SOURCES:
296 if (gdeleted || !psf->sf_crcount)
297 return 0;
298 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
299 case IGMPV3_BLOCK_OLD_SOURCES:
300 if (pmc->sfmode == MCAST_INCLUDE)
301 return gdeleted || (psf->sf_crcount && sdeleted);
302 return psf->sf_crcount && !gdeleted && !sdeleted;
303 }
304 return 0;
305}
306
307static int
308igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
309{
310 struct ip_sf_list *psf;
311 int scount = 0;
312
Weilong Chenc71151f2013-12-23 14:37:29 +0800313 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
315 continue;
316 scount++;
317 }
318 return scount;
319}
320
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000321#define igmp_skb_size(skb) (*(unsigned int *)((skb)->cb))
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
324{
325 struct sk_buff *skb;
326 struct rtable *rt;
327 struct iphdr *pip;
328 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700329 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700330 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000331 int hlen = LL_RESERVED_SPACE(dev);
332 int tlen = dev->needed_tailroom;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000334 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000335 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000336 GFP_ATOMIC | __GFP_NOWARN);
337 if (skb)
338 break;
339 size >>= 1;
340 if (size < 256)
341 return NULL;
342 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200343 skb->priority = TC_PRIO_CONTROL;
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000344 igmp_skb_size(skb) = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
David S. Miller31e45432011-05-03 20:25:42 -0700346 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500347 0, 0,
348 IPPROTO_IGMP, 0, dev->ifindex);
349 if (IS_ERR(rt)) {
350 kfree_skb(skb);
351 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Changli Gaod8d1f302010-06-10 23:31:35 -0700354 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 skb->dev = dev;
356
Herbert Xu66088242011-11-18 02:20:04 +0000357 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300359 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700360 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300361 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 pip->version = 4;
364 pip->ihl = (sizeof(struct iphdr)+4)>>2;
365 pip->tos = 0xc0;
366 pip->frag_off = htons(IP_DF);
367 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700368 pip->daddr = fl4.daddr;
369 pip->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 pip->protocol = IPPROTO_IGMP;
371 pip->tot_len = 0; /* filled in later */
Ansis Atteka703133d2013-09-18 15:29:53 -0700372 ip_select_ident(skb, &rt->dst, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000373 ((u8 *)&pip[1])[0] = IPOPT_RA;
374 ((u8 *)&pip[1])[1] = 4;
375 ((u8 *)&pip[1])[2] = 0;
376 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700378 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300379 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300380 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
382 pig->resv1 = 0;
383 pig->csum = 0;
384 pig->resv2 = 0;
385 pig->ngrec = 0;
386 return skb;
387}
388
389static int igmpv3_sendpack(struct sk_buff *skb)
390{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300391 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000392 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300394 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Herbert Xuc439cb22008-01-11 19:14:00 -0800396 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
400{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800401 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
405 int type, struct igmpv3_grec **ppgr)
406{
407 struct net_device *dev = pmc->interface->dev;
408 struct igmpv3_report *pih;
409 struct igmpv3_grec *pgr;
410
411 if (!skb)
412 skb = igmpv3_newpack(dev, dev->mtu);
413 if (!skb)
414 return NULL;
415 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
416 pgr->grec_type = type;
417 pgr->grec_auxwords = 0;
418 pgr->grec_nsrcs = 0;
419 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300420 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 pih->ngrec = htons(ntohs(pih->ngrec)+1);
422 *ppgr = pgr;
423 return skb;
424}
425
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000426#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? igmp_skb_size(skb) - (skb)->len : \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 skb_tailroom(skb)) : 0)
428
429static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
430 int type, int gdeleted, int sdeleted)
431{
432 struct net_device *dev = pmc->interface->dev;
433 struct igmpv3_report *pih;
434 struct igmpv3_grec *pgr = NULL;
435 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800436 int scount, stotal, first, isquery, truncate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 if (pmc->multiaddr == IGMP_ALL_HOSTS)
439 return skb;
440
441 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
442 type == IGMPV3_MODE_IS_EXCLUDE;
443 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
444 type == IGMPV3_CHANGE_TO_EXCLUDE;
445
David L Stevensad125832006-01-18 14:20:56 -0800446 stotal = scount = 0;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
449
David L Stevensad125832006-01-18 14:20:56 -0800450 if (!*psf_list)
451 goto empty_source;
452
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300453 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 /* EX and TO_EX get a fresh packet, if needed */
456 if (truncate) {
457 if (pih && pih->ngrec &&
458 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
459 if (skb)
460 igmpv3_sendpack(skb);
461 skb = igmpv3_newpack(dev, dev->mtu);
462 }
463 }
464 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800466 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700467 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 psf_next = psf->sf_next;
470
471 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
472 psf_prev = psf;
473 continue;
474 }
475
476 /* clear marks on query responses */
477 if (isquery)
478 psf->sf_gsresp = 0;
479
Al Viro63007722006-09-27 18:31:32 -0700480 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 first*sizeof(struct igmpv3_grec)) {
482 if (truncate && !first)
483 break; /* truncate these */
484 if (pgr)
485 pgr->grec_nsrcs = htons(scount);
486 if (skb)
487 igmpv3_sendpack(skb);
488 skb = igmpv3_newpack(dev, dev->mtu);
489 first = 1;
490 scount = 0;
491 }
492 if (first) {
493 skb = add_grhead(skb, pmc, type, &pgr);
494 first = 0;
495 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800496 if (!skb)
497 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700498 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800500 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
502 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
503 psf->sf_crcount--;
504 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
505 if (psf_prev)
506 psf_prev->sf_next = psf->sf_next;
507 else
508 *psf_list = psf->sf_next;
509 kfree(psf);
510 continue;
511 }
512 }
513 psf_prev = psf;
514 }
David L Stevensad125832006-01-18 14:20:56 -0800515
516empty_source:
517 if (!stotal) {
518 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
519 type == IGMPV3_BLOCK_OLD_SOURCES)
520 return skb;
521 if (pmc->crcount || isquery) {
522 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800523 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800524 igmpv3_sendpack(skb);
525 skb = NULL; /* add_grhead will get a new one */
526 }
527 skb = add_grhead(skb, pmc, type, &pgr);
528 }
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (pgr)
531 pgr->grec_nsrcs = htons(scount);
532
533 if (isquery)
534 pmc->gsquery = 0; /* clear query state on report */
535 return skb;
536}
537
538static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
539{
540 struct sk_buff *skb = NULL;
541 int type;
542
543 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000544 rcu_read_lock();
545 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (pmc->multiaddr == IGMP_ALL_HOSTS)
547 continue;
548 spin_lock_bh(&pmc->lock);
549 if (pmc->sfcount[MCAST_EXCLUDE])
550 type = IGMPV3_MODE_IS_EXCLUDE;
551 else
552 type = IGMPV3_MODE_IS_INCLUDE;
553 skb = add_grec(skb, pmc, type, 0, 0);
554 spin_unlock_bh(&pmc->lock);
555 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000556 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 } else {
558 spin_lock_bh(&pmc->lock);
559 if (pmc->sfcount[MCAST_EXCLUDE])
560 type = IGMPV3_MODE_IS_EXCLUDE;
561 else
562 type = IGMPV3_MODE_IS_INCLUDE;
563 skb = add_grec(skb, pmc, type, 0, 0);
564 spin_unlock_bh(&pmc->lock);
565 }
566 if (!skb)
567 return 0;
568 return igmpv3_sendpack(skb);
569}
570
571/*
572 * remove zero-count source records from a source filter list
573 */
574static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
575{
576 struct ip_sf_list *psf_prev, *psf_next, *psf;
577
578 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800579 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 psf_next = psf->sf_next;
581 if (psf->sf_crcount == 0) {
582 if (psf_prev)
583 psf_prev->sf_next = psf->sf_next;
584 else
585 *ppsf = psf->sf_next;
586 kfree(psf);
587 } else
588 psf_prev = psf;
589 }
590}
591
592static void igmpv3_send_cr(struct in_device *in_dev)
593{
594 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
595 struct sk_buff *skb = NULL;
596 int type, dtype;
597
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000598 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_lock_bh(&in_dev->mc_tomb_lock);
600
601 /* deleted MCA's */
602 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800603 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 pmc_next = pmc->next;
605 if (pmc->sfmode == MCAST_INCLUDE) {
606 type = IGMPV3_BLOCK_OLD_SOURCES;
607 dtype = IGMPV3_BLOCK_OLD_SOURCES;
608 skb = add_grec(skb, pmc, type, 1, 0);
609 skb = add_grec(skb, pmc, dtype, 1, 1);
610 }
611 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (pmc->sfmode == MCAST_EXCLUDE) {
613 type = IGMPV3_CHANGE_TO_INCLUDE;
614 skb = add_grec(skb, pmc, type, 1, 0);
615 }
David L Stevensad125832006-01-18 14:20:56 -0800616 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (pmc->crcount == 0) {
618 igmpv3_clear_zeros(&pmc->tomb);
619 igmpv3_clear_zeros(&pmc->sources);
620 }
621 }
622 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
623 if (pmc_prev)
624 pmc_prev->next = pmc_next;
625 else
626 in_dev->mc_tomb = pmc_next;
627 in_dev_put(pmc->interface);
628 kfree(pmc);
629 } else
630 pmc_prev = pmc;
631 }
632 spin_unlock_bh(&in_dev->mc_tomb_lock);
633
634 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000635 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 spin_lock_bh(&pmc->lock);
637 if (pmc->sfcount[MCAST_EXCLUDE]) {
638 type = IGMPV3_BLOCK_OLD_SOURCES;
639 dtype = IGMPV3_ALLOW_NEW_SOURCES;
640 } else {
641 type = IGMPV3_ALLOW_NEW_SOURCES;
642 dtype = IGMPV3_BLOCK_OLD_SOURCES;
643 }
644 skb = add_grec(skb, pmc, type, 0, 0);
645 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
646
647 /* filter mode changes */
648 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (pmc->sfmode == MCAST_EXCLUDE)
650 type = IGMPV3_CHANGE_TO_EXCLUDE;
651 else
652 type = IGMPV3_CHANGE_TO_INCLUDE;
653 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800654 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
656 spin_unlock_bh(&pmc->lock);
657 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000658 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 if (!skb)
661 return;
662 (void) igmpv3_sendpack(skb);
663}
664
665static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
666 int type)
667{
668 struct sk_buff *skb;
669 struct iphdr *iph;
670 struct igmphdr *ih;
671 struct rtable *rt;
672 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700673 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700674 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700675 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700676 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000677 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
680 return igmpv3_send_report(in_dev, pmc);
681 else if (type == IGMP_HOST_LEAVE_MESSAGE)
682 dst = IGMP_ALL_ROUTER;
683 else
684 dst = group;
685
David S. Miller31e45432011-05-03 20:25:42 -0700686 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500687 0, 0,
688 IPPROTO_IGMP, 0, dev->ifindex);
689 if (IS_ERR(rt))
690 return -1;
691
Herbert Xu66088242011-11-18 02:20:04 +0000692 hlen = LL_RESERVED_SPACE(dev);
693 tlen = dev->needed_tailroom;
694 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (skb == NULL) {
696 ip_rt_put(rt);
697 return -1;
698 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200699 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Changli Gaod8d1f302010-06-10 23:31:35 -0700701 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Herbert Xu66088242011-11-18 02:20:04 +0000703 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300705 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700706 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300707 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 iph->version = 4;
710 iph->ihl = (sizeof(struct iphdr)+4)>>2;
711 iph->tos = 0xc0;
712 iph->frag_off = htons(IP_DF);
713 iph->ttl = 1;
714 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700715 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 iph->protocol = IPPROTO_IGMP;
Ansis Atteka703133d2013-09-18 15:29:53 -0700717 ip_select_ident(skb, &rt->dst, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000718 ((u8 *)&iph[1])[0] = IPOPT_RA;
719 ((u8 *)&iph[1])[1] = 4;
720 ((u8 *)&iph[1])[2] = 0;
721 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800724 ih->type = type;
725 ih->code = 0;
726 ih->csum = 0;
727 ih->group = group;
728 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Herbert Xuc439cb22008-01-11 19:14:00 -0800730 return ip_local_out(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733static void igmp_gq_timer_expire(unsigned long data)
734{
735 struct in_device *in_dev = (struct in_device *)data;
736
737 in_dev->mr_gq_running = 0;
738 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700739 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
742static void igmp_ifc_timer_expire(unsigned long data)
743{
744 struct in_device *in_dev = (struct in_device *)data;
745
746 igmpv3_send_cr(in_dev);
747 if (in_dev->mr_ifc_count) {
748 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100749 igmp_ifc_start_timer(in_dev,
750 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700752 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static void igmp_ifc_event(struct in_device *in_dev)
756{
757 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
758 return;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900759 in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 IGMP_Unsolicited_Report_Count;
761 igmp_ifc_start_timer(in_dev, 1);
762}
763
764
765static void igmp_timer_expire(unsigned long data)
766{
Weilong Chenc71151f2013-12-23 14:37:29 +0800767 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct in_device *in_dev = im->interface;
769
770 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800771 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 if (im->unsolicit_count) {
774 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100775 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777 im->reporter = 1;
778 spin_unlock(&im->lock);
779
780 if (IGMP_V1_SEEN(in_dev))
781 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
782 else if (IGMP_V2_SEEN(in_dev))
783 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
784 else
785 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
786
787 ip_ma_put(im);
788}
789
David L Stevensad125832006-01-18 14:20:56 -0800790/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700791static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct ip_sf_list *psf;
794 int i, scount;
795
796 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800797 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (scount == nsrcs)
799 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800800 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800801 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000802 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800803 pmc->sfcount[MCAST_EXCLUDE] !=
804 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800805 break;
David L Stevensad125832006-01-18 14:20:56 -0800806 if (srcs[i] == psf->sf_inaddr) {
807 scount++;
808 break;
809 }
810 }
811 }
812 pmc->gsquery = 0;
813 if (scount == nsrcs) /* all sources excluded */
814 return 0;
815 return 1;
816}
817
Al Viroea4d9e72006-09-27 18:30:52 -0700818static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800819{
820 struct ip_sf_list *psf;
821 int i, scount;
822
823 if (pmc->sfmode == MCAST_EXCLUDE)
824 return igmp_xmarksources(pmc, nsrcs, srcs);
825
826 /* mark INCLUDE-mode sources */
827 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800828 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800829 if (scount == nsrcs)
830 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800831 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (srcs[i] == psf->sf_inaddr) {
833 psf->sf_gsresp = 1;
834 scount++;
835 break;
836 }
837 }
David L Stevensad125832006-01-18 14:20:56 -0800838 if (!scount) {
839 pmc->gsquery = 0;
840 return 0;
841 }
842 pmc->gsquery = 1;
843 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Eric Dumazetd679c532012-09-06 20:37:06 +0000846/* return true if packet was dropped */
847static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 struct ip_mc_list *im;
850
851 /* Timers are only set for non-local groups */
852
853 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000854 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000856 rcu_read_lock();
857 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (im->multiaddr == group) {
859 igmp_stop_timer(im);
860 break;
861 }
862 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000863 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000864 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Eric Dumazetd679c532012-09-06 20:37:06 +0000867/* return true if packet was dropped */
868static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 int len)
870{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300871 struct igmphdr *ih = igmp_hdr(skb);
872 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700874 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 int max_delay;
876 int mark = 0;
877
878
David Stevens5b7c8402010-09-30 14:29:40 +0000879 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (ih->code == 0) {
881 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 max_delay = IGMP_Query_Response_Interval;
884 in_dev->mr_v1_seen = jiffies +
885 IGMP_V1_Router_Present_Timeout;
886 group = 0;
887 } else {
888 /* v2 router present */
889 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
890 in_dev->mr_v2_seen = jiffies +
891 IGMP_V2_Router_Present_Timeout;
892 }
893 /* cancel the interface change timer */
894 in_dev->mr_ifc_count = 0;
895 if (del_timer(&in_dev->mr_ifc_timer))
896 __in_dev_put(in_dev);
897 /* clear deleted report items */
898 igmpv3_clear_delrec(in_dev);
899 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000900 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000901 } else if (IGMP_V1_SEEN(in_dev)) {
902 /* This is a v3 query with v1 queriers present */
903 max_delay = IGMP_Query_Response_Interval;
904 group = 0;
905 } else if (IGMP_V2_SEEN(in_dev)) {
906 /* this is a v3 query with v2 queriers present;
907 * Interpretation of the max_delay code is problematic here.
908 * A real v2 host would use ih_code directly, while v3 has a
909 * different encoding. We use the v3 encoding as more likely
910 * to be intended in a v3 query.
911 */
912 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800913 if (!max_delay)
914 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 } else { /* v3 */
916 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000917 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900918
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300919 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900921 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700922 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000923 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300924 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
927 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
928 if (!max_delay)
929 max_delay = 1; /* can't mod w/ 0 */
930 in_dev->mr_maxdelay = max_delay;
931 if (ih3->qrv)
932 in_dev->mr_qrv = ih3->qrv;
933 if (!group) { /* general query */
934 if (ih3->nsrcs)
Eric Dumazetd679c532012-09-06 20:37:06 +0000935 return false; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000937 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939 /* mark sources to include, if group & source-specific */
940 mark = ih3->nsrcs != 0;
941 }
942
943 /*
944 * - Start the timers in all of our membership records
945 * that the query applies to for the interface on
946 * which the query arrived excl. those that belong
947 * to a "local" group (224.0.0.X)
948 * - For timers already running check if they need to
949 * be reset.
950 * - Use the igmp->igmp_code field as the maximum
951 * delay possible
952 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000953 rcu_read_lock();
954 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -0800955 int changed;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (group && group != im->multiaddr)
958 continue;
959 if (im->multiaddr == IGMP_ALL_HOSTS)
960 continue;
961 spin_lock_bh(&im->lock);
962 if (im->tm_running)
963 im->gsquery = im->gsquery && mark;
964 else
965 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -0800966 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900967 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -0800969 if (changed)
970 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000972 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000973 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000976/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977int igmp_rcv(struct sk_buff *skb)
978{
979 /* This basically follows the spec line by line -- see RFC1112 */
980 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000981 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +0000983 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Denis V. Lunevcd557bc2008-02-09 23:22:26 -0800985 if (in_dev == NULL)
986 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Herbert Xufb286bb2005-11-10 13:01:24 -0800988 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000989 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -0800990
991 switch (skb->ip_summed) {
Patrick McHardy84fa7932006-08-29 16:44:56 -0700992 case CHECKSUM_COMPLETE:
Al Virod3bc23e2006-11-14 21:24:49 -0800993 if (!csum_fold(skb->csum))
Herbert Xufb286bb2005-11-10 13:01:24 -0800994 break;
995 /* fall through */
996 case CHECKSUM_NONE:
997 skb->csum = 0;
998 if (__skb_checksum_complete(skb))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +0000999 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001002 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 switch (ih->type) {
1004 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001005 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 break;
1007 case IGMP_HOST_MEMBERSHIP_REPORT:
1008 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001010 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 break;
David Stevens24c69272005-12-02 20:32:59 -08001012 /* don't rely on MC router hearing unicast reports */
1013 if (skb->pkt_type == PACKET_MULTICAST ||
1014 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001015 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 break;
1017 case IGMP_PIM:
1018#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return pim_rcv_v1(skb);
1020#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001021 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 case IGMP_DVMRP:
1023 case IGMP_TRACE:
1024 case IGMP_HOST_LEAVE_MESSAGE:
1025 case IGMP_MTRACE:
1026 case IGMP_MTRACE_RESP:
1027 break;
1028 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001029 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001031
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001032drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001033 if (dropped)
1034 kfree_skb(skb);
1035 else
1036 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return 0;
1038}
1039
1040#endif
1041
1042
1043/*
1044 * Add a filter to a device
1045 */
1046
Al Viro63007722006-09-27 18:31:32 -07001047static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
1049 char buf[MAX_ADDR_LEN];
1050 struct net_device *dev = in_dev->dev;
1051
1052 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1053 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001054 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 routine. Something sort of:
1056 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1057 --ANK
1058 */
1059 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001060 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063/*
1064 * Remove a filter from a device
1065 */
1066
Al Viro63007722006-09-27 18:31:32 -07001067static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068{
1069 char buf[MAX_ADDR_LEN];
1070 struct net_device *dev = in_dev->dev;
1071
1072 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001073 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
1076#ifdef CONFIG_IP_MULTICAST
1077/*
1078 * deleted ip_mc_list manipulation
1079 */
1080static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1081{
1082 struct ip_mc_list *pmc;
1083
1084 /* this is an "ip_mc_list" for convenience; only the fields below
1085 * are actually used. In particular, the refcnt and users are not
1086 * used for management of the delete list. Using the same structure
1087 * for deleted items allows change reports to use common code with
1088 * non-deleted or query-response MCA's.
1089 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001090 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (!pmc)
1092 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 spin_lock_bh(&im->lock);
1094 pmc->interface = im->interface;
1095 in_dev_hold(in_dev);
1096 pmc->multiaddr = im->multiaddr;
1097 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1098 IGMP_Unsolicited_Report_Count;
1099 pmc->sfmode = im->sfmode;
1100 if (pmc->sfmode == MCAST_INCLUDE) {
1101 struct ip_sf_list *psf;
1102
1103 pmc->tomb = im->tomb;
1104 pmc->sources = im->sources;
1105 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001106 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 psf->sf_crcount = pmc->crcount;
1108 }
1109 spin_unlock_bh(&im->lock);
1110
1111 spin_lock_bh(&in_dev->mc_tomb_lock);
1112 pmc->next = in_dev->mc_tomb;
1113 in_dev->mc_tomb = pmc;
1114 spin_unlock_bh(&in_dev->mc_tomb_lock);
1115}
1116
Al Viro63007722006-09-27 18:31:32 -07001117static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct ip_mc_list *pmc, *pmc_prev;
1120 struct ip_sf_list *psf, *psf_next;
1121
1122 spin_lock_bh(&in_dev->mc_tomb_lock);
1123 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001124 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (pmc->multiaddr == multiaddr)
1126 break;
1127 pmc_prev = pmc;
1128 }
1129 if (pmc) {
1130 if (pmc_prev)
1131 pmc_prev->next = pmc->next;
1132 else
1133 in_dev->mc_tomb = pmc->next;
1134 }
1135 spin_unlock_bh(&in_dev->mc_tomb_lock);
1136 if (pmc) {
Weilong Chenc71151f2013-12-23 14:37:29 +08001137 for (psf = pmc->tomb; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 psf_next = psf->sf_next;
1139 kfree(psf);
1140 }
1141 in_dev_put(pmc->interface);
1142 kfree(pmc);
1143 }
1144}
1145
1146static void igmpv3_clear_delrec(struct in_device *in_dev)
1147{
1148 struct ip_mc_list *pmc, *nextpmc;
1149
1150 spin_lock_bh(&in_dev->mc_tomb_lock);
1151 pmc = in_dev->mc_tomb;
1152 in_dev->mc_tomb = NULL;
1153 spin_unlock_bh(&in_dev->mc_tomb_lock);
1154
1155 for (; pmc; pmc = nextpmc) {
1156 nextpmc = pmc->next;
1157 ip_mc_clear_src(pmc);
1158 in_dev_put(pmc->interface);
1159 kfree(pmc);
1160 }
1161 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001162 rcu_read_lock();
1163 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct ip_sf_list *psf, *psf_next;
1165
1166 spin_lock_bh(&pmc->lock);
1167 psf = pmc->tomb;
1168 pmc->tomb = NULL;
1169 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001170 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 psf_next = psf->sf_next;
1172 kfree(psf);
1173 }
1174 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001175 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177#endif
1178
1179static void igmp_group_dropped(struct ip_mc_list *im)
1180{
1181 struct in_device *in_dev = im->interface;
1182#ifdef CONFIG_IP_MULTICAST
1183 int reporter;
1184#endif
1185
1186 if (im->loaded) {
1187 im->loaded = 0;
1188 ip_mc_filter_del(in_dev, im->multiaddr);
1189 }
1190
1191#ifdef CONFIG_IP_MULTICAST
1192 if (im->multiaddr == IGMP_ALL_HOSTS)
1193 return;
1194
1195 reporter = im->reporter;
1196 igmp_stop_timer(im);
1197
1198 if (!in_dev->dead) {
1199 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001200 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (IGMP_V2_SEEN(in_dev)) {
1202 if (reporter)
1203 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001204 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 /* IGMPv3 */
1207 igmpv3_add_delrec(in_dev, im);
1208
1209 igmp_ifc_event(in_dev);
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
1214static void igmp_group_added(struct ip_mc_list *im)
1215{
1216 struct in_device *in_dev = im->interface;
1217
1218 if (im->loaded == 0) {
1219 im->loaded = 1;
1220 ip_mc_filter_add(in_dev, im->multiaddr);
1221 }
1222
1223#ifdef CONFIG_IP_MULTICAST
1224 if (im->multiaddr == IGMP_ALL_HOSTS)
1225 return;
1226
1227 if (in_dev->dead)
1228 return;
1229 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1230 spin_lock_bh(&im->lock);
1231 igmp_start_timer(im, IGMP_Initial_Report_Delay);
1232 spin_unlock_bh(&im->lock);
1233 return;
1234 }
1235 /* else, v3 */
1236
1237 im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1238 IGMP_Unsolicited_Report_Count;
1239 igmp_ifc_event(in_dev);
1240#endif
1241}
1242
1243
1244/*
1245 * Multicast list managers
1246 */
1247
Eric Dumazete9897072013-06-07 08:48:57 -07001248static u32 ip_mc_hash(const struct ip_mc_list *im)
1249{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001250 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001251}
1252
1253static void ip_mc_hash_add(struct in_device *in_dev,
1254 struct ip_mc_list *im)
1255{
1256 struct ip_mc_list __rcu **mc_hash;
1257 u32 hash;
1258
1259 mc_hash = rtnl_dereference(in_dev->mc_hash);
1260 if (mc_hash) {
1261 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001262 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001263 rcu_assign_pointer(mc_hash[hash], im);
1264 return;
1265 }
1266
1267 /* do not use a hash table for small number of items */
1268 if (in_dev->mc_count < 4)
1269 return;
1270
1271 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1272 GFP_KERNEL);
1273 if (!mc_hash)
1274 return;
1275
1276 for_each_pmc_rtnl(in_dev, im) {
1277 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001278 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001279 RCU_INIT_POINTER(mc_hash[hash], im);
1280 }
1281
1282 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1283}
1284
1285static void ip_mc_hash_remove(struct in_device *in_dev,
1286 struct ip_mc_list *im)
1287{
1288 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1289 struct ip_mc_list *aux;
1290
1291 if (!mc_hash)
1292 return;
1293 mc_hash += ip_mc_hash(im);
1294 while ((aux = rtnl_dereference(*mc_hash)) != im)
1295 mc_hash = &aux->next_hash;
1296 *mc_hash = im->next_hash;
1297}
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300/*
1301 * A socket has joined a multicast group on device dev.
1302 */
1303
Al Viro8f935bb2006-09-27 18:30:07 -07001304void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
1306 struct ip_mc_list *im;
1307
1308 ASSERT_RTNL();
1309
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001310 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (im->multiaddr == addr) {
1312 im->users++;
1313 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1314 goto out;
1315 }
1316 }
1317
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001318 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!im)
1320 goto out;
1321
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001322 im->users = 1;
1323 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001325 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 /* initial mode is (EX, empty) */
1327 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 atomic_set(&im->refcnt, 1);
1330 spin_lock_init(&im->lock);
1331#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001332 setup_timer(&im->timer, &igmp_timer_expire, (unsigned long)im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 im->unsolicit_count = IGMP_Unsolicited_Report_Count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001335
1336 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001337 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001338 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001339
Eric Dumazete9897072013-06-07 08:48:57 -07001340 ip_mc_hash_add(in_dev, im);
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342#ifdef CONFIG_IP_MULTICAST
1343 igmpv3_del_delrec(in_dev, im->multiaddr);
1344#endif
1345 igmp_group_added(im);
1346 if (!in_dev->dead)
1347 ip_rt_multicast_event(in_dev);
1348out:
1349 return;
1350}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001351EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001354 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001355 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001356static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001357{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001358#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001359 struct ip_mc_list *im;
1360 int type;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001361
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001362 ASSERT_RTNL();
1363
1364 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001365 if (im->multiaddr == IGMP_ALL_HOSTS)
1366 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001367
Eric Dumazet866f3b22010-11-18 09:33:19 -08001368 /* a failover is happening and switches
1369 * must be notified immediately
1370 */
1371 if (IGMP_V1_SEEN(in_dev))
1372 type = IGMP_HOST_MEMBERSHIP_REPORT;
1373 else if (IGMP_V2_SEEN(in_dev))
1374 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1375 else
1376 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1377 igmp_send_report(in_dev, im, type);
1378 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001379#endif
1380}
1381
1382/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 * A socket has left a multicast group on device dev
1384 */
1385
Al Viro8f935bb2006-09-27 18:30:07 -07001386void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001388 struct ip_mc_list *i;
1389 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001392
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001393 for (ip = &in_dev->mc_list;
1394 (i = rtnl_dereference(*ip)) != NULL;
1395 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001396 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001398 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001399 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001400 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001402 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 if (!in_dev->dead)
1405 ip_rt_multicast_event(in_dev);
1406
1407 ip_ma_put(i);
1408 return;
1409 }
1410 break;
1411 }
1412 }
1413}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001414EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Moni Shoua75c78502009-09-15 02:37:40 -07001416/* Device changing type */
1417
1418void ip_mc_unmap(struct in_device *in_dev)
1419{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001420 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001421
1422 ASSERT_RTNL();
1423
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001424 for_each_pmc_rtnl(in_dev, pmc)
1425 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001426}
1427
1428void ip_mc_remap(struct in_device *in_dev)
1429{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001430 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001431
1432 ASSERT_RTNL();
1433
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001434 for_each_pmc_rtnl(in_dev, pmc)
1435 igmp_group_added(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/* Device going down */
1439
1440void ip_mc_down(struct in_device *in_dev)
1441{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001442 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
1444 ASSERT_RTNL();
1445
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001446 for_each_pmc_rtnl(in_dev, pmc)
1447 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449#ifdef CONFIG_IP_MULTICAST
1450 in_dev->mr_ifc_count = 0;
1451 if (del_timer(&in_dev->mr_ifc_timer))
1452 __in_dev_put(in_dev);
1453 in_dev->mr_gq_running = 0;
1454 if (del_timer(&in_dev->mr_gq_timer))
1455 __in_dev_put(in_dev);
1456 igmpv3_clear_delrec(in_dev);
1457#endif
1458
1459 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1460}
1461
1462void ip_mc_init_dev(struct in_device *in_dev)
1463{
1464 ASSERT_RTNL();
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001467 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1468 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001469 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1470 (unsigned long)in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1472#endif
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 spin_lock_init(&in_dev->mc_tomb_lock);
1475}
1476
1477/* Device going up */
1478
1479void ip_mc_up(struct in_device *in_dev)
1480{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001481 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 ASSERT_RTNL();
1484
1485 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1486
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001487 for_each_pmc_rtnl(in_dev, pmc)
1488 igmp_group_added(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491/*
1492 * Device is about to be destroyed: clean up.
1493 */
1494
1495void ip_mc_destroy_dev(struct in_device *in_dev)
1496{
1497 struct ip_mc_list *i;
1498
1499 ASSERT_RTNL();
1500
1501 /* Deactivate timers */
1502 ip_mc_down(in_dev);
1503
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001504 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1505 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001506 in_dev->mc_count--;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001507
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001508 /* We've dropped the groups in ip_mc_down already */
1509 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001514/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001515static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 struct net_device *dev = NULL;
1518 struct in_device *idev = NULL;
1519
1520 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001521 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return idev;
1523 }
1524 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001525 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (!dev)
1527 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529
David S. Millerb23dd4f2011-03-02 14:31:35 -08001530 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001531 struct rtable *rt = ip_route_output(net,
1532 imr->imr_multiaddr.s_addr,
1533 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001534 if (!IS_ERR(rt)) {
1535 dev = rt->dst.dev;
1536 ip_rt_put(rt);
1537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539 if (dev) {
1540 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001541 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 return idev;
1544}
1545
1546/*
1547 * Join a socket to a group
1548 */
Brian Haleyab32ea52006-09-22 14:15:41 -07001549int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS;
1550int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552
1553static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001554 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 struct ip_sf_list *psf, *psf_prev;
1557 int rv = 0;
1558
1559 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001560 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (psf->sf_inaddr == *psfsrc)
1562 break;
1563 psf_prev = psf;
1564 }
1565 if (!psf || psf->sf_count[sfmode] == 0) {
1566 /* source filter not found, or count wrong => bug */
1567 return -ESRCH;
1568 }
1569 psf->sf_count[sfmode]--;
1570 if (psf->sf_count[sfmode] == 0) {
1571 ip_rt_multicast_event(pmc->interface);
1572 }
1573 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1574#ifdef CONFIG_IP_MULTICAST
1575 struct in_device *in_dev = pmc->interface;
1576#endif
1577
1578 /* no more filters for this source */
1579 if (psf_prev)
1580 psf_prev->sf_next = psf->sf_next;
1581 else
1582 pmc->sources = psf->sf_next;
1583#ifdef CONFIG_IP_MULTICAST
1584 if (psf->sf_oldin &&
1585 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001586 psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 IGMP_Unsolicited_Report_Count;
1588 psf->sf_next = pmc->tomb;
1589 pmc->tomb = psf;
1590 rv = 1;
1591 } else
1592#endif
1593 kfree(psf);
1594 }
1595 return rv;
1596}
1597
1598#ifndef CONFIG_IP_MULTICAST
1599#define igmp_ifc_event(x) do { } while (0)
1600#endif
1601
Al Viro8f935bb2006-09-27 18:30:07 -07001602static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1603 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 struct ip_mc_list *pmc;
1606 int changerec = 0;
1607 int i, err;
1608
1609 if (!in_dev)
1610 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001611 rcu_read_lock();
1612 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (*pmca == pmc->multiaddr)
1614 break;
1615 }
1616 if (!pmc) {
1617 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001618 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return -ESRCH;
1620 }
1621 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001622 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623#ifdef CONFIG_IP_MULTICAST
1624 sf_markstate(pmc);
1625#endif
1626 if (!delta) {
1627 err = -EINVAL;
1628 if (!pmc->sfcount[sfmode])
1629 goto out_unlock;
1630 pmc->sfcount[sfmode]--;
1631 }
1632 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001633 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1635
1636 changerec |= rv > 0;
1637 if (!err && rv < 0)
1638 err = rv;
1639 }
1640 if (pmc->sfmode == MCAST_EXCLUDE &&
1641 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1642 pmc->sfcount[MCAST_INCLUDE]) {
1643#ifdef CONFIG_IP_MULTICAST
1644 struct ip_sf_list *psf;
1645#endif
1646
1647 /* filter mode change */
1648 pmc->sfmode = MCAST_INCLUDE;
1649#ifdef CONFIG_IP_MULTICAST
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001650 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 IGMP_Unsolicited_Report_Count;
1652 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001653 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 psf->sf_crcount = 0;
1655 igmp_ifc_event(pmc->interface);
1656 } else if (sf_setstate(pmc) || changerec) {
1657 igmp_ifc_event(pmc->interface);
1658#endif
1659 }
1660out_unlock:
1661 spin_unlock_bh(&pmc->lock);
1662 return err;
1663}
1664
1665/*
1666 * Add multicast single-source filter to the interface list
1667 */
1668static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001669 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct ip_sf_list *psf, *psf_prev;
1672
1673 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001674 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (psf->sf_inaddr == *psfsrc)
1676 break;
1677 psf_prev = psf;
1678 }
1679 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001680 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (!psf)
1682 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 psf->sf_inaddr = *psfsrc;
1684 if (psf_prev) {
1685 psf_prev->sf_next = psf;
1686 } else
1687 pmc->sources = psf;
1688 }
1689 psf->sf_count[sfmode]++;
1690 if (psf->sf_count[sfmode] == 1) {
1691 ip_rt_multicast_event(pmc->interface);
1692 }
1693 return 0;
1694}
1695
1696#ifdef CONFIG_IP_MULTICAST
1697static void sf_markstate(struct ip_mc_list *pmc)
1698{
1699 struct ip_sf_list *psf;
1700 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1701
Weilong Chenc71151f2013-12-23 14:37:29 +08001702 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 if (pmc->sfcount[MCAST_EXCLUDE]) {
1704 psf->sf_oldin = mca_xcount ==
1705 psf->sf_count[MCAST_EXCLUDE] &&
1706 !psf->sf_count[MCAST_INCLUDE];
1707 } else
1708 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1709}
1710
1711static int sf_setstate(struct ip_mc_list *pmc)
1712{
David L Stevensad125832006-01-18 14:20:56 -08001713 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1715 int qrv = pmc->interface->mr_qrv;
1716 int new_in, rv;
1717
1718 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001719 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (pmc->sfcount[MCAST_EXCLUDE]) {
1721 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1722 !psf->sf_count[MCAST_INCLUDE];
1723 } else
1724 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001725 if (new_in) {
1726 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001727 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001728
Weilong Chenc71151f2013-12-23 14:37:29 +08001729 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001730 if (dpsf->sf_inaddr == psf->sf_inaddr)
1731 break;
1732 prev = dpsf;
1733 }
1734 if (dpsf) {
1735 if (prev)
1736 prev->sf_next = dpsf->sf_next;
1737 else
1738 pmc->tomb = dpsf->sf_next;
1739 kfree(dpsf);
1740 }
1741 psf->sf_crcount = qrv;
1742 rv++;
1743 }
1744 } else if (psf->sf_oldin) {
1745
1746 psf->sf_crcount = 0;
1747 /*
1748 * add or update "delete" records if an active filter
1749 * is now inactive
1750 */
Weilong Chenc71151f2013-12-23 14:37:29 +08001751 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08001752 if (dpsf->sf_inaddr == psf->sf_inaddr)
1753 break;
1754 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00001755 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08001756 if (!dpsf)
1757 continue;
1758 *dpsf = *psf;
1759 /* pmc->lock held by callers */
1760 dpsf->sf_next = pmc->tomb;
1761 pmc->tomb = dpsf;
1762 }
1763 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 rv++;
1765 }
1766 }
1767 return rv;
1768}
1769#endif
1770
1771/*
1772 * Add multicast source filter list to the interface list
1773 */
Al Viro8f935bb2006-09-27 18:30:07 -07001774static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1775 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 struct ip_mc_list *pmc;
1778 int isexclude;
1779 int i, err;
1780
1781 if (!in_dev)
1782 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001783 rcu_read_lock();
1784 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (*pmca == pmc->multiaddr)
1786 break;
1787 }
1788 if (!pmc) {
1789 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001790 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 return -ESRCH;
1792 }
1793 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001794 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796#ifdef CONFIG_IP_MULTICAST
1797 sf_markstate(pmc);
1798#endif
1799 isexclude = pmc->sfmode == MCAST_EXCLUDE;
1800 if (!delta)
1801 pmc->sfcount[sfmode]++;
1802 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001803 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00001804 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (err)
1806 break;
1807 }
1808 if (err) {
1809 int j;
1810
Jun Zhao685f94e62011-11-22 17:19:03 +00001811 if (!delta)
1812 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08001813 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00001814 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1816#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct ip_sf_list *psf;
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07001818 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819#endif
1820
1821 /* filter mode change */
1822 if (pmc->sfcount[MCAST_EXCLUDE])
1823 pmc->sfmode = MCAST_EXCLUDE;
1824 else if (pmc->sfcount[MCAST_INCLUDE])
1825 pmc->sfmode = MCAST_INCLUDE;
1826#ifdef CONFIG_IP_MULTICAST
1827 /* else no filters; keep old mode for reports */
1828
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001829 pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 IGMP_Unsolicited_Report_Count;
1831 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001832 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 psf->sf_crcount = 0;
1834 igmp_ifc_event(in_dev);
1835 } else if (sf_setstate(pmc)) {
1836 igmp_ifc_event(in_dev);
1837#endif
1838 }
1839 spin_unlock_bh(&pmc->lock);
1840 return err;
1841}
1842
1843static void ip_mc_clear_src(struct ip_mc_list *pmc)
1844{
1845 struct ip_sf_list *psf, *nextpsf;
1846
Weilong Chenc71151f2013-12-23 14:37:29 +08001847 for (psf = pmc->tomb; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 nextpsf = psf->sf_next;
1849 kfree(psf);
1850 }
1851 pmc->tomb = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001852 for (psf = pmc->sources; psf; psf = nextpsf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 nextpsf = psf->sf_next;
1854 kfree(psf);
1855 }
1856 pmc->sources = NULL;
1857 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07001858 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 pmc->sfcount[MCAST_EXCLUDE] = 1;
1860}
1861
1862
1863/*
1864 * Join a multicast group
1865 */
1866int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1867{
1868 int err;
Al Viro8f935bb2006-09-27 18:30:07 -07001869 __be32 addr = imr->imr_multiaddr.s_addr;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001870 struct ip_mc_socklist *iml = NULL, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct in_device *in_dev;
1872 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07001873 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07001874 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 int count = 0;
1876
Joe Perchesf97c1e02007-12-16 13:45:43 -08001877 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return -EINVAL;
1879
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001880 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Daniel Lezcano877aced2008-08-13 16:15:57 -07001882 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 if (!in_dev) {
1885 iml = NULL;
1886 err = -ENODEV;
1887 goto done;
1888 }
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07001891 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001892 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07001893 if (i->multi.imr_multiaddr.s_addr == addr &&
1894 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 count++;
1897 }
1898 err = -ENOBUFS;
David L Stevensca9b9072005-07-08 17:38:07 -07001899 if (count >= sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001901 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
David L Stevensca9b9072005-07-08 17:38:07 -07001902 if (iml == NULL)
1903 goto done;
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001906 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 iml->sflist = NULL;
1908 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001909 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08001913 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return err;
1915}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001916EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1919 struct in_device *in_dev)
1920{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001921 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 int err;
1923
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001924 if (psf == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /* any-source empty exclude case */
1926 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1927 iml->sfmode, 0, NULL, 0);
1928 }
1929 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001930 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001931 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001932 /* decrease mem now to avoid the memleak warning */
1933 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08001934 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return err;
1936}
1937
1938/*
1939 * Ask a socket to leave a group.
1940 */
1941
1942int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1943{
1944 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001945 struct ip_mc_socklist *iml;
1946 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07001947 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001948 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07001949 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07001950 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07001951 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 rtnl_lock();
Daniel Lezcano877aced2008-08-13 16:15:57 -07001954 in_dev = ip_mc_find_dev(net, imr);
David L Stevens84b42ba2005-07-08 17:48:38 -07001955 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001956 for (imlp = &inet->mc_list;
1957 (iml = rtnl_dereference(*imlp)) != NULL;
1958 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07001959 if (iml->multi.imr_multiaddr.s_addr != group)
1960 continue;
1961 if (ifindex) {
1962 if (iml->multi.imr_ifindex != ifindex)
1963 continue;
1964 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
1965 iml->multi.imr_address.s_addr)
1966 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
David L Stevensacd6e002006-08-17 16:27:39 -07001968 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001970 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07001971
1972 if (in_dev)
David L Stevens84b42ba2005-07-08 17:48:38 -07001973 ip_mc_dec_group(in_dev, group);
David L Stevensacd6e002006-08-17 16:27:39 -07001974 rtnl_unlock();
Flavio Leitnerc85bb412010-02-02 07:32:29 -08001975 /* decrease mem now to avoid the memleak warning */
1976 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08001977 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07001978 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
David L Stevensacd6e002006-08-17 16:27:39 -07001980 if (!in_dev)
1981 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 rtnl_unlock();
David L Stevensacd6e002006-08-17 16:27:39 -07001983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
stephen hemminger193ba922012-10-01 12:32:34 +00001985EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987int ip_mc_source(int add, int omode, struct sock *sk, struct
1988 ip_mreq_source *mreqs, int ifindex)
1989{
1990 int err;
1991 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07001992 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 struct ip_mc_socklist *pmc;
1994 struct in_device *in_dev = NULL;
1995 struct inet_sock *inet = inet_sk(sk);
1996 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07001997 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07001998 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int i, j, rv;
2000
Joe Perchesf97c1e02007-12-16 13:45:43 -08002001 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return -EINVAL;
2003
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002004 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
2006 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2007 imr.imr_address.s_addr = mreqs->imr_interface;
2008 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002009 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
2011 if (!in_dev) {
2012 err = -ENODEV;
2013 goto done;
2014 }
2015 err = -EADDRNOTAVAIL;
2016
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002017 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002018 if ((pmc->multi.imr_multiaddr.s_addr ==
2019 imr.imr_multiaddr.s_addr) &&
2020 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 break;
2022 }
David L Stevens917f2f12005-07-08 17:45:16 -07002023 if (!pmc) { /* must have a prior join */
2024 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /* if a source filter was set, must be the same mode as before */
2028 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002029 if (pmc->sfmode != omode) {
2030 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 } else if (pmc->sfmode != omode) {
2034 /* allow mode switches for empty-set filters */
2035 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002036 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 NULL, 0);
2038 pmc->sfmode = omode;
2039 }
2040
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002041 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (!add) {
2043 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002044 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002046 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002048 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (rv == 0)
2050 break;
2051 }
2052 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002053 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
David L Stevens8cdaaa12005-07-08 17:39:23 -07002055 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2056 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2057 leavegroup = 1;
2058 goto done;
2059 }
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002062 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 &mreqs->imr_sourceaddr, 1);
2064
Weilong Chenc71151f2013-12-23 14:37:29 +08002065 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 psl->sl_addr[j-1] = psl->sl_addr[j];
2067 psl->sl_count--;
2068 err = 0;
2069 goto done;
2070 }
2071 /* else, add a new source to the filter */
2072
2073 if (psl && psl->sl_count >= sysctl_igmp_max_msf) {
2074 err = -ENOBUFS;
2075 goto done;
2076 }
2077 if (!psl || psl->sl_count == psl->sl_max) {
2078 struct ip_sf_socklist *newpsl;
2079 int count = IP_SFBLOCK;
2080
2081 if (psl)
2082 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002083 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (!newpsl) {
2085 err = -ENOBUFS;
2086 goto done;
2087 }
2088 newpsl->sl_max = count;
2089 newpsl->sl_count = count - IP_SFBLOCK;
2090 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002091 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002093 /* decrease mem now to avoid the memleak warning */
2094 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002095 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002097 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002098 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002101 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002103 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (rv == 0)
2105 break;
2106 }
2107 if (rv == 0) /* address already there is an error */
2108 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002109 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 psl->sl_addr[j+1] = psl->sl_addr[j];
2111 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2112 psl->sl_count++;
2113 err = 0;
2114 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002115 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 &mreqs->imr_sourceaddr, 1);
2117done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002118 rtnl_unlock();
David L Stevens8cdaaa12005-07-08 17:39:23 -07002119 if (leavegroup)
2120 return ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return err;
2122}
2123
2124int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2125{
David L Stevens9951f032005-07-08 17:47:28 -07002126 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002128 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 struct ip_mc_socklist *pmc;
2130 struct in_device *in_dev;
2131 struct inet_sock *inet = inet_sk(sk);
2132 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002133 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002134 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Joe Perchesf97c1e02007-12-16 13:45:43 -08002136 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return -EINVAL;
2138 if (msf->imsf_fmode != MCAST_INCLUDE &&
2139 msf->imsf_fmode != MCAST_EXCLUDE)
2140 return -EINVAL;
2141
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002142 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2145 imr.imr_address.s_addr = msf->imsf_interface;
2146 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002147 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 if (!in_dev) {
2150 err = -ENODEV;
2151 goto done;
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
David L Stevens9951f032005-07-08 17:47:28 -07002154 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2155 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2156 leavegroup = 1;
2157 goto done;
2158 }
2159
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002160 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2162 pmc->multi.imr_ifindex == imr.imr_ifindex)
2163 break;
2164 }
David L Stevens917f2f12005-07-08 17:45:16 -07002165 if (!pmc) { /* must have a prior join */
2166 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002170 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2171 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (!newpsl) {
2173 err = -ENOBUFS;
2174 goto done;
2175 }
2176 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2177 memcpy(newpsl->sl_addr, msf->imsf_slist,
2178 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2179 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2180 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2181 if (err) {
2182 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2183 goto done;
2184 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002185 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002187 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2188 msf->imsf_fmode, 0, NULL, 0);
2189 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002190 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (psl) {
2192 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2193 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002194 /* decrease mem now to avoid the memleak warning */
2195 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002196 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 } else
2198 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2199 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002200 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002202 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002204 rtnl_unlock();
David L Stevens9951f032005-07-08 17:47:28 -07002205 if (leavegroup)
2206 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return err;
2208}
2209
2210int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2211 struct ip_msfilter __user *optval, int __user *optlen)
2212{
2213 int err, len, count, copycount;
2214 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002215 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 struct ip_mc_socklist *pmc;
2217 struct in_device *in_dev;
2218 struct inet_sock *inet = inet_sk(sk);
2219 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002220 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Joe Perchesf97c1e02007-12-16 13:45:43 -08002222 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 return -EINVAL;
2224
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002225 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2228 imr.imr_address.s_addr = msf->imsf_interface;
2229 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002230 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 if (!in_dev) {
2233 err = -ENODEV;
2234 goto done;
2235 }
2236 err = -EADDRNOTAVAIL;
2237
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002238 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2240 pmc->multi.imr_ifindex == imr.imr_ifindex)
2241 break;
2242 }
2243 if (!pmc) /* must have a prior join */
2244 goto done;
2245 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002246 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002247 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 if (!psl) {
2249 len = 0;
2250 count = 0;
2251 } else {
2252 count = psl->sl_count;
2253 }
2254 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2255 len = copycount * sizeof(psl->sl_addr[0]);
2256 msf->imsf_numsrc = count;
2257 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2258 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2259 return -EFAULT;
2260 }
2261 if (len &&
2262 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2263 return -EFAULT;
2264 return 0;
2265done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002266 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return err;
2268}
2269
2270int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2271 struct group_filter __user *optval, int __user *optlen)
2272{
2273 int err, i, count, copycount;
2274 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002275 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 struct ip_mc_socklist *pmc;
2277 struct inet_sock *inet = inet_sk(sk);
2278 struct ip_sf_socklist *psl;
2279
2280 psin = (struct sockaddr_in *)&gsf->gf_group;
2281 if (psin->sin_family != AF_INET)
2282 return -EINVAL;
2283 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002284 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return -EINVAL;
2286
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002287 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 err = -EADDRNOTAVAIL;
2290
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002291 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2293 pmc->multi.imr_ifindex == gsf->gf_interface)
2294 break;
2295 }
2296 if (!pmc) /* must have a prior join */
2297 goto done;
2298 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002299 psl = rtnl_dereference(pmc->sflist);
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002300 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 count = psl ? psl->sl_count : 0;
2302 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2303 gsf->gf_numsrc = count;
2304 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2305 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2306 return -EFAULT;
2307 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002308 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 struct sockaddr_storage ss;
2310
2311 psin = (struct sockaddr_in *)&ss;
2312 memset(&ss, 0, sizeof(ss));
2313 psin->sin_family = AF_INET;
2314 psin->sin_addr.s_addr = psl->sl_addr[i];
2315 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2316 return -EFAULT;
2317 }
2318 return 0;
2319done:
Stephen Hemminger6756ae42006-03-20 22:23:58 -08002320 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return err;
2322}
2323
2324/*
2325 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2326 */
Al Viroc0cda062006-09-27 18:31:10 -07002327int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
2329 struct inet_sock *inet = inet_sk(sk);
2330 struct ip_mc_socklist *pmc;
2331 struct ip_sf_socklist *psl;
2332 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002333 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002335 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002336 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002337 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002339 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002340 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2342 pmc->multi.imr_ifindex == dif)
2343 break;
2344 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002345 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002347 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002348 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002349 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002351 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Weilong Chenc71151f2013-12-23 14:37:29 +08002353 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (psl->sl_addr[i] == rmt_addr)
2355 break;
2356 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002357 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002359 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002361 goto unlock;
2362 ret = 1;
2363unlock:
2364 rcu_read_unlock();
2365out:
2366 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367}
2368
2369/*
2370 * A socket is closing.
2371 */
2372
2373void ip_mc_drop_socket(struct sock *sk)
2374{
2375 struct inet_sock *inet = inet_sk(sk);
2376 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002377 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 if (inet->mc_list == NULL)
2380 return;
2381
2382 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002383 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002386 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002387 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002388 (void) ip_mc_leave_src(sk, iml, in_dev);
Eric Dumazet18943d22010-11-08 11:15:54 +00002389 if (in_dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002391 /* decrease mem now to avoid the memleak warning */
2392 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002393 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395 rtnl_unlock();
2396}
2397
David S. Millerdbdd9a52011-03-10 16:34:38 -08002398/* called with rcu_read_lock() */
2399int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u16 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002402 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 struct ip_sf_list *psf;
2404 int rv = 0;
2405
Eric Dumazete9897072013-06-07 08:48:57 -07002406 mc_hash = rcu_dereference(in_dev->mc_hash);
2407 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002408 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002409
2410 for (im = rcu_dereference(mc_hash[hash]);
2411 im != NULL;
2412 im = rcu_dereference(im->next_hash)) {
2413 if (im->multiaddr == mc_addr)
2414 break;
2415 }
2416 } else {
2417 for_each_pmc_rcu(in_dev, im) {
2418 if (im->multiaddr == mc_addr)
2419 break;
2420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
2422 if (im && proto == IPPROTO_IGMP) {
2423 rv = 1;
2424 } else if (im) {
2425 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002426 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (psf->sf_inaddr == src_addr)
2428 break;
2429 }
2430 if (psf)
2431 rv = psf->sf_count[MCAST_INCLUDE] ||
2432 psf->sf_count[MCAST_EXCLUDE] !=
2433 im->sfcount[MCAST_EXCLUDE];
2434 else
2435 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2436 } else
2437 rv = 1; /* unspecified source; tentatively allow */
2438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return rv;
2440}
2441
2442#if defined(CONFIG_PROC_FS)
2443struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002444 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct net_device *dev;
2446 struct in_device *in_dev;
2447};
2448
2449#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2450
2451static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2452{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002453 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 struct ip_mc_list *im = NULL;
2455 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2456
Pavel Emelianov7562f872007-05-03 15:13:45 -07002457 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002458 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002460
2461 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (!in_dev)
2463 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002464 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (im) {
2466 state->in_dev = in_dev;
2467 break;
2468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 }
2470 return im;
2471}
2472
2473static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2474{
2475 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002476
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002477 im = rcu_dereference(im->next_rcu);
2478 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002479 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if (!state->dev) {
2481 state->in_dev = NULL;
2482 break;
2483 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002484 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (!state->in_dev)
2486 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002487 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489 return im;
2490}
2491
2492static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2493{
2494 struct ip_mc_list *im = igmp_mc_get_first(seq);
2495 if (im)
2496 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2497 --pos;
2498 return pos ? NULL : im;
2499}
2500
2501static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002502 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
stephen hemminger61fbab72009-11-10 07:54:55 +00002504 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2506}
2507
2508static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2509{
2510 struct ip_mc_list *im;
2511 if (v == SEQ_START_TOKEN)
2512 im = igmp_mc_get_first(seq);
2513 else
2514 im = igmp_mc_get_next(seq, v);
2515 ++*pos;
2516 return im;
2517}
2518
2519static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002520 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002523
2524 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002526 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527}
2528
2529static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2530{
2531 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002532 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2534 else {
2535 struct ip_mc_list *im = (struct ip_mc_list *)v;
2536 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2537 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002538 long delta;
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540#ifdef CONFIG_IP_MULTICAST
2541 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2542 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2543 "V3";
2544#else
2545 querier = "NONE";
2546#endif
2547
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002548 if (rcu_dereference(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002550 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552
Eric Dumazeta399a802012-08-08 21:13:53 +00002553 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002555 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002557 im->tm_running,
2558 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 im->reporter);
2560 }
2561 return 0;
2562}
2563
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002564static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 .start = igmp_mc_seq_start,
2566 .next = igmp_mc_seq_next,
2567 .stop = igmp_mc_seq_stop,
2568 .show = igmp_mc_seq_show,
2569};
2570
2571static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2572{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002573 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002574 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Arjan van de Ven9a321442007-02-12 00:55:35 -08002577static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 .owner = THIS_MODULE,
2579 .open = igmp_mc_seq_open,
2580 .read = seq_read,
2581 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002582 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583};
2584
2585struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002586 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 struct net_device *dev;
2588 struct in_device *idev;
2589 struct ip_mc_list *im;
2590};
2591
2592#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2593
2594static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2595{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002596 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct ip_sf_list *psf = NULL;
2598 struct ip_mc_list *im = NULL;
2599 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2600
Pavel Emelianov7562f872007-05-03 15:13:45 -07002601 state->idev = NULL;
2602 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002603 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002605 idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (unlikely(idev == NULL))
2607 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002608 im = rcu_dereference(idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (likely(im != NULL)) {
2610 spin_lock_bh(&im->lock);
2611 psf = im->sources;
2612 if (likely(psf != NULL)) {
2613 state->im = im;
2614 state->idev = idev;
2615 break;
2616 }
2617 spin_unlock_bh(&im->lock);
2618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620 return psf;
2621}
2622
2623static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2624{
2625 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2626
2627 psf = psf->sf_next;
2628 while (!psf) {
2629 spin_unlock_bh(&state->im->lock);
2630 state->im = state->im->next;
2631 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002632 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if (!state->dev) {
2634 state->idev = NULL;
2635 goto out;
2636 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002637 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 if (!state->idev)
2639 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002640 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 }
2642 if (!state->im)
2643 break;
2644 spin_lock_bh(&state->im->lock);
2645 psf = state->im->sources;
2646 }
2647out:
2648 return psf;
2649}
2650
2651static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2652{
2653 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2654 if (psf)
2655 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2656 --pos;
2657 return pos ? NULL : psf;
2658}
2659
2660static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002661 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
stephen hemminger61fbab72009-11-10 07:54:55 +00002663 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2665}
2666
2667static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2668{
2669 struct ip_sf_list *psf;
2670 if (v == SEQ_START_TOKEN)
2671 psf = igmp_mcf_get_first(seq);
2672 else
2673 psf = igmp_mcf_get_next(seq, v);
2674 ++*pos;
2675 return psf;
2676}
2677
2678static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002679 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
2681 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2682 if (likely(state->im != NULL)) {
2683 spin_unlock_bh(&state->im->lock);
2684 state->im = NULL;
2685 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002686 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002688 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
2691static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2692{
2693 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2694 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2695
2696 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002697 seq_printf(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 "%3s %6s "
2699 "%10s %10s %6s %6s\n", "Idx",
2700 "Device", "MCA",
2701 "SRC", "INC", "EXC");
2702 } else {
2703 seq_printf(seq,
2704 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002705 "0x%08x %6lu %6lu\n",
2706 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 ntohl(state->im->multiaddr),
2708 ntohl(psf->sf_inaddr),
2709 psf->sf_count[MCAST_INCLUDE],
2710 psf->sf_count[MCAST_EXCLUDE]);
2711 }
2712 return 0;
2713}
2714
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002715static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 .start = igmp_mcf_seq_start,
2717 .next = igmp_mcf_seq_next,
2718 .stop = igmp_mcf_seq_stop,
2719 .show = igmp_mcf_seq_show,
2720};
2721
2722static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2723{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002724 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002725 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
Arjan van de Ven9a321442007-02-12 00:55:35 -08002728static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 .owner = THIS_MODULE,
2730 .open = igmp_mcf_seq_open,
2731 .read = seq_read,
2732 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002733 .release = seq_release_net,
2734};
2735
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002736static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002737{
2738 struct proc_dir_entry *pde;
2739
Gao fengd4beaa62013-02-18 01:34:54 +00002740 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002741 if (!pde)
2742 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002743 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2744 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002745 if (!pde)
2746 goto out_mcfilter;
2747 return 0;
2748
2749out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00002750 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002751out_igmp:
2752 return -ENOMEM;
2753}
2754
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002755static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002756{
Gao fengece31ff2013-02-18 01:34:56 +00002757 remove_proc_entry("mcfilter", net->proc_net);
2758 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002759}
2760
2761static struct pernet_operations igmp_net_ops = {
2762 .init = igmp_net_init,
2763 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764};
WANG Cong72c1d3b2014-01-10 16:09:45 -08002765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002767static int igmp_netdev_event(struct notifier_block *this,
2768 unsigned long event, void *ptr)
2769{
2770 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2771 struct in_device *in_dev;
2772
2773 switch (event) {
2774 case NETDEV_RESEND_IGMP:
2775 in_dev = __in_dev_get_rtnl(dev);
2776 if (in_dev)
2777 ip_mc_rejoin_groups(in_dev);
2778 break;
2779 default:
2780 break;
2781 }
2782 return NOTIFY_DONE;
2783}
2784
2785static struct notifier_block igmp_notifier = {
2786 .notifier_call = igmp_netdev_event,
2787};
2788
WANG Cong72c1d3b2014-01-10 16:09:45 -08002789int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
WANG Cong72c1d3b2014-01-10 16:09:45 -08002791#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02002792 int err;
2793
2794 err = register_pernet_subsys(&igmp_net_ops);
2795 if (err)
2796 return err;
2797 err = register_netdevice_notifier(&igmp_notifier);
2798 if (err)
2799 goto reg_notif_fail;
2800 return 0;
2801
2802reg_notif_fail:
2803 unregister_pernet_subsys(&igmp_net_ops);
2804 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08002805#else
2806 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08002808}