blob: 03589a04f9aaeb5f3c2571bd4cdb96ffd13062bb [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
Hangbin Liu966c37f2018-10-26 11:30:35 +0800110#define IGMP_QUERY_INTERVAL (125*HZ)
Fabian Frederick436f7c22014-11-04 20:52:14 +0100111#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Fabian Frederick436f7c22014-11-04 20:52:14 +0100113#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Fabian Frederick436f7c22014-11-04 20:52:14 +0100115/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * IGMP specs require to report membership immediately after
117 * joining a group, but we delay the first report by a
118 * small interval. It seems more natural and still does not
119 * contradict to specs provided this delay is small enough.
120 */
121
Herbert Xu42f811b2007-06-04 23:34:44 -0700122#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900123 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700124 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
125 ((in_dev)->mr_v1_seen && \
126 time_before(jiffies, (in_dev)->mr_v1_seen)))
127#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900128 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700129 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
130 ((in_dev)->mr_v2_seen && \
131 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
William Manleycab70042013-08-06 19:03:13 +0100133static int unsolicited_report_interval(struct in_device *in_dev)
134{
William Manley26900482013-08-06 19:03:15 +0100135 int interval_ms, interval_jiffies;
136
William Manleycab70042013-08-06 19:03:13 +0100137 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100138 interval_ms = IN_DEV_CONF_GET(
139 in_dev,
140 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100141 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100142 interval_ms = IN_DEV_CONF_GET(
143 in_dev,
144 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
145
146 interval_jiffies = msecs_to_jiffies(interval_ms);
147
148 /* _timer functions can't handle a delay of 0 jiffies so ensure
149 * we always return a positive value.
150 */
151 if (interval_jiffies <= 0)
152 interval_jiffies = 1;
153 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100154}
155
Florian Fainelli9fb20802019-02-01 20:20:52 -0800156static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
157 gfp_t gfp);
Hangbin Liu24803f32016-11-14 16:16:28 +0800158static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void igmpv3_clear_delrec(struct in_device *in_dev);
160static int sf_setstate(struct ip_mc_list *pmc);
161static void sf_markstate(struct ip_mc_list *pmc);
162#endif
163static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700164static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
165 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static void ip_ma_put(struct ip_mc_list *im)
168{
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300169 if (refcount_dec_and_test(&im->refcnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800171 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173}
174
David S. Millerd9aa9382010-11-15 08:52:02 -0800175#define for_each_pmc_rcu(in_dev, pmc) \
176 for (pmc = rcu_dereference(in_dev->mc_list); \
177 pmc != NULL; \
178 pmc = rcu_dereference(pmc->next_rcu))
179
180#define for_each_pmc_rtnl(in_dev, pmc) \
181 for (pmc = rtnl_dereference(in_dev->mc_list); \
182 pmc != NULL; \
183 pmc = rtnl_dereference(pmc->next_rcu))
184
Eric Dumazet903869b2019-05-22 18:35:16 -0700185static void ip_sf_list_clear_all(struct ip_sf_list *psf)
186{
187 struct ip_sf_list *next;
188
189 while (psf) {
190 next = psf->sf_next;
191 kfree(psf);
192 psf = next;
193 }
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196#ifdef CONFIG_IP_MULTICAST
197
198/*
199 * Timer management
200 */
201
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000202static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 spin_lock_bh(&im->lock);
205 if (del_timer(&im->timer))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300206 refcount_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800207 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 im->reporter = 0;
209 im->unsolicit_count = 0;
210 spin_unlock_bh(&im->lock);
211}
212
213/* It must be called with locked im->lock */
214static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
215{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500216 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800218 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (!mod_timer(&im->timer, jiffies+tv+2))
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300220 refcount_inc(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
223static void igmp_gq_start_timer(struct in_device *in_dev)
224{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500225 int tv = prandom_u32() % in_dev->mr_maxdelay;
Michal Tesar7ababb72017-01-02 14:38:36 +0100226 unsigned long exp = jiffies + tv + 2;
227
228 if (in_dev->mr_gq_running &&
229 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
230 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 in_dev->mr_gq_running = 1;
Michal Tesar7ababb72017-01-02 14:38:36 +0100233 if (!mod_timer(&in_dev->mr_gq_timer, exp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 in_dev_hold(in_dev);
235}
236
237static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
238{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500239 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
242 in_dev_hold(in_dev);
243}
244
245static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
246{
247 spin_lock_bh(&im->lock);
248 im->unsolicit_count = 0;
249 if (del_timer(&im->timer)) {
250 if ((long)(im->timer.expires-jiffies) < max_delay) {
251 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800252 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 spin_unlock_bh(&im->lock);
254 return;
255 }
Reshetova, Elena8851ab52017-06-30 13:08:02 +0300256 refcount_dec(&im->refcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 igmp_start_timer(im, max_delay);
259 spin_unlock_bh(&im->lock);
260}
261
262
263/*
264 * Send an IGMP report.
265 */
266
267#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
268
269
270static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
271 int gdeleted, int sdeleted)
272{
273 switch (type) {
274 case IGMPV3_MODE_IS_INCLUDE:
275 case IGMPV3_MODE_IS_EXCLUDE:
276 if (gdeleted || sdeleted)
277 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800278 if (!(pmc->gsquery && !psf->sf_gsresp)) {
279 if (pmc->sfmode == MCAST_INCLUDE)
280 return 1;
281 /* don't include if this source is excluded
282 * in all filters
283 */
284 if (psf->sf_count[MCAST_INCLUDE])
285 return type == IGMPV3_MODE_IS_INCLUDE;
286 return pmc->sfcount[MCAST_EXCLUDE] ==
287 psf->sf_count[MCAST_EXCLUDE];
288 }
289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 case IGMPV3_CHANGE_TO_INCLUDE:
291 if (gdeleted || sdeleted)
292 return 0;
293 return psf->sf_count[MCAST_INCLUDE] != 0;
294 case IGMPV3_CHANGE_TO_EXCLUDE:
295 if (gdeleted || sdeleted)
296 return 0;
297 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
298 psf->sf_count[MCAST_INCLUDE])
299 return 0;
300 return pmc->sfcount[MCAST_EXCLUDE] ==
301 psf->sf_count[MCAST_EXCLUDE];
302 case IGMPV3_ALLOW_NEW_SOURCES:
303 if (gdeleted || !psf->sf_crcount)
304 return 0;
305 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
306 case IGMPV3_BLOCK_OLD_SOURCES:
307 if (pmc->sfmode == MCAST_INCLUDE)
308 return gdeleted || (psf->sf_crcount && sdeleted);
309 return psf->sf_crcount && !gdeleted && !sdeleted;
310 }
311 return 0;
312}
313
314static int
315igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
316{
317 struct ip_sf_list *psf;
318 int scount = 0;
319
Weilong Chenc71151f2013-12-23 14:37:29 +0800320 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
322 continue;
323 scount++;
324 }
325 return scount;
326}
327
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800328/* source address selection per RFC 3376 section 4.2.13 */
329static __be32 igmpv3_get_srcaddr(struct net_device *dev,
330 const struct flowi4 *fl4)
331{
332 struct in_device *in_dev = __in_dev_get_rcu(dev);
Florian Westphalcd5a4112019-05-31 18:27:07 +0200333 const struct in_ifaddr *ifa;
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800334
335 if (!in_dev)
336 return htonl(INADDR_ANY);
337
Florian Westphalcd5a4112019-05-31 18:27:07 +0200338 in_dev_for_each_ifa_rcu(ifa, in_dev) {
Felix Fietkauad23b752018-01-19 11:50:46 +0100339 if (fl4->saddr == ifa->ifa_local)
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800340 return fl4->saddr;
Florian Westphalcd5a4112019-05-31 18:27:07 +0200341 }
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800342
343 return htonl(INADDR_ANY);
344}
345
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100346static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 struct sk_buff *skb;
349 struct rtable *rt;
350 struct iphdr *pip;
351 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700352 struct net *net = dev_net(dev);
David S. Miller31e4543d2011-05-03 20:25:42 -0700353 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000354 int hlen = LL_RESERVED_SPACE(dev);
355 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100356 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000358 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000359 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000360 GFP_ATOMIC | __GFP_NOWARN);
361 if (skb)
362 break;
363 size >>= 1;
364 if (size < 256)
365 return NULL;
366 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200367 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
David S. Miller31e4543d2011-05-03 20:25:42 -0700369 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500370 0, 0,
371 IPPROTO_IGMP, 0, dev->ifindex);
372 if (IS_ERR(rt)) {
373 kfree_skb(skb);
374 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Changli Gaod8d1f302010-06-10 23:31:35 -0700377 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 skb->dev = dev;
379
Herbert Xu66088242011-11-18 02:20:04 +0000380 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800381 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300383 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700384 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300385 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 pip->version = 4;
388 pip->ihl = (sizeof(struct iphdr)+4)>>2;
389 pip->tos = 0xc0;
390 pip->frag_off = htons(IP_DF);
391 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700392 pip->daddr = fl4.daddr;
Eric Dumazete7aadb22018-02-01 10:26:57 -0800393
394 rcu_read_lock();
Kevin Cernekeea46182b2017-12-11 11:13:45 -0800395 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
Eric Dumazete7aadb22018-02-01 10:26:57 -0800396 rcu_read_unlock();
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 pip->protocol = IPPROTO_IGMP;
399 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100400 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000401 ((u8 *)&pip[1])[0] = IPOPT_RA;
402 ((u8 *)&pip[1])[1] = 4;
403 ((u8 *)&pip[1])[2] = 0;
404 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700406 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300407 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300408 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
410 pig->resv1 = 0;
411 pig->csum = 0;
412 pig->resv2 = 0;
413 pig->ngrec = 0;
414 return skb;
415}
416
417static int igmpv3_sendpack(struct sk_buff *skb)
418{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300419 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000420 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300422 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Eric W. Biederman33224b12015-10-07 16:48:46 -0500424 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
428{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800429 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
432static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
Eric Dumazetb5476022017-12-11 07:17:39 -0800433 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct net_device *dev = pmc->interface->dev;
436 struct igmpv3_report *pih;
437 struct igmpv3_grec *pgr;
438
Eric Dumazetb5476022017-12-11 07:17:39 -0800439 if (!skb) {
440 skb = igmpv3_newpack(dev, mtu);
441 if (!skb)
442 return NULL;
443 }
Johannes Berg4df864c2017-06-16 14:29:21 +0200444 pgr = skb_put(skb, sizeof(struct igmpv3_grec));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 pgr->grec_type = type;
446 pgr->grec_auxwords = 0;
447 pgr->grec_nsrcs = 0;
448 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300449 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 pih->ngrec = htons(ntohs(pih->ngrec)+1);
451 *ppgr = pgr;
452 return skb;
453}
454
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100455#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
458 int type, int gdeleted, int sdeleted)
459{
460 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200461 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct igmpv3_report *pih;
463 struct igmpv3_grec *pgr = NULL;
464 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800465 int scount, stotal, first, isquery, truncate;
Eric Dumazetb5476022017-12-11 07:17:39 -0800466 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 if (pmc->multiaddr == IGMP_ALL_HOSTS)
469 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200470 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100471 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Eric Dumazetb5476022017-12-11 07:17:39 -0800473 mtu = READ_ONCE(dev->mtu);
474 if (mtu < IPV4_MIN_MTU)
475 return skb;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
478 type == IGMPV3_MODE_IS_EXCLUDE;
479 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
480 type == IGMPV3_CHANGE_TO_EXCLUDE;
481
David L Stevensad125832006-01-18 14:20:56 -0800482 stotal = scount = 0;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
485
David L Stevensad125832006-01-18 14:20:56 -0800486 if (!*psf_list)
487 goto empty_source;
488
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300489 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* EX and TO_EX get a fresh packet, if needed */
492 if (truncate) {
493 if (pih && pih->ngrec &&
494 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
495 if (skb)
496 igmpv3_sendpack(skb);
Eric Dumazetb5476022017-12-11 07:17:39 -0800497 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499 }
500 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800502 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700503 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 psf_next = psf->sf_next;
506
507 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
508 psf_prev = psf;
509 continue;
510 }
511
Hangbin Liua0525172016-08-02 18:02:57 +0800512 /* Based on RFC3376 5.1. Should not send source-list change
513 * records when there is a filter mode change.
514 */
515 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
516 (!gdeleted && pmc->crcount)) &&
517 (type == IGMPV3_ALLOW_NEW_SOURCES ||
518 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
519 goto decrease_sf_crcount;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* clear marks on query responses */
522 if (isquery)
523 psf->sf_gsresp = 0;
524
Al Viro63007722006-09-27 18:31:32 -0700525 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 first*sizeof(struct igmpv3_grec)) {
527 if (truncate && !first)
528 break; /* truncate these */
529 if (pgr)
530 pgr->grec_nsrcs = htons(scount);
531 if (skb)
532 igmpv3_sendpack(skb);
Eric Dumazetb5476022017-12-11 07:17:39 -0800533 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 first = 1;
535 scount = 0;
536 }
537 if (first) {
Eric Dumazetb5476022017-12-11 07:17:39 -0800538 skb = add_grhead(skb, pmc, type, &pgr, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 first = 0;
540 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800541 if (!skb)
542 return NULL;
Johannes Berg4df864c2017-06-16 14:29:21 +0200543 psrc = skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800545 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
547 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
Hangbin Liua0525172016-08-02 18:02:57 +0800548decrease_sf_crcount:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 psf->sf_crcount--;
550 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
551 if (psf_prev)
552 psf_prev->sf_next = psf->sf_next;
553 else
554 *psf_list = psf->sf_next;
555 kfree(psf);
556 continue;
557 }
558 }
559 psf_prev = psf;
560 }
David L Stevensad125832006-01-18 14:20:56 -0800561
562empty_source:
563 if (!stotal) {
564 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
565 type == IGMPV3_BLOCK_OLD_SOURCES)
566 return skb;
567 if (pmc->crcount || isquery) {
568 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800569 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800570 igmpv3_sendpack(skb);
571 skb = NULL; /* add_grhead will get a new one */
572 }
Eric Dumazetb5476022017-12-11 07:17:39 -0800573 skb = add_grhead(skb, pmc, type, &pgr, mtu);
David L Stevensad125832006-01-18 14:20:56 -0800574 }
575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 if (pgr)
577 pgr->grec_nsrcs = htons(scount);
578
579 if (isquery)
580 pmc->gsquery = 0; /* clear query state on report */
581 return skb;
582}
583
584static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
585{
586 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200587 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 int type;
589
590 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000591 rcu_read_lock();
592 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (pmc->multiaddr == IGMP_ALL_HOSTS)
594 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100595 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200596 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100597 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 spin_lock_bh(&pmc->lock);
599 if (pmc->sfcount[MCAST_EXCLUDE])
600 type = IGMPV3_MODE_IS_EXCLUDE;
601 else
602 type = IGMPV3_MODE_IS_INCLUDE;
603 skb = add_grec(skb, pmc, type, 0, 0);
604 spin_unlock_bh(&pmc->lock);
605 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000606 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 } else {
608 spin_lock_bh(&pmc->lock);
609 if (pmc->sfcount[MCAST_EXCLUDE])
610 type = IGMPV3_MODE_IS_EXCLUDE;
611 else
612 type = IGMPV3_MODE_IS_INCLUDE;
613 skb = add_grec(skb, pmc, type, 0, 0);
614 spin_unlock_bh(&pmc->lock);
615 }
616 if (!skb)
617 return 0;
618 return igmpv3_sendpack(skb);
619}
620
621/*
622 * remove zero-count source records from a source filter list
623 */
624static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
625{
626 struct ip_sf_list *psf_prev, *psf_next, *psf;
627
628 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800629 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 psf_next = psf->sf_next;
631 if (psf->sf_crcount == 0) {
632 if (psf_prev)
633 psf_prev->sf_next = psf->sf_next;
634 else
635 *ppsf = psf->sf_next;
636 kfree(psf);
637 } else
638 psf_prev = psf;
639 }
640}
641
Eric Dumazet3580d042019-05-22 16:51:22 -0700642static void kfree_pmc(struct ip_mc_list *pmc)
643{
644 ip_sf_list_clear_all(pmc->sources);
645 ip_sf_list_clear_all(pmc->tomb);
646 kfree(pmc);
647}
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static void igmpv3_send_cr(struct in_device *in_dev)
650{
651 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
652 struct sk_buff *skb = NULL;
653 int type, dtype;
654
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000655 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 spin_lock_bh(&in_dev->mc_tomb_lock);
657
658 /* deleted MCA's */
659 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800660 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 pmc_next = pmc->next;
662 if (pmc->sfmode == MCAST_INCLUDE) {
663 type = IGMPV3_BLOCK_OLD_SOURCES;
664 dtype = IGMPV3_BLOCK_OLD_SOURCES;
665 skb = add_grec(skb, pmc, type, 1, 0);
666 skb = add_grec(skb, pmc, dtype, 1, 1);
667 }
668 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (pmc->sfmode == MCAST_EXCLUDE) {
670 type = IGMPV3_CHANGE_TO_INCLUDE;
671 skb = add_grec(skb, pmc, type, 1, 0);
672 }
David L Stevensad125832006-01-18 14:20:56 -0800673 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (pmc->crcount == 0) {
675 igmpv3_clear_zeros(&pmc->tomb);
676 igmpv3_clear_zeros(&pmc->sources);
677 }
678 }
679 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
680 if (pmc_prev)
681 pmc_prev->next = pmc_next;
682 else
683 in_dev->mc_tomb = pmc_next;
684 in_dev_put(pmc->interface);
Eric Dumazet3580d042019-05-22 16:51:22 -0700685 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 } else
687 pmc_prev = pmc;
688 }
689 spin_unlock_bh(&in_dev->mc_tomb_lock);
690
691 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000692 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 spin_lock_bh(&pmc->lock);
694 if (pmc->sfcount[MCAST_EXCLUDE]) {
695 type = IGMPV3_BLOCK_OLD_SOURCES;
696 dtype = IGMPV3_ALLOW_NEW_SOURCES;
697 } else {
698 type = IGMPV3_ALLOW_NEW_SOURCES;
699 dtype = IGMPV3_BLOCK_OLD_SOURCES;
700 }
701 skb = add_grec(skb, pmc, type, 0, 0);
702 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
703
704 /* filter mode changes */
705 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (pmc->sfmode == MCAST_EXCLUDE)
707 type = IGMPV3_CHANGE_TO_EXCLUDE;
708 else
709 type = IGMPV3_CHANGE_TO_INCLUDE;
710 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800711 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 spin_unlock_bh(&pmc->lock);
714 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000715 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 if (!skb)
718 return;
719 (void) igmpv3_sendpack(skb);
720}
721
722static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
723 int type)
724{
725 struct sk_buff *skb;
726 struct iphdr *iph;
727 struct igmphdr *ih;
728 struct rtable *rt;
729 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700730 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700731 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e4543d2011-05-03 20:25:42 -0700732 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700733 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000734 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
737 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100738
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200739 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100740 return 0;
741
742 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 dst = IGMP_ALL_ROUTER;
744 else
745 dst = group;
746
David S. Miller31e4543d2011-05-03 20:25:42 -0700747 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500748 0, 0,
749 IPPROTO_IGMP, 0, dev->ifindex);
750 if (IS_ERR(rt))
751 return -1;
752
Herbert Xu66088242011-11-18 02:20:04 +0000753 hlen = LL_RESERVED_SPACE(dev);
754 tlen = dev->needed_tailroom;
755 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100756 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ip_rt_put(rt);
758 return -1;
759 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200760 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Changli Gaod8d1f302010-06-10 23:31:35 -0700762 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Herbert Xu66088242011-11-18 02:20:04 +0000764 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300766 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700767 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300768 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 iph->version = 4;
771 iph->ihl = (sizeof(struct iphdr)+4)>>2;
772 iph->tos = 0xc0;
773 iph->frag_off = htons(IP_DF);
774 iph->ttl = 1;
775 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700776 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100778 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000779 ((u8 *)&iph[1])[0] = IPOPT_RA;
780 ((u8 *)&iph[1])[1] = 4;
781 ((u8 *)&iph[1])[2] = 0;
782 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Johannes Berg4df864c2017-06-16 14:29:21 +0200784 ih = skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800785 ih->type = type;
786 ih->code = 0;
787 ih->csum = 0;
788 ih->group = group;
789 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Eric W. Biederman33224b12015-10-07 16:48:46 -0500791 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Kees Cooke99e88a2017-10-16 14:43:17 -0700794static void igmp_gq_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Kees Cooke99e88a2017-10-16 14:43:17 -0700796 struct in_device *in_dev = from_timer(in_dev, t, mr_gq_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 in_dev->mr_gq_running = 0;
799 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700800 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Kees Cooke99e88a2017-10-16 14:43:17 -0700803static void igmp_ifc_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Kees Cooke99e88a2017-10-16 14:43:17 -0700805 struct in_device *in_dev = from_timer(in_dev, t, mr_ifc_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 igmpv3_send_cr(in_dev);
Greg Kroah-Hartman83da0c02021-08-27 18:38:12 +0200808 if (in_dev->mr_ifc_count) {
809 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100810 igmp_ifc_start_timer(in_dev,
811 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700813 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
816static void igmp_ifc_event(struct in_device *in_dev)
817{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200818 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
820 return;
Greg Kroah-Hartman83da0c02021-08-27 18:38:12 +0200821 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 igmp_ifc_start_timer(in_dev, 1);
823}
824
825
Kees Cooke99e88a2017-10-16 14:43:17 -0700826static void igmp_timer_expire(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Kees Cooke99e88a2017-10-16 14:43:17 -0700828 struct ip_mc_list *im = from_timer(im, t, timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 struct in_device *in_dev = im->interface;
830
831 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800832 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Hangbin Liu4fb72532018-08-29 18:06:08 +0800834 if (im->unsolicit_count && --im->unsolicit_count)
William Manleycab70042013-08-06 19:03:13 +0100835 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Hangbin Liu4fb72532018-08-29 18:06:08 +0800836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 im->reporter = 1;
838 spin_unlock(&im->lock);
839
840 if (IGMP_V1_SEEN(in_dev))
841 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
842 else if (IGMP_V2_SEEN(in_dev))
843 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
844 else
845 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
846
847 ip_ma_put(im);
848}
849
David L Stevensad125832006-01-18 14:20:56 -0800850/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700851static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct ip_sf_list *psf;
854 int i, scount;
855
856 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800857 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (scount == nsrcs)
859 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800860 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800861 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000862 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800863 pmc->sfcount[MCAST_EXCLUDE] !=
864 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800865 break;
David L Stevensad125832006-01-18 14:20:56 -0800866 if (srcs[i] == psf->sf_inaddr) {
867 scount++;
868 break;
869 }
870 }
871 }
872 pmc->gsquery = 0;
873 if (scount == nsrcs) /* all sources excluded */
874 return 0;
875 return 1;
876}
877
Al Viroea4d9e72006-09-27 18:30:52 -0700878static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800879{
880 struct ip_sf_list *psf;
881 int i, scount;
882
883 if (pmc->sfmode == MCAST_EXCLUDE)
884 return igmp_xmarksources(pmc, nsrcs, srcs);
885
886 /* mark INCLUDE-mode sources */
887 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800888 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800889 if (scount == nsrcs)
890 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800891 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (srcs[i] == psf->sf_inaddr) {
893 psf->sf_gsresp = 1;
894 scount++;
895 break;
896 }
897 }
David L Stevensad125832006-01-18 14:20:56 -0800898 if (!scount) {
899 pmc->gsquery = 0;
900 return 0;
901 }
902 pmc->gsquery = 1;
903 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904}
905
Eric Dumazetd679c532012-09-06 20:37:06 +0000906/* return true if packet was dropped */
907static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200910 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* Timers are only set for non-local groups */
913
914 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000915 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200916 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100917 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000919 rcu_read_lock();
920 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (im->multiaddr == group) {
922 igmp_stop_timer(im);
923 break;
924 }
925 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000926 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000927 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Eric Dumazetd679c532012-09-06 20:37:06 +0000930/* return true if packet was dropped */
931static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 int len)
933{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300934 struct igmphdr *ih = igmp_hdr(skb);
935 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700937 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 int max_delay;
939 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200940 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942
David Stevens5b7c8402010-09-30 14:29:40 +0000943 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (ih->code == 0) {
945 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900946
Fabian Frederick436f7c22014-11-04 20:52:14 +0100947 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 in_dev->mr_v1_seen = jiffies +
Hangbin Liu966c37f2018-10-26 11:30:35 +0800949 (in_dev->mr_qrv * in_dev->mr_qi) +
950 in_dev->mr_qri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 group = 0;
952 } else {
953 /* v2 router present */
954 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
955 in_dev->mr_v2_seen = jiffies +
Hangbin Liu966c37f2018-10-26 11:30:35 +0800956 (in_dev->mr_qrv * in_dev->mr_qi) +
957 in_dev->mr_qri;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959 /* cancel the interface change timer */
Greg Kroah-Hartman83da0c02021-08-27 18:38:12 +0200960 in_dev->mr_ifc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (del_timer(&in_dev->mr_ifc_timer))
962 __in_dev_put(in_dev);
963 /* clear deleted report items */
964 igmpv3_clear_delrec(in_dev);
965 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000966 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000967 } else if (IGMP_V1_SEEN(in_dev)) {
968 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100969 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000970 group = 0;
971 } else if (IGMP_V2_SEEN(in_dev)) {
972 /* this is a v3 query with v2 queriers present;
973 * Interpretation of the max_delay code is problematic here.
974 * A real v2 host would use ih_code directly, while v3 has a
975 * different encoding. We use the v3 encoding as more likely
976 * to be intended in a v3 query.
977 */
978 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800979 if (!max_delay)
980 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 } else { /* v3 */
982 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000983 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900984
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300985 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900987 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700988 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000989 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300990 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
992
993 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
994 if (!max_delay)
995 max_delay = 1; /* can't mod w/ 0 */
996 in_dev->mr_maxdelay = max_delay;
Hangbin Liu966c37f2018-10-26 11:30:35 +0800997
998 /* RFC3376, 4.1.6. QRV and 4.1.7. QQIC, when the most recently
999 * received value was zero, use the default or statically
1000 * configured value.
1001 */
1002 in_dev->mr_qrv = ih3->qrv ?: net->ipv4.sysctl_igmp_qrv;
1003 in_dev->mr_qi = IGMPV3_QQIC(ih3->qqic)*HZ ?: IGMP_QUERY_INTERVAL;
1004
1005 /* RFC3376, 8.3. Query Response Interval:
1006 * The number of seconds represented by the [Query Response
1007 * Interval] must be less than the [Query Interval].
1008 */
1009 if (in_dev->mr_qri >= in_dev->mr_qi)
1010 in_dev->mr_qri = (in_dev->mr_qi/HZ - 1)*HZ;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (!group) { /* general query */
1013 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +02001014 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +00001016 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 /* mark sources to include, if group & source-specific */
1019 mark = ih3->nsrcs != 0;
1020 }
1021
1022 /*
1023 * - Start the timers in all of our membership records
1024 * that the query applies to for the interface on
1025 * which the query arrived excl. those that belong
1026 * to a "local" group (224.0.0.X)
1027 * - For timers already running check if they need to
1028 * be reset.
1029 * - Use the igmp->igmp_code field as the maximum
1030 * delay possible
1031 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001032 rcu_read_lock();
1033 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -08001034 int changed;
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 if (group && group != im->multiaddr)
1037 continue;
1038 if (im->multiaddr == IGMP_ALL_HOSTS)
1039 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001040 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001041 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001042 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 spin_lock_bh(&im->lock);
1044 if (im->tm_running)
1045 im->gsquery = im->gsquery && mark;
1046 else
1047 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -08001048 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001049 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -08001051 if (changed)
1052 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001054 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +00001055 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001058/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059int igmp_rcv(struct sk_buff *skb)
1060{
1061 /* This basically follows the spec line by line -- see RFC1112 */
1062 struct igmphdr *ih;
David Ahernc7b725b2017-08-15 18:38:42 -07001063 struct net_device *dev = skb->dev;
1064 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001066 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
David Ahernc7b725b2017-08-15 18:38:42 -07001068 if (netif_is_l3_master(dev)) {
1069 dev = dev_get_by_index_rcu(dev_net(dev), IPCB(skb)->iif);
1070 if (!dev)
1071 goto drop;
1072 }
1073
1074 in_dev = __in_dev_get_rcu(dev);
Ian Morris51456b22015-04-03 09:17:26 +01001075 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001076 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Herbert Xufb286bb2005-11-10 13:01:24 -08001078 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001079 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001080
Tom Herbertde08dc12014-05-07 16:52:10 -07001081 if (skb_checksum_simple_validate(skb))
1082 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001084 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 switch (ih->type) {
1086 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001087 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 break;
1089 case IGMP_HOST_MEMBERSHIP_REPORT:
1090 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001092 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 break;
David Stevens24c69272005-12-02 20:32:59 -08001094 /* don't rely on MC router hearing unicast reports */
1095 if (skb->pkt_type == PACKET_MULTICAST ||
1096 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001097 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 break;
1099 case IGMP_PIM:
1100#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 return pim_rcv_v1(skb);
1102#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001103 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 case IGMP_DVMRP:
1105 case IGMP_TRACE:
1106 case IGMP_HOST_LEAVE_MESSAGE:
1107 case IGMP_MTRACE:
1108 case IGMP_MTRACE_RESP:
1109 break;
1110 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001111 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001113
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001114drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001115 if (dropped)
1116 kfree_skb(skb);
1117 else
1118 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 return 0;
1120}
1121
1122#endif
1123
1124
1125/*
1126 * Add a filter to a device
1127 */
1128
Al Viro63007722006-09-27 18:31:32 -07001129static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 char buf[MAX_ADDR_LEN];
1132 struct net_device *dev = in_dev->dev;
1133
1134 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1135 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001136 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 routine. Something sort of:
1138 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1139 --ANK
1140 */
1141 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001142 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145/*
1146 * Remove a filter from a device
1147 */
1148
Al Viro63007722006-09-27 18:31:32 -07001149static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
1151 char buf[MAX_ADDR_LEN];
1152 struct net_device *dev = in_dev->dev;
1153
1154 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001155 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158#ifdef CONFIG_IP_MULTICAST
1159/*
1160 * deleted ip_mc_list manipulation
1161 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001162static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im,
1163 gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001166 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 /* this is an "ip_mc_list" for convenience; only the fields below
1169 * are actually used. In particular, the refcnt and users are not
1170 * used for management of the delete list. Using the same structure
1171 * for deleted items allows change reports to use common code with
1172 * non-deleted or query-response MCA's.
1173 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001174 pmc = kzalloc(sizeof(*pmc), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 if (!pmc)
1176 return;
WANG Congb4846fc2017-06-20 10:46:27 -07001177 spin_lock_init(&pmc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_lock_bh(&im->lock);
1179 pmc->interface = im->interface;
1180 in_dev_hold(in_dev);
1181 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001182 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 pmc->sfmode = im->sfmode;
1184 if (pmc->sfmode == MCAST_INCLUDE) {
1185 struct ip_sf_list *psf;
1186
1187 pmc->tomb = im->tomb;
1188 pmc->sources = im->sources;
1189 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001190 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 psf->sf_crcount = pmc->crcount;
1192 }
1193 spin_unlock_bh(&im->lock);
1194
1195 spin_lock_bh(&in_dev->mc_tomb_lock);
1196 pmc->next = in_dev->mc_tomb;
1197 in_dev->mc_tomb = pmc;
1198 spin_unlock_bh(&in_dev->mc_tomb_lock);
1199}
1200
Hangbin Liu24803f32016-11-14 16:16:28 +08001201/*
1202 * restore ip_mc_list deleted records
1203 */
1204static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001207 struct ip_sf_list *psf;
1208 struct net *net = dev_net(in_dev->dev);
1209 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 spin_lock_bh(&in_dev->mc_tomb_lock);
1212 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001213 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (pmc->multiaddr == multiaddr)
1215 break;
1216 pmc_prev = pmc;
1217 }
1218 if (pmc) {
1219 if (pmc_prev)
1220 pmc_prev->next = pmc->next;
1221 else
1222 in_dev->mc_tomb = pmc->next;
1223 }
1224 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001225
1226 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001228 im->interface = pmc->interface;
Hangbin Liu08d3ffc2018-07-20 14:04:27 +08001229 if (im->sfmode == MCAST_INCLUDE) {
Eric Dumazete5b1c6c2019-06-27 01:27:01 -07001230 swap(im->tomb, pmc->tomb);
1231 swap(im->sources, pmc->sources);
Hangbin Liu24803f32016-11-14 16:16:28 +08001232 for (psf = im->sources; psf; psf = psf->sf_next)
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001233 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1234 } else {
1235 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 in_dev_put(pmc->interface);
Eric Dumazet3580d042019-05-22 16:51:22 -07001238 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001240 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
Hangbin Liu24803f32016-11-14 16:16:28 +08001243/*
1244 * flush ip_mc_list deleted records
1245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246static void igmpv3_clear_delrec(struct in_device *in_dev)
1247{
1248 struct ip_mc_list *pmc, *nextpmc;
1249
1250 spin_lock_bh(&in_dev->mc_tomb_lock);
1251 pmc = in_dev->mc_tomb;
1252 in_dev->mc_tomb = NULL;
1253 spin_unlock_bh(&in_dev->mc_tomb_lock);
1254
1255 for (; pmc; pmc = nextpmc) {
1256 nextpmc = pmc->next;
1257 ip_mc_clear_src(pmc);
1258 in_dev_put(pmc->interface);
Eric Dumazet3580d042019-05-22 16:51:22 -07001259 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001262 rcu_read_lock();
1263 for_each_pmc_rcu(in_dev, pmc) {
Eric Dumazet3580d042019-05-22 16:51:22 -07001264 struct ip_sf_list *psf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 spin_lock_bh(&pmc->lock);
1267 psf = pmc->tomb;
1268 pmc->tomb = NULL;
1269 spin_unlock_bh(&pmc->lock);
Eric Dumazet3580d042019-05-22 16:51:22 -07001270 ip_sf_list_clear_all(psf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001272 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274#endif
1275
Florian Fainelli9fb20802019-02-01 20:20:52 -08001276static void __igmp_group_dropped(struct ip_mc_list *im, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277{
1278 struct in_device *in_dev = im->interface;
1279#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001280 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int reporter;
1282#endif
1283
1284 if (im->loaded) {
1285 im->loaded = 0;
1286 ip_mc_filter_del(in_dev, im->multiaddr);
1287 }
1288
1289#ifdef CONFIG_IP_MULTICAST
1290 if (im->multiaddr == IGMP_ALL_HOSTS)
1291 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001292 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001293 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 reporter = im->reporter;
1296 igmp_stop_timer(im);
1297
1298 if (!in_dev->dead) {
1299 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001300 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if (IGMP_V2_SEEN(in_dev)) {
1302 if (reporter)
1303 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001304 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306 /* IGMPv3 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001307 igmpv3_add_delrec(in_dev, im, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 igmp_ifc_event(in_dev);
1310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
Florian Fainelli9fb20802019-02-01 20:20:52 -08001314static void igmp_group_dropped(struct ip_mc_list *im)
1315{
1316 __igmp_group_dropped(im, GFP_KERNEL);
1317}
1318
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001319static void igmp_group_added(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320{
1321 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001322#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001323 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001324#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 if (im->loaded == 0) {
1327 im->loaded = 1;
1328 ip_mc_filter_add(in_dev, im->multiaddr);
1329 }
1330
1331#ifdef CONFIG_IP_MULTICAST
1332 if (im->multiaddr == IGMP_ALL_HOSTS)
1333 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001334 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001335 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 if (in_dev->dead)
1338 return;
Hangbin Liuff065252018-08-29 18:06:10 +08001339
1340 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1342 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001343 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 spin_unlock_bh(&im->lock);
1345 return;
1346 }
1347 /* else, v3 */
1348
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001349 /* Based on RFC3376 5.1, for newly added INCLUDE SSM, we should
1350 * not send filter-mode change record as the mode should be from
1351 * IN() to IN(A).
1352 */
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001353 if (im->sfmode == MCAST_EXCLUDE)
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001354 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 igmp_ifc_event(in_dev);
1357#endif
1358}
1359
1360
1361/*
1362 * Multicast list managers
1363 */
1364
Eric Dumazete9897072013-06-07 08:48:57 -07001365static u32 ip_mc_hash(const struct ip_mc_list *im)
1366{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001367 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001368}
1369
1370static void ip_mc_hash_add(struct in_device *in_dev,
1371 struct ip_mc_list *im)
1372{
1373 struct ip_mc_list __rcu **mc_hash;
1374 u32 hash;
1375
1376 mc_hash = rtnl_dereference(in_dev->mc_hash);
1377 if (mc_hash) {
1378 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001379 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001380 rcu_assign_pointer(mc_hash[hash], im);
1381 return;
1382 }
1383
1384 /* do not use a hash table for small number of items */
1385 if (in_dev->mc_count < 4)
1386 return;
1387
1388 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1389 GFP_KERNEL);
1390 if (!mc_hash)
1391 return;
1392
1393 for_each_pmc_rtnl(in_dev, im) {
1394 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001395 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001396 RCU_INIT_POINTER(mc_hash[hash], im);
1397 }
1398
1399 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1400}
1401
1402static void ip_mc_hash_remove(struct in_device *in_dev,
1403 struct ip_mc_list *im)
1404{
1405 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1406 struct ip_mc_list *aux;
1407
1408 if (!mc_hash)
1409 return;
1410 mc_hash += ip_mc_hash(im);
1411 while ((aux = rtnl_dereference(*mc_hash)) != im)
1412 mc_hash = &aux->next_hash;
1413 *mc_hash = im->next_hash;
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417/*
1418 * A socket has joined a multicast group on device dev.
1419 */
Florian Fainelli9fb20802019-02-01 20:20:52 -08001420static void ____ip_mc_inc_group(struct in_device *in_dev, __be32 addr,
1421 unsigned int mode, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
1423 struct ip_mc_list *im;
1424
1425 ASSERT_RTNL();
1426
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001427 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (im->multiaddr == addr) {
1429 im->users++;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001430 ip_mc_add_src(in_dev, &addr, mode, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 goto out;
1432 }
1433 }
1434
Florian Fainelli9fb20802019-02-01 20:20:52 -08001435 im = kzalloc(sizeof(*im), gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!im)
1437 goto out;
1438
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001439 im->users = 1;
1440 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001442 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* initial mode is (EX, empty) */
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001444 im->sfmode = mode;
1445 im->sfcount[mode] = 1;
Reshetova, Elena8851ab52017-06-30 13:08:02 +03001446 refcount_set(&im->refcnt, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 spin_lock_init(&im->lock);
1448#ifdef CONFIG_IP_MULTICAST
Kees Cooke99e88a2017-10-16 14:43:17 -07001449 timer_setup(&im->timer, igmp_timer_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001451
1452 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001453 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001454 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001455
Eric Dumazete9897072013-06-07 08:48:57 -07001456 ip_mc_hash_add(in_dev, im);
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001459 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001461 igmp_group_added(im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if (!in_dev->dead)
1463 ip_rt_multicast_event(in_dev);
1464out:
1465 return;
1466}
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001467
Florian Fainelli9fb20802019-02-01 20:20:52 -08001468void __ip_mc_inc_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
1469{
1470 ____ip_mc_inc_group(in_dev, addr, MCAST_EXCLUDE, gfp);
1471}
1472EXPORT_SYMBOL(__ip_mc_inc_group);
1473
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001474void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
1475{
Li RongQinga1c4cd62019-08-20 13:52:47 +08001476 __ip_mc_inc_group(in_dev, addr, GFP_KERNEL);
Hangbin Liu6e2059b2018-07-10 22:41:26 +08001477}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001478EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001480static int ip_mc_check_iphdr(struct sk_buff *skb)
1481{
1482 const struct iphdr *iph;
1483 unsigned int len;
1484 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1485
1486 if (!pskb_may_pull(skb, offset))
1487 return -EINVAL;
1488
1489 iph = ip_hdr(skb);
1490
1491 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1492 return -EINVAL;
1493
1494 offset += ip_hdrlen(skb) - sizeof(*iph);
1495
1496 if (!pskb_may_pull(skb, offset))
1497 return -EINVAL;
1498
1499 iph = ip_hdr(skb);
1500
1501 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1502 return -EINVAL;
1503
1504 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1505 if (skb->len < len || len < offset)
1506 return -EINVAL;
1507
1508 skb_set_transport_header(skb, offset);
1509
1510 return 0;
1511}
1512
1513static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1514{
1515 unsigned int len = skb_transport_offset(skb);
1516
1517 len += sizeof(struct igmpv3_report);
1518
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001519 return ip_mc_may_pull(skb, len) ? 0 : -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001520}
1521
1522static int ip_mc_check_igmp_query(struct sk_buff *skb)
1523{
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001524 unsigned int transport_len = ip_transport_len(skb);
1525 unsigned int len;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001526
1527 /* IGMPv{1,2}? */
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001528 if (transport_len != sizeof(struct igmphdr)) {
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001529 /* or IGMPv3? */
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001530 if (transport_len < sizeof(struct igmpv3_query))
1531 return -EINVAL;
1532
1533 len = skb_transport_offset(skb) + sizeof(struct igmpv3_query);
1534 if (!ip_mc_may_pull(skb, len))
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001535 return -EINVAL;
1536 }
1537
1538 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1539 * all-systems destination addresses (224.0.0.1) for general queries
1540 */
1541 if (!igmp_hdr(skb)->group &&
1542 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1543 return -EINVAL;
1544
1545 return 0;
1546}
1547
1548static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1549{
1550 switch (igmp_hdr(skb)->type) {
1551 case IGMP_HOST_LEAVE_MESSAGE:
1552 case IGMP_HOST_MEMBERSHIP_REPORT:
1553 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001554 return 0;
1555 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1556 return ip_mc_check_igmp_reportv3(skb);
1557 case IGMP_HOST_MEMBERSHIP_QUERY:
1558 return ip_mc_check_igmp_query(skb);
1559 default:
1560 return -ENOMSG;
1561 }
1562}
1563
Alexey Dobriyan5a43f692019-10-04 00:26:52 +03001564static __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001565{
1566 return skb_checksum_simple_validate(skb);
1567}
1568
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001569static int ip_mc_check_igmp_csum(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001570{
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001571 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001572 unsigned int transport_len = ip_transport_len(skb);
1573 struct sk_buff *skb_chk;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001574
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001575 if (!ip_mc_may_pull(skb, len))
1576 return -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001577
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001578 skb_chk = skb_checksum_trimmed(skb, transport_len,
1579 ip_mc_validate_checksum);
1580 if (!skb_chk)
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001581 return -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001582
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001583 if (skb_chk != skb)
Linus Lüssinga5169932015-08-13 05:54:07 +02001584 kfree_skb(skb_chk);
1585
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001586 return 0;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001587}
1588
1589/**
1590 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1591 * @skb: the skb to validate
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001592 *
1593 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001594 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001595 *
1596 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1597 * standard
1598 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1599 * -ENOMEM: A memory allocation failure happened.
1600 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001601 * Caller needs to set the skb network header and free any returned skb if it
1602 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001603 */
Linus Lüssingba5ea6142019-01-21 07:26:25 +01001604int ip_mc_check_igmp(struct sk_buff *skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001605{
1606 int ret = ip_mc_check_iphdr(skb);
1607
1608 if (ret < 0)
1609 return ret;
1610
1611 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1612 return -ENOMSG;
1613
Linus Lüssinga2e2ca32019-01-21 07:26:26 +01001614 ret = ip_mc_check_igmp_csum(skb);
1615 if (ret < 0)
1616 return ret;
1617
1618 return ip_mc_check_igmp_msg(skb);
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001619}
1620EXPORT_SYMBOL(ip_mc_check_igmp);
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001623 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001624 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001625static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001626{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001627#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001628 struct ip_mc_list *im;
1629 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001630 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001631
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001632 ASSERT_RTNL();
1633
1634 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001635 if (im->multiaddr == IGMP_ALL_HOSTS)
1636 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001637 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001638 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001639 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001640
Eric Dumazet866f3b22010-11-18 09:33:19 -08001641 /* a failover is happening and switches
1642 * must be notified immediately
1643 */
1644 if (IGMP_V1_SEEN(in_dev))
1645 type = IGMP_HOST_MEMBERSHIP_REPORT;
1646 else if (IGMP_V2_SEEN(in_dev))
1647 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1648 else
1649 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1650 igmp_send_report(in_dev, im, type);
1651 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001652#endif
1653}
1654
1655/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 * A socket has left a multicast group on device dev
1657 */
1658
Florian Fainelli9fb20802019-02-01 20:20:52 -08001659void __ip_mc_dec_group(struct in_device *in_dev, __be32 addr, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001661 struct ip_mc_list *i;
1662 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001665
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001666 for (ip = &in_dev->mc_list;
1667 (i = rtnl_dereference(*ip)) != NULL;
1668 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001669 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001671 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001672 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001673 in_dev->mc_count--;
Florian Fainelli9fb20802019-02-01 20:20:52 -08001674 __igmp_group_dropped(i, gfp);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001675 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 if (!in_dev->dead)
1678 ip_rt_multicast_event(in_dev);
1679
1680 ip_ma_put(i);
1681 return;
1682 }
1683 break;
1684 }
1685 }
1686}
Florian Fainelli9fb20802019-02-01 20:20:52 -08001687EXPORT_SYMBOL(__ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Moni Shoua75c78502009-09-15 02:37:40 -07001689/* Device changing type */
1690
1691void ip_mc_unmap(struct in_device *in_dev)
1692{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001693 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001694
1695 ASSERT_RTNL();
1696
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001697 for_each_pmc_rtnl(in_dev, pmc)
1698 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001699}
1700
1701void ip_mc_remap(struct in_device *in_dev)
1702{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001703 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001704
1705 ASSERT_RTNL();
1706
Hangbin Liu24803f32016-11-14 16:16:28 +08001707 for_each_pmc_rtnl(in_dev, pmc) {
1708#ifdef CONFIG_IP_MULTICAST
1709 igmpv3_del_delrec(in_dev, pmc);
1710#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001711 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001712 }
Moni Shoua75c78502009-09-15 02:37:40 -07001713}
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715/* Device going down */
1716
1717void ip_mc_down(struct in_device *in_dev)
1718{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001719 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 ASSERT_RTNL();
1722
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001723 for_each_pmc_rtnl(in_dev, pmc)
1724 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726#ifdef CONFIG_IP_MULTICAST
Greg Kroah-Hartman83da0c02021-08-27 18:38:12 +02001727 in_dev->mr_ifc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (del_timer(&in_dev->mr_ifc_timer))
1729 __in_dev_put(in_dev);
1730 in_dev->mr_gq_running = 0;
1731 if (del_timer(&in_dev->mr_gq_timer))
1732 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733#endif
1734
1735 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1736}
1737
Hangbin Liu966c37f2018-10-26 11:30:35 +08001738#ifdef CONFIG_IP_MULTICAST
1739static void ip_mc_reset(struct in_device *in_dev)
1740{
1741 struct net *net = dev_net(in_dev->dev);
1742
1743 in_dev->mr_qi = IGMP_QUERY_INTERVAL;
1744 in_dev->mr_qri = IGMP_QUERY_RESPONSE_INTERVAL;
1745 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
1746}
1747#else
1748static void ip_mc_reset(struct in_device *in_dev)
1749{
1750}
1751#endif
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753void ip_mc_init_dev(struct in_device *in_dev)
1754{
1755 ASSERT_RTNL();
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757#ifdef CONFIG_IP_MULTICAST
Kees Cooke99e88a2017-10-16 14:43:17 -07001758 timer_setup(&in_dev->mr_gq_timer, igmp_gq_timer_expire, 0);
1759 timer_setup(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760#endif
Hangbin Liu966c37f2018-10-26 11:30:35 +08001761 ip_mc_reset(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 spin_lock_init(&in_dev->mc_tomb_lock);
1764}
1765
1766/* Device going up */
1767
1768void ip_mc_up(struct in_device *in_dev)
1769{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001770 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 ASSERT_RTNL();
1773
Hangbin Liu966c37f2018-10-26 11:30:35 +08001774 ip_mc_reset(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1776
Hangbin Liu24803f32016-11-14 16:16:28 +08001777 for_each_pmc_rtnl(in_dev, pmc) {
1778#ifdef CONFIG_IP_MULTICAST
1779 igmpv3_del_delrec(in_dev, pmc);
1780#endif
Hangbin Liu0ae0d602018-07-20 14:07:42 +08001781 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
1785/*
1786 * Device is about to be destroyed: clean up.
1787 */
1788
1789void ip_mc_destroy_dev(struct in_device *in_dev)
1790{
1791 struct ip_mc_list *i;
1792
1793 ASSERT_RTNL();
1794
1795 /* Deactivate timers */
1796 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001797#ifdef CONFIG_IP_MULTICAST
1798 igmpv3_clear_delrec(in_dev);
1799#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001801 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1802 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001803 in_dev->mc_count--;
Chengyang Fanac31cc82021-06-16 17:59:25 +08001804 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001809/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001810static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct net_device *dev = NULL;
1813 struct in_device *idev = NULL;
1814
1815 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001816 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 return idev;
1818 }
1819 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001820 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 if (!dev)
1822 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
1824
David S. Millerb23dd4f2011-03-02 14:31:35 -08001825 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001826 struct rtable *rt = ip_route_output(net,
1827 imr->imr_multiaddr.s_addr,
1828 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001829 if (!IS_ERR(rt)) {
1830 dev = rt->dst.dev;
1831 ip_rt_put(rt);
1832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834 if (dev) {
1835 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001836 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838 return idev;
1839}
1840
1841/*
1842 * Join a socket to a group
1843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001846 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 struct ip_sf_list *psf, *psf_prev;
1849 int rv = 0;
1850
1851 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001852 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (psf->sf_inaddr == *psfsrc)
1854 break;
1855 psf_prev = psf;
1856 }
1857 if (!psf || psf->sf_count[sfmode] == 0) {
1858 /* source filter not found, or count wrong => bug */
1859 return -ESRCH;
1860 }
1861 psf->sf_count[sfmode]--;
1862 if (psf->sf_count[sfmode] == 0) {
1863 ip_rt_multicast_event(pmc->interface);
1864 }
1865 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1866#ifdef CONFIG_IP_MULTICAST
1867 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001868 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869#endif
1870
1871 /* no more filters for this source */
1872 if (psf_prev)
1873 psf_prev->sf_next = psf->sf_next;
1874 else
1875 pmc->sources = psf->sf_next;
1876#ifdef CONFIG_IP_MULTICAST
1877 if (psf->sf_oldin &&
1878 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001879 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 psf->sf_next = pmc->tomb;
1881 pmc->tomb = psf;
1882 rv = 1;
1883 } else
1884#endif
1885 kfree(psf);
1886 }
1887 return rv;
1888}
1889
1890#ifndef CONFIG_IP_MULTICAST
1891#define igmp_ifc_event(x) do { } while (0)
1892#endif
1893
Al Viro8f935bb2006-09-27 18:30:07 -07001894static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1895 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 struct ip_mc_list *pmc;
1898 int changerec = 0;
1899 int i, err;
1900
1901 if (!in_dev)
1902 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001903 rcu_read_lock();
1904 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (*pmca == pmc->multiaddr)
1906 break;
1907 }
1908 if (!pmc) {
1909 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001910 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return -ESRCH;
1912 }
1913 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001914 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915#ifdef CONFIG_IP_MULTICAST
1916 sf_markstate(pmc);
1917#endif
1918 if (!delta) {
1919 err = -EINVAL;
1920 if (!pmc->sfcount[sfmode])
1921 goto out_unlock;
1922 pmc->sfcount[sfmode]--;
1923 }
1924 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001925 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1927
1928 changerec |= rv > 0;
1929 if (!err && rv < 0)
1930 err = rv;
1931 }
1932 if (pmc->sfmode == MCAST_EXCLUDE &&
1933 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1934 pmc->sfcount[MCAST_INCLUDE]) {
1935#ifdef CONFIG_IP_MULTICAST
1936 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001937 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938#endif
1939
1940 /* filter mode change */
1941 pmc->sfmode = MCAST_INCLUDE;
1942#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001943 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Greg Kroah-Hartman83da0c02021-08-27 18:38:12 +02001944 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001945 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 psf->sf_crcount = 0;
1947 igmp_ifc_event(pmc->interface);
1948 } else if (sf_setstate(pmc) || changerec) {
1949 igmp_ifc_event(pmc->interface);
1950#endif
1951 }
1952out_unlock:
1953 spin_unlock_bh(&pmc->lock);
1954 return err;
1955}
1956
1957/*
1958 * Add multicast single-source filter to the interface list
1959 */
1960static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001961 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 struct ip_sf_list *psf, *psf_prev;
1964
1965 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001966 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (psf->sf_inaddr == *psfsrc)
1968 break;
1969 psf_prev = psf;
1970 }
1971 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001972 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (!psf)
1974 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 psf->sf_inaddr = *psfsrc;
1976 if (psf_prev) {
1977 psf_prev->sf_next = psf;
1978 } else
1979 pmc->sources = psf;
1980 }
1981 psf->sf_count[sfmode]++;
1982 if (psf->sf_count[sfmode] == 1) {
1983 ip_rt_multicast_event(pmc->interface);
1984 }
1985 return 0;
1986}
1987
1988#ifdef CONFIG_IP_MULTICAST
1989static void sf_markstate(struct ip_mc_list *pmc)
1990{
1991 struct ip_sf_list *psf;
1992 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1993
Weilong Chenc71151f2013-12-23 14:37:29 +08001994 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (pmc->sfcount[MCAST_EXCLUDE]) {
1996 psf->sf_oldin = mca_xcount ==
1997 psf->sf_count[MCAST_EXCLUDE] &&
1998 !psf->sf_count[MCAST_INCLUDE];
1999 } else
2000 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
2001}
2002
2003static int sf_setstate(struct ip_mc_list *pmc)
2004{
David L Stevensad125832006-01-18 14:20:56 -08002005 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
2007 int qrv = pmc->interface->mr_qrv;
2008 int new_in, rv;
2009
2010 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002011 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (pmc->sfcount[MCAST_EXCLUDE]) {
2013 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
2014 !psf->sf_count[MCAST_INCLUDE];
2015 } else
2016 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08002017 if (new_in) {
2018 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05002019 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08002020
Weilong Chenc71151f2013-12-23 14:37:29 +08002021 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08002022 if (dpsf->sf_inaddr == psf->sf_inaddr)
2023 break;
2024 prev = dpsf;
2025 }
2026 if (dpsf) {
2027 if (prev)
2028 prev->sf_next = dpsf->sf_next;
2029 else
2030 pmc->tomb = dpsf->sf_next;
2031 kfree(dpsf);
2032 }
2033 psf->sf_crcount = qrv;
2034 rv++;
2035 }
2036 } else if (psf->sf_oldin) {
2037
2038 psf->sf_crcount = 0;
2039 /*
2040 * add or update "delete" records if an active filter
2041 * is now inactive
2042 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002043 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08002044 if (dpsf->sf_inaddr == psf->sf_inaddr)
2045 break;
2046 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00002047 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08002048 if (!dpsf)
2049 continue;
2050 *dpsf = *psf;
2051 /* pmc->lock held by callers */
2052 dpsf->sf_next = pmc->tomb;
2053 pmc->tomb = dpsf;
2054 }
2055 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 rv++;
2057 }
2058 }
2059 return rv;
2060}
2061#endif
2062
2063/*
2064 * Add multicast source filter list to the interface list
2065 */
Al Viro8f935bb2006-09-27 18:30:07 -07002066static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2067 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068{
2069 struct ip_mc_list *pmc;
2070 int isexclude;
2071 int i, err;
2072
2073 if (!in_dev)
2074 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002075 rcu_read_lock();
2076 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (*pmca == pmc->multiaddr)
2078 break;
2079 }
2080 if (!pmc) {
2081 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002082 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 return -ESRCH;
2084 }
2085 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002086 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088#ifdef CONFIG_IP_MULTICAST
2089 sf_markstate(pmc);
2090#endif
2091 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2092 if (!delta)
2093 pmc->sfcount[sfmode]++;
2094 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002095 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002096 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (err)
2098 break;
2099 }
2100 if (err) {
2101 int j;
2102
Jun Zhao685f94e62011-11-22 17:19:03 +00002103 if (!delta)
2104 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002105 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002106 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2108#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002110 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002111 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112#endif
2113
2114 /* filter mode change */
2115 if (pmc->sfcount[MCAST_EXCLUDE])
2116 pmc->sfmode = MCAST_EXCLUDE;
2117 else if (pmc->sfcount[MCAST_INCLUDE])
2118 pmc->sfmode = MCAST_INCLUDE;
2119#ifdef CONFIG_IP_MULTICAST
2120 /* else no filters; keep old mode for reports */
2121
Nikolay Borisov165094a2016-02-08 23:29:24 +02002122 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Greg Kroah-Hartman83da0c02021-08-27 18:38:12 +02002123 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002124 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 psf->sf_crcount = 0;
2126 igmp_ifc_event(in_dev);
2127 } else if (sf_setstate(pmc)) {
2128 igmp_ifc_event(in_dev);
2129#endif
2130 }
2131 spin_unlock_bh(&pmc->lock);
2132 return err;
2133}
2134
2135static void ip_mc_clear_src(struct ip_mc_list *pmc)
2136{
Eric Dumazet3580d042019-05-22 16:51:22 -07002137 struct ip_sf_list *tomb, *sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
WANG Congc38b7d32017-06-12 09:52:26 -07002139 spin_lock_bh(&pmc->lock);
2140 tomb = pmc->tomb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 pmc->tomb = NULL;
WANG Congc38b7d32017-06-12 09:52:26 -07002142 sources = pmc->sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 pmc->sources = NULL;
2144 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002145 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 pmc->sfcount[MCAST_EXCLUDE] = 1;
WANG Congc38b7d32017-06-12 09:52:26 -07002147 spin_unlock_bh(&pmc->lock);
2148
Eric Dumazet3580d042019-05-22 16:51:22 -07002149 ip_sf_list_clear_all(tomb);
2150 ip_sf_list_clear_all(sources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002153/* Join a multicast group
2154 */
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002155static int __ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr,
2156 unsigned int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Al Viro8f935bb2006-09-27 18:30:07 -07002158 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002159 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct in_device *in_dev;
2161 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002162 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002163 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002165 int err;
2166
2167 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Joe Perchesf97c1e02007-12-16 13:45:43 -08002169 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return -EINVAL;
2171
Daniel Lezcano877aced2008-08-13 16:15:57 -07002172 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 err = -ENODEV;
2176 goto done;
2177 }
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002180 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002181 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002182 if (i->multi.imr_multiaddr.s_addr == addr &&
2183 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 count++;
2186 }
2187 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002188 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002190 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002191 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002192 goto done;
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002195 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 iml->sflist = NULL;
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002197 iml->sfmode = mode;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002198 rcu_assign_pointer(inet->mc_list, iml);
Li RongQinga1c4cd62019-08-20 13:52:47 +08002199 ____ip_mc_inc_group(in_dev, addr, mode, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return err;
2203}
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002204
2205/* Join ASM (Any-Source Multicast) group
2206 */
2207int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
2208{
2209 return __ip_mc_join_group(sk, imr, MCAST_EXCLUDE);
2210}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002211EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Hangbin Liu6e2059b2018-07-10 22:41:26 +08002213/* Join SSM (Source-Specific Multicast) group
2214 */
2215int ip_mc_join_group_ssm(struct sock *sk, struct ip_mreqn *imr,
2216 unsigned int mode)
2217{
2218 return __ip_mc_join_group(sk, imr, mode);
2219}
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2222 struct in_device *in_dev)
2223{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002224 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 int err;
2226
Ian Morris51456b22015-04-03 09:17:26 +01002227 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 /* any-source empty exclude case */
2229 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2230 iml->sfmode, 0, NULL, 0);
2231 }
2232 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002233 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002234 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002235 /* decrease mem now to avoid the memleak warning */
2236 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002237 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return err;
2239}
2240
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002241int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
2243 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002244 struct ip_mc_socklist *iml;
2245 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002246 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002247 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002248 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002249 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002250 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Eric Dumazet959d10f2015-02-17 03:19:24 -08002252 ASSERT_RTNL();
2253
Daniel Lezcano877aced2008-08-13 16:15:57 -07002254 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002255 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002256 ret = -ENODEV;
2257 goto out;
2258 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002259 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002260 for (imlp = &inet->mc_list;
2261 (iml = rtnl_dereference(*imlp)) != NULL;
2262 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002263 if (iml->multi.imr_multiaddr.s_addr != group)
2264 continue;
2265 if (ifindex) {
2266 if (iml->multi.imr_ifindex != ifindex)
2267 continue;
2268 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2269 iml->multi.imr_address.s_addr)
2270 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
David L Stevensacd6e002006-08-17 16:27:39 -07002272 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002274 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002275
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002276 if (in_dev)
2277 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002278
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002279 /* decrease mem now to avoid the memleak warning */
2280 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002281 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
dingtianhong52ad3532014-07-02 13:50:48 +08002284out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002285 return ret;
2286}
stephen hemminger193ba922012-10-01 12:32:34 +00002287EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289int ip_mc_source(int add, int omode, struct sock *sk, struct
2290 ip_mreq_source *mreqs, int ifindex)
2291{
2292 int err;
2293 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002294 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 struct ip_mc_socklist *pmc;
2296 struct in_device *in_dev = NULL;
2297 struct inet_sock *inet = inet_sk(sk);
2298 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002299 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002300 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 int i, j, rv;
2302
Joe Perchesf97c1e02007-12-16 13:45:43 -08002303 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 return -EINVAL;
2305
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002306 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2309 imr.imr_address.s_addr = mreqs->imr_interface;
2310 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002311 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 if (!in_dev) {
2314 err = -ENODEV;
2315 goto done;
2316 }
2317 err = -EADDRNOTAVAIL;
2318
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002319 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002320 if ((pmc->multi.imr_multiaddr.s_addr ==
2321 imr.imr_multiaddr.s_addr) &&
2322 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 break;
2324 }
David L Stevens917f2f12005-07-08 17:45:16 -07002325 if (!pmc) { /* must have a prior join */
2326 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /* if a source filter was set, must be the same mode as before */
2330 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002331 if (pmc->sfmode != omode) {
2332 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 } else if (pmc->sfmode != omode) {
2336 /* allow mode switches for empty-set filters */
2337 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002338 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 NULL, 0);
2340 pmc->sfmode = omode;
2341 }
2342
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002343 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (!add) {
2345 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002346 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002348 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002350 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (rv == 0)
2352 break;
2353 }
2354 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002355 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
David L Stevens8cdaaa12005-07-08 17:39:23 -07002357 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2358 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2359 leavegroup = 1;
2360 goto done;
2361 }
2362
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002364 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 &mreqs->imr_sourceaddr, 1);
2366
Weilong Chenc71151f2013-12-23 14:37:29 +08002367 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 psl->sl_addr[j-1] = psl->sl_addr[j];
2369 psl->sl_count--;
2370 err = 0;
2371 goto done;
2372 }
2373 /* else, add a new source to the filter */
2374
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002375 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 err = -ENOBUFS;
2377 goto done;
2378 }
2379 if (!psl || psl->sl_count == psl->sl_max) {
2380 struct ip_sf_socklist *newpsl;
2381 int count = IP_SFBLOCK;
2382
2383 if (psl)
2384 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002385 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 if (!newpsl) {
2387 err = -ENOBUFS;
2388 goto done;
2389 }
2390 newpsl->sl_max = count;
2391 newpsl->sl_count = count - IP_SFBLOCK;
2392 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002393 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002395 /* decrease mem now to avoid the memleak warning */
2396 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002397 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002399 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002400 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002403 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002405 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (rv == 0)
2407 break;
2408 }
2409 if (rv == 0) /* address already there is an error */
2410 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002411 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 psl->sl_addr[j+1] = psl->sl_addr[j];
2413 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2414 psl->sl_count++;
2415 err = 0;
2416 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002417 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 &mreqs->imr_sourceaddr, 1);
2419done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002420 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002421 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return err;
2423}
2424
2425int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2426{
David L Stevens9951f032005-07-08 17:47:28 -07002427 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002429 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 struct ip_mc_socklist *pmc;
2431 struct in_device *in_dev;
2432 struct inet_sock *inet = inet_sk(sk);
2433 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002434 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002435 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Joe Perchesf97c1e02007-12-16 13:45:43 -08002437 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return -EINVAL;
2439 if (msf->imsf_fmode != MCAST_INCLUDE &&
2440 msf->imsf_fmode != MCAST_EXCLUDE)
2441 return -EINVAL;
2442
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002443 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2446 imr.imr_address.s_addr = msf->imsf_interface;
2447 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002448 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
2450 if (!in_dev) {
2451 err = -ENODEV;
2452 goto done;
2453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
David L Stevens9951f032005-07-08 17:47:28 -07002455 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2456 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2457 leavegroup = 1;
2458 goto done;
2459 }
2460
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002461 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2463 pmc->multi.imr_ifindex == imr.imr_ifindex)
2464 break;
2465 }
David L Stevens917f2f12005-07-08 17:45:16 -07002466 if (!pmc) { /* must have a prior join */
2467 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002471 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2472 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (!newpsl) {
2474 err = -ENOBUFS;
2475 goto done;
2476 }
2477 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2478 memcpy(newpsl->sl_addr, msf->imsf_slist,
2479 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2480 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2481 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2482 if (err) {
2483 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2484 goto done;
2485 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002486 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002488 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2489 msf->imsf_fmode, 0, NULL, 0);
2490 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002491 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (psl) {
2493 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2494 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002495 /* decrease mem now to avoid the memleak warning */
2496 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002497 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 } else
2499 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2500 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002501 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002503 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504done:
David L Stevens9951f032005-07-08 17:47:28 -07002505 if (leavegroup)
2506 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 return err;
2508}
2509
2510int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2511 struct ip_msfilter __user *optval, int __user *optlen)
2512{
2513 int err, len, count, copycount;
2514 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002515 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct ip_mc_socklist *pmc;
2517 struct in_device *in_dev;
2518 struct inet_sock *inet = inet_sk(sk);
2519 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002520 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
WANG Cong87e9f032015-11-03 15:41:16 -08002522 ASSERT_RTNL();
2523
Joe Perchesf97c1e02007-12-16 13:45:43 -08002524 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 return -EINVAL;
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2528 imr.imr_address.s_addr = msf->imsf_interface;
2529 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002530 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
2532 if (!in_dev) {
2533 err = -ENODEV;
2534 goto done;
2535 }
2536 err = -EADDRNOTAVAIL;
2537
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002538 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2540 pmc->multi.imr_ifindex == imr.imr_ifindex)
2541 break;
2542 }
2543 if (!pmc) /* must have a prior join */
2544 goto done;
2545 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002546 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 if (!psl) {
2548 len = 0;
2549 count = 0;
2550 } else {
2551 count = psl->sl_count;
2552 }
2553 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2554 len = copycount * sizeof(psl->sl_addr[0]);
2555 msf->imsf_numsrc = count;
2556 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2557 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2558 return -EFAULT;
2559 }
2560 if (len &&
2561 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2562 return -EFAULT;
2563 return 0;
2564done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 return err;
2566}
2567
2568int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
Al Viro931ca7a2020-03-29 17:18:30 -04002569 struct sockaddr_storage __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Al Viro931ca7a2020-03-29 17:18:30 -04002571 int i, count, copycount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002573 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 struct ip_mc_socklist *pmc;
2575 struct inet_sock *inet = inet_sk(sk);
2576 struct ip_sf_socklist *psl;
2577
WANG Cong87e9f032015-11-03 15:41:16 -08002578 ASSERT_RTNL();
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 psin = (struct sockaddr_in *)&gsf->gf_group;
2581 if (psin->sin_family != AF_INET)
2582 return -EINVAL;
2583 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002584 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 return -EINVAL;
2586
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002587 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2589 pmc->multi.imr_ifindex == gsf->gf_interface)
2590 break;
2591 }
2592 if (!pmc) /* must have a prior join */
Al Viro931ca7a2020-03-29 17:18:30 -04002593 return -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002595 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 count = psl ? psl->sl_count : 0;
2597 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2598 gsf->gf_numsrc = count;
Al Viro931ca7a2020-03-29 17:18:30 -04002599 for (i = 0; i < copycount; i++, p++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 struct sockaddr_storage ss;
2601
2602 psin = (struct sockaddr_in *)&ss;
2603 memset(&ss, 0, sizeof(ss));
2604 psin->sin_family = AF_INET;
2605 psin->sin_addr.s_addr = psl->sl_addr[i];
Al Viro931ca7a2020-03-29 17:18:30 -04002606 if (copy_to_user(p, &ss, sizeof(ss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 return -EFAULT;
2608 }
2609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
2612/*
2613 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2614 */
David Ahern60d9b032017-08-07 08:44:19 -07002615int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr,
2616 int dif, int sdif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
2618 struct inet_sock *inet = inet_sk(sk);
2619 struct ip_mc_socklist *pmc;
2620 struct ip_sf_socklist *psl;
2621 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002622 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002624 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002625 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002626 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002628 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002629 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
David Ahern60d9b032017-08-07 08:44:19 -07002631 (pmc->multi.imr_ifindex == dif ||
2632 (sdif && pmc->multi.imr_ifindex == sdif)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 break;
2634 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002635 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002637 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002638 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002639 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002641 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Weilong Chenc71151f2013-12-23 14:37:29 +08002643 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (psl->sl_addr[i] == rmt_addr)
2645 break;
2646 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002647 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002649 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002651 goto unlock;
2652 ret = 1;
2653unlock:
2654 rcu_read_unlock();
2655out:
2656 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
2659/*
2660 * A socket is closing.
2661 */
2662
2663void ip_mc_drop_socket(struct sock *sk)
2664{
2665 struct inet_sock *inet = inet_sk(sk);
2666 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002667 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Ian Morris51456b22015-04-03 09:17:26 +01002669 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return;
2671
2672 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002673 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002676 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002677 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002678 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002679 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002681 /* decrease mem now to avoid the memleak warning */
2682 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002683 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 }
2685 rtnl_unlock();
2686}
2687
David S. Millerdbdd9a52011-03-10 16:34:38 -08002688/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002689int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002692 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 struct ip_sf_list *psf;
2694 int rv = 0;
2695
Eric Dumazete9897072013-06-07 08:48:57 -07002696 mc_hash = rcu_dereference(in_dev->mc_hash);
2697 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002698 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002699
2700 for (im = rcu_dereference(mc_hash[hash]);
2701 im != NULL;
2702 im = rcu_dereference(im->next_hash)) {
2703 if (im->multiaddr == mc_addr)
2704 break;
2705 }
2706 } else {
2707 for_each_pmc_rcu(in_dev, im) {
2708 if (im->multiaddr == mc_addr)
2709 break;
2710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
2712 if (im && proto == IPPROTO_IGMP) {
2713 rv = 1;
2714 } else if (im) {
2715 if (src_addr) {
Liu Jianddd7e8b2021-07-16 12:06:17 +08002716 spin_lock_bh(&im->lock);
Weilong Chenc71151f2013-12-23 14:37:29 +08002717 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (psf->sf_inaddr == src_addr)
2719 break;
2720 }
2721 if (psf)
2722 rv = psf->sf_count[MCAST_INCLUDE] ||
2723 psf->sf_count[MCAST_EXCLUDE] !=
2724 im->sfcount[MCAST_EXCLUDE];
2725 else
2726 rv = im->sfcount[MCAST_EXCLUDE] != 0;
Liu Jianddd7e8b2021-07-16 12:06:17 +08002727 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 } else
2729 rv = 1; /* unspecified source; tentatively allow */
2730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 return rv;
2732}
2733
2734#if defined(CONFIG_PROC_FS)
2735struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002736 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct net_device *dev;
2738 struct in_device *in_dev;
2739};
2740
2741#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2742
2743static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2744{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002745 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 struct ip_mc_list *im = NULL;
2747 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2748
Pavel Emelianov7562f872007-05-03 15:13:45 -07002749 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002750 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002752
2753 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 if (!in_dev)
2755 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002756 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 if (im) {
2758 state->in_dev = in_dev;
2759 break;
2760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 }
2762 return im;
2763}
2764
2765static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2766{
2767 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002768
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002769 im = rcu_dereference(im->next_rcu);
2770 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002771 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if (!state->dev) {
2773 state->in_dev = NULL;
2774 break;
2775 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002776 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 if (!state->in_dev)
2778 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002779 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781 return im;
2782}
2783
2784static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2785{
2786 struct ip_mc_list *im = igmp_mc_get_first(seq);
2787 if (im)
2788 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2789 --pos;
2790 return pos ? NULL : im;
2791}
2792
2793static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002794 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
stephen hemminger61fbab72009-11-10 07:54:55 +00002796 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2798}
2799
2800static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2801{
2802 struct ip_mc_list *im;
2803 if (v == SEQ_START_TOKEN)
2804 im = igmp_mc_get_first(seq);
2805 else
2806 im = igmp_mc_get_next(seq, v);
2807 ++*pos;
2808 return im;
2809}
2810
2811static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002812 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
2814 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002815
2816 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002818 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
2821static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2822{
2823 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002824 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2826 else {
2827 struct ip_mc_list *im = (struct ip_mc_list *)v;
2828 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2829 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002830 long delta;
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832#ifdef CONFIG_IP_MULTICAST
2833 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2834 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2835 "V3";
2836#else
2837 querier = "NONE";
2838#endif
2839
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002840 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002842 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
2844
Eric Dumazeta399a802012-08-08 21:13:53 +00002845 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002847 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002849 im->tm_running,
2850 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 im->reporter);
2852 }
2853 return 0;
2854}
2855
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002856static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 .start = igmp_mc_seq_start,
2858 .next = igmp_mc_seq_next,
2859 .stop = igmp_mc_seq_stop,
2860 .show = igmp_mc_seq_show,
2861};
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002864 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct net_device *dev;
2866 struct in_device *idev;
2867 struct ip_mc_list *im;
2868};
2869
2870#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2871
2872static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2873{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002874 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 struct ip_sf_list *psf = NULL;
2876 struct ip_mc_list *im = NULL;
2877 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2878
Pavel Emelianov7562f872007-05-03 15:13:45 -07002879 state->idev = NULL;
2880 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002881 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002883 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002884 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002886 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002887 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 spin_lock_bh(&im->lock);
2889 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002890 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 state->im = im;
2892 state->idev = idev;
2893 break;
2894 }
2895 spin_unlock_bh(&im->lock);
2896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
2898 return psf;
2899}
2900
2901static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2902{
2903 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2904
2905 psf = psf->sf_next;
2906 while (!psf) {
2907 spin_unlock_bh(&state->im->lock);
2908 state->im = state->im->next;
2909 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002910 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (!state->dev) {
2912 state->idev = NULL;
2913 goto out;
2914 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002915 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (!state->idev)
2917 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002918 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
2920 if (!state->im)
2921 break;
2922 spin_lock_bh(&state->im->lock);
2923 psf = state->im->sources;
2924 }
2925out:
2926 return psf;
2927}
2928
2929static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2930{
2931 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2932 if (psf)
2933 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2934 --pos;
2935 return pos ? NULL : psf;
2936}
2937
2938static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002939 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
stephen hemminger61fbab72009-11-10 07:54:55 +00002941 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2943}
2944
2945static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2946{
2947 struct ip_sf_list *psf;
2948 if (v == SEQ_START_TOKEN)
2949 psf = igmp_mcf_get_first(seq);
2950 else
2951 psf = igmp_mcf_get_next(seq, v);
2952 ++*pos;
2953 return psf;
2954}
2955
2956static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002957 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
2959 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002960 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 spin_unlock_bh(&state->im->lock);
2962 state->im = NULL;
2963 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002964 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002966 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
2969static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2970{
2971 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2972 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2973
2974 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002975 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 } else {
2977 seq_printf(seq,
2978 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002979 "0x%08x %6lu %6lu\n",
2980 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 ntohl(state->im->multiaddr),
2982 ntohl(psf->sf_inaddr),
2983 psf->sf_count[MCAST_INCLUDE],
2984 psf->sf_count[MCAST_EXCLUDE]);
2985 }
2986 return 0;
2987}
2988
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002989static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 .start = igmp_mcf_seq_start,
2991 .next = igmp_mcf_seq_next,
2992 .stop = igmp_mcf_seq_stop,
2993 .show = igmp_mcf_seq_show,
2994};
2995
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002996static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002997{
2998 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002999 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003000
Christoph Hellwigc3506372018-04-10 19:42:55 +02003001 pde = proc_create_net("igmp", 0444, net->proc_net, &igmp_mc_seq_ops,
3002 sizeof(struct igmp_mc_iter_state));
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003003 if (!pde)
3004 goto out_igmp;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003005 pde = proc_create_net("mcfilter", 0444, net->proc_net,
3006 &igmp_mcf_seq_ops, sizeof(struct igmp_mcf_iter_state));
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003007 if (!pde)
3008 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08003009 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3010 SOCK_DGRAM, 0, net);
3011 if (err < 0) {
3012 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3013 err);
3014 goto out_sock;
3015 }
3016
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003017 return 0;
3018
Madhu Challa93a714d2015-02-25 09:58:35 -08003019out_sock:
3020 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003021out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00003022 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003023out_igmp:
3024 return -ENOMEM;
3025}
3026
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003027static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003028{
Gao fengece31ff2013-02-18 01:34:56 +00003029 remove_proc_entry("mcfilter", net->proc_net);
3030 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08003031 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003032}
3033
3034static struct pernet_operations igmp_net_ops = {
3035 .init = igmp_net_init,
3036 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037};
WANG Cong72c1d3b2014-01-10 16:09:45 -08003038#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003040static int igmp_netdev_event(struct notifier_block *this,
3041 unsigned long event, void *ptr)
3042{
3043 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3044 struct in_device *in_dev;
3045
3046 switch (event) {
3047 case NETDEV_RESEND_IGMP:
3048 in_dev = __in_dev_get_rtnl(dev);
3049 if (in_dev)
3050 ip_mc_rejoin_groups(in_dev);
3051 break;
3052 default:
3053 break;
3054 }
3055 return NOTIFY_DONE;
3056}
3057
3058static struct notifier_block igmp_notifier = {
3059 .notifier_call = igmp_netdev_event,
3060};
3061
WANG Cong72c1d3b2014-01-10 16:09:45 -08003062int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003064#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003065 int err;
3066
3067 err = register_pernet_subsys(&igmp_net_ops);
3068 if (err)
3069 return err;
3070 err = register_netdevice_notifier(&igmp_notifier);
3071 if (err)
3072 goto reg_notif_fail;
3073 return 0;
3074
3075reg_notif_fail:
3076 unregister_pernet_subsys(&igmp_net_ops);
3077 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003078#else
3079 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003081}