blob: b448cf32296c9380355734f8d2a4db19148139bd [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 Torvalds7c0f6ba2016-12-24 11:46:01 -080075#include <linux/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 Cernekeea46182b2017-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_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
115#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
Hangbin Liu966c37f2018-10-26 11:30:35 +0800116#define IGMP_QUERY_INTERVAL (125*HZ)
Fabian Frederick436f7c22014-11-04 20:52:14 +0100117#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Fabian Frederick436f7c22014-11-04 20:52:14 +0100119#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Fabian Frederick436f7c22014-11-04 20:52:14 +0100121/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * IGMP specs require to report membership immediately after
123 * joining a group, but we delay the first report by a
124 * small interval. It seems more natural and still does not
125 * contradict to specs provided this delay is small enough.
126 */
127
Herbert Xu42f811b2007-06-04 23:34:44 -0700128#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900129 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700130 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
131 ((in_dev)->mr_v1_seen && \
132 time_before(jiffies, (in_dev)->mr_v1_seen)))
133#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900134 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700135 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
136 ((in_dev)->mr_v2_seen && \
137 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
William Manleycab70042013-08-06 19:03:13 +0100139static int unsolicited_report_interval(struct in_device *in_dev)
140{
William Manley26900482013-08-06 19:03:15 +0100141 int interval_ms, interval_jiffies;
142
William Manleycab70042013-08-06 19:03:13 +0100143 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100144 interval_ms = IN_DEV_CONF_GET(
145 in_dev,
146 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100147 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100148 interval_ms = IN_DEV_CONF_GET(
149 in_dev,
150 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
151
152 interval_jiffies = msecs_to_jiffies(interval_ms);
153
154 /* _timer functions can't handle a delay of 0 jiffies so ensure
155 * we always return a positive value.
156 */
157 if (interval_jiffies <= 0)
158 interval_jiffies = 1;
159 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100160}
161
Florian Fainelli9fb20802019-02-01 20:20:52 -0800162static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
163 gfp_t gfp);
Hangbin Liu24803f32016-11-14 16:16:28 +0800164static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void igmpv3_clear_delrec(struct in_device *in_dev);
166static int sf_setstate(struct ip_mc_list *pmc);
167static void sf_markstate(struct ip_mc_list *pmc);
168#endif
169static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700170static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
171 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173static void ip_ma_put(struct ip_mc_list *im)
174{
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300175 if (refcount_dec_and_test(&im->refcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800177 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179}
180
David S. Millerd9aa9382010-11-15 08:52:02 -0800181#define for_each_pmc_rcu(in_dev, pmc) \
182 for (pmc = rcu_dereference(in_dev->mc_list); \
183 pmc != NULL; \
184 pmc = rcu_dereference(pmc->next_rcu))
185
186#define for_each_pmc_rtnl(in_dev, pmc) \
187 for (pmc = rtnl_dereference(in_dev->mc_list); \
188 pmc != NULL; \
189 pmc = rtnl_dereference(pmc->next_rcu))
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#ifdef CONFIG_IP_MULTICAST
192
193/*
194 * Timer management
195 */
196
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000197static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 spin_lock_bh(&im->lock);
200 if (del_timer(&im->timer))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300201 refcount_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800202 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 im->reporter = 0;
204 im->unsolicit_count = 0;
205 spin_unlock_bh(&im->lock);
206}
207
208/* It must be called with locked im->lock */
209static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
210{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500211 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800213 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (!mod_timer(&im->timer, jiffies+tv+2))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300215 refcount_inc(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
218static void igmp_gq_start_timer(struct in_device *in_dev)
219{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500220 int tv = prandom_u32() % in_dev->mr_maxdelay;
Michal Tesar7ababb72017-01-02 14:38:36 +0100221 unsigned long exp = jiffies + tv + 2;
222
223 if (in_dev->mr_gq_running &&
224 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
225 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 in_dev->mr_gq_running = 1;
Michal Tesar7ababb72017-01-02 14:38:36 +0100228 if (!mod_timer(&in_dev->mr_gq_timer, exp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 in_dev_hold(in_dev);
230}
231
232static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
233{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500234 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
237 in_dev_hold(in_dev);
238}
239
240static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
241{
242 spin_lock_bh(&im->lock);
243 im->unsolicit_count = 0;
244 if (del_timer(&im->timer)) {
245 if ((long)(im->timer.expires-jiffies) < max_delay) {
246 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800247 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 spin_unlock_bh(&im->lock);
249 return;
250 }
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300251 refcount_dec(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 igmp_start_timer(im, max_delay);
254 spin_unlock_bh(&im->lock);
255}
256
257
258/*
259 * Send an IGMP report.
260 */
261
262#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
263
264
265static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
266 int gdeleted, int sdeleted)
267{
268 switch (type) {
269 case IGMPV3_MODE_IS_INCLUDE:
270 case IGMPV3_MODE_IS_EXCLUDE:
271 if (gdeleted || sdeleted)
272 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800273 if (!(pmc->gsquery && !psf->sf_gsresp)) {
274 if (pmc->sfmode == MCAST_INCLUDE)
275 return 1;
276 /* don't include if this source is excluded
277 * in all filters
278 */
279 if (psf->sf_count[MCAST_INCLUDE])
280 return type == IGMPV3_MODE_IS_INCLUDE;
281 return pmc->sfcount[MCAST_EXCLUDE] ==
282 psf->sf_count[MCAST_EXCLUDE];
283 }
284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 case IGMPV3_CHANGE_TO_INCLUDE:
286 if (gdeleted || sdeleted)
287 return 0;
288 return psf->sf_count[MCAST_INCLUDE] != 0;
289 case IGMPV3_CHANGE_TO_EXCLUDE:
290 if (gdeleted || sdeleted)
291 return 0;
292 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
293 psf->sf_count[MCAST_INCLUDE])
294 return 0;
295 return pmc->sfcount[MCAST_EXCLUDE] ==
296 psf->sf_count[MCAST_EXCLUDE];
297 case IGMPV3_ALLOW_NEW_SOURCES:
298 if (gdeleted || !psf->sf_crcount)
299 return 0;
300 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
301 case IGMPV3_BLOCK_OLD_SOURCES:
302 if (pmc->sfmode == MCAST_INCLUDE)
303 return gdeleted || (psf->sf_crcount && sdeleted);
304 return psf->sf_crcount && !gdeleted && !sdeleted;
305 }
306 return 0;
307}
308
309static int
310igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
311{
312 struct ip_sf_list *psf;
313 int scount = 0;
314
Weilong Chenc71151f2013-12-23 14:37:29 +0800315 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
317 continue;
318 scount++;
319 }
320 return scount;
321}
322
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800323/* source address selection per RFC 3376 section 4.2.13 */
324static __be32 igmpv3_get_srcaddr(struct net_device *dev,
325 const struct flowi4 *fl4)
326{
327 struct in_device *in_dev = __in_dev_get_rcu(dev);
328
329 if (!in_dev)
330 return htonl(INADDR_ANY);
331
332 for_ifa(in_dev) {
Felix Fietkauad23b752018-01-19 11:50:46 +0100333 if (fl4->saddr == ifa->ifa_local)
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800334 return fl4->saddr;
335 } endfor_ifa(in_dev);
336
337 return htonl(INADDR_ANY);
338}
339
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100340static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 struct sk_buff *skb;
343 struct rtable *rt;
344 struct iphdr *pip;
345 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700346 struct net *net = dev_net(dev);
David S. Miller31e4543d2011-05-03 20:25:42 -0700347 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000348 int hlen = LL_RESERVED_SPACE(dev);
349 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100350 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000352 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000353 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000354 GFP_ATOMIC | __GFP_NOWARN);
355 if (skb)
356 break;
357 size >>= 1;
358 if (size < 256)
359 return NULL;
360 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200361 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
David S. Miller31e4543d2011-05-03 20:25:42 -0700363 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500364 0, 0,
365 IPPROTO_IGMP, 0, dev->ifindex);
366 if (IS_ERR(rt)) {
367 kfree_skb(skb);
368 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Changli Gaod8d1f302010-06-10 23:31:35 -0700371 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 skb->dev = dev;
373
Herbert Xu66088242011-11-18 02:20:04 +0000374 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800375 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300377 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700378 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300379 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 pip->version = 4;
382 pip->ihl = (sizeof(struct iphdr)+4)>>2;
383 pip->tos = 0xc0;
384 pip->frag_off = htons(IP_DF);
385 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700386 pip->daddr = fl4.daddr;
Eric Dumazete7aadb22018-02-01 10:26:57 -0800387
388 rcu_read_lock();
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800389 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
Eric Dumazete7aadb22018-02-01 10:26:57 -0800390 rcu_read_unlock();
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 pip->protocol = IPPROTO_IGMP;
393 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100394 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000395 ((u8 *)&pip[1])[0] = IPOPT_RA;
396 ((u8 *)&pip[1])[1] = 4;
397 ((u8 *)&pip[1])[2] = 0;
398 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700400 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300401 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300402 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
404 pig->resv1 = 0;
405 pig->csum = 0;
406 pig->resv2 = 0;
407 pig->ngrec = 0;
408 return skb;
409}
410
411static int igmpv3_sendpack(struct sk_buff *skb)
412{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300413 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000414 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300416 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Eric W. Biederman33224b12015-10-07 16:48:46 -0500418 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
422{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800423 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
Eric Dumazetb5476022017-12-11 07:17:39 -0800427 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 struct net_device *dev = pmc->interface->dev;
430 struct igmpv3_report *pih;
431 struct igmpv3_grec *pgr;
432
Eric Dumazetb5476022017-12-11 07:17:39 -0800433 if (!skb) {
434 skb = igmpv3_newpack(dev, mtu);
435 if (!skb)
436 return NULL;
437 }
Johannes Berg4df864c2017-06-16 14:29:21 +0200438 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 pgr->grec_type = type;
440 pgr->grec_auxwords = 0;
441 pgr->grec_nsrcs = 0;
442 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300443 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 pih->ngrec = htons(ntohs(pih->ngrec)+1);
445 *ppgr = pgr;
446 return skb;
447}
448
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100449#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
452 int type, int gdeleted, int sdeleted)
453{
454 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200455 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct igmpv3_report *pih;
457 struct igmpv3_grec *pgr = NULL;
458 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800459 int scount, stotal, first, isquery, truncate;
Eric Dumazetb5476022017-12-11 07:17:39 -0800460 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (pmc->multiaddr == IGMP_ALL_HOSTS)
463 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200464 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100465 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Eric Dumazetb5476022017-12-11 07:17:39 -0800467 mtu = READ_ONCE(dev->mtu);
468 if (mtu < IPV4_MIN_MTU)
469 return skb;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
472 type == IGMPV3_MODE_IS_EXCLUDE;
473 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
474 type == IGMPV3_CHANGE_TO_EXCLUDE;
475
David L Stevensad125832006-01-18 14:20:56 -0800476 stotal = scount = 0;
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
479
David L Stevensad125832006-01-18 14:20:56 -0800480 if (!*psf_list)
481 goto empty_source;
482
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300483 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
485 /* EX and TO_EX get a fresh packet, if needed */
486 if (truncate) {
487 if (pih && pih->ngrec &&
488 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
489 if (skb)
490 igmpv3_sendpack(skb);
Eric Dumazetb5476022017-12-11 07:17:39 -0800491 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 }
494 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800496 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700497 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
499 psf_next = psf->sf_next;
500
501 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
502 psf_prev = psf;
503 continue;
504 }
505
Hangbin Liua0525172016-08-02 18:02:57 +0800506 /* Based on RFC3376 5.1. Should not send source-list change
507 * records when there is a filter mode change.
508 */
509 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
510 (!gdeleted && pmc->crcount)) &&
511 (type == IGMPV3_ALLOW_NEW_SOURCES ||
512 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
513 goto decrease_sf_crcount;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 /* clear marks on query responses */
516 if (isquery)
517 psf->sf_gsresp = 0;
518
Al Viro63007722006-09-27 18:31:32 -0700519 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 first*sizeof(struct igmpv3_grec)) {
521 if (truncate && !first)
522 break; /* truncate these */
523 if (pgr)
524 pgr->grec_nsrcs = htons(scount);
525 if (skb)
526 igmpv3_sendpack(skb);
Eric Dumazetb5476022017-12-11 07:17:39 -0800527 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 first = 1;
529 scount = 0;
530 }
531 if (first) {
Eric Dumazetb5476022017-12-11 07:17:39 -0800532 skb = add_grhead(skb, pmc, type, &pgr, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 first = 0;
534 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800535 if (!skb)
536 return NULL;
Johannes Berg4df864c2017-06-16 14:29:21 +0200537 psrc = skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800539 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
541 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
Hangbin Liua0525172016-08-02 18:02:57 +0800542decrease_sf_crcount:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 psf->sf_crcount--;
544 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
545 if (psf_prev)
546 psf_prev->sf_next = psf->sf_next;
547 else
548 *psf_list = psf->sf_next;
549 kfree(psf);
550 continue;
551 }
552 }
553 psf_prev = psf;
554 }
David L Stevensad125832006-01-18 14:20:56 -0800555
556empty_source:
557 if (!stotal) {
558 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
559 type == IGMPV3_BLOCK_OLD_SOURCES)
560 return skb;
561 if (pmc->crcount || isquery) {
562 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800563 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800564 igmpv3_sendpack(skb);
565 skb = NULL; /* add_grhead will get a new one */
566 }
Eric Dumazetb5476022017-12-11 07:17:39 -0800567 skb = add_grhead(skb, pmc, type, &pgr, mtu);
David L Stevensad125832006-01-18 14:20:56 -0800568 }
569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (pgr)
571 pgr->grec_nsrcs = htons(scount);
572
573 if (isquery)
574 pmc->gsquery = 0; /* clear query state on report */
575 return skb;
576}
577
578static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
579{
580 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200581 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int type;
583
584 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000585 rcu_read_lock();
586 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (pmc->multiaddr == IGMP_ALL_HOSTS)
588 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100589 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200590 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100591 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock_bh(&pmc->lock);
593 if (pmc->sfcount[MCAST_EXCLUDE])
594 type = IGMPV3_MODE_IS_EXCLUDE;
595 else
596 type = IGMPV3_MODE_IS_INCLUDE;
597 skb = add_grec(skb, pmc, type, 0, 0);
598 spin_unlock_bh(&pmc->lock);
599 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000600 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } else {
602 spin_lock_bh(&pmc->lock);
603 if (pmc->sfcount[MCAST_EXCLUDE])
604 type = IGMPV3_MODE_IS_EXCLUDE;
605 else
606 type = IGMPV3_MODE_IS_INCLUDE;
607 skb = add_grec(skb, pmc, type, 0, 0);
608 spin_unlock_bh(&pmc->lock);
609 }
610 if (!skb)
611 return 0;
612 return igmpv3_sendpack(skb);
613}
614
615/*
616 * remove zero-count source records from a source filter list
617 */
618static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
619{
620 struct ip_sf_list *psf_prev, *psf_next, *psf;
621
622 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800623 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 psf_next = psf->sf_next;
625 if (psf->sf_crcount == 0) {
626 if (psf_prev)
627 psf_prev->sf_next = psf->sf_next;
628 else
629 *ppsf = psf->sf_next;
630 kfree(psf);
631 } else
632 psf_prev = psf;
633 }
634}
635
636static void igmpv3_send_cr(struct in_device *in_dev)
637{
638 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
639 struct sk_buff *skb = NULL;
640 int type, dtype;
641
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000642 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 spin_lock_bh(&in_dev->mc_tomb_lock);
644
645 /* deleted MCA's */
646 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800647 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 pmc_next = pmc->next;
649 if (pmc->sfmode == MCAST_INCLUDE) {
650 type = IGMPV3_BLOCK_OLD_SOURCES;
651 dtype = IGMPV3_BLOCK_OLD_SOURCES;
652 skb = add_grec(skb, pmc, type, 1, 0);
653 skb = add_grec(skb, pmc, dtype, 1, 1);
654 }
655 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (pmc->sfmode == MCAST_EXCLUDE) {
657 type = IGMPV3_CHANGE_TO_INCLUDE;
658 skb = add_grec(skb, pmc, type, 1, 0);
659 }
David L Stevensad125832006-01-18 14:20:56 -0800660 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (pmc->crcount == 0) {
662 igmpv3_clear_zeros(&pmc->tomb);
663 igmpv3_clear_zeros(&pmc->sources);
664 }
665 }
666 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
667 if (pmc_prev)
668 pmc_prev->next = pmc_next;
669 else
670 in_dev->mc_tomb = pmc_next;
671 in_dev_put(pmc->interface);
672 kfree(pmc);
673 } else
674 pmc_prev = pmc;
675 }
676 spin_unlock_bh(&in_dev->mc_tomb_lock);
677
678 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000679 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 spin_lock_bh(&pmc->lock);
681 if (pmc->sfcount[MCAST_EXCLUDE]) {
682 type = IGMPV3_BLOCK_OLD_SOURCES;
683 dtype = IGMPV3_ALLOW_NEW_SOURCES;
684 } else {
685 type = IGMPV3_ALLOW_NEW_SOURCES;
686 dtype = IGMPV3_BLOCK_OLD_SOURCES;
687 }
688 skb = add_grec(skb, pmc, type, 0, 0);
689 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
690
691 /* filter mode changes */
692 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if (pmc->sfmode == MCAST_EXCLUDE)
694 type = IGMPV3_CHANGE_TO_EXCLUDE;
695 else
696 type = IGMPV3_CHANGE_TO_INCLUDE;
697 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800698 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
700 spin_unlock_bh(&pmc->lock);
701 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000702 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (!skb)
705 return;
706 (void) igmpv3_sendpack(skb);
707}
708
709static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
710 int type)
711{
712 struct sk_buff *skb;
713 struct iphdr *iph;
714 struct igmphdr *ih;
715 struct rtable *rt;
716 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700717 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700718 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e4543d2011-05-03 20:25:42 -0700719 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700720 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000721 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
724 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100725
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200726 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100727 return 0;
728
729 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 dst = IGMP_ALL_ROUTER;
731 else
732 dst = group;
733
David S. Miller31e4543d2011-05-03 20:25:42 -0700734 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500735 0, 0,
736 IPPROTO_IGMP, 0, dev->ifindex);
737 if (IS_ERR(rt))
738 return -1;
739
Herbert Xu66088242011-11-18 02:20:04 +0000740 hlen = LL_RESERVED_SPACE(dev);
741 tlen = dev->needed_tailroom;
742 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100743 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 ip_rt_put(rt);
745 return -1;
746 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200747 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Changli Gaod8d1f302010-06-10 23:31:35 -0700749 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Herbert Xu66088242011-11-18 02:20:04 +0000751 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300753 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700754 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300755 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 iph->version = 4;
758 iph->ihl = (sizeof(struct iphdr)+4)>>2;
759 iph->tos = 0xc0;
760 iph->frag_off = htons(IP_DF);
761 iph->ttl = 1;
762 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700763 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100765 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000766 ((u8 *)&iph[1])[0] = IPOPT_RA;
767 ((u8 *)&iph[1])[1] = 4;
768 ((u8 *)&iph[1])[2] = 0;
769 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Johannes Berg4df864c2017-06-16 14:29:21 +0200771 ih = skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800772 ih->type = type;
773 ih->code = 0;
774 ih->csum = 0;
775 ih->group = group;
776 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Eric W. Biederman33224b12015-10-07 16:48:46 -0500778 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Kees Cooke99e88a2017-10-16 14:43:17 -0700781static void igmp_gq_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Kees Cooke99e88a2017-10-16 14:43:17 -0700783 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 in_dev->mr_gq_running = 0;
786 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700787 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Kees Cooke99e88a2017-10-16 14:43:17 -0700790static void igmp_ifc_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Kees Cooke99e88a2017-10-16 14:43:17 -0700792 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 igmpv3_send_cr(in_dev);
795 if (in_dev->mr_ifc_count) {
796 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100797 igmp_ifc_start_timer(in_dev,
798 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700800 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
803static void igmp_ifc_event(struct in_device *in_dev)
804{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200805 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
807 return;
Nikolay Borisov165094a2016-02-08 23:29:24 +0200808 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 igmp_ifc_start_timer(in_dev, 1);
810}
811
812
Kees Cooke99e88a2017-10-16 14:43:17 -0700813static void igmp_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Kees Cooke99e88a2017-10-16 14:43:17 -0700815 struct ip_mc_list *im = from_timer(im, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 struct in_device *in_dev = im->interface;
817
818 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800819 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Hangbin Liu4fb72532018-08-29 18:06:08 +0800821 if (im->unsolicit_count && --im->unsolicit_count)
William Manleycab70042013-08-06 19:03:13 +0100822 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Hangbin Liu4fb72532018-08-29 18:06:08 +0800823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 im->reporter = 1;
825 spin_unlock(&im->lock);
826
827 if (IGMP_V1_SEEN(in_dev))
828 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
829 else if (IGMP_V2_SEEN(in_dev))
830 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
831 else
832 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
833
834 ip_ma_put(im);
835}
836
David L Stevensad125832006-01-18 14:20:56 -0800837/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700838static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct ip_sf_list *psf;
841 int i, scount;
842
843 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800844 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (scount == nsrcs)
846 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800847 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800848 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000849 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800850 pmc->sfcount[MCAST_EXCLUDE] !=
851 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800852 break;
David L Stevensad125832006-01-18 14:20:56 -0800853 if (srcs[i] == psf->sf_inaddr) {
854 scount++;
855 break;
856 }
857 }
858 }
859 pmc->gsquery = 0;
860 if (scount == nsrcs) /* all sources excluded */
861 return 0;
862 return 1;
863}
864
Al Viroea4d9e72006-09-27 18:30:52 -0700865static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800866{
867 struct ip_sf_list *psf;
868 int i, scount;
869
870 if (pmc->sfmode == MCAST_EXCLUDE)
871 return igmp_xmarksources(pmc, nsrcs, srcs);
872
873 /* mark INCLUDE-mode sources */
874 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800875 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800876 if (scount == nsrcs)
877 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800878 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (srcs[i] == psf->sf_inaddr) {
880 psf->sf_gsresp = 1;
881 scount++;
882 break;
883 }
884 }
David L Stevensad125832006-01-18 14:20:56 -0800885 if (!scount) {
886 pmc->gsquery = 0;
887 return 0;
888 }
889 pmc->gsquery = 1;
890 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Eric Dumazetd679c532012-09-06 20:37:06 +0000893/* return true if packet was dropped */
894static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200897 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 /* Timers are only set for non-local groups */
900
901 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000902 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200903 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100904 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000906 rcu_read_lock();
907 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (im->multiaddr == group) {
909 igmp_stop_timer(im);
910 break;
911 }
912 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000913 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000914 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Eric Dumazetd679c532012-09-06 20:37:06 +0000917/* return true if packet was dropped */
918static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int len)
920{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300921 struct igmphdr *ih = igmp_hdr(skb);
922 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700924 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 int max_delay;
926 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200927 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929
David Stevens5b7c8402010-09-30 14:29:40 +0000930 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (ih->code == 0) {
932 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900933
Fabian Frederick436f7c22014-11-04 20:52:14 +0100934 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 in_dev->mr_v1_seen = jiffies +
Hangbin Liu966c37f2018-10-26 11:30:35 +0800936 (in_dev->mr_qrv * in_dev->mr_qi) +
937 in_dev->mr_qri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 group = 0;
939 } else {
940 /* v2 router present */
941 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
942 in_dev->mr_v2_seen = jiffies +
Hangbin Liu966c37f2018-10-26 11:30:35 +0800943 (in_dev->mr_qrv * in_dev->mr_qi) +
944 in_dev->mr_qri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 /* cancel the interface change timer */
947 in_dev->mr_ifc_count = 0;
948 if (del_timer(&in_dev->mr_ifc_timer))
949 __in_dev_put(in_dev);
950 /* clear deleted report items */
951 igmpv3_clear_delrec(in_dev);
952 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000953 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000954 } else if (IGMP_V1_SEEN(in_dev)) {
955 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100956 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000957 group = 0;
958 } else if (IGMP_V2_SEEN(in_dev)) {
959 /* this is a v3 query with v2 queriers present;
960 * Interpretation of the max_delay code is problematic here.
961 * A real v2 host would use ih_code directly, while v3 has a
962 * different encoding. We use the v3 encoding as more likely
963 * to be intended in a v3 query.
964 */
965 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800966 if (!max_delay)
967 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 } else { /* v3 */
969 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000970 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900971
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300972 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900974 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700975 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000976 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300977 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
979
980 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
981 if (!max_delay)
982 max_delay = 1; /* can't mod w/ 0 */
983 in_dev->mr_maxdelay = max_delay;
Hangbin Liu966c37f2018-10-26 11:30:35 +0800984
985 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
986 * received value was zero, use the default or statically
987 * configured value.
988 */
989 in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
990 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
991
992 /* RFC3376, 8.3. Query Response Interval:
993 * The number of seconds represented by the [Query Response
994 * Interval] must be less than the [Query Interval].
995 */
996 if (in_dev->mr_qri >= in_dev->mr_qi)
997 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (!group) { /* general query */
1000 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +02001001 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +00001003 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 /* mark sources to include, if group & source-specific */
1006 mark = ih3->nsrcs != 0;
1007 }
1008
1009 /*
1010 * - Start the timers in all of our membership records
1011 * that the query applies to for the interface on
1012 * which the query arrived excl. those that belong
1013 * to a "local" group (224.0.0.X)
1014 * - For timers already running check if they need to
1015 * be reset.
1016 * - Use the igmp->igmp_code field as the maximum
1017 * delay possible
1018 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001019 rcu_read_lock();
1020 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -08001021 int changed;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (group && group != im->multiaddr)
1024 continue;
1025 if (im->multiaddr == IGMP_ALL_HOSTS)
1026 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001027 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001028 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001029 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 spin_lock_bh(&im->lock);
1031 if (im->tm_running)
1032 im->gsquery = im->gsquery && mark;
1033 else
1034 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -08001035 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001036 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -08001038 if (changed)
1039 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001041 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +00001042 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043}
1044
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001045/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046int igmp_rcv(struct sk_buff *skb)
1047{
1048 /* This basically follows the spec line by line -- see RFC1112 */
1049 struct igmphdr *ih;
David Ahernc7b725b2017-08-15 18:38:42 -07001050 struct net_device *dev = skb->dev;
1051 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001053 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
David Ahernc7b725b2017-08-15 18:38:42 -07001055 if (netif_is_l3_master(dev)) {
1056 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1057 if (!dev)
1058 goto drop;
1059 }
1060
1061 in_dev = __in_dev_get_rcu(dev);
Ian Morris51456b22015-04-03 09:17:26 +01001062 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001063 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Herbert Xufb286bb2005-11-10 13:01:24 -08001065 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001066 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001067
Tom Herbertde08dc12014-05-07 16:52:10 -07001068 if (skb_checksum_simple_validate(skb))
1069 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001071 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 switch (ih->type) {
1073 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001074 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 break;
1076 case IGMP_HOST_MEMBERSHIP_REPORT:
1077 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001079 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 break;
David Stevens24c69272005-12-02 20:32:59 -08001081 /* don't rely on MC router hearing unicast reports */
1082 if (skb->pkt_type == PACKET_MULTICAST ||
1083 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001084 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 break;
1086 case IGMP_PIM:
1087#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return pim_rcv_v1(skb);
1089#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001090 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 case IGMP_DVMRP:
1092 case IGMP_TRACE:
1093 case IGMP_HOST_LEAVE_MESSAGE:
1094 case IGMP_MTRACE:
1095 case IGMP_MTRACE_RESP:
1096 break;
1097 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001100
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001101drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001102 if (dropped)
1103 kfree_skb(skb);
1104 else
1105 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 return 0;
1107}
1108
1109#endif
1110
1111
1112/*
1113 * Add a filter to a device
1114 */
1115
Al Viro63007722006-09-27 18:31:32 -07001116static void ip_mc_filter_add(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 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1122 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001123 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 routine. Something sort of:
1125 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1126 --ANK
1127 */
1128 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001129 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/*
1133 * Remove a filter from a device
1134 */
1135
Al Viro63007722006-09-27 18:31:32 -07001136static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
1138 char buf[MAX_ADDR_LEN];
1139 struct net_device *dev = in_dev->dev;
1140
1141 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001142 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145#ifdef CONFIG_IP_MULTICAST
1146/*
1147 * deleted ip_mc_list manipulation
1148 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001149static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
1150 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
1152 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001153 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 /* this is an "ip_mc_list" for convenience; only the fields below
1156 * are actually used. In particular, the refcnt and users are not
1157 * used for management of the delete list. Using the same structure
1158 * for deleted items allows change reports to use common code with
1159 * non-deleted or query-response MCA's.
1160 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001161 pmc = kzalloc(sizeof(*pmc), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!pmc)
1163 return;
WANG Congb4846fc2017-06-20 10:46:27 -07001164 spin_lock_init(&pmc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 spin_lock_bh(&im->lock);
1166 pmc->interface = im->interface;
1167 in_dev_hold(in_dev);
1168 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001169 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 pmc->sfmode = im->sfmode;
1171 if (pmc->sfmode == MCAST_INCLUDE) {
1172 struct ip_sf_list *psf;
1173
1174 pmc->tomb = im->tomb;
1175 pmc->sources = im->sources;
1176 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001177 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 psf->sf_crcount = pmc->crcount;
1179 }
1180 spin_unlock_bh(&im->lock);
1181
1182 spin_lock_bh(&in_dev->mc_tomb_lock);
1183 pmc->next = in_dev->mc_tomb;
1184 in_dev->mc_tomb = pmc;
1185 spin_unlock_bh(&in_dev->mc_tomb_lock);
1186}
1187
Hangbin Liu24803f32016-11-14 16:16:28 +08001188/*
1189 * restore ip_mc_list deleted records
1190 */
1191static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192{
1193 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001194 struct ip_sf_list *psf;
1195 struct net *net = dev_net(in_dev->dev);
1196 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 spin_lock_bh(&in_dev->mc_tomb_lock);
1199 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001200 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (pmc->multiaddr == multiaddr)
1202 break;
1203 pmc_prev = pmc;
1204 }
1205 if (pmc) {
1206 if (pmc_prev)
1207 pmc_prev->next = pmc->next;
1208 else
1209 in_dev->mc_tomb = pmc->next;
1210 }
1211 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001212
1213 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001215 im->interface = pmc->interface;
Hangbin Liu08d3ffc2018-07-20 14:04:27 +08001216 if (im->sfmode == MCAST_INCLUDE) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001217 im->tomb = pmc->tomb;
1218 im->sources = pmc->sources;
1219 for (psf = im->sources; psf; psf = psf->sf_next)
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001220 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1221 } else {
1222 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 in_dev_put(pmc->interface);
Hangbin Liu9c8bb162017-02-08 21:16:45 +08001225 kfree(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001227 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Hangbin Liu24803f32016-11-14 16:16:28 +08001230/*
1231 * flush ip_mc_list deleted records
1232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233static void igmpv3_clear_delrec(struct in_device *in_dev)
1234{
1235 struct ip_mc_list *pmc, *nextpmc;
1236
1237 spin_lock_bh(&in_dev->mc_tomb_lock);
1238 pmc = in_dev->mc_tomb;
1239 in_dev->mc_tomb = NULL;
1240 spin_unlock_bh(&in_dev->mc_tomb_lock);
1241
1242 for (; pmc; pmc = nextpmc) {
1243 nextpmc = pmc->next;
1244 ip_mc_clear_src(pmc);
1245 in_dev_put(pmc->interface);
1246 kfree(pmc);
1247 }
1248 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001249 rcu_read_lock();
1250 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct ip_sf_list *psf, *psf_next;
1252
1253 spin_lock_bh(&pmc->lock);
1254 psf = pmc->tomb;
1255 pmc->tomb = NULL;
1256 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001257 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 psf_next = psf->sf_next;
1259 kfree(psf);
1260 }
1261 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001262 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264#endif
1265
Florian Fainelli9fb20802019-02-01 20:20:52 -08001266static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
1268 struct in_device *in_dev = im->interface;
1269#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001270 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 int reporter;
1272#endif
1273
1274 if (im->loaded) {
1275 im->loaded = 0;
1276 ip_mc_filter_del(in_dev, im->multiaddr);
1277 }
1278
1279#ifdef CONFIG_IP_MULTICAST
1280 if (im->multiaddr == IGMP_ALL_HOSTS)
1281 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001282 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001283 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 reporter = im->reporter;
1286 igmp_stop_timer(im);
1287
1288 if (!in_dev->dead) {
1289 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001290 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (IGMP_V2_SEEN(in_dev)) {
1292 if (reporter)
1293 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001294 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 /* IGMPv3 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001297 igmpv3_add_delrec(in_dev, im, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 igmp_ifc_event(in_dev);
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
Florian Fainelli9fb20802019-02-01 20:20:52 -08001304static void igmp_group_dropped(struct ip_mc_list *im)
1305{
1306 __igmp_group_dropped(im, GFP_KERNEL);
1307}
1308
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001309static void igmp_group_added(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001312#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001313 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001314#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (im->loaded == 0) {
1317 im->loaded = 1;
1318 ip_mc_filter_add(in_dev, im->multiaddr);
1319 }
1320
1321#ifdef CONFIG_IP_MULTICAST
1322 if (im->multiaddr == IGMP_ALL_HOSTS)
1323 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001324 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001325 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (in_dev->dead)
1328 return;
Hangbin Liuff065252018-08-29 18:06:10 +08001329
1330 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1332 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001333 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 spin_unlock_bh(&im->lock);
1335 return;
1336 }
1337 /* else, v3 */
1338
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001339 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1340 * not send filter-mode change record as the mode should be from
1341 * IN() to IN(A).
1342 */
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001343 if (im->sfmode == MCAST_EXCLUDE)
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001344 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 igmp_ifc_event(in_dev);
1347#endif
1348}
1349
1350
1351/*
1352 * Multicast list managers
1353 */
1354
Eric Dumazete9897072013-06-07 08:48:57 -07001355static u32 ip_mc_hash(const struct ip_mc_list *im)
1356{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001357 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001358}
1359
1360static void ip_mc_hash_add(struct in_device *in_dev,
1361 struct ip_mc_list *im)
1362{
1363 struct ip_mc_list __rcu **mc_hash;
1364 u32 hash;
1365
1366 mc_hash = rtnl_dereference(in_dev->mc_hash);
1367 if (mc_hash) {
1368 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001369 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001370 rcu_assign_pointer(mc_hash[hash], im);
1371 return;
1372 }
1373
1374 /* do not use a hash table for small number of items */
1375 if (in_dev->mc_count < 4)
1376 return;
1377
1378 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1379 GFP_KERNEL);
1380 if (!mc_hash)
1381 return;
1382
1383 for_each_pmc_rtnl(in_dev, im) {
1384 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001385 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001386 RCU_INIT_POINTER(mc_hash[hash], im);
1387 }
1388
1389 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1390}
1391
1392static void ip_mc_hash_remove(struct in_device *in_dev,
1393 struct ip_mc_list *im)
1394{
1395 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1396 struct ip_mc_list *aux;
1397
1398 if (!mc_hash)
1399 return;
1400 mc_hash += ip_mc_hash(im);
1401 while ((aux = rtnl_dereference(*mc_hash)) != im)
1402 mc_hash = &aux->next_hash;
1403 *mc_hash = im->next_hash;
1404}
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407/*
1408 * A socket has joined a multicast group on device dev.
1409 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001410static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1411 unsigned int mode, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
1413 struct ip_mc_list *im;
1414
1415 ASSERT_RTNL();
1416
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001417 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 if (im->multiaddr == addr) {
1419 im->users++;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001420 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 goto out;
1422 }
1423 }
1424
Florian Fainelli9fb20802019-02-01 20:20:52 -08001425 im = kzalloc(sizeof(*im), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (!im)
1427 goto out;
1428
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001429 im->users = 1;
1430 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001432 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 /* initial mode is (EX, empty) */
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001434 im->sfmode = mode;
1435 im->sfcount[mode] = 1;
Reshetova, Elena8851ab52017-06-30 13:08:02 +03001436 refcount_set(&im->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 spin_lock_init(&im->lock);
1438#ifdef CONFIG_IP_MULTICAST
Kees Cooke99e88a2017-10-16 14:43:17 -07001439 timer_setup(&im->timer, igmp_timer_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001441
1442 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001443 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001444 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001445
Eric Dumazete9897072013-06-07 08:48:57 -07001446 ip_mc_hash_add(in_dev, im);
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001449 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001451 igmp_group_added(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!in_dev->dead)
1453 ip_rt_multicast_event(in_dev);
1454out:
1455 return;
1456}
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001457
Florian Fainelli9fb20802019-02-01 20:20:52 -08001458void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1459{
1460 ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
1461}
1462EXPORT_SYMBOL(__ip_mc_inc_group);
1463
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001464void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1465{
1466 __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE);
1467}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001468EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001470static int ip_mc_check_iphdr(struct sk_buff *skb)
1471{
1472 const struct iphdr *iph;
1473 unsigned int len;
1474 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1475
1476 if (!pskb_may_pull(skb, offset))
1477 return -EINVAL;
1478
1479 iph = ip_hdr(skb);
1480
1481 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1482 return -EINVAL;
1483
1484 offset += ip_hdrlen(skb) - sizeof(*iph);
1485
1486 if (!pskb_may_pull(skb, offset))
1487 return -EINVAL;
1488
1489 iph = ip_hdr(skb);
1490
1491 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1492 return -EINVAL;
1493
1494 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1495 if (skb->len < len || len < offset)
1496 return -EINVAL;
1497
1498 skb_set_transport_header(skb, offset);
1499
1500 return 0;
1501}
1502
1503static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1504{
1505 unsigned int len = skb_transport_offset(skb);
1506
1507 len += sizeof(struct igmpv3_report);
1508
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001509 return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001510}
1511
1512static int ip_mc_check_igmp_query(struct sk_buff *skb)
1513{
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001514 unsigned int transport_len = ip_transport_len(skb);
1515 unsigned int len;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001516
1517 /* IGMPv{1,2}? */
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001518 if (transport_len != sizeof(struct igmphdr)) {
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001519 /* or IGMPv3? */
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001520 if (transport_len < sizeof(struct igmpv3_query))
1521 return -EINVAL;
1522
1523 len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1524 if (!ip_mc_may_pull(skb, len))
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001525 return -EINVAL;
1526 }
1527
1528 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1529 * all-systems destination addresses (224.0.0.1) for general queries
1530 */
1531 if (!igmp_hdr(skb)->group &&
1532 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1533 return -EINVAL;
1534
1535 return 0;
1536}
1537
1538static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1539{
1540 switch (igmp_hdr(skb)->type) {
1541 case IGMP_HOST_LEAVE_MESSAGE:
1542 case IGMP_HOST_MEMBERSHIP_REPORT:
1543 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1544 /* fall through */
1545 return 0;
1546 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1547 return ip_mc_check_igmp_reportv3(skb);
1548 case IGMP_HOST_MEMBERSHIP_QUERY:
1549 return ip_mc_check_igmp_query(skb);
1550 default:
1551 return -ENOMSG;
1552 }
1553}
1554
1555static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1556{
1557 return skb_checksum_simple_validate(skb);
1558}
1559
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001560static int ip_mc_check_igmp_csum(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001561{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001562 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001563 unsigned int transport_len = ip_transport_len(skb);
1564 struct sk_buff *skb_chk;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001565
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001566 if (!ip_mc_may_pull(skb, len))
1567 return -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001568
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001569 skb_chk = skb_checksum_trimmed(skb, transport_len,
1570 ip_mc_validate_checksum);
1571 if (!skb_chk)
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001572 return -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001573
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001574 if (skb_chk != skb)
Linus Lüssinga5169932015-08-13 05:54:07 +02001575 kfree_skb(skb_chk);
1576
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001577 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001578}
1579
1580/**
1581 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1582 * @skb: the skb to validate
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001583 *
1584 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001585 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001586 *
1587 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1588 * standard
1589 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1590 * -ENOMEM: A memory allocation failure happened.
1591 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001592 * Caller needs to set the skb network header and free any returned skb if it
1593 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001594 */
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001595int ip_mc_check_igmp(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001596{
1597 int ret = ip_mc_check_iphdr(skb);
1598
1599 if (ret < 0)
1600 return ret;
1601
1602 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1603 return -ENOMSG;
1604
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001605 ret = ip_mc_check_igmp_csum(skb);
1606 if (ret < 0)
1607 return ret;
1608
1609 return ip_mc_check_igmp_msg(skb);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001610}
1611EXPORT_SYMBOL(ip_mc_check_igmp);
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001614 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001615 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001616static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001617{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001618#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001619 struct ip_mc_list *im;
1620 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001621 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001622
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001623 ASSERT_RTNL();
1624
1625 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001626 if (im->multiaddr == IGMP_ALL_HOSTS)
1627 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001628 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001629 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001630 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001631
Eric Dumazet866f3b22010-11-18 09:33:19 -08001632 /* a failover is happening and switches
1633 * must be notified immediately
1634 */
1635 if (IGMP_V1_SEEN(in_dev))
1636 type = IGMP_HOST_MEMBERSHIP_REPORT;
1637 else if (IGMP_V2_SEEN(in_dev))
1638 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1639 else
1640 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1641 igmp_send_report(in_dev, im, type);
1642 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001643#endif
1644}
1645
1646/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * A socket has left a multicast group on device dev
1648 */
1649
Florian Fainelli9fb20802019-02-01 20:20:52 -08001650void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001652 struct ip_mc_list *i;
1653 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001656
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001657 for (ip = &in_dev->mc_list;
1658 (i = rtnl_dereference(*ip)) != NULL;
1659 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001660 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001662 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001663 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001664 in_dev->mc_count--;
Florian Fainelli9fb20802019-02-01 20:20:52 -08001665 __igmp_group_dropped(i, gfp);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001666 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 if (!in_dev->dead)
1669 ip_rt_multicast_event(in_dev);
1670
1671 ip_ma_put(i);
1672 return;
1673 }
1674 break;
1675 }
1676 }
1677}
Florian Fainelli9fb20802019-02-01 20:20:52 -08001678EXPORT_SYMBOL(__ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Moni Shoua75c78502009-09-15 02:37:40 -07001680/* Device changing type */
1681
1682void ip_mc_unmap(struct in_device *in_dev)
1683{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001684 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001685
1686 ASSERT_RTNL();
1687
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001688 for_each_pmc_rtnl(in_dev, pmc)
1689 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001690}
1691
1692void ip_mc_remap(struct in_device *in_dev)
1693{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001694 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001695
1696 ASSERT_RTNL();
1697
Hangbin Liu24803f32016-11-14 16:16:28 +08001698 for_each_pmc_rtnl(in_dev, pmc) {
1699#ifdef CONFIG_IP_MULTICAST
1700 igmpv3_del_delrec(in_dev, pmc);
1701#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001702 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001703 }
Moni Shoua75c78502009-09-15 02:37:40 -07001704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/* Device going down */
1707
1708void ip_mc_down(struct in_device *in_dev)
1709{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001710 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 ASSERT_RTNL();
1713
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001714 for_each_pmc_rtnl(in_dev, pmc)
1715 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717#ifdef CONFIG_IP_MULTICAST
1718 in_dev->mr_ifc_count = 0;
1719 if (del_timer(&in_dev->mr_ifc_timer))
1720 __in_dev_put(in_dev);
1721 in_dev->mr_gq_running = 0;
1722 if (del_timer(&in_dev->mr_gq_timer))
1723 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724#endif
1725
1726 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1727}
1728
Hangbin Liu966c37f2018-10-26 11:30:35 +08001729#ifdef CONFIG_IP_MULTICAST
1730static void ip_mc_reset(struct in_device *in_dev)
1731{
1732 struct net *net = dev_net(in_dev->dev);
1733
1734 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1735 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1736 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1737}
1738#else
1739static void ip_mc_reset(struct in_device *in_dev)
1740{
1741}
1742#endif
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744void ip_mc_init_dev(struct in_device *in_dev)
1745{
1746 ASSERT_RTNL();
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748#ifdef CONFIG_IP_MULTICAST
Kees Cooke99e88a2017-10-16 14:43:17 -07001749 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1750 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751#endif
Hangbin Liu966c37f2018-10-26 11:30:35 +08001752 ip_mc_reset(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 spin_lock_init(&in_dev->mc_tomb_lock);
1755}
1756
1757/* Device going up */
1758
1759void ip_mc_up(struct in_device *in_dev)
1760{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001761 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 ASSERT_RTNL();
1764
Hangbin Liu966c37f2018-10-26 11:30:35 +08001765 ip_mc_reset(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1767
Hangbin Liu24803f32016-11-14 16:16:28 +08001768 for_each_pmc_rtnl(in_dev, pmc) {
1769#ifdef CONFIG_IP_MULTICAST
1770 igmpv3_del_delrec(in_dev, pmc);
1771#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001772 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
1776/*
1777 * Device is about to be destroyed: clean up.
1778 */
1779
1780void ip_mc_destroy_dev(struct in_device *in_dev)
1781{
1782 struct ip_mc_list *i;
1783
1784 ASSERT_RTNL();
1785
1786 /* Deactivate timers */
1787 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001788#ifdef CONFIG_IP_MULTICAST
1789 igmpv3_clear_delrec(in_dev);
1790#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001792 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1793 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001794 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001799/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001800static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 struct net_device *dev = NULL;
1803 struct in_device *idev = NULL;
1804
1805 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001806 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 return idev;
1808 }
1809 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001810 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (!dev)
1812 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814
David S. Millerb23dd4f2011-03-02 14:31:35 -08001815 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001816 struct rtable *rt = ip_route_output(net,
1817 imr->imr_multiaddr.s_addr,
1818 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001819 if (!IS_ERR(rt)) {
1820 dev = rt->dst.dev;
1821 ip_rt_put(rt);
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824 if (dev) {
1825 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001826 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828 return idev;
1829}
1830
1831/*
1832 * Join a socket to a group
1833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001836 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
1838 struct ip_sf_list *psf, *psf_prev;
1839 int rv = 0;
1840
1841 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001842 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (psf->sf_inaddr == *psfsrc)
1844 break;
1845 psf_prev = psf;
1846 }
1847 if (!psf || psf->sf_count[sfmode] == 0) {
1848 /* source filter not found, or count wrong => bug */
1849 return -ESRCH;
1850 }
1851 psf->sf_count[sfmode]--;
1852 if (psf->sf_count[sfmode] == 0) {
1853 ip_rt_multicast_event(pmc->interface);
1854 }
1855 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1856#ifdef CONFIG_IP_MULTICAST
1857 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001858 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859#endif
1860
1861 /* no more filters for this source */
1862 if (psf_prev)
1863 psf_prev->sf_next = psf->sf_next;
1864 else
1865 pmc->sources = psf->sf_next;
1866#ifdef CONFIG_IP_MULTICAST
1867 if (psf->sf_oldin &&
1868 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001869 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 psf->sf_next = pmc->tomb;
1871 pmc->tomb = psf;
1872 rv = 1;
1873 } else
1874#endif
1875 kfree(psf);
1876 }
1877 return rv;
1878}
1879
1880#ifndef CONFIG_IP_MULTICAST
1881#define igmp_ifc_event(x) do { } while (0)
1882#endif
1883
Al Viro8f935bb2006-09-27 18:30:07 -07001884static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1885 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
1887 struct ip_mc_list *pmc;
1888 int changerec = 0;
1889 int i, err;
1890
1891 if (!in_dev)
1892 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001893 rcu_read_lock();
1894 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (*pmca == pmc->multiaddr)
1896 break;
1897 }
1898 if (!pmc) {
1899 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001900 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 return -ESRCH;
1902 }
1903 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001904 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905#ifdef CONFIG_IP_MULTICAST
1906 sf_markstate(pmc);
1907#endif
1908 if (!delta) {
1909 err = -EINVAL;
1910 if (!pmc->sfcount[sfmode])
1911 goto out_unlock;
1912 pmc->sfcount[sfmode]--;
1913 }
1914 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001915 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1917
1918 changerec |= rv > 0;
1919 if (!err && rv < 0)
1920 err = rv;
1921 }
1922 if (pmc->sfmode == MCAST_EXCLUDE &&
1923 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1924 pmc->sfcount[MCAST_INCLUDE]) {
1925#ifdef CONFIG_IP_MULTICAST
1926 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001927 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928#endif
1929
1930 /* filter mode change */
1931 pmc->sfmode = MCAST_INCLUDE;
1932#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001933 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001935 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 psf->sf_crcount = 0;
1937 igmp_ifc_event(pmc->interface);
1938 } else if (sf_setstate(pmc) || changerec) {
1939 igmp_ifc_event(pmc->interface);
1940#endif
1941 }
1942out_unlock:
1943 spin_unlock_bh(&pmc->lock);
1944 return err;
1945}
1946
1947/*
1948 * Add multicast single-source filter to the interface list
1949 */
1950static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001951 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
1953 struct ip_sf_list *psf, *psf_prev;
1954
1955 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001956 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (psf->sf_inaddr == *psfsrc)
1958 break;
1959 psf_prev = psf;
1960 }
1961 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001962 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (!psf)
1964 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 psf->sf_inaddr = *psfsrc;
1966 if (psf_prev) {
1967 psf_prev->sf_next = psf;
1968 } else
1969 pmc->sources = psf;
1970 }
1971 psf->sf_count[sfmode]++;
1972 if (psf->sf_count[sfmode] == 1) {
1973 ip_rt_multicast_event(pmc->interface);
1974 }
1975 return 0;
1976}
1977
1978#ifdef CONFIG_IP_MULTICAST
1979static void sf_markstate(struct ip_mc_list *pmc)
1980{
1981 struct ip_sf_list *psf;
1982 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1983
Weilong Chenc71151f2013-12-23 14:37:29 +08001984 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (pmc->sfcount[MCAST_EXCLUDE]) {
1986 psf->sf_oldin = mca_xcount ==
1987 psf->sf_count[MCAST_EXCLUDE] &&
1988 !psf->sf_count[MCAST_INCLUDE];
1989 } else
1990 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1991}
1992
1993static int sf_setstate(struct ip_mc_list *pmc)
1994{
David L Stevensad125832006-01-18 14:20:56 -08001995 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1997 int qrv = pmc->interface->mr_qrv;
1998 int new_in, rv;
1999
2000 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002001 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (pmc->sfcount[MCAST_EXCLUDE]) {
2003 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2004 !psf->sf_count[MCAST_INCLUDE];
2005 } else
2006 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08002007 if (new_in) {
2008 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05002009 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08002010
Weilong Chenc71151f2013-12-23 14:37:29 +08002011 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08002012 if (dpsf->sf_inaddr == psf->sf_inaddr)
2013 break;
2014 prev = dpsf;
2015 }
2016 if (dpsf) {
2017 if (prev)
2018 prev->sf_next = dpsf->sf_next;
2019 else
2020 pmc->tomb = dpsf->sf_next;
2021 kfree(dpsf);
2022 }
2023 psf->sf_crcount = qrv;
2024 rv++;
2025 }
2026 } else if (psf->sf_oldin) {
2027
2028 psf->sf_crcount = 0;
2029 /*
2030 * add or update "delete" records if an active filter
2031 * is now inactive
2032 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002033 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08002034 if (dpsf->sf_inaddr == psf->sf_inaddr)
2035 break;
2036 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00002037 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08002038 if (!dpsf)
2039 continue;
2040 *dpsf = *psf;
2041 /* pmc->lock held by callers */
2042 dpsf->sf_next = pmc->tomb;
2043 pmc->tomb = dpsf;
2044 }
2045 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 rv++;
2047 }
2048 }
2049 return rv;
2050}
2051#endif
2052
2053/*
2054 * Add multicast source filter list to the interface list
2055 */
Al Viro8f935bb2006-09-27 18:30:07 -07002056static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2057 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 struct ip_mc_list *pmc;
2060 int isexclude;
2061 int i, err;
2062
2063 if (!in_dev)
2064 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002065 rcu_read_lock();
2066 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (*pmca == pmc->multiaddr)
2068 break;
2069 }
2070 if (!pmc) {
2071 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002072 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return -ESRCH;
2074 }
2075 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002076 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
2078#ifdef CONFIG_IP_MULTICAST
2079 sf_markstate(pmc);
2080#endif
2081 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2082 if (!delta)
2083 pmc->sfcount[sfmode]++;
2084 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002085 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002086 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (err)
2088 break;
2089 }
2090 if (err) {
2091 int j;
2092
Jun Zhao685f94e62011-11-22 17:19:03 +00002093 if (!delta)
2094 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002095 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002096 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2098#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002100 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002101 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102#endif
2103
2104 /* filter mode change */
2105 if (pmc->sfcount[MCAST_EXCLUDE])
2106 pmc->sfmode = MCAST_EXCLUDE;
2107 else if (pmc->sfcount[MCAST_INCLUDE])
2108 pmc->sfmode = MCAST_INCLUDE;
2109#ifdef CONFIG_IP_MULTICAST
2110 /* else no filters; keep old mode for reports */
2111
Nikolay Borisov165094a2016-02-08 23:29:24 +02002112 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002114 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 psf->sf_crcount = 0;
2116 igmp_ifc_event(in_dev);
2117 } else if (sf_setstate(pmc)) {
2118 igmp_ifc_event(in_dev);
2119#endif
2120 }
2121 spin_unlock_bh(&pmc->lock);
2122 return err;
2123}
2124
2125static void ip_mc_clear_src(struct ip_mc_list *pmc)
2126{
WANG Congc38b7d32017-06-12 09:52:26 -07002127 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
WANG Congc38b7d32017-06-12 09:52:26 -07002129 spin_lock_bh(&pmc->lock);
2130 tomb = pmc->tomb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 pmc->tomb = NULL;
WANG Congc38b7d32017-06-12 09:52:26 -07002132 sources = pmc->sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 pmc->sources = NULL;
2134 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002135 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 pmc->sfcount[MCAST_EXCLUDE] = 1;
WANG Congc38b7d32017-06-12 09:52:26 -07002137 spin_unlock_bh(&pmc->lock);
2138
2139 for (psf = tomb; psf; psf = nextpsf) {
2140 nextpsf = psf->sf_next;
2141 kfree(psf);
2142 }
2143 for (psf = sources; psf; psf = nextpsf) {
2144 nextpsf = psf->sf_next;
2145 kfree(psf);
2146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002149/* Join a multicast group
2150 */
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002151static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2152 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Al Viro8f935bb2006-09-27 18:30:07 -07002154 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002155 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct in_device *in_dev;
2157 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002158 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002159 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002161 int err;
2162
2163 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Joe Perchesf97c1e02007-12-16 13:45:43 -08002165 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return -EINVAL;
2167
Daniel Lezcano877aced2008-08-13 16:15:57 -07002168 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 err = -ENODEV;
2172 goto done;
2173 }
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002176 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002177 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002178 if (i->multi.imr_multiaddr.s_addr == addr &&
2179 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 count++;
2182 }
2183 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002184 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002186 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002187 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002188 goto done;
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002191 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 iml->sflist = NULL;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002193 iml->sfmode = mode;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002194 rcu_assign_pointer(inet->mc_list, iml);
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002195 __ip_mc_inc_group(in_dev, addr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return err;
2199}
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002200
2201/* Join ASM (Any-Source Multicast) group
2202 */
2203int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2204{
2205 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2206}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002207EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002209/* Join SSM (Source-Specific Multicast) group
2210 */
2211int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2212 unsigned int mode)
2213{
2214 return __ip_mc_join_group(sk, imr, mode);
2215}
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2218 struct in_device *in_dev)
2219{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002220 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 int err;
2222
Ian Morris51456b22015-04-03 09:17:26 +01002223 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /* any-source empty exclude case */
2225 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2226 iml->sfmode, 0, NULL, 0);
2227 }
2228 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002229 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002230 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002231 /* decrease mem now to avoid the memleak warning */
2232 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002233 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 return err;
2235}
2236
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002237int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
2239 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002240 struct ip_mc_socklist *iml;
2241 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002242 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002243 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002244 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002245 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002246 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Eric Dumazet959d10f2015-02-17 03:19:24 -08002248 ASSERT_RTNL();
2249
Daniel Lezcano877aced2008-08-13 16:15:57 -07002250 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002251 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002252 ret = -ENODEV;
2253 goto out;
2254 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002255 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002256 for (imlp = &inet->mc_list;
2257 (iml = rtnl_dereference(*imlp)) != NULL;
2258 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002259 if (iml->multi.imr_multiaddr.s_addr != group)
2260 continue;
2261 if (ifindex) {
2262 if (iml->multi.imr_ifindex != ifindex)
2263 continue;
2264 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2265 iml->multi.imr_address.s_addr)
2266 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
David L Stevensacd6e002006-08-17 16:27:39 -07002268 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002270 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002271
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002272 if (in_dev)
2273 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002274
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002275 /* decrease mem now to avoid the memleak warning */
2276 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002277 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 }
dingtianhong52ad3532014-07-02 13:50:48 +08002280out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002281 return ret;
2282}
stephen hemminger193ba922012-10-01 12:32:34 +00002283EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285int ip_mc_source(int add, int omode, struct sock *sk, struct
2286 ip_mreq_source *mreqs, int ifindex)
2287{
2288 int err;
2289 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002290 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 struct ip_mc_socklist *pmc;
2292 struct in_device *in_dev = NULL;
2293 struct inet_sock *inet = inet_sk(sk);
2294 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002295 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002296 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 int i, j, rv;
2298
Joe Perchesf97c1e02007-12-16 13:45:43 -08002299 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return -EINVAL;
2301
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002302 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2305 imr.imr_address.s_addr = mreqs->imr_interface;
2306 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002307 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 if (!in_dev) {
2310 err = -ENODEV;
2311 goto done;
2312 }
2313 err = -EADDRNOTAVAIL;
2314
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002315 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002316 if ((pmc->multi.imr_multiaddr.s_addr ==
2317 imr.imr_multiaddr.s_addr) &&
2318 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 break;
2320 }
David L Stevens917f2f12005-07-08 17:45:16 -07002321 if (!pmc) { /* must have a prior join */
2322 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 /* if a source filter was set, must be the same mode as before */
2326 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002327 if (pmc->sfmode != omode) {
2328 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 } else if (pmc->sfmode != omode) {
2332 /* allow mode switches for empty-set filters */
2333 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002334 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 NULL, 0);
2336 pmc->sfmode = omode;
2337 }
2338
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002339 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (!add) {
2341 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002342 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002344 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002346 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if (rv == 0)
2348 break;
2349 }
2350 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002351 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
David L Stevens8cdaaa12005-07-08 17:39:23 -07002353 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2354 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2355 leavegroup = 1;
2356 goto done;
2357 }
2358
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002360 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 &mreqs->imr_sourceaddr, 1);
2362
Weilong Chenc71151f2013-12-23 14:37:29 +08002363 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 psl->sl_addr[j-1] = psl->sl_addr[j];
2365 psl->sl_count--;
2366 err = 0;
2367 goto done;
2368 }
2369 /* else, add a new source to the filter */
2370
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002371 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 err = -ENOBUFS;
2373 goto done;
2374 }
2375 if (!psl || psl->sl_count == psl->sl_max) {
2376 struct ip_sf_socklist *newpsl;
2377 int count = IP_SFBLOCK;
2378
2379 if (psl)
2380 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002381 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 if (!newpsl) {
2383 err = -ENOBUFS;
2384 goto done;
2385 }
2386 newpsl->sl_max = count;
2387 newpsl->sl_count = count - IP_SFBLOCK;
2388 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002389 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002391 /* decrease mem now to avoid the memleak warning */
2392 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002393 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002395 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002396 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002399 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002401 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (rv == 0)
2403 break;
2404 }
2405 if (rv == 0) /* address already there is an error */
2406 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002407 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 psl->sl_addr[j+1] = psl->sl_addr[j];
2409 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2410 psl->sl_count++;
2411 err = 0;
2412 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002413 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 &mreqs->imr_sourceaddr, 1);
2415done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002416 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002417 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return err;
2419}
2420
2421int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2422{
David L Stevens9951f032005-07-08 17:47:28 -07002423 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002425 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct ip_mc_socklist *pmc;
2427 struct in_device *in_dev;
2428 struct inet_sock *inet = inet_sk(sk);
2429 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002430 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002431 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
Joe Perchesf97c1e02007-12-16 13:45:43 -08002433 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 return -EINVAL;
2435 if (msf->imsf_fmode != MCAST_INCLUDE &&
2436 msf->imsf_fmode != MCAST_EXCLUDE)
2437 return -EINVAL;
2438
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002439 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2442 imr.imr_address.s_addr = msf->imsf_interface;
2443 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002444 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 if (!in_dev) {
2447 err = -ENODEV;
2448 goto done;
2449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
David L Stevens9951f032005-07-08 17:47:28 -07002451 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2452 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2453 leavegroup = 1;
2454 goto done;
2455 }
2456
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002457 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2459 pmc->multi.imr_ifindex == imr.imr_ifindex)
2460 break;
2461 }
David L Stevens917f2f12005-07-08 17:45:16 -07002462 if (!pmc) { /* must have a prior join */
2463 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002467 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2468 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (!newpsl) {
2470 err = -ENOBUFS;
2471 goto done;
2472 }
2473 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2474 memcpy(newpsl->sl_addr, msf->imsf_slist,
2475 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2476 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2477 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2478 if (err) {
2479 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2480 goto done;
2481 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002482 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002484 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2485 msf->imsf_fmode, 0, NULL, 0);
2486 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002487 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (psl) {
2489 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2490 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002491 /* decrease mem now to avoid the memleak warning */
2492 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002493 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 } else
2495 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2496 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002497 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002499 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500done:
David L Stevens9951f032005-07-08 17:47:28 -07002501 if (leavegroup)
2502 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 return err;
2504}
2505
2506int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2507 struct ip_msfilter __user *optval, int __user *optlen)
2508{
2509 int err, len, count, copycount;
2510 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002511 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 struct ip_mc_socklist *pmc;
2513 struct in_device *in_dev;
2514 struct inet_sock *inet = inet_sk(sk);
2515 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002516 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
WANG Cong87e9f032015-11-03 15:41:16 -08002518 ASSERT_RTNL();
2519
Joe Perchesf97c1e02007-12-16 13:45:43 -08002520 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return -EINVAL;
2522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2524 imr.imr_address.s_addr = msf->imsf_interface;
2525 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002526 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 if (!in_dev) {
2529 err = -ENODEV;
2530 goto done;
2531 }
2532 err = -EADDRNOTAVAIL;
2533
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002534 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2536 pmc->multi.imr_ifindex == imr.imr_ifindex)
2537 break;
2538 }
2539 if (!pmc) /* must have a prior join */
2540 goto done;
2541 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002542 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (!psl) {
2544 len = 0;
2545 count = 0;
2546 } else {
2547 count = psl->sl_count;
2548 }
2549 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2550 len = copycount * sizeof(psl->sl_addr[0]);
2551 msf->imsf_numsrc = count;
2552 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2553 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2554 return -EFAULT;
2555 }
2556 if (len &&
2557 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2558 return -EFAULT;
2559 return 0;
2560done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return err;
2562}
2563
2564int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2565 struct group_filter __user *optval, int __user *optlen)
2566{
2567 int err, i, count, copycount;
2568 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002569 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 struct ip_mc_socklist *pmc;
2571 struct inet_sock *inet = inet_sk(sk);
2572 struct ip_sf_socklist *psl;
2573
WANG Cong87e9f032015-11-03 15:41:16 -08002574 ASSERT_RTNL();
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 psin = (struct sockaddr_in *)&gsf->gf_group;
2577 if (psin->sin_family != AF_INET)
2578 return -EINVAL;
2579 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002580 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return -EINVAL;
2582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 err = -EADDRNOTAVAIL;
2584
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002585 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2587 pmc->multi.imr_ifindex == gsf->gf_interface)
2588 break;
2589 }
2590 if (!pmc) /* must have a prior join */
2591 goto done;
2592 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002593 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 count = psl ? psl->sl_count : 0;
2595 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2596 gsf->gf_numsrc = count;
2597 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2598 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2599 return -EFAULT;
2600 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002601 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 struct sockaddr_storage ss;
2603
2604 psin = (struct sockaddr_in *)&ss;
2605 memset(&ss, 0, sizeof(ss));
2606 psin->sin_family = AF_INET;
2607 psin->sin_addr.s_addr = psl->sl_addr[i];
2608 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2609 return -EFAULT;
2610 }
2611 return 0;
2612done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return err;
2614}
2615
2616/*
2617 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2618 */
David Ahern60d9b032017-08-07 08:44:19 -07002619int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2620 int dif, int sdif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
2622 struct inet_sock *inet = inet_sk(sk);
2623 struct ip_mc_socklist *pmc;
2624 struct ip_sf_socklist *psl;
2625 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002626 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002628 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002629 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002630 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002632 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002633 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
David Ahern60d9b032017-08-07 08:44:19 -07002635 (pmc->multi.imr_ifindex == dif ||
2636 (sdif && pmc->multi.imr_ifindex == sdif)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 break;
2638 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002639 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002641 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002642 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002643 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002645 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Weilong Chenc71151f2013-12-23 14:37:29 +08002647 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (psl->sl_addr[i] == rmt_addr)
2649 break;
2650 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002651 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002653 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002655 goto unlock;
2656 ret = 1;
2657unlock:
2658 rcu_read_unlock();
2659out:
2660 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
2663/*
2664 * A socket is closing.
2665 */
2666
2667void ip_mc_drop_socket(struct sock *sk)
2668{
2669 struct inet_sock *inet = inet_sk(sk);
2670 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002671 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Ian Morris51456b22015-04-03 09:17:26 +01002673 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return;
2675
2676 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002677 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002680 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002681 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002682 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002683 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002685 /* decrease mem now to avoid the memleak warning */
2686 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002687 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
2689 rtnl_unlock();
2690}
2691
David S. Millerdbdd9a52011-03-10 16:34:38 -08002692/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002693int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
2695 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002696 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 struct ip_sf_list *psf;
2698 int rv = 0;
2699
Eric Dumazete9897072013-06-07 08:48:57 -07002700 mc_hash = rcu_dereference(in_dev->mc_hash);
2701 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002702 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002703
2704 for (im = rcu_dereference(mc_hash[hash]);
2705 im != NULL;
2706 im = rcu_dereference(im->next_hash)) {
2707 if (im->multiaddr == mc_addr)
2708 break;
2709 }
2710 } else {
2711 for_each_pmc_rcu(in_dev, im) {
2712 if (im->multiaddr == mc_addr)
2713 break;
2714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
2716 if (im && proto == IPPROTO_IGMP) {
2717 rv = 1;
2718 } else if (im) {
2719 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002720 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (psf->sf_inaddr == src_addr)
2722 break;
2723 }
2724 if (psf)
2725 rv = psf->sf_count[MCAST_INCLUDE] ||
2726 psf->sf_count[MCAST_EXCLUDE] !=
2727 im->sfcount[MCAST_EXCLUDE];
2728 else
2729 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2730 } else
2731 rv = 1; /* unspecified source; tentatively allow */
2732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return rv;
2734}
2735
2736#if defined(CONFIG_PROC_FS)
2737struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002738 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 struct net_device *dev;
2740 struct in_device *in_dev;
2741};
2742
2743#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2744
2745static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2746{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002747 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 struct ip_mc_list *im = NULL;
2749 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2750
Pavel Emelianov7562f872007-05-03 15:13:45 -07002751 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002752 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002754
2755 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (!in_dev)
2757 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002758 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 if (im) {
2760 state->in_dev = in_dev;
2761 break;
2762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
2764 return im;
2765}
2766
2767static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2768{
2769 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002770
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002771 im = rcu_dereference(im->next_rcu);
2772 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002773 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (!state->dev) {
2775 state->in_dev = NULL;
2776 break;
2777 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002778 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (!state->in_dev)
2780 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002781 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
2783 return im;
2784}
2785
2786static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2787{
2788 struct ip_mc_list *im = igmp_mc_get_first(seq);
2789 if (im)
2790 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2791 --pos;
2792 return pos ? NULL : im;
2793}
2794
2795static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002796 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797{
stephen hemminger61fbab72009-11-10 07:54:55 +00002798 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2800}
2801
2802static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2803{
2804 struct ip_mc_list *im;
2805 if (v == SEQ_START_TOKEN)
2806 im = igmp_mc_get_first(seq);
2807 else
2808 im = igmp_mc_get_next(seq, v);
2809 ++*pos;
2810 return im;
2811}
2812
2813static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002814 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
2816 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002817
2818 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002820 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821}
2822
2823static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2824{
2825 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002826 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2828 else {
2829 struct ip_mc_list *im = (struct ip_mc_list *)v;
2830 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2831 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002832 long delta;
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834#ifdef CONFIG_IP_MULTICAST
2835 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2836 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2837 "V3";
2838#else
2839 querier = "NONE";
2840#endif
2841
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002842 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002844 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846
Eric Dumazeta399a802012-08-08 21:13:53 +00002847 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002849 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002851 im->tm_running,
2852 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 im->reporter);
2854 }
2855 return 0;
2856}
2857
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002858static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 .start = igmp_mc_seq_start,
2860 .next = igmp_mc_seq_next,
2861 .stop = igmp_mc_seq_stop,
2862 .show = igmp_mc_seq_show,
2863};
2864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002866 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 struct net_device *dev;
2868 struct in_device *idev;
2869 struct ip_mc_list *im;
2870};
2871
2872#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2873
2874static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2875{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002876 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 struct ip_sf_list *psf = NULL;
2878 struct ip_mc_list *im = NULL;
2879 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2880
Pavel Emelianov7562f872007-05-03 15:13:45 -07002881 state->idev = NULL;
2882 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002883 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002885 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002886 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002888 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002889 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 spin_lock_bh(&im->lock);
2891 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002892 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 state->im = im;
2894 state->idev = idev;
2895 break;
2896 }
2897 spin_unlock_bh(&im->lock);
2898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 }
2900 return psf;
2901}
2902
2903static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2904{
2905 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2906
2907 psf = psf->sf_next;
2908 while (!psf) {
2909 spin_unlock_bh(&state->im->lock);
2910 state->im = state->im->next;
2911 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002912 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (!state->dev) {
2914 state->idev = NULL;
2915 goto out;
2916 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002917 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 if (!state->idev)
2919 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002920 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 }
2922 if (!state->im)
2923 break;
2924 spin_lock_bh(&state->im->lock);
2925 psf = state->im->sources;
2926 }
2927out:
2928 return psf;
2929}
2930
2931static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2932{
2933 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2934 if (psf)
2935 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2936 --pos;
2937 return pos ? NULL : psf;
2938}
2939
2940static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002941 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
stephen hemminger61fbab72009-11-10 07:54:55 +00002943 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2945}
2946
2947static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2948{
2949 struct ip_sf_list *psf;
2950 if (v == SEQ_START_TOKEN)
2951 psf = igmp_mcf_get_first(seq);
2952 else
2953 psf = igmp_mcf_get_next(seq, v);
2954 ++*pos;
2955 return psf;
2956}
2957
2958static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002959 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960{
2961 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002962 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 spin_unlock_bh(&state->im->lock);
2964 state->im = NULL;
2965 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002966 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002968 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969}
2970
2971static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2972{
2973 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2974 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2975
2976 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002977 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 } else {
2979 seq_printf(seq,
2980 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002981 "0x%08x %6lu %6lu\n",
2982 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 ntohl(state->im->multiaddr),
2984 ntohl(psf->sf_inaddr),
2985 psf->sf_count[MCAST_INCLUDE],
2986 psf->sf_count[MCAST_EXCLUDE]);
2987 }
2988 return 0;
2989}
2990
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002991static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 .start = igmp_mcf_seq_start,
2993 .next = igmp_mcf_seq_next,
2994 .stop = igmp_mcf_seq_stop,
2995 .show = igmp_mcf_seq_show,
2996};
2997
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002998static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002999{
3000 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08003001 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003002
Christoph Hellwigc3506372018-04-10 19:42:55 +02003003 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3004 sizeof(struct igmp_mc_iter_state));
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003005 if (!pde)
3006 goto out_igmp;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003007 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3008 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003009 if (!pde)
3010 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08003011 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3012 SOCK_DGRAM, 0, net);
3013 if (err < 0) {
3014 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3015 err);
3016 goto out_sock;
3017 }
3018
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003019 return 0;
3020
Madhu Challa93a714d2015-02-25 09:58:35 -08003021out_sock:
3022 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003023out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00003024 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003025out_igmp:
3026 return -ENOMEM;
3027}
3028
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003029static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003030{
Gao fengece31ff2013-02-18 01:34:56 +00003031 remove_proc_entry("mcfilter", net->proc_net);
3032 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08003033 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003034}
3035
3036static struct pernet_operations igmp_net_ops = {
3037 .init = igmp_net_init,
3038 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039};
WANG Cong72c1d3b2014-01-10 16:09:45 -08003040#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003042static int igmp_netdev_event(struct notifier_block *this,
3043 unsigned long event, void *ptr)
3044{
3045 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3046 struct in_device *in_dev;
3047
3048 switch (event) {
3049 case NETDEV_RESEND_IGMP:
3050 in_dev = __in_dev_get_rtnl(dev);
3051 if (in_dev)
3052 ip_mc_rejoin_groups(in_dev);
3053 break;
3054 default:
3055 break;
3056 }
3057 return NOTIFY_DONE;
3058}
3059
3060static struct notifier_block igmp_notifier = {
3061 .notifier_call = igmp_netdev_event,
3062};
3063
WANG Cong72c1d3b2014-01-10 16:09:45 -08003064int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003066#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003067 int err;
3068
3069 err = register_pernet_subsys(&igmp_net_ops);
3070 if (err)
3071 return err;
3072 err = register_netdevice_notifier(&igmp_notifier);
3073 if (err)
3074 goto reg_notif_fail;
3075 return 0;
3076
3077reg_notif_fail:
3078 unregister_pernet_subsys(&igmp_net_ops);
3079 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003080#else
3081 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003083}