blob: 480d0b22db1a6d0cb36688fff01966fdb7375d1f [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
Eric Dumazet903869b2019-05-22 18:35:16 -0700187static void ip_sf_list_clear_all(struct ip_sf_list *psf)
188{
189 struct ip_sf_list *next;
190
191 while (psf) {
192 next = psf->sf_next;
193 kfree(psf);
194 psf = next;
195 }
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#ifdef CONFIG_IP_MULTICAST
199
200/*
201 * Timer management
202 */
203
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000204static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 spin_lock_bh(&im->lock);
207 if (del_timer(&im->timer))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300208 refcount_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800209 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 im->reporter = 0;
211 im->unsolicit_count = 0;
212 spin_unlock_bh(&im->lock);
213}
214
215/* It must be called with locked im->lock */
216static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
217{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500218 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800220 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (!mod_timer(&im->timer, jiffies+tv+2))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300222 refcount_inc(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225static void igmp_gq_start_timer(struct in_device *in_dev)
226{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500227 int tv = prandom_u32() % in_dev->mr_maxdelay;
Michal Tesar7ababb72017-01-02 14:38:36 +0100228 unsigned long exp = jiffies + tv + 2;
229
230 if (in_dev->mr_gq_running &&
231 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
232 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 in_dev->mr_gq_running = 1;
Michal Tesar7ababb72017-01-02 14:38:36 +0100235 if (!mod_timer(&in_dev->mr_gq_timer, exp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 in_dev_hold(in_dev);
237}
238
239static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
240{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500241 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
244 in_dev_hold(in_dev);
245}
246
247static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
248{
249 spin_lock_bh(&im->lock);
250 im->unsolicit_count = 0;
251 if (del_timer(&im->timer)) {
252 if ((long)(im->timer.expires-jiffies) < max_delay) {
253 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800254 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 spin_unlock_bh(&im->lock);
256 return;
257 }
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300258 refcount_dec(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 igmp_start_timer(im, max_delay);
261 spin_unlock_bh(&im->lock);
262}
263
264
265/*
266 * Send an IGMP report.
267 */
268
269#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
270
271
272static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
273 int gdeleted, int sdeleted)
274{
275 switch (type) {
276 case IGMPV3_MODE_IS_INCLUDE:
277 case IGMPV3_MODE_IS_EXCLUDE:
278 if (gdeleted || sdeleted)
279 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800280 if (!(pmc->gsquery && !psf->sf_gsresp)) {
281 if (pmc->sfmode == MCAST_INCLUDE)
282 return 1;
283 /* don't include if this source is excluded
284 * in all filters
285 */
286 if (psf->sf_count[MCAST_INCLUDE])
287 return type == IGMPV3_MODE_IS_INCLUDE;
288 return pmc->sfcount[MCAST_EXCLUDE] ==
289 psf->sf_count[MCAST_EXCLUDE];
290 }
291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 case IGMPV3_CHANGE_TO_INCLUDE:
293 if (gdeleted || sdeleted)
294 return 0;
295 return psf->sf_count[MCAST_INCLUDE] != 0;
296 case IGMPV3_CHANGE_TO_EXCLUDE:
297 if (gdeleted || sdeleted)
298 return 0;
299 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
300 psf->sf_count[MCAST_INCLUDE])
301 return 0;
302 return pmc->sfcount[MCAST_EXCLUDE] ==
303 psf->sf_count[MCAST_EXCLUDE];
304 case IGMPV3_ALLOW_NEW_SOURCES:
305 if (gdeleted || !psf->sf_crcount)
306 return 0;
307 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
308 case IGMPV3_BLOCK_OLD_SOURCES:
309 if (pmc->sfmode == MCAST_INCLUDE)
310 return gdeleted || (psf->sf_crcount && sdeleted);
311 return psf->sf_crcount && !gdeleted && !sdeleted;
312 }
313 return 0;
314}
315
316static int
317igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
318{
319 struct ip_sf_list *psf;
320 int scount = 0;
321
Weilong Chenc71151f2013-12-23 14:37:29 +0800322 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
324 continue;
325 scount++;
326 }
327 return scount;
328}
329
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800330/* source address selection per RFC 3376 section 4.2.13 */
331static __be32 igmpv3_get_srcaddr(struct net_device *dev,
332 const struct flowi4 *fl4)
333{
334 struct in_device *in_dev = __in_dev_get_rcu(dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +0200335 const struct in_ifaddr *ifa;
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800336
337 if (!in_dev)
338 return htonl(INADDR_ANY);
339
Florian Westphalcd5a4112019-05-31 18:27:07 +0200340 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Felix Fietkauad23b752018-01-19 11:50:46 +0100341 if (fl4->saddr == ifa->ifa_local)
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800342 return fl4->saddr;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200343 }
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800344
345 return htonl(INADDR_ANY);
346}
347
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100348static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
350 struct sk_buff *skb;
351 struct rtable *rt;
352 struct iphdr *pip;
353 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700354 struct net *net = dev_net(dev);
David S. Miller31e4543d2011-05-03 20:25:42 -0700355 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000356 int hlen = LL_RESERVED_SPACE(dev);
357 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100358 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000360 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000361 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000362 GFP_ATOMIC | __GFP_NOWARN);
363 if (skb)
364 break;
365 size >>= 1;
366 if (size < 256)
367 return NULL;
368 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200369 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
David S. Miller31e4543d2011-05-03 20:25:42 -0700371 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500372 0, 0,
373 IPPROTO_IGMP, 0, dev->ifindex);
374 if (IS_ERR(rt)) {
375 kfree_skb(skb);
376 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Changli Gaod8d1f302010-06-10 23:31:35 -0700379 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 skb->dev = dev;
381
Herbert Xu66088242011-11-18 02:20:04 +0000382 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800383 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300385 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700386 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300387 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 pip->version = 4;
390 pip->ihl = (sizeof(struct iphdr)+4)>>2;
391 pip->tos = 0xc0;
392 pip->frag_off = htons(IP_DF);
393 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700394 pip->daddr = fl4.daddr;
Eric Dumazete7aadb22018-02-01 10:26:57 -0800395
396 rcu_read_lock();
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800397 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
Eric Dumazete7aadb22018-02-01 10:26:57 -0800398 rcu_read_unlock();
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 pip->protocol = IPPROTO_IGMP;
401 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100402 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000403 ((u8 *)&pip[1])[0] = IPOPT_RA;
404 ((u8 *)&pip[1])[1] = 4;
405 ((u8 *)&pip[1])[2] = 0;
406 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700408 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300409 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300410 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
412 pig->resv1 = 0;
413 pig->csum = 0;
414 pig->resv2 = 0;
415 pig->ngrec = 0;
416 return skb;
417}
418
419static int igmpv3_sendpack(struct sk_buff *skb)
420{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300421 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000422 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300424 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Eric W. Biederman33224b12015-10-07 16:48:46 -0500426 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
430{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800431 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
Eric Dumazetb5476022017-12-11 07:17:39 -0800435 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 struct net_device *dev = pmc->interface->dev;
438 struct igmpv3_report *pih;
439 struct igmpv3_grec *pgr;
440
Eric Dumazetb5476022017-12-11 07:17:39 -0800441 if (!skb) {
442 skb = igmpv3_newpack(dev, mtu);
443 if (!skb)
444 return NULL;
445 }
Johannes Berg4df864c2017-06-16 14:29:21 +0200446 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 pgr->grec_type = type;
448 pgr->grec_auxwords = 0;
449 pgr->grec_nsrcs = 0;
450 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300451 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 pih->ngrec = htons(ntohs(pih->ngrec)+1);
453 *ppgr = pgr;
454 return skb;
455}
456
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100457#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
460 int type, int gdeleted, int sdeleted)
461{
462 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200463 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 struct igmpv3_report *pih;
465 struct igmpv3_grec *pgr = NULL;
466 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800467 int scount, stotal, first, isquery, truncate;
Eric Dumazetb5476022017-12-11 07:17:39 -0800468 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 if (pmc->multiaddr == IGMP_ALL_HOSTS)
471 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200472 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100473 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric Dumazetb5476022017-12-11 07:17:39 -0800475 mtu = READ_ONCE(dev->mtu);
476 if (mtu < IPV4_MIN_MTU)
477 return skb;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
480 type == IGMPV3_MODE_IS_EXCLUDE;
481 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
482 type == IGMPV3_CHANGE_TO_EXCLUDE;
483
David L Stevensad125832006-01-18 14:20:56 -0800484 stotal = scount = 0;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
487
David L Stevensad125832006-01-18 14:20:56 -0800488 if (!*psf_list)
489 goto empty_source;
490
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300491 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* EX and TO_EX get a fresh packet, if needed */
494 if (truncate) {
495 if (pih && pih->ngrec &&
496 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
497 if (skb)
498 igmpv3_sendpack(skb);
Eric Dumazetb5476022017-12-11 07:17:39 -0800499 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
502 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800504 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700505 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 psf_next = psf->sf_next;
508
509 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
510 psf_prev = psf;
511 continue;
512 }
513
Hangbin Liua0525172016-08-02 18:02:57 +0800514 /* Based on RFC3376 5.1. Should not send source-list change
515 * records when there is a filter mode change.
516 */
517 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
518 (!gdeleted && pmc->crcount)) &&
519 (type == IGMPV3_ALLOW_NEW_SOURCES ||
520 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
521 goto decrease_sf_crcount;
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* clear marks on query responses */
524 if (isquery)
525 psf->sf_gsresp = 0;
526
Al Viro63007722006-09-27 18:31:32 -0700527 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 first*sizeof(struct igmpv3_grec)) {
529 if (truncate && !first)
530 break; /* truncate these */
531 if (pgr)
532 pgr->grec_nsrcs = htons(scount);
533 if (skb)
534 igmpv3_sendpack(skb);
Eric Dumazetb5476022017-12-11 07:17:39 -0800535 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 first = 1;
537 scount = 0;
538 }
539 if (first) {
Eric Dumazetb5476022017-12-11 07:17:39 -0800540 skb = add_grhead(skb, pmc, type, &pgr, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 first = 0;
542 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800543 if (!skb)
544 return NULL;
Johannes Berg4df864c2017-06-16 14:29:21 +0200545 psrc = skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800547 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
549 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
Hangbin Liua0525172016-08-02 18:02:57 +0800550decrease_sf_crcount:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 psf->sf_crcount--;
552 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
553 if (psf_prev)
554 psf_prev->sf_next = psf->sf_next;
555 else
556 *psf_list = psf->sf_next;
557 kfree(psf);
558 continue;
559 }
560 }
561 psf_prev = psf;
562 }
David L Stevensad125832006-01-18 14:20:56 -0800563
564empty_source:
565 if (!stotal) {
566 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
567 type == IGMPV3_BLOCK_OLD_SOURCES)
568 return skb;
569 if (pmc->crcount || isquery) {
570 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800571 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800572 igmpv3_sendpack(skb);
573 skb = NULL; /* add_grhead will get a new one */
574 }
Eric Dumazetb5476022017-12-11 07:17:39 -0800575 skb = add_grhead(skb, pmc, type, &pgr, mtu);
David L Stevensad125832006-01-18 14:20:56 -0800576 }
577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (pgr)
579 pgr->grec_nsrcs = htons(scount);
580
581 if (isquery)
582 pmc->gsquery = 0; /* clear query state on report */
583 return skb;
584}
585
586static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
587{
588 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200589 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int type;
591
592 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000593 rcu_read_lock();
594 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (pmc->multiaddr == IGMP_ALL_HOSTS)
596 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100597 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200598 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100599 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 spin_lock_bh(&pmc->lock);
601 if (pmc->sfcount[MCAST_EXCLUDE])
602 type = IGMPV3_MODE_IS_EXCLUDE;
603 else
604 type = IGMPV3_MODE_IS_INCLUDE;
605 skb = add_grec(skb, pmc, type, 0, 0);
606 spin_unlock_bh(&pmc->lock);
607 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000608 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 } else {
610 spin_lock_bh(&pmc->lock);
611 if (pmc->sfcount[MCAST_EXCLUDE])
612 type = IGMPV3_MODE_IS_EXCLUDE;
613 else
614 type = IGMPV3_MODE_IS_INCLUDE;
615 skb = add_grec(skb, pmc, type, 0, 0);
616 spin_unlock_bh(&pmc->lock);
617 }
618 if (!skb)
619 return 0;
620 return igmpv3_sendpack(skb);
621}
622
623/*
624 * remove zero-count source records from a source filter list
625 */
626static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
627{
628 struct ip_sf_list *psf_prev, *psf_next, *psf;
629
630 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800631 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 psf_next = psf->sf_next;
633 if (psf->sf_crcount == 0) {
634 if (psf_prev)
635 psf_prev->sf_next = psf->sf_next;
636 else
637 *ppsf = psf->sf_next;
638 kfree(psf);
639 } else
640 psf_prev = psf;
641 }
642}
643
Eric Dumazet3580d042019-05-22 16:51:22 -0700644static void kfree_pmc(struct ip_mc_list *pmc)
645{
646 ip_sf_list_clear_all(pmc->sources);
647 ip_sf_list_clear_all(pmc->tomb);
648 kfree(pmc);
649}
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static void igmpv3_send_cr(struct in_device *in_dev)
652{
653 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
654 struct sk_buff *skb = NULL;
655 int type, dtype;
656
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000657 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 spin_lock_bh(&in_dev->mc_tomb_lock);
659
660 /* deleted MCA's */
661 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800662 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 pmc_next = pmc->next;
664 if (pmc->sfmode == MCAST_INCLUDE) {
665 type = IGMPV3_BLOCK_OLD_SOURCES;
666 dtype = IGMPV3_BLOCK_OLD_SOURCES;
667 skb = add_grec(skb, pmc, type, 1, 0);
668 skb = add_grec(skb, pmc, dtype, 1, 1);
669 }
670 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 if (pmc->sfmode == MCAST_EXCLUDE) {
672 type = IGMPV3_CHANGE_TO_INCLUDE;
673 skb = add_grec(skb, pmc, type, 1, 0);
674 }
David L Stevensad125832006-01-18 14:20:56 -0800675 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (pmc->crcount == 0) {
677 igmpv3_clear_zeros(&pmc->tomb);
678 igmpv3_clear_zeros(&pmc->sources);
679 }
680 }
681 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
682 if (pmc_prev)
683 pmc_prev->next = pmc_next;
684 else
685 in_dev->mc_tomb = pmc_next;
686 in_dev_put(pmc->interface);
Eric Dumazet3580d042019-05-22 16:51:22 -0700687 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 } else
689 pmc_prev = pmc;
690 }
691 spin_unlock_bh(&in_dev->mc_tomb_lock);
692
693 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000694 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 spin_lock_bh(&pmc->lock);
696 if (pmc->sfcount[MCAST_EXCLUDE]) {
697 type = IGMPV3_BLOCK_OLD_SOURCES;
698 dtype = IGMPV3_ALLOW_NEW_SOURCES;
699 } else {
700 type = IGMPV3_ALLOW_NEW_SOURCES;
701 dtype = IGMPV3_BLOCK_OLD_SOURCES;
702 }
703 skb = add_grec(skb, pmc, type, 0, 0);
704 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
705
706 /* filter mode changes */
707 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 if (pmc->sfmode == MCAST_EXCLUDE)
709 type = IGMPV3_CHANGE_TO_EXCLUDE;
710 else
711 type = IGMPV3_CHANGE_TO_INCLUDE;
712 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800713 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 spin_unlock_bh(&pmc->lock);
716 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000717 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 if (!skb)
720 return;
721 (void) igmpv3_sendpack(skb);
722}
723
724static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
725 int type)
726{
727 struct sk_buff *skb;
728 struct iphdr *iph;
729 struct igmphdr *ih;
730 struct rtable *rt;
731 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700732 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700733 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e4543d2011-05-03 20:25:42 -0700734 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700735 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000736 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
739 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100740
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200741 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100742 return 0;
743
744 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 dst = IGMP_ALL_ROUTER;
746 else
747 dst = group;
748
David S. Miller31e4543d2011-05-03 20:25:42 -0700749 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500750 0, 0,
751 IPPROTO_IGMP, 0, dev->ifindex);
752 if (IS_ERR(rt))
753 return -1;
754
Herbert Xu66088242011-11-18 02:20:04 +0000755 hlen = LL_RESERVED_SPACE(dev);
756 tlen = dev->needed_tailroom;
757 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100758 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ip_rt_put(rt);
760 return -1;
761 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200762 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Changli Gaod8d1f302010-06-10 23:31:35 -0700764 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Herbert Xu66088242011-11-18 02:20:04 +0000766 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300768 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700769 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300770 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 iph->version = 4;
773 iph->ihl = (sizeof(struct iphdr)+4)>>2;
774 iph->tos = 0xc0;
775 iph->frag_off = htons(IP_DF);
776 iph->ttl = 1;
777 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700778 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100780 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000781 ((u8 *)&iph[1])[0] = IPOPT_RA;
782 ((u8 *)&iph[1])[1] = 4;
783 ((u8 *)&iph[1])[2] = 0;
784 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Johannes Berg4df864c2017-06-16 14:29:21 +0200786 ih = skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800787 ih->type = type;
788 ih->code = 0;
789 ih->csum = 0;
790 ih->group = group;
791 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Eric W. Biederman33224b12015-10-07 16:48:46 -0500793 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
Kees Cooke99e88a2017-10-16 14:43:17 -0700796static void igmp_gq_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Kees Cooke99e88a2017-10-16 14:43:17 -0700798 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 in_dev->mr_gq_running = 0;
801 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700802 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Kees Cooke99e88a2017-10-16 14:43:17 -0700805static void igmp_ifc_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Kees Cooke99e88a2017-10-16 14:43:17 -0700807 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 igmpv3_send_cr(in_dev);
810 if (in_dev->mr_ifc_count) {
811 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100812 igmp_ifc_start_timer(in_dev,
813 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700815 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818static void igmp_ifc_event(struct in_device *in_dev)
819{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200820 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
822 return;
Nikolay Borisov165094a2016-02-08 23:29:24 +0200823 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 igmp_ifc_start_timer(in_dev, 1);
825}
826
827
Kees Cooke99e88a2017-10-16 14:43:17 -0700828static void igmp_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Kees Cooke99e88a2017-10-16 14:43:17 -0700830 struct ip_mc_list *im = from_timer(im, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct in_device *in_dev = im->interface;
832
833 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800834 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Hangbin Liu4fb72532018-08-29 18:06:08 +0800836 if (im->unsolicit_count && --im->unsolicit_count)
William Manleycab70042013-08-06 19:03:13 +0100837 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Hangbin Liu4fb72532018-08-29 18:06:08 +0800838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 im->reporter = 1;
840 spin_unlock(&im->lock);
841
842 if (IGMP_V1_SEEN(in_dev))
843 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
844 else if (IGMP_V2_SEEN(in_dev))
845 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
846 else
847 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
848
849 ip_ma_put(im);
850}
851
David L Stevensad125832006-01-18 14:20:56 -0800852/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700853static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct ip_sf_list *psf;
856 int i, scount;
857
858 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800859 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (scount == nsrcs)
861 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800862 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800863 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000864 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800865 pmc->sfcount[MCAST_EXCLUDE] !=
866 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800867 break;
David L Stevensad125832006-01-18 14:20:56 -0800868 if (srcs[i] == psf->sf_inaddr) {
869 scount++;
870 break;
871 }
872 }
873 }
874 pmc->gsquery = 0;
875 if (scount == nsrcs) /* all sources excluded */
876 return 0;
877 return 1;
878}
879
Al Viroea4d9e72006-09-27 18:30:52 -0700880static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800881{
882 struct ip_sf_list *psf;
883 int i, scount;
884
885 if (pmc->sfmode == MCAST_EXCLUDE)
886 return igmp_xmarksources(pmc, nsrcs, srcs);
887
888 /* mark INCLUDE-mode sources */
889 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800890 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800891 if (scount == nsrcs)
892 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800893 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (srcs[i] == psf->sf_inaddr) {
895 psf->sf_gsresp = 1;
896 scount++;
897 break;
898 }
899 }
David L Stevensad125832006-01-18 14:20:56 -0800900 if (!scount) {
901 pmc->gsquery = 0;
902 return 0;
903 }
904 pmc->gsquery = 1;
905 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906}
907
Eric Dumazetd679c532012-09-06 20:37:06 +0000908/* return true if packet was dropped */
909static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200912 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 /* Timers are only set for non-local groups */
915
916 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000917 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200918 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100919 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000921 rcu_read_lock();
922 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (im->multiaddr == group) {
924 igmp_stop_timer(im);
925 break;
926 }
927 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000928 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000929 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930}
931
Eric Dumazetd679c532012-09-06 20:37:06 +0000932/* return true if packet was dropped */
933static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int len)
935{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300936 struct igmphdr *ih = igmp_hdr(skb);
937 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700939 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int max_delay;
941 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200942 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944
David Stevens5b7c8402010-09-30 14:29:40 +0000945 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 if (ih->code == 0) {
947 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900948
Fabian Frederick436f7c22014-11-04 20:52:14 +0100949 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 in_dev->mr_v1_seen = jiffies +
Hangbin Liu966c37f2018-10-26 11:30:35 +0800951 (in_dev->mr_qrv * in_dev->mr_qi) +
952 in_dev->mr_qri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 group = 0;
954 } else {
955 /* v2 router present */
956 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
957 in_dev->mr_v2_seen = jiffies +
Hangbin Liu966c37f2018-10-26 11:30:35 +0800958 (in_dev->mr_qrv * in_dev->mr_qi) +
959 in_dev->mr_qri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 /* cancel the interface change timer */
962 in_dev->mr_ifc_count = 0;
963 if (del_timer(&in_dev->mr_ifc_timer))
964 __in_dev_put(in_dev);
965 /* clear deleted report items */
966 igmpv3_clear_delrec(in_dev);
967 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000968 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000969 } else if (IGMP_V1_SEEN(in_dev)) {
970 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100971 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000972 group = 0;
973 } else if (IGMP_V2_SEEN(in_dev)) {
974 /* this is a v3 query with v2 queriers present;
975 * Interpretation of the max_delay code is problematic here.
976 * A real v2 host would use ih_code directly, while v3 has a
977 * different encoding. We use the v3 encoding as more likely
978 * to be intended in a v3 query.
979 */
980 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800981 if (!max_delay)
982 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 } else { /* v3 */
984 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000985 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900986
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300987 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900989 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700990 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000991 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300992 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
995 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
996 if (!max_delay)
997 max_delay = 1; /* can't mod w/ 0 */
998 in_dev->mr_maxdelay = max_delay;
Hangbin Liu966c37f2018-10-26 11:30:35 +0800999
1000 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
1001 * received value was zero, use the default or statically
1002 * configured value.
1003 */
1004 in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
1005 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
1006
1007 /* RFC3376, 8.3. Query Response Interval:
1008 * The number of seconds represented by the [Query Response
1009 * Interval] must be less than the [Query Interval].
1010 */
1011 if (in_dev->mr_qri >= in_dev->mr_qi)
1012 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if (!group) { /* general query */
1015 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +02001016 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +00001018 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020 /* mark sources to include, if group & source-specific */
1021 mark = ih3->nsrcs != 0;
1022 }
1023
1024 /*
1025 * - Start the timers in all of our membership records
1026 * that the query applies to for the interface on
1027 * which the query arrived excl. those that belong
1028 * to a "local" group (224.0.0.X)
1029 * - For timers already running check if they need to
1030 * be reset.
1031 * - Use the igmp->igmp_code field as the maximum
1032 * delay possible
1033 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001034 rcu_read_lock();
1035 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -08001036 int changed;
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 if (group && group != im->multiaddr)
1039 continue;
1040 if (im->multiaddr == IGMP_ALL_HOSTS)
1041 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001042 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001043 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001044 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 spin_lock_bh(&im->lock);
1046 if (im->tm_running)
1047 im->gsquery = im->gsquery && mark;
1048 else
1049 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -08001050 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001051 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -08001053 if (changed)
1054 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001056 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +00001057 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001060/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061int igmp_rcv(struct sk_buff *skb)
1062{
1063 /* This basically follows the spec line by line -- see RFC1112 */
1064 struct igmphdr *ih;
David Ahernc7b725b2017-08-15 18:38:42 -07001065 struct net_device *dev = skb->dev;
1066 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001068 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
David Ahernc7b725b2017-08-15 18:38:42 -07001070 if (netif_is_l3_master(dev)) {
1071 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1072 if (!dev)
1073 goto drop;
1074 }
1075
1076 in_dev = __in_dev_get_rcu(dev);
Ian Morris51456b22015-04-03 09:17:26 +01001077 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001078 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Herbert Xufb286bb2005-11-10 13:01:24 -08001080 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001081 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001082
Tom Herbertde08dc12014-05-07 16:52:10 -07001083 if (skb_checksum_simple_validate(skb))
1084 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001086 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 switch (ih->type) {
1088 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001089 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 break;
1091 case IGMP_HOST_MEMBERSHIP_REPORT:
1092 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001094 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 break;
David Stevens24c69272005-12-02 20:32:59 -08001096 /* don't rely on MC router hearing unicast reports */
1097 if (skb->pkt_type == PACKET_MULTICAST ||
1098 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001099 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 break;
1101 case IGMP_PIM:
1102#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return pim_rcv_v1(skb);
1104#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001105 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 case IGMP_DVMRP:
1107 case IGMP_TRACE:
1108 case IGMP_HOST_LEAVE_MESSAGE:
1109 case IGMP_MTRACE:
1110 case IGMP_MTRACE_RESP:
1111 break;
1112 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001115
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001116drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001117 if (dropped)
1118 kfree_skb(skb);
1119 else
1120 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 return 0;
1122}
1123
1124#endif
1125
1126
1127/*
1128 * Add a filter to a device
1129 */
1130
Al Viro63007722006-09-27 18:31:32 -07001131static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
1133 char buf[MAX_ADDR_LEN];
1134 struct net_device *dev = in_dev->dev;
1135
1136 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1137 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001138 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 routine. Something sort of:
1140 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1141 --ANK
1142 */
1143 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001144 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147/*
1148 * Remove a filter from a device
1149 */
1150
Al Viro63007722006-09-27 18:31:32 -07001151static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 char buf[MAX_ADDR_LEN];
1154 struct net_device *dev = in_dev->dev;
1155
1156 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001157 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
1160#ifdef CONFIG_IP_MULTICAST
1161/*
1162 * deleted ip_mc_list manipulation
1163 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001164static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
1165 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
1167 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001168 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 /* this is an "ip_mc_list" for convenience; only the fields below
1171 * are actually used. In particular, the refcnt and users are not
1172 * used for management of the delete list. Using the same structure
1173 * for deleted items allows change reports to use common code with
1174 * non-deleted or query-response MCA's.
1175 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001176 pmc = kzalloc(sizeof(*pmc), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (!pmc)
1178 return;
WANG Congb4846fc2017-06-20 10:46:27 -07001179 spin_lock_init(&pmc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 spin_lock_bh(&im->lock);
1181 pmc->interface = im->interface;
1182 in_dev_hold(in_dev);
1183 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001184 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 pmc->sfmode = im->sfmode;
1186 if (pmc->sfmode == MCAST_INCLUDE) {
1187 struct ip_sf_list *psf;
1188
1189 pmc->tomb = im->tomb;
1190 pmc->sources = im->sources;
1191 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001192 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 psf->sf_crcount = pmc->crcount;
1194 }
1195 spin_unlock_bh(&im->lock);
1196
1197 spin_lock_bh(&in_dev->mc_tomb_lock);
1198 pmc->next = in_dev->mc_tomb;
1199 in_dev->mc_tomb = pmc;
1200 spin_unlock_bh(&in_dev->mc_tomb_lock);
1201}
1202
Hangbin Liu24803f32016-11-14 16:16:28 +08001203/*
1204 * restore ip_mc_list deleted records
1205 */
1206static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001209 struct ip_sf_list *psf;
1210 struct net *net = dev_net(in_dev->dev);
1211 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 spin_lock_bh(&in_dev->mc_tomb_lock);
1214 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001215 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (pmc->multiaddr == multiaddr)
1217 break;
1218 pmc_prev = pmc;
1219 }
1220 if (pmc) {
1221 if (pmc_prev)
1222 pmc_prev->next = pmc->next;
1223 else
1224 in_dev->mc_tomb = pmc->next;
1225 }
1226 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001227
1228 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001230 im->interface = pmc->interface;
Hangbin Liu08d3ffc2018-07-20 14:04:27 +08001231 if (im->sfmode == MCAST_INCLUDE) {
Eric Dumazete5b1c6c2019-06-27 01:27:01 -07001232 swap(im->tomb, pmc->tomb);
1233 swap(im->sources, pmc->sources);
Hangbin Liu24803f32016-11-14 16:16:28 +08001234 for (psf = im->sources; psf; psf = psf->sf_next)
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001235 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1236 } else {
1237 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239 in_dev_put(pmc->interface);
Eric Dumazet3580d042019-05-22 16:51:22 -07001240 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001242 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Hangbin Liu24803f32016-11-14 16:16:28 +08001245/*
1246 * flush ip_mc_list deleted records
1247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static void igmpv3_clear_delrec(struct in_device *in_dev)
1249{
1250 struct ip_mc_list *pmc, *nextpmc;
1251
1252 spin_lock_bh(&in_dev->mc_tomb_lock);
1253 pmc = in_dev->mc_tomb;
1254 in_dev->mc_tomb = NULL;
1255 spin_unlock_bh(&in_dev->mc_tomb_lock);
1256
1257 for (; pmc; pmc = nextpmc) {
1258 nextpmc = pmc->next;
1259 ip_mc_clear_src(pmc);
1260 in_dev_put(pmc->interface);
Eric Dumazet3580d042019-05-22 16:51:22 -07001261 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001264 rcu_read_lock();
1265 for_each_pmc_rcu(in_dev, pmc) {
Eric Dumazet3580d042019-05-22 16:51:22 -07001266 struct ip_sf_list *psf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 spin_lock_bh(&pmc->lock);
1269 psf = pmc->tomb;
1270 pmc->tomb = NULL;
1271 spin_unlock_bh(&pmc->lock);
Eric Dumazet3580d042019-05-22 16:51:22 -07001272 ip_sf_list_clear_all(psf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001274 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276#endif
1277
Florian Fainelli9fb20802019-02-01 20:20:52 -08001278static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct in_device *in_dev = im->interface;
1281#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001282 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 int reporter;
1284#endif
1285
1286 if (im->loaded) {
1287 im->loaded = 0;
1288 ip_mc_filter_del(in_dev, im->multiaddr);
1289 }
1290
1291#ifdef CONFIG_IP_MULTICAST
1292 if (im->multiaddr == IGMP_ALL_HOSTS)
1293 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001294 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001295 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 reporter = im->reporter;
1298 igmp_stop_timer(im);
1299
1300 if (!in_dev->dead) {
1301 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001302 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 if (IGMP_V2_SEEN(in_dev)) {
1304 if (reporter)
1305 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001306 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 /* IGMPv3 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001309 igmpv3_add_delrec(in_dev, im, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 igmp_ifc_event(in_dev);
1312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Florian Fainelli9fb20802019-02-01 20:20:52 -08001316static void igmp_group_dropped(struct ip_mc_list *im)
1317{
1318 __igmp_group_dropped(im, GFP_KERNEL);
1319}
1320
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001321static void igmp_group_added(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001324#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001325 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (im->loaded == 0) {
1329 im->loaded = 1;
1330 ip_mc_filter_add(in_dev, im->multiaddr);
1331 }
1332
1333#ifdef CONFIG_IP_MULTICAST
1334 if (im->multiaddr == IGMP_ALL_HOSTS)
1335 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001336 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001337 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (in_dev->dead)
1340 return;
Hangbin Liuff065252018-08-29 18:06:10 +08001341
1342 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1344 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001345 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 spin_unlock_bh(&im->lock);
1347 return;
1348 }
1349 /* else, v3 */
1350
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001351 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1352 * not send filter-mode change record as the mode should be from
1353 * IN() to IN(A).
1354 */
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001355 if (im->sfmode == MCAST_EXCLUDE)
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001356 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 igmp_ifc_event(in_dev);
1359#endif
1360}
1361
1362
1363/*
1364 * Multicast list managers
1365 */
1366
Eric Dumazete9897072013-06-07 08:48:57 -07001367static u32 ip_mc_hash(const struct ip_mc_list *im)
1368{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001369 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001370}
1371
1372static void ip_mc_hash_add(struct in_device *in_dev,
1373 struct ip_mc_list *im)
1374{
1375 struct ip_mc_list __rcu **mc_hash;
1376 u32 hash;
1377
1378 mc_hash = rtnl_dereference(in_dev->mc_hash);
1379 if (mc_hash) {
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_assign_pointer(mc_hash[hash], im);
1383 return;
1384 }
1385
1386 /* do not use a hash table for small number of items */
1387 if (in_dev->mc_count < 4)
1388 return;
1389
1390 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1391 GFP_KERNEL);
1392 if (!mc_hash)
1393 return;
1394
1395 for_each_pmc_rtnl(in_dev, im) {
1396 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001397 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001398 RCU_INIT_POINTER(mc_hash[hash], im);
1399 }
1400
1401 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1402}
1403
1404static void ip_mc_hash_remove(struct in_device *in_dev,
1405 struct ip_mc_list *im)
1406{
1407 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1408 struct ip_mc_list *aux;
1409
1410 if (!mc_hash)
1411 return;
1412 mc_hash += ip_mc_hash(im);
1413 while ((aux = rtnl_dereference(*mc_hash)) != im)
1414 mc_hash = &aux->next_hash;
1415 *mc_hash = im->next_hash;
1416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419/*
1420 * A socket has joined a multicast group on device dev.
1421 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001422static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1423 unsigned int mode, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct ip_mc_list *im;
1426
1427 ASSERT_RTNL();
1428
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001429 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 if (im->multiaddr == addr) {
1431 im->users++;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001432 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto out;
1434 }
1435 }
1436
Florian Fainelli9fb20802019-02-01 20:20:52 -08001437 im = kzalloc(sizeof(*im), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 if (!im)
1439 goto out;
1440
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001441 im->users = 1;
1442 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001444 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /* initial mode is (EX, empty) */
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001446 im->sfmode = mode;
1447 im->sfcount[mode] = 1;
Reshetova, Elena8851ab52017-06-30 13:08:02 +03001448 refcount_set(&im->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 spin_lock_init(&im->lock);
1450#ifdef CONFIG_IP_MULTICAST
Kees Cooke99e88a2017-10-16 14:43:17 -07001451 timer_setup(&im->timer, igmp_timer_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001453
1454 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001455 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001456 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001457
Eric Dumazete9897072013-06-07 08:48:57 -07001458 ip_mc_hash_add(in_dev, im);
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001461 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001463 igmp_group_added(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (!in_dev->dead)
1465 ip_rt_multicast_event(in_dev);
1466out:
1467 return;
1468}
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001469
Florian Fainelli9fb20802019-02-01 20:20:52 -08001470void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1471{
1472 ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
1473}
1474EXPORT_SYMBOL(__ip_mc_inc_group);
1475
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001476void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1477{
Li RongQinga1c4cd62019-08-20 13:52:47 +08001478 __ip_mc_inc_group(in_dev, addr, GFP_KERNEL);
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001479}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001480EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001482static int ip_mc_check_iphdr(struct sk_buff *skb)
1483{
1484 const struct iphdr *iph;
1485 unsigned int len;
1486 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1487
1488 if (!pskb_may_pull(skb, offset))
1489 return -EINVAL;
1490
1491 iph = ip_hdr(skb);
1492
1493 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1494 return -EINVAL;
1495
1496 offset += ip_hdrlen(skb) - sizeof(*iph);
1497
1498 if (!pskb_may_pull(skb, offset))
1499 return -EINVAL;
1500
1501 iph = ip_hdr(skb);
1502
1503 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1504 return -EINVAL;
1505
1506 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1507 if (skb->len < len || len < offset)
1508 return -EINVAL;
1509
1510 skb_set_transport_header(skb, offset);
1511
1512 return 0;
1513}
1514
1515static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1516{
1517 unsigned int len = skb_transport_offset(skb);
1518
1519 len += sizeof(struct igmpv3_report);
1520
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001521 return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001522}
1523
1524static int ip_mc_check_igmp_query(struct sk_buff *skb)
1525{
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001526 unsigned int transport_len = ip_transport_len(skb);
1527 unsigned int len;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001528
1529 /* IGMPv{1,2}? */
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001530 if (transport_len != sizeof(struct igmphdr)) {
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001531 /* or IGMPv3? */
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001532 if (transport_len < sizeof(struct igmpv3_query))
1533 return -EINVAL;
1534
1535 len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1536 if (!ip_mc_may_pull(skb, len))
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001537 return -EINVAL;
1538 }
1539
1540 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1541 * all-systems destination addresses (224.0.0.1) for general queries
1542 */
1543 if (!igmp_hdr(skb)->group &&
1544 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1545 return -EINVAL;
1546
1547 return 0;
1548}
1549
1550static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1551{
1552 switch (igmp_hdr(skb)->type) {
1553 case IGMP_HOST_LEAVE_MESSAGE:
1554 case IGMP_HOST_MEMBERSHIP_REPORT:
1555 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001556 return 0;
1557 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1558 return ip_mc_check_igmp_reportv3(skb);
1559 case IGMP_HOST_MEMBERSHIP_QUERY:
1560 return ip_mc_check_igmp_query(skb);
1561 default:
1562 return -ENOMSG;
1563 }
1564}
1565
1566static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1567{
1568 return skb_checksum_simple_validate(skb);
1569}
1570
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001571static int ip_mc_check_igmp_csum(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001572{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001573 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001574 unsigned int transport_len = ip_transport_len(skb);
1575 struct sk_buff *skb_chk;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001576
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001577 if (!ip_mc_may_pull(skb, len))
1578 return -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001579
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001580 skb_chk = skb_checksum_trimmed(skb, transport_len,
1581 ip_mc_validate_checksum);
1582 if (!skb_chk)
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001583 return -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001584
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001585 if (skb_chk != skb)
Linus Lüssinga5169932015-08-13 05:54:07 +02001586 kfree_skb(skb_chk);
1587
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001588 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001589}
1590
1591/**
1592 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1593 * @skb: the skb to validate
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001594 *
1595 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001596 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001597 *
1598 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1599 * standard
1600 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1601 * -ENOMEM: A memory allocation failure happened.
1602 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001603 * Caller needs to set the skb network header and free any returned skb if it
1604 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001605 */
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001606int ip_mc_check_igmp(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001607{
1608 int ret = ip_mc_check_iphdr(skb);
1609
1610 if (ret < 0)
1611 return ret;
1612
1613 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1614 return -ENOMSG;
1615
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001616 ret = ip_mc_check_igmp_csum(skb);
1617 if (ret < 0)
1618 return ret;
1619
1620 return ip_mc_check_igmp_msg(skb);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001621}
1622EXPORT_SYMBOL(ip_mc_check_igmp);
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001625 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001626 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001627static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001628{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001629#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001630 struct ip_mc_list *im;
1631 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001632 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001633
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001634 ASSERT_RTNL();
1635
1636 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001637 if (im->multiaddr == IGMP_ALL_HOSTS)
1638 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001639 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001640 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001641 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001642
Eric Dumazet866f3b22010-11-18 09:33:19 -08001643 /* a failover is happening and switches
1644 * must be notified immediately
1645 */
1646 if (IGMP_V1_SEEN(in_dev))
1647 type = IGMP_HOST_MEMBERSHIP_REPORT;
1648 else if (IGMP_V2_SEEN(in_dev))
1649 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1650 else
1651 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1652 igmp_send_report(in_dev, im, type);
1653 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001654#endif
1655}
1656
1657/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 * A socket has left a multicast group on device dev
1659 */
1660
Florian Fainelli9fb20802019-02-01 20:20:52 -08001661void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001663 struct ip_mc_list *i;
1664 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001667
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001668 for (ip = &in_dev->mc_list;
1669 (i = rtnl_dereference(*ip)) != NULL;
1670 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001671 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001673 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001674 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001675 in_dev->mc_count--;
Florian Fainelli9fb20802019-02-01 20:20:52 -08001676 __igmp_group_dropped(i, gfp);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001677 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 if (!in_dev->dead)
1680 ip_rt_multicast_event(in_dev);
1681
1682 ip_ma_put(i);
1683 return;
1684 }
1685 break;
1686 }
1687 }
1688}
Florian Fainelli9fb20802019-02-01 20:20:52 -08001689EXPORT_SYMBOL(__ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Moni Shoua75c78502009-09-15 02:37:40 -07001691/* Device changing type */
1692
1693void ip_mc_unmap(struct in_device *in_dev)
1694{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001695 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001696
1697 ASSERT_RTNL();
1698
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001699 for_each_pmc_rtnl(in_dev, pmc)
1700 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001701}
1702
1703void ip_mc_remap(struct in_device *in_dev)
1704{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001705 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001706
1707 ASSERT_RTNL();
1708
Hangbin Liu24803f32016-11-14 16:16:28 +08001709 for_each_pmc_rtnl(in_dev, pmc) {
1710#ifdef CONFIG_IP_MULTICAST
1711 igmpv3_del_delrec(in_dev, pmc);
1712#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001713 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001714 }
Moni Shoua75c78502009-09-15 02:37:40 -07001715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717/* Device going down */
1718
1719void ip_mc_down(struct in_device *in_dev)
1720{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001721 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 ASSERT_RTNL();
1724
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001725 for_each_pmc_rtnl(in_dev, pmc)
1726 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728#ifdef CONFIG_IP_MULTICAST
1729 in_dev->mr_ifc_count = 0;
1730 if (del_timer(&in_dev->mr_ifc_timer))
1731 __in_dev_put(in_dev);
1732 in_dev->mr_gq_running = 0;
1733 if (del_timer(&in_dev->mr_gq_timer))
1734 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735#endif
1736
1737 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1738}
1739
Hangbin Liu966c37f2018-10-26 11:30:35 +08001740#ifdef CONFIG_IP_MULTICAST
1741static void ip_mc_reset(struct in_device *in_dev)
1742{
1743 struct net *net = dev_net(in_dev->dev);
1744
1745 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1746 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1747 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1748}
1749#else
1750static void ip_mc_reset(struct in_device *in_dev)
1751{
1752}
1753#endif
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755void ip_mc_init_dev(struct in_device *in_dev)
1756{
1757 ASSERT_RTNL();
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759#ifdef CONFIG_IP_MULTICAST
Kees Cooke99e88a2017-10-16 14:43:17 -07001760 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1761 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762#endif
Hangbin Liu966c37f2018-10-26 11:30:35 +08001763 ip_mc_reset(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 spin_lock_init(&in_dev->mc_tomb_lock);
1766}
1767
1768/* Device going up */
1769
1770void ip_mc_up(struct in_device *in_dev)
1771{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001772 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 ASSERT_RTNL();
1775
Hangbin Liu966c37f2018-10-26 11:30:35 +08001776 ip_mc_reset(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1778
Hangbin Liu24803f32016-11-14 16:16:28 +08001779 for_each_pmc_rtnl(in_dev, pmc) {
1780#ifdef CONFIG_IP_MULTICAST
1781 igmpv3_del_delrec(in_dev, pmc);
1782#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001783 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
1787/*
1788 * Device is about to be destroyed: clean up.
1789 */
1790
1791void ip_mc_destroy_dev(struct in_device *in_dev)
1792{
1793 struct ip_mc_list *i;
1794
1795 ASSERT_RTNL();
1796
1797 /* Deactivate timers */
1798 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001799#ifdef CONFIG_IP_MULTICAST
1800 igmpv3_clear_delrec(in_dev);
1801#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001803 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1804 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001805 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001810/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001811static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct net_device *dev = NULL;
1814 struct in_device *idev = NULL;
1815
1816 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001817 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 return idev;
1819 }
1820 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001821 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (!dev)
1823 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825
David S. Millerb23dd4f2011-03-02 14:31:35 -08001826 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001827 struct rtable *rt = ip_route_output(net,
1828 imr->imr_multiaddr.s_addr,
1829 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001830 if (!IS_ERR(rt)) {
1831 dev = rt->dst.dev;
1832 ip_rt_put(rt);
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
1835 if (dev) {
1836 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001837 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839 return idev;
1840}
1841
1842/*
1843 * Join a socket to a group
1844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001847 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 struct ip_sf_list *psf, *psf_prev;
1850 int rv = 0;
1851
1852 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001853 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (psf->sf_inaddr == *psfsrc)
1855 break;
1856 psf_prev = psf;
1857 }
1858 if (!psf || psf->sf_count[sfmode] == 0) {
1859 /* source filter not found, or count wrong => bug */
1860 return -ESRCH;
1861 }
1862 psf->sf_count[sfmode]--;
1863 if (psf->sf_count[sfmode] == 0) {
1864 ip_rt_multicast_event(pmc->interface);
1865 }
1866 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1867#ifdef CONFIG_IP_MULTICAST
1868 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001869 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870#endif
1871
1872 /* no more filters for this source */
1873 if (psf_prev)
1874 psf_prev->sf_next = psf->sf_next;
1875 else
1876 pmc->sources = psf->sf_next;
1877#ifdef CONFIG_IP_MULTICAST
1878 if (psf->sf_oldin &&
1879 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001880 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 psf->sf_next = pmc->tomb;
1882 pmc->tomb = psf;
1883 rv = 1;
1884 } else
1885#endif
1886 kfree(psf);
1887 }
1888 return rv;
1889}
1890
1891#ifndef CONFIG_IP_MULTICAST
1892#define igmp_ifc_event(x) do { } while (0)
1893#endif
1894
Al Viro8f935bb2006-09-27 18:30:07 -07001895static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1896 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 struct ip_mc_list *pmc;
1899 int changerec = 0;
1900 int i, err;
1901
1902 if (!in_dev)
1903 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001904 rcu_read_lock();
1905 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (*pmca == pmc->multiaddr)
1907 break;
1908 }
1909 if (!pmc) {
1910 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001911 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return -ESRCH;
1913 }
1914 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001915 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916#ifdef CONFIG_IP_MULTICAST
1917 sf_markstate(pmc);
1918#endif
1919 if (!delta) {
1920 err = -EINVAL;
1921 if (!pmc->sfcount[sfmode])
1922 goto out_unlock;
1923 pmc->sfcount[sfmode]--;
1924 }
1925 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001926 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1928
1929 changerec |= rv > 0;
1930 if (!err && rv < 0)
1931 err = rv;
1932 }
1933 if (pmc->sfmode == MCAST_EXCLUDE &&
1934 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1935 pmc->sfcount[MCAST_INCLUDE]) {
1936#ifdef CONFIG_IP_MULTICAST
1937 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001938 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939#endif
1940
1941 /* filter mode change */
1942 pmc->sfmode = MCAST_INCLUDE;
1943#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001944 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001946 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 psf->sf_crcount = 0;
1948 igmp_ifc_event(pmc->interface);
1949 } else if (sf_setstate(pmc) || changerec) {
1950 igmp_ifc_event(pmc->interface);
1951#endif
1952 }
1953out_unlock:
1954 spin_unlock_bh(&pmc->lock);
1955 return err;
1956}
1957
1958/*
1959 * Add multicast single-source filter to the interface list
1960 */
1961static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001962 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 struct ip_sf_list *psf, *psf_prev;
1965
1966 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001967 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (psf->sf_inaddr == *psfsrc)
1969 break;
1970 psf_prev = psf;
1971 }
1972 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001973 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 if (!psf)
1975 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 psf->sf_inaddr = *psfsrc;
1977 if (psf_prev) {
1978 psf_prev->sf_next = psf;
1979 } else
1980 pmc->sources = psf;
1981 }
1982 psf->sf_count[sfmode]++;
1983 if (psf->sf_count[sfmode] == 1) {
1984 ip_rt_multicast_event(pmc->interface);
1985 }
1986 return 0;
1987}
1988
1989#ifdef CONFIG_IP_MULTICAST
1990static void sf_markstate(struct ip_mc_list *pmc)
1991{
1992 struct ip_sf_list *psf;
1993 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1994
Weilong Chenc71151f2013-12-23 14:37:29 +08001995 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (pmc->sfcount[MCAST_EXCLUDE]) {
1997 psf->sf_oldin = mca_xcount ==
1998 psf->sf_count[MCAST_EXCLUDE] &&
1999 !psf->sf_count[MCAST_INCLUDE];
2000 } else
2001 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2002}
2003
2004static int sf_setstate(struct ip_mc_list *pmc)
2005{
David L Stevensad125832006-01-18 14:20:56 -08002006 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2008 int qrv = pmc->interface->mr_qrv;
2009 int new_in, rv;
2010
2011 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002012 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (pmc->sfcount[MCAST_EXCLUDE]) {
2014 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2015 !psf->sf_count[MCAST_INCLUDE];
2016 } else
2017 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08002018 if (new_in) {
2019 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05002020 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08002021
Weilong Chenc71151f2013-12-23 14:37:29 +08002022 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08002023 if (dpsf->sf_inaddr == psf->sf_inaddr)
2024 break;
2025 prev = dpsf;
2026 }
2027 if (dpsf) {
2028 if (prev)
2029 prev->sf_next = dpsf->sf_next;
2030 else
2031 pmc->tomb = dpsf->sf_next;
2032 kfree(dpsf);
2033 }
2034 psf->sf_crcount = qrv;
2035 rv++;
2036 }
2037 } else if (psf->sf_oldin) {
2038
2039 psf->sf_crcount = 0;
2040 /*
2041 * add or update "delete" records if an active filter
2042 * is now inactive
2043 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002044 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08002045 if (dpsf->sf_inaddr == psf->sf_inaddr)
2046 break;
2047 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00002048 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08002049 if (!dpsf)
2050 continue;
2051 *dpsf = *psf;
2052 /* pmc->lock held by callers */
2053 dpsf->sf_next = pmc->tomb;
2054 pmc->tomb = dpsf;
2055 }
2056 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 rv++;
2058 }
2059 }
2060 return rv;
2061}
2062#endif
2063
2064/*
2065 * Add multicast source filter list to the interface list
2066 */
Al Viro8f935bb2006-09-27 18:30:07 -07002067static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2068 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
2070 struct ip_mc_list *pmc;
2071 int isexclude;
2072 int i, err;
2073
2074 if (!in_dev)
2075 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002076 rcu_read_lock();
2077 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (*pmca == pmc->multiaddr)
2079 break;
2080 }
2081 if (!pmc) {
2082 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002083 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return -ESRCH;
2085 }
2086 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002087 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089#ifdef CONFIG_IP_MULTICAST
2090 sf_markstate(pmc);
2091#endif
2092 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2093 if (!delta)
2094 pmc->sfcount[sfmode]++;
2095 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002096 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002097 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (err)
2099 break;
2100 }
2101 if (err) {
2102 int j;
2103
Jun Zhao685f94e62011-11-22 17:19:03 +00002104 if (!delta)
2105 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002106 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002107 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2109#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002111 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002112 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#endif
2114
2115 /* filter mode change */
2116 if (pmc->sfcount[MCAST_EXCLUDE])
2117 pmc->sfmode = MCAST_EXCLUDE;
2118 else if (pmc->sfcount[MCAST_INCLUDE])
2119 pmc->sfmode = MCAST_INCLUDE;
2120#ifdef CONFIG_IP_MULTICAST
2121 /* else no filters; keep old mode for reports */
2122
Nikolay Borisov165094a2016-02-08 23:29:24 +02002123 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002125 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 psf->sf_crcount = 0;
2127 igmp_ifc_event(in_dev);
2128 } else if (sf_setstate(pmc)) {
2129 igmp_ifc_event(in_dev);
2130#endif
2131 }
2132 spin_unlock_bh(&pmc->lock);
2133 return err;
2134}
2135
2136static void ip_mc_clear_src(struct ip_mc_list *pmc)
2137{
Eric Dumazet3580d042019-05-22 16:51:22 -07002138 struct ip_sf_list *tomb, *sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
WANG Congc38b7d32017-06-12 09:52:26 -07002140 spin_lock_bh(&pmc->lock);
2141 tomb = pmc->tomb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 pmc->tomb = NULL;
WANG Congc38b7d32017-06-12 09:52:26 -07002143 sources = pmc->sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 pmc->sources = NULL;
2145 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002146 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 pmc->sfcount[MCAST_EXCLUDE] = 1;
WANG Congc38b7d32017-06-12 09:52:26 -07002148 spin_unlock_bh(&pmc->lock);
2149
Eric Dumazet3580d042019-05-22 16:51:22 -07002150 ip_sf_list_clear_all(tomb);
2151 ip_sf_list_clear_all(sources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002154/* Join a multicast group
2155 */
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002156static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2157 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Al Viro8f935bb2006-09-27 18:30:07 -07002159 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002160 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 struct in_device *in_dev;
2162 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002163 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002164 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002166 int err;
2167
2168 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Joe Perchesf97c1e02007-12-16 13:45:43 -08002170 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return -EINVAL;
2172
Daniel Lezcano877aced2008-08-13 16:15:57 -07002173 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 err = -ENODEV;
2177 goto done;
2178 }
2179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002181 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002182 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002183 if (i->multi.imr_multiaddr.s_addr == addr &&
2184 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 count++;
2187 }
2188 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002189 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002191 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002192 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002193 goto done;
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002196 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 iml->sflist = NULL;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002198 iml->sfmode = mode;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002199 rcu_assign_pointer(inet->mc_list, iml);
Li RongQinga1c4cd62019-08-20 13:52:47 +08002200 ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return err;
2204}
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002205
2206/* Join ASM (Any-Source Multicast) group
2207 */
2208int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2209{
2210 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2211}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002212EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002214/* Join SSM (Source-Specific Multicast) group
2215 */
2216int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2217 unsigned int mode)
2218{
2219 return __ip_mc_join_group(sk, imr, mode);
2220}
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2223 struct in_device *in_dev)
2224{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002225 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 int err;
2227
Ian Morris51456b22015-04-03 09:17:26 +01002228 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /* any-source empty exclude case */
2230 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2231 iml->sfmode, 0, NULL, 0);
2232 }
2233 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002234 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002235 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002236 /* decrease mem now to avoid the memleak warning */
2237 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002238 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return err;
2240}
2241
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002242int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
2244 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002245 struct ip_mc_socklist *iml;
2246 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002247 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002248 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002249 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002250 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002251 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Eric Dumazet959d10f2015-02-17 03:19:24 -08002253 ASSERT_RTNL();
2254
Daniel Lezcano877aced2008-08-13 16:15:57 -07002255 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002256 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002257 ret = -ENODEV;
2258 goto out;
2259 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002260 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002261 for (imlp = &inet->mc_list;
2262 (iml = rtnl_dereference(*imlp)) != NULL;
2263 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002264 if (iml->multi.imr_multiaddr.s_addr != group)
2265 continue;
2266 if (ifindex) {
2267 if (iml->multi.imr_ifindex != ifindex)
2268 continue;
2269 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2270 iml->multi.imr_address.s_addr)
2271 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
David L Stevensacd6e002006-08-17 16:27:39 -07002273 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002275 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002276
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002277 if (in_dev)
2278 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002279
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002280 /* decrease mem now to avoid the memleak warning */
2281 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002282 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002283 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
dingtianhong52ad3532014-07-02 13:50:48 +08002285out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002286 return ret;
2287}
stephen hemminger193ba922012-10-01 12:32:34 +00002288EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290int ip_mc_source(int add, int omode, struct sock *sk, struct
2291 ip_mreq_source *mreqs, int ifindex)
2292{
2293 int err;
2294 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002295 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 struct ip_mc_socklist *pmc;
2297 struct in_device *in_dev = NULL;
2298 struct inet_sock *inet = inet_sk(sk);
2299 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002300 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002301 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 int i, j, rv;
2303
Joe Perchesf97c1e02007-12-16 13:45:43 -08002304 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return -EINVAL;
2306
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002307 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2310 imr.imr_address.s_addr = mreqs->imr_interface;
2311 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002312 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 if (!in_dev) {
2315 err = -ENODEV;
2316 goto done;
2317 }
2318 err = -EADDRNOTAVAIL;
2319
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002320 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002321 if ((pmc->multi.imr_multiaddr.s_addr ==
2322 imr.imr_multiaddr.s_addr) &&
2323 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 break;
2325 }
David L Stevens917f2f12005-07-08 17:45:16 -07002326 if (!pmc) { /* must have a prior join */
2327 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /* if a source filter was set, must be the same mode as before */
2331 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002332 if (pmc->sfmode != omode) {
2333 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 } else if (pmc->sfmode != omode) {
2337 /* allow mode switches for empty-set filters */
2338 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002339 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 NULL, 0);
2341 pmc->sfmode = omode;
2342 }
2343
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002344 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (!add) {
2346 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002347 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002349 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002351 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (rv == 0)
2353 break;
2354 }
2355 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002356 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
David L Stevens8cdaaa12005-07-08 17:39:23 -07002358 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2359 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2360 leavegroup = 1;
2361 goto done;
2362 }
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002365 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 &mreqs->imr_sourceaddr, 1);
2367
Weilong Chenc71151f2013-12-23 14:37:29 +08002368 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 psl->sl_addr[j-1] = psl->sl_addr[j];
2370 psl->sl_count--;
2371 err = 0;
2372 goto done;
2373 }
2374 /* else, add a new source to the filter */
2375
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002376 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 err = -ENOBUFS;
2378 goto done;
2379 }
2380 if (!psl || psl->sl_count == psl->sl_max) {
2381 struct ip_sf_socklist *newpsl;
2382 int count = IP_SFBLOCK;
2383
2384 if (psl)
2385 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002386 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (!newpsl) {
2388 err = -ENOBUFS;
2389 goto done;
2390 }
2391 newpsl->sl_max = count;
2392 newpsl->sl_count = count - IP_SFBLOCK;
2393 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002394 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002396 /* decrease mem now to avoid the memleak warning */
2397 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002398 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002400 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002401 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002404 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002406 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (rv == 0)
2408 break;
2409 }
2410 if (rv == 0) /* address already there is an error */
2411 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002412 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 psl->sl_addr[j+1] = psl->sl_addr[j];
2414 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2415 psl->sl_count++;
2416 err = 0;
2417 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002418 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 &mreqs->imr_sourceaddr, 1);
2420done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002421 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002422 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return err;
2424}
2425
2426int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2427{
David L Stevens9951f032005-07-08 17:47:28 -07002428 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002430 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 struct ip_mc_socklist *pmc;
2432 struct in_device *in_dev;
2433 struct inet_sock *inet = inet_sk(sk);
2434 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002435 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002436 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Joe Perchesf97c1e02007-12-16 13:45:43 -08002438 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return -EINVAL;
2440 if (msf->imsf_fmode != MCAST_INCLUDE &&
2441 msf->imsf_fmode != MCAST_EXCLUDE)
2442 return -EINVAL;
2443
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002444 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
2446 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2447 imr.imr_address.s_addr = msf->imsf_interface;
2448 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002449 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
2451 if (!in_dev) {
2452 err = -ENODEV;
2453 goto done;
2454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
David L Stevens9951f032005-07-08 17:47:28 -07002456 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2457 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2458 leavegroup = 1;
2459 goto done;
2460 }
2461
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002462 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2464 pmc->multi.imr_ifindex == imr.imr_ifindex)
2465 break;
2466 }
David L Stevens917f2f12005-07-08 17:45:16 -07002467 if (!pmc) { /* must have a prior join */
2468 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002472 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2473 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (!newpsl) {
2475 err = -ENOBUFS;
2476 goto done;
2477 }
2478 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2479 memcpy(newpsl->sl_addr, msf->imsf_slist,
2480 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2481 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2482 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2483 if (err) {
2484 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2485 goto done;
2486 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002487 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002489 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2490 msf->imsf_fmode, 0, NULL, 0);
2491 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002492 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (psl) {
2494 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2495 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002496 /* decrease mem now to avoid the memleak warning */
2497 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002498 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 } else
2500 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2501 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002502 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002504 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505done:
David L Stevens9951f032005-07-08 17:47:28 -07002506 if (leavegroup)
2507 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 return err;
2509}
2510
2511int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2512 struct ip_msfilter __user *optval, int __user *optlen)
2513{
2514 int err, len, count, copycount;
2515 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002516 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 struct ip_mc_socklist *pmc;
2518 struct in_device *in_dev;
2519 struct inet_sock *inet = inet_sk(sk);
2520 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002521 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
WANG Cong87e9f032015-11-03 15:41:16 -08002523 ASSERT_RTNL();
2524
Joe Perchesf97c1e02007-12-16 13:45:43 -08002525 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 return -EINVAL;
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2529 imr.imr_address.s_addr = msf->imsf_interface;
2530 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002531 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
2533 if (!in_dev) {
2534 err = -ENODEV;
2535 goto done;
2536 }
2537 err = -EADDRNOTAVAIL;
2538
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002539 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2541 pmc->multi.imr_ifindex == imr.imr_ifindex)
2542 break;
2543 }
2544 if (!pmc) /* must have a prior join */
2545 goto done;
2546 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002547 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (!psl) {
2549 len = 0;
2550 count = 0;
2551 } else {
2552 count = psl->sl_count;
2553 }
2554 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2555 len = copycount * sizeof(psl->sl_addr[0]);
2556 msf->imsf_numsrc = count;
2557 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2558 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2559 return -EFAULT;
2560 }
2561 if (len &&
2562 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2563 return -EFAULT;
2564 return 0;
2565done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return err;
2567}
2568
2569int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2570 struct group_filter __user *optval, int __user *optlen)
2571{
2572 int err, i, count, copycount;
2573 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002574 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 struct ip_mc_socklist *pmc;
2576 struct inet_sock *inet = inet_sk(sk);
2577 struct ip_sf_socklist *psl;
2578
WANG Cong87e9f032015-11-03 15:41:16 -08002579 ASSERT_RTNL();
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 psin = (struct sockaddr_in *)&gsf->gf_group;
2582 if (psin->sin_family != AF_INET)
2583 return -EINVAL;
2584 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002585 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return -EINVAL;
2587
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 err = -EADDRNOTAVAIL;
2589
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002590 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2592 pmc->multi.imr_ifindex == gsf->gf_interface)
2593 break;
2594 }
2595 if (!pmc) /* must have a prior join */
2596 goto done;
2597 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002598 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 count = psl ? psl->sl_count : 0;
2600 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2601 gsf->gf_numsrc = count;
2602 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2603 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2604 return -EFAULT;
2605 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002606 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 struct sockaddr_storage ss;
2608
2609 psin = (struct sockaddr_in *)&ss;
2610 memset(&ss, 0, sizeof(ss));
2611 psin->sin_family = AF_INET;
2612 psin->sin_addr.s_addr = psl->sl_addr[i];
2613 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2614 return -EFAULT;
2615 }
2616 return 0;
2617done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return err;
2619}
2620
2621/*
2622 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2623 */
David Ahern60d9b032017-08-07 08:44:19 -07002624int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2625 int dif, int sdif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
2627 struct inet_sock *inet = inet_sk(sk);
2628 struct ip_mc_socklist *pmc;
2629 struct ip_sf_socklist *psl;
2630 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002631 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002633 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002634 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002635 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002637 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002638 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
David Ahern60d9b032017-08-07 08:44:19 -07002640 (pmc->multi.imr_ifindex == dif ||
2641 (sdif && pmc->multi.imr_ifindex == sdif)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 break;
2643 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002644 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002646 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002647 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002648 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002650 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Weilong Chenc71151f2013-12-23 14:37:29 +08002652 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (psl->sl_addr[i] == rmt_addr)
2654 break;
2655 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002656 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002658 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002660 goto unlock;
2661 ret = 1;
2662unlock:
2663 rcu_read_unlock();
2664out:
2665 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
2668/*
2669 * A socket is closing.
2670 */
2671
2672void ip_mc_drop_socket(struct sock *sk)
2673{
2674 struct inet_sock *inet = inet_sk(sk);
2675 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002676 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Ian Morris51456b22015-04-03 09:17:26 +01002678 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return;
2680
2681 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002682 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002685 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002686 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002687 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002688 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002690 /* decrease mem now to avoid the memleak warning */
2691 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002692 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
2694 rtnl_unlock();
2695}
2696
David S. Millerdbdd9a52011-03-10 16:34:38 -08002697/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002698int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002701 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 struct ip_sf_list *psf;
2703 int rv = 0;
2704
Eric Dumazete9897072013-06-07 08:48:57 -07002705 mc_hash = rcu_dereference(in_dev->mc_hash);
2706 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002707 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002708
2709 for (im = rcu_dereference(mc_hash[hash]);
2710 im != NULL;
2711 im = rcu_dereference(im->next_hash)) {
2712 if (im->multiaddr == mc_addr)
2713 break;
2714 }
2715 } else {
2716 for_each_pmc_rcu(in_dev, im) {
2717 if (im->multiaddr == mc_addr)
2718 break;
2719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
2721 if (im && proto == IPPROTO_IGMP) {
2722 rv = 1;
2723 } else if (im) {
2724 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002725 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (psf->sf_inaddr == src_addr)
2727 break;
2728 }
2729 if (psf)
2730 rv = psf->sf_count[MCAST_INCLUDE] ||
2731 psf->sf_count[MCAST_EXCLUDE] !=
2732 im->sfcount[MCAST_EXCLUDE];
2733 else
2734 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2735 } else
2736 rv = 1; /* unspecified source; tentatively allow */
2737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return rv;
2739}
2740
2741#if defined(CONFIG_PROC_FS)
2742struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002743 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 struct net_device *dev;
2745 struct in_device *in_dev;
2746};
2747
2748#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2749
2750static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2751{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002752 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 struct ip_mc_list *im = NULL;
2754 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2755
Pavel Emelianov7562f872007-05-03 15:13:45 -07002756 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002757 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002759
2760 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (!in_dev)
2762 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002763 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (im) {
2765 state->in_dev = in_dev;
2766 break;
2767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
2769 return im;
2770}
2771
2772static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2773{
2774 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002775
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002776 im = rcu_dereference(im->next_rcu);
2777 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002778 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (!state->dev) {
2780 state->in_dev = NULL;
2781 break;
2782 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002783 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 if (!state->in_dev)
2785 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002786 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
2788 return im;
2789}
2790
2791static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2792{
2793 struct ip_mc_list *im = igmp_mc_get_first(seq);
2794 if (im)
2795 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2796 --pos;
2797 return pos ? NULL : im;
2798}
2799
2800static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002801 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
stephen hemminger61fbab72009-11-10 07:54:55 +00002803 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2805}
2806
2807static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2808{
2809 struct ip_mc_list *im;
2810 if (v == SEQ_START_TOKEN)
2811 im = igmp_mc_get_first(seq);
2812 else
2813 im = igmp_mc_get_next(seq, v);
2814 ++*pos;
2815 return im;
2816}
2817
2818static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002819 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002822
2823 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002825 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826}
2827
2828static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2829{
2830 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002831 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2833 else {
2834 struct ip_mc_list *im = (struct ip_mc_list *)v;
2835 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2836 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002837 long delta;
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839#ifdef CONFIG_IP_MULTICAST
2840 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2841 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2842 "V3";
2843#else
2844 querier = "NONE";
2845#endif
2846
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002847 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002849 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851
Eric Dumazeta399a802012-08-08 21:13:53 +00002852 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002854 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002856 im->tm_running,
2857 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 im->reporter);
2859 }
2860 return 0;
2861}
2862
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002863static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 .start = igmp_mc_seq_start,
2865 .next = igmp_mc_seq_next,
2866 .stop = igmp_mc_seq_stop,
2867 .show = igmp_mc_seq_show,
2868};
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002871 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 struct net_device *dev;
2873 struct in_device *idev;
2874 struct ip_mc_list *im;
2875};
2876
2877#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2878
2879static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2880{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002881 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 struct ip_sf_list *psf = NULL;
2883 struct ip_mc_list *im = NULL;
2884 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2885
Pavel Emelianov7562f872007-05-03 15:13:45 -07002886 state->idev = NULL;
2887 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002888 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002890 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002891 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002893 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002894 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 spin_lock_bh(&im->lock);
2896 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002897 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 state->im = im;
2899 state->idev = idev;
2900 break;
2901 }
2902 spin_unlock_bh(&im->lock);
2903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905 return psf;
2906}
2907
2908static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2909{
2910 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2911
2912 psf = psf->sf_next;
2913 while (!psf) {
2914 spin_unlock_bh(&state->im->lock);
2915 state->im = state->im->next;
2916 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002917 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 if (!state->dev) {
2919 state->idev = NULL;
2920 goto out;
2921 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002922 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (!state->idev)
2924 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002925 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927 if (!state->im)
2928 break;
2929 spin_lock_bh(&state->im->lock);
2930 psf = state->im->sources;
2931 }
2932out:
2933 return psf;
2934}
2935
2936static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2937{
2938 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2939 if (psf)
2940 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2941 --pos;
2942 return pos ? NULL : psf;
2943}
2944
2945static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002946 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
stephen hemminger61fbab72009-11-10 07:54:55 +00002948 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2950}
2951
2952static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2953{
2954 struct ip_sf_list *psf;
2955 if (v == SEQ_START_TOKEN)
2956 psf = igmp_mcf_get_first(seq);
2957 else
2958 psf = igmp_mcf_get_next(seq, v);
2959 ++*pos;
2960 return psf;
2961}
2962
2963static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002964 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002967 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 spin_unlock_bh(&state->im->lock);
2969 state->im = NULL;
2970 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002971 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002973 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
2976static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2977{
2978 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2979 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2980
2981 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002982 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 } else {
2984 seq_printf(seq,
2985 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002986 "0x%08x %6lu %6lu\n",
2987 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 ntohl(state->im->multiaddr),
2989 ntohl(psf->sf_inaddr),
2990 psf->sf_count[MCAST_INCLUDE],
2991 psf->sf_count[MCAST_EXCLUDE]);
2992 }
2993 return 0;
2994}
2995
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002996static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 .start = igmp_mcf_seq_start,
2998 .next = igmp_mcf_seq_next,
2999 .stop = igmp_mcf_seq_stop,
3000 .show = igmp_mcf_seq_show,
3001};
3002
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003003static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003004{
3005 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08003006 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003007
Christoph Hellwigc3506372018-04-10 19:42:55 +02003008 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3009 sizeof(struct igmp_mc_iter_state));
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003010 if (!pde)
3011 goto out_igmp;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003012 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3013 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003014 if (!pde)
3015 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08003016 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3017 SOCK_DGRAM, 0, net);
3018 if (err < 0) {
3019 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3020 err);
3021 goto out_sock;
3022 }
3023
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003024 return 0;
3025
Madhu Challa93a714d2015-02-25 09:58:35 -08003026out_sock:
3027 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003028out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00003029 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003030out_igmp:
3031 return -ENOMEM;
3032}
3033
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003034static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003035{
Gao fengece31ff2013-02-18 01:34:56 +00003036 remove_proc_entry("mcfilter", net->proc_net);
3037 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08003038 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003039}
3040
3041static struct pernet_operations igmp_net_ops = {
3042 .init = igmp_net_init,
3043 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044};
WANG Cong72c1d3b2014-01-10 16:09:45 -08003045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003047static int igmp_netdev_event(struct notifier_block *this,
3048 unsigned long event, void *ptr)
3049{
3050 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3051 struct in_device *in_dev;
3052
3053 switch (event) {
3054 case NETDEV_RESEND_IGMP:
3055 in_dev = __in_dev_get_rtnl(dev);
3056 if (in_dev)
3057 ip_mc_rejoin_groups(in_dev);
3058 break;
3059 default:
3060 break;
3061 }
3062 return NOTIFY_DONE;
3063}
3064
3065static struct notifier_block igmp_notifier = {
3066 .notifier_call = igmp_netdev_event,
3067};
3068
WANG Cong72c1d3b2014-01-10 16:09:45 -08003069int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003071#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003072 int err;
3073
3074 err = register_pernet_subsys(&igmp_net_ops);
3075 if (err)
3076 return err;
3077 err = register_netdevice_notifier(&igmp_notifier);
3078 if (err)
3079 goto reg_notif_fail;
3080 return 0;
3081
3082reg_notif_fail:
3083 unregister_pernet_subsys(&igmp_net_ops);
3084 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003085#else
3086 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003088}