blob: 7f5fe07d0b13afe762c587ac49294e0a8ccb2894 [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>
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -080092#include <linux/byteorder/generic.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020093
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020094#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020095#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <net/ip.h>
97#include <net/protocol.h>
98#include <net/route.h>
99#include <net/sock.h>
100#include <net/checksum.h>
Madhu Challa93a714d2015-02-25 09:58:35 -0800101#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <linux/netfilter_ipv4.h>
103#ifdef CONFIG_IP_MROUTE
104#include <linux/mroute.h>
105#endif
106#ifdef CONFIG_PROC_FS
107#include <linux/proc_fs.h>
108#include <linux/seq_file.h>
109#endif
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#ifdef CONFIG_IP_MULTICAST
112/* Parameter names and values are taken from igmp-v2-06 draft */
113
Fabian Frederick436f7c22014-11-04 20:52:14 +0100114#define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
115#define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
116#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
117#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
118#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
119#define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121
Fabian Frederick436f7c22014-11-04 20:52:14 +0100122#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Fabian Frederick436f7c22014-11-04 20:52:14 +0100124/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
129 */
130
Herbert Xu42f811b2007-06-04 23:34:44 -0700131#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
William Manleycab70042013-08-06 19:03:13 +0100142static int unsolicited_report_interval(struct in_device *in_dev)
143{
William Manley26900482013-08-06 19:03:15 +0100144 int interval_ms, interval_jiffies;
145
William Manleycab70042013-08-06 19:03:13 +0100146 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100147 interval_ms = IN_DEV_CONF_GET(
148 in_dev,
149 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100150 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100151 interval_ms = IN_DEV_CONF_GET(
152 in_dev,
153 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
154
155 interval_jiffies = msecs_to_jiffies(interval_ms);
156
157 /* _timer functions can't handle a delay of 0 jiffies so ensure
158 * we always return a positive value.
159 */
160 if (interval_jiffies <= 0)
161 interval_jiffies = 1;
162 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Hangbin Liu24803f32016-11-14 16:16:28 +0800166static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void igmpv3_clear_delrec(struct in_device *in_dev);
168static int sf_setstate(struct ip_mc_list *pmc);
169static void sf_markstate(struct ip_mc_list *pmc);
170#endif
171static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700172static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
173 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175static void ip_ma_put(struct ip_mc_list *im)
176{
177 if (atomic_dec_and_test(&im->refcnt)) {
178 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800179 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181}
182
David S. Millerd9aa9382010-11-15 08:52:02 -0800183#define for_each_pmc_rcu(in_dev, pmc) \
184 for (pmc = rcu_dereference(in_dev->mc_list); \
185 pmc != NULL; \
186 pmc = rcu_dereference(pmc->next_rcu))
187
188#define for_each_pmc_rtnl(in_dev, pmc) \
189 for (pmc = rtnl_dereference(in_dev->mc_list); \
190 pmc != NULL; \
191 pmc = rtnl_dereference(pmc->next_rcu))
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193#ifdef CONFIG_IP_MULTICAST
194
195/*
196 * Timer management
197 */
198
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000199static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 spin_lock_bh(&im->lock);
202 if (del_timer(&im->timer))
203 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800204 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 im->reporter = 0;
206 im->unsolicit_count = 0;
207 spin_unlock_bh(&im->lock);
208}
209
210/* It must be called with locked im->lock */
211static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
212{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500213 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800215 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!mod_timer(&im->timer, jiffies+tv+2))
217 atomic_inc(&im->refcnt);
218}
219
220static void igmp_gq_start_timer(struct in_device *in_dev)
221{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500222 int tv = prandom_u32() % in_dev->mr_maxdelay;
Michal Tesarfe1e13c2017-01-02 14:38:36 +0100223 unsigned long exp = jiffies + tv + 2;
224
225 if (in_dev->mr_gq_running &&
226 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
227 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 in_dev->mr_gq_running = 1;
Michal Tesarfe1e13c2017-01-02 14:38:36 +0100230 if (!mod_timer(&in_dev->mr_gq_timer, exp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 in_dev_hold(in_dev);
232}
233
234static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
235{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500236 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
239 in_dev_hold(in_dev);
240}
241
242static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
243{
244 spin_lock_bh(&im->lock);
245 im->unsolicit_count = 0;
246 if (del_timer(&im->timer)) {
247 if ((long)(im->timer.expires-jiffies) < max_delay) {
248 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800249 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 spin_unlock_bh(&im->lock);
251 return;
252 }
253 atomic_dec(&im->refcnt);
254 }
255 igmp_start_timer(im, max_delay);
256 spin_unlock_bh(&im->lock);
257}
258
259
260/*
261 * Send an IGMP report.
262 */
263
264#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
265
266
267static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
268 int gdeleted, int sdeleted)
269{
270 switch (type) {
271 case IGMPV3_MODE_IS_INCLUDE:
272 case IGMPV3_MODE_IS_EXCLUDE:
273 if (gdeleted || sdeleted)
274 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800275 if (!(pmc->gsquery && !psf->sf_gsresp)) {
276 if (pmc->sfmode == MCAST_INCLUDE)
277 return 1;
278 /* don't include if this source is excluded
279 * in all filters
280 */
281 if (psf->sf_count[MCAST_INCLUDE])
282 return type == IGMPV3_MODE_IS_INCLUDE;
283 return pmc->sfcount[MCAST_EXCLUDE] ==
284 psf->sf_count[MCAST_EXCLUDE];
285 }
286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 case IGMPV3_CHANGE_TO_INCLUDE:
288 if (gdeleted || sdeleted)
289 return 0;
290 return psf->sf_count[MCAST_INCLUDE] != 0;
291 case IGMPV3_CHANGE_TO_EXCLUDE:
292 if (gdeleted || sdeleted)
293 return 0;
294 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
295 psf->sf_count[MCAST_INCLUDE])
296 return 0;
297 return pmc->sfcount[MCAST_EXCLUDE] ==
298 psf->sf_count[MCAST_EXCLUDE];
299 case IGMPV3_ALLOW_NEW_SOURCES:
300 if (gdeleted || !psf->sf_crcount)
301 return 0;
302 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
303 case IGMPV3_BLOCK_OLD_SOURCES:
304 if (pmc->sfmode == MCAST_INCLUDE)
305 return gdeleted || (psf->sf_crcount && sdeleted);
306 return psf->sf_crcount && !gdeleted && !sdeleted;
307 }
308 return 0;
309}
310
311static int
312igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
313{
314 struct ip_sf_list *psf;
315 int scount = 0;
316
Weilong Chenc71151f2013-12-23 14:37:29 +0800317 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
319 continue;
320 scount++;
321 }
322 return scount;
323}
324
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800325/* source address selection per RFC 3376 section 4.2.13 */
326static __be32 igmpv3_get_srcaddr(struct net_device *dev,
327 const struct flowi4 *fl4)
328{
329 struct in_device *in_dev = __in_dev_get_rcu(dev);
330
331 if (!in_dev)
332 return htonl(INADDR_ANY);
333
334 for_ifa(in_dev) {
Felix Fietkau0ae16962018-01-19 11:50:46 +0100335 if (fl4->saddr == ifa->ifa_local)
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800336 return fl4->saddr;
337 } endfor_ifa(in_dev);
338
339 return htonl(INADDR_ANY);
340}
341
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100342static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 struct sk_buff *skb;
345 struct rtable *rt;
346 struct iphdr *pip;
347 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700348 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700349 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000350 int hlen = LL_RESERVED_SPACE(dev);
351 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100352 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000354 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000355 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000356 GFP_ATOMIC | __GFP_NOWARN);
357 if (skb)
358 break;
359 size >>= 1;
360 if (size < 256)
361 return NULL;
362 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200363 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
David S. Miller31e45432011-05-03 20:25:42 -0700365 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500366 0, 0,
367 IPPROTO_IGMP, 0, dev->ifindex);
368 if (IS_ERR(rt)) {
369 kfree_skb(skb);
370 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Changli Gaod8d1f302010-06-10 23:31:35 -0700373 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 skb->dev = dev;
375
Herbert Xu66088242011-11-18 02:20:04 +0000376 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800377 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300379 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700380 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300381 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 pip->version = 4;
384 pip->ihl = (sizeof(struct iphdr)+4)>>2;
385 pip->tos = 0xc0;
386 pip->frag_off = htons(IP_DF);
387 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700388 pip->daddr = fl4.daddr;
Eric Dumazetce43c072018-02-01 10:26:57 -0800389
390 rcu_read_lock();
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800391 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
Eric Dumazetce43c072018-02-01 10:26:57 -0800392 rcu_read_unlock();
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 pip->protocol = IPPROTO_IGMP;
395 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100396 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000397 ((u8 *)&pip[1])[0] = IPOPT_RA;
398 ((u8 *)&pip[1])[1] = 4;
399 ((u8 *)&pip[1])[2] = 0;
400 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700402 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300403 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300404 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
406 pig->resv1 = 0;
407 pig->csum = 0;
408 pig->resv2 = 0;
409 pig->ngrec = 0;
410 return skb;
411}
412
413static int igmpv3_sendpack(struct sk_buff *skb)
414{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300415 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000416 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300418 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Eric W. Biederman33224b12015-10-07 16:48:46 -0500420 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
423static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
424{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800425 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
428static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800429 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 struct net_device *dev = pmc->interface->dev;
432 struct igmpv3_report *pih;
433 struct igmpv3_grec *pgr;
434
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800435 if (!skb) {
436 skb = igmpv3_newpack(dev, mtu);
437 if (!skb)
438 return NULL;
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
441 pgr->grec_type = type;
442 pgr->grec_auxwords = 0;
443 pgr->grec_nsrcs = 0;
444 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300445 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 pih->ngrec = htons(ntohs(pih->ngrec)+1);
447 *ppgr = pgr;
448 return skb;
449}
450
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100451#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
454 int type, int gdeleted, int sdeleted)
455{
456 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200457 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct igmpv3_report *pih;
459 struct igmpv3_grec *pgr = NULL;
460 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800461 int scount, stotal, first, isquery, truncate;
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800462 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (pmc->multiaddr == IGMP_ALL_HOSTS)
465 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200466 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100467 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800469 mtu = READ_ONCE(dev->mtu);
470 if (mtu < IPV4_MIN_MTU)
471 return skb;
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
474 type == IGMPV3_MODE_IS_EXCLUDE;
475 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
476 type == IGMPV3_CHANGE_TO_EXCLUDE;
477
David L Stevensad125832006-01-18 14:20:56 -0800478 stotal = scount = 0;
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
481
David L Stevensad125832006-01-18 14:20:56 -0800482 if (!*psf_list)
483 goto empty_source;
484
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300485 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* EX and TO_EX get a fresh packet, if needed */
488 if (truncate) {
489 if (pih && pih->ngrec &&
490 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
491 if (skb)
492 igmpv3_sendpack(skb);
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800493 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495 }
496 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800498 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700499 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 psf_next = psf->sf_next;
502
503 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
504 psf_prev = psf;
505 continue;
506 }
507
Hangbin Liua0525172016-08-02 18:02:57 +0800508 /* Based on RFC3376 5.1. Should not send source-list change
509 * records when there is a filter mode change.
510 */
511 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
512 (!gdeleted && pmc->crcount)) &&
513 (type == IGMPV3_ALLOW_NEW_SOURCES ||
514 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
515 goto decrease_sf_crcount;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* clear marks on query responses */
518 if (isquery)
519 psf->sf_gsresp = 0;
520
Al Viro63007722006-09-27 18:31:32 -0700521 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 first*sizeof(struct igmpv3_grec)) {
523 if (truncate && !first)
524 break; /* truncate these */
525 if (pgr)
526 pgr->grec_nsrcs = htons(scount);
527 if (skb)
528 igmpv3_sendpack(skb);
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800529 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 first = 1;
531 scount = 0;
532 }
533 if (first) {
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800534 skb = add_grhead(skb, pmc, type, &pgr, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 first = 0;
536 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800537 if (!skb)
538 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700539 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800541 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
543 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
Hangbin Liua0525172016-08-02 18:02:57 +0800544decrease_sf_crcount:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 psf->sf_crcount--;
546 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
547 if (psf_prev)
548 psf_prev->sf_next = psf->sf_next;
549 else
550 *psf_list = psf->sf_next;
551 kfree(psf);
552 continue;
553 }
554 }
555 psf_prev = psf;
556 }
David L Stevensad125832006-01-18 14:20:56 -0800557
558empty_source:
559 if (!stotal) {
560 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
561 type == IGMPV3_BLOCK_OLD_SOURCES)
562 return skb;
563 if (pmc->crcount || isquery) {
564 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800565 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800566 igmpv3_sendpack(skb);
567 skb = NULL; /* add_grhead will get a new one */
568 }
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800569 skb = add_grhead(skb, pmc, type, &pgr, mtu);
David L Stevensad125832006-01-18 14:20:56 -0800570 }
571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (pgr)
573 pgr->grec_nsrcs = htons(scount);
574
575 if (isquery)
576 pmc->gsquery = 0; /* clear query state on report */
577 return skb;
578}
579
580static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
581{
582 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200583 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 int type;
585
586 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000587 rcu_read_lock();
588 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (pmc->multiaddr == IGMP_ALL_HOSTS)
590 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100591 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200592 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100593 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 spin_lock_bh(&pmc->lock);
595 if (pmc->sfcount[MCAST_EXCLUDE])
596 type = IGMPV3_MODE_IS_EXCLUDE;
597 else
598 type = IGMPV3_MODE_IS_INCLUDE;
599 skb = add_grec(skb, pmc, type, 0, 0);
600 spin_unlock_bh(&pmc->lock);
601 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000602 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 } else {
604 spin_lock_bh(&pmc->lock);
605 if (pmc->sfcount[MCAST_EXCLUDE])
606 type = IGMPV3_MODE_IS_EXCLUDE;
607 else
608 type = IGMPV3_MODE_IS_INCLUDE;
609 skb = add_grec(skb, pmc, type, 0, 0);
610 spin_unlock_bh(&pmc->lock);
611 }
612 if (!skb)
613 return 0;
614 return igmpv3_sendpack(skb);
615}
616
617/*
618 * remove zero-count source records from a source filter list
619 */
620static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
621{
622 struct ip_sf_list *psf_prev, *psf_next, *psf;
623
624 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800625 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 psf_next = psf->sf_next;
627 if (psf->sf_crcount == 0) {
628 if (psf_prev)
629 psf_prev->sf_next = psf->sf_next;
630 else
631 *ppsf = psf->sf_next;
632 kfree(psf);
633 } else
634 psf_prev = psf;
635 }
636}
637
638static void igmpv3_send_cr(struct in_device *in_dev)
639{
640 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
641 struct sk_buff *skb = NULL;
642 int type, dtype;
643
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000644 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 spin_lock_bh(&in_dev->mc_tomb_lock);
646
647 /* deleted MCA's */
648 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800649 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 pmc_next = pmc->next;
651 if (pmc->sfmode == MCAST_INCLUDE) {
652 type = IGMPV3_BLOCK_OLD_SOURCES;
653 dtype = IGMPV3_BLOCK_OLD_SOURCES;
654 skb = add_grec(skb, pmc, type, 1, 0);
655 skb = add_grec(skb, pmc, dtype, 1, 1);
656 }
657 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (pmc->sfmode == MCAST_EXCLUDE) {
659 type = IGMPV3_CHANGE_TO_INCLUDE;
660 skb = add_grec(skb, pmc, type, 1, 0);
661 }
David L Stevensad125832006-01-18 14:20:56 -0800662 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 if (pmc->crcount == 0) {
664 igmpv3_clear_zeros(&pmc->tomb);
665 igmpv3_clear_zeros(&pmc->sources);
666 }
667 }
668 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
669 if (pmc_prev)
670 pmc_prev->next = pmc_next;
671 else
672 in_dev->mc_tomb = pmc_next;
673 in_dev_put(pmc->interface);
674 kfree(pmc);
675 } else
676 pmc_prev = pmc;
677 }
678 spin_unlock_bh(&in_dev->mc_tomb_lock);
679
680 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000681 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 spin_lock_bh(&pmc->lock);
683 if (pmc->sfcount[MCAST_EXCLUDE]) {
684 type = IGMPV3_BLOCK_OLD_SOURCES;
685 dtype = IGMPV3_ALLOW_NEW_SOURCES;
686 } else {
687 type = IGMPV3_ALLOW_NEW_SOURCES;
688 dtype = IGMPV3_BLOCK_OLD_SOURCES;
689 }
690 skb = add_grec(skb, pmc, type, 0, 0);
691 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
692
693 /* filter mode changes */
694 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (pmc->sfmode == MCAST_EXCLUDE)
696 type = IGMPV3_CHANGE_TO_EXCLUDE;
697 else
698 type = IGMPV3_CHANGE_TO_INCLUDE;
699 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800700 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702 spin_unlock_bh(&pmc->lock);
703 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000704 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 if (!skb)
707 return;
708 (void) igmpv3_sendpack(skb);
709}
710
711static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
712 int type)
713{
714 struct sk_buff *skb;
715 struct iphdr *iph;
716 struct igmphdr *ih;
717 struct rtable *rt;
718 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700719 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700720 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700721 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700722 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000723 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
726 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100727
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200728 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100729 return 0;
730
731 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 dst = IGMP_ALL_ROUTER;
733 else
734 dst = group;
735
David S. Miller31e45432011-05-03 20:25:42 -0700736 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500737 0, 0,
738 IPPROTO_IGMP, 0, dev->ifindex);
739 if (IS_ERR(rt))
740 return -1;
741
Herbert Xu66088242011-11-18 02:20:04 +0000742 hlen = LL_RESERVED_SPACE(dev);
743 tlen = dev->needed_tailroom;
744 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100745 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 ip_rt_put(rt);
747 return -1;
748 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200749 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Changli Gaod8d1f302010-06-10 23:31:35 -0700751 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Herbert Xu66088242011-11-18 02:20:04 +0000753 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300755 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700756 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300757 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 iph->version = 4;
760 iph->ihl = (sizeof(struct iphdr)+4)>>2;
761 iph->tos = 0xc0;
762 iph->frag_off = htons(IP_DF);
763 iph->ttl = 1;
764 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700765 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100767 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000768 ((u8 *)&iph[1])[0] = IPOPT_RA;
769 ((u8 *)&iph[1])[1] = 4;
770 ((u8 *)&iph[1])[2] = 0;
771 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800774 ih->type = type;
775 ih->code = 0;
776 ih->csum = 0;
777 ih->group = group;
778 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Eric W. Biederman33224b12015-10-07 16:48:46 -0500780 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
783static void igmp_gq_timer_expire(unsigned long data)
784{
785 struct in_device *in_dev = (struct in_device *)data;
786
787 in_dev->mr_gq_running = 0;
788 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700789 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
792static void igmp_ifc_timer_expire(unsigned long data)
793{
794 struct in_device *in_dev = (struct in_device *)data;
795
796 igmpv3_send_cr(in_dev);
797 if (in_dev->mr_ifc_count) {
798 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100799 igmp_ifc_start_timer(in_dev,
800 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700802 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805static void igmp_ifc_event(struct in_device *in_dev)
806{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200807 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
809 return;
Nikolay Borisov165094a2016-02-08 23:29:24 +0200810 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 igmp_ifc_start_timer(in_dev, 1);
812}
813
814
815static void igmp_timer_expire(unsigned long data)
816{
Weilong Chenc71151f2013-12-23 14:37:29 +0800817 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 struct in_device *in_dev = im->interface;
819
820 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800821 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (im->unsolicit_count) {
824 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100825 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
827 im->reporter = 1;
828 spin_unlock(&im->lock);
829
830 if (IGMP_V1_SEEN(in_dev))
831 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
832 else if (IGMP_V2_SEEN(in_dev))
833 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
834 else
835 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
836
837 ip_ma_put(im);
838}
839
David L Stevensad125832006-01-18 14:20:56 -0800840/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700841static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 struct ip_sf_list *psf;
844 int i, scount;
845
846 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800847 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (scount == nsrcs)
849 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800850 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800851 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000852 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800853 pmc->sfcount[MCAST_EXCLUDE] !=
854 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800855 break;
David L Stevensad125832006-01-18 14:20:56 -0800856 if (srcs[i] == psf->sf_inaddr) {
857 scount++;
858 break;
859 }
860 }
861 }
862 pmc->gsquery = 0;
863 if (scount == nsrcs) /* all sources excluded */
864 return 0;
865 return 1;
866}
867
Al Viroea4d9e72006-09-27 18:30:52 -0700868static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800869{
870 struct ip_sf_list *psf;
871 int i, scount;
872
873 if (pmc->sfmode == MCAST_EXCLUDE)
874 return igmp_xmarksources(pmc, nsrcs, srcs);
875
876 /* mark INCLUDE-mode sources */
877 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800878 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800879 if (scount == nsrcs)
880 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800881 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (srcs[i] == psf->sf_inaddr) {
883 psf->sf_gsresp = 1;
884 scount++;
885 break;
886 }
887 }
David L Stevensad125832006-01-18 14:20:56 -0800888 if (!scount) {
889 pmc->gsquery = 0;
890 return 0;
891 }
892 pmc->gsquery = 1;
893 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Eric Dumazetd679c532012-09-06 20:37:06 +0000896/* return true if packet was dropped */
897static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
899 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200900 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* Timers are only set for non-local groups */
903
904 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000905 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200906 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100907 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000909 rcu_read_lock();
910 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (im->multiaddr == group) {
912 igmp_stop_timer(im);
913 break;
914 }
915 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000916 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000917 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Eric Dumazetd679c532012-09-06 20:37:06 +0000920/* return true if packet was dropped */
921static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int len)
923{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300924 struct igmphdr *ih = igmp_hdr(skb);
925 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700927 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 int max_delay;
929 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200930 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932
David Stevens5b7c8402010-09-30 14:29:40 +0000933 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (ih->code == 0) {
935 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900936
Fabian Frederick436f7c22014-11-04 20:52:14 +0100937 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 in_dev->mr_v1_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100939 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 group = 0;
941 } else {
942 /* v2 router present */
943 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
944 in_dev->mr_v2_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100945 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 /* cancel the interface change timer */
948 in_dev->mr_ifc_count = 0;
949 if (del_timer(&in_dev->mr_ifc_timer))
950 __in_dev_put(in_dev);
951 /* clear deleted report items */
952 igmpv3_clear_delrec(in_dev);
953 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000954 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000955 } else if (IGMP_V1_SEEN(in_dev)) {
956 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100957 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000958 group = 0;
959 } else if (IGMP_V2_SEEN(in_dev)) {
960 /* this is a v3 query with v2 queriers present;
961 * Interpretation of the max_delay code is problematic here.
962 * A real v2 host would use ih_code directly, while v3 has a
963 * different encoding. We use the v3 encoding as more likely
964 * to be intended in a v3 query.
965 */
966 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800967 if (!max_delay)
968 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 } else { /* v3 */
970 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000971 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900972
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300973 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900975 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700976 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000977 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300978 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980
981 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
982 if (!max_delay)
983 max_delay = 1; /* can't mod w/ 0 */
984 in_dev->mr_maxdelay = max_delay;
985 if (ih3->qrv)
986 in_dev->mr_qrv = ih3->qrv;
987 if (!group) { /* general query */
988 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +0200989 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000991 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 /* mark sources to include, if group & source-specific */
994 mark = ih3->nsrcs != 0;
995 }
996
997 /*
998 * - Start the timers in all of our membership records
999 * that the query applies to for the interface on
1000 * which the query arrived excl. those that belong
1001 * to a "local" group (224.0.0.X)
1002 * - For timers already running check if they need to
1003 * be reset.
1004 * - Use the igmp->igmp_code field as the maximum
1005 * delay possible
1006 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001007 rcu_read_lock();
1008 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -08001009 int changed;
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 if (group && group != im->multiaddr)
1012 continue;
1013 if (im->multiaddr == IGMP_ALL_HOSTS)
1014 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001015 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001016 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001017 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 spin_lock_bh(&im->lock);
1019 if (im->tm_running)
1020 im->gsquery = im->gsquery && mark;
1021 else
1022 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -08001023 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001024 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -08001026 if (changed)
1027 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001029 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +00001030 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001033/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034int igmp_rcv(struct sk_buff *skb)
1035{
1036 /* This basically follows the spec line by line -- see RFC1112 */
1037 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001038 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001040 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ian Morris51456b22015-04-03 09:17:26 +01001042 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001043 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Herbert Xufb286bb2005-11-10 13:01:24 -08001045 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001046 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001047
Tom Herbertde08dc12014-05-07 16:52:10 -07001048 if (skb_checksum_simple_validate(skb))
1049 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001051 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 switch (ih->type) {
1053 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001054 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 break;
1056 case IGMP_HOST_MEMBERSHIP_REPORT:
1057 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001059 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 break;
David Stevens24c69272005-12-02 20:32:59 -08001061 /* don't rely on MC router hearing unicast reports */
1062 if (skb->pkt_type == PACKET_MULTICAST ||
1063 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001064 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 break;
1066 case IGMP_PIM:
1067#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return pim_rcv_v1(skb);
1069#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001070 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 case IGMP_DVMRP:
1072 case IGMP_TRACE:
1073 case IGMP_HOST_LEAVE_MESSAGE:
1074 case IGMP_MTRACE:
1075 case IGMP_MTRACE_RESP:
1076 break;
1077 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001080
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001081drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001082 if (dropped)
1083 kfree_skb(skb);
1084 else
1085 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return 0;
1087}
1088
1089#endif
1090
1091
1092/*
1093 * Add a filter to a device
1094 */
1095
Al Viro63007722006-09-27 18:31:32 -07001096static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 char buf[MAX_ADDR_LEN];
1099 struct net_device *dev = in_dev->dev;
1100
1101 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1102 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001103 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 routine. Something sort of:
1105 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1106 --ANK
1107 */
1108 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001109 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
1112/*
1113 * Remove a filter from a device
1114 */
1115
Al Viro63007722006-09-27 18:31:32 -07001116static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 char buf[MAX_ADDR_LEN];
1119 struct net_device *dev = in_dev->dev;
1120
1121 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001122 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125#ifdef CONFIG_IP_MULTICAST
1126/*
1127 * deleted ip_mc_list manipulation
1128 */
1129static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1130{
1131 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001132 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 /* this is an "ip_mc_list" for convenience; only the fields below
1135 * are actually used. In particular, the refcnt and users are not
1136 * used for management of the delete list. Using the same structure
1137 * for deleted items allows change reports to use common code with
1138 * non-deleted or query-response MCA's.
1139 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001140 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!pmc)
1142 return;
WANG Congcac2a9b2017-06-20 10:46:27 -07001143 spin_lock_init(&pmc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 spin_lock_bh(&im->lock);
1145 pmc->interface = im->interface;
1146 in_dev_hold(in_dev);
1147 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001148 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 pmc->sfmode = im->sfmode;
1150 if (pmc->sfmode == MCAST_INCLUDE) {
1151 struct ip_sf_list *psf;
1152
1153 pmc->tomb = im->tomb;
1154 pmc->sources = im->sources;
1155 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001156 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 psf->sf_crcount = pmc->crcount;
1158 }
1159 spin_unlock_bh(&im->lock);
1160
1161 spin_lock_bh(&in_dev->mc_tomb_lock);
1162 pmc->next = in_dev->mc_tomb;
1163 in_dev->mc_tomb = pmc;
1164 spin_unlock_bh(&in_dev->mc_tomb_lock);
1165}
1166
Hangbin Liu24803f32016-11-14 16:16:28 +08001167/*
1168 * restore ip_mc_list deleted records
1169 */
1170static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001173 struct ip_sf_list *psf;
1174 struct net *net = dev_net(in_dev->dev);
1175 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 spin_lock_bh(&in_dev->mc_tomb_lock);
1178 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001179 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (pmc->multiaddr == multiaddr)
1181 break;
1182 pmc_prev = pmc;
1183 }
1184 if (pmc) {
1185 if (pmc_prev)
1186 pmc_prev->next = pmc->next;
1187 else
1188 in_dev->mc_tomb = pmc->next;
1189 }
1190 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001191
1192 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001194 im->interface = pmc->interface;
1195 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1196 im->sfmode = pmc->sfmode;
1197 if (pmc->sfmode == MCAST_INCLUDE) {
1198 im->tomb = pmc->tomb;
1199 im->sources = pmc->sources;
1200 for (psf = im->sources; psf; psf = psf->sf_next)
1201 psf->sf_crcount = im->crcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 in_dev_put(pmc->interface);
Hangbin Liu16a3fbe2017-02-08 21:16:45 +08001204 kfree(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001206 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
Hangbin Liu24803f32016-11-14 16:16:28 +08001209/*
1210 * flush ip_mc_list deleted records
1211 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212static void igmpv3_clear_delrec(struct in_device *in_dev)
1213{
1214 struct ip_mc_list *pmc, *nextpmc;
1215
1216 spin_lock_bh(&in_dev->mc_tomb_lock);
1217 pmc = in_dev->mc_tomb;
1218 in_dev->mc_tomb = NULL;
1219 spin_unlock_bh(&in_dev->mc_tomb_lock);
1220
1221 for (; pmc; pmc = nextpmc) {
1222 nextpmc = pmc->next;
1223 ip_mc_clear_src(pmc);
1224 in_dev_put(pmc->interface);
1225 kfree(pmc);
1226 }
1227 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001228 rcu_read_lock();
1229 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 struct ip_sf_list *psf, *psf_next;
1231
1232 spin_lock_bh(&pmc->lock);
1233 psf = pmc->tomb;
1234 pmc->tomb = NULL;
1235 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001236 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 psf_next = psf->sf_next;
1238 kfree(psf);
1239 }
1240 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001241 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243#endif
1244
1245static void igmp_group_dropped(struct ip_mc_list *im)
1246{
1247 struct in_device *in_dev = im->interface;
1248#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001249 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 int reporter;
1251#endif
1252
1253 if (im->loaded) {
1254 im->loaded = 0;
1255 ip_mc_filter_del(in_dev, im->multiaddr);
1256 }
1257
1258#ifdef CONFIG_IP_MULTICAST
1259 if (im->multiaddr == IGMP_ALL_HOSTS)
1260 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001261 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001262 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 reporter = im->reporter;
1265 igmp_stop_timer(im);
1266
1267 if (!in_dev->dead) {
1268 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001269 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (IGMP_V2_SEEN(in_dev)) {
1271 if (reporter)
1272 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001273 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 /* IGMPv3 */
1276 igmpv3_add_delrec(in_dev, im);
1277
1278 igmp_ifc_event(in_dev);
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283static void igmp_group_added(struct ip_mc_list *im)
1284{
1285 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001286#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001287 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001288#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 if (im->loaded == 0) {
1291 im->loaded = 1;
1292 ip_mc_filter_add(in_dev, im->multiaddr);
1293 }
1294
1295#ifdef CONFIG_IP_MULTICAST
1296 if (im->multiaddr == IGMP_ALL_HOSTS)
1297 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001298 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 if (in_dev->dead)
1302 return;
1303 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1304 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001305 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 spin_unlock_bh(&im->lock);
1307 return;
1308 }
1309 /* else, v3 */
1310
Nikolay Borisov165094a2016-02-08 23:29:24 +02001311 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 igmp_ifc_event(in_dev);
1313#endif
1314}
1315
1316
1317/*
1318 * Multicast list managers
1319 */
1320
Eric Dumazete9897072013-06-07 08:48:57 -07001321static u32 ip_mc_hash(const struct ip_mc_list *im)
1322{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001323 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001324}
1325
1326static void ip_mc_hash_add(struct in_device *in_dev,
1327 struct ip_mc_list *im)
1328{
1329 struct ip_mc_list __rcu **mc_hash;
1330 u32 hash;
1331
1332 mc_hash = rtnl_dereference(in_dev->mc_hash);
1333 if (mc_hash) {
1334 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001335 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001336 rcu_assign_pointer(mc_hash[hash], im);
1337 return;
1338 }
1339
1340 /* do not use a hash table for small number of items */
1341 if (in_dev->mc_count < 4)
1342 return;
1343
1344 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1345 GFP_KERNEL);
1346 if (!mc_hash)
1347 return;
1348
1349 for_each_pmc_rtnl(in_dev, im) {
1350 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001351 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001352 RCU_INIT_POINTER(mc_hash[hash], im);
1353 }
1354
1355 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1356}
1357
1358static void ip_mc_hash_remove(struct in_device *in_dev,
1359 struct ip_mc_list *im)
1360{
1361 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1362 struct ip_mc_list *aux;
1363
1364 if (!mc_hash)
1365 return;
1366 mc_hash += ip_mc_hash(im);
1367 while ((aux = rtnl_dereference(*mc_hash)) != im)
1368 mc_hash = &aux->next_hash;
1369 *mc_hash = im->next_hash;
1370}
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373/*
1374 * A socket has joined a multicast group on device dev.
1375 */
1376
Al Viro8f935bb2006-09-27 18:30:07 -07001377void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct ip_mc_list *im;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001380#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001381 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 ASSERT_RTNL();
1385
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001386 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 if (im->multiaddr == addr) {
1388 im->users++;
1389 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1390 goto out;
1391 }
1392 }
1393
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001394 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (!im)
1396 goto out;
1397
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001398 im->users = 1;
1399 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001401 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /* initial mode is (EX, empty) */
1403 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 atomic_set(&im->refcnt, 1);
1406 spin_lock_init(&im->lock);
1407#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301408 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001409 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001411
1412 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001413 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001414 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001415
Eric Dumazete9897072013-06-07 08:48:57 -07001416 ip_mc_hash_add(in_dev, im);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001419 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420#endif
1421 igmp_group_added(im);
1422 if (!in_dev->dead)
1423 ip_rt_multicast_event(in_dev);
1424out:
1425 return;
1426}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001427EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001429static int ip_mc_check_iphdr(struct sk_buff *skb)
1430{
1431 const struct iphdr *iph;
1432 unsigned int len;
1433 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1434
1435 if (!pskb_may_pull(skb, offset))
1436 return -EINVAL;
1437
1438 iph = ip_hdr(skb);
1439
1440 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1441 return -EINVAL;
1442
1443 offset += ip_hdrlen(skb) - sizeof(*iph);
1444
1445 if (!pskb_may_pull(skb, offset))
1446 return -EINVAL;
1447
1448 iph = ip_hdr(skb);
1449
1450 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1451 return -EINVAL;
1452
1453 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1454 if (skb->len < len || len < offset)
1455 return -EINVAL;
1456
1457 skb_set_transport_header(skb, offset);
1458
1459 return 0;
1460}
1461
1462static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1463{
1464 unsigned int len = skb_transport_offset(skb);
1465
1466 len += sizeof(struct igmpv3_report);
1467
1468 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1469}
1470
1471static int ip_mc_check_igmp_query(struct sk_buff *skb)
1472{
1473 unsigned int len = skb_transport_offset(skb);
1474
1475 len += sizeof(struct igmphdr);
1476 if (skb->len < len)
1477 return -EINVAL;
1478
1479 /* IGMPv{1,2}? */
1480 if (skb->len != len) {
1481 /* or IGMPv3? */
1482 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1483 if (skb->len < len || !pskb_may_pull(skb, len))
1484 return -EINVAL;
1485 }
1486
1487 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1488 * all-systems destination addresses (224.0.0.1) for general queries
1489 */
1490 if (!igmp_hdr(skb)->group &&
1491 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1492 return -EINVAL;
1493
1494 return 0;
1495}
1496
1497static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1498{
1499 switch (igmp_hdr(skb)->type) {
1500 case IGMP_HOST_LEAVE_MESSAGE:
1501 case IGMP_HOST_MEMBERSHIP_REPORT:
1502 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1503 /* fall through */
1504 return 0;
1505 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1506 return ip_mc_check_igmp_reportv3(skb);
1507 case IGMP_HOST_MEMBERSHIP_QUERY:
1508 return ip_mc_check_igmp_query(skb);
1509 default:
1510 return -ENOMSG;
1511 }
1512}
1513
1514static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1515{
1516 return skb_checksum_simple_validate(skb);
1517}
1518
1519static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1520
1521{
1522 struct sk_buff *skb_chk;
1523 unsigned int transport_len;
1524 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga5169932015-08-13 05:54:07 +02001525 int ret = -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001526
1527 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1528
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001529 skb_chk = skb_checksum_trimmed(skb, transport_len,
1530 ip_mc_validate_checksum);
1531 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02001532 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001533
Linus Lüssinga5169932015-08-13 05:54:07 +02001534 if (!pskb_may_pull(skb_chk, len))
1535 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001536
1537 ret = ip_mc_check_igmp_msg(skb_chk);
Linus Lüssinga5169932015-08-13 05:54:07 +02001538 if (ret)
1539 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001540
1541 if (skb_trimmed)
1542 *skb_trimmed = skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02001543 /* free now unneeded clone */
1544 else if (skb_chk != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001545 kfree_skb(skb_chk);
1546
Linus Lüssinga5169932015-08-13 05:54:07 +02001547 ret = 0;
1548
1549err:
1550 if (ret && skb_chk && skb_chk != skb)
1551 kfree_skb(skb_chk);
1552
1553 return ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001554}
1555
1556/**
1557 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1558 * @skb: the skb to validate
1559 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1560 *
1561 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001562 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001563 *
1564 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1565 * standard
1566 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1567 * -ENOMEM: A memory allocation failure happened.
1568 *
1569 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1570 * to NULL): After parsing an IGMP packet successfully it will point to
1571 * an skb which has its tail aligned to the IP packet end. This might
1572 * either be the originally provided skb or a trimmed, cloned version if
1573 * the skb frame had data beyond the IP packet. A cloned skb allows us
1574 * to leave the original skb and its full frame unchanged (which might be
1575 * desirable for layer 2 frame jugglers).
1576 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001577 * Caller needs to set the skb network header and free any returned skb if it
1578 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001579 */
1580int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1581{
1582 int ret = ip_mc_check_iphdr(skb);
1583
1584 if (ret < 0)
1585 return ret;
1586
1587 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1588 return -ENOMSG;
1589
1590 return __ip_mc_check_igmp(skb, skb_trimmed);
1591}
1592EXPORT_SYMBOL(ip_mc_check_igmp);
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001595 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001596 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001597static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001598{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001599#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001600 struct ip_mc_list *im;
1601 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001602 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001603
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001604 ASSERT_RTNL();
1605
1606 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001607 if (im->multiaddr == IGMP_ALL_HOSTS)
1608 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001609 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001610 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001611 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001612
Eric Dumazet866f3b22010-11-18 09:33:19 -08001613 /* a failover is happening and switches
1614 * must be notified immediately
1615 */
1616 if (IGMP_V1_SEEN(in_dev))
1617 type = IGMP_HOST_MEMBERSHIP_REPORT;
1618 else if (IGMP_V2_SEEN(in_dev))
1619 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1620 else
1621 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1622 igmp_send_report(in_dev, im, type);
1623 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001624#endif
1625}
1626
1627/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * A socket has left a multicast group on device dev
1629 */
1630
Al Viro8f935bb2006-09-27 18:30:07 -07001631void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001633 struct ip_mc_list *i;
1634 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001637
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001638 for (ip = &in_dev->mc_list;
1639 (i = rtnl_dereference(*ip)) != NULL;
1640 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001641 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001643 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001644 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001645 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001647 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 if (!in_dev->dead)
1650 ip_rt_multicast_event(in_dev);
1651
1652 ip_ma_put(i);
1653 return;
1654 }
1655 break;
1656 }
1657 }
1658}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001659EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Moni Shoua75c78502009-09-15 02:37:40 -07001661/* Device changing type */
1662
1663void ip_mc_unmap(struct in_device *in_dev)
1664{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001665 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001666
1667 ASSERT_RTNL();
1668
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001669 for_each_pmc_rtnl(in_dev, pmc)
1670 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001671}
1672
1673void ip_mc_remap(struct in_device *in_dev)
1674{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001675 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001676
1677 ASSERT_RTNL();
1678
Hangbin Liu24803f32016-11-14 16:16:28 +08001679 for_each_pmc_rtnl(in_dev, pmc) {
1680#ifdef CONFIG_IP_MULTICAST
1681 igmpv3_del_delrec(in_dev, pmc);
1682#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001683 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001684 }
Moni Shoua75c78502009-09-15 02:37:40 -07001685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687/* Device going down */
1688
1689void ip_mc_down(struct in_device *in_dev)
1690{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001691 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 ASSERT_RTNL();
1694
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001695 for_each_pmc_rtnl(in_dev, pmc)
1696 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698#ifdef CONFIG_IP_MULTICAST
1699 in_dev->mr_ifc_count = 0;
1700 if (del_timer(&in_dev->mr_ifc_timer))
1701 __in_dev_put(in_dev);
1702 in_dev->mr_gq_running = 0;
1703 if (del_timer(&in_dev->mr_gq_timer))
1704 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705#endif
1706
1707 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1708}
1709
1710void ip_mc_init_dev(struct in_device *in_dev)
1711{
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001712#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001713 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 ASSERT_RTNL();
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001718 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1719 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001720 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1721 (unsigned long)in_dev);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001722 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723#endif
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 spin_lock_init(&in_dev->mc_tomb_lock);
1726}
1727
1728/* Device going up */
1729
1730void ip_mc_up(struct in_device *in_dev)
1731{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001732 struct ip_mc_list *pmc;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001733#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001734 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001735#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 ASSERT_RTNL();
1738
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001739#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001740 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001741#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1743
Hangbin Liu24803f32016-11-14 16:16:28 +08001744 for_each_pmc_rtnl(in_dev, pmc) {
1745#ifdef CONFIG_IP_MULTICAST
1746 igmpv3_del_delrec(in_dev, pmc);
1747#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001748 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752/*
1753 * Device is about to be destroyed: clean up.
1754 */
1755
1756void ip_mc_destroy_dev(struct in_device *in_dev)
1757{
1758 struct ip_mc_list *i;
1759
1760 ASSERT_RTNL();
1761
1762 /* Deactivate timers */
1763 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001764#ifdef CONFIG_IP_MULTICAST
1765 igmpv3_clear_delrec(in_dev);
1766#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001768 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1769 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001770 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001775/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001776static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct net_device *dev = NULL;
1779 struct in_device *idev = NULL;
1780
1781 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001782 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return idev;
1784 }
1785 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001786 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 if (!dev)
1788 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790
David S. Millerb23dd4f2011-03-02 14:31:35 -08001791 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001792 struct rtable *rt = ip_route_output(net,
1793 imr->imr_multiaddr.s_addr,
1794 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001795 if (!IS_ERR(rt)) {
1796 dev = rt->dst.dev;
1797 ip_rt_put(rt);
1798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800 if (dev) {
1801 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001802 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804 return idev;
1805}
1806
1807/*
1808 * Join a socket to a group
1809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001812 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
1814 struct ip_sf_list *psf, *psf_prev;
1815 int rv = 0;
1816
1817 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001818 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (psf->sf_inaddr == *psfsrc)
1820 break;
1821 psf_prev = psf;
1822 }
1823 if (!psf || psf->sf_count[sfmode] == 0) {
1824 /* source filter not found, or count wrong => bug */
1825 return -ESRCH;
1826 }
1827 psf->sf_count[sfmode]--;
1828 if (psf->sf_count[sfmode] == 0) {
1829 ip_rt_multicast_event(pmc->interface);
1830 }
1831 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1832#ifdef CONFIG_IP_MULTICAST
1833 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001834 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835#endif
1836
1837 /* no more filters for this source */
1838 if (psf_prev)
1839 psf_prev->sf_next = psf->sf_next;
1840 else
1841 pmc->sources = psf->sf_next;
1842#ifdef CONFIG_IP_MULTICAST
1843 if (psf->sf_oldin &&
1844 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001845 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 psf->sf_next = pmc->tomb;
1847 pmc->tomb = psf;
1848 rv = 1;
1849 } else
1850#endif
1851 kfree(psf);
1852 }
1853 return rv;
1854}
1855
1856#ifndef CONFIG_IP_MULTICAST
1857#define igmp_ifc_event(x) do { } while (0)
1858#endif
1859
Al Viro8f935bb2006-09-27 18:30:07 -07001860static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1861 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862{
1863 struct ip_mc_list *pmc;
1864 int changerec = 0;
1865 int i, err;
1866
1867 if (!in_dev)
1868 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001869 rcu_read_lock();
1870 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (*pmca == pmc->multiaddr)
1872 break;
1873 }
1874 if (!pmc) {
1875 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001876 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return -ESRCH;
1878 }
1879 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001880 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881#ifdef CONFIG_IP_MULTICAST
1882 sf_markstate(pmc);
1883#endif
1884 if (!delta) {
1885 err = -EINVAL;
1886 if (!pmc->sfcount[sfmode])
1887 goto out_unlock;
1888 pmc->sfcount[sfmode]--;
1889 }
1890 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001891 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1893
1894 changerec |= rv > 0;
1895 if (!err && rv < 0)
1896 err = rv;
1897 }
1898 if (pmc->sfmode == MCAST_EXCLUDE &&
1899 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1900 pmc->sfcount[MCAST_INCLUDE]) {
1901#ifdef CONFIG_IP_MULTICAST
1902 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001903 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904#endif
1905
1906 /* filter mode change */
1907 pmc->sfmode = MCAST_INCLUDE;
1908#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001909 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001911 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 psf->sf_crcount = 0;
1913 igmp_ifc_event(pmc->interface);
1914 } else if (sf_setstate(pmc) || changerec) {
1915 igmp_ifc_event(pmc->interface);
1916#endif
1917 }
1918out_unlock:
1919 spin_unlock_bh(&pmc->lock);
1920 return err;
1921}
1922
1923/*
1924 * Add multicast single-source filter to the interface list
1925 */
1926static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001927 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 struct ip_sf_list *psf, *psf_prev;
1930
1931 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001932 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 if (psf->sf_inaddr == *psfsrc)
1934 break;
1935 psf_prev = psf;
1936 }
1937 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001938 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (!psf)
1940 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 psf->sf_inaddr = *psfsrc;
1942 if (psf_prev) {
1943 psf_prev->sf_next = psf;
1944 } else
1945 pmc->sources = psf;
1946 }
1947 psf->sf_count[sfmode]++;
1948 if (psf->sf_count[sfmode] == 1) {
1949 ip_rt_multicast_event(pmc->interface);
1950 }
1951 return 0;
1952}
1953
1954#ifdef CONFIG_IP_MULTICAST
1955static void sf_markstate(struct ip_mc_list *pmc)
1956{
1957 struct ip_sf_list *psf;
1958 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1959
Weilong Chenc71151f2013-12-23 14:37:29 +08001960 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (pmc->sfcount[MCAST_EXCLUDE]) {
1962 psf->sf_oldin = mca_xcount ==
1963 psf->sf_count[MCAST_EXCLUDE] &&
1964 !psf->sf_count[MCAST_INCLUDE];
1965 } else
1966 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1967}
1968
1969static int sf_setstate(struct ip_mc_list *pmc)
1970{
David L Stevensad125832006-01-18 14:20:56 -08001971 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1973 int qrv = pmc->interface->mr_qrv;
1974 int new_in, rv;
1975
1976 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001977 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (pmc->sfcount[MCAST_EXCLUDE]) {
1979 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1980 !psf->sf_count[MCAST_INCLUDE];
1981 } else
1982 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001983 if (new_in) {
1984 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001985 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08001986
Weilong Chenc71151f2013-12-23 14:37:29 +08001987 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08001988 if (dpsf->sf_inaddr == psf->sf_inaddr)
1989 break;
1990 prev = dpsf;
1991 }
1992 if (dpsf) {
1993 if (prev)
1994 prev->sf_next = dpsf->sf_next;
1995 else
1996 pmc->tomb = dpsf->sf_next;
1997 kfree(dpsf);
1998 }
1999 psf->sf_crcount = qrv;
2000 rv++;
2001 }
2002 } else if (psf->sf_oldin) {
2003
2004 psf->sf_crcount = 0;
2005 /*
2006 * add or update "delete" records if an active filter
2007 * is now inactive
2008 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002009 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08002010 if (dpsf->sf_inaddr == psf->sf_inaddr)
2011 break;
2012 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00002013 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08002014 if (!dpsf)
2015 continue;
2016 *dpsf = *psf;
2017 /* pmc->lock held by callers */
2018 dpsf->sf_next = pmc->tomb;
2019 pmc->tomb = dpsf;
2020 }
2021 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 rv++;
2023 }
2024 }
2025 return rv;
2026}
2027#endif
2028
2029/*
2030 * Add multicast source filter list to the interface list
2031 */
Al Viro8f935bb2006-09-27 18:30:07 -07002032static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2033 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034{
2035 struct ip_mc_list *pmc;
2036 int isexclude;
2037 int i, err;
2038
2039 if (!in_dev)
2040 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002041 rcu_read_lock();
2042 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (*pmca == pmc->multiaddr)
2044 break;
2045 }
2046 if (!pmc) {
2047 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002048 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return -ESRCH;
2050 }
2051 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002052 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054#ifdef CONFIG_IP_MULTICAST
2055 sf_markstate(pmc);
2056#endif
2057 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2058 if (!delta)
2059 pmc->sfcount[sfmode]++;
2060 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002061 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002062 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (err)
2064 break;
2065 }
2066 if (err) {
2067 int j;
2068
Jun Zhao685f94e62011-11-22 17:19:03 +00002069 if (!delta)
2070 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002071 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002072 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2074#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002076 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002077 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078#endif
2079
2080 /* filter mode change */
2081 if (pmc->sfcount[MCAST_EXCLUDE])
2082 pmc->sfmode = MCAST_EXCLUDE;
2083 else if (pmc->sfcount[MCAST_INCLUDE])
2084 pmc->sfmode = MCAST_INCLUDE;
2085#ifdef CONFIG_IP_MULTICAST
2086 /* else no filters; keep old mode for reports */
2087
Nikolay Borisov165094a2016-02-08 23:29:24 +02002088 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002090 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 psf->sf_crcount = 0;
2092 igmp_ifc_event(in_dev);
2093 } else if (sf_setstate(pmc)) {
2094 igmp_ifc_event(in_dev);
2095#endif
2096 }
2097 spin_unlock_bh(&pmc->lock);
2098 return err;
2099}
2100
2101static void ip_mc_clear_src(struct ip_mc_list *pmc)
2102{
WANG Congecd66272017-06-12 09:52:26 -07002103 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
WANG Congecd66272017-06-12 09:52:26 -07002105 spin_lock_bh(&pmc->lock);
2106 tomb = pmc->tomb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 pmc->tomb = NULL;
WANG Congecd66272017-06-12 09:52:26 -07002108 sources = pmc->sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 pmc->sources = NULL;
2110 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002111 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 pmc->sfcount[MCAST_EXCLUDE] = 1;
WANG Congecd66272017-06-12 09:52:26 -07002113 spin_unlock_bh(&pmc->lock);
2114
2115 for (psf = tomb; psf; psf = nextpsf) {
2116 nextpsf = psf->sf_next;
2117 kfree(psf);
2118 }
2119 for (psf = sources; psf; psf = nextpsf) {
2120 nextpsf = psf->sf_next;
2121 kfree(psf);
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123}
2124
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002125/* Join a multicast group
2126 */
2127
2128int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Al Viro8f935bb2006-09-27 18:30:07 -07002130 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002131 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct in_device *in_dev;
2133 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002134 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002135 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002137 int err;
2138
2139 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Joe Perchesf97c1e02007-12-16 13:45:43 -08002141 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return -EINVAL;
2143
Daniel Lezcano877aced2008-08-13 16:15:57 -07002144 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 err = -ENODEV;
2148 goto done;
2149 }
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002152 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002153 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002154 if (i->multi.imr_multiaddr.s_addr == addr &&
2155 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 count++;
2158 }
2159 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002160 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002162 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002163 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002164 goto done;
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002167 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 iml->sflist = NULL;
2169 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002170 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return err;
2175}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002176EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2179 struct in_device *in_dev)
2180{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002181 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int err;
2183
Ian Morris51456b22015-04-03 09:17:26 +01002184 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /* any-source empty exclude case */
2186 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2187 iml->sfmode, 0, NULL, 0);
2188 }
2189 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002190 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002191 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002192 /* decrease mem now to avoid the memleak warning */
2193 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002194 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 return err;
2196}
2197
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002198int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002201 struct ip_mc_socklist *iml;
2202 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002203 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002204 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002205 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002206 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002207 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Eric Dumazet959d10f2015-02-17 03:19:24 -08002209 ASSERT_RTNL();
2210
Daniel Lezcano877aced2008-08-13 16:15:57 -07002211 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002212 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002213 ret = -ENODEV;
2214 goto out;
2215 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002216 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002217 for (imlp = &inet->mc_list;
2218 (iml = rtnl_dereference(*imlp)) != NULL;
2219 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002220 if (iml->multi.imr_multiaddr.s_addr != group)
2221 continue;
2222 if (ifindex) {
2223 if (iml->multi.imr_ifindex != ifindex)
2224 continue;
2225 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2226 iml->multi.imr_address.s_addr)
2227 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
David L Stevensacd6e002006-08-17 16:27:39 -07002229 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002231 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002232
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002233 if (in_dev)
2234 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002235
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002236 /* decrease mem now to avoid the memleak warning */
2237 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002238 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 }
dingtianhong52ad3532014-07-02 13:50:48 +08002241out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002242 return ret;
2243}
stephen hemminger193ba922012-10-01 12:32:34 +00002244EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246int ip_mc_source(int add, int omode, struct sock *sk, struct
2247 ip_mreq_source *mreqs, int ifindex)
2248{
2249 int err;
2250 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002251 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 struct ip_mc_socklist *pmc;
2253 struct in_device *in_dev = NULL;
2254 struct inet_sock *inet = inet_sk(sk);
2255 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002256 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002257 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 int i, j, rv;
2259
Joe Perchesf97c1e02007-12-16 13:45:43 -08002260 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 return -EINVAL;
2262
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002263 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2266 imr.imr_address.s_addr = mreqs->imr_interface;
2267 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002268 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270 if (!in_dev) {
2271 err = -ENODEV;
2272 goto done;
2273 }
2274 err = -EADDRNOTAVAIL;
2275
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002276 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002277 if ((pmc->multi.imr_multiaddr.s_addr ==
2278 imr.imr_multiaddr.s_addr) &&
2279 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 break;
2281 }
David L Stevens917f2f12005-07-08 17:45:16 -07002282 if (!pmc) { /* must have a prior join */
2283 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 /* if a source filter was set, must be the same mode as before */
2287 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002288 if (pmc->sfmode != omode) {
2289 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 } else if (pmc->sfmode != omode) {
2293 /* allow mode switches for empty-set filters */
2294 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002295 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 NULL, 0);
2297 pmc->sfmode = omode;
2298 }
2299
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002300 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 if (!add) {
2302 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002303 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002305 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002307 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (rv == 0)
2309 break;
2310 }
2311 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002312 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
David L Stevens8cdaaa12005-07-08 17:39:23 -07002314 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2315 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2316 leavegroup = 1;
2317 goto done;
2318 }
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002321 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 &mreqs->imr_sourceaddr, 1);
2323
Weilong Chenc71151f2013-12-23 14:37:29 +08002324 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 psl->sl_addr[j-1] = psl->sl_addr[j];
2326 psl->sl_count--;
2327 err = 0;
2328 goto done;
2329 }
2330 /* else, add a new source to the filter */
2331
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002332 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 err = -ENOBUFS;
2334 goto done;
2335 }
2336 if (!psl || psl->sl_count == psl->sl_max) {
2337 struct ip_sf_socklist *newpsl;
2338 int count = IP_SFBLOCK;
2339
2340 if (psl)
2341 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002342 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 if (!newpsl) {
2344 err = -ENOBUFS;
2345 goto done;
2346 }
2347 newpsl->sl_max = count;
2348 newpsl->sl_count = count - IP_SFBLOCK;
2349 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002350 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002352 /* decrease mem now to avoid the memleak warning */
2353 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002354 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002356 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002357 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 }
2359 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002360 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002362 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (rv == 0)
2364 break;
2365 }
2366 if (rv == 0) /* address already there is an error */
2367 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002368 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 psl->sl_addr[j+1] = psl->sl_addr[j];
2370 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2371 psl->sl_count++;
2372 err = 0;
2373 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002374 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 &mreqs->imr_sourceaddr, 1);
2376done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002377 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002378 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return err;
2380}
2381
2382int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2383{
David L Stevens9951f032005-07-08 17:47:28 -07002384 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002386 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 struct ip_mc_socklist *pmc;
2388 struct in_device *in_dev;
2389 struct inet_sock *inet = inet_sk(sk);
2390 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002391 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002392 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Joe Perchesf97c1e02007-12-16 13:45:43 -08002394 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return -EINVAL;
2396 if (msf->imsf_fmode != MCAST_INCLUDE &&
2397 msf->imsf_fmode != MCAST_EXCLUDE)
2398 return -EINVAL;
2399
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002400 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
2402 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2403 imr.imr_address.s_addr = msf->imsf_interface;
2404 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002405 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 if (!in_dev) {
2408 err = -ENODEV;
2409 goto done;
2410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
David L Stevens9951f032005-07-08 17:47:28 -07002412 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2413 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2414 leavegroup = 1;
2415 goto done;
2416 }
2417
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002418 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2420 pmc->multi.imr_ifindex == imr.imr_ifindex)
2421 break;
2422 }
David L Stevens917f2f12005-07-08 17:45:16 -07002423 if (!pmc) { /* must have a prior join */
2424 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002428 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2429 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (!newpsl) {
2431 err = -ENOBUFS;
2432 goto done;
2433 }
2434 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2435 memcpy(newpsl->sl_addr, msf->imsf_slist,
2436 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2437 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2438 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2439 if (err) {
2440 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2441 goto done;
2442 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002443 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002445 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2446 msf->imsf_fmode, 0, NULL, 0);
2447 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002448 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if (psl) {
2450 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2451 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002452 /* decrease mem now to avoid the memleak warning */
2453 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002454 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 } else
2456 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2457 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002458 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002460 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461done:
David L Stevens9951f032005-07-08 17:47:28 -07002462 if (leavegroup)
2463 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return err;
2465}
2466
2467int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2468 struct ip_msfilter __user *optval, int __user *optlen)
2469{
2470 int err, len, count, copycount;
2471 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002472 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 struct ip_mc_socklist *pmc;
2474 struct in_device *in_dev;
2475 struct inet_sock *inet = inet_sk(sk);
2476 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002477 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
WANG Cong87e9f032015-11-03 15:41:16 -08002479 ASSERT_RTNL();
2480
Joe Perchesf97c1e02007-12-16 13:45:43 -08002481 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 return -EINVAL;
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2485 imr.imr_address.s_addr = msf->imsf_interface;
2486 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002487 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 if (!in_dev) {
2490 err = -ENODEV;
2491 goto done;
2492 }
2493 err = -EADDRNOTAVAIL;
2494
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002495 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2497 pmc->multi.imr_ifindex == imr.imr_ifindex)
2498 break;
2499 }
2500 if (!pmc) /* must have a prior join */
2501 goto done;
2502 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002503 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (!psl) {
2505 len = 0;
2506 count = 0;
2507 } else {
2508 count = psl->sl_count;
2509 }
2510 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2511 len = copycount * sizeof(psl->sl_addr[0]);
2512 msf->imsf_numsrc = count;
2513 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2514 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2515 return -EFAULT;
2516 }
2517 if (len &&
2518 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2519 return -EFAULT;
2520 return 0;
2521done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return err;
2523}
2524
2525int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2526 struct group_filter __user *optval, int __user *optlen)
2527{
2528 int err, i, count, copycount;
2529 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002530 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct ip_mc_socklist *pmc;
2532 struct inet_sock *inet = inet_sk(sk);
2533 struct ip_sf_socklist *psl;
2534
WANG Cong87e9f032015-11-03 15:41:16 -08002535 ASSERT_RTNL();
2536
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 psin = (struct sockaddr_in *)&gsf->gf_group;
2538 if (psin->sin_family != AF_INET)
2539 return -EINVAL;
2540 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002541 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return -EINVAL;
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 err = -EADDRNOTAVAIL;
2545
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002546 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2548 pmc->multi.imr_ifindex == gsf->gf_interface)
2549 break;
2550 }
2551 if (!pmc) /* must have a prior join */
2552 goto done;
2553 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002554 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 count = psl ? psl->sl_count : 0;
2556 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2557 gsf->gf_numsrc = count;
2558 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2559 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2560 return -EFAULT;
2561 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002562 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 struct sockaddr_storage ss;
2564
2565 psin = (struct sockaddr_in *)&ss;
2566 memset(&ss, 0, sizeof(ss));
2567 psin->sin_family = AF_INET;
2568 psin->sin_addr.s_addr = psl->sl_addr[i];
2569 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2570 return -EFAULT;
2571 }
2572 return 0;
2573done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return err;
2575}
2576
2577/*
2578 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2579 */
Al Viroc0cda062006-09-27 18:31:10 -07002580int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 struct inet_sock *inet = inet_sk(sk);
2583 struct ip_mc_socklist *pmc;
2584 struct ip_sf_socklist *psl;
2585 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002586 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002588 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002589 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002592 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002593 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2595 pmc->multi.imr_ifindex == dif)
2596 break;
2597 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002598 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002600 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002601 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002602 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002604 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605
Weilong Chenc71151f2013-12-23 14:37:29 +08002606 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (psl->sl_addr[i] == rmt_addr)
2608 break;
2609 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002610 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002612 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002614 goto unlock;
2615 ret = 1;
2616unlock:
2617 rcu_read_unlock();
2618out:
2619 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620}
2621
2622/*
2623 * A socket is closing.
2624 */
2625
2626void ip_mc_drop_socket(struct sock *sk)
2627{
2628 struct inet_sock *inet = inet_sk(sk);
2629 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002630 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Ian Morris51456b22015-04-03 09:17:26 +01002632 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return;
2634
2635 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002636 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002639 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002640 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002641 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002642 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002644 /* decrease mem now to avoid the memleak warning */
2645 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002646 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648 rtnl_unlock();
2649}
2650
David S. Millerdbdd9a52011-03-10 16:34:38 -08002651/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002652int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002655 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct ip_sf_list *psf;
2657 int rv = 0;
2658
Eric Dumazete9897072013-06-07 08:48:57 -07002659 mc_hash = rcu_dereference(in_dev->mc_hash);
2660 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002661 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002662
2663 for (im = rcu_dereference(mc_hash[hash]);
2664 im != NULL;
2665 im = rcu_dereference(im->next_hash)) {
2666 if (im->multiaddr == mc_addr)
2667 break;
2668 }
2669 } else {
2670 for_each_pmc_rcu(in_dev, im) {
2671 if (im->multiaddr == mc_addr)
2672 break;
2673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 if (im && proto == IPPROTO_IGMP) {
2676 rv = 1;
2677 } else if (im) {
2678 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002679 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 if (psf->sf_inaddr == src_addr)
2681 break;
2682 }
2683 if (psf)
2684 rv = psf->sf_count[MCAST_INCLUDE] ||
2685 psf->sf_count[MCAST_EXCLUDE] !=
2686 im->sfcount[MCAST_EXCLUDE];
2687 else
2688 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2689 } else
2690 rv = 1; /* unspecified source; tentatively allow */
2691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return rv;
2693}
2694
2695#if defined(CONFIG_PROC_FS)
2696struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002697 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct net_device *dev;
2699 struct in_device *in_dev;
2700};
2701
2702#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2703
2704static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2705{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002706 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 struct ip_mc_list *im = NULL;
2708 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2709
Pavel Emelianov7562f872007-05-03 15:13:45 -07002710 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002711 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002713
2714 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 if (!in_dev)
2716 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002717 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (im) {
2719 state->in_dev = in_dev;
2720 break;
2721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723 return im;
2724}
2725
2726static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2727{
2728 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002729
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002730 im = rcu_dereference(im->next_rcu);
2731 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002732 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 if (!state->dev) {
2734 state->in_dev = NULL;
2735 break;
2736 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002737 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 if (!state->in_dev)
2739 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002740 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
2742 return im;
2743}
2744
2745static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2746{
2747 struct ip_mc_list *im = igmp_mc_get_first(seq);
2748 if (im)
2749 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2750 --pos;
2751 return pos ? NULL : im;
2752}
2753
2754static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002755 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
stephen hemminger61fbab72009-11-10 07:54:55 +00002757 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2759}
2760
2761static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2762{
2763 struct ip_mc_list *im;
2764 if (v == SEQ_START_TOKEN)
2765 im = igmp_mc_get_first(seq);
2766 else
2767 im = igmp_mc_get_next(seq, v);
2768 ++*pos;
2769 return im;
2770}
2771
2772static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002773 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774{
2775 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002776
2777 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002779 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
2782static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2783{
2784 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002785 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2787 else {
2788 struct ip_mc_list *im = (struct ip_mc_list *)v;
2789 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2790 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002791 long delta;
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793#ifdef CONFIG_IP_MULTICAST
2794 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2795 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2796 "V3";
2797#else
2798 querier = "NONE";
2799#endif
2800
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002801 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002803 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 }
2805
Eric Dumazeta399a802012-08-08 21:13:53 +00002806 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002808 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002810 im->tm_running,
2811 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 im->reporter);
2813 }
2814 return 0;
2815}
2816
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002817static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 .start = igmp_mc_seq_start,
2819 .next = igmp_mc_seq_next,
2820 .stop = igmp_mc_seq_stop,
2821 .show = igmp_mc_seq_show,
2822};
2823
2824static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2825{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002826 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002827 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
Arjan van de Ven9a321442007-02-12 00:55:35 -08002830static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 .owner = THIS_MODULE,
2832 .open = igmp_mc_seq_open,
2833 .read = seq_read,
2834 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002835 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836};
2837
2838struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002839 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 struct net_device *dev;
2841 struct in_device *idev;
2842 struct ip_mc_list *im;
2843};
2844
2845#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2846
2847static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2848{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002849 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 struct ip_sf_list *psf = NULL;
2851 struct ip_mc_list *im = NULL;
2852 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2853
Pavel Emelianov7562f872007-05-03 15:13:45 -07002854 state->idev = NULL;
2855 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002856 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002858 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002859 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002861 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002862 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 spin_lock_bh(&im->lock);
2864 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002865 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 state->im = im;
2867 state->idev = idev;
2868 break;
2869 }
2870 spin_unlock_bh(&im->lock);
2871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873 return psf;
2874}
2875
2876static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2877{
2878 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2879
2880 psf = psf->sf_next;
2881 while (!psf) {
2882 spin_unlock_bh(&state->im->lock);
2883 state->im = state->im->next;
2884 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002885 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (!state->dev) {
2887 state->idev = NULL;
2888 goto out;
2889 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002890 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 if (!state->idev)
2892 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002893 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895 if (!state->im)
2896 break;
2897 spin_lock_bh(&state->im->lock);
2898 psf = state->im->sources;
2899 }
2900out:
2901 return psf;
2902}
2903
2904static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2905{
2906 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2907 if (psf)
2908 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2909 --pos;
2910 return pos ? NULL : psf;
2911}
2912
2913static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002914 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
stephen hemminger61fbab72009-11-10 07:54:55 +00002916 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2918}
2919
2920static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2921{
2922 struct ip_sf_list *psf;
2923 if (v == SEQ_START_TOKEN)
2924 psf = igmp_mcf_get_first(seq);
2925 else
2926 psf = igmp_mcf_get_next(seq, v);
2927 ++*pos;
2928 return psf;
2929}
2930
2931static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002932 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933{
2934 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002935 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 spin_unlock_bh(&state->im->lock);
2937 state->im = NULL;
2938 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002939 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002941 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
2944static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2945{
2946 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2947 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2948
2949 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002950 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 } else {
2952 seq_printf(seq,
2953 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002954 "0x%08x %6lu %6lu\n",
2955 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 ntohl(state->im->multiaddr),
2957 ntohl(psf->sf_inaddr),
2958 psf->sf_count[MCAST_INCLUDE],
2959 psf->sf_count[MCAST_EXCLUDE]);
2960 }
2961 return 0;
2962}
2963
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002964static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 .start = igmp_mcf_seq_start,
2966 .next = igmp_mcf_seq_next,
2967 .stop = igmp_mcf_seq_stop,
2968 .show = igmp_mcf_seq_show,
2969};
2970
2971static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2972{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002973 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002974 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
Arjan van de Ven9a321442007-02-12 00:55:35 -08002977static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 .owner = THIS_MODULE,
2979 .open = igmp_mcf_seq_open,
2980 .read = seq_read,
2981 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002982 .release = seq_release_net,
2983};
2984
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002985static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002986{
2987 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002988 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002989
Gao fengd4beaa62013-02-18 01:34:54 +00002990 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002991 if (!pde)
2992 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00002993 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
2994 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002995 if (!pde)
2996 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08002997 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2998 SOCK_DGRAM, 0, net);
2999 if (err < 0) {
3000 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3001 err);
3002 goto out_sock;
3003 }
3004
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003005 return 0;
3006
Madhu Challa93a714d2015-02-25 09:58:35 -08003007out_sock:
3008 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003009out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00003010 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003011out_igmp:
3012 return -ENOMEM;
3013}
3014
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003015static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003016{
Gao fengece31ff2013-02-18 01:34:56 +00003017 remove_proc_entry("mcfilter", net->proc_net);
3018 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08003019 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003020}
3021
3022static struct pernet_operations igmp_net_ops = {
3023 .init = igmp_net_init,
3024 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025};
WANG Cong72c1d3b2014-01-10 16:09:45 -08003026#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003028static int igmp_netdev_event(struct notifier_block *this,
3029 unsigned long event, void *ptr)
3030{
3031 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3032 struct in_device *in_dev;
3033
3034 switch (event) {
3035 case NETDEV_RESEND_IGMP:
3036 in_dev = __in_dev_get_rtnl(dev);
3037 if (in_dev)
3038 ip_mc_rejoin_groups(in_dev);
3039 break;
3040 default:
3041 break;
3042 }
3043 return NOTIFY_DONE;
3044}
3045
3046static struct notifier_block igmp_notifier = {
3047 .notifier_call = igmp_netdev_event,
3048};
3049
WANG Cong72c1d3b2014-01-10 16:09:45 -08003050int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003052#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003053 int err;
3054
3055 err = register_pernet_subsys(&igmp_net_ops);
3056 if (err)
3057 return err;
3058 err = register_netdevice_notifier(&igmp_notifier);
3059 if (err)
3060 goto reg_notif_fail;
3061 return 0;
3062
3063reg_notif_fail:
3064 unregister_pernet_subsys(&igmp_net_ops);
3065 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003066#else
3067 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003069}