blob: c0cc3171badc54b3d9bff1411698c8021131d101 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Linux NET3: Internet Group Management Protocol [IGMP]
4 *
5 * This code implements the IGMP protocol as defined in RFC1112. There has
6 * been a further revision of this protocol since which is now supported.
7 *
8 * If you have trouble with this module be careful what gcc you have used,
9 * the older version didn't come out right using gcc 2.5.8, the newer one
10 * seems to fall out with gcc 2.6.2.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Authors:
Alan Cox113aa832008-10-13 19:01:08 -070013 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Fixes:
16 *
17 * Alan Cox : Added lots of __inline__ to optimise
18 * the memory usage of all the tiny little
19 * functions.
20 * Alan Cox : Dumped the header building experiment.
21 * Alan Cox : Minor tweaks ready for multicast routing
22 * and extended IGMP protocol.
23 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
24 * writes utterly bogus code otherwise (sigh)
25 * fixed IGMP loopback to behave in the manner
26 * desired by mrouted, fixed the fact it has been
27 * broken since 1.3.6 and cleaned up a few minor
28 * points.
29 *
30 * Chih-Jen Chang : Tried to revise IGMP to Version 2
31 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090032 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * ipmulti-3.5 source code.
34 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
35 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
36 * the mrouted version on that device.
37 * Chih-Jen Chang : Added the max_resp_time parameter to
38 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
39 * to identify the multicast router version
40 * and do what the IGMP version 2 specified.
41 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
42 * Tsu-Sheng Tsao if the specified time expired.
43 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
44 * Alan Cox : Use GFP_ATOMIC in the right places.
45 * Christian Daudt : igmp timer wasn't set for local group
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090046 * memberships but was being deleted,
47 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * from %p with timer not initialized\n"
49 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * igmp_timer_expire function (960205).
52 * Christian Daudt : igmp_heard_report now only calls
53 * igmp_timer_expire if tm->running is
54 * true (960216).
55 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
56 * igmp_heard_query never trigger. Expiry
57 * miscalculation fixed in igmp_heard_query
58 * and random() made to return unsigned to
59 * prevent negative expiry times.
60 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
61 * fix from pending 2.1.x patches.
62 * Alan Cox: Forget to enable FDDI support earlier.
63 * Alexey Kuznetsov: Fixed leaving groups on device down.
64 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
65 * David L Stevens: IGMPv3 support, with help from
66 * Vinay Kulkarni
67 */
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090070#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080071#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/types.h>
73#include <linux/kernel.h>
74#include <linux/jiffies.h>
75#include <linux/string.h>
76#include <linux/socket.h>
77#include <linux/sockios.h>
78#include <linux/in.h>
79#include <linux/inet.h>
80#include <linux/netdevice.h>
81#include <linux/skbuff.h>
82#include <linux/inetdevice.h>
83#include <linux/igmp.h>
84#include <linux/if_arp.h>
85#include <linux/rtnetlink.h>
86#include <linux/times.h>
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +020087#include <linux/pkt_sched.h>
Kevin Cernekeea46182b2017-12-11 11:13:45 -080088#include <linux/byteorder/generic.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020089
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020090#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020091#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <net/ip.h>
93#include <net/protocol.h>
94#include <net/route.h>
95#include <net/sock.h>
96#include <net/checksum.h>
Madhu Challa93a714d2015-02-25 09:58:35 -080097#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/netfilter_ipv4.h>
99#ifdef CONFIG_IP_MROUTE
100#include <linux/mroute.h>
101#endif
102#ifdef CONFIG_PROC_FS
103#include <linux/proc_fs.h>
104#include <linux/seq_file.h>
105#endif
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#ifdef CONFIG_IP_MULTICAST
108/* Parameter names and values are taken from igmp-v2-06 draft */
109
Fabian Frederick436f7c22014-11-04 20:52:14 +0100110#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
111#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
Hangbin Liu966c37f2018-10-26 11:30:35 +0800112#define IGMP_QUERY_INTERVAL (125*HZ)
Fabian Frederick436f7c22014-11-04 20:52:14 +0100113#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Fabian Frederick436f7c22014-11-04 20:52:14 +0100115#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Fabian Frederick436f7c22014-11-04 20:52:14 +0100117/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 * IGMP specs require to report membership immediately after
119 * joining a group, but we delay the first report by a
120 * small interval. It seems more natural and still does not
121 * contradict to specs provided this delay is small enough.
122 */
123
Herbert Xu42f811b2007-06-04 23:34:44 -0700124#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900125 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700126 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
127 ((in_dev)->mr_v1_seen && \
128 time_before(jiffies, (in_dev)->mr_v1_seen)))
129#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900130 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700131 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
132 ((in_dev)->mr_v2_seen && \
133 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
William Manleycab70042013-08-06 19:03:13 +0100135static int unsolicited_report_interval(struct in_device *in_dev)
136{
William Manley26900482013-08-06 19:03:15 +0100137 int interval_ms, interval_jiffies;
138
William Manleycab70042013-08-06 19:03:13 +0100139 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100140 interval_ms = IN_DEV_CONF_GET(
141 in_dev,
142 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100143 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100144 interval_ms = IN_DEV_CONF_GET(
145 in_dev,
146 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
147
148 interval_jiffies = msecs_to_jiffies(interval_ms);
149
150 /* _timer functions can't handle a delay of 0 jiffies so ensure
151 * we always return a positive value.
152 */
153 if (interval_jiffies <= 0)
154 interval_jiffies = 1;
155 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100156}
157
Florian Fainelli9fb20802019-02-01 20:20:52 -0800158static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
159 gfp_t gfp);
Hangbin Liu24803f32016-11-14 16:16:28 +0800160static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static void igmpv3_clear_delrec(struct in_device *in_dev);
162static int sf_setstate(struct ip_mc_list *pmc);
163static void sf_markstate(struct ip_mc_list *pmc);
164#endif
165static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700166static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
167 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169static void ip_ma_put(struct ip_mc_list *im)
170{
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300171 if (refcount_dec_and_test(&im->refcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800173 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175}
176
David S. Millerd9aa9382010-11-15 08:52:02 -0800177#define for_each_pmc_rcu(in_dev, pmc) \
178 for (pmc = rcu_dereference(in_dev->mc_list); \
179 pmc != NULL; \
180 pmc = rcu_dereference(pmc->next_rcu))
181
182#define for_each_pmc_rtnl(in_dev, pmc) \
183 for (pmc = rtnl_dereference(in_dev->mc_list); \
184 pmc != NULL; \
185 pmc = rtnl_dereference(pmc->next_rcu))
186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#ifdef CONFIG_IP_MULTICAST
188
189/*
190 * Timer management
191 */
192
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000193static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 spin_lock_bh(&im->lock);
196 if (del_timer(&im->timer))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300197 refcount_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800198 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 im->reporter = 0;
200 im->unsolicit_count = 0;
201 spin_unlock_bh(&im->lock);
202}
203
204/* It must be called with locked im->lock */
205static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
206{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500207 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800209 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (!mod_timer(&im->timer, jiffies+tv+2))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300211 refcount_inc(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
214static void igmp_gq_start_timer(struct in_device *in_dev)
215{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500216 int tv = prandom_u32() % in_dev->mr_maxdelay;
Michal Tesar7ababb72017-01-02 14:38:36 +0100217 unsigned long exp = jiffies + tv + 2;
218
219 if (in_dev->mr_gq_running &&
220 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
221 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 in_dev->mr_gq_running = 1;
Michal Tesar7ababb72017-01-02 14:38:36 +0100224 if (!mod_timer(&in_dev->mr_gq_timer, exp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 in_dev_hold(in_dev);
226}
227
228static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
229{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500230 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
233 in_dev_hold(in_dev);
234}
235
236static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
237{
238 spin_lock_bh(&im->lock);
239 im->unsolicit_count = 0;
240 if (del_timer(&im->timer)) {
241 if ((long)(im->timer.expires-jiffies) < max_delay) {
242 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800243 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 spin_unlock_bh(&im->lock);
245 return;
246 }
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300247 refcount_dec(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 igmp_start_timer(im, max_delay);
250 spin_unlock_bh(&im->lock);
251}
252
253
254/*
255 * Send an IGMP report.
256 */
257
258#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
259
260
261static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
262 int gdeleted, int sdeleted)
263{
264 switch (type) {
265 case IGMPV3_MODE_IS_INCLUDE:
266 case IGMPV3_MODE_IS_EXCLUDE:
267 if (gdeleted || sdeleted)
268 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800269 if (!(pmc->gsquery && !psf->sf_gsresp)) {
270 if (pmc->sfmode == MCAST_INCLUDE)
271 return 1;
272 /* don't include if this source is excluded
273 * in all filters
274 */
275 if (psf->sf_count[MCAST_INCLUDE])
276 return type == IGMPV3_MODE_IS_INCLUDE;
277 return pmc->sfcount[MCAST_EXCLUDE] ==
278 psf->sf_count[MCAST_EXCLUDE];
279 }
280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 case IGMPV3_CHANGE_TO_INCLUDE:
282 if (gdeleted || sdeleted)
283 return 0;
284 return psf->sf_count[MCAST_INCLUDE] != 0;
285 case IGMPV3_CHANGE_TO_EXCLUDE:
286 if (gdeleted || sdeleted)
287 return 0;
288 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
289 psf->sf_count[MCAST_INCLUDE])
290 return 0;
291 return pmc->sfcount[MCAST_EXCLUDE] ==
292 psf->sf_count[MCAST_EXCLUDE];
293 case IGMPV3_ALLOW_NEW_SOURCES:
294 if (gdeleted || !psf->sf_crcount)
295 return 0;
296 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
297 case IGMPV3_BLOCK_OLD_SOURCES:
298 if (pmc->sfmode == MCAST_INCLUDE)
299 return gdeleted || (psf->sf_crcount && sdeleted);
300 return psf->sf_crcount && !gdeleted && !sdeleted;
301 }
302 return 0;
303}
304
305static int
306igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
307{
308 struct ip_sf_list *psf;
309 int scount = 0;
310
Weilong Chenc71151f2013-12-23 14:37:29 +0800311 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
313 continue;
314 scount++;
315 }
316 return scount;
317}
318
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800319/* source address selection per RFC 3376 section 4.2.13 */
320static __be32 igmpv3_get_srcaddr(struct net_device *dev,
321 const struct flowi4 *fl4)
322{
323 struct in_device *in_dev = __in_dev_get_rcu(dev);
324
325 if (!in_dev)
326 return htonl(INADDR_ANY);
327
328 for_ifa(in_dev) {
Felix Fietkauad23b752018-01-19 11:50:46 +0100329 if (fl4->saddr == ifa->ifa_local)
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800330 return fl4->saddr;
331 } endfor_ifa(in_dev);
332
333 return htonl(INADDR_ANY);
334}
335
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100336static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 struct sk_buff *skb;
339 struct rtable *rt;
340 struct iphdr *pip;
341 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700342 struct net *net = dev_net(dev);
David S. Miller31e4543d2011-05-03 20:25:42 -0700343 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000344 int hlen = LL_RESERVED_SPACE(dev);
345 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100346 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000348 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000349 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000350 GFP_ATOMIC | __GFP_NOWARN);
351 if (skb)
352 break;
353 size >>= 1;
354 if (size < 256)
355 return NULL;
356 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200357 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
David S. Miller31e4543d2011-05-03 20:25:42 -0700359 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500360 0, 0,
361 IPPROTO_IGMP, 0, dev->ifindex);
362 if (IS_ERR(rt)) {
363 kfree_skb(skb);
364 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Changli Gaod8d1f302010-06-10 23:31:35 -0700367 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 skb->dev = dev;
369
Herbert Xu66088242011-11-18 02:20:04 +0000370 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800371 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300373 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700374 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300375 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 pip->version = 4;
378 pip->ihl = (sizeof(struct iphdr)+4)>>2;
379 pip->tos = 0xc0;
380 pip->frag_off = htons(IP_DF);
381 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700382 pip->daddr = fl4.daddr;
Eric Dumazete7aadb22018-02-01 10:26:57 -0800383
384 rcu_read_lock();
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800385 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
Eric Dumazete7aadb22018-02-01 10:26:57 -0800386 rcu_read_unlock();
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 pip->protocol = IPPROTO_IGMP;
389 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100390 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000391 ((u8 *)&pip[1])[0] = IPOPT_RA;
392 ((u8 *)&pip[1])[1] = 4;
393 ((u8 *)&pip[1])[2] = 0;
394 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700396 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300397 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300398 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
400 pig->resv1 = 0;
401 pig->csum = 0;
402 pig->resv2 = 0;
403 pig->ngrec = 0;
404 return skb;
405}
406
407static int igmpv3_sendpack(struct sk_buff *skb)
408{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300409 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000410 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300412 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Eric W. Biederman33224b12015-10-07 16:48:46 -0500414 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
418{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800419 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
422static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
Eric Dumazetb5476022017-12-11 07:17:39 -0800423 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct net_device *dev = pmc->interface->dev;
426 struct igmpv3_report *pih;
427 struct igmpv3_grec *pgr;
428
Eric Dumazetb5476022017-12-11 07:17:39 -0800429 if (!skb) {
430 skb = igmpv3_newpack(dev, mtu);
431 if (!skb)
432 return NULL;
433 }
Johannes Berg4df864c2017-06-16 14:29:21 +0200434 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 pgr->grec_type = type;
436 pgr->grec_auxwords = 0;
437 pgr->grec_nsrcs = 0;
438 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300439 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 pih->ngrec = htons(ntohs(pih->ngrec)+1);
441 *ppgr = pgr;
442 return skb;
443}
444
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100445#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
448 int type, int gdeleted, int sdeleted)
449{
450 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200451 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct igmpv3_report *pih;
453 struct igmpv3_grec *pgr = NULL;
454 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800455 int scount, stotal, first, isquery, truncate;
Eric Dumazetb5476022017-12-11 07:17:39 -0800456 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 if (pmc->multiaddr == IGMP_ALL_HOSTS)
459 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200460 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100461 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Eric Dumazetb5476022017-12-11 07:17:39 -0800463 mtu = READ_ONCE(dev->mtu);
464 if (mtu < IPV4_MIN_MTU)
465 return skb;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
468 type == IGMPV3_MODE_IS_EXCLUDE;
469 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
470 type == IGMPV3_CHANGE_TO_EXCLUDE;
471
David L Stevensad125832006-01-18 14:20:56 -0800472 stotal = scount = 0;
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
475
David L Stevensad125832006-01-18 14:20:56 -0800476 if (!*psf_list)
477 goto empty_source;
478
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300479 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 /* EX and TO_EX get a fresh packet, if needed */
482 if (truncate) {
483 if (pih && pih->ngrec &&
484 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
485 if (skb)
486 igmpv3_sendpack(skb);
Eric Dumazetb5476022017-12-11 07:17:39 -0800487 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489 }
490 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800492 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700493 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 psf_next = psf->sf_next;
496
497 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
498 psf_prev = psf;
499 continue;
500 }
501
Hangbin Liua0525172016-08-02 18:02:57 +0800502 /* Based on RFC3376 5.1. Should not send source-list change
503 * records when there is a filter mode change.
504 */
505 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
506 (!gdeleted && pmc->crcount)) &&
507 (type == IGMPV3_ALLOW_NEW_SOURCES ||
508 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
509 goto decrease_sf_crcount;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* clear marks on query responses */
512 if (isquery)
513 psf->sf_gsresp = 0;
514
Al Viro63007722006-09-27 18:31:32 -0700515 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 first*sizeof(struct igmpv3_grec)) {
517 if (truncate && !first)
518 break; /* truncate these */
519 if (pgr)
520 pgr->grec_nsrcs = htons(scount);
521 if (skb)
522 igmpv3_sendpack(skb);
Eric Dumazetb5476022017-12-11 07:17:39 -0800523 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 first = 1;
525 scount = 0;
526 }
527 if (first) {
Eric Dumazetb5476022017-12-11 07:17:39 -0800528 skb = add_grhead(skb, pmc, type, &pgr, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 first = 0;
530 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800531 if (!skb)
532 return NULL;
Johannes Berg4df864c2017-06-16 14:29:21 +0200533 psrc = skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800535 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
537 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
Hangbin Liua0525172016-08-02 18:02:57 +0800538decrease_sf_crcount:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 psf->sf_crcount--;
540 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
541 if (psf_prev)
542 psf_prev->sf_next = psf->sf_next;
543 else
544 *psf_list = psf->sf_next;
545 kfree(psf);
546 continue;
547 }
548 }
549 psf_prev = psf;
550 }
David L Stevensad125832006-01-18 14:20:56 -0800551
552empty_source:
553 if (!stotal) {
554 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
555 type == IGMPV3_BLOCK_OLD_SOURCES)
556 return skb;
557 if (pmc->crcount || isquery) {
558 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800559 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800560 igmpv3_sendpack(skb);
561 skb = NULL; /* add_grhead will get a new one */
562 }
Eric Dumazetb5476022017-12-11 07:17:39 -0800563 skb = add_grhead(skb, pmc, type, &pgr, mtu);
David L Stevensad125832006-01-18 14:20:56 -0800564 }
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (pgr)
567 pgr->grec_nsrcs = htons(scount);
568
569 if (isquery)
570 pmc->gsquery = 0; /* clear query state on report */
571 return skb;
572}
573
574static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
575{
576 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200577 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int type;
579
580 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000581 rcu_read_lock();
582 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (pmc->multiaddr == IGMP_ALL_HOSTS)
584 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100585 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200586 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100587 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 spin_lock_bh(&pmc->lock);
589 if (pmc->sfcount[MCAST_EXCLUDE])
590 type = IGMPV3_MODE_IS_EXCLUDE;
591 else
592 type = IGMPV3_MODE_IS_INCLUDE;
593 skb = add_grec(skb, pmc, type, 0, 0);
594 spin_unlock_bh(&pmc->lock);
595 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000596 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 } else {
598 spin_lock_bh(&pmc->lock);
599 if (pmc->sfcount[MCAST_EXCLUDE])
600 type = IGMPV3_MODE_IS_EXCLUDE;
601 else
602 type = IGMPV3_MODE_IS_INCLUDE;
603 skb = add_grec(skb, pmc, type, 0, 0);
604 spin_unlock_bh(&pmc->lock);
605 }
606 if (!skb)
607 return 0;
608 return igmpv3_sendpack(skb);
609}
610
611/*
612 * remove zero-count source records from a source filter list
613 */
614static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
615{
616 struct ip_sf_list *psf_prev, *psf_next, *psf;
617
618 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800619 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 psf_next = psf->sf_next;
621 if (psf->sf_crcount == 0) {
622 if (psf_prev)
623 psf_prev->sf_next = psf->sf_next;
624 else
625 *ppsf = psf->sf_next;
626 kfree(psf);
627 } else
628 psf_prev = psf;
629 }
630}
631
632static void igmpv3_send_cr(struct in_device *in_dev)
633{
634 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
635 struct sk_buff *skb = NULL;
636 int type, dtype;
637
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000638 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 spin_lock_bh(&in_dev->mc_tomb_lock);
640
641 /* deleted MCA's */
642 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800643 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 pmc_next = pmc->next;
645 if (pmc->sfmode == MCAST_INCLUDE) {
646 type = IGMPV3_BLOCK_OLD_SOURCES;
647 dtype = IGMPV3_BLOCK_OLD_SOURCES;
648 skb = add_grec(skb, pmc, type, 1, 0);
649 skb = add_grec(skb, pmc, dtype, 1, 1);
650 }
651 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (pmc->sfmode == MCAST_EXCLUDE) {
653 type = IGMPV3_CHANGE_TO_INCLUDE;
654 skb = add_grec(skb, pmc, type, 1, 0);
655 }
David L Stevensad125832006-01-18 14:20:56 -0800656 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (pmc->crcount == 0) {
658 igmpv3_clear_zeros(&pmc->tomb);
659 igmpv3_clear_zeros(&pmc->sources);
660 }
661 }
662 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
663 if (pmc_prev)
664 pmc_prev->next = pmc_next;
665 else
666 in_dev->mc_tomb = pmc_next;
667 in_dev_put(pmc->interface);
668 kfree(pmc);
669 } else
670 pmc_prev = pmc;
671 }
672 spin_unlock_bh(&in_dev->mc_tomb_lock);
673
674 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000675 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 spin_lock_bh(&pmc->lock);
677 if (pmc->sfcount[MCAST_EXCLUDE]) {
678 type = IGMPV3_BLOCK_OLD_SOURCES;
679 dtype = IGMPV3_ALLOW_NEW_SOURCES;
680 } else {
681 type = IGMPV3_ALLOW_NEW_SOURCES;
682 dtype = IGMPV3_BLOCK_OLD_SOURCES;
683 }
684 skb = add_grec(skb, pmc, type, 0, 0);
685 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
686
687 /* filter mode changes */
688 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 if (pmc->sfmode == MCAST_EXCLUDE)
690 type = IGMPV3_CHANGE_TO_EXCLUDE;
691 else
692 type = IGMPV3_CHANGE_TO_INCLUDE;
693 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800694 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696 spin_unlock_bh(&pmc->lock);
697 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000698 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 if (!skb)
701 return;
702 (void) igmpv3_sendpack(skb);
703}
704
705static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
706 int type)
707{
708 struct sk_buff *skb;
709 struct iphdr *iph;
710 struct igmphdr *ih;
711 struct rtable *rt;
712 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700713 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700714 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e4543d2011-05-03 20:25:42 -0700715 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700716 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000717 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
720 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100721
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200722 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100723 return 0;
724
725 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 dst = IGMP_ALL_ROUTER;
727 else
728 dst = group;
729
David S. Miller31e4543d2011-05-03 20:25:42 -0700730 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500731 0, 0,
732 IPPROTO_IGMP, 0, dev->ifindex);
733 if (IS_ERR(rt))
734 return -1;
735
Herbert Xu66088242011-11-18 02:20:04 +0000736 hlen = LL_RESERVED_SPACE(dev);
737 tlen = dev->needed_tailroom;
738 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100739 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 ip_rt_put(rt);
741 return -1;
742 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200743 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Changli Gaod8d1f302010-06-10 23:31:35 -0700745 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Herbert Xu66088242011-11-18 02:20:04 +0000747 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300749 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700750 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300751 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 iph->version = 4;
754 iph->ihl = (sizeof(struct iphdr)+4)>>2;
755 iph->tos = 0xc0;
756 iph->frag_off = htons(IP_DF);
757 iph->ttl = 1;
758 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700759 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100761 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000762 ((u8 *)&iph[1])[0] = IPOPT_RA;
763 ((u8 *)&iph[1])[1] = 4;
764 ((u8 *)&iph[1])[2] = 0;
765 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Johannes Berg4df864c2017-06-16 14:29:21 +0200767 ih = skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800768 ih->type = type;
769 ih->code = 0;
770 ih->csum = 0;
771 ih->group = group;
772 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Eric W. Biederman33224b12015-10-07 16:48:46 -0500774 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Kees Cooke99e88a2017-10-16 14:43:17 -0700777static void igmp_gq_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Kees Cooke99e88a2017-10-16 14:43:17 -0700779 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 in_dev->mr_gq_running = 0;
782 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700783 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Kees Cooke99e88a2017-10-16 14:43:17 -0700786static void igmp_ifc_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Kees Cooke99e88a2017-10-16 14:43:17 -0700788 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 igmpv3_send_cr(in_dev);
791 if (in_dev->mr_ifc_count) {
792 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100793 igmp_ifc_start_timer(in_dev,
794 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700796 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799static void igmp_ifc_event(struct in_device *in_dev)
800{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200801 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
803 return;
Nikolay Borisov165094a2016-02-08 23:29:24 +0200804 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 igmp_ifc_start_timer(in_dev, 1);
806}
807
808
Kees Cooke99e88a2017-10-16 14:43:17 -0700809static void igmp_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Kees Cooke99e88a2017-10-16 14:43:17 -0700811 struct ip_mc_list *im = from_timer(im, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct in_device *in_dev = im->interface;
813
814 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800815 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Hangbin Liu4fb72532018-08-29 18:06:08 +0800817 if (im->unsolicit_count && --im->unsolicit_count)
William Manleycab70042013-08-06 19:03:13 +0100818 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Hangbin Liu4fb72532018-08-29 18:06:08 +0800819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 im->reporter = 1;
821 spin_unlock(&im->lock);
822
823 if (IGMP_V1_SEEN(in_dev))
824 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
825 else if (IGMP_V2_SEEN(in_dev))
826 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
827 else
828 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
829
830 ip_ma_put(im);
831}
832
David L Stevensad125832006-01-18 14:20:56 -0800833/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700834static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 struct ip_sf_list *psf;
837 int i, scount;
838
839 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800840 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (scount == nsrcs)
842 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800843 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800844 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000845 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800846 pmc->sfcount[MCAST_EXCLUDE] !=
847 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800848 break;
David L Stevensad125832006-01-18 14:20:56 -0800849 if (srcs[i] == psf->sf_inaddr) {
850 scount++;
851 break;
852 }
853 }
854 }
855 pmc->gsquery = 0;
856 if (scount == nsrcs) /* all sources excluded */
857 return 0;
858 return 1;
859}
860
Al Viroea4d9e72006-09-27 18:30:52 -0700861static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800862{
863 struct ip_sf_list *psf;
864 int i, scount;
865
866 if (pmc->sfmode == MCAST_EXCLUDE)
867 return igmp_xmarksources(pmc, nsrcs, srcs);
868
869 /* mark INCLUDE-mode sources */
870 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800871 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800872 if (scount == nsrcs)
873 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800874 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (srcs[i] == psf->sf_inaddr) {
876 psf->sf_gsresp = 1;
877 scount++;
878 break;
879 }
880 }
David L Stevensad125832006-01-18 14:20:56 -0800881 if (!scount) {
882 pmc->gsquery = 0;
883 return 0;
884 }
885 pmc->gsquery = 1;
886 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
Eric Dumazetd679c532012-09-06 20:37:06 +0000889/* return true if packet was dropped */
890static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200893 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* Timers are only set for non-local groups */
896
897 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000898 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200899 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100900 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000902 rcu_read_lock();
903 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (im->multiaddr == group) {
905 igmp_stop_timer(im);
906 break;
907 }
908 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000909 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000910 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Eric Dumazetd679c532012-09-06 20:37:06 +0000913/* return true if packet was dropped */
914static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 int len)
916{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300917 struct igmphdr *ih = igmp_hdr(skb);
918 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700920 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 int max_delay;
922 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200923 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925
David Stevens5b7c8402010-09-30 14:29:40 +0000926 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (ih->code == 0) {
928 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900929
Fabian Frederick436f7c22014-11-04 20:52:14 +0100930 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 in_dev->mr_v1_seen = jiffies +
Hangbin Liu966c37f2018-10-26 11:30:35 +0800932 (in_dev->mr_qrv * in_dev->mr_qi) +
933 in_dev->mr_qri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 group = 0;
935 } else {
936 /* v2 router present */
937 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
938 in_dev->mr_v2_seen = jiffies +
Hangbin Liu966c37f2018-10-26 11:30:35 +0800939 (in_dev->mr_qrv * in_dev->mr_qi) +
940 in_dev->mr_qri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
942 /* cancel the interface change timer */
943 in_dev->mr_ifc_count = 0;
944 if (del_timer(&in_dev->mr_ifc_timer))
945 __in_dev_put(in_dev);
946 /* clear deleted report items */
947 igmpv3_clear_delrec(in_dev);
948 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000949 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000950 } else if (IGMP_V1_SEEN(in_dev)) {
951 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100952 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000953 group = 0;
954 } else if (IGMP_V2_SEEN(in_dev)) {
955 /* this is a v3 query with v2 queriers present;
956 * Interpretation of the max_delay code is problematic here.
957 * A real v2 host would use ih_code directly, while v3 has a
958 * different encoding. We use the v3 encoding as more likely
959 * to be intended in a v3 query.
960 */
961 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800962 if (!max_delay)
963 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 } else { /* v3 */
965 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000966 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900967
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300968 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900970 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700971 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000972 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300973 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975
976 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
977 if (!max_delay)
978 max_delay = 1; /* can't mod w/ 0 */
979 in_dev->mr_maxdelay = max_delay;
Hangbin Liu966c37f2018-10-26 11:30:35 +0800980
981 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
982 * received value was zero, use the default or statically
983 * configured value.
984 */
985 in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
986 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
987
988 /* RFC3376, 8.3. Query Response Interval:
989 * The number of seconds represented by the [Query Response
990 * Interval] must be less than the [Query Interval].
991 */
992 if (in_dev->mr_qri >= in_dev->mr_qi)
993 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 if (!group) { /* general query */
996 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +0200997 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +0000999 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 /* mark sources to include, if group & source-specific */
1002 mark = ih3->nsrcs != 0;
1003 }
1004
1005 /*
1006 * - Start the timers in all of our membership records
1007 * that the query applies to for the interface on
1008 * which the query arrived excl. those that belong
1009 * to a "local" group (224.0.0.X)
1010 * - For timers already running check if they need to
1011 * be reset.
1012 * - Use the igmp->igmp_code field as the maximum
1013 * delay possible
1014 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001015 rcu_read_lock();
1016 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -08001017 int changed;
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (group && group != im->multiaddr)
1020 continue;
1021 if (im->multiaddr == IGMP_ALL_HOSTS)
1022 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001023 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001024 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001025 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 spin_lock_bh(&im->lock);
1027 if (im->tm_running)
1028 im->gsquery = im->gsquery && mark;
1029 else
1030 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -08001031 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001032 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -08001034 if (changed)
1035 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001037 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +00001038 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001041/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042int igmp_rcv(struct sk_buff *skb)
1043{
1044 /* This basically follows the spec line by line -- see RFC1112 */
1045 struct igmphdr *ih;
David Ahernc7b725b2017-08-15 18:38:42 -07001046 struct net_device *dev = skb->dev;
1047 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001049 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
David Ahernc7b725b2017-08-15 18:38:42 -07001051 if (netif_is_l3_master(dev)) {
1052 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1053 if (!dev)
1054 goto drop;
1055 }
1056
1057 in_dev = __in_dev_get_rcu(dev);
Ian Morris51456b22015-04-03 09:17:26 +01001058 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001059 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Herbert Xufb286bb2005-11-10 13:01:24 -08001061 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001062 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001063
Tom Herbertde08dc12014-05-07 16:52:10 -07001064 if (skb_checksum_simple_validate(skb))
1065 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001067 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 switch (ih->type) {
1069 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001070 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 break;
1072 case IGMP_HOST_MEMBERSHIP_REPORT:
1073 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001075 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 break;
David Stevens24c69272005-12-02 20:32:59 -08001077 /* don't rely on MC router hearing unicast reports */
1078 if (skb->pkt_type == PACKET_MULTICAST ||
1079 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001080 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 break;
1082 case IGMP_PIM:
1083#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return pim_rcv_v1(skb);
1085#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001086 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 case IGMP_DVMRP:
1088 case IGMP_TRACE:
1089 case IGMP_HOST_LEAVE_MESSAGE:
1090 case IGMP_MTRACE:
1091 case IGMP_MTRACE_RESP:
1092 break;
1093 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001096
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001097drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001098 if (dropped)
1099 kfree_skb(skb);
1100 else
1101 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return 0;
1103}
1104
1105#endif
1106
1107
1108/*
1109 * Add a filter to a device
1110 */
1111
Al Viro63007722006-09-27 18:31:32 -07001112static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114 char buf[MAX_ADDR_LEN];
1115 struct net_device *dev = in_dev->dev;
1116
1117 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1118 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001119 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 routine. Something sort of:
1121 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1122 --ANK
1123 */
1124 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001125 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128/*
1129 * Remove a filter from a device
1130 */
1131
Al Viro63007722006-09-27 18:31:32 -07001132static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133{
1134 char buf[MAX_ADDR_LEN];
1135 struct net_device *dev = in_dev->dev;
1136
1137 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001138 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141#ifdef CONFIG_IP_MULTICAST
1142/*
1143 * deleted ip_mc_list manipulation
1144 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001145static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
1146 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001149 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /* this is an "ip_mc_list" for convenience; only the fields below
1152 * are actually used. In particular, the refcnt and users are not
1153 * used for management of the delete list. Using the same structure
1154 * for deleted items allows change reports to use common code with
1155 * non-deleted or query-response MCA's.
1156 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001157 pmc = kzalloc(sizeof(*pmc), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (!pmc)
1159 return;
WANG Congb4846fc2017-06-20 10:46:27 -07001160 spin_lock_init(&pmc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_lock_bh(&im->lock);
1162 pmc->interface = im->interface;
1163 in_dev_hold(in_dev);
1164 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001165 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 pmc->sfmode = im->sfmode;
1167 if (pmc->sfmode == MCAST_INCLUDE) {
1168 struct ip_sf_list *psf;
1169
1170 pmc->tomb = im->tomb;
1171 pmc->sources = im->sources;
1172 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001173 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 psf->sf_crcount = pmc->crcount;
1175 }
1176 spin_unlock_bh(&im->lock);
1177
1178 spin_lock_bh(&in_dev->mc_tomb_lock);
1179 pmc->next = in_dev->mc_tomb;
1180 in_dev->mc_tomb = pmc;
1181 spin_unlock_bh(&in_dev->mc_tomb_lock);
1182}
1183
Hangbin Liu24803f32016-11-14 16:16:28 +08001184/*
1185 * restore ip_mc_list deleted records
1186 */
1187static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
1189 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001190 struct ip_sf_list *psf;
1191 struct net *net = dev_net(in_dev->dev);
1192 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 spin_lock_bh(&in_dev->mc_tomb_lock);
1195 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001196 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (pmc->multiaddr == multiaddr)
1198 break;
1199 pmc_prev = pmc;
1200 }
1201 if (pmc) {
1202 if (pmc_prev)
1203 pmc_prev->next = pmc->next;
1204 else
1205 in_dev->mc_tomb = pmc->next;
1206 }
1207 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001208
1209 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001211 im->interface = pmc->interface;
Hangbin Liu08d3ffc2018-07-20 14:04:27 +08001212 if (im->sfmode == MCAST_INCLUDE) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001213 im->tomb = pmc->tomb;
1214 im->sources = pmc->sources;
1215 for (psf = im->sources; psf; psf = psf->sf_next)
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001216 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1217 } else {
1218 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220 in_dev_put(pmc->interface);
Hangbin Liu9c8bb162017-02-08 21:16:45 +08001221 kfree(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001223 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Hangbin Liu24803f32016-11-14 16:16:28 +08001226/*
1227 * flush ip_mc_list deleted records
1228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static void igmpv3_clear_delrec(struct in_device *in_dev)
1230{
1231 struct ip_mc_list *pmc, *nextpmc;
1232
1233 spin_lock_bh(&in_dev->mc_tomb_lock);
1234 pmc = in_dev->mc_tomb;
1235 in_dev->mc_tomb = NULL;
1236 spin_unlock_bh(&in_dev->mc_tomb_lock);
1237
1238 for (; pmc; pmc = nextpmc) {
1239 nextpmc = pmc->next;
1240 ip_mc_clear_src(pmc);
1241 in_dev_put(pmc->interface);
1242 kfree(pmc);
1243 }
1244 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001245 rcu_read_lock();
1246 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct ip_sf_list *psf, *psf_next;
1248
1249 spin_lock_bh(&pmc->lock);
1250 psf = pmc->tomb;
1251 pmc->tomb = NULL;
1252 spin_unlock_bh(&pmc->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08001253 for (; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 psf_next = psf->sf_next;
1255 kfree(psf);
1256 }
1257 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001258 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259}
1260#endif
1261
Florian Fainelli9fb20802019-02-01 20:20:52 -08001262static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct in_device *in_dev = im->interface;
1265#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001266 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 int reporter;
1268#endif
1269
1270 if (im->loaded) {
1271 im->loaded = 0;
1272 ip_mc_filter_del(in_dev, im->multiaddr);
1273 }
1274
1275#ifdef CONFIG_IP_MULTICAST
1276 if (im->multiaddr == IGMP_ALL_HOSTS)
1277 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001278 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001279 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 reporter = im->reporter;
1282 igmp_stop_timer(im);
1283
1284 if (!in_dev->dead) {
1285 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001286 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (IGMP_V2_SEEN(in_dev)) {
1288 if (reporter)
1289 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001290 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
1292 /* IGMPv3 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001293 igmpv3_add_delrec(in_dev, im, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 igmp_ifc_event(in_dev);
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Florian Fainelli9fb20802019-02-01 20:20:52 -08001300static void igmp_group_dropped(struct ip_mc_list *im)
1301{
1302 __igmp_group_dropped(im, GFP_KERNEL);
1303}
1304
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001305static void igmp_group_added(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
1307 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001308#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001309 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001310#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 if (im->loaded == 0) {
1313 im->loaded = 1;
1314 ip_mc_filter_add(in_dev, im->multiaddr);
1315 }
1316
1317#ifdef CONFIG_IP_MULTICAST
1318 if (im->multiaddr == IGMP_ALL_HOSTS)
1319 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001320 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001321 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (in_dev->dead)
1324 return;
Hangbin Liuff065252018-08-29 18:06:10 +08001325
1326 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1328 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001329 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 spin_unlock_bh(&im->lock);
1331 return;
1332 }
1333 /* else, v3 */
1334
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001335 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1336 * not send filter-mode change record as the mode should be from
1337 * IN() to IN(A).
1338 */
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001339 if (im->sfmode == MCAST_EXCLUDE)
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001340 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 igmp_ifc_event(in_dev);
1343#endif
1344}
1345
1346
1347/*
1348 * Multicast list managers
1349 */
1350
Eric Dumazete9897072013-06-07 08:48:57 -07001351static u32 ip_mc_hash(const struct ip_mc_list *im)
1352{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001353 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001354}
1355
1356static void ip_mc_hash_add(struct in_device *in_dev,
1357 struct ip_mc_list *im)
1358{
1359 struct ip_mc_list __rcu **mc_hash;
1360 u32 hash;
1361
1362 mc_hash = rtnl_dereference(in_dev->mc_hash);
1363 if (mc_hash) {
1364 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001365 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001366 rcu_assign_pointer(mc_hash[hash], im);
1367 return;
1368 }
1369
1370 /* do not use a hash table for small number of items */
1371 if (in_dev->mc_count < 4)
1372 return;
1373
1374 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1375 GFP_KERNEL);
1376 if (!mc_hash)
1377 return;
1378
1379 for_each_pmc_rtnl(in_dev, im) {
1380 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001381 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001382 RCU_INIT_POINTER(mc_hash[hash], im);
1383 }
1384
1385 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1386}
1387
1388static void ip_mc_hash_remove(struct in_device *in_dev,
1389 struct ip_mc_list *im)
1390{
1391 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1392 struct ip_mc_list *aux;
1393
1394 if (!mc_hash)
1395 return;
1396 mc_hash += ip_mc_hash(im);
1397 while ((aux = rtnl_dereference(*mc_hash)) != im)
1398 mc_hash = &aux->next_hash;
1399 *mc_hash = im->next_hash;
1400}
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403/*
1404 * A socket has joined a multicast group on device dev.
1405 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001406static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1407 unsigned int mode, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
1409 struct ip_mc_list *im;
1410
1411 ASSERT_RTNL();
1412
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001413 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (im->multiaddr == addr) {
1415 im->users++;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001416 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 goto out;
1418 }
1419 }
1420
Florian Fainelli9fb20802019-02-01 20:20:52 -08001421 im = kzalloc(sizeof(*im), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 if (!im)
1423 goto out;
1424
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001425 im->users = 1;
1426 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001428 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /* initial mode is (EX, empty) */
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001430 im->sfmode = mode;
1431 im->sfcount[mode] = 1;
Reshetova, Elena8851ab52017-06-30 13:08:02 +03001432 refcount_set(&im->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 spin_lock_init(&im->lock);
1434#ifdef CONFIG_IP_MULTICAST
Kees Cooke99e88a2017-10-16 14:43:17 -07001435 timer_setup(&im->timer, igmp_timer_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001437
1438 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001439 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001440 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001441
Eric Dumazete9897072013-06-07 08:48:57 -07001442 ip_mc_hash_add(in_dev, im);
1443
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001445 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001447 igmp_group_added(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (!in_dev->dead)
1449 ip_rt_multicast_event(in_dev);
1450out:
1451 return;
1452}
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001453
Florian Fainelli9fb20802019-02-01 20:20:52 -08001454void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1455{
1456 ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
1457}
1458EXPORT_SYMBOL(__ip_mc_inc_group);
1459
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001460void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1461{
1462 __ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE);
1463}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001464EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001466static int ip_mc_check_iphdr(struct sk_buff *skb)
1467{
1468 const struct iphdr *iph;
1469 unsigned int len;
1470 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1471
1472 if (!pskb_may_pull(skb, offset))
1473 return -EINVAL;
1474
1475 iph = ip_hdr(skb);
1476
1477 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1478 return -EINVAL;
1479
1480 offset += ip_hdrlen(skb) - sizeof(*iph);
1481
1482 if (!pskb_may_pull(skb, offset))
1483 return -EINVAL;
1484
1485 iph = ip_hdr(skb);
1486
1487 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1488 return -EINVAL;
1489
1490 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1491 if (skb->len < len || len < offset)
1492 return -EINVAL;
1493
1494 skb_set_transport_header(skb, offset);
1495
1496 return 0;
1497}
1498
1499static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1500{
1501 unsigned int len = skb_transport_offset(skb);
1502
1503 len += sizeof(struct igmpv3_report);
1504
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001505 return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001506}
1507
1508static int ip_mc_check_igmp_query(struct sk_buff *skb)
1509{
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001510 unsigned int transport_len = ip_transport_len(skb);
1511 unsigned int len;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001512
1513 /* IGMPv{1,2}? */
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001514 if (transport_len != sizeof(struct igmphdr)) {
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001515 /* or IGMPv3? */
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001516 if (transport_len < sizeof(struct igmpv3_query))
1517 return -EINVAL;
1518
1519 len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1520 if (!ip_mc_may_pull(skb, len))
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001521 return -EINVAL;
1522 }
1523
1524 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1525 * all-systems destination addresses (224.0.0.1) for general queries
1526 */
1527 if (!igmp_hdr(skb)->group &&
1528 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1529 return -EINVAL;
1530
1531 return 0;
1532}
1533
1534static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1535{
1536 switch (igmp_hdr(skb)->type) {
1537 case IGMP_HOST_LEAVE_MESSAGE:
1538 case IGMP_HOST_MEMBERSHIP_REPORT:
1539 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001540 return 0;
1541 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1542 return ip_mc_check_igmp_reportv3(skb);
1543 case IGMP_HOST_MEMBERSHIP_QUERY:
1544 return ip_mc_check_igmp_query(skb);
1545 default:
1546 return -ENOMSG;
1547 }
1548}
1549
1550static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1551{
1552 return skb_checksum_simple_validate(skb);
1553}
1554
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001555static int ip_mc_check_igmp_csum(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001556{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001557 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001558 unsigned int transport_len = ip_transport_len(skb);
1559 struct sk_buff *skb_chk;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001560
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001561 if (!ip_mc_may_pull(skb, len))
1562 return -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001563
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001564 skb_chk = skb_checksum_trimmed(skb, transport_len,
1565 ip_mc_validate_checksum);
1566 if (!skb_chk)
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001567 return -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001568
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001569 if (skb_chk != skb)
Linus Lüssinga5169932015-08-13 05:54:07 +02001570 kfree_skb(skb_chk);
1571
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001572 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001573}
1574
1575/**
1576 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1577 * @skb: the skb to validate
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001578 *
1579 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001580 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001581 *
1582 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1583 * standard
1584 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1585 * -ENOMEM: A memory allocation failure happened.
1586 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001587 * Caller needs to set the skb network header and free any returned skb if it
1588 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001589 */
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001590int ip_mc_check_igmp(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001591{
1592 int ret = ip_mc_check_iphdr(skb);
1593
1594 if (ret < 0)
1595 return ret;
1596
1597 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1598 return -ENOMSG;
1599
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001600 ret = ip_mc_check_igmp_csum(skb);
1601 if (ret < 0)
1602 return ret;
1603
1604 return ip_mc_check_igmp_msg(skb);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001605}
1606EXPORT_SYMBOL(ip_mc_check_igmp);
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001609 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001610 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001611static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001612{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001613#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001614 struct ip_mc_list *im;
1615 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001616 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001617
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001618 ASSERT_RTNL();
1619
1620 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001621 if (im->multiaddr == IGMP_ALL_HOSTS)
1622 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001623 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001624 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001625 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001626
Eric Dumazet866f3b22010-11-18 09:33:19 -08001627 /* a failover is happening and switches
1628 * must be notified immediately
1629 */
1630 if (IGMP_V1_SEEN(in_dev))
1631 type = IGMP_HOST_MEMBERSHIP_REPORT;
1632 else if (IGMP_V2_SEEN(in_dev))
1633 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1634 else
1635 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1636 igmp_send_report(in_dev, im, type);
1637 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001638#endif
1639}
1640
1641/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * A socket has left a multicast group on device dev
1643 */
1644
Florian Fainelli9fb20802019-02-01 20:20:52 -08001645void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001647 struct ip_mc_list *i;
1648 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001651
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001652 for (ip = &in_dev->mc_list;
1653 (i = rtnl_dereference(*ip)) != NULL;
1654 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001655 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001657 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001658 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001659 in_dev->mc_count--;
Florian Fainelli9fb20802019-02-01 20:20:52 -08001660 __igmp_group_dropped(i, gfp);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001661 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 if (!in_dev->dead)
1664 ip_rt_multicast_event(in_dev);
1665
1666 ip_ma_put(i);
1667 return;
1668 }
1669 break;
1670 }
1671 }
1672}
Florian Fainelli9fb20802019-02-01 20:20:52 -08001673EXPORT_SYMBOL(__ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Moni Shoua75c78502009-09-15 02:37:40 -07001675/* Device changing type */
1676
1677void ip_mc_unmap(struct in_device *in_dev)
1678{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001679 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001680
1681 ASSERT_RTNL();
1682
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001683 for_each_pmc_rtnl(in_dev, pmc)
1684 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001685}
1686
1687void ip_mc_remap(struct in_device *in_dev)
1688{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001689 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001690
1691 ASSERT_RTNL();
1692
Hangbin Liu24803f32016-11-14 16:16:28 +08001693 for_each_pmc_rtnl(in_dev, pmc) {
1694#ifdef CONFIG_IP_MULTICAST
1695 igmpv3_del_delrec(in_dev, pmc);
1696#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001697 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001698 }
Moni Shoua75c78502009-09-15 02:37:40 -07001699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701/* Device going down */
1702
1703void ip_mc_down(struct in_device *in_dev)
1704{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001705 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 ASSERT_RTNL();
1708
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001709 for_each_pmc_rtnl(in_dev, pmc)
1710 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712#ifdef CONFIG_IP_MULTICAST
1713 in_dev->mr_ifc_count = 0;
1714 if (del_timer(&in_dev->mr_ifc_timer))
1715 __in_dev_put(in_dev);
1716 in_dev->mr_gq_running = 0;
1717 if (del_timer(&in_dev->mr_gq_timer))
1718 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719#endif
1720
1721 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1722}
1723
Hangbin Liu966c37f2018-10-26 11:30:35 +08001724#ifdef CONFIG_IP_MULTICAST
1725static void ip_mc_reset(struct in_device *in_dev)
1726{
1727 struct net *net = dev_net(in_dev->dev);
1728
1729 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1730 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1731 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1732}
1733#else
1734static void ip_mc_reset(struct in_device *in_dev)
1735{
1736}
1737#endif
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739void ip_mc_init_dev(struct in_device *in_dev)
1740{
1741 ASSERT_RTNL();
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743#ifdef CONFIG_IP_MULTICAST
Kees Cooke99e88a2017-10-16 14:43:17 -07001744 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1745 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746#endif
Hangbin Liu966c37f2018-10-26 11:30:35 +08001747 ip_mc_reset(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 spin_lock_init(&in_dev->mc_tomb_lock);
1750}
1751
1752/* Device going up */
1753
1754void ip_mc_up(struct in_device *in_dev)
1755{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001756 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 ASSERT_RTNL();
1759
Hangbin Liu966c37f2018-10-26 11:30:35 +08001760 ip_mc_reset(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1762
Hangbin Liu24803f32016-11-14 16:16:28 +08001763 for_each_pmc_rtnl(in_dev, pmc) {
1764#ifdef CONFIG_IP_MULTICAST
1765 igmpv3_del_delrec(in_dev, pmc);
1766#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001767 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
1771/*
1772 * Device is about to be destroyed: clean up.
1773 */
1774
1775void ip_mc_destroy_dev(struct in_device *in_dev)
1776{
1777 struct ip_mc_list *i;
1778
1779 ASSERT_RTNL();
1780
1781 /* Deactivate timers */
1782 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001783#ifdef CONFIG_IP_MULTICAST
1784 igmpv3_clear_delrec(in_dev);
1785#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001787 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1788 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001789 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001794/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001795static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 struct net_device *dev = NULL;
1798 struct in_device *idev = NULL;
1799
1800 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001801 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return idev;
1803 }
1804 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001805 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (!dev)
1807 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
1809
David S. Millerb23dd4f2011-03-02 14:31:35 -08001810 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001811 struct rtable *rt = ip_route_output(net,
1812 imr->imr_multiaddr.s_addr,
1813 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001814 if (!IS_ERR(rt)) {
1815 dev = rt->dst.dev;
1816 ip_rt_put(rt);
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 if (dev) {
1820 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001821 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
1823 return idev;
1824}
1825
1826/*
1827 * Join a socket to a group
1828 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001831 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 struct ip_sf_list *psf, *psf_prev;
1834 int rv = 0;
1835
1836 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001837 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (psf->sf_inaddr == *psfsrc)
1839 break;
1840 psf_prev = psf;
1841 }
1842 if (!psf || psf->sf_count[sfmode] == 0) {
1843 /* source filter not found, or count wrong => bug */
1844 return -ESRCH;
1845 }
1846 psf->sf_count[sfmode]--;
1847 if (psf->sf_count[sfmode] == 0) {
1848 ip_rt_multicast_event(pmc->interface);
1849 }
1850 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1851#ifdef CONFIG_IP_MULTICAST
1852 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001853 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854#endif
1855
1856 /* no more filters for this source */
1857 if (psf_prev)
1858 psf_prev->sf_next = psf->sf_next;
1859 else
1860 pmc->sources = psf->sf_next;
1861#ifdef CONFIG_IP_MULTICAST
1862 if (psf->sf_oldin &&
1863 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001864 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 psf->sf_next = pmc->tomb;
1866 pmc->tomb = psf;
1867 rv = 1;
1868 } else
1869#endif
1870 kfree(psf);
1871 }
1872 return rv;
1873}
1874
1875#ifndef CONFIG_IP_MULTICAST
1876#define igmp_ifc_event(x) do { } while (0)
1877#endif
1878
Al Viro8f935bb2006-09-27 18:30:07 -07001879static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1880 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
1882 struct ip_mc_list *pmc;
1883 int changerec = 0;
1884 int i, err;
1885
1886 if (!in_dev)
1887 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001888 rcu_read_lock();
1889 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (*pmca == pmc->multiaddr)
1891 break;
1892 }
1893 if (!pmc) {
1894 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001895 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return -ESRCH;
1897 }
1898 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001899 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900#ifdef CONFIG_IP_MULTICAST
1901 sf_markstate(pmc);
1902#endif
1903 if (!delta) {
1904 err = -EINVAL;
1905 if (!pmc->sfcount[sfmode])
1906 goto out_unlock;
1907 pmc->sfcount[sfmode]--;
1908 }
1909 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001910 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1912
1913 changerec |= rv > 0;
1914 if (!err && rv < 0)
1915 err = rv;
1916 }
1917 if (pmc->sfmode == MCAST_EXCLUDE &&
1918 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1919 pmc->sfcount[MCAST_INCLUDE]) {
1920#ifdef CONFIG_IP_MULTICAST
1921 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001922 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923#endif
1924
1925 /* filter mode change */
1926 pmc->sfmode = MCAST_INCLUDE;
1927#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001928 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001930 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 psf->sf_crcount = 0;
1932 igmp_ifc_event(pmc->interface);
1933 } else if (sf_setstate(pmc) || changerec) {
1934 igmp_ifc_event(pmc->interface);
1935#endif
1936 }
1937out_unlock:
1938 spin_unlock_bh(&pmc->lock);
1939 return err;
1940}
1941
1942/*
1943 * Add multicast single-source filter to the interface list
1944 */
1945static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001946 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
1948 struct ip_sf_list *psf, *psf_prev;
1949
1950 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001951 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 if (psf->sf_inaddr == *psfsrc)
1953 break;
1954 psf_prev = psf;
1955 }
1956 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001957 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (!psf)
1959 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 psf->sf_inaddr = *psfsrc;
1961 if (psf_prev) {
1962 psf_prev->sf_next = psf;
1963 } else
1964 pmc->sources = psf;
1965 }
1966 psf->sf_count[sfmode]++;
1967 if (psf->sf_count[sfmode] == 1) {
1968 ip_rt_multicast_event(pmc->interface);
1969 }
1970 return 0;
1971}
1972
1973#ifdef CONFIG_IP_MULTICAST
1974static void sf_markstate(struct ip_mc_list *pmc)
1975{
1976 struct ip_sf_list *psf;
1977 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1978
Weilong Chenc71151f2013-12-23 14:37:29 +08001979 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 if (pmc->sfcount[MCAST_EXCLUDE]) {
1981 psf->sf_oldin = mca_xcount ==
1982 psf->sf_count[MCAST_EXCLUDE] &&
1983 !psf->sf_count[MCAST_INCLUDE];
1984 } else
1985 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1986}
1987
1988static int sf_setstate(struct ip_mc_list *pmc)
1989{
David L Stevensad125832006-01-18 14:20:56 -08001990 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1992 int qrv = pmc->interface->mr_qrv;
1993 int new_in, rv;
1994
1995 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001996 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if (pmc->sfcount[MCAST_EXCLUDE]) {
1998 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1999 !psf->sf_count[MCAST_INCLUDE];
2000 } else
2001 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08002002 if (new_in) {
2003 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05002004 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08002005
Weilong Chenc71151f2013-12-23 14:37:29 +08002006 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08002007 if (dpsf->sf_inaddr == psf->sf_inaddr)
2008 break;
2009 prev = dpsf;
2010 }
2011 if (dpsf) {
2012 if (prev)
2013 prev->sf_next = dpsf->sf_next;
2014 else
2015 pmc->tomb = dpsf->sf_next;
2016 kfree(dpsf);
2017 }
2018 psf->sf_crcount = qrv;
2019 rv++;
2020 }
2021 } else if (psf->sf_oldin) {
2022
2023 psf->sf_crcount = 0;
2024 /*
2025 * add or update "delete" records if an active filter
2026 * is now inactive
2027 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002028 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08002029 if (dpsf->sf_inaddr == psf->sf_inaddr)
2030 break;
2031 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00002032 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08002033 if (!dpsf)
2034 continue;
2035 *dpsf = *psf;
2036 /* pmc->lock held by callers */
2037 dpsf->sf_next = pmc->tomb;
2038 pmc->tomb = dpsf;
2039 }
2040 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 rv++;
2042 }
2043 }
2044 return rv;
2045}
2046#endif
2047
2048/*
2049 * Add multicast source filter list to the interface list
2050 */
Al Viro8f935bb2006-09-27 18:30:07 -07002051static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2052 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
2054 struct ip_mc_list *pmc;
2055 int isexclude;
2056 int i, err;
2057
2058 if (!in_dev)
2059 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002060 rcu_read_lock();
2061 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (*pmca == pmc->multiaddr)
2063 break;
2064 }
2065 if (!pmc) {
2066 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002067 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 return -ESRCH;
2069 }
2070 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002071 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073#ifdef CONFIG_IP_MULTICAST
2074 sf_markstate(pmc);
2075#endif
2076 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2077 if (!delta)
2078 pmc->sfcount[sfmode]++;
2079 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002080 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002081 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (err)
2083 break;
2084 }
2085 if (err) {
2086 int j;
2087
Jun Zhao685f94e62011-11-22 17:19:03 +00002088 if (!delta)
2089 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002090 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002091 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2093#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002095 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002096 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097#endif
2098
2099 /* filter mode change */
2100 if (pmc->sfcount[MCAST_EXCLUDE])
2101 pmc->sfmode = MCAST_EXCLUDE;
2102 else if (pmc->sfcount[MCAST_INCLUDE])
2103 pmc->sfmode = MCAST_INCLUDE;
2104#ifdef CONFIG_IP_MULTICAST
2105 /* else no filters; keep old mode for reports */
2106
Nikolay Borisov165094a2016-02-08 23:29:24 +02002107 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002109 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 psf->sf_crcount = 0;
2111 igmp_ifc_event(in_dev);
2112 } else if (sf_setstate(pmc)) {
2113 igmp_ifc_event(in_dev);
2114#endif
2115 }
2116 spin_unlock_bh(&pmc->lock);
2117 return err;
2118}
2119
2120static void ip_mc_clear_src(struct ip_mc_list *pmc)
2121{
WANG Congc38b7d32017-06-12 09:52:26 -07002122 struct ip_sf_list *psf, *nextpsf, *tomb, *sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
WANG Congc38b7d32017-06-12 09:52:26 -07002124 spin_lock_bh(&pmc->lock);
2125 tomb = pmc->tomb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 pmc->tomb = NULL;
WANG Congc38b7d32017-06-12 09:52:26 -07002127 sources = pmc->sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 pmc->sources = NULL;
2129 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002130 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 pmc->sfcount[MCAST_EXCLUDE] = 1;
WANG Congc38b7d32017-06-12 09:52:26 -07002132 spin_unlock_bh(&pmc->lock);
2133
2134 for (psf = tomb; psf; psf = nextpsf) {
2135 nextpsf = psf->sf_next;
2136 kfree(psf);
2137 }
2138 for (psf = sources; psf; psf = nextpsf) {
2139 nextpsf = psf->sf_next;
2140 kfree(psf);
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002144/* Join a multicast group
2145 */
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002146static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2147 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
Al Viro8f935bb2006-09-27 18:30:07 -07002149 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002150 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 struct in_device *in_dev;
2152 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002153 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002154 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002156 int err;
2157
2158 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Joe Perchesf97c1e02007-12-16 13:45:43 -08002160 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return -EINVAL;
2162
Daniel Lezcano877aced2008-08-13 16:15:57 -07002163 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 err = -ENODEV;
2167 goto done;
2168 }
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002171 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002172 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002173 if (i->multi.imr_multiaddr.s_addr == addr &&
2174 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 count++;
2177 }
2178 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002179 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002181 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002182 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002183 goto done;
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002186 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 iml->sflist = NULL;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002188 iml->sfmode = mode;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002189 rcu_assign_pointer(inet->mc_list, iml);
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002190 __ip_mc_inc_group(in_dev, addr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return err;
2194}
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002195
2196/* Join ASM (Any-Source Multicast) group
2197 */
2198int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2199{
2200 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2201}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002202EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002204/* Join SSM (Source-Specific Multicast) group
2205 */
2206int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2207 unsigned int mode)
2208{
2209 return __ip_mc_join_group(sk, imr, mode);
2210}
2211
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2213 struct in_device *in_dev)
2214{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002215 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 int err;
2217
Ian Morris51456b22015-04-03 09:17:26 +01002218 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 /* any-source empty exclude case */
2220 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2221 iml->sfmode, 0, NULL, 0);
2222 }
2223 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002224 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002225 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002226 /* decrease mem now to avoid the memleak warning */
2227 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002228 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return err;
2230}
2231
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002232int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002235 struct ip_mc_socklist *iml;
2236 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002237 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002238 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002239 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002240 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002241 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Eric Dumazet959d10f2015-02-17 03:19:24 -08002243 ASSERT_RTNL();
2244
Daniel Lezcano877aced2008-08-13 16:15:57 -07002245 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002246 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002247 ret = -ENODEV;
2248 goto out;
2249 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002250 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002251 for (imlp = &inet->mc_list;
2252 (iml = rtnl_dereference(*imlp)) != NULL;
2253 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002254 if (iml->multi.imr_multiaddr.s_addr != group)
2255 continue;
2256 if (ifindex) {
2257 if (iml->multi.imr_ifindex != ifindex)
2258 continue;
2259 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2260 iml->multi.imr_address.s_addr)
2261 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
David L Stevensacd6e002006-08-17 16:27:39 -07002263 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002265 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002266
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002267 if (in_dev)
2268 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002269
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002270 /* decrease mem now to avoid the memleak warning */
2271 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002272 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
dingtianhong52ad3532014-07-02 13:50:48 +08002275out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002276 return ret;
2277}
stephen hemminger193ba922012-10-01 12:32:34 +00002278EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280int ip_mc_source(int add, int omode, struct sock *sk, struct
2281 ip_mreq_source *mreqs, int ifindex)
2282{
2283 int err;
2284 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002285 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 struct ip_mc_socklist *pmc;
2287 struct in_device *in_dev = NULL;
2288 struct inet_sock *inet = inet_sk(sk);
2289 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002290 struct net *net = sock_net(sk);
David L Stevens8cdaaa152005-07-08 17:39:23 -07002291 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 int i, j, rv;
2293
Joe Perchesf97c1e02007-12-16 13:45:43 -08002294 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return -EINVAL;
2296
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002297 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
2299 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2300 imr.imr_address.s_addr = mreqs->imr_interface;
2301 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002302 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 if (!in_dev) {
2305 err = -ENODEV;
2306 goto done;
2307 }
2308 err = -EADDRNOTAVAIL;
2309
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002310 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002311 if ((pmc->multi.imr_multiaddr.s_addr ==
2312 imr.imr_multiaddr.s_addr) &&
2313 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 break;
2315 }
David L Stevens917f2f12005-07-08 17:45:16 -07002316 if (!pmc) { /* must have a prior join */
2317 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 /* if a source filter was set, must be the same mode as before */
2321 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002322 if (pmc->sfmode != omode) {
2323 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 } else if (pmc->sfmode != omode) {
2327 /* allow mode switches for empty-set filters */
2328 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002329 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 NULL, 0);
2331 pmc->sfmode = omode;
2332 }
2333
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002334 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (!add) {
2336 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002337 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002339 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002341 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 if (rv == 0)
2343 break;
2344 }
2345 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002346 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
David L Stevens8cdaaa152005-07-08 17:39:23 -07002348 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2349 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2350 leavegroup = 1;
2351 goto done;
2352 }
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002355 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 &mreqs->imr_sourceaddr, 1);
2357
Weilong Chenc71151f2013-12-23 14:37:29 +08002358 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 psl->sl_addr[j-1] = psl->sl_addr[j];
2360 psl->sl_count--;
2361 err = 0;
2362 goto done;
2363 }
2364 /* else, add a new source to the filter */
2365
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002366 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 err = -ENOBUFS;
2368 goto done;
2369 }
2370 if (!psl || psl->sl_count == psl->sl_max) {
2371 struct ip_sf_socklist *newpsl;
2372 int count = IP_SFBLOCK;
2373
2374 if (psl)
2375 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002376 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if (!newpsl) {
2378 err = -ENOBUFS;
2379 goto done;
2380 }
2381 newpsl->sl_max = count;
2382 newpsl->sl_count = count - IP_SFBLOCK;
2383 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002384 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002386 /* decrease mem now to avoid the memleak warning */
2387 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002388 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002390 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002391 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002394 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002396 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (rv == 0)
2398 break;
2399 }
2400 if (rv == 0) /* address already there is an error */
2401 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002402 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 psl->sl_addr[j+1] = psl->sl_addr[j];
2404 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2405 psl->sl_count++;
2406 err = 0;
2407 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002408 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 &mreqs->imr_sourceaddr, 1);
2410done:
David L Stevens8cdaaa152005-07-08 17:39:23 -07002411 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002412 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return err;
2414}
2415
2416int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2417{
David L Stevens9951f032005-07-08 17:47:28 -07002418 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002420 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 struct ip_mc_socklist *pmc;
2422 struct in_device *in_dev;
2423 struct inet_sock *inet = inet_sk(sk);
2424 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002425 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002426 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
Joe Perchesf97c1e02007-12-16 13:45:43 -08002428 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return -EINVAL;
2430 if (msf->imsf_fmode != MCAST_INCLUDE &&
2431 msf->imsf_fmode != MCAST_EXCLUDE)
2432 return -EINVAL;
2433
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002434 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2437 imr.imr_address.s_addr = msf->imsf_interface;
2438 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002439 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
2441 if (!in_dev) {
2442 err = -ENODEV;
2443 goto done;
2444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
David L Stevens9951f032005-07-08 17:47:28 -07002446 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2447 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2448 leavegroup = 1;
2449 goto done;
2450 }
2451
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002452 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2454 pmc->multi.imr_ifindex == imr.imr_ifindex)
2455 break;
2456 }
David L Stevens917f2f12005-07-08 17:45:16 -07002457 if (!pmc) { /* must have a prior join */
2458 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002462 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2463 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (!newpsl) {
2465 err = -ENOBUFS;
2466 goto done;
2467 }
2468 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2469 memcpy(newpsl->sl_addr, msf->imsf_slist,
2470 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2471 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2472 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2473 if (err) {
2474 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2475 goto done;
2476 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002477 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002479 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2480 msf->imsf_fmode, 0, NULL, 0);
2481 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002482 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (psl) {
2484 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2485 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002486 /* decrease mem now to avoid the memleak warning */
2487 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002488 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 } else
2490 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2491 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002492 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002494 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495done:
David L Stevens9951f032005-07-08 17:47:28 -07002496 if (leavegroup)
2497 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return err;
2499}
2500
2501int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2502 struct ip_msfilter __user *optval, int __user *optlen)
2503{
2504 int err, len, count, copycount;
2505 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002506 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct ip_mc_socklist *pmc;
2508 struct in_device *in_dev;
2509 struct inet_sock *inet = inet_sk(sk);
2510 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002511 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
WANG Cong87e9f032015-11-03 15:41:16 -08002513 ASSERT_RTNL();
2514
Joe Perchesf97c1e02007-12-16 13:45:43 -08002515 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return -EINVAL;
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2519 imr.imr_address.s_addr = msf->imsf_interface;
2520 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002521 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
2523 if (!in_dev) {
2524 err = -ENODEV;
2525 goto done;
2526 }
2527 err = -EADDRNOTAVAIL;
2528
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002529 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2531 pmc->multi.imr_ifindex == imr.imr_ifindex)
2532 break;
2533 }
2534 if (!pmc) /* must have a prior join */
2535 goto done;
2536 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002537 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 if (!psl) {
2539 len = 0;
2540 count = 0;
2541 } else {
2542 count = psl->sl_count;
2543 }
2544 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2545 len = copycount * sizeof(psl->sl_addr[0]);
2546 msf->imsf_numsrc = count;
2547 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2548 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2549 return -EFAULT;
2550 }
2551 if (len &&
2552 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2553 return -EFAULT;
2554 return 0;
2555done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 return err;
2557}
2558
2559int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2560 struct group_filter __user *optval, int __user *optlen)
2561{
2562 int err, i, count, copycount;
2563 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002564 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 struct ip_mc_socklist *pmc;
2566 struct inet_sock *inet = inet_sk(sk);
2567 struct ip_sf_socklist *psl;
2568
WANG Cong87e9f032015-11-03 15:41:16 -08002569 ASSERT_RTNL();
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 psin = (struct sockaddr_in *)&gsf->gf_group;
2572 if (psin->sin_family != AF_INET)
2573 return -EINVAL;
2574 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002575 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 return -EINVAL;
2577
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 err = -EADDRNOTAVAIL;
2579
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002580 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2582 pmc->multi.imr_ifindex == gsf->gf_interface)
2583 break;
2584 }
2585 if (!pmc) /* must have a prior join */
2586 goto done;
2587 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002588 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 count = psl ? psl->sl_count : 0;
2590 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2591 gsf->gf_numsrc = count;
2592 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2593 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2594 return -EFAULT;
2595 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002596 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct sockaddr_storage ss;
2598
2599 psin = (struct sockaddr_in *)&ss;
2600 memset(&ss, 0, sizeof(ss));
2601 psin->sin_family = AF_INET;
2602 psin->sin_addr.s_addr = psl->sl_addr[i];
2603 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2604 return -EFAULT;
2605 }
2606 return 0;
2607done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 return err;
2609}
2610
2611/*
2612 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2613 */
David Ahern60d9b032017-08-07 08:44:19 -07002614int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2615 int dif, int sdif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
2617 struct inet_sock *inet = inet_sk(sk);
2618 struct ip_mc_socklist *pmc;
2619 struct ip_sf_socklist *psl;
2620 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002621 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002623 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002624 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002627 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002628 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
David Ahern60d9b032017-08-07 08:44:19 -07002630 (pmc->multi.imr_ifindex == dif ||
2631 (sdif && pmc->multi.imr_ifindex == sdif)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 break;
2633 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002634 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002636 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002637 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002638 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002640 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Weilong Chenc71151f2013-12-23 14:37:29 +08002642 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (psl->sl_addr[i] == rmt_addr)
2644 break;
2645 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002646 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002648 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002650 goto unlock;
2651 ret = 1;
2652unlock:
2653 rcu_read_unlock();
2654out:
2655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658/*
2659 * A socket is closing.
2660 */
2661
2662void ip_mc_drop_socket(struct sock *sk)
2663{
2664 struct inet_sock *inet = inet_sk(sk);
2665 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002666 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Ian Morris51456b22015-04-03 09:17:26 +01002668 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return;
2670
2671 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002672 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002675 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002676 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002677 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002678 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002680 /* decrease mem now to avoid the memleak warning */
2681 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002682 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
2684 rtnl_unlock();
2685}
2686
David S. Millerdbdd9a52011-03-10 16:34:38 -08002687/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002688int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
2690 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002691 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 struct ip_sf_list *psf;
2693 int rv = 0;
2694
Eric Dumazete9897072013-06-07 08:48:57 -07002695 mc_hash = rcu_dereference(in_dev->mc_hash);
2696 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002697 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002698
2699 for (im = rcu_dereference(mc_hash[hash]);
2700 im != NULL;
2701 im = rcu_dereference(im->next_hash)) {
2702 if (im->multiaddr == mc_addr)
2703 break;
2704 }
2705 } else {
2706 for_each_pmc_rcu(in_dev, im) {
2707 if (im->multiaddr == mc_addr)
2708 break;
2709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711 if (im && proto == IPPROTO_IGMP) {
2712 rv = 1;
2713 } else if (im) {
2714 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002715 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (psf->sf_inaddr == src_addr)
2717 break;
2718 }
2719 if (psf)
2720 rv = psf->sf_count[MCAST_INCLUDE] ||
2721 psf->sf_count[MCAST_EXCLUDE] !=
2722 im->sfcount[MCAST_EXCLUDE];
2723 else
2724 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2725 } else
2726 rv = 1; /* unspecified source; tentatively allow */
2727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return rv;
2729}
2730
2731#if defined(CONFIG_PROC_FS)
2732struct igmp_mc_iter_state {
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08002733 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 struct net_device *dev;
2735 struct in_device *in_dev;
2736};
2737
2738#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2739
2740static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2741{
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08002742 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct ip_mc_list *im = NULL;
2744 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2745
Pavel Emelianov7562f872007-05-03 15:13:45 -07002746 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002747 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002749
2750 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (!in_dev)
2752 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002753 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (im) {
2755 state->in_dev = in_dev;
2756 break;
2757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
2759 return im;
2760}
2761
2762static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2763{
2764 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002765
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002766 im = rcu_dereference(im->next_rcu);
2767 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002768 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (!state->dev) {
2770 state->in_dev = NULL;
2771 break;
2772 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002773 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (!state->in_dev)
2775 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002776 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
2778 return im;
2779}
2780
2781static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2782{
2783 struct ip_mc_list *im = igmp_mc_get_first(seq);
2784 if (im)
2785 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2786 --pos;
2787 return pos ? NULL : im;
2788}
2789
2790static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002791 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
stephen hemminger61fbab72009-11-10 07:54:55 +00002793 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2795}
2796
2797static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2798{
2799 struct ip_mc_list *im;
2800 if (v == SEQ_START_TOKEN)
2801 im = igmp_mc_get_first(seq);
2802 else
2803 im = igmp_mc_get_next(seq, v);
2804 ++*pos;
2805 return im;
2806}
2807
2808static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002809 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810{
2811 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002812
2813 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002815 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
2818static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2819{
2820 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002821 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2823 else {
2824 struct ip_mc_list *im = (struct ip_mc_list *)v;
2825 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2826 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002827 long delta;
2828
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829#ifdef CONFIG_IP_MULTICAST
2830 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2831 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2832 "V3";
2833#else
2834 querier = "NONE";
2835#endif
2836
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002837 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002839 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
2841
Eric Dumazeta399a802012-08-08 21:13:53 +00002842 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002844 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002846 im->tm_running,
2847 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 im->reporter);
2849 }
2850 return 0;
2851}
2852
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002853static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 .start = igmp_mc_seq_start,
2855 .next = igmp_mc_seq_next,
2856 .stop = igmp_mc_seq_stop,
2857 .show = igmp_mc_seq_show,
2858};
2859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860struct igmp_mcf_iter_state {
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08002861 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 struct net_device *dev;
2863 struct in_device *idev;
2864 struct ip_mc_list *im;
2865};
2866
2867#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2868
2869static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2870{
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08002871 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 struct ip_sf_list *psf = NULL;
2873 struct ip_mc_list *im = NULL;
2874 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2875
Pavel Emelianov7562f872007-05-03 15:13:45 -07002876 state->idev = NULL;
2877 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002878 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002880 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002881 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002883 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002884 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 spin_lock_bh(&im->lock);
2886 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002887 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 state->im = im;
2889 state->idev = idev;
2890 break;
2891 }
2892 spin_unlock_bh(&im->lock);
2893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895 return psf;
2896}
2897
2898static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2899{
2900 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2901
2902 psf = psf->sf_next;
2903 while (!psf) {
2904 spin_unlock_bh(&state->im->lock);
2905 state->im = state->im->next;
2906 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002907 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (!state->dev) {
2909 state->idev = NULL;
2910 goto out;
2911 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002912 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (!state->idev)
2914 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002915 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
2917 if (!state->im)
2918 break;
2919 spin_lock_bh(&state->im->lock);
2920 psf = state->im->sources;
2921 }
2922out:
2923 return psf;
2924}
2925
2926static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2927{
2928 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2929 if (psf)
2930 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2931 --pos;
2932 return pos ? NULL : psf;
2933}
2934
2935static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002936 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
stephen hemminger61fbab72009-11-10 07:54:55 +00002938 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2940}
2941
2942static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2943{
2944 struct ip_sf_list *psf;
2945 if (v == SEQ_START_TOKEN)
2946 psf = igmp_mcf_get_first(seq);
2947 else
2948 psf = igmp_mcf_get_next(seq, v);
2949 ++*pos;
2950 return psf;
2951}
2952
2953static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002954 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
2956 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002957 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 spin_unlock_bh(&state->im->lock);
2959 state->im = NULL;
2960 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002961 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002963 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
2966static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2967{
2968 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2969 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2970
2971 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002972 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 } else {
2974 seq_printf(seq,
2975 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002976 "0x%08x %6lu %6lu\n",
2977 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 ntohl(state->im->multiaddr),
2979 ntohl(psf->sf_inaddr),
2980 psf->sf_count[MCAST_INCLUDE],
2981 psf->sf_count[MCAST_EXCLUDE]);
2982 }
2983 return 0;
2984}
2985
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002986static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 .start = igmp_mcf_seq_start,
2988 .next = igmp_mcf_seq_next,
2989 .stop = igmp_mcf_seq_stop,
2990 .show = igmp_mcf_seq_show,
2991};
2992
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002993static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08002994{
2995 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002996 int err;
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08002997
Christoph Hellwigc3506372018-04-10 19:42:55 +02002998 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
2999 sizeof(struct igmp_mc_iter_state));
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08003000 if (!pde)
3001 goto out_igmp;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003002 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3003 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08003004 if (!pde)
3005 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08003006 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3007 SOCK_DGRAM, 0, net);
3008 if (err < 0) {
3009 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3010 err);
3011 goto out_sock;
3012 }
3013
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08003014 return 0;
3015
Madhu Challa93a714d2015-02-25 09:58:35 -08003016out_sock:
3017 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08003018out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00003019 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08003020out_igmp:
3021 return -ENOMEM;
3022}
3023
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003024static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08003025{
Gao fengece31ff2013-02-18 01:34:56 +00003026 remove_proc_entry("mcfilter", net->proc_net);
3027 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08003028 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e7282008-12-25 16:42:51 -08003029}
3030
3031static struct pernet_operations igmp_net_ops = {
3032 .init = igmp_net_init,
3033 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034};
WANG Cong72c1d3b2014-01-10 16:09:45 -08003035#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003037static int igmp_netdev_event(struct notifier_block *this,
3038 unsigned long event, void *ptr)
3039{
3040 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3041 struct in_device *in_dev;
3042
3043 switch (event) {
3044 case NETDEV_RESEND_IGMP:
3045 in_dev = __in_dev_get_rtnl(dev);
3046 if (in_dev)
3047 ip_mc_rejoin_groups(in_dev);
3048 break;
3049 default:
3050 break;
3051 }
3052 return NOTIFY_DONE;
3053}
3054
3055static struct notifier_block igmp_notifier = {
3056 .notifier_call = igmp_netdev_event,
3057};
3058
WANG Cong72c1d3b2014-01-10 16:09:45 -08003059int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003061#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003062 int err;
3063
3064 err = register_pernet_subsys(&igmp_net_ops);
3065 if (err)
3066 return err;
3067 err = register_netdevice_notifier(&igmp_notifier);
3068 if (err)
3069 goto reg_notif_fail;
3070 return 0;
3071
3072reg_notif_fail:
3073 unregister_pernet_subsys(&igmp_net_ops);
3074 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003075#else
3076 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003078}