blob: 02c1736c0b8977d87e47b2fb6520a89369be4106 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux NET3: Internet Group Management Protocol [IGMP]
3 *
4 * This code implements the IGMP protocol as defined in RFC1112. There has
5 * been a further revision of this protocol since which is now supported.
6 *
7 * If you have trouble with this module be careful what gcc you have used,
8 * the older version didn't come out right using gcc 2.5.8, the newer one
9 * seems to fall out with gcc 2.6.2.
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Authors:
Alan Cox113aa832008-10-13 19:01:08 -070012 * Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 *
19 * Fixes:
20 *
21 * Alan Cox : Added lots of __inline__ to optimise
22 * the memory usage of all the tiny little
23 * functions.
24 * Alan Cox : Dumped the header building experiment.
25 * Alan Cox : Minor tweaks ready for multicast routing
26 * and extended IGMP protocol.
27 * Alan Cox : Removed a load of inline directives. Gcc 2.5.8
28 * writes utterly bogus code otherwise (sigh)
29 * fixed IGMP loopback to behave in the manner
30 * desired by mrouted, fixed the fact it has been
31 * broken since 1.3.6 and cleaned up a few minor
32 * points.
33 *
34 * Chih-Jen Chang : Tried to revise IGMP to Version 2
35 * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090036 * The enhancements are mainly based on Steve Deering's
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * ipmulti-3.5 source code.
38 * Chih-Jen Chang : Added the igmp_get_mrouter_info and
39 * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of
40 * the mrouted version on that device.
41 * Chih-Jen Chang : Added the max_resp_time parameter to
42 * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter
43 * to identify the multicast router version
44 * and do what the IGMP version 2 specified.
45 * Chih-Jen Chang : Added a timer to revert to IGMP V2 router
46 * Tsu-Sheng Tsao if the specified time expired.
47 * Alan Cox : Stop IGMP from 0.0.0.0 being accepted.
48 * Alan Cox : Use GFP_ATOMIC in the right places.
49 * Christian Daudt : igmp timer wasn't set for local group
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090050 * memberships but was being deleted,
51 * which caused a "del_timer() called
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * from %p with timer not initialized\n"
53 * message (960131).
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +090054 * Christian Daudt : removed del_timer from
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 * igmp_timer_expire function (960205).
56 * Christian Daudt : igmp_heard_report now only calls
57 * igmp_timer_expire if tm->running is
58 * true (960216).
59 * Malcolm Beattie : ttl comparison wrong in igmp_rcv made
60 * igmp_heard_query never trigger. Expiry
61 * miscalculation fixed in igmp_heard_query
62 * and random() made to return unsigned to
63 * prevent negative expiry times.
64 * Alexey Kuznetsov: Wrong group leaving behaviour, backport
65 * fix from pending 2.1.x patches.
66 * Alan Cox: Forget to enable FDDI support earlier.
67 * Alexey Kuznetsov: Fixed leaving groups on device down.
68 * Alexey Kuznetsov: Accordance to igmp-v2-06 draft.
69 * David L Stevens: IGMPv3 support, with help from
70 * Vinay Kulkarni
71 */
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090074#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#include <linux/types.h>
77#include <linux/kernel.h>
78#include <linux/jiffies.h>
79#include <linux/string.h>
80#include <linux/socket.h>
81#include <linux/sockios.h>
82#include <linux/in.h>
83#include <linux/inet.h>
84#include <linux/netdevice.h>
85#include <linux/skbuff.h>
86#include <linux/inetdevice.h>
87#include <linux/igmp.h>
88#include <linux/if_arp.h>
89#include <linux/rtnetlink.h>
90#include <linux/times.h>
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +020091#include <linux/pkt_sched.h>
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -080092#include <linux/byteorder/generic.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020093
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020094#include <net/net_namespace.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020095#include <net/arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <net/ip.h>
97#include <net/protocol.h>
98#include <net/route.h>
99#include <net/sock.h>
100#include <net/checksum.h>
Madhu Challa93a714d2015-02-25 09:58:35 -0800101#include <net/inet_common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102#include <linux/netfilter_ipv4.h>
103#ifdef CONFIG_IP_MROUTE
104#include <linux/mroute.h>
105#endif
106#ifdef CONFIG_PROC_FS
107#include <linux/proc_fs.h>
108#include <linux/seq_file.h>
109#endif
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#ifdef CONFIG_IP_MULTICAST
112/* Parameter names and values are taken from igmp-v2-06 draft */
113
Fabian Frederick436f7c22014-11-04 20:52:14 +0100114#define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ)
115#define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ)
116#define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ)
117#define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ)
118#define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ)
119#define IGMP_QUERY_ROBUSTNESS_VARIABLE 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121
Fabian Frederick436f7c22014-11-04 20:52:14 +0100122#define IGMP_INITIAL_REPORT_DELAY (1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Fabian Frederick436f7c22014-11-04 20:52:14 +0100124/* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 * IGMP specs require to report membership immediately after
126 * joining a group, but we delay the first report by a
127 * small interval. It seems more natural and still does not
128 * contradict to specs provided this delay is small enough.
129 */
130
Herbert Xu42f811b2007-06-04 23:34:44 -0700131#define IGMP_V1_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900132 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700133 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \
134 ((in_dev)->mr_v1_seen && \
135 time_before(jiffies, (in_dev)->mr_v1_seen)))
136#define IGMP_V2_SEEN(in_dev) \
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900137 (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \
Herbert Xu42f811b2007-06-04 23:34:44 -0700138 IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \
139 ((in_dev)->mr_v2_seen && \
140 time_before(jiffies, (in_dev)->mr_v2_seen)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
William Manleycab70042013-08-06 19:03:13 +0100142static int unsolicited_report_interval(struct in_device *in_dev)
143{
William Manley26900482013-08-06 19:03:15 +0100144 int interval_ms, interval_jiffies;
145
William Manleycab70042013-08-06 19:03:13 +0100146 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
William Manley26900482013-08-06 19:03:15 +0100147 interval_ms = IN_DEV_CONF_GET(
148 in_dev,
149 IGMPV2_UNSOLICITED_REPORT_INTERVAL);
William Manleycab70042013-08-06 19:03:13 +0100150 else /* v3 */
William Manley26900482013-08-06 19:03:15 +0100151 interval_ms = IN_DEV_CONF_GET(
152 in_dev,
153 IGMPV3_UNSOLICITED_REPORT_INTERVAL);
154
155 interval_jiffies = msecs_to_jiffies(interval_ms);
156
157 /* _timer functions can't handle a delay of 0 jiffies so ensure
158 * we always return a positive value.
159 */
160 if (interval_jiffies <= 0)
161 interval_jiffies = 1;
162 return interval_jiffies;
William Manleycab70042013-08-06 19:03:13 +0100163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Hangbin Liu24803f32016-11-14 16:16:28 +0800166static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void igmpv3_clear_delrec(struct in_device *in_dev);
168static int sf_setstate(struct ip_mc_list *pmc);
169static void sf_markstate(struct ip_mc_list *pmc);
170#endif
171static void ip_mc_clear_src(struct ip_mc_list *pmc);
Al Viro8f935bb2006-09-27 18:30:07 -0700172static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
173 int sfcount, __be32 *psfsrc, int delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175static void ip_ma_put(struct ip_mc_list *im)
176{
177 if (atomic_dec_and_test(&im->refcnt)) {
178 in_dev_put(im->interface);
Lai Jiangshan42ea299d2011-03-18 11:44:08 +0800179 kfree_rcu(im, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181}
182
David S. Millerd9aa9382010-11-15 08:52:02 -0800183#define for_each_pmc_rcu(in_dev, pmc) \
184 for (pmc = rcu_dereference(in_dev->mc_list); \
185 pmc != NULL; \
186 pmc = rcu_dereference(pmc->next_rcu))
187
188#define for_each_pmc_rtnl(in_dev, pmc) \
189 for (pmc = rtnl_dereference(in_dev->mc_list); \
190 pmc != NULL; \
191 pmc = rtnl_dereference(pmc->next_rcu))
192
Eric Dumazet2113c722019-05-22 18:35:16 -0700193static void ip_sf_list_clear_all(struct ip_sf_list *psf)
194{
195 struct ip_sf_list *next;
196
197 while (psf) {
198 next = psf->sf_next;
199 kfree(psf);
200 psf = next;
201 }
202}
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204#ifdef CONFIG_IP_MULTICAST
205
206/*
207 * Timer management
208 */
209
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000210static void igmp_stop_timer(struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 spin_lock_bh(&im->lock);
213 if (del_timer(&im->timer))
214 atomic_dec(&im->refcnt);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800215 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 im->reporter = 0;
217 im->unsolicit_count = 0;
218 spin_unlock_bh(&im->lock);
219}
220
221/* It must be called with locked im->lock */
222static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
223{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500224 int tv = prandom_u32() % max_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800226 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (!mod_timer(&im->timer, jiffies+tv+2))
228 atomic_inc(&im->refcnt);
229}
230
231static void igmp_gq_start_timer(struct in_device *in_dev)
232{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500233 int tv = prandom_u32() % in_dev->mr_maxdelay;
Michal Tesarfe1e13c2017-01-02 14:38:36 +0100234 unsigned long exp = jiffies + tv + 2;
235
236 if (in_dev->mr_gq_running &&
237 time_after_eq(exp, (in_dev->mr_gq_timer).expires))
238 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 in_dev->mr_gq_running = 1;
Michal Tesarfe1e13c2017-01-02 14:38:36 +0100241 if (!mod_timer(&in_dev->mr_gq_timer, exp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 in_dev_hold(in_dev);
243}
244
245static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
246{
Aruna-Hewapathirane63862b52014-01-11 07:15:59 -0500247 int tv = prandom_u32() % delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
250 in_dev_hold(in_dev);
251}
252
253static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
254{
255 spin_lock_bh(&im->lock);
256 im->unsolicit_count = 0;
257 if (del_timer(&im->timer)) {
258 if ((long)(im->timer.expires-jiffies) < max_delay) {
259 add_timer(&im->timer);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800260 im->tm_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 spin_unlock_bh(&im->lock);
262 return;
263 }
264 atomic_dec(&im->refcnt);
265 }
266 igmp_start_timer(im, max_delay);
267 spin_unlock_bh(&im->lock);
268}
269
270
271/*
272 * Send an IGMP report.
273 */
274
275#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
276
277
278static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
279 int gdeleted, int sdeleted)
280{
281 switch (type) {
282 case IGMPV3_MODE_IS_INCLUDE:
283 case IGMPV3_MODE_IS_EXCLUDE:
284 if (gdeleted || sdeleted)
285 return 0;
David L Stevensad125832006-01-18 14:20:56 -0800286 if (!(pmc->gsquery && !psf->sf_gsresp)) {
287 if (pmc->sfmode == MCAST_INCLUDE)
288 return 1;
289 /* don't include if this source is excluded
290 * in all filters
291 */
292 if (psf->sf_count[MCAST_INCLUDE])
293 return type == IGMPV3_MODE_IS_INCLUDE;
294 return pmc->sfcount[MCAST_EXCLUDE] ==
295 psf->sf_count[MCAST_EXCLUDE];
296 }
297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case IGMPV3_CHANGE_TO_INCLUDE:
299 if (gdeleted || sdeleted)
300 return 0;
301 return psf->sf_count[MCAST_INCLUDE] != 0;
302 case IGMPV3_CHANGE_TO_EXCLUDE:
303 if (gdeleted || sdeleted)
304 return 0;
305 if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
306 psf->sf_count[MCAST_INCLUDE])
307 return 0;
308 return pmc->sfcount[MCAST_EXCLUDE] ==
309 psf->sf_count[MCAST_EXCLUDE];
310 case IGMPV3_ALLOW_NEW_SOURCES:
311 if (gdeleted || !psf->sf_crcount)
312 return 0;
313 return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
314 case IGMPV3_BLOCK_OLD_SOURCES:
315 if (pmc->sfmode == MCAST_INCLUDE)
316 return gdeleted || (psf->sf_crcount && sdeleted);
317 return psf->sf_crcount && !gdeleted && !sdeleted;
318 }
319 return 0;
320}
321
322static int
323igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
324{
325 struct ip_sf_list *psf;
326 int scount = 0;
327
Weilong Chenc71151f2013-12-23 14:37:29 +0800328 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
330 continue;
331 scount++;
332 }
333 return scount;
334}
335
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800336/* source address selection per RFC 3376 section 4.2.13 */
337static __be32 igmpv3_get_srcaddr(struct net_device *dev,
338 const struct flowi4 *fl4)
339{
340 struct in_device *in_dev = __in_dev_get_rcu(dev);
341
342 if (!in_dev)
343 return htonl(INADDR_ANY);
344
345 for_ifa(in_dev) {
Felix Fietkau0ae16962018-01-19 11:50:46 +0100346 if (fl4->saddr == ifa->ifa_local)
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800347 return fl4->saddr;
348 } endfor_ifa(in_dev);
349
350 return htonl(INADDR_ANY);
351}
352
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100353static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sk_buff *skb;
356 struct rtable *rt;
357 struct iphdr *pip;
358 struct igmpv3_report *pig;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700359 struct net *net = dev_net(dev);
David S. Miller31e45432011-05-03 20:25:42 -0700360 struct flowi4 fl4;
Herbert Xu66088242011-11-18 02:20:04 +0000361 int hlen = LL_RESERVED_SPACE(dev);
362 int tlen = dev->needed_tailroom;
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100363 unsigned int size = mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000365 while (1) {
Herbert Xu66088242011-11-18 02:20:04 +0000366 skb = alloc_skb(size + hlen + tlen,
Eric Dumazet57e1ab62010-11-16 20:36:42 +0000367 GFP_ATOMIC | __GFP_NOWARN);
368 if (skb)
369 break;
370 size >>= 1;
371 if (size < 256)
372 return NULL;
373 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200374 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
David S. Miller31e45432011-05-03 20:25:42 -0700376 rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500377 0, 0,
378 IPPROTO_IGMP, 0, dev->ifindex);
379 if (IS_ERR(rt)) {
380 kfree_skb(skb);
381 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Changli Gaod8d1f302010-06-10 23:31:35 -0700384 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 skb->dev = dev;
386
Herbert Xu66088242011-11-18 02:20:04 +0000387 skb_reserve(skb, hlen);
Benjamin Poirier1837b2e2016-02-29 15:03:33 -0800388 skb_tailroom_reserve(skb, mtu, tlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300390 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700391 pip = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300392 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 pip->version = 4;
395 pip->ihl = (sizeof(struct iphdr)+4)>>2;
396 pip->tos = 0xc0;
397 pip->frag_off = htons(IP_DF);
398 pip->ttl = 1;
David S. Miller492f64c2011-05-03 20:53:12 -0700399 pip->daddr = fl4.daddr;
Eric Dumazetce43c072018-02-01 10:26:57 -0800400
401 rcu_read_lock();
Kevin Cernekee2c1a0b22017-12-11 11:13:45 -0800402 pip->saddr = igmpv3_get_srcaddr(dev, &fl4);
Eric Dumazetce43c072018-02-01 10:26:57 -0800403 rcu_read_unlock();
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 pip->protocol = IPPROTO_IGMP;
406 pip->tot_len = 0; /* filled in later */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100407 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000408 ((u8 *)&pip[1])[0] = IPOPT_RA;
409 ((u8 *)&pip[1])[1] = 4;
410 ((u8 *)&pip[1])[2] = 0;
411 ((u8 *)&pip[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700413 skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4;
Arnaldo Carvalho de Melod10ba342007-03-14 21:05:37 -0300414 skb_put(skb, sizeof(*pig));
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300415 pig = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
417 pig->resv1 = 0;
418 pig->csum = 0;
419 pig->resv2 = 0;
420 pig->ngrec = 0;
421 return skb;
422}
423
424static int igmpv3_sendpack(struct sk_buff *skb)
425{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300426 struct igmphdr *pig = igmp_hdr(skb);
Simon Hormanf7c0c2a2013-05-28 20:34:27 +0000427 const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300429 pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Eric W. Biederman33224b12015-10-07 16:48:46 -0500431 return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
435{
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800436 return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800440 int type, struct igmpv3_grec **ppgr, unsigned int mtu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct net_device *dev = pmc->interface->dev;
443 struct igmpv3_report *pih;
444 struct igmpv3_grec *pgr;
445
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800446 if (!skb) {
447 skb = igmpv3_newpack(dev, mtu);
448 if (!skb)
449 return NULL;
450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
452 pgr->grec_type = type;
453 pgr->grec_auxwords = 0;
454 pgr->grec_nsrcs = 0;
455 pgr->grec_mca = pmc->multiaddr;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300456 pih = igmpv3_report_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 pih->ngrec = htons(ntohs(pih->ngrec)+1);
458 *ppgr = pgr;
459 return skb;
460}
461
Daniel Borkmann4c672e42014-11-05 20:27:38 +0100462#define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
465 int type, int gdeleted, int sdeleted)
466{
467 struct net_device *dev = pmc->interface->dev;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200468 struct net *net = dev_net(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct igmpv3_report *pih;
470 struct igmpv3_grec *pgr = NULL;
471 struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
David L Stevensad125832006-01-18 14:20:56 -0800472 int scount, stotal, first, isquery, truncate;
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800473 unsigned int mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 if (pmc->multiaddr == IGMP_ALL_HOSTS)
476 return skb;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200477 if (ipv4_is_local_multicast(pmc->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100478 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800480 mtu = READ_ONCE(dev->mtu);
481 if (mtu < IPV4_MIN_MTU)
482 return skb;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 isquery = type == IGMPV3_MODE_IS_INCLUDE ||
485 type == IGMPV3_MODE_IS_EXCLUDE;
486 truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
487 type == IGMPV3_CHANGE_TO_EXCLUDE;
488
David L Stevensad125832006-01-18 14:20:56 -0800489 stotal = scount = 0;
490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
492
David L Stevensad125832006-01-18 14:20:56 -0800493 if (!*psf_list)
494 goto empty_source;
495
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300496 pih = skb ? igmpv3_report_hdr(skb) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* EX and TO_EX get a fresh packet, if needed */
499 if (truncate) {
500 if (pih && pih->ngrec &&
501 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
502 if (skb)
503 igmpv3_sendpack(skb);
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800504 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
506 }
507 first = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800509 for (psf = *psf_list; psf; psf = psf_next) {
Al Viroea4d9e72006-09-27 18:30:52 -0700510 __be32 *psrc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 psf_next = psf->sf_next;
513
514 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
515 psf_prev = psf;
516 continue;
517 }
518
Hangbin Liua0525172016-08-02 18:02:57 +0800519 /* Based on RFC3376 5.1. Should not send source-list change
520 * records when there is a filter mode change.
521 */
522 if (((gdeleted && pmc->sfmode == MCAST_EXCLUDE) ||
523 (!gdeleted && pmc->crcount)) &&
524 (type == IGMPV3_ALLOW_NEW_SOURCES ||
525 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount)
526 goto decrease_sf_crcount;
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 /* clear marks on query responses */
529 if (isquery)
530 psf->sf_gsresp = 0;
531
Al Viro63007722006-09-27 18:31:32 -0700532 if (AVAILABLE(skb) < sizeof(__be32) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 first*sizeof(struct igmpv3_grec)) {
534 if (truncate && !first)
535 break; /* truncate these */
536 if (pgr)
537 pgr->grec_nsrcs = htons(scount);
538 if (skb)
539 igmpv3_sendpack(skb);
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800540 skb = igmpv3_newpack(dev, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 first = 1;
542 scount = 0;
543 }
544 if (first) {
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800545 skb = add_grhead(skb, pmc, type, &pgr, mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 first = 0;
547 }
Alexey Dobriyancc63f702007-02-06 14:35:25 -0800548 if (!skb)
549 return NULL;
Al Viro63007722006-09-27 18:31:32 -0700550 psrc = (__be32 *)skb_put(skb, sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *psrc = psf->sf_inaddr;
David L Stevensad125832006-01-18 14:20:56 -0800552 scount++; stotal++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
554 type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
Hangbin Liua0525172016-08-02 18:02:57 +0800555decrease_sf_crcount:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 psf->sf_crcount--;
557 if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
558 if (psf_prev)
559 psf_prev->sf_next = psf->sf_next;
560 else
561 *psf_list = psf->sf_next;
562 kfree(psf);
563 continue;
564 }
565 }
566 psf_prev = psf;
567 }
David L Stevensad125832006-01-18 14:20:56 -0800568
569empty_source:
570 if (!stotal) {
571 if (type == IGMPV3_ALLOW_NEW_SOURCES ||
572 type == IGMPV3_BLOCK_OLD_SOURCES)
573 return skb;
574 if (pmc->crcount || isquery) {
575 /* make sure we have room for group header */
Weilong Chenc71151f2013-12-23 14:37:29 +0800576 if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) {
David L Stevensad125832006-01-18 14:20:56 -0800577 igmpv3_sendpack(skb);
578 skb = NULL; /* add_grhead will get a new one */
579 }
Eric Dumazetc2f78bf2017-12-11 07:17:39 -0800580 skb = add_grhead(skb, pmc, type, &pgr, mtu);
David L Stevensad125832006-01-18 14:20:56 -0800581 }
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (pgr)
584 pgr->grec_nsrcs = htons(scount);
585
586 if (isquery)
587 pmc->gsquery = 0; /* clear query state on report */
588 return skb;
589}
590
591static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
592{
593 struct sk_buff *skb = NULL;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200594 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 int type;
596
597 if (!pmc) {
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000598 rcu_read_lock();
599 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (pmc->multiaddr == IGMP_ALL_HOSTS)
601 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100602 if (ipv4_is_local_multicast(pmc->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200603 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100604 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 spin_lock_bh(&pmc->lock);
606 if (pmc->sfcount[MCAST_EXCLUDE])
607 type = IGMPV3_MODE_IS_EXCLUDE;
608 else
609 type = IGMPV3_MODE_IS_INCLUDE;
610 skb = add_grec(skb, pmc, type, 0, 0);
611 spin_unlock_bh(&pmc->lock);
612 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000613 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 } else {
615 spin_lock_bh(&pmc->lock);
616 if (pmc->sfcount[MCAST_EXCLUDE])
617 type = IGMPV3_MODE_IS_EXCLUDE;
618 else
619 type = IGMPV3_MODE_IS_INCLUDE;
620 skb = add_grec(skb, pmc, type, 0, 0);
621 spin_unlock_bh(&pmc->lock);
622 }
623 if (!skb)
624 return 0;
625 return igmpv3_sendpack(skb);
626}
627
628/*
629 * remove zero-count source records from a source filter list
630 */
631static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
632{
633 struct ip_sf_list *psf_prev, *psf_next, *psf;
634
635 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800636 for (psf = *ppsf; psf; psf = psf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 psf_next = psf->sf_next;
638 if (psf->sf_crcount == 0) {
639 if (psf_prev)
640 psf_prev->sf_next = psf->sf_next;
641 else
642 *ppsf = psf->sf_next;
643 kfree(psf);
644 } else
645 psf_prev = psf;
646 }
647}
648
Eric Dumazet405d3bb2019-05-22 16:51:22 -0700649static void kfree_pmc(struct ip_mc_list *pmc)
650{
651 ip_sf_list_clear_all(pmc->sources);
652 ip_sf_list_clear_all(pmc->tomb);
653 kfree(pmc);
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656static void igmpv3_send_cr(struct in_device *in_dev)
657{
658 struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
659 struct sk_buff *skb = NULL;
660 int type, dtype;
661
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000662 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 spin_lock_bh(&in_dev->mc_tomb_lock);
664
665 /* deleted MCA's */
666 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +0800667 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 pmc_next = pmc->next;
669 if (pmc->sfmode == MCAST_INCLUDE) {
670 type = IGMPV3_BLOCK_OLD_SOURCES;
671 dtype = IGMPV3_BLOCK_OLD_SOURCES;
672 skb = add_grec(skb, pmc, type, 1, 0);
673 skb = add_grec(skb, pmc, dtype, 1, 1);
674 }
675 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (pmc->sfmode == MCAST_EXCLUDE) {
677 type = IGMPV3_CHANGE_TO_INCLUDE;
678 skb = add_grec(skb, pmc, type, 1, 0);
679 }
David L Stevensad125832006-01-18 14:20:56 -0800680 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (pmc->crcount == 0) {
682 igmpv3_clear_zeros(&pmc->tomb);
683 igmpv3_clear_zeros(&pmc->sources);
684 }
685 }
686 if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
687 if (pmc_prev)
688 pmc_prev->next = pmc_next;
689 else
690 in_dev->mc_tomb = pmc_next;
691 in_dev_put(pmc->interface);
Eric Dumazet405d3bb2019-05-22 16:51:22 -0700692 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 } else
694 pmc_prev = pmc;
695 }
696 spin_unlock_bh(&in_dev->mc_tomb_lock);
697
698 /* change recs */
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000699 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 spin_lock_bh(&pmc->lock);
701 if (pmc->sfcount[MCAST_EXCLUDE]) {
702 type = IGMPV3_BLOCK_OLD_SOURCES;
703 dtype = IGMPV3_ALLOW_NEW_SOURCES;
704 } else {
705 type = IGMPV3_ALLOW_NEW_SOURCES;
706 dtype = IGMPV3_BLOCK_OLD_SOURCES;
707 }
708 skb = add_grec(skb, pmc, type, 0, 0);
709 skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */
710
711 /* filter mode changes */
712 if (pmc->crcount) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (pmc->sfmode == MCAST_EXCLUDE)
714 type = IGMPV3_CHANGE_TO_EXCLUDE;
715 else
716 type = IGMPV3_CHANGE_TO_INCLUDE;
717 skb = add_grec(skb, pmc, type, 0, 0);
David L Stevensad125832006-01-18 14:20:56 -0800718 pmc->crcount--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720 spin_unlock_bh(&pmc->lock);
721 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000722 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (!skb)
725 return;
726 (void) igmpv3_sendpack(skb);
727}
728
729static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
730 int type)
731{
732 struct sk_buff *skb;
733 struct iphdr *iph;
734 struct igmphdr *ih;
735 struct rtable *rt;
736 struct net_device *dev = in_dev->dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -0700737 struct net *net = dev_net(dev);
Al Viro63007722006-09-27 18:31:32 -0700738 __be32 group = pmc ? pmc->multiaddr : 0;
David S. Miller31e45432011-05-03 20:25:42 -0700739 struct flowi4 fl4;
Al Viro63007722006-09-27 18:31:32 -0700740 __be32 dst;
Herbert Xu66088242011-11-18 02:20:04 +0000741 int hlen, tlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
744 return igmpv3_send_report(in_dev, pmc);
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100745
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200746 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100747 return 0;
748
749 if (type == IGMP_HOST_LEAVE_MESSAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 dst = IGMP_ALL_ROUTER;
751 else
752 dst = group;
753
David S. Miller31e45432011-05-03 20:25:42 -0700754 rt = ip_route_output_ports(net, &fl4, NULL, dst, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -0500755 0, 0,
756 IPPROTO_IGMP, 0, dev->ifindex);
757 if (IS_ERR(rt))
758 return -1;
759
Herbert Xu66088242011-11-18 02:20:04 +0000760 hlen = LL_RESERVED_SPACE(dev);
761 tlen = dev->needed_tailroom;
762 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +0100763 if (!skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 ip_rt_put(rt);
765 return -1;
766 }
Hannes Frederic Sowa9d4a0312013-07-26 17:05:16 +0200767 skb->priority = TC_PRIO_CONTROL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Changli Gaod8d1f302010-06-10 23:31:35 -0700769 skb_dst_set(skb, &rt->dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Herbert Xu66088242011-11-18 02:20:04 +0000771 skb_reserve(skb, hlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300773 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700774 iph = ip_hdr(skb);
Arnaldo Carvalho de Melo7e28ecc2007-03-10 18:40:59 -0300775 skb_put(skb, sizeof(struct iphdr) + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 iph->version = 4;
778 iph->ihl = (sizeof(struct iphdr)+4)>>2;
779 iph->tos = 0xc0;
780 iph->frag_off = htons(IP_DF);
781 iph->ttl = 1;
782 iph->daddr = dst;
David S. Miller492f64c2011-05-03 20:53:12 -0700783 iph->saddr = fl4.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 iph->protocol = IPPROTO_IGMP;
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +0100785 ip_select_ident(net, skb, NULL);
Daniel Baluta5e73ea12012-04-15 01:34:41 +0000786 ((u8 *)&iph[1])[0] = IPOPT_RA;
787 ((u8 *)&iph[1])[1] = 4;
788 ((u8 *)&iph[1])[2] = 0;
789 ((u8 *)&iph[1])[3] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800792 ih->type = type;
793 ih->code = 0;
794 ih->csum = 0;
795 ih->group = group;
796 ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Eric W. Biederman33224b12015-10-07 16:48:46 -0500798 return ip_local_out(net, skb->sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
801static void igmp_gq_timer_expire(unsigned long data)
802{
803 struct in_device *in_dev = (struct in_device *)data;
804
805 in_dev->mr_gq_running = 0;
806 igmpv3_send_report(in_dev, NULL);
Salam Noureddinee2401652013-09-29 13:39:42 -0700807 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810static void igmp_ifc_timer_expire(unsigned long data)
811{
812 struct in_device *in_dev = (struct in_device *)data;
813
814 igmpv3_send_cr(in_dev);
815 if (in_dev->mr_ifc_count) {
816 in_dev->mr_ifc_count--;
William Manleycab70042013-08-06 19:03:13 +0100817 igmp_ifc_start_timer(in_dev,
818 unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 }
Salam Noureddinee2401652013-09-29 13:39:42 -0700820 in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823static void igmp_ifc_event(struct in_device *in_dev)
824{
Nikolay Borisov165094a2016-02-08 23:29:24 +0200825 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
827 return;
Nikolay Borisov165094a2016-02-08 23:29:24 +0200828 in_dev->mr_ifc_count = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 igmp_ifc_start_timer(in_dev, 1);
830}
831
832
833static void igmp_timer_expire(unsigned long data)
834{
Weilong Chenc71151f2013-12-23 14:37:29 +0800835 struct ip_mc_list *im = (struct ip_mc_list *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct in_device *in_dev = im->interface;
837
838 spin_lock(&im->lock);
Jianjun Konga7e9ff72008-11-03 00:26:09 -0800839 im->tm_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 if (im->unsolicit_count) {
842 im->unsolicit_count--;
William Manleycab70042013-08-06 19:03:13 +0100843 igmp_start_timer(im, unsolicited_report_interval(in_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845 im->reporter = 1;
846 spin_unlock(&im->lock);
847
848 if (IGMP_V1_SEEN(in_dev))
849 igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
850 else if (IGMP_V2_SEEN(in_dev))
851 igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
852 else
853 igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
854
855 ip_ma_put(im);
856}
857
David L Stevensad125832006-01-18 14:20:56 -0800858/* mark EXCLUDE-mode sources */
Al Viroea4d9e72006-09-27 18:30:52 -0700859static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct ip_sf_list *psf;
862 int i, scount;
863
864 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800865 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (scount == nsrcs)
867 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800868 for (i = 0; i < nsrcs; i++) {
David L Stevensad125832006-01-18 14:20:56 -0800869 /* skip inactive filters */
Yan, Zhenge05c4ad32011-08-23 22:54:37 +0000870 if (psf->sf_count[MCAST_INCLUDE] ||
David L Stevensad125832006-01-18 14:20:56 -0800871 pmc->sfcount[MCAST_EXCLUDE] !=
872 psf->sf_count[MCAST_EXCLUDE])
RongQing.Lice713ee2012-04-05 17:36:29 +0800873 break;
David L Stevensad125832006-01-18 14:20:56 -0800874 if (srcs[i] == psf->sf_inaddr) {
875 scount++;
876 break;
877 }
878 }
879 }
880 pmc->gsquery = 0;
881 if (scount == nsrcs) /* all sources excluded */
882 return 0;
883 return 1;
884}
885
Al Viroea4d9e72006-09-27 18:30:52 -0700886static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs)
David L Stevensad125832006-01-18 14:20:56 -0800887{
888 struct ip_sf_list *psf;
889 int i, scount;
890
891 if (pmc->sfmode == MCAST_EXCLUDE)
892 return igmp_xmarksources(pmc, nsrcs, srcs);
893
894 /* mark INCLUDE-mode sources */
895 scount = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +0800896 for (psf = pmc->sources; psf; psf = psf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -0800897 if (scount == nsrcs)
898 break;
Weilong Chenc71151f2013-12-23 14:37:29 +0800899 for (i = 0; i < nsrcs; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (srcs[i] == psf->sf_inaddr) {
901 psf->sf_gsresp = 1;
902 scount++;
903 break;
904 }
905 }
David L Stevensad125832006-01-18 14:20:56 -0800906 if (!scount) {
907 pmc->gsquery = 0;
908 return 0;
909 }
910 pmc->gsquery = 1;
911 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Eric Dumazetd679c532012-09-06 20:37:06 +0000914/* return true if packet was dropped */
915static bool igmp_heard_report(struct in_device *in_dev, __be32 group)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 struct ip_mc_list *im;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200918 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 /* Timers are only set for non-local groups */
921
922 if (group == IGMP_ALL_HOSTS)
Eric Dumazetd679c532012-09-06 20:37:06 +0000923 return false;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200924 if (ipv4_is_local_multicast(group) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +0100925 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000927 rcu_read_lock();
928 for_each_pmc_rcu(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (im->multiaddr == group) {
930 igmp_stop_timer(im);
931 break;
932 }
933 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +0000934 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +0000935 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
Eric Dumazetd679c532012-09-06 20:37:06 +0000938/* return true if packet was dropped */
939static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 int len)
941{
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300942 struct igmphdr *ih = igmp_hdr(skb);
943 struct igmpv3_query *ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct ip_mc_list *im;
Al Viro63007722006-09-27 18:31:32 -0700945 __be32 group = ih->group;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 int max_delay;
947 int mark = 0;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +0200948 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950
David Stevens5b7c8402010-09-30 14:29:40 +0000951 if (len == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (ih->code == 0) {
953 /* Alas, old v1 router presents here. */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900954
Fabian Frederick436f7c22014-11-04 20:52:14 +0100955 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 in_dev->mr_v1_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100957 IGMP_V1_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 group = 0;
959 } else {
960 /* v2 router present */
961 max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
962 in_dev->mr_v2_seen = jiffies +
Fabian Frederick436f7c22014-11-04 20:52:14 +0100963 IGMP_V2_ROUTER_PRESENT_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 }
965 /* cancel the interface change timer */
966 in_dev->mr_ifc_count = 0;
967 if (del_timer(&in_dev->mr_ifc_timer))
968 __in_dev_put(in_dev);
969 /* clear deleted report items */
970 igmpv3_clear_delrec(in_dev);
971 } else if (len < 12) {
Eric Dumazetd679c532012-09-06 20:37:06 +0000972 return true; /* ignore bogus packet; freed by caller */
David Stevens5b7c8402010-09-30 14:29:40 +0000973 } else if (IGMP_V1_SEEN(in_dev)) {
974 /* This is a v3 query with v1 queriers present */
Fabian Frederick436f7c22014-11-04 20:52:14 +0100975 max_delay = IGMP_QUERY_RESPONSE_INTERVAL;
David Stevens5b7c8402010-09-30 14:29:40 +0000976 group = 0;
977 } else if (IGMP_V2_SEEN(in_dev)) {
978 /* this is a v3 query with v2 queriers present;
979 * Interpretation of the max_delay code is problematic here.
980 * A real v2 host would use ih_code directly, while v3 has a
981 * different encoding. We use the v3 encoding as more likely
982 * to be intended in a v3 query.
983 */
984 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
Ben Hutchingsa8c1f652012-01-09 14:06:46 -0800985 if (!max_delay)
986 max_delay = 1; /* can't mod w/ 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 } else { /* v3 */
988 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000989 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900990
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300991 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (ih3->nsrcs) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900993 if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)
Al Viro63007722006-09-27 18:31:32 -0700994 + ntohs(ih3->nsrcs)*sizeof(__be32)))
Eric Dumazetd679c532012-09-06 20:37:06 +0000995 return true;
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -0300996 ih3 = igmpv3_query_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
998
999 max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
1000 if (!max_delay)
1001 max_delay = 1; /* can't mod w/ 0 */
1002 in_dev->mr_maxdelay = max_delay;
1003 if (ih3->qrv)
1004 in_dev->mr_qrv = ih3->qrv;
1005 if (!group) { /* general query */
1006 if (ih3->nsrcs)
Daniel Borkmannb47bd8d2014-10-05 17:27:50 +02001007 return true; /* no sources allowed */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 igmp_gq_start_timer(in_dev);
Eric Dumazetd679c532012-09-06 20:37:06 +00001009 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 /* mark sources to include, if group & source-specific */
1012 mark = ih3->nsrcs != 0;
1013 }
1014
1015 /*
1016 * - Start the timers in all of our membership records
1017 * that the query applies to for the interface on
1018 * which the query arrived excl. those that belong
1019 * to a "local" group (224.0.0.X)
1020 * - For timers already running check if they need to
1021 * be reset.
1022 * - Use the igmp->igmp_code field as the maximum
1023 * delay possible
1024 */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001025 rcu_read_lock();
1026 for_each_pmc_rcu(in_dev, im) {
David L Stevensad125832006-01-18 14:20:56 -08001027 int changed;
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (group && group != im->multiaddr)
1030 continue;
1031 if (im->multiaddr == IGMP_ALL_HOSTS)
1032 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001033 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001034 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001035 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 spin_lock_bh(&im->lock);
1037 if (im->tm_running)
1038 im->gsquery = im->gsquery && mark;
1039 else
1040 im->gsquery = mark;
David L Stevensad125832006-01-18 14:20:56 -08001041 changed = !im->gsquery ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001042 igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 spin_unlock_bh(&im->lock);
David L Stevensad125832006-01-18 14:20:56 -08001044 if (changed)
1045 igmp_mod_timer(im, max_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001047 rcu_read_unlock();
Eric Dumazetd679c532012-09-06 20:37:06 +00001048 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001051/* called in rcu_read_lock() section */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052int igmp_rcv(struct sk_buff *skb)
1053{
1054 /* This basically follows the spec line by line -- see RFC1112 */
1055 struct igmphdr *ih;
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001056 struct in_device *in_dev = __in_dev_get_rcu(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int len = skb->len;
Eric Dumazetd679c532012-09-06 20:37:06 +00001058 bool dropped = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Ian Morris51456b22015-04-03 09:17:26 +01001060 if (!in_dev)
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001061 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Herbert Xufb286bb2005-11-10 13:01:24 -08001063 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
Eric Dumazet9a57a9d2010-06-07 03:17:10 +00001064 goto drop;
Herbert Xufb286bb2005-11-10 13:01:24 -08001065
Tom Herbertde08dc12014-05-07 16:52:10 -07001066 if (skb_checksum_simple_validate(skb))
1067 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001069 ih = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 switch (ih->type) {
1071 case IGMP_HOST_MEMBERSHIP_QUERY:
Eric Dumazetd679c532012-09-06 20:37:06 +00001072 dropped = igmp_heard_query(in_dev, skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 break;
1074 case IGMP_HOST_MEMBERSHIP_REPORT:
1075 case IGMPV2_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 /* Is it our report looped back? */
David S. Millerc7537962010-11-11 17:07:48 -08001077 if (rt_is_output_route(skb_rtable(skb)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 break;
David Stevens24c69272005-12-02 20:32:59 -08001079 /* don't rely on MC router hearing unicast reports */
1080 if (skb->pkt_type == PACKET_MULTICAST ||
1081 skb->pkt_type == PACKET_BROADCAST)
Eric Dumazetd679c532012-09-06 20:37:06 +00001082 dropped = igmp_heard_report(in_dev, ih->group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
1084 case IGMP_PIM:
1085#ifdef CONFIG_IP_PIMSM_V1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return pim_rcv_v1(skb);
1087#endif
Herbert Xuc6b471e2010-02-07 17:26:30 +00001088 case IGMPV3_HOST_MEMBERSHIP_REPORT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 case IGMP_DVMRP:
1090 case IGMP_TRACE:
1091 case IGMP_HOST_LEAVE_MESSAGE:
1092 case IGMP_MTRACE:
1093 case IGMP_MTRACE_RESP:
1094 break;
1095 default:
Linus Torvaldsdd1c1852006-01-31 13:11:41 -08001096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001098
Denis V. Lunevcd557bc2008-02-09 23:22:26 -08001099drop:
Eric Dumazetd679c532012-09-06 20:37:06 +00001100 if (dropped)
1101 kfree_skb(skb);
1102 else
1103 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return 0;
1105}
1106
1107#endif
1108
1109
1110/*
1111 * Add a filter to a device
1112 */
1113
Al Viro63007722006-09-27 18:31:32 -07001114static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 char buf[MAX_ADDR_LEN];
1117 struct net_device *dev = in_dev->dev;
1118
1119 /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
1120 We will get multicast token leakage, when IFF_MULTICAST
Jiri Pirkob81693d2011-08-16 06:29:02 +00001121 is changed. This check should be done in ndo_set_rx_mode
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 routine. Something sort of:
1123 if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
1124 --ANK
1125 */
1126 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001127 dev_mc_add(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130/*
1131 * Remove a filter from a device
1132 */
1133
Al Viro63007722006-09-27 18:31:32 -07001134static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 char buf[MAX_ADDR_LEN];
1137 struct net_device *dev = in_dev->dev;
1138
1139 if (arp_mc_map(addr, buf, dev, 0) == 0)
Jiri Pirko22bedad32010-04-01 21:22:57 +00001140 dev_mc_del(dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143#ifdef CONFIG_IP_MULTICAST
1144/*
1145 * deleted ip_mc_list manipulation
1146 */
1147static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
1148{
1149 struct ip_mc_list *pmc;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001150 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 /* this is an "ip_mc_list" for convenience; only the fields below
1153 * are actually used. In particular, the refcnt and users are not
1154 * used for management of the delete list. Using the same structure
1155 * for deleted items allows change reports to use common code with
1156 * non-deleted or query-response MCA's.
1157 */
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001158 pmc = kzalloc(sizeof(*pmc), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (!pmc)
1160 return;
WANG Congcac2a9b2017-06-20 10:46:27 -07001161 spin_lock_init(&pmc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 spin_lock_bh(&im->lock);
1163 pmc->interface = im->interface;
1164 in_dev_hold(in_dev);
1165 pmc->multiaddr = im->multiaddr;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001166 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 pmc->sfmode = im->sfmode;
1168 if (pmc->sfmode == MCAST_INCLUDE) {
1169 struct ip_sf_list *psf;
1170
1171 pmc->tomb = im->tomb;
1172 pmc->sources = im->sources;
1173 im->tomb = im->sources = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001174 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 psf->sf_crcount = pmc->crcount;
1176 }
1177 spin_unlock_bh(&im->lock);
1178
1179 spin_lock_bh(&in_dev->mc_tomb_lock);
1180 pmc->next = in_dev->mc_tomb;
1181 in_dev->mc_tomb = pmc;
1182 spin_unlock_bh(&in_dev->mc_tomb_lock);
1183}
1184
Hangbin Liu24803f32016-11-14 16:16:28 +08001185/*
1186 * restore ip_mc_list deleted records
1187 */
1188static void igmpv3_del_delrec(struct in_device *in_dev, struct ip_mc_list *im)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct ip_mc_list *pmc, *pmc_prev;
Hangbin Liu24803f32016-11-14 16:16:28 +08001191 struct ip_sf_list *psf;
1192 struct net *net = dev_net(in_dev->dev);
1193 __be32 multiaddr = im->multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 spin_lock_bh(&in_dev->mc_tomb_lock);
1196 pmc_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001197 for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 if (pmc->multiaddr == multiaddr)
1199 break;
1200 pmc_prev = pmc;
1201 }
1202 if (pmc) {
1203 if (pmc_prev)
1204 pmc_prev->next = pmc->next;
1205 else
1206 in_dev->mc_tomb = pmc->next;
1207 }
1208 spin_unlock_bh(&in_dev->mc_tomb_lock);
Hangbin Liu24803f32016-11-14 16:16:28 +08001209
1210 spin_lock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 if (pmc) {
Hangbin Liu24803f32016-11-14 16:16:28 +08001212 im->interface = pmc->interface;
1213 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Hangbin Liucc403d52018-07-20 14:04:27 +08001214 if (im->sfmode == MCAST_INCLUDE) {
Eric Dumazet84d0edf2019-06-27 01:27:01 -07001215 swap(im->tomb, pmc->tomb);
1216 swap(im->sources, pmc->sources);
Hangbin Liu24803f32016-11-14 16:16:28 +08001217 for (psf = im->sources; psf; psf = psf->sf_next)
1218 psf->sf_crcount = im->crcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220 in_dev_put(pmc->interface);
Eric Dumazet405d3bb2019-05-22 16:51:22 -07001221 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Hangbin Liu24803f32016-11-14 16:16:28 +08001223 spin_unlock_bh(&im->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
Hangbin Liu24803f32016-11-14 16:16:28 +08001226/*
1227 * flush ip_mc_list deleted records
1228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static void igmpv3_clear_delrec(struct in_device *in_dev)
1230{
1231 struct ip_mc_list *pmc, *nextpmc;
1232
1233 spin_lock_bh(&in_dev->mc_tomb_lock);
1234 pmc = in_dev->mc_tomb;
1235 in_dev->mc_tomb = NULL;
1236 spin_unlock_bh(&in_dev->mc_tomb_lock);
1237
1238 for (; pmc; pmc = nextpmc) {
1239 nextpmc = pmc->next;
1240 ip_mc_clear_src(pmc);
1241 in_dev_put(pmc->interface);
Eric Dumazet405d3bb2019-05-22 16:51:22 -07001242 kfree_pmc(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244 /* clear dead sources, too */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001245 rcu_read_lock();
1246 for_each_pmc_rcu(in_dev, pmc) {
Eric Dumazet405d3bb2019-05-22 16:51:22 -07001247 struct ip_sf_list *psf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 spin_lock_bh(&pmc->lock);
1250 psf = pmc->tomb;
1251 pmc->tomb = NULL;
1252 spin_unlock_bh(&pmc->lock);
Eric Dumazet405d3bb2019-05-22 16:51:22 -07001253 ip_sf_list_clear_all(psf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001255 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257#endif
1258
1259static void igmp_group_dropped(struct ip_mc_list *im)
1260{
1261 struct in_device *in_dev = im->interface;
1262#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001263 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int reporter;
1265#endif
1266
1267 if (im->loaded) {
1268 im->loaded = 0;
1269 ip_mc_filter_del(in_dev, im->multiaddr);
1270 }
1271
1272#ifdef CONFIG_IP_MULTICAST
1273 if (im->multiaddr == IGMP_ALL_HOSTS)
1274 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001275 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001276 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 reporter = im->reporter;
1279 igmp_stop_timer(im);
1280
1281 if (!in_dev->dead) {
1282 if (IGMP_V1_SEEN(in_dev))
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001283 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (IGMP_V2_SEEN(in_dev)) {
1285 if (reporter)
1286 igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001287 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289 /* IGMPv3 */
1290 igmpv3_add_delrec(in_dev, im);
1291
1292 igmp_ifc_event(in_dev);
1293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295}
1296
1297static void igmp_group_added(struct ip_mc_list *im)
1298{
1299 struct in_device *in_dev = im->interface;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001300#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001301 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001302#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (im->loaded == 0) {
1305 im->loaded = 1;
1306 ip_mc_filter_add(in_dev, im->multiaddr);
1307 }
1308
1309#ifdef CONFIG_IP_MULTICAST
1310 if (im->multiaddr == IGMP_ALL_HOSTS)
1311 return;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001312 if (ipv4_is_local_multicast(im->multiaddr) && !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001313 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (in_dev->dead)
1316 return;
1317 if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1318 spin_lock_bh(&im->lock);
Fabian Frederick436f7c22014-11-04 20:52:14 +01001319 igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 spin_unlock_bh(&im->lock);
1321 return;
1322 }
1323 /* else, v3 */
1324
Nikolay Borisov165094a2016-02-08 23:29:24 +02001325 im->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 igmp_ifc_event(in_dev);
1327#endif
1328}
1329
1330
1331/*
1332 * Multicast list managers
1333 */
1334
Eric Dumazete9897072013-06-07 08:48:57 -07001335static u32 ip_mc_hash(const struct ip_mc_list *im)
1336{
Eric Dumazetc70eba72013-06-12 14:11:16 -07001337 return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07001338}
1339
1340static void ip_mc_hash_add(struct in_device *in_dev,
1341 struct ip_mc_list *im)
1342{
1343 struct ip_mc_list __rcu **mc_hash;
1344 u32 hash;
1345
1346 mc_hash = rtnl_dereference(in_dev->mc_hash);
1347 if (mc_hash) {
1348 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001349 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001350 rcu_assign_pointer(mc_hash[hash], im);
1351 return;
1352 }
1353
1354 /* do not use a hash table for small number of items */
1355 if (in_dev->mc_count < 4)
1356 return;
1357
1358 mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG,
1359 GFP_KERNEL);
1360 if (!mc_hash)
1361 return;
1362
1363 for_each_pmc_rtnl(in_dev, im) {
1364 hash = ip_mc_hash(im);
Eric Dumazetc70eba72013-06-12 14:11:16 -07001365 im->next_hash = mc_hash[hash];
Eric Dumazete9897072013-06-07 08:48:57 -07001366 RCU_INIT_POINTER(mc_hash[hash], im);
1367 }
1368
1369 rcu_assign_pointer(in_dev->mc_hash, mc_hash);
1370}
1371
1372static void ip_mc_hash_remove(struct in_device *in_dev,
1373 struct ip_mc_list *im)
1374{
1375 struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash);
1376 struct ip_mc_list *aux;
1377
1378 if (!mc_hash)
1379 return;
1380 mc_hash += ip_mc_hash(im);
1381 while ((aux = rtnl_dereference(*mc_hash)) != im)
1382 mc_hash = &aux->next_hash;
1383 *mc_hash = im->next_hash;
1384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387/*
1388 * A socket has joined a multicast group on device dev.
1389 */
1390
Al Viro8f935bb2006-09-27 18:30:07 -07001391void ip_mc_inc_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct ip_mc_list *im;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001394#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001395 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001396#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 ASSERT_RTNL();
1399
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001400 for_each_pmc_rtnl(in_dev, im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 if (im->multiaddr == addr) {
1402 im->users++;
1403 ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0);
1404 goto out;
1405 }
1406 }
1407
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001408 im = kzalloc(sizeof(*im), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (!im)
1410 goto out;
1411
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001412 im->users = 1;
1413 im->interface = in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 in_dev_hold(in_dev);
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001415 im->multiaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 /* initial mode is (EX, empty) */
1417 im->sfmode = MCAST_EXCLUDE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 im->sfcount[MCAST_EXCLUDE] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 atomic_set(&im->refcnt, 1);
1420 spin_lock_init(&im->lock);
1421#ifdef CONFIG_IP_MULTICAST
Himangi Saraogi179542a2014-07-25 01:48:44 +05301422 setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001423 im->unsolicit_count = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001425
1426 im->next_rcu = in_dev->mc_list;
Rami Rosenb8bae412008-10-07 15:34:37 -07001427 in_dev->mc_count++;
Eric Dumazetcf778b02012-01-12 04:41:32 +00001428 rcu_assign_pointer(in_dev->mc_list, im);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001429
Eric Dumazete9897072013-06-07 08:48:57 -07001430 ip_mc_hash_add(in_dev, im);
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432#ifdef CONFIG_IP_MULTICAST
Hangbin Liu24803f32016-11-14 16:16:28 +08001433 igmpv3_del_delrec(in_dev, im);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434#endif
1435 igmp_group_added(im);
1436 if (!in_dev->dead)
1437 ip_rt_multicast_event(in_dev);
1438out:
1439 return;
1440}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001441EXPORT_SYMBOL(ip_mc_inc_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001443static int ip_mc_check_iphdr(struct sk_buff *skb)
1444{
1445 const struct iphdr *iph;
1446 unsigned int len;
1447 unsigned int offset = skb_network_offset(skb) + sizeof(*iph);
1448
1449 if (!pskb_may_pull(skb, offset))
1450 return -EINVAL;
1451
1452 iph = ip_hdr(skb);
1453
1454 if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph))
1455 return -EINVAL;
1456
1457 offset += ip_hdrlen(skb) - sizeof(*iph);
1458
1459 if (!pskb_may_pull(skb, offset))
1460 return -EINVAL;
1461
1462 iph = ip_hdr(skb);
1463
1464 if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl)))
1465 return -EINVAL;
1466
1467 len = skb_network_offset(skb) + ntohs(iph->tot_len);
1468 if (skb->len < len || len < offset)
1469 return -EINVAL;
1470
1471 skb_set_transport_header(skb, offset);
1472
1473 return 0;
1474}
1475
1476static int ip_mc_check_igmp_reportv3(struct sk_buff *skb)
1477{
1478 unsigned int len = skb_transport_offset(skb);
1479
1480 len += sizeof(struct igmpv3_report);
1481
1482 return pskb_may_pull(skb, len) ? 0 : -EINVAL;
1483}
1484
1485static int ip_mc_check_igmp_query(struct sk_buff *skb)
1486{
1487 unsigned int len = skb_transport_offset(skb);
1488
1489 len += sizeof(struct igmphdr);
1490 if (skb->len < len)
1491 return -EINVAL;
1492
1493 /* IGMPv{1,2}? */
1494 if (skb->len != len) {
1495 /* or IGMPv3? */
1496 len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr);
1497 if (skb->len < len || !pskb_may_pull(skb, len))
1498 return -EINVAL;
1499 }
1500
1501 /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer
1502 * all-systems destination addresses (224.0.0.1) for general queries
1503 */
1504 if (!igmp_hdr(skb)->group &&
1505 ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP))
1506 return -EINVAL;
1507
1508 return 0;
1509}
1510
1511static int ip_mc_check_igmp_msg(struct sk_buff *skb)
1512{
1513 switch (igmp_hdr(skb)->type) {
1514 case IGMP_HOST_LEAVE_MESSAGE:
1515 case IGMP_HOST_MEMBERSHIP_REPORT:
1516 case IGMPV2_HOST_MEMBERSHIP_REPORT:
1517 /* fall through */
1518 return 0;
1519 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1520 return ip_mc_check_igmp_reportv3(skb);
1521 case IGMP_HOST_MEMBERSHIP_QUERY:
1522 return ip_mc_check_igmp_query(skb);
1523 default:
1524 return -ENOMSG;
1525 }
1526}
1527
1528static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb)
1529{
1530 return skb_checksum_simple_validate(skb);
1531}
1532
1533static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1534
1535{
1536 struct sk_buff *skb_chk;
1537 unsigned int transport_len;
1538 unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr);
Linus Lüssinga5169932015-08-13 05:54:07 +02001539 int ret = -EINVAL;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001540
1541 transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb);
1542
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001543 skb_chk = skb_checksum_trimmed(skb, transport_len,
1544 ip_mc_validate_checksum);
1545 if (!skb_chk)
Linus Lüssinga5169932015-08-13 05:54:07 +02001546 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001547
Linus Lüssinga5169932015-08-13 05:54:07 +02001548 if (!pskb_may_pull(skb_chk, len))
1549 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001550
1551 ret = ip_mc_check_igmp_msg(skb_chk);
Linus Lüssinga5169932015-08-13 05:54:07 +02001552 if (ret)
1553 goto err;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001554
1555 if (skb_trimmed)
1556 *skb_trimmed = skb_chk;
Linus Lüssinga5169932015-08-13 05:54:07 +02001557 /* free now unneeded clone */
1558 else if (skb_chk != skb)
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001559 kfree_skb(skb_chk);
1560
Linus Lüssinga5169932015-08-13 05:54:07 +02001561 ret = 0;
1562
1563err:
1564 if (ret && skb_chk && skb_chk != skb)
1565 kfree_skb(skb_chk);
1566
1567 return ret;
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001568}
1569
1570/**
1571 * ip_mc_check_igmp - checks whether this is a sane IGMP packet
1572 * @skb: the skb to validate
1573 * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional)
1574 *
1575 * Checks whether an IPv4 packet is a valid IGMP packet. If so sets
Linus Lüssinga5169932015-08-13 05:54:07 +02001576 * skb transport header accordingly and returns zero.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001577 *
1578 * -EINVAL: A broken packet was detected, i.e. it violates some internet
1579 * standard
1580 * -ENOMSG: IP header validation succeeded but it is not an IGMP packet.
1581 * -ENOMEM: A memory allocation failure happened.
1582 *
1583 * Optionally, an skb pointer might be provided via skb_trimmed (or set it
1584 * to NULL): After parsing an IGMP packet successfully it will point to
1585 * an skb which has its tail aligned to the IP packet end. This might
1586 * either be the originally provided skb or a trimmed, cloned version if
1587 * the skb frame had data beyond the IP packet. A cloned skb allows us
1588 * to leave the original skb and its full frame unchanged (which might be
1589 * desirable for layer 2 frame jugglers).
1590 *
Linus Lüssinga5169932015-08-13 05:54:07 +02001591 * Caller needs to set the skb network header and free any returned skb if it
1592 * differs from the provided skb.
Linus Lüssing9afd85c2015-05-02 14:01:07 +02001593 */
1594int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed)
1595{
1596 int ret = ip_mc_check_iphdr(skb);
1597
1598 if (ret < 0)
1599 return ret;
1600
1601 if (ip_hdr(skb)->protocol != IPPROTO_IGMP)
1602 return -ENOMSG;
1603
1604 return __ip_mc_check_igmp(skb, skb_trimmed);
1605}
1606EXPORT_SYMBOL(ip_mc_check_igmp);
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/*
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001609 * Resend IGMP JOIN report; used by netdev notifier.
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001610 */
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001611static void ip_mc_rejoin_groups(struct in_device *in_dev)
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001612{
Geert Uytterhoeven08882662007-03-12 17:02:37 -07001613#ifdef CONFIG_IP_MULTICAST
Eric Dumazet866f3b22010-11-18 09:33:19 -08001614 struct ip_mc_list *im;
1615 int type;
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001616 struct net *net = dev_net(in_dev->dev);
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001617
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02001618 ASSERT_RTNL();
1619
1620 for_each_pmc_rtnl(in_dev, im) {
Eric Dumazet866f3b22010-11-18 09:33:19 -08001621 if (im->multiaddr == IGMP_ALL_HOSTS)
1622 continue;
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001623 if (ipv4_is_local_multicast(im->multiaddr) &&
Nikolay Borisov87a8a2a2016-02-09 00:13:50 +02001624 !net->ipv4.sysctl_igmp_llm_reports)
Philip Downeydf2cf4a2015-08-27 16:46:26 +01001625 continue;
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001626
Eric Dumazet866f3b22010-11-18 09:33:19 -08001627 /* a failover is happening and switches
1628 * must be notified immediately
1629 */
1630 if (IGMP_V1_SEEN(in_dev))
1631 type = IGMP_HOST_MEMBERSHIP_REPORT;
1632 else if (IGMP_V2_SEEN(in_dev))
1633 type = IGMPV2_HOST_MEMBERSHIP_REPORT;
1634 else
1635 type = IGMPV3_HOST_MEMBERSHIP_REPORT;
1636 igmp_send_report(in_dev, im, type);
1637 }
Jay Vosburgha816c7c2007-02-28 17:03:37 -08001638#endif
1639}
1640
1641/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 * A socket has left a multicast group on device dev
1643 */
1644
Al Viro8f935bb2006-09-27 18:30:07 -07001645void ip_mc_dec_group(struct in_device *in_dev, __be32 addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001647 struct ip_mc_list *i;
1648 struct ip_mc_list __rcu **ip;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 ASSERT_RTNL();
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001651
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001652 for (ip = &in_dev->mc_list;
1653 (i = rtnl_dereference(*ip)) != NULL;
1654 ip = &i->next_rcu) {
Jianjun Konga7e9ff72008-11-03 00:26:09 -08001655 if (i->multiaddr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (--i->users == 0) {
Eric Dumazete9897072013-06-07 08:48:57 -07001657 ip_mc_hash_remove(in_dev, i);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001658 *ip = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001659 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 igmp_group_dropped(i);
Veaceslav Falico24cf3af2011-05-23 23:15:05 +00001661 ip_mc_clear_src(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 if (!in_dev->dead)
1664 ip_rt_multicast_event(in_dev);
1665
1666 ip_ma_put(i);
1667 return;
1668 }
1669 break;
1670 }
1671 }
1672}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00001673EXPORT_SYMBOL(ip_mc_dec_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Moni Shoua75c78502009-09-15 02:37:40 -07001675/* Device changing type */
1676
1677void ip_mc_unmap(struct in_device *in_dev)
1678{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001679 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001680
1681 ASSERT_RTNL();
1682
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001683 for_each_pmc_rtnl(in_dev, pmc)
1684 igmp_group_dropped(pmc);
Moni Shoua75c78502009-09-15 02:37:40 -07001685}
1686
1687void ip_mc_remap(struct in_device *in_dev)
1688{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001689 struct ip_mc_list *pmc;
Moni Shoua75c78502009-09-15 02:37:40 -07001690
1691 ASSERT_RTNL();
1692
Hangbin Liu24803f32016-11-14 16:16:28 +08001693 for_each_pmc_rtnl(in_dev, pmc) {
1694#ifdef CONFIG_IP_MULTICAST
1695 igmpv3_del_delrec(in_dev, pmc);
1696#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001697 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001698 }
Moni Shoua75c78502009-09-15 02:37:40 -07001699}
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701/* Device going down */
1702
1703void ip_mc_down(struct in_device *in_dev)
1704{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001705 struct ip_mc_list *pmc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 ASSERT_RTNL();
1708
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001709 for_each_pmc_rtnl(in_dev, pmc)
1710 igmp_group_dropped(pmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712#ifdef CONFIG_IP_MULTICAST
1713 in_dev->mr_ifc_count = 0;
1714 if (del_timer(&in_dev->mr_ifc_timer))
1715 __in_dev_put(in_dev);
1716 in_dev->mr_gq_running = 0;
1717 if (del_timer(&in_dev->mr_gq_timer))
1718 __in_dev_put(in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719#endif
1720
1721 ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1722}
1723
1724void ip_mc_init_dev(struct in_device *in_dev)
1725{
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001726#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001727 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 ASSERT_RTNL();
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731#ifdef CONFIG_IP_MULTICAST
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001732 setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire,
1733 (unsigned long)in_dev);
Pavel Emelyanovb24b8a22008-01-23 21:20:07 -08001734 setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire,
1735 (unsigned long)in_dev);
Nikolay Borisov165094a2016-02-08 23:29:24 +02001736 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737#endif
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 spin_lock_init(&in_dev->mc_tomb_lock);
1740}
1741
1742/* Device going up */
1743
1744void ip_mc_up(struct in_device *in_dev)
1745{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001746 struct ip_mc_list *pmc;
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001747#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001748 struct net *net = dev_net(in_dev->dev);
Nikolay Borisovdcd87992016-02-15 12:11:28 +02001749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 ASSERT_RTNL();
1752
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001753#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001754 in_dev->mr_qrv = net->ipv4.sysctl_igmp_qrv;
Hannes Frederic Sowaa9fe8e22014-09-02 15:49:26 +02001755#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1757
Hangbin Liu24803f32016-11-14 16:16:28 +08001758 for_each_pmc_rtnl(in_dev, pmc) {
1759#ifdef CONFIG_IP_MULTICAST
1760 igmpv3_del_delrec(in_dev, pmc);
1761#endif
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001762 igmp_group_added(pmc);
Hangbin Liu24803f32016-11-14 16:16:28 +08001763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764}
1765
1766/*
1767 * Device is about to be destroyed: clean up.
1768 */
1769
1770void ip_mc_destroy_dev(struct in_device *in_dev)
1771{
1772 struct ip_mc_list *i;
1773
1774 ASSERT_RTNL();
1775
1776 /* Deactivate timers */
1777 ip_mc_down(in_dev);
Hangbin Liu24803f32016-11-14 16:16:28 +08001778#ifdef CONFIG_IP_MULTICAST
1779 igmpv3_clear_delrec(in_dev);
1780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001782 while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) {
1783 in_dev->mc_list = i->next_rcu;
Rami Rosenb8bae412008-10-07 15:34:37 -07001784 in_dev->mc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 ip_ma_put(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001789/* RTNL is locked */
Daniel Lezcano877aced2008-08-13 16:15:57 -07001790static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct net_device *dev = NULL;
1793 struct in_device *idev = NULL;
1794
1795 if (imr->imr_ifindex) {
Daniel Lezcano877aced2008-08-13 16:15:57 -07001796 idev = inetdev_by_index(net, imr->imr_ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return idev;
1798 }
1799 if (imr->imr_address.s_addr) {
Eric Dumazet9e917dc2010-10-19 00:39:18 +00001800 dev = __ip_dev_find(net, imr->imr_address.s_addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (!dev)
1802 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804
David S. Millerb23dd4f2011-03-02 14:31:35 -08001805 if (!dev) {
David S. Miller78fbfd82011-03-12 00:00:52 -05001806 struct rtable *rt = ip_route_output(net,
1807 imr->imr_multiaddr.s_addr,
1808 0, 0, 0);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001809 if (!IS_ERR(rt)) {
1810 dev = rt->dst.dev;
1811 ip_rt_put(rt);
1812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
1814 if (dev) {
1815 imr->imr_ifindex = dev->ifindex;
Herbert Xue5ed6392005-10-03 14:35:55 -07001816 idev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
1818 return idev;
1819}
1820
1821/*
1822 * Join a socket to a group
1823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
1825static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
Al Viro8f935bb2006-09-27 18:30:07 -07001826 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 struct ip_sf_list *psf, *psf_prev;
1829 int rv = 0;
1830
1831 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001832 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (psf->sf_inaddr == *psfsrc)
1834 break;
1835 psf_prev = psf;
1836 }
1837 if (!psf || psf->sf_count[sfmode] == 0) {
1838 /* source filter not found, or count wrong => bug */
1839 return -ESRCH;
1840 }
1841 psf->sf_count[sfmode]--;
1842 if (psf->sf_count[sfmode] == 0) {
1843 ip_rt_multicast_event(pmc->interface);
1844 }
1845 if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1846#ifdef CONFIG_IP_MULTICAST
1847 struct in_device *in_dev = pmc->interface;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001848 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849#endif
1850
1851 /* no more filters for this source */
1852 if (psf_prev)
1853 psf_prev->sf_next = psf->sf_next;
1854 else
1855 pmc->sources = psf->sf_next;
1856#ifdef CONFIG_IP_MULTICAST
1857 if (psf->sf_oldin &&
1858 !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
Nikolay Borisov165094a2016-02-08 23:29:24 +02001859 psf->sf_crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 psf->sf_next = pmc->tomb;
1861 pmc->tomb = psf;
1862 rv = 1;
1863 } else
1864#endif
1865 kfree(psf);
1866 }
1867 return rv;
1868}
1869
1870#ifndef CONFIG_IP_MULTICAST
1871#define igmp_ifc_event(x) do { } while (0)
1872#endif
1873
Al Viro8f935bb2006-09-27 18:30:07 -07001874static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
1875 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 struct ip_mc_list *pmc;
1878 int changerec = 0;
1879 int i, err;
1880
1881 if (!in_dev)
1882 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001883 rcu_read_lock();
1884 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (*pmca == pmc->multiaddr)
1886 break;
1887 }
1888 if (!pmc) {
1889 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001890 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return -ESRCH;
1892 }
1893 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00001894 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895#ifdef CONFIG_IP_MULTICAST
1896 sf_markstate(pmc);
1897#endif
1898 if (!delta) {
1899 err = -EINVAL;
1900 if (!pmc->sfcount[sfmode])
1901 goto out_unlock;
1902 pmc->sfcount[sfmode]--;
1903 }
1904 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001905 for (i = 0; i < sfcount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1907
1908 changerec |= rv > 0;
1909 if (!err && rv < 0)
1910 err = rv;
1911 }
1912 if (pmc->sfmode == MCAST_EXCLUDE &&
1913 pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1914 pmc->sfcount[MCAST_INCLUDE]) {
1915#ifdef CONFIG_IP_MULTICAST
1916 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02001917 struct net *net = dev_net(in_dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918#endif
1919
1920 /* filter mode change */
1921 pmc->sfmode = MCAST_INCLUDE;
1922#ifdef CONFIG_IP_MULTICAST
Nikolay Borisov165094a2016-02-08 23:29:24 +02001923 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08001925 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 psf->sf_crcount = 0;
1927 igmp_ifc_event(pmc->interface);
1928 } else if (sf_setstate(pmc) || changerec) {
1929 igmp_ifc_event(pmc->interface);
1930#endif
1931 }
1932out_unlock:
1933 spin_unlock_bh(&pmc->lock);
1934 return err;
1935}
1936
1937/*
1938 * Add multicast single-source filter to the interface list
1939 */
1940static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
Jun Zhao5eb81e892011-11-30 06:21:04 +00001941 __be32 *psfsrc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 struct ip_sf_list *psf, *psf_prev;
1944
1945 psf_prev = NULL;
Weilong Chenc71151f2013-12-23 14:37:29 +08001946 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (psf->sf_inaddr == *psfsrc)
1948 break;
1949 psf_prev = psf;
1950 }
1951 if (!psf) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001952 psf = kzalloc(sizeof(*psf), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (!psf)
1954 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 psf->sf_inaddr = *psfsrc;
1956 if (psf_prev) {
1957 psf_prev->sf_next = psf;
1958 } else
1959 pmc->sources = psf;
1960 }
1961 psf->sf_count[sfmode]++;
1962 if (psf->sf_count[sfmode] == 1) {
1963 ip_rt_multicast_event(pmc->interface);
1964 }
1965 return 0;
1966}
1967
1968#ifdef CONFIG_IP_MULTICAST
1969static void sf_markstate(struct ip_mc_list *pmc)
1970{
1971 struct ip_sf_list *psf;
1972 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1973
Weilong Chenc71151f2013-12-23 14:37:29 +08001974 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (pmc->sfcount[MCAST_EXCLUDE]) {
1976 psf->sf_oldin = mca_xcount ==
1977 psf->sf_count[MCAST_EXCLUDE] &&
1978 !psf->sf_count[MCAST_INCLUDE];
1979 } else
1980 psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1981}
1982
1983static int sf_setstate(struct ip_mc_list *pmc)
1984{
David L Stevensad125832006-01-18 14:20:56 -08001985 struct ip_sf_list *psf, *dpsf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1987 int qrv = pmc->interface->mr_qrv;
1988 int new_in, rv;
1989
1990 rv = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08001991 for (psf = pmc->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 if (pmc->sfcount[MCAST_EXCLUDE]) {
1993 new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1994 !psf->sf_count[MCAST_INCLUDE];
1995 } else
1996 new_in = psf->sf_count[MCAST_INCLUDE] != 0;
David L Stevensad125832006-01-18 14:20:56 -08001997 if (new_in) {
1998 if (!psf->sf_oldin) {
Al Viro76edc602006-02-01 05:54:35 -05001999 struct ip_sf_list *prev = NULL;
David L Stevensad125832006-01-18 14:20:56 -08002000
Weilong Chenc71151f2013-12-23 14:37:29 +08002001 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) {
David L Stevensad125832006-01-18 14:20:56 -08002002 if (dpsf->sf_inaddr == psf->sf_inaddr)
2003 break;
2004 prev = dpsf;
2005 }
2006 if (dpsf) {
2007 if (prev)
2008 prev->sf_next = dpsf->sf_next;
2009 else
2010 pmc->tomb = dpsf->sf_next;
2011 kfree(dpsf);
2012 }
2013 psf->sf_crcount = qrv;
2014 rv++;
2015 }
2016 } else if (psf->sf_oldin) {
2017
2018 psf->sf_crcount = 0;
2019 /*
2020 * add or update "delete" records if an active filter
2021 * is now inactive
2022 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002023 for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next)
David L Stevensad125832006-01-18 14:20:56 -08002024 if (dpsf->sf_inaddr == psf->sf_inaddr)
2025 break;
2026 if (!dpsf) {
Joe Perches3ed37a62010-05-31 17:23:21 +00002027 dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC);
David L Stevensad125832006-01-18 14:20:56 -08002028 if (!dpsf)
2029 continue;
2030 *dpsf = *psf;
2031 /* pmc->lock held by callers */
2032 dpsf->sf_next = pmc->tomb;
2033 pmc->tomb = dpsf;
2034 }
2035 dpsf->sf_crcount = qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 rv++;
2037 }
2038 }
2039 return rv;
2040}
2041#endif
2042
2043/*
2044 * Add multicast source filter list to the interface list
2045 */
Al Viro8f935bb2006-09-27 18:30:07 -07002046static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode,
2047 int sfcount, __be32 *psfsrc, int delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 struct ip_mc_list *pmc;
2050 int isexclude;
2051 int i, err;
2052
2053 if (!in_dev)
2054 return -ENODEV;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002055 rcu_read_lock();
2056 for_each_pmc_rcu(in_dev, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (*pmca == pmc->multiaddr)
2058 break;
2059 }
2060 if (!pmc) {
2061 /* MCA not found?? bug */
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002062 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 return -ESRCH;
2064 }
2065 spin_lock_bh(&pmc->lock);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002066 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
2068#ifdef CONFIG_IP_MULTICAST
2069 sf_markstate(pmc);
2070#endif
2071 isexclude = pmc->sfmode == MCAST_EXCLUDE;
2072 if (!delta)
2073 pmc->sfcount[sfmode]++;
2074 err = 0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002075 for (i = 0; i < sfcount; i++) {
Jun Zhao5eb81e892011-11-30 06:21:04 +00002076 err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (err)
2078 break;
2079 }
2080 if (err) {
2081 int j;
2082
Jun Zhao685f94e62011-11-22 17:19:03 +00002083 if (!delta)
2084 pmc->sfcount[sfmode]--;
Weilong Chenc71151f2013-12-23 14:37:29 +08002085 for (j = 0; j < i; j++)
Julia Lawalla1889c02011-07-28 02:46:01 +00002086 (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
2088#ifdef CONFIG_IP_MULTICAST
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct ip_sf_list *psf;
Nikolay Borisov165094a2016-02-08 23:29:24 +02002090 struct net *net = dev_net(pmc->interface->dev);
Stephen Hemmingercfcabdc2007-10-09 01:59:42 -07002091 in_dev = pmc->interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092#endif
2093
2094 /* filter mode change */
2095 if (pmc->sfcount[MCAST_EXCLUDE])
2096 pmc->sfmode = MCAST_EXCLUDE;
2097 else if (pmc->sfcount[MCAST_INCLUDE])
2098 pmc->sfmode = MCAST_INCLUDE;
2099#ifdef CONFIG_IP_MULTICAST
2100 /* else no filters; keep old mode for reports */
2101
Nikolay Borisov165094a2016-02-08 23:29:24 +02002102 pmc->crcount = in_dev->mr_qrv ?: net->ipv4.sysctl_igmp_qrv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 in_dev->mr_ifc_count = pmc->crcount;
Weilong Chenc71151f2013-12-23 14:37:29 +08002104 for (psf = pmc->sources; psf; psf = psf->sf_next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 psf->sf_crcount = 0;
2106 igmp_ifc_event(in_dev);
2107 } else if (sf_setstate(pmc)) {
2108 igmp_ifc_event(in_dev);
2109#endif
2110 }
2111 spin_unlock_bh(&pmc->lock);
2112 return err;
2113}
2114
2115static void ip_mc_clear_src(struct ip_mc_list *pmc)
2116{
Eric Dumazet405d3bb2019-05-22 16:51:22 -07002117 struct ip_sf_list *tomb, *sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
WANG Congecd66272017-06-12 09:52:26 -07002119 spin_lock_bh(&pmc->lock);
2120 tomb = pmc->tomb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 pmc->tomb = NULL;
WANG Congecd66272017-06-12 09:52:26 -07002122 sources = pmc->sources;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 pmc->sources = NULL;
2124 pmc->sfmode = MCAST_EXCLUDE;
Denis Lukianovde9daad2005-09-14 20:53:42 -07002125 pmc->sfcount[MCAST_INCLUDE] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 pmc->sfcount[MCAST_EXCLUDE] = 1;
WANG Congecd66272017-06-12 09:52:26 -07002127 spin_unlock_bh(&pmc->lock);
2128
Eric Dumazet405d3bb2019-05-22 16:51:22 -07002129 ip_sf_list_clear_all(tomb);
2130 ip_sf_list_clear_all(sources);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131}
2132
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002133/* Join a multicast group
2134 */
2135
2136int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Al Viro8f935bb2006-09-27 18:30:07 -07002138 __be32 addr = imr->imr_multiaddr.s_addr;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002139 struct ip_mc_socklist *iml, *i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 struct in_device *in_dev;
2141 struct inet_sock *inet = inet_sk(sk);
Daniel Lezcano877aced2008-08-13 16:15:57 -07002142 struct net *net = sock_net(sk);
David L Stevensca9b9072005-07-08 17:38:07 -07002143 int ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 int count = 0;
Eric Dumazet959d10f2015-02-17 03:19:24 -08002145 int err;
2146
2147 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Joe Perchesf97c1e02007-12-16 13:45:43 -08002149 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return -EINVAL;
2151
Daniel Lezcano877aced2008-08-13 16:15:57 -07002152 in_dev = ip_mc_find_dev(net, imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 if (!in_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 err = -ENODEV;
2156 goto done;
2157 }
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 err = -EADDRINUSE;
David L Stevensca9b9072005-07-08 17:38:07 -07002160 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002161 for_each_pmc_rtnl(inet, i) {
David L Stevensca9b9072005-07-08 17:38:07 -07002162 if (i->multi.imr_multiaddr.s_addr == addr &&
2163 i->multi.imr_ifindex == ifindex)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 count++;
2166 }
2167 err = -ENOBUFS;
Nikolay Borisov815c5272016-02-08 23:29:21 +02002168 if (count >= net->ipv4.sysctl_igmp_max_memberships)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto done;
Jianjun Konga7e9ff72008-11-03 00:26:09 -08002170 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +01002171 if (!iml)
David L Stevensca9b9072005-07-08 17:38:07 -07002172 goto done;
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 memcpy(&iml->multi, imr, sizeof(*imr));
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002175 iml->next_rcu = inet->mc_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 iml->sflist = NULL;
2177 iml->sfmode = MCAST_EXCLUDE;
Eric Dumazetcf778b02012-01-12 04:41:32 +00002178 rcu_assign_pointer(inet->mc_list, iml);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 ip_mc_inc_group(in_dev, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return err;
2183}
Eric Dumazet4bc2f182010-07-09 21:22:10 +00002184EXPORT_SYMBOL(ip_mc_join_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
2186static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
2187 struct in_device *in_dev)
2188{
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002189 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 int err;
2191
Ian Morris51456b22015-04-03 09:17:26 +01002192 if (!psf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /* any-source empty exclude case */
2194 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
2195 iml->sfmode, 0, NULL, 0);
2196 }
2197 err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002198 iml->sfmode, psf->sl_count, psf->sl_addr, 0);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00002199 RCU_INIT_POINTER(iml->sflist, NULL);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002200 /* decrease mem now to avoid the memleak warning */
2201 atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002202 kfree_rcu(psf, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return err;
2204}
2205
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002206int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 struct inet_sock *inet = inet_sk(sk);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002209 struct ip_mc_socklist *iml;
2210 struct ip_mc_socklist __rcu **imlp;
David L Stevens84b42ba2005-07-08 17:48:38 -07002211 struct in_device *in_dev;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002212 struct net *net = sock_net(sk);
Al Viro8f935bb2006-09-27 18:30:07 -07002213 __be32 group = imr->imr_multiaddr.s_addr;
David L Stevens84b42ba2005-07-08 17:48:38 -07002214 u32 ifindex;
David L Stevensacd6e002006-08-17 16:27:39 -07002215 int ret = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Eric Dumazet959d10f2015-02-17 03:19:24 -08002217 ASSERT_RTNL();
2218
Daniel Lezcano877aced2008-08-13 16:15:57 -07002219 in_dev = ip_mc_find_dev(net, imr);
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002220 if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) {
dingtianhong52ad3532014-07-02 13:50:48 +08002221 ret = -ENODEV;
2222 goto out;
2223 }
David L Stevens84b42ba2005-07-08 17:48:38 -07002224 ifindex = imr->imr_ifindex;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002225 for (imlp = &inet->mc_list;
2226 (iml = rtnl_dereference(*imlp)) != NULL;
2227 imlp = &iml->next_rcu) {
David L Stevensacd6e002006-08-17 16:27:39 -07002228 if (iml->multi.imr_multiaddr.s_addr != group)
2229 continue;
2230 if (ifindex) {
2231 if (iml->multi.imr_ifindex != ifindex)
2232 continue;
2233 } else if (imr->imr_address.s_addr && imr->imr_address.s_addr !=
2234 iml->multi.imr_address.s_addr)
2235 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
David L Stevensacd6e002006-08-17 16:27:39 -07002237 (void) ip_mc_leave_src(sk, iml, in_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002239 *imlp = iml->next_rcu;
David L Stevensacd6e002006-08-17 16:27:39 -07002240
Andrew Lunn4eba7bb2015-12-01 16:31:08 +01002241 if (in_dev)
2242 ip_mc_dec_group(in_dev, group);
Eric Dumazet959d10f2015-02-17 03:19:24 -08002243
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002244 /* decrease mem now to avoid the memleak warning */
2245 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002246 kfree_rcu(iml, rcu);
David L Stevensacd6e002006-08-17 16:27:39 -07002247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
dingtianhong52ad3532014-07-02 13:50:48 +08002249out:
Eric Dumazet959d10f2015-02-17 03:19:24 -08002250 return ret;
2251}
stephen hemminger193ba922012-10-01 12:32:34 +00002252EXPORT_SYMBOL(ip_mc_leave_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254int ip_mc_source(int add, int omode, struct sock *sk, struct
2255 ip_mreq_source *mreqs, int ifindex)
2256{
2257 int err;
2258 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002259 __be32 addr = mreqs->imr_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 struct ip_mc_socklist *pmc;
2261 struct in_device *in_dev = NULL;
2262 struct inet_sock *inet = inet_sk(sk);
2263 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002264 struct net *net = sock_net(sk);
David L Stevens8cdaaa12005-07-08 17:39:23 -07002265 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 int i, j, rv;
2267
Joe Perchesf97c1e02007-12-16 13:45:43 -08002268 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return -EINVAL;
2270
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002271 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
2274 imr.imr_address.s_addr = mreqs->imr_interface;
2275 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002276 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 if (!in_dev) {
2279 err = -ENODEV;
2280 goto done;
2281 }
2282 err = -EADDRNOTAVAIL;
2283
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002284 for_each_pmc_rtnl(inet, pmc) {
Joe Perchesf64f9e72009-11-29 16:55:45 -08002285 if ((pmc->multi.imr_multiaddr.s_addr ==
2286 imr.imr_multiaddr.s_addr) &&
2287 (pmc->multi.imr_ifindex == imr.imr_ifindex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 break;
2289 }
David L Stevens917f2f12005-07-08 17:45:16 -07002290 if (!pmc) { /* must have a prior join */
2291 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 /* if a source filter was set, must be the same mode as before */
2295 if (pmc->sflist) {
David L Stevens917f2f12005-07-08 17:45:16 -07002296 if (pmc->sfmode != omode) {
2297 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 } else if (pmc->sfmode != omode) {
2301 /* allow mode switches for empty-set filters */
2302 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002303 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 NULL, 0);
2305 pmc->sfmode = omode;
2306 }
2307
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002308 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (!add) {
2310 if (!psl)
David L Stevens917f2f12005-07-08 17:45:16 -07002311 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 rv = !0;
Weilong Chenc71151f2013-12-23 14:37:29 +08002313 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002315 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (rv == 0)
2317 break;
2318 }
2319 if (rv) /* source not found */
David L Stevens917f2f12005-07-08 17:45:16 -07002320 goto done; /* err = -EADDRNOTAVAIL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
David L Stevens8cdaaa12005-07-08 17:39:23 -07002322 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2323 if (psl->sl_count == 1 && omode == MCAST_INCLUDE) {
2324 leavegroup = 1;
2325 goto done;
2326 }
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 /* update the interface filter */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002329 ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 &mreqs->imr_sourceaddr, 1);
2331
Weilong Chenc71151f2013-12-23 14:37:29 +08002332 for (j = i+1; j < psl->sl_count; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 psl->sl_addr[j-1] = psl->sl_addr[j];
2334 psl->sl_count--;
2335 err = 0;
2336 goto done;
2337 }
2338 /* else, add a new source to the filter */
2339
Nikolay Borisov166b6b22016-02-08 23:29:22 +02002340 if (psl && psl->sl_count >= net->ipv4.sysctl_igmp_max_msf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 err = -ENOBUFS;
2342 goto done;
2343 }
2344 if (!psl || psl->sl_count == psl->sl_max) {
2345 struct ip_sf_socklist *newpsl;
2346 int count = IP_SFBLOCK;
2347
2348 if (psl)
2349 count += psl->sl_max;
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002350 newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 if (!newpsl) {
2352 err = -ENOBUFS;
2353 goto done;
2354 }
2355 newpsl->sl_max = count;
2356 newpsl->sl_count = count - IP_SFBLOCK;
2357 if (psl) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002358 for (i = 0; i < psl->sl_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 newpsl->sl_addr[i] = psl->sl_addr[i];
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002360 /* decrease mem now to avoid the memleak warning */
2361 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002362 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 }
Eric Dumazetcf778b02012-01-12 04:41:32 +00002364 rcu_assign_pointer(pmc->sflist, newpsl);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002365 psl = newpsl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367 rv = 1; /* > 0 for insert logic below if sl_count is 0 */
Weilong Chenc71151f2013-12-23 14:37:29 +08002368 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr,
Al Viro63007722006-09-27 18:31:32 -07002370 sizeof(__be32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if (rv == 0)
2372 break;
2373 }
2374 if (rv == 0) /* address already there is an error */
2375 goto done;
Weilong Chenc71151f2013-12-23 14:37:29 +08002376 for (j = psl->sl_count-1; j >= i; j--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 psl->sl_addr[j+1] = psl->sl_addr[j];
2378 psl->sl_addr[i] = mreqs->imr_sourceaddr;
2379 psl->sl_count++;
2380 err = 0;
2381 /* update the interface list */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002382 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 &mreqs->imr_sourceaddr, 1);
2384done:
David L Stevens8cdaaa12005-07-08 17:39:23 -07002385 if (leavegroup)
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002386 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return err;
2388}
2389
2390int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2391{
David L Stevens9951f032005-07-08 17:47:28 -07002392 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002394 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 struct ip_mc_socklist *pmc;
2396 struct in_device *in_dev;
2397 struct inet_sock *inet = inet_sk(sk);
2398 struct ip_sf_socklist *newpsl, *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002399 struct net *net = sock_net(sk);
David L Stevens9951f032005-07-08 17:47:28 -07002400 int leavegroup = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Joe Perchesf97c1e02007-12-16 13:45:43 -08002402 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return -EINVAL;
2404 if (msf->imsf_fmode != MCAST_INCLUDE &&
2405 msf->imsf_fmode != MCAST_EXCLUDE)
2406 return -EINVAL;
2407
Marcelo Ricardo Leitner54ff9ef2015-03-18 14:50:43 -03002408 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2411 imr.imr_address.s_addr = msf->imsf_interface;
2412 imr.imr_ifindex = ifindex;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002413 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 if (!in_dev) {
2416 err = -ENODEV;
2417 goto done;
2418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
David L Stevens9951f032005-07-08 17:47:28 -07002420 /* special case - (INCLUDE, empty) == LEAVE_GROUP */
2421 if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) {
2422 leavegroup = 1;
2423 goto done;
2424 }
2425
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002426 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2428 pmc->multi.imr_ifindex == imr.imr_ifindex)
2429 break;
2430 }
David L Stevens917f2f12005-07-08 17:45:16 -07002431 if (!pmc) { /* must have a prior join */
2432 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 goto done;
David L Stevens917f2f12005-07-08 17:45:16 -07002434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (msf->imsf_numsrc) {
Kris Katterjohn8b3a7002006-01-11 15:56:43 -08002436 newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc),
2437 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 if (!newpsl) {
2439 err = -ENOBUFS;
2440 goto done;
2441 }
2442 newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
2443 memcpy(newpsl->sl_addr, msf->imsf_slist,
2444 msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
2445 err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2446 msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
2447 if (err) {
2448 sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
2449 goto done;
2450 }
Yan Zheng8713dbf2005-10-28 08:02:08 +08002451 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 newpsl = NULL;
Yan Zheng8713dbf2005-10-28 08:02:08 +08002453 (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
2454 msf->imsf_fmode, 0, NULL, 0);
2455 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002456 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if (psl) {
2458 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2459 psl->sl_count, psl->sl_addr, 0);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002460 /* decrease mem now to avoid the memleak warning */
2461 atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc);
Lai Jiangshan7519cce2011-03-18 11:44:46 +08002462 kfree_rcu(psl, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 } else
2464 (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
2465 0, NULL, 0);
Eric Dumazetcf778b02012-01-12 04:41:32 +00002466 rcu_assign_pointer(pmc->sflist, newpsl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 pmc->sfmode = msf->imsf_fmode;
David L Stevens917f2f12005-07-08 17:45:16 -07002468 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469done:
David L Stevens9951f032005-07-08 17:47:28 -07002470 if (leavegroup)
2471 err = ip_mc_leave_group(sk, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return err;
2473}
2474
2475int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
2476 struct ip_msfilter __user *optval, int __user *optlen)
2477{
2478 int err, len, count, copycount;
2479 struct ip_mreqn imr;
Al Viro63007722006-09-27 18:31:32 -07002480 __be32 addr = msf->imsf_multiaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 struct ip_mc_socklist *pmc;
2482 struct in_device *in_dev;
2483 struct inet_sock *inet = inet_sk(sk);
2484 struct ip_sf_socklist *psl;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002485 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486
WANG Cong87e9f032015-11-03 15:41:16 -08002487 ASSERT_RTNL();
2488
Joe Perchesf97c1e02007-12-16 13:45:43 -08002489 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return -EINVAL;
2491
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2493 imr.imr_address.s_addr = msf->imsf_interface;
2494 imr.imr_ifindex = 0;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002495 in_dev = ip_mc_find_dev(net, &imr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 if (!in_dev) {
2498 err = -ENODEV;
2499 goto done;
2500 }
2501 err = -EADDRNOTAVAIL;
2502
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002503 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
2505 pmc->multi.imr_ifindex == imr.imr_ifindex)
2506 break;
2507 }
2508 if (!pmc) /* must have a prior join */
2509 goto done;
2510 msf->imsf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002511 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if (!psl) {
2513 len = 0;
2514 count = 0;
2515 } else {
2516 count = psl->sl_count;
2517 }
2518 copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
2519 len = copycount * sizeof(psl->sl_addr[0]);
2520 msf->imsf_numsrc = count;
2521 if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
2522 copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) {
2523 return -EFAULT;
2524 }
2525 if (len &&
2526 copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len))
2527 return -EFAULT;
2528 return 0;
2529done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return err;
2531}
2532
2533int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
2534 struct group_filter __user *optval, int __user *optlen)
2535{
2536 int err, i, count, copycount;
2537 struct sockaddr_in *psin;
Al Viro63007722006-09-27 18:31:32 -07002538 __be32 addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 struct ip_mc_socklist *pmc;
2540 struct inet_sock *inet = inet_sk(sk);
2541 struct ip_sf_socklist *psl;
2542
WANG Cong87e9f032015-11-03 15:41:16 -08002543 ASSERT_RTNL();
2544
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 psin = (struct sockaddr_in *)&gsf->gf_group;
2546 if (psin->sin_family != AF_INET)
2547 return -EINVAL;
2548 addr = psin->sin_addr.s_addr;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002549 if (!ipv4_is_multicast(addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 return -EINVAL;
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 err = -EADDRNOTAVAIL;
2553
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002554 for_each_pmc_rtnl(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (pmc->multi.imr_multiaddr.s_addr == addr &&
2556 pmc->multi.imr_ifindex == gsf->gf_interface)
2557 break;
2558 }
2559 if (!pmc) /* must have a prior join */
2560 goto done;
2561 gsf->gf_fmode = pmc->sfmode;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002562 psl = rtnl_dereference(pmc->sflist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 count = psl ? psl->sl_count : 0;
2564 copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
2565 gsf->gf_numsrc = count;
2566 if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
2567 copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) {
2568 return -EFAULT;
2569 }
Weilong Chenc71151f2013-12-23 14:37:29 +08002570 for (i = 0; i < copycount; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 struct sockaddr_storage ss;
2572
2573 psin = (struct sockaddr_in *)&ss;
2574 memset(&ss, 0, sizeof(ss));
2575 psin->sin_family = AF_INET;
2576 psin->sin_addr.s_addr = psl->sl_addr[i];
2577 if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss)))
2578 return -EFAULT;
2579 }
2580 return 0;
2581done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 return err;
2583}
2584
2585/*
2586 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2587 */
Al Viroc0cda062006-09-27 18:31:10 -07002588int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 struct inet_sock *inet = inet_sk(sk);
2591 struct ip_mc_socklist *pmc;
2592 struct ip_sf_socklist *psl;
2593 int i;
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002594 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002596 ret = 1;
Joe Perchesf97c1e02007-12-16 13:45:43 -08002597 if (!ipv4_is_multicast(loc_addr))
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002598 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002600 rcu_read_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002601 for_each_pmc_rcu(inet, pmc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2603 pmc->multi.imr_ifindex == dif)
2604 break;
2605 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002606 ret = inet->mc_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 if (!pmc)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002608 goto unlock;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002609 psl = rcu_dereference(pmc->sflist);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002610 ret = (pmc->sfmode == MCAST_EXCLUDE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if (!psl)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002612 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Weilong Chenc71151f2013-12-23 14:37:29 +08002614 for (i = 0; i < psl->sl_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (psl->sl_addr[i] == rmt_addr)
2616 break;
2617 }
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002618 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002620 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002622 goto unlock;
2623 ret = 1;
2624unlock:
2625 rcu_read_unlock();
2626out:
2627 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
2630/*
2631 * A socket is closing.
2632 */
2633
2634void ip_mc_drop_socket(struct sock *sk)
2635{
2636 struct inet_sock *inet = inet_sk(sk);
2637 struct ip_mc_socklist *iml;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002638 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Ian Morris51456b22015-04-03 09:17:26 +01002640 if (!inet->mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return;
2642
2643 rtnl_lock();
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002644 while ((iml = rtnl_dereference(inet->mc_list)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 struct in_device *in_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002647 inet->mc_list = iml->next_rcu;
Daniel Lezcano877aced2008-08-13 16:15:57 -07002648 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
Michal Ruzickabb699cbc2006-08-15 00:20:17 -07002649 (void) ip_mc_leave_src(sk, iml, in_dev);
Ian Morris00db4122015-04-03 09:17:27 +01002650 if (in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
Flavio Leitnerc85bb412010-02-02 07:32:29 -08002652 /* decrease mem now to avoid the memleak warning */
2653 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
Lai Jiangshan10d50e72011-03-18 11:45:08 +08002654 kfree_rcu(iml, rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
2656 rtnl_unlock();
2657}
2658
David S. Millerdbdd9a52011-03-10 16:34:38 -08002659/* called with rcu_read_lock() */
Alexander Duyck2094acb2015-09-28 11:10:31 -07002660int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 struct ip_mc_list *im;
Eric Dumazete9897072013-06-07 08:48:57 -07002663 struct ip_mc_list __rcu **mc_hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 struct ip_sf_list *psf;
2665 int rv = 0;
2666
Eric Dumazete9897072013-06-07 08:48:57 -07002667 mc_hash = rcu_dereference(in_dev->mc_hash);
2668 if (mc_hash) {
Eric Dumazetc70eba72013-06-12 14:11:16 -07002669 u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG);
Eric Dumazete9897072013-06-07 08:48:57 -07002670
2671 for (im = rcu_dereference(mc_hash[hash]);
2672 im != NULL;
2673 im = rcu_dereference(im->next_hash)) {
2674 if (im->multiaddr == mc_addr)
2675 break;
2676 }
2677 } else {
2678 for_each_pmc_rcu(in_dev, im) {
2679 if (im->multiaddr == mc_addr)
2680 break;
2681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683 if (im && proto == IPPROTO_IGMP) {
2684 rv = 1;
2685 } else if (im) {
2686 if (src_addr) {
Weilong Chenc71151f2013-12-23 14:37:29 +08002687 for (psf = im->sources; psf; psf = psf->sf_next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 if (psf->sf_inaddr == src_addr)
2689 break;
2690 }
2691 if (psf)
2692 rv = psf->sf_count[MCAST_INCLUDE] ||
2693 psf->sf_count[MCAST_EXCLUDE] !=
2694 im->sfcount[MCAST_EXCLUDE];
2695 else
2696 rv = im->sfcount[MCAST_EXCLUDE] != 0;
2697 } else
2698 rv = 1; /* unspecified source; tentatively allow */
2699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return rv;
2701}
2702
2703#if defined(CONFIG_PROC_FS)
2704struct igmp_mc_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002705 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 struct net_device *dev;
2707 struct in_device *in_dev;
2708};
2709
2710#define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private)
2711
2712static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2713{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002714 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 struct ip_mc_list *im = NULL;
2716 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2717
Pavel Emelianov7562f872007-05-03 15:13:45 -07002718 state->in_dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002719 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct in_device *in_dev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002721
2722 in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 if (!in_dev)
2724 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002725 im = rcu_dereference(in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (im) {
2727 state->in_dev = in_dev;
2728 break;
2729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 }
2731 return im;
2732}
2733
2734static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2735{
2736 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet6baff152009-11-11 17:48:52 +00002737
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002738 im = rcu_dereference(im->next_rcu);
2739 while (!im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002740 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 if (!state->dev) {
2742 state->in_dev = NULL;
2743 break;
2744 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002745 state->in_dev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 if (!state->in_dev)
2747 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002748 im = rcu_dereference(state->in_dev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
2750 return im;
2751}
2752
2753static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2754{
2755 struct ip_mc_list *im = igmp_mc_get_first(seq);
2756 if (im)
2757 while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2758 --pos;
2759 return pos ? NULL : im;
2760}
2761
2762static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002763 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
stephen hemminger61fbab72009-11-10 07:54:55 +00002765 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2767}
2768
2769static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2770{
2771 struct ip_mc_list *im;
2772 if (v == SEQ_START_TOKEN)
2773 im = igmp_mc_get_first(seq);
2774 else
2775 im = igmp_mc_get_next(seq, v);
2776 ++*pos;
2777 return im;
2778}
2779
2780static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002781 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782{
2783 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002784
2785 state->in_dev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002787 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
2790static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2791{
2792 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002793 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n");
2795 else {
2796 struct ip_mc_list *im = (struct ip_mc_list *)v;
2797 struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2798 char *querier;
Eric Dumazeta399a802012-08-08 21:13:53 +00002799 long delta;
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801#ifdef CONFIG_IP_MULTICAST
2802 querier = IGMP_V1_SEEN(state->in_dev) ? "V1" :
2803 IGMP_V2_SEEN(state->in_dev) ? "V2" :
2804 "V3";
2805#else
2806 querier = "NONE";
2807#endif
2808
Andreea-Cristina Bernate6b68882014-08-17 15:49:41 +03002809 if (rcu_access_pointer(state->in_dev->mc_list) == im) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 seq_printf(seq, "%d\t%-10s: %5d %7s\n",
Rami Rosenb8bae412008-10-07 15:34:37 -07002811 state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
2813
Eric Dumazeta399a802012-08-08 21:13:53 +00002814 delta = im->timer.expires - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 seq_printf(seq,
Alexey Dobriyan338fcf92006-06-05 21:04:39 -07002816 "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 im->multiaddr, im->users,
Eric Dumazeta399a802012-08-08 21:13:53 +00002818 im->tm_running,
2819 im->tm_running ? jiffies_delta_to_clock_t(delta) : 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 im->reporter);
2821 }
2822 return 0;
2823}
2824
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002825static const struct seq_operations igmp_mc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 .start = igmp_mc_seq_start,
2827 .next = igmp_mc_seq_next,
2828 .stop = igmp_mc_seq_stop,
2829 .show = igmp_mc_seq_show,
2830};
2831
2832static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2833{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002834 return seq_open_net(inode, file, &igmp_mc_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002835 sizeof(struct igmp_mc_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Arjan van de Ven9a321442007-02-12 00:55:35 -08002838static const struct file_operations igmp_mc_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 .owner = THIS_MODULE,
2840 .open = igmp_mc_seq_open,
2841 .read = seq_read,
2842 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002843 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844};
2845
2846struct igmp_mcf_iter_state {
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002847 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct net_device *dev;
2849 struct in_device *idev;
2850 struct ip_mc_list *im;
2851};
2852
2853#define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private)
2854
2855static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2856{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002857 struct net *net = seq_file_net(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct ip_sf_list *psf = NULL;
2859 struct ip_mc_list *im = NULL;
2860 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2861
Pavel Emelianov7562f872007-05-03 15:13:45 -07002862 state->idev = NULL;
2863 state->im = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002864 for_each_netdev_rcu(net, state->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 struct in_device *idev;
Eric Dumazet6baff152009-11-11 17:48:52 +00002866 idev = __in_dev_get_rcu(state->dev);
Ian Morris51456b22015-04-03 09:17:26 +01002867 if (unlikely(!idev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002869 im = rcu_dereference(idev->mc_list);
Ian Morris00db4122015-04-03 09:17:27 +01002870 if (likely(im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 spin_lock_bh(&im->lock);
2872 psf = im->sources;
Ian Morris00db4122015-04-03 09:17:27 +01002873 if (likely(psf)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 state->im = im;
2875 state->idev = idev;
2876 break;
2877 }
2878 spin_unlock_bh(&im->lock);
2879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 }
2881 return psf;
2882}
2883
2884static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2885{
2886 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2887
2888 psf = psf->sf_next;
2889 while (!psf) {
2890 spin_unlock_bh(&state->im->lock);
2891 state->im = state->im->next;
2892 while (!state->im) {
Eric Dumazet6baff152009-11-11 17:48:52 +00002893 state->dev = next_net_device_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (!state->dev) {
2895 state->idev = NULL;
2896 goto out;
2897 }
Eric Dumazet6baff152009-11-11 17:48:52 +00002898 state->idev = __in_dev_get_rcu(state->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 if (!state->idev)
2900 continue;
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002901 state->im = rcu_dereference(state->idev->mc_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
2903 if (!state->im)
2904 break;
2905 spin_lock_bh(&state->im->lock);
2906 psf = state->im->sources;
2907 }
2908out:
2909 return psf;
2910}
2911
2912static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2913{
2914 struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2915 if (psf)
2916 while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2917 --pos;
2918 return pos ? NULL : psf;
2919}
2920
2921static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
stephen hemminger61fbab72009-11-10 07:54:55 +00002922 __acquires(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
stephen hemminger61fbab72009-11-10 07:54:55 +00002924 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2926}
2927
2928static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2929{
2930 struct ip_sf_list *psf;
2931 if (v == SEQ_START_TOKEN)
2932 psf = igmp_mcf_get_first(seq);
2933 else
2934 psf = igmp_mcf_get_next(seq, v);
2935 ++*pos;
2936 return psf;
2937}
2938
2939static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
stephen hemminger61fbab72009-11-10 07:54:55 +00002940 __releases(rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
2942 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
Ian Morris00db4122015-04-03 09:17:27 +01002943 if (likely(state->im)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 spin_unlock_bh(&state->im->lock);
2945 state->im = NULL;
2946 }
Eric Dumazet1d7138d2010-11-12 05:46:50 +00002947 state->idev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 state->dev = NULL;
stephen hemminger61fbab72009-11-10 07:54:55 +00002949 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
2952static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2953{
2954 struct ip_sf_list *psf = (struct ip_sf_list *)v;
2955 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2956
2957 if (v == SEQ_START_TOKEN) {
Joe Perches1744bea2014-11-04 15:37:03 -08002958 seq_puts(seq, "Idx Device MCA SRC INC EXC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 } else {
2960 seq_printf(seq,
2961 "%3d %6.6s 0x%08x "
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002962 "0x%08x %6lu %6lu\n",
2963 state->dev->ifindex, state->dev->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 ntohl(state->im->multiaddr),
2965 ntohl(psf->sf_inaddr),
2966 psf->sf_count[MCAST_INCLUDE],
2967 psf->sf_count[MCAST_EXCLUDE]);
2968 }
2969 return 0;
2970}
2971
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002972static const struct seq_operations igmp_mcf_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 .start = igmp_mcf_seq_start,
2974 .next = igmp_mcf_seq_next,
2975 .stop = igmp_mcf_seq_stop,
2976 .show = igmp_mcf_seq_show,
2977};
2978
2979static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2980{
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002981 return seq_open_net(inode, file, &igmp_mcf_seq_ops,
Pavel Emelyanovcf7732e2007-10-10 02:29:29 -07002982 sizeof(struct igmp_mcf_iter_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
Arjan van de Ven9a321442007-02-12 00:55:35 -08002985static const struct file_operations igmp_mcf_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 .owner = THIS_MODULE,
2987 .open = igmp_mcf_seq_open,
2988 .read = seq_read,
2989 .llseek = seq_lseek,
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002990 .release = seq_release_net,
2991};
2992
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002993static int __net_init igmp_net_init(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002994{
2995 struct proc_dir_entry *pde;
Madhu Challa93a714d2015-02-25 09:58:35 -08002996 int err;
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002997
Gao fengd4beaa62013-02-18 01:34:54 +00002998 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08002999 if (!pde)
3000 goto out_igmp;
Gao fengd4beaa62013-02-18 01:34:54 +00003001 pde = proc_create("mcfilter", S_IRUGO, net->proc_net,
3002 &igmp_mcf_seq_fops);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003003 if (!pde)
3004 goto out_mcfilter;
Madhu Challa93a714d2015-02-25 09:58:35 -08003005 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
3006 SOCK_DGRAM, 0, net);
3007 if (err < 0) {
3008 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
3009 err);
3010 goto out_sock;
3011 }
3012
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003013 return 0;
3014
Madhu Challa93a714d2015-02-25 09:58:35 -08003015out_sock:
3016 remove_proc_entry("mcfilter", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003017out_mcfilter:
Gao fengece31ff2013-02-18 01:34:56 +00003018 remove_proc_entry("igmp", net->proc_net);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003019out_igmp:
3020 return -ENOMEM;
3021}
3022
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00003023static void __net_exit igmp_net_exit(struct net *net)
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003024{
Gao fengece31ff2013-02-18 01:34:56 +00003025 remove_proc_entry("mcfilter", net->proc_net);
3026 remove_proc_entry("igmp", net->proc_net);
Madhu Challa93a714d2015-02-25 09:58:35 -08003027 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
Alexey Dobriyan7091e722008-12-25 16:42:51 -08003028}
3029
3030static struct pernet_operations igmp_net_ops = {
3031 .init = igmp_net_init,
3032 .exit = igmp_net_exit,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033};
WANG Cong72c1d3b2014-01-10 16:09:45 -08003034#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003036static int igmp_netdev_event(struct notifier_block *this,
3037 unsigned long event, void *ptr)
3038{
3039 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3040 struct in_device *in_dev;
3041
3042 switch (event) {
3043 case NETDEV_RESEND_IGMP:
3044 in_dev = __in_dev_get_rtnl(dev);
3045 if (in_dev)
3046 ip_mc_rejoin_groups(in_dev);
3047 break;
3048 default:
3049 break;
3050 }
3051 return NOTIFY_DONE;
3052}
3053
3054static struct notifier_block igmp_notifier = {
3055 .notifier_call = igmp_netdev_event,
3056};
3057
WANG Cong72c1d3b2014-01-10 16:09:45 -08003058int __init igmp_mc_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
WANG Cong72c1d3b2014-01-10 16:09:45 -08003060#if defined(CONFIG_PROC_FS)
Jiri Pirko4aa5dee2013-07-20 12:13:53 +02003061 int err;
3062
3063 err = register_pernet_subsys(&igmp_net_ops);
3064 if (err)
3065 return err;
3066 err = register_netdevice_notifier(&igmp_notifier);
3067 if (err)
3068 goto reg_notif_fail;
3069 return 0;
3070
3071reg_notif_fail:
3072 unregister_pernet_subsys(&igmp_net_ops);
3073 return err;
WANG Cong72c1d3b2014-01-10 16:09:45 -08003074#else
3075 return register_netdevice_notifier(&igmp_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076#endif
WANG Cong72c1d3b2014-01-10 16:09:45 -08003077}